Actual source code: mpisbaij.c
1: #include <../src/mat/impls/baij/mpi/mpibaij.h>
2: #include <../src/mat/impls/sbaij/mpi/mpisbaij.h>
3: #include <../src/mat/impls/sbaij/seq/sbaij.h>
4: #include <../src/mat/impls/dense/seq/dense.h>
5: #include <petscblaslapack.h>
6: #include <petscsf.h>
8: static PetscErrorCode MatDestroy_MPISBAIJ(Mat mat)
9: {
10: Mat_MPISBAIJ *baij = (Mat_MPISBAIJ *)mat->data;
12: PetscFunctionBegin;
13: PetscCall(PetscLogObjectState((PetscObject)mat, "Rows=%" PetscInt_FMT ",Cols=%" PetscInt_FMT, mat->rmap->N, mat->cmap->N));
14: PetscCall(MatStashDestroy_Private(&mat->stash));
15: PetscCall(MatStashDestroy_Private(&mat->bstash));
16: PetscCall(MatDestroy(&baij->A));
17: PetscCall(MatDestroy(&baij->B));
18: #if PetscDefined(USE_CTABLE)
19: PetscCall(PetscHMapIDestroy(&baij->colmap));
20: #else
21: PetscCall(PetscFree(baij->colmap));
22: #endif
23: PetscCall(PetscFree(baij->garray));
24: PetscCall(VecDestroy(&baij->lvec));
25: PetscCall(VecScatterDestroy(&baij->Mvctx));
26: PetscCall(VecDestroy(&baij->slvec0));
27: PetscCall(VecDestroy(&baij->slvec0b));
28: PetscCall(VecDestroy(&baij->slvec1));
29: PetscCall(VecDestroy(&baij->slvec1a));
30: PetscCall(VecDestroy(&baij->slvec1b));
31: PetscCall(VecScatterDestroy(&baij->sMvctx));
32: PetscCall(PetscFree2(baij->rowvalues, baij->rowindices));
33: PetscCall(PetscFree(baij->barray));
34: PetscCall(PetscFree(baij->hd));
35: PetscCall(VecDestroy(&baij->diag));
36: PetscCall(VecDestroy(&baij->bb1));
37: PetscCall(VecDestroy(&baij->xx1));
38: #if PetscDefined(USE_REAL_MAT_SINGLE)
39: PetscCall(PetscFree(baij->setvaluescopy));
40: #endif
41: PetscCall(PetscFree(baij->in_loc));
42: PetscCall(PetscFree(baij->v_loc));
43: PetscCall(PetscFree(baij->rangebs));
44: PetscCall(PetscFree(mat->data));
46: PetscCall(PetscObjectChangeTypeName((PetscObject)mat, NULL));
47: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatStoreValues_C", NULL));
48: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatRetrieveValues_C", NULL));
49: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_mpisbaij_mpidense_C", NULL));
50: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatGetMultPetscSF_C", NULL));
51: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatMPISBAIJSetPreallocation_C", NULL));
52: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatMPISBAIJSetPreallocationCSR_C", NULL));
53: #if PetscDefined(HAVE_ELEMENTAL)
54: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_mpisbaij_elemental_C", NULL));
55: #endif
56: #if PetscDefined(HAVE_SCALAPACK) && (PetscDefined(USE_REAL_SINGLE) || PetscDefined(USE_REAL_DOUBLE))
57: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_mpisbaij_scalapack_C", NULL));
58: #endif
59: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_mpisbaij_mpiaij_C", NULL));
60: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_mpisbaij_mpibaij_C", NULL));
61: PetscFunctionReturn(PETSC_SUCCESS);
62: }
64: /* defines MatSetValues_MPI_Hash(), MatAssemblyBegin_MPI_Hash(), MatAssemblyEnd_MPI_Hash(), MatSetUp_MPI_Hash() */
65: #define TYPE SBAIJ
66: #define TYPE_SBAIJ
67: #include "../src/mat/impls/aij/mpi/mpihashmat.h"
68: #undef TYPE
69: #undef TYPE_SBAIJ
71: #if PetscDefined(HAVE_ELEMENTAL)
72: PETSC_INTERN PetscErrorCode MatConvert_MPISBAIJ_Elemental(Mat, MatType, MatReuse, Mat *);
73: #endif
74: #if PetscDefined(HAVE_SCALAPACK) && (PetscDefined(USE_REAL_SINGLE) || PetscDefined(USE_REAL_DOUBLE))
75: PETSC_INTERN PetscErrorCode MatConvert_SBAIJ_ScaLAPACK(Mat, MatType, MatReuse, Mat *);
76: #endif
78: /* This could be moved to matimpl.h */
79: static PetscErrorCode MatPreallocateWithMats_Private(Mat B, PetscInt nm, Mat X[], PetscBool symm[], PetscBool fill)
80: {
81: Mat preallocator;
82: PetscInt r, rstart, rend;
83: PetscInt bs, i, m, n, M, N;
84: PetscBool cong = PETSC_TRUE;
86: PetscFunctionBegin;
89: for (i = 0; i < nm; i++) {
91: PetscCall(PetscLayoutCompare(B->rmap, X[i]->rmap, &cong));
92: PetscCheck(cong, PetscObjectComm((PetscObject)B), PETSC_ERR_SUP, "Not for different layouts");
93: }
95: PetscCall(MatGetBlockSize(B, &bs));
96: PetscCall(MatGetSize(B, &M, &N));
97: PetscCall(MatGetLocalSize(B, &m, &n));
98: PetscCall(MatCreate(PetscObjectComm((PetscObject)B), &preallocator));
99: PetscCall(MatSetType(preallocator, MATPREALLOCATOR));
100: PetscCall(MatSetBlockSize(preallocator, bs));
101: PetscCall(MatSetSizes(preallocator, m, n, M, N));
102: PetscCall(MatSetUp(preallocator));
103: PetscCall(MatGetOwnershipRange(preallocator, &rstart, &rend));
104: for (r = rstart; r < rend; ++r) {
105: PetscInt ncols;
106: const PetscInt *row;
108: for (i = 0; i < nm; i++) {
109: PetscCall(MatGetRow(X[i], r, &ncols, &row, NULL));
110: PetscCall(MatSetValues(preallocator, 1, &r, ncols, row, NULL, INSERT_VALUES));
111: if (symm && symm[i]) PetscCall(MatSetValues(preallocator, ncols, row, 1, &r, NULL, INSERT_VALUES));
112: PetscCall(MatRestoreRow(X[i], r, &ncols, &row, NULL));
113: }
114: }
115: PetscCall(MatAssemblyBegin(preallocator, MAT_FINAL_ASSEMBLY));
116: PetscCall(MatAssemblyEnd(preallocator, MAT_FINAL_ASSEMBLY));
117: PetscCall(MatPreallocatorPreallocate(preallocator, fill, B));
118: PetscCall(MatDestroy(&preallocator));
119: PetscFunctionReturn(PETSC_SUCCESS);
120: }
122: PETSC_INTERN PetscErrorCode MatSBAIJCreateSymmetricStructure_Private(Mat A, MatType newtype, PetscBool structure_only, Mat *B)
123: {
124: PetscBool symm = PETSC_TRUE, isdense;
125: PetscInt bs;
127: PetscFunctionBegin;
128: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), B));
129: PetscCall(MatSetSizes(*B, A->rmap->n, A->cmap->n, A->rmap->N, A->cmap->N));
130: PetscCall(MatSetType(*B, newtype));
131: PetscCall(MatSetOption(*B, MAT_STRUCTURE_ONLY, structure_only));
132: PetscCall(MatGetBlockSize(A, &bs));
133: PetscCall(MatSetBlockSize(*B, bs));
134: PetscCall(PetscLayoutSetUp((*B)->rmap));
135: PetscCall(PetscLayoutSetUp((*B)->cmap));
136: PetscCall(PetscObjectTypeCompareAny((PetscObject)*B, &isdense, MATSEQDENSE, MATMPIDENSE, MATSEQDENSECUDA, ""));
137: if (!isdense) {
138: /* create the complete symmetric nonzero structure */
139: PetscCall(MatGetRowUpperTriangular(A));
140: PetscCall(MatPreallocateWithMats_Private(*B, 1, &A, &symm, PETSC_TRUE));
141: PetscCall(MatRestoreRowUpperTriangular(A));
142: } else PetscCall(MatSetUp(*B));
143: PetscFunctionReturn(PETSC_SUCCESS);
144: }
146: PETSC_INTERN PetscErrorCode MatConvert_MPISBAIJ_Basic(Mat A, MatType newtype, MatReuse reuse, Mat *newmat)
147: {
148: Mat B;
150: PetscFunctionBegin;
151: if (reuse != MAT_REUSE_MATRIX) PetscCall(MatSBAIJCreateSymmetricStructure_Private(A, newtype, PETSC_FALSE, &B));
152: else {
153: B = *newmat;
154: PetscCall(MatZeroEntries(B));
155: }
157: PetscCall(MatGetRowUpperTriangular(A));
158: for (PetscInt r = A->rmap->rstart; r < A->rmap->rend; r++) {
159: PetscInt ncols;
160: const PetscInt *row;
161: const PetscScalar *vals;
163: PetscCall(MatGetRow(A, r, &ncols, &row, &vals));
164: PetscCall(MatSetValues(B, 1, &r, ncols, row, vals, INSERT_VALUES));
165: if (PetscDefined(USE_COMPLEX) && A->hermitian == PETSC_BOOL3_TRUE) {
166: PetscInt i;
167: for (i = 0; i < ncols; i++) PetscCall(MatSetValue(B, row[i], r, PetscConj(vals[i]), INSERT_VALUES));
168: } else {
169: PetscCall(MatSetValues(B, ncols, row, 1, &r, vals, INSERT_VALUES));
170: }
171: PetscCall(MatRestoreRow(A, r, &ncols, &row, &vals));
172: }
173: PetscCall(MatRestoreRowUpperTriangular(A));
174: PetscCall(MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY));
175: PetscCall(MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY));
177: if (reuse == MAT_INPLACE_MATRIX) {
178: PetscCall(MatHeaderReplace(A, &B));
179: } else {
180: *newmat = B;
181: }
182: PetscFunctionReturn(PETSC_SUCCESS);
183: }
185: static PetscErrorCode MatStoreValues_MPISBAIJ(Mat mat)
186: {
187: Mat_MPISBAIJ *aij = (Mat_MPISBAIJ *)mat->data;
189: PetscFunctionBegin;
190: PetscCall(MatStoreValues(aij->A));
191: PetscCall(MatStoreValues(aij->B));
192: PetscFunctionReturn(PETSC_SUCCESS);
193: }
195: static PetscErrorCode MatRetrieveValues_MPISBAIJ(Mat mat)
196: {
197: Mat_MPISBAIJ *aij = (Mat_MPISBAIJ *)mat->data;
199: PetscFunctionBegin;
200: PetscCall(MatRetrieveValues(aij->A));
201: PetscCall(MatRetrieveValues(aij->B));
202: PetscFunctionReturn(PETSC_SUCCESS);
203: }
205: #define MatSetValues_SeqSBAIJ_A_Private(row, col, value, addv, orow, ocol) \
206: do { \
207: brow = (row) / bs; \
208: rp = aj + ai[brow]; \
209: if (!A->structure_only) ap = aa + bs2 * ai[brow]; \
210: rmax = aimax[brow]; \
211: nrow = ailen[brow]; \
212: bcol = (col) / bs; \
213: ridx = (row) % bs; \
214: cidx = (col) % bs; \
215: low = 0; \
216: high = nrow; \
217: while (high - low > 3) { \
218: t = (low + high) / 2; \
219: if (rp[t] > bcol) high = t; \
220: else low = t; \
221: } \
222: for (_i = low; _i < high; _i++) { \
223: if (rp[_i] > bcol) break; \
224: if (rp[_i] == bcol) { \
225: if (A->structure_only) goto a_noinsert; \
226: bap = ap + bs2 * _i + bs * cidx + ridx; \
227: if (addv == ADD_VALUES) *bap += value; \
228: else *bap = value; \
229: goto a_noinsert; \
230: } \
231: } \
232: if (a->nonew == 1) goto a_noinsert; \
233: PetscCheck(a->nonew != -1, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Inserting a new nonzero at global row/column (%" PetscInt_FMT ", %" PetscInt_FMT ") into matrix", orow, ocol); \
234: if (A->structure_only) MatSeqXAIJReallocateAIJ_structure_only(A, a->mbs, bs2, nrow, brow, bcol, rmax, ai, aj, rp, aimax, a->nonew, MatScalar); \
235: else MatSeqXAIJReallocateAIJ(A, a->mbs, bs2, nrow, brow, bcol, rmax, aa, ai, aj, rp, ap, aimax, a->nonew, MatScalar); \
236: N = nrow++ - 1; \
237: /* shift up all the later entries in this row */ \
238: PetscCall(PetscArraymove(rp + _i + 1, rp + _i, N - _i + 1)); \
239: rp[_i] = bcol; \
240: if (!A->structure_only) { \
241: PetscCall(PetscArraymove(ap + bs2 * (_i + 1), ap + bs2 * _i, bs2 * (N - _i + 1))); \
242: PetscCall(PetscArrayzero(ap + bs2 * _i, bs2)); \
243: ap[bs2 * _i + bs * cidx + ridx] = value; \
244: } \
245: a_noinsert:; \
246: ailen[brow] = nrow; \
247: } while (0)
249: #define MatSetValues_SeqSBAIJ_B_Private(row, col, value, addv, orow, ocol) \
250: do { \
251: brow = (row) / bs; \
252: rp = bj + bi[brow]; \
253: if (!B->structure_only) ap = ba + bs2 * bi[brow]; \
254: rmax = bimax[brow]; \
255: nrow = bilen[brow]; \
256: bcol = (col) / bs; \
257: ridx = (row) % bs; \
258: cidx = (col) % bs; \
259: low = 0; \
260: high = nrow; \
261: while (high - low > 3) { \
262: t = (low + high) / 2; \
263: if (rp[t] > bcol) high = t; \
264: else low = t; \
265: } \
266: for (_i = low; _i < high; _i++) { \
267: if (rp[_i] > bcol) break; \
268: if (rp[_i] == bcol) { \
269: if (B->structure_only) goto b_noinsert; \
270: bap = ap + bs2 * _i + bs * cidx + ridx; \
271: if (addv == ADD_VALUES) *bap += value; \
272: else *bap = value; \
273: goto b_noinsert; \
274: } \
275: } \
276: if (b->nonew == 1) goto b_noinsert; \
277: PetscCheck(b->nonew != -1, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Inserting a new nonzero at global row/column (%" PetscInt_FMT ", %" PetscInt_FMT ") into matrix", orow, ocol); \
278: if (B->structure_only) MatSeqXAIJReallocateAIJ_structure_only(B, b->mbs, bs2, nrow, brow, bcol, rmax, bi, bj, rp, bimax, b->nonew, MatScalar); \
279: else MatSeqXAIJReallocateAIJ(B, b->mbs, bs2, nrow, brow, bcol, rmax, ba, bi, bj, rp, ap, bimax, b->nonew, MatScalar); \
280: N = nrow++ - 1; \
281: /* shift up all the later entries in this row */ \
282: PetscCall(PetscArraymove(rp + _i + 1, rp + _i, N - _i + 1)); \
283: rp[_i] = bcol; \
284: if (!B->structure_only) { \
285: PetscCall(PetscArraymove(ap + bs2 * (_i + 1), ap + bs2 * _i, bs2 * (N - _i + 1))); \
286: PetscCall(PetscArrayzero(ap + bs2 * _i, bs2)); \
287: ap[bs2 * _i + bs * cidx + ridx] = value; \
288: } \
289: b_noinsert:; \
290: bilen[brow] = nrow; \
291: } while (0)
293: /* Only add/insert a(i,j) with i<=j (blocks).
294: Any a(i,j) with i>j input by user is ignored or generates an error
295: */
296: static PetscErrorCode MatSetValues_MPISBAIJ(Mat mat, PetscInt m, const PetscInt im[], PetscInt n, const PetscInt in[], const PetscScalar v[], InsertMode addv)
297: {
298: Mat_MPISBAIJ *baij = (Mat_MPISBAIJ *)mat->data;
299: MatScalar value = 0.0;
300: PetscBool roworiented = baij->roworiented;
301: PetscInt i, j, row, col;
302: PetscInt rstart_orig = mat->rmap->rstart;
303: PetscInt rend_orig = mat->rmap->rend, cstart_orig = mat->cmap->rstart;
304: PetscInt cend_orig = mat->cmap->rend, bs = mat->rmap->bs;
306: /* Some Variables required in the macro */
307: Mat A = baij->A;
308: Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ *)A->data;
309: PetscInt *aimax = a->imax, *ai = a->i, *ailen = a->ilen, *aj = a->j;
310: MatScalar *aa = a->a;
312: Mat B = baij->B;
313: Mat_SeqBAIJ *b = (Mat_SeqBAIJ *)B->data;
314: PetscInt *bimax = b->imax, *bi = b->i, *bilen = b->ilen, *bj = b->j;
315: MatScalar *ba = b->a;
317: PetscInt *rp, ii, nrow, _i, rmax, N, brow, bcol;
318: PetscInt low, high, t, ridx, cidx, bs2 = a->bs2;
319: MatScalar *ap = NULL, *bap;
321: /* for stash */
322: PetscInt n_loc, *in_loc = NULL;
323: MatScalar *v_loc = NULL;
325: PetscFunctionBegin;
326: if (!baij->donotstash) {
327: if (n > baij->n_loc) {
328: PetscCall(PetscFree(baij->in_loc));
329: PetscCall(PetscFree(baij->v_loc));
330: PetscCall(PetscMalloc1(n, &baij->in_loc));
331: PetscCall(PetscMalloc1(n, &baij->v_loc));
333: baij->n_loc = n;
334: }
335: in_loc = baij->in_loc;
336: v_loc = baij->v_loc;
337: }
339: for (i = 0; i < m; i++) {
340: if (im[i] < 0) continue;
341: PetscCheck(im[i] < mat->rmap->N, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Row too large: row %" PetscInt_FMT " max %" PetscInt_FMT, im[i], mat->rmap->N - 1);
342: if (im[i] >= rstart_orig && im[i] < rend_orig) { /* this processor entry */
343: row = im[i] - rstart_orig; /* local row index */
344: for (j = 0; j < n; j++) {
345: if (im[i] / bs > in[j] / bs) {
346: PetscCheck(a->ignore_ltriangular, PETSC_COMM_SELF, PETSC_ERR_USER, "Lower triangular value cannot be set for sbaij format. Ignoring these values, run with -mat_ignore_lower_triangular or call MatSetOption(mat,MAT_IGNORE_LOWER_TRIANGULAR,PETSC_TRUE)");
347: continue; /* ignore lower triangular blocks */
348: }
349: if (in[j] >= cstart_orig && in[j] < cend_orig) { /* diag entry (A) */
350: col = in[j] - cstart_orig; /* local col index */
351: brow = row / bs;
352: bcol = col / bs;
353: if (brow > bcol) continue; /* ignore lower triangular blocks of A */
354: if (!mat->structure_only) {
355: if (roworiented) value = v[i * n + j];
356: else value = v[i + j * m];
357: }
358: MatSetValues_SeqSBAIJ_A_Private(row, col, value, addv, im[i], in[j]);
359: } else if (in[j] < 0) {
360: continue;
361: } else {
362: PetscCheck(in[j] < mat->cmap->N, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Column too large: col %" PetscInt_FMT " max %" PetscInt_FMT, in[j], mat->cmap->N - 1);
363: /* off-diag entry (B) */
364: if (mat->was_assembled) {
365: if (!baij->colmap) PetscCall(MatCreateColmap_MPIBAIJ_Private(mat));
366: #if PetscDefined(USE_CTABLE)
367: PetscCall(PetscHMapIGetWithDefault(baij->colmap, in[j] / bs + 1, 0, &col));
368: col = col - 1;
369: #else
370: col = baij->colmap[in[j] / bs] - 1;
371: #endif
372: if (col < 0 && !((Mat_SeqSBAIJ *)baij->A->data)->nonew) {
373: PetscCall(MatDisAssemble_MPISBAIJ(mat));
374: col = in[j];
375: /* Reinitialize the variables required by MatSetValues_SeqBAIJ_B_Private() */
376: B = baij->B;
377: b = (Mat_SeqBAIJ *)B->data;
378: bimax = b->imax;
379: bi = b->i;
380: bilen = b->ilen;
381: bj = b->j;
382: ba = b->a;
383: } else col += in[j] % bs;
384: } else col = in[j];
385: if (!mat->structure_only) {
386: if (roworiented) value = v[i * n + j];
387: else value = v[i + j * m];
388: }
389: MatSetValues_SeqSBAIJ_B_Private(row, col, value, addv, im[i], in[j]);
390: /* PetscCall(MatSetValues_SeqBAIJ(baij->B,1,&row,1,&col,&value,addv)); */
391: }
392: }
393: } else { /* off processor entry */
394: PetscCheck(!mat->nooffprocentries, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Setting off process row %" PetscInt_FMT " even though MatSetOption(,MAT_NO_OFF_PROC_ENTRIES,PETSC_TRUE) was set", im[i]);
395: if (!baij->donotstash) {
396: mat->assembled = PETSC_FALSE;
397: n_loc = 0;
398: for (j = 0; j < n; j++) {
399: if (im[i] / bs > in[j] / bs) continue; /* ignore lower triangular blocks */
400: in_loc[n_loc] = in[j];
401: if (mat->structure_only) v_loc[n_loc] = 0;
402: else if (roworiented) v_loc[n_loc] = v[i * n + j];
403: else v_loc[n_loc] = v[j * m + i];
404: n_loc++;
405: }
406: PetscCall(MatStashValuesRow_Private(&mat->stash, im[i], n_loc, in_loc, v_loc, PETSC_FALSE));
407: }
408: }
409: }
410: PetscFunctionReturn(PETSC_SUCCESS);
411: }
413: static inline PetscErrorCode MatSetValuesBlocked_SeqSBAIJ_Inlined(Mat A, PetscInt row, PetscInt col, const PetscScalar v[], InsertMode is, PetscInt orow, PetscInt ocol)
414: {
415: Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ *)A->data;
416: PetscInt *rp, low, high, t, ii, jj, nrow, i, rmax, N;
417: PetscInt *imax = a->imax, *ai = a->i, *ailen = a->ilen;
418: PetscInt *aj = a->j, nonew = a->nonew, bs2 = a->bs2, bs = A->rmap->bs;
419: PetscBool roworiented = a->roworiented;
420: const PetscScalar *value = v;
421: MatScalar *ap, *aa = a->a, *bap;
423: PetscFunctionBegin;
424: if (col < row) {
425: PetscCheck(a->ignore_ltriangular, PETSC_COMM_SELF, PETSC_ERR_USER, "Lower triangular value cannot be set for sbaij format. Ignoring these values, run with -mat_ignore_lower_triangular or call MatSetOption(mat,MAT_IGNORE_LOWER_TRIANGULAR,PETSC_TRUE)");
426: PetscFunctionReturn(PETSC_SUCCESS); /* ignore lower triangular block */
427: }
428: rp = aj + ai[row];
429: ap = PetscSafePointerPlusOffset(aa, bs2 * ai[row]);
430: rmax = imax[row];
431: nrow = ailen[row];
432: value = v;
433: low = 0;
434: high = nrow;
436: while (high - low > 7) {
437: t = (low + high) / 2;
438: if (rp[t] > col) high = t;
439: else low = t;
440: }
441: for (i = low; i < high; i++) {
442: if (rp[i] > col) break;
443: if (rp[i] == col) {
444: if (A->structure_only) goto noinsert2;
445: bap = ap + bs2 * i;
446: if (roworiented) {
447: if (is == ADD_VALUES) {
448: for (ii = 0; ii < bs; ii++) {
449: for (jj = ii; jj < bs2; jj += bs) bap[jj] += *value++;
450: }
451: } else {
452: for (ii = 0; ii < bs; ii++) {
453: for (jj = ii; jj < bs2; jj += bs) bap[jj] = *value++;
454: }
455: }
456: } else {
457: if (is == ADD_VALUES) {
458: for (ii = 0; ii < bs; ii++) {
459: for (jj = 0; jj < bs; jj++) *bap++ += *value++;
460: }
461: } else {
462: for (ii = 0; ii < bs; ii++) {
463: for (jj = 0; jj < bs; jj++) *bap++ = *value++;
464: }
465: }
466: }
467: goto noinsert2;
468: }
469: }
470: if (nonew == 1) goto noinsert2;
471: PetscCheck(nonew != -1, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Inserting a new block index nonzero block (%" PetscInt_FMT ", %" PetscInt_FMT ") in the matrix", orow, ocol);
472: if (A->structure_only) MatSeqXAIJReallocateAIJ_structure_only(A, a->mbs, bs2, nrow, row, col, rmax, ai, aj, rp, imax, nonew, MatScalar);
473: else MatSeqXAIJReallocateAIJ(A, a->mbs, bs2, nrow, row, col, rmax, aa, ai, aj, rp, ap, imax, nonew, MatScalar);
474: N = nrow++ - 1;
475: high++;
476: /* shift up all the later entries in this row */
477: PetscCall(PetscArraymove(rp + i + 1, rp + i, N - i + 1));
478: rp[i] = col;
479: if (!A->structure_only) {
480: PetscCall(PetscArraymove(ap + bs2 * (i + 1), ap + bs2 * i, bs2 * (N - i + 1)));
481: bap = ap + bs2 * i;
482: if (roworiented) {
483: for (ii = 0; ii < bs; ii++) {
484: for (jj = ii; jj < bs2; jj += bs) bap[jj] = *value++;
485: }
486: } else {
487: for (ii = 0; ii < bs; ii++) {
488: for (jj = 0; jj < bs; jj++) *bap++ = *value++;
489: }
490: }
491: }
492: noinsert2:;
493: ailen[row] = nrow;
494: PetscFunctionReturn(PETSC_SUCCESS);
495: }
497: /*
498: This routine is exactly duplicated in mpibaij.c
499: */
500: static inline PetscErrorCode MatSetValuesBlocked_SeqBAIJ_Inlined(Mat A, PetscInt row, PetscInt col, const PetscScalar v[], InsertMode is, PetscInt orow, PetscInt ocol)
501: {
502: Mat_SeqBAIJ *a = (Mat_SeqBAIJ *)A->data;
503: PetscInt *rp, low, high, t, ii, jj, nrow, i, rmax, N;
504: PetscInt *imax = a->imax, *ai = a->i, *ailen = a->ilen;
505: PetscInt *aj = a->j, nonew = a->nonew, bs2 = a->bs2, bs = A->rmap->bs;
506: PetscBool roworiented = a->roworiented;
507: const PetscScalar *value = v;
508: MatScalar *ap, *aa = a->a, *bap;
510: PetscFunctionBegin;
511: rp = aj + ai[row];
512: ap = PetscSafePointerPlusOffset(aa, bs2 * ai[row]);
513: rmax = imax[row];
514: nrow = ailen[row];
515: low = 0;
516: high = nrow;
517: value = v;
518: while (high - low > 7) {
519: t = (low + high) / 2;
520: if (rp[t] > col) high = t;
521: else low = t;
522: }
523: for (i = low; i < high; i++) {
524: if (rp[i] > col) break;
525: if (rp[i] == col) {
526: if (A->structure_only) goto noinsert2;
527: bap = ap + bs2 * i;
528: if (roworiented) {
529: if (is == ADD_VALUES) {
530: for (ii = 0; ii < bs; ii++) {
531: for (jj = ii; jj < bs2; jj += bs) bap[jj] += *value++;
532: }
533: } else {
534: for (ii = 0; ii < bs; ii++) {
535: for (jj = ii; jj < bs2; jj += bs) bap[jj] = *value++;
536: }
537: }
538: } else {
539: if (is == ADD_VALUES) {
540: for (ii = 0; ii < bs; ii++, value += bs) {
541: for (jj = 0; jj < bs; jj++) bap[jj] += value[jj];
542: bap += bs;
543: }
544: } else {
545: for (ii = 0; ii < bs; ii++, value += bs) {
546: for (jj = 0; jj < bs; jj++) bap[jj] = value[jj];
547: bap += bs;
548: }
549: }
550: }
551: goto noinsert2;
552: }
553: }
554: if (nonew == 1) goto noinsert2;
555: PetscCheck(nonew != -1, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Inserting a new global block indexed nonzero block (%" PetscInt_FMT ", %" PetscInt_FMT ") in the matrix", orow, ocol);
556: if (A->structure_only) MatSeqXAIJReallocateAIJ_structure_only(A, a->mbs, bs2, nrow, row, col, rmax, ai, aj, rp, imax, nonew, MatScalar);
557: else MatSeqXAIJReallocateAIJ(A, a->mbs, bs2, nrow, row, col, rmax, aa, ai, aj, rp, ap, imax, nonew, MatScalar);
558: N = nrow++ - 1;
559: high++;
560: /* shift up all the later entries in this row */
561: PetscCall(PetscArraymove(rp + i + 1, rp + i, N - i + 1));
562: rp[i] = col;
563: if (!A->structure_only) {
564: PetscCall(PetscArraymove(ap + bs2 * (i + 1), ap + bs2 * i, bs2 * (N - i + 1)));
565: bap = ap + bs2 * i;
566: if (roworiented) {
567: for (ii = 0; ii < bs; ii++) {
568: for (jj = ii; jj < bs2; jj += bs) bap[jj] = *value++;
569: }
570: } else {
571: for (ii = 0; ii < bs; ii++) {
572: for (jj = 0; jj < bs; jj++) *bap++ = *value++;
573: }
574: }
575: }
576: noinsert2:;
577: ailen[row] = nrow;
578: PetscFunctionReturn(PETSC_SUCCESS);
579: }
581: /*
582: This routine could be optimized by removing the need for the block copy below and passing stride information
583: to the above inline routines; similarly in MatSetValuesBlocked_MPIBAIJ()
584: */
585: static PetscErrorCode MatSetValuesBlocked_MPISBAIJ(Mat mat, PetscInt m, const PetscInt im[], PetscInt n, const PetscInt in[], const MatScalar v[], InsertMode addv)
586: {
587: Mat_MPISBAIJ *baij = (Mat_MPISBAIJ *)mat->data;
588: const MatScalar *value;
589: MatScalar *barray = baij->barray;
590: PetscBool roworiented = baij->roworiented, ignore_ltriangular = ((Mat_SeqSBAIJ *)baij->A->data)->ignore_ltriangular;
591: PetscInt i, j, ii, jj, row, col, rstart = baij->rstartbs;
592: PetscInt rend = baij->rendbs, cstart = baij->cstartbs, stepval;
593: PetscInt cend = baij->cendbs, bs = mat->rmap->bs, bs2 = baij->bs2;
595: PetscFunctionBegin;
596: if (!mat->structure_only && !barray) {
597: PetscCall(PetscMalloc1(bs2, &barray));
598: baij->barray = barray;
599: }
601: if (roworiented) stepval = (n - 1) * bs;
602: else stepval = (m - 1) * bs;
603: for (i = 0; i < m; i++) {
604: if (im[i] < 0) continue;
605: PetscCheck(im[i] < baij->Mbs, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Block indexed row too large %" PetscInt_FMT " max %" PetscInt_FMT, im[i], baij->Mbs - 1);
606: if (im[i] >= rstart && im[i] < rend) {
607: row = im[i] - rstart;
608: for (j = 0; j < n; j++) {
609: if (in[j] < 0) continue;
610: if (im[i] > in[j]) {
611: PetscCheck(ignore_ltriangular, PETSC_COMM_SELF, PETSC_ERR_USER, "Lower triangular value cannot be set for sbaij format. Ignoring these values, run with -mat_ignore_lower_triangular or call MatSetOption(mat,MAT_IGNORE_LOWER_TRIANGULAR,PETSC_TRUE)");
612: continue; /* ignore lower triangular blocks */
613: }
614: if (!mat->structure_only) {
615: /* If NumCol = 1 then a copy is not required */
616: if (roworiented && n == 1) {
617: barray = (MatScalar *)v + i * bs2;
618: } else if ((!roworiented) && (m == 1)) {
619: barray = (MatScalar *)v + j * bs2;
620: } else { /* Here a copy is required */
621: if (roworiented) {
622: value = v + i * (stepval + bs) * bs + j * bs;
623: } else {
624: value = v + j * (stepval + bs) * bs + i * bs;
625: }
626: for (ii = 0; ii < bs; ii++, value += stepval) {
627: for (jj = 0; jj < bs; jj++) *barray++ = *value++;
628: }
629: barray -= bs2;
630: }
631: }
633: if (in[j] >= cstart && in[j] < cend) {
634: col = in[j] - cstart;
635: PetscCall(MatSetValuesBlocked_SeqSBAIJ_Inlined(baij->A, row, col, barray, addv, im[i], in[j]));
636: } else if (in[j] < 0) {
637: continue;
638: } else {
639: PetscCheck(in[j] < baij->Nbs, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Block indexed column too large %" PetscInt_FMT " max %" PetscInt_FMT, in[j], baij->Nbs - 1);
640: if (mat->was_assembled) {
641: if (!baij->colmap) PetscCall(MatCreateColmap_MPIBAIJ_Private(mat));
643: #if PetscDefined(USE_CTABLE)
644: PetscCall(PetscHMapIGetWithDefault(baij->colmap, in[j] + 1, 0, &col));
645: col = col < 1 ? -1 : (col - 1) / bs;
646: #else
647: col = baij->colmap[in[j]] < 1 ? -1 : (baij->colmap[in[j]] - 1) / bs;
648: #endif
649: if (col < 0 && !((Mat_SeqBAIJ *)baij->A->data)->nonew) {
650: PetscCall(MatDisAssemble_MPISBAIJ(mat));
651: col = in[j];
652: }
653: } else col = in[j];
654: PetscCall(MatSetValuesBlocked_SeqBAIJ_Inlined(baij->B, row, col, barray, addv, im[i], in[j]));
655: }
656: }
657: } else {
658: PetscCheck(!mat->nooffprocentries, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Setting off process block indexed row %" PetscInt_FMT " even though MatSetOption(,MAT_NO_OFF_PROC_ENTRIES,PETSC_TRUE) was set", im[i]);
659: if (!baij->donotstash) {
660: if (roworiented) {
661: PetscCall(MatStashValuesRowBlocked_Private(&mat->bstash, im[i], n, in, v, m, n, i));
662: } else {
663: PetscCall(MatStashValuesColBlocked_Private(&mat->bstash, im[i], n, in, v, m, n, i));
664: }
665: }
666: }
667: }
668: PetscFunctionReturn(PETSC_SUCCESS);
669: }
671: static PetscErrorCode MatGetValues_MPISBAIJ(Mat mat, PetscInt m, const PetscInt idxm[], PetscInt n, const PetscInt idxn[], PetscScalar v[])
672: {
673: Mat_MPISBAIJ *baij = (Mat_MPISBAIJ *)mat->data;
674: PetscInt bs = mat->rmap->bs, i, j, bsrstart = mat->rmap->rstart, bsrend = mat->rmap->rend;
675: PetscInt bscstart = mat->cmap->rstart, bscend = mat->cmap->rend, row, col, data;
676: PetscBool roworiented = baij->roworiented;
677: PetscScalar *value;
679: PetscFunctionBegin;
680: for (i = 0; i < m; i++) {
681: if (idxm[i] < 0) continue; /* negative row */
682: PetscCheck(idxm[i] < mat->rmap->N, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Row too large: row %" PetscInt_FMT " max %" PetscInt_FMT, idxm[i], mat->rmap->N - 1);
683: PetscCheck(idxm[i] >= bsrstart && idxm[i] < bsrend, PETSC_COMM_SELF, PETSC_ERR_SUP, "Only local values currently supported");
684: row = idxm[i] - bsrstart;
685: for (j = 0; j < n; j++) {
686: if (idxn[j] < 0) continue; /* negative column */
687: PetscCheck(idxn[j] < mat->cmap->N, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Column too large: col %" PetscInt_FMT " max %" PetscInt_FMT, idxn[j], mat->cmap->N - 1);
688: value = roworiented ? &v[j + i * n] : &v[i + j * m];
689: if (idxn[j] >= bscstart && idxn[j] < bscend) {
690: col = idxn[j] - bscstart;
691: PetscCall(MatGetValues_SeqSBAIJ(baij->A, 1, &row, 1, &col, value));
692: } else {
693: if (!baij->colmap) PetscCall(MatCreateColmap_MPIBAIJ_Private(mat));
694: #if PetscDefined(USE_CTABLE)
695: PetscCall(PetscHMapIGetWithDefault(baij->colmap, idxn[j] / bs + 1, 0, &data));
696: data--;
697: #else
698: data = baij->colmap[idxn[j] / bs] - 1;
699: #endif
700: if (data < 0 || baij->garray[data / bs] != idxn[j] / bs) *value = 0.0;
701: else {
702: col = data + idxn[j] % bs;
703: PetscCall(MatGetValues_SeqBAIJ(baij->B, 1, &row, 1, &col, value));
704: }
705: }
706: }
707: }
708: PetscFunctionReturn(PETSC_SUCCESS);
709: }
711: static PetscErrorCode MatNorm_MPISBAIJ(Mat mat, NormType type, PetscReal *norm)
712: {
713: Mat_MPISBAIJ *baij = (Mat_MPISBAIJ *)mat->data;
714: PetscReal sum[2];
716: PetscFunctionBegin;
717: if (baij->size == 1) {
718: PetscCall(MatNorm(baij->A, type, norm));
719: } else {
720: if (type == NORM_FROBENIUS) {
721: PetscCall(MatNorm(baij->A, type, &sum[0]));
722: sum[0] *= sum[0];
723: PetscCall(MatNorm(baij->B, type, &sum[1]));
724: sum[1] *= sum[1];
725: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, sum, 2, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)mat)));
726: *norm = PetscSqrtReal(sum[0] + 2 * sum[1]);
727: } else if (type == NORM_INFINITY || type == NORM_1) { /* max row/column sum */
728: Mat_SeqSBAIJ *amat = (Mat_SeqSBAIJ *)baij->A->data;
729: Mat_SeqBAIJ *bmat = (Mat_SeqBAIJ *)baij->B->data;
730: PetscReal *rsum, vabs;
731: PetscInt *jj, *garray = baij->garray, rstart = baij->rstartbs, nz;
732: PetscInt brow, bcol, col, bs = baij->A->rmap->bs, row, grow, gcol, mbs = amat->mbs;
733: MatScalar *v;
735: PetscCall(PetscCalloc1(mat->cmap->N, &rsum));
736: /* Amat */
737: v = amat->a;
738: jj = amat->j;
739: for (brow = 0; brow < mbs; brow++) {
740: grow = bs * (rstart + brow);
741: nz = amat->i[brow + 1] - amat->i[brow];
742: for (bcol = 0; bcol < nz; bcol++) {
743: gcol = bs * (rstart + *jj);
744: jj++;
745: for (col = 0; col < bs; col++) {
746: for (row = 0; row < bs; row++) {
747: vabs = PetscAbsScalar(*v);
748: v++;
749: rsum[gcol + col] += vabs;
750: /* non-diagonal block */
751: if (bcol > 0 && vabs > 0.0) rsum[grow + row] += vabs;
752: }
753: }
754: }
755: PetscCall(PetscLogFlops(nz * bs * bs));
756: }
757: /* Bmat */
758: v = bmat->a;
759: jj = bmat->j;
760: for (brow = 0; brow < mbs; brow++) {
761: grow = bs * (rstart + brow);
762: nz = bmat->i[brow + 1] - bmat->i[brow];
763: for (bcol = 0; bcol < nz; bcol++) {
764: gcol = bs * garray[*jj];
765: jj++;
766: for (col = 0; col < bs; col++) {
767: for (row = 0; row < bs; row++) {
768: vabs = PetscAbsScalar(*v);
769: v++;
770: rsum[gcol + col] += vabs;
771: rsum[grow + row] += vabs;
772: }
773: }
774: }
775: PetscCall(PetscLogFlops(nz * bs * bs));
776: }
777: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, rsum, mat->cmap->N, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)mat)));
778: *norm = 0.0;
779: for (col = 0; col < mat->cmap->N; col++) {
780: if (rsum[col] > *norm) *norm = rsum[col];
781: }
782: PetscCall(PetscFree(rsum));
783: } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "No support for this norm yet");
784: }
785: PetscFunctionReturn(PETSC_SUCCESS);
786: }
788: static PetscErrorCode MatAssemblyBegin_MPISBAIJ(Mat mat, MatAssemblyType mode)
789: {
790: Mat_MPISBAIJ *baij = (Mat_MPISBAIJ *)mat->data;
791: PetscInt nstash, reallocs;
793: PetscFunctionBegin;
794: if (baij->donotstash || mat->nooffprocentries) PetscFunctionReturn(PETSC_SUCCESS);
796: PetscCall(MatStashScatterBegin_Private(mat, &mat->stash, mat->rmap->range));
797: PetscCall(MatStashScatterBegin_Private(mat, &mat->bstash, baij->rangebs));
798: PetscCall(MatStashGetInfo_Private(&mat->stash, &nstash, &reallocs));
799: PetscCall(PetscInfo(mat, "Stash has %" PetscInt_FMT " entries, uses %" PetscInt_FMT " mallocs.\n", nstash, reallocs));
800: PetscCall(MatStashGetInfo_Private(&mat->bstash, &nstash, &reallocs));
801: PetscCall(PetscInfo(mat, "Block-Stash has %" PetscInt_FMT " entries, uses %" PetscInt_FMT " mallocs.\n", nstash, reallocs));
802: PetscFunctionReturn(PETSC_SUCCESS);
803: }
805: static PetscErrorCode MatAssemblyEnd_MPISBAIJ(Mat mat, MatAssemblyType mode)
806: {
807: Mat_MPISBAIJ *baij = (Mat_MPISBAIJ *)mat->data;
808: Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ *)baij->A->data;
809: PetscInt i, j, rstart, ncols, flg, bs2 = baij->bs2;
810: PetscInt *row, *col;
811: PetscBool all_assembled;
812: PetscMPIInt n;
813: PetscBool r1, r2, r3;
814: MatScalar *val;
816: /* do not use 'b=(Mat_SeqBAIJ*)baij->B->data' as B can be reset in disassembly */
817: PetscFunctionBegin;
818: if (!baij->donotstash && !mat->nooffprocentries) {
819: while (1) {
820: PetscCall(MatStashScatterGetMesg_Private(&mat->stash, &n, &row, &col, &val, &flg));
821: if (!flg) break;
823: for (i = 0; i < n;) {
824: /* Now identify the consecutive vals belonging to the same row */
825: for (j = i, rstart = row[j]; j < n; j++) {
826: if (row[j] != rstart) break;
827: }
828: if (j < n) ncols = j - i;
829: else ncols = n - i;
830: /* Now assemble all these values with a single function call */
831: PetscCall(MatSetValues_MPISBAIJ(mat, 1, row + i, ncols, col + i, val + i, mat->insertmode));
832: i = j;
833: }
834: }
835: PetscCall(MatStashScatterEnd_Private(&mat->stash));
836: /* Now process the block-stash. Since the values are stashed column-oriented,
837: set the row-oriented flag to column-oriented, and after MatSetValues()
838: restore the original flags */
839: r1 = baij->roworiented;
840: r2 = a->roworiented;
841: r3 = ((Mat_SeqBAIJ *)baij->B->data)->roworiented;
843: baij->roworiented = PETSC_FALSE;
844: a->roworiented = PETSC_FALSE;
846: ((Mat_SeqBAIJ *)baij->B->data)->roworiented = PETSC_FALSE; /* b->roworiented */
847: while (1) {
848: PetscCall(MatStashScatterGetMesg_Private(&mat->bstash, &n, &row, &col, &val, &flg));
849: if (!flg) break;
851: for (i = 0; i < n;) {
852: /* Now identify the consecutive vals belonging to the same row */
853: for (j = i, rstart = row[j]; j < n; j++) {
854: if (row[j] != rstart) break;
855: }
856: if (j < n) ncols = j - i;
857: else ncols = n - i;
858: PetscCall(MatSetValuesBlocked_MPISBAIJ(mat, 1, row + i, ncols, col + i, val + i * bs2, mat->insertmode));
859: i = j;
860: }
861: }
862: PetscCall(MatStashScatterEnd_Private(&mat->bstash));
864: baij->roworiented = r1;
865: a->roworiented = r2;
867: ((Mat_SeqBAIJ *)baij->B->data)->roworiented = r3; /* b->roworiented */
868: }
870: PetscCall(MatAssemblyBegin(baij->A, mode));
871: PetscCall(MatAssemblyEnd(baij->A, mode));
873: /* determine if any process has disassembled, if so we must
874: also disassemble ourselves, in order that we may reassemble. */
875: /*
876: if nonzero structure of submatrix B cannot change then we know that
877: no process disassembled thus we can skip this stuff
878: */
879: if (!((Mat_SeqBAIJ *)baij->B->data)->nonew) {
880: PetscCallMPI(MPIU_Allreduce(&mat->was_assembled, &all_assembled, 1, MPI_C_BOOL, MPI_LAND, PetscObjectComm((PetscObject)mat)));
881: if (mat->was_assembled && !all_assembled) PetscCall(MatDisAssemble_MPISBAIJ(mat));
882: }
884: if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) PetscCall(MatSetUpMultiply_MPISBAIJ(mat)); /* setup Mvctx and sMvctx */
885: PetscCall(MatAssemblyBegin(baij->B, mode));
886: PetscCall(MatAssemblyEnd(baij->B, mode));
888: PetscCall(PetscFree2(baij->rowvalues, baij->rowindices));
890: /* if no new nonzero locations are allowed in matrix then only set the matrix state the first time through */
891: if ((!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) || !((Mat_SeqBAIJ *)baij->A->data)->nonew) {
892: mat->nonzerostate = baij->A->nonzerostate + baij->B->nonzerostate;
893: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &mat->nonzerostate, 1, MPIU_INT64, MPI_SUM, PetscObjectComm((PetscObject)mat)));
894: }
895: PetscFunctionReturn(PETSC_SUCCESS);
896: }
898: #include <petscdraw.h>
899: static PetscErrorCode MatView_MPISBAIJ_ASCIIorDraworSocket(Mat mat, PetscViewer viewer)
900: {
901: Mat_MPISBAIJ *baij = (Mat_MPISBAIJ *)mat->data;
902: PetscMPIInt rank = baij->rank;
903: PetscBool isascii, isdraw;
904: PetscViewer sviewer;
905: PetscViewerFormat format;
907: PetscFunctionBegin;
908: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
909: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
910: if (isascii) {
911: PetscCall(PetscViewerGetFormat(viewer, &format));
912: if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
913: MatInfo info;
914: PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)mat), &rank));
915: PetscCall(MatGetInfo(mat, MAT_LOCAL, &info));
916: PetscCall(PetscViewerASCIIPushSynchronized(viewer));
917: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Local rows %" PetscInt_FMT " nz %" PetscInt_FMT " nz alloced %" PetscInt_FMT " bs %" PetscInt_FMT " mem %g\n", rank, mat->rmap->n, (PetscInt)info.nz_used, (PetscInt)info.nz_allocated,
918: mat->rmap->bs, info.memory));
919: PetscCall(MatGetInfo(baij->A, MAT_LOCAL, &info));
920: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "[%d] on-diagonal part: nz %" PetscInt_FMT " \n", rank, (PetscInt)info.nz_used));
921: PetscCall(MatGetInfo(baij->B, MAT_LOCAL, &info));
922: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "[%d] off-diagonal part: nz %" PetscInt_FMT " \n", rank, (PetscInt)info.nz_used));
923: PetscCall(PetscViewerFlush(viewer));
924: PetscCall(PetscViewerASCIIPopSynchronized(viewer));
925: PetscCall(PetscViewerASCIIPrintf(viewer, "Information on VecScatter used in matrix-vector product: \n"));
926: PetscCall(VecScatterView(baij->Mvctx, viewer));
927: PetscFunctionReturn(PETSC_SUCCESS);
928: } else if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_FACTOR_INFO) PetscFunctionReturn(PETSC_SUCCESS);
929: }
931: if (isdraw) {
932: PetscDraw draw;
933: PetscBool isnull;
934: PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw));
935: PetscCall(PetscDrawIsNull(draw, &isnull));
936: if (isnull) PetscFunctionReturn(PETSC_SUCCESS);
937: }
939: { /* assemble the entire matrix onto first process */
940: Mat A, Av;
941: IS isrow, iscol;
943: PetscCall(ISCreateStride(PetscObjectComm((PetscObject)mat), rank == 0 ? mat->rmap->N : 0, 0, 1, &isrow));
944: PetscCall(ISCreateStride(PetscObjectComm((PetscObject)mat), rank == 0 ? mat->cmap->N : 0, 0, 1, &iscol));
945: PetscCall(MatCreateSubMatrix(mat, isrow, iscol, MAT_INITIAL_MATRIX, &A));
946: PetscCall(MatMPIBAIJGetSeqBAIJ(A, &Av, NULL, NULL));
947: PetscCall(ISDestroy(&isrow));
948: PetscCall(ISDestroy(&iscol));
949: /*
950: Everyone has to call to draw the matrix since the graphics waits are
951: synchronized across all processors that share the PetscDraw object
952: */
953: PetscCall(PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &sviewer));
954: if (rank == 0) {
955: if (((PetscObject)mat)->name) PetscCall(PetscObjectSetName((PetscObject)Av, ((PetscObject)mat)->name));
956: PetscCall(MatView_SeqSBAIJ(Av, sviewer));
957: }
958: PetscCall(PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &sviewer));
959: PetscCall(MatDestroy(&A));
960: }
961: PetscFunctionReturn(PETSC_SUCCESS);
962: }
964: /* Used for both MPIBAIJ and MPISBAIJ matrices */
965: #define MatView_MPISBAIJ_Binary MatView_MPIBAIJ_Binary
967: static PetscErrorCode MatView_MPISBAIJ(Mat mat, PetscViewer viewer)
968: {
969: PetscBool isascii, isdraw, issocket, isbinary;
971: PetscFunctionBegin;
972: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
973: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
974: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSOCKET, &issocket));
975: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
976: if (isascii || isdraw || issocket) PetscCall(MatView_MPISBAIJ_ASCIIorDraworSocket(mat, viewer));
977: else if (isbinary) PetscCall(MatView_MPISBAIJ_Binary(mat, viewer));
978: PetscFunctionReturn(PETSC_SUCCESS);
979: }
981: static PetscErrorCode MatMultAdd_MPISBAIJ_Private(Mat A, Vec xx, Vec yy, Vec zz, PetscBool hermitian)
982: {
983: Mat_MPISBAIJ *a = (Mat_MPISBAIJ *)A->data;
984: PetscScalar *from;
985: const PetscScalar *x;
987: PetscFunctionBegin;
988: /* subdiagonal part */
989: if (hermitian == PETSC_TRUE) PetscUseTypeMethod(a->B, multhermitiantranspose, xx, a->slvec0b);
990: else PetscUseTypeMethod(a->B, multtranspose, xx, a->slvec0b);
992: /* copy x into the slvec0 Vec */
993: PetscCall(VecGetArray(a->slvec0, &from));
994: PetscCall(VecGetArrayRead(xx, &x));
996: PetscCall(PetscArraycpy(from, x, A->rmap->n));
997: PetscCall(VecRestoreArray(a->slvec0, &from));
998: PetscCall(VecRestoreArrayRead(xx, &x));
1000: PetscCall(VecZeroEntries(a->slvec1));
1001: PetscCall(VecScatterBegin(a->sMvctx, a->slvec0, a->slvec1, ADD_VALUES, SCATTER_FORWARD));
1002: /* diagonal part */
1003: if (yy != NULL) PetscUseTypeMethod(a->A, multadd, xx, yy, zz);
1004: else PetscUseTypeMethod(a->A, mult, xx, zz);
1005: PetscCall(VecScatterEnd(a->sMvctx, a->slvec0, a->slvec1, ADD_VALUES, SCATTER_FORWARD));
1007: PetscCall(VecGetArray(zz, &from));
1008: PetscCall(VecGetArrayRead(a->slvec1a, &x));
1009: for (PetscInt i = 0; i < A->rmap->n; ++i) from[i] += x[i];
1010: PetscCall(VecRestoreArrayRead(a->slvec1a, &x));
1011: PetscCall(VecRestoreArray(zz, &from));
1012: PetscCall(PetscLogFlops(A->rmap->n));
1014: /* superdiagonal part */
1015: PetscUseTypeMethod(a->B, multadd, a->slvec1b, zz, zz);
1016: PetscFunctionReturn(PETSC_SUCCESS);
1017: }
1019: #if PetscDefined(USE_COMPLEX)
1020: static PetscErrorCode MatMult_MPISBAIJ_Hermitian(Mat A, Vec xx, Vec yy)
1021: {
1022: PetscFunctionBegin;
1023: PetscCall(MatMultAdd_MPISBAIJ_Private(A, xx, NULL, yy, PETSC_TRUE));
1024: PetscFunctionReturn(PETSC_SUCCESS);
1025: }
1027: static PetscErrorCode MatMultAdd_MPISBAIJ_Hermitian(Mat A, Vec xx, Vec yy, Vec zz)
1028: {
1029: PetscFunctionBegin;
1030: PetscCall(MatMultAdd_MPISBAIJ_Private(A, xx, yy, zz, PETSC_TRUE));
1031: PetscFunctionReturn(PETSC_SUCCESS);
1032: }
1033: #endif
1035: static PetscErrorCode MatMult_MPISBAIJ(Mat A, Vec xx, Vec yy)
1036: {
1037: PetscFunctionBegin;
1038: PetscCall(MatMultAdd_MPISBAIJ_Private(A, xx, NULL, yy, PETSC_FALSE));
1039: PetscFunctionReturn(PETSC_SUCCESS);
1040: }
1042: static PetscErrorCode MatMultAdd_MPISBAIJ(Mat A, Vec xx, Vec yy, Vec zz)
1043: {
1044: PetscFunctionBegin;
1045: PetscCall(MatMultAdd_MPISBAIJ_Private(A, xx, yy, zz, PETSC_FALSE));
1046: PetscFunctionReturn(PETSC_SUCCESS);
1047: }
1049: /*
1050: This only works correctly for square matrices where the subblock A->A is the
1051: diagonal block
1052: */
1053: static PetscErrorCode MatGetDiagonal_MPISBAIJ(Mat A, Vec v)
1054: {
1055: Mat_MPISBAIJ *a = (Mat_MPISBAIJ *)A->data;
1057: PetscFunctionBegin;
1058: /* PetscCheck(a->rmap->N == a->cmap->N,PETSC_COMM_SELF,PETSC_ERR_SUP,"Supports only square matrix where A->A is diag block"); */
1059: PetscCall(MatGetDiagonal(a->A, v));
1060: PetscFunctionReturn(PETSC_SUCCESS);
1061: }
1063: static PetscErrorCode MatScale_MPISBAIJ(Mat A, PetscScalar aa)
1064: {
1065: Mat_MPISBAIJ *a = (Mat_MPISBAIJ *)A->data;
1067: PetscFunctionBegin;
1068: PetscCall(MatScale(a->A, aa));
1069: PetscCall(MatScale(a->B, aa));
1070: PetscFunctionReturn(PETSC_SUCCESS);
1071: }
1073: static PetscErrorCode MatGetRow_MPISBAIJ(Mat matin, PetscInt row, PetscInt *nz, PetscInt **idx, PetscScalar **v)
1074: {
1075: Mat_MPISBAIJ *mat = (Mat_MPISBAIJ *)matin->data;
1076: PetscScalar *vworkA, *vworkB, **pvA, **pvB, *v_p;
1077: PetscInt bs = matin->rmap->bs, bs2 = mat->bs2, i, *cworkA, *cworkB, **pcA, **pcB;
1078: PetscInt nztot, nzA, nzB, lrow, brstart = matin->rmap->rstart, brend = matin->rmap->rend;
1079: PetscInt *cmap, *idx_p, cstart = mat->rstartbs;
1081: PetscFunctionBegin;
1082: PetscCheck(!mat->getrowactive, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Already active");
1083: mat->getrowactive = PETSC_TRUE;
1085: if (!mat->rowvalues && (idx || v)) {
1086: /*
1087: allocate enough space to hold information from the longest row.
1088: */
1089: Mat_SeqSBAIJ *Aa = (Mat_SeqSBAIJ *)mat->A->data;
1090: Mat_SeqBAIJ *Ba = (Mat_SeqBAIJ *)mat->B->data;
1091: PetscInt max = 1, mbs = mat->mbs, tmp;
1092: for (i = 0; i < mbs; i++) {
1093: tmp = Aa->i[i + 1] - Aa->i[i] + Ba->i[i + 1] - Ba->i[i]; /* row length */
1094: if (max < tmp) max = tmp;
1095: }
1096: PetscCall(PetscMalloc2(max * bs2, &mat->rowvalues, max * bs2, &mat->rowindices));
1097: }
1099: PetscCheck(row >= brstart && row < brend, PETSC_COMM_SELF, PETSC_ERR_SUP, "Only local rows");
1100: lrow = row - brstart; /* local row index */
1102: pvA = &vworkA;
1103: pcA = &cworkA;
1104: pvB = &vworkB;
1105: pcB = &cworkB;
1106: if (!v) {
1107: pvA = NULL;
1108: pvB = NULL;
1109: }
1110: if (!idx) {
1111: pcA = NULL;
1112: if (!v) pcB = NULL;
1113: }
1114: PetscUseTypeMethod(mat->A, getrow, lrow, &nzA, pcA, pvA);
1115: PetscUseTypeMethod(mat->B, getrow, lrow, &nzB, pcB, pvB);
1116: nztot = nzA + nzB;
1118: cmap = mat->garray;
1119: if (v || idx) {
1120: if (nztot) {
1121: /* Sort by increasing column numbers, assuming A and B already sorted */
1122: PetscInt imark = -1;
1123: if (v) {
1124: *v = v_p = mat->rowvalues;
1125: for (i = 0; i < nzB; i++) {
1126: if (cmap[cworkB[i] / bs] < cstart) v_p[i] = vworkB[i];
1127: else break;
1128: }
1129: imark = i;
1130: for (i = 0; i < nzA; i++) v_p[imark + i] = vworkA[i];
1131: for (i = imark; i < nzB; i++) v_p[nzA + i] = vworkB[i];
1132: }
1133: if (idx) {
1134: *idx = idx_p = mat->rowindices;
1135: if (imark > -1) {
1136: for (i = 0; i < imark; i++) idx_p[i] = cmap[cworkB[i] / bs] * bs + cworkB[i] % bs;
1137: } else {
1138: for (i = 0; i < nzB; i++) {
1139: if (cmap[cworkB[i] / bs] < cstart) idx_p[i] = cmap[cworkB[i] / bs] * bs + cworkB[i] % bs;
1140: else break;
1141: }
1142: imark = i;
1143: }
1144: for (i = 0; i < nzA; i++) idx_p[imark + i] = cstart * bs + cworkA[i];
1145: for (i = imark; i < nzB; i++) idx_p[nzA + i] = cmap[cworkB[i] / bs] * bs + cworkB[i] % bs;
1146: }
1147: } else {
1148: if (idx) *idx = NULL;
1149: if (v) *v = NULL;
1150: }
1151: }
1152: *nz = nztot;
1153: PetscUseTypeMethod(mat->A, restorerow, lrow, &nzA, pcA, pvA);
1154: PetscUseTypeMethod(mat->B, restorerow, lrow, &nzB, pcB, pvB);
1155: PetscFunctionReturn(PETSC_SUCCESS);
1156: }
1158: static PetscErrorCode MatRestoreRow_MPISBAIJ(Mat mat, PetscInt row, PetscInt *nz, PetscInt **idx, PetscScalar **v)
1159: {
1160: Mat_MPISBAIJ *baij = (Mat_MPISBAIJ *)mat->data;
1162: PetscFunctionBegin;
1163: PetscCheck(baij->getrowactive, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "MatGetRow() must be called first");
1164: baij->getrowactive = PETSC_FALSE;
1165: PetscFunctionReturn(PETSC_SUCCESS);
1166: }
1168: static PetscErrorCode MatGetRowUpperTriangular_MPISBAIJ(Mat A)
1169: {
1170: Mat_MPISBAIJ *a = (Mat_MPISBAIJ *)A->data;
1171: Mat_SeqSBAIJ *aA = (Mat_SeqSBAIJ *)a->A->data;
1173: PetscFunctionBegin;
1174: aA->getrow_utriangular = PETSC_TRUE;
1175: PetscFunctionReturn(PETSC_SUCCESS);
1176: }
1177: static PetscErrorCode MatRestoreRowUpperTriangular_MPISBAIJ(Mat A)
1178: {
1179: Mat_MPISBAIJ *a = (Mat_MPISBAIJ *)A->data;
1180: Mat_SeqSBAIJ *aA = (Mat_SeqSBAIJ *)a->A->data;
1182: PetscFunctionBegin;
1183: aA->getrow_utriangular = PETSC_FALSE;
1184: PetscFunctionReturn(PETSC_SUCCESS);
1185: }
1187: static PetscErrorCode MatConjugate_MPISBAIJ(Mat mat)
1188: {
1189: Mat_MPISBAIJ *a = (Mat_MPISBAIJ *)mat->data;
1191: PetscFunctionBegin;
1192: PetscCall(MatConjugate(a->A));
1193: PetscCall(MatConjugate(a->B));
1194: PetscFunctionReturn(PETSC_SUCCESS);
1195: }
1197: static PetscErrorCode MatRealPart_MPISBAIJ(Mat A)
1198: {
1199: Mat_MPISBAIJ *a = (Mat_MPISBAIJ *)A->data;
1201: PetscFunctionBegin;
1202: PetscCall(MatRealPart(a->A));
1203: PetscCall(MatRealPart(a->B));
1204: PetscFunctionReturn(PETSC_SUCCESS);
1205: }
1207: static PetscErrorCode MatImaginaryPart_MPISBAIJ(Mat A)
1208: {
1209: Mat_MPISBAIJ *a = (Mat_MPISBAIJ *)A->data;
1211: PetscFunctionBegin;
1212: PetscCall(MatImaginaryPart(a->A));
1213: PetscCall(MatImaginaryPart(a->B));
1214: PetscFunctionReturn(PETSC_SUCCESS);
1215: }
1217: /* Check if isrow is a subset of iscol_local, called by MatCreateSubMatrix_MPISBAIJ()
1218: Input: isrow - distributed(parallel),
1219: iscol_local - locally owned (seq)
1220: */
1221: static PetscErrorCode ISEqual_private(IS isrow, IS iscol_local, PetscBool *flg)
1222: {
1223: PetscInt sz1, sz2, *a1, *a2, i, j, k, nmatch;
1224: const PetscInt *ptr1, *ptr2;
1226: PetscFunctionBegin;
1227: *flg = PETSC_FALSE;
1228: PetscCall(ISGetLocalSize(isrow, &sz1));
1229: PetscCall(ISGetLocalSize(iscol_local, &sz2));
1230: if (sz1 > sz2) PetscFunctionReturn(PETSC_SUCCESS);
1232: PetscCall(ISGetIndices(isrow, &ptr1));
1233: PetscCall(ISGetIndices(iscol_local, &ptr2));
1235: PetscCall(PetscMalloc1(sz1, &a1));
1236: PetscCall(PetscMalloc1(sz2, &a2));
1237: PetscCall(PetscArraycpy(a1, ptr1, sz1));
1238: PetscCall(PetscArraycpy(a2, ptr2, sz2));
1239: PetscCall(PetscSortInt(sz1, a1));
1240: PetscCall(PetscSortInt(sz2, a2));
1242: nmatch = 0;
1243: k = 0;
1244: for (i = 0; i < sz1; i++) {
1245: for (j = k; j < sz2; j++) {
1246: if (a1[i] == a2[j]) {
1247: k = j;
1248: nmatch++;
1249: break;
1250: }
1251: }
1252: }
1253: PetscCall(ISRestoreIndices(isrow, &ptr1));
1254: PetscCall(ISRestoreIndices(iscol_local, &ptr2));
1255: PetscCall(PetscFree(a1));
1256: PetscCall(PetscFree(a2));
1257: if (nmatch < sz1) {
1258: *flg = PETSC_FALSE;
1259: } else {
1260: *flg = PETSC_TRUE;
1261: }
1262: PetscFunctionReturn(PETSC_SUCCESS);
1263: }
1265: static PetscErrorCode MatCreateSubMatrix_MPISBAIJ(Mat mat, IS isrow, IS iscol, MatReuse call, Mat *newmat)
1266: {
1267: Mat C[2];
1268: IS iscol_local, isrow_local;
1269: PetscInt csize, csize_local, rsize;
1270: PetscBool isequal, issorted, isidentity = PETSC_FALSE;
1272: PetscFunctionBegin;
1273: PetscCall(ISGetLocalSize(iscol, &csize));
1274: PetscCall(ISGetLocalSize(isrow, &rsize));
1275: if (call == MAT_REUSE_MATRIX) {
1276: PetscCall(PetscObjectQuery((PetscObject)*newmat, "ISAllGather", (PetscObject *)&iscol_local));
1277: PetscCheck(iscol_local, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Submatrix passed in was not used before, cannot reuse");
1278: } else {
1279: PetscCall(ISAllGather(iscol, &iscol_local));
1280: PetscCall(ISSorted(iscol_local, &issorted));
1281: PetscCheck(issorted, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "For symmetric format, iscol must be sorted");
1282: }
1283: PetscCall(ISEqual_private(isrow, iscol_local, &isequal));
1284: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &isequal, 1, MPI_C_BOOL, MPI_LAND, PetscObjectComm((PetscObject)mat)));
1285: if (!isequal) {
1286: PetscCall(ISGetLocalSize(iscol_local, &csize_local));
1287: isidentity = (PetscBool)(mat->cmap->N == csize_local);
1288: if (!isidentity && mat->structure_only) {
1289: Mat full;
1291: PetscCall(MatSBAIJCreateSymmetricStructure_Private(mat, MATMPIBAIJ, PETSC_TRUE, &full));
1292: PetscCall(MatCreateSubMatrix(full, isrow, iscol, call, newmat));
1293: PetscCall(MatDestroy(&full));
1294: if (call == MAT_INITIAL_MATRIX) PetscCall(ISDestroy(&iscol_local));
1295: PetscFunctionReturn(PETSC_SUCCESS);
1296: }
1297: if (!isidentity) {
1298: if (call == MAT_REUSE_MATRIX) {
1299: PetscCall(PetscObjectQuery((PetscObject)*newmat, "ISAllGather_other", (PetscObject *)&isrow_local));
1300: PetscCheck(isrow_local, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Submatrix passed in was not used before, cannot reuse");
1301: } else {
1302: PetscCall(ISAllGather(isrow, &isrow_local));
1303: PetscCall(ISSorted(isrow_local, &issorted));
1304: PetscCheck(issorted, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "For symmetric format, isrow must be sorted");
1305: }
1306: }
1307: }
1308: /* now call MatCreateSubMatrix_MPIBAIJ() */
1309: PetscCall(MatCreateSubMatrix_MPIBAIJ_Private(mat, isrow, iscol_local, csize, isequal || isidentity ? call : MAT_INITIAL_MATRIX, isequal || isidentity ? newmat : C, (PetscBool)(isequal || isidentity)));
1310: if (!isequal && !isidentity) {
1311: if (call == MAT_INITIAL_MATRIX) {
1312: IS intersect;
1313: PetscInt ni;
1315: PetscCall(ISIntersect(isrow_local, iscol_local, &intersect));
1316: PetscCall(ISGetLocalSize(intersect, &ni));
1317: PetscCall(ISDestroy(&intersect));
1318: PetscCheck(ni == 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Cannot create such a submatrix: for symmetric format, when requesting an off-diagonal submatrix, isrow and iscol should have an empty intersection (number of common indices is %" PetscInt_FMT ")", ni);
1319: }
1320: PetscCall(MatCreateSubMatrix_MPIBAIJ_Private(mat, iscol, isrow_local, rsize, MAT_INITIAL_MATRIX, C + 1, PETSC_FALSE));
1321: PetscCall(MatTranspose(C[1], MAT_INPLACE_MATRIX, C + 1));
1322: PetscCall(MatAXPY(C[0], 1.0, C[1], DIFFERENT_NONZERO_PATTERN));
1323: if (call == MAT_REUSE_MATRIX) PetscCall(MatCopy(C[0], *newmat, SAME_NONZERO_PATTERN));
1324: else if (mat->rmap->bs == 1) PetscCall(MatConvert(C[0], MATAIJ, MAT_INITIAL_MATRIX, newmat));
1325: else {
1326: *newmat = C[0];
1327: PetscCall(PetscObjectReference((PetscObject)*newmat));
1328: }
1329: PetscCall(MatDestroy(C));
1330: PetscCall(MatDestroy(C + 1));
1331: }
1332: if (call == MAT_INITIAL_MATRIX) {
1333: if (!isequal && !isidentity) {
1334: PetscCall(PetscObjectCompose((PetscObject)*newmat, "ISAllGather_other", (PetscObject)isrow_local));
1335: PetscCall(ISDestroy(&isrow_local));
1336: }
1337: PetscCall(PetscObjectCompose((PetscObject)*newmat, "ISAllGather", (PetscObject)iscol_local));
1338: PetscCall(ISDestroy(&iscol_local));
1339: }
1340: PetscFunctionReturn(PETSC_SUCCESS);
1341: }
1343: static PetscErrorCode MatZeroEntries_MPISBAIJ(Mat A)
1344: {
1345: Mat_MPISBAIJ *l = (Mat_MPISBAIJ *)A->data;
1347: PetscFunctionBegin;
1348: PetscCall(MatZeroEntries(l->A));
1349: PetscCall(MatZeroEntries(l->B));
1350: PetscFunctionReturn(PETSC_SUCCESS);
1351: }
1353: static PetscErrorCode MatGetInfo_MPISBAIJ(Mat matin, MatInfoType flag, MatInfo *info)
1354: {
1355: Mat_MPISBAIJ *a = (Mat_MPISBAIJ *)matin->data;
1356: Mat A = a->A, B = a->B;
1357: PetscLogDouble irecv[5];
1359: PetscFunctionBegin;
1360: info->block_size = (PetscReal)matin->rmap->bs;
1362: PetscCall(MatGetInfo(A, MAT_LOCAL, info));
1364: irecv[0] = info->nz_used;
1365: irecv[1] = info->nz_allocated;
1366: irecv[2] = info->nz_unneeded;
1367: irecv[3] = info->memory;
1368: irecv[4] = info->mallocs;
1370: PetscCall(MatGetInfo(B, MAT_LOCAL, info));
1372: irecv[0] += info->nz_used;
1373: irecv[1] += info->nz_allocated;
1374: irecv[2] += info->nz_unneeded;
1375: irecv[3] += info->memory;
1376: irecv[4] += info->mallocs;
1377: if (flag == MAT_LOCAL) {
1378: info->nz_used = irecv[0];
1379: info->nz_allocated = irecv[1];
1380: info->nz_unneeded = irecv[2];
1381: info->memory = irecv[3];
1382: info->mallocs = irecv[4];
1383: } else if (flag == MAT_GLOBAL_MAX) {
1384: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, irecv, 5, MPIU_PETSCLOGDOUBLE, MPI_MAX, PetscObjectComm((PetscObject)matin)));
1386: info->nz_used = irecv[0];
1387: info->nz_allocated = irecv[1];
1388: info->nz_unneeded = irecv[2];
1389: info->memory = irecv[3];
1390: info->mallocs = irecv[4];
1391: } else if (flag == MAT_GLOBAL_SUM) {
1392: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, irecv, 5, MPIU_PETSCLOGDOUBLE, MPI_SUM, PetscObjectComm((PetscObject)matin)));
1394: info->nz_used = irecv[0];
1395: info->nz_allocated = irecv[1];
1396: info->nz_unneeded = irecv[2];
1397: info->memory = irecv[3];
1398: info->mallocs = irecv[4];
1399: } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unknown MatInfoType argument %d", (int)flag);
1400: info->fill_ratio_given = 0; /* no parallel LU/ILU/Cholesky */
1401: info->fill_ratio_needed = 0;
1402: info->factor_mallocs = 0;
1403: PetscFunctionReturn(PETSC_SUCCESS);
1404: }
1406: static PetscErrorCode MatSetOption_MPISBAIJ(Mat A, MatOption op, PetscBool flg)
1407: {
1408: Mat_MPISBAIJ *a = (Mat_MPISBAIJ *)A->data;
1410: PetscFunctionBegin;
1411: switch (op) {
1412: case MAT_NEW_NONZERO_LOCATIONS:
1413: case MAT_NEW_NONZERO_ALLOCATION_ERR:
1414: case MAT_UNUSED_NONZERO_LOCATION_ERR:
1415: case MAT_KEEP_NONZERO_PATTERN:
1416: case MAT_NEW_NONZERO_LOCATION_ERR:
1417: case MAT_ROW_ORIENTED:
1418: MatCheckPreallocated(A, 1);
1419: if (op == MAT_ROW_ORIENTED) a->roworiented = flg;
1420: PetscCall(MatSetOption(a->A, op, flg));
1421: PetscCall(MatSetOption(a->B, op, flg));
1422: break;
1423: case MAT_STRUCTURE_ONLY:
1424: if (a->A) PetscCall(MatSetOption(a->A, op, flg));
1425: if (a->B) PetscCall(MatSetOption(a->B, op, flg));
1426: break;
1427: case MAT_IGNORE_OFF_PROC_ENTRIES:
1428: a->donotstash = flg;
1429: break;
1430: case MAT_USE_HASH_TABLE:
1431: a->ht_flag = flg;
1432: break;
1433: case MAT_HERMITIAN:
1434: if (a->A && A->rmap->n == A->cmap->n) PetscCall(MatSetOption(a->A, op, flg));
1435: #if PetscDefined(USE_COMPLEX)
1436: if (flg) { /* need different mat-vec ops */
1437: A->ops->mult = MatMult_MPISBAIJ_Hermitian;
1438: A->ops->multadd = MatMultAdd_MPISBAIJ_Hermitian;
1439: A->ops->multtranspose = NULL;
1440: A->ops->multtransposeadd = NULL;
1441: }
1442: #endif
1443: break;
1444: case MAT_SPD:
1445: case MAT_SYMMETRIC:
1446: if (a->A && A->rmap->n == A->cmap->n) PetscCall(MatSetOption(a->A, op, flg));
1447: if (PetscDefined(USE_COMPLEX) && flg) { /* restore to use default mat-vec ops */
1448: A->ops->mult = MatMult_MPISBAIJ;
1449: A->ops->multadd = MatMultAdd_MPISBAIJ;
1450: A->ops->multtranspose = MatMult_MPISBAIJ;
1451: A->ops->multtransposeadd = MatMultAdd_MPISBAIJ;
1452: }
1453: break;
1454: case MAT_STRUCTURALLY_SYMMETRIC:
1455: if (a->A && A->rmap->n == A->cmap->n) PetscCall(MatSetOption(a->A, op, flg));
1456: break;
1457: case MAT_IGNORE_LOWER_TRIANGULAR:
1458: case MAT_ERROR_LOWER_TRIANGULAR:
1459: case MAT_GETROW_UPPERTRIANGULAR:
1460: MatCheckPreallocated(A, 1);
1461: PetscCall(MatSetOption(a->A, op, flg));
1462: break;
1463: default:
1464: break;
1465: }
1466: PetscFunctionReturn(PETSC_SUCCESS);
1467: }
1469: static PetscErrorCode MatTranspose_MPISBAIJ(Mat A, MatReuse reuse, Mat *B)
1470: {
1471: PetscFunctionBegin;
1472: if (reuse == MAT_REUSE_MATRIX) PetscCall(MatTransposeCheckNonzeroState_Private(A, *B));
1473: if (reuse == MAT_INITIAL_MATRIX) {
1474: PetscCall(MatDuplicate(A, MAT_COPY_VALUES, B));
1475: } else if (reuse == MAT_REUSE_MATRIX) {
1476: PetscCall(MatCopy(A, *B, SAME_NONZERO_PATTERN));
1477: }
1478: PetscFunctionReturn(PETSC_SUCCESS);
1479: }
1481: static PetscErrorCode MatDiagonalScale_MPISBAIJ(Mat mat, Vec ll, Vec rr)
1482: {
1483: Mat_MPISBAIJ *baij = (Mat_MPISBAIJ *)mat->data;
1484: Mat a = baij->A, b = baij->B;
1485: PetscInt nv, m, n;
1487: PetscFunctionBegin;
1488: if (!ll) PetscFunctionReturn(PETSC_SUCCESS);
1490: PetscCall(MatGetLocalSize(mat, &m, &n));
1491: PetscCheck(m == n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "For symmetric format, local size %" PetscInt_FMT " %" PetscInt_FMT " must be same", m, n);
1493: PetscCall(VecGetLocalSize(rr, &nv));
1494: PetscCheck(nv == n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Left and right vector non-conforming local size");
1496: PetscCall(VecScatterBegin(baij->Mvctx, rr, baij->lvec, INSERT_VALUES, SCATTER_FORWARD));
1498: /* left diagonalscale the off-diagonal part */
1499: PetscUseTypeMethod(b, diagonalscale, ll, NULL);
1501: /* scale the diagonal part */
1502: PetscUseTypeMethod(a, diagonalscale, ll, rr);
1504: /* right diagonalscale the off-diagonal part */
1505: PetscCall(VecScatterEnd(baij->Mvctx, rr, baij->lvec, INSERT_VALUES, SCATTER_FORWARD));
1506: PetscUseTypeMethod(b, diagonalscale, NULL, baij->lvec);
1507: /* MatDiagonalScale() cannot be used on the blocks: they are on PETSC_COMM_SELF while ll and rr
1508: are parallel, so the interface's communicator check rejects them. Advance the block states
1509: here instead, as the interface would; MatSOR_SeqSBAIJ() caches its inverse diagonal on the
1510: diagonal block's state. */
1511: PetscCall(PetscObjectStateIncrease((PetscObject)a));
1512: PetscCall(PetscObjectStateIncrease((PetscObject)b));
1513: PetscFunctionReturn(PETSC_SUCCESS);
1514: }
1516: static PetscErrorCode MatSetUnfactored_MPISBAIJ(Mat A)
1517: {
1518: Mat_MPISBAIJ *a = (Mat_MPISBAIJ *)A->data;
1520: PetscFunctionBegin;
1521: PetscCall(MatSetUnfactored(a->A));
1522: PetscFunctionReturn(PETSC_SUCCESS);
1523: }
1525: static PetscErrorCode MatDuplicate_MPISBAIJ(Mat, MatDuplicateOption, Mat *);
1527: static PetscErrorCode MatEqual_MPISBAIJ(Mat A, Mat B, PetscBool *flag)
1528: {
1529: Mat_MPISBAIJ *matB = (Mat_MPISBAIJ *)B->data, *matA = (Mat_MPISBAIJ *)A->data;
1530: Mat a, b, c, d;
1532: PetscFunctionBegin;
1533: a = matA->A;
1534: b = matA->B;
1535: c = matB->A;
1536: d = matB->B;
1538: PetscCall(MatEqual(a, c, flag));
1539: if (*flag) PetscCall(MatEqual(b, d, flag));
1540: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, flag, 1, MPI_C_BOOL, MPI_LAND, PetscObjectComm((PetscObject)A)));
1541: PetscFunctionReturn(PETSC_SUCCESS);
1542: }
1544: static PetscErrorCode MatCopy_MPISBAIJ(Mat A, Mat B, MatStructure str)
1545: {
1546: PetscBool isbaij;
1548: PetscFunctionBegin;
1549: PetscCall(PetscObjectTypeCompareAny((PetscObject)B, &isbaij, MATSEQSBAIJ, MATMPISBAIJ, ""));
1550: PetscCheck(isbaij, PetscObjectComm((PetscObject)B), PETSC_ERR_SUP, "Not for matrix type %s", ((PetscObject)B)->type_name);
1551: /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */
1552: if (str != SAME_NONZERO_PATTERN || A->ops->copy != B->ops->copy) {
1553: PetscCall(MatGetRowUpperTriangular(A));
1554: PetscCall(MatCopy_Basic(A, B, str));
1555: PetscCall(MatRestoreRowUpperTriangular(A));
1556: } else {
1557: Mat_MPISBAIJ *a = (Mat_MPISBAIJ *)A->data;
1558: Mat_MPISBAIJ *b = (Mat_MPISBAIJ *)B->data;
1560: PetscCall(MatCopy(a->A, b->A, str));
1561: PetscCall(MatCopy(a->B, b->B, str));
1562: }
1563: PetscCall(PetscObjectStateIncrease((PetscObject)B));
1564: PetscFunctionReturn(PETSC_SUCCESS);
1565: }
1567: static PetscErrorCode MatAXPY_MPISBAIJ(Mat Y, PetscScalar a, Mat X, MatStructure str)
1568: {
1569: Mat_MPISBAIJ *xx = (Mat_MPISBAIJ *)X->data, *yy = (Mat_MPISBAIJ *)Y->data;
1570: PetscBLASInt bnz, one = 1;
1571: Mat_SeqSBAIJ *xa, *ya;
1572: Mat_SeqBAIJ *xb, *yb;
1574: PetscFunctionBegin;
1575: if (str == SAME_NONZERO_PATTERN) {
1576: PetscScalar alpha = a;
1577: xa = (Mat_SeqSBAIJ *)xx->A->data;
1578: ya = (Mat_SeqSBAIJ *)yy->A->data;
1579: PetscCall(PetscBLASIntCast(xa->nz, &bnz));
1580: PetscCallBLAS("BLASaxpy", BLASaxpy_(&bnz, &alpha, xa->a, &one, ya->a, &one));
1581: xb = (Mat_SeqBAIJ *)xx->B->data;
1582: yb = (Mat_SeqBAIJ *)yy->B->data;
1583: PetscCall(PetscBLASIntCast(xb->nz, &bnz));
1584: PetscCallBLAS("BLASaxpy", BLASaxpy_(&bnz, &alpha, xb->a, &one, yb->a, &one));
1585: /* the blocks' values were changed directly, so advance their states as MatAXPY() on each
1586: block would; MatSOR_SeqSBAIJ() caches its inverse diagonal on the diagonal block's state */
1587: PetscCall(PetscObjectStateIncrease((PetscObject)yy->A));
1588: PetscCall(PetscObjectStateIncrease((PetscObject)yy->B));
1589: PetscCall(PetscObjectStateIncrease((PetscObject)Y));
1590: } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
1591: const PetscBool getrow_utriangular = ((Mat_SeqSBAIJ *)xx->A->data)->getrow_utriangular;
1593: PetscCall(MatAXPY_Basic(Y, a, X, str));
1594: PetscCall(MatSetOption(X, MAT_GETROW_UPPERTRIANGULAR, getrow_utriangular));
1595: } else {
1596: Mat B;
1597: PetscInt *nnz_d, *nnz_o, bs = Y->rmap->bs;
1598: PetscCheck(bs == X->rmap->bs, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Matrices must have same block size");
1599: PetscCall(MatGetRowUpperTriangular(X));
1600: PetscCall(MatGetRowUpperTriangular(Y));
1601: PetscCall(PetscMalloc1(yy->A->rmap->N, &nnz_d));
1602: PetscCall(PetscMalloc1(yy->B->rmap->N, &nnz_o));
1603: PetscCall(MatCreate(PetscObjectComm((PetscObject)Y), &B));
1604: PetscCall(PetscObjectSetName((PetscObject)B, ((PetscObject)Y)->name));
1605: PetscCall(MatSetSizes(B, Y->rmap->n, Y->cmap->n, Y->rmap->N, Y->cmap->N));
1606: PetscCall(MatSetBlockSizesFromMats(B, Y, Y));
1607: PetscCall(MatSetType(B, MATMPISBAIJ));
1608: PetscCall(MatAXPYGetPreallocation_SeqSBAIJ(yy->A, xx->A, nnz_d));
1609: PetscCall(MatAXPYGetPreallocation_MPIBAIJ(yy->B, yy->garray, xx->B, xx->garray, nnz_o));
1610: PetscCall(MatMPISBAIJSetPreallocation(B, bs, 0, nnz_d, 0, nnz_o));
1611: PetscCall(MatAXPY_BasicWithPreallocation(B, Y, a, X, str));
1612: PetscCall(MatHeaderMerge(Y, &B));
1613: PetscCall(PetscFree(nnz_d));
1614: PetscCall(PetscFree(nnz_o));
1615: PetscCall(MatRestoreRowUpperTriangular(X));
1616: PetscCall(MatRestoreRowUpperTriangular(Y));
1617: }
1618: PetscFunctionReturn(PETSC_SUCCESS);
1619: }
1621: static PetscErrorCode MatCreateSubMatrices_MPISBAIJ(Mat A, PetscInt n, const IS irow[], const IS icol[], MatReuse scall, Mat *B[])
1622: {
1623: PetscBool action[3] = {PETSC_FALSE, PETSC_FALSE, PETSC_FALSE}; /* {convert to MATBAIJ, sort and permute with MPISBAIJ, all columns request} */
1625: PetscFunctionBegin;
1626: for (PetscInt i = 0; i < n; i++) {
1627: if (action[0] == PETSC_FALSE && irow[i] != icol[i]) {
1628: PetscInt ncol;
1630: /* MatCreateSubMatrices_MPIBAIJ() preserves the MATSBAIJ format for sorted row IS with all columns */
1631: PetscCall(ISGetLocalSize(icol[i], &ncol));
1632: if (ncol == A->cmap->N) PetscCall(ISIdentity(icol[i], action));
1633: if (action[0]) {
1634: action[2] = PETSC_TRUE;
1635: if (action[1] == PETSC_FALSE) {
1636: PetscCall(ISSorted(irow[i], action + 1));
1637: action[0] = (PetscBool)!action[1];
1638: action[1] = PETSC_FALSE;
1639: }
1640: } else {
1641: PetscCall(ISEqual(irow[i], icol[i], action));
1642: action[0] = (PetscBool)!action[0];
1643: if (action[0] == PETSC_FALSE) action[1] = PETSC_TRUE;
1644: }
1645: }
1646: if (action[0] == PETSC_FALSE && action[1] == PETSC_FALSE && irow[i] == icol[i]) {
1647: PetscCall(ISSorted(irow[i], action + 1));
1648: action[1] = (PetscBool)!action[1];
1649: }
1650: }
1651: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, action, 3, MPI_C_BOOL, MPI_LOR, PetscObjectComm((PetscObject)A)));
1652: /* sorting cannot be mixed with the all-columns MATSBAIJ path */
1653: if (action[0] == PETSC_FALSE && action[1] == PETSC_TRUE && action[2] == PETSC_TRUE) action[0] = PETSC_TRUE;
1654: if (action[0] == PETSC_TRUE) {
1655: Mat Ageneral;
1657: /* different row and column sets need entries from both triangular parts of A */
1658: PetscCall(MatConvert(A, MATMPIBAIJ, MAT_INITIAL_MATRIX, &Ageneral));
1659: PetscCall(MatCreateSubMatrices_MPIBAIJ(Ageneral, n, irow, icol, scall, B));
1660: PetscCall(MatDestroy(&Ageneral));
1661: } else if (action[1] == PETSC_FALSE) PetscCall(MatCreateSubMatrices_MPIBAIJ(A, n, irow, icol, scall, B)); /* B[] are MATSBAIJ matrices */
1662: else {
1663: Mat *Bsorted;
1664: IS *isrow_sorted, *iscol_sorted, *isrow_iperm, *iscol_iperm;
1665: IS perm;
1667: PetscCall(PetscMalloc4(n, &isrow_sorted, n, &iscol_sorted, n, &isrow_iperm, n, &iscol_iperm));
1668: for (PetscInt i = 0; i < n; i++) {
1669: PetscCall(ISDuplicate(irow[i], isrow_sorted + i));
1670: PetscCall(ISSort(isrow_sorted[i]));
1671: PetscCall(ISSortPermutation(irow[i], PETSC_TRUE, &perm));
1672: PetscCall(ISInvertPermutation(perm, PETSC_DECIDE, isrow_iperm + i));
1673: PetscCall(ISDestroy(&perm));
1674: if (irow[i] == icol[i]) {
1675: iscol_sorted[i] = isrow_sorted[i];
1676: PetscCall(PetscObjectReference((PetscObject)iscol_sorted[i]));
1677: iscol_iperm[i] = isrow_iperm[i];
1678: PetscCall(PetscObjectReference((PetscObject)iscol_iperm[i]));
1679: } else {
1680: iscol_sorted[i] = isrow_sorted[i];
1681: PetscCall(PetscObjectReference((PetscObject)iscol_sorted[i]));
1682: PetscCall(ISSortPermutation(icol[i], PETSC_TRUE, &perm));
1683: PetscCall(ISInvertPermutation(perm, PETSC_DECIDE, iscol_iperm + i));
1684: PetscCall(ISDestroy(&perm));
1685: }
1686: }
1687: PetscCall(MatCreateSubMatrices_MPIBAIJ(A, n, isrow_sorted, iscol_sorted, MAT_INITIAL_MATRIX, &Bsorted)); /* Bsorted[] are MATSBAIJ matrices */
1688: for (PetscInt i = 0; i < n; i++) {
1689: Mat Bpermuted;
1690: PetscBool sameorder;
1692: PetscCall(ISEqualUnsorted(isrow_iperm[i], iscol_iperm[i], &sameorder));
1693: if (sameorder) PetscCall(MatPermute(Bsorted[i], isrow_iperm[i], iscol_iperm[i], &Bpermuted));
1694: else {
1695: Mat Bgeneral;
1697: PetscCall(MatConvert(Bsorted[i], MATSEQBAIJ, MAT_INITIAL_MATRIX, &Bgeneral));
1698: PetscCall(MatPermute(Bgeneral, isrow_iperm[i], iscol_iperm[i], &Bpermuted));
1699: PetscCall(MatDestroy(&Bgeneral));
1700: }
1701: PetscCall(MatDestroy(Bsorted + i));
1702: Bsorted[i] = Bpermuted;
1703: }
1704: if (scall == MAT_REUSE_MATRIX) {
1705: for (PetscInt i = 0; i < n; i++) PetscCall(MatCopy(Bsorted[i], (*B)[i], DIFFERENT_NONZERO_PATTERN));
1706: PetscCall(MatDestroySubMatrices(n, &Bsorted));
1707: } else *B = Bsorted;
1708: for (PetscInt i = 0; i < n; i++) {
1709: PetscCall(ISDestroy(isrow_sorted + i));
1710: PetscCall(ISDestroy(iscol_sorted + i));
1711: PetscCall(ISDestroy(isrow_iperm + i));
1712: PetscCall(ISDestroy(iscol_iperm + i));
1713: }
1714: PetscCall(PetscFree4(isrow_sorted, iscol_sorted, isrow_iperm, iscol_iperm));
1715: }
1716: PetscFunctionReturn(PETSC_SUCCESS);
1717: }
1719: static PetscErrorCode MatShift_MPISBAIJ(Mat Y, PetscScalar a)
1720: {
1721: Mat_MPISBAIJ *maij = (Mat_MPISBAIJ *)Y->data;
1722: Mat_SeqSBAIJ *aij = (Mat_SeqSBAIJ *)maij->A->data;
1724: PetscFunctionBegin;
1725: if (!Y->preallocated) PetscCall(MatMPISBAIJSetPreallocation(Y, Y->rmap->bs, 1, NULL, 0, NULL));
1726: else if (!aij->nz) {
1727: const PetscInt nonew = aij->nonew;
1729: PetscCall(MatSeqSBAIJSetPreallocation(maij->A, Y->rmap->bs, 1, NULL));
1730: aij->nonew = nonew;
1731: }
1732: PetscCall(MatShift_Basic(Y, a));
1733: PetscFunctionReturn(PETSC_SUCCESS);
1734: }
1736: static PetscErrorCode MatZeroRowsColumns_MPISBAIJ(Mat A, PetscInt N, const PetscInt rows[], PetscScalar diag, Vec x, Vec b)
1737: {
1738: Mat_MPISBAIJ *l = (Mat_MPISBAIJ *)A->data;
1739: PetscMPIInt n, p = 0;
1740: PetscInt i, j, k, r, len = 0, row, col, count;
1741: PetscInt *lrows, *owners = A->rmap->range;
1742: PetscSFNode *rrows;
1743: PetscSF sf;
1744: const PetscScalar *xx;
1745: PetscScalar *bb, *mask;
1746: Vec xmask, lmask, lvec_contrib = NULL;
1747: Mat_SeqBAIJ *baij = (Mat_SeqBAIJ *)l->B->data;
1748: PetscInt bs = A->rmap->bs, bs2 = baij->bs2;
1749: PetscScalar *aa;
1751: PetscFunctionBegin;
1752: PetscCall(PetscMPIIntCast(A->rmap->n, &n));
1753: /* create PetscSF where leaves are input rows and roots are owned rows */
1754: PetscCall(PetscMalloc1(n, &lrows));
1755: for (r = 0; r < n; ++r) lrows[r] = -1;
1756: PetscCall(PetscMalloc1(N, &rrows));
1757: for (r = 0; r < N; ++r) {
1758: const PetscInt idx = rows[r];
1759: PetscCheck(idx >= 0 && A->rmap->N > idx, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Row %" PetscInt_FMT " out of range [0,%" PetscInt_FMT ")", idx, A->rmap->N);
1760: if (idx < owners[p] || owners[p + 1] <= idx) { /* short-circuit the search if the last p owns this row too */
1761: PetscCall(PetscLayoutFindOwner(A->rmap, idx, &p));
1762: }
1763: rrows[r].rank = p;
1764: rrows[r].index = rows[r] - owners[p];
1765: }
1766: PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)A), &sf));
1767: PetscCall(PetscSFSetGraph(sf, n, N, NULL, PETSC_OWN_POINTER, rrows, PETSC_OWN_POINTER));
1768: /* collect flags for rows to be zeroed */
1769: PetscCall(PetscSFReduceBegin(sf, MPIU_INT, (PetscInt *)rows, lrows, MPI_LOR));
1770: PetscCall(PetscSFReduceEnd(sf, MPIU_INT, (PetscInt *)rows, lrows, MPI_LOR));
1771: PetscCall(PetscSFDestroy(&sf));
1772: /* compress and put in row numbers */
1773: for (r = 0; r < n; ++r) {
1774: if (lrows[r] >= 0) lrows[len++] = r;
1775: }
1776: /* zero diagonal part of matrix */
1777: PetscCall(MatZeroRowsColumns(l->A, len, lrows, diag, x, b));
1778: /* handle off-diagonal part of matrix */
1779: PetscCall(MatCreateVecs(A, &xmask, NULL));
1780: PetscCall(VecDuplicate(l->lvec, &lmask));
1781: PetscCall(VecGetArray(xmask, &bb));
1782: for (i = 0; i < len; i++) bb[lrows[i]] = 1;
1783: PetscCall(VecRestoreArray(xmask, &bb));
1784: PetscCall(VecScatterBegin(l->Mvctx, xmask, lmask, ADD_VALUES, SCATTER_FORWARD));
1785: PetscCall(VecScatterEnd(l->Mvctx, xmask, lmask, ADD_VALUES, SCATTER_FORWARD));
1786: PetscCall(VecDestroy(&xmask));
1787: if (x) {
1788: PetscCall(VecScatterBegin(l->Mvctx, x, l->lvec, INSERT_VALUES, SCATTER_FORWARD));
1789: PetscCall(VecScatterEnd(l->Mvctx, x, l->lvec, INSERT_VALUES, SCATTER_FORWARD));
1790: PetscCall(VecGetArrayRead(l->lvec, &xx));
1791: PetscCall(VecGetArray(b, &bb));
1792: }
1793: PetscCall(VecGetArray(lmask, &mask));
1794: /* MPISBAIJ stores only the upper off-diagonal in l->B; for each zeroed local row r and
1795: non-zeroed off-process column c in that row, accumulate -A[r,c] * x[r] into lvec_contrib.
1796: A SCATTER_REVERSE below sends these contributions to b[c] on the owning (higher-rank)
1797: process, the missing symmetric lower-triangular update. We skip entries where c is
1798: also a zeroed row (mask[col] != 0) since b[c] = diag * x[c] is handled separately. */
1799: if (x) {
1800: const PetscScalar *x_vals;
1801: PetscScalar *c_vals;
1803: PetscCall(VecDuplicate(l->lvec, &lvec_contrib));
1804: PetscCall(VecGetArray(lvec_contrib, &c_vals));
1805: PetscCall(VecGetArrayRead(x, &x_vals));
1806: /* Only accumulate b[c] -= A[r,c] * x[r] when off-process col c is not also a zeroed row
1807: (mask[c] non-zero means col c is zeroed, so b[c] = diag * x[c] is already set).
1808: This mirrors the MatSeqSBAIJ pattern: if (zeroed[r] && !zeroed[c]) bb[c] -= A[r,c] * x[r].
1809: c_vals is indexed by the local B column index. */
1810: for (i = 0; i < len; ++i) {
1811: row = lrows[i];
1812: for (j = baij->i[row / bs]; j < baij->i[row / bs + 1]; ++j) {
1813: for (k = 0; k < bs; ++k) {
1814: col = baij->j[j] * bs + k;
1815: if (!PetscAbsScalar(mask[col])) {
1816: aa = baij->a + j * bs2 + (row % bs) + bs * k;
1817: c_vals[col] -= aa[0] * x_vals[row];
1818: }
1819: }
1820: }
1821: }
1822: PetscCall(VecRestoreArrayRead(x, &x_vals));
1823: PetscCall(VecRestoreArray(lvec_contrib, &c_vals));
1824: }
1825: /* remove zeroed rows of off-diagonal matrix */
1826: for (i = 0; i < len; ++i) {
1827: row = lrows[i];
1828: count = (baij->i[row / bs + 1] - baij->i[row / bs]) * bs;
1829: aa = PetscSafePointerPlusOffset(baij->a, baij->i[row / bs] * bs2 + (row % bs));
1830: for (k = 0; k < count; ++k) {
1831: aa[0] = 0.0;
1832: aa += bs;
1833: }
1834: }
1835: /* loop over all elements of off process part of matrix zeroing removed columns */
1836: for (i = 0; i < l->B->rmap->N; ++i) {
1837: row = i / bs;
1838: for (j = baij->i[row]; j < baij->i[row + 1]; ++j) {
1839: for (k = 0; k < bs; ++k) {
1840: col = bs * baij->j[j] + k;
1841: if (PetscAbsScalar(mask[col])) {
1842: aa = baij->a + j * bs2 + (i % bs) + bs * k;
1843: if (x) bb[i] -= aa[0] * xx[col];
1844: aa[0] = 0.0;
1845: }
1846: }
1847: }
1848: }
1849: if (x) {
1850: PetscCall(VecRestoreArray(b, &bb));
1851: PetscCall(VecRestoreArrayRead(l->lvec, &xx));
1852: /* scatter the accumulated contributions to b[c] on higher-rank processes owning column c */
1853: PetscCall(VecScatterBegin(l->Mvctx, lvec_contrib, b, ADD_VALUES, SCATTER_REVERSE));
1854: PetscCall(VecScatterEnd(l->Mvctx, lvec_contrib, b, ADD_VALUES, SCATTER_REVERSE));
1855: PetscCall(VecDestroy(&lvec_contrib));
1856: }
1857: PetscCall(PetscObjectStateIncrease((PetscObject)l->B));
1858: PetscCall(VecRestoreArray(lmask, &mask));
1859: PetscCall(VecDestroy(&lmask));
1860: PetscCall(PetscFree(lrows));
1862: /* only change matrix nonzero state if pattern was allowed to be changed */
1863: if (!((Mat_SeqSBAIJ *)l->A->data)->nonew) {
1864: A->nonzerostate = l->A->nonzerostate + l->B->nonzerostate;
1865: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &A->nonzerostate, 1, MPIU_INT64, MPI_SUM, PetscObjectComm((PetscObject)A)));
1866: }
1867: PetscFunctionReturn(PETSC_SUCCESS);
1868: }
1870: static PetscErrorCode MatGetDiagonalBlock_MPISBAIJ(Mat A, Mat *a)
1871: {
1872: PetscFunctionBegin;
1873: *a = ((Mat_MPISBAIJ *)A->data)->A;
1874: PetscFunctionReturn(PETSC_SUCCESS);
1875: }
1877: static PetscErrorCode MatEliminateZeros_MPISBAIJ(Mat A, PetscBool keep)
1878: {
1879: Mat_MPISBAIJ *a = (Mat_MPISBAIJ *)A->data;
1881: PetscFunctionBegin;
1882: PetscCall(MatEliminateZeros_SeqSBAIJ(a->A, keep)); // possibly keep zero diagonal coefficients
1883: PetscCall(MatEliminateZeros_SeqBAIJ(a->B, PETSC_FALSE)); // never keep zero diagonal coefficients
1884: PetscFunctionReturn(PETSC_SUCCESS);
1885: }
1887: static PetscErrorCode MatLoad_MPISBAIJ(Mat, PetscViewer);
1888: static PetscErrorCode MatGetRowMaxAbs_MPISBAIJ(Mat, Vec, PetscInt[]);
1889: static PetscErrorCode MatSOR_MPISBAIJ(Mat, Vec, PetscReal, MatSORType, PetscReal, PetscInt, PetscInt, Vec);
1891: static struct _MatOps MatOps_Values = {MatSetValues_MPISBAIJ,
1892: MatGetRow_MPISBAIJ,
1893: MatRestoreRow_MPISBAIJ,
1894: MatMult_MPISBAIJ,
1895: /* 4*/ MatMultAdd_MPISBAIJ,
1896: MatMult_MPISBAIJ, /* transpose versions are same as non-transpose */
1897: MatMultAdd_MPISBAIJ,
1898: NULL,
1899: NULL,
1900: NULL,
1901: /* 10*/ NULL,
1902: NULL,
1903: NULL,
1904: MatSOR_MPISBAIJ,
1905: MatTranspose_MPISBAIJ,
1906: /* 15*/ MatGetInfo_MPISBAIJ,
1907: MatEqual_MPISBAIJ,
1908: MatGetDiagonal_MPISBAIJ,
1909: MatDiagonalScale_MPISBAIJ,
1910: MatNorm_MPISBAIJ,
1911: /* 20*/ MatAssemblyBegin_MPISBAIJ,
1912: MatAssemblyEnd_MPISBAIJ,
1913: MatSetOption_MPISBAIJ,
1914: MatZeroEntries_MPISBAIJ,
1915: /* 24*/ NULL,
1916: NULL,
1917: NULL,
1918: NULL,
1919: NULL,
1920: /* 29*/ MatSetUp_MPI_Hash,
1921: NULL,
1922: NULL,
1923: MatGetDiagonalBlock_MPISBAIJ,
1924: NULL,
1925: /* 34*/ MatDuplicate_MPISBAIJ,
1926: NULL,
1927: NULL,
1928: NULL,
1929: NULL,
1930: /* 39*/ MatAXPY_MPISBAIJ,
1931: MatCreateSubMatrices_MPISBAIJ,
1932: MatIncreaseOverlap_MPISBAIJ,
1933: MatGetValues_MPISBAIJ,
1934: MatCopy_MPISBAIJ,
1935: /* 44*/ NULL,
1936: MatScale_MPISBAIJ,
1937: MatShift_MPISBAIJ,
1938: NULL,
1939: MatZeroRowsColumns_MPISBAIJ,
1940: /* 49*/ NULL,
1941: NULL,
1942: NULL,
1943: NULL,
1944: NULL,
1945: /* 54*/ NULL,
1946: NULL,
1947: MatSetUnfactored_MPISBAIJ,
1948: NULL,
1949: MatSetValuesBlocked_MPISBAIJ,
1950: /* 59*/ MatCreateSubMatrix_MPISBAIJ,
1951: NULL,
1952: NULL,
1953: NULL,
1954: NULL,
1955: /* 64*/ NULL,
1956: NULL,
1957: NULL,
1958: NULL,
1959: MatGetRowMaxAbs_MPISBAIJ,
1960: /* 69*/ NULL,
1961: MatConvert_MPISBAIJ_Basic,
1962: NULL,
1963: NULL,
1964: NULL,
1965: NULL,
1966: NULL,
1967: NULL,
1968: NULL,
1969: MatLoad_MPISBAIJ,
1970: /* 79*/ NULL,
1971: NULL,
1972: NULL,
1973: NULL,
1974: NULL,
1975: /* 84*/ NULL,
1976: NULL,
1977: NULL,
1978: NULL,
1979: NULL,
1980: /* 89*/ NULL,
1981: NULL,
1982: NULL,
1983: NULL,
1984: MatConjugate_MPISBAIJ,
1985: /* 94*/ NULL,
1986: NULL,
1987: MatRealPart_MPISBAIJ,
1988: MatImaginaryPart_MPISBAIJ,
1989: MatGetRowUpperTriangular_MPISBAIJ,
1990: /* 99*/ MatRestoreRowUpperTriangular_MPISBAIJ,
1991: NULL,
1992: NULL,
1993: NULL,
1994: NULL,
1995: /*104*/ NULL,
1996: NULL,
1997: NULL,
1998: NULL,
1999: NULL,
2000: /*109*/ NULL,
2001: NULL,
2002: NULL,
2003: NULL,
2004: NULL,
2005: /*114*/ NULL,
2006: NULL,
2007: NULL,
2008: NULL,
2009: NULL,
2010: /*119*/ NULL,
2011: NULL,
2012: NULL,
2013: NULL,
2014: NULL,
2015: /*124*/ NULL,
2016: MatSetBlockSizes_Default,
2017: NULL,
2018: NULL,
2019: NULL,
2020: /*129*/ MatCreateMPIMatConcatenateSeqMat_MPISBAIJ,
2021: NULL,
2022: NULL,
2023: NULL,
2024: NULL,
2025: /*134*/ NULL,
2026: MatEliminateZeros_MPISBAIJ,
2027: NULL,
2028: NULL,
2029: NULL,
2030: /*139*/ NULL,
2031: MatCopyHashToXAIJ_MPI_Hash,
2032: NULL,
2033: NULL,
2034: NULL,
2035: /*144*/ NULL,
2036: NULL,
2037: NULL,
2038: NULL};
2040: static PetscErrorCode MatMPISBAIJSetPreallocation_MPISBAIJ(Mat B, PetscInt bs, PetscInt d_nz, const PetscInt *d_nnz, PetscInt o_nz, const PetscInt *o_nnz)
2041: {
2042: Mat_MPISBAIJ *b = (Mat_MPISBAIJ *)B->data;
2043: PetscInt i, mbs, Mbs;
2044: PetscMPIInt size;
2046: PetscFunctionBegin;
2047: if (B->hash_active) {
2048: B->ops[0] = b->cops;
2049: B->hash_active = PETSC_FALSE;
2050: }
2051: if (!B->preallocated) PetscCall(MatStashCreate_Private(PetscObjectComm((PetscObject)B), bs, &B->bstash));
2052: PetscCall(MatSetBlockSize(B, bs));
2053: PetscCall(PetscLayoutSetUp(B->rmap));
2054: PetscCall(PetscLayoutSetUp(B->cmap));
2055: PetscCall(PetscLayoutGetBlockSize(B->rmap, &bs));
2056: PetscCheck(B->rmap->N <= B->cmap->N, PetscObjectComm((PetscObject)B), PETSC_ERR_SUP, "MPISBAIJ matrix cannot have more rows %" PetscInt_FMT " than columns %" PetscInt_FMT, B->rmap->N, B->cmap->N);
2057: PetscCheck(B->rmap->n <= B->cmap->n, PETSC_COMM_SELF, PETSC_ERR_SUP, "MPISBAIJ matrix cannot have more local rows %" PetscInt_FMT " than columns %" PetscInt_FMT, B->rmap->n, B->cmap->n);
2059: mbs = B->rmap->n / bs;
2060: Mbs = B->rmap->N / bs;
2061: PetscCheck(mbs * bs == B->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "No of local rows %" PetscInt_FMT " must be divisible by blocksize %" PetscInt_FMT, B->rmap->N, bs);
2063: B->rmap->bs = bs;
2064: b->bs2 = bs * bs;
2065: b->mbs = mbs;
2066: b->Mbs = Mbs;
2067: b->nbs = B->cmap->n / bs;
2068: b->Nbs = B->cmap->N / bs;
2070: for (i = 0; i <= b->size; i++) b->rangebs[i] = B->rmap->range[i] / bs;
2071: b->rstartbs = B->rmap->rstart / bs;
2072: b->rendbs = B->rmap->rend / bs;
2074: b->cstartbs = B->cmap->rstart / bs;
2075: b->cendbs = B->cmap->rend / bs;
2077: #if PetscDefined(USE_CTABLE)
2078: PetscCall(PetscHMapIDestroy(&b->colmap));
2079: #else
2080: PetscCall(PetscFree(b->colmap));
2081: #endif
2082: PetscCall(PetscFree(b->garray));
2083: PetscCall(VecDestroy(&b->lvec));
2084: PetscCall(VecScatterDestroy(&b->Mvctx));
2085: PetscCall(VecDestroy(&b->slvec0));
2086: PetscCall(VecDestroy(&b->slvec0b));
2087: PetscCall(VecDestroy(&b->slvec1));
2088: PetscCall(VecDestroy(&b->slvec1a));
2089: PetscCall(VecDestroy(&b->slvec1b));
2090: PetscCall(VecScatterDestroy(&b->sMvctx));
2092: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)B), &size));
2094: MatSeqXAIJGetOptions_Private(b->B);
2095: PetscCall(MatDestroy(&b->B));
2096: PetscCall(MatCreate(PETSC_COMM_SELF, &b->B));
2097: PetscCall(MatSetSizes(b->B, B->rmap->n, size > 1 ? B->cmap->N : 0, B->rmap->n, size > 1 ? B->cmap->N : 0));
2098: PetscCall(MatSetType(b->B, MATSEQBAIJ));
2099: MatSeqXAIJRestoreOptions_Private(b->B);
2100: PetscCall(MatSetOption(b->B, MAT_STRUCTURE_ONLY, B->structure_only));
2102: MatSeqSBAIJGetOptions_Private(b->A);
2103: PetscCall(MatDestroy(&b->A));
2104: PetscCall(MatCreate(PETSC_COMM_SELF, &b->A));
2105: PetscCall(MatSetSizes(b->A, B->rmap->n, B->cmap->n, B->rmap->n, B->cmap->n));
2106: PetscCall(MatSetType(b->A, MATSEQSBAIJ));
2107: MatSeqSBAIJRestoreOptions_Private(b->A);
2108: PetscCall(MatSetOption(b->A, MAT_STRUCTURE_ONLY, B->structure_only));
2110: PetscCall(MatSeqSBAIJSetPreallocation(b->A, bs, d_nz, d_nnz));
2111: PetscCall(MatSeqBAIJSetPreallocation(b->B, bs, o_nz, o_nnz));
2112: B->preallocated = PETSC_TRUE;
2113: B->was_assembled = PETSC_FALSE;
2114: B->assembled = PETSC_FALSE;
2115: PetscFunctionReturn(PETSC_SUCCESS);
2116: }
2118: static PetscErrorCode MatMPISBAIJSetPreallocationCSR_MPISBAIJ(Mat B, PetscInt bs, const PetscInt ii[], const PetscInt jj[], const PetscScalar V[])
2119: {
2120: PetscInt m, rstart, cend;
2121: PetscInt i, j, d, nz, bd, nz_max = 0, *d_nnz = NULL, *o_nnz = NULL;
2122: const PetscInt *JJ = NULL;
2123: PetscScalar *values = NULL;
2124: PetscBool roworiented = ((Mat_MPISBAIJ *)B->data)->roworiented, ignore_ltriangular;
2125: const PetscBool nooffprocentries = B->nooffprocentries;
2127: PetscFunctionBegin;
2128: PetscCheck(bs >= 1, PetscObjectComm((PetscObject)B), PETSC_ERR_ARG_OUTOFRANGE, "Invalid block size specified, must be positive but it is %" PetscInt_FMT, bs);
2129: PetscCall(PetscLayoutSetBlockSize(B->rmap, bs));
2130: PetscCall(PetscLayoutSetBlockSize(B->cmap, bs));
2131: PetscCall(PetscLayoutSetUp(B->rmap));
2132: PetscCall(PetscLayoutSetUp(B->cmap));
2133: PetscCall(PetscLayoutGetBlockSize(B->rmap, &bs));
2134: m = B->rmap->n / bs;
2135: rstart = B->rmap->rstart / bs;
2136: cend = B->cmap->rend / bs;
2138: PetscCheck(!ii[0], PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "ii[0] must be 0 but it is %" PetscInt_FMT, ii[0]);
2139: PetscCall(PetscMalloc2(m, &d_nnz, m, &o_nnz));
2140: for (i = 0; i < m; i++) {
2141: nz = ii[i + 1] - ii[i];
2142: PetscCheck(nz >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Local row %" PetscInt_FMT " has a negative number of columns %" PetscInt_FMT, i, nz);
2143: /* count the ones on the diagonal and above, split into diagonal and off-diagonal portions. */
2144: JJ = jj + ii[i];
2145: bd = 0;
2146: for (j = 0; j < nz; j++) {
2147: if (*JJ >= i + rstart) break;
2148: JJ++;
2149: bd++;
2150: }
2151: d = 0;
2152: for (; j < nz; j++) {
2153: if (*JJ++ >= cend) break;
2154: d++;
2155: }
2156: d_nnz[i] = d;
2157: o_nnz[i] = nz - d - bd;
2158: nz = nz - bd;
2159: nz_max = PetscMax(nz_max, nz);
2160: }
2161: PetscCall(MatMPISBAIJSetPreallocation(B, bs, 0, d_nnz, 0, o_nnz));
2162: ignore_ltriangular = ((Mat_SeqSBAIJ *)((Mat_MPISBAIJ *)B->data)->A->data)->ignore_ltriangular;
2163: PetscCall(MatSetOption(B, MAT_IGNORE_LOWER_TRIANGULAR, PETSC_TRUE));
2164: PetscCall(PetscFree2(d_nnz, o_nnz));
2166: values = (PetscScalar *)V;
2167: if (!values) PetscCall(PetscCalloc1(bs * bs * nz_max, &values));
2168: for (i = 0; i < m; i++) {
2169: PetscInt row = i + rstart;
2170: PetscInt ncols = ii[i + 1] - ii[i];
2171: const PetscInt *icols = jj + ii[i];
2172: if (bs == 1 || !roworiented) { /* block ordering matches the non-nested layout of MatSetValues so we can insert entire rows */
2173: const PetscScalar *svals = values + (V ? (bs * bs * ii[i]) : 0);
2174: PetscCall(MatSetValuesBlocked_MPISBAIJ(B, 1, &row, ncols, icols, svals, INSERT_VALUES));
2175: } else { /* block ordering does not match so we can only insert one block at a time. */
2176: for (PetscInt j = 0; j < ncols; j++) {
2177: const PetscScalar *svals = values + (V ? (bs * bs * (ii[i] + j)) : 0);
2178: PetscCall(MatSetValuesBlocked_MPISBAIJ(B, 1, &row, 1, &icols[j], svals, INSERT_VALUES));
2179: }
2180: }
2181: }
2183: PetscCall(MatSetOption(B, MAT_IGNORE_LOWER_TRIANGULAR, ignore_ltriangular));
2184: if (!V) PetscCall(PetscFree(values));
2185: B->nooffprocentries = PETSC_TRUE;
2186: PetscCall(MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY));
2187: PetscCall(MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY));
2188: B->nooffprocentries = nooffprocentries;
2190: PetscCall(MatSetOption(B, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
2191: PetscFunctionReturn(PETSC_SUCCESS);
2192: }
2194: /*MC
2195: MATMPISBAIJ - MATMPISBAIJ = "mpisbaij" - A matrix type to be used for distributed symmetric sparse block matrices,
2196: based on block compressed sparse row format. Only the upper triangular portion of the "diagonal" portion of
2197: the matrix is stored.
2199: For complex numbers by default this matrix is symmetric, NOT Hermitian symmetric. To make it Hermitian symmetric you
2200: can call `MatSetOption(A, MAT_HERMITIAN, PETSC_TRUE)`.
2202: Options Database Key:
2203: . -mat_type mpisbaij - sets the matrix type to "mpisbaij" during a call to `MatSetFromOptions()`
2205: Level: beginner
2207: Notes:
2208: Call `MatSetOption(A, MAT_STRUCTURE_ONLY, PETSC_TRUE)` before preallocation or `MatSetUp()` to store only the nonzero pattern.
2209: The assembled matrix has no numerical value array. Row and column indices supplied during insertion are retained, while numerical values are ignored.
2210: Such matrices can be used for structural operations, but not for numerical operations.
2212: The number of rows in the matrix must be less than or equal to the number of columns. Similarly the number of rows in the
2213: diagonal portion of the matrix of each process must be less than or equal to the number of columns.
2215: .seealso: [](ch_matrices), `Mat`, `MATSBAIJ`, `MATBAIJ`, `MatCreateBAIJ()`, `MATSEQSBAIJ`, `MatType`
2216: M*/
2218: typedef struct {
2219: MPIAIJ_MPIDense scatter;
2220: PetscSF sf[2];
2221: } MPISBAIJ_MPIDense;
2223: static PetscErrorCode MatMPISBAIJ_MPIDenseDestroy(PetscCtxRt ctx)
2224: {
2225: MPISBAIJ_MPIDense *data = *(MPISBAIJ_MPIDense **)ctx;
2227: PetscFunctionBegin;
2228: PetscCall(PetscSFDestroy(&data->sf[0]));
2229: PetscCall(PetscSFDestroy(&data->sf[1]));
2230: PetscCall(MatDestroy(&data->scatter.source));
2231: PetscCall(MatMPIDenseScatterDestroy_Private(&data->scatter));
2232: PetscCall(PetscFree(data));
2233: PetscFunctionReturn(PETSC_SUCCESS);
2234: }
2236: static PetscErrorCode MatMatMultNumeric_MPISBAIJ_MPIDense(Mat A, Mat B, Mat C)
2237: {
2238: Mat_MPISBAIJ *a = (Mat_MPISBAIJ *)A->data;
2239: MPISBAIJ_MPIDense *data;
2240: Mat Cl;
2241: PetscInt bs, m = A->rmap->n, n = a->B->cmap->n, N = B->cmap->N, Bbn, ldc;
2242: PetscBool hermitian = (PetscBool)(PetscDefined(USE_COMPLEX) && A->hermitian == PETSC_BOOL3_TRUE);
2244: PetscFunctionBegin;
2245: MatCheckProduct(C, 3);
2246: PetscCheck(C->product->data, PetscObjectComm((PetscObject)C), PETSC_ERR_PLIB, "Product data empty");
2247: if (N == 0) PetscFunctionReturn(PETSC_SUCCESS);
2248: data = (MPISBAIJ_MPIDense *)C->product->data;
2249: PetscCall(MatGetBlockSize(A, &bs));
2250: PetscCall(MatMPIDenseScatterUpdateLDA_Private(a->Mvctx, bs, B, C, &data->scatter));
2251: Bbn = data->scatter.workB->cmap->n;
2252: PetscCall(MatDenseGetLocalMatrix(B, &B));
2253: PetscCall(MatDenseGetLocalMatrix(C, &Cl));
2254: PetscCall(MatDenseGetLDA(Cl, &ldc));
2255: if (data->scatter.source == NULL && (n > 0 || (m > 0 && Bbn > 1 && ldc != m))) PetscCall(MatCreateSeqDense(PETSC_COMM_SELF, m, Bbn, NULL, &data->scatter.source));
2256: PetscCall(MatMatMultNumeric_SeqSBAIJ_SeqDense(a->A, B, Cl));
2257: for (PetscInt j = 0; j < N; j += Bbn) {
2258: const PetscInt cols = PetscMin(Bbn, N - j);
2259: Mat Bb = B, Cb = Cl, workB = data->scatter.workB, workC = n > 0 || (cols > 1 && ldc != m) ? data->scatter.source : NULL;
2260: Mat root;
2262: if (cols != N) {
2263: PetscCall(MatDenseGetSubMatrix(B, PETSC_DECIDE, PETSC_DECIDE, j, j + cols, &Bb));
2264: PetscCall(MatDenseGetSubMatrix(Cl, PETSC_DECIDE, PETSC_DECIDE, j, j + cols, &Cb));
2265: }
2266: if (cols < Bbn) {
2267: PetscCall(MatDenseGetSubMatrix(data->scatter.workB, PETSC_DECIDE, PETSC_DECIDE, 0, cols, &workB));
2268: if (workC != NULL) PetscCall(MatDenseGetSubMatrix(data->scatter.source, PETSC_DECIDE, PETSC_DECIDE, 0, cols, &workC));
2269: }
2270: PetscCall(MatMPIDenseScatter_Private(a->Mvctx, n, bs, workB, &data->scatter, Bb, C));
2271: if (n > 0) {
2272: PetscCall(MatMatMultNumeric_SeqBAIJ_SeqDense(a->B, workB, workC));
2273: if (hermitian == PETSC_TRUE) PetscCall(MatHermitianTransposeMatMultNumeric_SeqBAIJ_SeqDense(a->B, Bb, workB));
2274: else PetscCall(MatTransposeMatMultNumeric_SeqBAIJ_SeqDense(a->B, Bb, workB));
2275: } else if (workC != NULL) PetscCall(MatZeroEntries(workC));
2276: root = workC ? workC : Cb;
2277: PetscCall(MatDenseScatterWithSF_Private(data->sf[cols < Bbn], workB, root, MPIU_SUM, SCATTER_REVERSE, PETSC_FALSE));
2278: if (workC != NULL) PetscCall(MatAXPY(Cb, 1.0, workC, SAME_NONZERO_PATTERN));
2279: if (cols < Bbn) {
2280: if (workC != NULL) PetscCall(MatDenseRestoreSubMatrix(data->scatter.source, &workC));
2281: PetscCall(MatDenseRestoreSubMatrix(data->scatter.workB, &workB));
2282: }
2283: if (cols != N) {
2284: PetscCall(MatDenseRestoreSubMatrix(B, &Bb));
2285: PetscCall(MatDenseRestoreSubMatrix(Cl, &Cb));
2286: }
2287: }
2288: PetscFunctionReturn(PETSC_SUCCESS);
2289: }
2291: static PetscErrorCode MatMatMultSymbolic_MPISBAIJ_MPIDense(Mat A, Mat B, PetscReal fill, Mat C)
2292: {
2293: Mat_MPISBAIJ *a = (Mat_MPISBAIJ *)A->data;
2294: MPISBAIJ_MPIDense *data;
2295: PetscInt bs, n = a->B->cmap->n, N = B->cmap->N;
2297: PetscFunctionBegin;
2298: MatCheckProduct(C, 4);
2299: PetscCheck(!C->product->data, PetscObjectComm((PetscObject)C), PETSC_ERR_PLIB, "Product data not empty");
2300: PetscCall(MatDenseSetUpProduct_Private(C, A->rmap, B->cmap, B, NULL));
2301: PetscCall(PetscNew(&data));
2302: if (N > 0) {
2303: PetscInt Bbn;
2305: PetscCall(MatGetBlockSize(A, &bs));
2306: PetscCall(MatMPIDenseScatterSetUp_Private(a->Mvctx, n, bs, PETSC_DETERMINE, B, C, &data->scatter, NULL, NULL));
2307: Bbn = data->scatter.workB->cmap->n;
2308: PetscCall(PetscSFCreateStridedSF(a->Mvctx, Bbn, A->rmap->n, n, &data->sf[0]));
2309: if (N % Bbn > 0) PetscCall(PetscSFCreateStridedSF(a->Mvctx, N % Bbn, A->rmap->n, n, &data->sf[1]));
2310: }
2311: C->product->data = data;
2312: C->product->destroy = MatMPISBAIJ_MPIDenseDestroy;
2313: C->ops->matmultnumeric = MatMatMultNumeric_MPISBAIJ_MPIDense;
2314: PetscFunctionReturn(PETSC_SUCCESS);
2315: }
2317: static PetscErrorCode MatProductSetFromOptions_MPISBAIJ_MPIDense(Mat C)
2318: {
2319: Mat_Product *product = C->product;
2321: PetscFunctionBegin;
2322: MatCheckProduct(C, 1);
2323: if (product->type == MATPRODUCT_AB || ((PetscDefined(USE_REAL) || product->A->symmetric == PETSC_BOOL3_TRUE) && product->type == MATPRODUCT_AtB)) {
2324: C->ops->matmultsymbolic = MatMatMultSymbolic_MPISBAIJ_MPIDense;
2325: C->ops->productsymbolic = MatProductSymbolic_AB;
2326: }
2327: PetscFunctionReturn(PETSC_SUCCESS);
2328: }
2330: static PetscErrorCode MatGetMultPetscSF_MPISBAIJ(Mat A, PetscSF *sf)
2331: {
2332: Mat_MPISBAIJ *a = (Mat_MPISBAIJ *)A->data;
2334: PetscFunctionBegin;
2335: *sf = a->Mvctx;
2336: PetscFunctionReturn(PETSC_SUCCESS);
2337: }
2339: PETSC_EXTERN PetscErrorCode MatCreate_MPISBAIJ(Mat B)
2340: {
2341: Mat_MPISBAIJ *b;
2342: PetscBool flg = PETSC_FALSE;
2344: PetscFunctionBegin;
2345: PetscCall(PetscNew(&b));
2346: B->data = (void *)b;
2347: B->ops[0] = MatOps_Values;
2349: B->ops->destroy = MatDestroy_MPISBAIJ;
2350: B->ops->view = MatView_MPISBAIJ;
2351: B->assembled = PETSC_FALSE;
2352: B->insertmode = NOT_SET_VALUES;
2354: PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)B), &b->rank));
2355: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)B), &b->size));
2357: /* build local table of row and column ownerships */
2358: PetscCall(PetscMalloc1(b->size + 2, &b->rangebs));
2360: /* build cache for off array entries formed */
2361: PetscCall(MatStashCreate_Private(PetscObjectComm((PetscObject)B), 1, &B->stash));
2363: b->donotstash = PETSC_FALSE;
2364: b->colmap = NULL;
2365: b->garray = NULL;
2366: b->roworiented = PETSC_TRUE;
2368: /* stuff used in block assembly */
2369: b->barray = NULL;
2371: /* stuff used for matrix vector multiply */
2372: b->lvec = NULL;
2373: b->Mvctx = NULL;
2374: b->slvec0 = NULL;
2375: b->slvec0b = NULL;
2376: b->slvec1 = NULL;
2377: b->slvec1a = NULL;
2378: b->slvec1b = NULL;
2379: b->sMvctx = NULL;
2381: /* stuff for MatGetRow() */
2382: b->rowindices = NULL;
2383: b->rowvalues = NULL;
2384: b->getrowactive = PETSC_FALSE;
2386: /* hash table stuff */
2387: b->ht = NULL;
2388: b->hd = NULL;
2389: b->ht_size = 0;
2390: b->ht_flag = PETSC_FALSE;
2391: b->ht_fact = 0;
2392: b->ht_total_ct = 0;
2393: b->ht_insert_ct = 0;
2395: b->in_loc = NULL;
2396: b->v_loc = NULL;
2397: b->n_loc = 0;
2399: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatStoreValues_C", MatStoreValues_MPISBAIJ));
2400: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatRetrieveValues_C", MatRetrieveValues_MPISBAIJ));
2401: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMPISBAIJSetPreallocation_C", MatMPISBAIJSetPreallocation_MPISBAIJ));
2402: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMPISBAIJSetPreallocationCSR_C", MatMPISBAIJSetPreallocationCSR_MPISBAIJ));
2403: #if PetscDefined(HAVE_ELEMENTAL)
2404: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_mpisbaij_elemental_C", MatConvert_MPISBAIJ_Elemental));
2405: #endif
2406: #if PetscDefined(HAVE_SCALAPACK) && (PetscDefined(USE_REAL_SINGLE) || PetscDefined(USE_REAL_DOUBLE))
2407: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_mpisbaij_scalapack_C", MatConvert_SBAIJ_ScaLAPACK));
2408: #endif
2409: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_mpisbaij_mpiaij_C", MatConvert_MPISBAIJ_Basic));
2410: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_mpisbaij_mpibaij_C", MatConvert_MPISBAIJ_Basic));
2411: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_mpisbaij_mpidense_C", MatProductSetFromOptions_MPISBAIJ_MPIDense));
2412: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatGetMultPetscSF_C", MatGetMultPetscSF_MPISBAIJ));
2414: B->symmetric = PETSC_BOOL3_TRUE;
2415: B->structurally_symmetric = PETSC_BOOL3_TRUE;
2416: B->symmetry_eternal = PETSC_TRUE;
2417: B->structural_symmetry_eternal = PETSC_TRUE;
2418: #if !PetscDefined(USE_COMPLEX)
2419: B->hermitian = PETSC_BOOL3_TRUE;
2420: #endif
2422: PetscCall(PetscObjectChangeTypeName((PetscObject)B, MATMPISBAIJ));
2423: PetscOptionsBegin(PetscObjectComm((PetscObject)B), NULL, "Options for loading MPISBAIJ matrix 1", "Mat");
2424: PetscCall(PetscOptionsBool("-mat_use_hash_table", "Use hash table to save memory in constructing matrix", "MatSetOption", flg, &flg, NULL));
2425: if (flg) {
2426: PetscReal fact = 1.39;
2427: PetscCall(MatSetOption(B, MAT_USE_HASH_TABLE, PETSC_TRUE));
2428: PetscCall(PetscOptionsReal("-mat_use_hash_table", "Use hash table factor", "MatMPIBAIJSetHashTableFactor", fact, &fact, NULL));
2429: if (fact <= 1.0) fact = 1.39;
2430: PetscCall(MatMPIBAIJSetHashTableFactor(B, fact));
2431: PetscCall(PetscInfo(B, "Hash table Factor used %5.2g\n", (double)fact));
2432: }
2433: PetscOptionsEnd();
2434: PetscFunctionReturn(PETSC_SUCCESS);
2435: }
2437: // PetscClangLinter pragma disable: -fdoc-section-header-unknown
2438: /*MC
2439: MATSBAIJ - MATSBAIJ = "sbaij" - A matrix type to be used for symmetric block sparse matrices.
2441: This matrix type is identical to `MATSEQSBAIJ` when constructed with a single process communicator,
2442: and `MATMPISBAIJ` otherwise.
2444: Options Database Key:
2445: . -mat_type sbaij - sets the matrix type to `MATSBAIJ` during a call to `MatSetFromOptions()`
2447: Level: beginner
2449: Notes:
2450: Call `MatSetOption(A, MAT_STRUCTURE_ONLY, PETSC_TRUE)` before preallocation or `MatSetUp()` to store only the nonzero pattern.
2451: The assembled matrix has no numerical value array. Row and column indices supplied during insertion are retained, while numerical values are ignored.
2452: Such matrices can be used for structural operations, but not for numerical operations.
2454: .seealso: [](ch_matrices), `Mat`, `MATSEQSBAIJ`, `MATMPISBAIJ`, `MatCreateSBAIJ()`, `MATSEQBAIJ`, `MATMPIBAIJ`
2455: M*/
2457: /*@
2458: MatMPISBAIJSetPreallocation - For good matrix assembly performance
2459: the user should preallocate the matrix storage by setting the parameters
2460: d_nz (or d_nnz) and o_nz (or o_nnz). By setting these parameters accurately,
2461: performance can be increased by more than a factor of 50.
2463: Collective
2465: Input Parameters:
2466: + B - the matrix
2467: . bs - size of block, the blocks are ALWAYS square. One can use MatSetBlockSizes() to set a different row and column blocksize but the row
2468: blocksize always defines the size of the blocks. The column blocksize sets the blocksize of the vectors obtained with MatCreateVecs()
2469: . d_nz - number of block nonzeros per block row in diagonal portion of local
2470: submatrix (same for all local rows)
2471: . d_nnz - array containing the number of block nonzeros in the various block rows
2472: in the upper triangular and diagonal part of the in diagonal portion of the local
2473: (possibly different for each block row) or `NULL`. If you plan to factor the matrix you must leave room
2474: for the diagonal entry and set a value even if it is zero.
2475: . o_nz - number of block nonzeros per block row in the off-diagonal portion of local
2476: submatrix (same for all local rows).
2477: - o_nnz - array containing the number of nonzeros in the various block rows of the
2478: off-diagonal portion of the local submatrix that is right of the diagonal
2479: (possibly different for each block row) or `NULL`.
2481: Options Database Keys:
2482: + -mat_no_unroll (true|false) - uses code that does not unroll the loops in the
2483: block calculations (much slower)
2484: - -mat_block_size bs - size of the blocks to use
2486: Level: intermediate
2488: Notes:
2490: If `PETSC_DECIDE` or `PETSC_DETERMINE` is used for a particular argument on one processor
2491: than it must be used on all processors that share the object for that argument.
2493: If the *_nnz parameter is given then the *_nz parameter is ignored
2495: Storage Information:
2496: For a square global matrix we define each processor's diagonal portion
2497: to be its local rows and the corresponding columns (a square submatrix);
2498: each processor's off-diagonal portion encompasses the remainder of the
2499: local matrix (a rectangular submatrix).
2501: The user can specify preallocated storage for the diagonal part of
2502: the local submatrix with either `d_nz` or `d_nnz` (not both). Set
2503: `d_nz` = `PETSC_DEFAULT` and `d_nnz` = `NULL` for PETSc to control dynamic
2504: memory allocation. Likewise, specify preallocated storage for the
2505: off-diagonal part of the local submatrix with `o_nz` or `o_nnz` (not both).
2507: You can call `MatGetInfo()` to get information on how effective the preallocation was;
2508: for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
2509: You can also run with the option `-info` and look for messages with the string
2510: malloc in them to see if additional memory allocation was needed.
2512: Consider a processor that owns rows 3, 4 and 5 of a parallel matrix. In
2513: the figure below we depict these three local rows and all columns (0-11).
2515: .vb
2516: 0 1 2 3 4 5 6 7 8 9 10 11
2517: --------------------------
2518: row 3 |. . . d d d o o o o o o
2519: row 4 |. . . d d d o o o o o o
2520: row 5 |. . . d d d o o o o o o
2521: --------------------------
2522: .ve
2524: Thus, any entries in the d locations are stored in the d (diagonal)
2525: submatrix, and any entries in the o locations are stored in the
2526: o (off-diagonal) submatrix. Note that the d matrix is stored in
2527: `MATSEQSBAIJ` format and the o submatrix in `MATSEQBAIJ` format.
2529: Now `d_nz` should indicate the number of block nonzeros per row in the upper triangular
2530: plus the diagonal part of the d matrix,
2531: and `o_nz` should indicate the number of block nonzeros per row in the o matrix
2533: In general, for PDE problems in which most nonzeros are near the diagonal,
2534: one expects `d_nz` >> `o_nz`.
2536: .seealso: [](ch_matrices), `Mat`, `MATMPISBAIJ`, `MATSBAIJ`, `MatCreate()`, `MatCreateSeqSBAIJ()`, `MatSetValues()`, `MatCreateBAIJ()`, `PetscSplitOwnership()`
2537: @*/
2538: PetscErrorCode MatMPISBAIJSetPreallocation(Mat B, PetscInt bs, PetscInt d_nz, const PetscInt d_nnz[], PetscInt o_nz, const PetscInt o_nnz[])
2539: {
2540: PetscFunctionBegin;
2544: PetscTryMethod(B, "MatMPISBAIJSetPreallocation_C", (Mat, PetscInt, PetscInt, const PetscInt[], PetscInt, const PetscInt[]), (B, bs, d_nz, d_nnz, o_nz, o_nnz));
2545: PetscFunctionReturn(PETSC_SUCCESS);
2546: }
2548: // PetscClangLinter pragma disable: -fdoc-section-header-unknown
2549: /*@
2550: MatCreateSBAIJ - Creates a sparse parallel matrix in symmetric block AIJ format, `MATSBAIJ`,
2551: (block compressed row). For good matrix assembly performance
2552: the user should preallocate the matrix storage by setting the parameters
2553: `d_nz` (or `d_nnz`) and `o_nz` (or `o_nnz`).
2555: Collective
2557: Input Parameters:
2558: + comm - MPI communicator
2559: . bs - size of block, the blocks are ALWAYS square. One can use `MatSetBlockSizes()` to set a different row and column blocksize but the row
2560: blocksize always defines the size of the blocks. The column blocksize sets the blocksize of the vectors obtained with `MatCreateVecs()`
2561: . m - number of local rows (or `PETSC_DECIDE` to have calculated if `M` is given)
2562: This value should be the same as the local size used in creating the
2563: y vector for the matrix-vector product y = Ax.
2564: . n - number of local columns (or `PETSC_DECIDE` to have calculated if `N` is given)
2565: This value should be the same as the local size used in creating the
2566: x vector for the matrix-vector product y = Ax.
2567: . M - number of global rows (or `PETSC_DETERMINE` to have calculated if `m` is given)
2568: . N - number of global columns (or `PETSC_DETERMINE` to have calculated if `n` is given)
2569: . d_nz - number of block nonzeros per block row in diagonal portion of local
2570: submatrix (same for all local rows)
2571: . d_nnz - array containing the number of block nonzeros in the various block rows
2572: in the upper triangular portion of the in diagonal portion of the local
2573: (possibly different for each block block row) or `NULL`.
2574: If you plan to factor the matrix you must leave room for the diagonal entry and
2575: set its value even if it is zero.
2576: . o_nz - number of block nonzeros per block row in the off-diagonal portion of local
2577: submatrix (same for all local rows).
2578: - o_nnz - array containing the number of nonzeros in the various block rows of the
2579: off-diagonal portion of the local submatrix (possibly different for
2580: each block row) or `NULL`.
2582: Output Parameter:
2583: . A - the matrix
2585: Options Database Keys:
2586: + -mat_no_unroll (true|false) - uses code that does not unroll the loops in the
2587: block calculations (much slower)
2588: - -mat_block_size bs - size of the blocks to use
2590: Level: intermediate
2592: Notes:
2593: It is recommended that one use `MatCreateFromOptions()` or the `MatCreate()`, `MatSetType()` and/or `MatSetFromOptions()`,
2594: MatXXXXSetPreallocation() paradigm instead of this routine directly.
2595: [MatXXXXSetPreallocation() is, for example, `MatSeqAIJSetPreallocation()`]
2597: The number of rows and columns must be divisible by blocksize.
2598: This matrix type does not support complex Hermitian operation.
2600: The user MUST specify either the local or global matrix dimensions
2601: (possibly both).
2603: If `PETSC_DECIDE` or `PETSC_DETERMINE` is used for a particular argument on one processor
2604: than it must be used on all processors that share the object for that argument.
2606: If `m` and `n` are not `PETSC_DECIDE`, then the values determines the `PetscLayout` of the matrix and the ranges returned by
2607: `MatGetOwnershipRange()`, `MatGetOwnershipRanges()`, `MatGetOwnershipRangeColumn()`, and `MatGetOwnershipRangesColumn()`.
2609: If the *_nnz parameter is given then the *_nz parameter is ignored
2611: Storage Information:
2612: For a square global matrix we define each processor's diagonal portion
2613: to be its local rows and the corresponding columns (a square submatrix);
2614: each processor's off-diagonal portion encompasses the remainder of the
2615: local matrix (a rectangular submatrix).
2617: The user can specify preallocated storage for the diagonal part of
2618: the local submatrix with either `d_nz` or `d_nnz` (not both). Set
2619: `d_nz` = `PETSC_DEFAULT` and `d_nnz` = `NULL` for PETSc to control dynamic
2620: memory allocation. Likewise, specify preallocated storage for the
2621: off-diagonal part of the local submatrix with `o_nz` or `o_nnz` (not both).
2623: Consider a processor that owns rows 3, 4 and 5 of a parallel matrix. In
2624: the figure below we depict these three local rows and all columns (0-11).
2626: .vb
2627: 0 1 2 3 4 5 6 7 8 9 10 11
2628: --------------------------
2629: row 3 |. . . d d d o o o o o o
2630: row 4 |. . . d d d o o o o o o
2631: row 5 |. . . d d d o o o o o o
2632: --------------------------
2633: .ve
2635: Thus, any entries in the d locations are stored in the d (diagonal)
2636: submatrix, and any entries in the o locations are stored in the
2637: o (off-diagonal) submatrix. Note that the d matrix is stored in
2638: `MATSEQSBAIJ` format and the o submatrix in `MATSEQBAIJ` format.
2640: Now `d_nz` should indicate the number of block nonzeros per row in the upper triangular
2641: plus the diagonal part of the d matrix,
2642: and `o_nz` should indicate the number of block nonzeros per row in the o matrix.
2643: In general, for PDE problems in which most nonzeros are near the diagonal,
2644: one expects `d_nz` >> `o_nz`.
2646: .seealso: [](ch_matrices), `Mat`, `MATSBAIJ`, `MatCreate()`, `MatCreateSeqSBAIJ()`, `MatSetValues()`, `MatCreateBAIJ()`,
2647: `MatGetOwnershipRange()`, `MatGetOwnershipRanges()`, `MatGetOwnershipRangeColumn()`, `MatGetOwnershipRangesColumn()`, `PetscLayout`
2648: @*/
2649: PetscErrorCode MatCreateSBAIJ(MPI_Comm comm, PetscInt bs, PetscInt m, PetscInt n, PetscInt M, PetscInt N, PetscInt d_nz, const PetscInt d_nnz[], PetscInt o_nz, const PetscInt o_nnz[], Mat *A)
2650: {
2651: PetscMPIInt size;
2653: PetscFunctionBegin;
2654: PetscCall(MatCreate(comm, A));
2655: PetscCall(MatSetSizes(*A, m, n, M, N));
2656: PetscCallMPI(MPI_Comm_size(comm, &size));
2657: if (size > 1) {
2658: PetscCall(MatSetType(*A, MATMPISBAIJ));
2659: PetscCall(MatMPISBAIJSetPreallocation(*A, bs, d_nz, d_nnz, o_nz, o_nnz));
2660: } else {
2661: PetscCall(MatSetType(*A, MATSEQSBAIJ));
2662: PetscCall(MatSeqSBAIJSetPreallocation(*A, bs, d_nz, d_nnz));
2663: }
2664: PetscFunctionReturn(PETSC_SUCCESS);
2665: }
2667: static PetscErrorCode MatDuplicate_MPISBAIJ(Mat matin, MatDuplicateOption cpvalues, Mat *newmat)
2668: {
2669: Mat mat;
2670: Mat_MPISBAIJ *a, *oldmat = (Mat_MPISBAIJ *)matin->data;
2671: PetscInt len = 0, nt, bs = matin->rmap->bs, mbs = oldmat->mbs;
2672: PetscScalar *array;
2674: PetscFunctionBegin;
2675: *newmat = NULL;
2677: PetscCall(MatCreate(PetscObjectComm((PetscObject)matin), &mat));
2678: PetscCall(MatSetSizes(mat, matin->rmap->n, matin->cmap->n, matin->rmap->N, matin->cmap->N));
2679: PetscCall(MatSetType(mat, ((PetscObject)matin)->type_name));
2680: PetscCall(MatSetOption(mat, MAT_STRUCTURE_ONLY, matin->structure_only));
2681: PetscCall(PetscLayoutReference(matin->rmap, &mat->rmap));
2682: PetscCall(PetscLayoutReference(matin->cmap, &mat->cmap));
2684: if (matin->hash_active) PetscCall(MatSetUp(mat));
2685: else {
2686: mat->factortype = matin->factortype;
2687: mat->preallocated = PETSC_TRUE;
2688: mat->assembled = PETSC_TRUE;
2689: mat->insertmode = NOT_SET_VALUES;
2691: a = (Mat_MPISBAIJ *)mat->data;
2692: a->bs2 = oldmat->bs2;
2693: a->mbs = oldmat->mbs;
2694: a->nbs = oldmat->nbs;
2695: a->Mbs = oldmat->Mbs;
2696: a->Nbs = oldmat->Nbs;
2698: a->size = oldmat->size;
2699: a->rank = oldmat->rank;
2700: a->donotstash = oldmat->donotstash;
2701: a->roworiented = oldmat->roworiented;
2702: a->rowindices = NULL;
2703: a->rowvalues = NULL;
2704: a->getrowactive = PETSC_FALSE;
2705: a->barray = NULL;
2706: a->rstartbs = oldmat->rstartbs;
2707: a->rendbs = oldmat->rendbs;
2708: a->cstartbs = oldmat->cstartbs;
2709: a->cendbs = oldmat->cendbs;
2711: /* hash table stuff */
2712: a->ht = NULL;
2713: a->hd = NULL;
2714: a->ht_size = 0;
2715: a->ht_flag = oldmat->ht_flag;
2716: a->ht_fact = oldmat->ht_fact;
2717: a->ht_total_ct = 0;
2718: a->ht_insert_ct = 0;
2720: PetscCall(PetscArraycpy(a->rangebs, oldmat->rangebs, a->size + 2));
2721: if (oldmat->colmap) {
2722: #if PetscDefined(USE_CTABLE)
2723: PetscCall(PetscHMapIDuplicate(oldmat->colmap, &a->colmap));
2724: #else
2725: PetscCall(PetscMalloc1(a->Nbs, &a->colmap));
2726: PetscCall(PetscArraycpy(a->colmap, oldmat->colmap, a->Nbs));
2727: #endif
2728: } else a->colmap = NULL;
2730: if (oldmat->garray && (len = ((Mat_SeqBAIJ *)oldmat->B->data)->nbs)) {
2731: PetscCall(PetscMalloc1(len, &a->garray));
2732: PetscCall(PetscArraycpy(a->garray, oldmat->garray, len));
2733: } else a->garray = NULL;
2735: PetscCall(MatStashCreate_Private(PetscObjectComm((PetscObject)matin), matin->rmap->bs, &mat->bstash));
2736: if (oldmat->lvec) PetscCall(VecDuplicate(oldmat->lvec, &a->lvec));
2737: if (oldmat->Mvctx) {
2738: a->Mvctx = oldmat->Mvctx;
2739: PetscCall(PetscObjectReference((PetscObject)oldmat->Mvctx));
2740: }
2742: PetscCall(VecDuplicate(oldmat->slvec0, &a->slvec0));
2743: PetscCall(VecDuplicate(oldmat->slvec1, &a->slvec1));
2745: PetscCall(VecGetLocalSize(a->slvec1, &nt));
2746: PetscCall(VecGetArray(a->slvec1, &array));
2747: PetscCall(VecCreateSeqWithArray(PETSC_COMM_SELF, 1, bs * mbs, array, &a->slvec1a));
2748: PetscCall(VecCreateSeqWithArray(PETSC_COMM_SELF, 1, nt - bs * mbs, array + bs * mbs, &a->slvec1b));
2749: PetscCall(VecRestoreArray(a->slvec1, &array));
2750: PetscCall(VecGetArray(a->slvec0, &array));
2751: PetscCall(VecCreateSeqWithArray(PETSC_COMM_SELF, 1, nt - bs * mbs, array + bs * mbs, &a->slvec0b));
2752: PetscCall(VecRestoreArray(a->slvec0, &array));
2754: /* ierr = VecScatterCopy(oldmat->sMvctx,&a->sMvctx); - not written yet, replaced by the lazy trick: */
2755: PetscCall(PetscObjectReference((PetscObject)oldmat->sMvctx));
2756: a->sMvctx = oldmat->sMvctx;
2758: PetscCall(MatDuplicate(oldmat->A, cpvalues, &a->A));
2759: PetscCall(MatDuplicate(oldmat->B, cpvalues, &a->B));
2760: }
2761: PetscCall(PetscFunctionListDuplicate(((PetscObject)matin)->qlist, &((PetscObject)mat)->qlist));
2762: *newmat = mat;
2763: PetscFunctionReturn(PETSC_SUCCESS);
2764: }
2766: /* Used for both MPIBAIJ and MPISBAIJ matrices */
2767: #define MatLoad_MPISBAIJ_Binary MatLoad_MPIBAIJ_Binary
2769: static PetscErrorCode MatLoad_MPISBAIJ(Mat mat, PetscViewer viewer)
2770: {
2771: PetscBool isbinary;
2773: PetscFunctionBegin;
2774: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
2775: PetscCheck(isbinary, PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Viewer type %s not yet supported for reading %s matrices", ((PetscObject)viewer)->type_name, ((PetscObject)mat)->type_name);
2776: PetscCall(MatLoad_MPISBAIJ_Binary(mat, viewer));
2777: PetscFunctionReturn(PETSC_SUCCESS);
2778: }
2780: static PetscErrorCode MatGetRowMaxAbs_MPISBAIJ(Mat A, Vec v, PetscInt idx[])
2781: {
2782: Mat_MPISBAIJ *a = (Mat_MPISBAIJ *)A->data;
2783: Mat_SeqBAIJ *b = (Mat_SeqBAIJ *)a->B->data;
2784: PetscReal atmp;
2785: PetscReal *work, *svalues, *rvalues;
2786: PetscInt i, bs, mbs, *bi, *bj, brow, j, ncols, krow, kcol, col, row, Mbs, bcol;
2787: PetscMPIInt rank, size;
2788: PetscInt *rowners_bs, count, source;
2789: PetscScalar *va;
2790: MatScalar *ba;
2791: MPI_Status stat;
2793: PetscFunctionBegin;
2794: PetscCheck(!idx, PETSC_COMM_SELF, PETSC_ERR_SUP, "Send email to petsc-maint@mcs.anl.gov");
2795: PetscCall(MatGetRowMaxAbs(a->A, v, NULL));
2796: PetscCall(VecGetArray(v, &va));
2798: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)A), &size));
2799: PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank));
2801: bs = A->rmap->bs;
2802: mbs = a->mbs;
2803: Mbs = a->Mbs;
2804: ba = b->a;
2805: bi = b->i;
2806: bj = b->j;
2808: /* find ownerships */
2809: rowners_bs = A->rmap->range;
2811: /* each proc creates an array to be distributed */
2812: PetscCall(PetscCalloc1(bs * Mbs, &work));
2814: /* row_max for B */
2815: if (rank != size - 1) {
2816: for (i = 0; i < mbs; i++) {
2817: ncols = bi[1] - bi[0];
2818: bi++;
2819: brow = bs * i;
2820: for (j = 0; j < ncols; j++) {
2821: bcol = bs * (*bj);
2822: for (kcol = 0; kcol < bs; kcol++) {
2823: col = bcol + kcol; /* local col index */
2824: col += rowners_bs[rank + 1]; /* global col index */
2825: for (krow = 0; krow < bs; krow++) {
2826: atmp = PetscAbsScalar(*ba);
2827: ba++;
2828: row = brow + krow; /* local row index */
2829: if (PetscRealPart(va[row]) < atmp) va[row] = atmp;
2830: if (work[col] < atmp) work[col] = atmp;
2831: }
2832: }
2833: bj++;
2834: }
2835: }
2837: /* send values to its owners */
2838: for (PetscMPIInt dest = rank + 1; dest < size; dest++) {
2839: svalues = work + rowners_bs[dest];
2840: count = rowners_bs[dest + 1] - rowners_bs[dest];
2841: PetscCallMPI(MPIU_Send(svalues, count, MPIU_REAL, dest, rank, PetscObjectComm((PetscObject)A)));
2842: }
2843: }
2845: /* receive values */
2846: if (rank) {
2847: rvalues = work;
2848: count = rowners_bs[rank + 1] - rowners_bs[rank];
2849: for (source = 0; source < rank; source++) {
2850: PetscCallMPI(MPIU_Recv(rvalues, count, MPIU_REAL, MPI_ANY_SOURCE, MPI_ANY_TAG, PetscObjectComm((PetscObject)A), &stat));
2851: /* process values */
2852: for (i = 0; i < count; i++) {
2853: if (PetscRealPart(va[i]) < rvalues[i]) va[i] = rvalues[i];
2854: }
2855: }
2856: }
2858: PetscCall(VecRestoreArray(v, &va));
2859: PetscCall(PetscFree(work));
2860: PetscFunctionReturn(PETSC_SUCCESS);
2861: }
2863: static PetscErrorCode MatSOR_MPISBAIJ(Mat matin, Vec bb, PetscReal omega, MatSORType flag, PetscReal fshift, PetscInt its, PetscInt lits, Vec xx)
2864: {
2865: Mat_MPISBAIJ *mat = (Mat_MPISBAIJ *)matin->data;
2866: PetscInt mbs = mat->mbs, bs = matin->rmap->bs;
2867: PetscScalar *x, *ptr, *from;
2868: Vec bb1;
2869: const PetscScalar *b;
2871: PetscFunctionBegin;
2872: PetscCheck(its > 0 && lits > 0, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Relaxation requires global its %" PetscInt_FMT " and local its %" PetscInt_FMT " both positive", its, lits);
2873: PetscCheck(bs <= 1, PETSC_COMM_SELF, PETSC_ERR_SUP, "SSOR for block size > 1 is not yet implemented");
2875: if (flag == SOR_APPLY_UPPER) {
2876: PetscUseTypeMethod(mat->A, sor, bb, omega, flag, fshift, lits, 1, xx);
2877: PetscFunctionReturn(PETSC_SUCCESS);
2878: }
2880: if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP) {
2881: if (flag & SOR_ZERO_INITIAL_GUESS) {
2882: PetscUseTypeMethod(mat->A, sor, bb, omega, flag, fshift, lits, lits, xx);
2883: its--;
2884: }
2886: PetscCall(VecDuplicate(bb, &bb1));
2887: while (its--) {
2888: /* lower triangular part: slvec0b = - B^T*xx */
2889: PetscUseTypeMethod(mat->B, multtranspose, xx, mat->slvec0b);
2891: /* copy xx into slvec0 */
2892: PetscCall(VecGetArray(mat->slvec0, &ptr));
2893: PetscCall(VecGetArray(xx, &x));
2894: PetscCall(PetscArraycpy(ptr, x, bs * mbs));
2895: PetscCall(VecRestoreArray(mat->slvec0, &ptr));
2897: PetscCall(VecScale(mat->slvec0, -1.0));
2899: /* copy bb into slvec1a */
2900: PetscCall(VecGetArray(mat->slvec1, &ptr));
2901: PetscCall(VecGetArrayRead(bb, &b));
2902: PetscCall(PetscArraycpy(ptr, b, bs * mbs));
2903: PetscCall(VecRestoreArray(mat->slvec1, &ptr));
2905: /* set slvec1b = 0 */
2906: PetscCall(PetscObjectStateIncrease((PetscObject)mat->slvec1b));
2907: PetscCall(VecZeroEntries(mat->slvec1b));
2909: PetscCall(VecScatterBegin(mat->sMvctx, mat->slvec0, mat->slvec1, ADD_VALUES, SCATTER_FORWARD));
2910: PetscCall(VecRestoreArray(xx, &x));
2911: PetscCall(VecRestoreArrayRead(bb, &b));
2912: PetscCall(VecScatterEnd(mat->sMvctx, mat->slvec0, mat->slvec1, ADD_VALUES, SCATTER_FORWARD));
2914: /* upper triangular part: bb1 = bb1 - B*x */
2915: PetscUseTypeMethod(mat->B, multadd, mat->slvec1b, mat->slvec1a, bb1);
2917: /* local diagonal sweep */
2918: PetscUseTypeMethod(mat->A, sor, bb1, omega, SOR_SYMMETRIC_SWEEP, fshift, lits, lits, xx);
2919: }
2920: PetscCall(VecDestroy(&bb1));
2921: } else if ((flag & SOR_LOCAL_FORWARD_SWEEP) && (its == 1) && (flag & SOR_ZERO_INITIAL_GUESS)) {
2922: PetscUseTypeMethod(mat->A, sor, bb, omega, flag, fshift, lits, 1, xx);
2923: } else if ((flag & SOR_LOCAL_BACKWARD_SWEEP) && (its == 1) && (flag & SOR_ZERO_INITIAL_GUESS)) {
2924: PetscUseTypeMethod(mat->A, sor, bb, omega, flag, fshift, lits, 1, xx);
2925: } else if (flag & SOR_EISENSTAT) {
2926: Vec xx1;
2927: PetscBool hasop;
2928: const PetscScalar *diag;
2929: PetscScalar *sl, scale = (omega - 2.0) / omega;
2930: PetscInt n;
2932: if (!mat->xx1) {
2933: PetscCall(VecDuplicate(bb, &mat->xx1));
2934: PetscCall(VecDuplicate(bb, &mat->bb1));
2935: }
2936: xx1 = mat->xx1;
2937: bb1 = mat->bb1;
2939: PetscUseTypeMethod(mat->A, sor, bb, omega, (MatSORType)(SOR_ZERO_INITIAL_GUESS | SOR_LOCAL_BACKWARD_SWEEP), fshift, lits, 1, xx);
2941: if (!mat->diag) {
2942: /* this is wrong for same matrix with new nonzero values */
2943: PetscCall(MatCreateVecs(matin, &mat->diag, NULL));
2944: PetscCall(MatGetDiagonal(matin, mat->diag));
2945: }
2946: PetscCall(MatHasOperation(matin, MATOP_MULT_DIAGONAL_BLOCK, &hasop));
2948: if (hasop) {
2949: PetscCall(MatMultDiagonalBlock(matin, xx, bb1));
2950: PetscCall(VecAYPX(mat->slvec1a, scale, bb));
2951: } else {
2952: /*
2953: These two lines are replaced by code that may be a bit faster for a good compiler
2954: PetscCall(VecPointwiseMult(mat->slvec1a,mat->diag,xx));
2955: PetscCall(VecAYPX(mat->slvec1a,scale,bb));
2956: */
2957: PetscCall(VecGetArray(mat->slvec1a, &sl));
2958: PetscCall(VecGetArrayRead(mat->diag, &diag));
2959: PetscCall(VecGetArrayRead(bb, &b));
2960: PetscCall(VecGetArray(xx, &x));
2961: PetscCall(VecGetLocalSize(xx, &n));
2962: if (omega == 1.0) {
2963: for (PetscInt i = 0; i < n; i++) sl[i] = b[i] - diag[i] * x[i];
2964: PetscCall(PetscLogFlops(2.0 * n));
2965: } else {
2966: for (PetscInt i = 0; i < n; i++) sl[i] = b[i] + scale * diag[i] * x[i];
2967: PetscCall(PetscLogFlops(3.0 * n));
2968: }
2969: PetscCall(VecRestoreArray(mat->slvec1a, &sl));
2970: PetscCall(VecRestoreArrayRead(mat->diag, &diag));
2971: PetscCall(VecRestoreArrayRead(bb, &b));
2972: PetscCall(VecRestoreArray(xx, &x));
2973: }
2975: /* multiply off-diagonal portion of matrix */
2976: PetscCall(PetscObjectStateIncrease((PetscObject)mat->slvec1b));
2977: PetscCall(VecZeroEntries(mat->slvec1b));
2978: PetscUseTypeMethod(mat->B, multtranspose, xx, mat->slvec0b);
2979: PetscCall(VecGetArray(mat->slvec0, &from));
2980: PetscCall(VecGetArray(xx, &x));
2981: PetscCall(PetscArraycpy(from, x, bs * mbs));
2982: PetscCall(VecRestoreArray(mat->slvec0, &from));
2983: PetscCall(VecRestoreArray(xx, &x));
2984: PetscCall(VecScatterBegin(mat->sMvctx, mat->slvec0, mat->slvec1, ADD_VALUES, SCATTER_FORWARD));
2985: PetscCall(VecScatterEnd(mat->sMvctx, mat->slvec0, mat->slvec1, ADD_VALUES, SCATTER_FORWARD));
2986: PetscUseTypeMethod(mat->B, multadd, mat->slvec1b, mat->slvec1a, mat->slvec1a);
2988: /* local sweep */
2989: PetscUseTypeMethod(mat->A, sor, mat->slvec1a, omega, (MatSORType)(SOR_ZERO_INITIAL_GUESS | SOR_LOCAL_FORWARD_SWEEP), fshift, lits, 1, xx1);
2990: PetscCall(VecAXPY(xx, 1.0, xx1));
2991: } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "MatSORType is not supported for SBAIJ matrix format");
2992: PetscFunctionReturn(PETSC_SUCCESS);
2993: }
2995: /*@
2996: MatCreateMPISBAIJWithArrays - creates a `MATMPISBAIJ` matrix using arrays that contain in standard CSR format for the local rows.
2998: Collective
3000: Input Parameters:
3001: + comm - MPI communicator
3002: . bs - the block size, only a block size of 1 is supported
3003: . m - number of local rows (Cannot be `PETSC_DECIDE`)
3004: . n - This value should be the same as the local size used in creating the
3005: x vector for the matrix-vector product $y = Ax$. (or `PETSC_DECIDE` to have
3006: calculated if `N` is given) For square matrices `n` is almost always `m`.
3007: . M - number of global rows (or `PETSC_DETERMINE` to have calculated if `m` is given)
3008: . N - number of global columns (or `PETSC_DETERMINE` to have calculated if `n` is given)
3009: . i - row indices; that is i[0] = 0, i[row] = i[row-1] + number of block elements in that row block row of the matrix
3010: . j - column indices
3011: - a - matrix values
3013: Output Parameter:
3014: . mat - the matrix
3016: Level: intermediate
3018: Notes:
3019: The `i`, `j`, and `a` arrays ARE copied by this routine into the internal format used by PETSc;
3020: thus you CANNOT change the matrix entries by changing the values of `a` after you have
3021: called this routine. Use `MatCreateMPIAIJWithSplitArrays()` to avoid needing to copy the arrays.
3023: The `i` and `j` indices are 0 based, and `i` indices are indices corresponding to the local `j` array.
3025: .seealso: [](ch_matrices), `Mat`, `MATMPISBAIJ`, `MatCreate()`, `MatCreateSeqAIJ()`, `MatSetValues()`, `MatMPIAIJSetPreallocation()`, `MatMPIAIJSetPreallocationCSR()`,
3026: `MATMPIAIJ`, `MatCreateAIJ()`, `MatCreateMPIAIJWithSplitArrays()`, `MatMPISBAIJSetPreallocationCSR()`
3027: @*/
3028: PetscErrorCode MatCreateMPISBAIJWithArrays(MPI_Comm comm, PetscInt bs, PetscInt m, PetscInt n, PetscInt M, PetscInt N, const PetscInt i[], const PetscInt j[], const PetscScalar a[], Mat *mat)
3029: {
3030: PetscFunctionBegin;
3031: PetscCheck(!i[0], PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "i (row indices) must start with 0");
3032: PetscCheck(m >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "local number of rows (m) cannot be PETSC_DECIDE, or negative");
3033: PetscCall(MatCreate(comm, mat));
3034: PetscCall(MatSetSizes(*mat, m, n, M, N));
3035: PetscCall(MatSetType(*mat, MATMPISBAIJ));
3036: PetscCall(MatMPISBAIJSetPreallocationCSR(*mat, bs, i, j, a));
3037: PetscFunctionReturn(PETSC_SUCCESS);
3038: }
3040: /*@
3041: MatMPISBAIJSetPreallocationCSR - Creates a sparse parallel matrix in `MATMPISBAIJ` format using the given nonzero structure and (optional) numerical values
3043: Collective
3045: Input Parameters:
3046: + B - the matrix
3047: . bs - the block size
3048: . i - the indices into `j` for the start of each local row (indices start with zero)
3049: . j - the column indices for each local row (indices start with zero) these must be sorted for each row
3050: - v - optional values in the matrix, pass `NULL` if not provided
3052: Level: advanced
3054: Notes:
3055: The `i`, `j`, and `v` arrays ARE copied by this routine into the internal format used by PETSc;
3056: thus you CANNOT change the matrix entries by changing the values of `v` after you have
3057: called this routine.
3059: Though this routine has Preallocation() in the name it also sets the exact nonzero locations of the matrix entries
3060: and usually the numerical values as well
3062: Any entries passed in that are below the diagonal are ignored
3064: .seealso: [](ch_matrices), `Mat`, `MATMPISBAIJ`, `MatCreate()`, `MatCreateSeqAIJ()`, `MatSetValues()`, `MatMPIBAIJSetPreallocation()`, `MatCreateAIJ()`, `MATMPIAIJ`,
3065: `MatCreateMPISBAIJWithArrays()`
3066: @*/
3067: PetscErrorCode MatMPISBAIJSetPreallocationCSR(Mat B, PetscInt bs, const PetscInt i[], const PetscInt j[], const PetscScalar v[])
3068: {
3069: PetscFunctionBegin;
3070: PetscTryMethod(B, "MatMPISBAIJSetPreallocationCSR_C", (Mat, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[]), (B, bs, i, j, v));
3071: PetscFunctionReturn(PETSC_SUCCESS);
3072: }
3074: PetscErrorCode MatCreateMPIMatConcatenateSeqMat_MPISBAIJ(MPI_Comm comm, Mat inmat, PetscInt n, MatReuse scall, Mat *outmat)
3075: {
3076: PetscInt m, N, i, rstart, nnz, Ii, bs, cbs;
3077: PetscInt *indx;
3078: PetscScalar *values;
3079: const PetscBool getrow_utriangular = ((Mat_SeqSBAIJ *)inmat->data)->getrow_utriangular;
3081: PetscFunctionBegin;
3082: PetscCall(MatGetSize(inmat, &m, &N));
3083: if (scall == MAT_INITIAL_MATRIX) { /* symbolic phase */
3084: Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ *)inmat->data;
3085: PetscInt *dnz, *onz, mbs, Nbs, nbs;
3086: PetscInt *bindx, rmax = a->rmax, j;
3087: PetscMPIInt rank, size;
3089: PetscCall(MatGetBlockSizes(inmat, &bs, &cbs));
3090: mbs = m / bs;
3091: Nbs = N / cbs;
3092: if (n == PETSC_DECIDE) PetscCall(PetscSplitOwnershipBlock(comm, cbs, &n, &N));
3093: nbs = n / cbs;
3095: PetscCall(PetscMalloc1(rmax, &bindx));
3096: MatPreallocateBegin(comm, mbs, nbs, dnz, onz); /* inline function, output __end and __rstart are used below */
3098: PetscCallMPI(MPI_Comm_rank(comm, &rank));
3099: PetscCallMPI(MPI_Comm_size(comm, &size));
3100: if (rank == size - 1) {
3101: /* Check sum(nbs) = Nbs */
3102: PetscCheck(__end == Nbs, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Sum of local block columns %" PetscInt_FMT " != global block columns %" PetscInt_FMT, __end, Nbs);
3103: }
3105: rstart = __rstart; /* block rstart of *outmat; see inline function MatPreallocateBegin */
3106: PetscCall(MatSetOption(inmat, MAT_GETROW_UPPERTRIANGULAR, PETSC_TRUE));
3107: for (i = 0; i < mbs; i++) {
3108: PetscCall(MatGetRow_SeqSBAIJ(inmat, i * bs, &nnz, &indx, NULL)); /* non-blocked nnz and indx */
3109: nnz = nnz / bs;
3110: for (j = 0; j < nnz; j++) bindx[j] = indx[j * bs] / bs;
3111: PetscCall(MatPreallocateSet(i + rstart, nnz, bindx, dnz, onz));
3112: PetscCall(MatRestoreRow_SeqSBAIJ(inmat, i * bs, &nnz, &indx, NULL));
3113: }
3114: PetscCall(MatSetOption(inmat, MAT_GETROW_UPPERTRIANGULAR, getrow_utriangular));
3115: PetscCall(PetscFree(bindx));
3117: PetscCall(MatCreate(comm, outmat));
3118: PetscCall(MatSetSizes(*outmat, m, n, PETSC_DETERMINE, PETSC_DETERMINE));
3119: PetscCall(MatSetBlockSizes(*outmat, bs, cbs));
3120: PetscCall(MatSetType(*outmat, MATSBAIJ));
3121: PetscCall(MatSeqSBAIJSetPreallocation(*outmat, bs, 0, dnz));
3122: PetscCall(MatMPISBAIJSetPreallocation(*outmat, bs, 0, dnz, 0, onz));
3123: MatPreallocateEnd(dnz, onz);
3124: }
3126: /* numeric phase */
3127: PetscCall(MatGetBlockSizes(inmat, &bs, &cbs));
3128: PetscCall(MatGetOwnershipRange(*outmat, &rstart, NULL));
3130: PetscCall(MatSetOption(inmat, MAT_GETROW_UPPERTRIANGULAR, PETSC_TRUE));
3131: for (i = 0; i < m; i++) {
3132: PetscCall(MatGetRow_SeqSBAIJ(inmat, i, &nnz, &indx, &values));
3133: Ii = i + rstart;
3134: PetscCall(MatSetValues(*outmat, 1, &Ii, nnz, indx, values, INSERT_VALUES));
3135: PetscCall(MatRestoreRow_SeqSBAIJ(inmat, i, &nnz, &indx, &values));
3136: }
3137: PetscCall(MatSetOption(inmat, MAT_GETROW_UPPERTRIANGULAR, getrow_utriangular));
3138: PetscCall(MatAssemblyBegin(*outmat, MAT_FINAL_ASSEMBLY));
3139: PetscCall(MatAssemblyEnd(*outmat, MAT_FINAL_ASSEMBLY));
3140: PetscFunctionReturn(PETSC_SUCCESS);
3141: }