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 <petscblaslapack.h>
5: #include <petscsf.h>
7: static PetscErrorCode MatDestroy_MPISBAIJ(Mat mat)
8: {
9: Mat_MPISBAIJ *baij = (Mat_MPISBAIJ *)mat->data;
11: PetscFunctionBegin;
12: PetscCall(PetscLogObjectState((PetscObject)mat, "Rows=%" PetscInt_FMT ",Cols=%" PetscInt_FMT, mat->rmap->N, mat->cmap->N));
13: PetscCall(MatStashDestroy_Private(&mat->stash));
14: PetscCall(MatStashDestroy_Private(&mat->bstash));
15: PetscCall(MatDestroy(&baij->A));
16: PetscCall(MatDestroy(&baij->B));
17: #if PetscDefined(USE_CTABLE)
18: PetscCall(PetscHMapIDestroy(&baij->colmap));
19: #else
20: PetscCall(PetscFree(baij->colmap));
21: #endif
22: PetscCall(PetscFree(baij->garray));
23: PetscCall(VecDestroy(&baij->lvec));
24: PetscCall(VecScatterDestroy(&baij->Mvctx));
25: PetscCall(VecDestroy(&baij->slvec0));
26: PetscCall(VecDestroy(&baij->slvec0b));
27: PetscCall(VecDestroy(&baij->slvec1));
28: PetscCall(VecDestroy(&baij->slvec1a));
29: PetscCall(VecDestroy(&baij->slvec1b));
30: PetscCall(VecScatterDestroy(&baij->sMvctx));
31: PetscCall(PetscFree2(baij->rowvalues, baij->rowindices));
32: PetscCall(PetscFree(baij->barray));
33: PetscCall(PetscFree(baij->hd));
34: PetscCall(VecDestroy(&baij->diag));
35: PetscCall(VecDestroy(&baij->bb1));
36: PetscCall(VecDestroy(&baij->xx1));
37: #if PetscDefined(USE_REAL_MAT_SINGLE)
38: PetscCall(PetscFree(baij->setvaluescopy));
39: #endif
40: PetscCall(PetscFree(baij->in_loc));
41: PetscCall(PetscFree(baij->v_loc));
42: PetscCall(PetscFree(baij->rangebs));
43: PetscCall(PetscFree(mat->data));
45: PetscCall(PetscObjectChangeTypeName((PetscObject)mat, NULL));
46: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatStoreValues_C", NULL));
47: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatRetrieveValues_C", NULL));
48: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatGetMultPetscSF_C", NULL));
49: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatMPISBAIJSetPreallocation_C", NULL));
50: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatMPISBAIJSetPreallocationCSR_C", NULL));
51: #if PetscDefined(HAVE_ELEMENTAL)
52: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_mpisbaij_elemental_C", NULL));
53: #endif
54: #if PetscDefined(HAVE_SCALAPACK) && (PetscDefined(USE_REAL_SINGLE) || PetscDefined(USE_REAL_DOUBLE))
55: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_mpisbaij_scalapack_C", NULL));
56: #endif
57: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_mpisbaij_mpiaij_C", NULL));
58: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_mpisbaij_mpibaij_C", NULL));
59: PetscFunctionReturn(PETSC_SUCCESS);
60: }
62: /* defines MatSetValues_MPI_Hash(), MatAssemblyBegin_MPI_Hash(), MatAssemblyEnd_MPI_Hash(), MatSetUp_MPI_Hash() */
63: #define TYPE SBAIJ
64: #define TYPE_SBAIJ
65: #include "../src/mat/impls/aij/mpi/mpihashmat.h"
66: #undef TYPE
67: #undef TYPE_SBAIJ
69: #if PetscDefined(HAVE_ELEMENTAL)
70: PETSC_INTERN PetscErrorCode MatConvert_MPISBAIJ_Elemental(Mat, MatType, MatReuse, Mat *);
71: #endif
72: #if PetscDefined(HAVE_SCALAPACK) && (PetscDefined(USE_REAL_SINGLE) || PetscDefined(USE_REAL_DOUBLE))
73: PETSC_INTERN PetscErrorCode MatConvert_SBAIJ_ScaLAPACK(Mat, MatType, MatReuse, Mat *);
74: #endif
76: /* This could be moved to matimpl.h */
77: static PetscErrorCode MatPreallocateWithMats_Private(Mat B, PetscInt nm, Mat X[], PetscBool symm[], PetscBool fill)
78: {
79: Mat preallocator;
80: PetscInt r, rstart, rend;
81: PetscInt bs, i, m, n, M, N;
82: PetscBool cong = PETSC_TRUE;
84: PetscFunctionBegin;
87: for (i = 0; i < nm; i++) {
89: PetscCall(PetscLayoutCompare(B->rmap, X[i]->rmap, &cong));
90: PetscCheck(cong, PetscObjectComm((PetscObject)B), PETSC_ERR_SUP, "Not for different layouts");
91: }
93: PetscCall(MatGetBlockSize(B, &bs));
94: PetscCall(MatGetSize(B, &M, &N));
95: PetscCall(MatGetLocalSize(B, &m, &n));
96: PetscCall(MatCreate(PetscObjectComm((PetscObject)B), &preallocator));
97: PetscCall(MatSetType(preallocator, MATPREALLOCATOR));
98: PetscCall(MatSetBlockSize(preallocator, bs));
99: PetscCall(MatSetSizes(preallocator, m, n, M, N));
100: PetscCall(MatSetUp(preallocator));
101: PetscCall(MatGetOwnershipRange(preallocator, &rstart, &rend));
102: for (r = rstart; r < rend; ++r) {
103: PetscInt ncols;
104: const PetscInt *row;
106: for (i = 0; i < nm; i++) {
107: PetscCall(MatGetRow(X[i], r, &ncols, &row, NULL));
108: PetscCall(MatSetValues(preallocator, 1, &r, ncols, row, NULL, INSERT_VALUES));
109: if (symm && symm[i]) PetscCall(MatSetValues(preallocator, ncols, row, 1, &r, NULL, INSERT_VALUES));
110: PetscCall(MatRestoreRow(X[i], r, &ncols, &row, NULL));
111: }
112: }
113: PetscCall(MatAssemblyBegin(preallocator, MAT_FINAL_ASSEMBLY));
114: PetscCall(MatAssemblyEnd(preallocator, MAT_FINAL_ASSEMBLY));
115: PetscCall(MatPreallocatorPreallocate(preallocator, fill, B));
116: PetscCall(MatDestroy(&preallocator));
117: PetscFunctionReturn(PETSC_SUCCESS);
118: }
120: PETSC_INTERN PetscErrorCode MatSBAIJCreateSymmetricStructure_Private(Mat A, MatType newtype, PetscBool structure_only, Mat *B)
121: {
122: PetscBool symm = PETSC_TRUE, isdense;
123: PetscInt bs;
125: PetscFunctionBegin;
126: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), B));
127: PetscCall(MatSetSizes(*B, A->rmap->n, A->cmap->n, A->rmap->N, A->cmap->N));
128: PetscCall(MatSetType(*B, newtype));
129: PetscCall(MatSetOption(*B, MAT_STRUCTURE_ONLY, structure_only));
130: PetscCall(MatGetBlockSize(A, &bs));
131: PetscCall(MatSetBlockSize(*B, bs));
132: PetscCall(PetscLayoutSetUp((*B)->rmap));
133: PetscCall(PetscLayoutSetUp((*B)->cmap));
134: PetscCall(PetscObjectTypeCompareAny((PetscObject)*B, &isdense, MATSEQDENSE, MATMPIDENSE, MATSEQDENSECUDA, ""));
135: if (!isdense) {
136: /* create the complete symmetric nonzero structure */
137: PetscCall(MatGetRowUpperTriangular(A));
138: PetscCall(MatPreallocateWithMats_Private(*B, 1, &A, &symm, PETSC_TRUE));
139: PetscCall(MatRestoreRowUpperTriangular(A));
140: } else PetscCall(MatSetUp(*B));
141: PetscFunctionReturn(PETSC_SUCCESS);
142: }
144: PETSC_INTERN PetscErrorCode MatConvert_MPISBAIJ_Basic(Mat A, MatType newtype, MatReuse reuse, Mat *newmat)
145: {
146: Mat B;
148: PetscFunctionBegin;
149: if (reuse != MAT_REUSE_MATRIX) PetscCall(MatSBAIJCreateSymmetricStructure_Private(A, newtype, PETSC_FALSE, &B));
150: else {
151: B = *newmat;
152: PetscCall(MatZeroEntries(B));
153: }
155: PetscCall(MatGetRowUpperTriangular(A));
156: for (PetscInt r = A->rmap->rstart; r < A->rmap->rend; r++) {
157: PetscInt ncols;
158: const PetscInt *row;
159: const PetscScalar *vals;
161: PetscCall(MatGetRow(A, r, &ncols, &row, &vals));
162: PetscCall(MatSetValues(B, 1, &r, ncols, row, vals, INSERT_VALUES));
163: if (PetscDefined(USE_COMPLEX) && A->hermitian == PETSC_BOOL3_TRUE) {
164: PetscInt i;
165: for (i = 0; i < ncols; i++) PetscCall(MatSetValue(B, row[i], r, PetscConj(vals[i]), INSERT_VALUES));
166: } else {
167: PetscCall(MatSetValues(B, ncols, row, 1, &r, vals, INSERT_VALUES));
168: }
169: PetscCall(MatRestoreRow(A, r, &ncols, &row, &vals));
170: }
171: PetscCall(MatRestoreRowUpperTriangular(A));
172: PetscCall(MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY));
173: PetscCall(MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY));
175: if (reuse == MAT_INPLACE_MATRIX) {
176: PetscCall(MatHeaderReplace(A, &B));
177: } else {
178: *newmat = B;
179: }
180: PetscFunctionReturn(PETSC_SUCCESS);
181: }
183: static PetscErrorCode MatStoreValues_MPISBAIJ(Mat mat)
184: {
185: Mat_MPISBAIJ *aij = (Mat_MPISBAIJ *)mat->data;
187: PetscFunctionBegin;
188: PetscCall(MatStoreValues(aij->A));
189: PetscCall(MatStoreValues(aij->B));
190: PetscFunctionReturn(PETSC_SUCCESS);
191: }
193: static PetscErrorCode MatRetrieveValues_MPISBAIJ(Mat mat)
194: {
195: Mat_MPISBAIJ *aij = (Mat_MPISBAIJ *)mat->data;
197: PetscFunctionBegin;
198: PetscCall(MatRetrieveValues(aij->A));
199: PetscCall(MatRetrieveValues(aij->B));
200: PetscFunctionReturn(PETSC_SUCCESS);
201: }
203: #define MatSetValues_SeqSBAIJ_A_Private(row, col, value, addv, orow, ocol) \
204: do { \
205: brow = (row) / bs; \
206: rp = aj + ai[brow]; \
207: if (!A->structure_only) ap = aa + bs2 * ai[brow]; \
208: rmax = aimax[brow]; \
209: nrow = ailen[brow]; \
210: bcol = (col) / bs; \
211: ridx = (row) % bs; \
212: cidx = (col) % bs; \
213: low = 0; \
214: high = nrow; \
215: while (high - low > 3) { \
216: t = (low + high) / 2; \
217: if (rp[t] > bcol) high = t; \
218: else low = t; \
219: } \
220: for (_i = low; _i < high; _i++) { \
221: if (rp[_i] > bcol) break; \
222: if (rp[_i] == bcol) { \
223: if (A->structure_only) goto a_noinsert; \
224: bap = ap + bs2 * _i + bs * cidx + ridx; \
225: if (addv == ADD_VALUES) *bap += value; \
226: else *bap = value; \
227: goto a_noinsert; \
228: } \
229: } \
230: if (a->nonew == 1) goto a_noinsert; \
231: 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); \
232: if (A->structure_only) MatSeqXAIJReallocateAIJ_structure_only(A, a->mbs, bs2, nrow, brow, bcol, rmax, ai, aj, rp, aimax, a->nonew, MatScalar); \
233: else MatSeqXAIJReallocateAIJ(A, a->mbs, bs2, nrow, brow, bcol, rmax, aa, ai, aj, rp, ap, aimax, a->nonew, MatScalar); \
234: N = nrow++ - 1; \
235: /* shift up all the later entries in this row */ \
236: PetscCall(PetscArraymove(rp + _i + 1, rp + _i, N - _i + 1)); \
237: rp[_i] = bcol; \
238: if (!A->structure_only) { \
239: PetscCall(PetscArraymove(ap + bs2 * (_i + 1), ap + bs2 * _i, bs2 * (N - _i + 1))); \
240: PetscCall(PetscArrayzero(ap + bs2 * _i, bs2)); \
241: ap[bs2 * _i + bs * cidx + ridx] = value; \
242: } \
243: a_noinsert:; \
244: ailen[brow] = nrow; \
245: } while (0)
247: #define MatSetValues_SeqSBAIJ_B_Private(row, col, value, addv, orow, ocol) \
248: do { \
249: brow = (row) / bs; \
250: rp = bj + bi[brow]; \
251: if (!B->structure_only) ap = ba + bs2 * bi[brow]; \
252: rmax = bimax[brow]; \
253: nrow = bilen[brow]; \
254: bcol = (col) / bs; \
255: ridx = (row) % bs; \
256: cidx = (col) % bs; \
257: low = 0; \
258: high = nrow; \
259: while (high - low > 3) { \
260: t = (low + high) / 2; \
261: if (rp[t] > bcol) high = t; \
262: else low = t; \
263: } \
264: for (_i = low; _i < high; _i++) { \
265: if (rp[_i] > bcol) break; \
266: if (rp[_i] == bcol) { \
267: if (B->structure_only) goto b_noinsert; \
268: bap = ap + bs2 * _i + bs * cidx + ridx; \
269: if (addv == ADD_VALUES) *bap += value; \
270: else *bap = value; \
271: goto b_noinsert; \
272: } \
273: } \
274: if (b->nonew == 1) goto b_noinsert; \
275: 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); \
276: if (B->structure_only) MatSeqXAIJReallocateAIJ_structure_only(B, b->mbs, bs2, nrow, brow, bcol, rmax, bi, bj, rp, bimax, b->nonew, MatScalar); \
277: else MatSeqXAIJReallocateAIJ(B, b->mbs, bs2, nrow, brow, bcol, rmax, ba, bi, bj, rp, ap, bimax, b->nonew, MatScalar); \
278: N = nrow++ - 1; \
279: /* shift up all the later entries in this row */ \
280: PetscCall(PetscArraymove(rp + _i + 1, rp + _i, N - _i + 1)); \
281: rp[_i] = bcol; \
282: if (!B->structure_only) { \
283: PetscCall(PetscArraymove(ap + bs2 * (_i + 1), ap + bs2 * _i, bs2 * (N - _i + 1))); \
284: PetscCall(PetscArrayzero(ap + bs2 * _i, bs2)); \
285: ap[bs2 * _i + bs * cidx + ridx] = value; \
286: } \
287: b_noinsert:; \
288: bilen[brow] = nrow; \
289: } while (0)
291: /* Only add/insert a(i,j) with i<=j (blocks).
292: Any a(i,j) with i>j input by user is ignored or generates an error
293: */
294: static PetscErrorCode MatSetValues_MPISBAIJ(Mat mat, PetscInt m, const PetscInt im[], PetscInt n, const PetscInt in[], const PetscScalar v[], InsertMode addv)
295: {
296: Mat_MPISBAIJ *baij = (Mat_MPISBAIJ *)mat->data;
297: MatScalar value = 0.0;
298: PetscBool roworiented = baij->roworiented;
299: PetscInt i, j, row, col;
300: PetscInt rstart_orig = mat->rmap->rstart;
301: PetscInt rend_orig = mat->rmap->rend, cstart_orig = mat->cmap->rstart;
302: PetscInt cend_orig = mat->cmap->rend, bs = mat->rmap->bs;
304: /* Some Variables required in the macro */
305: Mat A = baij->A;
306: Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ *)A->data;
307: PetscInt *aimax = a->imax, *ai = a->i, *ailen = a->ilen, *aj = a->j;
308: MatScalar *aa = a->a;
310: Mat B = baij->B;
311: Mat_SeqBAIJ *b = (Mat_SeqBAIJ *)B->data;
312: PetscInt *bimax = b->imax, *bi = b->i, *bilen = b->ilen, *bj = b->j;
313: MatScalar *ba = b->a;
315: PetscInt *rp, ii, nrow, _i, rmax, N, brow, bcol;
316: PetscInt low, high, t, ridx, cidx, bs2 = a->bs2;
317: MatScalar *ap = NULL, *bap;
319: /* for stash */
320: PetscInt n_loc, *in_loc = NULL;
321: MatScalar *v_loc = NULL;
323: PetscFunctionBegin;
324: if (!baij->donotstash) {
325: if (n > baij->n_loc) {
326: PetscCall(PetscFree(baij->in_loc));
327: PetscCall(PetscFree(baij->v_loc));
328: PetscCall(PetscMalloc1(n, &baij->in_loc));
329: PetscCall(PetscMalloc1(n, &baij->v_loc));
331: baij->n_loc = n;
332: }
333: in_loc = baij->in_loc;
334: v_loc = baij->v_loc;
335: }
337: for (i = 0; i < m; i++) {
338: if (im[i] < 0) continue;
339: 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);
340: if (im[i] >= rstart_orig && im[i] < rend_orig) { /* this processor entry */
341: row = im[i] - rstart_orig; /* local row index */
342: for (j = 0; j < n; j++) {
343: if (im[i] / bs > in[j] / bs) {
344: 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)");
345: continue; /* ignore lower triangular blocks */
346: }
347: if (in[j] >= cstart_orig && in[j] < cend_orig) { /* diag entry (A) */
348: col = in[j] - cstart_orig; /* local col index */
349: brow = row / bs;
350: bcol = col / bs;
351: if (brow > bcol) continue; /* ignore lower triangular blocks of A */
352: if (!mat->structure_only) {
353: if (roworiented) value = v[i * n + j];
354: else value = v[i + j * m];
355: }
356: MatSetValues_SeqSBAIJ_A_Private(row, col, value, addv, im[i], in[j]);
357: } else if (in[j] < 0) {
358: continue;
359: } else {
360: 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);
361: /* off-diag entry (B) */
362: if (mat->was_assembled) {
363: if (!baij->colmap) PetscCall(MatCreateColmap_MPIBAIJ_Private(mat));
364: #if PetscDefined(USE_CTABLE)
365: PetscCall(PetscHMapIGetWithDefault(baij->colmap, in[j] / bs + 1, 0, &col));
366: col = col - 1;
367: #else
368: col = baij->colmap[in[j] / bs] - 1;
369: #endif
370: if (col < 0 && !((Mat_SeqSBAIJ *)baij->A->data)->nonew) {
371: PetscCall(MatDisAssemble_MPISBAIJ(mat));
372: col = in[j];
373: /* Reinitialize the variables required by MatSetValues_SeqBAIJ_B_Private() */
374: B = baij->B;
375: b = (Mat_SeqBAIJ *)B->data;
376: bimax = b->imax;
377: bi = b->i;
378: bilen = b->ilen;
379: bj = b->j;
380: ba = b->a;
381: } else col += in[j] % bs;
382: } else col = in[j];
383: if (!mat->structure_only) {
384: if (roworiented) value = v[i * n + j];
385: else value = v[i + j * m];
386: }
387: MatSetValues_SeqSBAIJ_B_Private(row, col, value, addv, im[i], in[j]);
388: /* PetscCall(MatSetValues_SeqBAIJ(baij->B,1,&row,1,&col,&value,addv)); */
389: }
390: }
391: } else { /* off processor entry */
392: 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]);
393: if (!baij->donotstash) {
394: mat->assembled = PETSC_FALSE;
395: n_loc = 0;
396: for (j = 0; j < n; j++) {
397: if (im[i] / bs > in[j] / bs) continue; /* ignore lower triangular blocks */
398: in_loc[n_loc] = in[j];
399: if (mat->structure_only) v_loc[n_loc] = 0;
400: else if (roworiented) v_loc[n_loc] = v[i * n + j];
401: else v_loc[n_loc] = v[j * m + i];
402: n_loc++;
403: }
404: PetscCall(MatStashValuesRow_Private(&mat->stash, im[i], n_loc, in_loc, v_loc, PETSC_FALSE));
405: }
406: }
407: }
408: PetscFunctionReturn(PETSC_SUCCESS);
409: }
411: static inline PetscErrorCode MatSetValuesBlocked_SeqSBAIJ_Inlined(Mat A, PetscInt row, PetscInt col, const PetscScalar v[], InsertMode is, PetscInt orow, PetscInt ocol)
412: {
413: Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ *)A->data;
414: PetscInt *rp, low, high, t, ii, jj, nrow, i, rmax, N;
415: PetscInt *imax = a->imax, *ai = a->i, *ailen = a->ilen;
416: PetscInt *aj = a->j, nonew = a->nonew, bs2 = a->bs2, bs = A->rmap->bs;
417: PetscBool roworiented = a->roworiented;
418: const PetscScalar *value = v;
419: MatScalar *ap, *aa = a->a, *bap;
421: PetscFunctionBegin;
422: if (col < row) {
423: 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)");
424: PetscFunctionReturn(PETSC_SUCCESS); /* ignore lower triangular block */
425: }
426: rp = aj + ai[row];
427: ap = PetscSafePointerPlusOffset(aa, bs2 * ai[row]);
428: rmax = imax[row];
429: nrow = ailen[row];
430: value = v;
431: low = 0;
432: high = nrow;
434: while (high - low > 7) {
435: t = (low + high) / 2;
436: if (rp[t] > col) high = t;
437: else low = t;
438: }
439: for (i = low; i < high; i++) {
440: if (rp[i] > col) break;
441: if (rp[i] == col) {
442: if (A->structure_only) goto noinsert2;
443: bap = ap + bs2 * i;
444: if (roworiented) {
445: if (is == ADD_VALUES) {
446: for (ii = 0; ii < bs; ii++) {
447: for (jj = ii; jj < bs2; jj += bs) bap[jj] += *value++;
448: }
449: } else {
450: for (ii = 0; ii < bs; ii++) {
451: for (jj = ii; jj < bs2; jj += bs) bap[jj] = *value++;
452: }
453: }
454: } else {
455: if (is == ADD_VALUES) {
456: for (ii = 0; ii < bs; ii++) {
457: for (jj = 0; jj < bs; jj++) *bap++ += *value++;
458: }
459: } else {
460: for (ii = 0; ii < bs; ii++) {
461: for (jj = 0; jj < bs; jj++) *bap++ = *value++;
462: }
463: }
464: }
465: goto noinsert2;
466: }
467: }
468: if (nonew == 1) goto noinsert2;
469: 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);
470: if (A->structure_only) MatSeqXAIJReallocateAIJ_structure_only(A, a->mbs, bs2, nrow, row, col, rmax, ai, aj, rp, imax, nonew, MatScalar);
471: else MatSeqXAIJReallocateAIJ(A, a->mbs, bs2, nrow, row, col, rmax, aa, ai, aj, rp, ap, imax, nonew, MatScalar);
472: N = nrow++ - 1;
473: high++;
474: /* shift up all the later entries in this row */
475: PetscCall(PetscArraymove(rp + i + 1, rp + i, N - i + 1));
476: rp[i] = col;
477: if (!A->structure_only) {
478: PetscCall(PetscArraymove(ap + bs2 * (i + 1), ap + bs2 * i, bs2 * (N - i + 1)));
479: bap = ap + bs2 * i;
480: if (roworiented) {
481: for (ii = 0; ii < bs; ii++) {
482: for (jj = ii; jj < bs2; jj += bs) bap[jj] = *value++;
483: }
484: } else {
485: for (ii = 0; ii < bs; ii++) {
486: for (jj = 0; jj < bs; jj++) *bap++ = *value++;
487: }
488: }
489: }
490: noinsert2:;
491: ailen[row] = nrow;
492: PetscFunctionReturn(PETSC_SUCCESS);
493: }
495: /*
496: This routine is exactly duplicated in mpibaij.c
497: */
498: static inline PetscErrorCode MatSetValuesBlocked_SeqBAIJ_Inlined(Mat A, PetscInt row, PetscInt col, const PetscScalar v[], InsertMode is, PetscInt orow, PetscInt ocol)
499: {
500: Mat_SeqBAIJ *a = (Mat_SeqBAIJ *)A->data;
501: PetscInt *rp, low, high, t, ii, jj, nrow, i, rmax, N;
502: PetscInt *imax = a->imax, *ai = a->i, *ailen = a->ilen;
503: PetscInt *aj = a->j, nonew = a->nonew, bs2 = a->bs2, bs = A->rmap->bs;
504: PetscBool roworiented = a->roworiented;
505: const PetscScalar *value = v;
506: MatScalar *ap, *aa = a->a, *bap;
508: PetscFunctionBegin;
509: rp = aj + ai[row];
510: ap = PetscSafePointerPlusOffset(aa, bs2 * ai[row]);
511: rmax = imax[row];
512: nrow = ailen[row];
513: low = 0;
514: high = nrow;
515: value = v;
516: while (high - low > 7) {
517: t = (low + high) / 2;
518: if (rp[t] > col) high = t;
519: else low = t;
520: }
521: for (i = low; i < high; i++) {
522: if (rp[i] > col) break;
523: if (rp[i] == col) {
524: if (A->structure_only) goto noinsert2;
525: bap = ap + bs2 * i;
526: if (roworiented) {
527: if (is == ADD_VALUES) {
528: for (ii = 0; ii < bs; ii++) {
529: for (jj = ii; jj < bs2; jj += bs) bap[jj] += *value++;
530: }
531: } else {
532: for (ii = 0; ii < bs; ii++) {
533: for (jj = ii; jj < bs2; jj += bs) bap[jj] = *value++;
534: }
535: }
536: } else {
537: if (is == ADD_VALUES) {
538: for (ii = 0; ii < bs; ii++, value += bs) {
539: for (jj = 0; jj < bs; jj++) bap[jj] += value[jj];
540: bap += bs;
541: }
542: } else {
543: for (ii = 0; ii < bs; ii++, value += bs) {
544: for (jj = 0; jj < bs; jj++) bap[jj] = value[jj];
545: bap += bs;
546: }
547: }
548: }
549: goto noinsert2;
550: }
551: }
552: if (nonew == 1) goto noinsert2;
553: 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);
554: if (A->structure_only) MatSeqXAIJReallocateAIJ_structure_only(A, a->mbs, bs2, nrow, row, col, rmax, ai, aj, rp, imax, nonew, MatScalar);
555: else MatSeqXAIJReallocateAIJ(A, a->mbs, bs2, nrow, row, col, rmax, aa, ai, aj, rp, ap, imax, nonew, MatScalar);
556: N = nrow++ - 1;
557: high++;
558: /* shift up all the later entries in this row */
559: PetscCall(PetscArraymove(rp + i + 1, rp + i, N - i + 1));
560: rp[i] = col;
561: if (!A->structure_only) {
562: PetscCall(PetscArraymove(ap + bs2 * (i + 1), ap + bs2 * i, bs2 * (N - i + 1)));
563: bap = ap + bs2 * i;
564: if (roworiented) {
565: for (ii = 0; ii < bs; ii++) {
566: for (jj = ii; jj < bs2; jj += bs) bap[jj] = *value++;
567: }
568: } else {
569: for (ii = 0; ii < bs; ii++) {
570: for (jj = 0; jj < bs; jj++) *bap++ = *value++;
571: }
572: }
573: }
574: noinsert2:;
575: ailen[row] = nrow;
576: PetscFunctionReturn(PETSC_SUCCESS);
577: }
579: /*
580: This routine could be optimized by removing the need for the block copy below and passing stride information
581: to the above inline routines; similarly in MatSetValuesBlocked_MPIBAIJ()
582: */
583: static PetscErrorCode MatSetValuesBlocked_MPISBAIJ(Mat mat, PetscInt m, const PetscInt im[], PetscInt n, const PetscInt in[], const MatScalar v[], InsertMode addv)
584: {
585: Mat_MPISBAIJ *baij = (Mat_MPISBAIJ *)mat->data;
586: const MatScalar *value;
587: MatScalar *barray = baij->barray;
588: PetscBool roworiented = baij->roworiented, ignore_ltriangular = ((Mat_SeqSBAIJ *)baij->A->data)->ignore_ltriangular;
589: PetscInt i, j, ii, jj, row, col, rstart = baij->rstartbs;
590: PetscInt rend = baij->rendbs, cstart = baij->cstartbs, stepval;
591: PetscInt cend = baij->cendbs, bs = mat->rmap->bs, bs2 = baij->bs2;
593: PetscFunctionBegin;
594: if (!mat->structure_only && !barray) {
595: PetscCall(PetscMalloc1(bs2, &barray));
596: baij->barray = barray;
597: }
599: if (roworiented) stepval = (n - 1) * bs;
600: else stepval = (m - 1) * bs;
601: for (i = 0; i < m; i++) {
602: if (im[i] < 0) continue;
603: 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);
604: if (im[i] >= rstart && im[i] < rend) {
605: row = im[i] - rstart;
606: for (j = 0; j < n; j++) {
607: if (in[j] < 0) continue;
608: if (im[i] > in[j]) {
609: 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)");
610: continue; /* ignore lower triangular blocks */
611: }
612: if (!mat->structure_only) {
613: /* If NumCol = 1 then a copy is not required */
614: if (roworiented && n == 1) {
615: barray = (MatScalar *)v + i * bs2;
616: } else if ((!roworiented) && (m == 1)) {
617: barray = (MatScalar *)v + j * bs2;
618: } else { /* Here a copy is required */
619: if (roworiented) {
620: value = v + i * (stepval + bs) * bs + j * bs;
621: } else {
622: value = v + j * (stepval + bs) * bs + i * bs;
623: }
624: for (ii = 0; ii < bs; ii++, value += stepval) {
625: for (jj = 0; jj < bs; jj++) *barray++ = *value++;
626: }
627: barray -= bs2;
628: }
629: }
631: if (in[j] >= cstart && in[j] < cend) {
632: col = in[j] - cstart;
633: PetscCall(MatSetValuesBlocked_SeqSBAIJ_Inlined(baij->A, row, col, barray, addv, im[i], in[j]));
634: } else if (in[j] < 0) {
635: continue;
636: } else {
637: 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);
638: if (mat->was_assembled) {
639: if (!baij->colmap) PetscCall(MatCreateColmap_MPIBAIJ_Private(mat));
641: #if PetscDefined(USE_CTABLE)
642: PetscCall(PetscHMapIGetWithDefault(baij->colmap, in[j] + 1, 0, &col));
643: col = col < 1 ? -1 : (col - 1) / bs;
644: #else
645: col = baij->colmap[in[j]] < 1 ? -1 : (baij->colmap[in[j]] - 1) / bs;
646: #endif
647: if (col < 0 && !((Mat_SeqBAIJ *)baij->A->data)->nonew) {
648: PetscCall(MatDisAssemble_MPISBAIJ(mat));
649: col = in[j];
650: }
651: } else col = in[j];
652: PetscCall(MatSetValuesBlocked_SeqBAIJ_Inlined(baij->B, row, col, barray, addv, im[i], in[j]));
653: }
654: }
655: } else {
656: 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]);
657: if (!baij->donotstash) {
658: if (roworiented) {
659: PetscCall(MatStashValuesRowBlocked_Private(&mat->bstash, im[i], n, in, v, m, n, i));
660: } else {
661: PetscCall(MatStashValuesColBlocked_Private(&mat->bstash, im[i], n, in, v, m, n, i));
662: }
663: }
664: }
665: }
666: PetscFunctionReturn(PETSC_SUCCESS);
667: }
669: static PetscErrorCode MatGetValues_MPISBAIJ(Mat mat, PetscInt m, const PetscInt idxm[], PetscInt n, const PetscInt idxn[], PetscScalar v[])
670: {
671: Mat_MPISBAIJ *baij = (Mat_MPISBAIJ *)mat->data;
672: PetscInt bs = mat->rmap->bs, i, j, bsrstart = mat->rmap->rstart, bsrend = mat->rmap->rend;
673: PetscInt bscstart = mat->cmap->rstart, bscend = mat->cmap->rend, row, col, data;
674: PetscBool roworiented = baij->roworiented;
675: PetscScalar *value;
677: PetscFunctionBegin;
678: for (i = 0; i < m; i++) {
679: if (idxm[i] < 0) continue; /* negative row */
680: 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);
681: PetscCheck(idxm[i] >= bsrstart && idxm[i] < bsrend, PETSC_COMM_SELF, PETSC_ERR_SUP, "Only local values currently supported");
682: row = idxm[i] - bsrstart;
683: for (j = 0; j < n; j++) {
684: if (idxn[j] < 0) continue; /* negative column */
685: 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);
686: value = roworiented ? &v[j + i * n] : &v[i + j * m];
687: if (idxn[j] >= bscstart && idxn[j] < bscend) {
688: col = idxn[j] - bscstart;
689: PetscCall(MatGetValues_SeqSBAIJ(baij->A, 1, &row, 1, &col, value));
690: } else {
691: if (!baij->colmap) PetscCall(MatCreateColmap_MPIBAIJ_Private(mat));
692: #if PetscDefined(USE_CTABLE)
693: PetscCall(PetscHMapIGetWithDefault(baij->colmap, idxn[j] / bs + 1, 0, &data));
694: data--;
695: #else
696: data = baij->colmap[idxn[j] / bs] - 1;
697: #endif
698: if (data < 0 || baij->garray[data / bs] != idxn[j] / bs) *value = 0.0;
699: else {
700: col = data + idxn[j] % bs;
701: PetscCall(MatGetValues_SeqBAIJ(baij->B, 1, &row, 1, &col, value));
702: }
703: }
704: }
705: }
706: PetscFunctionReturn(PETSC_SUCCESS);
707: }
709: static PetscErrorCode MatNorm_MPISBAIJ(Mat mat, NormType type, PetscReal *norm)
710: {
711: Mat_MPISBAIJ *baij = (Mat_MPISBAIJ *)mat->data;
712: PetscReal sum[2];
714: PetscFunctionBegin;
715: if (baij->size == 1) {
716: PetscCall(MatNorm(baij->A, type, norm));
717: } else {
718: if (type == NORM_FROBENIUS) {
719: PetscCall(MatNorm(baij->A, type, &sum[0]));
720: sum[0] *= sum[0];
721: PetscCall(MatNorm(baij->B, type, &sum[1]));
722: sum[1] *= sum[1];
723: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, sum, 2, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)mat)));
724: *norm = PetscSqrtReal(sum[0] + 2 * sum[1]);
725: } else if (type == NORM_INFINITY || type == NORM_1) { /* max row/column sum */
726: Mat_SeqSBAIJ *amat = (Mat_SeqSBAIJ *)baij->A->data;
727: Mat_SeqBAIJ *bmat = (Mat_SeqBAIJ *)baij->B->data;
728: PetscReal *rsum, vabs;
729: PetscInt *jj, *garray = baij->garray, rstart = baij->rstartbs, nz;
730: PetscInt brow, bcol, col, bs = baij->A->rmap->bs, row, grow, gcol, mbs = amat->mbs;
731: MatScalar *v;
733: PetscCall(PetscCalloc1(mat->cmap->N, &rsum));
734: /* Amat */
735: v = amat->a;
736: jj = amat->j;
737: for (brow = 0; brow < mbs; brow++) {
738: grow = bs * (rstart + brow);
739: nz = amat->i[brow + 1] - amat->i[brow];
740: for (bcol = 0; bcol < nz; bcol++) {
741: gcol = bs * (rstart + *jj);
742: jj++;
743: for (col = 0; col < bs; col++) {
744: for (row = 0; row < bs; row++) {
745: vabs = PetscAbsScalar(*v);
746: v++;
747: rsum[gcol + col] += vabs;
748: /* non-diagonal block */
749: if (bcol > 0 && vabs > 0.0) rsum[grow + row] += vabs;
750: }
751: }
752: }
753: PetscCall(PetscLogFlops(nz * bs * bs));
754: }
755: /* Bmat */
756: v = bmat->a;
757: jj = bmat->j;
758: for (brow = 0; brow < mbs; brow++) {
759: grow = bs * (rstart + brow);
760: nz = bmat->i[brow + 1] - bmat->i[brow];
761: for (bcol = 0; bcol < nz; bcol++) {
762: gcol = bs * garray[*jj];
763: jj++;
764: for (col = 0; col < bs; col++) {
765: for (row = 0; row < bs; row++) {
766: vabs = PetscAbsScalar(*v);
767: v++;
768: rsum[gcol + col] += vabs;
769: rsum[grow + row] += vabs;
770: }
771: }
772: }
773: PetscCall(PetscLogFlops(nz * bs * bs));
774: }
775: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, rsum, mat->cmap->N, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)mat)));
776: *norm = 0.0;
777: for (col = 0; col < mat->cmap->N; col++) {
778: if (rsum[col] > *norm) *norm = rsum[col];
779: }
780: PetscCall(PetscFree(rsum));
781: } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "No support for this norm yet");
782: }
783: PetscFunctionReturn(PETSC_SUCCESS);
784: }
786: static PetscErrorCode MatAssemblyBegin_MPISBAIJ(Mat mat, MatAssemblyType mode)
787: {
788: Mat_MPISBAIJ *baij = (Mat_MPISBAIJ *)mat->data;
789: PetscInt nstash, reallocs;
791: PetscFunctionBegin;
792: if (baij->donotstash || mat->nooffprocentries) PetscFunctionReturn(PETSC_SUCCESS);
794: PetscCall(MatStashScatterBegin_Private(mat, &mat->stash, mat->rmap->range));
795: PetscCall(MatStashScatterBegin_Private(mat, &mat->bstash, baij->rangebs));
796: PetscCall(MatStashGetInfo_Private(&mat->stash, &nstash, &reallocs));
797: PetscCall(PetscInfo(mat, "Stash has %" PetscInt_FMT " entries, uses %" PetscInt_FMT " mallocs.\n", nstash, reallocs));
798: PetscCall(MatStashGetInfo_Private(&mat->bstash, &nstash, &reallocs));
799: PetscCall(PetscInfo(mat, "Block-Stash has %" PetscInt_FMT " entries, uses %" PetscInt_FMT " mallocs.\n", nstash, reallocs));
800: PetscFunctionReturn(PETSC_SUCCESS);
801: }
803: static PetscErrorCode MatAssemblyEnd_MPISBAIJ(Mat mat, MatAssemblyType mode)
804: {
805: Mat_MPISBAIJ *baij = (Mat_MPISBAIJ *)mat->data;
806: Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ *)baij->A->data;
807: PetscInt i, j, rstart, ncols, flg, bs2 = baij->bs2;
808: PetscInt *row, *col;
809: PetscBool all_assembled;
810: PetscMPIInt n;
811: PetscBool r1, r2, r3;
812: MatScalar *val;
814: /* do not use 'b=(Mat_SeqBAIJ*)baij->B->data' as B can be reset in disassembly */
815: PetscFunctionBegin;
816: if (!baij->donotstash && !mat->nooffprocentries) {
817: while (1) {
818: PetscCall(MatStashScatterGetMesg_Private(&mat->stash, &n, &row, &col, &val, &flg));
819: if (!flg) break;
821: for (i = 0; i < n;) {
822: /* Now identify the consecutive vals belonging to the same row */
823: for (j = i, rstart = row[j]; j < n; j++) {
824: if (row[j] != rstart) break;
825: }
826: if (j < n) ncols = j - i;
827: else ncols = n - i;
828: /* Now assemble all these values with a single function call */
829: PetscCall(MatSetValues_MPISBAIJ(mat, 1, row + i, ncols, col + i, val + i, mat->insertmode));
830: i = j;
831: }
832: }
833: PetscCall(MatStashScatterEnd_Private(&mat->stash));
834: /* Now process the block-stash. Since the values are stashed column-oriented,
835: set the row-oriented flag to column-oriented, and after MatSetValues()
836: restore the original flags */
837: r1 = baij->roworiented;
838: r2 = a->roworiented;
839: r3 = ((Mat_SeqBAIJ *)baij->B->data)->roworiented;
841: baij->roworiented = PETSC_FALSE;
842: a->roworiented = PETSC_FALSE;
844: ((Mat_SeqBAIJ *)baij->B->data)->roworiented = PETSC_FALSE; /* b->roworiented */
845: while (1) {
846: PetscCall(MatStashScatterGetMesg_Private(&mat->bstash, &n, &row, &col, &val, &flg));
847: if (!flg) break;
849: for (i = 0; i < n;) {
850: /* Now identify the consecutive vals belonging to the same row */
851: for (j = i, rstart = row[j]; j < n; j++) {
852: if (row[j] != rstart) break;
853: }
854: if (j < n) ncols = j - i;
855: else ncols = n - i;
856: PetscCall(MatSetValuesBlocked_MPISBAIJ(mat, 1, row + i, ncols, col + i, val + i * bs2, mat->insertmode));
857: i = j;
858: }
859: }
860: PetscCall(MatStashScatterEnd_Private(&mat->bstash));
862: baij->roworiented = r1;
863: a->roworiented = r2;
865: ((Mat_SeqBAIJ *)baij->B->data)->roworiented = r3; /* b->roworiented */
866: }
868: PetscCall(MatAssemblyBegin(baij->A, mode));
869: PetscCall(MatAssemblyEnd(baij->A, mode));
871: /* determine if any process has disassembled, if so we must
872: also disassemble ourselves, in order that we may reassemble. */
873: /*
874: if nonzero structure of submatrix B cannot change then we know that
875: no process disassembled thus we can skip this stuff
876: */
877: if (!((Mat_SeqBAIJ *)baij->B->data)->nonew) {
878: PetscCallMPI(MPIU_Allreduce(&mat->was_assembled, &all_assembled, 1, MPI_C_BOOL, MPI_LAND, PetscObjectComm((PetscObject)mat)));
879: if (mat->was_assembled && !all_assembled) PetscCall(MatDisAssemble_MPISBAIJ(mat));
880: }
882: if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) PetscCall(MatSetUpMultiply_MPISBAIJ(mat)); /* setup Mvctx and sMvctx */
883: PetscCall(MatAssemblyBegin(baij->B, mode));
884: PetscCall(MatAssemblyEnd(baij->B, mode));
886: PetscCall(PetscFree2(baij->rowvalues, baij->rowindices));
888: baij->rowvalues = NULL;
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: PetscCall(MatSetOption(X, MAT_GETROW_UPPERTRIANGULAR, PETSC_TRUE));
1592: PetscCall(MatAXPY_Basic(Y, a, X, str));
1593: PetscCall(MatSetOption(X, MAT_GETROW_UPPERTRIANGULAR, PETSC_FALSE));
1594: } else {
1595: Mat B;
1596: PetscInt *nnz_d, *nnz_o, bs = Y->rmap->bs;
1597: PetscCheck(bs == X->rmap->bs, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Matrices must have same block size");
1598: PetscCall(MatGetRowUpperTriangular(X));
1599: PetscCall(MatGetRowUpperTriangular(Y));
1600: PetscCall(PetscMalloc1(yy->A->rmap->N, &nnz_d));
1601: PetscCall(PetscMalloc1(yy->B->rmap->N, &nnz_o));
1602: PetscCall(MatCreate(PetscObjectComm((PetscObject)Y), &B));
1603: PetscCall(PetscObjectSetName((PetscObject)B, ((PetscObject)Y)->name));
1604: PetscCall(MatSetSizes(B, Y->rmap->n, Y->cmap->n, Y->rmap->N, Y->cmap->N));
1605: PetscCall(MatSetBlockSizesFromMats(B, Y, Y));
1606: PetscCall(MatSetType(B, MATMPISBAIJ));
1607: PetscCall(MatAXPYGetPreallocation_SeqSBAIJ(yy->A, xx->A, nnz_d));
1608: PetscCall(MatAXPYGetPreallocation_MPIBAIJ(yy->B, yy->garray, xx->B, xx->garray, nnz_o));
1609: PetscCall(MatMPISBAIJSetPreallocation(B, bs, 0, nnz_d, 0, nnz_o));
1610: PetscCall(MatAXPY_BasicWithPreallocation(B, Y, a, X, str));
1611: PetscCall(MatHeaderMerge(Y, &B));
1612: PetscCall(PetscFree(nnz_d));
1613: PetscCall(PetscFree(nnz_o));
1614: PetscCall(MatRestoreRowUpperTriangular(X));
1615: PetscCall(MatRestoreRowUpperTriangular(Y));
1616: }
1617: PetscFunctionReturn(PETSC_SUCCESS);
1618: }
1620: static PetscErrorCode MatCreateSubMatrices_MPISBAIJ(Mat A, PetscInt n, const IS irow[], const IS icol[], MatReuse scall, Mat *B[])
1621: {
1622: PetscBool action[3] = {PETSC_FALSE, PETSC_FALSE, PETSC_FALSE}; /* {convert to MATBAIJ, sort and permute with MPISBAIJ, all columns request} */
1624: PetscFunctionBegin;
1625: for (PetscInt i = 0; i < n; i++) {
1626: if (action[0] == PETSC_FALSE && irow[i] != icol[i]) {
1627: PetscInt ncol;
1629: /* MatCreateSubMatrices_MPIBAIJ() preserves the MATSBAIJ format for sorted row IS with all columns */
1630: PetscCall(ISGetLocalSize(icol[i], &ncol));
1631: if (ncol == A->cmap->N) PetscCall(ISIdentity(icol[i], action));
1632: if (action[0]) {
1633: action[2] = PETSC_TRUE;
1634: if (action[1] == PETSC_FALSE) {
1635: PetscCall(ISSorted(irow[i], action + 1));
1636: action[0] = (PetscBool)!action[1];
1637: action[1] = PETSC_FALSE;
1638: }
1639: } else {
1640: PetscCall(ISEqual(irow[i], icol[i], action));
1641: action[0] = (PetscBool)!action[0];
1642: if (action[0] == PETSC_FALSE) action[1] = PETSC_TRUE;
1643: }
1644: }
1645: if (action[0] == PETSC_FALSE && action[1] == PETSC_FALSE && irow[i] == icol[i]) {
1646: PetscCall(ISSorted(irow[i], action + 1));
1647: action[1] = (PetscBool)!action[1];
1648: }
1649: }
1650: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, action, 3, MPI_C_BOOL, MPI_LOR, PetscObjectComm((PetscObject)A)));
1651: /* sorting cannot be mixed with the all-columns MATSBAIJ path */
1652: if (action[0] == PETSC_FALSE && action[1] == PETSC_TRUE && action[2] == PETSC_TRUE) action[0] = PETSC_TRUE;
1653: if (action[0] == PETSC_TRUE) {
1654: Mat Ageneral;
1656: /* different row and column sets need entries from both triangular parts of A */
1657: PetscCall(MatConvert(A, MATMPIBAIJ, MAT_INITIAL_MATRIX, &Ageneral));
1658: PetscCall(MatCreateSubMatrices_MPIBAIJ(Ageneral, n, irow, icol, scall, B));
1659: PetscCall(MatDestroy(&Ageneral));
1660: } else if (action[1] == PETSC_FALSE) PetscCall(MatCreateSubMatrices_MPIBAIJ(A, n, irow, icol, scall, B)); /* B[] are MATSBAIJ matrices */
1661: else {
1662: Mat *Bsorted;
1663: IS *isrow_sorted, *iscol_sorted, *isrow_iperm, *iscol_iperm;
1664: IS perm;
1666: PetscCall(PetscMalloc4(n, &isrow_sorted, n, &iscol_sorted, n, &isrow_iperm, n, &iscol_iperm));
1667: for (PetscInt i = 0; i < n; i++) {
1668: PetscCall(ISDuplicate(irow[i], isrow_sorted + i));
1669: PetscCall(ISSort(isrow_sorted[i]));
1670: PetscCall(ISSortPermutation(irow[i], PETSC_TRUE, &perm));
1671: PetscCall(ISInvertPermutation(perm, PETSC_DECIDE, isrow_iperm + i));
1672: PetscCall(ISDestroy(&perm));
1673: if (irow[i] == icol[i]) {
1674: iscol_sorted[i] = isrow_sorted[i];
1675: PetscCall(PetscObjectReference((PetscObject)iscol_sorted[i]));
1676: iscol_iperm[i] = isrow_iperm[i];
1677: PetscCall(PetscObjectReference((PetscObject)iscol_iperm[i]));
1678: } else {
1679: iscol_sorted[i] = isrow_sorted[i];
1680: PetscCall(PetscObjectReference((PetscObject)iscol_sorted[i]));
1681: PetscCall(ISSortPermutation(icol[i], PETSC_TRUE, &perm));
1682: PetscCall(ISInvertPermutation(perm, PETSC_DECIDE, iscol_iperm + i));
1683: PetscCall(ISDestroy(&perm));
1684: }
1685: }
1686: PetscCall(MatCreateSubMatrices_MPIBAIJ(A, n, isrow_sorted, iscol_sorted, MAT_INITIAL_MATRIX, &Bsorted)); /* Bsorted[] are MATSBAIJ matrices */
1687: for (PetscInt i = 0; i < n; i++) {
1688: Mat Bpermuted;
1689: PetscBool sameorder;
1691: PetscCall(ISEqualUnsorted(isrow_iperm[i], iscol_iperm[i], &sameorder));
1692: if (sameorder) PetscCall(MatPermute(Bsorted[i], isrow_iperm[i], iscol_iperm[i], &Bpermuted));
1693: else {
1694: Mat Bgeneral;
1696: PetscCall(MatConvert(Bsorted[i], MATSEQBAIJ, MAT_INITIAL_MATRIX, &Bgeneral));
1697: PetscCall(MatPermute(Bgeneral, isrow_iperm[i], iscol_iperm[i], &Bpermuted));
1698: PetscCall(MatDestroy(&Bgeneral));
1699: }
1700: PetscCall(MatDestroy(Bsorted + i));
1701: Bsorted[i] = Bpermuted;
1702: }
1703: if (scall == MAT_REUSE_MATRIX) {
1704: for (PetscInt i = 0; i < n; i++) PetscCall(MatCopy(Bsorted[i], (*B)[i], DIFFERENT_NONZERO_PATTERN));
1705: PetscCall(MatDestroySubMatrices(n, &Bsorted));
1706: } else *B = Bsorted;
1707: for (PetscInt i = 0; i < n; i++) {
1708: PetscCall(ISDestroy(isrow_sorted + i));
1709: PetscCall(ISDestroy(iscol_sorted + i));
1710: PetscCall(ISDestroy(isrow_iperm + i));
1711: PetscCall(ISDestroy(iscol_iperm + i));
1712: }
1713: PetscCall(PetscFree4(isrow_sorted, iscol_sorted, isrow_iperm, iscol_iperm));
1714: }
1715: PetscFunctionReturn(PETSC_SUCCESS);
1716: }
1718: static PetscErrorCode MatShift_MPISBAIJ(Mat Y, PetscScalar a)
1719: {
1720: Mat_MPISBAIJ *maij = (Mat_MPISBAIJ *)Y->data;
1721: Mat_SeqSBAIJ *aij = (Mat_SeqSBAIJ *)maij->A->data;
1723: PetscFunctionBegin;
1724: if (!Y->preallocated) PetscCall(MatMPISBAIJSetPreallocation(Y, Y->rmap->bs, 1, NULL, 0, NULL));
1725: else if (!aij->nz) {
1726: const PetscInt nonew = aij->nonew;
1728: PetscCall(MatSeqSBAIJSetPreallocation(maij->A, Y->rmap->bs, 1, NULL));
1729: aij->nonew = nonew;
1730: }
1731: PetscCall(MatShift_Basic(Y, a));
1732: PetscFunctionReturn(PETSC_SUCCESS);
1733: }
1735: static PetscErrorCode MatZeroRowsColumns_MPISBAIJ(Mat A, PetscInt N, const PetscInt rows[], PetscScalar diag, Vec x, Vec b)
1736: {
1737: Mat_MPISBAIJ *l = (Mat_MPISBAIJ *)A->data;
1738: PetscMPIInt n, p = 0;
1739: PetscInt i, j, k, r, len = 0, row, col, count;
1740: PetscInt *lrows, *owners = A->rmap->range;
1741: PetscSFNode *rrows;
1742: PetscSF sf;
1743: const PetscScalar *xx;
1744: PetscScalar *bb, *mask;
1745: Vec xmask, lmask, lvec_contrib = NULL;
1746: Mat_SeqBAIJ *baij = (Mat_SeqBAIJ *)l->B->data;
1747: PetscInt bs = A->rmap->bs, bs2 = baij->bs2;
1748: PetscScalar *aa;
1750: PetscFunctionBegin;
1751: PetscCall(PetscMPIIntCast(A->rmap->n, &n));
1752: /* create PetscSF where leaves are input rows and roots are owned rows */
1753: PetscCall(PetscMalloc1(n, &lrows));
1754: for (r = 0; r < n; ++r) lrows[r] = -1;
1755: PetscCall(PetscMalloc1(N, &rrows));
1756: for (r = 0; r < N; ++r) {
1757: const PetscInt idx = rows[r];
1758: 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);
1759: if (idx < owners[p] || owners[p + 1] <= idx) { /* short-circuit the search if the last p owns this row too */
1760: PetscCall(PetscLayoutFindOwner(A->rmap, idx, &p));
1761: }
1762: rrows[r].rank = p;
1763: rrows[r].index = rows[r] - owners[p];
1764: }
1765: PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)A), &sf));
1766: PetscCall(PetscSFSetGraph(sf, n, N, NULL, PETSC_OWN_POINTER, rrows, PETSC_OWN_POINTER));
1767: /* collect flags for rows to be zeroed */
1768: PetscCall(PetscSFReduceBegin(sf, MPIU_INT, (PetscInt *)rows, lrows, MPI_LOR));
1769: PetscCall(PetscSFReduceEnd(sf, MPIU_INT, (PetscInt *)rows, lrows, MPI_LOR));
1770: PetscCall(PetscSFDestroy(&sf));
1771: /* compress and put in row numbers */
1772: for (r = 0; r < n; ++r) {
1773: if (lrows[r] >= 0) lrows[len++] = r;
1774: }
1775: /* zero diagonal part of matrix */
1776: PetscCall(MatZeroRowsColumns(l->A, len, lrows, diag, x, b));
1777: /* handle off-diagonal part of matrix */
1778: PetscCall(MatCreateVecs(A, &xmask, NULL));
1779: PetscCall(VecDuplicate(l->lvec, &lmask));
1780: PetscCall(VecGetArray(xmask, &bb));
1781: for (i = 0; i < len; i++) bb[lrows[i]] = 1;
1782: PetscCall(VecRestoreArray(xmask, &bb));
1783: PetscCall(VecScatterBegin(l->Mvctx, xmask, lmask, ADD_VALUES, SCATTER_FORWARD));
1784: PetscCall(VecScatterEnd(l->Mvctx, xmask, lmask, ADD_VALUES, SCATTER_FORWARD));
1785: PetscCall(VecDestroy(&xmask));
1786: if (x) {
1787: PetscCall(VecScatterBegin(l->Mvctx, x, l->lvec, INSERT_VALUES, SCATTER_FORWARD));
1788: PetscCall(VecScatterEnd(l->Mvctx, x, l->lvec, INSERT_VALUES, SCATTER_FORWARD));
1789: PetscCall(VecGetArrayRead(l->lvec, &xx));
1790: PetscCall(VecGetArray(b, &bb));
1791: }
1792: PetscCall(VecGetArray(lmask, &mask));
1793: /* MPISBAIJ stores only the upper off-diagonal in l->B; for each zeroed local row r and
1794: non-zeroed off-process column c in that row, accumulate -A[r,c] * x[r] into lvec_contrib.
1795: A SCATTER_REVERSE below sends these contributions to b[c] on the owning (higher-rank)
1796: process, the missing symmetric lower-triangular update. We skip entries where c is
1797: also a zeroed row (mask[col] != 0) since b[c] = diag * x[c] is handled separately. */
1798: if (x) {
1799: const PetscScalar *x_vals;
1800: PetscScalar *c_vals;
1802: PetscCall(VecDuplicate(l->lvec, &lvec_contrib));
1803: PetscCall(VecGetArray(lvec_contrib, &c_vals));
1804: PetscCall(VecGetArrayRead(x, &x_vals));
1805: /* Only accumulate b[c] -= A[r,c] * x[r] when off-process col c is not also a zeroed row
1806: (mask[c] non-zero means col c is zeroed, so b[c] = diag * x[c] is already set).
1807: This mirrors the MatSeqSBAIJ pattern: if (zeroed[r] && !zeroed[c]) bb[c] -= A[r,c] * x[r].
1808: c_vals is indexed by the local B column index. */
1809: for (i = 0; i < len; ++i) {
1810: row = lrows[i];
1811: for (j = baij->i[row / bs]; j < baij->i[row / bs + 1]; ++j) {
1812: for (k = 0; k < bs; ++k) {
1813: col = baij->j[j] * bs + k;
1814: if (!PetscAbsScalar(mask[col])) {
1815: aa = baij->a + j * bs2 + (row % bs) + bs * k;
1816: c_vals[col] -= aa[0] * x_vals[row];
1817: }
1818: }
1819: }
1820: }
1821: PetscCall(VecRestoreArrayRead(x, &x_vals));
1822: PetscCall(VecRestoreArray(lvec_contrib, &c_vals));
1823: }
1824: /* remove zeroed rows of off-diagonal matrix */
1825: for (i = 0; i < len; ++i) {
1826: row = lrows[i];
1827: count = (baij->i[row / bs + 1] - baij->i[row / bs]) * bs;
1828: aa = PetscSafePointerPlusOffset(baij->a, baij->i[row / bs] * bs2 + (row % bs));
1829: for (k = 0; k < count; ++k) {
1830: aa[0] = 0.0;
1831: aa += bs;
1832: }
1833: }
1834: /* loop over all elements of off process part of matrix zeroing removed columns */
1835: for (i = 0; i < l->B->rmap->N; ++i) {
1836: row = i / bs;
1837: for (j = baij->i[row]; j < baij->i[row + 1]; ++j) {
1838: for (k = 0; k < bs; ++k) {
1839: col = bs * baij->j[j] + k;
1840: if (PetscAbsScalar(mask[col])) {
1841: aa = baij->a + j * bs2 + (i % bs) + bs * k;
1842: if (x) bb[i] -= aa[0] * xx[col];
1843: aa[0] = 0.0;
1844: }
1845: }
1846: }
1847: }
1848: if (x) {
1849: PetscCall(VecRestoreArray(b, &bb));
1850: PetscCall(VecRestoreArrayRead(l->lvec, &xx));
1851: /* scatter the accumulated contributions to b[c] on higher-rank processes owning column c */
1852: PetscCall(VecScatterBegin(l->Mvctx, lvec_contrib, b, ADD_VALUES, SCATTER_REVERSE));
1853: PetscCall(VecScatterEnd(l->Mvctx, lvec_contrib, b, ADD_VALUES, SCATTER_REVERSE));
1854: PetscCall(VecDestroy(&lvec_contrib));
1855: }
1856: PetscCall(VecRestoreArray(lmask, &mask));
1857: PetscCall(VecDestroy(&lmask));
1858: PetscCall(PetscFree(lrows));
1860: /* only change matrix nonzero state if pattern was allowed to be changed */
1861: if (!((Mat_SeqSBAIJ *)l->A->data)->nonew) {
1862: A->nonzerostate = l->A->nonzerostate + l->B->nonzerostate;
1863: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &A->nonzerostate, 1, MPIU_INT64, MPI_SUM, PetscObjectComm((PetscObject)A)));
1864: }
1865: PetscFunctionReturn(PETSC_SUCCESS);
1866: }
1868: static PetscErrorCode MatGetDiagonalBlock_MPISBAIJ(Mat A, Mat *a)
1869: {
1870: PetscFunctionBegin;
1871: *a = ((Mat_MPISBAIJ *)A->data)->A;
1872: PetscFunctionReturn(PETSC_SUCCESS);
1873: }
1875: static PetscErrorCode MatEliminateZeros_MPISBAIJ(Mat A, PetscBool keep)
1876: {
1877: Mat_MPISBAIJ *a = (Mat_MPISBAIJ *)A->data;
1879: PetscFunctionBegin;
1880: PetscCall(MatEliminateZeros_SeqSBAIJ(a->A, keep)); // possibly keep zero diagonal coefficients
1881: PetscCall(MatEliminateZeros_SeqBAIJ(a->B, PETSC_FALSE)); // never keep zero diagonal coefficients
1882: PetscFunctionReturn(PETSC_SUCCESS);
1883: }
1885: static PetscErrorCode MatLoad_MPISBAIJ(Mat, PetscViewer);
1886: static PetscErrorCode MatGetRowMaxAbs_MPISBAIJ(Mat, Vec, PetscInt[]);
1887: static PetscErrorCode MatSOR_MPISBAIJ(Mat, Vec, PetscReal, MatSORType, PetscReal, PetscInt, PetscInt, Vec);
1889: static struct _MatOps MatOps_Values = {MatSetValues_MPISBAIJ,
1890: MatGetRow_MPISBAIJ,
1891: MatRestoreRow_MPISBAIJ,
1892: MatMult_MPISBAIJ,
1893: /* 4*/ MatMultAdd_MPISBAIJ,
1894: MatMult_MPISBAIJ, /* transpose versions are same as non-transpose */
1895: MatMultAdd_MPISBAIJ,
1896: NULL,
1897: NULL,
1898: NULL,
1899: /* 10*/ NULL,
1900: NULL,
1901: NULL,
1902: MatSOR_MPISBAIJ,
1903: MatTranspose_MPISBAIJ,
1904: /* 15*/ MatGetInfo_MPISBAIJ,
1905: MatEqual_MPISBAIJ,
1906: MatGetDiagonal_MPISBAIJ,
1907: MatDiagonalScale_MPISBAIJ,
1908: MatNorm_MPISBAIJ,
1909: /* 20*/ MatAssemblyBegin_MPISBAIJ,
1910: MatAssemblyEnd_MPISBAIJ,
1911: MatSetOption_MPISBAIJ,
1912: MatZeroEntries_MPISBAIJ,
1913: /* 24*/ NULL,
1914: NULL,
1915: NULL,
1916: NULL,
1917: NULL,
1918: /* 29*/ MatSetUp_MPI_Hash,
1919: NULL,
1920: NULL,
1921: MatGetDiagonalBlock_MPISBAIJ,
1922: NULL,
1923: /* 34*/ MatDuplicate_MPISBAIJ,
1924: NULL,
1925: NULL,
1926: NULL,
1927: NULL,
1928: /* 39*/ MatAXPY_MPISBAIJ,
1929: MatCreateSubMatrices_MPISBAIJ,
1930: MatIncreaseOverlap_MPISBAIJ,
1931: MatGetValues_MPISBAIJ,
1932: MatCopy_MPISBAIJ,
1933: /* 44*/ NULL,
1934: MatScale_MPISBAIJ,
1935: MatShift_MPISBAIJ,
1936: NULL,
1937: MatZeroRowsColumns_MPISBAIJ,
1938: /* 49*/ NULL,
1939: NULL,
1940: NULL,
1941: NULL,
1942: NULL,
1943: /* 54*/ NULL,
1944: NULL,
1945: MatSetUnfactored_MPISBAIJ,
1946: NULL,
1947: MatSetValuesBlocked_MPISBAIJ,
1948: /* 59*/ MatCreateSubMatrix_MPISBAIJ,
1949: NULL,
1950: NULL,
1951: NULL,
1952: NULL,
1953: /* 64*/ NULL,
1954: NULL,
1955: NULL,
1956: NULL,
1957: MatGetRowMaxAbs_MPISBAIJ,
1958: /* 69*/ NULL,
1959: MatConvert_MPISBAIJ_Basic,
1960: NULL,
1961: NULL,
1962: NULL,
1963: NULL,
1964: NULL,
1965: NULL,
1966: NULL,
1967: MatLoad_MPISBAIJ,
1968: /* 79*/ NULL,
1969: NULL,
1970: NULL,
1971: NULL,
1972: NULL,
1973: /* 84*/ NULL,
1974: NULL,
1975: NULL,
1976: NULL,
1977: NULL,
1978: /* 89*/ NULL,
1979: NULL,
1980: NULL,
1981: NULL,
1982: MatConjugate_MPISBAIJ,
1983: /* 94*/ NULL,
1984: NULL,
1985: MatRealPart_MPISBAIJ,
1986: MatImaginaryPart_MPISBAIJ,
1987: MatGetRowUpperTriangular_MPISBAIJ,
1988: /* 99*/ MatRestoreRowUpperTriangular_MPISBAIJ,
1989: NULL,
1990: NULL,
1991: NULL,
1992: NULL,
1993: /*104*/ NULL,
1994: NULL,
1995: NULL,
1996: NULL,
1997: NULL,
1998: /*109*/ NULL,
1999: NULL,
2000: NULL,
2001: NULL,
2002: NULL,
2003: /*114*/ NULL,
2004: NULL,
2005: NULL,
2006: NULL,
2007: NULL,
2008: /*119*/ NULL,
2009: NULL,
2010: NULL,
2011: NULL,
2012: NULL,
2013: /*124*/ NULL,
2014: MatSetBlockSizes_Default,
2015: NULL,
2016: NULL,
2017: NULL,
2018: /*129*/ MatCreateMPIMatConcatenateSeqMat_MPISBAIJ,
2019: NULL,
2020: NULL,
2021: NULL,
2022: NULL,
2023: /*134*/ NULL,
2024: MatEliminateZeros_MPISBAIJ,
2025: NULL,
2026: NULL,
2027: NULL,
2028: /*139*/ NULL,
2029: MatCopyHashToXAIJ_MPI_Hash,
2030: NULL,
2031: NULL,
2032: NULL,
2033: /*144*/ NULL,
2034: NULL,
2035: NULL,
2036: NULL};
2038: static PetscErrorCode MatMPISBAIJSetPreallocation_MPISBAIJ(Mat B, PetscInt bs, PetscInt d_nz, const PetscInt *d_nnz, PetscInt o_nz, const PetscInt *o_nnz)
2039: {
2040: Mat_MPISBAIJ *b = (Mat_MPISBAIJ *)B->data;
2041: PetscInt i, mbs, Mbs;
2042: PetscMPIInt size;
2044: PetscFunctionBegin;
2045: if (B->hash_active) {
2046: B->ops[0] = b->cops;
2047: B->hash_active = PETSC_FALSE;
2048: }
2049: if (!B->preallocated) PetscCall(MatStashCreate_Private(PetscObjectComm((PetscObject)B), bs, &B->bstash));
2050: PetscCall(MatSetBlockSize(B, bs));
2051: PetscCall(PetscLayoutSetUp(B->rmap));
2052: PetscCall(PetscLayoutSetUp(B->cmap));
2053: PetscCall(PetscLayoutGetBlockSize(B->rmap, &bs));
2054: 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);
2055: 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);
2057: mbs = B->rmap->n / bs;
2058: Mbs = B->rmap->N / bs;
2059: 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);
2061: B->rmap->bs = bs;
2062: b->bs2 = bs * bs;
2063: b->mbs = mbs;
2064: b->Mbs = Mbs;
2065: b->nbs = B->cmap->n / bs;
2066: b->Nbs = B->cmap->N / bs;
2068: for (i = 0; i <= b->size; i++) b->rangebs[i] = B->rmap->range[i] / bs;
2069: b->rstartbs = B->rmap->rstart / bs;
2070: b->rendbs = B->rmap->rend / bs;
2072: b->cstartbs = B->cmap->rstart / bs;
2073: b->cendbs = B->cmap->rend / bs;
2075: #if PetscDefined(USE_CTABLE)
2076: PetscCall(PetscHMapIDestroy(&b->colmap));
2077: #else
2078: PetscCall(PetscFree(b->colmap));
2079: #endif
2080: PetscCall(PetscFree(b->garray));
2081: PetscCall(VecDestroy(&b->lvec));
2082: PetscCall(VecScatterDestroy(&b->Mvctx));
2083: PetscCall(VecDestroy(&b->slvec0));
2084: PetscCall(VecDestroy(&b->slvec0b));
2085: PetscCall(VecDestroy(&b->slvec1));
2086: PetscCall(VecDestroy(&b->slvec1a));
2087: PetscCall(VecDestroy(&b->slvec1b));
2088: PetscCall(VecScatterDestroy(&b->sMvctx));
2090: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)B), &size));
2092: MatSeqXAIJGetOptions_Private(b->B);
2093: PetscCall(MatDestroy(&b->B));
2094: PetscCall(MatCreate(PETSC_COMM_SELF, &b->B));
2095: PetscCall(MatSetSizes(b->B, B->rmap->n, size > 1 ? B->cmap->N : 0, B->rmap->n, size > 1 ? B->cmap->N : 0));
2096: PetscCall(MatSetType(b->B, MATSEQBAIJ));
2097: MatSeqXAIJRestoreOptions_Private(b->B);
2098: PetscCall(MatSetOption(b->B, MAT_STRUCTURE_ONLY, B->structure_only));
2100: MatSeqSBAIJGetOptions_Private(b->A);
2101: PetscCall(MatDestroy(&b->A));
2102: PetscCall(MatCreate(PETSC_COMM_SELF, &b->A));
2103: PetscCall(MatSetSizes(b->A, B->rmap->n, B->cmap->n, B->rmap->n, B->cmap->n));
2104: PetscCall(MatSetType(b->A, MATSEQSBAIJ));
2105: MatSeqSBAIJRestoreOptions_Private(b->A);
2106: PetscCall(MatSetOption(b->A, MAT_STRUCTURE_ONLY, B->structure_only));
2108: PetscCall(MatSeqSBAIJSetPreallocation(b->A, bs, d_nz, d_nnz));
2109: PetscCall(MatSeqBAIJSetPreallocation(b->B, bs, o_nz, o_nnz));
2110: B->preallocated = PETSC_TRUE;
2111: B->was_assembled = PETSC_FALSE;
2112: B->assembled = PETSC_FALSE;
2113: PetscFunctionReturn(PETSC_SUCCESS);
2114: }
2116: static PetscErrorCode MatMPISBAIJSetPreallocationCSR_MPISBAIJ(Mat B, PetscInt bs, const PetscInt ii[], const PetscInt jj[], const PetscScalar V[])
2117: {
2118: PetscInt m, rstart, cend;
2119: PetscInt i, j, d, nz, bd, nz_max = 0, *d_nnz = NULL, *o_nnz = NULL;
2120: const PetscInt *JJ = NULL;
2121: PetscScalar *values = NULL;
2122: PetscBool roworiented = ((Mat_MPISBAIJ *)B->data)->roworiented;
2123: PetscBool nooffprocentries;
2125: PetscFunctionBegin;
2126: PetscCheck(bs >= 1, PetscObjectComm((PetscObject)B), PETSC_ERR_ARG_OUTOFRANGE, "Invalid block size specified, must be positive but it is %" PetscInt_FMT, bs);
2127: PetscCall(PetscLayoutSetBlockSize(B->rmap, bs));
2128: PetscCall(PetscLayoutSetBlockSize(B->cmap, bs));
2129: PetscCall(PetscLayoutSetUp(B->rmap));
2130: PetscCall(PetscLayoutSetUp(B->cmap));
2131: PetscCall(PetscLayoutGetBlockSize(B->rmap, &bs));
2132: m = B->rmap->n / bs;
2133: rstart = B->rmap->rstart / bs;
2134: cend = B->cmap->rend / bs;
2136: PetscCheck(!ii[0], PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "ii[0] must be 0 but it is %" PetscInt_FMT, ii[0]);
2137: PetscCall(PetscMalloc2(m, &d_nnz, m, &o_nnz));
2138: for (i = 0; i < m; i++) {
2139: nz = ii[i + 1] - ii[i];
2140: PetscCheck(nz >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Local row %" PetscInt_FMT " has a negative number of columns %" PetscInt_FMT, i, nz);
2141: /* count the ones on the diagonal and above, split into diagonal and off-diagonal portions. */
2142: JJ = jj + ii[i];
2143: bd = 0;
2144: for (j = 0; j < nz; j++) {
2145: if (*JJ >= i + rstart) break;
2146: JJ++;
2147: bd++;
2148: }
2149: d = 0;
2150: for (; j < nz; j++) {
2151: if (*JJ++ >= cend) break;
2152: d++;
2153: }
2154: d_nnz[i] = d;
2155: o_nnz[i] = nz - d - bd;
2156: nz = nz - bd;
2157: nz_max = PetscMax(nz_max, nz);
2158: }
2159: PetscCall(MatMPISBAIJSetPreallocation(B, bs, 0, d_nnz, 0, o_nnz));
2160: PetscCall(MatSetOption(B, MAT_IGNORE_LOWER_TRIANGULAR, PETSC_TRUE));
2161: PetscCall(PetscFree2(d_nnz, o_nnz));
2163: values = (PetscScalar *)V;
2164: if (!values) PetscCall(PetscCalloc1(bs * bs * nz_max, &values));
2165: for (i = 0; i < m; i++) {
2166: PetscInt row = i + rstart;
2167: PetscInt ncols = ii[i + 1] - ii[i];
2168: const PetscInt *icols = jj + ii[i];
2169: if (bs == 1 || !roworiented) { /* block ordering matches the non-nested layout of MatSetValues so we can insert entire rows */
2170: const PetscScalar *svals = values + (V ? (bs * bs * ii[i]) : 0);
2171: PetscCall(MatSetValuesBlocked_MPISBAIJ(B, 1, &row, ncols, icols, svals, INSERT_VALUES));
2172: } else { /* block ordering does not match so we can only insert one block at a time. */
2173: for (PetscInt j = 0; j < ncols; j++) {
2174: const PetscScalar *svals = values + (V ? (bs * bs * (ii[i] + j)) : 0);
2175: PetscCall(MatSetValuesBlocked_MPISBAIJ(B, 1, &row, 1, &icols[j], svals, INSERT_VALUES));
2176: }
2177: }
2178: }
2180: if (!V) PetscCall(PetscFree(values));
2181: nooffprocentries = B->nooffprocentries;
2182: B->nooffprocentries = PETSC_TRUE;
2183: PetscCall(MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY));
2184: PetscCall(MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY));
2185: B->nooffprocentries = nooffprocentries;
2187: PetscCall(MatSetOption(B, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
2188: PetscFunctionReturn(PETSC_SUCCESS);
2189: }
2191: /*MC
2192: MATMPISBAIJ - MATMPISBAIJ = "mpisbaij" - A matrix type to be used for distributed symmetric sparse block matrices,
2193: based on block compressed sparse row format. Only the upper triangular portion of the "diagonal" portion of
2194: the matrix is stored.
2196: For complex numbers by default this matrix is symmetric, NOT Hermitian symmetric. To make it Hermitian symmetric you
2197: can call `MatSetOption(A, MAT_HERMITIAN, PETSC_TRUE)`.
2199: Options Database Key:
2200: . -mat_type mpisbaij - sets the matrix type to "mpisbaij" during a call to `MatSetFromOptions()`
2202: Level: beginner
2204: Notes:
2205: Call `MatSetOption(A, MAT_STRUCTURE_ONLY, PETSC_TRUE)` before preallocation or `MatSetUp()` to store only the nonzero pattern.
2206: The assembled matrix has no numerical value array. Row and column indices supplied during insertion are retained, while numerical values are ignored.
2207: Such matrices can be used for structural operations, but not for numerical operations.
2209: 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
2210: diagonal portion of the matrix of each process must be less than or equal to the number of columns.
2212: .seealso: [](ch_matrices), `Mat`, `MATSBAIJ`, `MATBAIJ`, `MatCreateBAIJ()`, `MATSEQSBAIJ`, `MatType`
2213: M*/
2215: static PetscErrorCode MatGetMultPetscSF_MPISBAIJ(Mat A, PetscSF *sf)
2216: {
2217: Mat_MPISBAIJ *a = (Mat_MPISBAIJ *)A->data;
2219: PetscFunctionBegin;
2220: *sf = a->Mvctx;
2221: PetscFunctionReturn(PETSC_SUCCESS);
2222: }
2224: PETSC_EXTERN PetscErrorCode MatCreate_MPISBAIJ(Mat B)
2225: {
2226: Mat_MPISBAIJ *b;
2227: PetscBool flg = PETSC_FALSE;
2229: PetscFunctionBegin;
2230: PetscCall(PetscNew(&b));
2231: B->data = (void *)b;
2232: B->ops[0] = MatOps_Values;
2234: B->ops->destroy = MatDestroy_MPISBAIJ;
2235: B->ops->view = MatView_MPISBAIJ;
2236: B->assembled = PETSC_FALSE;
2237: B->insertmode = NOT_SET_VALUES;
2239: PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)B), &b->rank));
2240: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)B), &b->size));
2242: /* build local table of row and column ownerships */
2243: PetscCall(PetscMalloc1(b->size + 2, &b->rangebs));
2245: /* build cache for off array entries formed */
2246: PetscCall(MatStashCreate_Private(PetscObjectComm((PetscObject)B), 1, &B->stash));
2248: b->donotstash = PETSC_FALSE;
2249: b->colmap = NULL;
2250: b->garray = NULL;
2251: b->roworiented = PETSC_TRUE;
2253: /* stuff used in block assembly */
2254: b->barray = NULL;
2256: /* stuff used for matrix vector multiply */
2257: b->lvec = NULL;
2258: b->Mvctx = NULL;
2259: b->slvec0 = NULL;
2260: b->slvec0b = NULL;
2261: b->slvec1 = NULL;
2262: b->slvec1a = NULL;
2263: b->slvec1b = NULL;
2264: b->sMvctx = NULL;
2266: /* stuff for MatGetRow() */
2267: b->rowindices = NULL;
2268: b->rowvalues = NULL;
2269: b->getrowactive = PETSC_FALSE;
2271: /* hash table stuff */
2272: b->ht = NULL;
2273: b->hd = NULL;
2274: b->ht_size = 0;
2275: b->ht_flag = PETSC_FALSE;
2276: b->ht_fact = 0;
2277: b->ht_total_ct = 0;
2278: b->ht_insert_ct = 0;
2280: b->in_loc = NULL;
2281: b->v_loc = NULL;
2282: b->n_loc = 0;
2284: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatStoreValues_C", MatStoreValues_MPISBAIJ));
2285: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatRetrieveValues_C", MatRetrieveValues_MPISBAIJ));
2286: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMPISBAIJSetPreallocation_C", MatMPISBAIJSetPreallocation_MPISBAIJ));
2287: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMPISBAIJSetPreallocationCSR_C", MatMPISBAIJSetPreallocationCSR_MPISBAIJ));
2288: #if PetscDefined(HAVE_ELEMENTAL)
2289: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_mpisbaij_elemental_C", MatConvert_MPISBAIJ_Elemental));
2290: #endif
2291: #if PetscDefined(HAVE_SCALAPACK) && (PetscDefined(USE_REAL_SINGLE) || PetscDefined(USE_REAL_DOUBLE))
2292: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_mpisbaij_scalapack_C", MatConvert_SBAIJ_ScaLAPACK));
2293: #endif
2294: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_mpisbaij_mpiaij_C", MatConvert_MPISBAIJ_Basic));
2295: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_mpisbaij_mpibaij_C", MatConvert_MPISBAIJ_Basic));
2296: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatGetMultPetscSF_C", MatGetMultPetscSF_MPISBAIJ));
2298: B->symmetric = PETSC_BOOL3_TRUE;
2299: B->structurally_symmetric = PETSC_BOOL3_TRUE;
2300: B->symmetry_eternal = PETSC_TRUE;
2301: B->structural_symmetry_eternal = PETSC_TRUE;
2302: #if !PetscDefined(USE_COMPLEX)
2303: B->hermitian = PETSC_BOOL3_TRUE;
2304: #endif
2306: PetscCall(PetscObjectChangeTypeName((PetscObject)B, MATMPISBAIJ));
2307: PetscOptionsBegin(PetscObjectComm((PetscObject)B), NULL, "Options for loading MPISBAIJ matrix 1", "Mat");
2308: PetscCall(PetscOptionsBool("-mat_use_hash_table", "Use hash table to save memory in constructing matrix", "MatSetOption", flg, &flg, NULL));
2309: if (flg) {
2310: PetscReal fact = 1.39;
2311: PetscCall(MatSetOption(B, MAT_USE_HASH_TABLE, PETSC_TRUE));
2312: PetscCall(PetscOptionsReal("-mat_use_hash_table", "Use hash table factor", "MatMPIBAIJSetHashTableFactor", fact, &fact, NULL));
2313: if (fact <= 1.0) fact = 1.39;
2314: PetscCall(MatMPIBAIJSetHashTableFactor(B, fact));
2315: PetscCall(PetscInfo(B, "Hash table Factor used %5.2g\n", (double)fact));
2316: }
2317: PetscOptionsEnd();
2318: PetscFunctionReturn(PETSC_SUCCESS);
2319: }
2321: // PetscClangLinter pragma disable: -fdoc-section-header-unknown
2322: /*MC
2323: MATSBAIJ - MATSBAIJ = "sbaij" - A matrix type to be used for symmetric block sparse matrices.
2325: This matrix type is identical to `MATSEQSBAIJ` when constructed with a single process communicator,
2326: and `MATMPISBAIJ` otherwise.
2328: Options Database Key:
2329: . -mat_type sbaij - sets the matrix type to `MATSBAIJ` during a call to `MatSetFromOptions()`
2331: Level: beginner
2333: Notes:
2334: Call `MatSetOption(A, MAT_STRUCTURE_ONLY, PETSC_TRUE)` before preallocation or `MatSetUp()` to store only the nonzero pattern.
2335: The assembled matrix has no numerical value array. Row and column indices supplied during insertion are retained, while numerical values are ignored.
2336: Such matrices can be used for structural operations, but not for numerical operations.
2338: .seealso: [](ch_matrices), `Mat`, `MATSEQSBAIJ`, `MATMPISBAIJ`, `MatCreateSBAIJ()`, `MATSEQBAIJ`, `MATMPIBAIJ`
2339: M*/
2341: /*@
2342: MatMPISBAIJSetPreallocation - For good matrix assembly performance
2343: the user should preallocate the matrix storage by setting the parameters
2344: d_nz (or d_nnz) and o_nz (or o_nnz). By setting these parameters accurately,
2345: performance can be increased by more than a factor of 50.
2347: Collective
2349: Input Parameters:
2350: + B - the matrix
2351: . bs - size of block, the blocks are ALWAYS square. One can use MatSetBlockSizes() to set a different row and column blocksize but the row
2352: blocksize always defines the size of the blocks. The column blocksize sets the blocksize of the vectors obtained with MatCreateVecs()
2353: . d_nz - number of block nonzeros per block row in diagonal portion of local
2354: submatrix (same for all local rows)
2355: . d_nnz - array containing the number of block nonzeros in the various block rows
2356: in the upper triangular and diagonal part of the in diagonal portion of the local
2357: (possibly different for each block row) or `NULL`. If you plan to factor the matrix you must leave room
2358: for the diagonal entry and set a value even if it is zero.
2359: . o_nz - number of block nonzeros per block row in the off-diagonal portion of local
2360: submatrix (same for all local rows).
2361: - o_nnz - array containing the number of nonzeros in the various block rows of the
2362: off-diagonal portion of the local submatrix that is right of the diagonal
2363: (possibly different for each block row) or `NULL`.
2365: Options Database Keys:
2366: + -mat_no_unroll - uses code that does not unroll the loops in the
2367: block calculations (much slower)
2368: - -mat_block_size - size of the blocks to use
2370: Level: intermediate
2372: Notes:
2374: If `PETSC_DECIDE` or `PETSC_DETERMINE` is used for a particular argument on one processor
2375: than it must be used on all processors that share the object for that argument.
2377: If the *_nnz parameter is given then the *_nz parameter is ignored
2379: Storage Information:
2380: For a square global matrix we define each processor's diagonal portion
2381: to be its local rows and the corresponding columns (a square submatrix);
2382: each processor's off-diagonal portion encompasses the remainder of the
2383: local matrix (a rectangular submatrix).
2385: The user can specify preallocated storage for the diagonal part of
2386: the local submatrix with either `d_nz` or `d_nnz` (not both). Set
2387: `d_nz` = `PETSC_DEFAULT` and `d_nnz` = `NULL` for PETSc to control dynamic
2388: memory allocation. Likewise, specify preallocated storage for the
2389: off-diagonal part of the local submatrix with `o_nz` or `o_nnz` (not both).
2391: You can call `MatGetInfo()` to get information on how effective the preallocation was;
2392: for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
2393: You can also run with the option `-info` and look for messages with the string
2394: malloc in them to see if additional memory allocation was needed.
2396: Consider a processor that owns rows 3, 4 and 5 of a parallel matrix. In
2397: the figure below we depict these three local rows and all columns (0-11).
2399: .vb
2400: 0 1 2 3 4 5 6 7 8 9 10 11
2401: --------------------------
2402: row 3 |. . . d d d o o o o o o
2403: row 4 |. . . d d d o o o o o o
2404: row 5 |. . . d d d o o o o o o
2405: --------------------------
2406: .ve
2408: Thus, any entries in the d locations are stored in the d (diagonal)
2409: submatrix, and any entries in the o locations are stored in the
2410: o (off-diagonal) submatrix. Note that the d matrix is stored in
2411: `MATSEQSBAIJ` format and the o submatrix in `MATSEQBAIJ` format.
2413: Now `d_nz` should indicate the number of block nonzeros per row in the upper triangular
2414: plus the diagonal part of the d matrix,
2415: and `o_nz` should indicate the number of block nonzeros per row in the o matrix
2417: In general, for PDE problems in which most nonzeros are near the diagonal,
2418: one expects `d_nz` >> `o_nz`.
2420: .seealso: [](ch_matrices), `Mat`, `MATMPISBAIJ`, `MATSBAIJ`, `MatCreate()`, `MatCreateSeqSBAIJ()`, `MatSetValues()`, `MatCreateBAIJ()`, `PetscSplitOwnership()`
2421: @*/
2422: PetscErrorCode MatMPISBAIJSetPreallocation(Mat B, PetscInt bs, PetscInt d_nz, const PetscInt d_nnz[], PetscInt o_nz, const PetscInt o_nnz[])
2423: {
2424: PetscFunctionBegin;
2428: PetscTryMethod(B, "MatMPISBAIJSetPreallocation_C", (Mat, PetscInt, PetscInt, const PetscInt[], PetscInt, const PetscInt[]), (B, bs, d_nz, d_nnz, o_nz, o_nnz));
2429: PetscFunctionReturn(PETSC_SUCCESS);
2430: }
2432: // PetscClangLinter pragma disable: -fdoc-section-header-unknown
2433: /*@
2434: MatCreateSBAIJ - Creates a sparse parallel matrix in symmetric block AIJ format, `MATSBAIJ`,
2435: (block compressed row). For good matrix assembly performance
2436: the user should preallocate the matrix storage by setting the parameters
2437: `d_nz` (or `d_nnz`) and `o_nz` (or `o_nnz`).
2439: Collective
2441: Input Parameters:
2442: + comm - MPI communicator
2443: . bs - size of block, the blocks are ALWAYS square. One can use `MatSetBlockSizes()` to set a different row and column blocksize but the row
2444: blocksize always defines the size of the blocks. The column blocksize sets the blocksize of the vectors obtained with `MatCreateVecs()`
2445: . m - number of local rows (or `PETSC_DECIDE` to have calculated if `M` is given)
2446: This value should be the same as the local size used in creating the
2447: y vector for the matrix-vector product y = Ax.
2448: . n - number of local columns (or `PETSC_DECIDE` to have calculated if `N` is given)
2449: This value should be the same as the local size used in creating the
2450: x vector for the matrix-vector product y = Ax.
2451: . M - number of global rows (or `PETSC_DETERMINE` to have calculated if `m` is given)
2452: . N - number of global columns (or `PETSC_DETERMINE` to have calculated if `n` is given)
2453: . d_nz - number of block nonzeros per block row in diagonal portion of local
2454: submatrix (same for all local rows)
2455: . d_nnz - array containing the number of block nonzeros in the various block rows
2456: in the upper triangular portion of the in diagonal portion of the local
2457: (possibly different for each block block row) or `NULL`.
2458: If you plan to factor the matrix you must leave room for the diagonal entry and
2459: set its value even if it is zero.
2460: . o_nz - number of block nonzeros per block row in the off-diagonal portion of local
2461: submatrix (same for all local rows).
2462: - o_nnz - array containing the number of nonzeros in the various block rows of the
2463: off-diagonal portion of the local submatrix (possibly different for
2464: each block row) or `NULL`.
2466: Output Parameter:
2467: . A - the matrix
2469: Options Database Keys:
2470: + -mat_no_unroll - uses code that does not unroll the loops in the
2471: block calculations (much slower)
2472: . -mat_block_size - size of the blocks to use
2473: - -mat_mpi - use the parallel matrix data structures even on one processor
2474: (defaults to using SeqBAIJ format on one processor)
2476: Level: intermediate
2478: Notes:
2479: It is recommended that one use `MatCreateFromOptions()` or the `MatCreate()`, `MatSetType()` and/or `MatSetFromOptions()`,
2480: MatXXXXSetPreallocation() paradigm instead of this routine directly.
2481: [MatXXXXSetPreallocation() is, for example, `MatSeqAIJSetPreallocation()`]
2483: The number of rows and columns must be divisible by blocksize.
2484: This matrix type does not support complex Hermitian operation.
2486: The user MUST specify either the local or global matrix dimensions
2487: (possibly both).
2489: If `PETSC_DECIDE` or `PETSC_DETERMINE` is used for a particular argument on one processor
2490: than it must be used on all processors that share the object for that argument.
2492: If `m` and `n` are not `PETSC_DECIDE`, then the values determines the `PetscLayout` of the matrix and the ranges returned by
2493: `MatGetOwnershipRange()`, `MatGetOwnershipRanges()`, `MatGetOwnershipRangeColumn()`, and `MatGetOwnershipRangesColumn()`.
2495: If the *_nnz parameter is given then the *_nz parameter is ignored
2497: Storage Information:
2498: For a square global matrix we define each processor's diagonal portion
2499: to be its local rows and the corresponding columns (a square submatrix);
2500: each processor's off-diagonal portion encompasses the remainder of the
2501: local matrix (a rectangular submatrix).
2503: The user can specify preallocated storage for the diagonal part of
2504: the local submatrix with either `d_nz` or `d_nnz` (not both). Set
2505: `d_nz` = `PETSC_DEFAULT` and `d_nnz` = `NULL` for PETSc to control dynamic
2506: memory allocation. Likewise, specify preallocated storage for the
2507: off-diagonal part of the local submatrix with `o_nz` or `o_nnz` (not both).
2509: Consider a processor that owns rows 3, 4 and 5 of a parallel matrix. In
2510: the figure below we depict these three local rows and all columns (0-11).
2512: .vb
2513: 0 1 2 3 4 5 6 7 8 9 10 11
2514: --------------------------
2515: row 3 |. . . d d d o o o o o o
2516: row 4 |. . . d d d o o o o o o
2517: row 5 |. . . d d d o o o o o o
2518: --------------------------
2519: .ve
2521: Thus, any entries in the d locations are stored in the d (diagonal)
2522: submatrix, and any entries in the o locations are stored in the
2523: o (off-diagonal) submatrix. Note that the d matrix is stored in
2524: `MATSEQSBAIJ` format and the o submatrix in `MATSEQBAIJ` format.
2526: Now `d_nz` should indicate the number of block nonzeros per row in the upper triangular
2527: plus the diagonal part of the d matrix,
2528: and `o_nz` should indicate the number of block nonzeros per row in the o matrix.
2529: In general, for PDE problems in which most nonzeros are near the diagonal,
2530: one expects `d_nz` >> `o_nz`.
2532: .seealso: [](ch_matrices), `Mat`, `MATSBAIJ`, `MatCreate()`, `MatCreateSeqSBAIJ()`, `MatSetValues()`, `MatCreateBAIJ()`,
2533: `MatGetOwnershipRange()`, `MatGetOwnershipRanges()`, `MatGetOwnershipRangeColumn()`, `MatGetOwnershipRangesColumn()`, `PetscLayout`
2534: @*/
2535: 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)
2536: {
2537: PetscMPIInt size;
2539: PetscFunctionBegin;
2540: PetscCall(MatCreate(comm, A));
2541: PetscCall(MatSetSizes(*A, m, n, M, N));
2542: PetscCallMPI(MPI_Comm_size(comm, &size));
2543: if (size > 1) {
2544: PetscCall(MatSetType(*A, MATMPISBAIJ));
2545: PetscCall(MatMPISBAIJSetPreallocation(*A, bs, d_nz, d_nnz, o_nz, o_nnz));
2546: } else {
2547: PetscCall(MatSetType(*A, MATSEQSBAIJ));
2548: PetscCall(MatSeqSBAIJSetPreallocation(*A, bs, d_nz, d_nnz));
2549: }
2550: PetscFunctionReturn(PETSC_SUCCESS);
2551: }
2553: static PetscErrorCode MatDuplicate_MPISBAIJ(Mat matin, MatDuplicateOption cpvalues, Mat *newmat)
2554: {
2555: Mat mat;
2556: Mat_MPISBAIJ *a, *oldmat = (Mat_MPISBAIJ *)matin->data;
2557: PetscInt len = 0, nt, bs = matin->rmap->bs, mbs = oldmat->mbs;
2558: PetscScalar *array;
2560: PetscFunctionBegin;
2561: *newmat = NULL;
2563: PetscCall(MatCreate(PetscObjectComm((PetscObject)matin), &mat));
2564: PetscCall(MatSetSizes(mat, matin->rmap->n, matin->cmap->n, matin->rmap->N, matin->cmap->N));
2565: PetscCall(MatSetType(mat, ((PetscObject)matin)->type_name));
2566: PetscCall(MatSetOption(mat, MAT_STRUCTURE_ONLY, matin->structure_only));
2567: PetscCall(PetscLayoutReference(matin->rmap, &mat->rmap));
2568: PetscCall(PetscLayoutReference(matin->cmap, &mat->cmap));
2570: if (matin->hash_active) PetscCall(MatSetUp(mat));
2571: else {
2572: mat->factortype = matin->factortype;
2573: mat->preallocated = PETSC_TRUE;
2574: mat->assembled = PETSC_TRUE;
2575: mat->insertmode = NOT_SET_VALUES;
2577: a = (Mat_MPISBAIJ *)mat->data;
2578: a->bs2 = oldmat->bs2;
2579: a->mbs = oldmat->mbs;
2580: a->nbs = oldmat->nbs;
2581: a->Mbs = oldmat->Mbs;
2582: a->Nbs = oldmat->Nbs;
2584: a->size = oldmat->size;
2585: a->rank = oldmat->rank;
2586: a->donotstash = oldmat->donotstash;
2587: a->roworiented = oldmat->roworiented;
2588: a->rowindices = NULL;
2589: a->rowvalues = NULL;
2590: a->getrowactive = PETSC_FALSE;
2591: a->barray = NULL;
2592: a->rstartbs = oldmat->rstartbs;
2593: a->rendbs = oldmat->rendbs;
2594: a->cstartbs = oldmat->cstartbs;
2595: a->cendbs = oldmat->cendbs;
2597: /* hash table stuff */
2598: a->ht = NULL;
2599: a->hd = NULL;
2600: a->ht_size = 0;
2601: a->ht_flag = oldmat->ht_flag;
2602: a->ht_fact = oldmat->ht_fact;
2603: a->ht_total_ct = 0;
2604: a->ht_insert_ct = 0;
2606: PetscCall(PetscArraycpy(a->rangebs, oldmat->rangebs, a->size + 2));
2607: if (oldmat->colmap) {
2608: #if PetscDefined(USE_CTABLE)
2609: PetscCall(PetscHMapIDuplicate(oldmat->colmap, &a->colmap));
2610: #else
2611: PetscCall(PetscMalloc1(a->Nbs, &a->colmap));
2612: PetscCall(PetscArraycpy(a->colmap, oldmat->colmap, a->Nbs));
2613: #endif
2614: } else a->colmap = NULL;
2616: if (oldmat->garray && (len = ((Mat_SeqBAIJ *)oldmat->B->data)->nbs)) {
2617: PetscCall(PetscMalloc1(len, &a->garray));
2618: PetscCall(PetscArraycpy(a->garray, oldmat->garray, len));
2619: } else a->garray = NULL;
2621: PetscCall(MatStashCreate_Private(PetscObjectComm((PetscObject)matin), matin->rmap->bs, &mat->bstash));
2622: PetscCall(VecDuplicate(oldmat->lvec, &a->lvec));
2623: PetscCall(VecScatterCopy(oldmat->Mvctx, &a->Mvctx));
2625: PetscCall(VecDuplicate(oldmat->slvec0, &a->slvec0));
2626: PetscCall(VecDuplicate(oldmat->slvec1, &a->slvec1));
2628: PetscCall(VecGetLocalSize(a->slvec1, &nt));
2629: PetscCall(VecGetArray(a->slvec1, &array));
2630: PetscCall(VecCreateSeqWithArray(PETSC_COMM_SELF, 1, bs * mbs, array, &a->slvec1a));
2631: PetscCall(VecCreateSeqWithArray(PETSC_COMM_SELF, 1, nt - bs * mbs, array + bs * mbs, &a->slvec1b));
2632: PetscCall(VecRestoreArray(a->slvec1, &array));
2633: PetscCall(VecGetArray(a->slvec0, &array));
2634: PetscCall(VecCreateSeqWithArray(PETSC_COMM_SELF, 1, nt - bs * mbs, array + bs * mbs, &a->slvec0b));
2635: PetscCall(VecRestoreArray(a->slvec0, &array));
2637: /* ierr = VecScatterCopy(oldmat->sMvctx,&a->sMvctx); - not written yet, replaced by the lazy trick: */
2638: PetscCall(PetscObjectReference((PetscObject)oldmat->sMvctx));
2639: a->sMvctx = oldmat->sMvctx;
2641: PetscCall(MatDuplicate(oldmat->A, cpvalues, &a->A));
2642: PetscCall(MatDuplicate(oldmat->B, cpvalues, &a->B));
2643: }
2644: PetscCall(PetscFunctionListDuplicate(((PetscObject)matin)->qlist, &((PetscObject)mat)->qlist));
2645: *newmat = mat;
2646: PetscFunctionReturn(PETSC_SUCCESS);
2647: }
2649: /* Used for both MPIBAIJ and MPISBAIJ matrices */
2650: #define MatLoad_MPISBAIJ_Binary MatLoad_MPIBAIJ_Binary
2652: static PetscErrorCode MatLoad_MPISBAIJ(Mat mat, PetscViewer viewer)
2653: {
2654: PetscBool isbinary;
2656: PetscFunctionBegin;
2657: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
2658: 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);
2659: PetscCall(MatLoad_MPISBAIJ_Binary(mat, viewer));
2660: PetscFunctionReturn(PETSC_SUCCESS);
2661: }
2663: static PetscErrorCode MatGetRowMaxAbs_MPISBAIJ(Mat A, Vec v, PetscInt idx[])
2664: {
2665: Mat_MPISBAIJ *a = (Mat_MPISBAIJ *)A->data;
2666: Mat_SeqBAIJ *b = (Mat_SeqBAIJ *)a->B->data;
2667: PetscReal atmp;
2668: PetscReal *work, *svalues, *rvalues;
2669: PetscInt i, bs, mbs, *bi, *bj, brow, j, ncols, krow, kcol, col, row, Mbs, bcol;
2670: PetscMPIInt rank, size;
2671: PetscInt *rowners_bs, count, source;
2672: PetscScalar *va;
2673: MatScalar *ba;
2674: MPI_Status stat;
2676: PetscFunctionBegin;
2677: PetscCheck(!idx, PETSC_COMM_SELF, PETSC_ERR_SUP, "Send email to petsc-maint@mcs.anl.gov");
2678: PetscCall(MatGetRowMaxAbs(a->A, v, NULL));
2679: PetscCall(VecGetArray(v, &va));
2681: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)A), &size));
2682: PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank));
2684: bs = A->rmap->bs;
2685: mbs = a->mbs;
2686: Mbs = a->Mbs;
2687: ba = b->a;
2688: bi = b->i;
2689: bj = b->j;
2691: /* find ownerships */
2692: rowners_bs = A->rmap->range;
2694: /* each proc creates an array to be distributed */
2695: PetscCall(PetscCalloc1(bs * Mbs, &work));
2697: /* row_max for B */
2698: if (rank != size - 1) {
2699: for (i = 0; i < mbs; i++) {
2700: ncols = bi[1] - bi[0];
2701: bi++;
2702: brow = bs * i;
2703: for (j = 0; j < ncols; j++) {
2704: bcol = bs * (*bj);
2705: for (kcol = 0; kcol < bs; kcol++) {
2706: col = bcol + kcol; /* local col index */
2707: col += rowners_bs[rank + 1]; /* global col index */
2708: for (krow = 0; krow < bs; krow++) {
2709: atmp = PetscAbsScalar(*ba);
2710: ba++;
2711: row = brow + krow; /* local row index */
2712: if (PetscRealPart(va[row]) < atmp) va[row] = atmp;
2713: if (work[col] < atmp) work[col] = atmp;
2714: }
2715: }
2716: bj++;
2717: }
2718: }
2720: /* send values to its owners */
2721: for (PetscMPIInt dest = rank + 1; dest < size; dest++) {
2722: svalues = work + rowners_bs[dest];
2723: count = rowners_bs[dest + 1] - rowners_bs[dest];
2724: PetscCallMPI(MPIU_Send(svalues, count, MPIU_REAL, dest, rank, PetscObjectComm((PetscObject)A)));
2725: }
2726: }
2728: /* receive values */
2729: if (rank) {
2730: rvalues = work;
2731: count = rowners_bs[rank + 1] - rowners_bs[rank];
2732: for (source = 0; source < rank; source++) {
2733: PetscCallMPI(MPIU_Recv(rvalues, count, MPIU_REAL, MPI_ANY_SOURCE, MPI_ANY_TAG, PetscObjectComm((PetscObject)A), &stat));
2734: /* process values */
2735: for (i = 0; i < count; i++) {
2736: if (PetscRealPart(va[i]) < rvalues[i]) va[i] = rvalues[i];
2737: }
2738: }
2739: }
2741: PetscCall(VecRestoreArray(v, &va));
2742: PetscCall(PetscFree(work));
2743: PetscFunctionReturn(PETSC_SUCCESS);
2744: }
2746: static PetscErrorCode MatSOR_MPISBAIJ(Mat matin, Vec bb, PetscReal omega, MatSORType flag, PetscReal fshift, PetscInt its, PetscInt lits, Vec xx)
2747: {
2748: Mat_MPISBAIJ *mat = (Mat_MPISBAIJ *)matin->data;
2749: PetscInt mbs = mat->mbs, bs = matin->rmap->bs;
2750: PetscScalar *x, *ptr, *from;
2751: Vec bb1;
2752: const PetscScalar *b;
2754: PetscFunctionBegin;
2755: 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);
2756: PetscCheck(bs <= 1, PETSC_COMM_SELF, PETSC_ERR_SUP, "SSOR for block size > 1 is not yet implemented");
2758: if (flag == SOR_APPLY_UPPER) {
2759: PetscUseTypeMethod(mat->A, sor, bb, omega, flag, fshift, lits, 1, xx);
2760: PetscFunctionReturn(PETSC_SUCCESS);
2761: }
2763: if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP) {
2764: if (flag & SOR_ZERO_INITIAL_GUESS) {
2765: PetscUseTypeMethod(mat->A, sor, bb, omega, flag, fshift, lits, lits, xx);
2766: its--;
2767: }
2769: PetscCall(VecDuplicate(bb, &bb1));
2770: while (its--) {
2771: /* lower triangular part: slvec0b = - B^T*xx */
2772: PetscUseTypeMethod(mat->B, multtranspose, xx, mat->slvec0b);
2774: /* copy xx into slvec0 */
2775: PetscCall(VecGetArray(mat->slvec0, &ptr));
2776: PetscCall(VecGetArray(xx, &x));
2777: PetscCall(PetscArraycpy(ptr, x, bs * mbs));
2778: PetscCall(VecRestoreArray(mat->slvec0, &ptr));
2780: PetscCall(VecScale(mat->slvec0, -1.0));
2782: /* copy bb into slvec1a */
2783: PetscCall(VecGetArray(mat->slvec1, &ptr));
2784: PetscCall(VecGetArrayRead(bb, &b));
2785: PetscCall(PetscArraycpy(ptr, b, bs * mbs));
2786: PetscCall(VecRestoreArray(mat->slvec1, &ptr));
2788: /* set slvec1b = 0 */
2789: PetscCall(PetscObjectStateIncrease((PetscObject)mat->slvec1b));
2790: PetscCall(VecZeroEntries(mat->slvec1b));
2792: PetscCall(VecScatterBegin(mat->sMvctx, mat->slvec0, mat->slvec1, ADD_VALUES, SCATTER_FORWARD));
2793: PetscCall(VecRestoreArray(xx, &x));
2794: PetscCall(VecRestoreArrayRead(bb, &b));
2795: PetscCall(VecScatterEnd(mat->sMvctx, mat->slvec0, mat->slvec1, ADD_VALUES, SCATTER_FORWARD));
2797: /* upper triangular part: bb1 = bb1 - B*x */
2798: PetscUseTypeMethod(mat->B, multadd, mat->slvec1b, mat->slvec1a, bb1);
2800: /* local diagonal sweep */
2801: PetscUseTypeMethod(mat->A, sor, bb1, omega, SOR_SYMMETRIC_SWEEP, fshift, lits, lits, xx);
2802: }
2803: PetscCall(VecDestroy(&bb1));
2804: } else if ((flag & SOR_LOCAL_FORWARD_SWEEP) && (its == 1) && (flag & SOR_ZERO_INITIAL_GUESS)) {
2805: PetscUseTypeMethod(mat->A, sor, bb, omega, flag, fshift, lits, 1, xx);
2806: } else if ((flag & SOR_LOCAL_BACKWARD_SWEEP) && (its == 1) && (flag & SOR_ZERO_INITIAL_GUESS)) {
2807: PetscUseTypeMethod(mat->A, sor, bb, omega, flag, fshift, lits, 1, xx);
2808: } else if (flag & SOR_EISENSTAT) {
2809: Vec xx1;
2810: PetscBool hasop;
2811: const PetscScalar *diag;
2812: PetscScalar *sl, scale = (omega - 2.0) / omega;
2813: PetscInt n;
2815: if (!mat->xx1) {
2816: PetscCall(VecDuplicate(bb, &mat->xx1));
2817: PetscCall(VecDuplicate(bb, &mat->bb1));
2818: }
2819: xx1 = mat->xx1;
2820: bb1 = mat->bb1;
2822: PetscUseTypeMethod(mat->A, sor, bb, omega, (MatSORType)(SOR_ZERO_INITIAL_GUESS | SOR_LOCAL_BACKWARD_SWEEP), fshift, lits, 1, xx);
2824: if (!mat->diag) {
2825: /* this is wrong for same matrix with new nonzero values */
2826: PetscCall(MatCreateVecs(matin, &mat->diag, NULL));
2827: PetscCall(MatGetDiagonal(matin, mat->diag));
2828: }
2829: PetscCall(MatHasOperation(matin, MATOP_MULT_DIAGONAL_BLOCK, &hasop));
2831: if (hasop) {
2832: PetscCall(MatMultDiagonalBlock(matin, xx, bb1));
2833: PetscCall(VecAYPX(mat->slvec1a, scale, bb));
2834: } else {
2835: /*
2836: These two lines are replaced by code that may be a bit faster for a good compiler
2837: PetscCall(VecPointwiseMult(mat->slvec1a,mat->diag,xx));
2838: PetscCall(VecAYPX(mat->slvec1a,scale,bb));
2839: */
2840: PetscCall(VecGetArray(mat->slvec1a, &sl));
2841: PetscCall(VecGetArrayRead(mat->diag, &diag));
2842: PetscCall(VecGetArrayRead(bb, &b));
2843: PetscCall(VecGetArray(xx, &x));
2844: PetscCall(VecGetLocalSize(xx, &n));
2845: if (omega == 1.0) {
2846: for (PetscInt i = 0; i < n; i++) sl[i] = b[i] - diag[i] * x[i];
2847: PetscCall(PetscLogFlops(2.0 * n));
2848: } else {
2849: for (PetscInt i = 0; i < n; i++) sl[i] = b[i] + scale * diag[i] * x[i];
2850: PetscCall(PetscLogFlops(3.0 * n));
2851: }
2852: PetscCall(VecRestoreArray(mat->slvec1a, &sl));
2853: PetscCall(VecRestoreArrayRead(mat->diag, &diag));
2854: PetscCall(VecRestoreArrayRead(bb, &b));
2855: PetscCall(VecRestoreArray(xx, &x));
2856: }
2858: /* multiply off-diagonal portion of matrix */
2859: PetscCall(PetscObjectStateIncrease((PetscObject)mat->slvec1b));
2860: PetscCall(VecZeroEntries(mat->slvec1b));
2861: PetscUseTypeMethod(mat->B, multtranspose, xx, mat->slvec0b);
2862: PetscCall(VecGetArray(mat->slvec0, &from));
2863: PetscCall(VecGetArray(xx, &x));
2864: PetscCall(PetscArraycpy(from, x, bs * mbs));
2865: PetscCall(VecRestoreArray(mat->slvec0, &from));
2866: PetscCall(VecRestoreArray(xx, &x));
2867: PetscCall(VecScatterBegin(mat->sMvctx, mat->slvec0, mat->slvec1, ADD_VALUES, SCATTER_FORWARD));
2868: PetscCall(VecScatterEnd(mat->sMvctx, mat->slvec0, mat->slvec1, ADD_VALUES, SCATTER_FORWARD));
2869: PetscUseTypeMethod(mat->B, multadd, mat->slvec1b, mat->slvec1a, mat->slvec1a);
2871: /* local sweep */
2872: PetscUseTypeMethod(mat->A, sor, mat->slvec1a, omega, (MatSORType)(SOR_ZERO_INITIAL_GUESS | SOR_LOCAL_FORWARD_SWEEP), fshift, lits, 1, xx1);
2873: PetscCall(VecAXPY(xx, 1.0, xx1));
2874: } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "MatSORType is not supported for SBAIJ matrix format");
2875: PetscFunctionReturn(PETSC_SUCCESS);
2876: }
2878: /*@
2879: MatCreateMPISBAIJWithArrays - creates a `MATMPISBAIJ` matrix using arrays that contain in standard CSR format for the local rows.
2881: Collective
2883: Input Parameters:
2884: + comm - MPI communicator
2885: . bs - the block size, only a block size of 1 is supported
2886: . m - number of local rows (Cannot be `PETSC_DECIDE`)
2887: . n - This value should be the same as the local size used in creating the
2888: x vector for the matrix-vector product $ y = Ax $. (or `PETSC_DECIDE` to have
2889: calculated if `N` is given) For square matrices `n` is almost always `m`.
2890: . M - number of global rows (or `PETSC_DETERMINE` to have calculated if `m` is given)
2891: . N - number of global columns (or `PETSC_DETERMINE` to have calculated if `n` is given)
2892: . 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
2893: . j - column indices
2894: - a - matrix values
2896: Output Parameter:
2897: . mat - the matrix
2899: Level: intermediate
2901: Notes:
2902: The `i`, `j`, and `a` arrays ARE copied by this routine into the internal format used by PETSc;
2903: thus you CANNOT change the matrix entries by changing the values of `a` after you have
2904: called this routine. Use `MatCreateMPIAIJWithSplitArrays()` to avoid needing to copy the arrays.
2906: The `i` and `j` indices are 0 based, and `i` indices are indices corresponding to the local `j` array.
2908: .seealso: [](ch_matrices), `Mat`, `MATMPISBAIJ`, `MatCreate()`, `MatCreateSeqAIJ()`, `MatSetValues()`, `MatMPIAIJSetPreallocation()`, `MatMPIAIJSetPreallocationCSR()`,
2909: `MATMPIAIJ`, `MatCreateAIJ()`, `MatCreateMPIAIJWithSplitArrays()`, `MatMPISBAIJSetPreallocationCSR()`
2910: @*/
2911: 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)
2912: {
2913: PetscFunctionBegin;
2914: PetscCheck(!i[0], PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "i (row indices) must start with 0");
2915: PetscCheck(m >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "local number of rows (m) cannot be PETSC_DECIDE, or negative");
2916: PetscCall(MatCreate(comm, mat));
2917: PetscCall(MatSetSizes(*mat, m, n, M, N));
2918: PetscCall(MatSetType(*mat, MATMPISBAIJ));
2919: PetscCall(MatMPISBAIJSetPreallocationCSR(*mat, bs, i, j, a));
2920: PetscFunctionReturn(PETSC_SUCCESS);
2921: }
2923: /*@
2924: MatMPISBAIJSetPreallocationCSR - Creates a sparse parallel matrix in `MATMPISBAIJ` format using the given nonzero structure and (optional) numerical values
2926: Collective
2928: Input Parameters:
2929: + B - the matrix
2930: . bs - the block size
2931: . i - the indices into `j` for the start of each local row (indices start with zero)
2932: . j - the column indices for each local row (indices start with zero) these must be sorted for each row
2933: - v - optional values in the matrix, pass `NULL` if not provided
2935: Level: advanced
2937: Notes:
2938: The `i`, `j`, and `v` arrays ARE copied by this routine into the internal format used by PETSc;
2939: thus you CANNOT change the matrix entries by changing the values of `v` after you have
2940: called this routine.
2942: Though this routine has Preallocation() in the name it also sets the exact nonzero locations of the matrix entries
2943: and usually the numerical values as well
2945: Any entries passed in that are below the diagonal are ignored
2947: .seealso: [](ch_matrices), `Mat`, `MATMPISBAIJ`, `MatCreate()`, `MatCreateSeqAIJ()`, `MatSetValues()`, `MatMPIBAIJSetPreallocation()`, `MatCreateAIJ()`, `MATMPIAIJ`,
2948: `MatCreateMPISBAIJWithArrays()`
2949: @*/
2950: PetscErrorCode MatMPISBAIJSetPreallocationCSR(Mat B, PetscInt bs, const PetscInt i[], const PetscInt j[], const PetscScalar v[])
2951: {
2952: PetscFunctionBegin;
2953: PetscTryMethod(B, "MatMPISBAIJSetPreallocationCSR_C", (Mat, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[]), (B, bs, i, j, v));
2954: PetscFunctionReturn(PETSC_SUCCESS);
2955: }
2957: PetscErrorCode MatCreateMPIMatConcatenateSeqMat_MPISBAIJ(MPI_Comm comm, Mat inmat, PetscInt n, MatReuse scall, Mat *outmat)
2958: {
2959: PetscInt m, N, i, rstart, nnz, Ii, bs, cbs;
2960: PetscInt *indx;
2961: PetscScalar *values;
2963: PetscFunctionBegin;
2964: PetscCall(MatGetSize(inmat, &m, &N));
2965: if (scall == MAT_INITIAL_MATRIX) { /* symbolic phase */
2966: Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ *)inmat->data;
2967: PetscInt *dnz, *onz, mbs, Nbs, nbs;
2968: PetscInt *bindx, rmax = a->rmax, j;
2969: PetscMPIInt rank, size;
2971: PetscCall(MatGetBlockSizes(inmat, &bs, &cbs));
2972: mbs = m / bs;
2973: Nbs = N / cbs;
2974: if (n == PETSC_DECIDE) PetscCall(PetscSplitOwnershipBlock(comm, cbs, &n, &N));
2975: nbs = n / cbs;
2977: PetscCall(PetscMalloc1(rmax, &bindx));
2978: MatPreallocateBegin(comm, mbs, nbs, dnz, onz); /* inline function, output __end and __rstart are used below */
2980: PetscCallMPI(MPI_Comm_rank(comm, &rank));
2981: PetscCallMPI(MPI_Comm_size(comm, &size));
2982: if (rank == size - 1) {
2983: /* Check sum(nbs) = Nbs */
2984: PetscCheck(__end == Nbs, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Sum of local block columns %" PetscInt_FMT " != global block columns %" PetscInt_FMT, __end, Nbs);
2985: }
2987: rstart = __rstart; /* block rstart of *outmat; see inline function MatPreallocateBegin */
2988: PetscCall(MatSetOption(inmat, MAT_GETROW_UPPERTRIANGULAR, PETSC_TRUE));
2989: for (i = 0; i < mbs; i++) {
2990: PetscCall(MatGetRow_SeqSBAIJ(inmat, i * bs, &nnz, &indx, NULL)); /* non-blocked nnz and indx */
2991: nnz = nnz / bs;
2992: for (j = 0; j < nnz; j++) bindx[j] = indx[j * bs] / bs;
2993: PetscCall(MatPreallocateSet(i + rstart, nnz, bindx, dnz, onz));
2994: PetscCall(MatRestoreRow_SeqSBAIJ(inmat, i * bs, &nnz, &indx, NULL));
2995: }
2996: PetscCall(MatSetOption(inmat, MAT_GETROW_UPPERTRIANGULAR, PETSC_FALSE));
2997: PetscCall(PetscFree(bindx));
2999: PetscCall(MatCreate(comm, outmat));
3000: PetscCall(MatSetSizes(*outmat, m, n, PETSC_DETERMINE, PETSC_DETERMINE));
3001: PetscCall(MatSetBlockSizes(*outmat, bs, cbs));
3002: PetscCall(MatSetType(*outmat, MATSBAIJ));
3003: PetscCall(MatSeqSBAIJSetPreallocation(*outmat, bs, 0, dnz));
3004: PetscCall(MatMPISBAIJSetPreallocation(*outmat, bs, 0, dnz, 0, onz));
3005: MatPreallocateEnd(dnz, onz);
3006: }
3008: /* numeric phase */
3009: PetscCall(MatGetBlockSizes(inmat, &bs, &cbs));
3010: PetscCall(MatGetOwnershipRange(*outmat, &rstart, NULL));
3012: PetscCall(MatSetOption(inmat, MAT_GETROW_UPPERTRIANGULAR, PETSC_TRUE));
3013: for (i = 0; i < m; i++) {
3014: PetscCall(MatGetRow_SeqSBAIJ(inmat, i, &nnz, &indx, &values));
3015: Ii = i + rstart;
3016: PetscCall(MatSetValues(*outmat, 1, &Ii, nnz, indx, values, INSERT_VALUES));
3017: PetscCall(MatRestoreRow_SeqSBAIJ(inmat, i, &nnz, &indx, &values));
3018: }
3019: PetscCall(MatSetOption(inmat, MAT_GETROW_UPPERTRIANGULAR, PETSC_FALSE));
3020: PetscCall(MatAssemblyBegin(*outmat, MAT_FINAL_ASSEMBLY));
3021: PetscCall(MatAssemblyEnd(*outmat, MAT_FINAL_ASSEMBLY));
3022: PetscFunctionReturn(PETSC_SUCCESS);
3023: }