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