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);
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);
368#if PETSC_VERSION_GE(3, 11, 2)
371 PetscFileMode type = FILE_MODE_READ;
372 PetscViewerBinaryOpen(PETSC_COMM_WORLD, rOutputFileFullPath.c_str(),
375 if (rParallelLayout !=
nullptr)
379 VecGetSize(rParallelLayout, &num_rows);
380 VecGetLocalSize(rParallelLayout, &num_local_rows);
388 MatCreate(PETSC_COMM_WORLD, &rMat);
389 MatSetType(rMat, MATMPIAIJ);
405#if (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 2)
406 PetscViewerFileType type = PETSC_FILE_RDONLY;
408 PetscFileMode type = FILE_MODE_READ;
411 PetscViewerBinaryOpen(PETSC_COMM_WORLD, rOutputFileFullPath.c_str(),
414#if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 2)
415 MatCreate(PETSC_COMM_WORLD, &rMat);
416 MatSetType(rMat, MATMPIAIJ);
419 MatLoad(view, MATMPIAIJ, &rMat);
424 if (rParallelLayout !=
nullptr)
432 VecGetSize(rParallelLayout, &num_rows);
433 VecGetLocalSize(rParallelLayout, &num_local_rows);
439 MatCopy(rMat, temp_mat, DIFFERENT_NONZERO_PATTERN);
450#if (PETSC_VERSION_MAJOR == 2 && PETSC_VERSION_MINOR == 2)
451 PetscViewerFileType type = PETSC_FILE_RDONLY;
453 PetscFileMode type = FILE_MODE_READ;
456 PetscViewerBinaryOpen(PETSC_COMM_WORLD, rOutputFileFullPath.c_str(),
458 if (rParallelLayout ==
nullptr)
460#if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 2)
461 VecCreate(PETSC_COMM_WORLD, &rVec);
462 VecSetType(rVec, VECMPI);
465 VecLoad(view, VECMPI, &rVec);
470 VecDuplicate(rParallelLayout, &rVec);
471#if (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR >= 2)
474 VecLoadIntoVector(view, rVec);
482#ifdef __INTEL_COMPILER
490 MatPartitioning part;
491 MatPartitioningCreate(PETSC_COMM_WORLD, &part);
495 PetscPushErrorHandler(PetscIgnoreErrorHandler,
nullptr);
497#if (PETSC_VERSION_MAJOR == 2 || (PETSC_VERSION_MAJOR == 3 && PETSC_VERSION_MINOR < 2))
498 PetscErrorCode parmetis_installed_error = MatPartitioningSetType(part, MAT_PARTITIONING_PARMETIS);
500 PetscErrorCode parmetis_installed_error = MatPartitioningSetType(part, MATPARTITIONINGPARMETIS);
504 PetscPopErrorHandler();
513 if (parmetis_installed_error == 0)
515 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.");
520 return (parmetis_installed_error == 0);