Actual source code: gcreate.c

  1: #include <petsc/private/matimpl.h>

  3: #include <../src/mat/impls/aij/seq/aij.h>
  4: #include <../src/mat/impls/aij/mpi/mpiaij.h>

  6: PetscErrorCode MatSetBlockSizes_Default(Mat mat, PetscInt rbs, PetscInt cbs)
  7: {
  8:   PetscFunctionBegin;
  9:   if (!mat->preallocated) PetscFunctionReturn(PETSC_SUCCESS);
 10:   PetscCheck(mat->rmap->bs <= 0 || mat->rmap->bs == rbs, PetscObjectComm((PetscObject)mat), PETSC_ERR_SUP, "Cannot change row block size %" PetscInt_FMT " to %" PetscInt_FMT, mat->rmap->bs, rbs);
 11:   PetscCheck(mat->cmap->bs <= 0 || mat->cmap->bs == cbs, PetscObjectComm((PetscObject)mat), PETSC_ERR_SUP, "Cannot change column block size %" PetscInt_FMT " to %" PetscInt_FMT, mat->cmap->bs, cbs);
 12:   PetscFunctionReturn(PETSC_SUCCESS);
 13: }

 15: PetscErrorCode MatShift_Basic(Mat Y, PetscScalar a)
 16: {
 17:   PetscInt        i, start, end, oldValA = 0, oldValB = 0;
 18:   PetscScalar     alpha = a;
 19:   PetscBool       isSeqAIJDerived, isMPIAIJDerived; // all classes sharing SEQAIJHEADER or MPIAIJHEADER
 20:   Mat             A = NULL, B = NULL;
 21:   const PetscBool nooffprocentries = Y->nooffprocentries;

 23:   PetscFunctionBegin;
 24:   PetscCall(PetscObjectBaseTypeCompareAny((PetscObject)Y, &isSeqAIJDerived, MATSEQAIJ, MATSEQBAIJ, MATSEQSBAIJ, ""));
 25:   PetscCall(PetscObjectBaseTypeCompareAny((PetscObject)Y, &isMPIAIJDerived, MATMPIAIJ, MATMPIBAIJ, MATMPISBAIJ, ""));

 27:   if (isSeqAIJDerived) A = Y;
 28:   else if (isMPIAIJDerived) {
 29:     Mat_MPIAIJ *mpiaij = (Mat_MPIAIJ *)Y->data;
 30:     A                  = mpiaij->A;
 31:     B                  = mpiaij->B;
 32:   }

 34:   if (A) {
 35:     oldValA                        = ((Mat_SeqAIJ *)A->data)->nonew;
 36:     ((Mat_SeqAIJ *)A->data)->nonew = 0; // so that new nonzero locations are allowed
 37:   }
 38:   if (B) {
 39:     oldValB                        = ((Mat_SeqAIJ *)B->data)->nonew;
 40:     ((Mat_SeqAIJ *)B->data)->nonew = 0;
 41:   }

 43:   PetscCall(MatGetOwnershipRange(Y, &start, &end));
 44:   for (i = start; i < end; i++) {
 45:     if (i < Y->cmap->N) PetscCall(MatSetValues(Y, 1, &i, 1, &i, &alpha, ADD_VALUES));
 46:   }
 47:   Y->nooffprocentries = PETSC_TRUE;
 48:   PetscCall(MatAssemblyBegin(Y, MAT_FINAL_ASSEMBLY));
 49:   PetscCall(MatAssemblyEnd(Y, MAT_FINAL_ASSEMBLY));
 50:   Y->nooffprocentries = nooffprocentries;
 51:   if (A) ((Mat_SeqAIJ *)A->data)->nonew = oldValA;
 52:   if (B) ((Mat_SeqAIJ *)B->data)->nonew = oldValB;
 53:   PetscFunctionReturn(PETSC_SUCCESS);
 54: }

 56: /*@
 57:   MatCreate - Creates a matrix where the type is determined
 58:   from either a call to `MatSetType()` or from the options database
 59:   with a call to `MatSetFromOptions()`.

 61:   Collective

 63:   Input Parameter:
 64: . comm - MPI communicator

 66:   Output Parameter:
 67: . A - the matrix

 69:   Options Database Keys:
 70: + -mat_type seqaij   - `MATSEQAIJ` type, uses `MatCreateSeqAIJ()`
 71: . -mat_type mpiaij   - `MATMPIAIJ` type, uses `MatCreateAIJ()`
 72: . -mat_type seqdense - `MATSEQDENSE`, uses `MatCreateSeqDense()`
 73: . -mat_type mpidense - `MATMPIDENSE` type, uses `MatCreateDense()`
 74: . -mat_type seqbaij  - `MATSEQBAIJ` type, uses `MatCreateSeqBAIJ()`
 75: - -mat_type mpibaij  - `MATMPIBAIJ` type, uses `MatCreateBAIJ()`

 77:    See the manpages for particular formats (e.g., `MATSEQAIJ`)
 78:    for additional format-specific options.

 80:   Level: beginner

 82:   Notes:
 83:   The default matrix type is `MATAIJ`, using the routines `MatCreateSeqAIJ()` or
 84:   `MatCreateAIJ()` if you do not set a type in the options database. If you never call
 85:   `MatSetType()` or `MatSetFromOptions()` it will generate an error when you try to use the
 86:   matrix.

 88: .seealso: [](ch_matrices), `Mat`, `MatCreateSeqAIJ()`, `MatCreateAIJ()`,
 89:           `MatCreateSeqDense()`, `MatCreateDense()`,
 90:           `MatCreateSeqBAIJ()`, `MatCreateBAIJ()`,
 91:           `MatCreateSeqSBAIJ()`, `MatCreateSBAIJ()`,
 92:           `MatConvert()`
 93: @*/
 94: PetscErrorCode MatCreate(MPI_Comm comm, Mat *A)
 95: {
 96:   Mat B;

 98:   PetscFunctionBegin;
 99:   PetscAssertPointer(A, 2);
100:   PetscCall(MatInitializePackage());

102:   PetscCall(PetscHeaderCreate(B, MAT_CLASSID, "Mat", "Matrix", "Mat", comm, MatDestroy, MatView));
103:   PetscCall(PetscLayoutCreate(comm, &B->rmap));
104:   PetscCall(PetscLayoutCreate(comm, &B->cmap));
105:   PetscCall(PetscStrallocpy(VECSTANDARD, &B->defaultvectype));
106:   PetscCall(PetscStrallocpy(PETSCRANDER48, &B->defaultrandtype));

108:   B->symmetric                   = PETSC_BOOL3_UNKNOWN;
109:   B->hermitian                   = PETSC_BOOL3_UNKNOWN;
110:   B->structurally_symmetric      = PETSC_BOOL3_UNKNOWN;
111:   B->spd                         = PETSC_BOOL3_UNKNOWN;
112:   B->symmetry_eternal            = PETSC_FALSE;
113:   B->structural_symmetry_eternal = PETSC_FALSE;

115:   B->congruentlayouts = PETSC_DECIDE;
116:   B->preallocated     = PETSC_FALSE;
117: #if PetscDefined(HAVE_DEVICE)
118:   B->boundtocpu = PETSC_TRUE;
119: #endif
120:   *A = B;
121:   PetscFunctionReturn(PETSC_SUCCESS);
122: }

