Actual source code: mpibaij.c
1: #include <../src/mat/impls/baij/mpi/mpibaij.h>
3: #include <petsc/private/hashseti.h>
4: #include <petscblaslapack.h>
5: #include <petscsf.h>
7: #if PetscDefined(HAVE_LIBXSMM)
8: PETSC_INTERN PetscErrorCode MatConvert_MPIBAIJ_MPIBAIJLIBXSMM(Mat, MatType, MatReuse, Mat *);
9: #endif
11: static PetscErrorCode MatDestroy_MPIBAIJ(Mat mat)
12: {
13: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ *)mat->data;
15: PetscFunctionBegin;
16: PetscCall(PetscLogObjectState((PetscObject)mat, "Rows=%" PetscInt_FMT ",Cols=%" PetscInt_FMT, mat->rmap->N, mat->cmap->N));
17: PetscCall(MatStashDestroy_Private(&mat->stash));
18: PetscCall(MatStashDestroy_Private(&mat->bstash));
19: PetscCall(MatDestroy(&baij->A));
20: PetscCall(MatDestroy(&baij->B));
21: #if PetscDefined(USE_CTABLE)
22: PetscCall(PetscHMapIDestroy(&baij->colmap));
23: #else
24: PetscCall(PetscFree(baij->colmap));
25: #endif
26: PetscCall(PetscFree(baij->garray));
27: PetscCall(VecDestroy(&baij->lvec));
28: PetscCall(VecScatterDestroy(&baij->Mvctx));
29: PetscCall(PetscFree2(baij->rowvalues, baij->rowindices));
30: PetscCall(PetscFree(baij->barray));
31: PetscCall(PetscFree2(baij->hd, baij->ht));
32: PetscCall(PetscFree(baij->rangebs));
33: PetscCall(PetscFree(mat->data));
35: PetscCall(PetscObjectChangeTypeName((PetscObject)mat, NULL));
36: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatStoreValues_C", NULL));
37: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatRetrieveValues_C", NULL));
38: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatGetMultPetscSF_C", NULL));
39: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatMPIBAIJSetPreallocation_C", NULL));
40: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatMPIBAIJSetPreallocationCSR_C", NULL));
41: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDiagonalScaleLocal_C", NULL));
42: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatSetHashTableFactor_C", NULL));
43: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_mpibaij_mpidense_C", NULL));
44: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_mpibaij_mpisbaij_C", NULL));
45: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_mpibaij_mpiadj_C", NULL));
46: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_mpibaij_mpiaij_C", NULL));
47: #if PetscDefined(HAVE_HYPRE)
48: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_mpibaij_hypre_C", NULL));
49: #endif
50: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_mpibaij_is_C", NULL));
51: #if PetscDefined(HAVE_LIBXSMM)
52: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_mpibaij_mpibaijlibxsmm_C", NULL));
53: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatProductSetFromOptions_mpibaijlibxsmm_mpidense_C", NULL));
54: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_mpibaijlibxsmm_mpibaij_C", NULL));
55: #endif
56: PetscFunctionReturn(PETSC_SUCCESS);
57: }
59: /* defines MatSetValues_MPI_Hash(), MatAssemblyBegin_MPI_Hash(), and MatAssemblyEnd_MPI_Hash() */
60: #define TYPE BAIJ
61: #include "../src/mat/impls/aij/mpi/mpihashmat.h"
62: #undef TYPE
64: #if PetscDefined(HAVE_HYPRE)
65: PETSC_INTERN PetscErrorCode MatConvert_AIJ_HYPRE(Mat, MatType, MatReuse, Mat *);
66: #endif
68: static PetscErrorCode MatGetRowMaxAbs_MPIBAIJ(Mat A, Vec v, PetscInt idx[])
69: {
70: Mat_MPIBAIJ *a = (Mat_MPIBAIJ *)A->data;
71: PetscInt i, *idxb = NULL, m = A->rmap->n, bs = A->cmap->bs;
72: PetscScalar *vv;
73: Vec vB, vA;
74: const PetscScalar *va, *vb;
76: PetscFunctionBegin;
77: PetscCall(MatCreateVecs(a->A, NULL, &vA));
78: PetscCall(MatGetRowMaxAbs(a->A, vA, idx));
80: PetscCall(VecGetArrayRead(vA, &va));
81: if (idx) {
82: for (i = 0; i < m; i++) {
83: if (PetscAbsScalar(va[i])) idx[i] += A->cmap->rstart;
84: }
85: }
87: PetscCall(MatCreateVecs(a->B, NULL, &vB));
88: PetscCall(PetscMalloc1(m, &idxb));
89: PetscCall(MatGetRowMaxAbs(a->B, vB, idxb));
91: PetscCall(VecGetArrayWrite(v, &vv));
92: PetscCall(VecGetArrayRead(vB, &vb));
93: for (i = 0; i < m; i++) {
94: if (PetscAbsScalar(va[i]) < PetscAbsScalar(vb[i])) {
95: vv[i] = vb[i];
96: if (idx) idx[i] = bs * a->garray[idxb[i] / bs] + (idxb[i] % bs);
97: } else {
98: vv[i] = va[i];
99: if (idx && PetscAbsScalar(va[i]) == PetscAbsScalar(vb[i]) && idxb[i] != -1 && idx[i] > bs * a->garray[idxb[i] / bs] + (idxb[i] % bs)) idx[i] = bs * a->garray[idxb[i] / bs] + (idxb[i] % bs);
100: }
101: }
102: PetscCall(VecRestoreArrayWrite(v, &vv));
103: PetscCall(VecRestoreArrayRead(vA, &va));
104: PetscCall(VecRestoreArrayRead(vB, &vb));
105: PetscCall(PetscFree(idxb));
106: PetscCall(VecDestroy(&vA));
107: PetscCall(VecDestroy(&vB));
108: PetscFunctionReturn(PETSC_SUCCESS);
109: }
111: static PetscErrorCode MatGetRowSumAbs_MPIBAIJ(Mat A, Vec v)
112: {
113: Mat_MPIBAIJ *a = (Mat_MPIBAIJ *)A->data;
114: Vec vB, vA;
116: PetscFunctionBegin;
117: PetscCall(MatCreateVecs(a->A, NULL, &vA));
118: PetscCall(MatGetRowSumAbs(a->A, vA));
119: PetscCall(MatCreateVecs(a->B, NULL, &vB));
120: PetscCall(MatGetRowSumAbs(a->B, vB));
121: PetscCall(VecAXPY(vA, 1.0, vB));
122: PetscCall(VecDestroy(&vB));
123: PetscCall(VecCopy(vA, v));
124: PetscCall(VecDestroy(&vA));
125: PetscFunctionReturn(PETSC_SUCCESS);
126: }
128: static PetscErrorCode MatStoreValues_MPIBAIJ(Mat mat)
129: {
130: Mat_MPIBAIJ *aij = (Mat_MPIBAIJ *)mat->data;
132: PetscFunctionBegin;
133: PetscCall(MatStoreValues(aij->A));
134: PetscCall(MatStoreValues(aij->B));
135: PetscFunctionReturn(PETSC_SUCCESS);
136: }
138: static PetscErrorCode MatRetrieveValues_MPIBAIJ(Mat mat)
139: {
140: Mat_MPIBAIJ *aij = (Mat_MPIBAIJ *)mat->data;
142: PetscFunctionBegin;
143: PetscCall(MatRetrieveValues(aij->A));
144: PetscCall(MatRetrieveValues(aij->B));
145: PetscFunctionReturn(PETSC_SUCCESS);
146: }
148: /*
149: Local utility routine that creates a mapping from the global column
150: number to the local number in the off-diagonal part of the local
151: storage of the matrix. This is done in a non scalable way since the
152: length of colmap equals the global matrix length.
153: */
154: PetscErrorCode MatCreateColmap_MPIBAIJ_Private(Mat mat)
155: {
156: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ *)mat->data;
157: Mat_SeqBAIJ *B = (Mat_SeqBAIJ *)baij->B->data;
158: PetscInt nbs = B->nbs, i, bs = mat->rmap->bs;
160: PetscFunctionBegin;
161: #if PetscDefined(USE_CTABLE)
162: PetscCall(PetscHMapICreateWithSize(baij->nbs, &baij->colmap));
163: for (i = 0; i < nbs; i++) PetscCall(PetscHMapISet(baij->colmap, baij->garray[i] + 1, i * bs + 1));
164: #else
165: PetscCall(PetscCalloc1(baij->Nbs + 1, &baij->colmap));
166: for (i = 0; i < nbs; i++) baij->colmap[baij->garray[i]] = i * bs + 1;
167: #endif
168: PetscFunctionReturn(PETSC_SUCCESS);
169: }
171: #define MatSetValues_SeqBAIJ_A_Private(row, col, value, addv, orow, ocol) \
172: do { \
173: brow = row / bs; \
174: rp = PetscSafePointerPlusOffset(aj, ai[brow]); \
175: ap = PetscSafePointerPlusOffset(aa, bs2 * ai[brow]); \
176: rmax = aimax[brow]; \
177: nrow = ailen[brow]; \
178: bcol = col / bs; \
179: ridx = row % bs; \
180: cidx = col % bs; \
181: low = 0; \
182: high = nrow; \
183: while (high - low > 3) { \
184: t = (low + high) / 2; \
185: if (rp[t] > bcol) high = t; \
186: else low = t; \
187: } \
188: for (_i = low; _i < high; _i++) { \
189: if (rp[_i] > bcol) break; \
190: if (rp[_i] == bcol) { \
191: bap = ap + bs2 * _i + bs * cidx + ridx; \
192: if (addv == ADD_VALUES) *bap += value; \
193: else *bap = value; \
194: goto a_noinsert; \
195: } \
196: } \
197: if (a->nonew == 1) goto a_noinsert; \
198: 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); \
199: MatSeqXAIJReallocateAIJ(A, a->mbs, bs2, nrow, brow, bcol, rmax, aa, ai, aj, rp, ap, aimax, a->nonew, MatScalar); \
200: N = nrow++ - 1; \
201: /* shift up all the later entries in this row */ \
202: PetscCall(PetscArraymove(rp + _i + 1, rp + _i, N - _i + 1)); \
203: PetscCall(PetscArraymove(ap + bs2 * (_i + 1), ap + bs2 * _i, bs2 * (N - _i + 1))); \
204: PetscCall(PetscArrayzero(ap + bs2 * _i, bs2)); \
205: rp[_i] = bcol; \
206: ap[bs2 * _i + bs * cidx + ridx] = value; \
207: a_noinsert:; \
208: ailen[brow] = nrow; \
209: } while (0)
211: #define MatSetValues_SeqBAIJ_B_Private(row, col, value, addv, orow, ocol) \
212: do { \
213: brow = row / bs; \
214: rp = PetscSafePointerPlusOffset(bj, bi[brow]); \
215: ap = PetscSafePointerPlusOffset(ba, bs2 * bi[brow]); \
216: rmax = bimax[brow]; \
217: nrow = bilen[brow]; \
218: bcol = col / bs; \
219: ridx = row % bs; \
220: cidx = col % bs; \
221: low = 0; \
222: high = nrow; \
223: while (high - low > 3) { \
224: t = (low + high) / 2; \
225: if (rp[t] > bcol) high = t; \
226: else low = t; \
227: } \
228: for (_i = low; _i < high; _i++) { \
229: if (rp[_i] > bcol) break; \
230: if (rp[_i] == bcol) { \
231: bap = ap + bs2 * _i + bs * cidx + ridx; \
232: if (addv == ADD_VALUES) *bap += value; \
233: else *bap = value; \
234: goto b_noinsert; \
235: } \
236: } \
237: if (b->nonew == 1) goto b_noinsert; \
238: 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); \
239: MatSeqXAIJReallocateAIJ(B, b->mbs, bs2, nrow, brow, bcol, rmax, ba, bi, bj, rp, ap, bimax, b->nonew, MatScalar); \
240: N = nrow++ - 1; \
241: /* shift up all the later entries in this row */ \
242: PetscCall(PetscArraymove(rp + _i + 1, rp + _i, N - _i + 1)); \
243: PetscCall(PetscArraymove(ap + bs2 * (_i + 1), ap + bs2 * _i, bs2 * (N - _i + 1))); \
244: PetscCall(PetscArrayzero(ap + bs2 * _i, bs2)); \
245: rp[_i] = bcol; \
246: ap[bs2 * _i + bs * cidx + ridx] = value; \
247: b_noinsert:; \
248: bilen[brow] = nrow; \
249: } while (0)
251: PetscErrorCode MatSetValues_MPIBAIJ(Mat mat, PetscInt m, const PetscInt im[], PetscInt n, const PetscInt in[], const PetscScalar v[], InsertMode addv)
252: {
253: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ *)mat->data;
254: MatScalar value;
255: PetscBool roworiented = baij->roworiented;
256: PetscInt i, j, row, col;
257: PetscInt rstart_orig = mat->rmap->rstart;
258: PetscInt rend_orig = mat->rmap->rend, cstart_orig = mat->cmap->rstart;
259: PetscInt cend_orig = mat->cmap->rend, bs = mat->rmap->bs;
261: /* Some Variables required in the macro */
262: Mat A = baij->A;
263: Mat_SeqBAIJ *a = (Mat_SeqBAIJ *)A->data;
264: PetscInt *aimax = a->imax, *ai = a->i, *ailen = a->ilen, *aj = a->j;
265: MatScalar *aa = a->a;
267: Mat B = baij->B;
268: Mat_SeqBAIJ *b = (Mat_SeqBAIJ *)B->data;
269: PetscInt *bimax = b->imax, *bi = b->i, *bilen = b->ilen, *bj = b->j;
270: MatScalar *ba = b->a;
272: PetscInt *rp, ii, nrow, _i, rmax, N, brow, bcol;
273: PetscInt low, high, t, ridx, cidx, bs2 = a->bs2;
274: MatScalar *ap, *bap;
276: PetscFunctionBegin;
277: for (i = 0; i < m; i++) {
278: if (im[i] < 0) continue;
279: 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);
280: if (im[i] >= rstart_orig && im[i] < rend_orig) {
281: row = im[i] - rstart_orig;
282: for (j = 0; j < n; j++) {
283: if (in[j] >= cstart_orig && in[j] < cend_orig) {
284: col = in[j] - cstart_orig;
285: if (roworiented) value = v[i * n + j];
286: else value = v[i + j * m];
287: MatSetValues_SeqBAIJ_A_Private(row, col, value, addv, im[i], in[j]);
288: } else if (in[j] < 0) {
289: continue;
290: } else {
291: 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);
292: if (mat->was_assembled) {
293: if (!baij->colmap) PetscCall(MatCreateColmap_MPIBAIJ_Private(mat));
294: #if PetscDefined(USE_CTABLE)
295: PetscCall(PetscHMapIGetWithDefault(baij->colmap, in[j] / bs + 1, 0, &col));
296: col = col - 1;
297: #else
298: col = baij->colmap[in[j] / bs] - 1;
299: #endif
300: if (col < 0 && !((Mat_SeqBAIJ *)baij->B->data)->nonew) {
301: PetscCall(MatDisAssemble_MPIBAIJ(mat));
302: col = in[j];
303: /* Reinitialize the variables required by MatSetValues_SeqBAIJ_B_Private() */
304: B = baij->B;
305: b = (Mat_SeqBAIJ *)B->data;
306: bimax = b->imax;
307: bi = b->i;
308: bilen = b->ilen;
309: bj = b->j;
310: ba = b->a;
311: } else {
312: PetscCheck(col >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Inserting a new nonzero (%" PetscInt_FMT ", %" PetscInt_FMT ") into matrix", im[i], in[j]);
313: col += in[j] % bs;
314: }
315: } else col = in[j];
316: if (roworiented) value = v[i * n + j];
317: else value = v[i + j * m];
318: MatSetValues_SeqBAIJ_B_Private(row, col, value, addv, im[i], in[j]);
319: /* PetscCall(MatSetValues_SeqBAIJ(baij->B,1,&row,1,&col,&value,addv)); */
320: }
321: }
322: } else {
323: 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]);
324: if (!baij->donotstash) {
325: mat->assembled = PETSC_FALSE;
326: if (roworiented) {
327: PetscCall(MatStashValuesRow_Private(&mat->stash, im[i], n, in, v + i * n, PETSC_FALSE));
328: } else {
329: PetscCall(MatStashValuesCol_Private(&mat->stash, im[i], n, in, v + i, m, PETSC_FALSE));
330: }
331: }
332: }
333: }
334: PetscFunctionReturn(PETSC_SUCCESS);
335: }
337: static inline PetscErrorCode MatSetValuesBlocked_SeqBAIJ_Inlined(Mat A, PetscInt row, PetscInt col, const PetscScalar v[], InsertMode is, PetscInt orow, PetscInt ocol)
338: {
339: Mat_SeqBAIJ *a = (Mat_SeqBAIJ *)A->data;
340: PetscInt *rp, low, high, t, ii, jj, nrow, i, rmax, N;
341: PetscInt *imax = a->imax, *ai = a->i, *ailen = a->ilen;
342: PetscInt *aj = a->j, nonew = a->nonew, bs2 = a->bs2, bs = A->rmap->bs;
343: PetscBool roworiented = a->roworiented;
344: const PetscScalar *value = v;
345: MatScalar *ap, *aa = a->a, *bap;
347: PetscFunctionBegin;
348: rp = aj + ai[row];
349: ap = aa + bs2 * ai[row];
350: rmax = imax[row];
351: nrow = ailen[row];
352: value = v;
353: low = 0;
354: high = nrow;
355: while (high - low > 7) {
356: t = (low + high) / 2;
357: if (rp[t] > col) high = t;
358: else low = t;
359: }
360: for (i = low; i < high; i++) {
361: if (rp[i] > col) break;
362: if (rp[i] == col) {
363: bap = ap + bs2 * i;
364: if (roworiented) {
365: if (is == ADD_VALUES) {
366: for (ii = 0; ii < bs; ii++) {
367: for (jj = ii; jj < bs2; jj += bs) bap[jj] += *value++;
368: }
369: } else {
370: for (ii = 0; ii < bs; ii++) {
371: for (jj = ii; jj < bs2; jj += bs) bap[jj] = *value++;
372: }
373: }
374: } else {
375: if (is == ADD_VALUES) {
376: for (ii = 0; ii < bs; ii++, value += bs) {
377: for (jj = 0; jj < bs; jj++) bap[jj] += value[jj];
378: bap += bs;
379: }
380: } else {
381: for (ii = 0; ii < bs; ii++, value += bs) {
382: for (jj = 0; jj < bs; jj++) bap[jj] = value[jj];
383: bap += bs;
384: }
385: }
386: }
387: goto noinsert2;
388: }
389: }
390: if (nonew == 1) goto noinsert2;
391: 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);
392: MatSeqXAIJReallocateAIJ(A, a->mbs, bs2, nrow, row, col, rmax, aa, ai, aj, rp, ap, imax, nonew, MatScalar);
393: N = nrow++ - 1;
394: high++;
395: /* shift up all the later entries in this row */
396: PetscCall(PetscArraymove(rp + i + 1, rp + i, N - i + 1));
397: PetscCall(PetscArraymove(ap + bs2 * (i + 1), ap + bs2 * i, bs2 * (N - i + 1)));
398: rp[i] = col;
399: bap = ap + bs2 * i;
400: if (roworiented) {
401: for (ii = 0; ii < bs; ii++) {
402: for (jj = ii; jj < bs2; jj += bs) bap[jj] = *value++;
403: }
404: } else {
405: for (ii = 0; ii < bs; ii++) {
406: for (jj = 0; jj < bs; jj++) *bap++ = *value++;
407: }
408: }
409: noinsert2:;
410: ailen[row] = nrow;
411: PetscFunctionReturn(PETSC_SUCCESS);
412: }
414: /*
415: This routine should be optimized so that the block copy at ** Here a copy is required ** below is not needed
416: by passing additional stride information into the MatSetValuesBlocked_SeqBAIJ_Inlined() routine
417: */
418: static PetscErrorCode MatSetValuesBlocked_MPIBAIJ(Mat mat, PetscInt m, const PetscInt im[], PetscInt n, const PetscInt in[], const PetscScalar v[], InsertMode addv)
419: {
420: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ *)mat->data;
421: const PetscScalar *value;
422: MatScalar *barray = baij->barray;
423: PetscBool roworiented = baij->roworiented;
424: PetscInt i, j, ii, jj, row, col, rstart = baij->rstartbs;
425: PetscInt rend = baij->rendbs, cstart = baij->cstartbs, stepval;
426: PetscInt cend = baij->cendbs, bs = mat->rmap->bs, bs2 = baij->bs2;
428: PetscFunctionBegin;
429: if (!barray) {
430: PetscCall(PetscMalloc1(bs2, &barray));
431: baij->barray = barray;
432: }
434: if (roworiented) stepval = (n - 1) * bs;
435: else stepval = (m - 1) * bs;
437: for (i = 0; i < m; i++) {
438: if (im[i] < 0) continue;
439: 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);
440: if (im[i] >= rstart && im[i] < rend) {
441: row = im[i] - rstart;
442: for (j = 0; j < n; j++) {
443: /* If NumCol = 1 then a copy is not required */
444: if ((roworiented) && (n == 1)) {
445: barray = (MatScalar *)v + i * bs2;
446: } else if ((!roworiented) && (m == 1)) {
447: barray = (MatScalar *)v + j * bs2;
448: } else { /* Here a copy is required */
449: if (roworiented) {
450: value = v + (i * (stepval + bs) + j) * bs;
451: } else {
452: value = v + (j * (stepval + bs) + i) * bs;
453: }
454: for (ii = 0; ii < bs; ii++, value += bs + stepval) {
455: for (jj = 0; jj < bs; jj++) barray[jj] = value[jj];
456: barray += bs;
457: }
458: barray -= bs2;
459: }
461: if (in[j] >= cstart && in[j] < cend) {
462: col = in[j] - cstart;
463: PetscCall(MatSetValuesBlocked_SeqBAIJ_Inlined(baij->A, row, col, barray, addv, im[i], in[j]));
464: } else if (in[j] < 0) {
465: continue;
466: } else {
467: 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);
468: if (mat->was_assembled) {
469: if (!baij->colmap) PetscCall(MatCreateColmap_MPIBAIJ_Private(mat));
471: #if PetscDefined(USE_CTABLE)
472: PetscCall(PetscHMapIGetWithDefault(baij->colmap, in[j] + 1, 0, &col));
473: col = col < 1 ? -1 : (col - 1) / bs;
474: #else
475: col = baij->colmap[in[j]] < 1 ? -1 : (baij->colmap[in[j]] - 1) / bs;
476: #endif
477: if (col < 0 && !((Mat_SeqBAIJ *)baij->B->data)->nonew) {
478: PetscCall(MatDisAssemble_MPIBAIJ(mat));
479: col = in[j];
480: } else PetscCheck(col >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Inserting a new blocked indexed nonzero block (%" PetscInt_FMT ", %" PetscInt_FMT ") into matrix", im[i], in[j]);
481: } else col = in[j];
482: PetscCall(MatSetValuesBlocked_SeqBAIJ_Inlined(baij->B, row, col, barray, addv, im[i], in[j]));
483: }
484: }
485: } else {
486: 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]);
487: if (!baij->donotstash) {
488: if (roworiented) {
489: PetscCall(MatStashValuesRowBlocked_Private(&mat->bstash, im[i], n, in, v, m, n, i));
490: } else {
491: PetscCall(MatStashValuesColBlocked_Private(&mat->bstash, im[i], n, in, v, m, n, i));
492: }
493: }
494: }
495: }
496: PetscFunctionReturn(PETSC_SUCCESS);
497: }
499: #define HASH_KEY 0.6180339887
500: #define HASH(size, key, tmp) (tmp = (key) * HASH_KEY, (PetscInt)((size) * (tmp - (PetscInt)tmp)))
501: /* #define HASH(size,key) ((PetscInt)((size)*fmod(((key)*HASH_KEY),1))) */
502: /* #define HASH(size,key,tmp) ((PetscInt)((size)*fmod(((key)*HASH_KEY),1))) */
503: static PetscErrorCode MatSetValues_MPIBAIJ_HT(Mat mat, PetscInt m, const PetscInt im[], PetscInt n, const PetscInt in[], const PetscScalar v[], InsertMode addv)
504: {
505: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ *)mat->data;
506: PetscBool roworiented = baij->roworiented;
507: PetscInt i, j, row, col;
508: PetscInt rstart_orig = mat->rmap->rstart;
509: PetscInt rend_orig = mat->rmap->rend, Nbs = baij->Nbs;
510: PetscInt h1, key, size = baij->ht_size, bs = mat->rmap->bs, *HT = baij->ht, idx;
511: PetscReal tmp;
512: MatScalar **HD = baij->hd, value;
513: PetscInt total_ct = baij->ht_total_ct, insert_ct = baij->ht_insert_ct;
515: PetscFunctionBegin;
516: for (i = 0; i < m; i++) {
517: if (PetscDefined(USE_DEBUG)) {
518: PetscCheck(im[i] >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Negative row");
519: 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);
520: }
521: row = im[i];
522: if (row >= rstart_orig && row < rend_orig) {
523: for (j = 0; j < n; j++) {
524: col = in[j];
525: if (roworiented) value = v[i * n + j];
526: else value = v[i + j * m];
527: /* Look up PetscInto the Hash Table */
528: key = (row / bs) * Nbs + (col / bs) + 1;
529: h1 = HASH(size, key, tmp);
531: idx = h1;
532: if (PetscDefined(USE_DEBUG)) {
533: insert_ct++;
534: total_ct++;
535: if (HT[idx] != key) {
536: for (idx = h1; (idx < size) && (HT[idx] != key); idx++, total_ct++);
537: if (idx == size) {
538: for (idx = 0; (idx < h1) && (HT[idx] != key); idx++, total_ct++);
539: PetscCheck(idx != h1, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "(%" PetscInt_FMT ",%" PetscInt_FMT ") has no entry in the hash table", row, col);
540: }
541: }
542: } else if (HT[idx] != key) {
543: for (idx = h1; (idx < size) && (HT[idx] != key); idx++);
544: if (idx == size) {
545: for (idx = 0; (idx < h1) && (HT[idx] != key); idx++);
546: PetscCheck(idx != h1, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "(%" PetscInt_FMT ",%" PetscInt_FMT ") has no entry in the hash table", row, col);
547: }
548: }
549: /* A HASH table entry is found, so insert the values at the correct address */
550: if (addv == ADD_VALUES) *(HD[idx] + (col % bs) * bs + (row % bs)) += value;
551: else *(HD[idx] + (col % bs) * bs + (row % bs)) = value;
552: }
553: } else if (!baij->donotstash) {
554: if (roworiented) {
555: PetscCall(MatStashValuesRow_Private(&mat->stash, im[i], n, in, v + i * n, PETSC_FALSE));
556: } else {
557: PetscCall(MatStashValuesCol_Private(&mat->stash, im[i], n, in, v + i, m, PETSC_FALSE));
558: }
559: }
560: }
561: if (PetscDefined(USE_DEBUG)) {
562: baij->ht_total_ct += total_ct;
563: baij->ht_insert_ct += insert_ct;
564: }
565: PetscFunctionReturn(PETSC_SUCCESS);
566: }
568: static PetscErrorCode MatSetValuesBlocked_MPIBAIJ_HT(Mat mat, PetscInt m, const PetscInt im[], PetscInt n, const PetscInt in[], const PetscScalar v[], InsertMode addv)
569: {
570: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ *)mat->data;
571: PetscBool roworiented = baij->roworiented;
572: PetscInt i, j, ii, jj, row, col;
573: PetscInt rstart = baij->rstartbs;
574: PetscInt rend = mat->rmap->rend, stepval, bs = mat->rmap->bs, bs2 = baij->bs2, nbs2 = n * bs2;
575: PetscInt h1, key, size = baij->ht_size, idx, *HT = baij->ht, Nbs = baij->Nbs;
576: PetscReal tmp;
577: MatScalar **HD = baij->hd, *baij_a;
578: const PetscScalar *v_t, *value;
579: PetscInt total_ct = baij->ht_total_ct, insert_ct = baij->ht_insert_ct;
581: PetscFunctionBegin;
582: if (roworiented) stepval = (n - 1) * bs;
583: else stepval = (m - 1) * bs;
585: for (i = 0; i < m; i++) {
586: if (PetscDefined(USE_DEBUG)) {
587: PetscCheck(im[i] >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Negative row: %" PetscInt_FMT, im[i]);
588: PetscCheck(im[i] < baij->Mbs, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Row too large: row %" PetscInt_FMT " max %" PetscInt_FMT, im[i], baij->Mbs - 1);
589: }
590: row = im[i];
591: v_t = v + i * nbs2;
592: if (row >= rstart && row < rend) {
593: for (j = 0; j < n; j++) {
594: col = in[j];
596: /* Look up into the Hash Table */
597: key = row * Nbs + col + 1;
598: h1 = HASH(size, key, tmp);
600: idx = h1;
601: if (PetscDefined(USE_DEBUG)) {
602: total_ct++;
603: insert_ct++;
604: if (HT[idx] != key) {
605: for (idx = h1; (idx < size) && (HT[idx] != key); idx++, total_ct++);
606: if (idx == size) {
607: for (idx = 0; (idx < h1) && (HT[idx] != key); idx++, total_ct++);
608: PetscCheck(idx != h1, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "(%" PetscInt_FMT ",%" PetscInt_FMT ") has no entry in the hash table", row, col);
609: }
610: }
611: } else if (HT[idx] != key) {
612: for (idx = h1; (idx < size) && (HT[idx] != key); idx++);
613: if (idx == size) {
614: for (idx = 0; (idx < h1) && (HT[idx] != key); idx++);
615: PetscCheck(idx != h1, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "(%" PetscInt_FMT ",%" PetscInt_FMT ") has no entry in the hash table", row, col);
616: }
617: }
618: baij_a = HD[idx];
619: if (roworiented) {
620: /*value = v + i*(stepval+bs)*bs + j*bs;*/
621: /* value = v + (i*(stepval+bs)+j)*bs; */
622: value = v_t;
623: v_t += bs;
624: if (addv == ADD_VALUES) {
625: for (ii = 0; ii < bs; ii++, value += stepval) {
626: for (jj = ii; jj < bs2; jj += bs) baij_a[jj] += *value++;
627: }
628: } else {
629: for (ii = 0; ii < bs; ii++, value += stepval) {
630: for (jj = ii; jj < bs2; jj += bs) baij_a[jj] = *value++;
631: }
632: }
633: } else {
634: value = v + j * (stepval + bs) * bs + i * bs;
635: if (addv == ADD_VALUES) {
636: for (ii = 0; ii < bs; ii++, value += stepval, baij_a += bs) {
637: for (jj = 0; jj < bs; jj++) baij_a[jj] += *value++;
638: }
639: } else {
640: for (ii = 0; ii < bs; ii++, value += stepval, baij_a += bs) {
641: for (jj = 0; jj < bs; jj++) baij_a[jj] = *value++;
642: }
643: }
644: }
645: }
646: } else {
647: if (!baij->donotstash) {
648: if (roworiented) {
649: PetscCall(MatStashValuesRowBlocked_Private(&mat->bstash, im[i], n, in, v, m, n, i));
650: } else {
651: PetscCall(MatStashValuesColBlocked_Private(&mat->bstash, im[i], n, in, v, m, n, i));
652: }
653: }
654: }
655: }
656: if (PetscDefined(USE_DEBUG)) {
657: baij->ht_total_ct += total_ct;
658: baij->ht_insert_ct += insert_ct;
659: }
660: PetscFunctionReturn(PETSC_SUCCESS);
661: }
663: static PetscErrorCode MatGetValues_MPIBAIJ(Mat mat, PetscInt m, const PetscInt idxm[], PetscInt n, const PetscInt idxn[], PetscScalar v[])
664: {
665: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ *)mat->data;
666: PetscInt bs = mat->rmap->bs, i, j, bsrstart = mat->rmap->rstart, bsrend = mat->rmap->rend;
667: PetscInt bscstart = mat->cmap->rstart, bscend = mat->cmap->rend, row, col, data;
668: PetscBool roworiented = baij->roworiented;
669: PetscScalar *value;
671: PetscFunctionBegin;
672: for (i = 0; i < m; i++) {
673: if (idxm[i] < 0) continue; /* negative row */
674: 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);
675: PetscCheck(idxm[i] >= bsrstart && idxm[i] < bsrend, PETSC_COMM_SELF, PETSC_ERR_SUP, "Only local values currently supported");
676: row = idxm[i] - bsrstart;
677: for (j = 0; j < n; j++) {
678: if (idxn[j] < 0) continue; /* negative column */
679: 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);
680: value = roworiented ? &v[j + i * n] : &v[i + j * m];
681: if (idxn[j] >= bscstart && idxn[j] < bscend) {
682: col = idxn[j] - bscstart;
683: PetscCall(MatGetValues_SeqBAIJ(baij->A, 1, &row, 1, &col, value));
684: } else {
685: if (!baij->colmap) PetscCall(MatCreateColmap_MPIBAIJ_Private(mat));
686: #if PetscDefined(USE_CTABLE)
687: PetscCall(PetscHMapIGetWithDefault(baij->colmap, idxn[j] / bs + 1, 0, &data));
688: data--;
689: #else
690: data = baij->colmap[idxn[j] / bs] - 1;
691: #endif
692: if (data < 0 || baij->garray[data / bs] != idxn[j] / bs) *value = 0.0;
693: else {
694: col = data + idxn[j] % bs;
695: PetscCall(MatGetValues_SeqBAIJ(baij->B, 1, &row, 1, &col, value));
696: }
697: }
698: }
699: }
700: PetscFunctionReturn(PETSC_SUCCESS);
701: }
703: static PetscErrorCode MatNorm_MPIBAIJ(Mat mat, NormType type, PetscReal *nrm)
704: {
705: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ *)mat->data;
706: Mat_SeqBAIJ *amat = (Mat_SeqBAIJ *)baij->A->data, *bmat = (Mat_SeqBAIJ *)baij->B->data;
707: PetscInt i, j, bs2 = baij->bs2, bs = baij->A->rmap->bs, nz, row, col;
708: PetscReal sum = 0.0;
709: MatScalar *v;
711: PetscFunctionBegin;
712: if (baij->size == 1) {
713: PetscCall(MatNorm(baij->A, type, nrm));
714: } else {
715: if (type == NORM_FROBENIUS) {
716: v = amat->a;
717: nz = amat->nz * bs2;
718: for (i = 0; i < nz; i++) {
719: sum += PetscRealPart(PetscConj(*v) * (*v));
720: v++;
721: }
722: v = bmat->a;
723: nz = bmat->nz * bs2;
724: for (i = 0; i < nz; i++) {
725: sum += PetscRealPart(PetscConj(*v) * (*v));
726: v++;
727: }
728: PetscCallMPI(MPIU_Allreduce(&sum, nrm, 1, MPIU_REAL, MPIU_SUM, PetscObjectComm((PetscObject)mat)));
729: *nrm = PetscSqrtReal(*nrm);
730: } else if (type == NORM_1) { /* max column sum */
731: Vec col, bcol;
732: PetscScalar *array;
733: PetscInt *jj, *garray = baij->garray;
735: PetscCall(MatCreateVecs(mat, &col, NULL));
736: PetscCall(VecGetArrayWrite(col, &array));
737: v = amat->a;
738: jj = amat->j;
739: for (i = 0; i < amat->nz; i++) {
740: for (j = 0; j < bs; j++) {
741: PetscInt col = bs * *jj + j; /* column index */
743: for (row = 0; row < bs; row++) array[col] += PetscAbsScalar(*v++);
744: }
745: jj++;
746: }
747: PetscCall(VecRestoreArrayWrite(col, &array));
748: PetscCall(MatCreateVecs(baij->B, &bcol, NULL));
749: PetscCall(VecGetArrayWrite(bcol, &array));
750: v = bmat->a;
751: jj = bmat->j;
752: for (i = 0; i < bmat->nz; i++) {
753: for (j = 0; j < bs; j++) {
754: PetscInt col = bs * *jj + j; /* column index */
756: for (row = 0; row < bs; row++) array[col] += PetscAbsScalar(*v++);
757: }
758: jj++;
759: }
760: PetscCall(VecSetValuesBlocked(col, bmat->nbs, garray, array, ADD_VALUES));
761: PetscCall(VecRestoreArrayWrite(bcol, &array));
762: PetscCall(VecDestroy(&bcol));
763: PetscCall(VecAssemblyBegin(col));
764: PetscCall(VecAssemblyEnd(col));
765: PetscCall(VecNorm(col, NORM_INFINITY, nrm));
766: PetscCall(VecDestroy(&col));
767: } else if (type == NORM_INFINITY) { /* max row sum */
768: PetscReal *sums;
769: PetscCall(PetscMalloc1(bs, &sums));
770: sum = 0.0;
771: for (j = 0; j < amat->mbs; j++) {
772: for (row = 0; row < bs; row++) sums[row] = 0.0;
773: v = amat->a + bs2 * amat->i[j];
774: nz = amat->i[j + 1] - amat->i[j];
775: for (i = 0; i < nz; i++) {
776: for (col = 0; col < bs; col++) {
777: for (row = 0; row < bs; row++) {
778: sums[row] += PetscAbsScalar(*v);
779: v++;
780: }
781: }
782: }
783: v = bmat->a + bs2 * bmat->i[j];
784: nz = bmat->i[j + 1] - bmat->i[j];
785: for (i = 0; i < nz; i++) {
786: for (col = 0; col < bs; col++) {
787: for (row = 0; row < bs; row++) {
788: sums[row] += PetscAbsScalar(*v);
789: v++;
790: }
791: }
792: }
793: for (row = 0; row < bs; row++) {
794: if (sums[row] > sum) sum = sums[row];
795: }
796: }
797: PetscCallMPI(MPIU_Allreduce(&sum, nrm, 1, MPIU_REAL, MPIU_MAX, PetscObjectComm((PetscObject)mat)));
798: PetscCall(PetscFree(sums));
799: } else SETERRQ(PetscObjectComm((PetscObject)mat), PETSC_ERR_SUP, "No support for this norm yet");
800: }
801: PetscFunctionReturn(PETSC_SUCCESS);
802: }
804: /*
805: Creates the hash table, and sets the table
806: This table is created only once.
807: If new entries need to be added to the matrix
808: then the hash table has to be destroyed and
809: recreated.
810: */
811: static PetscErrorCode MatCreateHashTable_MPIBAIJ_Private(Mat mat, PetscReal factor)
812: {
813: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ *)mat->data;
814: Mat A = baij->A, B = baij->B;
815: Mat_SeqBAIJ *a = (Mat_SeqBAIJ *)A->data, *b = (Mat_SeqBAIJ *)B->data;
816: PetscInt i, j, k, nz = a->nz + b->nz, h1, *ai = a->i, *aj = a->j, *bi = b->i, *bj = b->j;
817: PetscInt ht_size, bs2 = baij->bs2, rstart = baij->rstartbs;
818: PetscInt cstart = baij->cstartbs, *garray = baij->garray, row, col, Nbs = baij->Nbs;
819: PetscInt *HT, key;
820: MatScalar **HD;
821: PetscReal tmp;
822: PetscInt ct = 0, max = 0;
824: PetscFunctionBegin;
825: if (baij->ht) PetscFunctionReturn(PETSC_SUCCESS);
827: baij->ht_size = (PetscInt)(factor * nz);
828: ht_size = baij->ht_size;
830: /* Allocate Memory for Hash Table */
831: PetscCall(PetscCalloc2(ht_size, &baij->hd, ht_size, &baij->ht));
832: HD = baij->hd;
833: HT = baij->ht;
835: /* Loop Over A */
836: for (i = 0; i < a->mbs; i++) {
837: for (j = ai[i]; j < ai[i + 1]; j++) {
838: row = i + rstart;
839: col = aj[j] + cstart;
841: key = row * Nbs + col + 1;
842: h1 = HASH(ht_size, key, tmp);
843: for (k = 0; k < ht_size; k++) {
844: if (!HT[(h1 + k) % ht_size]) {
845: HT[(h1 + k) % ht_size] = key;
846: HD[(h1 + k) % ht_size] = a->a + j * bs2;
847: break;
848: } else if (PetscDefined(USE_INFO)) ct++;
849: }
850: if (PetscDefined(USE_INFO) && k > max) max = k;
851: }
852: }
853: /* Loop Over B */
854: for (i = 0; i < b->mbs; i++) {
855: for (j = bi[i]; j < bi[i + 1]; j++) {
856: row = i + rstart;
857: col = garray[bj[j]];
858: key = row * Nbs + col + 1;
859: h1 = HASH(ht_size, key, tmp);
860: for (k = 0; k < ht_size; k++) {
861: if (!HT[(h1 + k) % ht_size]) {
862: HT[(h1 + k) % ht_size] = key;
863: HD[(h1 + k) % ht_size] = b->a + j * bs2;
864: break;
865: } else if (PetscDefined(USE_INFO)) ct++;
866: }
867: if (PetscDefined(USE_INFO) && k > max) max = k;
868: }
869: }
871: /* Print Summary */
872: if (PetscDefined(USE_INFO)) {
873: for (i = 0, j = 0; i < ht_size; i++) {
874: if (HT[i]) j++;
875: }
876: PetscCall(PetscInfo(mat, "Average Search = %5.2g,max search = %" PetscInt_FMT "\n", (!j) ? 0.0 : (double)(((PetscReal)(ct + j)) / j), max));
877: }
878: PetscFunctionReturn(PETSC_SUCCESS);
879: }
881: static PetscErrorCode MatAssemblyBegin_MPIBAIJ(Mat mat, MatAssemblyType mode)
882: {
883: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ *)mat->data;
884: PetscInt nstash, reallocs;
886: PetscFunctionBegin;
887: if (baij->donotstash || mat->nooffprocentries) PetscFunctionReturn(PETSC_SUCCESS);
889: PetscCall(MatStashScatterBegin_Private(mat, &mat->stash, mat->rmap->range));
890: PetscCall(MatStashScatterBegin_Private(mat, &mat->bstash, baij->rangebs));
891: PetscCall(MatStashGetInfo_Private(&mat->stash, &nstash, &reallocs));
892: PetscCall(PetscInfo(mat, "Stash has %" PetscInt_FMT " entries, uses %" PetscInt_FMT " mallocs.\n", nstash, reallocs));
893: PetscCall(MatStashGetInfo_Private(&mat->bstash, &nstash, &reallocs));
894: PetscCall(PetscInfo(mat, "Block-Stash has %" PetscInt_FMT " entries, uses %" PetscInt_FMT " mallocs.\n", nstash, reallocs));
895: PetscFunctionReturn(PETSC_SUCCESS);
896: }
898: static PetscErrorCode MatAssemblyEnd_MPIBAIJ(Mat mat, MatAssemblyType mode)
899: {
900: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ *)mat->data;
901: Mat_SeqBAIJ *a = (Mat_SeqBAIJ *)baij->A->data;
902: PetscInt i, j, rstart, ncols, flg, bs2 = baij->bs2;
903: PetscInt *row, *col;
904: PetscBool r1, r2, r3, all_assembled;
905: MatScalar *val;
906: PetscMPIInt n;
908: PetscFunctionBegin;
909: /* do not use 'b=(Mat_SeqBAIJ*)baij->B->data' as B can be reset in disassembly */
910: if (!baij->donotstash && !mat->nooffprocentries) {
911: while (1) {
912: PetscCall(MatStashScatterGetMesg_Private(&mat->stash, &n, &row, &col, &val, &flg));
913: if (!flg) break;
915: for (i = 0; i < n;) {
916: /* Now identify the consecutive vals belonging to the same row */
917: for (j = i, rstart = row[j]; j < n; j++) {
918: if (row[j] != rstart) break;
919: }
920: if (j < n) ncols = j - i;
921: else ncols = n - i;
922: /* Now assemble all these values with a single function call */
923: PetscCall(MatSetValues_MPIBAIJ(mat, 1, row + i, ncols, col + i, val + i, mat->insertmode));
924: i = j;
925: }
926: }
927: PetscCall(MatStashScatterEnd_Private(&mat->stash));
928: /* Now process the block-stash. Since the values are stashed column-oriented,
929: set the row-oriented flag to column-oriented, and after MatSetValues()
930: restore the original flags */
931: r1 = baij->roworiented;
932: r2 = a->roworiented;
933: r3 = ((Mat_SeqBAIJ *)baij->B->data)->roworiented;
935: baij->roworiented = PETSC_FALSE;
936: a->roworiented = PETSC_FALSE;
937: ((Mat_SeqBAIJ *)baij->B->data)->roworiented = PETSC_FALSE;
938: while (1) {
939: PetscCall(MatStashScatterGetMesg_Private(&mat->bstash, &n, &row, &col, &val, &flg));
940: if (!flg) break;
942: for (i = 0; i < n;) {
943: /* Now identify the consecutive vals belonging to the same row */
944: for (j = i, rstart = row[j]; j < n; j++) {
945: if (row[j] != rstart) break;
946: }
947: if (j < n) ncols = j - i;
948: else ncols = n - i;
949: PetscCall(MatSetValuesBlocked_MPIBAIJ(mat, 1, row + i, ncols, col + i, val + i * bs2, mat->insertmode));
950: i = j;
951: }
952: }
953: PetscCall(MatStashScatterEnd_Private(&mat->bstash));
955: baij->roworiented = r1;
956: a->roworiented = r2;
957: ((Mat_SeqBAIJ *)baij->B->data)->roworiented = r3;
958: }
960: PetscCall(MatAssemblyBegin(baij->A, mode));
961: PetscCall(MatAssemblyEnd(baij->A, mode));
963: /* determine if any process has disassembled, if so we must
964: also disassemble ourselves, in order that we may reassemble. */
965: /*
966: if nonzero structure of submatrix B cannot change then we know that
967: no process disassembled thus we can skip this stuff
968: */
969: if (!((Mat_SeqBAIJ *)baij->B->data)->nonew) {
970: PetscCallMPI(MPIU_Allreduce(&mat->was_assembled, &all_assembled, 1, MPI_C_BOOL, MPI_LAND, PetscObjectComm((PetscObject)mat)));
971: if (mat->was_assembled && !all_assembled) PetscCall(MatDisAssemble_MPIBAIJ(mat));
972: }
974: if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) PetscCall(MatSetUpMultiply_MPIBAIJ(mat));
975: PetscCall(MatAssemblyBegin(baij->B, mode));
976: PetscCall(MatAssemblyEnd(baij->B, mode));
978: if (PetscDefined(USE_INFO) && baij->ht && mode == MAT_FINAL_ASSEMBLY) {
979: PetscCall(PetscInfo(mat, "Average Hash Table Search in MatSetValues = %5.2f\n", (double)((PetscReal)baij->ht_total_ct) / baij->ht_insert_ct));
981: baij->ht_total_ct = 0;
982: baij->ht_insert_ct = 0;
983: }
984: if (baij->ht_flag && !baij->ht && mode == MAT_FINAL_ASSEMBLY) {
985: PetscCall(MatCreateHashTable_MPIBAIJ_Private(mat, baij->ht_fact));
987: mat->ops->setvalues = MatSetValues_MPIBAIJ_HT;
988: mat->ops->setvaluesblocked = MatSetValuesBlocked_MPIBAIJ_HT;
989: }
991: PetscCall(PetscFree2(baij->rowvalues, baij->rowindices));
993: baij->rowvalues = NULL;
995: /* if no new nonzero locations are allowed in matrix then only set the matrix state the first time through */
996: if ((!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) || !((Mat_SeqBAIJ *)baij->A->data)->nonew) {
997: mat->nonzerostate = baij->A->nonzerostate + baij->B->nonzerostate;
998: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &mat->nonzerostate, 1, MPIU_INT64, MPI_SUM, PetscObjectComm((PetscObject)mat)));
999: }
1000: PetscFunctionReturn(PETSC_SUCCESS);
1001: }
1003: #include <petscdraw.h>
1004: static PetscErrorCode MatView_MPIBAIJ_ASCIIorDraworSocket(Mat mat, PetscViewer viewer)
1005: {
1006: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ *)mat->data;
1007: PetscMPIInt rank = baij->rank;
1008: PetscInt bs = mat->rmap->bs;
1009: PetscBool isascii, isdraw;
1010: PetscViewer sviewer;
1011: PetscViewerFormat format;
1013: PetscFunctionBegin;
1014: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
1015: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
1016: if (isascii) {
1017: PetscCall(PetscViewerGetFormat(viewer, &format));
1018: if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
1019: MatInfo info;
1020: PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)mat), &rank));
1021: PetscCall(MatGetInfo(mat, MAT_LOCAL, &info));
1022: PetscCall(PetscViewerASCIIPushSynchronized(viewer));
1023: 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,
1024: mat->rmap->bs, info.memory));
1025: PetscCall(MatGetInfo(baij->A, MAT_LOCAL, &info));
1026: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "[%d] on-diagonal part: nz %" PetscInt_FMT " \n", rank, (PetscInt)info.nz_used));
1027: PetscCall(MatGetInfo(baij->B, MAT_LOCAL, &info));
1028: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "[%d] off-diagonal part: nz %" PetscInt_FMT " \n", rank, (PetscInt)info.nz_used));
1029: PetscCall(PetscViewerFlush(viewer));
1030: PetscCall(PetscViewerASCIIPopSynchronized(viewer));
1031: PetscCall(PetscViewerASCIIPrintf(viewer, "Information on VecScatter used in matrix-vector product: \n"));
1032: PetscCall(VecScatterView(baij->Mvctx, viewer));
1033: PetscFunctionReturn(PETSC_SUCCESS);
1034: } else if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_FACTOR_INFO) PetscFunctionReturn(PETSC_SUCCESS);
1035: }
1037: if (isdraw) {
1038: PetscDraw draw;
1039: PetscBool isnull;
1040: PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw));
1041: PetscCall(PetscDrawIsNull(draw, &isnull));
1042: if (isnull) PetscFunctionReturn(PETSC_SUCCESS);
1043: }
1045: {
1046: /* assemble the entire matrix onto first processor. */
1047: Mat A;
1048: Mat_SeqBAIJ *Aloc;
1049: PetscInt M = mat->rmap->N, N = mat->cmap->N, *ai, *aj, col, i, j, k, *rvals, mbs = baij->mbs;
1050: MatScalar *a;
1051: const char *matname;
1053: /* Here we are creating a temporary matrix, so will assume MPIBAIJ is acceptable */
1054: /* Perhaps this should be the type of mat? */
1055: PetscCall(MatCreate(PetscObjectComm((PetscObject)mat), &A));
1056: if (rank == 0) {
1057: PetscCall(MatSetSizes(A, M, N, M, N));
1058: } else {
1059: PetscCall(MatSetSizes(A, 0, 0, M, N));
1060: }
1061: PetscCall(MatSetType(A, MATMPIBAIJ));
1062: PetscCall(MatMPIBAIJSetPreallocation(A, mat->rmap->bs, 0, NULL, 0, NULL));
1063: PetscCall(MatSetOption(A, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_FALSE));
1065: /* copy over the A part */
1066: Aloc = (Mat_SeqBAIJ *)baij->A->data;
1067: ai = Aloc->i;
1068: aj = Aloc->j;
1069: a = Aloc->a;
1070: PetscCall(PetscMalloc1(bs, &rvals));
1072: for (i = 0; i < mbs; i++) {
1073: rvals[0] = bs * (baij->rstartbs + i);
1074: for (j = 1; j < bs; j++) rvals[j] = rvals[j - 1] + 1;
1075: for (j = ai[i]; j < ai[i + 1]; j++) {
1076: col = (baij->cstartbs + aj[j]) * bs;
1077: for (k = 0; k < bs; k++) {
1078: PetscCall(MatSetValues_MPIBAIJ(A, bs, rvals, 1, &col, a, INSERT_VALUES));
1079: col++;
1080: a += bs;
1081: }
1082: }
1083: }
1084: /* copy over the B part */
1085: Aloc = (Mat_SeqBAIJ *)baij->B->data;
1086: ai = Aloc->i;
1087: aj = Aloc->j;
1088: a = Aloc->a;
1089: for (i = 0; i < mbs; i++) {
1090: rvals[0] = bs * (baij->rstartbs + i);
1091: for (j = 1; j < bs; j++) rvals[j] = rvals[j - 1] + 1;
1092: for (j = ai[i]; j < ai[i + 1]; j++) {
1093: col = baij->garray[aj[j]] * bs;
1094: for (k = 0; k < bs; k++) {
1095: PetscCall(MatSetValues_MPIBAIJ(A, bs, rvals, 1, &col, a, INSERT_VALUES));
1096: col++;
1097: a += bs;
1098: }
1099: }
1100: }
1101: PetscCall(PetscFree(rvals));
1102: PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
1103: PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
1104: /*
1105: Everyone has to call to draw the matrix since the graphics waits are
1106: synchronized across all processors that share the PetscDraw object
1107: */
1108: PetscCall(PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &sviewer));
1109: if (((PetscObject)mat)->name) PetscCall(PetscObjectGetName((PetscObject)mat, &matname));
1110: if (rank == 0) {
1111: if (((PetscObject)mat)->name) PetscCall(PetscObjectSetName((PetscObject)((Mat_MPIBAIJ *)A->data)->A, matname));
1112: PetscCall(MatView_SeqBAIJ(((Mat_MPIBAIJ *)A->data)->A, sviewer));
1113: }
1114: PetscCall(PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &sviewer));
1115: PetscCall(MatDestroy(&A));
1116: }
1117: PetscFunctionReturn(PETSC_SUCCESS);
1118: }
1120: /* Used for both MPIBAIJ and MPISBAIJ matrices */
1121: PetscErrorCode MatView_MPIBAIJ_Binary(Mat mat, PetscViewer viewer)
1122: {
1123: Mat_MPIBAIJ *aij = (Mat_MPIBAIJ *)mat->data;
1124: Mat_SeqBAIJ *A = (Mat_SeqBAIJ *)aij->A->data;
1125: Mat_SeqBAIJ *B = (Mat_SeqBAIJ *)aij->B->data;
1126: const PetscInt *garray = aij->garray;
1127: PetscInt header[4], M, N, m, rs, cs, bs, cnt, i, j, ja, jb, k, l;
1128: PetscCount nz, hnz;
1129: PetscInt *rowlens, *colidxs;
1130: PetscScalar *matvals;
1131: PetscMPIInt rank;
1133: PetscFunctionBegin;
1134: PetscCall(PetscViewerSetUp(viewer));
1136: M = mat->rmap->N;
1137: N = mat->cmap->N;
1138: m = mat->rmap->n;
1139: rs = mat->rmap->rstart;
1140: cs = mat->cmap->rstart;
1141: bs = mat->rmap->bs;
1142: nz = bs * bs * (A->nz + B->nz);
1144: /* write matrix header */
1145: header[0] = MAT_FILE_CLASSID;
1146: header[1] = M;
1147: header[2] = N;
1148: PetscCallMPI(MPI_Reduce(&nz, &hnz, 1, MPIU_COUNT, MPI_SUM, 0, PetscObjectComm((PetscObject)mat)));
1149: PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)mat), &rank));
1150: if (rank == 0) PetscCall(PetscIntCast(hnz, &header[3]));
1151: PetscCall(PetscViewerBinaryWrite(viewer, header, 4, PETSC_INT));
1153: /* fill in and store row lengths */
1154: PetscCall(PetscMalloc1(m, &rowlens));
1155: for (cnt = 0, i = 0; i < A->mbs; i++)
1156: for (j = 0; j < bs; j++) rowlens[cnt++] = bs * (A->i[i + 1] - A->i[i] + B->i[i + 1] - B->i[i]);
1157: PetscCall(PetscViewerBinaryWriteAll(viewer, rowlens, m, rs, M, PETSC_INT));
1158: PetscCall(PetscFree(rowlens));
1160: /* fill in and store column indices */
1161: PetscCall(PetscMalloc1(nz, &colidxs));
1162: for (cnt = 0, i = 0; i < A->mbs; i++) {
1163: for (k = 0; k < bs; k++) {
1164: for (jb = B->i[i]; jb < B->i[i + 1]; jb++) {
1165: if (garray[B->j[jb]] > cs / bs) break;
1166: for (l = 0; l < bs; l++) colidxs[cnt++] = bs * garray[B->j[jb]] + l;
1167: }
1168: for (ja = A->i[i]; ja < A->i[i + 1]; ja++)
1169: for (l = 0; l < bs; l++) colidxs[cnt++] = bs * A->j[ja] + l + cs;
1170: for (; jb < B->i[i + 1]; jb++)
1171: for (l = 0; l < bs; l++) colidxs[cnt++] = bs * garray[B->j[jb]] + l;
1172: }
1173: }
1174: PetscCheck(cnt == nz, PETSC_COMM_SELF, PETSC_ERR_LIB, "Internal PETSc error: cnt = %" PetscInt_FMT " nz = %" PetscCount_FMT, cnt, nz);
1175: PetscCall(PetscViewerBinaryWriteAll(viewer, colidxs, nz, PETSC_DECIDE, PETSC_DECIDE, PETSC_INT));
1176: PetscCall(PetscFree(colidxs));
1178: /* fill in and store nonzero values */
1179: PetscCall(PetscMalloc1(nz, &matvals));
1180: for (cnt = 0, i = 0; i < A->mbs; i++) {
1181: for (k = 0; k < bs; k++) {
1182: for (jb = B->i[i]; jb < B->i[i + 1]; jb++) {
1183: if (garray[B->j[jb]] > cs / bs) break;
1184: for (l = 0; l < bs; l++) matvals[cnt++] = B->a[bs * (bs * jb + l) + k];
1185: }
1186: for (ja = A->i[i]; ja < A->i[i + 1]; ja++)
1187: for (l = 0; l < bs; l++) matvals[cnt++] = A->a[bs * (bs * ja + l) + k];
1188: for (; jb < B->i[i + 1]; jb++)
1189: for (l = 0; l < bs; l++) matvals[cnt++] = B->a[bs * (bs * jb + l) + k];
1190: }
1191: }
1192: PetscCall(PetscViewerBinaryWriteAll(viewer, matvals, nz, PETSC_DECIDE, PETSC_DECIDE, PETSC_SCALAR));
1193: PetscCall(PetscFree(matvals));
1195: /* write block size option to the viewer's .info file */
1196: PetscCall(MatView_Binary_BlockSizes(mat, viewer));
1197: PetscFunctionReturn(PETSC_SUCCESS);
1198: }
1200: PetscErrorCode MatView_MPIBAIJ(Mat mat, PetscViewer viewer)
1201: {
1202: PetscBool isascii, isdraw, issocket, isbinary;
1204: PetscFunctionBegin;
1205: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
1206: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
1207: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSOCKET, &issocket));
1208: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
1209: if (isascii || isdraw || issocket) PetscCall(MatView_MPIBAIJ_ASCIIorDraworSocket(mat, viewer));
1210: else if (isbinary) PetscCall(MatView_MPIBAIJ_Binary(mat, viewer));
1211: PetscFunctionReturn(PETSC_SUCCESS);
1212: }
1214: static PetscErrorCode MatMult_MPIBAIJ(Mat A, Vec xx, Vec yy)
1215: {
1216: Mat_MPIBAIJ *a = (Mat_MPIBAIJ *)A->data;
1217: PetscInt nt;
1219: PetscFunctionBegin;
1220: PetscCall(VecGetLocalSize(xx, &nt));
1221: PetscCheck(nt == A->cmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Incompatible partition of A and xx");
1222: PetscCall(VecGetLocalSize(yy, &nt));
1223: PetscCheck(nt == A->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Incompatible partition of A and yy");
1224: PetscCall(VecScatterBegin(a->Mvctx, xx, a->lvec, INSERT_VALUES, SCATTER_FORWARD));
1225: PetscUseTypeMethod(a->A, mult, xx, yy);
1226: PetscCall(VecScatterEnd(a->Mvctx, xx, a->lvec, INSERT_VALUES, SCATTER_FORWARD));
1227: PetscUseTypeMethod(a->B, multadd, a->lvec, yy, yy);
1228: PetscFunctionReturn(PETSC_SUCCESS);
1229: }
1231: static PetscErrorCode MatMultAdd_MPIBAIJ(Mat A, Vec xx, Vec yy, Vec zz)
1232: {
1233: Mat_MPIBAIJ *a = (Mat_MPIBAIJ *)A->data;
1235: PetscFunctionBegin;
1236: PetscCall(VecScatterBegin(a->Mvctx, xx, a->lvec, INSERT_VALUES, SCATTER_FORWARD));
1237: PetscUseTypeMethod(a->A, multadd, xx, yy, zz);
1238: PetscCall(VecScatterEnd(a->Mvctx, xx, a->lvec, INSERT_VALUES, SCATTER_FORWARD));
1239: PetscUseTypeMethod(a->B, multadd, a->lvec, zz, zz);
1240: PetscFunctionReturn(PETSC_SUCCESS);
1241: }
1243: static PetscErrorCode MatMultTranspose_MPIBAIJ(Mat A, Vec xx, Vec yy)
1244: {
1245: Mat_MPIBAIJ *a = (Mat_MPIBAIJ *)A->data;
1247: PetscFunctionBegin;
1248: /* do nondiagonal part */
1249: PetscUseTypeMethod(a->B, multtranspose, xx, a->lvec);
1250: /* do local part */
1251: PetscUseTypeMethod(a->A, multtranspose, xx, yy);
1252: /* add partial results together */
1253: PetscCall(VecScatterBegin(a->Mvctx, a->lvec, yy, ADD_VALUES, SCATTER_REVERSE));
1254: PetscCall(VecScatterEnd(a->Mvctx, a->lvec, yy, ADD_VALUES, SCATTER_REVERSE));
1255: PetscFunctionReturn(PETSC_SUCCESS);
1256: }
1258: static PetscErrorCode MatMultTransposeAdd_MPIBAIJ(Mat A, Vec xx, Vec yy, Vec zz)
1259: {
1260: Mat_MPIBAIJ *a = (Mat_MPIBAIJ *)A->data;
1262: PetscFunctionBegin;
1263: /* do nondiagonal part */
1264: PetscUseTypeMethod(a->B, multtranspose, xx, a->lvec);
1265: /* do local part */
1266: PetscUseTypeMethod(a->A, multtransposeadd, xx, yy, zz);
1267: /* add partial results together */
1268: PetscCall(VecScatterBegin(a->Mvctx, a->lvec, zz, ADD_VALUES, SCATTER_REVERSE));
1269: PetscCall(VecScatterEnd(a->Mvctx, a->lvec, zz, ADD_VALUES, SCATTER_REVERSE));
1270: PetscFunctionReturn(PETSC_SUCCESS);
1271: }
1273: /*
1274: This only works correctly for square matrices where the subblock A->A is the
1275: diagonal block
1276: */
1277: static PetscErrorCode MatGetDiagonal_MPIBAIJ(Mat A, Vec v)
1278: {
1279: PetscFunctionBegin;
1280: PetscCheck(A->rmap->N == A->cmap->N, PETSC_COMM_SELF, PETSC_ERR_SUP, "Supports only square matrix where A->A is diag block");
1281: PetscCall(MatGetDiagonal(((Mat_MPIBAIJ *)A->data)->A, v));
1282: PetscFunctionReturn(PETSC_SUCCESS);
1283: }
1285: static PetscErrorCode MatScale_MPIBAIJ(Mat A, PetscScalar aa)
1286: {
1287: Mat_MPIBAIJ *a = (Mat_MPIBAIJ *)A->data;
1289: PetscFunctionBegin;
1290: PetscCall(MatScale(a->A, aa));
1291: PetscCall(MatScale(a->B, aa));
1292: PetscFunctionReturn(PETSC_SUCCESS);
1293: }
1295: static PetscErrorCode MatGetRow_MPIBAIJ(Mat matin, PetscInt row, PetscInt *nz, PetscInt **idx, PetscScalar **v)
1296: {
1297: Mat_MPIBAIJ *mat = (Mat_MPIBAIJ *)matin->data;
1298: PetscScalar *vworkA, *vworkB, **pvA, **pvB, *v_p;
1299: PetscInt bs = matin->rmap->bs, bs2 = mat->bs2, i, *cworkA, *cworkB, **pcA, **pcB;
1300: PetscInt nztot, nzA, nzB, lrow, brstart = matin->rmap->rstart, brend = matin->rmap->rend;
1301: PetscInt *cmap, *idx_p, cstart = mat->cstartbs;
1303: PetscFunctionBegin;
1304: PetscCheck(row >= brstart && row < brend, PETSC_COMM_SELF, PETSC_ERR_SUP, "Only local rows");
1305: PetscCheck(!mat->getrowactive, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Already active");
1306: mat->getrowactive = PETSC_TRUE;
1308: if (!mat->rowvalues && (idx || v)) {
1309: /*
1310: allocate enough space to hold information from the longest row.
1311: */
1312: Mat_SeqBAIJ *Aa = (Mat_SeqBAIJ *)mat->A->data, *Ba = (Mat_SeqBAIJ *)mat->B->data;
1313: PetscInt max = 1, mbs = mat->mbs, tmp;
1314: for (i = 0; i < mbs; i++) {
1315: tmp = Aa->i[i + 1] - Aa->i[i] + Ba->i[i + 1] - Ba->i[i];
1316: if (max < tmp) max = tmp;
1317: }
1318: PetscCall(PetscMalloc2(max * bs2, &mat->rowvalues, max * bs2, &mat->rowindices));
1319: }
1320: lrow = row - brstart;
1322: pvA = &vworkA;
1323: pcA = &cworkA;
1324: pvB = &vworkB;
1325: pcB = &cworkB;
1326: if (!v) {
1327: pvA = NULL;
1328: pvB = NULL;
1329: }
1330: if (!idx) {
1331: pcA = NULL;
1332: if (!v) pcB = NULL;
1333: }
1334: PetscUseTypeMethod(mat->A, getrow, lrow, &nzA, pcA, pvA);
1335: PetscUseTypeMethod(mat->B, getrow, lrow, &nzB, pcB, pvB);
1336: nztot = nzA + nzB;
1338: cmap = mat->garray;
1339: if (v || idx) {
1340: if (nztot) {
1341: /* Sort by increasing column numbers, assuming A and B already sorted */
1342: PetscInt imark = -1;
1343: if (v) {
1344: *v = v_p = mat->rowvalues;
1345: for (i = 0; i < nzB; i++) {
1346: if (cmap[cworkB[i] / bs] < cstart) v_p[i] = vworkB[i];
1347: else break;
1348: }
1349: imark = i;
1350: for (i = 0; i < nzA; i++) v_p[imark + i] = vworkA[i];
1351: for (i = imark; i < nzB; i++) v_p[nzA + i] = vworkB[i];
1352: }
1353: if (idx) {
1354: *idx = idx_p = mat->rowindices;
1355: if (imark > -1) {
1356: for (i = 0; i < imark; i++) idx_p[i] = cmap[cworkB[i] / bs] * bs + cworkB[i] % bs;
1357: } else {
1358: for (i = 0; i < nzB; i++) {
1359: if (cmap[cworkB[i] / bs] < cstart) idx_p[i] = cmap[cworkB[i] / bs] * bs + cworkB[i] % bs;
1360: else break;
1361: }
1362: imark = i;
1363: }
1364: for (i = 0; i < nzA; i++) idx_p[imark + i] = cstart * bs + cworkA[i];
1365: for (i = imark; i < nzB; i++) idx_p[nzA + i] = cmap[cworkB[i] / bs] * bs + cworkB[i] % bs;
1366: }
1367: } else {
1368: if (idx) *idx = NULL;
1369: if (v) *v = NULL;
1370: }
1371: }
1372: *nz = nztot;
1373: PetscUseTypeMethod(mat->A, restorerow, lrow, &nzA, pcA, pvA);
1374: PetscUseTypeMethod(mat->B, restorerow, lrow, &nzB, pcB, pvB);
1375: PetscFunctionReturn(PETSC_SUCCESS);
1376: }
1378: static PetscErrorCode MatRestoreRow_MPIBAIJ(Mat mat, PetscInt row, PetscInt *nz, PetscInt **idx, PetscScalar **v)
1379: {
1380: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ *)mat->data;
1382: PetscFunctionBegin;
1383: PetscCheck(baij->getrowactive, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "MatGetRow not called");
1384: baij->getrowactive = PETSC_FALSE;
1385: PetscFunctionReturn(PETSC_SUCCESS);
1386: }
1388: static PetscErrorCode MatZeroEntries_MPIBAIJ(Mat A)
1389: {
1390: Mat_MPIBAIJ *l = (Mat_MPIBAIJ *)A->data;
1392: PetscFunctionBegin;
1393: PetscCall(MatZeroEntries(l->A));
1394: PetscCall(MatZeroEntries(l->B));
1395: PetscFunctionReturn(PETSC_SUCCESS);
1396: }
1398: static PetscErrorCode MatGetInfo_MPIBAIJ(Mat matin, MatInfoType flag, MatInfo *info)
1399: {
1400: Mat_MPIBAIJ *a = (Mat_MPIBAIJ *)matin->data;
1401: Mat A = a->A, B = a->B;
1402: PetscLogDouble irecv[5];
1404: PetscFunctionBegin;
1405: info->block_size = (PetscReal)matin->rmap->bs;
1407: PetscCall(MatGetInfo(A, MAT_LOCAL, info));
1409: irecv[0] = info->nz_used;
1410: irecv[1] = info->nz_allocated;
1411: irecv[2] = info->nz_unneeded;
1412: irecv[3] = info->memory;
1413: irecv[4] = info->mallocs;
1415: PetscCall(MatGetInfo(B, MAT_LOCAL, info));
1417: irecv[0] += info->nz_used;
1418: irecv[1] += info->nz_allocated;
1419: irecv[2] += info->nz_unneeded;
1420: irecv[3] += info->memory;
1421: irecv[4] += info->mallocs;
1423: if (flag == MAT_LOCAL) {
1424: info->nz_used = irecv[0];
1425: info->nz_allocated = irecv[1];
1426: info->nz_unneeded = irecv[2];
1427: info->memory = irecv[3];
1428: info->mallocs = irecv[4];
1429: } else if (flag == MAT_GLOBAL_MAX) {
1430: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, irecv, 5, MPIU_PETSCLOGDOUBLE, MPI_MAX, PetscObjectComm((PetscObject)matin)));
1432: info->nz_used = irecv[0];
1433: info->nz_allocated = irecv[1];
1434: info->nz_unneeded = irecv[2];
1435: info->memory = irecv[3];
1436: info->mallocs = irecv[4];
1437: } else if (flag == MAT_GLOBAL_SUM) {
1438: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, irecv, 5, MPIU_PETSCLOGDOUBLE, MPI_SUM, PetscObjectComm((PetscObject)matin)));
1440: info->nz_used = irecv[0];
1441: info->nz_allocated = irecv[1];
1442: info->nz_unneeded = irecv[2];
1443: info->memory = irecv[3];
1444: info->mallocs = irecv[4];
1445: } else SETERRQ(PetscObjectComm((PetscObject)matin), PETSC_ERR_ARG_WRONG, "Unknown MatInfoType argument %d", (int)flag);
1446: info->fill_ratio_given = 0; /* no parallel LU/ILU/Cholesky */
1447: info->fill_ratio_needed = 0;
1448: info->factor_mallocs = 0;
1449: PetscFunctionReturn(PETSC_SUCCESS);
1450: }
1452: static PetscErrorCode MatSetOption_MPIBAIJ(Mat A, MatOption op, PetscBool flg)
1453: {
1454: Mat_MPIBAIJ *a = (Mat_MPIBAIJ *)A->data;
1456: PetscFunctionBegin;
1457: switch (op) {
1458: case MAT_NEW_NONZERO_LOCATIONS:
1459: case MAT_NEW_NONZERO_ALLOCATION_ERR:
1460: case MAT_UNUSED_NONZERO_LOCATION_ERR:
1461: case MAT_KEEP_NONZERO_PATTERN:
1462: case MAT_NEW_NONZERO_LOCATION_ERR:
1463: case MAT_ROW_ORIENTED:
1464: MatCheckPreallocated(A, 1);
1465: if (op == MAT_ROW_ORIENTED) a->roworiented = flg;
1466: PetscCall(MatSetOption(a->A, op, flg));
1467: PetscCall(MatSetOption(a->B, op, flg));
1468: break;
1469: case MAT_IGNORE_OFF_PROC_ENTRIES:
1470: a->donotstash = flg;
1471: break;
1472: case MAT_USE_HASH_TABLE:
1473: a->ht_flag = flg;
1474: a->ht_fact = 1.39;
1475: break;
1476: case MAT_SPD:
1477: case MAT_SYMMETRIC:
1478: case MAT_STRUCTURALLY_SYMMETRIC:
1479: case MAT_HERMITIAN:
1480: case MAT_SYMMETRY_ETERNAL:
1481: case MAT_STRUCTURAL_SYMMETRY_ETERNAL:
1482: case MAT_SPD_ETERNAL:
1483: /* if the diagonal matrix is square it inherits some of the properties above */
1484: if (a->A && A->rmap->n == A->cmap->n) PetscCall(MatSetOption(a->A, op, flg));
1485: break;
1486: default:
1487: break;
1488: }
1489: PetscFunctionReturn(PETSC_SUCCESS);
1490: }
1492: static PetscErrorCode MatTranspose_MPIBAIJ(Mat A, MatReuse reuse, Mat *matout)
1493: {
1494: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ *)A->data;
1495: Mat_SeqBAIJ *Aloc;
1496: Mat B;
1497: PetscInt M = A->rmap->N, N = A->cmap->N, *ai, *aj, i, *rvals, j, k, col;
1498: PetscInt bs = A->rmap->bs, mbs = baij->mbs;
1499: MatScalar *a;
1501: PetscFunctionBegin;
1502: if (reuse == MAT_REUSE_MATRIX) PetscCall(MatTransposeCheckNonzeroState_Private(A, *matout));
1503: if (reuse == MAT_INITIAL_MATRIX || reuse == MAT_INPLACE_MATRIX) {
1504: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &B));
1505: PetscCall(MatSetSizes(B, A->cmap->n, A->rmap->n, N, M));
1506: PetscCall(MatSetType(B, ((PetscObject)A)->type_name));
1507: /* Do not know preallocation information, but must set block size */
1508: PetscCall(MatMPIBAIJSetPreallocation(B, A->rmap->bs, PETSC_DECIDE, NULL, PETSC_DECIDE, NULL));
1509: } else {
1510: B = *matout;
1511: }
1513: /* copy over the A part */
1514: Aloc = (Mat_SeqBAIJ *)baij->A->data;
1515: ai = Aloc->i;
1516: aj = Aloc->j;
1517: a = Aloc->a;
1518: PetscCall(PetscMalloc1(bs, &rvals));
1520: for (i = 0; i < mbs; i++) {
1521: rvals[0] = bs * (baij->rstartbs + i);
1522: for (j = 1; j < bs; j++) rvals[j] = rvals[j - 1] + 1;
1523: for (j = ai[i]; j < ai[i + 1]; j++) {
1524: col = (baij->cstartbs + aj[j]) * bs;
1525: for (k = 0; k < bs; k++) {
1526: PetscCall(MatSetValues_MPIBAIJ(B, 1, &col, bs, rvals, a, INSERT_VALUES));
1528: col++;
1529: a += bs;
1530: }
1531: }
1532: }
1533: /* copy over the B part */
1534: Aloc = (Mat_SeqBAIJ *)baij->B->data;
1535: ai = Aloc->i;
1536: aj = Aloc->j;
1537: a = Aloc->a;
1538: for (i = 0; i < mbs; i++) {
1539: rvals[0] = bs * (baij->rstartbs + i);
1540: for (j = 1; j < bs; j++) rvals[j] = rvals[j - 1] + 1;
1541: for (j = ai[i]; j < ai[i + 1]; j++) {
1542: col = baij->garray[aj[j]] * bs;
1543: for (k = 0; k < bs; k++) {
1544: PetscCall(MatSetValues_MPIBAIJ(B, 1, &col, bs, rvals, a, INSERT_VALUES));
1545: col++;
1546: a += bs;
1547: }
1548: }
1549: }
1550: PetscCall(PetscFree(rvals));
1551: PetscCall(MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY));
1552: PetscCall(MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY));
1554: if (reuse == MAT_INITIAL_MATRIX || reuse == MAT_REUSE_MATRIX) *matout = B;
1555: else PetscCall(MatHeaderMerge(A, &B));
1556: PetscFunctionReturn(PETSC_SUCCESS);
1557: }
1559: static PetscErrorCode MatDiagonalScale_MPIBAIJ(Mat mat, Vec ll, Vec rr)
1560: {
1561: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ *)mat->data;
1562: Mat a = baij->A, b = baij->B;
1563: PetscInt s1, s2, s3;
1565: PetscFunctionBegin;
1566: PetscCall(MatGetLocalSize(mat, &s2, &s3));
1567: if (rr) {
1568: PetscCall(VecGetLocalSize(rr, &s1));
1569: PetscCheck(s1 == s3, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "right vector non-conforming local size");
1570: /* Overlap communication with computation. */
1571: PetscCall(VecScatterBegin(baij->Mvctx, rr, baij->lvec, INSERT_VALUES, SCATTER_FORWARD));
1572: }
1573: if (ll) {
1574: PetscCall(VecGetLocalSize(ll, &s1));
1575: PetscCheck(s1 == s2, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "left vector non-conforming local size");
1576: PetscUseTypeMethod(b, diagonalscale, ll, NULL);
1577: }
1578: /* scale the diagonal block */
1579: PetscUseTypeMethod(a, diagonalscale, ll, rr);
1581: if (rr) {
1582: /* Do a scatter end and then right scale the off-diagonal block */
1583: PetscCall(VecScatterEnd(baij->Mvctx, rr, baij->lvec, INSERT_VALUES, SCATTER_FORWARD));
1584: PetscUseTypeMethod(b, diagonalscale, NULL, baij->lvec);
1585: }
1586: /* MatDiagonalScale() cannot be used on the blocks: they are on PETSC_COMM_SELF while ll and rr
1587: are parallel, so the interface's communicator check rejects them. Advance the block states
1588: here instead, as the interface would. MatDiagonalScale_MPIAIJ() does not need this because
1589: MatSeqAIJRestoreArray() advances the state for it. */
1590: PetscCall(PetscObjectStateIncrease((PetscObject)a));
1591: PetscCall(PetscObjectStateIncrease((PetscObject)b));
1592: PetscFunctionReturn(PETSC_SUCCESS);
1593: }
1595: static PetscErrorCode MatZeroRows_MPIBAIJ(Mat A, PetscInt N, const PetscInt rows[], PetscScalar diag, Vec x, Vec b)
1596: {
1597: Mat_MPIBAIJ *l = (Mat_MPIBAIJ *)A->data;
1598: PetscInt *lrows;
1599: PetscInt r, len;
1600: PetscBool cong;
1602: PetscFunctionBegin;
1603: /* get locally owned rows */
1604: PetscCall(MatZeroRowsMapLocal_Private(A, N, rows, &len, &lrows));
1605: /* fix right-hand side if needed */
1606: if (x && b) {
1607: const PetscScalar *xx;
1608: PetscScalar *bb;
1610: PetscCall(VecGetArrayRead(x, &xx));
1611: PetscCall(VecGetArray(b, &bb));
1612: for (r = 0; r < len; ++r) bb[lrows[r]] = diag * xx[lrows[r]];
1613: PetscCall(VecRestoreArrayRead(x, &xx));
1614: PetscCall(VecRestoreArray(b, &bb));
1615: }
1617: /* actually zap the local rows */
1618: /*
1619: Zero the required rows. If the "diagonal block" of the matrix
1620: is square and the user wishes to set the diagonal we use separate
1621: code so that MatSetValues() is not called for each diagonal allocating
1622: new memory, thus calling lots of mallocs and slowing things down.
1624: */
1625: /* must zero l->B before l->A because the (diag) case below may put values into l->B*/
1626: PetscCall(MatZeroRows_SeqBAIJ(l->B, len, lrows, 0.0, NULL, NULL));
1627: PetscCall(MatHasCongruentLayouts(A, &cong));
1628: if ((diag != 0.0) && cong) {
1629: PetscCall(MatZeroRows_SeqBAIJ(l->A, len, lrows, diag, NULL, NULL));
1630: } else if (diag != 0.0) {
1631: PetscCall(MatZeroRows_SeqBAIJ(l->A, len, lrows, 0.0, NULL, NULL));
1632: PetscCheck(!((Mat_SeqBAIJ *)l->A->data)->nonew, PETSC_COMM_SELF, PETSC_ERR_SUP, "MatZeroRows() on rectangular matrices cannot be used with the Mat options MAT_NEW_NONZERO_LOCATIONS, MAT_NEW_NONZERO_LOCATION_ERR, and MAT_NEW_NONZERO_ALLOCATION_ERR");
1633: for (r = 0; r < len; ++r) {
1634: const PetscInt row = lrows[r] + A->rmap->rstart;
1635: PetscCall(MatSetValues(A, 1, &row, 1, &row, &diag, INSERT_VALUES));
1636: }
1637: PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
1638: PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
1639: } else {
1640: PetscCall(MatZeroRows_SeqBAIJ(l->A, len, lrows, 0.0, NULL, NULL));
1641: }
1642: /* MatZeroRows() cannot be used on the blocks: it honors -mat_view, which would print each
1643: sequential block as well (see mat_tests-ex12_5). Advance the diagonal block's state here
1644: instead, as the interface would; MatInvertBlockDiagonal_MPIBAIJ() caches on that state. */
1645: PetscCall(PetscObjectStateIncrease((PetscObject)l->A));
1646: PetscCall(PetscFree(lrows));
1648: /* only change matrix nonzero state if pattern was allowed to be changed */
1649: if (!((Mat_SeqBAIJ *)l->A->data)->keepnonzeropattern || !((Mat_SeqBAIJ *)l->A->data)->nonew) {
1650: A->nonzerostate = l->A->nonzerostate + l->B->nonzerostate;
1651: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &A->nonzerostate, 1, MPIU_INT64, MPI_SUM, PetscObjectComm((PetscObject)A)));
1652: }
1653: PetscFunctionReturn(PETSC_SUCCESS);
1654: }
1656: static PetscErrorCode MatZeroRowsColumns_MPIBAIJ(Mat A, PetscInt N, const PetscInt rows[], PetscScalar diag, Vec x, Vec b)
1657: {
1658: Mat_MPIBAIJ *l = (Mat_MPIBAIJ *)A->data;
1659: PetscMPIInt n, p = 0;
1660: PetscInt i, j, k, r, len = 0, row, col, count;
1661: PetscInt *lrows, *owners = A->rmap->range;
1662: PetscSFNode *rrows;
1663: PetscSF sf;
1664: const PetscScalar *xx;
1665: PetscScalar *bb, *mask;
1666: Vec xmask, lmask;
1667: Mat_SeqBAIJ *baij = (Mat_SeqBAIJ *)l->B->data;
1668: PetscInt bs = A->rmap->bs, bs2 = baij->bs2;
1669: PetscScalar *aa;
1671: PetscFunctionBegin;
1672: PetscCall(PetscMPIIntCast(A->rmap->n, &n));
1673: /* create PetscSF where leaves are input rows and roots are owned rows */
1674: PetscCall(PetscMalloc1(n, &lrows));
1675: for (r = 0; r < n; ++r) lrows[r] = -1;
1676: PetscCall(PetscMalloc1(N, &rrows));
1677: for (r = 0; r < N; ++r) {
1678: const PetscInt idx = rows[r];
1679: 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);
1680: if (idx < owners[p] || owners[p + 1] <= idx) { /* short-circuit the search if the last p owns this row too */
1681: PetscCall(PetscLayoutFindOwner(A->rmap, idx, &p));
1682: }
1683: rrows[r].rank = p;
1684: rrows[r].index = rows[r] - owners[p];
1685: }
1686: PetscCall(PetscSFCreate(PetscObjectComm((PetscObject)A), &sf));
1687: PetscCall(PetscSFSetGraph(sf, n, N, NULL, PETSC_OWN_POINTER, rrows, PETSC_OWN_POINTER));
1688: /* collect flags for rows to be zeroed */
1689: PetscCall(PetscSFReduceBegin(sf, MPIU_INT, (PetscInt *)rows, lrows, MPI_LOR));
1690: PetscCall(PetscSFReduceEnd(sf, MPIU_INT, (PetscInt *)rows, lrows, MPI_LOR));
1691: PetscCall(PetscSFDestroy(&sf));
1692: /* compress and put in row numbers */
1693: for (r = 0; r < n; ++r)
1694: if (lrows[r] >= 0) lrows[len++] = r;
1695: /* zero diagonal part of matrix */
1696: PetscCall(MatZeroRowsColumns(l->A, len, lrows, diag, x, b));
1697: /* handle off-diagonal part of matrix */
1698: PetscCall(MatCreateVecs(A, &xmask, NULL));
1699: PetscCall(VecDuplicate(l->lvec, &lmask));
1700: PetscCall(VecGetArray(xmask, &bb));
1701: for (i = 0; i < len; i++) bb[lrows[i]] = 1;
1702: PetscCall(VecRestoreArray(xmask, &bb));
1703: PetscCall(VecScatterBegin(l->Mvctx, xmask, lmask, ADD_VALUES, SCATTER_FORWARD));
1704: PetscCall(VecScatterEnd(l->Mvctx, xmask, lmask, ADD_VALUES, SCATTER_FORWARD));
1705: PetscCall(VecDestroy(&xmask));
1706: if (x) {
1707: PetscCall(VecScatterBegin(l->Mvctx, x, l->lvec, INSERT_VALUES, SCATTER_FORWARD));
1708: PetscCall(VecScatterEnd(l->Mvctx, x, l->lvec, INSERT_VALUES, SCATTER_FORWARD));
1709: PetscCall(VecGetArrayRead(l->lvec, &xx));
1710: PetscCall(VecGetArray(b, &bb));
1711: }
1712: PetscCall(VecGetArray(lmask, &mask));
1713: /* remove zeroed rows of off-diagonal matrix */
1714: for (i = 0; i < len; ++i) {
1715: row = lrows[i];
1716: count = (baij->i[row / bs + 1] - baij->i[row / bs]) * bs;
1717: aa = PetscSafePointerPlusOffset(baij->a, baij->i[row / bs] * bs2 + (row % bs));
1718: for (k = 0; k < count; ++k) {
1719: aa[0] = 0.0;
1720: aa += bs;
1721: }
1722: }
1723: /* loop over all elements of off process part of matrix zeroing removed columns */
1724: for (i = 0; i < l->B->rmap->N; ++i) {
1725: row = i / bs;
1726: for (j = baij->i[row]; j < baij->i[row + 1]; ++j) {
1727: for (k = 0; k < bs; ++k) {
1728: col = bs * baij->j[j] + k;
1729: if (PetscAbsScalar(mask[col])) {
1730: aa = baij->a + j * bs2 + (i % bs) + bs * k;
1731: if (x) bb[i] -= aa[0] * xx[col];
1732: aa[0] = 0.0;
1733: }
1734: }
1735: }
1736: }
1737: if (x) {
1738: PetscCall(VecRestoreArray(b, &bb));
1739: PetscCall(VecRestoreArrayRead(l->lvec, &xx));
1740: }
1741: PetscCall(VecRestoreArray(lmask, &mask));
1742: PetscCall(VecDestroy(&lmask));
1743: PetscCall(PetscFree(lrows));
1745: /* only change matrix nonzero state if pattern was allowed to be changed */
1746: if (!((Mat_SeqBAIJ *)l->A->data)->nonew) {
1747: A->nonzerostate = l->A->nonzerostate + l->B->nonzerostate;
1748: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &A->nonzerostate, 1, MPIU_INT64, MPI_SUM, PetscObjectComm((PetscObject)A)));
1749: }
1750: PetscFunctionReturn(PETSC_SUCCESS);
1751: }
1753: static PetscErrorCode MatSetUnfactored_MPIBAIJ(Mat A)
1754: {
1755: Mat_MPIBAIJ *a = (Mat_MPIBAIJ *)A->data;
1757: PetscFunctionBegin;
1758: PetscCall(MatSetUnfactored(a->A));
1759: PetscFunctionReturn(PETSC_SUCCESS);
1760: }
1762: static PetscErrorCode MatDuplicate_MPIBAIJ(Mat, MatDuplicateOption, Mat *);
1764: static PetscErrorCode MatEqual_MPIBAIJ(Mat A, Mat B, PetscBool *flag)
1765: {
1766: Mat_MPIBAIJ *matB = (Mat_MPIBAIJ *)B->data, *matA = (Mat_MPIBAIJ *)A->data;
1767: Mat a, b, c, d;
1769: PetscFunctionBegin;
1770: a = matA->A;
1771: b = matA->B;
1772: c = matB->A;
1773: d = matB->B;
1775: PetscCall(MatEqual(a, c, flag));
1776: if (*flag) PetscCall(MatEqual(b, d, flag));
1777: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, flag, 1, MPI_C_BOOL, MPI_LAND, PetscObjectComm((PetscObject)A)));
1778: PetscFunctionReturn(PETSC_SUCCESS);
1779: }
1781: static PetscErrorCode MatCopy_MPIBAIJ(Mat A, Mat B, MatStructure str)
1782: {
1783: Mat_MPIBAIJ *a = (Mat_MPIBAIJ *)A->data;
1784: Mat_MPIBAIJ *b = (Mat_MPIBAIJ *)B->data;
1786: PetscFunctionBegin;
1787: /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */
1788: if ((str != SAME_NONZERO_PATTERN) || (A->ops->copy != B->ops->copy)) {
1789: PetscCall(MatCopy_Basic(A, B, str));
1790: } else {
1791: PetscCall(MatCopy(a->A, b->A, str));
1792: PetscCall(MatCopy(a->B, b->B, str));
1793: }
1794: PetscCall(PetscObjectStateIncrease((PetscObject)B));
1795: PetscFunctionReturn(PETSC_SUCCESS);
1796: }
1798: PetscErrorCode MatAXPYGetPreallocation_MPIBAIJ(Mat Y, const PetscInt *yltog, Mat X, const PetscInt *xltog, PetscInt *nnz)
1799: {
1800: PetscInt bs = Y->rmap->bs, m = Y->rmap->N / bs;
1801: Mat_SeqBAIJ *x = (Mat_SeqBAIJ *)X->data;
1802: Mat_SeqBAIJ *y = (Mat_SeqBAIJ *)Y->data;
1804: PetscFunctionBegin;
1805: PetscCall(MatAXPYGetPreallocation_MPIX_private(m, x->i, x->j, xltog, y->i, y->j, yltog, nnz));
1806: PetscFunctionReturn(PETSC_SUCCESS);
1807: }
1809: static PetscErrorCode MatAXPY_MPIBAIJ(Mat Y, PetscScalar a, Mat X, MatStructure str)
1810: {
1811: Mat_MPIBAIJ *xx = (Mat_MPIBAIJ *)X->data, *yy = (Mat_MPIBAIJ *)Y->data;
1812: PetscBLASInt bnz, one = 1;
1813: Mat_SeqBAIJ *x, *y;
1814: PetscInt bs2 = Y->rmap->bs * Y->rmap->bs;
1816: PetscFunctionBegin;
1817: if (str == SAME_NONZERO_PATTERN) {
1818: PetscScalar alpha = a;
1819: x = (Mat_SeqBAIJ *)xx->A->data;
1820: y = (Mat_SeqBAIJ *)yy->A->data;
1821: PetscCall(PetscBLASIntCast(x->nz * bs2, &bnz));
1822: PetscCallBLAS("BLASaxpy", BLASaxpy_(&bnz, &alpha, x->a, &one, y->a, &one));
1823: x = (Mat_SeqBAIJ *)xx->B->data;
1824: y = (Mat_SeqBAIJ *)yy->B->data;
1825: PetscCall(PetscBLASIntCast(x->nz * bs2, &bnz));
1826: PetscCallBLAS("BLASaxpy", BLASaxpy_(&bnz, &alpha, x->a, &one, y->a, &one));
1827: /* the blocks' values were changed directly, so advance their states as MatAXPY() on each
1828: block would; MatInvertBlockDiagonal_SeqBAIJ() caches on the diagonal block's state */
1829: PetscCall(PetscObjectStateIncrease((PetscObject)yy->A));
1830: PetscCall(PetscObjectStateIncrease((PetscObject)yy->B));
1831: PetscCall(PetscObjectStateIncrease((PetscObject)Y));
1832: } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
1833: PetscCall(MatAXPY_Basic(Y, a, X, str));
1834: } else {
1835: Mat B;
1836: PetscInt *nnz_d, *nnz_o, bs = Y->rmap->bs;
1837: PetscCall(PetscMalloc1(yy->A->rmap->N, &nnz_d));
1838: PetscCall(PetscMalloc1(yy->B->rmap->N, &nnz_o));
1839: PetscCall(MatCreate(PetscObjectComm((PetscObject)Y), &B));
1840: PetscCall(PetscObjectSetName((PetscObject)B, ((PetscObject)Y)->name));
1841: PetscCall(MatSetSizes(B, Y->rmap->n, Y->cmap->n, Y->rmap->N, Y->cmap->N));
1842: PetscCall(MatSetBlockSizesFromMats(B, Y, Y));
1843: PetscCall(MatSetType(B, MATMPIBAIJ));
1844: PetscCall(MatAXPYGetPreallocation_SeqBAIJ(yy->A, xx->A, nnz_d));
1845: PetscCall(MatAXPYGetPreallocation_MPIBAIJ(yy->B, yy->garray, xx->B, xx->garray, nnz_o));
1846: PetscCall(MatMPIBAIJSetPreallocation(B, bs, 0, nnz_d, 0, nnz_o));
1847: /* MatAXPY_BasicWithPreallocation() for BAIJ matrix is much slower than AIJ, even for bs=1 ! */
1848: PetscCall(MatAXPY_BasicWithPreallocation(B, Y, a, X, str));
1849: PetscCall(MatHeaderMerge(Y, &B));
1850: PetscCall(PetscFree(nnz_d));
1851: PetscCall(PetscFree(nnz_o));
1852: }
1853: PetscFunctionReturn(PETSC_SUCCESS);
1854: }
1856: static PetscErrorCode MatConjugate_MPIBAIJ(Mat mat)
1857: {
1858: Mat_MPIBAIJ *a = (Mat_MPIBAIJ *)mat->data;
1860: PetscFunctionBegin;
1861: PetscCall(MatConjugate(a->A));
1862: PetscCall(MatConjugate(a->B));
1863: PetscFunctionReturn(PETSC_SUCCESS);
1864: }
1866: static PetscErrorCode MatRealPart_MPIBAIJ(Mat A)
1867: {
1868: Mat_MPIBAIJ *a = (Mat_MPIBAIJ *)A->data;
1870: PetscFunctionBegin;
1871: PetscCall(MatRealPart(a->A));
1872: PetscCall(MatRealPart(a->B));
1873: PetscFunctionReturn(PETSC_SUCCESS);
1874: }
1876: static PetscErrorCode MatImaginaryPart_MPIBAIJ(Mat A)
1877: {
1878: Mat_MPIBAIJ *a = (Mat_MPIBAIJ *)A->data;
1880: PetscFunctionBegin;
1881: PetscCall(MatImaginaryPart(a->A));
1882: PetscCall(MatImaginaryPart(a->B));
1883: PetscFunctionReturn(PETSC_SUCCESS);
1884: }
1886: static PetscErrorCode MatCreateSubMatrix_MPIBAIJ(Mat mat, IS isrow, IS iscol, MatReuse call, Mat *newmat)
1887: {
1888: IS iscol_local;
1889: PetscInt csize;
1891: PetscFunctionBegin;
1892: PetscCall(ISGetLocalSize(iscol, &csize));
1893: if (call == MAT_REUSE_MATRIX) {
1894: PetscCall(PetscObjectQuery((PetscObject)*newmat, "ISAllGather", (PetscObject *)&iscol_local));
1895: PetscCheck(iscol_local, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Submatrix passed in was not used before, cannot reuse");
1896: } else {
1897: PetscCall(ISAllGather(iscol, &iscol_local));
1898: }
1899: PetscCall(MatCreateSubMatrix_MPIBAIJ_Private(mat, isrow, iscol_local, csize, call, newmat, PETSC_FALSE));
1900: if (call == MAT_INITIAL_MATRIX) {
1901: PetscCall(PetscObjectCompose((PetscObject)*newmat, "ISAllGather", (PetscObject)iscol_local));
1902: PetscCall(ISDestroy(&iscol_local));
1903: }
1904: PetscFunctionReturn(PETSC_SUCCESS);
1905: }
1907: /*
1908: Not great since it makes two copies of the submatrix, first an SeqBAIJ
1909: in local and then by concatenating the local matrices the end result.
1910: Writing it directly would be much like MatCreateSubMatrices_MPIBAIJ().
1911: This routine is used for BAIJ and SBAIJ matrices (unfortunate dependency).
1912: */
1913: PetscErrorCode MatCreateSubMatrix_MPIBAIJ_Private(Mat mat, IS isrow, IS iscol, PetscInt csize, MatReuse call, Mat *newmat, PetscBool sym)
1914: {
1915: PetscMPIInt rank, size;
1916: PetscInt i, m, n, rstart, row, rend, nz, *cwork, j, bs;
1917: PetscInt *ii, *jj, nlocal, *dlens, *olens, dlen, olen, jend, mglobal;
1918: Mat M, Mreuse;
1919: MatScalar *vwork, *aa;
1920: MPI_Comm comm;
1921: IS isrow_new, iscol_new;
1922: Mat_SeqBAIJ *aij;
1924: PetscFunctionBegin;
1925: PetscCall(PetscObjectGetComm((PetscObject)mat, &comm));
1926: PetscCallMPI(MPI_Comm_rank(comm, &rank));
1927: PetscCallMPI(MPI_Comm_size(comm, &size));
1928: /* The compression and expansion should be avoided. Doesn't point
1929: out errors, might change the indices, hence buggey */
1930: PetscCall(ISCompressIndicesGeneral(mat->rmap->N, mat->rmap->n, mat->rmap->bs, 1, &isrow, &isrow_new));
1931: if (isrow == iscol) {
1932: iscol_new = isrow_new;
1933: PetscCall(PetscObjectReference((PetscObject)iscol_new));
1934: } else PetscCall(ISCompressIndicesGeneral(mat->cmap->N, mat->cmap->n, mat->cmap->bs, 1, &iscol, &iscol_new));
1936: if (call == MAT_REUSE_MATRIX) {
1937: PetscCall(PetscObjectQuery((PetscObject)*newmat, "SubMatrix", (PetscObject *)&Mreuse));
1938: PetscCheck(Mreuse, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Submatrix passed in was not used before, cannot reuse");
1939: PetscCall(MatCreateSubMatrices_MPIBAIJ_local(mat, 1, &isrow_new, &iscol_new, MAT_REUSE_MATRIX, &Mreuse, sym));
1940: } else {
1941: PetscCall(MatCreateSubMatrices_MPIBAIJ_local(mat, 1, &isrow_new, &iscol_new, MAT_INITIAL_MATRIX, &Mreuse, sym));
1942: }
1943: PetscCall(ISDestroy(&isrow_new));
1944: PetscCall(ISDestroy(&iscol_new));
1945: /*
1946: m - number of local rows
1947: n - number of columns (same on all processors)
1948: rstart - first row in new global matrix generated
1949: */
1950: PetscCall(MatGetBlockSize(mat, &bs));
1951: PetscCall(MatGetSize(Mreuse, &m, &n));
1952: m = m / bs;
1953: n = n / bs;
1955: if (call == MAT_INITIAL_MATRIX) {
1956: aij = (Mat_SeqBAIJ *)Mreuse->data;
1957: ii = aij->i;
1958: jj = aij->j;
1960: /*
1961: Determine the number of non-zeros in the diagonal and off-diagonal
1962: portions of the matrix in order to do correct preallocation
1963: */
1965: /* first get start and end of "diagonal" columns */
1966: if (csize == PETSC_DECIDE) {
1967: PetscCall(ISGetSize(isrow, &mglobal));
1968: if (mglobal == n * bs) { /* square matrix */
1969: nlocal = m;
1970: } else {
1971: nlocal = n / size + ((n % size) > rank);
1972: }
1973: } else {
1974: nlocal = csize / bs;
1975: }
1976: PetscCallMPI(MPI_Scan(&nlocal, &rend, 1, MPIU_INT, MPI_SUM, comm));
1977: rstart = rend - nlocal;
1978: PetscCheck(rank != size - 1 || rend == n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Local column sizes %" PetscInt_FMT " do not add up to total number of columns %" PetscInt_FMT, rend, n);
1980: /* next, compute all the lengths */
1981: PetscCall(PetscMalloc2(m + 1, &dlens, m + 1, &olens));
1982: for (i = 0; i < m; i++) {
1983: jend = ii[i + 1] - ii[i];
1984: olen = 0;
1985: dlen = 0;
1986: for (j = 0; j < jend; j++) {
1987: if (*jj < rstart || *jj >= rend) olen++;
1988: else dlen++;
1989: jj++;
1990: }
1991: olens[i] = olen;
1992: dlens[i] = dlen;
1993: }
1994: PetscCall(MatCreate(comm, &M));
1995: PetscCall(MatSetSizes(M, bs * m, bs * nlocal, PETSC_DECIDE, bs * n));
1996: PetscCall(MatSetType(M, sym ? ((PetscObject)mat)->type_name : MATMPIBAIJ));
1997: PetscCall(MatMPIBAIJSetPreallocation(M, bs, 0, dlens, 0, olens));
1998: PetscCall(MatMPISBAIJSetPreallocation(M, bs, 0, dlens, 0, olens));
1999: PetscCall(PetscFree2(dlens, olens));
2000: } else {
2001: PetscInt ml, nl;
2003: M = *newmat;
2004: PetscCall(MatGetLocalSize(M, &ml, &nl));
2005: PetscCheck(ml == m, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Previous matrix must be same size/layout as request");
2006: PetscCall(MatZeroEntries(M));
2007: /*
2008: The next two lines are needed so we may call MatSetValues_MPIAIJ() below directly,
2009: rather than the slower MatSetValues().
2010: */
2011: M->was_assembled = PETSC_TRUE;
2012: M->assembled = PETSC_FALSE;
2013: }
2014: PetscCall(MatSetOption(M, MAT_ROW_ORIENTED, PETSC_FALSE));
2015: PetscCall(MatGetOwnershipRange(M, &rstart, &rend));
2016: aij = (Mat_SeqBAIJ *)Mreuse->data;
2017: ii = aij->i;
2018: jj = aij->j;
2019: aa = aij->a;
2020: for (i = 0; i < m; i++) {
2021: row = rstart / bs + i;
2022: nz = ii[i + 1] - ii[i];
2023: cwork = jj;
2024: jj = PetscSafePointerPlusOffset(jj, nz);
2025: vwork = aa;
2026: aa = PetscSafePointerPlusOffset(aa, nz * bs * bs);
2027: PetscCall(MatSetValuesBlocked_MPIBAIJ(M, 1, &row, nz, cwork, vwork, INSERT_VALUES));
2028: }
2030: PetscCall(MatAssemblyBegin(M, MAT_FINAL_ASSEMBLY));
2031: PetscCall(MatAssemblyEnd(M, MAT_FINAL_ASSEMBLY));
2032: *newmat = M;
2034: /* save submatrix used in processor for next request */
2035: if (call == MAT_INITIAL_MATRIX) {
2036: PetscCall(PetscObjectCompose((PetscObject)M, "SubMatrix", (PetscObject)Mreuse));
2037: PetscCall(PetscObjectDereference((PetscObject)Mreuse));
2038: }
2039: PetscFunctionReturn(PETSC_SUCCESS);
2040: }
2042: static PetscErrorCode MatPermute_MPIBAIJ(Mat A, IS rowp, IS colp, Mat *B)
2043: {
2044: MPI_Comm comm, pcomm;
2045: PetscInt clocal_size, nrows;
2046: const PetscInt *rows;
2047: PetscMPIInt size;
2048: IS crowp, lcolp;
2050: PetscFunctionBegin;
2051: PetscCall(PetscObjectGetComm((PetscObject)A, &comm));
2052: /* make a collective version of 'rowp' */
2053: PetscCall(PetscObjectGetComm((PetscObject)rowp, &pcomm));
2054: if (pcomm == comm) {
2055: crowp = rowp;
2056: } else {
2057: PetscCall(ISGetSize(rowp, &nrows));
2058: PetscCall(ISGetIndices(rowp, &rows));
2059: PetscCall(ISCreateGeneral(comm, nrows, rows, PETSC_COPY_VALUES, &crowp));
2060: PetscCall(ISRestoreIndices(rowp, &rows));
2061: }
2062: PetscCall(ISSetPermutation(crowp));
2063: /* make a local version of 'colp' */
2064: PetscCall(PetscObjectGetComm((PetscObject)colp, &pcomm));
2065: PetscCallMPI(MPI_Comm_size(pcomm, &size));
2066: if (size == 1) {
2067: lcolp = colp;
2068: } else {
2069: PetscCall(ISAllGather(colp, &lcolp));
2070: }
2071: PetscCall(ISSetPermutation(lcolp));
2072: /* now we just get the submatrix */
2073: PetscCall(MatGetLocalSize(A, NULL, &clocal_size));
2074: PetscCall(MatCreateSubMatrix_MPIBAIJ_Private(A, crowp, lcolp, clocal_size, MAT_INITIAL_MATRIX, B, PETSC_FALSE));
2075: /* clean up */
2076: if (pcomm != comm) PetscCall(ISDestroy(&crowp));
2077: if (size > 1) PetscCall(ISDestroy(&lcolp));
2078: PetscFunctionReturn(PETSC_SUCCESS);
2079: }
2081: static PetscErrorCode MatGetGhosts_MPIBAIJ(Mat mat, PetscInt *nghosts, const PetscInt *ghosts[])
2082: {
2083: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ *)mat->data;
2084: Mat_SeqBAIJ *B = (Mat_SeqBAIJ *)baij->B->data;
2086: PetscFunctionBegin;
2087: if (nghosts) *nghosts = B->nbs;
2088: if (ghosts) *ghosts = baij->garray;
2089: PetscFunctionReturn(PETSC_SUCCESS);
2090: }
2092: static PetscErrorCode MatGetSeqNonzeroStructure_MPIBAIJ(Mat A, Mat *newmat)
2093: {
2094: Mat B;
2095: Mat_MPIBAIJ *a = (Mat_MPIBAIJ *)A->data;
2096: Mat_SeqBAIJ *ad = (Mat_SeqBAIJ *)a->A->data, *bd = (Mat_SeqBAIJ *)a->B->data;
2097: Mat_SeqAIJ *b;
2098: PetscMPIInt size, rank, *recvcounts = NULL, *displs = NULL;
2099: PetscInt sendcount, i, *rstarts = A->rmap->range, n, cnt, j, bs = A->rmap->bs;
2100: PetscInt m, *garray = a->garray, *lens, *jsendbuf, *a_jsendbuf, *b_jsendbuf;
2102: PetscFunctionBegin;
2103: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)A), &size));
2104: PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)A), &rank));
2106: /* Tell every processor the number of nonzeros per row */
2107: PetscCall(PetscMalloc1(A->rmap->N / bs, &lens));
2108: for (i = A->rmap->rstart / bs; i < A->rmap->rend / bs; i++) lens[i] = ad->i[i - A->rmap->rstart / bs + 1] - ad->i[i - A->rmap->rstart / bs] + bd->i[i - A->rmap->rstart / bs + 1] - bd->i[i - A->rmap->rstart / bs];
2109: PetscCall(PetscMalloc1(2 * size, &recvcounts));
2110: displs = recvcounts + size;
2111: for (i = 0; i < size; i++) {
2112: PetscCall(PetscMPIIntCast(A->rmap->range[i + 1] / bs - A->rmap->range[i] / bs, &recvcounts[i]));
2113: PetscCall(PetscMPIIntCast(A->rmap->range[i] / bs, &displs[i]));
2114: }
2115: PetscCallMPI(MPI_Allgatherv(MPI_IN_PLACE, 0, MPI_DATATYPE_NULL, lens, recvcounts, displs, MPIU_INT, PetscObjectComm((PetscObject)A)));
2116: /* Create the sequential matrix of the same type as the local block diagonal */
2117: PetscCall(MatCreate(PETSC_COMM_SELF, &B));
2118: PetscCall(MatSetSizes(B, A->rmap->N / bs, A->cmap->N / bs, PETSC_DETERMINE, PETSC_DETERMINE));
2119: PetscCall(MatSetType(B, MATSEQAIJ));
2120: PetscCall(MatSeqAIJSetPreallocation(B, 0, lens));
2121: b = (Mat_SeqAIJ *)B->data;
2123: /* Copy my part of matrix column indices over */
2124: sendcount = ad->nz + bd->nz;
2125: jsendbuf = b->j + b->i[rstarts[rank] / bs];
2126: a_jsendbuf = ad->j;
2127: b_jsendbuf = bd->j;
2128: n = A->rmap->rend / bs - A->rmap->rstart / bs;
2129: cnt = 0;
2130: for (i = 0; i < n; i++) {
2131: /* put in lower diagonal portion */
2132: m = bd->i[i + 1] - bd->i[i];
2133: while (m > 0) {
2134: /* is it above diagonal (in bd (compressed) numbering) */
2135: if (garray[*b_jsendbuf] > A->rmap->rstart / bs + i) break;
2136: jsendbuf[cnt++] = garray[*b_jsendbuf++];
2137: m--;
2138: }
2140: /* put in diagonal portion */
2141: for (j = ad->i[i]; j < ad->i[i + 1]; j++) jsendbuf[cnt++] = A->rmap->rstart / bs + *a_jsendbuf++;
2143: /* put in upper diagonal portion */
2144: while (m-- > 0) jsendbuf[cnt++] = garray[*b_jsendbuf++];
2145: }
2146: PetscCheck(cnt == sendcount, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Corrupted PETSc matrix: nz given %" PetscInt_FMT " actual nz %" PetscInt_FMT, sendcount, cnt);
2148: /* Gather all column indices to all processors */
2149: for (i = 0; i < size; i++) {
2150: recvcounts[i] = 0;
2151: for (j = A->rmap->range[i] / bs; j < A->rmap->range[i + 1] / bs; j++) recvcounts[i] += lens[j];
2152: }
2153: displs[0] = 0;
2154: for (i = 1; i < size; i++) displs[i] = displs[i - 1] + recvcounts[i - 1];
2155: PetscCallMPI(MPI_Allgatherv(MPI_IN_PLACE, 0, MPI_DATATYPE_NULL, b->j, recvcounts, displs, MPIU_INT, PetscObjectComm((PetscObject)A)));
2156: /* Assemble the matrix into usable form (note numerical values not yet set) */
2157: /* set the b->ilen (length of each row) values */
2158: PetscCall(PetscArraycpy(b->ilen, lens, A->rmap->N / bs));
2159: /* set the b->i indices */
2160: b->i[0] = 0;
2161: for (i = 1; i <= A->rmap->N / bs; i++) b->i[i] = b->i[i - 1] + lens[i - 1];
2162: PetscCall(PetscFree(lens));
2163: PetscCall(MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY));
2164: PetscCall(MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY));
2165: PetscCall(PetscFree(recvcounts));
2167: PetscCall(MatPropagateSymmetryOptions(A, B));
2168: *newmat = B;
2169: PetscFunctionReturn(PETSC_SUCCESS);
2170: }
2172: static PetscErrorCode MatSOR_MPIBAIJ(Mat matin, Vec bb, PetscReal omega, MatSORType flag, PetscReal fshift, PetscInt its, PetscInt lits, Vec xx)
2173: {
2174: Mat_MPIBAIJ *mat = (Mat_MPIBAIJ *)matin->data;
2175: Vec bb1 = NULL;
2177: PetscFunctionBegin;
2178: if (flag == SOR_APPLY_UPPER) {
2179: PetscUseTypeMethod(mat->A, sor, bb, omega, flag, fshift, lits, 1, xx);
2180: PetscFunctionReturn(PETSC_SUCCESS);
2181: }
2183: if (its > 1 || ~flag & SOR_ZERO_INITIAL_GUESS) PetscCall(VecDuplicate(bb, &bb1));
2185: if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP) {
2186: if (flag & SOR_ZERO_INITIAL_GUESS) {
2187: PetscUseTypeMethod(mat->A, sor, bb, omega, flag, fshift, lits, 1, xx);
2188: its--;
2189: }
2191: while (its--) {
2192: PetscCall(VecScatterBegin(mat->Mvctx, xx, mat->lvec, INSERT_VALUES, SCATTER_FORWARD));
2193: PetscCall(VecScatterEnd(mat->Mvctx, xx, mat->lvec, INSERT_VALUES, SCATTER_FORWARD));
2195: /* update rhs: bb1 = bb - B*x */
2196: PetscCall(VecScale(mat->lvec, -1.0));
2197: PetscUseTypeMethod(mat->B, multadd, mat->lvec, bb, bb1);
2199: /* local sweep */
2200: PetscUseTypeMethod(mat->A, sor, bb1, omega, SOR_SYMMETRIC_SWEEP, fshift, lits, 1, xx);
2201: }
2202: } else if (flag & SOR_LOCAL_FORWARD_SWEEP) {
2203: if (flag & SOR_ZERO_INITIAL_GUESS) {
2204: PetscUseTypeMethod(mat->A, sor, bb, omega, flag, fshift, lits, 1, xx);
2205: its--;
2206: }
2207: while (its--) {
2208: PetscCall(VecScatterBegin(mat->Mvctx, xx, mat->lvec, INSERT_VALUES, SCATTER_FORWARD));
2209: PetscCall(VecScatterEnd(mat->Mvctx, xx, mat->lvec, INSERT_VALUES, SCATTER_FORWARD));
2211: /* update rhs: bb1 = bb - B*x */
2212: PetscCall(VecScale(mat->lvec, -1.0));
2213: PetscUseTypeMethod(mat->B, multadd, mat->lvec, bb, bb1);
2215: /* local sweep */
2216: PetscUseTypeMethod(mat->A, sor, bb1, omega, SOR_FORWARD_SWEEP, fshift, lits, 1, xx);
2217: }
2218: } else if (flag & SOR_LOCAL_BACKWARD_SWEEP) {
2219: if (flag & SOR_ZERO_INITIAL_GUESS) {
2220: PetscUseTypeMethod(mat->A, sor, bb, omega, flag, fshift, lits, 1, xx);
2221: its--;
2222: }
2223: while (its--) {
2224: PetscCall(VecScatterBegin(mat->Mvctx, xx, mat->lvec, INSERT_VALUES, SCATTER_FORWARD));
2225: PetscCall(VecScatterEnd(mat->Mvctx, xx, mat->lvec, INSERT_VALUES, SCATTER_FORWARD));
2227: /* update rhs: bb1 = bb - B*x */
2228: PetscCall(VecScale(mat->lvec, -1.0));
2229: PetscUseTypeMethod(mat->B, multadd, mat->lvec, bb, bb1);
2231: /* local sweep */
2232: PetscUseTypeMethod(mat->A, sor, bb1, omega, SOR_BACKWARD_SWEEP, fshift, lits, 1, xx);
2233: }
2234: } else SETERRQ(PetscObjectComm((PetscObject)matin), PETSC_ERR_SUP, "Parallel version of SOR requested not supported");
2236: PetscCall(VecDestroy(&bb1));
2237: PetscFunctionReturn(PETSC_SUCCESS);
2238: }
2240: static PetscErrorCode MatGetColumnReductions_MPIBAIJ(Mat A, PetscInt type, PetscReal *reductions)
2241: {
2242: Mat_MPIBAIJ *aij = (Mat_MPIBAIJ *)A->data;
2243: PetscInt m, N, i, *garray = aij->garray;
2244: PetscInt ib, jb, bs = A->rmap->bs;
2245: Mat_SeqBAIJ *a_aij = (Mat_SeqBAIJ *)aij->A->data;
2246: MatScalar *a_val = a_aij->a;
2247: Mat_SeqBAIJ *b_aij = (Mat_SeqBAIJ *)aij->B->data;
2248: MatScalar *b_val = b_aij->a;
2250: PetscFunctionBegin;
2251: PetscCall(MatGetSize(A, &m, &N));
2252: PetscCall(PetscArrayzero(reductions, N));
2253: if (type == NORM_2) {
2254: for (i = a_aij->i[0]; i < a_aij->i[aij->A->rmap->n / bs]; i++) {
2255: for (jb = 0; jb < bs; jb++) {
2256: for (ib = 0; ib < bs; ib++) {
2257: reductions[A->cmap->rstart + a_aij->j[i] * bs + jb] += PetscAbsScalar(*a_val * *a_val);
2258: a_val++;
2259: }
2260: }
2261: }
2262: for (i = b_aij->i[0]; i < b_aij->i[aij->B->rmap->n / bs]; i++) {
2263: for (jb = 0; jb < bs; jb++) {
2264: for (ib = 0; ib < bs; ib++) {
2265: reductions[garray[b_aij->j[i]] * bs + jb] += PetscAbsScalar(*b_val * *b_val);
2266: b_val++;
2267: }
2268: }
2269: }
2270: } else if (type == NORM_1) {
2271: for (i = a_aij->i[0]; i < a_aij->i[aij->A->rmap->n / bs]; i++) {
2272: for (jb = 0; jb < bs; jb++) {
2273: for (ib = 0; ib < bs; ib++) {
2274: reductions[A->cmap->rstart + a_aij->j[i] * bs + jb] += PetscAbsScalar(*a_val);
2275: a_val++;
2276: }
2277: }
2278: }
2279: for (i = b_aij->i[0]; i < b_aij->i[aij->B->rmap->n / bs]; i++) {
2280: for (jb = 0; jb < bs; jb++) {
2281: for (ib = 0; ib < bs; ib++) {
2282: reductions[garray[b_aij->j[i]] * bs + jb] += PetscAbsScalar(*b_val);
2283: b_val++;
2284: }
2285: }
2286: }
2287: } else if (type == NORM_INFINITY) {
2288: for (i = a_aij->i[0]; i < a_aij->i[aij->A->rmap->n / bs]; i++) {
2289: for (jb = 0; jb < bs; jb++) {
2290: for (ib = 0; ib < bs; ib++) {
2291: PetscInt col = A->cmap->rstart + a_aij->j[i] * bs + jb;
2292: reductions[col] = PetscMax(PetscAbsScalar(*a_val), reductions[col]);
2293: a_val++;
2294: }
2295: }
2296: }
2297: for (i = b_aij->i[0]; i < b_aij->i[aij->B->rmap->n / bs]; i++) {
2298: for (jb = 0; jb < bs; jb++) {
2299: for (ib = 0; ib < bs; ib++) {
2300: PetscInt col = garray[b_aij->j[i]] * bs + jb;
2301: reductions[col] = PetscMax(PetscAbsScalar(*b_val), reductions[col]);
2302: b_val++;
2303: }
2304: }
2305: }
2306: } else if (type == REDUCTION_SUM_REALPART || type == REDUCTION_MEAN_REALPART) {
2307: for (i = a_aij->i[0]; i < a_aij->i[aij->A->rmap->n / bs]; i++) {
2308: for (jb = 0; jb < bs; jb++) {
2309: for (ib = 0; ib < bs; ib++) {
2310: reductions[A->cmap->rstart + a_aij->j[i] * bs + jb] += PetscRealPart(*a_val);
2311: a_val++;
2312: }
2313: }
2314: }
2315: for (i = b_aij->i[0]; i < b_aij->i[aij->B->rmap->n / bs]; i++) {
2316: for (jb = 0; jb < bs; jb++) {
2317: for (ib = 0; ib < bs; ib++) {
2318: reductions[garray[b_aij->j[i]] * bs + jb] += PetscRealPart(*b_val);
2319: b_val++;
2320: }
2321: }
2322: }
2323: } else {
2324: PetscCheck(type == REDUCTION_SUM_IMAGINARYPART || type == REDUCTION_MEAN_IMAGINARYPART, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Unknown reduction type");
2325: for (i = a_aij->i[0]; i < a_aij->i[aij->A->rmap->n / bs]; i++) {
2326: for (jb = 0; jb < bs; jb++) {
2327: for (ib = 0; ib < bs; ib++) {
2328: reductions[A->cmap->rstart + a_aij->j[i] * bs + jb] += PetscImaginaryPart(*a_val);
2329: a_val++;
2330: }
2331: }
2332: }
2333: for (i = b_aij->i[0]; i < b_aij->i[aij->B->rmap->n / bs]; i++) {
2334: for (jb = 0; jb < bs; jb++) {
2335: for (ib = 0; ib < bs; ib++) {
2336: reductions[garray[b_aij->j[i]] * bs + jb] += PetscImaginaryPart(*b_val);
2337: b_val++;
2338: }
2339: }
2340: }
2341: }
2342: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, reductions, N, MPIU_REAL, type == NORM_INFINITY ? MPIU_MAX : MPIU_SUM, PetscObjectComm((PetscObject)A)));
2343: if (type == NORM_2) {
2344: for (i = 0; i < N; i++) reductions[i] = PetscSqrtReal(reductions[i]);
2345: } else if (type == REDUCTION_MEAN_REALPART || type == REDUCTION_MEAN_IMAGINARYPART) {
2346: for (i = 0; i < N; i++) reductions[i] /= m;
2347: }
2348: PetscFunctionReturn(PETSC_SUCCESS);
2349: }
2351: static PetscErrorCode MatInvertBlockDiagonal_MPIBAIJ(Mat A, const PetscScalar **values)
2352: {
2353: Mat_MPIBAIJ *a = (Mat_MPIBAIJ *)A->data;
2355: PetscFunctionBegin;
2356: PetscCall(MatInvertBlockDiagonal(a->A, values));
2357: A->factorerrortype = a->A->factorerrortype;
2358: A->factorerror_zeropivot_value = a->A->factorerror_zeropivot_value;
2359: A->factorerror_zeropivot_row = a->A->factorerror_zeropivot_row;
2360: PetscFunctionReturn(PETSC_SUCCESS);
2361: }
2363: static PetscErrorCode MatShift_MPIBAIJ(Mat Y, PetscScalar a)
2364: {
2365: Mat_MPIBAIJ *maij = (Mat_MPIBAIJ *)Y->data;
2366: Mat_SeqBAIJ *aij = (Mat_SeqBAIJ *)maij->A->data;
2368: PetscFunctionBegin;
2369: if (!Y->preallocated) {
2370: PetscCall(MatMPIBAIJSetPreallocation(Y, Y->rmap->bs, 1, NULL, 0, NULL));
2371: } else if (!aij->nz) {
2372: PetscInt nonew = aij->nonew;
2373: PetscCall(MatSeqBAIJSetPreallocation(maij->A, Y->rmap->bs, 1, NULL));
2374: aij->nonew = nonew;
2375: }
2376: PetscCall(MatShift_Basic(Y, a));
2377: PetscFunctionReturn(PETSC_SUCCESS);
2378: }
2380: static PetscErrorCode MatGetDiagonalBlock_MPIBAIJ(Mat A, Mat *a)
2381: {
2382: PetscFunctionBegin;
2383: *a = ((Mat_MPIBAIJ *)A->data)->A;
2384: PetscFunctionReturn(PETSC_SUCCESS);
2385: }
2387: static PetscErrorCode MatEliminateZeros_MPIBAIJ(Mat A, PetscBool keep)
2388: {
2389: Mat_MPIBAIJ *a = (Mat_MPIBAIJ *)A->data;
2391: PetscFunctionBegin;
2392: PetscCall(MatEliminateZeros_SeqBAIJ(a->A, keep)); // possibly keep zero diagonal coefficients
2393: PetscCall(MatEliminateZeros_SeqBAIJ(a->B, PETSC_FALSE)); // never keep zero diagonal coefficients
2394: PetscFunctionReturn(PETSC_SUCCESS);
2395: }
2397: static struct _MatOps MatOps_Values = {MatSetValues_MPIBAIJ,
2398: MatGetRow_MPIBAIJ,
2399: MatRestoreRow_MPIBAIJ,
2400: MatMult_MPIBAIJ,
2401: /* 4*/ MatMultAdd_MPIBAIJ,
2402: MatMultTranspose_MPIBAIJ,
2403: MatMultTransposeAdd_MPIBAIJ,
2404: NULL,
2405: NULL,
2406: NULL,
2407: /*10*/ NULL,
2408: NULL,
2409: NULL,
2410: MatSOR_MPIBAIJ,
2411: MatTranspose_MPIBAIJ,
2412: /*15*/ MatGetInfo_MPIBAIJ,
2413: MatEqual_MPIBAIJ,
2414: MatGetDiagonal_MPIBAIJ,
2415: MatDiagonalScale_MPIBAIJ,
2416: MatNorm_MPIBAIJ,
2417: /*20*/ MatAssemblyBegin_MPIBAIJ,
2418: MatAssemblyEnd_MPIBAIJ,
2419: MatSetOption_MPIBAIJ,
2420: MatZeroEntries_MPIBAIJ,
2421: /*24*/ MatZeroRows_MPIBAIJ,
2422: NULL,
2423: NULL,
2424: NULL,
2425: NULL,
2426: /*29*/ MatSetUp_MPI_Hash,
2427: NULL,
2428: NULL,
2429: MatGetDiagonalBlock_MPIBAIJ,
2430: NULL,
2431: /*34*/ MatDuplicate_MPIBAIJ,
2432: NULL,
2433: NULL,
2434: NULL,
2435: NULL,
2436: /*39*/ MatAXPY_MPIBAIJ,
2437: MatCreateSubMatrices_MPIBAIJ,
2438: MatIncreaseOverlap_MPIBAIJ,
2439: MatGetValues_MPIBAIJ,
2440: MatCopy_MPIBAIJ,
2441: /*44*/ NULL,
2442: MatScale_MPIBAIJ,
2443: MatShift_MPIBAIJ,
2444: NULL,
2445: MatZeroRowsColumns_MPIBAIJ,
2446: /*49*/ NULL,
2447: NULL,
2448: NULL,
2449: NULL,
2450: NULL,
2451: /*54*/ MatFDColoringCreate_MPIXAIJ,
2452: NULL,
2453: MatSetUnfactored_MPIBAIJ,
2454: MatPermute_MPIBAIJ,
2455: MatSetValuesBlocked_MPIBAIJ,
2456: /*59*/ MatCreateSubMatrix_MPIBAIJ,
2457: MatDestroy_MPIBAIJ,
2458: MatView_MPIBAIJ,
2459: NULL,
2460: NULL,
2461: /*64*/ NULL,
2462: NULL,
2463: NULL,
2464: NULL,
2465: MatGetRowMaxAbs_MPIBAIJ,
2466: /*69*/ NULL,
2467: NULL,
2468: NULL,
2469: MatFDColoringApply_BAIJ,
2470: NULL,
2471: /*74*/ NULL,
2472: NULL,
2473: NULL,
2474: NULL,
2475: MatLoad_MPIBAIJ,
2476: /*79*/ NULL,
2477: NULL,
2478: NULL,
2479: NULL,
2480: NULL,
2481: /*84*/ NULL,
2482: NULL,
2483: NULL,
2484: NULL,
2485: NULL,
2486: /*89*/ NULL,
2487: NULL,
2488: NULL,
2489: NULL,
2490: MatConjugate_MPIBAIJ,
2491: /*94*/ NULL,
2492: NULL,
2493: MatRealPart_MPIBAIJ,
2494: MatImaginaryPart_MPIBAIJ,
2495: NULL,
2496: /*99*/ NULL,
2497: NULL,
2498: NULL,
2499: NULL,
2500: NULL,
2501: /*104*/ MatGetSeqNonzeroStructure_MPIBAIJ,
2502: NULL,
2503: MatGetGhosts_MPIBAIJ,
2504: NULL,
2505: NULL,
2506: /*109*/ NULL,
2507: NULL,
2508: NULL,
2509: NULL,
2510: MatGetMultiProcBlock_MPIBAIJ,
2511: /*114*/ NULL,
2512: MatGetColumnReductions_MPIBAIJ,
2513: MatInvertBlockDiagonal_MPIBAIJ,
2514: NULL,
2515: NULL,
2516: /*119*/ NULL,
2517: NULL,
2518: NULL,
2519: NULL,
2520: NULL,
2521: /*124*/ NULL,
2522: MatSetBlockSizes_Default,
2523: NULL,
2524: MatFDColoringSetUp_MPIXAIJ,
2525: NULL,
2526: /*129*/ MatCreateMPIMatConcatenateSeqMat_MPIBAIJ,
2527: NULL,
2528: NULL,
2529: NULL,
2530: NULL,
2531: /*134*/ NULL,
2532: MatEliminateZeros_MPIBAIJ,
2533: MatGetRowSumAbs_MPIBAIJ,
2534: NULL,
2535: NULL,
2536: /*139*/ NULL,
2537: MatCopyHashToXAIJ_MPI_Hash,
2538: NULL,
2539: NULL,
2540: NULL,
2541: /*144*/ NULL,
2542: NULL,
2543: NULL,
2544: NULL};
2546: PETSC_INTERN PetscErrorCode MatConvert_MPIBAIJ_MPISBAIJ(Mat, MatType, MatReuse, Mat *);
2547: PETSC_INTERN PetscErrorCode MatConvert_XAIJ_IS(Mat, MatType, MatReuse, Mat *);
2549: static PetscErrorCode MatMPIBAIJSetPreallocationCSR_MPIBAIJ(Mat B, PetscInt bs, const PetscInt ii[], const PetscInt jj[], const PetscScalar V[])
2550: {
2551: PetscInt m, rstart, cstart, cend;
2552: PetscInt i, j, dlen, olen, nz, nz_max = 0, *d_nnz = NULL, *o_nnz = NULL;
2553: const PetscInt *JJ = NULL;
2554: PetscScalar *values = NULL;
2555: PetscBool roworiented = ((Mat_MPIBAIJ *)B->data)->roworiented;
2556: PetscBool nooffprocentries;
2558: PetscFunctionBegin;
2559: PetscCall(PetscLayoutSetBlockSize(B->rmap, bs));
2560: PetscCall(PetscLayoutSetBlockSize(B->cmap, bs));
2561: PetscCall(PetscLayoutSetUp(B->rmap));
2562: PetscCall(PetscLayoutSetUp(B->cmap));
2563: PetscCall(PetscLayoutGetBlockSize(B->rmap, &bs));
2564: m = B->rmap->n / bs;
2565: rstart = B->rmap->rstart / bs;
2566: cstart = B->cmap->rstart / bs;
2567: cend = B->cmap->rend / bs;
2569: PetscCheck(!ii[0], PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "ii[0] must be 0 but it is %" PetscInt_FMT, ii[0]);
2570: PetscCall(PetscMalloc2(m, &d_nnz, m, &o_nnz));
2571: for (i = 0; i < m; i++) {
2572: nz = ii[i + 1] - ii[i];
2573: PetscCheck(nz >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Local row %" PetscInt_FMT " has a negative number of columns %" PetscInt_FMT, i, nz);
2574: nz_max = PetscMax(nz_max, nz);
2575: dlen = 0;
2576: olen = 0;
2577: JJ = jj + ii[i];
2578: for (j = 0; j < nz; j++) {
2579: if (*JJ < cstart || *JJ >= cend) olen++;
2580: else dlen++;
2581: JJ++;
2582: }
2583: d_nnz[i] = dlen;
2584: o_nnz[i] = olen;
2585: }
2586: PetscCall(MatMPIBAIJSetPreallocation(B, bs, 0, d_nnz, 0, o_nnz));
2587: PetscCall(PetscFree2(d_nnz, o_nnz));
2589: values = (PetscScalar *)V;
2590: if (!values) PetscCall(PetscCalloc1(bs * bs * nz_max, &values));
2591: for (i = 0; i < m; i++) {
2592: PetscInt row = i + rstart;
2593: PetscInt ncols = ii[i + 1] - ii[i];
2594: const PetscInt *icols = jj + ii[i];
2595: if (bs == 1 || !roworiented) { /* block ordering matches the non-nested layout of MatSetValues so we can insert entire rows */
2596: const PetscScalar *svals = values + (V ? (bs * bs * ii[i]) : 0);
2597: PetscCall(MatSetValuesBlocked_MPIBAIJ(B, 1, &row, ncols, icols, svals, INSERT_VALUES));
2598: } else { /* block ordering does not match so we can only insert one block at a time. */
2599: PetscInt j;
2600: for (j = 0; j < ncols; j++) {
2601: const PetscScalar *svals = values + (V ? (bs * bs * (ii[i] + j)) : 0);
2602: PetscCall(MatSetValuesBlocked_MPIBAIJ(B, 1, &row, 1, &icols[j], svals, INSERT_VALUES));
2603: }
2604: }
2605: }
2607: if (!V) PetscCall(PetscFree(values));
2608: nooffprocentries = B->nooffprocentries;
2609: B->nooffprocentries = PETSC_TRUE;
2610: PetscCall(MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY));
2611: PetscCall(MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY));
2612: B->nooffprocentries = nooffprocentries;
2614: PetscCall(MatSetOption(B, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
2615: PetscFunctionReturn(PETSC_SUCCESS);
2616: }
2618: /*@
2619: MatMPIBAIJSetPreallocationCSR - Creates a sparse parallel matrix in `MATBAIJ` format using the given nonzero structure and (optional) numerical values
2621: Collective
2623: Input Parameters:
2624: + B - the matrix
2625: . bs - the block size
2626: . i - the indices into `j` for the start of each local row (starts with zero)
2627: . j - the column indices for each local row (starts with zero) these must be sorted for each row
2628: - v - optional values in the matrix, use `NULL` if not provided
2630: Level: advanced
2632: Notes:
2633: The `i`, `j`, and `v` arrays ARE copied by this routine into the internal format used by PETSc;
2634: thus you CANNOT change the matrix entries by changing the values of `v` after you have
2635: called this routine.
2637: The order of the entries in values is specified by the `MatOption` `MAT_ROW_ORIENTED`. For example, C programs
2638: may want to use the default `MAT_ROW_ORIENTED` with value `PETSC_TRUE` and use an array v[nnz][bs][bs] where the second index is
2639: over rows within a block and the last index is over columns within a block row. Fortran programs will likely set
2640: `MAT_ROW_ORIENTED` with value `PETSC_FALSE` and use a Fortran array v(bs,bs,nnz) in which the first index is over rows within a
2641: block column and the second index is over columns within a block.
2643: Though this routine has Preallocation() in the name it also sets the exact nonzero locations of the matrix entries and usually the numerical values as well
2645: .seealso: `Mat`, `MatCreate()`, `MatCreateSeqAIJ()`, `MatSetValues()`, `MatMPIBAIJSetPreallocation()`, `MatCreateAIJ()`, `MATMPIAIJ`, `MatCreateMPIBAIJWithArrays()`, `MATMPIBAIJ`
2646: @*/
2647: PetscErrorCode MatMPIBAIJSetPreallocationCSR(Mat B, PetscInt bs, const PetscInt i[], const PetscInt j[], const PetscScalar v[])
2648: {
2649: PetscFunctionBegin;
2653: PetscTryMethod(B, "MatMPIBAIJSetPreallocationCSR_C", (Mat, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[]), (B, bs, i, j, v));
2654: PetscFunctionReturn(PETSC_SUCCESS);
2655: }
2657: PetscErrorCode MatMPIBAIJSetPreallocation_MPIBAIJ(Mat B, PetscInt bs, PetscInt d_nz, const PetscInt *d_nnz, PetscInt o_nz, const PetscInt *o_nnz)
2658: {
2659: Mat_MPIBAIJ *b = (Mat_MPIBAIJ *)B->data;
2660: PetscInt i;
2661: PetscMPIInt size;
2663: PetscFunctionBegin;
2664: if (B->hash_active) {
2665: B->ops[0] = b->cops;
2666: B->hash_active = PETSC_FALSE;
2667: }
2668: if (!B->preallocated) PetscCall(MatStashCreate_Private(PetscObjectComm((PetscObject)B), bs, &B->bstash));
2669: PetscCall(MatSetBlockSize(B, bs));
2670: PetscCall(PetscLayoutSetUp(B->rmap));
2671: PetscCall(PetscLayoutSetUp(B->cmap));
2672: PetscCall(PetscLayoutGetBlockSize(B->rmap, &bs));
2674: if (d_nnz) {
2675: for (i = 0; i < B->rmap->n / bs; i++) PetscCheck(d_nnz[i] >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "d_nnz cannot be less than -1: local row %" PetscInt_FMT " value %" PetscInt_FMT, i, d_nnz[i]);
2676: }
2677: if (o_nnz) {
2678: for (i = 0; i < B->rmap->n / bs; i++) PetscCheck(o_nnz[i] >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "o_nnz cannot be less than -1: local row %" PetscInt_FMT " value %" PetscInt_FMT, i, o_nnz[i]);
2679: }
2681: b->bs2 = bs * bs;
2682: b->mbs = B->rmap->n / bs;
2683: b->nbs = B->cmap->n / bs;
2684: b->Mbs = B->rmap->N / bs;
2685: b->Nbs = B->cmap->N / bs;
2687: for (i = 0; i <= b->size; i++) b->rangebs[i] = B->rmap->range[i] / bs;
2688: b->rstartbs = B->rmap->rstart / bs;
2689: b->rendbs = B->rmap->rend / bs;
2690: b->cstartbs = B->cmap->rstart / bs;
2691: b->cendbs = B->cmap->rend / bs;
2693: #if PetscDefined(USE_CTABLE)
2694: PetscCall(PetscHMapIDestroy(&b->colmap));
2695: #else
2696: PetscCall(PetscFree(b->colmap));
2697: #endif
2698: PetscCall(PetscFree(b->garray));
2699: PetscCall(VecDestroy(&b->lvec));
2700: PetscCall(VecScatterDestroy(&b->Mvctx));
2702: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)B), &size));
2704: MatSeqXAIJGetOptions_Private(b->B);
2705: PetscCall(MatDestroy(&b->B));
2706: PetscCall(MatCreate(PETSC_COMM_SELF, &b->B));
2707: PetscCall(MatSetSizes(b->B, B->rmap->n, size > 1 ? B->cmap->N : 0, B->rmap->n, size > 1 ? B->cmap->N : 0));
2708: PetscCall(MatSetType(b->B, MATSEQBAIJ));
2709: MatSeqXAIJRestoreOptions_Private(b->B);
2711: MatSeqXAIJGetOptions_Private(b->A);
2712: PetscCall(MatDestroy(&b->A));
2713: PetscCall(MatCreate(PETSC_COMM_SELF, &b->A));
2714: PetscCall(MatSetSizes(b->A, B->rmap->n, B->cmap->n, B->rmap->n, B->cmap->n));
2715: PetscCall(MatSetType(b->A, MATSEQBAIJ));
2716: MatSeqXAIJRestoreOptions_Private(b->A);
2718: PetscCall(MatSeqBAIJSetPreallocation(b->A, bs, d_nz, d_nnz));
2719: PetscCall(MatSeqBAIJSetPreallocation(b->B, bs, o_nz, o_nnz));
2720: B->preallocated = PETSC_TRUE;
2721: B->was_assembled = PETSC_FALSE;
2722: B->assembled = PETSC_FALSE;
2723: PetscFunctionReturn(PETSC_SUCCESS);
2724: }
2726: extern PetscErrorCode MatDiagonalScaleLocal_MPIBAIJ(Mat, Vec);
2727: extern PetscErrorCode MatSetHashTableFactor_MPIBAIJ(Mat, PetscReal);
2729: PETSC_INTERN PetscErrorCode MatConvert_MPIBAIJ_MPIAdj(Mat B, MatType newtype, MatReuse reuse, Mat *adj)
2730: {
2731: Mat_MPIBAIJ *b = (Mat_MPIBAIJ *)B->data;
2732: Mat_SeqBAIJ *d = (Mat_SeqBAIJ *)b->A->data, *o = (Mat_SeqBAIJ *)b->B->data;
2733: PetscInt M = B->rmap->n / B->rmap->bs, i, *ii, *jj, cnt, j, k, rstart = B->rmap->rstart / B->rmap->bs;
2734: const PetscInt *id = d->i, *jd = d->j, *io = o->i, *jo = o->j, *garray = b->garray;
2736: PetscFunctionBegin;
2737: PetscCall(PetscMalloc1(M + 1, &ii));
2738: ii[0] = 0;
2739: for (i = 0; i < M; i++) {
2740: PetscCheck((id[i + 1] - id[i]) >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Indices wrong %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT, i, id[i], id[i + 1]);
2741: PetscCheck((io[i + 1] - io[i]) >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Indices wrong %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT, i, io[i], io[i + 1]);
2742: ii[i + 1] = ii[i] + id[i + 1] - id[i] + io[i + 1] - io[i];
2743: /* remove one from count of matrix has diagonal */
2744: for (j = id[i]; j < id[i + 1]; j++) {
2745: if (jd[j] == i) {
2746: ii[i + 1]--;
2747: break;
2748: }
2749: }
2750: }
2751: PetscCall(PetscMalloc1(ii[M], &jj));
2752: cnt = 0;
2753: for (i = 0; i < M; i++) {
2754: for (j = io[i]; j < io[i + 1]; j++) {
2755: if (garray[jo[j]] > rstart) break;
2756: jj[cnt++] = garray[jo[j]];
2757: }
2758: for (k = id[i]; k < id[i + 1]; k++) {
2759: if (jd[k] != i) jj[cnt++] = rstart + jd[k];
2760: }
2761: for (; j < io[i + 1]; j++) jj[cnt++] = garray[jo[j]];
2762: }
2763: PetscCall(MatCreateMPIAdj(PetscObjectComm((PetscObject)B), M, B->cmap->N / B->rmap->bs, ii, jj, NULL, adj));
2764: PetscFunctionReturn(PETSC_SUCCESS);
2765: }
2767: #include <../src/mat/impls/aij/mpi/mpiaij.h>
2769: PETSC_INTERN PetscErrorCode MatConvert_SeqBAIJ_SeqAIJ(Mat, MatType, MatReuse, Mat *);
2771: PETSC_INTERN PetscErrorCode MatConvert_MPIBAIJ_MPIAIJ(Mat A, MatType newtype, MatReuse reuse, Mat *newmat)
2772: {
2773: Mat_MPIBAIJ *a = (Mat_MPIBAIJ *)A->data;
2774: Mat_MPIAIJ *b;
2775: Mat B;
2777: PetscFunctionBegin;
2778: PetscCheck(A->assembled, PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "Matrix must be assembled");
2780: if (reuse == MAT_REUSE_MATRIX) {
2781: B = *newmat;
2782: } else {
2783: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &B));
2784: PetscCall(MatSetType(B, MATMPIAIJ));
2785: PetscCall(MatSetSizes(B, A->rmap->n, A->cmap->n, A->rmap->N, A->cmap->N));
2786: PetscCall(MatSetBlockSizes(B, A->rmap->bs, A->cmap->bs));
2787: PetscCall(MatSeqAIJSetPreallocation(B, 0, NULL));
2788: PetscCall(MatMPIAIJSetPreallocation(B, 0, NULL, 0, NULL));
2789: }
2790: b = (Mat_MPIAIJ *)B->data;
2792: if (reuse == MAT_REUSE_MATRIX) {
2793: PetscCall(MatConvert_SeqBAIJ_SeqAIJ(a->A, MATSEQAIJ, MAT_REUSE_MATRIX, &b->A));
2794: PetscCall(MatConvert_SeqBAIJ_SeqAIJ(a->B, MATSEQAIJ, MAT_REUSE_MATRIX, &b->B));
2795: } else {
2796: PetscInt *garray = a->garray;
2797: Mat_SeqAIJ *bB;
2798: PetscInt bs, nnz;
2799: PetscCall(MatDestroy(&b->A));
2800: PetscCall(MatDestroy(&b->B));
2801: /* just clear out the data structure */
2802: PetscCall(MatDisAssemble_MPIAIJ(B, PETSC_FALSE));
2803: PetscCall(MatConvert_SeqBAIJ_SeqAIJ(a->A, MATSEQAIJ, MAT_INITIAL_MATRIX, &b->A));
2804: PetscCall(MatConvert_SeqBAIJ_SeqAIJ(a->B, MATSEQAIJ, MAT_INITIAL_MATRIX, &b->B));
2806: /* Global numbering for b->B columns */
2807: bB = (Mat_SeqAIJ *)b->B->data;
2808: bs = A->rmap->bs;
2809: nnz = bB->i[A->rmap->n];
2810: for (PetscInt k = 0; k < nnz; k++) {
2811: PetscInt bj = bB->j[k] / bs;
2812: PetscInt br = bB->j[k] % bs;
2813: bB->j[k] = garray[bj] * bs + br;
2814: }
2815: }
2816: PetscCall(MatSetOption(B, MAT_NO_OFF_PROC_ENTRIES, PETSC_TRUE));
2817: PetscCall(MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY));
2818: PetscCall(MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY));
2819: PetscCall(MatSetOption(B, MAT_NO_OFF_PROC_ENTRIES, PETSC_FALSE));
2821: if (reuse == MAT_INPLACE_MATRIX) {
2822: PetscCall(MatHeaderReplace(A, &B));
2823: } else {
2824: *newmat = B;
2825: }
2826: PetscFunctionReturn(PETSC_SUCCESS);
2827: }
2829: /*MC
2830: MATMPIBAIJ - MATMPIBAIJ = "mpibaij" - A matrix type to be used for distributed block sparse matrices.
2832: Options Database Keys:
2833: + -mat_type mpibaij - sets the matrix type to `MATMPIBAIJ` during a call to `MatSetFromOptions()`
2834: . -mat_block_size bs - set the blocksize used to store the matrix
2835: . -mat_baij_mult_version version - indicate the version of the matrix-vector product to use (0 often indicates using BLAS)
2836: - -mat_use_hash_table fact - set hash table factor
2838: Level: beginner
2840: Note:
2841: `MatSetOption(A, MAT_STRUCTURE_ONLY, PETSC_TRUE)` may be called for this matrix type. In this no
2842: space is allocated for the nonzero entries and any entries passed with `MatSetValues()` are ignored
2844: .seealso: `Mat`, `MATBAIJ`, `MATSEQBAIJ`, `MatCreateBAIJ`
2845: M*/
2847: typedef struct {
2848: MPIAIJ_MPIDense scatter;
2849: Mat workC;
2850: } MPIBAIJ_MPIDense;
2852: static PetscErrorCode MatMPIBAIJ_MPIDenseDestroy(PetscCtxRt ctx)
2853: {
2854: MPIBAIJ_MPIDense *data = *(MPIBAIJ_MPIDense **)ctx;
2856: PetscFunctionBegin;
2857: PetscCall(MatDestroy(&data->workC));
2858: PetscCall(MatMPIDenseScatterDestroy_Private(&data->scatter));
2859: PetscCall(PetscFree(data));
2860: PetscFunctionReturn(PETSC_SUCCESS);
2861: }
2863: static PetscErrorCode MatMPIDenseScatter_MPIBAIJ(Mat A, Mat B, Mat workB, Mat C)
2864: {
2865: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ *)A->data;
2866: MPIBAIJ_MPIDense *data = (MPIBAIJ_MPIDense *)C->product->data;
2867: PetscInt bs;
2869: PetscFunctionBegin;
2870: PetscCall(MatGetBlockSize(A, &bs));
2871: PetscCall(MatMPIDenseScatter_Private(baij->Mvctx, baij->B->cmap->n, bs, workB, &data->scatter, B, C));
2872: PetscFunctionReturn(PETSC_SUCCESS);
2873: }
2875: static PetscErrorCode MatMatMultNumeric_MPIBAIJ_MPIDense(Mat A, Mat B, Mat C)
2876: {
2877: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ *)A->data;
2878: Mat_MPIDense *bdense = (Mat_MPIDense *)B->data;
2879: Mat_MPIDense *cdense = (Mat_MPIDense *)C->data;
2880: Mat workB;
2881: MPIBAIJ_MPIDense *data;
2883: PetscFunctionBegin;
2884: MatCheckProduct(C, 3);
2885: PetscCheck(C->product->data, PetscObjectComm((PetscObject)C), PETSC_ERR_PLIB, "Product data empty");
2886: data = (MPIBAIJ_MPIDense *)C->product->data;
2887: if (!cdense->A->product) {
2888: PetscCall(MatProductCreateWithMat(baij->A, bdense->A, NULL, cdense->A));
2889: PetscCall(MatProductSetType(cdense->A, MATPRODUCT_AB));
2890: PetscCall(MatProductSetFromOptions(cdense->A));
2891: PetscCall(MatProductSymbolic(cdense->A));
2892: } else PetscCall(MatProductReplaceMats(baij->A, bdense->A, NULL, cdense->A));
2893: PetscCall(MatProductNumeric(cdense->A));
2895: if (data->scatter.workB->cmap->n == B->cmap->N) {
2896: workB = data->scatter.workB;
2897: PetscCall(MatMPIDenseScatter_MPIBAIJ(A, B, workB, C));
2898: if (data->workC) {
2899: PetscCall(MatProductReplaceMats(baij->B, workB, NULL, data->workC));
2900: PetscCall(MatProductNumeric(data->workC));
2901: PetscCall(MatAXPY(cdense->A, 1.0, data->workC, SAME_NONZERO_PATTERN));
2902: }
2903: } else {
2904: Mat Bb, Cb, workC;
2905: Mat_MPIDense *cbdense;
2906: PetscInt BN = B->cmap->N, n = data->scatter.workB->cmap->n, cols;
2908: PetscCheck(n > 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Column batch size must be positive");
2909: for (PetscInt i = 0; i < BN; i += n) {
2910: cols = PetscMin(n, BN - i);
2911: workB = data->scatter.workB;
2912: workC = data->workC;
2913: if (cols != n) {
2914: PetscCall(MatDenseGetSubMatrix(data->scatter.workB, PETSC_DECIDE, PETSC_DECIDE, 0, cols, &workB));
2915: if (workC) PetscCall(MatDenseGetSubMatrix(data->workC, PETSC_DECIDE, PETSC_DECIDE, 0, cols, &workC));
2916: }
2917: PetscCall(MatDenseGetSubMatrix(B, PETSC_DECIDE, PETSC_DECIDE, i, i + cols, &Bb));
2918: PetscCall(MatDenseGetSubMatrix(C, PETSC_DECIDE, PETSC_DECIDE, i, i + cols, &Cb));
2919: PetscCall(MatMPIDenseScatter_MPIBAIJ(A, Bb, workB, C));
2920: if (workC) {
2921: cbdense = (Mat_MPIDense *)Cb->data;
2922: PetscCall(MatProductReplaceMats(baij->B, workB, NULL, workC));
2923: PetscCall(MatProductNumeric(workC));
2924: PetscCall(MatAXPY(cbdense->A, 1.0, workC, SAME_NONZERO_PATTERN));
2925: }
2926: if (cols != n) {
2927: if (workC) PetscCall(MatDenseRestoreSubMatrix(data->workC, &workC));
2928: PetscCall(MatDenseRestoreSubMatrix(data->scatter.workB, &workB));
2929: }
2930: PetscCall(MatDenseRestoreSubMatrix(B, &Bb));
2931: PetscCall(MatDenseRestoreSubMatrix(C, &Cb));
2932: }
2933: }
2934: PetscFunctionReturn(PETSC_SUCCESS);
2935: }
2937: static PetscErrorCode MatMatMultSymbolic_MPIBAIJ_MPIDense(Mat A, Mat B, PetscReal fill, Mat C)
2938: {
2939: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ *)A->data;
2940: MPIBAIJ_MPIDense *data;
2941: VecScatter ctx = baij->Mvctx;
2942: PetscInt nz = baij->B->cmap->n, bs;
2943: PetscInt Am = A->rmap->n, BN = B->cmap->N, Bbn, numBb;
2944: Mat workB1, workC1;
2945: PetscBool cisdense;
2947: PetscFunctionBegin;
2948: MatCheckProduct(C, 4);
2949: PetscCheck(!C->product->data, PetscObjectComm((PetscObject)C), PETSC_ERR_PLIB, "Product data not empty");
2950: PetscCall(PetscObjectBaseTypeCompare((PetscObject)C, MATMPIDENSE, &cisdense));
2951: if (!cisdense) PetscCall(MatSetType(C, ((PetscObject)B)->type_name));
2952: PetscCall(MatSetSizes(C, Am, B->cmap->n, A->rmap->N, BN));
2953: PetscCall(MatSetBlockSizesFromMats(C, A, B));
2954: PetscCall(MatSetUp(C));
2955: PetscCall(MatGetBlockSize(A, &bs));
2956: PetscCall(PetscNew(&data));
2957: PetscCall(MatMPIDenseScatterSetUp_Private(ctx, nz, bs, Am, B, C, &data->scatter, &Bbn, &numBb));
2959: PetscCall(MatSetOption(C, MAT_NO_OFF_PROC_ENTRIES, PETSC_TRUE));
2960: PetscCall(MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY));
2961: PetscCall(MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY));
2962: PetscCall(MatProductClear(baij->A));
2963: PetscCall(MatProductClear(((Mat_MPIDense *)B->data)->A));
2964: PetscCall(MatProductClear(((Mat_MPIDense *)C->data)->A));
2965: PetscCall(MatProductCreateWithMat(baij->A, ((Mat_MPIDense *)B->data)->A, NULL, ((Mat_MPIDense *)C->data)->A));
2966: PetscCall(MatProductSetType(((Mat_MPIDense *)C->data)->A, MATPRODUCT_AB));
2967: PetscCall(MatProductSetFromOptions(((Mat_MPIDense *)C->data)->A));
2968: PetscCall(MatProductSymbolic(((Mat_MPIDense *)C->data)->A));
2970: if (nz) {
2971: PetscCall(MatCreateSeqDense(PETSC_COMM_SELF, Am, Bbn ? Bbn : BN, NULL, &data->workC));
2972: PetscCall(MatProductCreateWithMat(baij->B, data->scatter.workB, NULL, data->workC));
2973: PetscCall(MatProductSetType(data->workC, MATPRODUCT_AB));
2974: PetscCall(MatProductSetFromOptions(data->workC));
2975: PetscCall(MatProductSymbolic(data->workC));
2976: if (numBb && BN % Bbn) {
2977: PetscCall(MatDenseGetSubMatrix(data->scatter.workB, PETSC_DECIDE, PETSC_DECIDE, 0, BN % Bbn, &workB1));
2978: PetscCall(MatDenseGetSubMatrix(data->workC, PETSC_DECIDE, PETSC_DECIDE, 0, BN % Bbn, &workC1));
2979: PetscCall(MatProductCreateWithMat(baij->B, workB1, NULL, workC1));
2980: PetscCall(MatProductSetType(workC1, MATPRODUCT_AB));
2981: PetscCall(MatProductSetFromOptions(workC1));
2982: PetscCall(MatProductSymbolic(workC1));
2983: PetscCall(MatDenseRestoreSubMatrix(data->workC, &workC1));
2984: PetscCall(MatDenseRestoreSubMatrix(data->scatter.workB, &workB1));
2985: }
2986: }
2988: C->product->data = data;
2989: C->product->destroy = MatMPIBAIJ_MPIDenseDestroy;
2990: C->ops->matmultnumeric = MatMatMultNumeric_MPIBAIJ_MPIDense;
2991: PetscFunctionReturn(PETSC_SUCCESS);
2992: }
2994: PETSC_INTERN PetscErrorCode MatProductSetFromOptions_MPIBAIJ_MPIDense(Mat C)
2995: {
2996: Mat_Product *product = C->product;
2998: PetscFunctionBegin;
2999: MatCheckProduct(C, 1);
3000: if (product->type == MATPRODUCT_AB) {
3001: C->ops->matmultsymbolic = MatMatMultSymbolic_MPIBAIJ_MPIDense;
3002: C->ops->productsymbolic = MatProductSymbolic_AB;
3003: }
3004: PetscFunctionReturn(PETSC_SUCCESS);
3005: }
3007: static PetscErrorCode MatGetMultPetscSF_MPIBAIJ(Mat A, PetscSF *sf)
3008: {
3009: Mat_MPIBAIJ *a = (Mat_MPIBAIJ *)A->data;
3011: PetscFunctionBegin;
3012: *sf = a->Mvctx;
3013: PetscFunctionReturn(PETSC_SUCCESS);
3014: }
3016: PETSC_EXTERN PetscErrorCode MatCreate_MPIBAIJ(Mat B)
3017: {
3018: Mat_MPIBAIJ *b;
3019: PetscBool flg = PETSC_FALSE;
3021: PetscFunctionBegin;
3022: PetscCall(PetscNew(&b));
3023: B->data = (void *)b;
3024: B->ops[0] = MatOps_Values;
3025: B->assembled = PETSC_FALSE;
3027: B->insertmode = NOT_SET_VALUES;
3028: PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)B), &b->rank));
3029: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)B), &b->size));
3031: /* build local table of row and column ownerships */
3032: PetscCall(PetscMalloc1(b->size + 1, &b->rangebs));
3034: /* build cache for off array entries formed */
3035: PetscCall(MatStashCreate_Private(PetscObjectComm((PetscObject)B), 1, &B->stash));
3037: b->donotstash = PETSC_FALSE;
3038: b->colmap = NULL;
3039: b->garray = NULL;
3040: b->roworiented = PETSC_TRUE;
3042: /* stuff used in block assembly */
3043: b->barray = NULL;
3045: /* stuff used for matrix vector multiply */
3046: b->lvec = NULL;
3047: b->Mvctx = NULL;
3049: /* stuff for MatGetRow() */
3050: b->rowindices = NULL;
3051: b->rowvalues = NULL;
3052: b->getrowactive = PETSC_FALSE;
3054: /* hash table stuff */
3055: b->ht = NULL;
3056: b->hd = NULL;
3057: b->ht_size = 0;
3058: b->ht_flag = PETSC_FALSE;
3059: b->ht_fact = 0;
3060: b->ht_total_ct = 0;
3061: b->ht_insert_ct = 0;
3063: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_mpibaij_mpiadj_C", MatConvert_MPIBAIJ_MPIAdj));
3064: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_mpibaij_mpiaij_C", MatConvert_MPIBAIJ_MPIAIJ));
3065: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_mpibaij_mpisbaij_C", MatConvert_MPIBAIJ_MPISBAIJ));
3066: #if PetscDefined(HAVE_HYPRE)
3067: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_mpibaij_hypre_C", MatConvert_AIJ_HYPRE));
3068: #endif
3069: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatStoreValues_C", MatStoreValues_MPIBAIJ));
3070: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatRetrieveValues_C", MatRetrieveValues_MPIBAIJ));
3071: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMPIBAIJSetPreallocation_C", MatMPIBAIJSetPreallocation_MPIBAIJ));
3072: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMPIBAIJSetPreallocationCSR_C", MatMPIBAIJSetPreallocationCSR_MPIBAIJ));
3073: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDiagonalScaleLocal_C", MatDiagonalScaleLocal_MPIBAIJ));
3074: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSetHashTableFactor_C", MatSetHashTableFactor_MPIBAIJ));
3075: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_mpibaij_is_C", MatConvert_XAIJ_IS));
3076: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatGetMultPetscSF_C", MatGetMultPetscSF_MPIBAIJ));
3077: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_mpibaij_mpidense_C", MatProductSetFromOptions_MPIBAIJ_MPIDense));
3078: #if PetscDefined(HAVE_LIBXSMM)
3079: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_mpibaij_mpibaijlibxsmm_C", MatConvert_MPIBAIJ_MPIBAIJLIBXSMM));
3080: #endif
3081: PetscCall(PetscObjectChangeTypeName((PetscObject)B, MATMPIBAIJ));
3083: PetscOptionsBegin(PetscObjectComm((PetscObject)B), NULL, "Options for loading MPIBAIJ matrix 1", "Mat");
3084: PetscCall(PetscOptionsName("-mat_use_hash_table", "Use hash table to save time in constructing matrix", "MatSetOption", &flg));
3085: if (flg) {
3086: PetscReal fact = 1.39;
3087: PetscCall(MatSetOption(B, MAT_USE_HASH_TABLE, PETSC_TRUE));
3088: PetscCall(PetscOptionsReal("-mat_use_hash_table", "Use hash table factor", "MatMPIBAIJSetHashTableFactor", fact, &fact, NULL));
3089: if (fact <= 1.0) fact = 1.39;
3090: PetscCall(MatMPIBAIJSetHashTableFactor(B, fact));
3091: PetscCall(PetscInfo(B, "Hash table Factor used %5.2g\n", (double)fact));
3092: }
3093: PetscOptionsEnd();
3094: PetscFunctionReturn(PETSC_SUCCESS);
3095: }
3097: // PetscClangLinter pragma disable: -fdoc-section-header-unknown
3098: /*MC
3099: MATBAIJ - MATBAIJ = "baij" - A matrix type to be used for block sparse matrices.
3101: This matrix type is identical to `MATSEQBAIJ` when constructed with a single process communicator,
3102: and `MATMPIBAIJ` otherwise.
3104: Options Database Keys:
3105: . -mat_type baij - sets the matrix type to `MATBAIJ` during a call to `MatSetFromOptions()`
3107: Level: beginner
3109: .seealso: `Mat`, `MatCreateBAIJ()`, `MATSEQBAIJ`, `MATMPIBAIJ`, `MatMPIBAIJSetPreallocation()`, `MatMPIBAIJSetPreallocationCSR()`
3110: M*/
3112: /*@
3113: MatMPIBAIJSetPreallocation - Allocates memory for a sparse parallel matrix in `MATMPIBAIJ` format
3114: (block compressed row).
3116: Collective
3118: Input Parameters:
3119: + B - the matrix
3120: . bs - size of block, the blocks are ALWAYS square. One can use `MatSetBlockSizes()` to set a different row and column blocksize but the row
3121: blocksize always defines the size of the blocks. The column blocksize sets the blocksize of the vectors obtained with `MatCreateVecs()`
3122: . d_nz - number of block nonzeros per block row in diagonal portion of local
3123: submatrix (same for all local rows)
3124: . d_nnz - array containing the number of block nonzeros in the various block rows
3125: of the in diagonal portion of the local (possibly different for each block
3126: row) or `NULL`. If you plan to factor the matrix you must leave room for the diagonal entry and
3127: set it even if it is zero.
3128: . o_nz - number of block nonzeros per block row in the off-diagonal portion of local
3129: submatrix (same for all local rows).
3130: - o_nnz - array containing the number of nonzeros in the various block rows of the
3131: off-diagonal portion of the local submatrix (possibly different for
3132: each block row) or `NULL`.
3134: If the *_nnz parameter is given then the *_nz parameter is ignored
3136: Options Database Keys:
3137: + -mat_block_size - size of the blocks to use
3138: - -mat_use_hash_table fact - set hash table factor
3140: Level: intermediate
3142: Notes:
3143: For good matrix assembly performance
3144: the user should preallocate the matrix storage by setting the parameters
3145: `d_nz` (or `d_nnz`) and `o_nz` (or `o_nnz`). By setting these parameters accurately,
3146: performance can be increased by more than a factor of 50.
3148: If `PETSC_DECIDE` or `PETSC_DETERMINE` is used for a particular argument on one processor
3149: than it must be used on all processors that share the object for that argument.
3151: Storage Information:
3152: For a square global matrix we define each processor's diagonal portion
3153: to be its local rows and the corresponding columns (a square submatrix);
3154: each processor's off-diagonal portion encompasses the remainder of the
3155: local matrix (a rectangular submatrix).
3157: The user can specify preallocated storage for the diagonal part of
3158: the local submatrix with either `d_nz` or `d_nnz` (not both). Set
3159: `d_nz` = `PETSC_DEFAULT` and `d_nnz` = `NULL` for PETSc to control dynamic
3160: memory allocation. Likewise, specify preallocated storage for the
3161: off-diagonal part of the local submatrix with `o_nz` or `o_nnz` (not both).
3163: Consider a processor that owns rows 3, 4 and 5 of a parallel matrix. In
3164: the figure below we depict these three local rows and all columns (0-11).
3166: .vb
3167: 0 1 2 3 4 5 6 7 8 9 10 11
3168: --------------------------
3169: row 3 |o o o d d d o o o o o o
3170: row 4 |o o o d d d o o o o o o
3171: row 5 |o o o d d d o o o o o o
3172: --------------------------
3173: .ve
3175: Thus, any entries in the d locations are stored in the d (diagonal)
3176: submatrix, and any entries in the o locations are stored in the
3177: o (off-diagonal) submatrix. Note that the d and the o submatrices are
3178: stored simply in the `MATSEQBAIJ` format for compressed row storage.
3180: Now `d_nz` should indicate the number of block nonzeros per row in the d matrix,
3181: and `o_nz` should indicate the number of block nonzeros per row in the o matrix.
3182: In general, for PDE problems in which most nonzeros are near the diagonal,
3183: one expects `d_nz` >> `o_nz`.
3185: You can call `MatGetInfo()` to get information on how effective the preallocation was;
3186: for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3187: You can also run with the option `-info` and look for messages with the string
3188: malloc in them to see if additional memory allocation was needed.
3190: .seealso: `Mat`, `MATMPIBAIJ`, `MatCreate()`, `MatCreateSeqBAIJ()`, `MatSetValues()`, `MatCreateBAIJ()`, `MatMPIBAIJSetPreallocationCSR()`, `PetscSplitOwnership()`
3191: @*/
3192: PetscErrorCode MatMPIBAIJSetPreallocation(Mat B, PetscInt bs, PetscInt d_nz, const PetscInt d_nnz[], PetscInt o_nz, const PetscInt o_nnz[])
3193: {
3194: PetscFunctionBegin;
3198: PetscTryMethod(B, "MatMPIBAIJSetPreallocation_C", (Mat, PetscInt, PetscInt, const PetscInt[], PetscInt, const PetscInt[]), (B, bs, d_nz, d_nnz, o_nz, o_nnz));
3199: PetscFunctionReturn(PETSC_SUCCESS);
3200: }
3202: // PetscClangLinter pragma disable: -fdoc-section-header-unknown
3203: /*@
3204: MatCreateBAIJ - Creates a sparse parallel matrix in `MATBAIJ` format
3205: (block compressed row).
3207: Collective
3209: Input Parameters:
3210: + comm - MPI communicator
3211: . bs - size of block, the blocks are ALWAYS square. One can use `MatSetBlockSizes()` to set a different row and column blocksize but the row
3212: blocksize always defines the size of the blocks. The column blocksize sets the blocksize of the vectors obtained with `MatCreateVecs()`
3213: . m - number of local rows (or `PETSC_DECIDE` to have calculated if M is given)
3214: This value should be the same as the local size used in creating the
3215: y vector for the matrix-vector product y = Ax.
3216: . n - number of local columns (or `PETSC_DECIDE` to have calculated if N is given)
3217: This value should be the same as the local size used in creating the
3218: x vector for the matrix-vector product y = Ax.
3219: . M - number of global rows (or `PETSC_DETERMINE` to have calculated if m is given)
3220: . N - number of global columns (or `PETSC_DETERMINE` to have calculated if n is given)
3221: . d_nz - number of nonzero blocks per block row in diagonal portion of local
3222: submatrix (same for all local rows)
3223: . d_nnz - array containing the number of nonzero blocks in the various block rows
3224: of the in diagonal portion of the local (possibly different for each block
3225: row) or NULL. If you plan to factor the matrix you must leave room for the diagonal entry
3226: and set it even if it is zero.
3227: . o_nz - number of nonzero blocks per block row in the off-diagonal portion of local
3228: submatrix (same for all local rows).
3229: - o_nnz - array containing the number of nonzero blocks in the various block rows of the
3230: off-diagonal portion of the local submatrix (possibly different for
3231: each block row) or NULL.
3233: Output Parameter:
3234: . A - the matrix
3236: Options Database Keys:
3237: + -mat_block_size - size of the blocks to use
3238: - -mat_use_hash_table fact - set hash table factor
3240: Level: intermediate
3242: Notes:
3243: It is recommended that one use `MatCreateFromOptions()` or the `MatCreate()`, `MatSetType()` and/or `MatSetFromOptions()`,
3244: MatXXXXSetPreallocation() paradigm instead of this routine directly.
3245: [MatXXXXSetPreallocation() is, for example, `MatSeqBAIJSetPreallocation()`]
3247: For good matrix assembly performance
3248: the user should preallocate the matrix storage by setting the parameters
3249: `d_nz` (or `d_nnz`) and `o_nz` (or `o_nnz`). By setting these parameters accurately,
3250: performance can be increased by more than a factor of 50.
3252: If the *_nnz parameter is given then the *_nz parameter is ignored
3254: A nonzero block is any block that as 1 or more nonzeros in it
3256: The user MUST specify either the local or global matrix dimensions
3257: (possibly both).
3259: If `PETSC_DECIDE` or `PETSC_DETERMINE` is used for a particular argument on one processor
3260: than it must be used on all processors that share the object for that argument.
3262: If `m` and `n` are not `PETSC_DECIDE`, then the values determine the `PetscLayout` of the matrix and the ranges returned by
3263: `MatGetOwnershipRange()`, `MatGetOwnershipRanges()`, `MatGetOwnershipRangeColumn()`, and `MatGetOwnershipRangesColumn()`.
3265: Storage Information:
3266: For a square global matrix we define each processor's diagonal portion
3267: to be its local rows and the corresponding columns (a square submatrix);
3268: each processor's off-diagonal portion encompasses the remainder of the
3269: local matrix (a rectangular submatrix).
3271: The user can specify preallocated storage for the diagonal part of
3272: the local submatrix with either d_nz or d_nnz (not both). Set
3273: `d_nz` = `PETSC_DEFAULT` and `d_nnz` = `NULL` for PETSc to control dynamic
3274: memory allocation. Likewise, specify preallocated storage for the
3275: off-diagonal part of the local submatrix with `o_nz` or `o_nnz` (not both).
3277: Consider a processor that owns rows 3, 4 and 5 of a parallel matrix. In
3278: the figure below we depict these three local rows and all columns (0-11).
3280: .vb
3281: 0 1 2 3 4 5 6 7 8 9 10 11
3282: --------------------------
3283: row 3 |o o o d d d o o o o o o
3284: row 4 |o o o d d d o o o o o o
3285: row 5 |o o o d d d o o o o o o
3286: --------------------------
3287: .ve
3289: Thus, any entries in the d locations are stored in the d (diagonal)
3290: submatrix, and any entries in the o locations are stored in the
3291: o (off-diagonal) submatrix. Note that the d and the o submatrices are
3292: stored simply in the `MATSEQBAIJ` format for compressed row storage.
3294: Now `d_nz` should indicate the number of block nonzeros per row in the d matrix,
3295: and `o_nz` should indicate the number of block nonzeros per row in the o matrix.
3296: In general, for PDE problems in which most nonzeros are near the diagonal,
3297: one expects `d_nz` >> `o_nz`.
3299: .seealso: `Mat`, `MatCreate()`, `MatCreateSeqBAIJ()`, `MatSetValues()`, `MatMPIBAIJSetPreallocation()`, `MatMPIBAIJSetPreallocationCSR()`,
3300: `MatGetOwnershipRange()`, `MatGetOwnershipRanges()`, `MatGetOwnershipRangeColumn()`, `MatGetOwnershipRangesColumn()`, `PetscLayout`
3301: @*/
3302: PetscErrorCode MatCreateBAIJ(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)
3303: {
3304: PetscMPIInt size;
3306: PetscFunctionBegin;
3307: PetscCall(MatCreate(comm, A));
3308: PetscCall(MatSetSizes(*A, m, n, M, N));
3309: PetscCallMPI(MPI_Comm_size(comm, &size));
3310: if (size > 1) {
3311: PetscCall(MatSetType(*A, MATMPIBAIJ));
3312: PetscCall(MatMPIBAIJSetPreallocation(*A, bs, d_nz, d_nnz, o_nz, o_nnz));
3313: } else {
3314: PetscCall(MatSetType(*A, MATSEQBAIJ));
3315: PetscCall(MatSeqBAIJSetPreallocation(*A, bs, d_nz, d_nnz));
3316: }
3317: PetscFunctionReturn(PETSC_SUCCESS);
3318: }
3320: static PetscErrorCode MatDuplicate_MPIBAIJ(Mat matin, MatDuplicateOption cpvalues, Mat *newmat)
3321: {
3322: Mat mat;
3323: Mat_MPIBAIJ *a, *oldmat = (Mat_MPIBAIJ *)matin->data;
3324: PetscInt len = 0;
3326: PetscFunctionBegin;
3327: *newmat = NULL;
3328: PetscCall(MatCreate(PetscObjectComm((PetscObject)matin), &mat));
3329: PetscCall(MatSetSizes(mat, matin->rmap->n, matin->cmap->n, matin->rmap->N, matin->cmap->N));
3330: PetscCall(MatSetType(mat, ((PetscObject)matin)->type_name));
3332: PetscCall(PetscLayoutReference(matin->rmap, &mat->rmap));
3333: PetscCall(PetscLayoutReference(matin->cmap, &mat->cmap));
3334: if (matin->hash_active) PetscCall(MatSetUp(mat));
3335: else {
3336: mat->factortype = matin->factortype;
3337: mat->preallocated = PETSC_TRUE;
3338: mat->assembled = PETSC_TRUE;
3339: mat->insertmode = NOT_SET_VALUES;
3341: a = (Mat_MPIBAIJ *)mat->data;
3342: mat->rmap->bs = matin->rmap->bs;
3343: a->bs2 = oldmat->bs2;
3344: a->mbs = oldmat->mbs;
3345: a->nbs = oldmat->nbs;
3346: a->Mbs = oldmat->Mbs;
3347: a->Nbs = oldmat->Nbs;
3349: a->size = oldmat->size;
3350: a->rank = oldmat->rank;
3351: a->donotstash = oldmat->donotstash;
3352: a->roworiented = oldmat->roworiented;
3353: a->rowindices = NULL;
3354: a->rowvalues = NULL;
3355: a->getrowactive = PETSC_FALSE;
3356: a->barray = NULL;
3357: a->rstartbs = oldmat->rstartbs;
3358: a->rendbs = oldmat->rendbs;
3359: a->cstartbs = oldmat->cstartbs;
3360: a->cendbs = oldmat->cendbs;
3362: /* hash table stuff */
3363: a->ht = NULL;
3364: a->hd = NULL;
3365: a->ht_size = 0;
3366: a->ht_flag = oldmat->ht_flag;
3367: a->ht_fact = oldmat->ht_fact;
3368: a->ht_total_ct = 0;
3369: a->ht_insert_ct = 0;
3371: PetscCall(PetscArraycpy(a->rangebs, oldmat->rangebs, a->size + 1));
3372: if (oldmat->colmap) {
3373: #if PetscDefined(USE_CTABLE)
3374: PetscCall(PetscHMapIDuplicate(oldmat->colmap, &a->colmap));
3375: #else
3376: PetscCall(PetscMalloc1(a->Nbs, &a->colmap));
3377: PetscCall(PetscArraycpy(a->colmap, oldmat->colmap, a->Nbs));
3378: #endif
3379: } else a->colmap = NULL;
3381: if (oldmat->garray && (len = ((Mat_SeqBAIJ *)oldmat->B->data)->nbs)) {
3382: PetscCall(PetscMalloc1(len, &a->garray));
3383: PetscCall(PetscArraycpy(a->garray, oldmat->garray, len));
3384: } else a->garray = NULL;
3386: PetscCall(MatStashCreate_Private(PetscObjectComm((PetscObject)matin), matin->rmap->bs, &mat->bstash));
3387: PetscCall(VecDuplicate(oldmat->lvec, &a->lvec));
3388: PetscCall(VecScatterCopy(oldmat->Mvctx, &a->Mvctx));
3390: PetscCall(MatDuplicate(oldmat->A, cpvalues, &a->A));
3391: PetscCall(MatDuplicate(oldmat->B, cpvalues, &a->B));
3392: }
3393: PetscCall(PetscFunctionListDuplicate(((PetscObject)matin)->qlist, &((PetscObject)mat)->qlist));
3394: *newmat = mat;
3395: PetscFunctionReturn(PETSC_SUCCESS);
3396: }
3398: /* Used for both MPIBAIJ and MPISBAIJ matrices */
3399: PetscErrorCode MatLoad_MPIBAIJ_Binary(Mat mat, PetscViewer viewer)
3400: {
3401: PetscInt header[4], M, N, nz, bs, m, n, mbs, nbs, rows, cols, sum, i, j, k;
3402: PetscInt *rowidxs, *colidxs, rs, cs, ce;
3403: PetscScalar *matvals;
3404: PetscBool nooffprocentries = mat->nooffprocentries;
3406: PetscFunctionBegin;
3407: PetscCall(PetscViewerSetUp(viewer));
3409: /* read in matrix header */
3410: PetscCall(PetscViewerBinaryRead(viewer, header, 4, NULL, PETSC_INT));
3411: PetscCheck(header[0] == MAT_FILE_CLASSID, PetscObjectComm((PetscObject)viewer), PETSC_ERR_FILE_UNEXPECTED, "Not a matrix object in file");
3412: M = header[1];
3413: N = header[2];
3414: nz = header[3];
3415: PetscCheck(M >= 0, PetscObjectComm((PetscObject)viewer), PETSC_ERR_FILE_UNEXPECTED, "Matrix row size (%" PetscInt_FMT ") in file is negative", M);
3416: PetscCheck(N >= 0, PetscObjectComm((PetscObject)viewer), PETSC_ERR_FILE_UNEXPECTED, "Matrix column size (%" PetscInt_FMT ") in file is negative", N);
3417: PetscCheck(nz >= 0, PETSC_COMM_SELF, PETSC_ERR_FILE_UNEXPECTED, "Matrix stored in special format on disk, cannot load as MPIBAIJ");
3419: /* set block sizes from the viewer's .info file */
3420: PetscCall(MatLoad_Binary_BlockSizes(mat, viewer));
3421: /* set local sizes if not set already */
3422: if (mat->rmap->n < 0 && M == N) mat->rmap->n = mat->cmap->n;
3423: if (mat->cmap->n < 0 && M == N) mat->cmap->n = mat->rmap->n;
3424: /* set global sizes if not set already */
3425: if (mat->rmap->N < 0) mat->rmap->N = M;
3426: if (mat->cmap->N < 0) mat->cmap->N = N;
3427: PetscCall(PetscLayoutSetUp(mat->rmap));
3428: PetscCall(PetscLayoutSetUp(mat->cmap));
3430: /* check if the matrix sizes are correct */
3431: PetscCall(MatGetSize(mat, &rows, &cols));
3432: PetscCheck(M == rows && N == cols, PETSC_COMM_SELF, PETSC_ERR_FILE_UNEXPECTED, "Matrix in file of different sizes (%" PetscInt_FMT ", %" PetscInt_FMT ") than the input matrix (%" PetscInt_FMT ", %" PetscInt_FMT ")", M, N, rows, cols);
3433: PetscCall(MatGetBlockSize(mat, &bs));
3434: PetscCall(MatGetLocalSize(mat, &m, &n));
3435: PetscCall(PetscLayoutGetRange(mat->rmap, &rs, NULL));
3436: PetscCall(PetscLayoutGetRange(mat->cmap, &cs, &ce));
3437: mbs = m / bs;
3438: nbs = n / bs;
3440: /* read in row lengths and build row indices */
3441: PetscCall(PetscMalloc1(m + 1, &rowidxs));
3442: PetscCall(PetscViewerBinaryReadAll(viewer, rowidxs + 1, m, PETSC_DECIDE, M, PETSC_INT));
3443: rowidxs[0] = 0;
3444: for (i = 0; i < m; i++) rowidxs[i + 1] += rowidxs[i];
3445: PetscCallMPI(MPIU_Allreduce(&rowidxs[m], &sum, 1, MPIU_INT, MPI_SUM, PetscObjectComm((PetscObject)viewer)));
3446: PetscCheck(sum == nz, PetscObjectComm((PetscObject)viewer), PETSC_ERR_FILE_UNEXPECTED, "Inconsistent matrix data in file: nonzeros = %" PetscInt_FMT ", sum-row-lengths = %" PetscInt_FMT, nz, sum);
3448: /* read in column indices and matrix values */
3449: PetscCall(PetscMalloc2(rowidxs[m], &colidxs, rowidxs[m], &matvals));
3450: PetscCall(PetscViewerBinaryReadAll(viewer, colidxs, rowidxs[m], PETSC_DETERMINE, PETSC_DETERMINE, PETSC_INT));
3451: PetscCall(PetscViewerBinaryReadAll(viewer, matvals, rowidxs[m], PETSC_DETERMINE, PETSC_DETERMINE, PETSC_SCALAR));
3453: { /* preallocate matrix storage */
3454: PetscBT bt; /* helper bit set to count diagonal nonzeros */
3455: PetscHSetI ht; /* helper hash set to count off-diagonal nonzeros */
3456: PetscBool sbaij, done;
3457: PetscInt *d_nnz, *o_nnz;
3459: PetscCall(PetscBTCreate(nbs, &bt));
3460: PetscCall(PetscHSetICreate(&ht));
3461: PetscCall(PetscCalloc2(mbs, &d_nnz, mbs, &o_nnz));
3462: PetscCall(PetscObjectTypeCompare((PetscObject)mat, MATMPISBAIJ, &sbaij));
3463: for (i = 0; i < mbs; i++) {
3464: PetscCall(PetscBTMemzero(nbs, bt));
3465: PetscCall(PetscHSetIClear(ht));
3466: for (k = 0; k < bs; k++) {
3467: const PetscInt row = bs * i + k;
3469: for (j = rowidxs[row]; j < rowidxs[row + 1]; j++) {
3470: const PetscInt col = colidxs[j];
3472: if (!sbaij || col / bs >= rs / bs + i) {
3473: if (col >= cs && col < ce) {
3474: if (!PetscBTLookupSet(bt, (col - cs) / bs)) d_nnz[i]++;
3475: } else {
3476: PetscCall(PetscHSetIQueryAdd(ht, col / bs, &done));
3477: if (done) o_nnz[i]++;
3478: }
3479: }
3480: }
3481: }
3482: }
3483: PetscCall(PetscBTDestroy(&bt));
3484: PetscCall(PetscHSetIDestroy(&ht));
3485: PetscCall(MatMPIBAIJSetPreallocation(mat, bs, 0, d_nnz, 0, o_nnz));
3486: PetscCall(MatMPISBAIJSetPreallocation(mat, bs, 0, d_nnz, 0, o_nnz));
3487: PetscCall(PetscFree2(d_nnz, o_nnz));
3488: }
3490: /* store matrix values */
3491: for (i = 0; i < m; i++) {
3492: PetscInt row = rs + i, s = rowidxs[i], e = rowidxs[i + 1];
3493: PetscUseTypeMethod(mat, setvalues, 1, &row, e - s, colidxs + s, matvals + s, INSERT_VALUES);
3494: }
3496: PetscCall(PetscFree(rowidxs));
3497: PetscCall(PetscFree2(colidxs, matvals));
3498: mat->nooffprocentries = PETSC_TRUE;
3499: PetscCall(MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY));
3500: PetscCall(MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY));
3501: mat->nooffprocentries = nooffprocentries;
3502: PetscFunctionReturn(PETSC_SUCCESS);
3503: }
3505: PetscErrorCode MatLoad_MPIBAIJ(Mat mat, PetscViewer viewer)
3506: {
3507: PetscBool isbinary;
3509: PetscFunctionBegin;
3510: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
3511: 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);
3512: PetscCall(MatLoad_MPIBAIJ_Binary(mat, viewer));
3513: PetscFunctionReturn(PETSC_SUCCESS);
3514: }
3516: /*@
3517: MatMPIBAIJSetHashTableFactor - Sets the factor required to compute the size of the matrices hash table
3519: Input Parameters:
3520: + mat - the matrix
3521: - fact - factor
3523: Options Database Key:
3524: . -mat_use_hash_table fact - provide the factor
3526: Level: advanced
3528: .seealso: `Mat`, `MATMPIBAIJ`, `MatSetOption()`
3529: @*/
3530: PetscErrorCode MatMPIBAIJSetHashTableFactor(Mat mat, PetscReal fact)
3531: {
3532: PetscFunctionBegin;
3533: PetscTryMethod(mat, "MatSetHashTableFactor_C", (Mat, PetscReal), (mat, fact));
3534: PetscFunctionReturn(PETSC_SUCCESS);
3535: }
3537: PetscErrorCode MatSetHashTableFactor_MPIBAIJ(Mat mat, PetscReal fact)
3538: {
3539: Mat_MPIBAIJ *baij;
3541: PetscFunctionBegin;
3542: baij = (Mat_MPIBAIJ *)mat->data;
3543: baij->ht_fact = fact;
3544: PetscFunctionReturn(PETSC_SUCCESS);
3545: }
3547: /*@
3548: MatMPIBAIJGetSeqBAIJ - Get the on-process (diagonal block) and off-process (off-diagonal block) sequential matrices
3549: that make up a `MATMPIBAIJ` or `MATMPISBAIJ` matrix, together with the local-to-global column map for the off-diagonal block.
3551: Not Collective
3553: Input Parameter:
3554: . A - the `MATMPIBAIJ` or `MATMPISBAIJ` matrix
3556: Output Parameters:
3557: + Ad - the diagonal block (`MATSEQBAIJ` or `MATSEQSBAIJ`), or `NULL` if not needed
3558: . Ao - the off-diagonal block `MATSEQBAIJ`, or `NULL` if not needed
3559: - colmap - the local-to-global column index map for `Ao`, or `NULL` if not needed
3561: Level: advanced
3563: .seealso: `Mat`, `MATMPIBAIJ`, `MATMPISBAIJ`, `MATSEQBAIJ`, `MATSEQSBAIJ`, `MatMPIAIJGetSeqAIJ()`
3564: @*/
3565: PetscErrorCode MatMPIBAIJGetSeqBAIJ(Mat A, Mat *Ad, Mat *Ao, const PetscInt *colmap[])
3566: {
3567: Mat_MPIBAIJ *a = (Mat_MPIBAIJ *)A->data;
3568: PetscBool flg;
3570: PetscFunctionBegin;
3571: PetscCall(PetscObjectTypeCompareAny((PetscObject)A, &flg, MATMPIBAIJ, MATMPISBAIJ, ""));
3572: PetscCheck(flg, PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "This function requires a MATMPIBAIJ or MATMPISBAIJ matrix as input");
3573: if (Ad) *Ad = a->A;
3574: if (Ao) *Ao = a->B;
3575: if (colmap) *colmap = a->garray;
3576: PetscFunctionReturn(PETSC_SUCCESS);
3577: }
3579: /*
3580: Special version for direct calls from Fortran (to eliminate two function call overheads
3581: */
3582: #if PetscDefined(HAVE_FORTRAN_CAPS)
3583: #define matmpibaijsetvaluesblocked_ MATMPIBAIJSETVALUESBLOCKED
3584: #elif !PetscDefined(HAVE_FORTRAN_UNDERSCORE)
3585: #define matmpibaijsetvaluesblocked_ matmpibaijsetvaluesblocked
3586: #endif
3588: // PetscClangLinter pragma disable: -fdoc-synopsis-matching-symbol-name
3589: /*@
3590: MatMPIBAIJSetValuesBlocked - Direct Fortran call to replace call to `MatSetValuesBlocked()`
3592: Collective
3594: Input Parameters:
3595: + matin - the matrix
3596: . min - number of input rows
3597: . im - input rows
3598: . nin - number of input columns
3599: . in - input columns
3600: . v - numerical values input
3601: - addvin - `INSERT_VALUES` or `ADD_VALUES`
3603: Level: advanced
3605: Developer Notes:
3606: This has a complete copy of `MatSetValuesBlocked_MPIBAIJ()` which is terrible code un-reuse.
3608: .seealso: `Mat`, `MatSetValuesBlocked()`
3609: @*/
3610: PETSC_EXTERN PetscErrorCode matmpibaijsetvaluesblocked_(Mat *matin, PetscInt *min, const PetscInt im[], PetscInt *nin, const PetscInt in[], const MatScalar v[], InsertMode *addvin)
3611: {
3612: /* convert input arguments to C version */
3613: Mat mat = *matin;
3614: PetscInt m = *min, n = *nin;
3615: InsertMode addv = *addvin;
3617: Mat_MPIBAIJ *baij = (Mat_MPIBAIJ *)mat->data;
3618: const MatScalar *value;
3619: MatScalar *barray = baij->barray;
3620: PetscBool roworiented = baij->roworiented;
3621: PetscInt i, j, ii, jj, row, col, rstart = baij->rstartbs;
3622: PetscInt rend = baij->rendbs, cstart = baij->cstartbs, stepval;
3623: PetscInt cend = baij->cendbs, bs = mat->rmap->bs, bs2 = baij->bs2;
3625: PetscFunctionBegin;
3626: /* tasks normally handled by MatSetValuesBlocked() */
3627: if (mat->insertmode == NOT_SET_VALUES) mat->insertmode = addv;
3628: else PetscCheck(mat->insertmode == addv, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Cannot mix add values and insert values");
3629: PetscCheck(!mat->factortype, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
3630: if (mat->assembled) {
3631: mat->was_assembled = PETSC_TRUE;
3632: mat->assembled = PETSC_FALSE;
3633: }
3634: PetscCall(PetscLogEventBegin(MAT_SetValues, mat, 0, 0, 0));
3636: if (!barray) {
3637: PetscCall(PetscMalloc1(bs2, &barray));
3638: baij->barray = barray;
3639: }
3641: if (roworiented) stepval = (n - 1) * bs;
3642: else stepval = (m - 1) * bs;
3644: for (i = 0; i < m; i++) {
3645: if (im[i] < 0) continue;
3646: PetscCheck(im[i] < baij->Mbs, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Row too large, row %" PetscInt_FMT " max %" PetscInt_FMT, im[i], baij->Mbs - 1);
3647: if (im[i] >= rstart && im[i] < rend) {
3648: row = im[i] - rstart;
3649: for (j = 0; j < n; j++) {
3650: /* If NumCol = 1 then a copy is not required */
3651: if ((roworiented) && (n == 1)) {
3652: barray = (MatScalar *)v + i * bs2;
3653: } else if ((!roworiented) && (m == 1)) {
3654: barray = (MatScalar *)v + j * bs2;
3655: } else { /* Here a copy is required */
3656: if (roworiented) {
3657: value = v + i * (stepval + bs) * bs + j * bs;
3658: } else {
3659: value = v + j * (stepval + bs) * bs + i * bs;
3660: }
3661: for (ii = 0; ii < bs; ii++, value += stepval) {
3662: for (jj = 0; jj < bs; jj++) *barray++ = *value++;
3663: }
3664: barray -= bs2;
3665: }
3667: if (in[j] >= cstart && in[j] < cend) {
3668: col = in[j] - cstart;
3669: PetscCall(MatSetValuesBlocked_SeqBAIJ_Inlined(baij->A, row, col, barray, addv, im[i], in[j]));
3670: } else if (in[j] < 0) {
3671: continue;
3672: } else {
3673: PetscCheck(in[j] < baij->Nbs, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Column too large, col %" PetscInt_FMT " max %" PetscInt_FMT, in[j], baij->Nbs - 1);
3674: if (mat->was_assembled) {
3675: if (!baij->colmap) PetscCall(MatCreateColmap_MPIBAIJ_Private(mat));
3677: #if PetscDefined(USE_CTABLE)
3678: if (PetscDefined(USE_DEBUG)) {
3679: PetscInt data;
3680: PetscCall(PetscHMapIGetWithDefault(baij->colmap, in[j] + 1, 0, &data));
3681: PetscCheck((data - 1) % bs == 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Incorrect colmap");
3682: }
3683: #else
3684: if (PetscDefined(USE_DEBUG)) PetscCheck((baij->colmap[in[j]] - 1) % bs == 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Incorrect colmap");
3685: #endif
3686: #if PetscDefined(USE_CTABLE)
3687: PetscCall(PetscHMapIGetWithDefault(baij->colmap, in[j] + 1, 0, &col));
3688: col = (col - 1) / bs;
3689: #else
3690: col = (baij->colmap[in[j]] - 1) / bs;
3691: #endif
3692: if (col < 0 && !((Mat_SeqBAIJ *)baij->A->data)->nonew) {
3693: PetscCall(MatDisAssemble_MPIBAIJ(mat));
3694: col = in[j];
3695: }
3696: } else col = in[j];
3697: PetscCall(MatSetValuesBlocked_SeqBAIJ_Inlined(baij->B, row, col, barray, addv, im[i], in[j]));
3698: }
3699: }
3700: } else {
3701: if (!baij->donotstash) {
3702: if (roworiented) {
3703: PetscCall(MatStashValuesRowBlocked_Private(&mat->bstash, im[i], n, in, v, m, n, i));
3704: } else {
3705: PetscCall(MatStashValuesColBlocked_Private(&mat->bstash, im[i], n, in, v, m, n, i));
3706: }
3707: }
3708: }
3709: }
3711: /* task normally handled by MatSetValuesBlocked() */
3712: PetscCall(PetscLogEventEnd(MAT_SetValues, mat, 0, 0, 0));
3713: PetscFunctionReturn(PETSC_SUCCESS);
3714: }
3716: /*@
3717: MatCreateMPIBAIJWithArrays - creates a `MATMPIBAIJ` matrix using arrays that contain in standard block CSR format for the local rows.
3719: Collective
3721: Input Parameters:
3722: + comm - MPI communicator
3723: . bs - the block size, only a block size of 1 is supported
3724: . m - number of local rows (Cannot be `PETSC_DECIDE`)
3725: . n - This value should be the same as the local size used in creating the
3726: x vector for the matrix-vector product $ y = Ax $. (or `PETSC_DECIDE` to have
3727: calculated if `N` is given) For square matrices `n` is almost always `m`.
3728: . M - number of global rows (or `PETSC_DETERMINE` to have calculated if `m` is given)
3729: . N - number of global columns (or `PETSC_DETERMINE` to have calculated if `n` is given)
3730: . i - row indices; that is i[0] = 0, i[row] = i[row-1] + number of block elements in that rowth block row of the matrix
3731: . j - column indices
3732: - a - matrix values
3734: Output Parameter:
3735: . mat - the matrix
3737: Level: intermediate
3739: Notes:
3740: The `i`, `j`, and `a` arrays ARE copied by this routine into the internal format used by PETSc;
3741: thus you CANNOT change the matrix entries by changing the values of a[] after you have
3742: called this routine. Use `MatCreateMPIAIJWithSplitArrays()` to avoid needing to copy the arrays.
3744: The order of the entries in values is the same as the block compressed sparse row storage format; that is, it is
3745: the same as a three dimensional array in Fortran values(bs,bs,nnz) that contains the first column of the first
3746: block, followed by the second column of the first block etc etc. That is, the blocks are contiguous in memory
3747: with column-major ordering within blocks.
3749: The `i` and `j` indices are 0 based, and `i` indices are indices corresponding to the local `j` array.
3751: .seealso: `Mat`, `MatCreate()`, `MatCreateSeqAIJ()`, `MatSetValues()`, `MatMPIAIJSetPreallocation()`, `MatMPIAIJSetPreallocationCSR()`,
3752: `MATMPIAIJ`, `MatCreateAIJ()`, `MatCreateMPIAIJWithSplitArrays()`
3753: @*/
3754: PetscErrorCode MatCreateMPIBAIJWithArrays(MPI_Comm comm, PetscInt bs, PetscInt m, PetscInt n, PetscInt M, PetscInt N, const PetscInt i[], const PetscInt j[], const PetscScalar a[], Mat *mat)
3755: {
3756: PetscFunctionBegin;
3757: PetscCheck(!i[0], PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "i (row indices) must start with 0");
3758: PetscCheck(m >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "local number of rows (m) cannot be PETSC_DECIDE, or negative");
3759: PetscCall(MatCreate(comm, mat));
3760: PetscCall(MatSetSizes(*mat, m, n, M, N));
3761: PetscCall(MatSetType(*mat, MATMPIBAIJ));
3762: PetscCall(MatSetBlockSize(*mat, bs));
3763: PetscCall(MatSetUp(*mat));
3764: PetscCall(MatSetOption(*mat, MAT_ROW_ORIENTED, PETSC_FALSE));
3765: PetscCall(MatMPIBAIJSetPreallocationCSR(*mat, bs, i, j, a));
3766: PetscCall(MatSetOption(*mat, MAT_ROW_ORIENTED, PETSC_TRUE));
3767: PetscFunctionReturn(PETSC_SUCCESS);
3768: }
3770: PetscErrorCode MatCreateMPIMatConcatenateSeqMat_MPIBAIJ(MPI_Comm comm, Mat inmat, PetscInt n, MatReuse scall, Mat *outmat)
3771: {
3772: PetscInt m, N, i, rstart, nnz, Ii, bs, cbs;
3773: PetscInt *indx;
3774: PetscScalar *values;
3776: PetscFunctionBegin;
3777: PetscCall(MatGetSize(inmat, &m, &N));
3778: if (scall == MAT_INITIAL_MATRIX) { /* symbolic phase */
3779: Mat_SeqBAIJ *a = (Mat_SeqBAIJ *)inmat->data;
3780: PetscInt *dnz, *onz, mbs, Nbs, nbs;
3781: PetscInt *bindx, rmax = a->rmax, j;
3782: PetscMPIInt rank, size;
3784: PetscCall(MatGetBlockSizes(inmat, &bs, &cbs));
3785: mbs = m / bs;
3786: Nbs = N / cbs;
3787: if (n == PETSC_DECIDE) PetscCall(PetscSplitOwnershipBlock(comm, cbs, &n, &N));
3788: nbs = n / cbs;
3790: PetscCall(PetscMalloc1(rmax, &bindx));
3791: MatPreallocateBegin(comm, mbs, nbs, dnz, onz); /* inline function, output __end and __rstart are used below */
3793: PetscCallMPI(MPI_Comm_rank(comm, &rank));
3794: PetscCallMPI(MPI_Comm_size(comm, &size));
3795: if (rank == size - 1) {
3796: /* Check sum(nbs) = Nbs */
3797: PetscCheck(__end == Nbs, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Sum of local block columns %" PetscInt_FMT " != global block columns %" PetscInt_FMT, __end, Nbs);
3798: }
3800: rstart = __rstart; /* block rstart of *outmat; see inline function MatPreallocateBegin */
3801: for (i = 0; i < mbs; i++) {
3802: PetscCall(MatGetRow_SeqBAIJ(inmat, i * bs, &nnz, &indx, NULL)); /* non-blocked nnz and indx */
3803: nnz = nnz / bs;
3804: for (j = 0; j < nnz; j++) bindx[j] = indx[j * bs] / bs;
3805: PetscCall(MatPreallocateSet(i + rstart, nnz, bindx, dnz, onz));
3806: PetscCall(MatRestoreRow_SeqBAIJ(inmat, i * bs, &nnz, &indx, NULL));
3807: }
3808: PetscCall(PetscFree(bindx));
3810: PetscCall(MatCreate(comm, outmat));
3811: PetscCall(MatSetSizes(*outmat, m, n, PETSC_DETERMINE, PETSC_DETERMINE));
3812: PetscCall(MatSetBlockSizes(*outmat, bs, cbs));
3813: PetscCall(MatSetType(*outmat, MATBAIJ));
3814: PetscCall(MatSeqBAIJSetPreallocation(*outmat, bs, 0, dnz));
3815: PetscCall(MatMPIBAIJSetPreallocation(*outmat, bs, 0, dnz, 0, onz));
3816: MatPreallocateEnd(dnz, onz);
3817: PetscCall(MatSetOption(*outmat, MAT_NO_OFF_PROC_ENTRIES, PETSC_TRUE));
3818: }
3820: /* numeric phase */
3821: PetscCall(MatGetBlockSizes(inmat, &bs, &cbs));
3822: PetscCall(MatGetOwnershipRange(*outmat, &rstart, NULL));
3824: for (i = 0; i < m; i++) {
3825: PetscCall(MatGetRow_SeqBAIJ(inmat, i, &nnz, &indx, &values));
3826: Ii = i + rstart;
3827: PetscCall(MatSetValues(*outmat, 1, &Ii, nnz, indx, values, INSERT_VALUES));
3828: PetscCall(MatRestoreRow_SeqBAIJ(inmat, i, &nnz, &indx, &values));
3829: }
3830: PetscCall(MatAssemblyBegin(*outmat, MAT_FINAL_ASSEMBLY));
3831: PetscCall(MatAssemblyEnd(*outmat, MAT_FINAL_ASSEMBLY));
3832: PetscFunctionReturn(PETSC_SUCCESS);
3833: }