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: 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: MatTransposeMatMultNumeric_SeqAIJ_SeqAIJ,
3528: MatTransposeColoringCreate_SeqAIJ,
3529: MatTransColoringApplySpToDen_SeqAIJ,
3530: MatTransColoringApplyDenToSp_SeqAIJ,
3531: /*124*/ MatRARtNumeric_SeqAIJ_SeqAIJ,
3532: NULL,
3533: NULL,
3534: MatFDColoringSetUp_SeqXAIJ,
3535: MatFindOffBlockDiagonalEntries_SeqAIJ,
3536: /*129*/ MatCreateMPIMatConcatenateSeqMat_SeqAIJ,
3537: MatDestroySubMatrices_SeqAIJ,
3538: NULL,
3539: NULL,
3540: MatCreateGraph_Simple_AIJ,
3541: /*134*/ MatTransposeSymbolic_SeqAIJ,
3542: MatEliminateZeros_SeqAIJ,
3543: MatGetRowSumAbs_SeqAIJ,
3544: NULL,
3545: NULL,
3546: /*139*/ NULL,
3547: MatCopyHashToXAIJ_Seq_Hash,
3548: NULL,
3549: NULL,
3550: MatADot_Default,
3551: /*144*/ MatANorm_Default,
3552: NULL,
3553: NULL};
3555: static PetscErrorCode MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat, PetscInt *indices)
3556: {
3557: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
3558: PetscInt i, nz, n;
3560: PetscFunctionBegin;
3561: nz = aij->maxnz;
3562: n = mat->rmap->n;
3563: for (i = 0; i < nz; i++) aij->j[i] = indices[i];
3564: aij->nz = nz;
3565: for (i = 0; i < n; i++) aij->ilen[i] = aij->imax[i];
3566: PetscFunctionReturn(PETSC_SUCCESS);
3567: }
3569: /*
3570: * Given a sparse matrix with global column indices, compact it by using a local column space.
3571: * The result matrix helps saving memory in other algorithms, such as MatPtAPSymbolic_MPIAIJ_MPIAIJ_scalable()
3572: */
3573: PetscErrorCode MatSeqAIJCompactOutExtraColumns_SeqAIJ(Mat mat, ISLocalToGlobalMapping *mapping)
3574: {
3575: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
3576: PetscHMapI gid1_lid1;
3577: PetscHashIter tpos;
3578: PetscInt gid, lid, i, ec, nz = aij->nz;
3579: PetscInt *garray, *jj = aij->j;
3581: PetscFunctionBegin;
3583: PetscAssertPointer(mapping, 2);
3584: /* use a table */
3585: PetscCall(PetscHMapICreateWithSize(mat->rmap->n, &gid1_lid1));
3586: ec = 0;
3587: for (i = 0; i < nz; i++) {
3588: PetscInt data, gid1 = jj[i] + 1;
3589: PetscCall(PetscHMapIGetWithDefault(gid1_lid1, gid1, 0, &data));
3590: if (!data) {
3591: /* one based table */
3592: PetscCall(PetscHMapISet(gid1_lid1, gid1, ++ec));
3593: }
3594: }
3595: /* form array of columns we need */
3596: PetscCall(PetscMalloc1(ec, &garray));
3597: PetscHashIterBegin(gid1_lid1, tpos);
3598: while (!PetscHashIterAtEnd(gid1_lid1, tpos)) {
3599: PetscHashIterGetKey(gid1_lid1, tpos, gid);
3600: PetscHashIterGetVal(gid1_lid1, tpos, lid);
3601: PetscHashIterNext(gid1_lid1, tpos);
3602: gid--;
3603: lid--;
3604: garray[lid] = gid;
3605: }
3606: PetscCall(PetscSortInt(ec, garray)); /* sort, and rebuild */
3607: PetscCall(PetscHMapIClear(gid1_lid1));
3608: for (i = 0; i < ec; i++) PetscCall(PetscHMapISet(gid1_lid1, garray[i] + 1, i + 1));
3609: /* compact out the extra columns in B */
3610: for (i = 0; i < nz; i++) {
3611: PetscInt gid1 = jj[i] + 1;
3612: PetscCall(PetscHMapIGetWithDefault(gid1_lid1, gid1, 0, &lid));
3613: lid--;
3614: jj[i] = lid;
3615: }
3616: PetscCall(PetscLayoutDestroy(&mat->cmap));
3617: PetscCall(PetscHMapIDestroy(&gid1_lid1));
3618: PetscCall(PetscLayoutCreateFromSizes(PetscObjectComm((PetscObject)mat), ec, ec, 1, &mat->cmap));
3619: PetscCall(ISLocalToGlobalMappingCreate(PETSC_COMM_SELF, mat->cmap->bs, mat->cmap->n, garray, PETSC_OWN_POINTER, mapping));
3620: PetscCall(ISLocalToGlobalMappingSetType(*mapping, ISLOCALTOGLOBALMAPPINGHASH));
3621: PetscFunctionReturn(PETSC_SUCCESS);
3622: }
3624: /*@
3625: MatSeqAIJSetColumnIndices - Set the column indices for all the rows
3626: in the matrix.
3628: Input Parameters:
3629: + mat - the `MATSEQAIJ` matrix
3630: - indices - the column indices
3632: Level: advanced
3634: Notes:
3635: This can be called if you have precomputed the nonzero structure of the
3636: matrix and want to provide it to the matrix object to improve the performance
3637: of the `MatSetValues()` operation.
3639: You MUST have set the correct numbers of nonzeros per row in the call to
3640: `MatCreateSeqAIJ()`, and the columns indices MUST be sorted.
3642: MUST be called before any calls to `MatSetValues()`
3644: The indices should start with zero, not one.
3646: .seealso: [](ch_matrices), `Mat`, `MATSEQAIJ`
3647: @*/
3648: PetscErrorCode MatSeqAIJSetColumnIndices(Mat mat, PetscInt *indices)
3649: {
3650: PetscFunctionBegin;
3652: PetscAssertPointer(indices, 2);
3653: PetscUseMethod(mat, "MatSeqAIJSetColumnIndices_C", (Mat, PetscInt *), (mat, indices));
3654: PetscFunctionReturn(PETSC_SUCCESS);
3655: }
3657: static PetscErrorCode MatStoreValues_SeqAIJ(Mat mat)
3658: {
3659: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
3660: size_t nz = aij->i[mat->rmap->n];
3662: PetscFunctionBegin;
3663: PetscCheck(aij->nonew, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3665: /* allocate space for values if not already there */
3666: if (!aij->saved_values) PetscCall(PetscMalloc1(nz + 1, &aij->saved_values));
3668: /* copy values over */
3669: PetscCall(PetscArraycpy(aij->saved_values, aij->a, nz));
3670: PetscFunctionReturn(PETSC_SUCCESS);
3671: }
3673: /*@
3674: MatStoreValues - Stashes a copy of the matrix values; this allows reusing of the linear part of a Jacobian, while recomputing only the
3675: nonlinear portion.
3677: Logically Collect
3679: Input Parameter:
3680: . mat - the matrix (currently only `MATAIJ` matrices support this option)
3682: Level: advanced
3684: Example Usage:
3685: .vb
3686: Using SNES
3687: Create Jacobian matrix
3688: Set linear terms into matrix
3689: Apply boundary conditions to matrix, at this time matrix must have
3690: final nonzero structure (i.e. setting the nonlinear terms and applying
3691: boundary conditions again will not change the nonzero structure
3692: MatSetOption(mat, MAT_NEW_NONZERO_LOCATIONS, PETSC_FALSE);
3693: MatStoreValues(mat);
3694: Call SNESSetJacobian() with matrix
3695: In your Jacobian routine
3696: MatRetrieveValues(mat);
3697: Set nonlinear terms in matrix
3699: Without `SNESSolve()`, i.e. when you handle nonlinear solve yourself:
3700: // build linear portion of Jacobian
3701: MatSetOption(mat, MAT_NEW_NONZERO_LOCATIONS, PETSC_FALSE);
3702: MatStoreValues(mat);
3703: loop over nonlinear iterations
3704: MatRetrieveValues(mat);
3705: // call MatSetValues(mat,...) to set nonliner portion of Jacobian
3706: // call MatAssemblyBegin/End() on matrix
3707: Solve linear system with Jacobian
3708: endloop
3709: .ve
3711: Notes:
3712: Matrix must already be assembled before calling this routine
3713: Must set the matrix option `MatSetOption`(mat,`MAT_NEW_NONZERO_LOCATIONS`,`PETSC_FALSE`); before
3714: calling this routine.
3716: When this is called multiple times it overwrites the previous set of stored values
3717: and does not allocated additional space.
3719: .seealso: [](ch_matrices), `Mat`, `MatRetrieveValues()`
3720: @*/
3721: PetscErrorCode MatStoreValues(Mat mat)
3722: {
3723: PetscFunctionBegin;
3725: PetscCheck(mat->assembled, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for unassembled matrix");
3726: PetscCheck(!mat->factortype, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
3727: PetscUseMethod(mat, "MatStoreValues_C", (Mat), (mat));
3728: PetscFunctionReturn(PETSC_SUCCESS);
3729: }
3731: static PetscErrorCode MatRetrieveValues_SeqAIJ(Mat mat)
3732: {
3733: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
3734: PetscInt nz = aij->i[mat->rmap->n];
3736: PetscFunctionBegin;
3737: PetscCheck(aij->nonew, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3738: PetscCheck(aij->saved_values, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Must call MatStoreValues(A);first");
3739: /* copy values over */
3740: PetscCall(PetscArraycpy(aij->a, aij->saved_values, nz));
3741: PetscFunctionReturn(PETSC_SUCCESS);
3742: }
3744: /*@
3745: MatRetrieveValues - Retrieves the copy of the matrix values that was stored with `MatStoreValues()`
3747: Logically Collect
3749: Input Parameter:
3750: . mat - the matrix (currently only `MATAIJ` matrices support this option)
3752: Level: advanced
3754: .seealso: [](ch_matrices), `Mat`, `MatStoreValues()`
3755: @*/
3756: PetscErrorCode MatRetrieveValues(Mat mat)
3757: {
3758: PetscFunctionBegin;
3760: PetscCheck(mat->assembled, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for unassembled matrix");
3761: PetscCheck(!mat->factortype, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
3762: PetscUseMethod(mat, "MatRetrieveValues_C", (Mat), (mat));
3763: PetscFunctionReturn(PETSC_SUCCESS);
3764: }
3766: /*@
3767: MatCreateSeqAIJ - Creates a sparse matrix in `MATSEQAIJ` (compressed row) format
3768: (the default parallel PETSc format). For good matrix assembly performance
3769: the user should preallocate the matrix storage by setting the parameter `nz`
3770: (or the array `nnz`).
3772: Collective
3774: Input Parameters:
3775: + comm - MPI communicator, set to `PETSC_COMM_SELF`
3776: . m - number of rows
3777: . n - number of columns
3778: . nz - number of nonzeros per row (same for all rows)
3779: - nnz - array containing the number of nonzeros in the various rows
3780: (possibly different for each row) or NULL
3782: Output Parameter:
3783: . A - the matrix
3785: Options Database Keys:
3786: + -mat_no_inode - Do not use inodes
3787: - -mat_inode_limit limit - Sets inode limit (max limit=5)
3789: Level: intermediate
3791: Notes:
3792: It is recommend to use `MatCreateFromOptions()` instead of this routine
3794: If `nnz` is given then `nz` is ignored
3796: The `MATSEQAIJ` format, also called
3797: compressed row storage, is fully compatible with standard Fortran
3798: storage. That is, the stored row and column indices can begin at
3799: either one (as in Fortran) or zero.
3801: Specify the preallocated storage with either `nz` or `nnz` (not both).
3802: Set `nz` = `PETSC_DEFAULT` and `nnz` = `NULL` for PETSc to control dynamic memory
3803: allocation.
3805: By default, this format uses inodes (identical nodes) when possible, to
3806: improve numerical efficiency of matrix-vector products and solves. We
3807: search for consecutive rows with the same nonzero structure, thereby
3808: reusing matrix information to achieve increased efficiency.
3810: .seealso: [](ch_matrices), `Mat`, [Sparse Matrix Creation](sec_matsparse), `MatCreate()`, `MatCreateAIJ()`, `MatSetValues()`, `MatSeqAIJSetColumnIndices()`, `MatCreateSeqAIJWithArrays()`
3811: @*/
3812: PetscErrorCode MatCreateSeqAIJ(MPI_Comm comm, PetscInt m, PetscInt n, PetscInt nz, const PetscInt nnz[], Mat *A)
3813: {
3814: PetscFunctionBegin;
3815: PetscCall(MatCreate(comm, A));
3816: PetscCall(MatSetSizes(*A, m, n, m, n));
3817: PetscCall(MatSetType(*A, MATSEQAIJ));
3818: PetscCall(MatSeqAIJSetPreallocation_SeqAIJ(*A, nz, nnz));
3819: PetscFunctionReturn(PETSC_SUCCESS);
3820: }
3822: /*@
3823: MatSeqAIJSetPreallocation - For good matrix assembly performance
3824: the user should preallocate the matrix storage by setting the parameter nz
3825: (or the array nnz). By setting these parameters accurately, performance
3826: during matrix assembly can be increased by more than a factor of 50.
3828: Collective
3830: Input Parameters:
3831: + B - The matrix
3832: . nz - number of nonzeros per row (same for all rows)
3833: - nnz - array containing the number of nonzeros in the various rows
3834: (possibly different for each row) or NULL
3836: Options Database Keys:
3837: + -mat_no_inode - Do not use inodes
3838: - -mat_inode_limit limit - Sets inode limit (max limit=5)
3840: Level: intermediate
3842: Notes:
3843: If `nnz` is given then `nz` is ignored
3845: The `MATSEQAIJ` format also called
3846: compressed row storage, is fully compatible with standard Fortran
3847: storage. That is, the stored row and column indices can begin at
3848: either one (as in Fortran) or zero. See the users' manual for details.
3850: Specify the preallocated storage with either `nz` or `nnz` (not both).
3851: Set nz = `PETSC_DEFAULT` and `nnz` = `NULL` for PETSc to control dynamic memory
3852: allocation.
3854: You can call `MatGetInfo()` to get information on how effective the preallocation was;
3855: for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3856: You can also run with the option -info and look for messages with the string
3857: malloc in them to see if additional memory allocation was needed.
3859: Developer Notes:
3860: Use nz of `MAT_SKIP_ALLOCATION` to not allocate any space for the matrix
3861: entries or columns indices
3863: By default, this format uses inodes (identical nodes) when possible, to
3864: improve numerical efficiency of matrix-vector products and solves. We
3865: search for consecutive rows with the same nonzero structure, thereby
3866: reusing matrix information to achieve increased efficiency.
3868: .seealso: [](ch_matrices), `Mat`, `MatCreate()`, `MatCreateAIJ()`, `MatSetValues()`, `MatSeqAIJSetColumnIndices()`, `MatCreateSeqAIJWithArrays()`, `MatGetInfo()`,
3869: `MatSeqAIJSetTotalPreallocation()`
3870: @*/
3871: PetscErrorCode MatSeqAIJSetPreallocation(Mat B, PetscInt nz, const PetscInt nnz[])
3872: {
3873: PetscFunctionBegin;
3876: PetscTryMethod(B, "MatSeqAIJSetPreallocation_C", (Mat, PetscInt, const PetscInt[]), (B, nz, nnz));
3877: PetscFunctionReturn(PETSC_SUCCESS);
3878: }
3880: PetscErrorCode MatSeqAIJSetPreallocation_SeqAIJ(Mat B, PetscInt nz, const PetscInt *nnz)
3881: {
3882: Mat_SeqAIJ *b = (Mat_SeqAIJ *)B->data;
3883: PetscBool skipallocation = PETSC_FALSE, realalloc = PETSC_FALSE;
3884: PetscInt i;
3886: PetscFunctionBegin;
3887: if (B->hash_active) {
3888: B->ops[0] = b->cops;
3889: PetscCall(PetscHMapIJVDestroy(&b->ht));
3890: PetscCall(PetscFree(b->dnz));
3891: B->hash_active = PETSC_FALSE;
3892: }
3893: if (nz >= 0 || nnz) realalloc = PETSC_TRUE;
3894: if (nz == MAT_SKIP_ALLOCATION) {
3895: skipallocation = PETSC_TRUE;
3896: nz = 0;
3897: }
3898: PetscCall(PetscLayoutSetUp(B->rmap));
3899: PetscCall(PetscLayoutSetUp(B->cmap));
3901: if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
3902: PetscCheck(nz >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "nz cannot be less than 0: value %" PetscInt_FMT, nz);
3903: if (nnz) {
3904: for (i = 0; i < B->rmap->n; i++) {
3905: 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]);
3906: 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);
3907: }
3908: }
3910: B->preallocated = PETSC_TRUE;
3911: if (!skipallocation) {
3912: if (!b->imax) PetscCall(PetscMalloc1(B->rmap->n, &b->imax));
3913: if (!b->ilen) {
3914: /* b->ilen will count nonzeros in each row so far. */
3915: PetscCall(PetscCalloc1(B->rmap->n, &b->ilen));
3916: } else {
3917: PetscCall(PetscMemzero(b->ilen, B->rmap->n * sizeof(PetscInt)));
3918: }
3919: if (!b->ipre) PetscCall(PetscMalloc1(B->rmap->n, &b->ipre));
3920: if (!nnz) {
3921: if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
3922: else if (nz < 0) nz = 1;
3923: nz = PetscMin(nz, B->cmap->n);
3924: for (i = 0; i < B->rmap->n; i++) b->imax[i] = nz;
3925: PetscCall(PetscIntMultError(nz, B->rmap->n, &nz));
3926: } else {
3927: PetscInt64 nz64 = 0;
3928: for (i = 0; i < B->rmap->n; i++) {
3929: b->imax[i] = nnz[i];
3930: nz64 += nnz[i];
3931: }
3932: PetscCall(PetscIntCast(nz64, &nz));
3933: }
3935: /* allocate the matrix space */
3936: PetscCall(MatSeqXAIJFreeAIJ(B, &b->a, &b->j, &b->i));
3937: PetscCall(PetscShmgetAllocateArray(nz, sizeof(PetscInt), (void **)&b->j));
3938: PetscCall(PetscShmgetAllocateArray(B->rmap->n + 1, sizeof(PetscInt), (void **)&b->i));
3939: b->free_ij = PETSC_TRUE;
3940: if (B->structure_only) {
3941: b->free_a = PETSC_FALSE;
3942: } else {
3943: PetscCall(PetscShmgetAllocateArray(nz, sizeof(PetscScalar), (void **)&b->a));
3944: b->free_a = PETSC_TRUE;
3945: }
3946: b->i[0] = 0;
3947: for (i = 1; i < B->rmap->n + 1; i++) b->i[i] = b->i[i - 1] + b->imax[i - 1];
3948: } else {
3949: b->free_a = PETSC_FALSE;
3950: b->free_ij = PETSC_FALSE;
3951: }
3953: if (b->ipre && nnz != b->ipre && b->imax) {
3954: /* reserve user-requested sparsity */
3955: PetscCall(PetscArraycpy(b->ipre, b->imax, B->rmap->n));
3956: }
3958: b->nz = 0;
3959: b->maxnz = nz;
3960: B->info.nz_unneeded = (double)b->maxnz;
3961: if (realalloc) PetscCall(MatSetOption(B, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_TRUE));
3962: B->was_assembled = PETSC_FALSE;
3963: B->assembled = PETSC_FALSE;
3964: /* We simply deem preallocation has changed nonzero state. Updating the state
3965: will give clients (like AIJKokkos) a chance to know something has happened.
3966: */
3967: B->nonzerostate++;
3968: PetscFunctionReturn(PETSC_SUCCESS);
3969: }
3971: PetscErrorCode MatResetPreallocation_SeqAIJ_Private(Mat A, PetscBool *memoryreset)
3972: {
3973: Mat_SeqAIJ *a;
3974: PetscInt i;
3975: PetscBool skipreset;
3977: PetscFunctionBegin;
3980: 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()");
3981: if (A->num_ass == 0) PetscFunctionReturn(PETSC_SUCCESS);
3983: /* Check local size. If zero, then return */
3984: if (!A->rmap->n) PetscFunctionReturn(PETSC_SUCCESS);
3986: a = (Mat_SeqAIJ *)A->data;
3987: /* if no saved info, we error out */
3988: PetscCheck(a->ipre, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "No saved preallocation info ");
3990: PetscCheck(a->i && a->imax && a->ilen, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "Memory info is incomplete, and cannot reset preallocation ");
3992: PetscCall(PetscArraycmp(a->ipre, a->ilen, A->rmap->n, &skipreset));
3993: if (skipreset) PetscCall(MatZeroEntries(A));
3994: else {
3995: PetscCall(PetscArraycpy(a->imax, a->ipre, A->rmap->n));
3996: PetscCall(PetscArrayzero(a->ilen, A->rmap->n));
3997: a->i[0] = 0;
3998: for (i = 1; i < A->rmap->n + 1; i++) a->i[i] = a->i[i - 1] + a->imax[i - 1];
3999: A->preallocated = PETSC_TRUE;
4000: a->nz = 0;
4001: a->maxnz = a->i[A->rmap->n];
4002: A->info.nz_unneeded = (double)a->maxnz;
4003: A->was_assembled = PETSC_FALSE;
4004: A->assembled = PETSC_FALSE;
4005: A->nonzerostate++;
4006: /* Log that the state of this object has changed; this will help guarantee that preconditioners get re-setup */
4007: PetscCall(PetscObjectStateIncrease((PetscObject)A));
4008: }
4009: if (memoryreset) *memoryreset = (PetscBool)!skipreset;
4010: PetscFunctionReturn(PETSC_SUCCESS);
4011: }
4013: static PetscErrorCode MatResetPreallocation_SeqAIJ(Mat A)
4014: {
4015: PetscFunctionBegin;
4016: PetscCall(MatResetPreallocation_SeqAIJ_Private(A, NULL));
4017: PetscFunctionReturn(PETSC_SUCCESS);
4018: }
4020: /*@
4021: MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in `MATSEQAIJ` format.
4023: Input Parameters:
4024: + B - the matrix
4025: . i - the indices into `j` for the start of each row (indices start with zero)
4026: . j - the column indices for each row (indices start with zero) these must be sorted for each row
4027: - v - optional values in the matrix, use `NULL` if not provided
4029: Level: developer
4031: Notes:
4032: The `i`,`j`,`v` values are COPIED with this routine; to avoid the copy use `MatCreateSeqAIJWithArrays()`
4034: This routine may be called multiple times with different nonzero patterns (or the same nonzero pattern). The nonzero
4035: structure will be the union of all the previous nonzero structures.
4037: Developer Notes:
4038: 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
4039: then just copies the `v` values directly with `PetscMemcpy()`.
4041: This routine could also take a `PetscCopyMode` argument to allow sharing the values instead of always copying them.
4043: .seealso: [](ch_matrices), `Mat`, `MatCreate()`, `MatCreateSeqAIJ()`, `MatSetValues()`, `MatSeqAIJSetPreallocation()`, `MATSEQAIJ`, `MatResetPreallocation()`
4044: @*/
4045: PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B, const PetscInt i[], const PetscInt j[], const PetscScalar v[])
4046: {
4047: PetscFunctionBegin;
4050: PetscTryMethod(B, "MatSeqAIJSetPreallocationCSR_C", (Mat, const PetscInt[], const PetscInt[], const PetscScalar[]), (B, i, j, v));
4051: PetscFunctionReturn(PETSC_SUCCESS);
4052: }
4054: static PetscErrorCode MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B, const PetscInt Ii[], const PetscInt J[], const PetscScalar v[])
4055: {
4056: PetscInt i;
4057: PetscInt m, n;
4058: PetscInt nz;
4059: PetscInt *nnz;
4061: PetscFunctionBegin;
4062: PetscCheck(Ii[0] == 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %" PetscInt_FMT, Ii[0]);
4064: PetscCall(PetscLayoutSetUp(B->rmap));
4065: PetscCall(PetscLayoutSetUp(B->cmap));
4067: PetscCall(MatGetSize(B, &m, &n));
4068: PetscCall(PetscMalloc1(m + 1, &nnz));
4069: for (i = 0; i < m; i++) {
4070: nz = Ii[i + 1] - Ii[i];
4071: PetscCheck(nz >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Local row %" PetscInt_FMT " has a negative number of columns %" PetscInt_FMT, i, nz);
4072: nnz[i] = nz;
4073: }
4074: PetscCall(MatSeqAIJSetPreallocation(B, 0, nnz));
4075: PetscCall(PetscFree(nnz));
4077: 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));
4079: PetscCall(MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY));
4080: PetscCall(MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY));
4082: PetscCall(MatSetOption(B, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
4083: PetscFunctionReturn(PETSC_SUCCESS);
4084: }
4086: /*@
4087: MatSeqAIJKron - Computes `C`, the Kronecker product of `A` and `B`.
4089: Input Parameters:
4090: + A - left-hand side matrix
4091: . B - right-hand side matrix
4092: - reuse - either `MAT_INITIAL_MATRIX` or `MAT_REUSE_MATRIX`
4094: Output Parameter:
4095: . C - Kronecker product of `A` and `B`
4097: Level: intermediate
4099: Note:
4100: `MAT_REUSE_MATRIX` can only be used when the nonzero structure of the product matrix has not changed from that last call to `MatSeqAIJKron()`.
4102: .seealso: [](ch_matrices), `Mat`, `MatCreateSeqAIJ()`, `MATSEQAIJ`, `MATKAIJ`, `MatReuse`
4103: @*/
4104: PetscErrorCode MatSeqAIJKron(Mat A, Mat B, MatReuse reuse, Mat *C)
4105: {
4106: PetscFunctionBegin;
4111: PetscAssertPointer(C, 4);
4112: if (reuse == MAT_REUSE_MATRIX) {
4115: }
4116: PetscTryMethod(A, "MatSeqAIJKron_C", (Mat, Mat, MatReuse, Mat *), (A, B, reuse, C));
4117: PetscFunctionReturn(PETSC_SUCCESS);
4118: }
4120: static PetscErrorCode MatSeqAIJKron_SeqAIJ(Mat A, Mat B, MatReuse reuse, Mat *C)
4121: {
4122: Mat newmat;
4123: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
4124: Mat_SeqAIJ *b = (Mat_SeqAIJ *)B->data;
4125: PetscScalar *v;
4126: const PetscScalar *aa, *ba;
4127: 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;
4128: PetscBool flg;
4130: PetscFunctionBegin;
4131: PetscCheck(!A->factortype, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
4132: PetscCheck(A->assembled, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for unassembled matrix");
4133: PetscCheck(!B->factortype, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
4134: PetscCheck(B->assembled, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for unassembled matrix");
4135: PetscCall(PetscObjectTypeCompare((PetscObject)B, MATSEQAIJ, &flg));
4136: PetscCheck(flg, PETSC_COMM_SELF, PETSC_ERR_SUP, "MatType %s", ((PetscObject)B)->type_name);
4137: PetscCheck(reuse == MAT_INITIAL_MATRIX || reuse == MAT_REUSE_MATRIX, PETSC_COMM_SELF, PETSC_ERR_SUP, "MatReuse %d", (int)reuse);
4138: if (reuse == MAT_INITIAL_MATRIX) {
4139: PetscCall(PetscMalloc2(am * bm + 1, &i, a->i[am] * b->i[bm], &j));
4140: PetscCall(MatCreate(PETSC_COMM_SELF, &newmat));
4141: PetscCall(MatSetSizes(newmat, am * bm, an * bn, am * bm, an * bn));
4142: PetscCall(MatSetType(newmat, MATAIJ));
4143: i[0] = 0;
4144: for (m = 0; m < am; ++m) {
4145: for (p = 0; p < bm; ++p) {
4146: i[m * bm + p + 1] = i[m * bm + p] + (a->i[m + 1] - a->i[m]) * (b->i[p + 1] - b->i[p]);
4147: for (n = a->i[m]; n < a->i[m + 1]; ++n) {
4148: for (q = b->i[p]; q < b->i[p + 1]; ++q) j[nnz++] = a->j[n] * bn + b->j[q];
4149: }
4150: }
4151: }
4152: PetscCall(MatSeqAIJSetPreallocationCSR(newmat, i, j, NULL));
4153: *C = newmat;
4154: PetscCall(PetscFree2(i, j));
4155: nnz = 0;
4156: }
4157: PetscCall(MatSeqAIJGetArray(*C, &v));
4158: PetscCall(MatSeqAIJGetArrayRead(A, &aa));
4159: PetscCall(MatSeqAIJGetArrayRead(B, &ba));
4160: for (m = 0; m < am; ++m) {
4161: for (p = 0; p < bm; ++p) {
4162: for (n = a->i[m]; n < a->i[m + 1]; ++n) {
4163: for (q = b->i[p]; q < b->i[p + 1]; ++q) v[nnz++] = aa[n] * ba[q];
4164: }
4165: }
4166: }
4167: PetscCall(MatSeqAIJRestoreArray(*C, &v));
4168: PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
4169: PetscCall(MatSeqAIJRestoreArrayRead(B, &ba));
4170: PetscFunctionReturn(PETSC_SUCCESS);
4171: }
4173: #include <../src/mat/impls/dense/seq/dense.h>
4174: #include <petsc/private/kernels/petscaxpy.h>
4176: /*
4177: Computes (B'*A')' since computing B*A directly is untenable
4179: n p p
4180: [ ] [ ] [ ]
4181: m [ A ] * n [ B ] = m [ C ]
4182: [ ] [ ] [ ]
4184: */
4185: PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A, Mat B, Mat C)
4186: {
4187: Mat_SeqDense *sub_a = (Mat_SeqDense *)A->data;
4188: Mat_SeqAIJ *sub_b = (Mat_SeqAIJ *)B->data;
4189: Mat_SeqDense *sub_c = (Mat_SeqDense *)C->data;
4190: PetscInt i, j, n, m, q, p;
4191: const PetscInt *ii, *idx;
4192: const PetscScalar *b, *a, *a_q;
4193: PetscScalar *c, *c_q;
4194: PetscInt clda = sub_c->lda;
4195: PetscInt alda = sub_a->lda;
4197: PetscFunctionBegin;
4198: m = A->rmap->n;
4199: n = A->cmap->n;
4200: p = B->cmap->n;
4201: a = sub_a->v;
4202: b = sub_b->a;
4203: c = sub_c->v;
4204: if (clda == m) {
4205: PetscCall(PetscArrayzero(c, m * p));
4206: } else {
4207: for (j = 0; j < p; j++)
4208: for (i = 0; i < m; i++) c[j * clda + i] = 0.0;
4209: }
4210: ii = sub_b->i;
4211: idx = sub_b->j;
4212: for (i = 0; i < n; i++) {
4213: q = ii[i + 1] - ii[i];
4214: while (q-- > 0) {
4215: c_q = c + clda * (*idx);
4216: a_q = a + alda * i;
4217: PetscKernelAXPY(c_q, *b, a_q, m);
4218: idx++;
4219: b++;
4220: }
4221: }
4222: PetscFunctionReturn(PETSC_SUCCESS);
4223: }
4225: PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A, Mat B, PetscReal fill, Mat C)
4226: {
4227: PetscInt m = A->rmap->n, n = B->cmap->n;
4228: PetscBool cisdense;
4230: PetscFunctionBegin;
4231: 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);
4232: PetscCall(MatSetSizes(C, m, n, m, n));
4233: PetscCall(MatSetBlockSizesFromMats(C, A, B));
4234: PetscCall(PetscObjectTypeCompareAny((PetscObject)C, &cisdense, MATSEQDENSE, MATSEQDENSECUDA, MATSEQDENSEHIP, ""));
4235: if (!cisdense) PetscCall(MatSetType(C, MATDENSE));
4236: PetscCall(MatSetUp(C));
4238: C->ops->matmultnumeric = MatMatMultNumeric_SeqDense_SeqAIJ;
4239: PetscFunctionReturn(PETSC_SUCCESS);
4240: }
4242: /*MC
4243: MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
4244: based on compressed sparse row format.
4246: Options Database Key:
4247: . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()
4249: Level: beginner
4251: Notes:
4252: `MatSetValues()` may be called for this matrix type with a `NULL` argument for the numerical values,
4253: in this case the values associated with the rows and columns one passes in are set to zero
4254: in the matrix
4256: `MatSetOptions`(,`MAT_STRUCTURE_ONLY`,`PETSC_TRUE`) may be called for this matrix type. In this no
4257: space is allocated for the nonzero entries and any entries passed with `MatSetValues()` are ignored
4259: Developer Note:
4260: It would be nice if all matrix formats supported passing `NULL` in for the numerical values
4262: .seealso: [](ch_matrices), `Mat`, `MatCreateSeqAIJ()`, `MatSetFromOptions()`, `MatSetType()`, `MatCreate()`, `MatType`, `MATSELL`, `MATSEQSELL`, `MATMPISELL`
4263: M*/
4265: /*MC
4266: MATAIJ - MATAIJ = "aij" - A matrix type to be used for sparse matrices.
4268: This matrix type is identical to `MATSEQAIJ` when constructed with a single process communicator,
4269: and `MATMPIAIJ` otherwise. As a result, for single process communicators,
4270: `MatSeqAIJSetPreallocation()` is supported, and similarly `MatMPIAIJSetPreallocation()` is supported
4271: for communicators controlling multiple processes. It is recommended that you call both of
4272: the above preallocation routines for simplicity.
4274: Options Database Key:
4275: . -mat_type aij - sets the matrix type to "aij" during a call to `MatSetFromOptions()`
4277: Level: beginner
4279: Note:
4280: Subclasses include `MATAIJCUSPARSE`, `MATAIJPERM`, `MATAIJSELL`, `MATAIJMKL`, `MATAIJCRL`, and also automatically switches over to use inodes when
4281: enough exist.
4283: .seealso: [](ch_matrices), `Mat`, `MatCreateAIJ()`, `MatCreateSeqAIJ()`, `MATSEQAIJ`, `MATMPIAIJ`, `MATSELL`, `MATSEQSELL`, `MATMPISELL`
4284: M*/
4286: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCRL(Mat, MatType, MatReuse, Mat *);
4287: #if defined(PETSC_HAVE_ELEMENTAL)
4288: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_Elemental(Mat, MatType, MatReuse, Mat *);
4289: #endif
4290: #if defined(PETSC_HAVE_SCALAPACK) && (defined(PETSC_USE_REAL_SINGLE) || defined(PETSC_USE_REAL_DOUBLE))
4291: PETSC_INTERN PetscErrorCode MatConvert_AIJ_ScaLAPACK(Mat, MatType, MatReuse, Mat *);
4292: #endif
4293: #if defined(PETSC_HAVE_HYPRE)
4294: PETSC_INTERN PetscErrorCode MatConvert_AIJ_HYPRE(Mat A, MatType, MatReuse, Mat *);
4295: #endif
4297: PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_SeqSELL(Mat, MatType, MatReuse, Mat *);
4298: PETSC_INTERN PetscErrorCode MatConvert_XAIJ_IS(Mat, MatType, MatReuse, Mat *);
4299: PETSC_INTERN PetscErrorCode MatProductSetFromOptions_IS_XAIJ(Mat);
4301: /*@C
4302: MatSeqAIJGetArray - gives read/write access to the array where the data for a `MATSEQAIJ` matrix is stored
4304: Not Collective
4306: Input Parameter:
4307: . A - a `MATSEQAIJ` matrix
4309: Output Parameter:
4310: . array - pointer to the data
4312: Level: intermediate
4314: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJRestoreArray()`
4315: @*/
4316: PetscErrorCode MatSeqAIJGetArray(Mat A, PetscScalar *array[])
4317: {
4318: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;
4320: PetscFunctionBegin;
4321: if (aij->ops->getarray) {
4322: PetscCall((*aij->ops->getarray)(A, array));
4323: } else {
4324: *array = aij->a;
4325: }
4326: PetscFunctionReturn(PETSC_SUCCESS);
4327: }
4329: /*@C
4330: MatSeqAIJRestoreArray - returns access to the array where the data for a `MATSEQAIJ` matrix is stored obtained by `MatSeqAIJGetArray()`
4332: Not Collective
4334: Input Parameters:
4335: + A - a `MATSEQAIJ` matrix
4336: - array - pointer to the data
4338: Level: intermediate
4340: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJGetArray()`
4341: @*/
4342: PetscErrorCode MatSeqAIJRestoreArray(Mat A, PetscScalar *array[])
4343: {
4344: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;
4346: PetscFunctionBegin;
4347: if (aij->ops->restorearray) {
4348: PetscCall((*aij->ops->restorearray)(A, array));
4349: } else {
4350: *array = NULL;
4351: }
4352: PetscCall(PetscObjectStateIncrease((PetscObject)A));
4353: PetscFunctionReturn(PETSC_SUCCESS);
4354: }
4356: /*@C
4357: MatSeqAIJGetArrayRead - gives read-only access to the array where the data for a `MATSEQAIJ` matrix is stored
4359: Not Collective; No Fortran Support
4361: Input Parameter:
4362: . A - a `MATSEQAIJ` matrix
4364: Output Parameter:
4365: . array - pointer to the data
4367: Level: intermediate
4369: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJGetArray()`, `MatSeqAIJRestoreArrayRead()`
4370: @*/
4371: PetscErrorCode MatSeqAIJGetArrayRead(Mat A, const PetscScalar *array[])
4372: {
4373: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;
4375: PetscFunctionBegin;
4376: if (aij->ops->getarrayread) {
4377: PetscCall((*aij->ops->getarrayread)(A, array));
4378: } else {
4379: *array = aij->a;
4380: }
4381: PetscFunctionReturn(PETSC_SUCCESS);
4382: }
4384: /*@C
4385: MatSeqAIJRestoreArrayRead - restore the read-only access array obtained from `MatSeqAIJGetArrayRead()`
4387: Not Collective; No Fortran Support
4389: Input Parameter:
4390: . A - a `MATSEQAIJ` matrix
4392: Output Parameter:
4393: . array - pointer to the data
4395: Level: intermediate
4397: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJGetArray()`, `MatSeqAIJGetArrayRead()`
4398: @*/
4399: PetscErrorCode MatSeqAIJRestoreArrayRead(Mat A, const PetscScalar *array[])
4400: {
4401: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;
4403: PetscFunctionBegin;
4404: if (aij->ops->restorearrayread) {
4405: PetscCall((*aij->ops->restorearrayread)(A, array));
4406: } else {
4407: *array = NULL;
4408: }
4409: PetscFunctionReturn(PETSC_SUCCESS);
4410: }
4412: /*@C
4413: MatSeqAIJGetArrayWrite - gives write-only access to the array where the data for a `MATSEQAIJ` matrix is stored
4415: Not Collective; No Fortran Support
4417: Input Parameter:
4418: . A - a `MATSEQAIJ` matrix
4420: Output Parameter:
4421: . array - pointer to the data
4423: Level: intermediate
4425: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJGetArray()`, `MatSeqAIJRestoreArrayRead()`
4426: @*/
4427: PetscErrorCode MatSeqAIJGetArrayWrite(Mat A, PetscScalar *array[])
4428: {
4429: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;
4431: PetscFunctionBegin;
4432: if (aij->ops->getarraywrite) {
4433: PetscCall((*aij->ops->getarraywrite)(A, array));
4434: } else {
4435: *array = aij->a;
4436: }
4437: PetscCall(PetscObjectStateIncrease((PetscObject)A));
4438: PetscFunctionReturn(PETSC_SUCCESS);
4439: }
4441: /*@C
4442: MatSeqAIJRestoreArrayWrite - restore the read-only access array obtained from MatSeqAIJGetArrayRead
4444: Not Collective; No Fortran Support
4446: Input Parameter:
4447: . A - a MATSEQAIJ matrix
4449: Output Parameter:
4450: . array - pointer to the data
4452: Level: intermediate
4454: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJGetArray()`, `MatSeqAIJGetArrayRead()`
4455: @*/
4456: PetscErrorCode MatSeqAIJRestoreArrayWrite(Mat A, PetscScalar *array[])
4457: {
4458: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;
4460: PetscFunctionBegin;
4461: if (aij->ops->restorearraywrite) {
4462: PetscCall((*aij->ops->restorearraywrite)(A, array));
4463: } else {
4464: *array = NULL;
4465: }
4466: PetscFunctionReturn(PETSC_SUCCESS);
4467: }
4469: /*@C
4470: MatSeqAIJGetCSRAndMemType - Get the CSR arrays and the memory type of the `MATSEQAIJ` matrix
4472: Not Collective; No Fortran Support
4474: Input Parameter:
4475: . mat - a matrix of type `MATSEQAIJ` or its subclasses
4477: Output Parameters:
4478: + i - row map array of the matrix
4479: . j - column index array of the matrix
4480: . a - data array of the matrix
4481: - mtype - memory type of the arrays
4483: Level: developer
4485: Notes:
4486: Any of the output parameters can be `NULL`, in which case the corresponding value is not returned.
4487: If mat is a device matrix, the arrays are on the device. Otherwise, they are on the host.
4489: One can call this routine on a preallocated but not assembled matrix to just get the memory of the CSR underneath the matrix.
4490: If the matrix is assembled, the data array `a` is guaranteed to have the latest values of the matrix.
4492: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJGetArray()`, `MatSeqAIJGetArrayRead()`
4493: @*/
4494: PetscErrorCode MatSeqAIJGetCSRAndMemType(Mat mat, const PetscInt *i[], const PetscInt *j[], PetscScalar *a[], PetscMemType *mtype)
4495: {
4496: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
4498: PetscFunctionBegin;
4499: PetscCheck(mat->preallocated, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "matrix is not preallocated");
4500: if (aij->ops->getcsrandmemtype) {
4501: PetscCall((*aij->ops->getcsrandmemtype)(mat, i, j, a, mtype));
4502: } else {
4503: if (i) *i = aij->i;
4504: if (j) *j = aij->j;
4505: if (a) *a = aij->a;
4506: if (mtype) *mtype = PETSC_MEMTYPE_HOST;
4507: }
4508: PetscFunctionReturn(PETSC_SUCCESS);
4509: }
4511: /*@
4512: MatSeqAIJGetMaxRowNonzeros - returns the maximum number of nonzeros in any row
4514: Not Collective
4516: Input Parameter:
4517: . A - a `MATSEQAIJ` matrix
4519: Output Parameter:
4520: . nz - the maximum number of nonzeros in any row
4522: Level: intermediate
4524: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJRestoreArray()`
4525: @*/
4526: PetscErrorCode MatSeqAIJGetMaxRowNonzeros(Mat A, PetscInt *nz)
4527: {
4528: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;
4530: PetscFunctionBegin;
4531: *nz = aij->rmax;
4532: PetscFunctionReturn(PETSC_SUCCESS);
4533: }
4535: static PetscErrorCode MatCOOStructDestroy_SeqAIJ(PetscCtxRt data)
4536: {
4537: MatCOOStruct_SeqAIJ *coo = *(MatCOOStruct_SeqAIJ **)data;
4539: PetscFunctionBegin;
4540: PetscCall(PetscFree(coo->perm));
4541: PetscCall(PetscFree(coo->jmap));
4542: PetscCall(PetscFree(coo));
4543: PetscFunctionReturn(PETSC_SUCCESS);
4544: }
4546: PetscErrorCode MatSetPreallocationCOO_SeqAIJ(Mat mat, PetscCount coo_n, PetscInt coo_i[], PetscInt coo_j[])
4547: {
4548: MPI_Comm comm;
4549: PetscInt *i, *j;
4550: PetscInt M, N, row, iprev;
4551: PetscCount k, p, q, nneg, nnz, start, end; /* Index the coo array, so use PetscCount as their type */
4552: PetscInt *Ai; /* Change to PetscCount once we use it for row pointers */
4553: PetscInt *Aj;
4554: PetscScalar *Aa;
4555: Mat_SeqAIJ *seqaij = (Mat_SeqAIJ *)mat->data;
4556: MatType rtype;
4557: PetscCount *perm, *jmap;
4558: MatCOOStruct_SeqAIJ *coo;
4559: PetscBool isorted;
4560: PetscBool hypre;
4562: PetscFunctionBegin;
4563: PetscCall(PetscObjectGetComm((PetscObject)mat, &comm));
4564: PetscCall(MatGetSize(mat, &M, &N));
4565: i = coo_i;
4566: j = coo_j;
4567: PetscCall(PetscMalloc1(coo_n, &perm));
4569: /* 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) */
4570: isorted = PETSC_TRUE;
4571: iprev = PETSC_INT_MIN;
4572: for (k = 0; k < coo_n; k++) {
4573: if (j[k] < 0) i[k] = -1;
4574: if (isorted) {
4575: if (i[k] < iprev) isorted = PETSC_FALSE;
4576: else iprev = i[k];
4577: }
4578: perm[k] = k;
4579: }
4581: /* Sort by row if not already */
4582: if (!isorted) PetscCall(PetscSortIntWithIntCountArrayPair(coo_n, i, j, perm));
4583: 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);
4585: /* Advance k to the first row with a non-negative index */
4586: for (k = 0; k < coo_n; k++)
4587: if (i[k] >= 0) break;
4588: nneg = k;
4589: 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 */
4590: nnz = 0; /* Total number of unique nonzeros to be counted */
4591: jmap++; /* Inc jmap by 1 for convenience */
4593: PetscCall(PetscShmgetAllocateArray(M + 1, sizeof(PetscInt), (void **)&Ai)); /* CSR of A */
4594: PetscCall(PetscArrayzero(Ai, M + 1));
4595: PetscCall(PetscShmgetAllocateArray(coo_n - nneg, sizeof(PetscInt), (void **)&Aj)); /* We have at most coo_n-nneg unique nonzeros */
4597: PetscCall(PetscStrcmp("_internal_COO_mat_for_hypre", ((PetscObject)mat)->name, &hypre));
4599: /* In each row, sort by column, then unique column indices to get row length */
4600: Ai++; /* Inc by 1 for convenience */
4601: q = 0; /* q-th unique nonzero, with q starting from 0 */
4602: while (k < coo_n) {
4603: PetscBool strictly_sorted; // this row is strictly sorted?
4604: PetscInt jprev;
4606: /* get [start,end) indices for this row; also check if cols in this row are strictly sorted */
4607: row = i[k];
4608: start = k;
4609: jprev = PETSC_INT_MIN;
4610: strictly_sorted = PETSC_TRUE;
4611: while (k < coo_n && i[k] == row) {
4612: if (strictly_sorted) {
4613: if (j[k] <= jprev) strictly_sorted = PETSC_FALSE;
4614: else jprev = j[k];
4615: }
4616: k++;
4617: }
4618: end = k;
4620: /* hack for HYPRE: swap min column to diag so that diagonal values will go first */
4621: if (hypre) {
4622: PetscInt minj = PETSC_INT_MAX;
4623: PetscBool hasdiag = PETSC_FALSE;
4625: if (strictly_sorted) { // fast path to swap the first and the diag
4626: PetscCount tmp;
4627: for (p = start; p < end; p++) {
4628: if (j[p] == row && p != start) {
4629: j[p] = j[start]; // swap j[], so that the diagonal value will go first (manipulated by perm[])
4630: j[start] = row;
4631: tmp = perm[start];
4632: perm[start] = perm[p]; // also swap perm[] so we can save the call to PetscSortIntWithCountArray() below
4633: perm[p] = tmp;
4634: break;
4635: }
4636: }
4637: } else {
4638: for (p = start; p < end; p++) {
4639: hasdiag = (PetscBool)(hasdiag || (j[p] == row));
4640: minj = PetscMin(minj, j[p]);
4641: }
4643: if (hasdiag) {
4644: for (p = start; p < end; p++) {
4645: if (j[p] == minj) j[p] = row;
4646: else if (j[p] == row) j[p] = minj;
4647: }
4648: }
4649: }
4650: }
4651: // sort by columns in a row. perm[] indicates their original order
4652: if (!strictly_sorted) PetscCall(PetscSortIntWithCountArray(end - start, j + start, perm + start));
4653: 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);
4655: if (strictly_sorted) { // fast path to set Aj[], jmap[], Ai[], nnz, q
4656: for (p = start; p < end; p++, q++) {
4657: Aj[q] = j[p];
4658: jmap[q] = 1;
4659: }
4660: PetscCall(PetscIntCast(end - start, Ai + row));
4661: nnz += Ai[row]; // q is already advanced
4662: } else {
4663: /* Find number of unique col entries in this row */
4664: Aj[q] = j[start]; /* Log the first nonzero in this row */
4665: jmap[q] = 1; /* Number of repeats of this nonzero entry */
4666: Ai[row] = 1;
4667: nnz++;
4669: for (p = start + 1; p < end; p++) { /* Scan remaining nonzero in this row */
4670: if (j[p] != j[p - 1]) { /* Meet a new nonzero */
4671: q++;
4672: jmap[q] = 1;
4673: Aj[q] = j[p];
4674: Ai[row]++;
4675: nnz++;
4676: } else {
4677: jmap[q]++;
4678: }
4679: }
4680: q++; /* Move to next row and thus next unique nonzero */
4681: }
4682: }
4684: Ai--; /* Back to the beginning of Ai[] */
4685: for (k = 0; k < M; k++) Ai[k + 1] += Ai[k];
4686: jmap--; // Back to the beginning of jmap[]
4687: jmap[0] = 0;
4688: for (k = 0; k < nnz; k++) jmap[k + 1] += jmap[k];
4690: if (nnz < coo_n - nneg) { /* Reallocate with actual number of unique nonzeros */
4691: PetscCount *jmap_new;
4692: PetscInt *Aj_new;
4694: PetscCall(PetscMalloc1(nnz + 1, &jmap_new));
4695: PetscCall(PetscArraycpy(jmap_new, jmap, nnz + 1));
4696: PetscCall(PetscFree(jmap));
4697: jmap = jmap_new;
4699: PetscCall(PetscShmgetAllocateArray(nnz, sizeof(PetscInt), (void **)&Aj_new));
4700: PetscCall(PetscArraycpy(Aj_new, Aj, nnz));
4701: PetscCall(PetscShmgetDeallocateArray((void **)&Aj));
4702: Aj = Aj_new;
4703: }
4705: if (nneg) { /* Discard heading entries with negative indices in perm[], as we'll access it from index 0 in MatSetValuesCOO */
4706: PetscCount *perm_new;
4708: PetscCall(PetscMalloc1(coo_n - nneg, &perm_new));
4709: PetscCall(PetscArraycpy(perm_new, perm + nneg, coo_n - nneg));
4710: PetscCall(PetscFree(perm));
4711: perm = perm_new;
4712: }
4714: PetscCall(MatGetRootType_Private(mat, &rtype));
4715: PetscCall(PetscShmgetAllocateArray(nnz, sizeof(PetscScalar), (void **)&Aa));
4716: PetscCall(PetscArrayzero(Aa, nnz));
4717: PetscCall(MatSetSeqAIJWithArrays_private(PETSC_COMM_SELF, M, N, Ai, Aj, Aa, rtype, mat));
4719: seqaij->free_a = seqaij->free_ij = PETSC_TRUE; /* Let newmat own Ai, Aj and Aa */
4721: // Put the COO struct in a container and then attach that to the matrix
4722: PetscCall(PetscMalloc1(1, &coo));
4723: PetscCall(PetscIntCast(nnz, &coo->nz));
4724: coo->n = coo_n;
4725: coo->Atot = coo_n - nneg; // Annz is seqaij->nz, so no need to record that again
4726: coo->jmap = jmap; // of length nnz+1
4727: coo->perm = perm;
4728: PetscCall(PetscObjectContainerCompose((PetscObject)mat, "__PETSc_MatCOOStruct_Host", coo, MatCOOStructDestroy_SeqAIJ));
4729: PetscFunctionReturn(PETSC_SUCCESS);
4730: }
4732: static PetscErrorCode MatSetValuesCOO_SeqAIJ(Mat A, const PetscScalar v[], InsertMode imode)
4733: {
4734: Mat_SeqAIJ *aseq = (Mat_SeqAIJ *)A->data;
4735: PetscCount i, j, Annz = aseq->nz;
4736: PetscCount *perm, *jmap;
4737: PetscScalar *Aa;
4738: PetscContainer container;
4739: MatCOOStruct_SeqAIJ *coo;
4741: PetscFunctionBegin;
4742: PetscCall(PetscObjectQuery((PetscObject)A, "__PETSc_MatCOOStruct_Host", (PetscObject *)&container));
4743: PetscCheck(container, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Not found MatCOOStruct on this matrix");
4744: PetscCall(PetscContainerGetPointer(container, &coo));
4745: perm = coo->perm;
4746: jmap = coo->jmap;
4747: PetscCall(MatSeqAIJGetArray(A, &Aa));
4748: for (i = 0; i < Annz; i++) {
4749: PetscScalar sum = 0.0;
4750: for (j = jmap[i]; j < jmap[i + 1]; j++) sum += v[perm[j]];
4751: Aa[i] = (imode == INSERT_VALUES ? 0.0 : Aa[i]) + sum;
4752: }
4753: PetscCall(MatSeqAIJRestoreArray(A, &Aa));
4754: PetscFunctionReturn(PETSC_SUCCESS);
4755: }
4757: #if defined(PETSC_HAVE_CUDA)
4758: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCUSPARSE(Mat, MatType, MatReuse, Mat *);
4759: #endif
4760: #if defined(PETSC_HAVE_HIP)
4761: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJHIPSPARSE(Mat, MatType, MatReuse, Mat *);
4762: #endif
4763: #if defined(PETSC_HAVE_KOKKOS_KERNELS)
4764: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJKokkos(Mat, MatType, MatReuse, Mat *);
4765: #endif
4767: PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJ(Mat B)
4768: {
4769: Mat_SeqAIJ *b;
4770: PetscMPIInt size;
4772: PetscFunctionBegin;
4773: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)B), &size));
4774: PetscCheck(size <= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Comm must be of size 1");
4776: PetscCall(PetscNew(&b));
4778: B->data = (void *)b;
4779: B->ops[0] = MatOps_Values;
4780: if (B->sortedfull) B->ops->setvalues = MatSetValues_SeqAIJ_SortedFull;
4782: b->row = NULL;
4783: b->col = NULL;
4784: b->icol = NULL;
4785: b->reallocs = 0;
4786: b->ignorezeroentries = PETSC_FALSE;
4787: b->roworiented = PETSC_TRUE;
4788: b->nonew = 0;
4789: b->diag = NULL;
4790: b->solve_work = NULL;
4791: B->spptr = NULL;
4792: b->saved_values = NULL;
4793: b->idiag = NULL;
4794: b->mdiag = NULL;
4795: b->ssor_work = NULL;
4796: b->omega = 1.0;
4797: b->fshift = 0.0;
4798: b->ibdiagvalid = PETSC_FALSE;
4799: b->keepnonzeropattern = PETSC_FALSE;
4801: PetscCall(PetscObjectChangeTypeName((PetscObject)B, MATSEQAIJ));
4802: #if defined(PETSC_HAVE_MATLAB)
4803: PetscCall(PetscObjectComposeFunction((PetscObject)B, "PetscMatlabEnginePut_C", MatlabEnginePut_SeqAIJ));
4804: PetscCall(PetscObjectComposeFunction((PetscObject)B, "PetscMatlabEngineGet_C", MatlabEngineGet_SeqAIJ));
4805: #endif
4806: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSeqAIJSetColumnIndices_C", MatSeqAIJSetColumnIndices_SeqAIJ));
4807: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatStoreValues_C", MatStoreValues_SeqAIJ));
4808: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatRetrieveValues_C", MatRetrieveValues_SeqAIJ));
4809: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqsbaij_C", MatConvert_SeqAIJ_SeqSBAIJ));
4810: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqbaij_C", MatConvert_SeqAIJ_SeqBAIJ));
4811: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqaijperm_C", MatConvert_SeqAIJ_SeqAIJPERM));
4812: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqaijsell_C", MatConvert_SeqAIJ_SeqAIJSELL));
4813: #if defined(PETSC_HAVE_MKL_SPARSE)
4814: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqaijmkl_C", MatConvert_SeqAIJ_SeqAIJMKL));
4815: #endif
4816: #if defined(PETSC_HAVE_CUDA)
4817: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqaijcusparse_C", MatConvert_SeqAIJ_SeqAIJCUSPARSE));
4818: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqaijcusparse_seqaij_C", MatProductSetFromOptions_SeqAIJ));
4819: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqaij_seqaijcusparse_C", MatProductSetFromOptions_SeqAIJ));
4820: #endif
4821: #if defined(PETSC_HAVE_HIP)
4822: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqaijhipsparse_C", MatConvert_SeqAIJ_SeqAIJHIPSPARSE));
4823: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqaijhipsparse_seqaij_C", MatProductSetFromOptions_SeqAIJ));
4824: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqaij_seqaijhipsparse_C", MatProductSetFromOptions_SeqAIJ));
4825: #endif
4826: #if defined(PETSC_HAVE_KOKKOS_KERNELS)
4827: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqaijkokkos_C", MatConvert_SeqAIJ_SeqAIJKokkos));
4828: #endif
4829: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqaijcrl_C", MatConvert_SeqAIJ_SeqAIJCRL));
4830: #if defined(PETSC_HAVE_ELEMENTAL)
4831: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_elemental_C", MatConvert_SeqAIJ_Elemental));
4832: #endif
4833: #if defined(PETSC_HAVE_SCALAPACK) && (defined(PETSC_USE_REAL_SINGLE) || defined(PETSC_USE_REAL_DOUBLE))
4834: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_scalapack_C", MatConvert_AIJ_ScaLAPACK));
4835: #endif
4836: #if defined(PETSC_HAVE_HYPRE)
4837: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_hypre_C", MatConvert_AIJ_HYPRE));
4838: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_transpose_seqaij_seqaij_C", MatProductSetFromOptions_Transpose_AIJ_AIJ));
4839: #endif
4840: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqdense_C", MatConvert_SeqAIJ_SeqDense));
4841: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqsell_C", MatConvert_SeqAIJ_SeqSELL));
4842: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_is_C", MatConvert_XAIJ_IS));
4843: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatIsTranspose_C", MatIsTranspose_SeqAIJ));
4844: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatIsHermitianTranspose_C", MatIsHermitianTranspose_SeqAIJ));
4845: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSeqAIJSetPreallocation_C", MatSeqAIJSetPreallocation_SeqAIJ));
4846: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatResetPreallocation_C", MatResetPreallocation_SeqAIJ));
4847: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatResetHash_C", MatResetHash_SeqAIJ));
4848: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSeqAIJSetPreallocationCSR_C", MatSeqAIJSetPreallocationCSR_SeqAIJ));
4849: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatReorderForNonzeroDiagonal_C", MatReorderForNonzeroDiagonal_SeqAIJ));
4850: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_is_seqaij_C", MatProductSetFromOptions_IS_XAIJ));
4851: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqdense_seqaij_C", MatProductSetFromOptions_SeqDense_SeqAIJ));
4852: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqaij_seqaij_C", MatProductSetFromOptions_SeqAIJ));
4853: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSeqAIJKron_C", MatSeqAIJKron_SeqAIJ));
4854: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSetPreallocationCOO_C", MatSetPreallocationCOO_SeqAIJ));
4855: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSetValuesCOO_C", MatSetValuesCOO_SeqAIJ));
4856: PetscCall(MatCreate_SeqAIJ_Inode(B));
4857: PetscCall(PetscObjectChangeTypeName((PetscObject)B, MATSEQAIJ));
4858: PetscCall(MatSeqAIJSetTypeFromOptions(B)); /* this allows changing the matrix subtype to say MATSEQAIJPERM */
4859: PetscFunctionReturn(PETSC_SUCCESS);
4860: }
4862: /*
4863: Given a matrix generated with MatGetFactor() duplicates all the information in A into C
4864: */
4865: PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C, Mat A, MatDuplicateOption cpvalues, PetscBool mallocmatspace)
4866: {
4867: Mat_SeqAIJ *c = (Mat_SeqAIJ *)C->data, *a = (Mat_SeqAIJ *)A->data;
4868: PetscInt m = A->rmap->n, i;
4870: PetscFunctionBegin;
4871: PetscCheck(A->assembled || cpvalues == MAT_DO_NOT_COPY_VALUES, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Cannot duplicate unassembled matrix");
4873: C->factortype = A->factortype;
4874: c->row = NULL;
4875: c->col = NULL;
4876: c->icol = NULL;
4877: c->reallocs = 0;
4878: C->assembled = A->assembled;
4880: if (A->preallocated) {
4881: PetscCall(PetscLayoutReference(A->rmap, &C->rmap));
4882: PetscCall(PetscLayoutReference(A->cmap, &C->cmap));
4884: if (!A->hash_active) {
4885: PetscCall(PetscMalloc1(m, &c->imax));
4886: PetscCall(PetscArraycpy(c->imax, a->imax, m));
4887: PetscCall(PetscMalloc1(m, &c->ilen));
4888: PetscCall(PetscArraycpy(c->ilen, a->ilen, m));
4890: /* allocate the matrix space */
4891: if (mallocmatspace) {
4892: PetscCall(PetscShmgetAllocateArray(a->i[m], sizeof(PetscScalar), (void **)&c->a));
4893: PetscCall(PetscShmgetAllocateArray(a->i[m], sizeof(PetscInt), (void **)&c->j));
4894: PetscCall(PetscShmgetAllocateArray(m + 1, sizeof(PetscInt), (void **)&c->i));
4895: PetscCall(PetscArraycpy(c->i, a->i, m + 1));
4896: c->free_a = PETSC_TRUE;
4897: c->free_ij = PETSC_TRUE;
4898: if (m > 0) {
4899: PetscCall(PetscArraycpy(c->j, a->j, a->i[m]));
4900: if (cpvalues == MAT_COPY_VALUES) {
4901: const PetscScalar *aa;
4903: PetscCall(MatSeqAIJGetArrayRead(A, &aa));
4904: PetscCall(PetscArraycpy(c->a, aa, a->i[m]));
4905: PetscCall(MatSeqAIJGetArrayRead(A, &aa));
4906: } else {
4907: PetscCall(PetscArrayzero(c->a, a->i[m]));
4908: }
4909: }
4910: }
4911: C->preallocated = PETSC_TRUE;
4912: } else {
4913: PetscCheck(mallocmatspace, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONGSTATE, "Cannot malloc matrix memory from a non-preallocated matrix");
4914: PetscCall(MatSetUp(C));
4915: }
4917: c->ignorezeroentries = a->ignorezeroentries;
4918: c->roworiented = a->roworiented;
4919: c->nonew = a->nonew;
4920: c->solve_work = NULL;
4921: c->saved_values = NULL;
4922: c->idiag = NULL;
4923: c->ssor_work = NULL;
4924: c->keepnonzeropattern = a->keepnonzeropattern;
4926: c->rmax = a->rmax;
4927: c->nz = a->nz;
4928: c->maxnz = a->nz; /* Since we allocate exactly the right amount */
4930: c->compressedrow.use = a->compressedrow.use;
4931: c->compressedrow.nrows = a->compressedrow.nrows;
4932: if (a->compressedrow.use) {
4933: i = a->compressedrow.nrows;
4934: PetscCall(PetscMalloc2(i + 1, &c->compressedrow.i, i, &c->compressedrow.rindex));
4935: PetscCall(PetscArraycpy(c->compressedrow.i, a->compressedrow.i, i + 1));
4936: PetscCall(PetscArraycpy(c->compressedrow.rindex, a->compressedrow.rindex, i));
4937: } else {
4938: c->compressedrow.use = PETSC_FALSE;
4939: c->compressedrow.i = NULL;
4940: c->compressedrow.rindex = NULL;
4941: }
4942: c->nonzerorowcnt = a->nonzerorowcnt;
4943: C->nonzerostate = A->nonzerostate;
4945: PetscCall(MatDuplicate_SeqAIJ_Inode(A, cpvalues, &C));
4946: }
4947: PetscCall(PetscFunctionListDuplicate(((PetscObject)A)->qlist, &((PetscObject)C)->qlist));
4948: PetscFunctionReturn(PETSC_SUCCESS);
4949: }
4951: PetscErrorCode MatDuplicate_SeqAIJ(Mat A, MatDuplicateOption cpvalues, Mat *B)
4952: {
4953: PetscFunctionBegin;
4954: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), B));
4955: PetscCall(MatSetSizes(*B, A->rmap->n, A->cmap->n, A->rmap->n, A->cmap->n));
4956: if (!(A->rmap->n % A->rmap->bs) && !(A->cmap->n % A->cmap->bs)) PetscCall(MatSetBlockSizesFromMats(*B, A, A));
4957: PetscCall(MatSetType(*B, ((PetscObject)A)->type_name));
4958: PetscCall(MatDuplicateNoCreate_SeqAIJ(*B, A, cpvalues, PETSC_TRUE));
4959: PetscFunctionReturn(PETSC_SUCCESS);
4960: }
4962: PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer)
4963: {
4964: PetscBool isbinary, ishdf5;
4966: PetscFunctionBegin;
4969: /* force binary viewer to load .info file if it has not yet done so */
4970: PetscCall(PetscViewerSetUp(viewer));
4971: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
4972: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
4973: if (isbinary) {
4974: PetscCall(MatLoad_SeqAIJ_Binary(newMat, viewer));
4975: } else if (ishdf5) {
4976: #if defined(PETSC_HAVE_HDF5)
4977: PetscCall(MatLoad_AIJ_HDF5(newMat, viewer));
4978: #else
4979: SETERRQ(PetscObjectComm((PetscObject)newMat), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
4980: #endif
4981: } else {
4982: 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);
4983: }
4984: PetscFunctionReturn(PETSC_SUCCESS);
4985: }
4987: PetscErrorCode MatLoad_SeqAIJ_Binary(Mat mat, PetscViewer viewer)
4988: {
4989: Mat_SeqAIJ *a = (Mat_SeqAIJ *)mat->data;
4990: PetscInt header[4], *rowlens, M, N, nz, sum, rows, cols, i;
4992: PetscFunctionBegin;
4993: PetscCall(PetscViewerSetUp(viewer));
4995: /* read in matrix header */
4996: PetscCall(PetscViewerBinaryRead(viewer, header, 4, NULL, PETSC_INT));
4997: PetscCheck(header[0] == MAT_FILE_CLASSID, PETSC_COMM_SELF, PETSC_ERR_FILE_UNEXPECTED, "Not a matrix object in file");
4998: M = header[1];
4999: N = header[2];
5000: nz = header[3];
5001: PetscCheck(M >= 0, PetscObjectComm((PetscObject)viewer), PETSC_ERR_FILE_UNEXPECTED, "Matrix row size (%" PetscInt_FMT ") in file is negative", M);
5002: PetscCheck(N >= 0, PetscObjectComm((PetscObject)viewer), PETSC_ERR_FILE_UNEXPECTED, "Matrix column size (%" PetscInt_FMT ") in file is negative", N);
5003: PetscCheck(nz >= 0, PETSC_COMM_SELF, PETSC_ERR_FILE_UNEXPECTED, "Matrix stored in special format on disk, cannot load as SeqAIJ");
5005: /* set block sizes from the viewer's .info file */
5006: PetscCall(MatLoad_Binary_BlockSizes(mat, viewer));
5007: /* set local and global sizes if not set already */
5008: if (mat->rmap->n < 0) mat->rmap->n = M;
5009: if (mat->cmap->n < 0) mat->cmap->n = N;
5010: if (mat->rmap->N < 0) mat->rmap->N = M;
5011: if (mat->cmap->N < 0) mat->cmap->N = N;
5012: PetscCall(PetscLayoutSetUp(mat->rmap));
5013: PetscCall(PetscLayoutSetUp(mat->cmap));
5015: /* check if the matrix sizes are correct */
5016: PetscCall(MatGetSize(mat, &rows, &cols));
5017: 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);
5019: /* read in row lengths */
5020: PetscCall(PetscMalloc1(M, &rowlens));
5021: PetscCall(PetscViewerBinaryRead(viewer, rowlens, M, NULL, PETSC_INT));
5022: /* check if sum(rowlens) is same as nz */
5023: sum = 0;
5024: for (i = 0; i < M; i++) sum += rowlens[i];
5025: 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);
5026: /* preallocate and check sizes */
5027: PetscCall(MatSeqAIJSetPreallocation_SeqAIJ(mat, 0, rowlens));
5028: PetscCall(MatGetSize(mat, &rows, &cols));
5029: 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);
5030: /* store row lengths */
5031: PetscCall(PetscArraycpy(a->ilen, rowlens, M));
5032: PetscCall(PetscFree(rowlens));
5034: /* fill in "i" row pointers */
5035: a->i[0] = 0;
5036: for (i = 0; i < M; i++) a->i[i + 1] = a->i[i] + a->ilen[i];
5037: /* read in "j" column indices */
5038: PetscCall(PetscViewerBinaryRead(viewer, a->j, nz, NULL, PETSC_INT));
5039: /* read in "a" nonzero values */
5040: PetscCall(PetscViewerBinaryRead(viewer, a->a, nz, NULL, PETSC_SCALAR));
5042: PetscCall(MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY));
5043: PetscCall(MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY));
5044: PetscFunctionReturn(PETSC_SUCCESS);
5045: }
5047: PetscErrorCode MatEqual_SeqAIJ(Mat A, Mat B, PetscBool *flg)
5048: {
5049: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data, *b = (Mat_SeqAIJ *)B->data;
5050: const PetscScalar *aa, *ba;
5052: PetscFunctionBegin;
5053: /* If the matrix dimensions are not equal,or no of nonzeros */
5054: if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) || (a->nz != b->nz)) {
5055: *flg = PETSC_FALSE;
5056: PetscFunctionReturn(PETSC_SUCCESS);
5057: }
5059: /* if the a->i are the same */
5060: PetscCall(PetscArraycmp(a->i, b->i, A->rmap->n + 1, flg));
5061: if (!*flg) PetscFunctionReturn(PETSC_SUCCESS);
5063: /* if a->j are the same */
5064: PetscCall(PetscArraycmp(a->j, b->j, a->nz, flg));
5065: if (!*flg) PetscFunctionReturn(PETSC_SUCCESS);
5067: PetscCall(MatSeqAIJGetArrayRead(A, &aa));
5068: PetscCall(MatSeqAIJGetArrayRead(B, &ba));
5069: /* if a->a are the same */
5070: PetscCall(PetscArraycmp(aa, ba, a->nz, flg));
5071: PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
5072: PetscCall(MatSeqAIJRestoreArrayRead(B, &ba));
5073: PetscFunctionReturn(PETSC_SUCCESS);
5074: }
5076: /*@
5077: MatCreateSeqAIJWithArrays - Creates an sequential `MATSEQAIJ` matrix using matrix elements (in CSR format)
5078: provided by the user.
5080: Collective
5082: Input Parameters:
5083: + comm - must be an MPI communicator of size 1
5084: . m - number of rows
5085: . n - number of columns
5086: . i - row indices; that is i[0] = 0, i[row] = i[row-1] + number of elements in that row of the matrix
5087: . j - column indices
5088: - a - matrix values
5090: Output Parameter:
5091: . mat - the matrix
5093: Level: intermediate
5095: Notes:
5096: The `i`, `j`, and `a` arrays are not copied by this routine, the user must free these arrays
5097: once the matrix is destroyed and not before
5099: You cannot set new nonzero locations into this matrix, that will generate an error.
5101: The `i` and `j` indices are 0 based
5103: The format which is used for the sparse matrix input, is equivalent to a
5104: row-major ordering.. i.e for the following matrix, the input data expected is
5105: as shown
5106: .vb
5107: 1 0 0
5108: 2 0 3
5109: 4 5 6
5111: i = {0,1,3,6} [size = nrow+1 = 3+1]
5112: j = {0,0,2,0,1,2} [size = 6]; values must be sorted for each row
5113: v = {1,2,3,4,5,6} [size = 6]
5114: .ve
5116: .seealso: [](ch_matrices), `Mat`, `MatCreate()`, `MatCreateAIJ()`, `MatCreateSeqAIJ()`, `MatCreateMPIAIJWithArrays()`, `MatMPIAIJSetPreallocationCSR()`
5117: @*/
5118: PetscErrorCode MatCreateSeqAIJWithArrays(MPI_Comm comm, PetscInt m, PetscInt n, PetscInt i[], PetscInt j[], PetscScalar a[], Mat *mat)
5119: {
5120: PetscInt ii;
5121: Mat_SeqAIJ *aij;
5122: PetscInt jj;
5124: PetscFunctionBegin;
5125: PetscCheck(m <= 0 || i[0] == 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "i (row indices) must start with 0");
5126: PetscCall(MatCreate(comm, mat));
5127: PetscCall(MatSetSizes(*mat, m, n, m, n));
5128: /* PetscCall(MatSetBlockSizes(*mat,,)); */
5129: PetscCall(MatSetType(*mat, MATSEQAIJ));
5130: PetscCall(MatSeqAIJSetPreallocation_SeqAIJ(*mat, MAT_SKIP_ALLOCATION, NULL));
5131: aij = (Mat_SeqAIJ *)(*mat)->data;
5132: PetscCall(PetscMalloc1(m, &aij->imax));
5133: PetscCall(PetscMalloc1(m, &aij->ilen));
5135: aij->i = i;
5136: aij->j = j;
5137: aij->a = a;
5138: aij->nonew = -1; /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
5139: aij->free_a = PETSC_FALSE;
5140: aij->free_ij = PETSC_FALSE;
5142: for (ii = 0, aij->nonzerorowcnt = 0, aij->rmax = 0; ii < m; ii++) {
5143: aij->ilen[ii] = aij->imax[ii] = i[ii + 1] - i[ii];
5144: if (PetscDefined(USE_DEBUG)) {
5145: 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]);
5146: for (jj = i[ii] + 1; jj < i[ii + 1]; jj++) {
5147: 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);
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 identical to previous entry", jj - i[ii], j[jj], ii);
5149: }
5150: }
5151: }
5152: if (PetscDefined(USE_DEBUG)) {
5153: for (ii = 0; ii < aij->i[m]; ii++) {
5154: PetscCheck(j[ii] >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Negative column index at location = %" PetscInt_FMT " index = %" PetscInt_FMT, ii, j[ii]);
5155: 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);
5156: }
5157: }
5159: PetscCall(MatAssemblyBegin(*mat, MAT_FINAL_ASSEMBLY));
5160: PetscCall(MatAssemblyEnd(*mat, MAT_FINAL_ASSEMBLY));
5161: PetscFunctionReturn(PETSC_SUCCESS);
5162: }
5164: /*@
5165: MatCreateSeqAIJFromTriple - Creates an sequential `MATSEQAIJ` matrix using matrix elements (in COO format)
5166: provided by the user.
5168: Collective
5170: Input Parameters:
5171: + comm - must be an MPI communicator of size 1
5172: . m - number of rows
5173: . n - number of columns
5174: . i - row indices
5175: . j - column indices
5176: . a - matrix values
5177: . nz - number of nonzeros
5178: - idx - if the `i` and `j` indices start with 1 use `PETSC_TRUE` otherwise use `PETSC_FALSE`
5180: Output Parameter:
5181: . mat - the matrix
5183: Level: intermediate
5185: Example:
5186: For the following matrix, the input data expected is as shown (using 0 based indexing)
5187: .vb
5188: 1 0 0
5189: 2 0 3
5190: 4 5 6
5192: i = {0,1,1,2,2,2}
5193: j = {0,0,2,0,1,2}
5194: v = {1,2,3,4,5,6}
5195: .ve
5197: Note:
5198: Instead of using this function, users should also consider `MatSetPreallocationCOO()` and `MatSetValuesCOO()`, which allow repeated or remote entries,
5199: and are particularly useful in iterative applications.
5201: .seealso: [](ch_matrices), `Mat`, `MatCreate()`, `MatCreateAIJ()`, `MatCreateSeqAIJ()`, `MatCreateSeqAIJWithArrays()`, `MatMPIAIJSetPreallocationCSR()`, `MatSetValuesCOO()`, `MatSetPreallocationCOO()`
5202: @*/
5203: PetscErrorCode MatCreateSeqAIJFromTriple(MPI_Comm comm, PetscInt m, PetscInt n, PetscInt i[], PetscInt j[], PetscScalar a[], Mat *mat, PetscCount nz, PetscBool idx)
5204: {
5205: PetscInt ii, *nnz, one = 1, row, col;
5207: PetscFunctionBegin;
5208: PetscCall(PetscCalloc1(m, &nnz));
5209: for (ii = 0; ii < nz; ii++) nnz[i[ii] - !!idx] += 1;
5210: PetscCall(MatCreate(comm, mat));
5211: PetscCall(MatSetSizes(*mat, m, n, m, n));
5212: PetscCall(MatSetType(*mat, MATSEQAIJ));
5213: PetscCall(MatSeqAIJSetPreallocation_SeqAIJ(*mat, 0, nnz));
5214: for (ii = 0; ii < nz; ii++) {
5215: if (idx) {
5216: row = i[ii] - 1;
5217: col = j[ii] - 1;
5218: } else {
5219: row = i[ii];
5220: col = j[ii];
5221: }
5222: PetscCall(MatSetValues(*mat, one, &row, one, &col, &a[ii], ADD_VALUES));
5223: }
5224: PetscCall(MatAssemblyBegin(*mat, MAT_FINAL_ASSEMBLY));
5225: PetscCall(MatAssemblyEnd(*mat, MAT_FINAL_ASSEMBLY));
5226: PetscCall(PetscFree(nnz));
5227: PetscFunctionReturn(PETSC_SUCCESS);
5228: }
5230: PetscErrorCode MatCreateMPIMatConcatenateSeqMat_SeqAIJ(MPI_Comm comm, Mat inmat, PetscInt n, MatReuse scall, Mat *outmat)
5231: {
5232: PetscFunctionBegin;
5233: PetscCall(MatCreateMPIMatConcatenateSeqMat_MPIAIJ(comm, inmat, n, scall, outmat));
5234: PetscFunctionReturn(PETSC_SUCCESS);
5235: }
5237: /*
5238: Permute A into C's *local* index space using rowemb,colemb.
5239: The embedding are supposed to be injections and the above implies that the range of rowemb is a subset
5240: of [0,m), colemb is in [0,n).
5241: If pattern == DIFFERENT_NONZERO_PATTERN, C is preallocated according to A.
5242: */
5243: PetscErrorCode MatSetSeqMat_SeqAIJ(Mat C, IS rowemb, IS colemb, MatStructure pattern, Mat B)
5244: {
5245: /* If making this function public, change the error returned in this function away from _PLIB. */
5246: Mat_SeqAIJ *Baij;
5247: PetscBool seqaij;
5248: PetscInt m, n, *nz, i, j, count;
5249: PetscScalar v;
5250: const PetscInt *rowindices, *colindices;
5252: PetscFunctionBegin;
5253: if (!B) PetscFunctionReturn(PETSC_SUCCESS);
5254: /* Check to make sure the target matrix (and embeddings) are compatible with C and each other. */
5255: PetscCall(PetscObjectBaseTypeCompare((PetscObject)B, MATSEQAIJ, &seqaij));
5256: PetscCheck(seqaij, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Input matrix is of wrong type");
5257: if (rowemb) {
5258: PetscCall(ISGetLocalSize(rowemb, &m));
5259: 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);
5260: } else PetscCheck(C->rmap->n == B->rmap->n, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Input matrix is row-incompatible with the target matrix");
5261: if (colemb) {
5262: PetscCall(ISGetLocalSize(colemb, &n));
5263: 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);
5264: } else PetscCheck(C->cmap->n == B->cmap->n, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Input matrix is col-incompatible with the target matrix");
5266: Baij = (Mat_SeqAIJ *)B->data;
5267: rowindices = NULL;
5268: if (rowemb) PetscCall(ISGetIndices(rowemb, &rowindices));
5269: if (pattern == DIFFERENT_NONZERO_PATTERN) {
5270: PetscCall(PetscMalloc1(C->rmap->n, &nz));
5271: if (rowemb) {
5272: PetscCall(PetscArrayzero(nz, C->rmap->n));
5273: for (i = 0; i < B->rmap->n; i++) nz[rowindices[i]] = Baij->i[i + 1] - Baij->i[i];
5274: } else {
5275: for (i = 0; i < B->rmap->n; i++) nz[i] = Baij->i[i + 1] - Baij->i[i];
5276: }
5277: PetscCall(MatSeqAIJSetPreallocation(C, 0, nz));
5278: PetscCall(PetscFree(nz));
5279: }
5280: if (pattern == SUBSET_NONZERO_PATTERN) PetscCall(MatZeroEntries(C));
5281: count = 0;
5282: colindices = NULL;
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: if (colemb) PetscCall(ISRestoreIndices(colemb, &colindices));
5298: if (rowemb) PetscCall(ISRestoreIndices(rowemb, &rowindices));
5299: /* FIXME: set C's nonzerostate correctly. */
5300: /* Assembly for C is necessary. */
5301: C->preallocated = PETSC_TRUE;
5302: C->assembled = PETSC_TRUE;
5303: C->was_assembled = PETSC_FALSE;
5304: PetscFunctionReturn(PETSC_SUCCESS);
5305: }
5307: PetscErrorCode MatEliminateZeros_SeqAIJ(Mat A, PetscBool keep)
5308: {
5309: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
5310: MatScalar *aa = a->a;
5311: PetscInt m = A->rmap->n, fshift = 0, fshift_prev = 0, i, k;
5312: PetscInt *ailen = a->ilen, *imax = a->imax, *ai = a->i, *aj = a->j, rmax = 0;
5314: PetscFunctionBegin;
5315: PetscCheck(A->assembled, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Cannot eliminate zeros for unassembled matrix");
5316: if (m) rmax = ailen[0]; /* determine row with most nonzeros */
5317: for (i = 1, a->nonzerorowcnt = 0; i <= m; i++) {
5318: /* move each nonzero entry back by the amount of zero slots (fshift) before it*/
5319: for (k = ai[i - 1]; k < ai[i]; k++) {
5320: if (aa[k] == 0 && (aj[k] != i - 1 || !keep)) fshift++;
5321: else {
5322: if (aa[k] == 0 && aj[k] == i - 1) PetscCall(PetscInfo(A, "Keep the diagonal zero at row %" PetscInt_FMT "\n", i - 1));
5323: aa[k - fshift] = aa[k];
5324: aj[k - fshift] = aj[k];
5325: }
5326: }
5327: ai[i - 1] -= fshift_prev; // safe to update ai[i-1] now since it will not be used in the next iteration
5328: fshift_prev = fshift;
5329: /* reset ilen and imax for each row */
5330: ailen[i - 1] = imax[i - 1] = ai[i] - fshift - ai[i - 1];
5331: a->nonzerorowcnt += ((ai[i] - fshift - ai[i - 1]) > 0);
5332: rmax = PetscMax(rmax, ailen[i - 1]);
5333: }
5334: if (fshift) {
5335: if (m) {
5336: ai[m] -= fshift;
5337: a->nz = ai[m];
5338: }
5339: 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));
5340: A->nonzerostate++;
5341: A->info.nz_unneeded += (PetscReal)fshift;
5342: a->rmax = rmax;
5343: if (a->inode.use && a->inode.checked) PetscCall(MatSeqAIJCheckInode(A));
5344: PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
5345: PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
5346: }
5347: PetscFunctionReturn(PETSC_SUCCESS);
5348: }
5350: PetscFunctionList MatSeqAIJList = NULL;
5352: /*@
5353: MatSeqAIJSetType - Converts a `MATSEQAIJ` matrix to a subtype
5355: Collective
5357: Input Parameters:
5358: + mat - the matrix object
5359: - matype - matrix type
5361: Options Database Key:
5362: . -mat_seqaij_type method - for example seqaijcrl
5364: Level: intermediate
5366: .seealso: [](ch_matrices), `Mat`, `PCSetType()`, `VecSetType()`, `MatCreate()`, `MatType`
5367: @*/
5368: PetscErrorCode MatSeqAIJSetType(Mat mat, MatType matype)
5369: {
5370: PetscBool sametype;
5371: PetscErrorCode (*r)(Mat, MatType, MatReuse, Mat *);
5373: PetscFunctionBegin;
5375: PetscCall(PetscObjectTypeCompare((PetscObject)mat, matype, &sametype));
5376: if (sametype) PetscFunctionReturn(PETSC_SUCCESS);
5378: PetscCall(PetscFunctionListFind(MatSeqAIJList, matype, &r));
5379: PetscCheck(r, PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown Mat type given: %s", matype);
5380: PetscCall((*r)(mat, matype, MAT_INPLACE_MATRIX, &mat));
5381: PetscFunctionReturn(PETSC_SUCCESS);
5382: }
5384: /*@C
5385: MatSeqAIJRegister - - Adds a new sub-matrix type for sequential `MATSEQAIJ` matrices
5387: Not Collective, No Fortran Support
5389: Input Parameters:
5390: + sname - name of a new user-defined matrix type, for example `MATSEQAIJCRL`
5391: - function - routine to convert to subtype
5393: Level: advanced
5395: Notes:
5396: `MatSeqAIJRegister()` may be called multiple times to add several user-defined solvers.
5398: Then, your matrix can be chosen with the procedural interface at runtime via the option
5399: .vb
5400: -mat_seqaij_type my_mat
5401: .ve
5403: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJRegisterAll()`
5404: @*/
5405: PetscErrorCode MatSeqAIJRegister(const char sname[], PetscErrorCode (*function)(Mat, MatType, MatReuse, Mat *))
5406: {
5407: PetscFunctionBegin;
5408: PetscCall(MatInitializePackage());
5409: PetscCall(PetscFunctionListAdd(&MatSeqAIJList, sname, function));
5410: PetscFunctionReturn(PETSC_SUCCESS);
5411: }
5413: PetscBool MatSeqAIJRegisterAllCalled = PETSC_FALSE;
5415: /*@C
5416: MatSeqAIJRegisterAll - Registers all of the matrix subtypes of `MATSSEQAIJ`
5418: Not Collective
5420: Level: advanced
5422: Note:
5423: This registers the versions of `MATSEQAIJ` for GPUs
5425: .seealso: [](ch_matrices), `Mat`, `MatRegisterAll()`, `MatSeqAIJRegister()`
5426: @*/
5427: PetscErrorCode MatSeqAIJRegisterAll(void)
5428: {
5429: PetscFunctionBegin;
5430: if (MatSeqAIJRegisterAllCalled) PetscFunctionReturn(PETSC_SUCCESS);
5431: MatSeqAIJRegisterAllCalled = PETSC_TRUE;
5433: PetscCall(MatSeqAIJRegister(MATSEQAIJCRL, MatConvert_SeqAIJ_SeqAIJCRL));
5434: PetscCall(MatSeqAIJRegister(MATSEQAIJPERM, MatConvert_SeqAIJ_SeqAIJPERM));
5435: PetscCall(MatSeqAIJRegister(MATSEQAIJSELL, MatConvert_SeqAIJ_SeqAIJSELL));
5436: #if defined(PETSC_HAVE_MKL_SPARSE)
5437: PetscCall(MatSeqAIJRegister(MATSEQAIJMKL, MatConvert_SeqAIJ_SeqAIJMKL));
5438: #endif
5439: #if defined(PETSC_HAVE_CUDA)
5440: PetscCall(MatSeqAIJRegister(MATSEQAIJCUSPARSE, MatConvert_SeqAIJ_SeqAIJCUSPARSE));
5441: #endif
5442: #if defined(PETSC_HAVE_HIP)
5443: PetscCall(MatSeqAIJRegister(MATSEQAIJHIPSPARSE, MatConvert_SeqAIJ_SeqAIJHIPSPARSE));
5444: #endif
5445: #if defined(PETSC_HAVE_KOKKOS_KERNELS)
5446: PetscCall(MatSeqAIJRegister(MATSEQAIJKOKKOS, MatConvert_SeqAIJ_SeqAIJKokkos));
5447: #endif
5448: #if defined(PETSC_HAVE_VIENNACL) && defined(PETSC_HAVE_VIENNACL_NO_CUDA)
5449: PetscCall(MatSeqAIJRegister(MATMPIAIJVIENNACL, MatConvert_SeqAIJ_SeqAIJViennaCL));
5450: #endif
5451: PetscFunctionReturn(PETSC_SUCCESS);
5452: }
5454: /*
5455: Special version for direct calls from Fortran
5456: */
5457: #if defined(PETSC_HAVE_FORTRAN_CAPS)
5458: #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
5459: #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
5460: #define matsetvaluesseqaij_ matsetvaluesseqaij
5461: #endif
5463: /* Change these macros so can be used in void function */
5465: /* Change these macros so can be used in void function */
5466: /* Identical to PetscCallVoid, except it assigns to *_ierr */
5467: #undef PetscCall
5468: #define PetscCall(...) \
5469: do { \
5470: PetscErrorCode ierr_msv_mpiaij = __VA_ARGS__; \
5471: if (PetscUnlikely(ierr_msv_mpiaij)) { \
5472: *_ierr = PetscError(PETSC_COMM_SELF, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr_msv_mpiaij, PETSC_ERROR_REPEAT, " "); \
5473: return; \
5474: } \
5475: } while (0)
5477: #undef SETERRQ
5478: #define SETERRQ(comm, ierr, ...) \
5479: do { \
5480: *_ierr = PetscError(comm, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr, PETSC_ERROR_INITIAL, __VA_ARGS__); \
5481: return; \
5482: } while (0)
5484: PETSC_EXTERN void matsetvaluesseqaij_(Mat *AA, PetscInt *mm, const PetscInt im[], PetscInt *nn, const PetscInt in[], const PetscScalar v[], InsertMode *isis, PetscErrorCode *_ierr)
5485: {
5486: Mat A = *AA;
5487: PetscInt m = *mm, n = *nn;
5488: InsertMode is = *isis;
5489: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
5490: PetscInt *rp, k, low, high, t, ii, row, nrow, i, col, l, rmax, N;
5491: PetscInt *imax, *ai, *ailen;
5492: PetscInt *aj, nonew = a->nonew, lastcol = -1;
5493: MatScalar *ap, value, *aa;
5494: PetscBool ignorezeroentries = a->ignorezeroentries;
5495: PetscBool roworiented = a->roworiented;
5497: PetscFunctionBegin;
5498: MatCheckPreallocated(A, 1);
5499: imax = a->imax;
5500: ai = a->i;
5501: ailen = a->ilen;
5502: aj = a->j;
5503: aa = a->a;
5505: for (k = 0; k < m; k++) { /* loop over added rows */
5506: row = im[k];
5507: if (row < 0) continue;
5508: PetscCheck(row < A->rmap->n, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_OUTOFRANGE, "Row too large");
5509: rp = aj + ai[row];
5510: ap = aa + ai[row];
5511: rmax = imax[row];
5512: nrow = ailen[row];
5513: low = 0;
5514: high = nrow;
5515: for (l = 0; l < n; l++) { /* loop over added columns */
5516: if (in[l] < 0) continue;
5517: PetscCheck(in[l] < A->cmap->n, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_OUTOFRANGE, "Column too large");
5518: col = in[l];
5519: if (roworiented) value = v[l + k * n];
5520: else value = v[k + l * m];
5522: if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
5524: if (col <= lastcol) low = 0;
5525: else high = nrow;
5526: lastcol = col;
5527: while (high - low > 5) {
5528: t = (low + high) / 2;
5529: if (rp[t] > col) high = t;
5530: else low = t;
5531: }
5532: for (i = low; i < high; i++) {
5533: if (rp[i] > col) break;
5534: if (rp[i] == col) {
5535: if (is == ADD_VALUES) ap[i] += value;
5536: else ap[i] = value;
5537: goto noinsert;
5538: }
5539: }
5540: if (value == 0.0 && ignorezeroentries) goto noinsert;
5541: if (nonew == 1) goto noinsert;
5542: PetscCheck(nonew != -1, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_OUTOFRANGE, "Inserting a new nonzero in the matrix");
5543: MatSeqXAIJReallocateAIJ(A, A->rmap->n, 1, nrow, row, col, rmax, aa, ai, aj, rp, ap, imax, nonew, MatScalar);
5544: N = nrow++ - 1;
5545: a->nz++;
5546: high++;
5547: /* shift up all the later entries in this row */
5548: for (ii = N; ii >= i; ii--) {
5549: rp[ii + 1] = rp[ii];
5550: ap[ii + 1] = ap[ii];
5551: }
5552: rp[i] = col;
5553: ap[i] = value;
5554: noinsert:;
5555: low = i + 1;
5556: }
5557: ailen[row] = nrow;
5558: }
5559: PetscFunctionReturnVoid();
5560: }
5561: /* Undefining these here since they were redefined from their original definition above! No
5562: * other PETSc functions should be defined past this point, as it is impossible to recover the
5563: * original definitions */
5564: #undef PetscCall
5565: #undef SETERRQ