124: /*@
125:   MatCreateFromOptions - Creates a matrix whose type is set from the options database

127:   Collective

129:   Input Parameters:
130: + comm   - MPI communicator
131: . prefix - [optional] prefix for the options database
132: . bs     - the blocksize (commonly 1)
133: . m      - the local number of rows (or `PETSC_DECIDE`)
134: . n      - the local number of columns (or `PETSC_DECIDE` or `PETSC_DETERMINE`)
135: . M      - the global number of rows (or `PETSC_DETERMINE`)
136: - N      - the global number of columns (or `PETSC_DETERMINE`)

138:   Output Parameter:
139: . A - the matrix

141:   Options Database Key:
142: . -mat_type - see `MatType`, for example `aij`, `aijcusparse`, `baij`, `sbaij`, `dense`, defaults to `aij`

144:   Level: beginner

146: .seealso: [](ch_matrices), `Mat`, `MatCreateSeqAIJ()`, `MatCreateAIJ()`,
147:           `MatCreateSeqDense()`, `MatCreateDense()`,
148:           `MatCreateSeqBAIJ()`, `MatCreateBAIJ()`,
149:           `MatCreateSeqSBAIJ()`, `MatCreateSBAIJ()`,
150:           `MatConvert()`, `MatCreate()`
151: @*/
152: PetscErrorCode MatCreateFromOptions(MPI_Comm comm, const char *prefix, PetscInt bs, PetscInt m, PetscInt n, PetscInt M, PetscInt N, Mat *A)
153: {
154:   PetscFunctionBegin;
155:   PetscAssertPointer(A, 8);
156:   PetscCall(MatCreate(comm, A));
157:   if (prefix) PetscCall(MatSetOptionsPrefix(*A, prefix));
158:   PetscCall(MatSetBlockSize(*A, bs));
159:   PetscCall(MatSetSizes(*A, m, n, M, N));
160:   PetscCall(MatSetFromOptions(*A));
161:   PetscFunctionReturn(PETSC_SUCCESS);
162: }

164: /*@
165:   MatSetErrorIfFailure - Causes `Mat` to generate an immediate error, for example a zero pivot, is detected.

167:   Logically Collective

169:   Input Parameters:
170: + mat - matrix obtained from `MatCreate()`
171: - flg - `PETSC_TRUE` indicates you want the error generated

173:   Level: advanced

175:   Note:
176:   If this flag is not set then the matrix operation will note the error and continue. The error may cause a later `PC` or `KSP` error
177:   or result in a `KSPConvergedReason` indicating the method did not converge.

179: .seealso: [](ch_matrices), `Mat`, `PCSetErrorIfFailure()`, `KSPConvergedReason`, `SNESConvergedReason`
180: @*/
181: PetscErrorCode MatSetErrorIfFailure(Mat mat, PetscBool flg)
182: {
183:   PetscFunctionBegin;
186:   mat->erroriffailure = flg;
187:   PetscFunctionReturn(PETSC_SUCCESS);
188: }

