42#include "Warnings.hpp"
55static 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);
142 <<
"Barrier " << mNumBarriers <<
" \"" << callerId <<
"\"." << std::endl
148 <<
"Barrier " << mNumBarriers++ <<
" \"" << callerId <<
"\"." << std::endl
156 Barrier(
"PetscTools::RoundRobin");
158 for (
unsigned turn = 0; turn < my_rank; turn++)
160 Barrier(
"PetscTools::RoundRobin");
167 for (
unsigned turn =
GetMyRank(); turn < num_procs; turn++)
169 Barrier(
"PetscTools::RoundRobin");
182 return PETSC_COMM_SELF;
186 return PETSC_COMM_WORLD;
194 unsigned anyones_flag_is_true = my_flag;
197 MPI_Allreduce(&my_flag, &anyones_flag_is_true, 1, MPI_UNSIGNED, MPI_MAX, PETSC_COMM_WORLD);
199 return (anyones_flag_is_true == 1);
212 EXCEPTION(
"Another process threw an exception; bailing out.");
222 VecCreate(PETSC_COMM_WORLD, &ret);
223 VecSetSizes(ret, localSize, size);
224 VecSetFromOptions(ret);
226 if (ignoreOffProcEntries)
228#if (PETSC_VERSION_MAJOR == 3)
229 VecSetOption(ret, VEC_IGNORE_OFF_PROC_ENTRIES, PETSC_TRUE);
231 VecSetOption(ret, VEC_IGNORE_OFF_PROC_ENTRIES);
240 assert(data.size() > 0);
244 VecGetArray(ret, &p_ret);
246 VecGetOwnershipRange(ret, &lo, &hi);
248 for (
int global_index = lo; global_index < hi; global_index++)
250 int local_index = global_index - lo;
251 p_ret[local_index] = data[global_index];
253 VecRestoreArray(ret, &p_ret);
263#if (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 2)
273 unsigned rowPreallocation,
276 bool ignoreOffProcEntries,
277 bool newAllocationError)
280 assert(numColumns > 0);
281 if ((
int)rowPreallocation > numColumns)
283 WARNING(
"Preallocation failure: requested number of nonzeros per row greater than number of columns");
284 rowPreallocation = numColumns;
287#if (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 2)
288 MatCreate(PETSC_COMM_WORLD, numLocalRows, numLocalColumns, numRows, numColumns, &rMat);
290 MatCreate(PETSC_COMM_WORLD, &rMat);
291 MatSetSizes(rMat, numLocalRows, numLocalColumns, numRows, numColumns);
296 MatSetType(rMat, MATSEQAIJ);
297 if (rowPreallocation > 0)
304 MatSetType(rMat, MATMPIAIJ);
305 if (rowPreallocation > 0)
311 MatSetFromOptions(rMat);
313 if (ignoreOffProcEntries)
315 if (rowPreallocation == 0)
318 WARNING(
"Ignoring MAT_IGNORE_OFF_PROC_ENTRIES flag because we might set non-zeroes later");
322#if (PETSC_VERSION_MAJOR == 3)
323 MatSetOption(rMat, MAT_IGNORE_OFF_PROC_ENTRIES, PETSC_TRUE);
325 MatSetOption(rMat, MAT_IGNORE_OFF_PROC_ENTRIES);
329#if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 3)
330 if (newAllocationError ==
false)
332 MatSetOption(rMat, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE);
340#if (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 2)
341 PetscViewerFileType type = PETSC_FILE_CREATE;
343 PetscFileMode type = FILE_MODE_WRITE;
346 PetscViewerBinaryOpen(PETSC_COMM_WORLD, rOutputFileFullPath.c_str(), type, &view);
354#if (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 2)
355 PetscViewerFileType type = PETSC_FILE_CREATE;
357 PetscFileMode type = FILE_MODE_WRITE;
360 PetscViewerBinaryOpen(PETSC_COMM_WORLD, rOutputFileFullPath.c_str(), type, &view);
375#if (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 2)
376 PetscViewerFileType type = PETSC_FILE_RDONLY;
378 PetscFileMode type = FILE_MODE_READ;
381 PetscViewerBinaryOpen(PETSC_COMM_WORLD, rOutputFileFullPath.c_str(),
384#if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 2)
385 MatCreate(PETSC_COMM_WORLD, &rMat);
386 MatSetType(rMat, MATMPIAIJ);
389 MatLoad(view, MATMPIAIJ, &rMat);
394 if (rParallelLayout !=
nullptr)
402 VecGetSize(rParallelLayout, &num_rows);
403 VecGetLocalSize(rParallelLayout, &num_local_rows);
409#if PETSC_VERSION_GE(3, 11, 2)
410 PetscTools::ChasteMatCopy(rMat, temp_mat, DIFFERENT_NONZERO_PATTERN);
412 MatCopy(rMat, temp_mat, DIFFERENT_NONZERO_PATTERN);
423#if (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 2)
424 PetscViewerFileType type = PETSC_FILE_RDONLY;
426 PetscFileMode type = FILE_MODE_READ;
429 PetscViewerBinaryOpen(PETSC_COMM_WORLD, rOutputFileFullPath.c_str(),
431 if (rParallelLayout ==
nullptr)
433#if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 2)
434 VecCreate(PETSC_COMM_WORLD, &rVec);
435 VecSetType(rVec, VECMPI);
438 VecLoad(view, VECMPI, &rVec);
443 VecDuplicate(rParallelLayout, &rVec);
444#if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 2)
447 VecLoadIntoVector(view, rVec);
455#ifdef __INTEL_COMPILER
463 MatPartitioning part;
464 MatPartitioningCreate(PETSC_COMM_WORLD, &part);
468 PetscPushErrorHandler(PetscIgnoreErrorHandler,
nullptr);
470#if (PETSC_VERSION_MAJOR == 2 || (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR < 2))
471 PetscErrorCode parmetis_installed_error = MatPartitioningSetType(part, MAT_PARTITIONING_PARMETIS);
473 PetscErrorCode parmetis_installed_error = MatPartitioningSetType(part, MATPARTITIONINGPARMETIS);
477 PetscPopErrorHandler();
486 if (parmetis_installed_error == 0)
488 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.");
493 return (parmetis_installed_error == 0);
496#if PETSC_VERSION_GE(3, 11, 2)
497PetscErrorCode PetscTools::ChasteMatCopy(
Mat A,
Mat B, MatStructure str)
500 PetscInt i, rstart = 0, rend = 0, nz;
502 const PetscScalar* vwork;
505 MatAssembled(B, &assembled);
506 if (assembled == PETSC_TRUE)
508 ierr = MatZeroEntries(B);
512 ierr = MatGetOwnershipRange(A, &rstart, &rend);
514 for (i = rstart; i < rend; i++)
516 ierr = MatGetRow(A, i, &nz, &cwork, &vwork);
518 ierr = MatSetValues(B, 1, &i, nz, cwork, vwork, INSERT_VALUES);
520 ierr = MatRestoreRow(A, i, &nz, &cwork, &vwork);
524 ierr = MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY);
526 ierr = MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY);
528 PetscFunctionReturn(0);
#define EXCEPTION(message)