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