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