190: /*@
191:   MatSetSizes - Sets the local and global sizes, and checks to determine compatibility

193:   Collective

195:   Input Parameters:
196: + A - the matrix
197: . m - number of local rows (or `PETSC_DECIDE`)
198: . n - number of local columns (or `PETSC_DECIDE`)
199: . M - number of global rows (or `PETSC_DETERMINE`)
200: - N - number of global columns (or `PETSC_DETERMINE`)

202:   Level: beginner

204:   Notes:
205:   `m` (`n`) and `M` (`N`) cannot be both `PETSC_DECIDE`
206:   If one processor calls this with `M` (`N`) of `PETSC_DECIDE` then all processors must, otherwise the program will hang.

208:   If `PETSC_DECIDE` is not used for the arguments 'm' and 'n', then the
209:   user must ensure that they are chosen to be compatible with the
210:   vectors. To do this, one first considers the matrix-vector product
211:   'y = A x'. The `m` that is used in the above routine must match the
212:   local size of 'y'. Likewise, the `n` used must match the local size of 'x'.

214:   If `m` and `n` are not `PETSC_DECIDE`, then the values determine the `PetscLayout` of the matrix and the ranges returned by
215:   `MatGetOwnershipRange()`,  `MatGetOwnershipRanges()`, `MatGetOwnershipRangeColumn()`, and `MatGetOwnershipRangesColumn()`.

217:   You cannot change the sizes once they have been set.

219:   The sizes must be set before `MatSetUp()` or MatXXXSetPreallocation() is called.

221: .seealso: [](ch_matrices), `Mat`, `MatGetSize()`, `PetscSplitOwnership()`, `MatGetOwnershipRange()`, `MatGetOwnershipRanges()`,
222:           `MatGetOwnershipRangeColumn()`, `MatGetOwnershipRangesColumn()`, `PetscLayout`, `VecSetSizes()`
223: @*/
224: PetscErrorCode MatSetSizes(Mat A, PetscInt m, PetscInt n, PetscInt M, PetscInt N)
225: {
226:   PetscFunctionBegin;
230:   PetscCheck(M <= 0 || m <= M, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Local row size %" PetscInt_FMT " cannot be larger than global row size %" PetscInt_FMT, m, M);
231:   PetscCheck(N <= 0 || n <= N, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Local column size %" PetscInt_FMT " cannot be larger than global column size %" PetscInt_FMT, n, N);
232:   PetscCheck((A->rmap->n < 0 || A->rmap->N < 0) || (A->rmap->n == m && (M <= 0 || A->rmap->N == M)), PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot change/reset row sizes to %" PetscInt_FMT " local %" PetscInt_FMT " global after previously setting them to %" PetscInt_FMT " local %" PetscInt_FMT " global", m, M,
233:              A->rmap->n, A->rmap->N);
234:   PetscCheck((A->cmap->n < 0 || A->cmap->N < 0) || (A->cmap->n == n && (N <= 0 || A->cmap->N == N)), PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot change/reset column sizes to %" PetscInt_FMT " local %" PetscInt_FMT " global after previously setting them to %" PetscInt_FMT " local %" PetscInt_FMT " global", n, N,
235:              A->cmap->n, A->cmap->N);
236:   A->rmap->n = m;
237:   A->cmap->n = n;
238:   A->rmap->N = M > -1 ? M : A->rmap->N;
239:   A->cmap->N = N > -1 ? N : A->cmap->N;
240:   PetscFunctionReturn(PETSC_SUCCESS);
241: }

243: /*@
244:   MatSetFromOptions - Creates a matrix where the type is determined
245:   from the options database.

247:   Collective

249:   Input Parameter:
250: . B - the matrix

252:   Options Database Keys:
253: + -mat_block_size size                         - set the block size used to store the matrix; see `MatSetBlockSize()`
254: . -mat_type type                               - set the matrix type; see `MatType` and `MatSetType()`
255: . -mat_vec_type vtype                          - set the `VecType` used by `MatCreateVecs()`; see `MatSetVecType()`
256: . -mat_is_symmetric tol                        - check symmetry during `MatAssemblyEnd()` with an optional tolerance; see `MatIsSymmetric()`
257: . -mat_null_space_test (true|false)            - test the attached null space during `MatAssemblyEnd()`; see `MatNullSpaceTest()`
258: . -mat_error_if_failure (true|false)           - generate an error if matrix factorization fails; see `MatSetErrorIfFailure()`
259: . -mat_spd (true|false)                        - set the `MAT_SPD` property with `MatSetOption()`
260: . -mat_new_nonzero_location_err (true|false)   - generate an error if insertion creates a new nonzero location; see `MAT_NEW_NONZERO_LOCATION_ERR` in `MatSetOption()`
261: . -mat_new_nonzero_allocation_err (true|false) - generate an error if insertion requires additional nonzero allocation; see `MAT_NEW_NONZERO_ALLOCATION_ERR` in `MatSetOption()`
262: . -mat_ignore_zero_entries (true|false)        - ignore eligible zero entries during insertion; see `MAT_IGNORE_ZERO_ENTRIES` in `MatSetOption()`
263: . -mat_form_explicit_transpose (true|false)    - hint to form an explicit transpose for operations such as `MatMultTranspose()`
264: - -mat_bind_below size                         - bind the matrix to the CPU if its number of local rows is less than size; see `MatBindToCPU()`

266:   Level: beginner

268:   Notes:
269:   Generates a parallel MPI matrix if the communicator has more than one processor.  The default
270:   matrix type is `MATAIJ`, using the routines `MatCreateSeqAIJ()` and `MatCreateAIJ()` if you
271:   do not select a type in the options database.

273:   See the manpages for particular formats (e.g., `MATSEQAIJ`)
274:   for additional format-specific options.

276: .seealso: [](ch_matrices), `Mat`, `MatType`, `MatCreateSeqAIJ()`, `MatCreateAIJ()`,
277:           `MatCreateSeqDense()`, `MatCreateDense()`,
278:           `MatCreateSeqBAIJ()`, `MatCreateBAIJ()`,
279:           `MatCreateSeqSBAIJ()`, `MatCreateSBAIJ()`,
280:           `MatConvert()`
281: @*/
282: PetscErrorCode MatSetFromOptions(Mat B)
283: {
284:   const char *deft = MATAIJ;
285:   char        type[256];
286:   PetscBool   flg, set;
287:   PetscInt    bind_below = 0, newbs = -1;

289:   PetscFunctionBegin;

292:   PetscObjectOptionsBegin((PetscObject)B);

294:   PetscCall(PetscOptionsInt("-mat_block_size", "Set the blocksize used to store the matrix", "MatSetBlockSize", newbs, &newbs, &flg));
295:   if (flg) {
296:     PetscCall(PetscLayoutSetBlockSize(B->rmap, newbs));
297:     PetscCall(PetscLayoutSetBlockSize(B->cmap, newbs));
298:   }

300:   PetscCall(PetscOptionsFList("-mat_type", "Matrix type", "MatSetType", MatList, deft, type, sizeof(type), &flg));
301:   if (flg) PetscCall(MatSetType(B, type));
302:   else if (!((PetscObject)B)->type_name) PetscCall(MatSetType(B, deft));
303:   PetscCall(PetscOptionsFList("-mat_vec_type", "Vec type used by MatCreateVecs()", "MatSetVecType", VecList, B->defaultvectype, type, sizeof(type), &flg));
304:   if (flg) PetscCall(MatSetVecType(B, type));

306:   if (newbs > 0) PetscTryTypeMethod(B, setblocksizes, newbs, newbs);

308:   PetscCall(PetscOptionsName("-mat_is_symmetric", "Checks if mat is symmetric on MatAssemblyEnd()", "MatIsSymmetric", &B->checksymmetryonassembly));
309:   PetscCall(PetscOptionsReal("-mat_is_symmetric", "Checks if mat is symmetric on MatAssemblyEnd()", "MatIsSymmetric", B->checksymmetrytol, &B->checksymmetrytol, NULL));
310:   PetscCall(PetscOptionsBool("-mat_null_space_test", "Checks if provided null space is correct in MatAssemblyEnd()", "MatNullSpaceTest", B->checknullspaceonassembly, &B->checknullspaceonassembly, NULL));
311:   PetscCall(PetscOptionsBool("-mat_error_if_failure", "Generate an error if an error occurs when factoring the matrix", "MatSetErrorIfFailure", B->erroriffailure, &B->erroriffailure, NULL));

313:   PetscTryTypeMethod(B, setfromoptions, PetscOptionsObject);

315:   flg = PETSC_FALSE;
316:   PetscCall(PetscOptionsBool("-mat_spd", "Set whether the matrix is symmetric positive definite", "MatSetOption", flg, &flg, &set));
317:   if (set) PetscCall(MatSetOption(B, MAT_SPD, flg));

319:   flg = PETSC_FALSE;
320:   PetscCall(PetscOptionsBool("-mat_new_nonzero_location_err", "Generate an error if new nonzeros are created in the matrix nonzero structure (useful to test preallocation)", "MatSetOption", flg, &flg, &set));
321:   if (set) PetscCall(MatSetOption(B, MAT_NEW_NONZERO_LOCATION_ERR, flg));
322:   flg = PETSC_FALSE;
323:   PetscCall(PetscOptionsBool("-mat_new_nonzero_allocation_err", "Generate an error if new nonzeros are allocated in the matrix nonzero structure (useful to test preallocation)", "MatSetOption", flg, &flg, &set));
324:   if (set) PetscCall(MatSetOption(B, MAT_NEW_NONZERO_ALLOCATION_ERR, flg));
325:   flg = PETSC_FALSE;
326:   PetscCall(PetscOptionsBool("-mat_ignore_zero_entries", "For AIJ/SELL/IS matrices this will stop zero values from creating a zero location in the matrix", "MatSetOption", flg, &flg, &set));
327:   if (set) PetscCall(MatSetOption(B, MAT_IGNORE_ZERO_ENTRIES, flg));

329:   flg = PETSC_FALSE;
330:   PetscCall(PetscOptionsBool("-mat_form_explicit_transpose", "Hint to form an explicit transpose for operations like MatMultTranspose", "MatSetOption", flg, &flg, &set));
331:   if (set) PetscCall(MatSetOption(B, MAT_FORM_EXPLICIT_TRANSPOSE, flg));

333:   /* Bind to CPU if below a user-specified size threshold.
334:    * This perhaps belongs in the options for the GPU Mat types, but MatBindToCPU() does nothing when called on non-GPU types,
335:    * and putting it here makes is more maintainable than duplicating this for all. */
336:   PetscCall(PetscOptionsInt("-mat_bind_below", "Set the size threshold (in local rows) below which the Mat is bound to the CPU", "MatBindToCPU", bind_below, &bind_below, &flg));
337:   if (flg && B->rmap->n < bind_below) PetscCall(MatBindToCPU(B, PETSC_TRUE));

339:   /* process any options handlers added with PetscObjectAddOptionsHandler() */
340:   PetscCall(PetscObjectProcessOptionsHandlers((PetscObject)B, PetscOptionsObject));
341:   PetscOptionsEnd();
342:   PetscFunctionReturn(PETSC_SUCCESS);
343: }

345: /*@
346:   MatXAIJSetPreallocation - set preallocation for serial and parallel `MATAIJ`, `MATBAIJ`, and `MATSBAIJ` matrices and their unassembled versions.

348:   Collective

350:   Input Parameters:
351: + A     - matrix being preallocated
352: . bs    - block size
353: . dnnz  - number of nonzero column blocks per block row of diagonal part of parallel matrix
354: . onnz  - number of nonzero column blocks per block row of off-diagonal part of parallel matrix
355: . dnnzu - number of nonzero column blocks per block row of upper-triangular part of diagonal part of parallel matrix
356: - onnzu - number of nonzero column blocks per block row of upper-triangular part of off-diagonal part of parallel matrix

358:   Level: beginner

360: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJSetPreallocation()`, `MatMPIAIJSetPreallocation()`, `MatSeqBAIJSetPreallocation()`, `MatMPIBAIJSetPreallocation()`,
361:           `MatSeqSBAIJSetPreallocation()`, `MatMPISBAIJSetPreallocation()`,
362:           `PetscSplitOwnership()`
363: @*/
364: PetscErrorCode MatXAIJSetPreallocation(Mat A, PetscInt bs, const PetscInt dnnz[], const PetscInt onnz[], const PetscInt dnnzu[], const PetscInt onnzu[])
365: {
366:   PetscInt  cbs;
367:   PetscBool aij, is, hyp;

369:   PetscFunctionBegin;
370:   if (bs != PETSC_DECIDE) { /* don't mess with an already set block size */
371:     PetscCall(MatSetBlockSize(A, bs));
372:   }
373:   PetscCall(PetscLayoutSetUp(A->rmap));
374:   PetscCall(PetscLayoutSetUp(A->cmap));
375:   PetscCall(MatGetBlockSizes(A, &bs, &cbs));
376:   /* these routines assumes bs == cbs, this should be checked somehow */
377:   PetscCall(MatSeqBAIJSetPreallocation(A, bs, 0, dnnz));
378:   PetscCall(MatMPIBAIJSetPreallocation(A, bs, 0, dnnz, 0, onnz));
379:   PetscCall(MatSeqSBAIJSetPreallocation(A, bs, 0, dnnzu));
380:   PetscCall(MatMPISBAIJSetPreallocation(A, bs, 0, dnnzu, 0, onnzu));
381:   /*
382:     In general, we have to do extra work to preallocate for scalar (AIJ) or unassembled (IS) matrices so we check whether it will do any
383:     good before going on with it.
384:   */
385:   PetscCall(PetscObjectHasFunction((PetscObject)A, "MatMPIAIJSetPreallocation_C", &aij));
386:   PetscCall(PetscObjectHasFunction((PetscObject)A, "MatISSetPreallocation_C", &is));
387:   PetscCall(PetscObjectHasFunction((PetscObject)A, "MatHYPRESetPreallocation_C", &hyp));
388:   if (!aij && !is && !hyp) PetscCall(PetscObjectHasFunction((PetscObject)A, "MatSeqAIJSetPreallocation_C", &aij));
389:   if (aij || is || hyp) {
390:     if (bs == cbs && bs == 1) {
391:       PetscCall(MatSeqAIJSetPreallocation(A, 0, dnnz));
392:       PetscCall(MatMPIAIJSetPreallocation(A, 0, dnnz, 0, onnz));
393:       PetscCall(MatISSetPreallocation(A, 0, dnnz, 0, onnz));
394: #if PetscDefined(HAVE_HYPRE)
395:       PetscCall(MatHYPRESetPreallocation(A, 0, dnnz, 0, onnz));
396: #endif
397:     } else { /* Convert block-row precallocation to scalar-row */
398:       PetscInt i, m, *sdnnz, *sonnz;
399:       PetscCall(MatGetLocalSize(A, &m, NULL));
400:       PetscCall(PetscMalloc2((!!dnnz) * m, &sdnnz, (!!onnz) * m, &sonnz));
401:       for (i = 0; i < m; i++) {
402:         if (dnnz) sdnnz[i] = dnnz[i / bs] * cbs;
403:         if (onnz) sonnz[i] = onnz[i / bs] * cbs;
404:       }
405:       PetscCall(MatSeqAIJSetPreallocation(A, 0, dnnz ? sdnnz : NULL));
406:       PetscCall(MatMPIAIJSetPreallocation(A, 0, dnnz ? sdnnz : NULL, 0, onnz ? sonnz : NULL));
407:       PetscCall(MatISSetPreallocation(A, 0, dnnz ? sdnnz : NULL, 0, onnz ? sonnz : NULL));
408: #if PetscDefined(HAVE_HYPRE)
409:       PetscCall(MatHYPRESetPreallocation(A, 0, dnnz ? sdnnz : NULL, 0, onnz ? sonnz : NULL));
410: #endif
411:       PetscCall(PetscFree2(sdnnz, sonnz));
412:     }
413:   }
414:   PetscFunctionReturn(PETSC_SUCCESS);
415: }

417: /*@
418:   MatHeaderMerge - Merges some information from the header of `C` to `A`; the `C` object is then destroyed

420:   Collective, No Fortran Support

422:   Input Parameters:
423: + A - a `Mat` being merged into
424: - C - the `Mat` providing the merge information

426:   Level: developer

428:   Notes:
429:   `A` and `C` must be of the same type.
430:   The object list and query function list in `A` are retained, as well as the object name, and prefix.
431:   The object state of `A` is increased by 1.

433:   Developer Note:
434:   This is somewhat different from `MatHeaderReplace()`, it would be nice to merge the code

436: .seealso: `Mat`, `MatHeaderReplace()`
437:  @*/
438: PetscErrorCode MatHeaderMerge(Mat A, Mat *C)
439: {
440:   PetscInt          refct;
441:   PetscOps          Abops;
442:   struct _MatOps    Aops;
443:   char             *mtype, *mname, *mprefix;
444:   Mat_Product      *product;
445:   Mat_Redundant    *redundant;
446:   PetscObjectState  state;
447:   PetscObjectList   olist;
448:   PetscFunctionList qlist;

450:   PetscFunctionBegin;
453:   if (A == *C) PetscFunctionReturn(PETSC_SUCCESS);
454:   PetscCheckSameTypeAndComm(A, 1, *C, 2);
455:   /* save the parts of A we need */
456:   Abops     = ((PetscObject)A)->bops[0];
457:   Aops      = A->ops[0];
458:   refct     = ((PetscObject)A)->refct;
459:   mtype     = ((PetscObject)A)->type_name;
460:   mname     = ((PetscObject)A)->name;
461:   state     = ((PetscObject)A)->state;
462:   mprefix   = ((PetscObject)A)->prefix;
463:   product   = A->product;
464:   redundant = A->redundant;
465:   qlist     = ((PetscObject)A)->qlist;
466:   olist     = ((PetscObject)A)->olist;

468:   /* zero these so the destroy below does not free them */
469:   ((PetscObject)A)->type_name = NULL;
470:   ((PetscObject)A)->name      = NULL;
471:   ((PetscObject)A)->qlist     = NULL;
472:   ((PetscObject)A)->olist     = NULL;

474:   /*
475:      free all the interior data structures from mat
476:      cannot use PetscUseTypeMethod(A,destroy); because compiler
477:      thinks it may print NULL type_name and name
478:   */
479:   PetscTryTypeMethod(A, destroy);

481:   PetscCall(PetscFree(A->defaultvectype));
482:   PetscCall(PetscFree(A->defaultrandtype));
483:   PetscCall(PetscLayoutDestroy(&A->rmap));
484:   PetscCall(PetscLayoutDestroy(&A->cmap));
485:   PetscCall(PetscComposedQuantitiesDestroy((PetscObject)A));

487:   /* copy C over to A */
488:   PetscCall(PetscFree(A->factorprefix));
489:   PetscCall(PetscMemcpy(A, *C, sizeof(struct _p_Mat)));

491:   /* return the parts of A we saved */
492:   ((PetscObject)A)->bops[0]   = Abops;
493:   A->ops[0]                   = Aops;
494:   ((PetscObject)A)->refct     = refct;
495:   ((PetscObject)A)->type_name = mtype;
496:   ((PetscObject)A)->name      = mname;
497:   ((PetscObject)A)->prefix    = mprefix;
498:   ((PetscObject)A)->state     = state + 1;
499:   A->product                  = product;
500:   A->redundant                = redundant;

502:   /* Append the saved lists */
503:   PetscCall(PetscFunctionListDuplicate(qlist, &((PetscObject)A)->qlist));
504:   PetscCall(PetscObjectListDuplicate(olist, &((PetscObject)A)->olist));
505:   PetscCall(PetscFunctionListDestroy(&qlist));
506:   PetscCall(PetscObjectListDestroy(&olist));

508:   /* since these two are copied into A we do not want them destroyed in C */
509:   ((PetscObject)*C)->qlist = NULL;
510:   ((PetscObject)*C)->olist = NULL;
511:   PetscCall(PetscHeaderDestroy(C));
512:   PetscFunctionReturn(PETSC_SUCCESS);
513: }

515: /*@
516:   MatHeaderReplace - Replaces the internal data of matrix `A` by the internal data of matrix `C` while deleting the outer wrapper of `C`

518:   Input Parameters:
519: + A - a `Mat` whose internal data is to be replaced
520: - C - the `Mat` providing new internal data for `A`

522:   Level: advanced

524:   Example Usage\:
525: .vb
526:   Mat C;
527:   MatCreateSeqAIJWithArrays(..., &C);
528:   MatHeaderReplace(A, &C);
529:   // C has been destroyed and A contains the matrix entries of C
530: .ve

532:   Note:
533:   This can be used inside a function provided to `SNESSetJacobian()`, `TSSetRHSJacobian()`, or `TSSetIJacobian()` in cases where the user code
534:   computes an entirely new sparse matrix  (generally with a different matrix nonzero structure/pattern) for each Newton update.
535:   It is usually better to reuse the matrix nonzero structure of `A` instead of constructing an entirely new one.

537:   Developer Note:
538:   This is somewhat different from `MatHeaderMerge()` it would be nice to merge the code

540: .seealso: `Mat`, `MatHeaderMerge()`
541:  @*/
542: PetscErrorCode MatHeaderReplace(Mat A, Mat *C)
543: {
544:   PetscInt         refct;
545:   PetscObjectState state;
546:   struct _p_Mat    buffer;
547:   MatStencilInfo   stencil;

549:   PetscFunctionBegin;
552:   if (A == *C) PetscFunctionReturn(PETSC_SUCCESS);
553:   PetscCheckSameComm(A, 1, *C, 2);
554:   PetscCheck(((PetscObject)*C)->refct == 1, PetscObjectComm((PetscObject)C), PETSC_ERR_ARG_WRONGSTATE, "Object C has refct %" PetscInt_FMT " > 1, would leave hanging reference", ((PetscObject)*C)->refct);

556:   /* swap C and A */
557:   refct   = ((PetscObject)A)->refct;
558:   state   = ((PetscObject)A)->state;
559:   stencil = A->stencil;
560:   PetscCall(PetscMemcpy(&buffer, A, sizeof(struct _p_Mat)));
561:   PetscCall(PetscMemcpy(A, *C, sizeof(struct _p_Mat)));
562:   PetscCall(PetscMemcpy(*C, &buffer, sizeof(struct _p_Mat)));
563:   ((PetscObject)A)->refct = refct;
564:   ((PetscObject)A)->state = state + 1;
565:   A->stencil              = stencil;

567:   ((PetscObject)*C)->refct = 1;
568:   PetscCall(MatDestroy(C));
569:   PetscFunctionReturn(PETSC_SUCCESS);
570: }

572: /*@
573:   MatBindToCPU - marks a matrix to temporarily stay on the CPU and perform computations on the CPU

575:   Logically Collective

577:   Input Parameters:
578: + A   - the matrix
579: - flg - bind to the CPU if value of `PETSC_TRUE`

581:   Level: intermediate

583:   Note:
584:   `MATAIJKOKKOS` has yet to implement CPU binding. If Kokkos is configured without GPU support,
585:   we deem a `MATAIJKOKKOS` matrix as bound to the CPU. Different from `MATAIJVIENNACL` with a CPU
586:   backend, `MATAIJKOKKOS` always use its own operation implementations (in contrast to CPU-bound
587:   `MATAIJVIENNACL`, which uses `MATAIJ`'s CPU operations).

589: .seealso: [](ch_matrices), `Mat`, `MatBoundToCPU()`
590: @*/
591: PetscErrorCode MatBindToCPU(Mat A, PetscBool flg)
592: {
593:   PetscFunctionBegin;
596: #if PetscDefined(HAVE_DEVICE)
597:   if (A->boundtocpu == flg) PetscFunctionReturn(PETSC_SUCCESS);
598:   A->boundtocpu = flg;
599:   PetscTryTypeMethod(A, bindtocpu, flg);
600: #endif
601:   PetscFunctionReturn(PETSC_SUCCESS);
602: }

604: /*@
605:   MatBoundToCPU - query if a matrix is bound to the CPU

607:   Input Parameter:
608: . A - the matrix

610:   Output Parameter:
611: . flg - the logical flag

613:   Level: intermediate

615: .seealso: [](ch_matrices), `Mat`, `MatBindToCPU()`
616: @*/
617: PetscErrorCode MatBoundToCPU(Mat A, PetscBool *flg)
618: {
619:   PetscFunctionBegin;
621:   PetscAssertPointer(flg, 2);
622: #if PetscDefined(HAVE_DEVICE)
623:   *flg = A->boundtocpu;
624: #else
625:   *flg = PETSC_TRUE;
626: #endif
627:   PetscFunctionReturn(PETSC_SUCCESS);
628: }

630: PetscErrorCode MatSetValuesCOO_Basic(Mat A, const PetscScalar coo_v[], InsertMode imode)
631: {
632:   IS              is_coo_i, is_coo_j;
633:   const PetscInt *coo_i, *coo_j;
634:   PetscInt        n, n_i, n_j;
635:   PetscScalar     zero = 0.;

637:   PetscFunctionBegin;
638:   PetscCall(PetscObjectQuery((PetscObject)A, "__PETSc_coo_i", (PetscObject *)&is_coo_i));
639:   PetscCall(PetscObjectQuery((PetscObject)A, "__PETSc_coo_j", (PetscObject *)&is_coo_j));
640:   PetscCheck(is_coo_i, PetscObjectComm((PetscObject)A), PETSC_ERR_COR, "Missing coo_i IS");
641:   PetscCheck(is_coo_j, PetscObjectComm((PetscObject)A), PETSC_ERR_COR, "Missing coo_j IS");
642:   PetscCall(ISGetLocalSize(is_coo_i, &n_i));
643:   PetscCall(ISGetLocalSize(is_coo_j, &n_j));
644:   PetscCheck(n_i == n_j, PETSC_COMM_SELF, PETSC_ERR_COR, "Wrong local size %" PetscInt_FMT " != %" PetscInt_FMT, n_i, n_j);
645:   PetscCall(ISGetIndices(is_coo_i, &coo_i));
646:   PetscCall(ISGetIndices(is_coo_j, &coo_j));
647:   if (imode != ADD_VALUES) PetscCall(MatZeroEntries(A));
648:   for (n = 0; n < n_i; n++) PetscCall(MatSetValue(A, coo_i[n], coo_j[n], coo_v ? coo_v[n] : zero, ADD_VALUES));
649:   PetscCall(ISRestoreIndices(is_coo_i, &coo_i));
650:   PetscCall(ISRestoreIndices(is_coo_j, &coo_j));
651:   PetscFunctionReturn(PETSC_SUCCESS);
652: }

654: PetscErrorCode MatSetPreallocationCOO_Basic(Mat A, PetscCount ncoo, PetscInt coo_i[], PetscInt coo_j[])
655: {
656:   Mat         preallocator;
657:   IS          is_coo_i, is_coo_j;
658:   PetscInt    ncoo_i;
659:   PetscScalar zero = 0.0;

661:   PetscFunctionBegin;
662:   PetscCall(PetscIntCast(ncoo, &ncoo_i));
663:   PetscCall(PetscLayoutSetUp(A->rmap));
664:   PetscCall(PetscLayoutSetUp(A->cmap));
665:   PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &preallocator));
666:   PetscCall(MatSetType(preallocator, MATPREALLOCATOR));
667:   PetscCall(MatSetSizes(preallocator, A->rmap->n, A->cmap->n, A->rmap->N, A->cmap->N));
668:   PetscCall(MatSetLayouts(preallocator, A->rmap, A->cmap));
669:   PetscCall(MatSetUp(preallocator));
670:   for (PetscCount n = 0; n < ncoo; n++) PetscCall(MatSetValue(preallocator, coo_i[n], coo_j[n], zero, INSERT_VALUES));
671:   PetscCall(MatAssemblyBegin(preallocator, MAT_FINAL_ASSEMBLY));
672:   PetscCall(MatAssemblyEnd(preallocator, MAT_FINAL_ASSEMBLY));
673:   PetscCall(MatPreallocatorPreallocate(preallocator, PETSC_TRUE, A));
674:   PetscCall(MatDestroy(&preallocator));
675:   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, ncoo_i, coo_i, PETSC_COPY_VALUES, &is_coo_i));
676:   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, ncoo_i, coo_j, PETSC_COPY_VALUES, &is_coo_j));
677:   PetscCall(PetscObjectCompose((PetscObject)A, "__PETSc_coo_i", (PetscObject)is_coo_i));
678:   PetscCall(PetscObjectCompose((PetscObject)A, "__PETSc_coo_j", (PetscObject)is_coo_j));
679:   PetscCall(ISDestroy(&is_coo_i));
680:   PetscCall(ISDestroy(&is_coo_j));
681:   PetscFunctionReturn(PETSC_SUCCESS);
682: }

