Actual source code: matnest.c
1: #include <../src/mat/impls/nest/matnestimpl.h>
2: #include <../src/mat/impls/aij/seq/aij.h>
3: #include <../src/mat/impls/shell/shell.h>
4: #include <petscsf.h>
6: static PetscErrorCode MatSetUp_NestIS_Private(Mat, PetscInt, const IS[], PetscInt, const IS[]);
7: static PetscErrorCode MatCreateVecs_Nest(Mat, Vec *, Vec *);
8: static PetscErrorCode MatReset_Nest(Mat);
10: PETSC_INTERN PetscErrorCode MatConvert_Nest_IS(Mat, MatType, MatReuse, Mat *);
12: /* private functions */
13: static PetscErrorCode MatNestGetSizes_Private(Mat A, PetscInt *m, PetscInt *n, PetscInt *M, PetscInt *N)
14: {
15: Mat_Nest *bA = (Mat_Nest *)A->data;
17: PetscFunctionBegin;
18: *m = *n = *M = *N = 0;
19: for (PetscInt i = 0; i < bA->nr; i++) { /* rows */
20: PetscInt sm, sM;
22: PetscCall(ISGetLocalSize(bA->isglobal.row[i], &sm));
23: PetscCall(ISGetSize(bA->isglobal.row[i], &sM));
24: *m += sm;
25: *M += sM;
26: }
27: for (PetscInt j = 0; j < bA->nc; j++) { /* cols */
28: PetscInt sn, sN;
30: PetscCall(ISGetLocalSize(bA->isglobal.col[j], &sn));
31: PetscCall(ISGetSize(bA->isglobal.col[j], &sN));
32: *n += sn;
33: *N += sN;
34: }
35: PetscFunctionReturn(PETSC_SUCCESS);
36: }
38: /* operations */
39: static PetscErrorCode MatMult_Nest(Mat A, Vec x, Vec y)
40: {
41: Mat_Nest *bA = (Mat_Nest *)A->data;
42: Vec *bx = bA->right, *by = bA->left;
44: PetscFunctionBegin;
45: for (PetscInt i = 0; i < bA->nr; i++) PetscCall(VecGetSubVector(y, bA->isglobal.row[i], &by[i]));
46: for (PetscInt i = 0; i < bA->nc; i++) PetscCall(VecGetSubVector(x, bA->isglobal.col[i], &bx[i]));
47: for (PetscInt i = 0; i < bA->nr; i++) {
48: PetscCall(VecZeroEntries(by[i]));
49: for (PetscInt j = 0; j < bA->nc; j++) {
50: if (!bA->m[i][j]) continue;
51: /* y[i] <- y[i] + A[i][j] * x[j] */
52: PetscCall(MatMultAdd(bA->m[i][j], bx[j], by[i], by[i]));
53: }
54: }
55: for (PetscInt i = 0; i < bA->nr; i++) PetscCall(VecRestoreSubVector(y, bA->isglobal.row[i], &by[i]));
56: for (PetscInt i = 0; i < bA->nc; i++) PetscCall(VecRestoreSubVector(x, bA->isglobal.col[i], &bx[i]));
57: PetscFunctionReturn(PETSC_SUCCESS);
58: }
60: static PetscErrorCode MatMultAdd_Nest(Mat A, Vec x, Vec y, Vec z)
61: {
62: Mat_Nest *bA = (Mat_Nest *)A->data;
63: Vec *bx = bA->right, *bz = bA->left;
65: PetscFunctionBegin;
66: for (PetscInt i = 0; i < bA->nr; i++) PetscCall(VecGetSubVector(z, bA->isglobal.row[i], &bz[i]));
67: for (PetscInt i = 0; i < bA->nc; i++) PetscCall(VecGetSubVector(x, bA->isglobal.col[i], &bx[i]));
68: for (PetscInt i = 0; i < bA->nr; i++) {
69: if (y != z) {
70: Vec by;
71: PetscCall(VecGetSubVector(y, bA->isglobal.row[i], &by));
72: PetscCall(VecCopy(by, bz[i]));
73: PetscCall(VecRestoreSubVector(y, bA->isglobal.row[i], &by));
74: }
75: for (PetscInt j = 0; j < bA->nc; j++) {
76: if (!bA->m[i][j]) continue;
77: /* y[i] <- y[i] + A[i][j] * x[j] */
78: PetscCall(MatMultAdd(bA->m[i][j], bx[j], bz[i], bz[i]));
79: }
80: }
81: for (PetscInt i = 0; i < bA->nr; i++) PetscCall(VecRestoreSubVector(z, bA->isglobal.row[i], &bz[i]));
82: for (PetscInt i = 0; i < bA->nc; i++) PetscCall(VecRestoreSubVector(x, bA->isglobal.col[i], &bx[i]));
83: PetscFunctionReturn(PETSC_SUCCESS);
84: }
86: typedef struct {
87: Mat *workC; /* array of Mat with specific containers depending on the underlying MatMatMult implementation */
88: PetscScalar *tarray; /* buffer for storing all temporary products A[i][j] B[j] */
89: PetscInt *dm, *dn, k; /* displacements and number of submatrices */
90: } Nest_Dense;
92: static PetscErrorCode MatProductNumeric_Nest_Dense(Mat C)
93: {
94: Mat_Nest *bA;
95: Nest_Dense *contents;
96: Mat viewB, viewC, productB, workC;
97: const PetscScalar *barray;
98: PetscScalar *carray;
99: PetscInt M, N, nr, nc, ldb, ldc;
100: Mat A, B;
102: PetscFunctionBegin;
103: MatCheckProduct(C, 1);
104: A = C->product->A;
105: B = C->product->B;
106: PetscCall(MatGetSize(B, NULL, &N));
107: if (!N) PetscFunctionReturn(PETSC_SUCCESS);
108: contents = (Nest_Dense *)C->product->data;
109: PetscCheck(contents, PetscObjectComm((PetscObject)C), PETSC_ERR_PLIB, "Product data empty");
110: bA = (Mat_Nest *)A->data;
111: nr = bA->nr;
112: nc = bA->nc;
113: PetscCall(MatDenseGetLDA(B, &ldb));
114: PetscCall(MatDenseGetLDA(C, &ldc));
115: PetscCall(MatZeroEntries(C));
116: PetscCall(MatDenseGetArrayRead(B, &barray));
117: PetscCall(MatDenseGetArray(C, &carray));
118: for (PetscInt i = 0; i < nr; i++) {
119: PetscCall(ISGetSize(bA->isglobal.row[i], &M));
120: PetscCall(MatCreateDense(PetscObjectComm((PetscObject)A), contents->dm[i + 1] - contents->dm[i], PETSC_DECIDE, M, N, PetscSafePointerPlusOffset(carray, contents->dm[i]), &viewC));
121: PetscCall(MatDenseSetLDA(viewC, ldc));
122: for (PetscInt j = 0; j < nc; j++) {
123: if (!bA->m[i][j]) continue;
124: PetscCall(ISGetSize(bA->isglobal.col[j], &M));
125: PetscCall(MatCreateDense(PetscObjectComm((PetscObject)A), contents->dn[j + 1] - contents->dn[j], PETSC_DECIDE, M, N, PetscSafePointerPlusOffset((PetscScalar *)barray, contents->dn[j]), &viewB));
126: PetscCall(MatDenseSetLDA(viewB, ldb));
128: /* MatMatMultNumeric(bA->m[i][j],viewB,contents->workC[i*nc + j]); */
129: workC = contents->workC[i * nc + j];
130: productB = workC->product->B;
131: workC->product->B = viewB; /* use newly created dense matrix viewB */
132: PetscCall(MatProductNumeric(workC));
133: PetscCall(MatDestroy(&viewB));
134: workC->product->B = productB; /* resume original B */
136: /* C[i] <- workC + C[i] */
137: PetscCall(MatAXPY(viewC, 1.0, contents->workC[i * nc + j], SAME_NONZERO_PATTERN));
138: }
139: PetscCall(MatDestroy(&viewC));
140: }
141: PetscCall(MatDenseRestoreArray(C, &carray));
142: PetscCall(MatDenseRestoreArrayRead(B, &barray));
143: PetscFunctionReturn(PETSC_SUCCESS);
144: }
146: static PetscErrorCode MatNest_DenseDestroy(PetscCtxRt ctx)
147: {
148: Nest_Dense *contents = *(Nest_Dense **)ctx;
150: PetscFunctionBegin;
151: PetscCall(PetscFree(contents->tarray));
152: for (PetscInt i = 0; i < contents->k; i++) PetscCall(MatDestroy(contents->workC + i));
153: PetscCall(PetscFree3(contents->dm, contents->dn, contents->workC));
154: PetscCall(PetscFree(contents));
155: PetscFunctionReturn(PETSC_SUCCESS);
156: }
158: static PetscErrorCode MatProductSymbolic_Nest_Dense(Mat C)
159: {
160: Mat_Nest *bA;
161: Mat viewB, workC;
162: const PetscScalar *barray;
163: PetscInt M, N, nr, nc, maxm = 0, ldb;
164: Nest_Dense *contents = NULL;
165: Mat A, B;
166: PetscReal fill;
168: PetscFunctionBegin;
169: MatCheckProduct(C, 1);
170: PetscCheck(!C->product->data, PetscObjectComm((PetscObject)C), PETSC_ERR_PLIB, "Product data not empty");
171: A = C->product->A;
172: B = C->product->B;
173: fill = C->product->fill;
174: bA = (Mat_Nest *)A->data;
175: nr = bA->nr;
176: nc = bA->nc;
177: PetscCall(MatDenseSetUpProduct_Private(C, A->rmap, B->cmap, B, NULL));
178: PetscCall(MatGetSize(B, NULL, &N));
179: if (!N) {
180: C->ops->productnumeric = MatProductNumeric_Nest_Dense;
181: PetscFunctionReturn(PETSC_SUCCESS);
182: }
184: PetscCall(PetscNew(&contents));
185: C->product->data = contents;
186: C->product->destroy = MatNest_DenseDestroy;
187: PetscCall(PetscCalloc3(nr + 1, &contents->dm, nc + 1, &contents->dn, nr * nc, &contents->workC));
188: contents->k = nr * nc;
189: for (PetscInt i = 0; i < nr; i++) {
190: PetscCall(ISGetLocalSize(bA->isglobal.row[i], contents->dm + i + 1));
191: maxm = PetscMax(maxm, contents->dm[i + 1]);
192: contents->dm[i + 1] += contents->dm[i];
193: }
194: for (PetscInt i = 0; i < nc; i++) {
195: PetscCall(ISGetLocalSize(bA->isglobal.col[i], contents->dn + i + 1));
196: contents->dn[i + 1] += contents->dn[i];
197: }
198: PetscCall(PetscMalloc1(maxm * N, &contents->tarray));
199: PetscCall(MatDenseGetLDA(B, &ldb));
200: PetscCall(MatGetSize(B, NULL, &N));
201: PetscCall(MatDenseGetArrayRead(B, &barray));
202: /* loops are permuted compared to MatMatMultNumeric so that viewB is created only once per column of A */
203: for (PetscInt j = 0; j < nc; j++) {
204: PetscCall(ISGetSize(bA->isglobal.col[j], &M));
205: PetscCall(MatCreateDense(PetscObjectComm((PetscObject)A), contents->dn[j + 1] - contents->dn[j], PETSC_DECIDE, M, N, PetscSafePointerPlusOffset((PetscScalar *)barray, contents->dn[j]), &viewB));
206: PetscCall(MatDenseSetLDA(viewB, ldb));
207: for (PetscInt i = 0; i < nr; i++) {
208: if (!bA->m[i][j]) continue;
209: /* MatMatMultSymbolic may attach a specific container (depending on MatType of bA->m[i][j]) to workC[i][j] */
211: PetscCall(MatProductCreate(bA->m[i][j], viewB, NULL, &contents->workC[i * nc + j]));
212: workC = contents->workC[i * nc + j];
213: PetscCall(MatProductSetType(workC, MATPRODUCT_AB));
214: PetscCall(MatProductSetAlgorithm(workC, "default"));
215: PetscCall(MatProductSetFill(workC, fill));
216: PetscCall(MatProductSetFromOptions(workC));
217: PetscCall(MatProductSymbolic(workC));
219: /* since tarray will be shared by all Mat */
220: PetscCall(MatSeqDenseSetPreallocation(workC, contents->tarray));
221: PetscCall(MatMPIDenseSetPreallocation(workC, contents->tarray));
222: }
223: PetscCall(MatDestroy(&viewB));
224: }
225: PetscCall(MatDenseRestoreArrayRead(B, &barray));
227: C->ops->productnumeric = MatProductNumeric_Nest_Dense;
228: PetscFunctionReturn(PETSC_SUCCESS);
229: }
231: static PetscErrorCode MatProductSetFromOptions_Nest_Dense(Mat C)
232: {
233: Mat_Product *product = C->product;
235: PetscFunctionBegin;
236: if (product->type == MATPRODUCT_AB) C->ops->productsymbolic = MatProductSymbolic_Nest_Dense;
237: PetscFunctionReturn(PETSC_SUCCESS);
238: }
240: static PetscErrorCode MatMultTransposeKernel_Nest(Mat A, Vec x, Vec y, PetscBool herm)
241: {
242: Mat_Nest *bA = (Mat_Nest *)A->data;
243: Vec *bx = bA->left, *by = bA->right;
245: PetscFunctionBegin;
246: for (PetscInt i = 0; i < bA->nr; i++) PetscCall(VecGetSubVector(x, bA->isglobal.row[i], &bx[i]));
247: for (PetscInt i = 0; i < bA->nc; i++) PetscCall(VecGetSubVector(y, bA->isglobal.col[i], &by[i]));
248: for (PetscInt j = 0; j < bA->nc; j++) {
249: PetscCall(VecZeroEntries(by[j]));
250: for (PetscInt i = 0; i < bA->nr; i++) {
251: if (!bA->m[i][j]) continue;
252: if (herm) PetscCall(MatMultHermitianTransposeAdd(bA->m[i][j], bx[i], by[j], by[j])); /* y[j] <- y[j] + (A[i][j])^H * x[i] */
253: else PetscCall(MatMultTransposeAdd(bA->m[i][j], bx[i], by[j], by[j])); /* y[j] <- y[j] + (A[i][j])^T * x[i] */
254: }
255: }
256: for (PetscInt i = 0; i < bA->nr; i++) PetscCall(VecRestoreSubVector(x, bA->isglobal.row[i], &bx[i]));
257: for (PetscInt i = 0; i < bA->nc; i++) PetscCall(VecRestoreSubVector(y, bA->isglobal.col[i], &by[i]));
258: PetscFunctionReturn(PETSC_SUCCESS);
259: }
261: static PetscErrorCode MatMultTranspose_Nest(Mat A, Vec x, Vec y)
262: {
263: PetscFunctionBegin;
264: PetscCall(MatMultTransposeKernel_Nest(A, x, y, PETSC_FALSE));
265: PetscFunctionReturn(PETSC_SUCCESS);
266: }
268: static PetscErrorCode MatMultHermitianTranspose_Nest(Mat A, Vec x, Vec y)
269: {
270: PetscFunctionBegin;
271: PetscCall(MatMultTransposeKernel_Nest(A, x, y, PETSC_TRUE));
272: PetscFunctionReturn(PETSC_SUCCESS);
273: }
275: static PetscErrorCode MatMultTransposeAddKernel_Nest(Mat A, Vec x, Vec y, Vec z, PetscBool herm)
276: {
277: Mat_Nest *bA = (Mat_Nest *)A->data;
278: Vec *bx = bA->left, *bz = bA->right;
280: PetscFunctionBegin;
281: for (PetscInt i = 0; i < bA->nr; i++) PetscCall(VecGetSubVector(x, bA->isglobal.row[i], &bx[i]));
282: for (PetscInt i = 0; i < bA->nc; i++) PetscCall(VecGetSubVector(z, bA->isglobal.col[i], &bz[i]));
283: for (PetscInt j = 0; j < bA->nc; j++) {
284: if (y != z) {
285: Vec by;
287: PetscCall(VecGetSubVector(y, bA->isglobal.col[j], &by));
288: PetscCall(VecCopy(by, bz[j]));
289: PetscCall(VecRestoreSubVector(y, bA->isglobal.col[j], &by));
290: }
291: for (PetscInt i = 0; i < bA->nr; i++) {
292: if (!bA->m[i][j]) continue;
293: if (herm) PetscCall(MatMultHermitianTransposeAdd(bA->m[i][j], bx[i], bz[j], bz[j])); /* z[j] <- y[j] + (A[i][j])^H * x[i] */
294: else PetscCall(MatMultTransposeAdd(bA->m[i][j], bx[i], bz[j], bz[j])); /* z[j] <- y[j] + (A[i][j])^T * x[i] */
295: }
296: }
297: for (PetscInt i = 0; i < bA->nr; i++) PetscCall(VecRestoreSubVector(x, bA->isglobal.row[i], &bx[i]));
298: for (PetscInt i = 0; i < bA->nc; i++) PetscCall(VecRestoreSubVector(z, bA->isglobal.col[i], &bz[i]));
299: PetscFunctionReturn(PETSC_SUCCESS);
300: }
302: static PetscErrorCode MatMultTransposeAdd_Nest(Mat A, Vec x, Vec y, Vec z)
303: {
304: PetscFunctionBegin;
305: PetscCall(MatMultTransposeAddKernel_Nest(A, x, y, z, PETSC_FALSE));
306: PetscFunctionReturn(PETSC_SUCCESS);
307: }
309: static PetscErrorCode MatMultHermitianTransposeAdd_Nest(Mat A, Vec x, Vec y, Vec z)
310: {
311: PetscFunctionBegin;
312: PetscCall(MatMultTransposeAddKernel_Nest(A, x, y, z, PETSC_TRUE));
313: PetscFunctionReturn(PETSC_SUCCESS);
314: }
316: static PetscErrorCode MatTranspose_Nest(Mat A, MatReuse reuse, Mat *B)
317: {
318: Mat_Nest *bA = (Mat_Nest *)A->data, *bC;
319: Mat C;
320: PetscInt i, j, nr = bA->nr, nc = bA->nc;
322: PetscFunctionBegin;
323: if (reuse == MAT_REUSE_MATRIX) PetscCall(MatTransposeCheckNonzeroState_Private(A, *B));
324: PetscCheck(reuse != MAT_INPLACE_MATRIX || nr == nc, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_SIZ, "Square nested matrix only for in-place");
326: if (reuse == MAT_INITIAL_MATRIX || reuse == MAT_INPLACE_MATRIX) {
327: Mat *subs;
328: IS *is_row, *is_col;
330: PetscCall(PetscCalloc1(nr * nc, &subs));
331: PetscCall(PetscMalloc2(nr, &is_row, nc, &is_col));
332: PetscCall(MatNestGetISs(A, is_row, is_col));
333: if (reuse == MAT_INPLACE_MATRIX) {
334: for (i = 0; i < nr; i++) {
335: for (j = 0; j < nc; j++) subs[i + nr * j] = bA->m[i][j];
336: }
337: }
339: PetscCall(MatCreateNest(PetscObjectComm((PetscObject)A), nc, is_col, nr, is_row, subs, &C));
340: PetscCall(PetscFree(subs));
341: PetscCall(PetscFree2(is_row, is_col));
342: } else {
343: C = *B;
344: }
346: bC = (Mat_Nest *)C->data;
347: for (i = 0; i < nr; i++) {
348: for (j = 0; j < nc; j++) {
349: if (bA->m[i][j]) {
350: PetscCall(MatTranspose(bA->m[i][j], reuse, &bC->m[j][i]));
351: } else {
352: bC->m[j][i] = NULL;
353: }
354: }
355: }
357: if (reuse == MAT_INITIAL_MATRIX || reuse == MAT_REUSE_MATRIX) {
358: *B = C;
359: } else {
360: PetscCall(MatHeaderMerge(A, &C));
361: }
362: PetscFunctionReturn(PETSC_SUCCESS);
363: }
365: static PetscErrorCode MatNestDestroyISList(PetscInt n, IS **list)
366: {
367: IS *lst = *list;
369: PetscFunctionBegin;
370: if (!lst) PetscFunctionReturn(PETSC_SUCCESS);
371: for (PetscInt i = 0; i < n; i++) PetscCall(ISDestroy(&lst[i]));
372: PetscCall(PetscFree(lst));
373: *list = NULL;
374: PetscFunctionReturn(PETSC_SUCCESS);
375: }
377: static PetscErrorCode MatReset_Nest(Mat A)
378: {
379: Mat_Nest *vs = (Mat_Nest *)A->data;
381: PetscFunctionBegin;
382: /* release the matrices and the place holders */
383: PetscCall(MatNestDestroyISList(vs->nr, &vs->isglobal.row));
384: PetscCall(MatNestDestroyISList(vs->nc, &vs->isglobal.col));
385: PetscCall(MatNestDestroyISList(vs->nr, &vs->islocal.row));
386: PetscCall(MatNestDestroyISList(vs->nc, &vs->islocal.col));
388: PetscCall(PetscFree(vs->row_len));
389: PetscCall(PetscFree(vs->col_len));
390: PetscCall(PetscFree(vs->nnzstate));
392: PetscCall(PetscFree2(vs->left, vs->right));
394: /* release the matrices and the place holders */
395: if (vs->m) {
396: for (PetscInt i = 0; i < vs->nr; i++) {
397: for (PetscInt j = 0; j < vs->nc; j++) PetscCall(MatDestroy(&vs->m[i][j]));
398: }
399: PetscCall(PetscFree(vs->m[0]));
400: PetscCall(PetscFree(vs->m));
401: }
403: /* restore defaults */
404: vs->nr = 0;
405: vs->nc = 0;
406: vs->splitassembly = PETSC_FALSE;
407: PetscFunctionReturn(PETSC_SUCCESS);
408: }
410: static PetscErrorCode MatDestroy_Nest(Mat A)
411: {
412: PetscFunctionBegin;
413: PetscCall(MatReset_Nest(A));
414: PetscCall(PetscFree(A->data));
415: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatNestGetSubMat_C", NULL));
416: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatNestSetSubMat_C", NULL));
417: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatNestGetSubMats_C", NULL));
418: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatNestGetSize_C", NULL));
419: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatNestGetISs_C", NULL));
420: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatNestGetLocalISs_C", NULL));
421: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatNestSetVecType_C", NULL));
422: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatNestSetSubMats_C", NULL));
423: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_nest_mpiaij_C", NULL));
424: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_nest_seqaij_C", NULL));
425: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_nest_aij_C", NULL));
426: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_nest_is_C", NULL));
427: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_nest_mpidense_C", NULL));
428: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_nest_seqdense_C", NULL));
429: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatProductSetFromOptions_nest_seqdense_C", NULL));
430: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatProductSetFromOptions_nest_mpidense_C", NULL));
431: PetscFunctionReturn(PETSC_SUCCESS);
432: }
434: static PetscErrorCode MatAssemblyBegin_Nest(Mat A, MatAssemblyType type)
435: {
436: Mat_Nest *vs = (Mat_Nest *)A->data;
437: PetscBool nnzstate = PETSC_FALSE;
439: PetscFunctionBegin;
440: for (PetscInt i = 0; i < vs->nr; i++) {
441: for (PetscInt j = 0; j < vs->nc; j++) {
442: PetscObjectState subnnzstate = 0;
443: if (vs->m[i][j]) {
444: PetscCall(MatAssemblyBegin(vs->m[i][j], type));
445: if (!vs->splitassembly) {
446: /* Note: split assembly will fail if the same block appears more than once (even indirectly through a nested
447: * sub-block). This could be fixed by adding a flag to Mat so that there was a way to check if a Mat was
448: * already performing an assembly, but the result would by more complicated and appears to offer less
449: * potential for diagnostics and correctness checking. Split assembly should be fixed once there is an
450: * interface for libraries to make asynchronous progress in "user-defined non-blocking collectives".
451: */
452: PetscCall(MatAssemblyEnd(vs->m[i][j], type));
453: PetscCall(MatGetNonzeroState(vs->m[i][j], &subnnzstate));
454: }
455: }
456: nnzstate = (PetscBool)(nnzstate || vs->nnzstate[i * vs->nc + j] != subnnzstate);
457: vs->nnzstate[i * vs->nc + j] = subnnzstate;
458: }
459: }
460: if (nnzstate) A->nonzerostate++;
461: PetscFunctionReturn(PETSC_SUCCESS);
462: }
464: static PetscErrorCode MatAssemblyEnd_Nest(Mat A, MatAssemblyType type)
465: {
466: Mat_Nest *vs = (Mat_Nest *)A->data;
468: PetscFunctionBegin;
469: for (PetscInt i = 0; i < vs->nr; i++) {
470: for (PetscInt j = 0; j < vs->nc; j++) {
471: if (vs->m[i][j]) {
472: if (vs->splitassembly) PetscCall(MatAssemblyEnd(vs->m[i][j], type));
473: }
474: }
475: }
476: PetscFunctionReturn(PETSC_SUCCESS);
477: }
479: static PetscErrorCode MatNestFindNonzeroSubMatRow(Mat A, PetscInt row, Mat *B)
480: {
481: Mat_Nest *vs = (Mat_Nest *)A->data;
482: Mat sub;
484: PetscFunctionBegin;
485: sub = (row < vs->nc) ? vs->m[row][row] : (Mat)NULL; /* Prefer to find on the diagonal */
486: for (PetscInt j = 0; !sub && j < vs->nc; j++) sub = vs->m[row][j];
487: if (sub) PetscCall(MatSetUp(sub)); /* Ensure that the sizes are available */
488: *B = sub;
489: PetscFunctionReturn(PETSC_SUCCESS);
490: }
492: static PetscErrorCode MatNestFindNonzeroSubMatCol(Mat A, PetscInt col, Mat *B)
493: {
494: Mat_Nest *vs = (Mat_Nest *)A->data;
495: Mat sub;
497: PetscFunctionBegin;
498: sub = (col < vs->nr) ? vs->m[col][col] : (Mat)NULL; /* Prefer to find on the diagonal */
499: for (PetscInt i = 0; !sub && i < vs->nr; i++) sub = vs->m[i][col];
500: if (sub) PetscCall(MatSetUp(sub)); /* Ensure that the sizes are available */
501: *B = sub;
502: PetscFunctionReturn(PETSC_SUCCESS);
503: }
505: static PetscErrorCode MatNestFindISRange(Mat A, PetscInt n, const IS list[], IS is, PetscInt *begin, PetscInt *end)
506: {
507: PetscInt i, j, size, m;
508: PetscBool flg;
509: IS out, concatenate[2];
511: PetscFunctionBegin;
512: PetscAssertPointer(list, 3);
514: if (begin) {
515: PetscAssertPointer(begin, 5);
516: *begin = -1;
517: }
518: if (end) {
519: PetscAssertPointer(end, 6);
520: *end = -1;
521: }
522: for (i = 0; i < n; i++) {
523: if (!list[i]) continue;
524: PetscCall(ISEqualUnsorted(list[i], is, &flg));
525: if (flg) {
526: if (begin) *begin = i;
527: if (end) *end = i + 1;
528: PetscFunctionReturn(PETSC_SUCCESS);
529: }
530: }
531: PetscCall(ISGetSize(is, &size));
532: for (i = 0; i < n - 1; i++) {
533: if (!list[i]) continue;
534: m = 0;
535: PetscCall(ISConcatenate(PetscObjectComm((PetscObject)A), 2, list + i, &out));
536: PetscCall(ISGetSize(out, &m));
537: for (j = i + 2; j < n && m < size; j++) {
538: if (list[j]) {
539: concatenate[0] = out;
540: concatenate[1] = list[j];
541: PetscCall(ISConcatenate(PetscObjectComm((PetscObject)A), 2, concatenate, &out));
542: PetscCall(ISDestroy(concatenate));
543: PetscCall(ISGetSize(out, &m));
544: }
545: }
546: if (m == size) {
547: PetscCall(ISEqualUnsorted(out, is, &flg));
548: if (flg) {
549: if (begin) *begin = i;
550: if (end) *end = j;
551: PetscCall(ISDestroy(&out));
552: PetscFunctionReturn(PETSC_SUCCESS);
553: }
554: }
555: PetscCall(ISDestroy(&out));
556: }
557: PetscFunctionReturn(PETSC_SUCCESS);
558: }
560: static PetscErrorCode MatNestFillEmptyMat_Private(Mat A, PetscInt i, PetscInt j, Mat *B)
561: {
562: Mat_Nest *vs = (Mat_Nest *)A->data;
563: PetscInt lr, lc;
565: PetscFunctionBegin;
566: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), B));
567: PetscCall(ISGetLocalSize(vs->isglobal.row[i], &lr));
568: PetscCall(ISGetLocalSize(vs->isglobal.col[j], &lc));
569: PetscCall(MatSetSizes(*B, lr, lc, PETSC_DECIDE, PETSC_DECIDE));
570: PetscCall(MatSetType(*B, MATAIJ));
571: PetscCall(MatSeqAIJSetPreallocation(*B, 0, NULL));
572: PetscCall(MatMPIAIJSetPreallocation(*B, 0, NULL, 0, NULL));
573: PetscCall(MatSetUp(*B));
574: PetscCall(MatSetOption(*B, MAT_NO_OFF_PROC_ENTRIES, PETSC_TRUE));
575: PetscCall(MatAssemblyBegin(*B, MAT_FINAL_ASSEMBLY));
576: PetscCall(MatAssemblyEnd(*B, MAT_FINAL_ASSEMBLY));
577: PetscFunctionReturn(PETSC_SUCCESS);
578: }
580: static PetscErrorCode MatNestGetBlock_Private(Mat A, PetscInt rbegin, PetscInt rend, PetscInt cbegin, PetscInt cend, Mat *B)
581: {
582: Mat_Nest *vs = (Mat_Nest *)A->data;
583: Mat *a;
584: PetscInt i, j, k, l, nr = rend - rbegin, nc = cend - cbegin;
585: char keyname[256];
586: PetscBool *b;
587: PetscBool flg;
589: PetscFunctionBegin;
590: *B = NULL;
591: PetscCall(PetscSNPrintf(keyname, sizeof(keyname), "NestBlock_%" PetscInt_FMT "-%" PetscInt_FMT "x%" PetscInt_FMT "-%" PetscInt_FMT, rbegin, rend, cbegin, cend));
592: PetscCall(PetscObjectQuery((PetscObject)A, keyname, (PetscObject *)B));
593: if (*B) PetscFunctionReturn(PETSC_SUCCESS);
595: PetscCall(PetscMalloc2(nr * nc, &a, nr * nc, &b));
596: for (i = 0; i < nr; i++) {
597: for (j = 0; j < nc; j++) {
598: a[i * nc + j] = vs->m[rbegin + i][cbegin + j];
599: b[i * nc + j] = PETSC_FALSE;
600: }
601: }
602: if (nc != vs->nc && nr != vs->nr) {
603: for (i = 0; i < nr; i++) {
604: for (j = 0; j < nc; j++) {
605: flg = PETSC_FALSE;
606: for (k = 0; (k < nr && !flg); k++) {
607: if (a[j + k * nc]) flg = PETSC_TRUE;
608: }
609: if (flg) {
610: flg = PETSC_FALSE;
611: for (l = 0; (l < nc && !flg); l++) {
612: if (a[i * nc + l]) flg = PETSC_TRUE;
613: }
614: }
615: if (!flg) {
616: b[i * nc + j] = PETSC_TRUE;
617: PetscCall(MatNestFillEmptyMat_Private(A, rbegin + i, cbegin + j, a + i * nc + j));
618: }
619: }
620: }
621: }
622: PetscCall(MatCreateNest(PetscObjectComm((PetscObject)A), nr, nr != vs->nr ? NULL : vs->isglobal.row, nc, nc != vs->nc ? NULL : vs->isglobal.col, a, B));
623: for (i = 0; i < nr; i++) {
624: for (j = 0; j < nc; j++) {
625: if (b[i * nc + j]) PetscCall(MatDestroy(a + i * nc + j));
626: }
627: }
628: PetscCall(PetscFree2(a, b));
629: (*B)->assembled = A->assembled;
630: PetscCall(PetscObjectCompose((PetscObject)A, keyname, (PetscObject)*B));
631: PetscCall(PetscObjectDereference((PetscObject)*B)); /* Leave the only remaining reference in the composition */
632: PetscFunctionReturn(PETSC_SUCCESS);
633: }
635: static PetscErrorCode MatNestFindSubMat(Mat A, IS isrow, IS iscol, PetscBool global, PetscBool *found, Mat *B)
636: {
637: Mat_Nest *vs = (Mat_Nest *)A->data;
638: PetscInt rbegin, rend, cbegin, cend;
640: PetscFunctionBegin;
641: *B = NULL;
642: PetscCall(MatNestFindISRange(A, vs->nr, global ? vs->isglobal.row : vs->islocal.row, isrow, &rbegin, &rend));
643: PetscCall(MatNestFindISRange(A, vs->nc, global ? vs->isglobal.col : vs->islocal.col, iscol, &cbegin, &cend));
644: if (rend == rbegin + 1 && cend == cbegin + 1) {
645: if (!vs->m[rbegin][cbegin]) PetscCall(MatNestFillEmptyMat_Private(A, rbegin, cbegin, vs->m[rbegin] + cbegin));
646: *B = vs->m[rbegin][cbegin];
647: if (found) *found = PETSC_TRUE;
648: } else if (rbegin != -1 && cbegin != -1) {
649: PetscCheck(global == PETSC_TRUE, PETSC_COMM_SELF, PETSC_ERR_SUP, "MATNEST local submatrix cannot select more than a single submatrix");
650: PetscCall(MatNestGetBlock_Private(A, rbegin, rend, cbegin, cend, B));
651: if (found) *found = PETSC_TRUE;
652: } else if (found) *found = PETSC_FALSE;
653: PetscFunctionReturn(PETSC_SUCCESS);
654: }
656: static PetscErrorCode MatNestFindFullBlocks_Private(Mat A, PetscInt n, const IS blockis[], IS is, const char axis[], PetscInt *nselected, PetscInt **selected, IS **isout)
657: {
658: const PetscInt *idx;
659: PetscInt *blocks;
660: IS *out;
661: PetscInt N, bs, cursor = 0, i, nblock, nlocal, nout = 0, offset = 0, start;
662: PetscBool complete, match;
664: PetscFunctionBegin;
665: PetscCall(ISGetSize(is, &N));
666: PetscCheck(N, PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "Empty %s index sets are not supported for MATNEST submatrices", axis);
667: PetscCall(ISGetLocalSize(is, &nlocal));
668: PetscCall(ISGetIndices(is, &idx));
669: PetscCall(PetscMalloc1(n, &blocks));
670: for (i = 0; i < n; i++) {
671: const PetscInt *bidx;
673: PetscCall(ISGetSize(blockis[i], &N));
674: if (!N) continue;
675: PetscCall(ISGetLocalSize(blockis[i], &nblock));
676: match = (PetscBool)(cursor + nblock <= nlocal);
677: if (match && nblock) {
678: PetscCall(ISGetIndices(blockis[i], &bidx));
679: PetscCall(PetscArraycmp(idx + cursor, bidx, nblock, &match));
680: PetscCall(ISRestoreIndices(blockis[i], &bidx));
681: }
682: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &match, 1, MPI_C_BOOL, MPI_LAND, PetscObjectComm((PetscObject)A)));
683: if (match) {
684: blocks[nout++] = i;
685: cursor += nblock;
686: }
687: }
688: complete = (PetscBool)(cursor == nlocal);
689: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &complete, 1, MPI_C_BOOL, MPI_LAND, PetscObjectComm((PetscObject)A)));
690: PetscCall(ISRestoreIndices(is, &idx));
691: if (!complete || !nout) PetscCall(PetscFree(blocks));
692: PetscCheck(complete && nout, PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "MATNEST submatrix %s index set must be an ordered union of complete MATNEST blocks", axis);
694: PetscCallMPI(MPI_Scan(&nlocal, &start, 1, MPIU_INT, MPI_SUM, PetscObjectComm((PetscObject)A)));
695: start -= nlocal;
696: PetscCall(PetscMalloc1(nout, &out));
697: for (i = 0; i < nout; i++) {
698: PetscCall(ISGetLocalSize(blockis[blocks[i]], &nblock));
699: PetscCall(ISGetBlockSize(blockis[blocks[i]], &bs));
700: PetscCall(ISCreateStride(PetscObjectComm((PetscObject)A), nblock, start + offset, 1, out + i));
701: PetscCall(ISSetBlockSize(out[i], bs));
702: offset += nblock;
703: }
704: PetscCheck(offset == nlocal, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Inconsistent MATNEST submatrix %s layout", axis);
705: *nselected = nout;
706: *selected = blocks;
707: *isout = out;
708: PetscFunctionReturn(PETSC_SUCCESS);
709: }
711: static PetscErrorCode MatCreateSubMatrix_Nest_Nontrivial(Mat A, IS isrow, IS iscol, MatReuse reuse, Mat *B)
712: {
713: Mat_Nest *vs = (Mat_Nest *)A->data;
714: Mat *submats;
715: IS *rowis, *colis;
716: PetscInt *rows, *cols;
717: PetscInt nr, nc;
718: PetscBool flg;
720: PetscFunctionBegin;
721: PetscCall(MatNestFindFullBlocks_Private(A, vs->nr, vs->isglobal.row, isrow, "row", &nr, &rows, &rowis));
722: PetscCall(MatNestFindFullBlocks_Private(A, vs->nc, vs->isglobal.col, iscol, "column", &nc, &cols, &colis));
723: PetscCall(PetscMalloc1(nr * nc, &submats));
724: for (PetscInt i = 0; i < nr; i++) {
725: for (PetscInt j = 0; j < nc; j++) submats[i * nc + j] = vs->m[rows[i]][cols[j]];
726: }
727: if (reuse == MAT_INITIAL_MATRIX) {
728: PetscCall(MatCreateNest(PetscObjectComm((PetscObject)A), nr, rowis, nc, colis, submats, B));
729: (*B)->assembled = A->assembled;
730: } else {
731: PetscCheck(reuse == MAT_REUSE_MATRIX, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_OUTOFRANGE, "Invalid MatReuse %d", (int)reuse);
732: PetscCall(PetscObjectTypeCompare((PetscObject)*B, MATNEST, &flg));
733: PetscCheck(flg, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONGSTATE, "Cannot reuse a non-MATNEST matrix for this MATNEST submatrix");
734: vs = (Mat_Nest *)(*B)->data;
735: PetscCheck(vs->nr == nr && vs->nc == nc, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONGSTATE, "Cannot reuse MATNEST submatrix with a different block layout");
736: for (PetscInt i = 0; i < nr; i++) {
737: PetscCall(ISEqualUnsorted(vs->isglobal.row[i], rowis[i], &flg));
738: PetscCheck(flg, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONGSTATE, "Cannot reuse MATNEST submatrix with a different row layout");
739: }
740: for (PetscInt j = 0; j < nc; j++) {
741: PetscCall(ISEqualUnsorted(vs->isglobal.col[j], colis[j], &flg));
742: PetscCheck(flg, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONGSTATE, "Cannot reuse MATNEST submatrix with a different column layout");
743: }
744: PetscCall(MatNestSetSubMats(*B, nr, rowis, nc, colis, submats));
745: (*B)->assembled = A->assembled;
746: }
747: PetscCall(PetscFree(submats));
748: for (PetscInt i = 0; i < nr; i++) PetscCall(ISDestroy(rowis + i));
749: for (PetscInt j = 0; j < nc; j++) PetscCall(ISDestroy(colis + j));
750: PetscCall(PetscFree(rows));
751: PetscCall(PetscFree(rowis));
752: PetscCall(PetscFree(cols));
753: PetscCall(PetscFree(colis));
754: PetscFunctionReturn(PETSC_SUCCESS);
755: }
757: /*
758: TODO: This does not actually returns a submatrix we can modify
759: */
760: static PetscErrorCode MatCreateSubMatrix_Nest(Mat A, IS isrow, IS iscol, MatReuse reuse, Mat *B)
761: {
762: Mat sub;
763: PetscBool found;
765: PetscFunctionBegin;
766: PetscCall(MatNestFindSubMat(A, isrow, iscol, PETSC_TRUE, &found, &sub));
767: if (!found) {
768: PetscCall(MatCreateSubMatrix_Nest_Nontrivial(A, isrow, iscol, reuse, B));
769: PetscFunctionReturn(PETSC_SUCCESS);
770: }
771: switch (reuse) {
772: case MAT_INITIAL_MATRIX:
773: PetscCall(PetscObjectReference((PetscObject)sub));
774: if (sub) PetscCall(PetscObjectStateIncrease((PetscObject)sub));
775: *B = sub;
776: break;
777: case MAT_REUSE_MATRIX:
778: PetscCheck(sub == *B, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONGSTATE, "Submatrix was not used before in this call");
779: if (sub) PetscCall(PetscObjectStateIncrease((PetscObject)sub));
780: break;
781: default:
782: break;
783: }
784: PetscFunctionReturn(PETSC_SUCCESS);
785: }
787: static PetscErrorCode MatGetLocalSubMatrix_Nest(Mat A, IS isrow, IS iscol, Mat *B)
788: {
789: Mat sub;
791: PetscFunctionBegin;
792: PetscCall(MatNestFindSubMat(A, isrow, iscol, PETSC_FALSE, NULL, &sub));
793: /* We allow the submatrix to be NULL, perhaps it would be better for the user to return an empty matrix instead */
794: PetscCall(PetscObjectReference((PetscObject)sub));
795: *B = sub;
796: PetscFunctionReturn(PETSC_SUCCESS);
797: }
799: static PetscErrorCode MatRestoreLocalSubMatrix_Nest(Mat A, IS isrow, IS iscol, Mat *B)
800: {
801: Mat sub;
803: PetscFunctionBegin;
804: PetscCall(MatNestFindSubMat(A, isrow, iscol, PETSC_FALSE, NULL, &sub));
805: PetscCheck(*B == sub, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONGSTATE, "Local submatrix has not been gotten");
806: if (sub) {
807: PetscCheck(((PetscObject)sub)->refct > 1, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONGSTATE, "Local submatrix has had reference count decremented too many times");
808: PetscCall(MatDestroy(B));
809: }
810: PetscFunctionReturn(PETSC_SUCCESS);
811: }
813: static PetscErrorCode MatGetDiagonal_Nest(Mat A, Vec v)
814: {
815: Mat_Nest *bA = (Mat_Nest *)A->data;
817: PetscFunctionBegin;
818: for (PetscInt i = 0; i < bA->nr; i++) {
819: Vec bv;
820: PetscCall(VecGetSubVector(v, bA->isglobal.row[i], &bv));
821: if (bA->m[i][i]) PetscCall(MatGetDiagonal(bA->m[i][i], bv));
822: else PetscCall(VecSet(bv, 0.0));
823: PetscCall(VecRestoreSubVector(v, bA->isglobal.row[i], &bv));
824: }
825: PetscFunctionReturn(PETSC_SUCCESS);
826: }
828: static PetscErrorCode MatDiagonalScale_Nest(Mat A, Vec l, Vec r)
829: {
830: Mat_Nest *bA = (Mat_Nest *)A->data;
831: Vec bl, *br;
833: PetscFunctionBegin;
834: PetscCall(PetscCalloc1(bA->nc, &br));
835: if (r) {
836: for (PetscInt j = 0; j < bA->nc; j++) PetscCall(VecGetSubVector(r, bA->isglobal.col[j], &br[j]));
837: }
838: bl = NULL;
839: for (PetscInt i = 0; i < bA->nr; i++) {
840: if (l) PetscCall(VecGetSubVector(l, bA->isglobal.row[i], &bl));
841: for (PetscInt j = 0; j < bA->nc; j++) {
842: if (bA->m[i][j]) PetscCall(MatDiagonalScale(bA->m[i][j], bl, br[j]));
843: }
844: if (l) PetscCall(VecRestoreSubVector(l, bA->isglobal.row[i], &bl));
845: }
846: if (r) {
847: for (PetscInt j = 0; j < bA->nc; j++) PetscCall(VecRestoreSubVector(r, bA->isglobal.col[j], &br[j]));
848: }
849: PetscCall(PetscFree(br));
850: PetscFunctionReturn(PETSC_SUCCESS);
851: }
853: static PetscErrorCode MatScale_Nest(Mat A, PetscScalar a)
854: {
855: Mat_Nest *bA = (Mat_Nest *)A->data;
857: PetscFunctionBegin;
858: for (PetscInt i = 0; i < bA->nr; i++) {
859: for (PetscInt j = 0; j < bA->nc; j++) {
860: if (bA->m[i][j]) PetscCall(MatScale(bA->m[i][j], a));
861: }
862: }
863: PetscFunctionReturn(PETSC_SUCCESS);
864: }
866: static PetscErrorCode MatShift_Nest(Mat A, PetscScalar a)
867: {
868: Mat_Nest *bA = (Mat_Nest *)A->data;
869: PetscBool nnzstate = PETSC_FALSE;
871: PetscFunctionBegin;
872: for (PetscInt i = 0; i < bA->nr; i++) {
873: PetscObjectState subnnzstate = 0;
874: PetscCheck(bA->m[i][i], PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "No support for shifting an empty diagonal block, insert a matrix in block (%" PetscInt_FMT ",%" PetscInt_FMT ")", i, i);
875: PetscCall(MatShift(bA->m[i][i], a));
876: PetscCall(MatGetNonzeroState(bA->m[i][i], &subnnzstate));
877: nnzstate = (PetscBool)(nnzstate || bA->nnzstate[i * bA->nc + i] != subnnzstate);
878: bA->nnzstate[i * bA->nc + i] = subnnzstate;
879: }
880: if (nnzstate) A->nonzerostate++;
881: PetscFunctionReturn(PETSC_SUCCESS);
882: }
884: static PetscErrorCode MatDiagonalSet_Nest(Mat A, Vec D, InsertMode is)
885: {
886: Mat_Nest *bA = (Mat_Nest *)A->data;
887: PetscBool nnzstate = PETSC_FALSE;
889: PetscFunctionBegin;
890: for (PetscInt i = 0; i < bA->nr; i++) {
891: PetscObjectState subnnzstate = 0;
892: Vec bv;
893: PetscCall(VecGetSubVector(D, bA->isglobal.row[i], &bv));
894: if (bA->m[i][i]) {
895: PetscCall(MatDiagonalSet(bA->m[i][i], bv, is));
896: PetscCall(MatGetNonzeroState(bA->m[i][i], &subnnzstate));
897: }
898: PetscCall(VecRestoreSubVector(D, bA->isglobal.row[i], &bv));
899: nnzstate = (PetscBool)(nnzstate || bA->nnzstate[i * bA->nc + i] != subnnzstate);
900: bA->nnzstate[i * bA->nc + i] = subnnzstate;
901: }
902: if (nnzstate) A->nonzerostate++;
903: PetscFunctionReturn(PETSC_SUCCESS);
904: }
906: static PetscErrorCode MatSetRandom_Nest(Mat A, PetscRandom rctx)
907: {
908: Mat_Nest *bA = (Mat_Nest *)A->data;
910: PetscFunctionBegin;
911: for (PetscInt i = 0; i < bA->nr; i++) {
912: for (PetscInt j = 0; j < bA->nc; j++) {
913: if (bA->m[i][j]) PetscCall(MatSetRandom(bA->m[i][j], rctx));
914: }
915: }
916: PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
917: PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
918: PetscFunctionReturn(PETSC_SUCCESS);
919: }
921: static PetscErrorCode MatCreateVecs_Nest(Mat A, Vec *right, Vec *left)
922: {
923: Mat_Nest *bA = (Mat_Nest *)A->data;
924: Vec *L, *R;
925: MPI_Comm comm;
926: PetscInt i, j;
928: PetscFunctionBegin;
929: PetscCall(PetscObjectGetComm((PetscObject)A, &comm));
930: if (right) {
931: /* allocate R */
932: PetscCall(PetscMalloc1(bA->nc, &R));
933: /* Create the right vectors */
934: for (j = 0; j < bA->nc; j++) {
935: for (i = 0; i < bA->nr; i++) {
936: if (bA->m[i][j]) {
937: PetscCall(MatCreateVecs(bA->m[i][j], &R[j], NULL));
938: break;
939: }
940: }
941: PetscCheck(i != bA->nr, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Mat(Nest) contains a null column.");
942: }
943: PetscCall(VecCreateNest(comm, bA->nc, bA->isglobal.col, R, right));
944: /* hand back control to the nest vector */
945: for (j = 0; j < bA->nc; j++) PetscCall(VecDestroy(&R[j]));
946: PetscCall(PetscFree(R));
947: }
949: if (left) {
950: /* allocate L */
951: PetscCall(PetscMalloc1(bA->nr, &L));
952: /* Create the left vectors */
953: for (i = 0; i < bA->nr; i++) {
954: for (j = 0; j < bA->nc; j++) {
955: if (bA->m[i][j]) {
956: PetscCall(MatCreateVecs(bA->m[i][j], NULL, &L[i]));
957: break;
958: }
959: }
960: PetscCheck(j != bA->nc, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Mat(Nest) contains a null row.");
961: }
963: PetscCall(VecCreateNest(comm, bA->nr, bA->isglobal.row, L, left));
964: for (i = 0; i < bA->nr; i++) PetscCall(VecDestroy(&L[i]));
966: PetscCall(PetscFree(L));
967: }
968: PetscFunctionReturn(PETSC_SUCCESS);
969: }
971: static PetscErrorCode MatView_Nest(Mat A, PetscViewer viewer)
972: {
973: Mat_Nest *bA = (Mat_Nest *)A->data;
974: PetscBool isascii, viewSub = PETSC_FALSE;
976: PetscFunctionBegin;
977: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
978: if (isascii) {
979: PetscViewerFormat format;
981: PetscCall(PetscViewerGetFormat(viewer, &format));
982: if (format == PETSC_VIEWER_ASCII_MATLAB) {
983: Mat T;
985: PetscCall(MatConvert(A, MATAIJ, MAT_INITIAL_MATRIX, &T));
986: PetscCall(MatView(T, viewer));
987: PetscCall(MatDestroy(&T));
988: PetscFunctionReturn(PETSC_SUCCESS);
989: }
990: PetscCall(PetscOptionsGetBool(((PetscObject)A)->options, ((PetscObject)A)->prefix, "-mat_view_nest_sub", &viewSub, NULL));
991: PetscCall(PetscViewerASCIIPushTab(viewer));
992: PetscCall(PetscViewerASCIIPrintf(viewer, "MatNest, rows=%" PetscInt_FMT ", cols=%" PetscInt_FMT ", structure:\n", bA->nr, bA->nc));
993: for (PetscInt i = 0; i < bA->nr; i++) {
994: for (PetscInt j = 0; j < bA->nc; j++) {
995: MatType type;
996: char name[256] = "", prefix[256] = "";
997: PetscInt NR, NC;
998: PetscBool isNest = PETSC_FALSE;
1000: if (!bA->m[i][j]) {
1001: PetscCall(PetscViewerASCIIPrintf(viewer, "(%" PetscInt_FMT ",%" PetscInt_FMT ") : NULL\n", i, j));
1002: continue;
1003: }
1004: PetscCall(MatGetSize(bA->m[i][j], &NR, &NC));
1005: PetscCall(MatGetType(bA->m[i][j], &type));
1006: if (((PetscObject)bA->m[i][j])->name) PetscCall(PetscSNPrintf(name, sizeof(name), "name=\"%s\", ", ((PetscObject)bA->m[i][j])->name));
1007: if (((PetscObject)bA->m[i][j])->prefix) PetscCall(PetscSNPrintf(prefix, sizeof(prefix), "prefix=\"%s\", ", ((PetscObject)bA->m[i][j])->prefix));
1008: PetscCall(PetscObjectTypeCompare((PetscObject)bA->m[i][j], MATNEST, &isNest));
1010: PetscCall(PetscViewerASCIIPrintf(viewer, "(%" PetscInt_FMT ",%" PetscInt_FMT ") : %s%stype=%s, rows=%" PetscInt_FMT ", cols=%" PetscInt_FMT "\n", i, j, name, prefix, type, NR, NC));
1012: if (isNest || viewSub) {
1013: PetscCall(PetscViewerASCIIPushTab(viewer)); /* push1 */
1014: PetscCall(MatView(bA->m[i][j], viewer));
1015: PetscCall(PetscViewerASCIIPopTab(viewer)); /* pop1 */
1016: }
1017: }
1018: }
1019: PetscCall(PetscViewerASCIIPopTab(viewer)); /* pop0 */
1020: }
1021: PetscFunctionReturn(PETSC_SUCCESS);
1022: }
1024: static PetscErrorCode MatZeroEntries_Nest(Mat A)
1025: {
1026: Mat_Nest *bA = (Mat_Nest *)A->data;
1028: PetscFunctionBegin;
1029: for (PetscInt i = 0; i < bA->nr; i++) {
1030: for (PetscInt j = 0; j < bA->nc; j++) {
1031: if (!bA->m[i][j]) continue;
1032: PetscCall(MatZeroEntries(bA->m[i][j]));
1033: }
1034: }
1035: PetscFunctionReturn(PETSC_SUCCESS);
1036: }
1038: static PetscErrorCode MatCopy_Nest(Mat A, Mat B, MatStructure str)
1039: {
1040: Mat_Nest *bA = (Mat_Nest *)A->data, *bB = (Mat_Nest *)B->data;
1041: PetscInt i, j, nr = bA->nr, nc = bA->nc;
1042: PetscBool nnzstate = PETSC_FALSE;
1044: PetscFunctionBegin;
1045: PetscCheck(nr == bB->nr && nc == bB->nc, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_INCOMP, "Cannot copy a Mat_Nest of block size (%" PetscInt_FMT ",%" PetscInt_FMT ") to a Mat_Nest of block size (%" PetscInt_FMT ",%" PetscInt_FMT ")", bB->nr, bB->nc, nr, nc);
1046: for (i = 0; i < nr; i++) {
1047: for (j = 0; j < nc; j++) {
1048: PetscObjectState subnnzstate = 0;
1049: if (bA->m[i][j] && bB->m[i][j]) {
1050: PetscCall(MatCopy(bA->m[i][j], bB->m[i][j], str));
1051: PetscCall(MatGetNonzeroState(bB->m[i][j], &subnnzstate));
1052: nnzstate = (PetscBool)(nnzstate || bB->nnzstate[i * nc + j] != subnnzstate);
1053: bB->nnzstate[i * nc + j] = subnnzstate;
1054: } else if (bA->m[i][j]) { // bB->m[i][j] is NULL
1055: Mat M;
1057: PetscCheck(str == DIFFERENT_NONZERO_PATTERN || str == UNKNOWN_NONZERO_PATTERN, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_INCOMP, "Matrix block does not exist at %" PetscInt_FMT ",%" PetscInt_FMT ". Use DIFFERENT_NONZERO_PATTERN or UNKNOWN_NONZERO_PATTERN", i, j);
1058: PetscCall(MatDuplicate(bA->m[i][j], MAT_COPY_VALUES, &M));
1059: PetscCall(MatNestSetSubMat(B, i, j, M));
1060: PetscCall(MatDestroy(&M));
1061: } else if (bB->m[i][j]) { // bA->m[i][j] is NULL
1062: PetscCheck(str == DIFFERENT_NONZERO_PATTERN || str == SUBSET_NONZERO_PATTERN || str == UNKNOWN_NONZERO_PATTERN, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_INCOMP, "Matrix block does not exist at %" PetscInt_FMT ",%" PetscInt_FMT ". Use DIFFERENT_NONZERO_PATTERN, SUBSET_NONZERO_PATTERN or UNKNOWN_NONZERO_PATTERN", i, j);
1063: PetscCall(MatNestSetSubMat(B, i, j, NULL));
1064: }
1065: }
1066: }
1067: if (nnzstate) B->nonzerostate++;
1068: PetscFunctionReturn(PETSC_SUCCESS);
1069: }
1071: static PetscErrorCode MatAXPY_Nest(Mat Y, PetscScalar a, Mat X, MatStructure str)
1072: {
1073: Mat_Nest *bY = (Mat_Nest *)Y->data, *bX = (Mat_Nest *)X->data;
1074: PetscInt i, j, nr = bY->nr, nc = bY->nc;
1075: PetscBool nnzstate = PETSC_FALSE;
1077: PetscFunctionBegin;
1078: PetscCheck(nr == bX->nr && nc == bX->nc, PetscObjectComm((PetscObject)Y), PETSC_ERR_ARG_INCOMP, "Cannot AXPY a MatNest of block size (%" PetscInt_FMT ",%" PetscInt_FMT ") with a MatNest of block size (%" PetscInt_FMT ",%" PetscInt_FMT ")", bX->nr, bX->nc, nr, nc);
1079: for (i = 0; i < nr; i++) {
1080: for (j = 0; j < nc; j++) {
1081: PetscObjectState subnnzstate = 0;
1082: if (bY->m[i][j] && bX->m[i][j]) {
1083: PetscCall(MatAXPY(bY->m[i][j], a, bX->m[i][j], str));
1084: } else if (bX->m[i][j]) {
1085: Mat M;
1087: PetscCheck(str == DIFFERENT_NONZERO_PATTERN || str == UNKNOWN_NONZERO_PATTERN, PetscObjectComm((PetscObject)Y), PETSC_ERR_ARG_INCOMP, "Matrix block does not exist at %" PetscInt_FMT ",%" PetscInt_FMT ". Use DIFFERENT_NONZERO_PATTERN or UNKNOWN_NONZERO_PATTERN", i, j);
1088: PetscCall(MatDuplicate(bX->m[i][j], MAT_COPY_VALUES, &M));
1089: PetscCall(MatScale(M, a));
1090: PetscCall(MatNestSetSubMat(Y, i, j, M));
1091: PetscCall(MatDestroy(&M));
1092: }
1093: if (bY->m[i][j]) PetscCall(MatGetNonzeroState(bY->m[i][j], &subnnzstate));
1094: nnzstate = (PetscBool)(nnzstate || bY->nnzstate[i * nc + j] != subnnzstate);
1095: bY->nnzstate[i * nc + j] = subnnzstate;
1096: }
1097: }
1098: if (nnzstate) Y->nonzerostate++;
1099: PetscFunctionReturn(PETSC_SUCCESS);
1100: }
1102: static PetscErrorCode MatDuplicate_Nest(Mat A, MatDuplicateOption op, Mat *B)
1103: {
1104: Mat_Nest *bA = (Mat_Nest *)A->data;
1105: Mat *b;
1106: PetscInt i, j, nr = bA->nr, nc = bA->nc;
1108: PetscFunctionBegin;
1109: PetscCall(PetscMalloc1(nr * nc, &b));
1110: for (i = 0; i < nr; i++) {
1111: for (j = 0; j < nc; j++) {
1112: if (bA->m[i][j]) PetscCall(MatDuplicate(bA->m[i][j], op, &b[i * nc + j]));
1113: else b[i * nc + j] = NULL;
1114: }
1115: }
1116: PetscCall(MatCreateNest(PetscObjectComm((PetscObject)A), nr, bA->isglobal.row, nc, bA->isglobal.col, b, B));
1117: /* Give the new MatNest exclusive ownership */
1118: for (i = 0; i < nr * nc; i++) PetscCall(MatDestroy(&b[i]));
1119: PetscCall(PetscFree(b));
1121: PetscCall(MatAssemblyBegin(*B, MAT_FINAL_ASSEMBLY));
1122: PetscCall(MatAssemblyEnd(*B, MAT_FINAL_ASSEMBLY));
1123: PetscFunctionReturn(PETSC_SUCCESS);
1124: }
1126: /* nest api */
1127: static PetscErrorCode MatNestGetSubMat_Nest(Mat A, PetscInt idxm, PetscInt jdxm, Mat *mat)
1128: {
1129: Mat_Nest *bA = (Mat_Nest *)A->data;
1131: PetscFunctionBegin;
1132: PetscCheck(idxm < bA->nr, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_OUTOFRANGE, "Row too large: row %" PetscInt_FMT " max %" PetscInt_FMT, idxm, bA->nr - 1);
1133: PetscCheck(jdxm < bA->nc, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_OUTOFRANGE, "Col too large: row %" PetscInt_FMT " max %" PetscInt_FMT, jdxm, bA->nc - 1);
1134: *mat = bA->m[idxm][jdxm];
1135: PetscFunctionReturn(PETSC_SUCCESS);
1136: }
1138: /*@
1139: MatNestGetSubMat - Returns a single, sub-matrix from a `MATNEST`
1141: Not Collective
1143: Input Parameters:
1144: + A - `MATNEST` matrix
1145: . idxm - index of the matrix within the nest matrix
1146: - jdxm - index of the matrix within the nest matrix
1148: Output Parameter:
1149: . sub - matrix at index `idxm`, `jdxm` within the nest matrix
1151: Level: developer
1153: .seealso: [](ch_matrices), `Mat`, `MATNEST`, `MatNestGetSize()`, `MatNestGetSubMats()`, `MatCreateNest()`, `MatNestSetSubMat()`,
1154: `MatNestGetLocalISs()`, `MatNestGetISs()`
1155: @*/
1156: PetscErrorCode MatNestGetSubMat(Mat A, PetscInt idxm, PetscInt jdxm, Mat *sub)
1157: {
1158: PetscFunctionBegin;
1162: PetscAssertPointer(sub, 4);
1163: PetscUseMethod(A, "MatNestGetSubMat_C", (Mat, PetscInt, PetscInt, Mat *), (A, idxm, jdxm, sub));
1164: PetscFunctionReturn(PETSC_SUCCESS);
1165: }
1167: static PetscErrorCode MatNestSetSubMat_Nest(Mat A, PetscInt idxm, PetscInt jdxm, Mat mat)
1168: {
1169: Mat_Nest *bA = (Mat_Nest *)A->data;
1170: PetscInt m, n, M, N, mi, ni, Mi, Ni;
1172: PetscFunctionBegin;
1173: PetscCheck(idxm < bA->nr, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_OUTOFRANGE, "Row too large: row %" PetscInt_FMT " max %" PetscInt_FMT, idxm, bA->nr - 1);
1174: PetscCheck(jdxm < bA->nc, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_OUTOFRANGE, "Col too large: row %" PetscInt_FMT " max %" PetscInt_FMT, jdxm, bA->nc - 1);
1175: if (mat) {
1176: PetscCall(MatGetLocalSize(mat, &m, &n));
1177: PetscCall(MatGetSize(mat, &M, &N));
1178: PetscCall(ISGetLocalSize(bA->isglobal.row[idxm], &mi));
1179: PetscCall(ISGetSize(bA->isglobal.row[idxm], &Mi));
1180: PetscCall(ISGetLocalSize(bA->isglobal.col[jdxm], &ni));
1181: PetscCall(ISGetSize(bA->isglobal.col[jdxm], &Ni));
1182: PetscCheck(M == Mi && N == Ni, PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_INCOMP, "Submatrix dimension (%" PetscInt_FMT ",%" PetscInt_FMT ") incompatible with nest block (%" PetscInt_FMT ",%" PetscInt_FMT ")", M, N, Mi, Ni);
1183: PetscCheck(m == mi && n == ni, PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_INCOMP, "Submatrix local dimension (%" PetscInt_FMT ",%" PetscInt_FMT ") incompatible with nest block (%" PetscInt_FMT ",%" PetscInt_FMT ")", m, n, mi, ni);
1184: }
1186: /* do not increase object state */
1187: if (mat == bA->m[idxm][jdxm]) PetscFunctionReturn(PETSC_SUCCESS);
1189: PetscCall(PetscObjectReference((PetscObject)mat));
1190: PetscCall(MatDestroy(&bA->m[idxm][jdxm]));
1191: bA->m[idxm][jdxm] = mat;
1192: PetscCall(PetscObjectStateIncrease((PetscObject)A));
1193: if (mat) PetscCall(MatGetNonzeroState(mat, &bA->nnzstate[idxm * bA->nc + jdxm]));
1194: else bA->nnzstate[idxm * bA->nc + jdxm] = 0;
1195: A->nonzerostate++;
1196: PetscFunctionReturn(PETSC_SUCCESS);
1197: }
1199: /*@
1200: MatNestSetSubMat - Set a single submatrix in the `MATNEST`
1202: Logically Collective
1204: Input Parameters:
1205: + A - `MATNEST` matrix
1206: . idxm - index of the matrix within the nest matrix
1207: . jdxm - index of the matrix within the nest matrix
1208: - sub - matrix at index `idxm`, `jdxm` within the nest matrix
1210: Level: developer
1212: Notes:
1213: The new submatrix must have the same size and communicator as that block of the nest.
1215: This increments the reference count of the submatrix.
1217: .seealso: [](ch_matrices), `Mat`, `MATNEST`, `MatNestSetSubMats()`, `MatNestGetSubMats()`, `MatNestGetLocalISs()`, `MatCreateNest()`,
1218: `MatNestGetSubMat()`, `MatNestGetISs()`, `MatNestGetSize()`
1219: @*/
1220: PetscErrorCode MatNestSetSubMat(Mat A, PetscInt idxm, PetscInt jdxm, Mat sub)
1221: {
1222: PetscFunctionBegin;
1227: PetscTryMethod(A, "MatNestSetSubMat_C", (Mat, PetscInt, PetscInt, Mat), (A, idxm, jdxm, sub));
1228: PetscFunctionReturn(PETSC_SUCCESS);
1229: }
1231: static PetscErrorCode MatNestGetSubMats_Nest(Mat A, PetscInt *M, PetscInt *N, Mat ***mat)
1232: {
1233: Mat_Nest *bA = (Mat_Nest *)A->data;
1235: PetscFunctionBegin;
1236: if (M) *M = bA->nr;
1237: if (N) *N = bA->nc;
1238: if (mat) *mat = bA->m;
1239: PetscFunctionReturn(PETSC_SUCCESS);
1240: }
1242: /*@
1243: MatNestGetSubMats - Returns the entire two dimensional array of matrices defining a `MATNEST` matrix.
1245: Not Collective
1247: Input Parameter:
1248: . A - nest matrix
1250: Output Parameters:
1251: + M - number of submatrix rows in the nest matrix
1252: . N - number of submatrix columns in the nest matrix
1253: - mat - array of matrices
1255: Level: developer
1257: Note:
1258: The user should not free the array `mat`.
1260: Fortran Notes:
1261: This routine has a calling sequence `call MatNestGetSubMats(A, M, N, mat, ierr)`
1262: where the space allocated for the optional argument `mat` is assumed large enough (if provided).
1263: Matrices in `mat` are returned in row-major order, see `MatCreateNest()` for an example.
1265: .seealso: [](ch_matrices), `Mat`, `MATNEST`, `MatNestGetSize()`, `MatNestGetSubMat()`, `MatNestGetLocalISs()`, `MatCreateNest()`,
1266: `MatNestSetSubMats()`, `MatNestGetISs()`, `MatNestSetSubMat()`
1267: @*/
1268: PetscErrorCode MatNestGetSubMats(Mat A, PetscInt *M, PetscInt *N, Mat ***mat)
1269: {
1270: PetscFunctionBegin;
1272: PetscUseMethod(A, "MatNestGetSubMats_C", (Mat, PetscInt *, PetscInt *, Mat ***), (A, M, N, mat));
1273: PetscFunctionReturn(PETSC_SUCCESS);
1274: }
1276: static PetscErrorCode MatNestGetSize_Nest(Mat A, PetscInt *M, PetscInt *N)
1277: {
1278: Mat_Nest *bA = (Mat_Nest *)A->data;
1280: PetscFunctionBegin;
1281: if (M) *M = bA->nr;
1282: if (N) *N = bA->nc;
1283: PetscFunctionReturn(PETSC_SUCCESS);
1284: }
1286: /*@
1287: MatNestGetSize - Returns the size of the `MATNEST` matrix.
1289: Not Collective
1291: Input Parameter:
1292: . A - `MATNEST` matrix
1294: Output Parameters:
1295: + M - number of rows in the nested mat
1296: - N - number of cols in the nested mat
1298: Level: developer
1300: Note:
1301: `size` refers to the number of submatrices in the row and column directions of the nested matrix
1303: .seealso: [](ch_matrices), `Mat`, `MATNEST`, `MatNestGetSubMat()`, `MatNestGetSubMats()`, `MatCreateNest()`, `MatNestGetLocalISs()`,
1304: `MatNestGetISs()`
1305: @*/
1306: PetscErrorCode MatNestGetSize(Mat A, PetscInt *M, PetscInt *N)
1307: {
1308: PetscFunctionBegin;
1310: PetscUseMethod(A, "MatNestGetSize_C", (Mat, PetscInt *, PetscInt *), (A, M, N));
1311: PetscFunctionReturn(PETSC_SUCCESS);
1312: }
1314: static PetscErrorCode MatNestGetISs_Nest(Mat A, IS rows[], IS cols[])
1315: {
1316: Mat_Nest *vs = (Mat_Nest *)A->data;
1318: PetscFunctionBegin;
1319: if (rows) {
1320: for (PetscInt i = 0; i < vs->nr; i++) rows[i] = vs->isglobal.row[i];
1321: }
1322: if (cols) {
1323: for (PetscInt i = 0; i < vs->nc; i++) cols[i] = vs->isglobal.col[i];
1324: }
1325: PetscFunctionReturn(PETSC_SUCCESS);
1326: }
1328: /*@
1329: MatNestGetISs - Returns the index sets partitioning the row and column spaces of a `MATNEST`
1331: Not Collective
1333: Input Parameter:
1334: . A - `MATNEST` matrix
1336: Output Parameters:
1337: + rows - array of row index sets (pass `NULL` to ignore)
1338: - cols - array of column index sets (pass `NULL` to ignore)
1340: Level: advanced
1342: Note:
1343: The user must have allocated arrays of the correct size. The reference count is not increased on the returned `IS`s.
1345: .seealso: [](ch_matrices), `Mat`, `MATNEST`, `MatNestGetSubMat()`, `MatNestGetSubMats()`, `MatNestGetSize()`, `MatNestGetLocalISs()`,
1346: `MatCreateNest()`, `MatNestSetSubMats()`
1347: @*/
1348: PetscErrorCode MatNestGetISs(Mat A, IS rows[], IS cols[])
1349: {
1350: PetscFunctionBegin;
1352: PetscUseMethod(A, "MatNestGetISs_C", (Mat, IS[], IS[]), (A, rows, cols));
1353: PetscFunctionReturn(PETSC_SUCCESS);
1354: }
1356: static PetscErrorCode MatNestGetLocalISs_Nest(Mat A, IS rows[], IS cols[])
1357: {
1358: Mat_Nest *vs = (Mat_Nest *)A->data;
1360: PetscFunctionBegin;
1361: if (rows) {
1362: for (PetscInt i = 0; i < vs->nr; i++) rows[i] = vs->islocal.row[i];
1363: }
1364: if (cols) {
1365: for (PetscInt i = 0; i < vs->nc; i++) cols[i] = vs->islocal.col[i];
1366: }
1367: PetscFunctionReturn(PETSC_SUCCESS);
1368: }
1370: /*@
1371: MatNestGetLocalISs - Returns the index sets partitioning the row and column spaces of a `MATNEST`
1373: Not Collective
1375: Input Parameter:
1376: . A - `MATNEST` matrix
1378: Output Parameters:
1379: + rows - array of row index sets (pass `NULL` to ignore)
1380: - cols - array of column index sets (pass `NULL` to ignore)
1382: Level: advanced
1384: Note:
1385: The user must have allocated arrays of the correct size. The reference count is not increased on the returned `IS`s.
1387: .seealso: [](ch_matrices), `Mat`, `MATNEST`, `MatNestGetSubMat()`, `MatNestGetSubMats()`, `MatNestGetSize()`, `MatNestGetISs()`, `MatCreateNest()`,
1388: `MatNestSetSubMats()`, `MatNestSetSubMat()`
1389: @*/
1390: PetscErrorCode MatNestGetLocalISs(Mat A, IS rows[], IS cols[])
1391: {
1392: PetscFunctionBegin;
1394: PetscUseMethod(A, "MatNestGetLocalISs_C", (Mat, IS[], IS[]), (A, rows, cols));
1395: PetscFunctionReturn(PETSC_SUCCESS);
1396: }
1398: static PetscErrorCode MatNestSetVecType_Nest(Mat A, VecType vtype)
1399: {
1400: PetscBool flg;
1402: PetscFunctionBegin;
1403: PetscCall(PetscStrcmp(vtype, VECNEST, &flg));
1404: /* In reality, this only distinguishes VECNEST and "other" */
1405: if (flg) A->ops->getvecs = MatCreateVecs_Nest;
1406: else A->ops->getvecs = NULL;
1407: PetscFunctionReturn(PETSC_SUCCESS);
1408: }
1410: /*@
1411: MatNestSetVecType - Sets the type of `Vec` returned by `MatCreateVecs()`
1413: Not Collective
1415: Input Parameters:
1416: + A - `MATNEST` matrix
1417: - vtype - `VecType` to use for creating vectors
1419: Level: developer
1421: .seealso: [](ch_matrices), `Mat`, `MATNEST`, `MatCreateVecs()`, `MatCreateNest()`, `VecType`
1422: @*/
1423: PetscErrorCode MatNestSetVecType(Mat A, VecType vtype)
1424: {
1425: PetscFunctionBegin;
1427: PetscTryMethod(A, "MatNestSetVecType_C", (Mat, VecType), (A, vtype));
1428: PetscFunctionReturn(PETSC_SUCCESS);
1429: }
1431: static PetscErrorCode MatNestSetSubMats_Nest(Mat A, PetscInt nr, const IS is_row[], PetscInt nc, const IS is_col[], const Mat a[])
1432: {
1433: Mat_Nest *s = (Mat_Nest *)A->data;
1434: PetscInt i, j, m, n, M, N;
1435: PetscBool cong, isstd, sametype = PETSC_FALSE;
1436: VecType vtype, type;
1438: PetscFunctionBegin;
1439: PetscCall(MatReset_Nest(A));
1441: s->nr = nr;
1442: s->nc = nc;
1444: /* Create space for submatrices */
1445: PetscCall(PetscMalloc1(nr, &s->m));
1446: PetscCall(PetscMalloc1(nr * nc, &s->m[0]));
1447: for (i = 0; i < nr; i++) {
1448: s->m[i] = s->m[0] + i * nc;
1449: for (j = 0; j < nc; j++) {
1450: s->m[i][j] = a ? a[i * nc + j] : NULL;
1451: PetscCall(PetscObjectReference((PetscObject)s->m[i][j]));
1452: }
1453: }
1454: PetscCall(MatGetVecType(A, &vtype));
1455: PetscCall(PetscStrcmp(vtype, VECSTANDARD, &isstd));
1456: if (isstd) {
1457: /* check if all blocks have the same vectype */
1458: vtype = NULL;
1459: for (i = 0; i < nr; i++) {
1460: for (j = 0; j < nc; j++) {
1461: if (s->m[i][j]) {
1462: if (!vtype) { /* first visited block */
1463: PetscCall(MatGetVecType(s->m[i][j], &vtype));
1464: sametype = PETSC_TRUE;
1465: } else if (sametype) {
1466: PetscCall(MatGetVecType(s->m[i][j], &type));
1467: PetscCall(PetscStrcmp(vtype, type, &sametype));
1468: }
1469: }
1470: }
1471: }
1472: if (sametype) { /* propagate vectype */
1473: PetscCall(MatSetVecType(A, vtype));
1474: }
1475: }
1477: PetscCall(MatSetUp_NestIS_Private(A, nr, is_row, nc, is_col));
1479: PetscCall(PetscMalloc1(nr, &s->row_len));
1480: PetscCall(PetscMalloc1(nc, &s->col_len));
1481: for (i = 0; i < nr; i++) s->row_len[i] = -1;
1482: for (j = 0; j < nc; j++) s->col_len[j] = -1;
1484: PetscCall(PetscCalloc1(nr * nc, &s->nnzstate));
1485: for (i = 0; i < nr; i++) {
1486: for (j = 0; j < nc; j++) {
1487: if (s->m[i][j]) PetscCall(MatGetNonzeroState(s->m[i][j], &s->nnzstate[i * nc + j]));
1488: }
1489: }
1491: PetscCall(MatNestGetSizes_Private(A, &m, &n, &M, &N));
1493: PetscCall(PetscLayoutSetSize(A->rmap, M));
1494: PetscCall(PetscLayoutSetLocalSize(A->rmap, m));
1495: PetscCall(PetscLayoutSetSize(A->cmap, N));
1496: PetscCall(PetscLayoutSetLocalSize(A->cmap, n));
1498: PetscCall(PetscLayoutSetUp(A->rmap));
1499: PetscCall(PetscLayoutSetUp(A->cmap));
1501: /* disable operations that are not supported for non-square matrices,
1502: or matrices for which is_row != is_col */
1503: PetscCall(MatHasCongruentLayouts(A, &cong));
1504: if (cong && nr != nc) cong = PETSC_FALSE;
1505: if (cong) {
1506: for (i = 0; cong && i < nr; i++) PetscCall(ISEqualUnsorted(s->isglobal.row[i], s->isglobal.col[i], &cong));
1507: }
1508: if (!cong) {
1509: A->ops->getdiagonal = NULL;
1510: A->ops->shift = NULL;
1511: A->ops->diagonalset = NULL;
1512: }
1514: PetscCall(PetscCalloc2(nr, &s->left, nc, &s->right));
1515: PetscCall(PetscObjectStateIncrease((PetscObject)A));
1516: A->nonzerostate++;
1517: PetscFunctionReturn(PETSC_SUCCESS);
1518: }
1520: /*@
1521: MatNestSetSubMats - Sets the nested submatrices in a `MATNEST`
1523: Collective
1525: Input Parameters:
1526: + A - `MATNEST` matrix
1527: . nr - number of nested row blocks
1528: . is_row - index sets for each nested row block, or `NULL` to make contiguous
1529: . nc - number of nested column blocks
1530: . is_col - index sets for each nested column block, or `NULL` to make contiguous
1531: - a - array of $nr \times nc$ submatrices, or `NULL`
1533: Level: advanced
1535: Notes:
1536: This always resets any block matrix information previously set.
1538: Pass `NULL` in the corresponding entry of `a` for an empty block.
1540: In both C and Fortran, `a` must be a one-dimensional array representing a two-dimensional row-major order array containing the matrices. See
1541: `MatCreateNest()` for an example.
1543: Fortran Note:
1544: Pass `PETSC_NULL_MAT` in the corresponding entry of `a` for an empty block
1546: .seealso: [](ch_matrices), `Mat`, `MATNEST`, `MatCreateNest()`, `MatNestSetSubMat()`, `MatNestGetSubMat()`, `MatNestGetSubMats()`
1547: @*/
1548: PetscErrorCode MatNestSetSubMats(Mat A, PetscInt nr, const IS is_row[], PetscInt nc, const IS is_col[], const Mat a[]) PeNSS
1549: {
1550: PetscFunctionBegin;
1553: PetscCheck(nr >= 0, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_OUTOFRANGE, "Number of rows cannot be negative");
1554: if (nr && is_row) {
1555: PetscAssertPointer(is_row, 3);
1557: }
1559: PetscCheck(nc >= 0, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_OUTOFRANGE, "Number of columns cannot be negative");
1560: if (nc && is_col) {
1561: PetscAssertPointer(is_col, 5);
1563: }
1564: PetscTryMethod(A, "MatNestSetSubMats_C", (Mat, PetscInt, const IS[], PetscInt, const IS[], const Mat[]), (A, nr, is_row, nc, is_col, a));
1565: PetscFunctionReturn(PETSC_SUCCESS);
1566: }
1568: static PetscErrorCode MatNestCreateAggregateL2G_Private(Mat A, PetscInt n, const IS islocal[], const IS isglobal[], PetscBool colflg, ISLocalToGlobalMapping *ltog)
1569: {
1570: PetscBool flg;
1571: PetscInt i, j, m, mi, *ix;
1573: PetscFunctionBegin;
1574: *ltog = NULL;
1575: for (i = 0, m = 0, flg = PETSC_FALSE; i < n; i++) {
1576: if (islocal[i]) {
1577: PetscCall(ISGetLocalSize(islocal[i], &mi));
1578: flg = PETSC_TRUE; /* We found a non-trivial entry */
1579: } else {
1580: PetscCall(ISGetLocalSize(isglobal[i], &mi));
1581: }
1582: m += mi;
1583: }
1584: if (!flg) PetscFunctionReturn(PETSC_SUCCESS);
1586: PetscCall(PetscMalloc1(m, &ix));
1587: for (i = 0, m = 0; i < n; i++) {
1588: ISLocalToGlobalMapping smap = NULL;
1589: Mat sub = NULL;
1590: PetscSF sf;
1591: PetscLayout map;
1592: const PetscInt *ix2;
1594: if (!colflg) {
1595: PetscCall(MatNestFindNonzeroSubMatRow(A, i, &sub));
1596: } else {
1597: PetscCall(MatNestFindNonzeroSubMatCol(A, i, &sub));
1598: }
1599: if (sub) {
1600: if (!colflg) PetscCall(MatGetLocalToGlobalMapping(sub, &smap, NULL));
1601: else PetscCall(MatGetLocalToGlobalMapping(sub, NULL, &smap));
1602: }
1603: /*
1604: Now we need to extract the monolithic global indices that correspond to the given split global indices.
1605: In many/most cases, we only want MatGetLocalSubMatrix() to work, in which case we only need to know the size of the local spaces.
1606: */
1607: PetscCall(ISGetIndices(isglobal[i], &ix2));
1608: if (islocal[i]) {
1609: PetscInt *ilocal, *iremote;
1610: PetscInt mil, nleaves;
1612: PetscCall(ISGetLocalSize(islocal[i], &mi));
1613: PetscCheck(smap, PetscObjectComm((PetscObject)A), PETSC_ERR_PLIB, "Missing local to global map");
1614: for (j = 0; j < mi; j++) ix[m + j] = j;
1615: PetscCall(ISLocalToGlobalMappingApply(smap, mi, ix + m, ix + m));
1617: /* PetscSFSetGraphLayout does not like negative indices */
1618: PetscCall(PetscMalloc2(mi, &ilocal, mi, &iremote));
1619: for (j = 0, nleaves = 0; j < mi; j++) {
1620: if (ix[m + j] < 0) continue;
1621: ilocal[nleaves] = j;
1622: iremote[nleaves] = ix[m + j];
1623: nleaves++;
1624: }
1625: PetscCall(ISGetLocalSize(isglobal[i], &mil));
1626: PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)A), &sf));
1627: PetscCall(PetscLayoutCreate(PetscObjectComm((PetscObject)A), &map));
1628: PetscCall(PetscLayoutSetLocalSize(map, mil));
1629: PetscCall(PetscLayoutSetUp(map));
1630: PetscCall(PetscSFSetGraphLayout(sf, map, nleaves, ilocal, PETSC_USE_POINTER, iremote));
1631: PetscCall(PetscLayoutDestroy(&map));
1632: PetscCall(PetscSFBcastBegin(sf, MPIU_INT, ix2, ix + m, MPI_REPLACE));
1633: PetscCall(PetscSFBcastEnd(sf, MPIU_INT, ix2, ix + m, MPI_REPLACE));
1634: PetscCall(PetscSFDestroy(&sf));
1635: PetscCall(PetscFree2(ilocal, iremote));
1636: } else {
1637: PetscCall(ISGetLocalSize(isglobal[i], &mi));
1638: for (j = 0; j < mi; j++) ix[m + j] = ix2[j];
1639: }
1640: PetscCall(ISRestoreIndices(isglobal[i], &ix2));
1641: m += mi;
1642: }
1643: PetscCall(ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)A), 1, m, ix, PETSC_OWN_POINTER, ltog));
1644: PetscFunctionReturn(PETSC_SUCCESS);
1645: }
1647: /* If an IS was provided, there is nothing Nest needs to do, otherwise Nest will build a strided IS */
1648: /*
1649: nprocessors = NP
1650: Nest x^T = ((g_0,g_1,...g_nprocs-1), (h_0,h_1,...h_NP-1))
1651: proc 0: => (g_0,h_0,)
1652: proc 1: => (g_1,h_1,)
1653: ...
1654: proc nprocs-1: => (g_NP-1,h_NP-1,)
1656: proc 0: proc 1: proc nprocs-1:
1657: is[0] = (0,1,2,...,nlocal(g_0)-1) (0,1,...,nlocal(g_1)-1) (0,1,...,nlocal(g_NP-1))
1659: proc 0:
1660: is[1] = (nlocal(g_0),nlocal(g_0)+1,...,nlocal(g_0)+nlocal(h_0)-1)
1661: proc 1:
1662: is[1] = (nlocal(g_1),nlocal(g_1)+1,...,nlocal(g_1)+nlocal(h_1)-1)
1664: proc NP-1:
1665: is[1] = (nlocal(g_NP-1),nlocal(g_NP-1)+1,...,nlocal(g_NP-1)+nlocal(h_NP-1)-1)
1666: */
1667: static PetscErrorCode MatSetUp_NestIS_Private(Mat A, PetscInt nr, const IS is_row[], PetscInt nc, const IS is_col[])
1668: {
1669: Mat_Nest *vs = (Mat_Nest *)A->data;
1670: PetscInt i, j, offset, n, nsum, bs;
1671: Mat sub = NULL;
1673: PetscFunctionBegin;
1674: PetscCall(PetscMalloc1(nr, &vs->isglobal.row));
1675: PetscCall(PetscMalloc1(nc, &vs->isglobal.col));
1676: if (is_row) { /* valid IS is passed in */
1677: /* refs on is[] are incremented */
1678: for (i = 0; i < vs->nr; i++) {
1679: PetscCall(PetscObjectReference((PetscObject)is_row[i]));
1680: vs->isglobal.row[i] = is_row[i];
1681: }
1682: } else { /* Create the ISs by inspecting sizes of a submatrix in each row */
1683: nsum = 0;
1684: for (i = 0; i < vs->nr; i++) { /* Add up the local sizes to compute the aggregate offset */
1685: PetscCall(MatNestFindNonzeroSubMatRow(A, i, &sub));
1686: PetscCheck(sub, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "No nonzero submatrix in row %" PetscInt_FMT, i);
1687: PetscCall(MatGetLocalSize(sub, &n, NULL));
1688: PetscCheck(n >= 0, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONGSTATE, "Sizes have not yet been set for submatrix");
1689: nsum += n;
1690: }
1691: PetscCallMPI(MPI_Scan(&nsum, &offset, 1, MPIU_INT, MPI_SUM, PetscObjectComm((PetscObject)A)));
1692: offset -= nsum;
1693: for (i = 0; i < vs->nr; i++) {
1694: PetscCall(MatNestFindNonzeroSubMatRow(A, i, &sub));
1695: PetscCall(MatGetLocalSize(sub, &n, NULL));
1696: PetscCall(MatGetBlockSizes(sub, &bs, NULL));
1697: PetscCall(ISCreateStride(PetscObjectComm((PetscObject)sub), n, offset, 1, &vs->isglobal.row[i]));
1698: PetscCall(ISSetBlockSize(vs->isglobal.row[i], bs));
1699: offset += n;
1700: }
1701: }
1703: if (is_col) { /* valid IS is passed in */
1704: /* refs on is[] are incremented */
1705: for (j = 0; j < vs->nc; j++) {
1706: PetscCall(PetscObjectReference((PetscObject)is_col[j]));
1707: vs->isglobal.col[j] = is_col[j];
1708: }
1709: } else { /* Create the ISs by inspecting sizes of a submatrix in each column */
1710: offset = A->cmap->rstart;
1711: nsum = 0;
1712: for (j = 0; j < vs->nc; j++) {
1713: PetscCall(MatNestFindNonzeroSubMatCol(A, j, &sub));
1714: PetscCheck(sub, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "No nonzero submatrix in column %" PetscInt_FMT, i);
1715: PetscCall(MatGetLocalSize(sub, NULL, &n));
1716: PetscCheck(n >= 0, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONGSTATE, "Sizes have not yet been set for submatrix");
1717: nsum += n;
1718: }
1719: PetscCallMPI(MPI_Scan(&nsum, &offset, 1, MPIU_INT, MPI_SUM, PetscObjectComm((PetscObject)A)));
1720: offset -= nsum;
1721: for (j = 0; j < vs->nc; j++) {
1722: PetscCall(MatNestFindNonzeroSubMatCol(A, j, &sub));
1723: PetscCall(MatGetLocalSize(sub, NULL, &n));
1724: PetscCall(MatGetBlockSizes(sub, NULL, &bs));
1725: PetscCall(ISCreateStride(PetscObjectComm((PetscObject)sub), n, offset, 1, &vs->isglobal.col[j]));
1726: PetscCall(ISSetBlockSize(vs->isglobal.col[j], bs));
1727: offset += n;
1728: }
1729: }
1731: /* Set up the local ISs */
1732: PetscCall(PetscMalloc1(vs->nr, &vs->islocal.row));
1733: PetscCall(PetscMalloc1(vs->nc, &vs->islocal.col));
1734: for (i = 0, offset = 0; i < vs->nr; i++) {
1735: IS isloc;
1736: ISLocalToGlobalMapping rmap = NULL;
1737: PetscInt nlocal, bs;
1738: PetscCall(MatNestFindNonzeroSubMatRow(A, i, &sub));
1739: if (sub) PetscCall(MatGetLocalToGlobalMapping(sub, &rmap, NULL));
1740: if (rmap) {
1741: PetscCall(MatGetBlockSizes(sub, &bs, NULL));
1742: PetscCall(ISLocalToGlobalMappingGetSize(rmap, &nlocal));
1743: PetscCall(ISCreateStride(PETSC_COMM_SELF, nlocal, offset, 1, &isloc));
1744: PetscCall(ISSetBlockSize(isloc, bs));
1745: } else {
1746: nlocal = 0;
1747: isloc = NULL;
1748: }
1749: vs->islocal.row[i] = isloc;
1750: offset += nlocal;
1751: }
1752: for (i = 0, offset = 0; i < vs->nc; i++) {
1753: IS isloc;
1754: ISLocalToGlobalMapping cmap = NULL;
1755: PetscInt nlocal, bs;
1756: PetscCall(MatNestFindNonzeroSubMatCol(A, i, &sub));
1757: if (sub) PetscCall(MatGetLocalToGlobalMapping(sub, NULL, &cmap));
1758: if (cmap) {
1759: PetscCall(MatGetBlockSizes(sub, NULL, &bs));
1760: PetscCall(ISLocalToGlobalMappingGetSize(cmap, &nlocal));
1761: PetscCall(ISCreateStride(PETSC_COMM_SELF, nlocal, offset, 1, &isloc));
1762: PetscCall(ISSetBlockSize(isloc, bs));
1763: } else {
1764: nlocal = 0;
1765: isloc = NULL;
1766: }
1767: vs->islocal.col[i] = isloc;
1768: offset += nlocal;
1769: }
1771: /* Set up the aggregate ISLocalToGlobalMapping */
1772: {
1773: ISLocalToGlobalMapping rmap, cmap;
1774: PetscCall(MatNestCreateAggregateL2G_Private(A, vs->nr, vs->islocal.row, vs->isglobal.row, PETSC_FALSE, &rmap));
1775: PetscCall(MatNestCreateAggregateL2G_Private(A, vs->nc, vs->islocal.col, vs->isglobal.col, PETSC_TRUE, &cmap));
1776: if (rmap && cmap) PetscCall(MatSetLocalToGlobalMapping(A, rmap, cmap));
1777: PetscCall(ISLocalToGlobalMappingDestroy(&rmap));
1778: PetscCall(ISLocalToGlobalMappingDestroy(&cmap));
1779: }
1781: if (PetscDefined(USE_DEBUG)) {
1782: for (i = 0; i < vs->nr; i++) {
1783: for (j = 0; j < vs->nc; j++) {
1784: PetscInt m, n, M, N, mi, ni, Mi, Ni;
1785: Mat B = vs->m[i][j];
1786: if (!B) continue;
1787: PetscCall(MatGetSize(B, &M, &N));
1788: PetscCall(MatGetLocalSize(B, &m, &n));
1789: PetscCall(ISGetSize(vs->isglobal.row[i], &Mi));
1790: PetscCall(ISGetSize(vs->isglobal.col[j], &Ni));
1791: PetscCall(ISGetLocalSize(vs->isglobal.row[i], &mi));
1792: PetscCall(ISGetLocalSize(vs->isglobal.col[j], &ni));
1793: PetscCheck(M == Mi && N == Ni, PetscObjectComm((PetscObject)sub), PETSC_ERR_ARG_INCOMP, "Global sizes (%" PetscInt_FMT ",%" PetscInt_FMT ") of nested submatrix (%" PetscInt_FMT ",%" PetscInt_FMT ") do not agree with space defined by index sets (%" PetscInt_FMT ",%" PetscInt_FMT ")", M, N, i, j, Mi, Ni);
1794: PetscCheck(m == mi && n == ni, PetscObjectComm((PetscObject)sub), PETSC_ERR_ARG_INCOMP, "Local sizes (%" PetscInt_FMT ",%" PetscInt_FMT ") of nested submatrix (%" PetscInt_FMT ",%" PetscInt_FMT ") do not agree with space defined by index sets (%" PetscInt_FMT ",%" PetscInt_FMT ")", m, n, i, j, mi, ni);
1795: }
1796: }
1797: }
1799: /* Set A->assembled if all non-null blocks are currently assembled */
1800: for (i = 0; i < vs->nr; i++) {
1801: for (j = 0; j < vs->nc; j++) {
1802: if (vs->m[i][j] && !vs->m[i][j]->assembled) PetscFunctionReturn(PETSC_SUCCESS);
1803: }
1804: }
1805: A->assembled = PETSC_TRUE;
1806: PetscFunctionReturn(PETSC_SUCCESS);
1807: }
1809: /*@
1810: MatCreateNest - Creates a new `MATNEST` matrix containing several nested submatrices, each stored separately
1812: Collective
1814: Input Parameters:
1815: + comm - Communicator for the new `MATNEST`
1816: . nr - number of nested row blocks
1817: . is_row - index sets for each nested row block, or `NULL` to make contiguous
1818: . nc - number of nested column blocks
1819: . is_col - index sets for each nested column block, or `NULL` to make contiguous
1820: - a - array of $nr \times nc$ submatrices, empty submatrices can be passed using `NULL`
1822: Output Parameter:
1823: . B - new matrix
1825: Level: advanced
1827: Note:
1828: In both C and Fortran, `a` must be a one-dimensional array representing a two-dimensional row-major order array holding references to the matrices.
1829: For instance, to represent the matrix
1830: $\begin{bmatrix} A_{11} & A_{12} \\ A_{21} & A_{22}\end{bmatrix}$
1831: one should use `Mat a[4]={A11,A12,A21,A22}`.
1833: Fortran Note:
1834: Pass `PETSC_NULL_MAT` in the corresponding entry of `a` for an empty block
1836: .seealso: [](ch_matrices), `Mat`, `MATNEST`, `MatCreate()`, `VecCreateNest()`, `DMCreateMatrix()`, `MatNestSetSubMat()`,
1837: `MatNestGetSubMat()`, `MatNestGetLocalISs()`, `MatNestGetSize()`,
1838: `MatNestGetISs()`, `MatNestSetSubMats()`, `MatNestGetSubMats()`
1839: @*/
1840: PetscErrorCode MatCreateNest(MPI_Comm comm, PetscInt nr, const IS is_row[], PetscInt nc, const IS is_col[], const Mat a[], Mat *B) PeNSS
1841: {
1842: PetscFunctionBegin;
1843: PetscCall(MatCreate(comm, B));
1844: PetscCall(MatSetType(*B, MATNEST));
1845: (*B)->preallocated = PETSC_TRUE;
1846: PetscCall(MatNestSetSubMats(*B, nr, is_row, nc, is_col, a));
1847: PetscFunctionReturn(PETSC_SUCCESS);
1848: }
1850: static PetscErrorCode MatConvert_Nest_SeqAIJ_fast(Mat A, MatType newtype, MatReuse reuse, Mat *newmat)
1851: {
1852: Mat_Nest *nest = (Mat_Nest *)A->data;
1853: Mat *trans;
1854: PetscScalar **avv;
1855: PetscScalar *vv;
1856: PetscInt **aii, **ajj;
1857: PetscInt *ii, *jj, *ci;
1858: PetscInt nr, nc, nnz, i, j;
1859: PetscBool done;
1861: PetscFunctionBegin;
1862: PetscCall(MatGetSize(A, &nr, &nc));
1863: if (reuse == MAT_REUSE_MATRIX) {
1864: PetscInt rnr;
1866: PetscCall(MatGetRowIJ(*newmat, 0, PETSC_FALSE, PETSC_FALSE, &rnr, (const PetscInt **)&ii, (const PetscInt **)&jj, &done));
1867: PetscCheck(done, PetscObjectComm((PetscObject)A), PETSC_ERR_PLIB, "MatGetRowIJ");
1868: PetscCheck(rnr == nr, PetscObjectComm((PetscObject)A), PETSC_ERR_USER, "Cannot reuse matrix, wrong number of rows");
1869: PetscCall(MatSeqAIJGetArray(*newmat, &vv));
1870: }
1871: /* extract CSR for nested SeqAIJ matrices */
1872: nnz = 0;
1873: PetscCall(PetscCalloc4(nest->nr * nest->nc, &aii, nest->nr * nest->nc, &ajj, nest->nr * nest->nc, &avv, nest->nr * nest->nc, &trans));
1874: for (i = 0; i < nest->nr; ++i) {
1875: for (j = 0; j < nest->nc; ++j) {
1876: Mat B = nest->m[i][j];
1877: if (B) {
1878: PetscScalar *naa;
1879: PetscInt *nii, *njj, nnr;
1880: PetscBool istrans;
1882: PetscCall(PetscObjectTypeCompare((PetscObject)B, MATTRANSPOSEVIRTUAL, &istrans));
1883: if (istrans) {
1884: Mat Bt;
1886: PetscCall(MatTransposeGetMat(B, &Bt));
1887: PetscCall(MatTranspose(Bt, MAT_INITIAL_MATRIX, &trans[i * nest->nc + j]));
1888: B = trans[i * nest->nc + j];
1889: } else {
1890: PetscCall(PetscObjectTypeCompare((PetscObject)B, MATHERMITIANTRANSPOSEVIRTUAL, &istrans));
1891: if (istrans) {
1892: Mat Bt;
1894: PetscCall(MatHermitianTransposeGetMat(B, &Bt));
1895: PetscCall(MatHermitianTranspose(Bt, MAT_INITIAL_MATRIX, &trans[i * nest->nc + j]));
1896: B = trans[i * nest->nc + j];
1897: }
1898: }
1899: PetscCall(MatGetRowIJ(B, 0, PETSC_FALSE, PETSC_FALSE, &nnr, (const PetscInt **)&nii, (const PetscInt **)&njj, &done));
1900: PetscCheck(done, PetscObjectComm((PetscObject)B), PETSC_ERR_PLIB, "MatGetRowIJ");
1901: PetscCall(MatSeqAIJGetArray(B, &naa));
1902: nnz += nii[nnr];
1904: aii[i * nest->nc + j] = nii;
1905: ajj[i * nest->nc + j] = njj;
1906: avv[i * nest->nc + j] = naa;
1907: }
1908: }
1909: }
1910: if (reuse != MAT_REUSE_MATRIX) {
1911: PetscCall(PetscMalloc1(nr + 1, &ii));
1912: PetscCall(PetscMalloc1(nnz, &jj));
1913: PetscCall(PetscMalloc1(nnz, &vv));
1914: } else {
1915: PetscCheck(nnz == ii[nr], PetscObjectComm((PetscObject)A), PETSC_ERR_USER, "Cannot reuse matrix, wrong number of nonzeros");
1916: }
1918: /* new row pointer */
1919: PetscCall(PetscArrayzero(ii, nr + 1));
1920: for (i = 0; i < nest->nr; ++i) {
1921: PetscInt ncr, rst;
1923: PetscCall(ISStrideGetInfo(nest->isglobal.row[i], &rst, NULL));
1924: PetscCall(ISGetLocalSize(nest->isglobal.row[i], &ncr));
1925: for (j = 0; j < nest->nc; ++j) {
1926: if (aii[i * nest->nc + j]) {
1927: PetscInt *nii = aii[i * nest->nc + j];
1929: for (PetscInt ir = rst; ir < ncr + rst; ++ir) {
1930: ii[ir + 1] += nii[1] - nii[0];
1931: nii++;
1932: }
1933: }
1934: }
1935: }
1936: for (i = 0; i < nr; i++) ii[i + 1] += ii[i];
1938: /* construct CSR for the new matrix */
1939: PetscCall(PetscCalloc1(nr, &ci));
1940: for (i = 0; i < nest->nr; ++i) {
1941: PetscInt ncr, rst;
1943: PetscCall(ISStrideGetInfo(nest->isglobal.row[i], &rst, NULL));
1944: PetscCall(ISGetLocalSize(nest->isglobal.row[i], &ncr));
1945: for (j = 0; j < nest->nc; ++j) {
1946: if (aii[i * nest->nc + j]) {
1947: PetscScalar *nvv = avv[i * nest->nc + j], vscale = 1.0, vshift = 0.0;
1948: PetscInt *nii = aii[i * nest->nc + j];
1949: PetscInt *njj = ajj[i * nest->nc + j];
1950: PetscInt cst;
1952: if (trans[i * nest->nc + j]) {
1953: vscale = ((Mat_Shell *)nest->m[i][j]->data)->vscale;
1954: vshift = ((Mat_Shell *)nest->m[i][j]->data)->vshift;
1955: }
1956: PetscCall(ISStrideGetInfo(nest->isglobal.col[j], &cst, NULL));
1957: for (PetscInt ir = rst; ir < ncr + rst; ++ir) {
1958: PetscInt ij, rsize = nii[1] - nii[0], ist = ii[ir] + ci[ir];
1960: for (ij = 0; ij < rsize; ij++) {
1961: jj[ist + ij] = *njj + cst;
1962: vv[ist + ij] = vscale * *nvv;
1963: if (PetscUnlikely(vshift != 0.0 && *njj == ir - rst)) vv[ist + ij] += vshift;
1964: njj++;
1965: nvv++;
1966: }
1967: ci[ir] += rsize;
1968: nii++;
1969: }
1970: }
1971: }
1972: }
1973: PetscCall(PetscFree(ci));
1975: /* restore info */
1976: for (i = 0; i < nest->nr; ++i) {
1977: for (j = 0; j < nest->nc; ++j) {
1978: Mat B = nest->m[i][j];
1979: if (B) {
1980: PetscInt nnr = 0, k = i * nest->nc + j;
1982: B = (trans[k] ? trans[k] : B);
1983: PetscCall(MatRestoreRowIJ(B, 0, PETSC_FALSE, PETSC_FALSE, &nnr, (const PetscInt **)&aii[k], (const PetscInt **)&ajj[k], &done));
1984: PetscCheck(done, PetscObjectComm((PetscObject)B), PETSC_ERR_PLIB, "MatRestoreRowIJ");
1985: PetscCall(MatSeqAIJRestoreArray(B, &avv[k]));
1986: PetscCall(MatDestroy(&trans[k]));
1987: }
1988: }
1989: }
1990: PetscCall(PetscFree4(aii, ajj, avv, trans));
1992: /* finalize newmat */
1993: if (reuse == MAT_INITIAL_MATRIX) {
1994: PetscCall(MatCreateSeqAIJWithArrays(PetscObjectComm((PetscObject)A), nr, nc, ii, jj, vv, newmat));
1995: } else if (reuse == MAT_INPLACE_MATRIX) {
1996: Mat B;
1998: PetscCall(MatCreateSeqAIJWithArrays(PetscObjectComm((PetscObject)A), nr, nc, ii, jj, vv, &B));
1999: PetscCall(MatHeaderReplace(A, &B));
2000: }
2001: PetscCall(MatAssemblyBegin(*newmat, MAT_FINAL_ASSEMBLY));
2002: PetscCall(MatAssemblyEnd(*newmat, MAT_FINAL_ASSEMBLY));
2003: {
2004: Mat_SeqAIJ *a = (Mat_SeqAIJ *)(*newmat)->data;
2005: a->free_a = PETSC_TRUE;
2006: a->free_ij = PETSC_TRUE;
2007: }
2008: PetscFunctionReturn(PETSC_SUCCESS);
2009: }
2011: PETSC_INTERN PetscErrorCode MatAXPY_Dense_Nest(Mat Y, PetscScalar a, Mat X)
2012: {
2013: Mat_Nest *nest = (Mat_Nest *)X->data;
2014: PetscInt i, j, k, rstart;
2015: PetscBool flg;
2017: PetscFunctionBegin;
2018: /* Fill by row */
2019: for (j = 0; j < nest->nc; ++j) {
2020: /* Using global column indices and ISAllGather() is not scalable. */
2021: IS bNis;
2022: PetscInt bN;
2023: const PetscInt *bNindices;
2024: PetscCall(ISAllGather(nest->isglobal.col[j], &bNis));
2025: PetscCall(ISGetSize(bNis, &bN));
2026: PetscCall(ISGetIndices(bNis, &bNindices));
2027: for (i = 0; i < nest->nr; ++i) {
2028: Mat B = nest->m[i][j], D = NULL;
2029: PetscInt bm, br;
2030: const PetscInt *bmindices;
2031: if (!B) continue;
2032: PetscCall(PetscObjectTypeCompareAny((PetscObject)B, &flg, MATTRANSPOSEVIRTUAL, MATHERMITIANTRANSPOSEVIRTUAL, ""));
2033: if (flg) {
2034: PetscTryMethod(B, "MatTransposeGetMat_C", (Mat, Mat *), (B, &D));
2035: PetscTryMethod(B, "MatHermitianTransposeGetMat_C", (Mat, Mat *), (B, &D));
2036: PetscCall(MatConvert(B, ((PetscObject)D)->type_name, MAT_INITIAL_MATRIX, &D));
2037: B = D;
2038: }
2039: PetscCall(PetscObjectTypeCompareAny((PetscObject)B, &flg, MATSEQSBAIJ, MATMPISBAIJ, ""));
2040: if (flg) {
2041: if (D) PetscCall(MatConvert(D, MATBAIJ, MAT_INPLACE_MATRIX, &D));
2042: else PetscCall(MatConvert(B, MATBAIJ, MAT_INITIAL_MATRIX, &D));
2043: B = D;
2044: }
2045: PetscCall(ISGetLocalSize(nest->isglobal.row[i], &bm));
2046: PetscCall(ISGetIndices(nest->isglobal.row[i], &bmindices));
2047: PetscCall(MatGetOwnershipRange(B, &rstart, NULL));
2048: for (br = 0; br < bm; ++br) {
2049: PetscInt row = bmindices[br], brncols, *cols;
2050: const PetscInt *brcols;
2051: const PetscScalar *brcoldata;
2052: PetscScalar *vals = NULL;
2053: PetscCall(MatGetRow(B, br + rstart, &brncols, &brcols, &brcoldata));
2054: PetscCall(PetscMalloc1(brncols, &cols));
2055: for (k = 0; k < brncols; k++) cols[k] = bNindices[brcols[k]];
2056: /*
2057: Nest blocks are required to be nonoverlapping -- otherwise nest and monolithic index layouts wouldn't match.
2058: Thus, we could use INSERT_VALUES, but I prefer ADD_VALUES.
2059: */
2060: if (a != 1.0) {
2061: PetscCall(PetscMalloc1(brncols, &vals));
2062: for (k = 0; k < brncols; k++) vals[k] = a * brcoldata[k];
2063: PetscCall(MatSetValues(Y, 1, &row, brncols, cols, vals, ADD_VALUES));
2064: PetscCall(PetscFree(vals));
2065: } else {
2066: PetscCall(MatSetValues(Y, 1, &row, brncols, cols, brcoldata, ADD_VALUES));
2067: }
2068: PetscCall(MatRestoreRow(B, br + rstart, &brncols, &brcols, &brcoldata));
2069: PetscCall(PetscFree(cols));
2070: }
2071: PetscCall(MatDestroy(&D));
2072: PetscCall(ISRestoreIndices(nest->isglobal.row[i], &bmindices));
2073: }
2074: PetscCall(ISRestoreIndices(bNis, &bNindices));
2075: PetscCall(ISDestroy(&bNis));
2076: }
2077: PetscCall(MatAssemblyBegin(Y, MAT_FINAL_ASSEMBLY));
2078: PetscCall(MatAssemblyEnd(Y, MAT_FINAL_ASSEMBLY));
2079: PetscFunctionReturn(PETSC_SUCCESS);
2080: }
2082: static PetscErrorCode MatConvert_Nest_AIJ(Mat A, MatType newtype, MatReuse reuse, Mat *newmat)
2083: {
2084: Mat_Nest *nest = (Mat_Nest *)A->data;
2085: PetscInt m, n, M, N, i, j, k, *dnnz, *onnz = NULL, rstart, cstart, cend;
2086: PetscMPIInt size;
2087: Mat C;
2089: PetscFunctionBegin;
2090: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)A), &size));
2091: if (size == 1) { /* look for a special case with SeqAIJ matrices and strided-1, contiguous, blocks */
2092: PetscInt nf;
2093: PetscBool fast;
2095: PetscCall(PetscStrcmp(newtype, MATAIJ, &fast));
2096: if (!fast) PetscCall(PetscStrcmp(newtype, MATSEQAIJ, &fast));
2097: for (i = 0; i < nest->nr && fast; ++i) {
2098: for (j = 0; j < nest->nc && fast; ++j) {
2099: Mat B = nest->m[i][j];
2100: if (B) {
2101: PetscCall(PetscObjectTypeCompare((PetscObject)B, MATSEQAIJ, &fast));
2102: if (!fast) {
2103: PetscBool istrans;
2105: PetscCall(PetscObjectTypeCompare((PetscObject)B, MATTRANSPOSEVIRTUAL, &istrans));
2106: if (istrans) {
2107: Mat Bt;
2109: PetscCall(MatTransposeGetMat(B, &Bt));
2110: PetscCall(PetscObjectTypeCompare((PetscObject)Bt, MATSEQAIJ, &fast));
2111: } else {
2112: PetscCall(PetscObjectTypeCompare((PetscObject)B, MATHERMITIANTRANSPOSEVIRTUAL, &istrans));
2113: if (istrans) {
2114: Mat Bt;
2116: PetscCall(MatHermitianTransposeGetMat(B, &Bt));
2117: PetscCall(PetscObjectTypeCompare((PetscObject)Bt, MATSEQAIJ, &fast));
2118: }
2119: }
2120: if (fast) fast = (PetscBool)(!((Mat_Shell *)B->data)->zrows && !((Mat_Shell *)B->data)->zcols && !((Mat_Shell *)B->data)->axpy && !((Mat_Shell *)B->data)->left && !((Mat_Shell *)B->data)->right && !((Mat_Shell *)B->data)->dshift);
2121: }
2122: }
2123: }
2124: }
2125: for (i = 0, nf = 0; i < nest->nr && fast; ++i) {
2126: PetscCall(PetscObjectTypeCompare((PetscObject)nest->isglobal.row[i], ISSTRIDE, &fast));
2127: if (fast) {
2128: PetscInt f, s;
2130: PetscCall(ISStrideGetInfo(nest->isglobal.row[i], &f, &s));
2131: if (f != nf || s != 1) {
2132: fast = PETSC_FALSE;
2133: } else {
2134: PetscCall(ISGetSize(nest->isglobal.row[i], &f));
2135: nf += f;
2136: }
2137: }
2138: }
2139: for (i = 0, nf = 0; i < nest->nc && fast; ++i) {
2140: PetscCall(PetscObjectTypeCompare((PetscObject)nest->isglobal.col[i], ISSTRIDE, &fast));
2141: if (fast) {
2142: PetscInt f, s;
2144: PetscCall(ISStrideGetInfo(nest->isglobal.col[i], &f, &s));
2145: if (f != nf || s != 1) {
2146: fast = PETSC_FALSE;
2147: } else {
2148: PetscCall(ISGetSize(nest->isglobal.col[i], &f));
2149: nf += f;
2150: }
2151: }
2152: }
2153: if (fast) {
2154: PetscCall(MatConvert_Nest_SeqAIJ_fast(A, newtype, reuse, newmat));
2155: PetscFunctionReturn(PETSC_SUCCESS);
2156: }
2157: }
2158: PetscCall(MatGetSize(A, &M, &N));
2159: PetscCall(MatGetLocalSize(A, &m, &n));
2160: PetscCall(MatGetOwnershipRangeColumn(A, &cstart, &cend));
2161: if (reuse == MAT_REUSE_MATRIX) C = *newmat;
2162: else {
2163: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &C));
2164: PetscCall(MatSetType(C, newtype));
2165: PetscCall(MatSetSizes(C, m, n, M, N));
2166: }
2167: PetscCall(PetscMalloc1(2 * m, &dnnz));
2168: if (m) {
2169: onnz = dnnz + m;
2170: for (k = 0; k < m; k++) {
2171: dnnz[k] = 0;
2172: onnz[k] = 0;
2173: }
2174: }
2175: for (j = 0; j < nest->nc; ++j) {
2176: IS bNis;
2177: PetscInt bN;
2178: const PetscInt *bNindices;
2179: PetscBool flg;
2180: /* Using global column indices and ISAllGather() is not scalable. */
2181: PetscCall(ISAllGather(nest->isglobal.col[j], &bNis));
2182: PetscCall(ISGetSize(bNis, &bN));
2183: PetscCall(ISGetIndices(bNis, &bNindices));
2184: for (i = 0; i < nest->nr; ++i) {
2185: PetscSF bmsf;
2186: PetscSFNode *iremote;
2187: Mat B = nest->m[i][j], D = NULL;
2188: PetscInt bm, *sub_dnnz, *sub_onnz, br;
2189: const PetscInt *bmindices;
2190: if (!B) continue;
2191: PetscCall(ISGetLocalSize(nest->isglobal.row[i], &bm));
2192: PetscCall(ISGetIndices(nest->isglobal.row[i], &bmindices));
2193: PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)A), &bmsf));
2194: PetscCall(PetscMalloc1(bm, &iremote));
2195: PetscCall(PetscMalloc1(bm, &sub_dnnz));
2196: PetscCall(PetscMalloc1(bm, &sub_onnz));
2197: for (k = 0; k < bm; ++k) {
2198: sub_dnnz[k] = 0;
2199: sub_onnz[k] = 0;
2200: }
2201: PetscCall(PetscObjectTypeCompareAny((PetscObject)B, &flg, MATTRANSPOSEVIRTUAL, MATHERMITIANTRANSPOSEVIRTUAL, ""));
2202: if (flg) {
2203: PetscTryMethod(B, "MatTransposeGetMat_C", (Mat, Mat *), (B, &D));
2204: PetscTryMethod(B, "MatHermitianTransposeGetMat_C", (Mat, Mat *), (B, &D));
2205: PetscCall(MatConvert(B, ((PetscObject)D)->type_name, MAT_INITIAL_MATRIX, &D));
2206: B = D;
2207: }
2208: PetscCall(PetscObjectTypeCompareAny((PetscObject)B, &flg, MATSEQSBAIJ, MATMPISBAIJ, ""));
2209: if (flg) {
2210: if (D) PetscCall(MatConvert(D, MATBAIJ, MAT_INPLACE_MATRIX, &D));
2211: else PetscCall(MatConvert(B, MATBAIJ, MAT_INITIAL_MATRIX, &D));
2212: B = D;
2213: }
2214: /*
2215: Locate the owners for all of the locally-owned global row indices for this row block.
2216: These determine the roots of PetscSF used to communicate preallocation data to row owners.
2217: The roots correspond to the dnnz and onnz entries; thus, there are two roots per row.
2218: */
2219: PetscCall(MatGetOwnershipRange(B, &rstart, NULL));
2220: for (br = 0; br < bm; ++br) {
2221: PetscInt row = bmindices[br], brncols, col;
2222: const PetscInt *brcols;
2223: PetscInt rowrel = 0; /* row's relative index on its owner rank */
2224: PetscMPIInt rowowner = 0;
2225: PetscCall(PetscLayoutFindOwnerIndex(A->rmap, row, &rowowner, &rowrel));
2226: /* how many roots */
2227: iremote[br].rank = rowowner;
2228: iremote[br].index = rowrel; /* edge from bmdnnz to dnnz */
2229: /* get nonzero pattern */
2230: PetscCall(MatGetRow(B, br + rstart, &brncols, &brcols, NULL));
2231: for (k = 0; k < brncols; k++) {
2232: col = bNindices[brcols[k]];
2233: if (col >= A->cmap->range[rowowner] && col < A->cmap->range[rowowner + 1]) {
2234: sub_dnnz[br]++;
2235: } else {
2236: sub_onnz[br]++;
2237: }
2238: }
2239: PetscCall(MatRestoreRow(B, br + rstart, &brncols, &brcols, NULL));
2240: }
2241: PetscCall(MatDestroy(&D));
2242: PetscCall(ISRestoreIndices(nest->isglobal.row[i], &bmindices));
2243: /* bsf will have to take care of disposing of bedges. */
2244: PetscCall(PetscSFSetGraph(bmsf, m, bm, NULL, PETSC_OWN_POINTER, iremote, PETSC_OWN_POINTER));
2245: PetscCall(PetscSFReduceBegin(bmsf, MPIU_INT, sub_dnnz, dnnz, MPI_SUM));
2246: PetscCall(PetscSFReduceEnd(bmsf, MPIU_INT, sub_dnnz, dnnz, MPI_SUM));
2247: PetscCall(PetscSFReduceBegin(bmsf, MPIU_INT, sub_onnz, onnz, MPI_SUM));
2248: PetscCall(PetscSFReduceEnd(bmsf, MPIU_INT, sub_onnz, onnz, MPI_SUM));
2249: PetscCall(PetscFree(sub_dnnz));
2250: PetscCall(PetscFree(sub_onnz));
2251: PetscCall(PetscSFDestroy(&bmsf));
2252: }
2253: PetscCall(ISRestoreIndices(bNis, &bNindices));
2254: PetscCall(ISDestroy(&bNis));
2255: }
2256: /* Resize preallocation if overestimated */
2257: for (i = 0; i < m; i++) {
2258: dnnz[i] = PetscMin(dnnz[i], A->cmap->n);
2259: onnz[i] = PetscMin(onnz[i], A->cmap->N - A->cmap->n);
2260: }
2261: PetscCall(MatSeqAIJSetPreallocation(C, 0, dnnz));
2262: PetscCall(MatMPIAIJSetPreallocation(C, 0, dnnz, 0, onnz));
2263: PetscCall(PetscFree(dnnz));
2264: PetscCall(MatAXPY_Dense_Nest(C, 1.0, A));
2265: if (reuse == MAT_INPLACE_MATRIX) PetscCall(MatHeaderReplace(A, &C));
2266: else *newmat = C;
2267: PetscFunctionReturn(PETSC_SUCCESS);
2268: }
2270: static PetscErrorCode MatConvert_Nest_Dense(Mat A, MatType newtype, MatReuse reuse, Mat *newmat)
2271: {
2272: Mat B;
2273: PetscInt m, n, M, N;
2275: PetscFunctionBegin;
2276: PetscCall(MatGetSize(A, &M, &N));
2277: PetscCall(MatGetLocalSize(A, &m, &n));
2278: if (reuse == MAT_REUSE_MATRIX) {
2279: B = *newmat;
2280: PetscCall(MatZeroEntries(B));
2281: } else {
2282: PetscCall(MatCreateDense(PetscObjectComm((PetscObject)A), m, PETSC_DECIDE, M, N, NULL, &B));
2283: }
2284: PetscCall(MatAXPY_Dense_Nest(B, 1.0, A));
2285: if (reuse == MAT_INPLACE_MATRIX) PetscCall(MatHeaderReplace(A, &B));
2286: else if (reuse == MAT_INITIAL_MATRIX) *newmat = B;
2287: PetscFunctionReturn(PETSC_SUCCESS);
2288: }
2290: static PetscErrorCode MatHasOperation_Nest(Mat mat, MatOperation op, PetscBool *has)
2291: {
2292: Mat_Nest *bA = (Mat_Nest *)mat->data;
2293: PetscBool flg = PETSC_TRUE;
2295: PetscFunctionBegin;
2296: *has = PETSC_FALSE;
2297: if (op == MATOP_MULT || op == MATOP_MULT_ADD || op == MATOP_MULT_TRANSPOSE || op == MATOP_MULT_TRANSPOSE_ADD || op == MATOP_MULT_HERMITIAN_TRANSPOSE || op == MATOP_MULT_HERMITIAN_TRANS_ADD) {
2298: MatOperation opAdd;
2300: if (op == MATOP_MULT || op == MATOP_MULT_ADD) opAdd = MATOP_MULT_ADD;
2301: else if (op == MATOP_MULT_TRANSPOSE || op == MATOP_MULT_TRANSPOSE_ADD) opAdd = MATOP_MULT_TRANSPOSE_ADD;
2302: else opAdd = MATOP_MULT_HERMITIAN_TRANS_ADD;
2303: for (PetscInt j = 0; j < bA->nc && flg; j++) {
2304: for (PetscInt i = 0; i < bA->nr; i++) {
2305: if (!bA->m[i][j]) continue;
2306: PetscCall(MatHasOperation(bA->m[i][j], opAdd, &flg));
2307: if (!flg) break;
2308: }
2309: }
2310: }
2311: if (flg && ((void **)mat->ops)[op]) *has = PETSC_TRUE;
2312: PetscFunctionReturn(PETSC_SUCCESS);
2313: }
2315: /*MC
2316: MATNEST - "nest" - Matrix type consisting of nested submatrices, each stored separately.
2318: Level: intermediate
2320: Notes:
2321: This matrix type permits scalable use of `PCFIELDSPLIT` and avoids the large memory costs of extracting submatrices.
2322: It allows the use of symmetric and block formats for parts of multi-physics simulations.
2323: It is usually used with `DMCOMPOSITE` and `DMCreateMatrix()`
2325: Each of the submatrices lives on the same MPI communicator as the original nest matrix (though they can have zero
2326: rows/columns on some processes.) Thus this is not meant for cases where the submatrices live on far fewer processes
2327: than the nest matrix.
2329: .seealso: [](ch_matrices), `Mat`, `MATNEST`, `MatCreate()`, `MatType`, `MatCreateNest()`, `MatNestSetSubMat()`, `MatNestGetSubMat()`,
2330: `VecCreateNest()`, `DMCreateMatrix()`, `DMCOMPOSITE`, `MatNestSetVecType()`, `MatNestGetLocalISs()`,
2331: `MatNestGetISs()`, `MatNestSetSubMats()`, `MatNestGetSubMats()`
2332: M*/
2333: PETSC_EXTERN PetscErrorCode MatCreate_Nest(Mat A)
2334: {
2335: Mat_Nest *s;
2337: PetscFunctionBegin;
2338: PetscCall(PetscNew(&s));
2339: A->data = (void *)s;
2341: s->nr = -1;
2342: s->nc = -1;
2343: s->m = NULL;
2344: s->splitassembly = PETSC_FALSE;
2346: PetscCall(PetscMemzero(A->ops, sizeof(*A->ops)));
2348: A->ops->mult = MatMult_Nest;
2349: A->ops->multadd = MatMultAdd_Nest;
2350: A->ops->multtranspose = MatMultTranspose_Nest;
2351: A->ops->multtransposeadd = MatMultTransposeAdd_Nest;
2352: A->ops->transpose = MatTranspose_Nest;
2353: A->ops->multhermitiantranspose = MatMultHermitianTranspose_Nest;
2354: A->ops->multhermitiantransposeadd = MatMultHermitianTransposeAdd_Nest;
2355: A->ops->assemblybegin = MatAssemblyBegin_Nest;
2356: A->ops->assemblyend = MatAssemblyEnd_Nest;
2357: A->ops->zeroentries = MatZeroEntries_Nest;
2358: A->ops->copy = MatCopy_Nest;
2359: A->ops->axpy = MatAXPY_Nest;
2360: A->ops->duplicate = MatDuplicate_Nest;
2361: A->ops->createsubmatrix = MatCreateSubMatrix_Nest;
2362: A->ops->destroy = MatDestroy_Nest;
2363: A->ops->view = MatView_Nest;
2364: A->ops->getlocalsubmatrix = MatGetLocalSubMatrix_Nest;
2365: A->ops->restorelocalsubmatrix = MatRestoreLocalSubMatrix_Nest;
2366: A->ops->getdiagonal = MatGetDiagonal_Nest;
2367: A->ops->diagonalscale = MatDiagonalScale_Nest;
2368: A->ops->scale = MatScale_Nest;
2369: A->ops->shift = MatShift_Nest;
2370: A->ops->diagonalset = MatDiagonalSet_Nest;
2371: A->ops->setrandom = MatSetRandom_Nest;
2372: A->ops->hasoperation = MatHasOperation_Nest;
2374: A->spptr = NULL;
2375: A->assembled = PETSC_FALSE;
2377: /* expose Nest api's */
2378: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatNestGetSubMat_C", MatNestGetSubMat_Nest));
2379: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatNestSetSubMat_C", MatNestSetSubMat_Nest));
2380: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatNestGetSubMats_C", MatNestGetSubMats_Nest));
2381: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatNestGetSize_C", MatNestGetSize_Nest));
2382: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatNestGetISs_C", MatNestGetISs_Nest));
2383: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatNestGetLocalISs_C", MatNestGetLocalISs_Nest));
2384: /* Use VECNEST by calling MatNestSetVecType(A, VECNEST) */
2385: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatNestSetVecType_C", MatNestSetVecType_Nest));
2386: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatNestSetSubMats_C", MatNestSetSubMats_Nest));
2387: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_nest_mpiaij_C", MatConvert_Nest_AIJ));
2388: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_nest_seqaij_C", MatConvert_Nest_AIJ));
2389: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_nest_aij_C", MatConvert_Nest_AIJ));
2390: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_nest_is_C", MatConvert_Nest_IS));
2391: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_nest_mpidense_C", MatConvert_Nest_Dense));
2392: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_nest_seqdense_C", MatConvert_Nest_Dense));
2393: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatProductSetFromOptions_nest_seqdense_C", MatProductSetFromOptions_Nest_Dense));
2394: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatProductSetFromOptions_nest_mpidense_C", MatProductSetFromOptions_Nest_Dense));
2396: PetscCall(PetscObjectChangeTypeName((PetscObject)A, MATNEST));
2397: PetscFunctionReturn(PETSC_SUCCESS);
2398: }