Actual source code: aij.c
1: /*
2: Defines the basic matrix operations for the AIJ (compressed row)
3: matrix storage format.
4: */
6: #include <../src/mat/impls/aij/seq/aij.h>
7: #include <petscblaslapack.h>
8: #include <petsc/private/kernels/blocktranspose.h>
10: /* defines MatSetValues_Seq_Hash(), MatAssemblyEnd_Seq_Hash(), MatSetUp_Seq_Hash() */
11: #define TYPE AIJ
12: #define TYPE_BS
13: #include "../src/mat/impls/aij/seq/seqhashmatsetvalues.h"
14: #include "../src/mat/impls/aij/seq/seqhashmat.h"
15: #undef TYPE
16: #undef TYPE_BS
18: MatGetDiagonalMarkers(SeqAIJ, 1)
20: static PetscErrorCode MatSeqAIJSetTypeFromOptions(Mat A)
21: {
22: PetscBool flg;
23: char type[256];
25: PetscFunctionBegin;
26: PetscObjectOptionsBegin((PetscObject)A);
27: PetscCall(PetscOptionsFList("-mat_seqaij_type", "Matrix SeqAIJ type", "MatSeqAIJSetType", MatSeqAIJList, "seqaij", type, sizeof(type), &flg));
28: if (flg) PetscCall(MatSeqAIJSetType(A, type));
29: PetscOptionsEnd();
30: PetscFunctionReturn(PETSC_SUCCESS);
31: }
33: static PetscErrorCode MatGetColumnReductions_SeqAIJ(Mat A, PetscInt type, PetscReal *reductions)
34: {
35: PetscInt i, m, n;
36: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;
38: PetscFunctionBegin;
39: PetscCall(MatGetSize(A, &m, &n));
40: PetscCall(PetscArrayzero(reductions, n));
41: if (type == NORM_2) {
42: for (i = 0; i < aij->i[m]; i++) reductions[aij->j[i]] += PetscAbsScalar(aij->a[i] * aij->a[i]);
43: } else if (type == NORM_1) {
44: for (i = 0; i < aij->i[m]; i++) reductions[aij->j[i]] += PetscAbsScalar(aij->a[i]);
45: } else if (type == NORM_INFINITY) {
46: for (i = 0; i < aij->i[m]; i++) reductions[aij->j[i]] = PetscMax(PetscAbsScalar(aij->a[i]), reductions[aij->j[i]]);
47: } else if (type == REDUCTION_SUM_REALPART || type == REDUCTION_MEAN_REALPART) {
48: for (i = 0; i < aij->i[m]; i++) reductions[aij->j[i]] += PetscRealPart(aij->a[i]);
49: } else if (type == REDUCTION_SUM_IMAGINARYPART || type == REDUCTION_MEAN_IMAGINARYPART) {
50: for (i = 0; i < aij->i[m]; i++) reductions[aij->j[i]] += PetscImaginaryPart(aij->a[i]);
51: } else SETERRQ(PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Unknown reduction type");
53: if (type == NORM_2) {
54: for (i = 0; i < n; i++) reductions[i] = PetscSqrtReal(reductions[i]);
55: } else if (type == REDUCTION_MEAN_REALPART || type == REDUCTION_MEAN_IMAGINARYPART) {
56: for (i = 0; i < n; i++) reductions[i] /= m;
57: }
58: PetscFunctionReturn(PETSC_SUCCESS);
59: }
61: static PetscErrorCode MatFindOffBlockDiagonalEntries_SeqAIJ(Mat A, IS *is)
62: {
63: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
64: PetscInt i, m = A->rmap->n, cnt = 0, bs = A->rmap->bs;
65: const PetscInt *jj = a->j, *ii = a->i;
66: PetscInt *rows;
68: PetscFunctionBegin;
69: for (i = 0; i < m; i++) {
70: if ((ii[i] != ii[i + 1]) && ((jj[ii[i]] < bs * (i / bs)) || (jj[ii[i + 1] - 1] > bs * ((i + bs) / bs) - 1))) cnt++;
71: }
72: PetscCall(PetscMalloc1(cnt, &rows));
73: cnt = 0;
74: for (i = 0; i < m; i++) {
75: if ((ii[i] != ii[i + 1]) && ((jj[ii[i]] < bs * (i / bs)) || (jj[ii[i + 1] - 1] > bs * ((i + bs) / bs) - 1))) {
76: rows[cnt] = i;
77: cnt++;
78: }
79: }
80: PetscCall(ISCreateGeneral(PETSC_COMM_SELF, cnt, rows, PETSC_OWN_POINTER, is));
81: PetscFunctionReturn(PETSC_SUCCESS);
82: }
84: PetscErrorCode MatFindZeroDiagonals_SeqAIJ_Private(Mat A, PetscInt *nrows, PetscInt **zrows)
85: {
86: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
87: const MatScalar *aa;
88: PetscInt i, m = A->rmap->n, cnt = 0;
89: const PetscInt *ii = a->i, *jj = a->j, *diag;
90: PetscInt *rows;
92: PetscFunctionBegin;
93: PetscCall(MatSeqAIJGetArrayRead(A, &aa));
94: PetscCall(MatGetDiagonalMarkers_SeqAIJ(A, &diag, NULL));
95: for (i = 0; i < m; i++) {
96: if ((diag[i] >= ii[i + 1]) || (jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) cnt++;
97: }
98: PetscCall(PetscMalloc1(cnt, &rows));
99: cnt = 0;
100: for (i = 0; i < m; i++) {
101: if ((diag[i] >= ii[i + 1]) || (jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) rows[cnt++] = i;
102: }
103: *nrows = cnt;
104: *zrows = rows;
105: PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
106: PetscFunctionReturn(PETSC_SUCCESS);
107: }
109: static PetscErrorCode MatFindZeroDiagonals_SeqAIJ(Mat A, IS *zrows)
110: {
111: PetscInt nrows, *rows;
113: PetscFunctionBegin;
114: *zrows = NULL;
115: PetscCall(MatFindZeroDiagonals_SeqAIJ_Private(A, &nrows, &rows));
116: PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)A), nrows, rows, PETSC_OWN_POINTER, zrows));
117: PetscFunctionReturn(PETSC_SUCCESS);
118: }
120: static PetscErrorCode MatFindNonzeroRows_SeqAIJ(Mat A, IS *keptrows)
121: {
122: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
123: const MatScalar *aa;
124: PetscInt m = A->rmap->n, cnt = 0;
125: const PetscInt *ii;
126: PetscInt n, i, j, *rows;
128: PetscFunctionBegin;
129: PetscCall(MatSeqAIJGetArrayRead(A, &aa));
130: *keptrows = NULL;
131: ii = a->i;
132: for (i = 0; i < m; i++) {
133: n = ii[i + 1] - ii[i];
134: if (!n) {
135: cnt++;
136: goto ok1;
137: }
138: for (j = ii[i]; j < ii[i + 1]; j++) {
139: if (aa[j] != 0.0) goto ok1;
140: }
141: cnt++;
142: ok1:;
143: }
144: if (!cnt) {
145: PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
146: PetscFunctionReturn(PETSC_SUCCESS);
147: }
148: PetscCall(PetscMalloc1(A->rmap->n - cnt, &rows));
149: cnt = 0;
150: for (i = 0; i < m; i++) {
151: n = ii[i + 1] - ii[i];
152: if (!n) continue;
153: for (j = ii[i]; j < ii[i + 1]; j++) {
154: if (aa[j] != 0.0) {
155: rows[cnt++] = i;
156: break;
157: }
158: }
159: }
160: PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
161: PetscCall(ISCreateGeneral(PETSC_COMM_SELF, cnt, rows, PETSC_OWN_POINTER, keptrows));
162: PetscFunctionReturn(PETSC_SUCCESS);
163: }
165: PetscErrorCode MatDiagonalSet_SeqAIJ(Mat Y, Vec D, InsertMode is)
166: {
167: PetscInt i, m = Y->rmap->n;
168: const PetscInt *diag;
169: MatScalar *aa;
170: const PetscScalar *v;
171: PetscBool diagDense;
173: PetscFunctionBegin;
174: if (Y->assembled) {
175: PetscCall(MatGetDiagonalMarkers_SeqAIJ(Y, &diag, &diagDense));
176: if (diagDense) {
177: PetscCall(VecGetArrayRead(D, &v));
178: PetscCall(MatSeqAIJGetArray(Y, &aa));
179: if (is == INSERT_VALUES) {
180: for (i = 0; i < m; i++) aa[diag[i]] = v[i];
181: } else {
182: for (i = 0; i < m; i++) aa[diag[i]] += v[i];
183: }
184: PetscCall(MatSeqAIJRestoreArray(Y, &aa));
185: PetscCall(VecRestoreArrayRead(D, &v));
186: PetscFunctionReturn(PETSC_SUCCESS);
187: }
188: }
189: PetscCall(MatDiagonalSet_Default(Y, D, is));
190: PetscFunctionReturn(PETSC_SUCCESS);
191: }
193: PetscErrorCode MatGetRowIJ_SeqAIJ(Mat A, PetscInt oshift, PetscBool symmetric, PetscBool inodecompressed, PetscInt *m, const PetscInt *ia[], const PetscInt *ja[], PetscBool *done)
194: {
195: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
196: PetscInt i, ishift;
198: PetscFunctionBegin;
199: if (m) *m = A->rmap->n;
200: if (!ia) PetscFunctionReturn(PETSC_SUCCESS);
201: ishift = 0;
202: if (symmetric && A->structurally_symmetric != PETSC_BOOL3_TRUE) {
203: PetscCall(MatToSymmetricIJ_SeqAIJ(A->rmap->n, a->i, a->j, PETSC_TRUE, ishift, oshift, (PetscInt **)ia, (PetscInt **)ja));
204: } else if (oshift == 1) {
205: PetscInt *tia;
206: PetscInt nz = a->i[A->rmap->n];
208: /* malloc space and add 1 to i and j indices */
209: PetscCall(PetscMalloc1(A->rmap->n + 1, &tia));
210: for (i = 0; i < A->rmap->n + 1; i++) tia[i] = a->i[i] + 1;
211: *ia = tia;
212: if (ja) {
213: PetscInt *tja;
215: PetscCall(PetscMalloc1(nz + 1, &tja));
216: for (i = 0; i < nz; i++) tja[i] = a->j[i] + 1;
217: *ja = tja;
218: }
219: } else {
220: *ia = a->i;
221: if (ja) *ja = a->j;
222: }
223: PetscFunctionReturn(PETSC_SUCCESS);
224: }
226: PetscErrorCode MatRestoreRowIJ_SeqAIJ(Mat A, PetscInt oshift, PetscBool symmetric, PetscBool inodecompressed, PetscInt *n, const PetscInt *ia[], const PetscInt *ja[], PetscBool *done)
227: {
228: PetscFunctionBegin;
229: if (!ia) PetscFunctionReturn(PETSC_SUCCESS);
230: if ((symmetric && A->structurally_symmetric != PETSC_BOOL3_TRUE) || oshift == 1) {
231: PetscCall(PetscFree(*ia));
232: if (ja) PetscCall(PetscFree(*ja));
233: }
234: PetscFunctionReturn(PETSC_SUCCESS);
235: }
237: PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat A, PetscInt oshift, PetscBool symmetric, PetscBool inodecompressed, PetscInt *nn, const PetscInt *ia[], const PetscInt *ja[], PetscBool *done)
238: {
239: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
240: PetscInt i, *collengths, *cia, *cja, n = A->cmap->n, m = A->rmap->n;
241: PetscInt nz = a->i[m], row, *jj, mr, col;
243: PetscFunctionBegin;
244: *nn = n;
245: if (!ia) PetscFunctionReturn(PETSC_SUCCESS);
246: if (symmetric) {
247: PetscCall(MatToSymmetricIJ_SeqAIJ(A->rmap->n, a->i, a->j, PETSC_TRUE, 0, oshift, (PetscInt **)ia, (PetscInt **)ja));
248: } else {
249: PetscCall(PetscCalloc1(n, &collengths));
250: PetscCall(PetscMalloc1(n + 1, &cia));
251: PetscCall(PetscMalloc1(nz, &cja));
252: jj = a->j;
253: for (i = 0; i < nz; i++) collengths[jj[i]]++;
254: cia[0] = oshift;
255: for (i = 0; i < n; i++) cia[i + 1] = cia[i] + collengths[i];
256: PetscCall(PetscArrayzero(collengths, n));
257: jj = a->j;
258: for (row = 0; row < m; row++) {
259: mr = a->i[row + 1] - a->i[row];
260: for (i = 0; i < mr; i++) {
261: col = *jj++;
263: cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
264: }
265: }
266: PetscCall(PetscFree(collengths));
267: *ia = cia;
268: *ja = cja;
269: }
270: PetscFunctionReturn(PETSC_SUCCESS);
271: }
273: PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat A, PetscInt oshift, PetscBool symmetric, PetscBool inodecompressed, PetscInt *n, const PetscInt *ia[], const PetscInt *ja[], PetscBool *done)
274: {
275: PetscFunctionBegin;
276: if (!ia) PetscFunctionReturn(PETSC_SUCCESS);
278: PetscCall(PetscFree(*ia));
279: PetscCall(PetscFree(*ja));
280: PetscFunctionReturn(PETSC_SUCCESS);
281: }
283: /*
284: MatGetColumnIJ_SeqAIJ_Color() and MatRestoreColumnIJ_SeqAIJ_Color() are customized from
285: MatGetColumnIJ_SeqAIJ() and MatRestoreColumnIJ_SeqAIJ() by adding an output
286: spidx[], index of a->a, to be used in MatTransposeColoringCreate_SeqAIJ() and MatFDColoringCreate_SeqXAIJ()
287: */
288: PetscErrorCode MatGetColumnIJ_SeqAIJ_Color(Mat A, PetscInt oshift, PetscBool symmetric, PetscBool inodecompressed, PetscInt *nn, const PetscInt *ia[], const PetscInt *ja[], PetscInt *spidx[], PetscBool *done)
289: {
290: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
291: PetscInt i, *collengths, *cia, *cja, n = A->cmap->n, m = A->rmap->n;
292: PetscInt nz = a->i[m], row, mr, col, tmp;
293: PetscInt *cspidx;
294: const PetscInt *jj;
296: PetscFunctionBegin;
297: *nn = n;
298: if (!ia) PetscFunctionReturn(PETSC_SUCCESS);
300: PetscCall(PetscCalloc1(n, &collengths));
301: PetscCall(PetscMalloc1(n + 1, &cia));
302: PetscCall(PetscMalloc1(nz, &cja));
303: PetscCall(PetscMalloc1(nz, &cspidx));
304: jj = a->j;
305: for (i = 0; i < nz; i++) collengths[jj[i]]++;
306: cia[0] = oshift;
307: for (i = 0; i < n; i++) cia[i + 1] = cia[i] + collengths[i];
308: PetscCall(PetscArrayzero(collengths, n));
309: jj = a->j;
310: for (row = 0; row < m; row++) {
311: mr = a->i[row + 1] - a->i[row];
312: for (i = 0; i < mr; i++) {
313: col = *jj++;
314: tmp = cia[col] + collengths[col]++ - oshift;
315: cspidx[tmp] = a->i[row] + i; /* index of a->j */
316: cja[tmp] = row + oshift;
317: }
318: }
319: PetscCall(PetscFree(collengths));
320: *ia = cia;
321: *ja = cja;
322: *spidx = cspidx;
323: PetscFunctionReturn(PETSC_SUCCESS);
324: }
326: PetscErrorCode MatRestoreColumnIJ_SeqAIJ_Color(Mat A, PetscInt oshift, PetscBool symmetric, PetscBool inodecompressed, PetscInt *n, const PetscInt *ia[], const PetscInt *ja[], PetscInt *spidx[], PetscBool *done)
327: {
328: PetscFunctionBegin;
329: PetscCall(MatRestoreColumnIJ_SeqAIJ(A, oshift, symmetric, inodecompressed, n, ia, ja, done));
330: PetscCall(PetscFree(*spidx));
331: PetscFunctionReturn(PETSC_SUCCESS);
332: }
334: static PetscErrorCode MatSetValuesRow_SeqAIJ(Mat A, PetscInt row, const PetscScalar v[])
335: {
336: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
337: PetscInt *ai = a->i;
338: PetscScalar *aa;
340: PetscFunctionBegin;
341: PetscCall(MatSeqAIJGetArray(A, &aa));
342: PetscCall(PetscArraycpy(aa + ai[row], v, ai[row + 1] - ai[row]));
343: PetscCall(MatSeqAIJRestoreArray(A, &aa));
344: PetscFunctionReturn(PETSC_SUCCESS);
345: }
347: #include <petsc/private/isimpl.h>
349: /*@
350: MatSeqAIJSetValuesLocalFast - An optimized version of `MatSetValuesLocal()` for `MATSEQAIJ` matrices, valid under
351: several restrictive assumptions.
353: Not Collective
355: Input Parameters:
356: + A - the `MATSEQAIJ` matrix
357: . m - the number of rows being set (must be 1)
358: . im - array of length `m` giving the local row index
359: . n - the number of columns being set
360: . in - array of length `n` giving the local column indices
361: . v - array of length `n` of values to add
362: - is - the insert mode (must be `ADD_VALUES`)
364: Level: developer
366: Notes:
367: This routine requires that a single row of values is set with each call, that no row or column
368: index is negative or larger than the number of rows or columns, that values are always added
369: (not inserted), and that no new nonzero locations are introduced.
371: The global column indices are not assumed to be sorted.
373: .seealso: `Mat`, `MATSEQAIJ`, `MatSetValuesLocal()`, `MatSetValues()`
374: @*/
375: PetscErrorCode MatSeqAIJSetValuesLocalFast(Mat A, PetscInt m, const PetscInt im[], PetscInt n, const PetscInt in[], const PetscScalar v[], InsertMode is)
376: {
377: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
378: PetscInt low, high, t, row, nrow, i, col, l;
379: const PetscInt *rp, *ai = a->i, *ailen = a->ilen, *aj = a->j;
380: PetscInt lastcol = -1;
381: MatScalar *ap, value, *aa;
382: const PetscInt *ridx = A->rmap->mapping->indices, *cidx = A->cmap->mapping->indices;
384: PetscFunctionBegin;
385: PetscCall(MatSeqAIJGetArray(A, &aa));
386: row = ridx[im[0]];
387: rp = aj + ai[row];
388: ap = aa + ai[row];
389: nrow = ailen[row];
390: low = 0;
391: high = nrow;
392: for (l = 0; l < n; l++) { /* loop over added columns */
393: col = cidx[in[l]];
394: value = v[l];
396: if (col <= lastcol) low = 0;
397: else high = nrow;
398: lastcol = col;
399: while (high - low > 5) {
400: t = (low + high) / 2;
401: if (rp[t] > col) high = t;
402: else low = t;
403: }
404: for (i = low; i < high; i++) {
405: if (rp[i] == col) {
406: ap[i] += value;
407: low = i + 1;
408: break;
409: }
410: }
411: }
412: PetscCall(MatSeqAIJRestoreArray(A, &aa));
413: return PETSC_SUCCESS;
414: }
416: PetscErrorCode MatSetValues_SeqAIJ(Mat A, PetscInt m, const PetscInt im[], PetscInt n, const PetscInt in[], const PetscScalar v[], InsertMode is)
417: {
418: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
419: PetscInt *rp, k, low, high, t, ii, row, nrow, i, col, l, rmax, N;
420: PetscInt *imax = a->imax, *ai = a->i, *ailen = a->ilen;
421: PetscInt *aj = a->j, nonew = a->nonew, lastcol = -1;
422: MatScalar *ap = NULL, value = 0.0, *aa;
423: PetscBool ignorezeroentries = a->ignorezeroentries;
424: PetscBool roworiented = a->roworiented;
426: PetscFunctionBegin;
427: PetscCall(MatSeqAIJGetArray(A, &aa));
428: for (k = 0; k < m; k++) { /* loop over added rows */
429: row = im[k];
430: if (row < 0) continue;
431: PetscCheck(row < A->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Row too large: row %" PetscInt_FMT " max %" PetscInt_FMT, row, A->rmap->n - 1);
432: rp = PetscSafePointerPlusOffset(aj, ai[row]);
433: if (!A->structure_only) ap = PetscSafePointerPlusOffset(aa, ai[row]);
434: rmax = imax[row];
435: nrow = ailen[row];
436: low = 0;
437: high = nrow;
438: for (l = 0; l < n; l++) { /* loop over added columns */
439: if (in[l] < 0) continue;
440: PetscCheck(in[l] < A->cmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Column too large: col %" PetscInt_FMT " max %" PetscInt_FMT, in[l], A->cmap->n - 1);
441: col = in[l];
442: if (v && !A->structure_only) value = roworiented ? v[l + k * n] : v[k + l * m];
443: if (!A->structure_only && value == 0.0 && ignorezeroentries && is == ADD_VALUES && row != col) continue;
445: if (col <= lastcol) low = 0;
446: else high = nrow;
447: lastcol = col;
448: while (high - low > 5) {
449: t = (low + high) / 2;
450: if (rp[t] > col) high = t;
451: else low = t;
452: }
453: for (i = low; i < high; i++) {
454: if (rp[i] > col) break;
455: if (rp[i] == col) {
456: if (!A->structure_only) {
457: if (is == ADD_VALUES) {
458: ap[i] += value;
459: (void)PetscLogFlops(1.0);
460: } else ap[i] = value;
461: }
462: low = i + 1;
463: goto noinsert;
464: }
465: }
466: if ((!A->structure_only && value == 0.0 && ignorezeroentries && row != col) || nonew == 1) goto noinsert;
467: PetscCheck(nonew != -1, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Inserting a new nonzero at (%" PetscInt_FMT ",%" PetscInt_FMT ") in the matrix", row, col);
468: if (A->structure_only) {
469: MatSeqXAIJReallocateAIJ_structure_only(A, A->rmap->n, 1, nrow, row, col, rmax, ai, aj, rp, imax, nonew, MatScalar);
470: } else {
471: MatSeqXAIJReallocateAIJ(A, A->rmap->n, 1, nrow, row, col, rmax, aa, ai, aj, rp, ap, imax, nonew, MatScalar);
472: }
473: N = nrow++ - 1;
474: a->nz++;
475: high++;
476: /* shift up all the later entries in this row */
477: PetscCall(PetscArraymove(rp + i + 1, rp + i, N - i + 1));
478: rp[i] = col;
479: if (!A->structure_only) {
480: PetscCall(PetscArraymove(ap + i + 1, ap + i, N - i + 1));
481: ap[i] = value;
482: }
483: low = i + 1;
484: noinsert:;
485: }
486: ailen[row] = nrow;
487: }
488: PetscCall(MatSeqAIJRestoreArray(A, &aa));
489: PetscFunctionReturn(PETSC_SUCCESS);
490: }
492: static PetscErrorCode MatSetValues_SeqAIJ_SortedFullNoPreallocation(Mat A, PetscInt m, const PetscInt im[], PetscInt n, const PetscInt in[], const PetscScalar v[], InsertMode is)
493: {
494: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
495: PetscInt *rp, k, row;
496: PetscInt *ai = a->i;
497: PetscInt *aj = a->j;
498: MatScalar *aa, *ap;
500: PetscFunctionBegin;
501: PetscCheck(!A->was_assembled, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Cannot call on assembled matrix.");
502: PetscCheck(m * n + a->nz <= a->maxnz, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of entries in matrix will be larger than maximum nonzeros allocated for %" PetscInt_FMT " in MatSeqAIJSetTotalPreallocation()", a->maxnz);
504: PetscCall(MatSeqAIJGetArray(A, &aa));
505: for (k = 0; k < m; k++) { /* loop over added rows */
506: row = im[k];
507: rp = aj + ai[row];
508: ap = PetscSafePointerPlusOffset(aa, ai[row]);
510: PetscCall(PetscArraycpy(rp, in, n));
511: if (!A->structure_only) {
512: if (v) {
513: PetscCall(PetscArraycpy(ap, v, n));
514: v += n;
515: } else {
516: PetscCall(PetscMemzero(ap, n * sizeof(PetscScalar)));
517: }
518: }
519: a->ilen[row] = n;
520: a->imax[row] = n;
521: a->i[row + 1] = a->i[row] + n;
522: a->nz += n;
523: }
524: PetscCall(MatSeqAIJRestoreArray(A, &aa));
525: PetscFunctionReturn(PETSC_SUCCESS);
526: }
528: /*@
529: MatSeqAIJSetTotalPreallocation - Sets an upper bound on the total number of expected nonzeros in the matrix.
531: Input Parameters:
532: + A - the `MATSEQAIJ` matrix
533: - nztotal - bound on the number of nonzeros
535: Level: advanced
537: Notes:
538: This can be called if you will be provided the matrix row by row (from row zero) with sorted column indices for each row.
539: Simply call `MatSetValues()` after this call to provide the matrix entries in the usual manner. This matrix may be used
540: as always with multiple matrix assemblies.
542: .seealso: [](ch_matrices), `Mat`, `MatSetOption()`, `MAT_SORTED_FULL`, `MatSetValues()`, `MatSeqAIJSetPreallocation()`
543: @*/
544: PetscErrorCode MatSeqAIJSetTotalPreallocation(Mat A, PetscInt nztotal)
545: {
546: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
548: PetscFunctionBegin;
549: PetscCall(PetscLayoutSetUp(A->rmap));
550: PetscCall(PetscLayoutSetUp(A->cmap));
551: a->maxnz = nztotal;
552: if (!a->imax) PetscCall(PetscMalloc1(A->rmap->n, &a->imax));
553: if (!a->ilen) {
554: PetscCall(PetscMalloc1(A->rmap->n, &a->ilen));
555: } else {
556: PetscCall(PetscMemzero(a->ilen, A->rmap->n * sizeof(PetscInt)));
557: }
559: /* allocate the matrix space */
560: PetscCall(PetscShmgetAllocateArray(A->rmap->n + 1, sizeof(PetscInt), (void **)&a->i));
561: PetscCall(PetscShmgetAllocateArray(nztotal, sizeof(PetscInt), (void **)&a->j));
562: a->free_ij = PETSC_TRUE;
563: if (A->structure_only) {
564: a->free_a = PETSC_FALSE;
565: } else {
566: PetscCall(PetscShmgetAllocateArray(nztotal, sizeof(PetscScalar), (void **)&a->a));
567: a->free_a = PETSC_TRUE;
568: }
569: a->i[0] = 0;
570: A->ops->setvalues = MatSetValues_SeqAIJ_SortedFullNoPreallocation;
571: A->preallocated = PETSC_TRUE;
572: PetscFunctionReturn(PETSC_SUCCESS);
573: }
575: static PetscErrorCode MatSetValues_SeqAIJ_SortedFull(Mat A, PetscInt m, const PetscInt im[], PetscInt n, const PetscInt in[], const PetscScalar v[], InsertMode is)
576: {
577: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
578: PetscInt *rp, k, row;
579: PetscInt *ai = a->i, *ailen = a->ilen;
580: PetscInt *aj = a->j;
581: MatScalar *aa, *ap;
583: PetscFunctionBegin;
584: PetscCall(MatSeqAIJGetArray(A, &aa));
585: for (k = 0; k < m; k++) { /* loop over added rows */
586: row = im[k];
587: PetscCheck(n <= a->imax[row], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Preallocation for row %" PetscInt_FMT " does not match number of columns provided", n);
588: rp = aj + ai[row];
589: ap = aa + ai[row];
590: if (!A->was_assembled) PetscCall(PetscArraycpy(rp, in, n));
591: if (!A->structure_only) {
592: if (v) {
593: PetscCall(PetscArraycpy(ap, v, n));
594: v += n;
595: } else {
596: PetscCall(PetscMemzero(ap, n * sizeof(PetscScalar)));
597: }
598: }
599: ailen[row] = n;
600: a->nz += n;
601: }
602: PetscCall(MatSeqAIJRestoreArray(A, &aa));
603: PetscFunctionReturn(PETSC_SUCCESS);
604: }
606: static PetscErrorCode MatGetValues_SeqAIJ(Mat A, PetscInt m, const PetscInt im[], PetscInt n, const PetscInt in[], PetscScalar v[])
607: {
608: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
609: PetscInt *rp, k, low, high, t, row, nrow, i, col, l, *aj = a->j;
610: PetscInt *ai = a->i, *ailen = a->ilen;
611: const MatScalar *ap, *aa;
612: PetscBool hyprecoo;
613: PetscBool roworiented = a->roworiented;
614: PetscScalar *value;
616: PetscFunctionBegin;
617: PetscCall(PetscStrcmp("_internal_COO_mat_for_hypre", ((PetscObject)A)->name, &hyprecoo));
619: PetscCall(MatSeqAIJGetArrayRead(A, &aa));
620: for (k = 0; k < m; k++) { /* loop over rows */
621: row = im[k];
622: if (row < 0) continue; /* negative row */
623: PetscCheck(row < A->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Row too large: row %" PetscInt_FMT " max %" PetscInt_FMT, row, A->rmap->n - 1);
624: rp = PetscSafePointerPlusOffset(aj, ai[row]);
625: ap = PetscSafePointerPlusOffset(aa, ai[row]);
626: nrow = ailen[row];
627: for (l = 0; l < n; l++) { /* loop over columns */
628: if (in[l] < 0) continue; /* negative column */
629: value = roworiented ? &v[l + k * n] : &v[k + l * m];
630: PetscCheck(in[l] < A->cmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Column too large: col %" PetscInt_FMT " max %" PetscInt_FMT, in[l], A->cmap->n - 1);
631: col = in[l];
632: /* hypre coo mat stores its diagonal at the front, out of sort */
633: if (hyprecoo) {
634: if (col == rp[0]) {
635: *value = ap[0];
636: goto finished;
637: }
638: low = 1;
639: } else low = 0;
640: high = nrow;
641: /* assume sorted */
642: while (high - low > 5) {
643: t = (low + high) / 2;
644: if (rp[t] > col) high = t;
645: else low = t;
646: }
647: for (i = low; i < high; i++) {
648: if (rp[i] > col) break;
649: if (rp[i] == col) {
650: *value = ap[i];
651: goto finished;
652: }
653: }
654: *value = 0.0;
655: finished:;
656: }
657: }
658: PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
659: PetscFunctionReturn(PETSC_SUCCESS);
660: }
662: static PetscErrorCode MatView_SeqAIJ_Binary(Mat mat, PetscViewer viewer)
663: {
664: Mat_SeqAIJ *A = (Mat_SeqAIJ *)mat->data;
665: const PetscScalar *av;
666: PetscInt header[4], M, N, m, nz, i;
667: PetscInt *rowlens;
669: PetscFunctionBegin;
670: PetscCall(PetscViewerSetUp(viewer));
672: M = mat->rmap->N;
673: N = mat->cmap->N;
674: m = mat->rmap->n;
675: nz = A->nz;
677: /* write matrix header */
678: header[0] = MAT_FILE_CLASSID;
679: header[1] = M;
680: header[2] = N;
681: header[3] = nz;
682: PetscCall(PetscViewerBinaryWrite(viewer, header, 4, PETSC_INT));
684: /* fill in and store row lengths */
685: PetscCall(PetscMalloc1(m, &rowlens));
686: for (i = 0; i < m; i++) rowlens[i] = A->i[i + 1] - A->i[i];
687: if (PetscDefined(USE_DEBUG)) {
688: PetscInt mnz = 0;
690: for (i = 0; i < m; i++) mnz += rowlens[i];
691: PetscCheck(nz == mnz, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Row lens %" PetscInt_FMT " do not sum to nz %" PetscInt_FMT, mnz, nz);
692: }
693: PetscCall(PetscViewerBinaryWrite(viewer, rowlens, m, PETSC_INT));
694: PetscCall(PetscFree(rowlens));
695: /* store column indices */
696: PetscCall(PetscViewerBinaryWrite(viewer, A->j, nz, PETSC_INT));
697: /* store nonzero values */
698: PetscCall(MatSeqAIJGetArrayRead(mat, &av));
699: PetscCall(PetscViewerBinaryWrite(viewer, av, nz, PETSC_SCALAR));
700: PetscCall(MatSeqAIJRestoreArrayRead(mat, &av));
702: /* write block size option to the viewer's .info file */
703: PetscCall(MatView_Binary_BlockSizes(mat, viewer));
704: PetscFunctionReturn(PETSC_SUCCESS);
705: }
707: static PetscErrorCode MatView_SeqAIJ_ASCII_structonly(Mat A, PetscViewer viewer)
708: {
709: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
710: PetscInt i, k, m = A->rmap->N;
712: PetscFunctionBegin;
713: PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_FALSE));
714: for (i = 0; i < m; i++) {
715: PetscCall(PetscViewerASCIIPrintf(viewer, "row %" PetscInt_FMT ":", i));
716: for (k = a->i[i]; k < a->i[i + 1]; k++) PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ") ", a->j[k]));
717: PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
718: }
719: PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_TRUE));
720: PetscFunctionReturn(PETSC_SUCCESS);
721: }
723: static PetscErrorCode MatView_SeqAIJ_ASCII(Mat A, PetscViewer viewer)
724: {
725: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
726: const PetscScalar *av;
727: PetscInt i, j, m = A->rmap->n;
728: const char *name;
729: PetscViewerFormat format;
731: PetscFunctionBegin;
732: if (A->structure_only) {
733: PetscCall(MatView_SeqAIJ_ASCII_structonly(A, viewer));
734: PetscFunctionReturn(PETSC_SUCCESS);
735: }
737: PetscCall(PetscViewerGetFormat(viewer, &format));
738: // By petsc's rule, even PETSC_VIEWER_ASCII_INFO_DETAIL doesn't print matrix entries
739: if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) PetscFunctionReturn(PETSC_SUCCESS);
741: /* trigger copy to CPU if needed */
742: PetscCall(MatSeqAIJGetArrayRead(A, &av));
743: PetscCall(MatSeqAIJRestoreArrayRead(A, &av));
744: if (format == PETSC_VIEWER_ASCII_MATLAB) {
745: PetscInt nofinalvalue = 0;
746: if (m && ((a->i[m] == a->i[m - 1]) || (a->j[a->nz - 1] != A->cmap->n - 1))) {
747: /* Need a dummy value to ensure the dimension of the matrix. */
748: nofinalvalue = 1;
749: }
750: PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_FALSE));
751: PetscCall(PetscViewerASCIIPrintf(viewer, "%% Size = %" PetscInt_FMT " %" PetscInt_FMT " \n", m, A->cmap->n));
752: PetscCall(PetscViewerASCIIPrintf(viewer, "%% Nonzeros = %" PetscInt_FMT " \n", a->nz));
753: PetscCall(PetscViewerASCIIPrintf(viewer, "zzz = zeros(%" PetscInt_FMT ",%d);\n", a->nz + nofinalvalue, PetscDefined(USE_COMPLEX) ? 4 : 3));
754: PetscCall(PetscViewerASCIIPrintf(viewer, "zzz = [\n"));
756: for (i = 0; i < m; i++) {
757: for (j = a->i[i]; j < a->i[i + 1]; j++) {
758: #if PetscDefined(USE_COMPLEX)
759: PetscCall(PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT " %" PetscInt_FMT " %18.16e %18.16e\n", i + 1, a->j[j] + 1, (double)PetscRealPart(a->a[j]), (double)PetscImaginaryPart(a->a[j])));
760: #else
761: PetscCall(PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT " %" PetscInt_FMT " %18.16e\n", i + 1, a->j[j] + 1, (double)a->a[j]));
762: #endif
763: }
764: }
765: if (nofinalvalue) {
766: if (PetscDefined(USE_COMPLEX)) PetscCall(PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT " %" PetscInt_FMT " %18.16e %18.16e\n", m, A->cmap->n, 0., 0.));
767: else PetscCall(PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT " %" PetscInt_FMT " %18.16e\n", m, A->cmap->n, 0.0));
768: }
769: PetscCall(PetscObjectGetName((PetscObject)A, &name));
770: PetscCall(PetscViewerASCIIPrintf(viewer, "];\n %s = spconvert(zzz);\n", name));
771: PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_TRUE));
772: } else if (format == PETSC_VIEWER_ASCII_COMMON) {
773: PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_FALSE));
774: for (i = 0; i < m; i++) {
775: PetscCall(PetscViewerASCIIPrintf(viewer, "row %" PetscInt_FMT ":", i));
776: for (j = a->i[i]; j < a->i[i + 1]; j++) {
777: #if PetscDefined(USE_COMPLEX)
778: if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
779: PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g + %g i)", a->j[j], (double)PetscRealPart(a->a[j]), (double)PetscImaginaryPart(a->a[j])));
780: } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
781: PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g - %g i)", a->j[j], (double)PetscRealPart(a->a[j]), (double)-PetscImaginaryPart(a->a[j])));
782: } else if (PetscRealPart(a->a[j]) != 0.0) {
783: PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g) ", a->j[j], (double)PetscRealPart(a->a[j])));
784: }
785: #else
786: if (a->a[j] != 0.0) PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g) ", a->j[j], (double)a->a[j]));
787: #endif
788: }
789: PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
790: }
791: PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_TRUE));
792: } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
793: PetscInt nzd = 0, fshift = 1, *sptr;
794: PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_FALSE));
795: PetscCall(PetscMalloc1(m + 1, &sptr));
796: for (i = 0; i < m; i++) {
797: sptr[i] = nzd + 1;
798: for (j = a->i[i]; j < a->i[i + 1]; j++) {
799: if (a->j[j] >= i) {
800: if (PetscDefined(USE_COMPLEX) ? (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) : a->a[j] != 0.0) nzd++;
801: }
802: }
803: }
804: sptr[m] = nzd + 1;
805: PetscCall(PetscViewerASCIIPrintf(viewer, " %" PetscInt_FMT " %" PetscInt_FMT "\n\n", m, nzd));
806: for (i = 0; i < m + 1; i += 6) {
807: if (i + 4 < m) {
808: PetscCall(PetscViewerASCIIPrintf(viewer, " %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT "\n", sptr[i], sptr[i + 1], sptr[i + 2], sptr[i + 3], sptr[i + 4], sptr[i + 5]));
809: } else if (i + 3 < m) {
810: PetscCall(PetscViewerASCIIPrintf(viewer, " %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT "\n", sptr[i], sptr[i + 1], sptr[i + 2], sptr[i + 3], sptr[i + 4]));
811: } else if (i + 2 < m) {
812: PetscCall(PetscViewerASCIIPrintf(viewer, " %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT "\n", sptr[i], sptr[i + 1], sptr[i + 2], sptr[i + 3]));
813: } else if (i + 1 < m) {
814: PetscCall(PetscViewerASCIIPrintf(viewer, " %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT "\n", sptr[i], sptr[i + 1], sptr[i + 2]));
815: } else if (i < m) {
816: PetscCall(PetscViewerASCIIPrintf(viewer, " %" PetscInt_FMT " %" PetscInt_FMT "\n", sptr[i], sptr[i + 1]));
817: } else {
818: PetscCall(PetscViewerASCIIPrintf(viewer, " %" PetscInt_FMT "\n", sptr[i]));
819: }
820: }
821: PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
822: PetscCall(PetscFree(sptr));
823: for (i = 0; i < m; i++) {
824: for (j = a->i[i]; j < a->i[i + 1]; j++) {
825: if (a->j[j] >= i) PetscCall(PetscViewerASCIIPrintf(viewer, " %" PetscInt_FMT " ", a->j[j] + fshift));
826: }
827: PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
828: }
829: PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
830: for (i = 0; i < m; i++) {
831: for (j = a->i[i]; j < a->i[i + 1]; j++) {
832: if (a->j[j] >= i) {
833: #if PetscDefined(USE_COMPLEX)
834: if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) PetscCall(PetscViewerASCIIPrintf(viewer, " %18.16e %18.16e ", (double)PetscRealPart(a->a[j]), (double)PetscImaginaryPart(a->a[j])));
835: #else
836: if (a->a[j] != 0.0) PetscCall(PetscViewerASCIIPrintf(viewer, " %18.16e ", (double)a->a[j]));
837: #endif
838: }
839: }
840: PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
841: }
842: PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_TRUE));
843: } else if (format == PETSC_VIEWER_ASCII_DENSE) {
844: PetscInt cnt = 0, jcnt;
845: PetscScalar value;
846: PetscBool realonly = PETSC_TRUE;
848: if (PetscDefined(USE_COMPLEX)) {
849: for (i = 0; i < a->i[m]; i++) {
850: if (PetscImaginaryPart(a->a[i]) != 0.0) {
851: realonly = PETSC_FALSE;
852: break;
853: }
854: }
855: }
857: PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_FALSE));
858: for (i = 0; i < m; i++) {
859: jcnt = 0;
860: for (j = 0; j < A->cmap->n; j++) {
861: if (jcnt < a->i[i + 1] - a->i[i] && j == a->j[cnt]) {
862: value = a->a[cnt++];
863: jcnt++;
864: } else {
865: value = 0.0;
866: }
867: if (!PetscDefined(USE_COMPLEX) || realonly) {
868: PetscCall(PetscViewerASCIIPrintf(viewer, " %7.5e ", (double)PetscRealPart(value)));
869: } else {
870: PetscCall(PetscViewerASCIIPrintf(viewer, " %7.5e+%7.5e i ", (double)PetscRealPart(value), (double)PetscImaginaryPart(value)));
871: }
872: }
873: PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
874: }
875: PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_TRUE));
876: } else if (format == PETSC_VIEWER_ASCII_MATRIXMARKET) {
877: PetscInt fshift = 1;
878: PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_FALSE));
879: PetscCall(PetscViewerASCIIPrintf(viewer, "%%%%MatrixMarket matrix coordinate %s general\n", PetscDefined(USE_COMPLEX) ? "complex" : "real"));
880: PetscCall(PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT "\n", m, A->cmap->n, a->nz));
881: for (i = 0; i < m; i++) {
882: for (j = a->i[i]; j < a->i[i + 1]; j++) {
883: #if PetscDefined(USE_COMPLEX)
884: PetscCall(PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT " %" PetscInt_FMT " %g %g\n", i + fshift, a->j[j] + fshift, (double)PetscRealPart(a->a[j]), (double)PetscImaginaryPart(a->a[j])));
885: #else
886: PetscCall(PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT " %" PetscInt_FMT " %g\n", i + fshift, a->j[j] + fshift, (double)a->a[j]));
887: #endif
888: }
889: }
890: PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_TRUE));
891: } else {
892: PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_FALSE));
893: if (A->factortype) {
894: const PetscInt *adiag;
896: PetscCall(MatGetDiagonalMarkers_SeqAIJ(A, &adiag, NULL));
897: for (i = 0; i < m; i++) {
898: PetscCall(PetscViewerASCIIPrintf(viewer, "row %" PetscInt_FMT ":", i));
899: /* L part */
900: for (j = a->i[i]; j < a->i[i + 1]; j++) {
901: if (PetscDefined(USE_COMPLEX) && PetscImaginaryPart(a->a[j]) > 0.0) {
902: PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g + %g i)", a->j[j], (double)PetscRealPart(a->a[j]), (double)PetscImaginaryPart(a->a[j])));
903: } else if (PetscDefined(USE_COMPLEX) && PetscImaginaryPart(a->a[j]) < 0.0) {
904: PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g - %g i)", a->j[j], (double)PetscRealPart(a->a[j]), (double)(-PetscImaginaryPart(a->a[j]))));
905: } else {
906: PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g) ", a->j[j], (double)PetscRealPart(a->a[j])));
907: }
908: }
909: /* diagonal */
910: j = adiag[i];
911: if (PetscDefined(USE_COMPLEX) && PetscImaginaryPart(a->a[j]) > 0.0) {
912: PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g + %g i)", a->j[j], (double)PetscRealPart(1 / a->a[j]), (double)PetscImaginaryPart(1 / a->a[j])));
913: } else if (PetscDefined(USE_COMPLEX) && PetscImaginaryPart(a->a[j]) < 0.0) {
914: PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g - %g i)", a->j[j], (double)PetscRealPart(1 / a->a[j]), (double)(-PetscImaginaryPart(1 / a->a[j]))));
915: } else {
916: PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g) ", a->j[j], (double)PetscRealPart(1 / a->a[j])));
917: }
919: /* U part */
920: for (j = adiag[i + 1] + 1; j < adiag[i]; j++) {
921: if (PetscDefined(USE_COMPLEX) && PetscImaginaryPart(a->a[j]) > 0.0) {
922: PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g + %g i)", a->j[j], (double)PetscRealPart(a->a[j]), (double)PetscImaginaryPart(a->a[j])));
923: } else if (PetscDefined(USE_COMPLEX) && PetscImaginaryPart(a->a[j]) < 0.0) {
924: PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g - %g i)", a->j[j], (double)PetscRealPart(a->a[j]), (double)(-PetscImaginaryPart(a->a[j]))));
925: } else {
926: PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g) ", a->j[j], (double)PetscRealPart(a->a[j])));
927: }
928: }
929: PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
930: }
931: } else {
932: for (i = 0; i < m; i++) {
933: PetscCall(PetscViewerASCIIPrintf(viewer, "row %" PetscInt_FMT ":", i));
934: for (j = a->i[i]; j < a->i[i + 1]; j++) {
935: if (PetscDefined(USE_COMPLEX) && PetscImaginaryPart(a->a[j]) > 0.0) {
936: PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g + %g i)", a->j[j], (double)PetscRealPart(a->a[j]), (double)PetscImaginaryPart(a->a[j])));
937: } else if (PetscDefined(USE_COMPLEX) && PetscImaginaryPart(a->a[j]) < 0.0) {
938: PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g - %g i)", a->j[j], (double)PetscRealPart(a->a[j]), (double)-PetscImaginaryPart(a->a[j])));
939: } else {
940: PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g) ", a->j[j], (double)PetscRealPart(a->a[j])));
941: }
942: }
943: PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
944: }
945: }
946: PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_TRUE));
947: }
948: PetscCall(PetscViewerFlush(viewer));
949: PetscFunctionReturn(PETSC_SUCCESS);
950: }
952: #include <petscdraw.h>
953: #if defined(__GNUC__) && !defined(__clang__)
954: #pragma GCC diagnostic push
955: #pragma GCC diagnostic ignored "-Wclobbered"
956: #endif
957: static PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw, void *Aa)
958: {
959: Mat A = (Mat)Aa;
960: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
961: PetscInt i, j, m = A->rmap->n;
962: int color;
963: PetscReal xl, yl, xr, yr, x_l, x_r, y_l, y_r;
964: PetscViewer viewer;
965: PetscViewerFormat format;
966: const PetscScalar *aa;
968: PetscFunctionBegin;
969: PetscCall(PetscObjectQuery((PetscObject)A, "Zoomviewer", (PetscObject *)&viewer));
970: PetscCall(PetscViewerGetFormat(viewer, &format));
971: PetscCall(PetscDrawGetCoordinates(draw, &xl, &yl, &xr, &yr));
973: /* loop over matrix elements drawing boxes */
974: PetscCall(MatSeqAIJGetArrayRead(A, &aa));
975: if (format != PETSC_VIEWER_DRAW_CONTOUR) {
976: PetscDrawCollectiveBegin(draw);
977: /* Blue for negative, Cyan for zero and Red for positive */
978: color = PETSC_DRAW_BLUE;
979: for (i = 0; i < m; i++) {
980: y_l = m - i - 1.0;
981: y_r = y_l + 1.0;
982: for (j = a->i[i]; j < a->i[i + 1]; j++) {
983: x_l = a->j[j];
984: x_r = x_l + 1.0;
985: if (PetscRealPart(aa[j]) >= 0.) continue;
986: PetscCall(PetscDrawRectangle(draw, x_l, y_l, x_r, y_r, color, color, color, color));
987: }
988: }
989: color = PETSC_DRAW_CYAN;
990: for (i = 0; i < m; i++) {
991: y_l = m - i - 1.0;
992: y_r = y_l + 1.0;
993: for (j = a->i[i]; j < a->i[i + 1]; j++) {
994: x_l = a->j[j];
995: x_r = x_l + 1.0;
996: if (aa[j] != 0.) continue;
997: PetscCall(PetscDrawRectangle(draw, x_l, y_l, x_r, y_r, color, color, color, color));
998: }
999: }
1000: color = PETSC_DRAW_RED;
1001: for (i = 0; i < m; i++) {
1002: y_l = m - i - 1.0;
1003: y_r = y_l + 1.0;
1004: for (j = a->i[i]; j < a->i[i + 1]; j++) {
1005: x_l = a->j[j];
1006: x_r = x_l + 1.0;
1007: if (PetscRealPart(aa[j]) <= 0.) continue;
1008: PetscCall(PetscDrawRectangle(draw, x_l, y_l, x_r, y_r, color, color, color, color));
1009: }
1010: }
1011: PetscDrawCollectiveEnd(draw);
1012: } else {
1013: /* use contour shading to indicate magnitude of values */
1014: /* first determine max of all nonzero values */
1015: PetscReal minv = 0.0, maxv = 0.0;
1016: PetscInt nz = a->nz, count = 0;
1017: PetscDraw popup;
1019: for (i = 0; i < nz; i++) {
1020: if (PetscAbsScalar(aa[i]) > maxv) maxv = PetscAbsScalar(aa[i]);
1021: }
1022: if (minv >= maxv) maxv = minv + PETSC_SMALL;
1023: PetscCall(PetscDrawGetPopup(draw, &popup));
1024: PetscCall(PetscDrawScalePopup(popup, minv, maxv));
1026: PetscDrawCollectiveBegin(draw);
1027: for (i = 0; i < m; i++) {
1028: y_l = m - i - 1.0;
1029: y_r = y_l + 1.0;
1030: for (j = a->i[i]; j < a->i[i + 1]; j++) {
1031: x_l = a->j[j];
1032: x_r = x_l + 1.0;
1033: color = PetscDrawRealToColor(PetscAbsScalar(aa[count]), minv, maxv);
1034: PetscCall(PetscDrawRectangle(draw, x_l, y_l, x_r, y_r, color, color, color, color));
1035: count++;
1036: }
1037: }
1038: PetscDrawCollectiveEnd(draw);
1039: }
1040: PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
1041: PetscFunctionReturn(PETSC_SUCCESS);
1042: }
1043: #if defined(__GNUC__) && !defined(__clang__)
1044: #pragma GCC diagnostic pop
1045: #endif
1047: #include <petscdraw.h>
1048: static PetscErrorCode MatView_SeqAIJ_Draw(Mat A, PetscViewer viewer)
1049: {
1050: PetscDraw draw;
1051: PetscReal xr, yr, xl, yl, h, w;
1052: PetscBool isnull;
1054: PetscFunctionBegin;
1055: PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw));
1056: PetscCall(PetscDrawIsNull(draw, &isnull));
1057: if (isnull) PetscFunctionReturn(PETSC_SUCCESS);
1059: xr = A->cmap->n;
1060: yr = A->rmap->n;
1061: h = yr / 10.0;
1062: w = xr / 10.0;
1063: xr += w;
1064: yr += h;
1065: xl = -w;
1066: yl = -h;
1067: PetscCall(PetscDrawSetCoordinates(draw, xl, yl, xr, yr));
1068: PetscCall(PetscObjectCompose((PetscObject)A, "Zoomviewer", (PetscObject)viewer));
1069: PetscCall(PetscDrawZoom(draw, MatView_SeqAIJ_Draw_Zoom, A));
1070: PetscCall(PetscObjectCompose((PetscObject)A, "Zoomviewer", NULL));
1071: PetscCall(PetscDrawSave(draw));
1072: PetscFunctionReturn(PETSC_SUCCESS);
1073: }
1075: PetscErrorCode MatView_SeqAIJ(Mat A, PetscViewer viewer)
1076: {
1077: PetscBool isascii, isbinary, isdraw;
1079: PetscFunctionBegin;
1080: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
1081: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
1082: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
1083: if (isascii) PetscCall(MatView_SeqAIJ_ASCII(A, viewer));
1084: else if (isbinary) PetscCall(MatView_SeqAIJ_Binary(A, viewer));
1085: else if (isdraw) PetscCall(MatView_SeqAIJ_Draw(A, viewer));
1086: PetscCall(MatView_SeqAIJ_Inode(A, viewer));
1087: PetscFunctionReturn(PETSC_SUCCESS);
1088: }
1090: PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A, MatAssemblyType mode)
1091: {
1092: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
1093: PetscInt fshift = 0, i, *ai = a->i, *aj = a->j, *imax = a->imax;
1094: PetscInt m = A->rmap->n, *ip, N, *ailen = a->ilen, rmax = 0;
1095: MatScalar *aa = a->a, *ap;
1096: PetscReal ratio = 0.6;
1098: PetscFunctionBegin;
1099: if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(PETSC_SUCCESS);
1100: if (A->was_assembled && A->ass_nonzerostate == A->nonzerostate) {
1101: /* we need to respect users asking to use or not the inodes routine in between matrix assemblies, e.g., via MatSetOption(A, MAT_USE_INODES, val) */
1102: PetscCall(MatAssemblyEnd_SeqAIJ_Inode(A, mode)); /* read the sparsity pattern */
1103: PetscFunctionReturn(PETSC_SUCCESS);
1104: }
1106: if (m) rmax = ailen[0]; /* determine row with most nonzeros */
1107: for (i = 1; i < m; i++) {
1108: /* move each row back by the amount of empty slots (fshift) before it*/
1109: fshift += imax[i - 1] - ailen[i - 1];
1110: rmax = PetscMax(rmax, ailen[i]);
1111: if (fshift) {
1112: ip = aj + ai[i];
1113: N = ailen[i];
1114: PetscCall(PetscArraymove(ip - fshift, ip, N));
1115: if (!A->structure_only) {
1116: ap = aa + ai[i];
1117: PetscCall(PetscArraymove(ap - fshift, ap, N));
1118: }
1119: }
1120: ai[i] = ai[i - 1] + ailen[i - 1];
1121: }
1122: if (m) {
1123: fshift += imax[m - 1] - ailen[m - 1];
1124: ai[m] = ai[m - 1] + ailen[m - 1];
1125: }
1126: /* reset ilen and imax for each row */
1127: a->nonzerorowcnt = 0;
1128: for (i = 0; i < m; i++) {
1129: ailen[i] = imax[i] = ai[i + 1] - ai[i];
1130: a->nonzerorowcnt += ((ai[i + 1] - ai[i]) > 0);
1131: }
1132: a->nz = ai[m];
1133: PetscCheck(!fshift || a->nounused != -1, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Unused space detected in matrix: %" PetscInt_FMT " X %" PetscInt_FMT ", %" PetscInt_FMT " unneeded", m, A->cmap->n, fshift);
1134: PetscCall(PetscInfo(A, "Matrix size: %" PetscInt_FMT " X %" PetscInt_FMT "; storage space: %" PetscInt_FMT " unneeded, %" PetscInt_FMT " used\n", m, A->cmap->n, fshift, a->nz));
1135: PetscCall(PetscInfo(A, "Number of mallocs during MatSetValues() is %" PetscInt_FMT "\n", a->reallocs));
1136: PetscCall(PetscInfo(A, "Maximum nonzeros in any row is %" PetscInt_FMT "\n", rmax));
1138: A->info.mallocs += a->reallocs;
1139: a->reallocs = 0;
1140: A->info.nz_unneeded = (PetscReal)fshift;
1141: a->rmax = rmax;
1143: if (!A->structure_only) PetscCall(MatCheckCompressedRow(A, a->nonzerorowcnt, &a->compressedrow, a->i, m, ratio));
1144: PetscCall(MatAssemblyEnd_SeqAIJ_Inode(A, mode));
1145: PetscFunctionReturn(PETSC_SUCCESS);
1146: }
1148: static PetscErrorCode MatRealPart_SeqAIJ(Mat A)
1149: {
1150: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
1151: PetscInt i, nz = a->nz;
1152: MatScalar *aa;
1154: PetscFunctionBegin;
1155: PetscCall(MatSeqAIJGetArray(A, &aa));
1156: for (i = 0; i < nz; i++) aa[i] = PetscRealPart(aa[i]);
1157: PetscCall(MatSeqAIJRestoreArray(A, &aa));
1158: PetscFunctionReturn(PETSC_SUCCESS);
1159: }
1161: static PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
1162: {
1163: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
1164: PetscInt i, nz = a->nz;
1165: MatScalar *aa;
1167: PetscFunctionBegin;
1168: PetscCall(MatSeqAIJGetArray(A, &aa));
1169: for (i = 0; i < nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
1170: PetscCall(MatSeqAIJRestoreArray(A, &aa));
1171: PetscFunctionReturn(PETSC_SUCCESS);
1172: }
1174: PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
1175: {
1176: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
1177: MatScalar *aa;
1179: PetscFunctionBegin;
1180: PetscCall(MatSeqAIJGetArrayWrite(A, &aa));
1181: PetscCall(PetscArrayzero(aa, a->i[A->rmap->n]));
1182: PetscCall(MatSeqAIJRestoreArrayWrite(A, &aa));
1183: PetscFunctionReturn(PETSC_SUCCESS);
1184: }
1186: static PetscErrorCode MatReset_SeqAIJ(Mat A)
1187: {
1188: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
1190: PetscFunctionBegin;
1191: if (A->hash_active) {
1192: A->ops[0] = a->cops;
1193: PetscCall(PetscHMapIJVDestroy(&a->ht));
1194: PetscCall(PetscFree(a->dnz));
1195: A->hash_active = PETSC_FALSE;
1196: }
1198: PetscCall(PetscLogObjectState((PetscObject)A, "Rows=%" PetscInt_FMT ", Cols=%" PetscInt_FMT ", NZ=%" PetscInt_FMT, A->rmap->n, A->cmap->n, a->nz));
1199: PetscCall(MatSeqXAIJFreeAIJ(A, &a->a, &a->j, &a->i));
1200: PetscCall(ISDestroy(&a->row));
1201: PetscCall(ISDestroy(&a->col));
1202: PetscCall(PetscFree(a->diag));
1203: PetscCall(PetscFree(a->ibdiag));
1204: a->ibdiagsize = 0;
1205: PetscCall(PetscFree(a->imax));
1206: PetscCall(PetscFree(a->ilen));
1207: PetscCall(PetscFree(a->ipre));
1208: PetscCall(PetscFree3(a->idiag, a->mdiag, a->ssor_work));
1209: PetscCall(PetscFree(a->solve_work));
1210: PetscCall(ISDestroy(&a->icol));
1211: PetscCall(PetscFree(a->saved_values));
1212: a->compressedrow.use = PETSC_FALSE;
1213: PetscCall(PetscFree2(a->compressedrow.i, a->compressedrow.rindex));
1214: PetscCall(MatDestroy_SeqAIJ_Inode(A));
1215: PetscFunctionReturn(PETSC_SUCCESS);
1216: }
1218: static PetscErrorCode MatResetHash_SeqAIJ(Mat A)
1219: {
1220: PetscFunctionBegin;
1221: PetscCall(MatReset_SeqAIJ(A));
1222: PetscCall(MatCreate_SeqAIJ_Inode(A));
1223: PetscCall(MatSetUp_Seq_Hash(A));
1224: A->nonzerostate++;
1225: PetscFunctionReturn(PETSC_SUCCESS);
1226: }
1228: PetscErrorCode MatDestroy_SeqAIJ(Mat A)
1229: {
1230: PetscFunctionBegin;
1231: PetscCall(MatReset_SeqAIJ(A));
1232: PetscCall(PetscFree(A->data));
1234: /* MatMatMultNumeric_SeqAIJ_SeqAIJ_Sorted may allocate this.
1235: That function is so heavily used (sometimes in an hidden way through multnumeric function pointers)
1236: that is hard to properly add this data to the MatProduct data. We free it here to avoid
1237: users reusing the matrix object with different data to incur in obscure segmentation faults
1238: due to different matrix sizes */
1239: PetscCall(PetscObjectCompose((PetscObject)A, "__PETSc__ab_dense", NULL));
1241: PetscCall(PetscObjectChangeTypeName((PetscObject)A, NULL));
1242: PetscCall(PetscObjectComposeFunction((PetscObject)A, "PetscMatlabEnginePut_C", NULL));
1243: PetscCall(PetscObjectComposeFunction((PetscObject)A, "PetscMatlabEngineGet_C", NULL));
1244: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatSeqAIJSetColumnIndices_C", NULL));
1245: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatStoreValues_C", NULL));
1246: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatRetrieveValues_C", NULL));
1247: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_seqsbaij_C", NULL));
1248: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_seqbaij_C", NULL));
1249: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_seqaijperm_C", NULL));
1250: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_seqaijsell_C", NULL));
1251: #if PetscDefined(HAVE_MKL_SPARSE)
1252: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_seqaijmkl_C", NULL));
1253: #endif
1254: #if PetscDefined(HAVE_CUDA)
1255: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_seqaijcusparse_C", NULL));
1256: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatProductSetFromOptions_seqaijcusparse_seqaij_C", NULL));
1257: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatProductSetFromOptions_seqaij_seqaijcusparse_C", NULL));
1258: #endif
1259: #if PetscDefined(HAVE_HIP)
1260: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_seqaijhipsparse_C", NULL));
1261: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatProductSetFromOptions_seqaijhipsparse_seqaij_C", NULL));
1262: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatProductSetFromOptions_seqaij_seqaijhipsparse_C", NULL));
1263: #endif
1264: #if PetscDefined(HAVE_KOKKOS_KERNELS)
1265: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_seqaijkokkos_C", NULL));
1266: #endif
1267: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_seqaijcrl_C", NULL));
1268: #if PetscDefined(HAVE_ELEMENTAL)
1269: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_elemental_C", NULL));
1270: #endif
1271: #if PetscDefined(HAVE_SCALAPACK) && (PetscDefined(USE_REAL_SINGLE) || PetscDefined(USE_REAL_DOUBLE))
1272: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_scalapack_C", NULL));
1273: #endif
1274: #if PetscDefined(HAVE_HYPRE)
1275: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_hypre_C", NULL));
1276: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatProductSetFromOptions_transpose_seqaij_seqaij_C", NULL));
1277: #endif
1278: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_seqdense_C", NULL));
1279: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_seqsell_C", NULL));
1280: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_is_C", NULL));
1281: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatIsTranspose_C", NULL));
1282: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatIsHermitianTranspose_C", NULL));
1283: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatSeqAIJSetPreallocation_C", NULL));
1284: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatResetPreallocation_C", NULL));
1285: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatResetHash_C", NULL));
1286: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatSeqAIJSetPreallocationCSR_C", NULL));
1287: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatReorderForNonzeroDiagonal_C", NULL));
1288: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatProductSetFromOptions_is_seqaij_C", NULL));
1289: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatProductSetFromOptions_seqdense_seqaij_C", NULL));
1290: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatProductSetFromOptions_seqaij_seqaij_C", NULL));
1291: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatSeqAIJKron_C", NULL));
1292: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatSetPreallocationCOO_C", NULL));
1293: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatSetValuesCOO_C", NULL));
1294: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatFactorGetSolverType_C", NULL));
1295: /* these calls do not belong here: the subclasses Duplicate/Destroy are wrong */
1296: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaijsell_seqaij_C", NULL));
1297: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaijperm_seqaij_C", NULL));
1298: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_seqaijviennacl_C", NULL));
1299: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatProductSetFromOptions_seqaijviennacl_seqdense_C", NULL));
1300: PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatProductSetFromOptions_seqaijviennacl_seqaij_C", NULL));
1301: PetscFunctionReturn(PETSC_SUCCESS);
1302: }
1304: PetscErrorCode MatSetOption_SeqAIJ(Mat A, MatOption op, PetscBool flg)
1305: {
1306: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
1308: PetscFunctionBegin;
1309: switch (op) {
1310: case MAT_ROW_ORIENTED:
1311: a->roworiented = flg;
1312: break;
1313: case MAT_KEEP_NONZERO_PATTERN:
1314: a->keepnonzeropattern = flg;
1315: break;
1316: case MAT_NEW_NONZERO_LOCATIONS:
1317: a->nonew = (flg ? 0 : 1);
1318: break;
1319: case MAT_NEW_NONZERO_LOCATION_ERR:
1320: a->nonew = (flg ? -1 : 0);
1321: break;
1322: case MAT_NEW_NONZERO_ALLOCATION_ERR:
1323: a->nonew = (flg ? -2 : 0);
1324: break;
1325: case MAT_UNUSED_NONZERO_LOCATION_ERR:
1326: a->nounused = (flg ? -1 : 0);
1327: break;
1328: case MAT_IGNORE_ZERO_ENTRIES:
1329: a->ignorezeroentries = flg;
1330: break;
1331: case MAT_USE_INODES:
1332: PetscCall(MatSetOption_SeqAIJ_Inode(A, MAT_USE_INODES, flg));
1333: break;
1334: case MAT_SUBMAT_SINGLEIS:
1335: A->submat_singleis = flg;
1336: break;
1337: case MAT_SORTED_FULL:
1338: if (flg) A->ops->setvalues = MatSetValues_SeqAIJ_SortedFull;
1339: else A->ops->setvalues = MatSetValues_SeqAIJ;
1340: break;
1341: case MAT_FORM_EXPLICIT_TRANSPOSE:
1342: A->form_explicit_transpose = flg;
1343: break;
1344: case MAT_STRUCTURE_ONLY:
1345: if (flg) {
1346: PetscCall(MatXAIJDeallocatea(A, &a->a));
1347: a->a = NULL;
1348: }
1349: break;
1350: default:
1351: break;
1352: }
1353: PetscFunctionReturn(PETSC_SUCCESS);
1354: }
1356: PETSC_INTERN PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A, Vec v)
1357: {
1358: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
1359: PetscInt n, *ai = a->i;
1360: PetscScalar *x;
1361: const PetscScalar *aa;
1362: const PetscInt *diag;
1363: PetscBool diagDense;
1365: PetscFunctionBegin;
1366: PetscCall(VecGetLocalSize(v, &n));
1367: PetscCheck(n == A->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nonconforming matrix and vector");
1368: PetscCall(MatSeqAIJGetArrayRead(A, &aa));
1369: if (A->factortype == MAT_FACTOR_ILU || A->factortype == MAT_FACTOR_LU) {
1370: PetscCall(MatGetDiagonalMarkers_SeqAIJ(A, &diag, NULL));
1371: PetscCall(VecGetArrayWrite(v, &x));
1372: for (PetscInt i = 0; i < n; i++) x[i] = 1.0 / aa[diag[i]];
1373: PetscCall(VecRestoreArrayWrite(v, &x));
1374: PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
1375: PetscFunctionReturn(PETSC_SUCCESS);
1376: }
1378: PetscCheck(A->factortype == MAT_FACTOR_NONE, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Not for factor matrices that are not ILU or LU");
1379: PetscCall(MatGetDiagonalMarkers_SeqAIJ(A, &diag, &diagDense));
1380: PetscCall(VecGetArrayWrite(v, &x));
1381: if (diagDense) {
1382: for (PetscInt i = 0; i < n; i++) x[i] = aa[diag[i]];
1383: } else {
1384: for (PetscInt i = 0; i < n; i++) x[i] = (diag[i] == ai[i + 1]) ? 0.0 : aa[diag[i]];
1385: }
1386: PetscCall(VecRestoreArrayWrite(v, &x));
1387: PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
1388: PetscFunctionReturn(PETSC_SUCCESS);
1389: }
1391: #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
1392: PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A, Vec xx, Vec zz, Vec yy)
1393: {
1394: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
1395: const MatScalar *aa;
1396: PetscScalar *y;
1397: const PetscScalar *x;
1398: PetscInt m = A->rmap->n;
1399: #if !PetscDefined(USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1400: const MatScalar *v;
1401: PetscScalar alpha;
1402: PetscInt n, i, j;
1403: const PetscInt *idx, *ii, *ridx = NULL;
1404: Mat_CompressedRow cprow = a->compressedrow;
1405: PetscBool usecprow = cprow.use;
1406: #endif
1408: PetscFunctionBegin;
1409: if (zz != yy) PetscCall(VecCopy(zz, yy));
1410: PetscCall(VecGetArrayRead(xx, &x));
1411: PetscCall(VecGetArray(yy, &y));
1412: PetscCall(MatSeqAIJGetArrayRead(A, &aa));
1414: #if PetscDefined(USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1415: fortranmulttransposeaddaij_(&m, x, a->i, a->j, aa, y);
1416: #else
1417: if (usecprow) {
1418: m = cprow.nrows;
1419: ii = cprow.i;
1420: ridx = cprow.rindex;
1421: } else {
1422: ii = a->i;
1423: }
1424: for (i = 0; i < m; i++) {
1425: idx = a->j + ii[i];
1426: v = aa + ii[i];
1427: n = ii[i + 1] - ii[i];
1428: if (usecprow) {
1429: alpha = x[ridx[i]];
1430: } else {
1431: alpha = x[i];
1432: }
1433: for (j = 0; j < n; j++) y[idx[j]] += alpha * v[j];
1434: }
1435: #endif
1436: PetscCall(PetscLogFlops(2.0 * a->nz));
1437: PetscCall(VecRestoreArrayRead(xx, &x));
1438: PetscCall(VecRestoreArray(yy, &y));
1439: PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
1440: PetscFunctionReturn(PETSC_SUCCESS);
1441: }
1443: PetscErrorCode MatMultTranspose_SeqAIJ(Mat A, Vec xx, Vec yy)
1444: {
1445: PetscFunctionBegin;
1446: PetscCall(VecSet(yy, 0.0));
1447: PetscCall(MatMultTransposeAdd_SeqAIJ(A, xx, yy, yy));
1448: PetscFunctionReturn(PETSC_SUCCESS);
1449: }
1451: #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
1453: PetscErrorCode MatMult_SeqAIJ(Mat A, Vec xx, Vec yy)
1454: {
1455: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
1456: PetscScalar *y;
1457: const PetscScalar *x;
1458: const MatScalar *a_a;
1459: PetscInt m = A->rmap->n;
1460: const PetscInt *ii, *ridx = NULL;
1461: PetscBool usecprow = a->compressedrow.use;
1463: #if PetscDefined(HAVE_PRAGMA_DISJOINT)
1464: #pragma disjoint(*x, *y, *aa)
1465: #endif
1467: PetscFunctionBegin;
1468: if (a->inode.use && a->inode.checked) {
1469: PetscCall(MatMult_SeqAIJ_Inode(A, xx, yy));
1470: PetscFunctionReturn(PETSC_SUCCESS);
1471: }
1472: PetscCall(MatSeqAIJGetArrayRead(A, &a_a));
1473: PetscCall(VecGetArrayRead(xx, &x));
1474: PetscCall(VecGetArray(yy, &y));
1475: ii = a->i;
1476: if (usecprow) { /* use compressed row format */
1477: PetscCall(PetscArrayzero(y, m));
1478: m = a->compressedrow.nrows;
1479: ii = a->compressedrow.i;
1480: ridx = a->compressedrow.rindex;
1481: PetscPragmaUseOMPKernels(parallel for)
1482: for (PetscInt i = 0; i < m; i++) {
1483: PetscInt n = ii[i + 1] - ii[i];
1484: const PetscInt *aj = a->j + ii[i];
1485: const PetscScalar *aa = a_a + ii[i];
1486: PetscScalar sum = 0.0;
1487: PetscSparseDensePlusDot(sum, x, aa, aj, n);
1488: /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
1489: y[ridx[i]] = sum;
1490: }
1491: } else { /* do not use compressed row format */
1492: #if PetscDefined(USE_FORTRAN_KERNEL_MULTAIJ)
1493: fortranmultaij_(&m, x, ii, a->j, a_a, y);
1494: #else
1495: PetscPragmaUseOMPKernels(parallel for)
1496: for (PetscInt i = 0; i < m; i++) {
1497: PetscInt n = ii[i + 1] - ii[i];
1498: const PetscInt *aj = a->j + ii[i];
1499: const PetscScalar *aa = a_a + ii[i];
1500: PetscScalar sum = 0.0;
1501: PetscSparseDensePlusDot(sum, x, aa, aj, n);
1502: y[i] = sum;
1503: }
1504: #endif
1505: }
1506: PetscCall(PetscLogFlops(2.0 * a->nz - a->nonzerorowcnt));
1507: PetscCall(VecRestoreArrayRead(xx, &x));
1508: PetscCall(VecRestoreArray(yy, &y));
1509: PetscCall(MatSeqAIJRestoreArrayRead(A, &a_a));
1510: PetscFunctionReturn(PETSC_SUCCESS);
1511: }
1513: // HACK!!!!! Used by src/mat/tests/ex170.c
1514: PETSC_EXTERN PetscErrorCode MatMultMax_SeqAIJ(Mat A, Vec xx, Vec yy)
1515: {
1516: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
1517: PetscScalar *y;
1518: const PetscScalar *x;
1519: const MatScalar *aa, *a_a;
1520: PetscInt m = A->rmap->n;
1521: const PetscInt *aj, *ii, *ridx = NULL;
1522: PetscInt n, i, nonzerorow = 0;
1523: PetscScalar sum;
1524: PetscBool usecprow = a->compressedrow.use;
1526: #if PetscDefined(HAVE_PRAGMA_DISJOINT)
1527: #pragma disjoint(*x, *y, *aa)
1528: #endif
1530: PetscFunctionBegin;
1531: PetscCall(MatSeqAIJGetArrayRead(A, &a_a));
1532: PetscCall(VecGetArrayRead(xx, &x));
1533: PetscCall(VecGetArray(yy, &y));
1534: if (usecprow) { /* use compressed row format */
1535: m = a->compressedrow.nrows;
1536: ii = a->compressedrow.i;
1537: ridx = a->compressedrow.rindex;
1538: for (i = 0; i < m; i++) {
1539: n = ii[i + 1] - ii[i];
1540: aj = a->j + ii[i];
1541: aa = a_a + ii[i];
1542: sum = 0.0;
1543: nonzerorow += (n > 0);
1544: PetscSparseDenseMaxDot(sum, x, aa, aj, n);
1545: /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
1546: y[*ridx++] = sum;
1547: }
1548: } else { /* do not use compressed row format */
1549: ii = a->i;
1550: for (i = 0; i < m; i++) {
1551: n = ii[i + 1] - ii[i];
1552: aj = a->j + ii[i];
1553: aa = a_a + ii[i];
1554: sum = 0.0;
1555: nonzerorow += (n > 0);
1556: PetscSparseDenseMaxDot(sum, x, aa, aj, n);
1557: y[i] = sum;
1558: }
1559: }
1560: PetscCall(PetscLogFlops(2.0 * a->nz - nonzerorow));
1561: PetscCall(VecRestoreArrayRead(xx, &x));
1562: PetscCall(VecRestoreArray(yy, &y));
1563: PetscCall(MatSeqAIJRestoreArrayRead(A, &a_a));
1564: PetscFunctionReturn(PETSC_SUCCESS);
1565: }
1567: // HACK!!!!! Used by src/mat/tests/ex170.c
1568: PETSC_EXTERN PetscErrorCode MatMultAddMax_SeqAIJ(Mat A, Vec xx, Vec yy, Vec zz)
1569: {
1570: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
1571: PetscScalar *y, *z;
1572: const PetscScalar *x;
1573: const MatScalar *aa, *a_a;
1574: PetscInt m = A->rmap->n, *aj, *ii;
1575: PetscInt n, i, *ridx = NULL;
1576: PetscScalar sum;
1577: PetscBool usecprow = a->compressedrow.use;
1579: PetscFunctionBegin;
1580: PetscCall(MatSeqAIJGetArrayRead(A, &a_a));
1581: PetscCall(VecGetArrayRead(xx, &x));
1582: PetscCall(VecGetArrayPair(yy, zz, &y, &z));
1583: if (usecprow) { /* use compressed row format */
1584: if (zz != yy) PetscCall(PetscArraycpy(z, y, m));
1585: m = a->compressedrow.nrows;
1586: ii = a->compressedrow.i;
1587: ridx = a->compressedrow.rindex;
1588: for (i = 0; i < m; i++) {
1589: n = ii[i + 1] - ii[i];
1590: aj = a->j + ii[i];
1591: aa = a_a + ii[i];
1592: sum = y[*ridx];
1593: PetscSparseDenseMaxDot(sum, x, aa, aj, n);
1594: z[*ridx++] = sum;
1595: }
1596: } else { /* do not use compressed row format */
1597: ii = a->i;
1598: for (i = 0; i < m; i++) {
1599: n = ii[i + 1] - ii[i];
1600: aj = a->j + ii[i];
1601: aa = a_a + ii[i];
1602: sum = y[i];
1603: PetscSparseDenseMaxDot(sum, x, aa, aj, n);
1604: z[i] = sum;
1605: }
1606: }
1607: PetscCall(PetscLogFlops(2.0 * a->nz));
1608: PetscCall(VecRestoreArrayRead(xx, &x));
1609: PetscCall(VecRestoreArrayPair(yy, zz, &y, &z));
1610: PetscCall(MatSeqAIJRestoreArrayRead(A, &a_a));
1611: PetscFunctionReturn(PETSC_SUCCESS);
1612: }
1614: #include <../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h>
1615: PetscErrorCode MatMultAdd_SeqAIJ(Mat A, Vec xx, Vec yy, Vec zz)
1616: {
1617: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
1618: PetscScalar *y, *z;
1619: const PetscScalar *x;
1620: const MatScalar *a_a;
1621: const PetscInt *ii, *ridx = NULL;
1622: PetscInt m = A->rmap->n;
1623: PetscBool usecprow = a->compressedrow.use;
1625: PetscFunctionBegin;
1626: if (a->inode.use && a->inode.checked) {
1627: PetscCall(MatMultAdd_SeqAIJ_Inode(A, xx, yy, zz));
1628: PetscFunctionReturn(PETSC_SUCCESS);
1629: }
1630: PetscCall(MatSeqAIJGetArrayRead(A, &a_a));
1631: PetscCall(VecGetArrayRead(xx, &x));
1632: PetscCall(VecGetArrayPair(yy, zz, &y, &z));
1633: if (usecprow) { /* use compressed row format */
1634: if (zz != yy) PetscCall(PetscArraycpy(z, y, m));
1635: m = a->compressedrow.nrows;
1636: ii = a->compressedrow.i;
1637: ridx = a->compressedrow.rindex;
1638: for (PetscInt i = 0; i < m; i++) {
1639: PetscInt n = ii[i + 1] - ii[i];
1640: const PetscInt *aj = a->j + ii[i];
1641: const PetscScalar *aa = a_a + ii[i];
1642: PetscScalar sum = y[*ridx];
1643: PetscSparseDensePlusDot(sum, x, aa, aj, n);
1644: z[*ridx++] = sum;
1645: }
1646: } else { /* do not use compressed row format */
1647: ii = a->i;
1648: #if PetscDefined(USE_FORTRAN_KERNEL_MULTADDAIJ)
1649: fortranmultaddaij_(&m, x, ii, a->j, a_a, y, z);
1650: #else
1651: PetscPragmaUseOMPKernels(parallel for)
1652: for (PetscInt i = 0; i < m; i++) {
1653: PetscInt n = ii[i + 1] - ii[i];
1654: const PetscInt *aj = a->j + ii[i];
1655: const PetscScalar *aa = a_a + ii[i];
1656: PetscScalar sum = y[i];
1657: PetscSparseDensePlusDot(sum, x, aa, aj, n);
1658: z[i] = sum;
1659: }
1660: #endif
1661: }
1662: PetscCall(PetscLogFlops(2.0 * a->nz));
1663: PetscCall(VecRestoreArrayRead(xx, &x));
1664: PetscCall(VecRestoreArrayPair(yy, zz, &y, &z));
1665: PetscCall(MatSeqAIJRestoreArrayRead(A, &a_a));
1666: PetscFunctionReturn(PETSC_SUCCESS);
1667: }
1669: static PetscErrorCode MatShift_SeqAIJ(Mat A, PetscScalar v)
1670: {
1671: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
1672: const PetscInt *diag;
1673: const PetscInt *ii = (const PetscInt *)a->i;
1674: const PetscInt ndiag = PetscMin(A->rmap->n, A->cmap->n);
1675: PetscBool diagDense, hasDiag;
1677: PetscFunctionBegin;
1678: /* If A is preallocated but has no nonzeros, preallocate it again only if it is not 0 x n or m x 0, otherwise the nonzero state
1679: would increase even though the shift creates no new nonzeros */
1680: if (!A->preallocated || (!a->nz && ndiag > 0)) {
1681: PetscCall(MatSeqAIJSetPreallocation(A, 1, NULL));
1682: PetscCall(MatShift_Basic(A, v));
1683: PetscFunctionReturn(PETSC_SUCCESS);
1684: }
1686: PetscCall(MatGetDiagonalMarkers_SeqAIJ(A, &diag, &diagDense));
1687: /* Reallocate A only if a diagonal entry is missing, otherwise the nonzero state would increase even though the shift creates no
1688: new nonzeros. diagDense alone cannot tell, as it also counts the rows past the last column, which cannot have a diagonal entry */
1689: hasDiag = diagDense;
1690: if (!hasDiag) {
1691: hasDiag = PETSC_TRUE;
1692: for (PetscInt i = 0; i < ndiag && hasDiag; i++) hasDiag = (PetscBool)(diag[i] < ii[i + 1]);
1693: }
1694: if (hasDiag) {
1695: PetscScalar *Aa;
1697: PetscCall(MatSeqAIJGetArray(A, &Aa));
1698: for (PetscInt i = 0; i < ndiag; i++) Aa[diag[i]] += v;
1699: PetscCall(MatSeqAIJRestoreArray(A, &Aa));
1700: } else {
1701: PetscScalar *olda = a->a; /* preserve pointers to current matrix nonzeros structure and values */
1702: PetscInt *oldj = a->j, *oldi = a->i;
1703: PetscBool free_a = a->free_a, free_ij = a->free_ij;
1704: const PetscScalar *Aa;
1705: PetscInt *mdiag = NULL;
1707: PetscCall(PetscCalloc1(A->rmap->n, &mdiag));
1708: for (PetscInt i = 0; i < A->rmap->n; i++) {
1709: if (i < A->cmap->n && diag[i] >= ii[i + 1]) { /* 'out of range' rows never have diagonals */
1710: mdiag[i] = 1;
1711: }
1712: }
1713: PetscCall(MatSeqAIJGetArrayRead(A, &Aa)); // sync the host
1714: PetscCall(MatSeqAIJRestoreArrayRead(A, &Aa));
1716: a->a = NULL;
1717: a->j = NULL;
1718: a->i = NULL;
1719: /* increase the values in imax for each row where a diagonal is being inserted then reallocate the matrix data structures */
1720: for (PetscInt i = 0; i < PetscMin(A->rmap->n, A->cmap->n); i++) a->imax[i] += mdiag[i];
1721: PetscCall(MatSeqAIJSetPreallocation_SeqAIJ(A, 0, a->imax));
1723: /* copy old values into new matrix data structure */
1724: for (PetscInt i = 0; i < A->rmap->n; i++) {
1725: PetscCall(MatSetValues(A, 1, &i, a->imax[i] - mdiag[i], &oldj[oldi[i]], &olda[oldi[i]], ADD_VALUES));
1726: if (i < A->cmap->n) PetscCall(MatSetValue(A, i, i, v, ADD_VALUES));
1727: }
1728: PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
1729: PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
1730: if (free_a) PetscCall(PetscShmgetDeallocateArray((void **)&olda));
1731: if (free_ij) PetscCall(PetscShmgetDeallocateArray((void **)&oldj));
1732: if (free_ij) PetscCall(PetscShmgetDeallocateArray((void **)&oldi));
1733: PetscCall(PetscFree(mdiag));
1734: }
1735: PetscFunctionReturn(PETSC_SUCCESS);
1736: }
1738: #include <petscblaslapack.h>
1739: #include <petsc/private/kernels/blockinvert.h>
1741: /*
1742: Note that values is allocated externally by the PC and then passed into this routine
1743: */
1744: static PetscErrorCode MatInvertVariableBlockDiagonal_SeqAIJ(Mat A, PetscInt nblocks, const PetscInt *bsizes, PetscScalar *diag)
1745: {
1746: PetscInt n = A->rmap->n, i, ncnt = 0, *indx, j, bsizemax = 0, *v_pivots;
1747: PetscBool allowzeropivot, zeropivotdetected = PETSC_FALSE;
1748: const PetscReal shift = 0.0;
1749: PetscInt ipvt[5];
1750: PetscCount flops = 0;
1751: PetscScalar work[25], *v_work;
1753: PetscFunctionBegin;
1754: allowzeropivot = PetscNot(A->erroriffailure);
1755: for (i = 0; i < nblocks; i++) ncnt += bsizes[i];
1756: PetscCheck(ncnt == n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Total blocksizes %" PetscInt_FMT " doesn't match number matrix rows %" PetscInt_FMT, ncnt, n);
1757: for (i = 0; i < nblocks; i++) bsizemax = PetscMax(bsizemax, bsizes[i]);
1758: PetscCall(PetscMalloc1(bsizemax, &indx));
1759: if (bsizemax > 7) PetscCall(PetscMalloc2(bsizemax, &v_work, bsizemax, &v_pivots));
1760: ncnt = 0;
1761: for (i = 0; i < nblocks; i++) {
1762: for (j = 0; j < bsizes[i]; j++) indx[j] = ncnt + j;
1763: PetscCall(MatGetValues(A, bsizes[i], indx, bsizes[i], indx, diag));
1764: switch (bsizes[i]) {
1765: case 1:
1766: *diag = 1.0 / (*diag);
1767: break;
1768: case 2:
1769: PetscCall(PetscKernel_A_gets_inverse_A_2(diag, shift, allowzeropivot, &zeropivotdetected));
1770: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1771: PetscCall(PetscKernel_A_gets_transpose_A_2(diag));
1772: break;
1773: case 3:
1774: PetscCall(PetscKernel_A_gets_inverse_A_3(diag, shift, allowzeropivot, &zeropivotdetected));
1775: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1776: PetscCall(PetscKernel_A_gets_transpose_A_3(diag));
1777: break;
1778: case 4:
1779: PetscCall(PetscKernel_A_gets_inverse_A_4(diag, shift, allowzeropivot, &zeropivotdetected));
1780: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1781: PetscCall(PetscKernel_A_gets_transpose_A_4(diag));
1782: break;
1783: case 5:
1784: PetscCall(PetscKernel_A_gets_inverse_A_5(diag, ipvt, work, shift, allowzeropivot, &zeropivotdetected));
1785: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1786: PetscCall(PetscKernel_A_gets_transpose_A_5(diag));
1787: break;
1788: case 6:
1789: PetscCall(PetscKernel_A_gets_inverse_A_6(diag, shift, allowzeropivot, &zeropivotdetected));
1790: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1791: PetscCall(PetscKernel_A_gets_transpose_A_6(diag));
1792: break;
1793: case 7:
1794: PetscCall(PetscKernel_A_gets_inverse_A_7(diag, shift, allowzeropivot, &zeropivotdetected));
1795: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1796: PetscCall(PetscKernel_A_gets_transpose_A_7(diag));
1797: break;
1798: default:
1799: PetscCall(PetscKernel_A_gets_inverse_A(bsizes[i], diag, v_pivots, v_work, allowzeropivot, &zeropivotdetected));
1800: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1801: PetscCall(PetscKernel_A_gets_transpose_A_N(diag, bsizes[i]));
1802: }
1803: ncnt += bsizes[i];
1804: diag += bsizes[i] * bsizes[i];
1805: flops += 2 * PetscPowInt64(bsizes[i], 3) / 3;
1806: }
1807: PetscCall(PetscLogFlops(flops));
1808: if (bsizemax > 7) PetscCall(PetscFree2(v_work, v_pivots));
1809: PetscCall(PetscFree(indx));
1810: PetscFunctionReturn(PETSC_SUCCESS);
1811: }
1813: /*
1814: Negative shift indicates do not generate an error if there is a zero diagonal, just invert it anyways
1815: */
1816: static PetscErrorCode MatInvertDiagonalForSOR_SeqAIJ(Mat A, PetscScalar omega, PetscScalar fshift)
1817: {
1818: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
1819: PetscInt i, m = A->rmap->n;
1820: const MatScalar *v;
1821: PetscScalar *idiag, *mdiag;
1822: PetscBool diagDense;
1823: const PetscInt *diag;
1825: PetscFunctionBegin;
1826: if (a->idiagState == ((PetscObject)A)->state && a->omega == omega && a->fshift == fshift) PetscFunctionReturn(PETSC_SUCCESS);
1827: PetscCall(MatGetDiagonalMarkers_SeqAIJ(A, &diag, &diagDense));
1828: PetscCheck(diagDense, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Matrix must have all diagonal locations to invert them");
1829: if (!a->idiag) PetscCall(PetscMalloc3(m, &a->idiag, m, &a->mdiag, m, &a->ssor_work));
1831: mdiag = a->mdiag;
1832: idiag = a->idiag;
1833: PetscCall(MatSeqAIJGetArrayRead(A, &v));
1834: if (omega == 1.0 && PetscRealPart(fshift) <= 0.0) {
1835: for (i = 0; i < m; i++) {
1836: mdiag[i] = v[diag[i]];
1837: if (!PetscAbsScalar(mdiag[i])) { /* zero diagonal */
1838: PetscCheck(PetscRealPart(fshift), PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Zero diagonal on row %" PetscInt_FMT, i);
1839: PetscCall(PetscInfo(A, "Zero diagonal on row %" PetscInt_FMT "\n", i));
1840: A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1841: A->factorerror_zeropivot_value = 0.0;
1842: A->factorerror_zeropivot_row = i;
1843: }
1844: idiag[i] = 1.0 / v[diag[i]];
1845: }
1846: PetscCall(PetscLogFlops(m));
1847: } else {
1848: for (i = 0; i < m; i++) {
1849: mdiag[i] = v[diag[i]];
1850: idiag[i] = omega / (fshift + v[diag[i]]);
1851: }
1852: PetscCall(PetscLogFlops(2.0 * m));
1853: }
1854: PetscCall(MatSeqAIJRestoreArrayRead(A, &v));
1855: a->idiagState = ((PetscObject)A)->state;
1856: a->omega = omega;
1857: a->fshift = fshift;
1858: PetscFunctionReturn(PETSC_SUCCESS);
1859: }
1861: PetscErrorCode MatSOR_SeqAIJ(Mat A, Vec bb, PetscReal omega, MatSORType flag, PetscReal fshift, PetscInt its, PetscInt lits, Vec xx)
1862: {
1863: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
1864: PetscScalar *x, d, sum, *t, scale;
1865: const MatScalar *v, *idiag = NULL, *mdiag, *aa;
1866: const PetscScalar *b, *bs, *xb, *ts;
1867: PetscInt n, m = A->rmap->n, i;
1868: const PetscInt *idx, *diag;
1870: PetscFunctionBegin;
1871: if (a->inode.use && a->inode.checked && omega == 1.0 && fshift == 0.0) {
1872: PetscCall(MatSOR_SeqAIJ_Inode(A, bb, omega, flag, fshift, its, lits, xx));
1873: PetscFunctionReturn(PETSC_SUCCESS);
1874: }
1875: its = its * lits;
1876: PetscCall(MatInvertDiagonalForSOR_SeqAIJ(A, omega, fshift));
1877: PetscCall(MatGetDiagonalMarkers_SeqAIJ(A, &diag, NULL));
1878: t = a->ssor_work;
1879: idiag = a->idiag;
1880: mdiag = a->mdiag;
1882: PetscCall(MatSeqAIJGetArrayRead(A, &aa));
1883: PetscCall(VecGetArray(xx, &x));
1884: PetscCall(VecGetArrayRead(bb, &b));
1885: /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
1886: if (flag == SOR_APPLY_UPPER) {
1887: /* apply (U + D/omega) to the vector */
1888: bs = b;
1889: for (i = 0; i < m; i++) {
1890: d = fshift + mdiag[i];
1891: n = a->i[i + 1] - diag[i] - 1;
1892: idx = a->j + diag[i] + 1;
1893: v = aa + diag[i] + 1;
1894: sum = b[i] * d / omega;
1895: PetscSparseDensePlusDot(sum, bs, v, idx, n);
1896: x[i] = sum;
1897: }
1898: PetscCall(VecRestoreArray(xx, &x));
1899: PetscCall(VecRestoreArrayRead(bb, &b));
1900: PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
1901: PetscCall(PetscLogFlops(a->nz));
1902: PetscFunctionReturn(PETSC_SUCCESS);
1903: }
1905: PetscCheck(flag != SOR_APPLY_LOWER, PETSC_COMM_SELF, PETSC_ERR_SUP, "SOR_APPLY_LOWER is not implemented");
1906: if (flag & SOR_EISENSTAT) {
1907: /* Let A = L + U + D; where L is lower triangular,
1908: U is upper triangular, E = D/omega; This routine applies
1910: (L + E)^{-1} A (U + E)^{-1}
1912: to a vector efficiently using Eisenstat's trick.
1913: */
1914: scale = (2.0 / omega) - 1.0;
1916: /* x = (E + U)^{-1} b */
1917: for (i = m - 1; i >= 0; i--) {
1918: n = a->i[i + 1] - diag[i] - 1;
1919: idx = a->j + diag[i] + 1;
1920: v = aa + diag[i] + 1;
1921: sum = b[i];
1922: PetscSparseDenseMinusDot(sum, x, v, idx, n);
1923: x[i] = sum * idiag[i];
1924: }
1926: /* t = b - (2*E - D)x */
1927: v = aa;
1928: for (i = 0; i < m; i++) t[i] = b[i] - scale * v[*diag++] * x[i];
1930: /* t = (E + L)^{-1}t */
1931: ts = t;
1932: diag = a->diag;
1933: for (i = 0; i < m; i++) {
1934: n = diag[i] - a->i[i];
1935: idx = a->j + a->i[i];
1936: v = aa + a->i[i];
1937: sum = t[i];
1938: PetscSparseDenseMinusDot(sum, ts, v, idx, n);
1939: t[i] = sum * idiag[i];
1940: /* x = x + t */
1941: x[i] += t[i];
1942: }
1944: PetscCall(PetscLogFlops(6.0 * m - 1 + 2.0 * a->nz));
1945: PetscCall(VecRestoreArray(xx, &x));
1946: PetscCall(VecRestoreArrayRead(bb, &b));
1947: PetscFunctionReturn(PETSC_SUCCESS);
1948: }
1949: if (flag & SOR_ZERO_INITIAL_GUESS) {
1950: if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
1951: for (i = 0; i < m; i++) {
1952: n = diag[i] - a->i[i];
1953: idx = a->j + a->i[i];
1954: v = aa + a->i[i];
1955: sum = b[i];
1956: PetscSparseDenseMinusDot(sum, x, v, idx, n);
1957: t[i] = sum;
1958: x[i] = sum * idiag[i];
1959: }
1960: xb = t;
1961: PetscCall(PetscLogFlops(a->nz));
1962: } else xb = b;
1963: if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
1964: for (i = m - 1; i >= 0; i--) {
1965: n = a->i[i + 1] - diag[i] - 1;
1966: idx = a->j + diag[i] + 1;
1967: v = aa + diag[i] + 1;
1968: sum = xb[i];
1969: PetscSparseDenseMinusDot(sum, x, v, idx, n);
1970: if (xb == b) {
1971: x[i] = sum * idiag[i];
1972: } else {
1973: x[i] = (1 - omega) * x[i] + sum * idiag[i]; /* omega in idiag */
1974: }
1975: }
1976: PetscCall(PetscLogFlops(a->nz)); /* assumes 1/2 in upper */
1977: }
1978: its--;
1979: }
1980: while (its--) {
1981: if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
1982: for (i = 0; i < m; i++) {
1983: /* lower */
1984: n = diag[i] - a->i[i];
1985: idx = a->j + a->i[i];
1986: v = aa + a->i[i];
1987: sum = b[i];
1988: PetscSparseDenseMinusDot(sum, x, v, idx, n);
1989: t[i] = sum; /* save application of the lower-triangular part */
1990: /* upper */
1991: n = a->i[i + 1] - diag[i] - 1;
1992: idx = a->j + diag[i] + 1;
1993: v = aa + diag[i] + 1;
1994: PetscSparseDenseMinusDot(sum, x, v, idx, n);
1995: x[i] = (1. - omega) * x[i] + sum * idiag[i]; /* omega in idiag */
1996: }
1997: xb = t;
1998: PetscCall(PetscLogFlops(2.0 * a->nz));
1999: } else xb = b;
2000: if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
2001: for (i = m - 1; i >= 0; i--) {
2002: sum = xb[i];
2003: if (xb == b) {
2004: /* whole matrix (no checkpointing available) */
2005: n = a->i[i + 1] - a->i[i];
2006: idx = a->j + a->i[i];
2007: v = aa + a->i[i];
2008: PetscSparseDenseMinusDot(sum, x, v, idx, n);
2009: x[i] = (1. - omega) * x[i] + (sum + mdiag[i] * x[i]) * idiag[i];
2010: } else { /* lower-triangular part has been saved, so only apply upper-triangular */
2011: n = a->i[i + 1] - diag[i] - 1;
2012: idx = a->j + diag[i] + 1;
2013: v = aa + diag[i] + 1;
2014: PetscSparseDenseMinusDot(sum, x, v, idx, n);
2015: x[i] = (1. - omega) * x[i] + sum * idiag[i]; /* omega in idiag */
2016: }
2017: }
2018: if (xb == b) PetscCall(PetscLogFlops(2.0 * a->nz));
2019: else PetscCall(PetscLogFlops(a->nz)); /* assumes 1/2 in upper */
2020: }
2021: }
2022: PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
2023: PetscCall(VecRestoreArray(xx, &x));
2024: PetscCall(VecRestoreArrayRead(bb, &b));
2025: PetscFunctionReturn(PETSC_SUCCESS);
2026: }
2028: static PetscErrorCode MatGetInfo_SeqAIJ(Mat A, MatInfoType flag, MatInfo *info)
2029: {
2030: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
2032: PetscFunctionBegin;
2033: info->block_size = 1.0;
2034: info->nz_allocated = a->maxnz;
2035: info->nz_used = a->nz;
2036: info->nz_unneeded = (a->maxnz - a->nz);
2037: info->assemblies = A->num_ass;
2038: info->mallocs = A->info.mallocs;
2039: info->memory = 0; /* REVIEW ME */
2040: if (A->factortype) {
2041: info->fill_ratio_given = A->info.fill_ratio_given;
2042: info->fill_ratio_needed = A->info.fill_ratio_needed;
2043: info->factor_mallocs = A->info.factor_mallocs;
2044: } else {
2045: info->fill_ratio_given = 0;
2046: info->fill_ratio_needed = 0;
2047: info->factor_mallocs = 0;
2048: }
2049: PetscFunctionReturn(PETSC_SUCCESS);
2050: }
2052: static PetscErrorCode MatZeroRows_SeqAIJ(Mat A, PetscInt N, const PetscInt rows[], PetscScalar diagv, Vec x, Vec b)
2053: {
2054: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
2055: PetscInt i, m = A->rmap->n - 1;
2056: const PetscScalar *xx;
2057: PetscScalar *bb, *aa;
2058: PetscInt d = 0;
2059: const PetscInt *diag;
2061: PetscFunctionBegin;
2062: if (x && b) {
2063: PetscCall(VecGetArrayRead(x, &xx));
2064: PetscCall(VecGetArray(b, &bb));
2065: for (i = 0; i < N; i++) {
2066: PetscCheck(rows[i] >= 0 && rows[i] <= m, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "row %" PetscInt_FMT " out of range", rows[i]);
2067: if (rows[i] >= A->cmap->n) continue;
2068: bb[rows[i]] = diagv * xx[rows[i]];
2069: }
2070: PetscCall(VecRestoreArrayRead(x, &xx));
2071: PetscCall(VecRestoreArray(b, &bb));
2072: }
2074: PetscCall(MatGetDiagonalMarkers_SeqAIJ(A, &diag, NULL));
2075: PetscCall(MatSeqAIJGetArray(A, &aa));
2076: if (a->keepnonzeropattern) {
2077: for (i = 0; i < N; i++) {
2078: PetscCheck(rows[i] >= 0 && rows[i] <= m, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "row %" PetscInt_FMT " out of range", rows[i]);
2079: PetscCall(PetscArrayzero(&aa[a->i[rows[i]]], a->ilen[rows[i]]));
2080: }
2081: if (diagv != 0.0) {
2082: for (i = 0; i < N; i++) {
2083: d = rows[i];
2084: if (d >= A->cmap->n) continue;
2085: PetscCheck(diag[d] < a->i[d + 1], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Matrix is missing diagonal entry in the zeroed row %" PetscInt_FMT, d);
2086: aa[diag[d]] = diagv;
2087: }
2088: }
2089: } else {
2090: if (diagv != 0.0) {
2091: for (i = 0; i < N; i++) {
2092: PetscCheck(rows[i] >= 0 && rows[i] <= m, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "row %" PetscInt_FMT " out of range", rows[i]);
2093: if (a->ilen[rows[i]] > 0) {
2094: if (rows[i] >= A->cmap->n) {
2095: a->ilen[rows[i]] = 0;
2096: } else {
2097: a->ilen[rows[i]] = 1;
2098: aa[a->i[rows[i]]] = diagv;
2099: a->j[a->i[rows[i]]] = rows[i];
2100: }
2101: } else if (rows[i] < A->cmap->n) { /* in case row was completely empty */
2102: PetscCall(MatSetValues_SeqAIJ(A, 1, &rows[i], 1, &rows[i], &diagv, INSERT_VALUES));
2103: }
2104: }
2105: } else {
2106: for (i = 0; i < N; i++) {
2107: PetscCheck(rows[i] >= 0 && rows[i] <= m, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "row %" PetscInt_FMT " out of range", rows[i]);
2108: a->ilen[rows[i]] = 0;
2109: }
2110: }
2111: if (N) A->nonzerostate++;
2112: }
2113: PetscCall(MatSeqAIJRestoreArray(A, &aa));
2114: PetscUseTypeMethod(A, assemblyend, MAT_FINAL_ASSEMBLY);
2115: PetscFunctionReturn(PETSC_SUCCESS);
2116: }
2118: static PetscErrorCode MatZeroRowsColumns_SeqAIJ(Mat A, PetscInt N, const PetscInt rows[], PetscScalar diagv, Vec x, Vec b)
2119: {
2120: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
2121: PetscInt i, j, m = A->rmap->n - 1;
2122: PetscBool *zeroed, vecs = PETSC_FALSE;
2123: const PetscScalar *xx;
2124: PetscScalar *bb, *aa;
2125: const PetscInt *diag;
2126: PetscBool diagDense;
2128: PetscFunctionBegin;
2129: if (!N) PetscFunctionReturn(PETSC_SUCCESS);
2130: PetscCall(MatGetDiagonalMarkers_SeqAIJ(A, &diag, &diagDense));
2131: PetscCall(MatSeqAIJGetArray(A, &aa));
2132: if (x && b) {
2133: PetscCall(VecGetArrayRead(x, &xx));
2134: PetscCall(VecGetArray(b, &bb));
2135: vecs = PETSC_TRUE;
2136: }
2137: PetscCall(PetscCalloc1(A->rmap->n, &zeroed));
2138: for (i = 0; i < N; i++) {
2139: PetscCheck(rows[i] >= 0 && rows[i] <= m, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "row %" PetscInt_FMT " out of range", rows[i]);
2140: PetscCall(PetscArrayzero(PetscSafePointerPlusOffset(aa, a->i[rows[i]]), a->ilen[rows[i]]));
2142: zeroed[rows[i]] = PETSC_TRUE;
2143: }
2144: for (i = 0; i < A->rmap->n; i++) {
2145: if (!zeroed[i]) {
2146: for (j = a->i[i]; j < a->i[i + 1]; j++) {
2147: if (a->j[j] < A->rmap->n && zeroed[a->j[j]]) {
2148: if (vecs) bb[i] -= aa[j] * xx[a->j[j]];
2149: aa[j] = 0.0;
2150: }
2151: }
2152: } else if (vecs && i < A->cmap->N) bb[i] = diagv * xx[i];
2153: }
2154: if (x && b) {
2155: PetscCall(VecRestoreArrayRead(x, &xx));
2156: PetscCall(VecRestoreArray(b, &bb));
2157: }
2158: PetscCall(PetscFree(zeroed));
2159: if (diagv != 0.0) {
2160: if (!diagDense) {
2161: for (i = 0; i < N; i++) {
2162: if (rows[i] >= A->cmap->N || rows[i] < 0) continue;
2163: PetscCall(MatSetValues_SeqAIJ(A, 1, &rows[i], 1, &rows[i], &diagv, INSERT_VALUES));
2164: }
2165: } else {
2166: for (i = 0; i < N; i++) aa[diag[rows[i]]] = diagv;
2167: }
2168: }
2169: PetscCall(MatSeqAIJRestoreArray(A, &aa));
2170: if (!diagDense) PetscUseTypeMethod(A, assemblyend, MAT_FINAL_ASSEMBLY);
2171: PetscFunctionReturn(PETSC_SUCCESS);
2172: }
2174: PetscErrorCode MatGetRow_SeqAIJ(Mat A, PetscInt row, PetscInt *nz, PetscInt **idx, PetscScalar **v)
2175: {
2176: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
2177: const PetscScalar *aa;
2179: PetscFunctionBegin;
2180: PetscCall(MatSeqAIJGetArrayRead(A, &aa));
2181: *nz = a->i[row + 1] - a->i[row];
2182: if (v) *v = PetscSafePointerPlusOffset((PetscScalar *)aa, a->i[row]);
2183: if (idx) {
2184: if (*nz && a->j) *idx = a->j + a->i[row];
2185: else *idx = NULL;
2186: }
2187: PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
2188: PetscFunctionReturn(PETSC_SUCCESS);
2189: }
2191: PetscErrorCode MatRestoreRow_SeqAIJ(Mat A, PetscInt row, PetscInt *nz, PetscInt **idx, PetscScalar **v)
2192: {
2193: PetscFunctionBegin;
2194: PetscFunctionReturn(PETSC_SUCCESS);
2195: }
2197: static PetscErrorCode MatNorm_SeqAIJ(Mat A, NormType type, PetscReal *nrm)
2198: {
2199: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
2200: const MatScalar *v;
2201: PetscReal sum = 0.0;
2202: PetscInt i, j;
2204: PetscFunctionBegin;
2205: PetscCall(MatSeqAIJGetArrayRead(A, &v));
2206: if (type == NORM_FROBENIUS) {
2207: #if PetscDefined(USE_REAL___FP16)
2208: PetscBLASInt one = 1, nz = a->nz;
2209: PetscCallBLAS("BLASnrm2", *nrm = BLASnrm2_(&nz, v, &one));
2210: #else
2211: for (i = 0; i < a->nz; i++) {
2212: sum += PetscRealPart(PetscConj(*v) * (*v));
2213: v++;
2214: }
2215: *nrm = PetscSqrtReal(sum);
2216: #endif
2217: PetscCall(PetscLogFlops(2.0 * a->nz));
2218: } else if (type == NORM_1) {
2219: PetscReal *tmp;
2220: PetscInt *jj = a->j;
2221: PetscCall(PetscCalloc1(A->cmap->n, &tmp));
2222: *nrm = 0.0;
2223: for (j = 0; j < a->nz; j++) {
2224: tmp[*jj++] += PetscAbsScalar(*v);
2225: v++;
2226: }
2227: for (j = 0; j < A->cmap->n; j++) {
2228: if (tmp[j] > *nrm) *nrm = tmp[j];
2229: }
2230: PetscCall(PetscFree(tmp));
2231: PetscCall(PetscLogFlops(a->nz));
2232: } else if (type == NORM_INFINITY) {
2233: *nrm = 0.0;
2234: for (j = 0; j < A->rmap->n; j++) {
2235: const PetscScalar *v2 = PetscSafePointerPlusOffset(v, a->i[j]);
2236: sum = 0.0;
2237: for (i = 0; i < a->i[j + 1] - a->i[j]; i++) {
2238: sum += PetscAbsScalar(*v2);
2239: v2++;
2240: }
2241: if (sum > *nrm) *nrm = sum;
2242: }
2243: PetscCall(PetscLogFlops(a->nz));
2244: } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "No support for two norm");
2245: PetscCall(MatSeqAIJRestoreArrayRead(A, &v));
2246: PetscFunctionReturn(PETSC_SUCCESS);
2247: }
2249: static PetscErrorCode MatIsTranspose_SeqAIJ(Mat A, Mat B, PetscReal tol, PetscBool *f)
2250: {
2251: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data, *bij = (Mat_SeqAIJ *)B->data;
2252: PetscInt *adx, *bdx, *aii, *bii, *aptr, *bptr;
2253: const MatScalar *va, *vb;
2254: PetscInt ma, na, mb, nb, i;
2256: PetscFunctionBegin;
2257: PetscCall(MatGetSize(A, &ma, &na));
2258: PetscCall(MatGetSize(B, &mb, &nb));
2259: if (ma != nb || na != mb) {
2260: *f = PETSC_FALSE;
2261: PetscFunctionReturn(PETSC_SUCCESS);
2262: }
2263: PetscCall(MatSeqAIJGetArrayRead(A, &va));
2264: PetscCall(MatSeqAIJGetArrayRead(B, &vb));
2265: aii = aij->i;
2266: bii = bij->i;
2267: adx = aij->j;
2268: bdx = bij->j;
2269: PetscCall(PetscMalloc1(ma, &aptr));
2270: PetscCall(PetscMalloc1(mb, &bptr));
2271: for (i = 0; i < ma; i++) aptr[i] = aii[i];
2272: for (i = 0; i < mb; i++) bptr[i] = bii[i];
2274: *f = PETSC_TRUE;
2275: for (i = 0; i < ma; i++) {
2276: while (aptr[i] < aii[i + 1]) {
2277: PetscInt idc, idr;
2278: PetscScalar vc, vr;
2279: /* column/row index/value */
2280: idc = adx[aptr[i]];
2281: idr = bdx[bptr[idc]];
2282: vc = va[aptr[i]];
2283: vr = vb[bptr[idc]];
2284: if (i != idr || PetscAbsScalar(vc - vr) > tol) {
2285: *f = PETSC_FALSE;
2286: goto done;
2287: } else {
2288: aptr[i]++;
2289: if (B || i != idc) bptr[idc]++;
2290: }
2291: }
2292: }
2293: done:
2294: PetscCall(PetscFree(aptr));
2295: PetscCall(PetscFree(bptr));
2296: PetscCall(MatSeqAIJRestoreArrayRead(A, &va));
2297: PetscCall(MatSeqAIJRestoreArrayRead(B, &vb));
2298: PetscFunctionReturn(PETSC_SUCCESS);
2299: }
2301: static PetscErrorCode MatIsHermitianTranspose_SeqAIJ(Mat A, Mat B, PetscReal tol, PetscBool *f)
2302: {
2303: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data, *bij = (Mat_SeqAIJ *)B->data;
2304: PetscInt *adx, *bdx, *aii, *bii, *aptr, *bptr;
2305: MatScalar *va, *vb;
2306: PetscInt ma, na, mb, nb, i;
2308: PetscFunctionBegin;
2309: PetscCall(MatGetSize(A, &ma, &na));
2310: PetscCall(MatGetSize(B, &mb, &nb));
2311: if (ma != nb || na != mb) {
2312: *f = PETSC_FALSE;
2313: PetscFunctionReturn(PETSC_SUCCESS);
2314: }
2315: aii = aij->i;
2316: bii = bij->i;
2317: adx = aij->j;
2318: bdx = bij->j;
2319: va = aij->a;
2320: vb = bij->a;
2321: PetscCall(PetscMalloc1(ma, &aptr));
2322: PetscCall(PetscMalloc1(mb, &bptr));
2323: for (i = 0; i < ma; i++) aptr[i] = aii[i];
2324: for (i = 0; i < mb; i++) bptr[i] = bii[i];
2326: *f = PETSC_TRUE;
2327: for (i = 0; i < ma; i++) {
2328: while (aptr[i] < aii[i + 1]) {
2329: PetscInt idc, idr;
2330: PetscScalar vc, vr;
2331: /* column/row index/value */
2332: idc = adx[aptr[i]];
2333: idr = bdx[bptr[idc]];
2334: vc = va[aptr[i]];
2335: vr = vb[bptr[idc]];
2336: if (i != idr || PetscAbsScalar(vc - PetscConj(vr)) > tol) {
2337: *f = PETSC_FALSE;
2338: goto done;
2339: } else {
2340: aptr[i]++;
2341: if (B || i != idc) bptr[idc]++;
2342: }
2343: }
2344: }
2345: done:
2346: PetscCall(PetscFree(aptr));
2347: PetscCall(PetscFree(bptr));
2348: PetscFunctionReturn(PETSC_SUCCESS);
2349: }
2351: PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A, Vec ll, Vec rr)
2352: {
2353: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
2354: const PetscScalar *l, *r;
2355: PetscScalar x;
2356: MatScalar *v;
2357: PetscInt i, j, m = A->rmap->n, n = A->cmap->n, M, nz = a->nz;
2358: const PetscInt *jj;
2360: PetscFunctionBegin;
2361: if (ll) {
2362: /* The local size is used so that VecMPI can be passed to this routine
2363: by MatDiagonalScale_MPIAIJ */
2364: PetscCall(VecGetLocalSize(ll, &m));
2365: PetscCheck(m == A->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Left scaling vector wrong length");
2366: PetscCall(VecGetArrayRead(ll, &l));
2367: PetscCall(MatSeqAIJGetArray(A, &v));
2368: for (i = 0; i < m; i++) {
2369: x = l[i];
2370: M = a->i[i + 1] - a->i[i];
2371: for (j = 0; j < M; j++) (*v++) *= x;
2372: }
2373: PetscCall(VecRestoreArrayRead(ll, &l));
2374: PetscCall(PetscLogFlops(nz));
2375: PetscCall(MatSeqAIJRestoreArray(A, &v));
2376: }
2377: if (rr) {
2378: PetscCall(VecGetLocalSize(rr, &n));
2379: PetscCheck(n == A->cmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Right scaling vector wrong length");
2380: PetscCall(VecGetArrayRead(rr, &r));
2381: PetscCall(MatSeqAIJGetArray(A, &v));
2382: jj = a->j;
2383: for (i = 0; i < nz; i++) (*v++) *= r[*jj++];
2384: PetscCall(MatSeqAIJRestoreArray(A, &v));
2385: PetscCall(VecRestoreArrayRead(rr, &r));
2386: PetscCall(PetscLogFlops(nz));
2387: }
2388: PetscFunctionReturn(PETSC_SUCCESS);
2389: }
2391: PetscErrorCode MatCreateSubMatrix_SeqAIJ(Mat A, IS isrow, IS iscol, PetscInt csize, MatReuse scall, Mat *B)
2392: {
2393: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data, *c;
2394: PetscInt *smap, i, k, kstart, kend, oldcols = A->cmap->n, *lens;
2395: PetscInt row, mat_i, *mat_j, tcol, first, step, *mat_ilen, sum, lensi;
2396: const PetscInt *irow, *icol;
2397: const PetscScalar *aa;
2398: PetscInt nrows, ncols;
2399: PetscInt *starts, *j_new, *i_new, *aj = a->j, *ai = a->i, ii, *ailen = a->ilen;
2400: MatScalar *a_new, *mat_a, *c_a;
2401: Mat C;
2402: PetscBool stride;
2404: PetscFunctionBegin;
2405: PetscCall(ISGetIndices(isrow, &irow));
2406: PetscCall(ISGetLocalSize(isrow, &nrows));
2407: PetscCall(ISGetLocalSize(iscol, &ncols));
2409: PetscCall(PetscObjectTypeCompare((PetscObject)iscol, ISSTRIDE, &stride));
2410: if (stride) {
2411: PetscCall(ISStrideGetInfo(iscol, &first, &step));
2412: } else {
2413: first = 0;
2414: step = 0;
2415: }
2416: if (stride && step == 1) {
2417: /* special case of contiguous rows */
2418: PetscCall(PetscMalloc2(nrows, &lens, nrows, &starts));
2419: /* loop over new rows determining lens and starting points */
2420: for (i = 0; i < nrows; i++) {
2421: kstart = ai[irow[i]];
2422: kend = kstart + ailen[irow[i]];
2423: starts[i] = kstart;
2424: for (k = kstart; k < kend; k++) {
2425: if (aj[k] >= first) {
2426: starts[i] = k;
2427: break;
2428: }
2429: }
2430: sum = 0;
2431: while (k < kend) {
2432: if (aj[k++] >= first + ncols) break;
2433: sum++;
2434: }
2435: lens[i] = sum;
2436: }
2437: /* create submatrix */
2438: if (scall == MAT_REUSE_MATRIX) {
2439: PetscInt n_cols, n_rows;
2440: PetscCall(MatGetSize(*B, &n_rows, &n_cols));
2441: PetscCheck(n_rows == nrows && n_cols == ncols, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Reused submatrix wrong size");
2442: PetscCall(MatZeroEntries(*B));
2443: C = *B;
2444: } else {
2445: PetscInt rbs, cbs;
2446: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &C));
2447: PetscCall(MatSetSizes(C, nrows, ncols, PETSC_DETERMINE, PETSC_DETERMINE));
2448: PetscCall(ISGetBlockSize(isrow, &rbs));
2449: PetscCall(ISGetBlockSize(iscol, &cbs));
2450: PetscCall(MatSetBlockSizes(C, rbs, cbs));
2451: PetscCall(MatSetType(C, ((PetscObject)A)->type_name));
2452: PetscCall(MatSetOption(C, MAT_STRUCTURE_ONLY, A->structure_only));
2453: PetscCall(MatSeqAIJSetPreallocation_SeqAIJ(C, 0, lens));
2454: }
2455: c = (Mat_SeqAIJ *)C->data;
2457: /* loop over rows inserting into submatrix */
2458: j_new = c->j;
2459: i_new = c->i;
2460: PetscCall(MatSeqAIJGetArrayWrite(C, &a_new)); // Not 'a_new = c->a-new', since that raw usage ignores offload state of C
2461: PetscCall(MatSeqAIJGetArrayRead(A, &aa));
2462: for (i = 0; i < nrows; i++) {
2463: ii = starts[i];
2464: lensi = lens[i];
2465: if (lensi) {
2466: for (k = 0; k < lensi; k++) *j_new++ = aj[ii + k] - first;
2467: if (!A->structure_only) {
2468: PetscCall(PetscArraycpy(a_new, aa + starts[i], lensi));
2469: a_new += lensi;
2470: }
2471: }
2472: i_new[i + 1] = i_new[i] + lensi;
2473: c->ilen[i] = lensi;
2474: }
2475: PetscCall(MatSeqAIJRestoreArrayWrite(C, &a_new)); // Set C's offload state properly
2476: PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
2477: PetscCall(PetscFree2(lens, starts));
2478: } else {
2479: PetscCall(ISGetIndices(iscol, &icol));
2480: PetscCall(PetscCalloc1(oldcols, &smap));
2481: PetscCall(PetscMalloc1(1 + nrows, &lens));
2482: for (i = 0; i < ncols; i++) {
2483: PetscCheck(icol[i] < oldcols, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Requesting column beyond largest column icol[%" PetscInt_FMT "] %" PetscInt_FMT " >= A->cmap->n %" PetscInt_FMT, i, icol[i], oldcols);
2484: smap[icol[i]] = i + 1;
2485: }
2487: /* determine lens of each row */
2488: for (i = 0; i < nrows; i++) {
2489: kstart = ai[irow[i]];
2490: kend = kstart + a->ilen[irow[i]];
2491: lens[i] = 0;
2492: for (k = kstart; k < kend; k++) {
2493: if (smap[aj[k]]) lens[i]++;
2494: }
2495: }
2496: /* Create and fill new matrix */
2497: if (scall == MAT_REUSE_MATRIX) {
2498: PetscBool equal;
2500: c = (Mat_SeqAIJ *)(*B)->data;
2501: PetscCheck((*B)->rmap->n == nrows && (*B)->cmap->n == ncols, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Cannot reuse matrix. wrong size");
2502: PetscCall(PetscArraycmp(c->ilen, lens, (*B)->rmap->n, &equal));
2503: PetscCheck(equal, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Cannot reuse matrix. wrong number of nonzeros");
2504: PetscCall(PetscArrayzero(c->ilen, (*B)->rmap->n));
2505: C = *B;
2506: } else {
2507: PetscInt rbs, cbs;
2508: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &C));
2509: PetscCall(MatSetSizes(C, nrows, ncols, PETSC_DETERMINE, PETSC_DETERMINE));
2510: PetscCall(ISGetBlockSize(isrow, &rbs));
2511: PetscCall(ISGetBlockSize(iscol, &cbs));
2512: if (rbs > 1 || cbs > 1) PetscCall(MatSetBlockSizes(C, rbs, cbs));
2513: PetscCall(MatSetType(C, ((PetscObject)A)->type_name));
2514: PetscCall(MatSetOption(C, MAT_STRUCTURE_ONLY, A->structure_only));
2515: PetscCall(MatSeqAIJSetPreallocation_SeqAIJ(C, 0, lens));
2516: }
2517: c = (Mat_SeqAIJ *)C->data;
2518: PetscCall(MatSeqAIJGetArrayRead(A, &aa));
2519: PetscCall(MatSeqAIJGetArrayWrite(C, &c_a)); // Not 'c->a', since that raw usage ignores offload state of C
2520: for (i = 0; i < nrows; i++) {
2521: row = irow[i];
2522: kstart = ai[row];
2523: kend = kstart + a->ilen[row];
2524: mat_i = c->i[i];
2525: mat_j = PetscSafePointerPlusOffset(c->j, mat_i);
2526: mat_a = PetscSafePointerPlusOffset(c_a, mat_i);
2527: mat_ilen = c->ilen + i;
2528: for (k = kstart; k < kend; k++) {
2529: if ((tcol = smap[a->j[k]])) {
2530: *mat_j++ = tcol - 1;
2531: if (!A->structure_only) *mat_a++ = aa[k];
2532: (*mat_ilen)++;
2533: }
2534: }
2535: }
2536: PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
2537: /* Free work space */
2538: PetscCall(ISRestoreIndices(iscol, &icol));
2539: PetscCall(PetscFree(smap));
2540: PetscCall(PetscFree(lens));
2541: /* sort */
2542: for (i = 0; i < nrows; i++) {
2543: PetscInt ilen;
2545: mat_i = c->i[i];
2546: mat_j = PetscSafePointerPlusOffset(c->j, mat_i);
2547: mat_a = PetscSafePointerPlusOffset(c_a, mat_i);
2548: ilen = c->ilen[i];
2549: if (A->structure_only) PetscCall(PetscSortInt(ilen, mat_j));
2550: else PetscCall(PetscSortIntWithScalarArray(ilen, mat_j, mat_a));
2551: }
2552: PetscCall(MatSeqAIJRestoreArrayWrite(C, &c_a));
2553: }
2554: #if PetscDefined(HAVE_DEVICE)
2555: PetscCall(MatBindToCPU(C, A->boundtocpu));
2556: #endif
2557: PetscCall(MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY));
2558: PetscCall(MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY));
2560: PetscCall(ISRestoreIndices(isrow, &irow));
2561: *B = C;
2562: PetscFunctionReturn(PETSC_SUCCESS);
2563: }
2565: static PetscErrorCode MatGetMultiProcBlock_SeqAIJ(Mat mat, MPI_Comm subComm, MatReuse scall, Mat *subMat)
2566: {
2567: Mat B;
2569: PetscFunctionBegin;
2570: if (scall == MAT_INITIAL_MATRIX) {
2571: PetscCall(MatCreate(subComm, &B));
2572: PetscCall(MatSetSizes(B, mat->rmap->n, mat->cmap->n, mat->rmap->n, mat->cmap->n));
2573: PetscCall(MatSetBlockSizesFromMats(B, mat, mat));
2574: PetscCall(MatSetType(B, MATSEQAIJ));
2575: PetscCall(MatDuplicateNoCreate_SeqAIJ(B, mat, MAT_COPY_VALUES, PETSC_TRUE));
2576: *subMat = B;
2577: } else {
2578: PetscCall(MatCopy_SeqAIJ(mat, *subMat, SAME_NONZERO_PATTERN));
2579: }
2580: PetscFunctionReturn(PETSC_SUCCESS);
2581: }
2583: static PetscErrorCode MatILUFactor_SeqAIJ(Mat inA, IS row, IS col, const MatFactorInfo *info)
2584: {
2585: Mat_SeqAIJ *a = (Mat_SeqAIJ *)inA->data;
2586: Mat outA;
2587: PetscBool row_identity, col_identity;
2589: PetscFunctionBegin;
2590: PetscCheck(info->levels == 0, PETSC_COMM_SELF, PETSC_ERR_SUP, "Only levels=0 supported for in-place ilu");
2592: PetscCall(ISIdentity(row, &row_identity));
2593: PetscCall(ISIdentity(col, &col_identity));
2595: outA = inA;
2596: PetscCall(PetscFree(inA->solvertype));
2597: PetscCall(PetscStrallocpy(MATSOLVERPETSC, &inA->solvertype));
2599: PetscCall(PetscObjectReference((PetscObject)row));
2600: PetscCall(ISDestroy(&a->row));
2602: a->row = row;
2604: PetscCall(PetscObjectReference((PetscObject)col));
2605: PetscCall(ISDestroy(&a->col));
2607: a->col = col;
2609: /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
2610: PetscCall(ISDestroy(&a->icol));
2611: PetscCall(ISInvertPermutation(col, PETSC_DECIDE, &a->icol));
2613: if (!a->solve_work) { /* this matrix may have been factored before */
2614: PetscCall(PetscMalloc1(inA->rmap->n, &a->solve_work));
2615: }
2617: if (row_identity && col_identity) {
2618: PetscCall(MatLUFactorNumeric_SeqAIJ_inplace(outA, inA, info));
2619: } else {
2620: PetscCall(MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA, inA, info));
2621: }
2622: outA->factortype = MAT_FACTOR_LU;
2623: PetscFunctionReturn(PETSC_SUCCESS);
2624: }
2626: PetscErrorCode MatScale_SeqAIJ(Mat inA, PetscScalar alpha)
2627: {
2628: Mat_SeqAIJ *a = (Mat_SeqAIJ *)inA->data;
2629: PetscScalar *v;
2630: PetscBLASInt one = 1, bnz;
2632: PetscFunctionBegin;
2633: PetscCall(MatSeqAIJGetArray(inA, &v));
2634: PetscCall(PetscBLASIntCast(a->nz, &bnz));
2635: PetscCallBLAS("BLASscal", BLASscal_(&bnz, &alpha, v, &one));
2636: PetscCall(PetscLogFlops(a->nz));
2637: PetscCall(MatSeqAIJRestoreArray(inA, &v));
2638: PetscFunctionReturn(PETSC_SUCCESS);
2639: }
2641: PetscErrorCode MatDestroySubMatrix_Private(Mat_SubSppt *submatj)
2642: {
2643: PetscInt i;
2645: PetscFunctionBegin;
2646: if (!submatj->id) { /* delete data that are linked only to submats[id=0] */
2647: PetscCall(PetscFree4(submatj->sbuf1, submatj->ptr, submatj->tmp, submatj->ctr));
2649: for (i = 0; i < submatj->nrqr; ++i) PetscCall(PetscFree(submatj->sbuf2[i]));
2650: PetscCall(PetscFree3(submatj->sbuf2, submatj->req_size, submatj->req_source1));
2652: if (submatj->rbuf1) {
2653: PetscCall(PetscFree(submatj->rbuf1[0]));
2654: PetscCall(PetscFree(submatj->rbuf1));
2655: }
2657: for (i = 0; i < submatj->nrqs; ++i) PetscCall(PetscFree(submatj->rbuf3[i]));
2658: PetscCall(PetscFree3(submatj->req_source2, submatj->rbuf2, submatj->rbuf3));
2659: PetscCall(PetscFree(submatj->pa));
2660: PetscCall(PetscFree2(submatj->local_a_parent, submatj->local_a_sub));
2661: PetscCall(PetscFree2(submatj->local_b_parent, submatj->local_b_sub));
2662: }
2664: #if PetscDefined(USE_CTABLE)
2665: PetscCall(PetscHMapIDestroy(&submatj->rmap));
2666: PetscCall(PetscFree(submatj->cmap_loc));
2667: PetscCall(PetscFree(submatj->rmap_loc));
2668: #else
2669: PetscCall(PetscFree(submatj->rmap));
2670: #endif
2672: if (!submatj->allcolumns) {
2673: #if PetscDefined(USE_CTABLE)
2674: PetscCall(PetscHMapIDestroy(&submatj->cmap));
2675: #else
2676: PetscCall(PetscFree(submatj->cmap));
2677: #endif
2678: }
2679: PetscCall(PetscFree(submatj->row2proc));
2681: PetscCall(PetscFree(submatj));
2682: PetscFunctionReturn(PETSC_SUCCESS);
2683: }
2685: PetscErrorCode MatDestroySubMatrix_SeqAIJ(Mat C)
2686: {
2687: Mat_SeqAIJ *c = (Mat_SeqAIJ *)C->data;
2688: Mat_SubSppt *submatj = c->submatis1;
2690: PetscFunctionBegin;
2691: PetscCall((*submatj->destroy)(C));
2692: PetscCall(MatDestroySubMatrix_Private(submatj));
2693: PetscFunctionReturn(PETSC_SUCCESS);
2694: }
2696: /* Note this has code duplication with MatDestroySubMatrices_SeqBAIJ() */
2697: static PetscErrorCode MatDestroySubMatrices_SeqAIJ(PetscInt n, Mat *mat[])
2698: {
2699: PetscInt i;
2700: Mat C;
2701: Mat_SeqAIJ *c;
2702: Mat_SubSppt *submatj;
2704: PetscFunctionBegin;
2705: for (i = 0; i < n; i++) {
2706: C = (*mat)[i];
2707: c = (Mat_SeqAIJ *)C->data;
2708: submatj = c->submatis1;
2709: if (submatj) {
2710: if (--((PetscObject)C)->refct <= 0) {
2711: PetscCall(PetscFree(C->factorprefix));
2712: PetscCall((*submatj->destroy)(C));
2713: PetscCall(MatDestroySubMatrix_Private(submatj));
2714: PetscCall(PetscFree(C->defaultvectype));
2715: PetscCall(PetscFree(C->defaultrandtype));
2716: PetscCall(PetscFree(C->solvertype));
2717: PetscCall(PetscLayoutDestroy(&C->rmap));
2718: PetscCall(PetscLayoutDestroy(&C->cmap));
2719: PetscCall(PetscHeaderDestroy(&C));
2720: }
2721: } else {
2722: PetscCall(MatDestroy(&C));
2723: }
2724: }
2726: /* Destroy Dummy submatrices created for reuse */
2727: PetscCall(MatDestroySubMatrices_Dummy(n, mat));
2729: PetscCall(PetscFree(*mat));
2730: PetscFunctionReturn(PETSC_SUCCESS);
2731: }
2733: static PetscErrorCode MatCreateSubMatrices_SeqAIJ(Mat A, PetscInt n, const IS irow[], const IS icol[], MatReuse scall, Mat *B[])
2734: {
2735: PetscInt i;
2737: PetscFunctionBegin;
2738: if (scall == MAT_INITIAL_MATRIX) PetscCall(PetscCalloc1(n + 1, B));
2740: for (i = 0; i < n; i++) PetscCall(MatCreateSubMatrix_SeqAIJ(A, irow[i], icol[i], PETSC_DECIDE, scall, &(*B)[i]));
2741: PetscFunctionReturn(PETSC_SUCCESS);
2742: }
2744: static PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A, PetscInt is_max, IS is[], PetscInt ov)
2745: {
2746: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
2747: PetscInt row, i, j, k, l, ll, m, n, *nidx, isz, val;
2748: const PetscInt *idx;
2749: PetscInt start, end, *ai, *aj, bs = A->rmap->bs == A->cmap->bs ? A->rmap->bs : 1;
2750: PetscBT table;
2752: PetscFunctionBegin;
2753: m = A->rmap->n / bs;
2754: ai = a->i;
2755: aj = a->j;
2757: PetscCheck(ov >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "illegal negative overlap value used");
2759: PetscCall(PetscMalloc1(m + 1, &nidx));
2760: PetscCall(PetscBTCreate(m, &table));
2762: for (i = 0; i < is_max; i++) {
2763: /* Initialize the two local arrays */
2764: isz = 0;
2765: PetscCall(PetscBTMemzero(m, table));
2767: /* Extract the indices, assume there can be duplicate entries */
2768: PetscCall(ISGetIndices(is[i], &idx));
2769: PetscCall(ISGetLocalSize(is[i], &n));
2771: if (bs > 1) {
2772: /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
2773: for (j = 0; j < n; ++j) {
2774: if (!PetscBTLookupSet(table, idx[j] / bs)) nidx[isz++] = idx[j] / bs;
2775: }
2776: PetscCall(ISRestoreIndices(is[i], &idx));
2777: PetscCall(ISDestroy(&is[i]));
2779: k = 0;
2780: for (j = 0; j < ov; j++) { /* for each overlap */
2781: n = isz;
2782: for (; k < n; k++) { /* do only those rows in nidx[k], which are not done yet */
2783: for (ll = 0; ll < bs; ll++) {
2784: row = bs * nidx[k] + ll;
2785: start = ai[row];
2786: end = ai[row + 1];
2787: for (l = start; l < end; l++) {
2788: val = aj[l] / bs;
2789: if (!PetscBTLookupSet(table, val)) nidx[isz++] = val;
2790: }
2791: }
2792: }
2793: }
2794: PetscCall(ISCreateBlock(PETSC_COMM_SELF, bs, isz, nidx, PETSC_COPY_VALUES, is + i));
2795: } else {
2796: /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
2797: for (j = 0; j < n; ++j) {
2798: if (!PetscBTLookupSet(table, idx[j])) nidx[isz++] = idx[j];
2799: }
2800: PetscCall(ISRestoreIndices(is[i], &idx));
2801: PetscCall(ISDestroy(&is[i]));
2803: k = 0;
2804: for (j = 0; j < ov; j++) { /* for each overlap */
2805: n = isz;
2806: for (; k < n; k++) { /* do only those rows in nidx[k], which are not done yet */
2807: row = nidx[k];
2808: start = ai[row];
2809: end = ai[row + 1];
2810: for (l = start; l < end; l++) {
2811: val = aj[l];
2812: if (!PetscBTLookupSet(table, val)) nidx[isz++] = val;
2813: }
2814: }
2815: }
2816: PetscCall(ISCreateGeneral(PETSC_COMM_SELF, isz, nidx, PETSC_COPY_VALUES, is + i));
2817: }
2818: }
2819: PetscCall(PetscBTDestroy(&table));
2820: PetscCall(PetscFree(nidx));
2821: PetscFunctionReturn(PETSC_SUCCESS);
2822: }
2824: static PetscErrorCode MatPermute_SeqAIJ(Mat A, IS rowp, IS colp, Mat *B)
2825: {
2826: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
2827: PetscInt i, nz = 0, m = A->rmap->n, n = A->cmap->n;
2828: const PetscInt *row, *col;
2829: PetscInt *cnew, j, *lens;
2830: IS icolp, irowp;
2831: PetscInt *cwork = NULL;
2832: PetscScalar *vwork = NULL;
2834: PetscFunctionBegin;
2835: PetscCall(ISInvertPermutation(rowp, PETSC_DECIDE, &irowp));
2836: PetscCall(ISGetIndices(irowp, &row));
2837: PetscCall(ISInvertPermutation(colp, PETSC_DECIDE, &icolp));
2838: PetscCall(ISGetIndices(icolp, &col));
2840: /* determine lengths of permuted rows */
2841: PetscCall(PetscMalloc1(m + 1, &lens));
2842: for (i = 0; i < m; i++) lens[row[i]] = a->i[i + 1] - a->i[i];
2843: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), B));
2844: PetscCall(MatSetSizes(*B, m, n, m, n));
2845: PetscCall(MatSetBlockSizesFromMats(*B, A, A));
2846: PetscCall(MatSetType(*B, ((PetscObject)A)->type_name));
2847: PetscCall(MatSeqAIJSetPreallocation_SeqAIJ(*B, 0, lens));
2848: PetscCall(PetscFree(lens));
2850: PetscCall(PetscMalloc1(n, &cnew));
2851: for (i = 0; i < m; i++) {
2852: PetscCall(MatGetRow_SeqAIJ(A, i, &nz, &cwork, &vwork));
2853: for (j = 0; j < nz; j++) cnew[j] = col[cwork[j]];
2854: PetscCall(MatSetValues_SeqAIJ(*B, 1, &row[i], nz, cnew, vwork, INSERT_VALUES));
2855: PetscCall(MatRestoreRow_SeqAIJ(A, i, &nz, &cwork, &vwork));
2856: }
2857: PetscCall(PetscFree(cnew));
2859: (*B)->assembled = PETSC_FALSE;
2861: #if PetscDefined(HAVE_DEVICE)
2862: PetscCall(MatBindToCPU(*B, A->boundtocpu));
2863: #endif
2864: PetscCall(MatAssemblyBegin(*B, MAT_FINAL_ASSEMBLY));
2865: PetscCall(MatAssemblyEnd(*B, MAT_FINAL_ASSEMBLY));
2866: PetscCall(ISRestoreIndices(irowp, &row));
2867: PetscCall(ISRestoreIndices(icolp, &col));
2868: PetscCall(ISDestroy(&irowp));
2869: PetscCall(ISDestroy(&icolp));
2870: if (rowp == colp) PetscCall(MatPropagateSymmetryOptions(A, *B));
2871: PetscFunctionReturn(PETSC_SUCCESS);
2872: }
2874: PetscErrorCode MatCopy_SeqAIJ(Mat A, Mat B, MatStructure str)
2875: {
2876: PetscFunctionBegin;
2877: /* If the two matrices have the same copy implementation, use fast copy. */
2878: if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
2879: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
2880: Mat_SeqAIJ *b = (Mat_SeqAIJ *)B->data;
2881: const PetscScalar *aa;
2882: PetscScalar *bb;
2884: PetscCall(MatSeqAIJGetArrayRead(A, &aa));
2885: PetscCall(MatSeqAIJGetArrayWrite(B, &bb));
2887: PetscCheck(a->i[A->rmap->n] == b->i[B->rmap->n], PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Number of nonzeros in two matrices are different %" PetscInt_FMT " != %" PetscInt_FMT, a->i[A->rmap->n], b->i[B->rmap->n]);
2888: PetscCall(PetscArraycpy(bb, aa, a->i[A->rmap->n]));
2889: PetscCall(PetscObjectStateIncrease((PetscObject)B));
2890: PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
2891: PetscCall(MatSeqAIJRestoreArrayWrite(B, &bb));
2892: } else {
2893: PetscCall(MatCopy_Basic(A, B, str));
2894: }
2895: PetscFunctionReturn(PETSC_SUCCESS);
2896: }
2898: PETSC_INTERN PetscErrorCode MatSeqAIJGetArray_SeqAIJ(Mat A, PetscScalar *array[])
2899: {
2900: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
2902: PetscFunctionBegin;
2903: *array = a->a;
2904: PetscFunctionReturn(PETSC_SUCCESS);
2905: }
2907: PETSC_INTERN PetscErrorCode MatSeqAIJRestoreArray_SeqAIJ(Mat A, PetscScalar *array[])
2908: {
2909: PetscFunctionBegin;
2910: *array = NULL;
2911: PetscFunctionReturn(PETSC_SUCCESS);
2912: }
2914: /*
2915: Computes the number of nonzeros per row needed for preallocation when X and Y
2916: have different nonzero structure.
2917: */
2918: PetscErrorCode MatAXPYGetPreallocation_SeqX_private(PetscInt m, const PetscInt *xi, const PetscInt *xj, const PetscInt *yi, const PetscInt *yj, PetscInt *nnz)
2919: {
2920: PetscInt i, j, k, nzx, nzy;
2922: PetscFunctionBegin;
2923: /* Set the number of nonzeros in the new matrix */
2924: for (i = 0; i < m; i++) {
2925: const PetscInt *xjj = PetscSafePointerPlusOffset(xj, xi[i]), *yjj = PetscSafePointerPlusOffset(yj, yi[i]);
2926: nzx = xi[i + 1] - xi[i];
2927: nzy = yi[i + 1] - yi[i];
2928: nnz[i] = 0;
2929: for (j = 0, k = 0; j < nzx; j++) { /* Point in X */
2930: for (; k < nzy && yjj[k] < xjj[j]; k++) nnz[i]++; /* Catch up to X */
2931: if (k < nzy && yjj[k] == xjj[j]) k++; /* Skip duplicate */
2932: nnz[i]++;
2933: }
2934: for (; k < nzy; k++) nnz[i]++;
2935: }
2936: PetscFunctionReturn(PETSC_SUCCESS);
2937: }
2939: PetscErrorCode MatAXPYGetPreallocation_SeqAIJ(Mat Y, Mat X, PetscInt *nnz)
2940: {
2941: PetscInt m = Y->rmap->N;
2942: Mat_SeqAIJ *x = (Mat_SeqAIJ *)X->data;
2943: Mat_SeqAIJ *y = (Mat_SeqAIJ *)Y->data;
2945: PetscFunctionBegin;
2946: /* Set the number of nonzeros in the new matrix */
2947: PetscCall(MatAXPYGetPreallocation_SeqX_private(m, x->i, x->j, y->i, y->j, nnz));
2948: PetscFunctionReturn(PETSC_SUCCESS);
2949: }
2951: PetscErrorCode MatAXPY_SeqAIJ(Mat Y, PetscScalar a, Mat X, MatStructure str)
2952: {
2953: Mat_SeqAIJ *x = (Mat_SeqAIJ *)X->data, *y = (Mat_SeqAIJ *)Y->data;
2955: PetscFunctionBegin;
2956: if (str == UNKNOWN_NONZERO_PATTERN || (PetscDefined(USE_DEBUG) && str == SAME_NONZERO_PATTERN)) {
2957: PetscBool e = x->nz == y->nz ? PETSC_TRUE : PETSC_FALSE;
2958: if (e) {
2959: PetscCall(PetscArraycmp(x->i, y->i, Y->rmap->n + 1, &e));
2960: if (e) {
2961: PetscCall(PetscArraycmp(x->j, y->j, y->nz, &e));
2962: if (e) str = SAME_NONZERO_PATTERN;
2963: }
2964: }
2965: if (!e) PetscCheck(str != SAME_NONZERO_PATTERN, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "MatStructure is not SAME_NONZERO_PATTERN");
2966: }
2967: if (str == SAME_NONZERO_PATTERN) {
2968: const PetscScalar *xa;
2969: PetscScalar *ya, alpha = a;
2970: PetscBLASInt one = 1, bnz;
2972: PetscCall(PetscBLASIntCast(x->nz, &bnz));
2973: PetscCall(MatSeqAIJGetArray(Y, &ya));
2974: PetscCall(MatSeqAIJGetArrayRead(X, &xa));
2975: PetscCallBLAS("BLASaxpy", BLASaxpy_(&bnz, &alpha, xa, &one, ya, &one));
2976: PetscCall(MatSeqAIJRestoreArrayRead(X, &xa));
2977: PetscCall(MatSeqAIJRestoreArray(Y, &ya));
2978: PetscCall(PetscLogFlops(2.0 * bnz));
2979: PetscCall(PetscObjectStateIncrease((PetscObject)Y));
2980: } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2981: PetscCall(MatAXPY_Basic(Y, a, X, str));
2982: } else {
2983: Mat B;
2984: PetscInt *nnz;
2985: PetscBool subset = PETSC_TRUE;
2987: PetscCall(PetscMalloc1(Y->rmap->N, &nnz));
2988: PetscCall(MatAXPYGetPreallocation_SeqAIJ(Y, X, nnz));
2989: /* nnz[i] is the size of the union of row i of X and Y, so the pattern of X is contained in that of Y when every nnz[i] equals
2990: the length of row i of Y. Adding in place then keeps the nonzero pattern of Y, and its nonzero state, unchanged. */
2991: for (PetscInt i = 0; i < Y->rmap->N && subset; i++) subset = (PetscBool)(nnz[i] == y->i[i + 1] - y->i[i]);
2992: if (subset) PetscCall(MatAXPY_Basic(Y, a, X, SUBSET_NONZERO_PATTERN));
2993: else {
2994: PetscCall(MatCreate(PetscObjectComm((PetscObject)Y), &B));
2995: PetscCall(PetscObjectSetName((PetscObject)B, ((PetscObject)Y)->name));
2996: PetscCall(MatSetLayouts(B, Y->rmap, Y->cmap));
2997: PetscCall(MatSetType(B, ((PetscObject)Y)->type_name));
2998: PetscCall(MatSeqAIJSetPreallocation(B, 0, nnz));
2999: PetscCall(MatAXPY_BasicWithPreallocation(B, Y, a, X, str));
3000: PetscCall(MatHeaderMerge(Y, &B));
3001: PetscCall(MatSeqAIJCheckInode(Y));
3002: }
3003: PetscCall(PetscFree(nnz));
3004: }
3005: PetscFunctionReturn(PETSC_SUCCESS);
3006: }
3008: PETSC_INTERN PetscErrorCode MatConjugate_SeqAIJ(Mat mat)
3009: {
3010: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
3011: PetscInt i, nz = aij->nz;
3012: PetscScalar *a;
3014: PetscFunctionBegin;
3015: PetscCall(MatSeqAIJGetArray(mat, &a));
3016: for (i = 0; i < nz; i++) a[i] = PetscConj(a[i]);
3017: PetscCall(MatSeqAIJRestoreArray(mat, &a));
3018: PetscFunctionReturn(PETSC_SUCCESS);
3019: }
3021: static PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A, Vec v, PetscInt idx[])
3022: {
3023: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
3024: PetscInt i, j, m = A->rmap->n, *ai, *aj, ncols, n;
3025: PetscReal atmp;
3026: PetscScalar *x;
3027: const MatScalar *aa, *av;
3029: PetscFunctionBegin;
3030: PetscCheck(!A->factortype, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
3031: PetscCall(MatSeqAIJGetArrayRead(A, &av));
3032: aa = av;
3033: ai = a->i;
3034: aj = a->j;
3036: PetscCall(VecGetArrayWrite(v, &x));
3037: PetscCall(VecGetLocalSize(v, &n));
3038: PetscCheck(n == A->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nonconforming matrix and vector");
3039: for (i = 0; i < m; i++) {
3040: ncols = ai[1] - ai[0];
3041: ai++;
3042: x[i] = 0;
3043: for (j = 0; j < ncols; j++) {
3044: atmp = PetscAbsScalar(*aa);
3045: if (PetscAbsScalar(x[i]) < atmp) {
3046: x[i] = atmp;
3047: if (idx) idx[i] = *aj;
3048: }
3049: aa++;
3050: aj++;
3051: }
3052: }
3053: PetscCall(VecRestoreArrayWrite(v, &x));
3054: PetscCall(MatSeqAIJRestoreArrayRead(A, &av));
3055: PetscFunctionReturn(PETSC_SUCCESS);
3056: }
3058: PETSC_INTERN PetscErrorCode MatGetRowSumAbs_SeqAIJ(Mat A, Vec v)
3059: {
3060: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
3061: PetscInt i, j, m = A->rmap->n, *ai, ncols, n;
3062: PetscScalar *x;
3063: const MatScalar *aa, *av;
3065: PetscFunctionBegin;
3066: PetscCheck(!A->factortype, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
3067: PetscCall(MatSeqAIJGetArrayRead(A, &av));
3068: aa = av;
3069: ai = a->i;
3071: PetscCall(VecGetArrayWrite(v, &x));
3072: PetscCall(VecGetLocalSize(v, &n));
3073: PetscCheck(n == A->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nonconforming matrix and vector");
3074: for (i = 0; i < m; i++) {
3075: ncols = ai[1] - ai[0];
3076: ai++;
3077: x[i] = 0;
3078: for (j = 0; j < ncols; j++) {
3079: x[i] += PetscAbsScalar(*aa);
3080: aa++;
3081: }
3082: }
3083: PetscCall(VecRestoreArrayWrite(v, &x));
3084: PetscCall(MatSeqAIJRestoreArrayRead(A, &av));
3085: PetscFunctionReturn(PETSC_SUCCESS);
3086: }
3088: static PetscErrorCode MatGetRowMax_SeqAIJ(Mat A, Vec v, PetscInt idx[])
3089: {
3090: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
3091: PetscInt i, j, m = A->rmap->n, *ai, *aj, ncols, n;
3092: PetscScalar *x;
3093: const MatScalar *aa, *av;
3095: PetscFunctionBegin;
3096: PetscCheck(!A->factortype, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
3097: PetscCall(MatSeqAIJGetArrayRead(A, &av));
3098: aa = av;
3099: ai = a->i;
3100: aj = a->j;
3102: PetscCall(VecGetArrayWrite(v, &x));
3103: PetscCall(VecGetLocalSize(v, &n));
3104: PetscCheck(n == A->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nonconforming matrix and vector");
3105: for (i = 0; i < m; i++) {
3106: ncols = ai[1] - ai[0];
3107: ai++;
3108: if (ncols == A->cmap->n) { /* row is dense */
3109: x[i] = *aa;
3110: if (idx) idx[i] = 0;
3111: } else { /* row is sparse so already KNOW maximum is 0.0 or higher */
3112: x[i] = 0.0;
3113: if (idx) {
3114: for (j = 0; j < ncols; j++) { /* find first implicit 0.0 in the row */
3115: if (aj[j] > j) {
3116: idx[i] = j;
3117: break;
3118: }
3119: }
3120: /* in case first implicit 0.0 in the row occurs at ncols-th column */
3121: if (j == ncols && j < A->cmap->n) idx[i] = j;
3122: }
3123: }
3124: for (j = 0; j < ncols; j++) {
3125: if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {
3126: x[i] = *aa;
3127: if (idx) idx[i] = *aj;
3128: }
3129: aa++;
3130: aj++;
3131: }
3132: }
3133: PetscCall(VecRestoreArrayWrite(v, &x));
3134: PetscCall(MatSeqAIJRestoreArrayRead(A, &av));
3135: PetscFunctionReturn(PETSC_SUCCESS);
3136: }
3138: static PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A, Vec v, PetscInt idx[])
3139: {
3140: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
3141: PetscInt i, j, m = A->rmap->n, *ai, *aj, ncols, n;
3142: PetscScalar *x;
3143: const MatScalar *aa, *av;
3145: PetscFunctionBegin;
3146: PetscCall(MatSeqAIJGetArrayRead(A, &av));
3147: aa = av;
3148: ai = a->i;
3149: aj = a->j;
3151: PetscCall(VecGetArrayWrite(v, &x));
3152: PetscCall(VecGetLocalSize(v, &n));
3153: PetscCheck(n == m, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nonconforming matrix and vector, %" PetscInt_FMT " vs. %" PetscInt_FMT " rows", m, n);
3154: for (i = 0; i < m; i++) {
3155: ncols = ai[1] - ai[0];
3156: ai++;
3157: if (ncols == A->cmap->n) { /* row is dense */
3158: x[i] = *aa;
3159: if (idx) idx[i] = 0;
3160: } else { /* row is sparse so already KNOW minimum is 0.0 or higher */
3161: x[i] = 0.0;
3162: if (idx) { /* find first implicit 0.0 in the row */
3163: for (j = 0; j < ncols; j++) {
3164: if (aj[j] > j) {
3165: idx[i] = j;
3166: break;
3167: }
3168: }
3169: /* in case first implicit 0.0 in the row occurs at ncols-th column */
3170: if (j == ncols && j < A->cmap->n) idx[i] = j;
3171: }
3172: }
3173: for (j = 0; j < ncols; j++) {
3174: if (PetscAbsScalar(x[i]) > PetscAbsScalar(*aa)) {
3175: x[i] = *aa;
3176: if (idx) idx[i] = *aj;
3177: }
3178: aa++;
3179: aj++;
3180: }
3181: }
3182: PetscCall(VecRestoreArrayWrite(v, &x));
3183: PetscCall(MatSeqAIJRestoreArrayRead(A, &av));
3184: PetscFunctionReturn(PETSC_SUCCESS);
3185: }
3187: static PetscErrorCode MatGetRowMin_SeqAIJ(Mat A, Vec v, PetscInt idx[])
3188: {
3189: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
3190: PetscInt i, j, m = A->rmap->n, ncols, n;
3191: const PetscInt *ai, *aj;
3192: PetscScalar *x;
3193: const MatScalar *aa, *av;
3195: PetscFunctionBegin;
3196: PetscCheck(!A->factortype, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
3197: PetscCall(MatSeqAIJGetArrayRead(A, &av));
3198: aa = av;
3199: ai = a->i;
3200: aj = a->j;
3202: PetscCall(VecGetArrayWrite(v, &x));
3203: PetscCall(VecGetLocalSize(v, &n));
3204: PetscCheck(n == m, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nonconforming matrix and vector");
3205: for (i = 0; i < m; i++) {
3206: ncols = ai[1] - ai[0];
3207: ai++;
3208: if (ncols == A->cmap->n) { /* row is dense */
3209: x[i] = *aa;
3210: if (idx) idx[i] = 0;
3211: } else { /* row is sparse so already KNOW minimum is 0.0 or lower */
3212: x[i] = 0.0;
3213: if (idx) { /* find first implicit 0.0 in the row */
3214: for (j = 0; j < ncols; j++) {
3215: if (aj[j] > j) {
3216: idx[i] = j;
3217: break;
3218: }
3219: }
3220: /* in case first implicit 0.0 in the row occurs at ncols-th column */
3221: if (j == ncols && j < A->cmap->n) idx[i] = j;
3222: }
3223: }
3224: for (j = 0; j < ncols; j++) {
3225: if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {
3226: x[i] = *aa;
3227: if (idx) idx[i] = *aj;
3228: }
3229: aa++;
3230: aj++;
3231: }
3232: }
3233: PetscCall(VecRestoreArrayWrite(v, &x));
3234: PetscCall(MatSeqAIJRestoreArrayRead(A, &av));
3235: PetscFunctionReturn(PETSC_SUCCESS);
3236: }
3238: static PetscErrorCode MatInvertBlockDiagonal_SeqAIJ(Mat A, const PetscScalar **values)
3239: {
3240: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
3241: PetscInt i, bs = A->rmap->bs, mbs = A->rmap->n / bs, ipvt[5], bs2 = bs * bs, *v_pivots, ij[7], *IJ, j;
3242: MatScalar *diag, work[25], *v_work;
3243: const PetscReal shift = 0.0;
3244: PetscBool allowzeropivot, zeropivotdetected = PETSC_FALSE;
3246: PetscFunctionBegin;
3247: allowzeropivot = PetscNot(A->erroriffailure);
3248: if (a->ibdiag && a->ibdiagsize == bs2 * mbs && a->ibdiagState == ((PetscObject)A)->state) {
3249: if (values) *values = a->ibdiag;
3250: PetscFunctionReturn(PETSC_SUCCESS);
3251: }
3252: /* reallocate only when the length changes, so that the pointer stays valid for callers that
3253: hold on to it, such as PCSetUp_PBJacobi_Host() */
3254: if (!a->ibdiag || a->ibdiagsize != bs2 * mbs) {
3255: PetscCall(PetscFree(a->ibdiag));
3256: PetscCall(PetscMalloc1(bs2 * mbs, &a->ibdiag));
3257: a->ibdiagsize = bs2 * mbs;
3258: }
3259: diag = a->ibdiag;
3260: if (values) *values = a->ibdiag;
3261: /* factor and invert each block */
3262: switch (bs) {
3263: case 1:
3264: for (i = 0; i < mbs; i++) {
3265: PetscCall(MatGetValues(A, 1, &i, 1, &i, diag + i));
3266: if (PetscAbsScalar(diag[i] + shift) < PETSC_MACHINE_EPSILON) {
3267: PetscCheck(allowzeropivot, PETSC_COMM_SELF, PETSC_ERR_MAT_LU_ZRPVT, "Zero pivot, row %" PetscInt_FMT " pivot %g tolerance %g", i, (double)PetscAbsScalar(diag[i]), (double)PETSC_MACHINE_EPSILON);
3268: A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3269: A->factorerror_zeropivot_value = PetscAbsScalar(diag[i]);
3270: A->factorerror_zeropivot_row = i;
3271: PetscCall(PetscInfo(A, "Zero pivot, row %" PetscInt_FMT " pivot %g tolerance %g\n", i, (double)PetscAbsScalar(diag[i]), (double)PETSC_MACHINE_EPSILON));
3272: }
3273: diag[i] = (PetscScalar)1.0 / (diag[i] + shift);
3274: }
3275: break;
3276: case 2:
3277: for (i = 0; i < mbs; i++) {
3278: ij[0] = 2 * i;
3279: ij[1] = 2 * i + 1;
3280: PetscCall(MatGetValues(A, 2, ij, 2, ij, diag));
3281: PetscCall(PetscKernel_A_gets_inverse_A_2(diag, shift, allowzeropivot, &zeropivotdetected));
3282: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3283: PetscCall(PetscKernel_A_gets_transpose_A_2(diag));
3284: diag += 4;
3285: }
3286: break;
3287: case 3:
3288: for (i = 0; i < mbs; i++) {
3289: ij[0] = 3 * i;
3290: ij[1] = 3 * i + 1;
3291: ij[2] = 3 * i + 2;
3292: PetscCall(MatGetValues(A, 3, ij, 3, ij, diag));
3293: PetscCall(PetscKernel_A_gets_inverse_A_3(diag, shift, allowzeropivot, &zeropivotdetected));
3294: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3295: PetscCall(PetscKernel_A_gets_transpose_A_3(diag));
3296: diag += 9;
3297: }
3298: break;
3299: case 4:
3300: for (i = 0; i < mbs; i++) {
3301: ij[0] = 4 * i;
3302: ij[1] = 4 * i + 1;
3303: ij[2] = 4 * i + 2;
3304: ij[3] = 4 * i + 3;
3305: PetscCall(MatGetValues(A, 4, ij, 4, ij, diag));
3306: PetscCall(PetscKernel_A_gets_inverse_A_4(diag, shift, allowzeropivot, &zeropivotdetected));
3307: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3308: PetscCall(PetscKernel_A_gets_transpose_A_4(diag));
3309: diag += 16;
3310: }
3311: break;
3312: case 5:
3313: for (i = 0; i < mbs; i++) {
3314: ij[0] = 5 * i;
3315: ij[1] = 5 * i + 1;
3316: ij[2] = 5 * i + 2;
3317: ij[3] = 5 * i + 3;
3318: ij[4] = 5 * i + 4;
3319: PetscCall(MatGetValues(A, 5, ij, 5, ij, diag));
3320: PetscCall(PetscKernel_A_gets_inverse_A_5(diag, ipvt, work, shift, allowzeropivot, &zeropivotdetected));
3321: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3322: PetscCall(PetscKernel_A_gets_transpose_A_5(diag));
3323: diag += 25;
3324: }
3325: break;
3326: case 6:
3327: for (i = 0; i < mbs; i++) {
3328: ij[0] = 6 * i;
3329: ij[1] = 6 * i + 1;
3330: ij[2] = 6 * i + 2;
3331: ij[3] = 6 * i + 3;
3332: ij[4] = 6 * i + 4;
3333: ij[5] = 6 * i + 5;
3334: PetscCall(MatGetValues(A, 6, ij, 6, ij, diag));
3335: PetscCall(PetscKernel_A_gets_inverse_A_6(diag, shift, allowzeropivot, &zeropivotdetected));
3336: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3337: PetscCall(PetscKernel_A_gets_transpose_A_6(diag));
3338: diag += 36;
3339: }
3340: break;
3341: case 7:
3342: for (i = 0; i < mbs; i++) {
3343: ij[0] = 7 * i;
3344: ij[1] = 7 * i + 1;
3345: ij[2] = 7 * i + 2;
3346: ij[3] = 7 * i + 3;
3347: ij[4] = 7 * i + 4;
3348: ij[5] = 7 * i + 5;
3349: ij[6] = 7 * i + 6;
3350: PetscCall(MatGetValues(A, 7, ij, 7, ij, diag));
3351: PetscCall(PetscKernel_A_gets_inverse_A_7(diag, shift, allowzeropivot, &zeropivotdetected));
3352: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3353: PetscCall(PetscKernel_A_gets_transpose_A_7(diag));
3354: diag += 49;
3355: }
3356: break;
3357: default:
3358: PetscCall(PetscMalloc3(bs, &v_work, bs, &v_pivots, bs, &IJ));
3359: for (i = 0; i < mbs; i++) {
3360: for (j = 0; j < bs; j++) IJ[j] = bs * i + j;
3361: PetscCall(MatGetValues(A, bs, IJ, bs, IJ, diag));
3362: PetscCall(PetscKernel_A_gets_inverse_A(bs, diag, v_pivots, v_work, allowzeropivot, &zeropivotdetected));
3363: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3364: PetscCall(PetscKernel_A_gets_transpose_A_N(diag, bs));
3365: diag += bs2;
3366: }
3367: PetscCall(PetscFree3(v_work, v_pivots, IJ));
3368: }
3369: a->ibdiagState = ((PetscObject)A)->state;
3370: PetscFunctionReturn(PETSC_SUCCESS);
3371: }
3373: static PetscErrorCode MatSetRandom_SeqAIJ(Mat x, PetscRandom rctx)
3374: {
3375: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)x->data;
3376: PetscScalar a, *aa;
3377: PetscInt m, n, i, j, col;
3379: PetscFunctionBegin;
3380: if (!x->assembled) {
3381: PetscCall(MatGetSize(x, &m, &n));
3382: for (i = 0; i < m; i++) {
3383: for (j = 0; j < aij->imax[i]; j++) {
3384: PetscCall(PetscRandomGetValue(rctx, &a));
3385: col = (PetscInt)(n * PetscRealPart(a));
3386: PetscCall(MatSetValues(x, 1, &i, 1, &col, &a, ADD_VALUES));
3387: }
3388: }
3389: } else {
3390: PetscCall(MatSeqAIJGetArrayWrite(x, &aa));
3391: for (i = 0; i < aij->nz; i++) PetscCall(PetscRandomGetValue(rctx, aa + i));
3392: PetscCall(MatSeqAIJRestoreArrayWrite(x, &aa));
3393: }
3394: PetscCall(MatAssemblyBegin(x, MAT_FINAL_ASSEMBLY));
3395: PetscCall(MatAssemblyEnd(x, MAT_FINAL_ASSEMBLY));
3396: PetscFunctionReturn(PETSC_SUCCESS);
3397: }
3399: /* Like MatSetRandom_SeqAIJ, but do not set values on columns in range of [low, high) */
3400: PetscErrorCode MatSetRandomSkipColumnRange_SeqAIJ_Private(Mat x, PetscInt low, PetscInt high, PetscRandom rctx)
3401: {
3402: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)x->data;
3403: PetscScalar a;
3404: PetscInt m, n, i, j, col, nskip;
3406: PetscFunctionBegin;
3407: nskip = high - low;
3408: PetscCall(MatGetSize(x, &m, &n));
3409: n -= nskip; /* shrink number of columns where nonzeros can be set */
3410: for (i = 0; i < m; i++) {
3411: for (j = 0; j < aij->imax[i]; j++) {
3412: PetscCall(PetscRandomGetValue(rctx, &a));
3413: col = (PetscInt)(n * PetscRealPart(a));
3414: if (col >= low) col += nskip; /* shift col rightward to skip the hole */
3415: PetscCall(MatSetValues(x, 1, &i, 1, &col, &a, ADD_VALUES));
3416: }
3417: }
3418: PetscCall(MatAssemblyBegin(x, MAT_FINAL_ASSEMBLY));
3419: PetscCall(MatAssemblyEnd(x, MAT_FINAL_ASSEMBLY));
3420: PetscFunctionReturn(PETSC_SUCCESS);
3421: }
3423: static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
3424: MatGetRow_SeqAIJ,
3425: MatRestoreRow_SeqAIJ,
3426: MatMult_SeqAIJ,
3427: /* 4*/ MatMultAdd_SeqAIJ,
3428: MatMultTranspose_SeqAIJ,
3429: MatMultTransposeAdd_SeqAIJ,
3430: NULL,
3431: NULL,
3432: NULL,
3433: /* 10*/ NULL,
3434: MatLUFactor_SeqAIJ,
3435: NULL,
3436: MatSOR_SeqAIJ,
3437: MatTranspose_SeqAIJ,
3438: /* 15*/ MatGetInfo_SeqAIJ,
3439: MatEqual_SeqAIJ,
3440: MatGetDiagonal_SeqAIJ,
3441: MatDiagonalScale_SeqAIJ,
3442: MatNorm_SeqAIJ,
3443: /* 20*/ NULL,
3444: MatAssemblyEnd_SeqAIJ,
3445: MatSetOption_SeqAIJ,
3446: MatZeroEntries_SeqAIJ,
3447: /* 24*/ MatZeroRows_SeqAIJ,
3448: NULL,
3449: NULL,
3450: NULL,
3451: NULL,
3452: /* 29*/ MatSetUp_Seq_Hash,
3453: NULL,
3454: NULL,
3455: NULL,
3456: NULL,
3457: /* 34*/ MatDuplicate_SeqAIJ,
3458: NULL,
3459: NULL,
3460: MatILUFactor_SeqAIJ,
3461: NULL,
3462: /* 39*/ MatAXPY_SeqAIJ,
3463: MatCreateSubMatrices_SeqAIJ,
3464: MatIncreaseOverlap_SeqAIJ,
3465: MatGetValues_SeqAIJ,
3466: MatCopy_SeqAIJ,
3467: /* 44*/ MatGetRowMax_SeqAIJ,
3468: MatScale_SeqAIJ,
3469: MatShift_SeqAIJ,
3470: MatDiagonalSet_SeqAIJ,
3471: MatZeroRowsColumns_SeqAIJ,
3472: /* 49*/ MatSetRandom_SeqAIJ,
3473: MatGetRowIJ_SeqAIJ,
3474: MatRestoreRowIJ_SeqAIJ,
3475: MatGetColumnIJ_SeqAIJ,
3476: MatRestoreColumnIJ_SeqAIJ,
3477: /* 54*/ MatFDColoringCreate_SeqXAIJ,
3478: NULL,
3479: NULL,
3480: MatPermute_SeqAIJ,
3481: NULL,
3482: /* 59*/ NULL,
3483: MatDestroy_SeqAIJ,
3484: MatView_SeqAIJ,
3485: NULL,
3486: NULL,
3487: /* 64*/ MatMatMatMultNumeric_SeqAIJ_SeqAIJ_SeqAIJ,
3488: NULL,
3489: NULL,
3490: NULL,
3491: MatGetRowMaxAbs_SeqAIJ,
3492: /* 69*/ MatGetRowMinAbs_SeqAIJ,
3493: NULL,
3494: NULL,
3495: MatFDColoringApply_AIJ,
3496: NULL,
3497: /* 74*/ MatFindZeroDiagonals_SeqAIJ,
3498: NULL,
3499: NULL,
3500: NULL,
3501: MatLoad_SeqAIJ,
3502: /* 79*/ NULL,
3503: NULL,
3504: NULL,
3505: NULL,
3506: NULL,
3507: /* 84*/ NULL,
3508: MatMatMultNumeric_SeqAIJ_SeqAIJ,
3509: MatPtAPNumeric_SeqAIJ_SeqAIJ_SparseAxpy,
3510: NULL,
3511: MatMatTransposeMultNumeric_SeqAIJ_SeqAIJ,
3512: /* 90*/ NULL,
3513: MatProductSetFromOptions_SeqAIJ,
3514: NULL,
3515: NULL,
3516: MatConjugate_SeqAIJ,
3517: /* 94*/ NULL,
3518: MatSetValuesRow_SeqAIJ,
3519: MatRealPart_SeqAIJ,
3520: MatImaginaryPart_SeqAIJ,
3521: NULL,
3522: /* 99*/ NULL,
3523: MatMatSolve_SeqAIJ,
3524: NULL,
3525: MatGetRowMin_SeqAIJ,
3526: NULL,
3527: /*104*/ NULL,
3528: NULL,
3529: NULL,
3530: NULL,
3531: NULL,
3532: /*109*/ NULL,
3533: NULL,
3534: NULL,
3535: NULL,
3536: MatGetMultiProcBlock_SeqAIJ,
3537: /*114*/ MatFindNonzeroRows_SeqAIJ,
3538: MatGetColumnReductions_SeqAIJ,
3539: MatInvertBlockDiagonal_SeqAIJ,
3540: MatInvertVariableBlockDiagonal_SeqAIJ,
3541: NULL,
3542: /*119*/ NULL,
3543: MatTransposeMatMultNumeric_SeqAIJ_SeqAIJ,
3544: MatTransposeColoringCreate_SeqAIJ,
3545: MatTransColoringApplySpToDen_SeqAIJ,
3546: MatTransColoringApplyDenToSp_SeqAIJ,
3547: /*124*/ MatRARtNumeric_SeqAIJ_SeqAIJ,
3548: NULL,
3549: NULL,
3550: MatFDColoringSetUp_SeqXAIJ,
3551: MatFindOffBlockDiagonalEntries_SeqAIJ,
3552: /*129*/ MatCreateMPIMatConcatenateSeqMat_SeqAIJ,
3553: MatDestroySubMatrices_SeqAIJ,
3554: NULL,
3555: NULL,
3556: MatCreateGraph_Simple_AIJ,
3557: /*134*/ MatTransposeSymbolic_SeqAIJ,
3558: MatEliminateZeros_SeqAIJ,
3559: MatGetRowSumAbs_SeqAIJ,
3560: NULL,
3561: NULL,
3562: /*139*/ NULL,
3563: MatCopyHashToXAIJ_Seq_Hash,
3564: NULL,
3565: NULL,
3566: NULL,
3567: /*144*/ NULL,
3568: NULL,
3569: NULL,
3570: NULL};
3572: static PetscErrorCode MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat, PetscInt *indices)
3573: {
3574: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
3575: PetscInt i, nz, n;
3577: PetscFunctionBegin;
3578: nz = aij->maxnz;
3579: n = mat->rmap->n;
3580: for (i = 0; i < nz; i++) aij->j[i] = indices[i];
3581: aij->nz = nz;
3582: for (i = 0; i < n; i++) aij->ilen[i] = aij->imax[i];
3583: PetscFunctionReturn(PETSC_SUCCESS);
3584: }
3586: /*
3587: * Given a sparse matrix with global column indices, compact it by using a local column space.
3588: * The result matrix helps saving memory in other algorithms, such as MatPtAPSymbolic_MPIAIJ_MPIAIJ_scalable()
3589: */
3590: PetscErrorCode MatSeqAIJCompactOutExtraColumns_SeqAIJ(Mat mat, ISLocalToGlobalMapping *mapping)
3591: {
3592: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
3593: PetscHMapI gid1_lid1;
3594: PetscHashIter tpos;
3595: PetscInt gid, lid, i, ec, nz = aij->nz;
3596: PetscInt *garray, *jj = aij->j;
3598: PetscFunctionBegin;
3600: PetscAssertPointer(mapping, 2);
3601: /* use a table */
3602: PetscCall(PetscHMapICreateWithSize(mat->rmap->n, &gid1_lid1));
3603: ec = 0;
3604: for (i = 0; i < nz; i++) {
3605: PetscInt data, gid1 = jj[i] + 1;
3606: PetscCall(PetscHMapIGetWithDefault(gid1_lid1, gid1, 0, &data));
3607: if (!data) {
3608: /* one based table */
3609: PetscCall(PetscHMapISet(gid1_lid1, gid1, ++ec));
3610: }
3611: }
3612: /* form array of columns we need */
3613: PetscCall(PetscMalloc1(ec, &garray));
3614: PetscHashIterBegin(gid1_lid1, tpos);
3615: while (!PetscHashIterAtEnd(gid1_lid1, tpos)) {
3616: PetscHashIterGetKey(gid1_lid1, tpos, gid);
3617: PetscHashIterGetVal(gid1_lid1, tpos, lid);
3618: PetscHashIterNext(gid1_lid1, tpos);
3619: gid--;
3620: lid--;
3621: garray[lid] = gid;
3622: }
3623: PetscCall(PetscSortInt(ec, garray)); /* sort, and rebuild */
3624: PetscCall(PetscHMapIClear(gid1_lid1));
3625: for (i = 0; i < ec; i++) PetscCall(PetscHMapISet(gid1_lid1, garray[i] + 1, i + 1));
3626: /* compact out the extra columns in B */
3627: for (i = 0; i < nz; i++) {
3628: PetscInt gid1 = jj[i] + 1;
3629: PetscCall(PetscHMapIGetWithDefault(gid1_lid1, gid1, 0, &lid));
3630: lid--;
3631: jj[i] = lid;
3632: }
3633: PetscCall(PetscLayoutDestroy(&mat->cmap));
3634: PetscCall(PetscHMapIDestroy(&gid1_lid1));
3635: PetscCall(PetscLayoutCreateFromSizes(PetscObjectComm((PetscObject)mat), ec, ec, 1, &mat->cmap));
3636: PetscCall(ISLocalToGlobalMappingCreate(PETSC_COMM_SELF, mat->cmap->bs, mat->cmap->n, garray, PETSC_OWN_POINTER, mapping));
3637: PetscCall(ISLocalToGlobalMappingSetType(*mapping, ISLOCALTOGLOBALMAPPINGHASH));
3638: PetscFunctionReturn(PETSC_SUCCESS);
3639: }
3641: /*@
3642: MatSeqAIJSetColumnIndices - Set the column indices for all the rows
3643: in the matrix.
3645: Input Parameters:
3646: + mat - the `MATSEQAIJ` matrix
3647: - indices - the column indices
3649: Level: advanced
3651: Notes:
3652: This can be called if you have precomputed the nonzero structure of the
3653: matrix and want to provide it to the matrix object to improve the performance
3654: of the `MatSetValues()` operation.
3656: You MUST have set the correct numbers of nonzeros per row in the call to
3657: `MatCreateSeqAIJ()`, and the columns indices MUST be sorted.
3659: MUST be called before any calls to `MatSetValues()`
3661: The indices should start with zero, not one.
3663: .seealso: [](ch_matrices), `Mat`, `MATSEQAIJ`
3664: @*/
3665: PetscErrorCode MatSeqAIJSetColumnIndices(Mat mat, PetscInt *indices)
3666: {
3667: PetscFunctionBegin;
3669: PetscAssertPointer(indices, 2);
3670: PetscUseMethod(mat, "MatSeqAIJSetColumnIndices_C", (Mat, PetscInt *), (mat, indices));
3671: PetscFunctionReturn(PETSC_SUCCESS);
3672: }
3674: static PetscErrorCode MatStoreValues_SeqAIJ(Mat mat)
3675: {
3676: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
3677: size_t nz = aij->i[mat->rmap->n];
3679: PetscFunctionBegin;
3680: PetscCheck(aij->nonew, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3682: /* allocate space for values if not already there */
3683: if (!aij->saved_values) PetscCall(PetscMalloc1(nz + 1, &aij->saved_values));
3685: /* copy values over */
3686: PetscCall(PetscArraycpy(aij->saved_values, aij->a, nz));
3687: PetscFunctionReturn(PETSC_SUCCESS);
3688: }
3690: /*@
3691: MatStoreValues - Stashes a copy of the matrix values; this allows reusing of the linear part of a Jacobian, while recomputing only the
3692: nonlinear portion.
3694: Logically Collect
3696: Input Parameter:
3697: . mat - the matrix (currently only `MATAIJ` matrices support this option)
3699: Level: advanced
3701: Example Usage:
3702: .vb
3703: Using SNES
3704: Create Jacobian matrix
3705: Set linear terms into matrix
3706: Apply boundary conditions to matrix, at this time matrix must have
3707: final nonzero structure (i.e. setting the nonlinear terms and applying
3708: boundary conditions again will not change the nonzero structure
3709: MatSetOption(mat, MAT_NEW_NONZERO_LOCATIONS, PETSC_FALSE);
3710: MatStoreValues(mat);
3711: Call SNESSetJacobian() with matrix
3712: In your Jacobian routine
3713: MatRetrieveValues(mat);
3714: Set nonlinear terms in matrix
3716: Without `SNESSolve()`, i.e. when you handle nonlinear solve yourself:
3717: // build linear portion of Jacobian
3718: MatSetOption(mat, MAT_NEW_NONZERO_LOCATIONS, PETSC_FALSE);
3719: MatStoreValues(mat);
3720: loop over nonlinear iterations
3721: MatRetrieveValues(mat);
3722: // call MatSetValues(mat,...) to set nonliner portion of Jacobian
3723: // call MatAssemblyBegin/End() on matrix
3724: Solve linear system with Jacobian
3725: endloop
3726: .ve
3728: Notes:
3729: Matrix must already be assembled before calling this routine
3730: Must set the matrix option `MatSetOption`(mat,`MAT_NEW_NONZERO_LOCATIONS`,`PETSC_FALSE`); before
3731: calling this routine.
3733: When this is called multiple times it overwrites the previous set of stored values
3734: and does not allocated additional space.
3736: .seealso: [](ch_matrices), `Mat`, `MatRetrieveValues()`
3737: @*/
3738: PetscErrorCode MatStoreValues(Mat mat)
3739: {
3740: PetscFunctionBegin;
3742: PetscCheck(mat->assembled, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for unassembled matrix");
3743: PetscCheck(!mat->factortype, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
3744: PetscUseMethod(mat, "MatStoreValues_C", (Mat), (mat));
3745: PetscFunctionReturn(PETSC_SUCCESS);
3746: }
3748: static PetscErrorCode MatRetrieveValues_SeqAIJ(Mat mat)
3749: {
3750: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
3751: PetscInt nz = aij->i[mat->rmap->n];
3753: PetscFunctionBegin;
3754: PetscCheck(aij->nonew, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3755: PetscCheck(aij->saved_values, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Must call MatStoreValues(A);first");
3756: /* copy values over */
3757: PetscCall(PetscArraycpy(aij->a, aij->saved_values, nz));
3758: PetscFunctionReturn(PETSC_SUCCESS);
3759: }
3761: /*@
3762: MatRetrieveValues - Retrieves the copy of the matrix values that was stored with `MatStoreValues()`
3764: Logically Collect
3766: Input Parameter:
3767: . mat - the matrix (currently only `MATAIJ` matrices support this option)
3769: Level: advanced
3771: .seealso: [](ch_matrices), `Mat`, `MatStoreValues()`
3772: @*/
3773: PetscErrorCode MatRetrieveValues(Mat mat)
3774: {
3775: PetscFunctionBegin;
3777: PetscCheck(mat->assembled, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for unassembled matrix");
3778: PetscCheck(!mat->factortype, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
3779: PetscUseMethod(mat, "MatRetrieveValues_C", (Mat), (mat));
3780: PetscCall(PetscObjectStateIncrease((PetscObject)mat));
3781: PetscFunctionReturn(PETSC_SUCCESS);
3782: }
3784: /*@
3785: MatCreateSeqAIJ - Creates a sparse matrix in `MATSEQAIJ` (compressed row) format
3786: (the default parallel PETSc format). For good matrix assembly performance
3787: the user should preallocate the matrix storage by setting the parameter `nz`
3788: (or the array `nnz`).
3790: Collective
3792: Input Parameters:
3793: + comm - MPI communicator, set to `PETSC_COMM_SELF`
3794: . m - number of rows
3795: . n - number of columns
3796: . nz - number of nonzeros per row (same for all rows)
3797: - nnz - array containing the number of nonzeros in the various rows
3798: (possibly different for each row) or NULL
3800: Output Parameter:
3801: . A - the matrix
3803: Options Database Keys:
3804: + -mat_no_inode - Do not use inodes
3805: - -mat_inode_limit limit - Sets inode limit (max limit=5)
3807: Level: intermediate
3809: Notes:
3810: It is recommend to use `MatCreateFromOptions()` instead of this routine
3812: If `nnz` is given then `nz` is ignored
3814: The `MATSEQAIJ` format, also called
3815: compressed row storage, is fully compatible with standard Fortran
3816: storage. That is, the stored row and column indices can begin at
3817: either one (as in Fortran) or zero.
3819: Specify the preallocated storage with either `nz` or `nnz` (not both).
3820: Set `nz` = `PETSC_DEFAULT` and `nnz` = `NULL` for PETSc to control dynamic memory
3821: allocation.
3823: By default, this format uses inodes (identical nodes) when possible, to
3824: improve numerical efficiency of matrix-vector products and solves. We
3825: search for consecutive rows with the same nonzero structure, thereby
3826: reusing matrix information to achieve increased efficiency.
3828: .seealso: [](ch_matrices), `Mat`, [Sparse Matrix Creation](sec_matsparse), `MatCreate()`, `MatCreateAIJ()`, `MatSetValues()`, `MatSeqAIJSetColumnIndices()`, `MatCreateSeqAIJWithArrays()`
3829: @*/
3830: PetscErrorCode MatCreateSeqAIJ(MPI_Comm comm, PetscInt m, PetscInt n, PetscInt nz, const PetscInt nnz[], Mat *A)
3831: {
3832: PetscFunctionBegin;
3833: PetscCall(MatCreate(comm, A));
3834: PetscCall(MatSetSizes(*A, m, n, m, n));
3835: PetscCall(MatSetType(*A, MATSEQAIJ));
3836: PetscCall(MatSeqAIJSetPreallocation_SeqAIJ(*A, nz, nnz));
3837: PetscFunctionReturn(PETSC_SUCCESS);
3838: }
3840: /*@
3841: MatSeqAIJSetPreallocation - For good matrix assembly performance
3842: the user should preallocate the matrix storage by setting the parameter nz
3843: (or the array nnz). By setting these parameters accurately, performance
3844: during matrix assembly can be increased by more than a factor of 50.
3846: Collective
3848: Input Parameters:
3849: + B - The matrix
3850: . nz - number of nonzeros per row (same for all rows)
3851: - nnz - array containing the number of nonzeros in the various rows
3852: (possibly different for each row) or NULL
3854: Options Database Keys:
3855: + -mat_no_inode - Do not use inodes
3856: - -mat_inode_limit limit - Sets inode limit (max limit=5)
3858: Level: intermediate
3860: Notes:
3861: If `nnz` is given then `nz` is ignored
3863: The `MATSEQAIJ` format also called
3864: compressed row storage, is fully compatible with standard Fortran
3865: storage. That is, the stored row and column indices can begin at
3866: either one (as in Fortran) or zero. See the users' manual for details.
3868: Specify the preallocated storage with either `nz` or `nnz` (not both).
3869: Set nz = `PETSC_DEFAULT` and `nnz` = `NULL` for PETSc to control dynamic memory
3870: allocation.
3872: You can call `MatGetInfo()` to get information on how effective the preallocation was;
3873: for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3874: You can also run with the option -info and look for messages with the string
3875: malloc in them to see if additional memory allocation was needed.
3877: Developer Notes:
3878: Use nz of `MAT_SKIP_ALLOCATION` to not allocate any space for the matrix
3879: entries or columns indices
3881: By default, this format uses inodes (identical nodes) when possible, to
3882: improve numerical efficiency of matrix-vector products and solves. We
3883: search for consecutive rows with the same nonzero structure, thereby
3884: reusing matrix information to achieve increased efficiency.
3886: .seealso: [](ch_matrices), `Mat`, `MatCreate()`, `MatCreateAIJ()`, `MatSetValues()`, `MatSeqAIJSetColumnIndices()`, `MatCreateSeqAIJWithArrays()`, `MatGetInfo()`,
3887: `MatSeqAIJSetTotalPreallocation()`
3888: @*/
3889: PetscErrorCode MatSeqAIJSetPreallocation(Mat B, PetscInt nz, const PetscInt nnz[])
3890: {
3891: PetscFunctionBegin;
3894: PetscTryMethod(B, "MatSeqAIJSetPreallocation_C", (Mat, PetscInt, const PetscInt[]), (B, nz, nnz));
3895: PetscFunctionReturn(PETSC_SUCCESS);
3896: }
3898: PetscErrorCode MatSeqAIJSetPreallocation_SeqAIJ(Mat B, PetscInt nz, const PetscInt *nnz)
3899: {
3900: Mat_SeqAIJ *b = (Mat_SeqAIJ *)B->data;
3901: PetscBool skipallocation = PETSC_FALSE, realalloc = PETSC_FALSE;
3902: PetscInt i;
3904: PetscFunctionBegin;
3905: if (B->hash_active) {
3906: B->ops[0] = b->cops;
3907: PetscCall(PetscHMapIJVDestroy(&b->ht));
3908: PetscCall(PetscFree(b->dnz));
3909: B->hash_active = PETSC_FALSE;
3910: }
3911: if (nz >= 0 || nnz) realalloc = PETSC_TRUE;
3912: if (nz == MAT_SKIP_ALLOCATION) {
3913: skipallocation = PETSC_TRUE;
3914: nz = 0;
3915: }
3916: PetscCall(PetscLayoutSetUp(B->rmap));
3917: PetscCall(PetscLayoutSetUp(B->cmap));
3919: if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
3920: PetscCheck(nz >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "nz cannot be less than 0: value %" PetscInt_FMT, nz);
3921: if (nnz) {
3922: for (i = 0; i < B->rmap->n; i++) {
3923: PetscCheck(nnz[i] >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "nnz cannot be less than 0: local row %" PetscInt_FMT " value %" PetscInt_FMT, i, nnz[i]);
3924: PetscCheck(nnz[i] <= B->cmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "nnz cannot be greater than row length: local row %" PetscInt_FMT " value %" PetscInt_FMT " rowlength %" PetscInt_FMT, i, nnz[i], B->cmap->n);
3925: }
3926: }
3928: B->preallocated = PETSC_TRUE;
3929: if (!skipallocation) {
3930: if (!b->imax) PetscCall(PetscMalloc1(B->rmap->n, &b->imax));
3931: if (!b->ilen) {
3932: /* b->ilen will count nonzeros in each row so far. */
3933: PetscCall(PetscCalloc1(B->rmap->n, &b->ilen));
3934: } else {
3935: PetscCall(PetscMemzero(b->ilen, B->rmap->n * sizeof(PetscInt)));
3936: }
3937: if (!b->ipre) PetscCall(PetscMalloc1(B->rmap->n, &b->ipre));
3938: if (!nnz) {
3939: if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
3940: else if (nz < 0) nz = 1;
3941: nz = PetscMin(nz, B->cmap->n);
3942: for (i = 0; i < B->rmap->n; i++) b->imax[i] = nz;
3943: PetscCall(PetscIntMultError(nz, B->rmap->n, &nz));
3944: } else {
3945: PetscInt64 nz64 = 0;
3946: for (i = 0; i < B->rmap->n; i++) {
3947: b->imax[i] = nnz[i];
3948: nz64 += nnz[i];
3949: }
3950: PetscCall(PetscIntCast(nz64, &nz));
3951: }
3953: /* allocate the matrix space */
3954: PetscCall(MatSeqXAIJFreeAIJ(B, &b->a, &b->j, &b->i));
3955: PetscCall(PetscShmgetAllocateArray(nz, sizeof(PetscInt), (void **)&b->j));
3956: PetscCall(PetscShmgetAllocateArray(B->rmap->n + 1, sizeof(PetscInt), (void **)&b->i));
3957: b->free_ij = PETSC_TRUE;
3958: if (B->structure_only) {
3959: b->free_a = PETSC_FALSE;
3960: } else {
3961: PetscCall(PetscShmgetAllocateArray(nz, sizeof(PetscScalar), (void **)&b->a));
3962: b->free_a = PETSC_TRUE;
3963: }
3964: b->i[0] = 0;
3965: for (i = 1; i < B->rmap->n + 1; i++) b->i[i] = b->i[i - 1] + b->imax[i - 1];
3966: } else {
3967: b->free_a = PETSC_FALSE;
3968: b->free_ij = PETSC_FALSE;
3969: }
3971: if (b->ipre && nnz != b->ipre && b->imax) {
3972: /* reserve user-requested sparsity */
3973: PetscCall(PetscArraycpy(b->ipre, b->imax, B->rmap->n));
3974: }
3976: b->nz = 0;
3977: b->maxnz = nz;
3978: B->info.nz_unneeded = (double)b->maxnz;
3979: if (realalloc) PetscCall(MatSetOption(B, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_TRUE));
3980: B->was_assembled = PETSC_FALSE;
3981: B->assembled = PETSC_FALSE;
3982: #if PetscDefined(HAVE_DEVICE)
3983: B->offloadmask = PETSC_OFFLOAD_CPU;
3984: #endif
3985: /* We simply deem preallocation has changed nonzero state. Updating the state
3986: will give clients (like AIJKokkos) a chance to know something has happened.
3987: */
3988: B->nonzerostate++;
3989: PetscFunctionReturn(PETSC_SUCCESS);
3990: }
3992: PetscErrorCode MatResetPreallocation_SeqAIJ_Private(Mat A, PetscBool *memoryreset)
3993: {
3994: Mat_SeqAIJ *a;
3995: PetscInt i;
3996: PetscBool skipreset;
3998: PetscFunctionBegin;
4001: PetscCheck(A->insertmode == NOT_SET_VALUES, PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot reset preallocation after setting some values but not yet calling MatAssemblyBegin()/MatAssemblyEnd()");
4002: if (A->num_ass == 0) PetscFunctionReturn(PETSC_SUCCESS);
4004: /* Check local size. If zero, then return */
4005: if (!A->rmap->n) PetscFunctionReturn(PETSC_SUCCESS);
4007: a = (Mat_SeqAIJ *)A->data;
4008: /* if no saved info, we error out */
4009: PetscCheck(a->ipre, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "No saved preallocation info ");
4011: PetscCheck(a->i && a->imax && a->ilen, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "Memory info is incomplete, and cannot reset preallocation ");
4013: PetscCall(PetscArraycmp(a->ipre, a->ilen, A->rmap->n, &skipreset));
4014: if (skipreset) PetscCall(MatZeroEntries(A));
4015: else {
4016: PetscCall(PetscArraycpy(a->imax, a->ipre, A->rmap->n));
4017: PetscCall(PetscArrayzero(a->ilen, A->rmap->n));
4018: a->i[0] = 0;
4019: for (i = 1; i < A->rmap->n + 1; i++) a->i[i] = a->i[i - 1] + a->imax[i - 1];
4020: A->preallocated = PETSC_TRUE;
4021: a->nz = 0;
4022: a->maxnz = a->i[A->rmap->n];
4023: A->info.nz_unneeded = (double)a->maxnz;
4024: A->was_assembled = PETSC_FALSE;
4025: A->assembled = PETSC_FALSE;
4026: A->nonzerostate++;
4027: /* Log that the state of this object has changed; this will help guarantee that preconditioners get re-setup */
4028: PetscCall(PetscObjectStateIncrease((PetscObject)A));
4029: }
4030: if (memoryreset) *memoryreset = (PetscBool)!skipreset;
4031: PetscFunctionReturn(PETSC_SUCCESS);
4032: }
4034: static PetscErrorCode MatResetPreallocation_SeqAIJ(Mat A)
4035: {
4036: PetscFunctionBegin;
4037: PetscCall(MatResetPreallocation_SeqAIJ_Private(A, NULL));
4038: PetscFunctionReturn(PETSC_SUCCESS);
4039: }
4041: /*@
4042: MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in `MATSEQAIJ` format.
4044: Input Parameters:
4045: + B - the matrix
4046: . i - the indices into `j` for the start of each row (indices start with zero)
4047: . j - the column indices for each row (indices start with zero) these must be sorted for each row
4048: - v - optional values in the matrix, use `NULL` if not provided
4050: Level: developer
4052: Notes:
4053: The `i`,`j`,`v` values are COPIED with this routine; to avoid the copy use `MatCreateSeqAIJWithArrays()`
4055: This routine may be called multiple times with different nonzero patterns (or the same nonzero pattern). The nonzero
4056: structure will be the union of all the previous nonzero structures.
4058: Developer Notes:
4059: An optimization could be added to the implementation where it checks if the `i`, and `j` are identical to the current `i` and `j` and
4060: then just copies the `v` values directly with `PetscMemcpy()`.
4062: This routine could also take a `PetscCopyMode` argument to allow sharing the values instead of always copying them.
4064: .seealso: [](ch_matrices), `Mat`, `MatCreate()`, `MatCreateSeqAIJ()`, `MatSetValues()`, `MatSeqAIJSetPreallocation()`, `MATSEQAIJ`, `MatResetPreallocation()`
4065: @*/
4066: PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B, const PetscInt i[], const PetscInt j[], const PetscScalar v[])
4067: {
4068: PetscFunctionBegin;
4071: PetscTryMethod(B, "MatSeqAIJSetPreallocationCSR_C", (Mat, const PetscInt[], const PetscInt[], const PetscScalar[]), (B, i, j, v));
4072: PetscFunctionReturn(PETSC_SUCCESS);
4073: }
4075: static PetscErrorCode MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B, const PetscInt Ii[], const PetscInt J[], const PetscScalar v[])
4076: {
4077: PetscInt i;
4078: PetscInt m, n;
4079: PetscInt nz;
4080: PetscInt *nnz;
4082: PetscFunctionBegin;
4083: PetscCheck(Ii[0] == 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %" PetscInt_FMT, Ii[0]);
4085: PetscCall(PetscLayoutSetUp(B->rmap));
4086: PetscCall(PetscLayoutSetUp(B->cmap));
4088: PetscCall(MatGetSize(B, &m, &n));
4089: PetscCall(PetscMalloc1(m + 1, &nnz));
4090: for (i = 0; i < m; i++) {
4091: nz = Ii[i + 1] - Ii[i];
4092: PetscCheck(nz >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Local row %" PetscInt_FMT " has a negative number of columns %" PetscInt_FMT, i, nz);
4093: nnz[i] = nz;
4094: }
4095: PetscCall(MatSeqAIJSetPreallocation(B, 0, nnz));
4096: PetscCall(PetscFree(nnz));
4098: for (i = 0; i < m; i++) PetscCall(MatSetValues_SeqAIJ(B, 1, &i, Ii[i + 1] - Ii[i], J + Ii[i], PetscSafePointerPlusOffset(v, Ii[i]), INSERT_VALUES));
4100: PetscCall(MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY));
4101: PetscCall(MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY));
4103: PetscCall(MatSetOption(B, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
4104: PetscFunctionReturn(PETSC_SUCCESS);
4105: }
4107: /*@
4108: MatSeqAIJKron - Computes `C`, the Kronecker product of `A` and `B`.
4110: Input Parameters:
4111: + A - left-hand side matrix
4112: . B - right-hand side matrix
4113: - reuse - either `MAT_INITIAL_MATRIX` or `MAT_REUSE_MATRIX`
4115: Output Parameter:
4116: . C - Kronecker product of `A` and `B`
4118: Level: intermediate
4120: Note:
4121: `MAT_REUSE_MATRIX` can only be used when the nonzero structure of the product matrix has not changed from that last call to `MatSeqAIJKron()`.
4123: .seealso: [](ch_matrices), `Mat`, `MatCreateSeqAIJ()`, `MATSEQAIJ`, `MATKAIJ`, `MatReuse`
4124: @*/
4125: PetscErrorCode MatSeqAIJKron(Mat A, Mat B, MatReuse reuse, Mat *C)
4126: {
4127: PetscFunctionBegin;
4132: PetscAssertPointer(C, 4);
4133: if (reuse == MAT_REUSE_MATRIX) {
4136: }
4137: PetscTryMethod(A, "MatSeqAIJKron_C", (Mat, Mat, MatReuse, Mat *), (A, B, reuse, C));
4138: PetscFunctionReturn(PETSC_SUCCESS);
4139: }
4141: static PetscErrorCode MatSeqAIJKron_SeqAIJ(Mat A, Mat B, MatReuse reuse, Mat *C)
4142: {
4143: Mat newmat;
4144: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
4145: Mat_SeqAIJ *b = (Mat_SeqAIJ *)B->data;
4146: PetscScalar *v;
4147: const PetscScalar *aa, *ba;
4148: PetscInt *i, *j, m, n, p, q, nnz = 0, am = A->rmap->n, bm = B->rmap->n, an = A->cmap->n, bn = B->cmap->n;
4149: PetscBool flg;
4151: PetscFunctionBegin;
4152: PetscCheck(!A->factortype, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
4153: PetscCheck(A->assembled, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for unassembled matrix");
4154: PetscCheck(!B->factortype, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
4155: PetscCheck(B->assembled, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for unassembled matrix");
4156: PetscCall(PetscObjectTypeCompare((PetscObject)B, MATSEQAIJ, &flg));
4157: PetscCheck(flg, PETSC_COMM_SELF, PETSC_ERR_SUP, "MatType %s", ((PetscObject)B)->type_name);
4158: PetscCheck(reuse == MAT_INITIAL_MATRIX || reuse == MAT_REUSE_MATRIX, PETSC_COMM_SELF, PETSC_ERR_SUP, "MatReuse %d", (int)reuse);
4159: if (reuse == MAT_INITIAL_MATRIX) {
4160: PetscCall(PetscMalloc2(am * bm + 1, &i, a->i[am] * b->i[bm], &j));
4161: PetscCall(MatCreate(PETSC_COMM_SELF, &newmat));
4162: PetscCall(MatSetSizes(newmat, am * bm, an * bn, am * bm, an * bn));
4163: PetscCall(MatSetType(newmat, MATAIJ));
4164: i[0] = 0;
4165: for (m = 0; m < am; ++m) {
4166: for (p = 0; p < bm; ++p) {
4167: i[m * bm + p + 1] = i[m * bm + p] + (a->i[m + 1] - a->i[m]) * (b->i[p + 1] - b->i[p]);
4168: for (n = a->i[m]; n < a->i[m + 1]; ++n) {
4169: for (q = b->i[p]; q < b->i[p + 1]; ++q) j[nnz++] = a->j[n] * bn + b->j[q];
4170: }
4171: }
4172: }
4173: PetscCall(MatSeqAIJSetPreallocationCSR(newmat, i, j, NULL));
4174: *C = newmat;
4175: PetscCall(PetscFree2(i, j));
4176: nnz = 0;
4177: }
4178: PetscCall(MatSeqAIJGetArray(*C, &v));
4179: PetscCall(MatSeqAIJGetArrayRead(A, &aa));
4180: PetscCall(MatSeqAIJGetArrayRead(B, &ba));
4181: for (m = 0; m < am; ++m) {
4182: for (p = 0; p < bm; ++p) {
4183: for (n = a->i[m]; n < a->i[m + 1]; ++n) {
4184: for (q = b->i[p]; q < b->i[p + 1]; ++q) v[nnz++] = aa[n] * ba[q];
4185: }
4186: }
4187: }
4188: PetscCall(MatSeqAIJRestoreArray(*C, &v));
4189: PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
4190: PetscCall(MatSeqAIJRestoreArrayRead(B, &ba));
4191: PetscFunctionReturn(PETSC_SUCCESS);
4192: }
4194: #include <../src/mat/impls/dense/seq/dense.h>
4195: #include <petsc/private/kernels/petscaxpy.h>
4197: /*
4198: Computes (B'*A')' since computing B*A directly is untenable
4200: n p p
4201: [ ] [ ] [ ]
4202: m [ A ] * n [ B ] = m [ C ]
4203: [ ] [ ] [ ]
4205: */
4206: PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A, Mat B, Mat C)
4207: {
4208: Mat_SeqDense *sub_a = (Mat_SeqDense *)A->data;
4209: Mat_SeqAIJ *sub_b = (Mat_SeqAIJ *)B->data;
4210: Mat_SeqDense *sub_c = (Mat_SeqDense *)C->data;
4211: PetscInt i, j, n, m, q, p;
4212: const PetscInt *ii, *idx;
4213: const PetscScalar *b, *a, *a_q;
4214: PetscScalar *c, *c_q;
4215: PetscInt clda = sub_c->lda;
4216: PetscInt alda = sub_a->lda;
4218: PetscFunctionBegin;
4219: m = A->rmap->n;
4220: n = A->cmap->n;
4221: p = B->cmap->n;
4222: a = sub_a->v;
4223: b = sub_b->a;
4224: c = sub_c->v;
4225: if (clda == m) {
4226: PetscCall(PetscArrayzero(c, m * p));
4227: } else {
4228: for (j = 0; j < p; j++)
4229: for (i = 0; i < m; i++) c[j * clda + i] = 0.0;
4230: }
4231: ii = sub_b->i;
4232: idx = sub_b->j;
4233: for (i = 0; i < n; i++) {
4234: q = ii[i + 1] - ii[i];
4235: while (q-- > 0) {
4236: c_q = c + clda * (*idx);
4237: a_q = a + alda * i;
4238: PetscKernelAXPY(c_q, *b, a_q, m);
4239: idx++;
4240: b++;
4241: }
4242: }
4243: PetscFunctionReturn(PETSC_SUCCESS);
4244: }
4246: PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A, Mat B, PetscReal fill, Mat C)
4247: {
4248: PetscInt m = A->rmap->n, n = B->cmap->n;
4249: PetscBool cisdense;
4251: PetscFunctionBegin;
4252: PetscCheck(A->cmap->n == B->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "A->cmap->n %" PetscInt_FMT " != B->rmap->n %" PetscInt_FMT, A->cmap->n, B->rmap->n);
4253: PetscCall(MatSetSizes(C, m, n, m, n));
4254: PetscCall(MatSetBlockSizesFromMats(C, A, B));
4255: PetscCall(PetscObjectTypeCompareAny((PetscObject)C, &cisdense, MATSEQDENSE, MATSEQDENSECUDA, MATSEQDENSEHIP, ""));
4256: if (!cisdense) {
4257: PetscCall(MatSetType(C, MATDENSE));
4258: PetscCall(MatSetVecType(C, A->defaultvectype));
4259: }
4260: PetscCall(MatSetUp(C));
4262: C->ops->matmultnumeric = MatMatMultNumeric_SeqDense_SeqAIJ;
4263: PetscFunctionReturn(PETSC_SUCCESS);
4264: }
4266: /*MC
4267: MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
4268: based on compressed sparse row format.
4270: Options Database Key:
4271: . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
4273: Level: beginner
4275: Notes:
4276: `MatSetValues()` may be called with a `NULL` argument for the numerical values to insert zeros at the supplied row and column indices.
4278: Call `MatSetOption(A, MAT_STRUCTURE_ONLY, PETSC_TRUE)` before preallocation or `MatSetUp()` to store only the nonzero pattern.
4279: The assembled matrix has no numerical value array. Row and column indices supplied during insertion are retained, while numerical values are ignored.
4280: Such matrices can be used for structural operations, but not for numerical operations.
4282: Developer Note:
4283: It would be nice if all matrix formats supported passing `NULL` in for the numerical values
4285: .seealso: [](ch_matrices), `Mat`, `MatCreateSeqAIJ()`, `MatSetFromOptions()`, `MatSetType()`, `MatCreate()`, `MatType`, `MATSELL`, `MATSEQSELL`, `MATMPISELL`
4286: M*/
4288: /*MC
4289: MATAIJ - MATAIJ = "aij" - A matrix type to be used for sparse matrices.
4291: This matrix type is identical to `MATSEQAIJ` when constructed with a single process communicator,
4292: and `MATMPIAIJ` otherwise. As a result, for single process communicators,
4293: `MatSeqAIJSetPreallocation()` is supported, and similarly `MatMPIAIJSetPreallocation()` is supported
4294: for communicators controlling multiple processes. It is recommended that you call both of
4295: the above preallocation routines for simplicity.
4297: Options Database Key:
4298: . -mat_type aij - sets the matrix type to "aij" during a call to `MatSetFromOptions()`
4300: Level: beginner
4302: Notes:
4303: Call `MatSetOption(A, MAT_STRUCTURE_ONLY, PETSC_TRUE)` before preallocation or `MatSetUp()` to store only the nonzero pattern.
4304: The assembled matrix has no numerical value array. Row and column indices supplied during insertion are retained, while numerical values are ignored.
4305: Such matrices can be used for structural operations, but not for numerical operations.
4307: Subclasses include `MATAIJCUSPARSE`, `MATAIJPERM`, `MATAIJSELL`, `MATAIJMKL`, `MATAIJCRL`, and also automatically switches over to use inodes when
4308: enough exist.
4310: .seealso: [](ch_matrices), `Mat`, `MatCreateAIJ()`, `MatCreateSeqAIJ()`, `MATSEQAIJ`, `MATMPIAIJ`, `MATSELL`, `MATSEQSELL`, `MATMPISELL`
4311: M*/
4313: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCRL(Mat, MatType, MatReuse, Mat *);
4314: #if PetscDefined(HAVE_ELEMENTAL)
4315: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_Elemental(Mat, MatType, MatReuse, Mat *);
4316: #endif
4317: #if PetscDefined(HAVE_SCALAPACK) && (PetscDefined(USE_REAL_SINGLE) || PetscDefined(USE_REAL_DOUBLE))
4318: PETSC_INTERN PetscErrorCode MatConvert_AIJ_ScaLAPACK(Mat, MatType, MatReuse, Mat *);
4319: #endif
4320: #if PetscDefined(HAVE_HYPRE)
4321: PETSC_INTERN PetscErrorCode MatConvert_AIJ_HYPRE(Mat A, MatType, MatReuse, Mat *);
4322: #endif
4324: PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_SeqSELL(Mat, MatType, MatReuse, Mat *);
4325: PETSC_INTERN PetscErrorCode MatConvert_XAIJ_IS(Mat, MatType, MatReuse, Mat *);
4326: PETSC_INTERN PetscErrorCode MatProductSetFromOptions_IS_XAIJ(Mat);
4328: /*@
4329: MatSeqAIJGetArray - gives read/write access to the array where the data for a `MATSEQAIJ` matrix is stored
4331: Not Collective
4333: Input Parameter:
4334: . A - a `MATSEQAIJ` matrix
4336: Output Parameter:
4337: . array - pointer to the data
4339: Level: intermediate
4341: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJRestoreArray()`
4342: @*/
4343: PetscErrorCode MatSeqAIJGetArray(Mat A, PetscScalar *array[])
4344: {
4345: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;
4347: PetscFunctionBegin;
4348: if (A->structure_only || aij->ops->getarray == NULL) *array = aij->a;
4349: else PetscCall((*aij->ops->getarray)(A, array));
4350: PetscFunctionReturn(PETSC_SUCCESS);
4351: }
4353: /*@
4354: MatSeqAIJRestoreArray - returns access to the array where the data for a `MATSEQAIJ` matrix is stored obtained by `MatSeqAIJGetArray()`
4356: Not Collective
4358: Input Parameters:
4359: + A - a `MATSEQAIJ` matrix
4360: - array - pointer to the data
4362: Level: intermediate
4364: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJGetArray()`
4365: @*/
4366: PetscErrorCode MatSeqAIJRestoreArray(Mat A, PetscScalar *array[])
4367: {
4368: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;
4370: PetscFunctionBegin;
4371: if (A->structure_only || aij->ops->restorearray == NULL) *array = NULL;
4372: else PetscCall((*aij->ops->restorearray)(A, array));
4373: PetscCall(PetscObjectStateIncrease((PetscObject)A));
4374: PetscFunctionReturn(PETSC_SUCCESS);
4375: }
4377: /*@
4378: MatSeqAIJGetArrayRead - gives read-only access to the array where the data for a `MATSEQAIJ` matrix is stored
4380: Not Collective
4382: Input Parameter:
4383: . A - a `MATSEQAIJ` matrix
4385: Output Parameter:
4386: . array - pointer to the data
4388: Level: intermediate
4390: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJGetArray()`, `MatSeqAIJRestoreArrayRead()`
4391: @*/
4392: PetscErrorCode MatSeqAIJGetArrayRead(Mat A, const PetscScalar *array[])
4393: {
4394: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;
4396: PetscFunctionBegin;
4397: if (A->structure_only || aij->ops->getarrayread == NULL) *array = aij->a;
4398: else PetscCall((*aij->ops->getarrayread)(A, array));
4399: PetscFunctionReturn(PETSC_SUCCESS);
4400: }
4402: /*@
4403: MatSeqAIJRestoreArrayRead - restore the read-only access array obtained from `MatSeqAIJGetArrayRead()`
4405: Not Collective
4407: Input Parameter:
4408: . A - a `MATSEQAIJ` matrix
4410: Output Parameter:
4411: . array - pointer to the data
4413: Level: intermediate
4415: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJGetArray()`, `MatSeqAIJGetArrayRead()`
4416: @*/
4417: PetscErrorCode MatSeqAIJRestoreArrayRead(Mat A, const PetscScalar *array[])
4418: {
4419: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;
4421: PetscFunctionBegin;
4422: if (A->structure_only || aij->ops->restorearrayread == NULL) *array = NULL;
4423: else PetscCall((*aij->ops->restorearrayread)(A, array));
4424: PetscFunctionReturn(PETSC_SUCCESS);
4425: }
4427: /*@
4428: MatSeqAIJGetArrayWrite - gives write-only access to the array where the data for a `MATSEQAIJ` matrix is stored
4430: Not Collective
4432: Input Parameter:
4433: . A - a `MATSEQAIJ` matrix
4435: Output Parameter:
4436: . array - pointer to the data
4438: Level: intermediate
4440: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJGetArray()`, `MatSeqAIJRestoreArrayWrite()`
4441: @*/
4442: PetscErrorCode MatSeqAIJGetArrayWrite(Mat A, PetscScalar *array[])
4443: {
4444: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;
4446: PetscFunctionBegin;
4447: if (A->structure_only || aij->ops->getarraywrite == NULL) *array = aij->a;
4448: else PetscCall((*aij->ops->getarraywrite)(A, array));
4449: PetscCall(PetscObjectStateIncrease((PetscObject)A));
4450: PetscFunctionReturn(PETSC_SUCCESS);
4451: }
4453: /*@
4454: MatSeqAIJRestoreArrayWrite - restore the write-only access array obtained from `MatSeqAIJGetArrayWrite()`
4456: Not Collective
4458: Input Parameter:
4459: . A - a `MATSEQAIJ` matrix
4461: Output Parameter:
4462: . array - pointer to the data
4464: Level: intermediate
4466: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJGetArray()`, `MatSeqAIJGetArrayWrite()`
4467: @*/
4468: PetscErrorCode MatSeqAIJRestoreArrayWrite(Mat A, PetscScalar *array[])
4469: {
4470: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;
4472: PetscFunctionBegin;
4473: if (A->structure_only || aij->ops->restorearraywrite == NULL) *array = NULL;
4474: else PetscCall((*aij->ops->restorearraywrite)(A, array));
4475: PetscFunctionReturn(PETSC_SUCCESS);
4476: }
4478: /*@
4479: MatSeqAIJGetCSRAndMemType - Get the CSR arrays and the memory type of the `MATSEQAIJ` matrix
4481: Not Collective; No Fortran Support
4483: Input Parameter:
4484: . mat - a matrix of type `MATSEQAIJ` or its subclasses
4486: Output Parameters:
4487: + i - row map array of the matrix
4488: . j - column index array of the matrix
4489: . a - data array of the matrix
4490: - mtype - memory type of the arrays
4492: Level: developer
4494: Notes:
4495: Any of the output parameters can be `NULL`, in which case the corresponding value is not returned.
4496: If mat is a device matrix, the arrays are on the device. Otherwise, they are on the host.
4498: One can call this routine on a preallocated but not assembled matrix to just get the memory of the CSR underneath the matrix.
4499: If the matrix is assembled, the data array `a` is guaranteed to have the latest values of the matrix.
4501: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJGetArray()`, `MatSeqAIJGetArrayRead()`
4502: @*/
4503: PetscErrorCode MatSeqAIJGetCSRAndMemType(Mat mat, const PetscInt *i[], const PetscInt *j[], PetscScalar *a[], PetscMemType *mtype)
4504: {
4505: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
4507: PetscFunctionBegin;
4508: PetscCheck(mat->preallocated, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "matrix is not preallocated");
4509: if (aij->ops->getcsrandmemtype) {
4510: PetscCall((*aij->ops->getcsrandmemtype)(mat, i, j, a, mtype));
4511: } else {
4512: if (i) *i = aij->i;
4513: if (j) *j = aij->j;
4514: if (a) *a = aij->a;
4515: if (mtype) *mtype = PETSC_MEMTYPE_HOST;
4516: }
4517: PetscFunctionReturn(PETSC_SUCCESS);
4518: }
4520: /*@
4521: MatSeqAIJGetMaxRowNonzeros - returns the maximum number of nonzeros in any row
4523: Not Collective
4525: Input Parameter:
4526: . A - a `MATSEQAIJ` matrix
4528: Output Parameter:
4529: . nz - the maximum number of nonzeros in any row
4531: Level: intermediate
4533: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJRestoreArray()`
4534: @*/
4535: PetscErrorCode MatSeqAIJGetMaxRowNonzeros(Mat A, PetscInt *nz)
4536: {
4537: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;
4539: PetscFunctionBegin;
4540: *nz = aij->rmax;
4541: PetscFunctionReturn(PETSC_SUCCESS);
4542: }
4544: static PetscErrorCode MatCOOStructDestroy_SeqAIJ(PetscCtxRt data)
4545: {
4546: MatCOOStruct_SeqAIJ *coo = *(MatCOOStruct_SeqAIJ **)data;
4548: PetscFunctionBegin;
4549: PetscCall(PetscFree(coo->perm));
4550: PetscCall(PetscFree(coo->jmap));
4551: PetscCall(PetscFree(coo));
4552: PetscFunctionReturn(PETSC_SUCCESS);
4553: }
4555: PetscErrorCode MatSetPreallocationCOO_SeqAIJ(Mat mat, PetscCount coo_n, PetscInt coo_i[], PetscInt coo_j[])
4556: {
4557: MPI_Comm comm;
4558: PetscInt *i, *j;
4559: PetscInt M, N, row, iprev;
4560: PetscCount k, p, q, nneg, nnz, start, end; /* Index the coo array, so use PetscCount as their type */
4561: PetscInt *Ai; /* Change to PetscCount once we use it for row pointers */
4562: PetscInt *Aj;
4563: PetscScalar *Aa = NULL;
4564: Mat_SeqAIJ *seqaij = (Mat_SeqAIJ *)mat->data;
4565: MatType rtype;
4566: PetscCount *perm, *jmap;
4567: MatCOOStruct_SeqAIJ *coo;
4568: PetscBool isorted;
4569: PetscBool hypre;
4571: PetscFunctionBegin;
4572: // The CSR storage built below replaces the hash table, so MatSetValues() must use the regular SeqAIJ operations
4573: if (mat->hash_active) {
4574: mat->ops[0] = seqaij->cops;
4575: PetscCall(PetscHMapIJVDestroy(&seqaij->ht));
4576: PetscCall(PetscFree(seqaij->dnz));
4577: mat->hash_active = PETSC_FALSE;
4578: }
4579: PetscCall(PetscObjectGetComm((PetscObject)mat, &comm));
4580: PetscCall(MatGetSize(mat, &M, &N));
4581: i = coo_i;
4582: j = coo_j;
4583: PetscCall(PetscMalloc1(coo_n, &perm));
4585: /* Ignore entries with negative row or col indices; at the same time, check if i[] is already sorted (e.g., MatConvert_AlJ_HYPRE results in this case) */
4586: isorted = PETSC_TRUE;
4587: iprev = PETSC_INT_MIN;
4588: for (k = 0; k < coo_n; k++) {
4589: if (j[k] < 0) i[k] = -1;
4590: if (isorted) {
4591: if (i[k] < iprev) isorted = PETSC_FALSE;
4592: else iprev = i[k];
4593: }
4594: perm[k] = k;
4595: }
4597: /* Sort by row if not already */
4598: if (!isorted) PetscCall(PetscSortIntWithIntCountArrayPair(coo_n, i, j, perm));
4599: PetscCheck(coo_n == 0 || i[coo_n - 1] < M, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "COO row index %" PetscInt_FMT " is >= the matrix row size %" PetscInt_FMT, i[coo_n - 1], M);
4601: /* Advance k to the first row with a non-negative index */
4602: for (k = 0; k < coo_n; k++)
4603: if (i[k] >= 0) break;
4604: nneg = k;
4605: PetscCall(PetscMalloc1(coo_n - nneg + 1, &jmap)); /* +1 to make a CSR-like data structure. jmap[i] originally is the number of repeats for i-th nonzero */
4606: nnz = 0; /* Total number of unique nonzeros to be counted */
4607: jmap++; /* Inc jmap by 1 for convenience */
4609: PetscCall(PetscShmgetAllocateArray(M + 1, sizeof(PetscInt), (void **)&Ai)); /* CSR of A */
4610: PetscCall(PetscArrayzero(Ai, M + 1));
4611: PetscCall(PetscShmgetAllocateArray(coo_n - nneg, sizeof(PetscInt), (void **)&Aj)); /* We have at most coo_n-nneg unique nonzeros */
4613: PetscCall(PetscStrcmp("_internal_COO_mat_for_hypre", ((PetscObject)mat)->name, &hypre));
4615: /* In each row, sort by column, then unique column indices to get row length */
4616: Ai++; /* Inc by 1 for convenience */
4617: q = 0; /* q-th unique nonzero, with q starting from 0 */
4618: while (k < coo_n) {
4619: PetscBool strictly_sorted; // this row is strictly sorted?
4620: PetscInt jprev;
4622: /* get [start,end) indices for this row; also check if cols in this row are strictly sorted */
4623: row = i[k];
4624: start = k;
4625: jprev = PETSC_INT_MIN;
4626: strictly_sorted = PETSC_TRUE;
4627: while (k < coo_n && i[k] == row) {
4628: if (strictly_sorted) {
4629: if (j[k] <= jprev) strictly_sorted = PETSC_FALSE;
4630: else jprev = j[k];
4631: }
4632: k++;
4633: }
4634: end = k;
4636: /* hack for HYPRE: swap min column to diag so that diagonal values will go first */
4637: if (hypre) {
4638: PetscInt minj = PETSC_INT_MAX;
4639: PetscBool hasdiag = PETSC_FALSE;
4641: if (strictly_sorted) { // fast path to swap the first and the diag
4642: PetscCount tmp;
4643: for (p = start; p < end; p++) {
4644: if (j[p] == row && p != start) {
4645: j[p] = j[start]; // swap j[], so that the diagonal value will go first (manipulated by perm[])
4646: j[start] = row;
4647: tmp = perm[start];
4648: perm[start] = perm[p]; // also swap perm[] so we can save the call to PetscSortIntWithCountArray() below
4649: perm[p] = tmp;
4650: break;
4651: }
4652: }
4653: } else {
4654: for (p = start; p < end; p++) {
4655: hasdiag = (PetscBool)(hasdiag || (j[p] == row));
4656: minj = PetscMin(minj, j[p]);
4657: }
4659: if (hasdiag) {
4660: for (p = start; p < end; p++) {
4661: if (j[p] == minj) j[p] = row;
4662: else if (j[p] == row) j[p] = minj;
4663: }
4664: }
4665: }
4666: }
4667: // sort by columns in a row. perm[] indicates their original order
4668: if (!strictly_sorted) PetscCall(PetscSortIntWithCountArray(end - start, j + start, perm + start));
4669: PetscCheck(end == start || j[end - 1] < N, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "COO column index %" PetscInt_FMT " is >= the matrix column size %" PetscInt_FMT, j[end - 1], N);
4671: if (strictly_sorted) { // fast path to set Aj[], jmap[], Ai[], nnz, q
4672: for (p = start; p < end; p++, q++) {
4673: Aj[q] = j[p];
4674: jmap[q] = 1;
4675: }
4676: PetscCall(PetscIntCast(end - start, Ai + row));
4677: nnz += Ai[row]; // q is already advanced
4678: } else {
4679: /* Find number of unique col entries in this row */
4680: Aj[q] = j[start]; /* Log the first nonzero in this row */
4681: jmap[q] = 1; /* Number of repeats of this nonzero entry */
4682: Ai[row] = 1;
4683: nnz++;
4685: for (p = start + 1; p < end; p++) { /* Scan remaining nonzero in this row */
4686: if (j[p] != j[p - 1]) { /* Meet a new nonzero */
4687: q++;
4688: jmap[q] = 1;
4689: Aj[q] = j[p];
4690: Ai[row]++;
4691: nnz++;
4692: } else {
4693: jmap[q]++;
4694: }
4695: }
4696: q++; /* Move to next row and thus next unique nonzero */
4697: }
4698: }
4700: Ai--; /* Back to the beginning of Ai[] */
4701: for (k = 0; k < M; k++) Ai[k + 1] += Ai[k];
4702: jmap--; // Back to the beginning of jmap[]
4703: jmap[0] = 0;
4704: for (k = 0; k < nnz; k++) jmap[k + 1] += jmap[k];
4706: if (nnz < coo_n - nneg) { /* Reallocate with actual number of unique nonzeros */
4707: PetscCount *jmap_new;
4708: PetscInt *Aj_new;
4710: PetscCall(PetscMalloc1(nnz + 1, &jmap_new));
4711: PetscCall(PetscArraycpy(jmap_new, jmap, nnz + 1));
4712: PetscCall(PetscFree(jmap));
4713: jmap = jmap_new;
4715: PetscCall(PetscShmgetAllocateArray(nnz, sizeof(PetscInt), (void **)&Aj_new));
4716: PetscCall(PetscArraycpy(Aj_new, Aj, nnz));
4717: PetscCall(PetscShmgetDeallocateArray((void **)&Aj));
4718: Aj = Aj_new;
4719: }
4721: if (nneg) { /* Discard heading entries with negative indices in perm[], as we'll access it from index 0 in MatSetValuesCOO */
4722: PetscCount *perm_new;
4724: PetscCall(PetscMalloc1(coo_n - nneg, &perm_new));
4725: PetscCall(PetscArraycpy(perm_new, perm + nneg, coo_n - nneg));
4726: PetscCall(PetscFree(perm));
4727: perm = perm_new;
4728: }
4730: PetscCall(MatGetRootType_Private(mat, &rtype));
4731: if (!mat->structure_only) {
4732: PetscCall(PetscShmgetAllocateArray(nnz, sizeof(PetscScalar), (void **)&Aa));
4733: PetscCall(PetscArrayzero(Aa, nnz));
4734: }
4735: PetscCall(MatSetSeqAIJWithArrays_private(PETSC_COMM_SELF, M, N, Ai, Aj, Aa, rtype, mat));
4737: seqaij->free_a = (PetscBool)!mat->structure_only;
4738: seqaij->free_ij = PETSC_TRUE; /* Let mat own Ai, Aj and any allocated Aa */
4740: // Put the COO struct in a container and then attach that to the matrix
4741: PetscCall(PetscMalloc1(1, &coo));
4742: PetscCall(PetscIntCast(nnz, &coo->nz));
4743: coo->n = coo_n;
4744: coo->Atot = coo_n - nneg; // Annz is seqaij->nz, so no need to record that again
4745: coo->jmap = jmap; // of length nnz+1
4746: coo->perm = perm;
4747: // MatSetPreallocationCOO() increments the nonzero state once more after this routine returns; if that changes, every
4748: // MatSetValuesCOO() fails the check in MatSeqAIJCheckCOONonzeroState(), so a mismatch cannot go unnoticed
4749: coo->nonzerostate = mat->nonzerostate + 1;
4750: PetscCall(PetscObjectContainerCompose((PetscObject)mat, "__PETSc_MatCOOStruct_Host", coo, MatCOOStructDestroy_SeqAIJ));
4751: PetscFunctionReturn(PETSC_SUCCESS);
4752: }
4754: // PetscClangLinter pragma disable: -fdoc-internal-linkage
4755: /*@
4756: MatSeqAIJCheckCOONonzeroState - Checks that the COO maps built by `MatSetPreallocationCOO()` can still be used by
4757: `MatSetValuesCOO()` on a `MATSEQAIJ` matrix
4759: Not Collective
4761: Input Parameter:
4762: . A - the `MATSEQAIJ` matrix
4764: Level: developer
4766: Notes:
4767: The maps address the nonzeros of the matrix by position, so they are only valid while its nonzero pattern is the one built by
4768: `MatSetPreallocationCOO()`. Any change to the pattern, for example a new nonzero inserted with `MatSetValues()`, would make
4769: `MatSetValuesCOO()` write values into the wrong locations, so this routine generates an error instead. It also generates an
4770: error when `MatSetPreallocationCOO()` has not been called.
4772: The `MatSetValuesCOO()` implementations of `MATSEQAIJ` and its device subtypes call this routine before using the maps.
4774: .seealso: [](ch_matrices), `Mat`, `MATSEQAIJ`, `MatSetPreallocationCOO()`, `MatSetValuesCOO()`, `MatMPIAIJCheckCOONonzeroState()`
4775: @*/
4776: PetscErrorCode MatSeqAIJCheckCOONonzeroState(Mat A)
4777: {
4778: PetscContainer container;
4779: MatCOOStruct_SeqAIJ *coo;
4781: PetscFunctionBegin;
4782: PetscCall(PetscObjectQuery((PetscObject)A, "__PETSc_MatCOOStruct_Host", (PetscObject *)&container));
4783: PetscCheck(container, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "MatSetPreallocationCOO() must be called before MatSetValuesCOO()");
4784: PetscCall(PetscContainerGetPointer(container, &coo));
4785: PetscCheck(coo->nonzerostate == A->nonzerostate, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "The nonzero pattern of the matrix has changed since MatSetPreallocationCOO(), for example because MatSetValues() inserted a new nonzero; call MatSetPreallocationCOO() again before MatSetValuesCOO(), or stop inserting new nonzeros with MatSetValues()");
4786: PetscFunctionReturn(PETSC_SUCCESS);
4787: }
4789: static PetscErrorCode MatSetValuesCOO_SeqAIJ(Mat A, const PetscScalar v[], InsertMode imode)
4790: {
4791: Mat_SeqAIJ *aseq = (Mat_SeqAIJ *)A->data;
4792: PetscCount i, j, Annz = aseq->nz;
4793: PetscCount *perm, *jmap;
4794: PetscScalar *Aa;
4795: PetscContainer container;
4796: MatCOOStruct_SeqAIJ *coo;
4798: PetscFunctionBegin;
4799: PetscCall(MatSeqAIJCheckCOONonzeroState(A));
4800: PetscCall(PetscObjectQuery((PetscObject)A, "__PETSc_MatCOOStruct_Host", (PetscObject *)&container));
4801: PetscCall(PetscContainerGetPointer(container, &coo));
4802: perm = coo->perm;
4803: jmap = coo->jmap;
4804: PetscCall(MatSeqAIJGetArray(A, &Aa));
4805: for (i = 0; i < Annz; i++) {
4806: PetscScalar sum = 0.0;
4807: for (j = jmap[i]; j < jmap[i + 1]; j++) sum += v[perm[j]];
4808: Aa[i] = (imode == INSERT_VALUES ? 0.0 : Aa[i]) + sum;
4809: }
4810: PetscCall(MatSeqAIJRestoreArray(A, &Aa));
4811: PetscFunctionReturn(PETSC_SUCCESS);
4812: }
4814: #if PetscDefined(HAVE_CUDA)
4815: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCUSPARSE(Mat, MatType, MatReuse, Mat *);
4816: #endif
4817: #if PetscDefined(HAVE_HIP)
4818: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJHIPSPARSE(Mat, MatType, MatReuse, Mat *);
4819: #endif
4820: #if PetscDefined(HAVE_KOKKOS_KERNELS)
4821: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJKokkos(Mat, MatType, MatReuse, Mat *);
4822: #endif
4824: PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJ(Mat B)
4825: {
4826: Mat_SeqAIJ *b;
4827: PetscMPIInt size;
4829: PetscFunctionBegin;
4830: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)B), &size));
4831: PetscCheck(size <= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Comm must be of size 1");
4833: PetscCall(PetscNew(&b));
4835: B->data = (void *)b;
4836: B->ops[0] = MatOps_Values;
4837: if (B->sortedfull) B->ops->setvalues = MatSetValues_SeqAIJ_SortedFull;
4839: b->row = NULL;
4840: b->col = NULL;
4841: b->icol = NULL;
4842: b->reallocs = 0;
4843: b->ignorezeroentries = PETSC_FALSE;
4844: b->roworiented = PETSC_TRUE;
4845: b->nonew = 0;
4846: b->diag = NULL;
4847: b->solve_work = NULL;
4848: B->spptr = NULL;
4849: b->saved_values = NULL;
4850: b->idiag = NULL;
4851: b->mdiag = NULL;
4852: b->ssor_work = NULL;
4853: b->omega = 1.0;
4854: b->fshift = 0.0;
4855: b->ibdiag = NULL;
4856: b->keepnonzeropattern = PETSC_FALSE;
4858: PetscCall(PetscObjectChangeTypeName((PetscObject)B, MATSEQAIJ));
4859: #if PetscDefined(HAVE_MATLAB)
4860: PetscCall(PetscObjectComposeFunction((PetscObject)B, "PetscMatlabEnginePut_C", MatlabEnginePut_SeqAIJ));
4861: PetscCall(PetscObjectComposeFunction((PetscObject)B, "PetscMatlabEngineGet_C", MatlabEngineGet_SeqAIJ));
4862: #endif
4863: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSeqAIJSetColumnIndices_C", MatSeqAIJSetColumnIndices_SeqAIJ));
4864: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatStoreValues_C", MatStoreValues_SeqAIJ));
4865: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatRetrieveValues_C", MatRetrieveValues_SeqAIJ));
4866: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqsbaij_C", MatConvert_SeqAIJ_SeqSBAIJ));
4867: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqbaij_C", MatConvert_SeqAIJ_SeqBAIJ));
4868: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqaijperm_C", MatConvert_SeqAIJ_SeqAIJPERM));
4869: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqaijsell_C", MatConvert_SeqAIJ_SeqAIJSELL));
4870: #if PetscDefined(HAVE_MKL_SPARSE)
4871: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqaijmkl_C", MatConvert_SeqAIJ_SeqAIJMKL));
4872: #endif
4873: #if PetscDefined(HAVE_CUDA)
4874: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqaijcusparse_C", MatConvert_SeqAIJ_SeqAIJCUSPARSE));
4875: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqaijcusparse_seqaij_C", MatProductSetFromOptions_SeqAIJ));
4876: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqaij_seqaijcusparse_C", MatProductSetFromOptions_SeqAIJ));
4877: #endif
4878: #if PetscDefined(HAVE_HIP)
4879: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqaijhipsparse_C", MatConvert_SeqAIJ_SeqAIJHIPSPARSE));
4880: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqaijhipsparse_seqaij_C", MatProductSetFromOptions_SeqAIJ));
4881: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqaij_seqaijhipsparse_C", MatProductSetFromOptions_SeqAIJ));
4882: #endif
4883: #if PetscDefined(HAVE_KOKKOS_KERNELS)
4884: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqaijkokkos_C", MatConvert_SeqAIJ_SeqAIJKokkos));
4885: #endif
4886: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqaijcrl_C", MatConvert_SeqAIJ_SeqAIJCRL));
4887: #if PetscDefined(HAVE_ELEMENTAL)
4888: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_elemental_C", MatConvert_SeqAIJ_Elemental));
4889: #endif
4890: #if PetscDefined(HAVE_SCALAPACK) && (PetscDefined(USE_REAL_SINGLE) || PetscDefined(USE_REAL_DOUBLE))
4891: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_scalapack_C", MatConvert_AIJ_ScaLAPACK));
4892: #endif
4893: #if PetscDefined(HAVE_HYPRE)
4894: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_hypre_C", MatConvert_AIJ_HYPRE));
4895: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_transpose_seqaij_seqaij_C", MatProductSetFromOptions_Transpose_AIJ_AIJ));
4896: #endif
4897: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqdense_C", MatConvert_SeqAIJ_SeqDense));
4898: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqsell_C", MatConvert_SeqAIJ_SeqSELL));
4899: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_is_C", MatConvert_XAIJ_IS));
4900: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatIsTranspose_C", MatIsTranspose_SeqAIJ));
4901: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatIsHermitianTranspose_C", MatIsHermitianTranspose_SeqAIJ));
4902: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSeqAIJSetPreallocation_C", MatSeqAIJSetPreallocation_SeqAIJ));
4903: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatResetPreallocation_C", MatResetPreallocation_SeqAIJ));
4904: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatResetHash_C", MatResetHash_SeqAIJ));
4905: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSeqAIJSetPreallocationCSR_C", MatSeqAIJSetPreallocationCSR_SeqAIJ));
4906: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatReorderForNonzeroDiagonal_C", MatReorderForNonzeroDiagonal_SeqAIJ));
4907: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_is_seqaij_C", MatProductSetFromOptions_IS_XAIJ));
4908: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqdense_seqaij_C", MatProductSetFromOptions_SeqDense_SeqAIJ));
4909: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqaij_seqaij_C", MatProductSetFromOptions_SeqAIJ));
4910: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSeqAIJKron_C", MatSeqAIJKron_SeqAIJ));
4911: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSetPreallocationCOO_C", MatSetPreallocationCOO_SeqAIJ));
4912: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSetValuesCOO_C", MatSetValuesCOO_SeqAIJ));
4913: PetscCall(MatCreate_SeqAIJ_Inode(B));
4914: PetscCall(PetscObjectChangeTypeName((PetscObject)B, MATSEQAIJ));
4915: PetscCall(MatSeqAIJSetTypeFromOptions(B)); /* this allows changing the matrix subtype to say MATSEQAIJPERM */
4916: PetscFunctionReturn(PETSC_SUCCESS);
4917: }
4919: /*
4920: Given a matrix generated with MatGetFactor() duplicates all the information in A into C
4921: */
4922: PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C, Mat A, MatDuplicateOption cpvalues, PetscBool mallocmatspace)
4923: {
4924: Mat_SeqAIJ *c = (Mat_SeqAIJ *)C->data, *a = (Mat_SeqAIJ *)A->data;
4925: PetscInt m = A->rmap->n, i;
4927: PetscFunctionBegin;
4928: PetscCheck(A->assembled || cpvalues == MAT_DO_NOT_COPY_VALUES, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Cannot duplicate unassembled matrix");
4929: PetscCall(MatSetOption(C, MAT_STRUCTURE_ONLY, A->structure_only));
4931: C->factortype = A->factortype;
4932: c->row = NULL;
4933: c->col = NULL;
4934: c->icol = NULL;
4935: c->reallocs = 0;
4936: C->assembled = A->assembled;
4938: if (A->preallocated) {
4939: PetscCall(PetscLayoutReference(A->rmap, &C->rmap));
4940: PetscCall(PetscLayoutReference(A->cmap, &C->cmap));
4942: if (!A->hash_active) {
4943: PetscCall(PetscMalloc1(m, &c->imax));
4944: PetscCall(PetscArraycpy(c->imax, a->imax, m));
4945: PetscCall(PetscMalloc1(m, &c->ilen));
4946: PetscCall(PetscArraycpy(c->ilen, a->ilen, m));
4948: /* allocate the matrix space */
4949: if (mallocmatspace) {
4950: if (!A->structure_only) PetscCall(PetscShmgetAllocateArray(a->i[m], sizeof(PetscScalar), (void **)&c->a));
4951: PetscCall(PetscShmgetAllocateArray(a->i[m], sizeof(PetscInt), (void **)&c->j));
4952: PetscCall(PetscShmgetAllocateArray(m + 1, sizeof(PetscInt), (void **)&c->i));
4953: PetscCall(PetscArraycpy(c->i, a->i, m + 1));
4954: c->free_a = PETSC_TRUE;
4955: c->free_ij = PETSC_TRUE;
4956: if (m > 0) {
4957: PetscCall(PetscArraycpy(c->j, a->j, a->i[m]));
4958: if (!A->structure_only) {
4959: if (cpvalues == MAT_COPY_VALUES) {
4960: const PetscScalar *aa;
4962: PetscCall(MatSeqAIJGetArrayRead(A, &aa));
4963: PetscCall(PetscArraycpy(c->a, aa, a->i[m]));
4964: PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
4965: } else PetscCall(PetscArrayzero(c->a, a->i[m]));
4966: }
4967: }
4968: }
4969: C->preallocated = PETSC_TRUE;
4970: } else {
4971: PetscCheck(mallocmatspace, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONGSTATE, "Cannot malloc matrix memory from a non-preallocated matrix");
4972: PetscCall(MatSetUp(C));
4973: }
4975: c->ignorezeroentries = a->ignorezeroentries;
4976: c->roworiented = a->roworiented;
4977: c->nonew = a->nonew;
4978: c->solve_work = NULL;
4979: c->saved_values = NULL;
4980: c->idiag = NULL;
4981: c->ssor_work = NULL;
4982: c->keepnonzeropattern = a->keepnonzeropattern;
4984: c->rmax = a->rmax;
4985: c->nz = a->nz;
4986: c->maxnz = a->nz; /* Since we allocate exactly the right amount */
4988: c->compressedrow.use = a->compressedrow.use;
4989: c->compressedrow.nrows = a->compressedrow.nrows;
4990: if (a->compressedrow.use) {
4991: i = a->compressedrow.nrows;
4992: PetscCall(PetscMalloc2(i + 1, &c->compressedrow.i, i, &c->compressedrow.rindex));
4993: PetscCall(PetscArraycpy(c->compressedrow.i, a->compressedrow.i, i + 1));
4994: PetscCall(PetscArraycpy(c->compressedrow.rindex, a->compressedrow.rindex, i));
4995: } else {
4996: c->compressedrow.use = PETSC_FALSE;
4997: c->compressedrow.i = NULL;
4998: c->compressedrow.rindex = NULL;
4999: }
5000: c->nonzerorowcnt = a->nonzerorowcnt;
5001: C->nonzerostate = A->nonzerostate;
5003: PetscCall(MatDuplicate_SeqAIJ_Inode(A, cpvalues, &C));
5004: }
5005: PetscCall(PetscFunctionListDuplicate(((PetscObject)A)->qlist, &((PetscObject)C)->qlist));
5006: PetscFunctionReturn(PETSC_SUCCESS);
5007: }
5009: PetscErrorCode MatDuplicate_SeqAIJ(Mat A, MatDuplicateOption cpvalues, Mat *B)
5010: {
5011: PetscFunctionBegin;
5012: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), B));
5013: PetscCall(MatSetSizes(*B, A->rmap->n, A->cmap->n, A->rmap->n, A->cmap->n));
5014: if (!(A->rmap->n % A->rmap->bs) && !(A->cmap->n % A->cmap->bs)) PetscCall(MatSetBlockSizesFromMats(*B, A, A));
5015: PetscCall(MatSetType(*B, ((PetscObject)A)->type_name));
5016: PetscCall(MatDuplicateNoCreate_SeqAIJ(*B, A, cpvalues, PETSC_TRUE));
5017: PetscFunctionReturn(PETSC_SUCCESS);
5018: }
5020: PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer)
5021: {
5022: PetscBool isbinary, ishdf5;
5024: PetscFunctionBegin;
5027: /* force binary viewer to load .info file if it has not yet done so */
5028: PetscCall(PetscViewerSetUp(viewer));
5029: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
5030: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
5031: if (isbinary) {
5032: PetscCall(MatLoad_SeqAIJ_Binary(newMat, viewer));
5033: } else if (ishdf5) {
5034: #if PetscDefined(HAVE_HDF5)
5035: PetscCall(MatLoad_AIJ_HDF5(newMat, viewer));
5036: #else
5037: SETERRQ(PetscObjectComm((PetscObject)newMat), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
5038: #endif
5039: } else {
5040: SETERRQ(PetscObjectComm((PetscObject)newMat), PETSC_ERR_SUP, "Viewer type %s not yet supported for reading %s matrices", ((PetscObject)viewer)->type_name, ((PetscObject)newMat)->type_name);
5041: }
5042: PetscFunctionReturn(PETSC_SUCCESS);
5043: }
5045: PetscErrorCode MatLoad_SeqAIJ_Binary(Mat mat, PetscViewer viewer)
5046: {
5047: Mat_SeqAIJ *a = (Mat_SeqAIJ *)mat->data;
5048: PetscInt header[4], *rowlens, M, N, nz, sum, rows, cols, i;
5050: PetscFunctionBegin;
5051: PetscCall(PetscViewerSetUp(viewer));
5053: /* read in matrix header */
5054: PetscCall(PetscViewerBinaryRead(viewer, header, 4, NULL, PETSC_INT));
5055: PetscCheck(header[0] == MAT_FILE_CLASSID, PETSC_COMM_SELF, PETSC_ERR_FILE_UNEXPECTED, "Not a matrix object in file");
5056: M = header[1];
5057: N = header[2];
5058: nz = header[3];
5059: PetscCheck(M >= 0, PetscObjectComm((PetscObject)viewer), PETSC_ERR_FILE_UNEXPECTED, "Matrix row size (%" PetscInt_FMT ") in file is negative", M);
5060: PetscCheck(N >= 0, PetscObjectComm((PetscObject)viewer), PETSC_ERR_FILE_UNEXPECTED, "Matrix column size (%" PetscInt_FMT ") in file is negative", N);
5061: PetscCheck(nz >= 0, PETSC_COMM_SELF, PETSC_ERR_FILE_UNEXPECTED, "Matrix stored in special format on disk, cannot load as SeqAIJ");
5063: /* set block sizes from the viewer's .info file */
5064: PetscCall(MatLoad_Binary_BlockSizes(mat, viewer));
5065: /* set local and global sizes if not set already */
5066: if (mat->rmap->n < 0) mat->rmap->n = M;
5067: if (mat->cmap->n < 0) mat->cmap->n = N;
5068: if (mat->rmap->N < 0) mat->rmap->N = M;
5069: if (mat->cmap->N < 0) mat->cmap->N = N;
5070: PetscCall(PetscLayoutSetUp(mat->rmap));
5071: PetscCall(PetscLayoutSetUp(mat->cmap));
5073: /* check if the matrix sizes are correct */
5074: PetscCall(MatGetSize(mat, &rows, &cols));
5075: 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);
5077: /* read in row lengths */
5078: PetscCall(PetscMalloc1(M, &rowlens));
5079: PetscCall(PetscViewerBinaryRead(viewer, rowlens, M, NULL, PETSC_INT));
5080: /* check if sum(rowlens) is same as nz */
5081: sum = 0;
5082: for (i = 0; i < M; i++) sum += rowlens[i];
5083: PetscCheck(sum == nz, PETSC_COMM_SELF, PETSC_ERR_FILE_UNEXPECTED, "Inconsistent matrix data in file: nonzeros = %" PetscInt_FMT ", sum-row-lengths = %" PetscInt_FMT, nz, sum);
5084: /* preallocate and check sizes */
5085: PetscCall(MatSeqAIJSetPreallocation_SeqAIJ(mat, 0, rowlens));
5086: PetscCall(MatGetSize(mat, &rows, &cols));
5087: PetscCheck(M == rows && N == cols, PETSC_COMM_SELF, PETSC_ERR_FILE_UNEXPECTED, "Matrix in file of different length (%" PetscInt_FMT ", %" PetscInt_FMT ") than the input matrix (%" PetscInt_FMT ", %" PetscInt_FMT ")", M, N, rows, cols);
5088: /* store row lengths */
5089: PetscCall(PetscArraycpy(a->ilen, rowlens, M));
5090: PetscCall(PetscFree(rowlens));
5092: /* fill in "i" row pointers */
5093: a->i[0] = 0;
5094: for (i = 0; i < M; i++) a->i[i + 1] = a->i[i] + a->ilen[i];
5095: /* read in "j" column indices */
5096: PetscCall(PetscViewerBinaryRead(viewer, a->j, nz, NULL, PETSC_INT));
5097: /* read in "a" nonzero values */
5098: PetscCall(PetscViewerBinaryRead(viewer, a->a, nz, NULL, PETSC_SCALAR));
5100: PetscCall(MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY));
5101: PetscCall(MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY));
5102: PetscFunctionReturn(PETSC_SUCCESS);
5103: }
5105: PetscErrorCode MatEqual_SeqAIJ(Mat A, Mat B, PetscBool *flg)
5106: {
5107: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data, *b = (Mat_SeqAIJ *)B->data;
5108: const PetscScalar *aa, *ba;
5110: PetscFunctionBegin;
5111: /* If the matrix dimensions are not equal,or no of nonzeros */
5112: if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) || (a->nz != b->nz)) {
5113: *flg = PETSC_FALSE;
5114: PetscFunctionReturn(PETSC_SUCCESS);
5115: }
5117: /* if the a->i are the same */
5118: PetscCall(PetscArraycmp(a->i, b->i, A->rmap->n + 1, flg));
5119: if (!*flg) PetscFunctionReturn(PETSC_SUCCESS);
5121: /* if a->j are the same */
5122: PetscCall(PetscArraycmp(a->j, b->j, a->nz, flg));
5123: if (!*flg) PetscFunctionReturn(PETSC_SUCCESS);
5125: PetscCall(MatSeqAIJGetArrayRead(A, &aa));
5126: PetscCall(MatSeqAIJGetArrayRead(B, &ba));
5127: /* if a->a are the same */
5128: PetscCall(PetscArraycmp(aa, ba, a->nz, flg));
5129: PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
5130: PetscCall(MatSeqAIJRestoreArrayRead(B, &ba));
5131: PetscFunctionReturn(PETSC_SUCCESS);
5132: }
5134: /*@
5135: MatCreateSeqAIJWithArrays - Creates an sequential `MATSEQAIJ` matrix using matrix elements (in CSR format)
5136: provided by the user.
5138: Collective
5140: Input Parameters:
5141: + comm - must be an MPI communicator of size 1
5142: . m - number of rows
5143: . n - number of columns
5144: . i - row indices; that is i[0] = 0, i[row] = i[row-1] + number of elements in that row of the matrix
5145: . j - column indices
5146: - a - matrix values
5148: Output Parameter:
5149: . mat - the matrix
5151: Level: intermediate
5153: Notes:
5154: The `i`, `j`, and `a` arrays are not copied by this routine, the user must free these arrays
5155: once the matrix is destroyed and not before
5157: You cannot set new nonzero locations into this matrix, that will generate an error.
5159: The `i` and `j` indices are 0 based
5161: The format which is used for the sparse matrix input, is equivalent to a
5162: row-major ordering.. i.e for the following matrix, the input data expected is
5163: as shown
5164: .vb
5165: 1 0 0
5166: 2 0 3
5167: 4 5 6
5169: i = {0,1,3,6} [size = nrow+1 = 3+1]
5170: j = {0,0,2,0,1,2} [size = 6]; values must be sorted for each row
5171: v = {1,2,3,4,5,6} [size = 6]
5172: .ve
5174: .seealso: [](ch_matrices), `Mat`, `MatCreate()`, `MatCreateAIJ()`, `MatCreateSeqAIJ()`, `MatCreateMPIAIJWithArrays()`, `MatMPIAIJSetPreallocationCSR()`
5175: @*/
5176: PetscErrorCode MatCreateSeqAIJWithArrays(MPI_Comm comm, PetscInt m, PetscInt n, PetscInt i[], PetscInt j[], PetscScalar a[], Mat *mat)
5177: {
5178: PetscInt ii;
5179: Mat_SeqAIJ *aij;
5181: PetscFunctionBegin;
5182: PetscCheck(m <= 0 || i[0] == 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "i (row indices) must start with 0");
5183: PetscCall(MatCreate(comm, mat));
5184: PetscCall(MatSetSizes(*mat, m, n, m, n));
5185: /* PetscCall(MatSetBlockSizes(*mat,,)); */
5186: PetscCall(MatSetType(*mat, MATSEQAIJ));
5187: PetscCall(MatSeqAIJSetPreallocation_SeqAIJ(*mat, MAT_SKIP_ALLOCATION, NULL));
5188: aij = (Mat_SeqAIJ *)(*mat)->data;
5189: PetscCall(PetscMalloc1(m, &aij->imax));
5190: PetscCall(PetscMalloc1(m, &aij->ilen));
5192: aij->i = i;
5193: aij->j = j;
5194: aij->a = a;
5195: aij->nonew = -1; /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
5196: aij->free_a = PETSC_FALSE;
5197: aij->free_ij = PETSC_FALSE;
5199: for (ii = 0, aij->nonzerorowcnt = 0, aij->rmax = 0; ii < m; ii++) {
5200: aij->ilen[ii] = aij->imax[ii] = i[ii + 1] - i[ii];
5201: if (PetscDefined(USE_DEBUG)) {
5202: PetscCheck(i[ii + 1] - i[ii] >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Negative row length in i (row indices) row = %" PetscInt_FMT " length = %" PetscInt_FMT, ii, i[ii + 1] - i[ii]);
5203: for (PetscInt jj = i[ii] + 1; jj < i[ii + 1]; jj++) {
5204: PetscCheck(j[jj] >= j[jj - 1], PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Column entry number %" PetscInt_FMT " (actual column %" PetscInt_FMT ") in row %" PetscInt_FMT " is not sorted", jj - i[ii], j[jj], ii);
5205: PetscCheck(j[jj] != j[jj - 1], PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Column entry number %" PetscInt_FMT " (actual column %" PetscInt_FMT ") in row %" PetscInt_FMT " is identical to previous entry", jj - i[ii], j[jj], ii);
5206: }
5207: }
5208: }
5209: if (PetscDefined(USE_DEBUG)) {
5210: for (ii = 0; ii < aij->i[m]; ii++) {
5211: PetscCheck(j[ii] >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Negative column index at location = %" PetscInt_FMT " index = %" PetscInt_FMT, ii, j[ii]);
5212: PetscCheck(j[ii] <= n - 1, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Column index to large at location = %" PetscInt_FMT " index = %" PetscInt_FMT " last column = %" PetscInt_FMT, ii, j[ii], n - 1);
5213: }
5214: }
5216: PetscCall(MatAssemblyBegin(*mat, MAT_FINAL_ASSEMBLY));
5217: PetscCall(MatAssemblyEnd(*mat, MAT_FINAL_ASSEMBLY));
5218: PetscFunctionReturn(PETSC_SUCCESS);
5219: }
5221: /*@
5222: MatCreateSeqAIJFromTriple - Creates an sequential `MATSEQAIJ` matrix using matrix elements (in COO format)
5223: provided by the user.
5225: Collective
5227: Input Parameters:
5228: + comm - must be an MPI communicator of size 1
5229: . m - number of rows
5230: . n - number of columns
5231: . i - row indices
5232: . j - column indices
5233: . a - matrix values
5234: . nz - number of nonzeros
5235: - idx - if the `i` and `j` indices start with 1 use `PETSC_TRUE` otherwise use `PETSC_FALSE`
5237: Output Parameter:
5238: . mat - the matrix
5240: Level: intermediate
5242: Example:
5243: For the following matrix, the input data expected is as shown (using 0 based indexing)
5244: .vb
5245: 1 0 0
5246: 2 0 3
5247: 4 5 6
5249: i = {0,1,1,2,2,2}
5250: j = {0,0,2,0,1,2}
5251: v = {1,2,3,4,5,6}
5252: .ve
5254: Note:
5255: Instead of using this function, users should also consider `MatSetPreallocationCOO()` and `MatSetValuesCOO()`, which allow repeated or remote entries,
5256: and are particularly useful in iterative applications.
5258: .seealso: [](ch_matrices), `Mat`, `MatCreate()`, `MatCreateAIJ()`, `MatCreateSeqAIJ()`, `MatCreateSeqAIJWithArrays()`, `MatMPIAIJSetPreallocationCSR()`, `MatSetValuesCOO()`, `MatSetPreallocationCOO()`
5259: @*/
5260: PetscErrorCode MatCreateSeqAIJFromTriple(MPI_Comm comm, PetscInt m, PetscInt n, PetscInt i[], PetscInt j[], PetscScalar a[], Mat *mat, PetscCount nz, PetscBool idx)
5261: {
5262: PetscInt ii, *nnz, one = 1, row, col;
5264: PetscFunctionBegin;
5265: PetscCall(PetscCalloc1(m, &nnz));
5266: for (ii = 0; ii < nz; ii++) nnz[i[ii] - !!idx] += 1;
5267: PetscCall(MatCreate(comm, mat));
5268: PetscCall(MatSetSizes(*mat, m, n, m, n));
5269: PetscCall(MatSetType(*mat, MATSEQAIJ));
5270: PetscCall(MatSeqAIJSetPreallocation_SeqAIJ(*mat, 0, nnz));
5271: for (ii = 0; ii < nz; ii++) {
5272: if (idx) {
5273: row = i[ii] - 1;
5274: col = j[ii] - 1;
5275: } else {
5276: row = i[ii];
5277: col = j[ii];
5278: }
5279: PetscCall(MatSetValues(*mat, one, &row, one, &col, &a[ii], ADD_VALUES));
5280: }
5281: PetscCall(MatAssemblyBegin(*mat, MAT_FINAL_ASSEMBLY));
5282: PetscCall(MatAssemblyEnd(*mat, MAT_FINAL_ASSEMBLY));
5283: PetscCall(PetscFree(nnz));
5284: PetscFunctionReturn(PETSC_SUCCESS);
5285: }
5287: PetscErrorCode MatCreateMPIMatConcatenateSeqMat_SeqAIJ(MPI_Comm comm, Mat inmat, PetscInt n, MatReuse scall, Mat *outmat)
5288: {
5289: PetscFunctionBegin;
5290: PetscCall(MatCreateMPIMatConcatenateSeqMat_MPIAIJ(comm, inmat, n, scall, outmat));
5291: PetscFunctionReturn(PETSC_SUCCESS);
5292: }
5294: /*
5295: Permute A into C's *local* index space using rowemb,colemb.
5296: The embedding are supposed to be injections and the above implies that the range of rowemb is a subset
5297: of [0,m), colemb is in [0,n).
5298: If pattern == DIFFERENT_NONZERO_PATTERN, C is preallocated according to A.
5299: */
5300: PetscErrorCode MatSetSeqMat_SeqAIJ(Mat C, IS rowemb, IS colemb, MatStructure pattern, Mat B)
5301: {
5302: /* If making this function public, change the error returned in this function away from _PLIB. */
5303: Mat_SeqAIJ *Baij;
5304: PetscBool seqaij;
5305: PetscInt m, n, *nz, i, j, count;
5306: PetscScalar v;
5307: const PetscInt *rowindices, *colindices;
5309: PetscFunctionBegin;
5310: if (!B) PetscFunctionReturn(PETSC_SUCCESS);
5311: /* Check to make sure the target matrix (and embeddings) are compatible with C and each other. */
5312: PetscCall(PetscObjectBaseTypeCompare((PetscObject)B, MATSEQAIJ, &seqaij));
5313: PetscCheck(seqaij, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Input matrix is of wrong type");
5314: if (rowemb) {
5315: PetscCall(ISGetLocalSize(rowemb, &m));
5316: PetscCheck(m == B->rmap->n, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Row IS of size %" PetscInt_FMT " is incompatible with matrix row size %" PetscInt_FMT, m, B->rmap->n);
5317: } else PetscCheck(C->rmap->n == B->rmap->n, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Input matrix is row-incompatible with the target matrix");
5318: if (colemb) {
5319: PetscCall(ISGetLocalSize(colemb, &n));
5320: PetscCheck(n == B->cmap->n, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Diag col IS of size %" PetscInt_FMT " is incompatible with input matrix col size %" PetscInt_FMT, n, B->cmap->n);
5321: } else PetscCheck(C->cmap->n == B->cmap->n, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Input matrix is col-incompatible with the target matrix");
5323: Baij = (Mat_SeqAIJ *)B->data;
5324: rowindices = NULL;
5325: if (rowemb) PetscCall(ISGetIndices(rowemb, &rowindices));
5326: if (pattern == DIFFERENT_NONZERO_PATTERN) {
5327: PetscCall(PetscMalloc1(C->rmap->n, &nz));
5328: if (rowemb) {
5329: PetscCall(PetscArrayzero(nz, C->rmap->n));
5330: for (i = 0; i < B->rmap->n; i++) nz[rowindices[i]] = Baij->i[i + 1] - Baij->i[i];
5331: } else {
5332: for (i = 0; i < B->rmap->n; i++) nz[i] = Baij->i[i + 1] - Baij->i[i];
5333: }
5334: PetscCall(MatSeqAIJSetPreallocation(C, 0, nz));
5335: PetscCall(PetscFree(nz));
5336: }
5337: if (pattern == SUBSET_NONZERO_PATTERN) PetscCall(MatZeroEntries(C));
5338: count = 0;
5339: colindices = NULL;
5340: if (colemb) PetscCall(ISGetIndices(colemb, &colindices));
5341: for (i = 0; i < B->rmap->n; i++) {
5342: PetscInt row;
5343: row = i;
5344: if (rowindices) row = rowindices[i];
5345: for (j = Baij->i[i]; j < Baij->i[i + 1]; j++) {
5346: PetscInt col;
5347: col = Baij->j[count];
5348: if (colindices) col = colindices[col];
5349: v = Baij->a[count];
5350: PetscCall(MatSetValues(C, 1, &row, 1, &col, &v, INSERT_VALUES));
5351: ++count;
5352: }
5353: }
5354: if (colemb) PetscCall(ISRestoreIndices(colemb, &colindices));
5355: if (rowemb) PetscCall(ISRestoreIndices(rowemb, &rowindices));
5356: /* FIXME: set C's nonzerostate correctly. */
5357: /* Assembly for C is necessary. */
5358: C->preallocated = PETSC_TRUE;
5359: C->assembled = PETSC_TRUE;
5360: C->was_assembled = PETSC_FALSE;
5361: PetscFunctionReturn(PETSC_SUCCESS);
5362: }
5364: PetscErrorCode MatEliminateZeros_SeqAIJ(Mat A, PetscBool keep)
5365: {
5366: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
5367: MatScalar *aa = a->a;
5368: PetscInt m = A->rmap->n, fshift = 0, fshift_prev = 0, i, k;
5369: PetscInt *ailen = a->ilen, *imax = a->imax, *ai = a->i, *aj = a->j, rmax = 0;
5371: PetscFunctionBegin;
5372: PetscCheck(A->assembled, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Cannot eliminate zeros for unassembled matrix");
5373: if (m) rmax = ailen[0]; /* determine row with most nonzeros */
5374: for (i = 1, a->nonzerorowcnt = 0; i <= m; i++) {
5375: /* move each nonzero entry back by the amount of zero slots (fshift) before it*/
5376: for (k = ai[i - 1]; k < ai[i]; k++) {
5377: if (aa[k] == 0 && (aj[k] != i - 1 || !keep)) fshift++;
5378: else {
5379: if (aa[k] == 0 && aj[k] == i - 1) PetscCall(PetscInfo(A, "Keep the diagonal zero at row %" PetscInt_FMT "\n", i - 1));
5380: aa[k - fshift] = aa[k];
5381: aj[k - fshift] = aj[k];
5382: }
5383: }
5384: ai[i - 1] -= fshift_prev; // safe to update ai[i-1] now since it will not be used in the next iteration
5385: fshift_prev = fshift;
5386: /* reset ilen and imax for each row */
5387: ailen[i - 1] = imax[i - 1] = ai[i] - fshift - ai[i - 1];
5388: a->nonzerorowcnt += ((ai[i] - fshift - ai[i - 1]) > 0);
5389: rmax = PetscMax(rmax, ailen[i - 1]);
5390: }
5391: if (fshift) {
5392: if (m) {
5393: ai[m] -= fshift;
5394: a->nz = ai[m];
5395: }
5396: PetscCall(PetscInfo(A, "Matrix size: %" PetscInt_FMT " X %" PetscInt_FMT "; zeros eliminated: %" PetscInt_FMT "; nonzeros left: %" PetscInt_FMT "\n", m, A->cmap->n, fshift, a->nz));
5397: A->nonzerostate++;
5398: A->info.nz_unneeded += (PetscReal)fshift;
5399: a->rmax = rmax;
5400: if (a->inode.use && a->inode.checked) PetscCall(MatSeqAIJCheckInode(A));
5401: PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
5402: PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
5403: }
5404: PetscFunctionReturn(PETSC_SUCCESS);
5405: }
5407: PetscFunctionList MatSeqAIJList = NULL;
5409: /*@
5410: MatSeqAIJSetType - Converts a `MATSEQAIJ` matrix to a subtype
5412: Collective
5414: Input Parameters:
5415: + mat - the matrix object
5416: - matype - matrix type
5418: Options Database Key:
5419: . -mat_seqaij_type method - for example seqaijcrl
5421: Level: intermediate
5423: .seealso: [](ch_matrices), `Mat`, `PCSetType()`, `VecSetType()`, `MatCreate()`, `MatType`
5424: @*/
5425: PetscErrorCode MatSeqAIJSetType(Mat mat, MatType matype)
5426: {
5427: PetscBool sametype;
5428: PetscErrorCode (*r)(Mat, MatType, MatReuse, Mat *);
5430: PetscFunctionBegin;
5432: PetscCall(PetscObjectTypeCompare((PetscObject)mat, matype, &sametype));
5433: if (sametype) PetscFunctionReturn(PETSC_SUCCESS);
5435: PetscCall(PetscFunctionListFind(MatSeqAIJList, matype, &r));
5436: PetscCheck(r, PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown Mat type given: %s", matype);
5437: PetscCall((*r)(mat, matype, MAT_INPLACE_MATRIX, &mat));
5438: PetscFunctionReturn(PETSC_SUCCESS);
5439: }
5441: /*@
5442: MatSeqAIJRegister - - Adds a new sub-matrix type for sequential `MATSEQAIJ` matrices
5444: Not Collective, No Fortran Support
5446: Input Parameters:
5447: + sname - name of a new user-defined matrix type, for example `MATSEQAIJCRL`
5448: - function - routine to convert to subtype
5450: Level: advanced
5452: Notes:
5453: `MatSeqAIJRegister()` may be called multiple times to add several user-defined solvers.
5455: Then, your matrix can be chosen with the procedural interface at runtime via the option
5456: .vb
5457: -mat_seqaij_type my_mat
5458: .ve
5460: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJRegisterAll()`
5461: @*/
5462: PetscErrorCode MatSeqAIJRegister(const char sname[], PetscErrorCode (*function)(Mat, MatType, MatReuse, Mat *))
5463: {
5464: PetscFunctionBegin;
5465: PetscCall(MatInitializePackage());
5466: PetscCall(PetscFunctionListAdd(&MatSeqAIJList, sname, function));
5467: PetscFunctionReturn(PETSC_SUCCESS);
5468: }
5470: PetscBool MatSeqAIJRegisterAllCalled = PETSC_FALSE;
5472: /*@
5473: MatSeqAIJRegisterAll - Registers all of the matrix subtypes of `MATSSEQAIJ`
5475: Not Collective
5477: Level: advanced
5479: Note:
5480: This registers the versions of `MATSEQAIJ` for GPUs
5482: .seealso: [](ch_matrices), `Mat`, `MatRegisterAll()`, `MatSeqAIJRegister()`
5483: @*/
5484: PetscErrorCode MatSeqAIJRegisterAll(void)
5485: {
5486: PetscFunctionBegin;
5487: if (MatSeqAIJRegisterAllCalled) PetscFunctionReturn(PETSC_SUCCESS);
5488: MatSeqAIJRegisterAllCalled = PETSC_TRUE;
5490: PetscCall(MatSeqAIJRegister(MATSEQAIJCRL, MatConvert_SeqAIJ_SeqAIJCRL));
5491: PetscCall(MatSeqAIJRegister(MATSEQAIJPERM, MatConvert_SeqAIJ_SeqAIJPERM));
5492: PetscCall(MatSeqAIJRegister(MATSEQAIJSELL, MatConvert_SeqAIJ_SeqAIJSELL));
5493: #if PetscDefined(HAVE_MKL_SPARSE)
5494: PetscCall(MatSeqAIJRegister(MATSEQAIJMKL, MatConvert_SeqAIJ_SeqAIJMKL));
5495: #endif
5496: #if PetscDefined(HAVE_CUDA)
5497: PetscCall(MatSeqAIJRegister(MATSEQAIJCUSPARSE, MatConvert_SeqAIJ_SeqAIJCUSPARSE));
5498: #endif
5499: #if PetscDefined(HAVE_HIP)
5500: PetscCall(MatSeqAIJRegister(MATSEQAIJHIPSPARSE, MatConvert_SeqAIJ_SeqAIJHIPSPARSE));
5501: #endif
5502: #if PetscDefined(HAVE_KOKKOS_KERNELS)
5503: PetscCall(MatSeqAIJRegister(MATSEQAIJKOKKOS, MatConvert_SeqAIJ_SeqAIJKokkos));
5504: #endif
5505: #if PetscDefined(HAVE_VIENNACL) && PetscDefined(HAVE_VIENNACL_NO_CUDA)
5506: PetscCall(MatSeqAIJRegister(MATMPIAIJVIENNACL, MatConvert_SeqAIJ_SeqAIJViennaCL));
5507: #endif
5508: PetscFunctionReturn(PETSC_SUCCESS);
5509: }