684: /*@
685:   MatSetPreallocationCOO - set preallocation for matrices using a coordinate format of the entries with global indices

687:   Collective

689:   Input Parameters:
690: + A     - matrix being preallocated
691: . ncoo  - number of entries
692: . coo_i - row indices
693: - coo_j - column indices

695:   Level: beginner

697:   Notes:
698:   The indices within `coo_i` and `coo_j` may be modified within this function. The caller should not rely on them
699:   having any specific value after this function returns. The arrays can be freed or reused immediately
700:   after this function returns.

702:   Entries can be repeated, see `MatSetValuesCOO()`. Entries with negative row or column indices are allowed
703:   but will be ignored. The corresponding entries in `MatSetValuesCOO()` will be ignored too. Remote entries
704:   are allowed and will be properly added or inserted to the matrix, unless the matrix option `MAT_IGNORE_OFF_PROC_ENTRIES`
705:   is set, in which case remote entries are ignored, or `MAT_NO_OFF_PROC_ENTRIES` is set, in which case an error will be generated.

707:   If you just want to create a sequential AIJ matrix (`MATSEQAIJ`), and your matrix entries in COO format are unique, you can also use
708:   `MatCreateSeqAIJFromTriple()`. But that is not recommended for iterative applications.

710: .seealso: [](ch_matrices), `Mat`, `MatSetValuesCOO()`, `MatSeqAIJSetPreallocation()`, `MatMPIAIJSetPreallocation()`, `MatSeqBAIJSetPreallocation()`,
711:           `MatMPIBAIJSetPreallocation()`, `MatSeqSBAIJSetPreallocation()`, `MatMPISBAIJSetPreallocation()`, `MatSetPreallocationCOOLocal()`,
712:           `DMSetMatrixPreallocateSkip()`, `MatCreateSeqAIJFromTriple()`
713: @*/
714: PetscErrorCode MatSetPreallocationCOO(Mat A, PetscCount ncoo, PetscInt coo_i[], PetscInt coo_j[])
715: {
716:   PetscErrorCode (*f)(Mat, PetscCount, PetscInt[], PetscInt[]) = NULL;

718:   PetscFunctionBegin;
721:   if (ncoo) PetscAssertPointer(coo_i, 3);
722:   if (ncoo) PetscAssertPointer(coo_j, 4);
723:   PetscCall(PetscLayoutSetUp(A->rmap));
724:   PetscCall(PetscLayoutSetUp(A->cmap));
725:   PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatSetPreallocationCOO_C", &f));

727:   PetscCall(PetscLogEventBegin(MAT_PreallCOO, A, 0, 0, 0));
728:   if (f) PetscCall((*f)(A, ncoo, coo_i, coo_j));
729:   else PetscCall(MatSetPreallocationCOO_Basic(A, ncoo, coo_i, coo_j)); /* allow fallback, very slow */
730:   PetscCall(PetscLogEventEnd(MAT_PreallCOO, A, 0, 0, 0));
731:   A->preallocated = PETSC_TRUE;
732:   A->nonzerostate++;
733:   PetscFunctionReturn(PETSC_SUCCESS);
734: }

