xref: /petsc/src/mat/impls/dummy/matdummy.c (revision 2d30e087755efd99e28fdfe792ffbeb2ee1ea928)
1 #include <petsc/private/matimpl.h> /*I "petscmat.h" I*/
2 #include <../src/mat/impls/aij/seq/aij.h>
3 
4 PetscErrorCode MatDestroySubMatrix_Dummy(Mat C) {
5   Mat_SubSppt *submatj = (Mat_SubSppt *)C->data;
6 
7   PetscFunctionBegin;
8   PetscCall(submatj->destroy(C));
9   PetscCall(MatDestroySubMatrix_Private(submatj));
10   PetscFunctionReturn(0);
11 }
12 
13 PetscErrorCode MatDestroySubMatrices_Dummy(PetscInt n, Mat *mat[]) {
14   PetscFunctionBegin;
15   /* Destroy dummy submatrices (*mat)[n]...(*mat)[n+nstages-1] used for reuse struct Mat_SubSppt */
16   if ((*mat)[n]) {
17     PetscBool isdummy;
18     PetscCall(PetscObjectTypeCompare((PetscObject)(*mat)[n], MATDUMMY, &isdummy));
19     if (isdummy) {
20       Mat_SubSppt *smat = (Mat_SubSppt *)((*mat)[n]->data); /* singleis and nstages are saved in (*mat)[n]->data */
21 
22       if (smat && !smat->singleis) {
23         PetscInt i, nstages = smat->nstages;
24         for (i = 0; i < nstages; i++) PetscCall(MatDestroy(&(*mat)[n + i]));
25       }
26     }
27   }
28 
29   /* memory is allocated even if n = 0 */
30   PetscCall(PetscFree(*mat));
31   PetscFunctionReturn(0);
32 }
33 
34 PetscErrorCode MatDestroy_Dummy(Mat A) {
35   PetscFunctionBegin;
36   PetscCall(PetscObjectChangeTypeName((PetscObject)A, NULL));
37   PetscFunctionReturn(0);
38 }
39 
40 /*MC
41    MATDUMMY - A matrix type to be used for reusing specific internal data structure.
42 
43   Level: developer
44 
45 .seealso: `Mat`
46 M*/
47 
48 PETSC_EXTERN PetscErrorCode MatCreate_Dummy(Mat A) {
49   PetscFunctionBegin;
50   /* matrix ops */
51   PetscCall(PetscMemzero(A->ops, sizeof(struct _MatOps)));
52   A->ops->destroy            = MatDestroy_Dummy;
53   A->ops->destroysubmatrices = MatDestroySubMatrices_Dummy;
54 
55   /* special MATPREALLOCATOR functions */
56   PetscCall(PetscObjectChangeTypeName((PetscObject)A, MATDUMMY));
57   PetscFunctionReturn(0);
58 }
59