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