736: /*@
737:   MatSetPreallocationCOOLocal - set preallocation for matrices using a coordinate format of the entries with local indices

739:   Collective

741:   Input Parameters:
742: + A     - matrix being preallocated
743: . ncoo  - number of entries
744: . coo_i - row indices (local numbering; may be modified)
745: - coo_j - column indices (local numbering; may be modified)

747:   Level: beginner

749:   Notes:
750:   The local indices are translated using the local to global mapping, thus `MatSetLocalToGlobalMapping()` must have been
751:   called prior to this function. For matrices created with `DMCreateMatrix()` the local to global mapping is often already provided.

753:   The indices `coo_i` and `coo_j` may be modified within this function. They might be translated to corresponding global
754:   indices, but the caller should not rely on them having any specific value after this function returns. The arrays
755:   can be freed or reused immediately after this function returns.

757:   Entries can be repeated, see `MatSetValuesCOO()`. Entries with negative row or column indices are allowed
758:   but will be ignored. The corresponding entries in `MatSetValuesCOO()` will be ignored too. Remote entries
759:   are allowed and will be properly added or inserted to the matrix.

761: .seealso: [](ch_matrices), `Mat`, `MatSetValuesCOO()`, `MatSeqAIJSetPreallocation()`, `MatMPIAIJSetPreallocation()`, `MatSeqBAIJSetPreallocation()`,
762:           `MatMPIBAIJSetPreallocation()`, `MatSeqSBAIJSetPreallocation()`, `MatMPISBAIJSetPreallocation()`, `MatSetPreallocationCOO()`,
763:           `DMSetMatrixPreallocateSkip()`
764: @*/
765: PetscErrorCode MatSetPreallocationCOOLocal(Mat A, PetscCount ncoo, PetscInt coo_i[], PetscInt coo_j[])
766: {
767:   PetscErrorCode (*f)(Mat, PetscCount, PetscInt[], PetscInt[]) = NULL;

769:   PetscFunctionBegin;
772:   if (ncoo) PetscAssertPointer(coo_i, 3);
773:   if (ncoo) PetscAssertPointer(coo_j, 4);
774:   PetscCall(PetscLayoutSetUp(A->rmap));
775:   PetscCall(PetscLayoutSetUp(A->cmap));

777:   PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatSetPreallocationCOOLocal_C", &f));
778:   if (f) {
779:     PetscCall((*f)(A, ncoo, coo_i, coo_j));
780:     A->nonzerostate++;
781:   } else {
782:     PetscInt               ncoo_i;
783:     ISLocalToGlobalMapping ltog_row, ltog_col;

785:     PetscCall(MatGetLocalToGlobalMapping(A, &ltog_row, &ltog_col));
786:     if (ltog_row) {
787:       PetscCall(PetscIntCast(ncoo, &ncoo_i));
788:       PetscCall(ISLocalToGlobalMappingApply(ltog_row, ncoo_i, coo_i, coo_i));
789:     }
790:     if (ltog_col) {
791:       PetscCall(PetscIntCast(ncoo, &ncoo_i));
792:       PetscCall(ISLocalToGlobalMappingApply(ltog_col, ncoo_i, coo_j, coo_j));
793:     }
794:     PetscCall(MatSetPreallocationCOO(A, ncoo, coo_i, coo_j));
795:   }
796:   A->preallocated = PETSC_TRUE;
797:   PetscFunctionReturn(PETSC_SUCCESS);
798: }

