Actual source code: mpisell.c
1: #include <../src/mat/impls/aij/mpi/mpiaij.h>
2: #include <../src/mat/impls/sell/mpi/mpisell.h>
3: #include <petsc/private/vecimpl.h>
4: #include <petsc/private/isimpl.h>
5: #include <petscblaslapack.h>
6: #include <petscsf.h>
8: /*MC
9: MATSELL - MATSELL = "sell" - A matrix type to be used for sparse matrices.
11: This matrix type is identical to `MATSEQSELL` when constructed with a single process communicator,
12: and `MATMPISELL` otherwise. As a result, for single process communicators,
13: `MatSeqSELLSetPreallocation()` is supported, and similarly `MatMPISELLSetPreallocation()` is supported
14: for communicators controlling multiple processes. It is recommended that you call both of
15: the above preallocation routines for simplicity.
17: Options Database Keys:
18: . -mat_type sell - sets the matrix type to `MATSELL` during a call to `MatSetFromOptions()`
20: Level: beginner
22: .seealso: `Mat`, `MATAIJ`, `MATBAIJ`, `MATSBAIJ`, `MatCreateSELL()`, `MatCreateSeqSELL()`, `MATSEQSELL`, `MATMPISELL`
23: M*/
25: static PetscErrorCode MatDiagonalSet_MPISELL(Mat Y, Vec D, InsertMode is)
26: {
27: Mat_MPISELL *sell = (Mat_MPISELL *)Y->data;
29: PetscFunctionBegin;
30: if (Y->assembled && Y->rmap->rstart == Y->cmap->rstart && Y->rmap->rend == Y->cmap->rend) {
31: PetscCall(MatDiagonalSet(sell->A, D, is));
32: } else {
33: PetscCall(MatDiagonalSet_Default(Y, D, is));
34: }
35: PetscFunctionReturn(PETSC_SUCCESS);
36: }
38: /*
39: Local utility routine that creates a mapping from the global column
40: number to the local number in the off-diagonal part of the local
41: storage of the matrix. When PETSC_USE_CTABLE is used this is scalable at
42: a slightly higher hash table cost; without it it is not scalable (each processor
43: has an order N integer array but is fast to access.
44: */
45: PetscErrorCode MatCreateColmap_MPISELL_Private(Mat mat)
46: {
47: Mat_MPISELL *sell = (Mat_MPISELL *)mat->data;
48: PetscInt n = sell->B->cmap->n, i;
50: PetscFunctionBegin;
51: PetscCheck(sell->garray, PETSC_COMM_SELF, PETSC_ERR_PLIB, "MPISELL Matrix was assembled but is missing garray");
52: #if PetscDefined(USE_CTABLE)
53: PetscCall(PetscHMapICreateWithSize(n, &sell->colmap));
54: for (i = 0; i < n; i++) PetscCall(PetscHMapISet(sell->colmap, sell->garray[i] + 1, i + 1));
55: #else
56: PetscCall(PetscCalloc1(mat->cmap->N + 1, &sell->colmap));
57: for (i = 0; i < n; i++) sell->colmap[sell->garray[i]] = i + 1;
58: #endif
59: PetscFunctionReturn(PETSC_SUCCESS);
60: }
62: static PetscErrorCode MatSetValues_MPISELL(Mat mat, PetscInt m, const PetscInt im[], PetscInt n, const PetscInt in[], const PetscScalar v[], InsertMode addv)
63: {
64: Mat_MPISELL *sell = (Mat_MPISELL *)mat->data;
65: PetscScalar value;
66: PetscInt i, j, rstart = mat->rmap->rstart, rend = mat->rmap->rend, shift1, shift2;
67: PetscInt cstart = mat->cmap->rstart, cend = mat->cmap->rend, row, col;
68: PetscBool roworiented = sell->roworiented;
70: /* Some Variables required in the macro */
71: Mat A = sell->A;
72: Mat_SeqSELL *a = (Mat_SeqSELL *)A->data;
73: PetscBool ignorezeroentries = a->ignorezeroentries, found;
74: PetscBool wroteA = PETSC_FALSE, wroteB = PETSC_FALSE;
75: Mat B = sell->B;
76: Mat_SeqSELL *b = (Mat_SeqSELL *)B->data;
77: PetscInt *cp1, *cp2, ii, _i, nrow1, nrow2, low1, high1, low2, high2, t, lastcol1, lastcol2, sliceheight = a->sliceheight;
78: MatScalar *vp1, *vp2;
80: PetscFunctionBegin;
81: for (i = 0; i < m; i++) {
82: if (im[i] < 0) continue;
83: 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);
84: if (im[i] >= rstart && im[i] < rend) {
85: row = im[i] - rstart;
86: lastcol1 = -1;
87: shift1 = a->sliidx[row / sliceheight] + (row % sliceheight); /* starting index of the row */
88: cp1 = PetscSafePointerPlusOffset(a->colidx, shift1);
89: vp1 = PetscSafePointerPlusOffset(a->val, shift1);
90: nrow1 = a->rlen[row];
91: low1 = 0;
92: high1 = nrow1;
93: lastcol2 = -1;
94: shift2 = b->sliidx[row / sliceheight] + (row % sliceheight); /* starting index of the row */
95: cp2 = PetscSafePointerPlusOffset(b->colidx, shift2);
96: vp2 = PetscSafePointerPlusOffset(b->val, shift2);
97: nrow2 = b->rlen[row];
98: low2 = 0;
99: high2 = nrow2;
101: for (j = 0; j < n; j++) {
102: if (roworiented) value = v[i * n + j];
103: else value = v[i + j * m];
104: if (ignorezeroentries && value == 0.0 && addv == ADD_VALUES && im[i] != in[j]) continue;
105: if (in[j] >= cstart && in[j] < cend) {
106: col = in[j] - cstart;
107: MatSetValue_SeqSELL_Private(A, row, col, value, addv, im[i], in[j], im[i] != in[j], cp1, vp1, lastcol1, low1, high1); /* set one value */
108: wroteA = (PetscBool)(wroteA || found);
109: } else if (in[j] < 0) {
110: continue;
111: } else {
112: 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);
113: if (mat->was_assembled) {
114: if (!sell->colmap) PetscCall(MatCreateColmap_MPISELL_Private(mat));
115: #if PetscDefined(USE_CTABLE)
116: PetscCall(PetscHMapIGetWithDefault(sell->colmap, in[j] + 1, 0, &col));
117: col--;
118: #else
119: col = sell->colmap[in[j]] - 1;
120: #endif
121: if (col < 0 && !((Mat_SeqSELL *)sell->B->data)->nonew) {
122: PetscCall(MatDisAssemble_MPISELL(mat));
123: col = in[j];
124: /* Reinitialize the variables required by MatSetValue_SeqSELL_Private() */
125: B = sell->B;
126: b = (Mat_SeqSELL *)B->data;
127: shift2 = b->sliidx[row / sliceheight] + (row % sliceheight); /* starting index of the row */
128: cp2 = b->colidx + shift2;
129: vp2 = b->val + shift2;
130: nrow2 = b->rlen[row];
131: low2 = 0;
132: high2 = nrow2;
133: } else if (col < 0 && !(ignorezeroentries && value == 0.0)) {
134: 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", im[i], in[j]);
135: PetscCall(PetscInfo(mat, "Skipping of insertion of new nonzero location in off-diagonal portion of matrix %g(%" PetscInt_FMT ",%" PetscInt_FMT ")\n", (double)PetscRealPart(value), im[i], in[j]));
136: }
137: } else col = in[j];
138: /* a suppressed new off-diagonal location; there is nothing to insert */
139: if (col < 0) continue;
140: /* no diagonal exception here: a zero stored by the off-diagonal block does nothing for the
141: diagonal that MatInvertDiagonalForSOR_SeqSELL() needs, which lives in the diagonal block.
142: A global (i,i) reaches this block only when the row and column layouts differ, and dropping
143: it is what MatSetValues_SeqAIJ_B_Private() does. */
144: MatSetValue_SeqSELL_Private(B, row, col, value, addv, im[i], in[j], PETSC_TRUE, cp2, vp2, lastcol2, low2, high2); /* set one value */
145: wroteB = (PetscBool)(wroteB || found);
146: }
147: }
148: } else {
149: 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]);
150: if (!sell->donotstash) {
151: mat->assembled = PETSC_FALSE;
152: if (roworiented) {
153: PetscCall(MatStashValuesRow_Private(&mat->stash, im[i], n, in, v + i * n, (PetscBool)(ignorezeroentries && (addv == ADD_VALUES))));
154: } else {
155: PetscCall(MatStashValuesCol_Private(&mat->stash, im[i], n, in, v + i, m, (PetscBool)(ignorezeroentries && (addv == ADD_VALUES))));
156: }
157: }
158: }
159: }
160: #if PetscDefined(HAVE_CUPM)
161: if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED && wroteA) A->offloadmask = PETSC_OFFLOAD_CPU;
162: if (B->offloadmask != PETSC_OFFLOAD_UNALLOCATED && wroteB) B->offloadmask = PETSC_OFFLOAD_CPU;
163: #endif
164: PetscFunctionReturn(PETSC_SUCCESS);
165: }
167: static PetscErrorCode MatGetValues_MPISELL(Mat mat, PetscInt m, const PetscInt idxm[], PetscInt n, const PetscInt idxn[], PetscScalar v[])
168: {
169: Mat_MPISELL *sell = (Mat_MPISELL *)mat->data;
170: PetscInt i, j, rstart = mat->rmap->rstart, rend = mat->rmap->rend;
171: PetscInt cstart = mat->cmap->rstart, cend = mat->cmap->rend, row, col;
172: PetscBool roworiented = sell->roworiented;
173: PetscScalar *value;
175: PetscFunctionBegin;
176: for (i = 0; i < m; i++) {
177: if (idxm[i] < 0) continue; /* negative row */
178: 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);
179: PetscCheck(idxm[i] >= rstart && idxm[i] < rend, PETSC_COMM_SELF, PETSC_ERR_SUP, "Only local values currently supported");
180: row = idxm[i] - rstart;
181: for (j = 0; j < n; j++) {
182: if (idxn[j] < 0) continue; /* negative column */
183: 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);
184: value = roworiented ? &v[j + i * n] : &v[i + j * m];
185: if (idxn[j] >= cstart && idxn[j] < cend) {
186: col = idxn[j] - cstart;
187: PetscCall(MatGetValues(sell->A, 1, &row, 1, &col, value));
188: } else {
189: if (!sell->colmap) PetscCall(MatCreateColmap_MPISELL_Private(mat));
190: #if PetscDefined(USE_CTABLE)
191: PetscCall(PetscHMapIGetWithDefault(sell->colmap, idxn[j] + 1, 0, &col));
192: col--;
193: #else
194: col = sell->colmap[idxn[j]] - 1;
195: #endif
196: if (col < 0 || sell->garray[col] != idxn[j]) *value = 0.0;
197: else PetscCall(MatGetValues(sell->B, 1, &row, 1, &col, value));
198: }
199: }
200: }
201: PetscFunctionReturn(PETSC_SUCCESS);
202: }
204: static PetscErrorCode MatAssemblyBegin_MPISELL(Mat mat, MatAssemblyType mode)
205: {
206: Mat_MPISELL *sell = (Mat_MPISELL *)mat->data;
207: PetscInt nstash, reallocs;
209: PetscFunctionBegin;
210: if (sell->donotstash || mat->nooffprocentries) PetscFunctionReturn(PETSC_SUCCESS);
212: PetscCall(MatStashScatterBegin_Private(mat, &mat->stash, mat->rmap->range));
213: PetscCall(MatStashGetInfo_Private(&mat->stash, &nstash, &reallocs));
214: PetscCall(PetscInfo(sell->A, "Stash has %" PetscInt_FMT " entries, uses %" PetscInt_FMT " mallocs.\n", nstash, reallocs));
215: PetscFunctionReturn(PETSC_SUCCESS);
216: }
218: PetscErrorCode MatAssemblyEnd_MPISELL(Mat mat, MatAssemblyType mode)
219: {
220: Mat_MPISELL *sell = (Mat_MPISELL *)mat->data;
221: PetscMPIInt n;
222: PetscInt i, flg;
223: PetscInt *row, *col;
224: PetscScalar *val;
225: PetscBool all_assembled;
226: /* do not use 'b = (Mat_SeqSELL*)sell->B->data' as B can be reset in disassembly */
227: PetscFunctionBegin;
228: if (!sell->donotstash && !mat->nooffprocentries) {
229: while (1) {
230: PetscCall(MatStashScatterGetMesg_Private(&mat->stash, &n, &row, &col, &val, &flg));
231: if (!flg) break;
233: for (i = 0; i < n; i++) { /* assemble one by one */
234: PetscCall(MatSetValues_MPISELL(mat, 1, row + i, 1, col + i, val + i, mat->insertmode));
235: }
236: }
237: PetscCall(MatStashScatterEnd_Private(&mat->stash));
238: }
239: /*
240: This check and its counterpart below mirror MatAssemblyEnd_MPIAIJ(). They fire when a producer
241: fills the host submatrices behind assembly's back and marks the outer mask CPU first, as
242: MatSetPreallocationCOO() and the host matrix products do for MPIAIJ. SELL has neither, so they
243: are dormant here.
244: */
245: #if PetscDefined(HAVE_CUPM)
246: if (mat->offloadmask == PETSC_OFFLOAD_CPU) sell->A->offloadmask = PETSC_OFFLOAD_CPU;
247: #endif
248: PetscCall(MatAssemblyBegin(sell->A, mode));
249: PetscCall(MatAssemblyEnd(sell->A, mode));
251: /*
252: determine if any process has disassembled, if so we must
253: also disassemble ourselves, in order that we may reassemble.
254: */
255: /*
256: if nonzero structure of submatrix B cannot change then we know that
257: no process disassembled thus we can skip this stuff
258: */
259: if (!((Mat_SeqSELL *)sell->B->data)->nonew) {
260: PetscCallMPI(MPIU_Allreduce(&mat->was_assembled, &all_assembled, 1, MPI_C_BOOL, MPI_LAND, PetscObjectComm((PetscObject)mat)));
261: if (mat->was_assembled && !all_assembled) PetscCall(MatDisAssemble_MPISELL(mat));
262: }
263: if (!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) PetscCall(MatSetUpMultiply_MPISELL(mat));
264: #if PetscDefined(HAVE_CUPM)
265: if (mat->offloadmask == PETSC_OFFLOAD_CPU && sell->B->offloadmask != PETSC_OFFLOAD_UNALLOCATED) sell->B->offloadmask = PETSC_OFFLOAD_CPU;
266: #endif
267: PetscCall(MatAssemblyBegin(sell->B, mode));
268: PetscCall(MatAssemblyEnd(sell->B, mode));
269: PetscCall(PetscFree2(sell->rowvalues, sell->rowindices));
270: PetscCall(VecDestroy(&sell->diag));
272: /* if no new nonzero locations are allowed in matrix then only set the matrix state the first time through */
273: if ((!mat->was_assembled && mode == MAT_FINAL_ASSEMBLY) || !((Mat_SeqSELL *)sell->A->data)->nonew) {
274: mat->nonzerostate = sell->A->nonzerostate + sell->B->nonzerostate;
275: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &mat->nonzerostate, 1, MPIU_INT64, MPI_SUM, PetscObjectComm((PetscObject)mat)));
276: }
277: #if PetscDefined(HAVE_CUPM)
278: mat->offloadmask = PETSC_OFFLOAD_BOTH;
279: #endif
280: PetscFunctionReturn(PETSC_SUCCESS);
281: }
283: static PetscErrorCode MatZeroEntries_MPISELL(Mat A)
284: {
285: Mat_MPISELL *l = (Mat_MPISELL *)A->data;
287: PetscFunctionBegin;
288: PetscCall(MatZeroEntries(l->A));
289: PetscCall(MatZeroEntries(l->B));
290: PetscFunctionReturn(PETSC_SUCCESS);
291: }
293: static PetscErrorCode MatMult_MPISELL(Mat A, Vec xx, Vec yy)
294: {
295: Mat_MPISELL *a = (Mat_MPISELL *)A->data;
296: PetscInt nt;
298: PetscFunctionBegin;
299: PetscCall(VecGetLocalSize(xx, &nt));
300: PetscCheck(nt == A->cmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Incompatible partition of A (%" PetscInt_FMT ") and xx (%" PetscInt_FMT ")", A->cmap->n, nt);
301: PetscCall(VecScatterBegin(a->Mvctx, xx, a->lvec, INSERT_VALUES, SCATTER_FORWARD));
302: PetscUseTypeMethod(a->A, mult, xx, yy);
303: PetscCall(VecScatterEnd(a->Mvctx, xx, a->lvec, INSERT_VALUES, SCATTER_FORWARD));
304: PetscUseTypeMethod(a->B, multadd, a->lvec, yy, yy);
305: PetscFunctionReturn(PETSC_SUCCESS);
306: }
308: static PetscErrorCode MatGetMultPetscSF_MPISELL(Mat A, PetscSF *sf)
309: {
310: Mat_MPISELL *a = (Mat_MPISELL *)A->data;
312: PetscFunctionBegin;
313: *sf = a->Mvctx;
314: PetscFunctionReturn(PETSC_SUCCESS);
315: }
317: static PetscErrorCode MatMultDiagonalBlock_MPISELL(Mat A, Vec bb, Vec xx)
318: {
319: Mat_MPISELL *a = (Mat_MPISELL *)A->data;
321: PetscFunctionBegin;
322: PetscCall(MatMultDiagonalBlock(a->A, bb, xx));
323: PetscFunctionReturn(PETSC_SUCCESS);
324: }
326: static PetscErrorCode MatMultAdd_MPISELL(Mat A, Vec xx, Vec yy, Vec zz)
327: {
328: Mat_MPISELL *a = (Mat_MPISELL *)A->data;
330: PetscFunctionBegin;
331: PetscCall(VecScatterBegin(a->Mvctx, xx, a->lvec, INSERT_VALUES, SCATTER_FORWARD));
332: PetscUseTypeMethod(a->A, multadd, xx, yy, zz);
333: PetscCall(VecScatterEnd(a->Mvctx, xx, a->lvec, INSERT_VALUES, SCATTER_FORWARD));
334: PetscUseTypeMethod(a->B, multadd, a->lvec, zz, zz);
335: PetscFunctionReturn(PETSC_SUCCESS);
336: }
338: static PetscErrorCode MatMultTranspose_MPISELL(Mat A, Vec xx, Vec yy)
339: {
340: Mat_MPISELL *a = (Mat_MPISELL *)A->data;
342: PetscFunctionBegin;
343: /* do nondiagonal part */
344: PetscUseTypeMethod(a->B, multtranspose, xx, a->lvec);
345: /* do local part */
346: PetscUseTypeMethod(a->A, multtranspose, xx, yy);
347: /* add partial results together */
348: PetscCall(VecScatterBegin(a->Mvctx, a->lvec, yy, ADD_VALUES, SCATTER_REVERSE));
349: PetscCall(VecScatterEnd(a->Mvctx, a->lvec, yy, ADD_VALUES, SCATTER_REVERSE));
350: PetscFunctionReturn(PETSC_SUCCESS);
351: }
353: static PetscErrorCode MatIsTranspose_MPISELL(Mat Amat, Mat Bmat, PetscReal tol, PetscBool *f)
354: {
355: MPI_Comm comm;
356: Mat_MPISELL *Asell = (Mat_MPISELL *)Amat->data, *Bsell;
357: Mat Adia = Asell->A, Bdia, Aoff, Boff, *Aoffs, *Boffs;
358: IS Me, Notme;
359: PetscInt M, N, first, last, *notme, i;
360: PetscMPIInt size;
362: PetscFunctionBegin;
363: /* Easy test: symmetric diagonal block */
364: Bsell = (Mat_MPISELL *)Bmat->data;
365: Bdia = Bsell->A;
366: PetscCall(MatIsTranspose(Adia, Bdia, tol, f));
367: if (!*f) PetscFunctionReturn(PETSC_SUCCESS);
368: PetscCall(PetscObjectGetComm((PetscObject)Amat, &comm));
369: PetscCallMPI(MPI_Comm_size(comm, &size));
370: if (size == 1) PetscFunctionReturn(PETSC_SUCCESS);
372: /* Hard test: off-diagonal block. This takes a MatCreateSubMatrix. */
373: PetscCall(MatGetSize(Amat, &M, &N));
374: PetscCall(MatGetOwnershipRange(Amat, &first, &last));
375: PetscCall(PetscMalloc1(N - last + first, ¬me));
376: for (i = 0; i < first; i++) notme[i] = i;
377: for (i = last; i < M; i++) notme[i - last + first] = i;
378: PetscCall(ISCreateGeneral(MPI_COMM_SELF, N - last + first, notme, PETSC_COPY_VALUES, &Notme));
379: PetscCall(ISCreateStride(MPI_COMM_SELF, last - first, first, 1, &Me));
380: PetscCall(MatCreateSubMatrices(Amat, 1, &Me, &Notme, MAT_INITIAL_MATRIX, &Aoffs));
381: Aoff = Aoffs[0];
382: PetscCall(MatCreateSubMatrices(Bmat, 1, &Notme, &Me, MAT_INITIAL_MATRIX, &Boffs));
383: Boff = Boffs[0];
384: PetscCall(MatIsTranspose(Aoff, Boff, tol, f));
385: PetscCall(MatDestroyMatrices(1, &Aoffs));
386: PetscCall(MatDestroyMatrices(1, &Boffs));
387: PetscCall(ISDestroy(&Me));
388: PetscCall(ISDestroy(&Notme));
389: PetscCall(PetscFree(notme));
390: PetscFunctionReturn(PETSC_SUCCESS);
391: }
393: static PetscErrorCode MatMultTransposeAdd_MPISELL(Mat A, Vec xx, Vec yy, Vec zz)
394: {
395: Mat_MPISELL *a = (Mat_MPISELL *)A->data;
397: PetscFunctionBegin;
398: /* do nondiagonal part */
399: PetscUseTypeMethod(a->B, multtranspose, xx, a->lvec);
400: /* do local part */
401: PetscUseTypeMethod(a->A, multtransposeadd, xx, yy, zz);
402: /* add partial results together */
403: PetscCall(VecScatterBegin(a->Mvctx, a->lvec, zz, ADD_VALUES, SCATTER_REVERSE));
404: PetscCall(VecScatterEnd(a->Mvctx, a->lvec, zz, ADD_VALUES, SCATTER_REVERSE));
405: PetscFunctionReturn(PETSC_SUCCESS);
406: }
408: /*
409: This only works correctly for square matrices where the subblock A->A is the
410: diagonal block
411: */
412: static PetscErrorCode MatGetDiagonal_MPISELL(Mat A, Vec v)
413: {
414: Mat_MPISELL *a = (Mat_MPISELL *)A->data;
416: PetscFunctionBegin;
417: PetscCheck(A->rmap->N == A->cmap->N, PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "Supports only square matrix where A->A is diag block");
418: PetscCheck(A->rmap->rstart == A->cmap->rstart && A->rmap->rend == A->cmap->rend, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "row partition must equal col partition");
419: PetscCall(MatGetDiagonal(a->A, v));
420: PetscFunctionReturn(PETSC_SUCCESS);
421: }
423: static PetscErrorCode MatScale_MPISELL(Mat A, PetscScalar aa)
424: {
425: Mat_MPISELL *a = (Mat_MPISELL *)A->data;
427: PetscFunctionBegin;
428: PetscCall(MatScale(a->A, aa));
429: PetscCall(MatScale(a->B, aa));
430: PetscFunctionReturn(PETSC_SUCCESS);
431: }
433: PetscErrorCode MatDestroy_MPISELL(Mat mat)
434: {
435: Mat_MPISELL *sell = (Mat_MPISELL *)mat->data;
437: PetscFunctionBegin;
438: PetscCall(PetscLogObjectState((PetscObject)mat, "Rows=%" PetscInt_FMT ", Cols=%" PetscInt_FMT, mat->rmap->N, mat->cmap->N));
439: PetscCall(MatStashDestroy_Private(&mat->stash));
440: PetscCall(VecDestroy(&sell->diag));
441: PetscCall(MatDestroy(&sell->A));
442: PetscCall(MatDestroy(&sell->B));
443: #if PetscDefined(USE_CTABLE)
444: PetscCall(PetscHMapIDestroy(&sell->colmap));
445: #else
446: PetscCall(PetscFree(sell->colmap));
447: #endif
448: PetscCall(PetscFree(sell->garray));
449: PetscCall(VecDestroy(&sell->lvec));
450: PetscCall(VecScatterDestroy(&sell->Mvctx));
451: PetscCall(PetscFree2(sell->rowvalues, sell->rowindices));
452: PetscCall(PetscFree(sell->ld));
453: PetscCall(PetscFree(mat->data));
455: PetscCall(PetscObjectChangeTypeName((PetscObject)mat, NULL));
456: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatStoreValues_C", NULL));
457: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatRetrieveValues_C", NULL));
458: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatIsTranspose_C", NULL));
459: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatMPISELLSetPreallocation_C", NULL));
460: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_mpisell_mpiaij_C", NULL));
461: #if PetscDefined(HAVE_CUDA)
462: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatConvert_mpisell_mpisellcuda_C", NULL));
463: #endif
464: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatDiagonalScaleLocal_C", NULL));
465: PetscCall(PetscObjectComposeFunction((PetscObject)mat, "MatGetMultPetscSF_C", NULL));
466: PetscFunctionReturn(PETSC_SUCCESS);
467: }
469: #include <petscdraw.h>
470: static PetscErrorCode MatView_MPISELL_ASCIIorDraworSocket(Mat mat, PetscViewer viewer)
471: {
472: Mat_MPISELL *sell = (Mat_MPISELL *)mat->data;
473: PetscMPIInt rank = sell->rank, size = sell->size;
474: PetscBool isdraw, isascii, isbinary;
475: PetscViewer sviewer;
476: PetscViewerFormat format;
478: PetscFunctionBegin;
479: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
480: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
481: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
482: if (isascii) {
483: PetscCall(PetscViewerGetFormat(viewer, &format));
484: if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
485: MatInfo info;
486: PetscInt *inodes;
488: PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)mat), &rank));
489: PetscCall(MatGetInfo(mat, MAT_LOCAL, &info));
490: PetscCall(MatInodeGetInodeSizes(sell->A, NULL, &inodes, NULL));
491: PetscCall(PetscViewerASCIIPushSynchronized(viewer));
492: if (!inodes) {
493: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Local rows %" PetscInt_FMT " nz %" PetscInt_FMT " nz alloced %" PetscInt_FMT " mem %" PetscInt_FMT ", not using I-node routines\n", rank, mat->rmap->n, (PetscInt)info.nz_used,
494: (PetscInt)info.nz_allocated, (PetscInt)info.memory));
495: } else {
496: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "[%d] Local rows %" PetscInt_FMT " nz %" PetscInt_FMT " nz alloced %" PetscInt_FMT " mem %" PetscInt_FMT ", using I-node routines\n", rank, mat->rmap->n, (PetscInt)info.nz_used,
497: (PetscInt)info.nz_allocated, (PetscInt)info.memory));
498: }
499: PetscCall(MatGetInfo(sell->A, MAT_LOCAL, &info));
500: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "[%d] on-diagonal part: nz %" PetscInt_FMT " \n", rank, (PetscInt)info.nz_used));
501: PetscCall(MatGetInfo(sell->B, MAT_LOCAL, &info));
502: PetscCall(PetscViewerASCIISynchronizedPrintf(viewer, "[%d] off-diagonal part: nz %" PetscInt_FMT " \n", rank, (PetscInt)info.nz_used));
503: PetscCall(PetscViewerFlush(viewer));
504: PetscCall(PetscViewerASCIIPopSynchronized(viewer));
505: PetscCall(PetscViewerASCIIPrintf(viewer, "Information on VecScatter used in matrix-vector product: \n"));
506: PetscCall(VecScatterView(sell->Mvctx, viewer));
507: PetscFunctionReturn(PETSC_SUCCESS);
508: } else if (format == PETSC_VIEWER_ASCII_INFO) {
509: PetscInt inodecount, inodelimit, *inodes;
510: PetscCall(MatInodeGetInodeSizes(sell->A, &inodecount, &inodes, &inodelimit));
511: if (inodes) {
512: PetscCall(PetscViewerASCIIPrintf(viewer, "using I-node (on process 0) routines: found %" PetscInt_FMT " nodes, limit used is %" PetscInt_FMT "\n", inodecount, inodelimit));
513: } else {
514: PetscCall(PetscViewerASCIIPrintf(viewer, "not using I-node (on process 0) routines\n"));
515: }
516: PetscFunctionReturn(PETSC_SUCCESS);
517: } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
518: PetscFunctionReturn(PETSC_SUCCESS);
519: }
520: } else if (isbinary) {
521: if (size == 1) {
522: PetscCall(PetscObjectSetName((PetscObject)sell->A, ((PetscObject)mat)->name));
523: PetscCall(MatView(sell->A, viewer));
524: } else {
525: /* PetscCall(MatView_MPISELL_Binary(mat,viewer)); */
526: }
527: PetscFunctionReturn(PETSC_SUCCESS);
528: } else if (isdraw) {
529: PetscDraw draw;
530: PetscBool isnull;
531: PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw));
532: PetscCall(PetscDrawIsNull(draw, &isnull));
533: if (isnull) PetscFunctionReturn(PETSC_SUCCESS);
534: }
536: {
537: /* assemble the entire matrix onto first processor. */
538: Mat A;
539: Mat_SeqSELL *Aloc;
540: PetscInt M = mat->rmap->N, N = mat->cmap->N, *acolidx, row, col, i, j;
541: MatScalar *aval;
542: PetscBool isnonzero;
544: PetscCall(MatCreate(PetscObjectComm((PetscObject)mat), &A));
545: if (rank == 0) {
546: PetscCall(MatSetSizes(A, M, N, M, N));
547: } else {
548: PetscCall(MatSetSizes(A, 0, 0, M, N));
549: }
550: /* This is just a temporary matrix, so explicitly using MATMPISELL is probably best */
551: PetscCall(MatSetType(A, MATMPISELL));
552: PetscCall(MatMPISELLSetPreallocation(A, 0, NULL, 0, NULL));
553: PetscCall(MatSetOption(A, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_FALSE));
555: /* copy over the A part */
556: Aloc = (Mat_SeqSELL *)sell->A->data;
557: acolidx = Aloc->colidx;
558: aval = Aloc->val;
559: for (i = 0; i < Aloc->totalslices; i++) { /* loop over slices */
560: for (j = Aloc->sliidx[i]; j < Aloc->sliidx[i + 1]; j++) {
561: isnonzero = (PetscBool)((j - Aloc->sliidx[i]) / Aloc->sliceheight < Aloc->rlen[i * Aloc->sliceheight + j % Aloc->sliceheight]);
562: if (isnonzero) { /* check the mask bit */
563: row = i * Aloc->sliceheight + j % Aloc->sliceheight + mat->rmap->rstart;
564: col = *acolidx + mat->rmap->rstart;
565: PetscCall(MatSetValues(A, 1, &row, 1, &col, aval, INSERT_VALUES));
566: }
567: aval++;
568: acolidx++;
569: }
570: }
572: /* copy over the B part */
573: Aloc = (Mat_SeqSELL *)sell->B->data;
574: acolidx = Aloc->colidx;
575: aval = Aloc->val;
576: for (i = 0; i < Aloc->totalslices; i++) {
577: for (j = Aloc->sliidx[i]; j < Aloc->sliidx[i + 1]; j++) {
578: isnonzero = (PetscBool)((j - Aloc->sliidx[i]) / Aloc->sliceheight < Aloc->rlen[i * Aloc->sliceheight + j % Aloc->sliceheight]);
579: if (isnonzero) {
580: row = i * Aloc->sliceheight + j % Aloc->sliceheight + mat->rmap->rstart;
581: col = sell->garray[*acolidx];
582: PetscCall(MatSetValues(A, 1, &row, 1, &col, aval, INSERT_VALUES));
583: }
584: aval++;
585: acolidx++;
586: }
587: }
589: PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
590: PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
591: /*
592: Everyone has to call to draw the matrix since the graphics waits are
593: synchronized across all processors that share the PetscDraw object
594: */
595: PetscCall(PetscViewerGetSubViewer(viewer, PETSC_COMM_SELF, &sviewer));
596: if (rank == 0) {
597: PetscCall(PetscObjectSetName((PetscObject)((Mat_MPISELL *)A->data)->A, ((PetscObject)mat)->name));
598: PetscCall(MatView_SeqSELL(((Mat_MPISELL *)A->data)->A, sviewer));
599: }
600: PetscCall(PetscViewerRestoreSubViewer(viewer, PETSC_COMM_SELF, &sviewer));
601: PetscCall(MatDestroy(&A));
602: }
603: PetscFunctionReturn(PETSC_SUCCESS);
604: }
606: static PetscErrorCode MatView_MPISELL(Mat mat, PetscViewer viewer)
607: {
608: PetscBool isascii, isdraw, issocket, isbinary;
610: PetscFunctionBegin;
611: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
612: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
613: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
614: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERSOCKET, &issocket));
615: if (isascii || isdraw || isbinary || issocket) PetscCall(MatView_MPISELL_ASCIIorDraworSocket(mat, viewer));
616: PetscFunctionReturn(PETSC_SUCCESS);
617: }
619: static PetscErrorCode MatGetGhosts_MPISELL(Mat mat, PetscInt *nghosts, const PetscInt *ghosts[])
620: {
621: Mat_MPISELL *sell = (Mat_MPISELL *)mat->data;
623: PetscFunctionBegin;
624: PetscCall(MatGetSize(sell->B, NULL, nghosts));
625: if (ghosts) *ghosts = sell->garray;
626: PetscFunctionReturn(PETSC_SUCCESS);
627: }
629: static PetscErrorCode MatGetInfo_MPISELL(Mat matin, MatInfoType flag, MatInfo *info)
630: {
631: Mat_MPISELL *mat = (Mat_MPISELL *)matin->data;
632: Mat A = mat->A, B = mat->B;
633: PetscLogDouble irecv[5];
635: PetscFunctionBegin;
636: info->block_size = 1.0;
637: PetscCall(MatGetInfo(A, MAT_LOCAL, info));
639: irecv[0] = info->nz_used;
640: irecv[1] = info->nz_allocated;
641: irecv[2] = info->nz_unneeded;
642: irecv[3] = info->memory;
643: irecv[4] = info->mallocs;
645: PetscCall(MatGetInfo(B, MAT_LOCAL, info));
647: irecv[0] += info->nz_used;
648: irecv[1] += info->nz_allocated;
649: irecv[2] += info->nz_unneeded;
650: irecv[3] += info->memory;
651: irecv[4] += info->mallocs;
652: if (flag == MAT_LOCAL) {
653: info->nz_used = irecv[0];
654: info->nz_allocated = irecv[1];
655: info->nz_unneeded = irecv[2];
656: info->memory = irecv[3];
657: info->mallocs = irecv[4];
658: } else if (flag == MAT_GLOBAL_MAX) {
659: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, irecv, 5, MPIU_PETSCLOGDOUBLE, MPI_MAX, PetscObjectComm((PetscObject)matin)));
661: info->nz_used = irecv[0];
662: info->nz_allocated = irecv[1];
663: info->nz_unneeded = irecv[2];
664: info->memory = irecv[3];
665: info->mallocs = irecv[4];
666: } else if (flag == MAT_GLOBAL_SUM) {
667: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, irecv, 5, MPIU_PETSCLOGDOUBLE, MPI_SUM, PetscObjectComm((PetscObject)matin)));
669: info->nz_used = irecv[0];
670: info->nz_allocated = irecv[1];
671: info->nz_unneeded = irecv[2];
672: info->memory = irecv[3];
673: info->mallocs = irecv[4];
674: }
675: info->fill_ratio_given = 0; /* no parallel LU/ILU/Cholesky */
676: info->fill_ratio_needed = 0;
677: info->factor_mallocs = 0;
678: PetscFunctionReturn(PETSC_SUCCESS);
679: }
681: static PetscErrorCode MatSetOption_MPISELL(Mat A, MatOption op, PetscBool flg)
682: {
683: Mat_MPISELL *a = (Mat_MPISELL *)A->data;
685: PetscFunctionBegin;
686: switch (op) {
687: case MAT_NEW_NONZERO_LOCATIONS:
688: case MAT_NEW_NONZERO_ALLOCATION_ERR:
689: case MAT_UNUSED_NONZERO_LOCATION_ERR:
690: case MAT_KEEP_NONZERO_PATTERN:
691: case MAT_NEW_NONZERO_LOCATION_ERR:
692: case MAT_USE_INODES:
693: case MAT_IGNORE_ZERO_ENTRIES:
694: MatCheckPreallocated(A, 1);
695: PetscCall(MatSetOption(a->A, op, flg));
696: PetscCall(MatSetOption(a->B, op, flg));
697: break;
698: case MAT_ROW_ORIENTED:
699: MatCheckPreallocated(A, 1);
700: a->roworiented = flg;
702: PetscCall(MatSetOption(a->A, op, flg));
703: PetscCall(MatSetOption(a->B, op, flg));
704: break;
705: case MAT_IGNORE_OFF_PROC_ENTRIES:
706: a->donotstash = flg;
707: break;
708: case MAT_SYMMETRIC:
709: MatCheckPreallocated(A, 1);
710: PetscCall(MatSetOption(a->A, op, flg));
711: break;
712: case MAT_STRUCTURALLY_SYMMETRIC:
713: MatCheckPreallocated(A, 1);
714: PetscCall(MatSetOption(a->A, op, flg));
715: break;
716: case MAT_HERMITIAN:
717: MatCheckPreallocated(A, 1);
718: PetscCall(MatSetOption(a->A, op, flg));
719: break;
720: case MAT_SYMMETRY_ETERNAL:
721: MatCheckPreallocated(A, 1);
722: PetscCall(MatSetOption(a->A, op, flg));
723: break;
724: case MAT_STRUCTURAL_SYMMETRY_ETERNAL:
725: MatCheckPreallocated(A, 1);
726: PetscCall(MatSetOption(a->A, op, flg));
727: break;
728: default:
729: break;
730: }
731: PetscFunctionReturn(PETSC_SUCCESS);
732: }
734: static PetscErrorCode MatDiagonalScale_MPISELL(Mat mat, Vec ll, Vec rr)
735: {
736: Mat_MPISELL *sell = (Mat_MPISELL *)mat->data;
737: Mat a = sell->A, b = sell->B;
738: PetscInt s1, s2, s3;
740: PetscFunctionBegin;
741: PetscCall(MatGetLocalSize(mat, &s2, &s3));
742: if (rr) {
743: PetscCall(VecGetLocalSize(rr, &s1));
744: PetscCheck(s1 == s3, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "right vector non-conforming local size");
745: /* Overlap communication with computation. */
746: PetscCall(VecScatterBegin(sell->Mvctx, rr, sell->lvec, INSERT_VALUES, SCATTER_FORWARD));
747: }
748: if (ll) {
749: PetscCall(VecGetLocalSize(ll, &s1));
750: PetscCheck(s1 == s2, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "left vector non-conforming local size");
751: PetscUseTypeMethod(b, diagonalscale, ll, NULL);
752: }
753: /* scale the diagonal block */
754: PetscUseTypeMethod(a, diagonalscale, ll, rr);
756: if (rr) {
757: /* Do a scatter end and then right scale the off-diagonal block */
758: PetscCall(VecScatterEnd(sell->Mvctx, rr, sell->lvec, INSERT_VALUES, SCATTER_FORWARD));
759: PetscUseTypeMethod(b, diagonalscale, NULL, sell->lvec);
760: }
761: PetscFunctionReturn(PETSC_SUCCESS);
762: }
764: static PetscErrorCode MatSetUnfactored_MPISELL(Mat A)
765: {
766: Mat_MPISELL *a = (Mat_MPISELL *)A->data;
768: PetscFunctionBegin;
769: PetscCall(MatSetUnfactored(a->A));
770: PetscFunctionReturn(PETSC_SUCCESS);
771: }
773: static PetscErrorCode MatEqual_MPISELL(Mat A, Mat B, PetscBool *flag)
774: {
775: Mat_MPISELL *matB = (Mat_MPISELL *)B->data, *matA = (Mat_MPISELL *)A->data;
776: Mat a, b, c, d;
778: PetscFunctionBegin;
779: a = matA->A;
780: b = matA->B;
781: c = matB->A;
782: d = matB->B;
784: PetscCall(MatEqual(a, c, flag));
785: if (*flag) PetscCall(MatEqual(b, d, flag));
786: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, flag, 1, MPI_C_BOOL, MPI_LAND, PetscObjectComm((PetscObject)A)));
787: PetscFunctionReturn(PETSC_SUCCESS);
788: }
790: static PetscErrorCode MatCopy_MPISELL(Mat A, Mat B, MatStructure str)
791: {
792: Mat_MPISELL *a = (Mat_MPISELL *)A->data;
793: Mat_MPISELL *b = (Mat_MPISELL *)B->data;
795: PetscFunctionBegin;
796: /* If the two matrices don't have the same copy implementation, they aren't compatible for fast copy. */
797: if (str != SAME_NONZERO_PATTERN || A->ops->copy != B->ops->copy) {
798: /* because of the column compression in the off-processor part of the matrix a->B,
799: the number of columns in a->B and b->B may be different, hence we cannot call
800: the MatCopy() directly on the two parts. If need be, we can provide a more
801: efficient copy than the MatCopy_Basic() by first uncompressing the a->B matrices
802: then copying the submatrices */
803: PetscCall(MatCopy_Basic(A, B, str));
804: } else {
805: PetscCall(MatCopy(a->A, b->A, str));
806: PetscCall(MatCopy(a->B, b->B, str));
807: }
808: PetscFunctionReturn(PETSC_SUCCESS);
809: }
811: static PetscErrorCode MatSetUp_MPISELL(Mat A)
812: {
813: PetscFunctionBegin;
814: PetscCall(MatMPISELLSetPreallocation(A, PETSC_DEFAULT, NULL, PETSC_DEFAULT, NULL));
815: PetscFunctionReturn(PETSC_SUCCESS);
816: }
818: static PetscErrorCode MatConjugate_MPISELL(Mat mat)
819: {
820: Mat_MPISELL *sell = (Mat_MPISELL *)mat->data;
822: PetscFunctionBegin;
823: PetscCall(MatConjugate_SeqSELL(sell->A));
824: PetscCall(MatConjugate_SeqSELL(sell->B));
825: PetscFunctionReturn(PETSC_SUCCESS);
826: }
828: static PetscErrorCode MatInvertBlockDiagonal_MPISELL(Mat A, const PetscScalar **values)
829: {
830: Mat_MPISELL *a = (Mat_MPISELL *)A->data;
832: PetscFunctionBegin;
833: PetscCall(MatInvertBlockDiagonal(a->A, values));
834: A->factorerrortype = a->A->factorerrortype;
835: PetscFunctionReturn(PETSC_SUCCESS);
836: }
838: static PetscErrorCode MatSetRandom_MPISELL(Mat x, PetscRandom rctx)
839: {
840: Mat_MPISELL *sell = (Mat_MPISELL *)x->data;
841: PetscBool nooffprocentries = x->nooffprocentries;
843: PetscFunctionBegin;
844: PetscCall(MatSetRandom(sell->A, rctx));
845: PetscCall(MatSetRandom(sell->B, rctx));
846: PetscCall(MatSetOption(x, MAT_NO_OFF_PROC_ENTRIES, PETSC_TRUE));
847: PetscCall(MatAssemblyBegin(x, MAT_FINAL_ASSEMBLY));
848: PetscCall(MatAssemblyEnd(x, MAT_FINAL_ASSEMBLY));
849: PetscCall(MatSetOption(x, MAT_NO_OFF_PROC_ENTRIES, nooffprocentries));
850: PetscFunctionReturn(PETSC_SUCCESS);
851: }
853: static PetscErrorCode MatSetFromOptions_MPISELL(Mat A, PetscOptionItems PetscOptionsObject)
854: {
855: PetscFunctionBegin;
856: PetscOptionsHeadBegin(PetscOptionsObject, "MPISELL options");
857: PetscOptionsHeadEnd();
858: PetscFunctionReturn(PETSC_SUCCESS);
859: }
861: static PetscErrorCode MatShift_MPISELL(Mat Y, PetscScalar a)
862: {
863: Mat_MPISELL *msell = (Mat_MPISELL *)Y->data;
864: Mat_SeqSELL *sell = (Mat_SeqSELL *)msell->A->data;
866: PetscFunctionBegin;
867: if (!Y->preallocated) {
868: PetscCall(MatMPISELLSetPreallocation(Y, 1, NULL, 0, NULL));
869: } else if (!sell->nz) {
870: PetscInt nonew = sell->nonew;
871: PetscCall(MatSeqSELLSetPreallocation(msell->A, 1, NULL));
872: sell->nonew = nonew;
873: }
874: PetscCall(MatShift_Basic(Y, a));
875: PetscFunctionReturn(PETSC_SUCCESS);
876: }
878: static PetscErrorCode MatGetDiagonalBlock_MPISELL(Mat A, Mat *a)
879: {
880: PetscFunctionBegin;
881: *a = ((Mat_MPISELL *)A->data)->A;
882: PetscFunctionReturn(PETSC_SUCCESS);
883: }
885: static PetscErrorCode MatStoreValues_MPISELL(Mat mat)
886: {
887: Mat_MPISELL *sell = (Mat_MPISELL *)mat->data;
889: PetscFunctionBegin;
890: PetscCall(MatStoreValues(sell->A));
891: PetscCall(MatStoreValues(sell->B));
892: PetscFunctionReturn(PETSC_SUCCESS);
893: }
895: static PetscErrorCode MatRetrieveValues_MPISELL(Mat mat)
896: {
897: Mat_MPISELL *sell = (Mat_MPISELL *)mat->data;
899: PetscFunctionBegin;
900: PetscCall(MatRetrieveValues(sell->A));
901: PetscCall(MatRetrieveValues(sell->B));
902: PetscFunctionReturn(PETSC_SUCCESS);
903: }
905: static PetscErrorCode MatMPISELLSetPreallocation_MPISELL(Mat B, PetscInt d_rlenmax, const PetscInt d_rlen[], PetscInt o_rlenmax, const PetscInt o_rlen[])
906: {
907: Mat_MPISELL *b;
909: PetscFunctionBegin;
910: PetscCall(PetscLayoutSetUp(B->rmap));
911: PetscCall(PetscLayoutSetUp(B->cmap));
912: b = (Mat_MPISELL *)B->data;
914: if (!B->preallocated) {
915: /* Explicitly create 2 MATSEQSELL matrices. */
916: PetscCall(MatCreate(PETSC_COMM_SELF, &b->A));
917: PetscCall(MatSetSizes(b->A, B->rmap->n, B->cmap->n, B->rmap->n, B->cmap->n));
918: PetscCall(MatSetBlockSizesFromMats(b->A, B, B));
919: PetscCall(MatSetType(b->A, MATSEQSELL));
920: PetscCall(MatCreate(PETSC_COMM_SELF, &b->B));
921: PetscCall(MatSetSizes(b->B, B->rmap->n, B->cmap->N, B->rmap->n, B->cmap->N));
922: PetscCall(MatSetBlockSizesFromMats(b->B, B, B));
923: PetscCall(MatSetType(b->B, MATSEQSELL));
924: }
926: PetscCall(MatSeqSELLSetPreallocation(b->A, d_rlenmax, d_rlen));
927: PetscCall(MatSeqSELLSetPreallocation(b->B, o_rlenmax, o_rlen));
928: B->preallocated = PETSC_TRUE;
929: B->was_assembled = PETSC_FALSE;
930: /*
931: critical for MatAssemblyEnd to work.
932: MatAssemblyBegin checks it to set up was_assembled
933: and MatAssemblyEnd checks was_assembled to determine whether to build garray
934: */
935: B->assembled = PETSC_FALSE;
936: PetscFunctionReturn(PETSC_SUCCESS);
937: }
939: static PetscErrorCode MatDuplicate_MPISELL(Mat matin, MatDuplicateOption cpvalues, Mat *newmat)
940: {
941: Mat mat;
942: Mat_MPISELL *a, *oldmat = (Mat_MPISELL *)matin->data;
944: PetscFunctionBegin;
945: *newmat = NULL;
946: PetscCall(MatCreate(PetscObjectComm((PetscObject)matin), &mat));
947: PetscCall(MatSetSizes(mat, matin->rmap->n, matin->cmap->n, matin->rmap->N, matin->cmap->N));
948: PetscCall(MatSetBlockSizesFromMats(mat, matin, matin));
949: PetscCall(MatSetType(mat, ((PetscObject)matin)->type_name));
950: a = (Mat_MPISELL *)mat->data;
952: mat->factortype = matin->factortype;
953: mat->assembled = PETSC_TRUE;
954: mat->insertmode = NOT_SET_VALUES;
955: mat->preallocated = PETSC_TRUE;
957: a->size = oldmat->size;
958: a->rank = oldmat->rank;
959: a->donotstash = oldmat->donotstash;
960: a->roworiented = oldmat->roworiented;
961: a->rowindices = NULL;
962: a->rowvalues = NULL;
963: a->getrowactive = PETSC_FALSE;
965: PetscCall(PetscLayoutReference(matin->rmap, &mat->rmap));
966: PetscCall(PetscLayoutReference(matin->cmap, &mat->cmap));
968: if (oldmat->colmap) {
969: #if PetscDefined(USE_CTABLE)
970: PetscCall(PetscHMapIDuplicate(oldmat->colmap, &a->colmap));
971: #else
972: PetscCall(PetscMalloc1(mat->cmap->N, &a->colmap));
973: PetscCall(PetscArraycpy(a->colmap, oldmat->colmap, mat->cmap->N));
974: #endif
975: } else a->colmap = NULL;
976: if (oldmat->garray) {
977: PetscInt len;
978: len = oldmat->B->cmap->n;
979: PetscCall(PetscMalloc1(len + 1, &a->garray));
980: if (len) PetscCall(PetscArraycpy(a->garray, oldmat->garray, len));
981: } else a->garray = NULL;
983: PetscCall(VecDuplicate(oldmat->lvec, &a->lvec));
984: PetscCall(VecScatterCopy(oldmat->Mvctx, &a->Mvctx));
985: PetscCall(MatDuplicate(oldmat->A, cpvalues, &a->A));
986: PetscCall(MatDuplicate(oldmat->B, cpvalues, &a->B));
987: PetscCall(PetscFunctionListDuplicate(((PetscObject)matin)->qlist, &((PetscObject)mat)->qlist));
988: *newmat = mat;
989: PetscFunctionReturn(PETSC_SUCCESS);
990: }
992: static const struct _MatOps MatOps_Values = {MatSetValues_MPISELL,
993: NULL,
994: NULL,
995: MatMult_MPISELL,
996: /* 4*/ MatMultAdd_MPISELL,
997: MatMultTranspose_MPISELL,
998: MatMultTransposeAdd_MPISELL,
999: NULL,
1000: NULL,
1001: NULL,
1002: /*10*/ NULL,
1003: NULL,
1004: NULL,
1005: MatSOR_MPISELL,
1006: NULL,
1007: /*15*/ MatGetInfo_MPISELL,
1008: MatEqual_MPISELL,
1009: MatGetDiagonal_MPISELL,
1010: MatDiagonalScale_MPISELL,
1011: NULL,
1012: /*20*/ MatAssemblyBegin_MPISELL,
1013: MatAssemblyEnd_MPISELL,
1014: MatSetOption_MPISELL,
1015: MatZeroEntries_MPISELL,
1016: /*24*/ NULL,
1017: NULL,
1018: NULL,
1019: NULL,
1020: NULL,
1021: /*29*/ MatSetUp_MPISELL,
1022: NULL,
1023: NULL,
1024: MatGetDiagonalBlock_MPISELL,
1025: NULL,
1026: /*34*/ MatDuplicate_MPISELL,
1027: NULL,
1028: NULL,
1029: NULL,
1030: NULL,
1031: /*39*/ NULL,
1032: NULL,
1033: NULL,
1034: MatGetValues_MPISELL,
1035: MatCopy_MPISELL,
1036: /*44*/ NULL,
1037: MatScale_MPISELL,
1038: MatShift_MPISELL,
1039: MatDiagonalSet_MPISELL,
1040: NULL,
1041: /*49*/ MatSetRandom_MPISELL,
1042: NULL,
1043: NULL,
1044: NULL,
1045: NULL,
1046: /*54*/ MatFDColoringCreate_MPIXAIJ,
1047: NULL,
1048: MatSetUnfactored_MPISELL,
1049: NULL,
1050: NULL,
1051: /*59*/ NULL,
1052: MatDestroy_MPISELL,
1053: MatView_MPISELL,
1054: NULL,
1055: NULL,
1056: /*64*/ NULL,
1057: NULL,
1058: NULL,
1059: NULL,
1060: NULL,
1061: /*69*/ NULL,
1062: NULL,
1063: NULL,
1064: MatFDColoringApply_AIJ, /* reuse AIJ function */
1065: MatSetFromOptions_MPISELL,
1066: NULL,
1067: /*75*/ NULL,
1068: NULL,
1069: NULL,
1070: NULL,
1071: NULL,
1072: /*80*/ NULL,
1073: NULL,
1074: NULL,
1075: /*83*/ NULL,
1076: NULL,
1077: NULL,
1078: NULL,
1079: NULL,
1080: NULL,
1081: /*89*/ NULL,
1082: NULL,
1083: NULL,
1084: NULL,
1085: MatConjugate_MPISELL,
1086: /*94*/ NULL,
1087: NULL,
1088: NULL,
1089: NULL,
1090: NULL,
1091: /*99*/ NULL,
1092: NULL,
1093: NULL,
1094: NULL,
1095: NULL,
1096: /*104*/ NULL,
1097: NULL,
1098: MatGetGhosts_MPISELL,
1099: NULL,
1100: NULL,
1101: /*109*/ MatMultDiagonalBlock_MPISELL,
1102: NULL,
1103: NULL,
1104: NULL,
1105: NULL,
1106: /*114*/ NULL,
1107: NULL,
1108: MatInvertBlockDiagonal_MPISELL,
1109: NULL,
1110: /*119*/ NULL,
1111: NULL,
1112: NULL,
1113: NULL,
1114: NULL,
1115: /*124*/ NULL,
1116: NULL,
1117: NULL,
1118: NULL,
1119: MatFDColoringSetUp_MPIXAIJ,
1120: /*129*/ NULL,
1121: NULL,
1122: NULL,
1123: NULL,
1124: NULL,
1125: /*134*/ NULL,
1126: NULL,
1127: NULL,
1128: NULL,
1129: NULL,
1130: /*139*/ NULL,
1131: NULL,
1132: NULL,
1133: NULL,
1134: NULL,
1135: NULL,
1136: /*144*/ NULL,
1137: NULL,
1138: NULL,
1139: NULL};
1141: /*@
1142: MatMPISELLSetPreallocation - Preallocates memory for a `MATMPISELL` sparse parallel matrix in sell format.
1143: For good matrix assembly performance the user should preallocate the matrix storage by
1144: setting the parameters `d_nz` (or `d_nnz`) and `o_nz` (or `o_nnz`).
1146: Collective
1148: Input Parameters:
1149: + B - the matrix
1150: . d_nz - number of nonzeros per row in DIAGONAL portion of local submatrix
1151: (same value is used for all local rows)
1152: . d_nnz - array containing the number of nonzeros in the various rows of the
1153: DIAGONAL portion of the local submatrix (possibly different for each row)
1154: or NULL (`PETSC_NULL_INTEGER` in Fortran), if `d_nz` is used to specify the nonzero structure.
1155: The size of this array is equal to the number of local rows, i.e 'm'.
1156: For matrices that will be factored, you must leave room for (and set)
1157: the diagonal entry even if it is zero.
1158: . o_nz - number of nonzeros per row in the OFF-DIAGONAL portion of local
1159: submatrix (same value is used for all local rows).
1160: - o_nnz - array containing the number of nonzeros in the various rows of the
1161: OFF-DIAGONAL portion of the local submatrix (possibly different for
1162: each row) or NULL (`PETSC_NULL_INTEGER` in Fortran), if `o_nz` is used to specify the nonzero
1163: structure. The size of this array is equal to the number
1164: of local rows, i.e 'm'.
1166: Example usage:
1167: Consider the following 8x8 matrix with 34 non-zero values, that is
1168: assembled across 3 processors. Lets assume that proc0 owns 3 rows,
1169: proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
1170: as follows
1172: .vb
1173: 1 2 0 | 0 3 0 | 0 4
1174: Proc0 0 5 6 | 7 0 0 | 8 0
1175: 9 0 10 | 11 0 0 | 12 0
1176: -------------------------------------
1177: 13 0 14 | 15 16 17 | 0 0
1178: Proc1 0 18 0 | 19 20 21 | 0 0
1179: 0 0 0 | 22 23 0 | 24 0
1180: -------------------------------------
1181: Proc2 25 26 27 | 0 0 28 | 29 0
1182: 30 0 0 | 31 32 33 | 0 34
1183: .ve
1185: This can be represented as a collection of submatrices as
1187: .vb
1188: A B C
1189: D E F
1190: G H I
1191: .ve
1193: Where the submatrices A,B,C are owned by proc0, D,E,F are
1194: owned by proc1, G,H,I are owned by proc2.
1196: The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
1197: The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
1198: The 'M','N' parameters are 8,8, and have the same values on all procs.
1200: The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
1201: submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
1202: corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
1203: Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
1204: part as `MATSEQSELL` matrices. For example, proc1 will store [E] as a `MATSEQSELL`
1205: matrix, and [DF] as another SeqSELL matrix.
1207: When `d_nz`, `o_nz` parameters are specified, `d_nz` storage elements are
1208: allocated for every row of the local DIAGONAL submatrix, and o_nz
1209: storage locations are allocated for every row of the OFF-DIAGONAL submatrix.
1210: One way to choose `d_nz` and `o_nz` is to use the maximum number of nonzeros over
1211: the local rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
1212: In this case, the values of d_nz,o_nz are
1213: .vb
1214: proc0 dnz = 2, o_nz = 2
1215: proc1 dnz = 3, o_nz = 2
1216: proc2 dnz = 1, o_nz = 4
1217: .ve
1218: We are allocating m*(d_nz+o_nz) storage locations for every proc. This
1219: translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
1220: for proc3. i.e we are using 12+15+10=37 storage locations to store
1221: 34 values.
1223: When `d_nnz`, `o_nnz` parameters are specified, the storage is specified
1224: for every row, corresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
1225: In the above case the values for d_nnz,o_nnz are
1226: .vb
1227: proc0 d_nnz = [2,2,2] and o_nnz = [2,2,2]
1228: proc1 d_nnz = [3,3,2] and o_nnz = [2,1,1]
1229: proc2 d_nnz = [1,1] and o_nnz = [4,4]
1230: .ve
1231: Here the space allocated is according to nz (or maximum values in the nnz
1232: if nnz is provided) for DIAGONAL and OFF-DIAGONAL submatrices, i.e (2+2+3+2)*3+(1+4)*2=37
1234: Level: intermediate
1236: Notes:
1237: If the *_nnz parameter is given then the *_nz parameter is ignored
1239: The stored row and column indices begin with zero.
1241: The parallel matrix is partitioned such that the first m0 rows belong to
1242: process 0, the next m1 rows belong to process 1, the next m2 rows belong
1243: to process 2 etc.. where m0,m1,m2... are the input parameter 'm'.
1245: The DIAGONAL portion of the local submatrix of a processor can be defined
1246: as the submatrix which is obtained by extraction the part corresponding to
1247: the rows r1-r2 and columns c1-c2 of the global matrix, where r1 is the
1248: first row that belongs to the processor, r2 is the last row belonging to
1249: the this processor, and c1-c2 is range of indices of the local part of a
1250: vector suitable for applying the matrix to. This is an mxn matrix. In the
1251: common case of a square matrix, the row and column ranges are the same and
1252: the DIAGONAL part is also square. The remaining portion of the local
1253: submatrix (mxN) constitute the OFF-DIAGONAL portion.
1255: If `o_nnz`, `d_nnz` are specified, then `o_nz`, and `d_nz` are ignored.
1257: You can call `MatGetInfo()` to get information on how effective the preallocation was;
1258: for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
1259: You can also run with the option -info and look for messages with the string
1260: malloc in them to see if additional memory allocation was needed.
1262: .seealso: `Mat`, `MatCreate()`, `MatCreateSeqSELL()`, `MatSetValues()`, `MatCreateSELL()`,
1263: `MATMPISELL`, `MatGetInfo()`, `PetscSplitOwnership()`, `MATSELL`
1264: @*/
1265: PetscErrorCode MatMPISELLSetPreallocation(Mat B, PetscInt d_nz, const PetscInt d_nnz[], PetscInt o_nz, const PetscInt o_nnz[])
1266: {
1267: PetscFunctionBegin;
1270: PetscTryMethod(B, "MatMPISELLSetPreallocation_C", (Mat, PetscInt, const PetscInt[], PetscInt, const PetscInt[]), (B, d_nz, d_nnz, o_nz, o_nnz));
1271: PetscFunctionReturn(PETSC_SUCCESS);
1272: }
1274: /*MC
1275: MATMPISELL - MATMPISELL = "mpisell" - A matrix type to be used for MPI sparse matrices,
1276: based on the sliced Ellpack format
1278: Options Database Key:
1279: . -mat_type sell - sets the matrix type to `MATSELL` during a call to `MatSetFromOptions()`
1281: Level: beginner
1283: .seealso: `Mat`, `MatCreateSELL()`, `MATSEQSELL`, `MATSELL`, `MATSEQAIJ`, `MATAIJ`, `MATMPIAIJ`
1284: M*/
1286: /*@
1287: MatCreateSELL - Creates a sparse parallel matrix in `MATSELL` format.
1289: Collective
1291: Input Parameters:
1292: + comm - MPI communicator
1293: . m - number of local rows (or `PETSC_DECIDE` to have calculated if M is given)
1294: This value should be the same as the local size used in creating the
1295: y vector for the matrix-vector product y = Ax.
1296: . n - This value should be the same as the local size used in creating the
1297: x vector for the matrix-vector product y = Ax. (or `PETSC_DECIDE` to have
1298: calculated if `N` is given) For square matrices n is almost always `m`.
1299: . M - number of global rows (or `PETSC_DETERMINE` to have calculated if `m` is given)
1300: . N - number of global columns (or `PETSC_DETERMINE` to have calculated if `n` is given)
1301: . d_rlenmax - max number of nonzeros per row in DIAGONAL portion of local submatrix
1302: (same value is used for all local rows)
1303: . d_rlen - array containing the number of nonzeros in the various rows of the
1304: DIAGONAL portion of the local submatrix (possibly different for each row)
1305: or `NULL`, if d_rlenmax is used to specify the nonzero structure.
1306: The size of this array is equal to the number of local rows, i.e `m`.
1307: . o_rlenmax - max number of nonzeros per row in the OFF-DIAGONAL portion of local
1308: submatrix (same value is used for all local rows).
1309: - o_rlen - array containing the number of nonzeros in the various rows of the
1310: OFF-DIAGONAL portion of the local submatrix (possibly different for
1311: each row) or `NULL`, if `o_rlenmax` is used to specify the nonzero
1312: structure. The size of this array is equal to the number
1313: of local rows, i.e `m`.
1315: Output Parameter:
1316: . A - the matrix
1318: Options Database Key:
1319: . -mat_sell_oneindex - Internally use indexing starting at 1
1320: rather than 0. When calling `MatSetValues()`,
1321: the user still MUST index entries starting at 0!
1323: Example:
1324: Consider the following 8x8 matrix with 34 non-zero values, that is
1325: assembled across 3 processors. Lets assume that proc0 owns 3 rows,
1326: proc1 owns 3 rows, proc2 owns 2 rows. This division can be shown
1327: as follows
1329: .vb
1330: 1 2 0 | 0 3 0 | 0 4
1331: Proc0 0 5 6 | 7 0 0 | 8 0
1332: 9 0 10 | 11 0 0 | 12 0
1333: -------------------------------------
1334: 13 0 14 | 15 16 17 | 0 0
1335: Proc1 0 18 0 | 19 20 21 | 0 0
1336: 0 0 0 | 22 23 0 | 24 0
1337: -------------------------------------
1338: Proc2 25 26 27 | 0 0 28 | 29 0
1339: 30 0 0 | 31 32 33 | 0 34
1340: .ve
1342: This can be represented as a collection of submatrices as
1343: .vb
1344: A B C
1345: D E F
1346: G H I
1347: .ve
1349: Where the submatrices A,B,C are owned by proc0, D,E,F are
1350: owned by proc1, G,H,I are owned by proc2.
1352: The 'm' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
1353: The 'n' parameters for proc0,proc1,proc2 are 3,3,2 respectively.
1354: The 'M','N' parameters are 8,8, and have the same values on all procs.
1356: The DIAGONAL submatrices corresponding to proc0,proc1,proc2 are
1357: submatrices [A], [E], [I] respectively. The OFF-DIAGONAL submatrices
1358: corresponding to proc0,proc1,proc2 are [BC], [DF], [GH] respectively.
1359: Internally, each processor stores the DIAGONAL part, and the OFF-DIAGONAL
1360: part as `MATSEQSELL` matrices. For example, proc1 will store [E] as a `MATSEQSELL`
1361: matrix, and [DF] as another `MATSEQSELL` matrix.
1363: When d_rlenmax, o_rlenmax parameters are specified, d_rlenmax storage elements are
1364: allocated for every row of the local DIAGONAL submatrix, and o_rlenmax
1365: storage locations are allocated for every row of the OFF-DIAGONAL submatrix.
1366: One way to choose `d_rlenmax` and `o_rlenmax` is to use the maximum number of nonzeros over
1367: the local rows for each of the local DIAGONAL, and the OFF-DIAGONAL submatrices.
1368: In this case, the values of d_rlenmax,o_rlenmax are
1369: .vb
1370: proc0 - d_rlenmax = 2, o_rlenmax = 2
1371: proc1 - d_rlenmax = 3, o_rlenmax = 2
1372: proc2 - d_rlenmax = 1, o_rlenmax = 4
1373: .ve
1374: We are allocating m*(d_rlenmax+o_rlenmax) storage locations for every proc. This
1375: translates to 3*(2+2)=12 for proc0, 3*(3+2)=15 for proc1, 2*(1+4)=10
1376: for proc3. i.e we are using 12+15+10=37 storage locations to store
1377: 34 values.
1379: When `d_rlen`, `o_rlen` parameters are specified, the storage is specified
1380: for every row, corresponding to both DIAGONAL and OFF-DIAGONAL submatrices.
1381: In the above case the values for `d_nnz`, `o_nnz` are
1382: .vb
1383: proc0 - d_nnz = [2,2,2] and o_nnz = [2,2,2]
1384: proc1 - d_nnz = [3,3,2] and o_nnz = [2,1,1]
1385: proc2 - d_nnz = [1,1] and o_nnz = [4,4]
1386: .ve
1387: Here the space allocated is still 37 though there are 34 nonzeros because
1388: the allocation is always done according to rlenmax.
1390: Level: intermediate
1392: Notes:
1393: It is recommended that one use the `MatCreate()`, `MatSetType()` and/or `MatSetFromOptions()`,
1394: MatXXXXSetPreallocation() paradigm instead of this routine directly.
1395: [MatXXXXSetPreallocation() is, for example, `MatSeqSELLSetPreallocation()`]
1397: If the *_rlen parameter is given then the *_rlenmax parameter is ignored
1399: `m`, `n`, `M`, `N` parameters specify the size of the matrix, and its partitioning across
1400: processors, while `d_rlenmax`, `d_rlen`, `o_rlenmax` , `o_rlen` parameters specify the approximate
1401: storage requirements for this matrix.
1403: If `PETSC_DECIDE` or `PETSC_DETERMINE` is used for a particular argument on one
1404: processor than it must be used on all processors that share the object for
1405: that argument.
1407: The user MUST specify either the local or global matrix dimensions
1408: (possibly both).
1410: The parallel matrix is partitioned across processors such that the
1411: first m0 rows belong to process 0, the next m1 rows belong to
1412: process 1, the next m2 rows belong to process 2 etc.. where
1413: m0,m1,m2,.. are the input parameter 'm'. i.e each processor stores
1414: values corresponding to [`m` x `N`] submatrix.
1416: The columns are logically partitioned with the n0 columns belonging
1417: to 0th partition, the next n1 columns belonging to the next
1418: partition etc.. where n0,n1,n2... are the input parameter `n`.
1420: The DIAGONAL portion of the local submatrix on any given processor
1421: is the submatrix corresponding to the rows and columns `m`, `n`
1422: corresponding to the given processor. i.e diagonal matrix on
1423: process 0 is [m0 x n0], diagonal matrix on process 1 is [m1 x n1]
1424: etc. The remaining portion of the local submatrix [m x (N-n)]
1425: constitute the OFF-DIAGONAL portion. The example below better
1426: illustrates this concept.
1428: For a square global matrix we define each processor's diagonal portion
1429: to be its local rows and the corresponding columns (a square submatrix);
1430: each processor's off-diagonal portion encompasses the remainder of the
1431: local matrix (a rectangular submatrix).
1433: If `o_rlen`, `d_rlen` are specified, then `o_rlenmax`, and `d_rlenmax` are ignored.
1435: When calling this routine with a single process communicator, a matrix of
1436: type `MATSEQSELL` is returned. If a matrix of type `MATMPISELL` is desired for this
1437: type of communicator, use the construction mechanism
1438: .vb
1439: MatCreate(...,&A);
1440: MatSetType(A,MATMPISELL);
1441: MatSetSizes(A, m,n,M,N);
1442: MatMPISELLSetPreallocation(A,...);
1443: .ve
1445: .seealso: `Mat`, `MATSELL`, `MatCreate()`, `MatCreateSeqSELL()`, `MatSetValues()`, `MatMPISELLSetPreallocation()`, `MATMPISELL`
1446: @*/
1447: PetscErrorCode MatCreateSELL(MPI_Comm comm, PetscInt m, PetscInt n, PetscInt M, PetscInt N, PetscInt d_rlenmax, const PetscInt d_rlen[], PetscInt o_rlenmax, const PetscInt o_rlen[], Mat *A)
1448: {
1449: PetscMPIInt size;
1451: PetscFunctionBegin;
1452: PetscCall(MatCreate(comm, A));
1453: PetscCall(MatSetSizes(*A, m, n, M, N));
1454: PetscCallMPI(MPI_Comm_size(comm, &size));
1455: if (size > 1) {
1456: PetscCall(MatSetType(*A, MATMPISELL));
1457: PetscCall(MatMPISELLSetPreallocation(*A, d_rlenmax, d_rlen, o_rlenmax, o_rlen));
1458: } else {
1459: PetscCall(MatSetType(*A, MATSEQSELL));
1460: PetscCall(MatSeqSELLSetPreallocation(*A, d_rlenmax, d_rlen));
1461: }
1462: PetscFunctionReturn(PETSC_SUCCESS);
1463: }
1465: /*@
1466: MatMPISELLGetSeqSELL - Returns the local pieces of this distributed matrix
1468: Not Collective
1470: Input Parameter:
1471: . A - the `MATMPISELL` matrix
1473: Output Parameters:
1474: + Ad - The diagonal portion of `A`
1475: . Ao - The off-diagonal portion of `A`
1476: - colmap - An array mapping local column numbers of `Ao` to global column numbers of the parallel matrix
1478: Level: advanced
1480: .seealso: `Mat`, `MATSEQSELL`, `MATMPISELL`
1481: @*/
1482: PetscErrorCode MatMPISELLGetSeqSELL(Mat A, Mat *Ad, Mat *Ao, const PetscInt *colmap[])
1483: {
1484: Mat_MPISELL *a = (Mat_MPISELL *)A->data;
1485: PetscBool flg;
1487: PetscFunctionBegin;
1488: PetscCall(PetscObjectTypeCompare((PetscObject)A, MATMPISELL, &flg));
1489: PetscCheck(flg, PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "This function requires a MATMPISELL matrix as input");
1490: if (Ad) *Ad = a->A;
1491: if (Ao) *Ao = a->B;
1492: if (colmap) *colmap = a->garray;
1493: PetscFunctionReturn(PETSC_SUCCESS);
1494: }
1496: /*@
1497: MatMPISELLGetLocalMatCondensed - Creates a `MATSEQSELL` matrix from an `MATMPISELL` matrix by
1498: taking all its local rows and NON-ZERO columns
1500: Not Collective
1502: Input Parameters:
1503: + A - the matrix
1504: . scall - either `MAT_INITIAL_MATRIX` or `MAT_REUSE_MATRIX`
1505: . row - index sets of rows to extract (or `NULL`)
1506: - col - index sets of columns to extract (or `NULL`)
1508: Output Parameter:
1509: . A_loc - the local sequential matrix generated
1511: Level: advanced
1513: .seealso: `Mat`, `MATSEQSELL`, `MATMPISELL`, `MatGetOwnershipRange()`, `MatMPISELLGetLocalMat()`
1514: @*/
1515: PetscErrorCode MatMPISELLGetLocalMatCondensed(Mat A, MatReuse scall, IS *row, IS *col, Mat *A_loc)
1516: {
1517: Mat_MPISELL *a = (Mat_MPISELL *)A->data;
1518: PetscInt i, start, end, ncols, nzA, nzB, *cmap, imark, *idx;
1519: IS isrowa, iscola;
1520: Mat *aloc;
1521: PetscBool match;
1523: PetscFunctionBegin;
1524: PetscCall(PetscObjectTypeCompare((PetscObject)A, MATMPISELL, &match));
1525: PetscCheck(match, PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "Requires MATMPISELL matrix as input");
1526: PetscCall(PetscLogEventBegin(MAT_Getlocalmatcondensed, A, 0, 0, 0));
1527: if (!row) {
1528: start = A->rmap->rstart;
1529: end = A->rmap->rend;
1530: PetscCall(ISCreateStride(PETSC_COMM_SELF, end - start, start, 1, &isrowa));
1531: } else {
1532: isrowa = *row;
1533: }
1534: if (!col) {
1535: start = A->cmap->rstart;
1536: cmap = a->garray;
1537: nzA = a->A->cmap->n;
1538: nzB = a->B->cmap->n;
1539: PetscCall(PetscMalloc1(nzA + nzB, &idx));
1540: ncols = 0;
1541: for (i = 0; i < nzB; i++) {
1542: if (cmap[i] < start) idx[ncols++] = cmap[i];
1543: else break;
1544: }
1545: imark = i;
1546: for (i = 0; i < nzA; i++) idx[ncols++] = start + i;
1547: for (i = imark; i < nzB; i++) idx[ncols++] = cmap[i];
1548: PetscCall(ISCreateGeneral(PETSC_COMM_SELF, ncols, idx, PETSC_OWN_POINTER, &iscola));
1549: } else {
1550: iscola = *col;
1551: }
1552: if (scall != MAT_INITIAL_MATRIX) {
1553: PetscCall(PetscMalloc1(1, &aloc));
1554: aloc[0] = *A_loc;
1555: }
1556: PetscCall(MatCreateSubMatrices(A, 1, &isrowa, &iscola, scall, &aloc));
1557: *A_loc = aloc[0];
1558: PetscCall(PetscFree(aloc));
1559: if (!row) PetscCall(ISDestroy(&isrowa));
1560: if (!col) PetscCall(ISDestroy(&iscola));
1561: PetscCall(PetscLogEventEnd(MAT_Getlocalmatcondensed, A, 0, 0, 0));
1562: PetscFunctionReturn(PETSC_SUCCESS);
1563: }
1565: #include <../src/mat/impls/aij/mpi/mpiaij.h>
1567: PetscErrorCode MatConvert_MPISELL_MPIAIJ(Mat A, MatType newtype, MatReuse reuse, Mat *newmat)
1568: {
1569: Mat_MPISELL *a = (Mat_MPISELL *)A->data;
1570: Mat B;
1571: Mat_MPIAIJ *b;
1573: PetscFunctionBegin;
1574: PetscCheck(A->assembled, PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "Matrix must be assembled");
1576: if (reuse == MAT_REUSE_MATRIX) {
1577: B = *newmat;
1578: } else {
1579: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &B));
1580: PetscCall(MatSetType(B, MATMPIAIJ));
1581: PetscCall(MatSetSizes(B, A->rmap->n, A->cmap->n, A->rmap->N, A->cmap->N));
1582: PetscCall(MatSetBlockSizes(B, A->rmap->bs, A->cmap->bs));
1583: PetscCall(MatSeqAIJSetPreallocation(B, 0, NULL));
1584: PetscCall(MatMPIAIJSetPreallocation(B, 0, NULL, 0, NULL));
1585: }
1586: b = (Mat_MPIAIJ *)B->data;
1588: if (reuse == MAT_REUSE_MATRIX) {
1589: PetscCall(MatConvert_SeqSELL_SeqAIJ(a->A, MATSEQAIJ, MAT_REUSE_MATRIX, &b->A));
1590: PetscCall(MatConvert_SeqSELL_SeqAIJ(a->B, MATSEQAIJ, MAT_REUSE_MATRIX, &b->B));
1591: } else {
1592: const PetscBool nooffprocentries_A = A->nooffprocentries, nooffprocentries_B = B->nooffprocentries;
1594: PetscCall(MatDestroy(&b->A));
1595: PetscCall(MatDestroy(&b->B));
1596: PetscCall(MatDisAssemble_MPISELL(A));
1597: PetscCall(MatConvert_SeqSELL_SeqAIJ(a->A, MATSEQAIJ, MAT_INITIAL_MATRIX, &b->A));
1598: PetscCall(MatConvert_SeqSELL_SeqAIJ(a->B, MATSEQAIJ, MAT_INITIAL_MATRIX, &b->B));
1599: B->nooffprocentries = PETSC_TRUE;
1600: PetscCall(MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY));
1601: PetscCall(MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY));
1602: B->nooffprocentries = nooffprocentries_B;
1603: A->nooffprocentries = PETSC_TRUE;
1604: PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
1605: PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
1606: A->nooffprocentries = nooffprocentries_A;
1607: }
1609: if (reuse == MAT_INPLACE_MATRIX) {
1610: PetscCall(MatHeaderReplace(A, &B));
1611: } else {
1612: *newmat = B;
1613: }
1614: PetscFunctionReturn(PETSC_SUCCESS);
1615: }
1617: PetscErrorCode MatConvert_MPIAIJ_MPISELL(Mat A, MatType newtype, MatReuse reuse, Mat *newmat)
1618: {
1619: Mat_MPIAIJ *a = (Mat_MPIAIJ *)A->data;
1620: Mat B;
1621: Mat_MPISELL *b;
1623: PetscFunctionBegin;
1624: PetscCheck(A->assembled, PetscObjectComm((PetscObject)A), PETSC_ERR_SUP, "Matrix must be assembled");
1626: if (reuse == MAT_REUSE_MATRIX) {
1627: B = *newmat;
1628: } else {
1629: Mat_SeqAIJ *Aa = (Mat_SeqAIJ *)a->A->data, *Ba = (Mat_SeqAIJ *)a->B->data;
1630: PetscInt i, d_nz = 0, o_nz = 0, m = A->rmap->N, n = A->cmap->N, lm = A->rmap->n, ln = A->cmap->n;
1631: PetscInt *d_nnz, *o_nnz;
1632: PetscCall(PetscMalloc2(lm, &d_nnz, lm, &o_nnz));
1633: for (i = 0; i < lm; i++) {
1634: d_nnz[i] = Aa->i[i + 1] - Aa->i[i];
1635: o_nnz[i] = Ba->i[i + 1] - Ba->i[i];
1636: if (d_nnz[i] > d_nz) d_nz = d_nnz[i];
1637: if (o_nnz[i] > o_nz) o_nz = o_nnz[i];
1638: }
1639: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &B));
1640: PetscCall(MatSetType(B, MATMPISELL));
1641: PetscCall(MatSetSizes(B, lm, ln, m, n));
1642: PetscCall(MatSetBlockSizes(B, A->rmap->bs, A->cmap->bs));
1643: PetscCall(MatSeqSELLSetPreallocation(B, d_nz, d_nnz));
1644: PetscCall(MatMPISELLSetPreallocation(B, d_nz, d_nnz, o_nz, o_nnz));
1645: PetscCall(PetscFree2(d_nnz, o_nnz));
1646: }
1647: b = (Mat_MPISELL *)B->data;
1649: if (reuse == MAT_REUSE_MATRIX) {
1650: PetscCall(MatConvert_SeqAIJ_SeqSELL(a->A, MATSEQSELL, MAT_REUSE_MATRIX, &b->A));
1651: PetscCall(MatConvert_SeqAIJ_SeqSELL(a->B, MATSEQSELL, MAT_REUSE_MATRIX, &b->B));
1652: } else {
1653: PetscBool nooffprocentries_A = A->nooffprocentries, nooffprocentries_B = B->nooffprocentries;
1655: PetscCall(MatDestroy(&b->A));
1656: PetscCall(MatDestroy(&b->B));
1657: /* Expand a->B from compacted local off-diag columns back to global columns so the new MPISELL's
1658: MatAssemblyEnd() builds the correct garray/Mvctx for its off-diagonal block. */
1659: PetscCall(MatDisAssemble_MPIAIJ(A, PETSC_FALSE));
1660: PetscCall(MatConvert_SeqAIJ_SeqSELL(a->A, MATSEQSELL, MAT_INITIAL_MATRIX, &b->A));
1661: PetscCall(MatConvert_SeqAIJ_SeqSELL(a->B, MATSEQSELL, MAT_INITIAL_MATRIX, &b->B));
1662: /* The locally-populated A and B have no stashed off-processor entries, so skip the stash scatter. */
1663: A->nooffprocentries = PETSC_TRUE;
1664: B->nooffprocentries = PETSC_TRUE;
1665: PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
1666: PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
1667: PetscCall(MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY));
1668: PetscCall(MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY));
1669: A->nooffprocentries = nooffprocentries_A;
1670: B->nooffprocentries = nooffprocentries_B;
1671: }
1673: if (reuse == MAT_INPLACE_MATRIX) {
1674: PetscCall(MatHeaderReplace(A, &B));
1675: } else {
1676: *newmat = B;
1677: }
1678: PetscFunctionReturn(PETSC_SUCCESS);
1679: }
1681: PetscErrorCode MatSOR_MPISELL(Mat matin, Vec bb, PetscReal omega, MatSORType flag, PetscReal fshift, PetscInt its, PetscInt lits, Vec xx)
1682: {
1683: Mat_MPISELL *mat = (Mat_MPISELL *)matin->data;
1684: Vec bb1 = NULL;
1686: PetscFunctionBegin;
1687: if (flag == SOR_APPLY_UPPER) {
1688: PetscUseTypeMethod(mat->A, sor, bb, omega, flag, fshift, lits, 1, xx);
1689: PetscFunctionReturn(PETSC_SUCCESS);
1690: }
1692: if (its > 1 || ~flag & SOR_ZERO_INITIAL_GUESS || flag & SOR_EISENSTAT) PetscCall(VecDuplicate(bb, &bb1));
1694: if ((flag & SOR_LOCAL_SYMMETRIC_SWEEP) == SOR_LOCAL_SYMMETRIC_SWEEP) {
1695: if (flag & SOR_ZERO_INITIAL_GUESS) {
1696: PetscUseTypeMethod(mat->A, sor, bb, omega, flag, fshift, lits, 1, xx);
1697: its--;
1698: }
1700: while (its--) {
1701: PetscCall(VecScatterBegin(mat->Mvctx, xx, mat->lvec, INSERT_VALUES, SCATTER_FORWARD));
1702: PetscCall(VecScatterEnd(mat->Mvctx, xx, mat->lvec, INSERT_VALUES, SCATTER_FORWARD));
1704: /* update rhs: bb1 = bb - B*x */
1705: PetscCall(VecScale(mat->lvec, -1.0));
1706: PetscUseTypeMethod(mat->B, multadd, mat->lvec, bb, bb1);
1708: /* local sweep */
1709: PetscUseTypeMethod(mat->A, sor, bb1, omega, SOR_SYMMETRIC_SWEEP, fshift, lits, 1, xx);
1710: }
1711: } else if (flag & SOR_LOCAL_FORWARD_SWEEP) {
1712: if (flag & SOR_ZERO_INITIAL_GUESS) {
1713: PetscUseTypeMethod(mat->A, sor, bb, omega, flag, fshift, lits, 1, xx);
1714: its--;
1715: }
1716: while (its--) {
1717: PetscCall(VecScatterBegin(mat->Mvctx, xx, mat->lvec, INSERT_VALUES, SCATTER_FORWARD));
1718: PetscCall(VecScatterEnd(mat->Mvctx, xx, mat->lvec, INSERT_VALUES, SCATTER_FORWARD));
1720: /* update rhs: bb1 = bb - B*x */
1721: PetscCall(VecScale(mat->lvec, -1.0));
1722: PetscUseTypeMethod(mat->B, multadd, mat->lvec, bb, bb1);
1724: /* local sweep */
1725: PetscUseTypeMethod(mat->A, sor, bb1, omega, SOR_FORWARD_SWEEP, fshift, lits, 1, xx);
1726: }
1727: } else if (flag & SOR_LOCAL_BACKWARD_SWEEP) {
1728: if (flag & SOR_ZERO_INITIAL_GUESS) {
1729: PetscUseTypeMethod(mat->A, sor, bb, omega, flag, fshift, lits, 1, xx);
1730: its--;
1731: }
1732: while (its--) {
1733: PetscCall(VecScatterBegin(mat->Mvctx, xx, mat->lvec, INSERT_VALUES, SCATTER_FORWARD));
1734: PetscCall(VecScatterEnd(mat->Mvctx, xx, mat->lvec, INSERT_VALUES, SCATTER_FORWARD));
1736: /* update rhs: bb1 = bb - B*x */
1737: PetscCall(VecScale(mat->lvec, -1.0));
1738: PetscUseTypeMethod(mat->B, multadd, mat->lvec, bb, bb1);
1740: /* local sweep */
1741: PetscUseTypeMethod(mat->A, sor, bb1, omega, SOR_BACKWARD_SWEEP, fshift, lits, 1, xx);
1742: }
1743: } else SETERRQ(PetscObjectComm((PetscObject)matin), PETSC_ERR_SUP, "Parallel SOR not supported");
1745: PetscCall(VecDestroy(&bb1));
1747: matin->factorerrortype = mat->A->factorerrortype;
1748: PetscFunctionReturn(PETSC_SUCCESS);
1749: }
1751: #if PetscDefined(HAVE_CUDA)
1752: PETSC_INTERN PetscErrorCode MatConvert_MPISELL_MPISELLCUDA(Mat, MatType, MatReuse, Mat *);
1753: #endif
1755: /*MC
1756: MATMPISELL - MATMPISELL = "MPISELL" - A matrix type to be used for parallel sparse matrices.
1758: Options Database Keys:
1759: . -mat_type mpisell - sets the matrix type to `MATMPISELL` during a call to `MatSetFromOptions()`
1761: Level: beginner
1763: .seealso: `Mat`, `MATSELL`, `MATSEQSELL`, `MatCreateSELL()`
1764: M*/
1765: PETSC_EXTERN PetscErrorCode MatCreate_MPISELL(Mat B)
1766: {
1767: Mat_MPISELL *b;
1768: PetscMPIInt size;
1770: PetscFunctionBegin;
1771: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)B), &size));
1772: PetscCall(PetscNew(&b));
1773: B->data = (void *)b;
1774: B->ops[0] = MatOps_Values;
1775: B->assembled = PETSC_FALSE;
1776: B->insertmode = NOT_SET_VALUES;
1777: b->size = size;
1778: PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)B), &b->rank));
1779: /* build cache for off array entries formed */
1780: PetscCall(MatStashCreate_Private(PetscObjectComm((PetscObject)B), 1, &B->stash));
1782: b->donotstash = PETSC_FALSE;
1783: b->colmap = NULL;
1784: b->garray = NULL;
1785: b->roworiented = PETSC_TRUE;
1787: /* stuff used for matrix vector multiply */
1788: b->lvec = NULL;
1789: b->Mvctx = NULL;
1791: /* stuff for MatGetRow() */
1792: b->rowindices = NULL;
1793: b->rowvalues = NULL;
1794: b->getrowactive = PETSC_FALSE;
1796: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatStoreValues_C", MatStoreValues_MPISELL));
1797: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatRetrieveValues_C", MatRetrieveValues_MPISELL));
1798: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatIsTranspose_C", MatIsTranspose_MPISELL));
1799: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatMPISELLSetPreallocation_C", MatMPISELLSetPreallocation_MPISELL));
1800: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_mpisell_mpiaij_C", MatConvert_MPISELL_MPIAIJ));
1801: #if PetscDefined(HAVE_CUDA)
1802: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_mpisell_mpisellcuda_C", MatConvert_MPISELL_MPISELLCUDA));
1803: #endif
1804: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatDiagonalScaleLocal_C", MatDiagonalScaleLocal_MPISELL));
1805: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatGetMultPetscSF_C", MatGetMultPetscSF_MPISELL));
1806: PetscCall(PetscObjectChangeTypeName((PetscObject)B, MATMPISELL));
1807: PetscFunctionReturn(PETSC_SUCCESS);
1808: }