38 #include "Warnings.hpp"
55 static unsigned mNumBarriers = 0u;
61 PetscInitialized(&is_there);
67 MPI_Comm_size(PETSC_COMM_WORLD, &num_procs);
71 MPI_Comm_rank(PETSC_COMM_WORLD, &my_rank);
139 #ifdef DEBUG_BARRIERS
141 std::cout <<
"DEBUG: proc " <<
PetscTools::GetMyRank() <<
": Before " <<
"Barrier " << mNumBarriers <<
" \""<< callerId <<
"\"." << std::endl << std::flush;
143 PetscBarrier(PETSC_NULL);
144 #ifdef DEBUG_BARRIERS
145 std::cout <<
"DEBUG: proc " <<
PetscTools::GetMyRank() <<
": Post " <<
"Barrier " << mNumBarriers++ <<
" \""<< callerId <<
"\"." << std::endl << std::flush;
152 Barrier(
"PetscTools::RoundRobin");
154 for (
unsigned turn=0; turn<my_rank; turn++)
156 Barrier(
"PetscTools::RoundRobin");
163 for (
unsigned turn=
GetMyRank(); turn<num_procs; turn++)
165 Barrier(
"PetscTools::RoundRobin");
178 return PETSC_COMM_SELF;
182 return PETSC_COMM_WORLD;
190 unsigned anyones_flag_is_true = my_flag;
193 MPI_Allreduce(&my_flag, &anyones_flag_is_true, 1, MPI_UNSIGNED, MPI_MAX, PETSC_COMM_WORLD);
195 return (anyones_flag_is_true == 1);
208 EXCEPTION(
"Another process threw an exception; bailing out.");
218 VecCreate(PETSC_COMM_WORLD, &ret);
219 VecSetSizes(ret, localSize, size);
220 VecSetFromOptions(ret);
222 if (ignoreOffProcEntries)
224 #if (PETSC_VERSION_MAJOR == 3) //PETSc 3.x.x
225 VecSetOption(ret, VEC_IGNORE_OFF_PROC_ENTRIES, PETSC_TRUE);
227 VecSetOption(ret, VEC_IGNORE_OFF_PROC_ENTRIES);
236 assert(data.size() > 0);
240 VecGetArray(ret, &p_ret);
242 VecGetOwnershipRange(ret, &lo, &hi);
244 for (
int global_index=lo; global_index<hi; global_index++)
246 int local_index = global_index - lo;
247 p_ret[local_index] = data[global_index];
249 VecRestoreArray(ret, &p_ret);
259 #if (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 2) //PETSc 2.2
269 unsigned rowPreallocation,
272 bool ignoreOffProcEntries,
273 bool newAllocationError)
276 assert(numColumns > 0);
277 if ((
int) rowPreallocation>numColumns)
279 WARNING(
"Preallocation failure: requested number of nonzeros per row greater than number of columns");
280 rowPreallocation = numColumns;
283 #if (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 2) //PETSc 2.2
284 MatCreate(PETSC_COMM_WORLD,numLocalRows,numLocalColumns,numRows,numColumns,&rMat);
286 MatCreate(PETSC_COMM_WORLD,&rMat);
287 MatSetSizes(rMat,numLocalRows,numLocalColumns,numRows,numColumns);
292 MatSetType(rMat, MATSEQAIJ);
293 if (rowPreallocation > 0)
295 MatSeqAIJSetPreallocation(rMat, rowPreallocation, PETSC_NULL);
300 MatSetType(rMat, MATMPIAIJ);
301 if (rowPreallocation > 0)
303 MatMPIAIJSetPreallocation(rMat, rowPreallocation, PETSC_NULL, rowPreallocation, PETSC_NULL);
307 MatSetFromOptions(rMat);
309 if (ignoreOffProcEntries)
311 if (rowPreallocation == 0)
314 WARNING(
"Ignoring MAT_IGNORE_OFF_PROC_ENTRIES flag because we might set non-zeroes later");
318 #if (PETSC_VERSION_MAJOR == 3) //PETSc 3.x.x
319 MatSetOption(rMat, MAT_IGNORE_OFF_PROC_ENTRIES, PETSC_TRUE);
321 MatSetOption(rMat, MAT_IGNORE_OFF_PROC_ENTRIES);
325 #if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 3) //PETSc 3.3 or later
326 if (newAllocationError ==
false)
328 MatSetOption(rMat, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE);
336 #if (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 2) //PETSc 2.2
337 PetscViewerFileType type = PETSC_FILE_CREATE;
339 PetscFileMode type = FILE_MODE_WRITE;
342 PetscViewerBinaryOpen(PETSC_COMM_WORLD, rOutputFileFullPath.c_str(), type, &view);
350 #if (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 2) //PETSc 2.2
351 PetscViewerFileType type = PETSC_FILE_CREATE;
353 PetscFileMode type = FILE_MODE_WRITE;
356 PetscViewerBinaryOpen(PETSC_COMM_WORLD, rOutputFileFullPath.c_str(), type, &view);
371 #if (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 2) //PETSc 2.2
372 PetscViewerFileType type = PETSC_FILE_RDONLY;
374 PetscFileMode type = FILE_MODE_READ;
377 PetscViewerBinaryOpen(PETSC_COMM_WORLD, rOutputFileFullPath.c_str(),
380 #if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 2) //PETSc 3.2 or later
381 MatCreate(PETSC_COMM_WORLD,&rMat);
382 MatSetType(rMat,MATMPIAIJ);
385 MatLoad(view, MATMPIAIJ, &rMat);
390 if (rParallelLayout != NULL)
398 VecGetSize(rParallelLayout, &num_rows);
399 VecGetLocalSize(rParallelLayout, &num_local_rows);
405 MatCopy(rMat, temp_mat, DIFFERENT_NONZERO_PATTERN);
415 #if (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 2) //PETSc 2.2
416 PetscViewerFileType type = PETSC_FILE_RDONLY;
418 PetscFileMode type = FILE_MODE_READ;
421 PetscViewerBinaryOpen(PETSC_COMM_WORLD, rOutputFileFullPath.c_str(),
423 if (rParallelLayout == NULL)
425 #if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 2) //PETSc 3.2 or later
426 VecCreate(PETSC_COMM_WORLD,&rVec);
427 VecSetType(rVec,VECMPI);
430 VecLoad(view, VECMPI, &rVec);
435 VecDuplicate(rParallelLayout, &rVec);
436 #if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 2) //PETSc 3.2 or later
439 VecLoadIntoVector(view, rVec);
447 #ifdef __INTEL_COMPILER
455 MatPartitioning part;
456 MatPartitioningCreate(PETSC_COMM_WORLD, &part);
461 PetscPushErrorHandler(PetscIgnoreErrorHandler, NULL);
463 #if (PETSC_VERSION_MAJOR == 2 || (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR < 2))
464 PetscErrorCode parmetis_installed_error = MatPartitioningSetType(part,MAT_PARTITIONING_PARMETIS);
466 PetscErrorCode parmetis_installed_error = MatPartitioningSetType(part,MATPARTITIONINGPARMETIS);
470 PetscPopErrorHandler();
479 if (parmetis_installed_error == 0)
481 WARN_ONCE_ONLY(
"The PETSc/parMETIS interface is correctly installed but does not yet work in Windows so matrix-based partitioning will be turned off.");
486 return (parmetis_installed_error == 0);
#define EXCEPTION(message)