800: /*@
801:   MatSetValuesCOO - set values at once in a matrix preallocated using `MatSetPreallocationCOO()`

803:   Collective

805:   Input Parameters:
806: + A     - matrix being preallocated
807: . coo_v - the matrix values (can be `NULL`)
808: - imode - the insert mode

810:   Level: beginner

812:   Notes:
813:   The values must follow the order of the indices prescribed with `MatSetPreallocationCOO()` or `MatSetPreallocationCOOLocal()`.

815:   When repeated entries are specified in the COO indices the `coo_v` values are first properly summed, regardless of the value of imode.
816:   The imode flag indicates if coo_v must be added to the current values of the matrix (`ADD_VALUES`) or overwritten (`INSERT_VALUES`).

818:   `MatAssemblyBegin()` and `MatAssemblyEnd()` do not need to be called after this routine. It automatically handles the assembly process.

820:   If the `MAT_STRUCTURE_ONLY` option is set to true, the values are ignored and only the assembly process is performed.

822:   `MatSetValues()` and other operations may modify existing entries of the matrix between calls to this routine. If they change
823:   the nonzero pattern built by `MatSetPreallocationCOO()`, for example by inserting a new nonzero, this routine generates an error
824:   for `MATAIJ` and its device subtypes; call `MatSetPreallocationCOO()` again before calling `MatSetValuesCOO()`.

826: .seealso: [](ch_matrices), `Mat`, `MatSetPreallocationCOO()`, `MatSetPreallocationCOOLocal()`, `InsertMode`, `INSERT_VALUES`, `ADD_VALUES`
827: @*/
828: PetscErrorCode MatSetValuesCOO(Mat A, const PetscScalar coo_v[], InsertMode imode)
829: {
830:   PetscErrorCode (*f)(Mat, const PetscScalar[], InsertMode) = NULL;
831:   PetscBool nooffprocentries                                = PETSC_FALSE;

833:   PetscFunctionBegin;
836:   MatCheckPreallocated(A, 1);
838:   PetscCall(PetscObjectQueryFunction((PetscObject)A, "MatSetValuesCOO_C", &f));
839:   PetscCall(PetscLogEventBegin(MAT_SetVCOO, A, 0, 0, 0));
840:   if (f) {
841:     if (!A->structure_only) PetscCall((*f)(A, coo_v, imode)); // all known COO implementations do not use MatStash. They do their own off-proc communication
842:     nooffprocentries    = A->nooffprocentries;
843:     A->nooffprocentries = PETSC_TRUE;
844:   } else if (!A->structure_only) PetscCall(MatSetValuesCOO_Basic(A, coo_v, imode)); // MatSetValues() may use MatStash
845:   PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
846:   PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
847:   if (f) A->nooffprocentries = nooffprocentries;
848:   PetscCall(PetscLogEventEnd(MAT_SetVCOO, A, 0, 0, 0));
849:   PetscFunctionReturn(PETSC_SUCCESS);
850: }

852: /*@
853:   MatSetBindingPropagates - Sets whether the state of being bound to the CPU for a GPU matrix type propagates to child and some other associated objects

855:   Input Parameters:
856: + A   - the matrix
857: - flg - flag indicating whether the boundtocpu flag should be propagated

859:   Level: developer

861:   Notes:
862:   If the value of flg is set to true, the following will occur
863: +   `MatCreateSubMatrices()` and `MatCreateRedundantMatrix()` - bind created matrices to CPU if the input matrix is bound to the CPU.
864: -   `MatCreateVecs()` - bind created vectors to CPU if the input matrix is bound to the CPU.

866:   The bindingpropagates flag itself is also propagated by the above routines.

868:   Developer Notes:
869:   If the fine-scale `DMDA` has the `-dm_bind_below` option set to true, then `DMCreateInterpolationScale()` calls `MatSetBindingPropagates()`
870:   on the restriction/interpolation operator to set the bindingpropagates flag to true.

872: .seealso: [](ch_matrices), `Mat`, `VecSetBindingPropagates()`, `MatGetBindingPropagates()`
873: @*/
874: PetscErrorCode MatSetBindingPropagates(Mat A, PetscBool flg)
875: {
876:   PetscFunctionBegin;
878: #if PetscDefined(HAVE_VIENNACL) || PetscDefined(HAVE_CUDA) || PetscDefined(HAVE_HIP)
879:   A->bindingpropagates = flg;
880: #endif
881:   PetscFunctionReturn(PETSC_SUCCESS);
882: }

884: /*@
885:   MatGetBindingPropagates - Gets whether the state of being bound to the CPU for a GPU matrix type propagates to child and some other associated objects

887:   Input Parameter:
888: . A - the matrix

890:   Output Parameter:
891: . flg - flag indicating whether the boundtocpu flag will be propagated

893:   Level: developer

895: .seealso: [](ch_matrices), `Mat`, `MatSetBindingPropagates()`
896: @*/
897: PetscErrorCode MatGetBindingPropagates(Mat A, PetscBool *flg)
898: {
899:   PetscFunctionBegin;
901:   PetscAssertPointer(flg, 2);
902: #if PetscDefined(HAVE_VIENNACL) || PetscDefined(HAVE_CUDA) || PetscDefined(HAVE_HIP)
903:   *flg = A->bindingpropagates;
904: #else
905:   *flg = PETSC_FALSE;
906: #endif
907:   PetscFunctionReturn(PETSC_SUCCESS);
908: }