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) {
2023: PetscCall(PetscLogFlops(2.0 * a->nz));
2024: } else {
2025: PetscCall(PetscLogFlops(a->nz)); /* assumes 1/2 in upper */
2026: }
2027: }
2028: }
2029: PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
2030: PetscCall(VecRestoreArray(xx, &x));
2031: PetscCall(VecRestoreArrayRead(bb, &b));
2032: PetscFunctionReturn(PETSC_SUCCESS);
2033: }
2035: static PetscErrorCode MatGetInfo_SeqAIJ(Mat A, MatInfoType flag, MatInfo *info)
2036: {
2037: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
2039: PetscFunctionBegin;
2040: info->block_size = 1.0;
2041: info->nz_allocated = a->maxnz;
2042: info->nz_used = a->nz;
2043: info->nz_unneeded = (a->maxnz - a->nz);
2044: info->assemblies = A->num_ass;
2045: info->mallocs = A->info.mallocs;
2046: info->memory = 0; /* REVIEW ME */
2047: if (A->factortype) {
2048: info->fill_ratio_given = A->info.fill_ratio_given;
2049: info->fill_ratio_needed = A->info.fill_ratio_needed;
2050: info->factor_mallocs = A->info.factor_mallocs;
2051: } else {
2052: info->fill_ratio_given = 0;
2053: info->fill_ratio_needed = 0;
2054: info->factor_mallocs = 0;
2055: }
2056: PetscFunctionReturn(PETSC_SUCCESS);
2057: }
2059: static PetscErrorCode MatZeroRows_SeqAIJ(Mat A, PetscInt N, const PetscInt rows[], PetscScalar diagv, Vec x, Vec b)
2060: {
2061: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
2062: PetscInt i, m = A->rmap->n - 1;
2063: const PetscScalar *xx;
2064: PetscScalar *bb, *aa;
2065: PetscInt d = 0;
2066: const PetscInt *diag;
2068: PetscFunctionBegin;
2069: if (x && b) {
2070: PetscCall(VecGetArrayRead(x, &xx));
2071: PetscCall(VecGetArray(b, &bb));
2072: for (i = 0; i < N; i++) {
2073: PetscCheck(rows[i] >= 0 && rows[i] <= m, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "row %" PetscInt_FMT " out of range", rows[i]);
2074: if (rows[i] >= A->cmap->n) continue;
2075: bb[rows[i]] = diagv * xx[rows[i]];
2076: }
2077: PetscCall(VecRestoreArrayRead(x, &xx));
2078: PetscCall(VecRestoreArray(b, &bb));
2079: }
2081: PetscCall(MatGetDiagonalMarkers_SeqAIJ(A, &diag, NULL));
2082: PetscCall(MatSeqAIJGetArray(A, &aa));
2083: if (a->keepnonzeropattern) {
2084: for (i = 0; i < N; i++) {
2085: PetscCheck(rows[i] >= 0 && rows[i] <= m, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "row %" PetscInt_FMT " out of range", rows[i]);
2086: PetscCall(PetscArrayzero(&aa[a->i[rows[i]]], a->ilen[rows[i]]));
2087: }
2088: if (diagv != 0.0) {
2089: for (i = 0; i < N; i++) {
2090: d = rows[i];
2091: if (d >= A->cmap->n) continue;
2092: 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);
2093: aa[diag[d]] = diagv;
2094: }
2095: }
2096: } else {
2097: if (diagv != 0.0) {
2098: for (i = 0; i < N; i++) {
2099: PetscCheck(rows[i] >= 0 && rows[i] <= m, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "row %" PetscInt_FMT " out of range", rows[i]);
2100: if (a->ilen[rows[i]] > 0) {
2101: if (rows[i] >= A->cmap->n) {
2102: a->ilen[rows[i]] = 0;
2103: } else {
2104: a->ilen[rows[i]] = 1;
2105: aa[a->i[rows[i]]] = diagv;
2106: a->j[a->i[rows[i]]] = rows[i];
2107: }
2108: } else if (rows[i] < A->cmap->n) { /* in case row was completely empty */
2109: PetscCall(MatSetValues_SeqAIJ(A, 1, &rows[i], 1, &rows[i], &diagv, INSERT_VALUES));
2110: }
2111: }
2112: } else {
2113: for (i = 0; i < N; i++) {
2114: PetscCheck(rows[i] >= 0 && rows[i] <= m, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "row %" PetscInt_FMT " out of range", rows[i]);
2115: a->ilen[rows[i]] = 0;
2116: }
2117: }
2118: A->nonzerostate++;
2119: }
2120: PetscCall(MatSeqAIJRestoreArray(A, &aa));
2121: PetscUseTypeMethod(A, assemblyend, MAT_FINAL_ASSEMBLY);
2122: PetscFunctionReturn(PETSC_SUCCESS);
2123: }
2125: static PetscErrorCode MatZeroRowsColumns_SeqAIJ(Mat A, PetscInt N, const PetscInt rows[], PetscScalar diagv, Vec x, Vec b)
2126: {
2127: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
2128: PetscInt i, j, m = A->rmap->n - 1, d = 0;
2129: PetscBool *zeroed, vecs = PETSC_FALSE;
2130: const PetscScalar *xx;
2131: PetscScalar *bb, *aa;
2132: const PetscInt *diag;
2133: PetscBool diagDense;
2135: PetscFunctionBegin;
2136: if (!N) PetscFunctionReturn(PETSC_SUCCESS);
2137: PetscCall(MatGetDiagonalMarkers_SeqAIJ(A, &diag, &diagDense));
2138: PetscCall(MatSeqAIJGetArray(A, &aa));
2139: if (x && b) {
2140: PetscCall(VecGetArrayRead(x, &xx));
2141: PetscCall(VecGetArray(b, &bb));
2142: vecs = PETSC_TRUE;
2143: }
2144: PetscCall(PetscCalloc1(A->rmap->n, &zeroed));
2145: for (i = 0; i < N; i++) {
2146: PetscCheck(rows[i] >= 0 && rows[i] <= m, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "row %" PetscInt_FMT " out of range", rows[i]);
2147: PetscCall(PetscArrayzero(PetscSafePointerPlusOffset(aa, a->i[rows[i]]), a->ilen[rows[i]]));
2149: zeroed[rows[i]] = PETSC_TRUE;
2150: }
2151: for (i = 0; i < A->rmap->n; i++) {
2152: if (!zeroed[i]) {
2153: for (j = a->i[i]; j < a->i[i + 1]; j++) {
2154: if (a->j[j] < A->rmap->n && zeroed[a->j[j]]) {
2155: if (vecs) bb[i] -= aa[j] * xx[a->j[j]];
2156: aa[j] = 0.0;
2157: }
2158: }
2159: } else if (vecs && i < A->cmap->N) bb[i] = diagv * xx[i];
2160: }
2161: if (x && b) {
2162: PetscCall(VecRestoreArrayRead(x, &xx));
2163: PetscCall(VecRestoreArray(b, &bb));
2164: }
2165: PetscCall(PetscFree(zeroed));
2166: if (diagv != 0.0) {
2167: if (!diagDense) {
2168: for (i = 0; i < N; i++) {
2169: if (rows[i] >= A->cmap->N) continue;
2170: 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]);
2171: PetscCall(MatSetValues_SeqAIJ(A, 1, &rows[i], 1, &rows[i], &diagv, INSERT_VALUES));
2172: }
2173: } else {
2174: for (i = 0; i < N; i++) aa[diag[rows[i]]] = diagv;
2175: }
2176: }
2177: PetscCall(MatSeqAIJRestoreArray(A, &aa));
2178: if (!diagDense) PetscUseTypeMethod(A, assemblyend, MAT_FINAL_ASSEMBLY);
2179: PetscFunctionReturn(PETSC_SUCCESS);
2180: }
2182: PetscErrorCode MatGetRow_SeqAIJ(Mat A, PetscInt row, PetscInt *nz, PetscInt **idx, PetscScalar **v)
2183: {
2184: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
2185: const PetscScalar *aa;
2187: PetscFunctionBegin;
2188: PetscCall(MatSeqAIJGetArrayRead(A, &aa));
2189: *nz = a->i[row + 1] - a->i[row];
2190: if (v) *v = PetscSafePointerPlusOffset((PetscScalar *)aa, a->i[row]);
2191: if (idx) {
2192: if (*nz && a->j) *idx = a->j + a->i[row];
2193: else *idx = NULL;
2194: }
2195: PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
2196: PetscFunctionReturn(PETSC_SUCCESS);
2197: }
2199: PetscErrorCode MatRestoreRow_SeqAIJ(Mat A, PetscInt row, PetscInt *nz, PetscInt **idx, PetscScalar **v)
2200: {
2201: PetscFunctionBegin;
2202: PetscFunctionReturn(PETSC_SUCCESS);
2203: }
2205: static PetscErrorCode MatNorm_SeqAIJ(Mat A, NormType type, PetscReal *nrm)
2206: {
2207: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
2208: const MatScalar *v;
2209: PetscReal sum = 0.0;
2210: PetscInt i, j;
2212: PetscFunctionBegin;
2213: PetscCall(MatSeqAIJGetArrayRead(A, &v));
2214: if (type == NORM_FROBENIUS) {
2215: #if defined(PETSC_USE_REAL___FP16)
2216: PetscBLASInt one = 1, nz = a->nz;
2217: PetscCallBLAS("BLASnrm2", *nrm = BLASnrm2_(&nz, v, &one));
2218: #else
2219: for (i = 0; i < a->nz; i++) {
2220: sum += PetscRealPart(PetscConj(*v) * (*v));
2221: v++;
2222: }
2223: *nrm = PetscSqrtReal(sum);
2224: #endif
2225: PetscCall(PetscLogFlops(2.0 * a->nz));
2226: } else if (type == NORM_1) {
2227: PetscReal *tmp;
2228: PetscInt *jj = a->j;
2229: PetscCall(PetscCalloc1(A->cmap->n, &tmp));
2230: *nrm = 0.0;
2231: for (j = 0; j < a->nz; j++) {
2232: tmp[*jj++] += PetscAbsScalar(*v);
2233: v++;
2234: }
2235: for (j = 0; j < A->cmap->n; j++) {
2236: if (tmp[j] > *nrm) *nrm = tmp[j];
2237: }
2238: PetscCall(PetscFree(tmp));
2239: PetscCall(PetscLogFlops(a->nz));
2240: } else if (type == NORM_INFINITY) {
2241: *nrm = 0.0;
2242: for (j = 0; j < A->rmap->n; j++) {
2243: const PetscScalar *v2 = PetscSafePointerPlusOffset(v, a->i[j]);
2244: sum = 0.0;
2245: for (i = 0; i < a->i[j + 1] - a->i[j]; i++) {
2246: sum += PetscAbsScalar(*v2);
2247: v2++;
2248: }
2249: if (sum > *nrm) *nrm = sum;
2250: }
2251: PetscCall(PetscLogFlops(a->nz));
2252: } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "No support for two norm");
2253: PetscCall(MatSeqAIJRestoreArrayRead(A, &v));
2254: PetscFunctionReturn(PETSC_SUCCESS);
2255: }
2257: static PetscErrorCode MatIsTranspose_SeqAIJ(Mat A, Mat B, PetscReal tol, PetscBool *f)
2258: {
2259: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data, *bij = (Mat_SeqAIJ *)B->data;
2260: PetscInt *adx, *bdx, *aii, *bii, *aptr, *bptr;
2261: const MatScalar *va, *vb;
2262: PetscInt ma, na, mb, nb, i;
2264: PetscFunctionBegin;
2265: PetscCall(MatGetSize(A, &ma, &na));
2266: PetscCall(MatGetSize(B, &mb, &nb));
2267: if (ma != nb || na != mb) {
2268: *f = PETSC_FALSE;
2269: PetscFunctionReturn(PETSC_SUCCESS);
2270: }
2271: PetscCall(MatSeqAIJGetArrayRead(A, &va));
2272: PetscCall(MatSeqAIJGetArrayRead(B, &vb));
2273: aii = aij->i;
2274: bii = bij->i;
2275: adx = aij->j;
2276: bdx = bij->j;
2277: PetscCall(PetscMalloc1(ma, &aptr));
2278: PetscCall(PetscMalloc1(mb, &bptr));
2279: for (i = 0; i < ma; i++) aptr[i] = aii[i];
2280: for (i = 0; i < mb; i++) bptr[i] = bii[i];
2282: *f = PETSC_TRUE;
2283: for (i = 0; i < ma; i++) {
2284: while (aptr[i] < aii[i + 1]) {
2285: PetscInt idc, idr;
2286: PetscScalar vc, vr;
2287: /* column/row index/value */
2288: idc = adx[aptr[i]];
2289: idr = bdx[bptr[idc]];
2290: vc = va[aptr[i]];
2291: vr = vb[bptr[idc]];
2292: if (i != idr || PetscAbsScalar(vc - vr) > tol) {
2293: *f = PETSC_FALSE;
2294: goto done;
2295: } else {
2296: aptr[i]++;
2297: if (B || i != idc) bptr[idc]++;
2298: }
2299: }
2300: }
2301: done:
2302: PetscCall(PetscFree(aptr));
2303: PetscCall(PetscFree(bptr));
2304: PetscCall(MatSeqAIJRestoreArrayRead(A, &va));
2305: PetscCall(MatSeqAIJRestoreArrayRead(B, &vb));
2306: PetscFunctionReturn(PETSC_SUCCESS);
2307: }
2309: static PetscErrorCode MatIsHermitianTranspose_SeqAIJ(Mat A, Mat B, PetscReal tol, PetscBool *f)
2310: {
2311: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data, *bij = (Mat_SeqAIJ *)B->data;
2312: PetscInt *adx, *bdx, *aii, *bii, *aptr, *bptr;
2313: MatScalar *va, *vb;
2314: PetscInt ma, na, mb, nb, i;
2316: PetscFunctionBegin;
2317: PetscCall(MatGetSize(A, &ma, &na));
2318: PetscCall(MatGetSize(B, &mb, &nb));
2319: if (ma != nb || na != mb) {
2320: *f = PETSC_FALSE;
2321: PetscFunctionReturn(PETSC_SUCCESS);
2322: }
2323: aii = aij->i;
2324: bii = bij->i;
2325: adx = aij->j;
2326: bdx = bij->j;
2327: va = aij->a;
2328: vb = bij->a;
2329: PetscCall(PetscMalloc1(ma, &aptr));
2330: PetscCall(PetscMalloc1(mb, &bptr));
2331: for (i = 0; i < ma; i++) aptr[i] = aii[i];
2332: for (i = 0; i < mb; i++) bptr[i] = bii[i];
2334: *f = PETSC_TRUE;
2335: for (i = 0; i < ma; i++) {
2336: while (aptr[i] < aii[i + 1]) {
2337: PetscInt idc, idr;
2338: PetscScalar vc, vr;
2339: /* column/row index/value */
2340: idc = adx[aptr[i]];
2341: idr = bdx[bptr[idc]];
2342: vc = va[aptr[i]];
2343: vr = vb[bptr[idc]];
2344: if (i != idr || PetscAbsScalar(vc - PetscConj(vr)) > tol) {
2345: *f = PETSC_FALSE;
2346: goto done;
2347: } else {
2348: aptr[i]++;
2349: if (B || i != idc) bptr[idc]++;
2350: }
2351: }
2352: }
2353: done:
2354: PetscCall(PetscFree(aptr));
2355: PetscCall(PetscFree(bptr));
2356: PetscFunctionReturn(PETSC_SUCCESS);
2357: }
2359: PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A, Vec ll, Vec rr)
2360: {
2361: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
2362: const PetscScalar *l, *r;
2363: PetscScalar x;
2364: MatScalar *v;
2365: PetscInt i, j, m = A->rmap->n, n = A->cmap->n, M, nz = a->nz;
2366: const PetscInt *jj;
2368: PetscFunctionBegin;
2369: if (ll) {
2370: /* The local size is used so that VecMPI can be passed to this routine
2371: by MatDiagonalScale_MPIAIJ */
2372: PetscCall(VecGetLocalSize(ll, &m));
2373: PetscCheck(m == A->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Left scaling vector wrong length");
2374: PetscCall(VecGetArrayRead(ll, &l));
2375: PetscCall(MatSeqAIJGetArray(A, &v));
2376: for (i = 0; i < m; i++) {
2377: x = l[i];
2378: M = a->i[i + 1] - a->i[i];
2379: for (j = 0; j < M; j++) (*v++) *= x;
2380: }
2381: PetscCall(VecRestoreArrayRead(ll, &l));
2382: PetscCall(PetscLogFlops(nz));
2383: PetscCall(MatSeqAIJRestoreArray(A, &v));
2384: }
2385: if (rr) {
2386: PetscCall(VecGetLocalSize(rr, &n));
2387: PetscCheck(n == A->cmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Right scaling vector wrong length");
2388: PetscCall(VecGetArrayRead(rr, &r));
2389: PetscCall(MatSeqAIJGetArray(A, &v));
2390: jj = a->j;
2391: for (i = 0; i < nz; i++) (*v++) *= r[*jj++];
2392: PetscCall(MatSeqAIJRestoreArray(A, &v));
2393: PetscCall(VecRestoreArrayRead(rr, &r));
2394: PetscCall(PetscLogFlops(nz));
2395: }
2396: PetscFunctionReturn(PETSC_SUCCESS);
2397: }
2399: PetscErrorCode MatCreateSubMatrix_SeqAIJ(Mat A, IS isrow, IS iscol, PetscInt csize, MatReuse scall, Mat *B)
2400: {
2401: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data, *c;
2402: PetscInt *smap, i, k, kstart, kend, oldcols = A->cmap->n, *lens;
2403: PetscInt row, mat_i, *mat_j, tcol, first, step, *mat_ilen, sum, lensi;
2404: const PetscInt *irow, *icol;
2405: const PetscScalar *aa;
2406: PetscInt nrows, ncols;
2407: PetscInt *starts, *j_new, *i_new, *aj = a->j, *ai = a->i, ii, *ailen = a->ilen;
2408: MatScalar *a_new, *mat_a, *c_a;
2409: Mat C;
2410: PetscBool stride;
2412: PetscFunctionBegin;
2413: PetscCall(ISGetIndices(isrow, &irow));
2414: PetscCall(ISGetLocalSize(isrow, &nrows));
2415: PetscCall(ISGetLocalSize(iscol, &ncols));
2417: PetscCall(PetscObjectTypeCompare((PetscObject)iscol, ISSTRIDE, &stride));
2418: if (stride) {
2419: PetscCall(ISStrideGetInfo(iscol, &first, &step));
2420: } else {
2421: first = 0;
2422: step = 0;
2423: }
2424: if (stride && step == 1) {
2425: /* special case of contiguous rows */
2426: PetscCall(PetscMalloc2(nrows, &lens, nrows, &starts));
2427: /* loop over new rows determining lens and starting points */
2428: for (i = 0; i < nrows; i++) {
2429: kstart = ai[irow[i]];
2430: kend = kstart + ailen[irow[i]];
2431: starts[i] = kstart;
2432: for (k = kstart; k < kend; k++) {
2433: if (aj[k] >= first) {
2434: starts[i] = k;
2435: break;
2436: }
2437: }
2438: sum = 0;
2439: while (k < kend) {
2440: if (aj[k++] >= first + ncols) break;
2441: sum++;
2442: }
2443: lens[i] = sum;
2444: }
2445: /* create submatrix */
2446: if (scall == MAT_REUSE_MATRIX) {
2447: PetscInt n_cols, n_rows;
2448: PetscCall(MatGetSize(*B, &n_rows, &n_cols));
2449: PetscCheck(n_rows == nrows && n_cols == ncols, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Reused submatrix wrong size");
2450: PetscCall(MatZeroEntries(*B));
2451: C = *B;
2452: } else {
2453: PetscInt rbs, cbs;
2454: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &C));
2455: PetscCall(MatSetSizes(C, nrows, ncols, PETSC_DETERMINE, PETSC_DETERMINE));
2456: PetscCall(ISGetBlockSize(isrow, &rbs));
2457: PetscCall(ISGetBlockSize(iscol, &cbs));
2458: PetscCall(MatSetBlockSizes(C, rbs, cbs));
2459: PetscCall(MatSetType(C, ((PetscObject)A)->type_name));
2460: PetscCall(MatSeqAIJSetPreallocation_SeqAIJ(C, 0, lens));
2461: }
2462: c = (Mat_SeqAIJ *)C->data;
2464: /* loop over rows inserting into submatrix */
2465: PetscCall(MatSeqAIJGetArrayWrite(C, &a_new)); // Not 'a_new = c->a-new', since that raw usage ignores offload state of C
2466: j_new = c->j;
2467: i_new = c->i;
2468: PetscCall(MatSeqAIJGetArrayRead(A, &aa));
2469: for (i = 0; i < nrows; i++) {
2470: ii = starts[i];
2471: lensi = lens[i];
2472: if (lensi) {
2473: for (k = 0; k < lensi; k++) *j_new++ = aj[ii + k] - first;
2474: PetscCall(PetscArraycpy(a_new, aa + starts[i], lensi));
2475: a_new += lensi;
2476: }
2477: i_new[i + 1] = i_new[i] + lensi;
2478: c->ilen[i] = lensi;
2479: }
2480: PetscCall(MatSeqAIJRestoreArrayWrite(C, &a_new)); // Set C's offload state properly
2481: PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
2482: PetscCall(PetscFree2(lens, starts));
2483: } else {
2484: PetscCall(ISGetIndices(iscol, &icol));
2485: PetscCall(PetscCalloc1(oldcols, &smap));
2486: PetscCall(PetscMalloc1(1 + nrows, &lens));
2487: for (i = 0; i < ncols; i++) {
2488: 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);
2489: smap[icol[i]] = i + 1;
2490: }
2492: /* determine lens of each row */
2493: for (i = 0; i < nrows; i++) {
2494: kstart = ai[irow[i]];
2495: kend = kstart + a->ilen[irow[i]];
2496: lens[i] = 0;
2497: for (k = kstart; k < kend; k++) {
2498: if (smap[aj[k]]) lens[i]++;
2499: }
2500: }
2501: /* Create and fill new matrix */
2502: if (scall == MAT_REUSE_MATRIX) {
2503: PetscBool equal;
2505: c = (Mat_SeqAIJ *)((*B)->data);
2506: PetscCheck((*B)->rmap->n == nrows && (*B)->cmap->n == ncols, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Cannot reuse matrix. wrong size");
2507: PetscCall(PetscArraycmp(c->ilen, lens, (*B)->rmap->n, &equal));
2508: PetscCheck(equal, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Cannot reuse matrix. wrong number of nonzeros");
2509: PetscCall(PetscArrayzero(c->ilen, (*B)->rmap->n));
2510: C = *B;
2511: } else {
2512: PetscInt rbs, cbs;
2513: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &C));
2514: PetscCall(MatSetSizes(C, nrows, ncols, PETSC_DETERMINE, PETSC_DETERMINE));
2515: PetscCall(ISGetBlockSize(isrow, &rbs));
2516: PetscCall(ISGetBlockSize(iscol, &cbs));
2517: if (rbs > 1 || cbs > 1) PetscCall(MatSetBlockSizes(C, rbs, cbs));
2518: PetscCall(MatSetType(C, ((PetscObject)A)->type_name));
2519: PetscCall(MatSeqAIJSetPreallocation_SeqAIJ(C, 0, lens));
2520: }
2521: PetscCall(MatSeqAIJGetArrayRead(A, &aa));
2523: c = (Mat_SeqAIJ *)C->data;
2524: PetscCall(MatSeqAIJGetArrayWrite(C, &c_a)); // Not 'c->a', since that raw usage ignores offload state of C
2525: for (i = 0; i < nrows; i++) {
2526: row = irow[i];
2527: kstart = ai[row];
2528: kend = kstart + a->ilen[row];
2529: mat_i = c->i[i];
2530: mat_j = PetscSafePointerPlusOffset(c->j, mat_i);
2531: mat_a = PetscSafePointerPlusOffset(c_a, mat_i);
2532: mat_ilen = c->ilen + i;
2533: for (k = kstart; k < kend; k++) {
2534: if ((tcol = smap[a->j[k]])) {
2535: *mat_j++ = tcol - 1;
2536: *mat_a++ = aa[k];
2537: (*mat_ilen)++;
2538: }
2539: }
2540: }
2541: PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
2542: /* Free work space */
2543: PetscCall(ISRestoreIndices(iscol, &icol));
2544: PetscCall(PetscFree(smap));
2545: PetscCall(PetscFree(lens));
2546: /* sort */
2547: for (i = 0; i < nrows; i++) {
2548: PetscInt ilen;
2550: mat_i = c->i[i];
2551: mat_j = PetscSafePointerPlusOffset(c->j, mat_i);
2552: mat_a = PetscSafePointerPlusOffset(c_a, mat_i);
2553: ilen = c->ilen[i];
2554: PetscCall(PetscSortIntWithScalarArray(ilen, mat_j, mat_a));
2555: }
2556: PetscCall(MatSeqAIJRestoreArrayWrite(C, &c_a));
2557: }
2558: #if defined(PETSC_HAVE_DEVICE)
2559: PetscCall(MatBindToCPU(C, A->boundtocpu));
2560: #endif
2561: PetscCall(MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY));
2562: PetscCall(MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY));
2564: PetscCall(ISRestoreIndices(isrow, &irow));
2565: *B = C;
2566: PetscFunctionReturn(PETSC_SUCCESS);
2567: }
2569: static PetscErrorCode MatGetMultiProcBlock_SeqAIJ(Mat mat, MPI_Comm subComm, MatReuse scall, Mat *subMat)
2570: {
2571: Mat B;
2573: PetscFunctionBegin;
2574: if (scall == MAT_INITIAL_MATRIX) {
2575: PetscCall(MatCreate(subComm, &B));
2576: PetscCall(MatSetSizes(B, mat->rmap->n, mat->cmap->n, mat->rmap->n, mat->cmap->n));
2577: PetscCall(MatSetBlockSizesFromMats(B, mat, mat));
2578: PetscCall(MatSetType(B, MATSEQAIJ));
2579: PetscCall(MatDuplicateNoCreate_SeqAIJ(B, mat, MAT_COPY_VALUES, PETSC_TRUE));
2580: *subMat = B;
2581: } else {
2582: PetscCall(MatCopy_SeqAIJ(mat, *subMat, SAME_NONZERO_PATTERN));
2583: }
2584: PetscFunctionReturn(PETSC_SUCCESS);
2585: }
2587: static PetscErrorCode MatILUFactor_SeqAIJ(Mat inA, IS row, IS col, const MatFactorInfo *info)
2588: {
2589: Mat_SeqAIJ *a = (Mat_SeqAIJ *)inA->data;
2590: Mat outA;
2591: PetscBool row_identity, col_identity;
2593: PetscFunctionBegin;
2594: PetscCheck(info->levels == 0, PETSC_COMM_SELF, PETSC_ERR_SUP, "Only levels=0 supported for in-place ilu");
2596: PetscCall(ISIdentity(row, &row_identity));
2597: PetscCall(ISIdentity(col, &col_identity));
2599: outA = inA;
2600: PetscCall(PetscFree(inA->solvertype));
2601: PetscCall(PetscStrallocpy(MATSOLVERPETSC, &inA->solvertype));
2603: PetscCall(PetscObjectReference((PetscObject)row));
2604: PetscCall(ISDestroy(&a->row));
2606: a->row = row;
2608: PetscCall(PetscObjectReference((PetscObject)col));
2609: PetscCall(ISDestroy(&a->col));
2611: a->col = col;
2613: /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
2614: PetscCall(ISDestroy(&a->icol));
2615: PetscCall(ISInvertPermutation(col, PETSC_DECIDE, &a->icol));
2617: if (!a->solve_work) { /* this matrix may have been factored before */
2618: PetscCall(PetscMalloc1(inA->rmap->n, &a->solve_work));
2619: }
2621: if (row_identity && col_identity) {
2622: PetscCall(MatLUFactorNumeric_SeqAIJ_inplace(outA, inA, info));
2623: } else {
2624: PetscCall(MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA, inA, info));
2625: }
2626: outA->factortype = MAT_FACTOR_LU;
2627: PetscFunctionReturn(PETSC_SUCCESS);
2628: }
2630: PetscErrorCode MatScale_SeqAIJ(Mat inA, PetscScalar alpha)
2631: {
2632: Mat_SeqAIJ *a = (Mat_SeqAIJ *)inA->data;
2633: PetscScalar *v;
2634: PetscBLASInt one = 1, bnz;
2636: PetscFunctionBegin;
2637: PetscCall(MatSeqAIJGetArray(inA, &v));
2638: PetscCall(PetscBLASIntCast(a->nz, &bnz));
2639: PetscCallBLAS("BLASscal", BLASscal_(&bnz, &alpha, v, &one));
2640: PetscCall(PetscLogFlops(a->nz));
2641: PetscCall(MatSeqAIJRestoreArray(inA, &v));
2642: PetscFunctionReturn(PETSC_SUCCESS);
2643: }
2645: PetscErrorCode MatDestroySubMatrix_Private(Mat_SubSppt *submatj)
2646: {
2647: PetscInt i;
2649: PetscFunctionBegin;
2650: if (!submatj->id) { /* delete data that are linked only to submats[id=0] */
2651: PetscCall(PetscFree4(submatj->sbuf1, submatj->ptr, submatj->tmp, submatj->ctr));
2653: for (i = 0; i < submatj->nrqr; ++i) PetscCall(PetscFree(submatj->sbuf2[i]));
2654: PetscCall(PetscFree3(submatj->sbuf2, submatj->req_size, submatj->req_source1));
2656: if (submatj->rbuf1) {
2657: PetscCall(PetscFree(submatj->rbuf1[0]));
2658: PetscCall(PetscFree(submatj->rbuf1));
2659: }
2661: for (i = 0; i < submatj->nrqs; ++i) PetscCall(PetscFree(submatj->rbuf3[i]));
2662: PetscCall(PetscFree3(submatj->req_source2, submatj->rbuf2, submatj->rbuf3));
2663: PetscCall(PetscFree(submatj->pa));
2664: }
2666: #if defined(PETSC_USE_CTABLE)
2667: PetscCall(PetscHMapIDestroy(&submatj->rmap));
2668: if (submatj->cmap_loc) PetscCall(PetscFree(submatj->cmap_loc));
2669: PetscCall(PetscFree(submatj->rmap_loc));
2670: #else
2671: PetscCall(PetscFree(submatj->rmap));
2672: #endif
2674: if (!submatj->allcolumns) {
2675: #if defined(PETSC_USE_CTABLE)
2676: PetscCall(PetscHMapIDestroy(&submatj->cmap));
2677: #else
2678: PetscCall(PetscFree(submatj->cmap));
2679: #endif
2680: }
2681: PetscCall(PetscFree(submatj->row2proc));
2683: PetscCall(PetscFree(submatj));
2684: PetscFunctionReturn(PETSC_SUCCESS);
2685: }
2687: PetscErrorCode MatDestroySubMatrix_SeqAIJ(Mat C)
2688: {
2689: Mat_SeqAIJ *c = (Mat_SeqAIJ *)C->data;
2690: Mat_SubSppt *submatj = c->submatis1;
2692: PetscFunctionBegin;
2693: PetscCall((*submatj->destroy)(C));
2694: PetscCall(MatDestroySubMatrix_Private(submatj));
2695: PetscFunctionReturn(PETSC_SUCCESS);
2696: }
2698: /* Note this has code duplication with MatDestroySubMatrices_SeqBAIJ() */
2699: static PetscErrorCode MatDestroySubMatrices_SeqAIJ(PetscInt n, Mat *mat[])
2700: {
2701: PetscInt i;
2702: Mat C;
2703: Mat_SeqAIJ *c;
2704: Mat_SubSppt *submatj;
2706: PetscFunctionBegin;
2707: for (i = 0; i < n; i++) {
2708: C = (*mat)[i];
2709: c = (Mat_SeqAIJ *)C->data;
2710: submatj = c->submatis1;
2711: if (submatj) {
2712: if (--((PetscObject)C)->refct <= 0) {
2713: PetscCall(PetscFree(C->factorprefix));
2714: PetscCall((*submatj->destroy)(C));
2715: PetscCall(MatDestroySubMatrix_Private(submatj));
2716: PetscCall(PetscFree(C->defaultvectype));
2717: PetscCall(PetscFree(C->defaultrandtype));
2718: PetscCall(PetscLayoutDestroy(&C->rmap));
2719: PetscCall(PetscLayoutDestroy(&C->cmap));
2720: PetscCall(PetscHeaderDestroy(&C));
2721: }
2722: } else {
2723: PetscCall(MatDestroy(&C));
2724: }
2725: }
2727: /* Destroy Dummy submatrices created for reuse */
2728: PetscCall(MatDestroySubMatrices_Dummy(n, mat));
2730: PetscCall(PetscFree(*mat));
2731: PetscFunctionReturn(PETSC_SUCCESS);
2732: }
2734: static PetscErrorCode MatCreateSubMatrices_SeqAIJ(Mat A, PetscInt n, const IS irow[], const IS icol[], MatReuse scall, Mat *B[])
2735: {
2736: PetscInt i;
2738: PetscFunctionBegin;
2739: if (scall == MAT_INITIAL_MATRIX) PetscCall(PetscCalloc1(n + 1, B));
2741: for (i = 0; i < n; i++) PetscCall(MatCreateSubMatrix_SeqAIJ(A, irow[i], icol[i], PETSC_DECIDE, scall, &(*B)[i]));
2742: PetscFunctionReturn(PETSC_SUCCESS);
2743: }
2745: static PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A, PetscInt is_max, IS is[], PetscInt ov)
2746: {
2747: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
2748: PetscInt row, i, j, k, l, ll, m, n, *nidx, isz, val;
2749: const PetscInt *idx;
2750: PetscInt start, end, *ai, *aj, bs = A->rmap->bs == A->cmap->bs ? A->rmap->bs : 1;
2751: PetscBT table;
2753: PetscFunctionBegin;
2754: m = A->rmap->n / bs;
2755: ai = a->i;
2756: aj = a->j;
2758: PetscCheck(ov >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "illegal negative overlap value used");
2760: PetscCall(PetscMalloc1(m + 1, &nidx));
2761: PetscCall(PetscBTCreate(m, &table));
2763: for (i = 0; i < is_max; i++) {
2764: /* Initialize the two local arrays */
2765: isz = 0;
2766: PetscCall(PetscBTMemzero(m, table));
2768: /* Extract the indices, assume there can be duplicate entries */
2769: PetscCall(ISGetIndices(is[i], &idx));
2770: PetscCall(ISGetLocalSize(is[i], &n));
2772: if (bs > 1) {
2773: /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
2774: for (j = 0; j < n; ++j) {
2775: if (!PetscBTLookupSet(table, idx[j] / bs)) nidx[isz++] = idx[j] / bs;
2776: }
2777: PetscCall(ISRestoreIndices(is[i], &idx));
2778: PetscCall(ISDestroy(&is[i]));
2780: k = 0;
2781: for (j = 0; j < ov; j++) { /* for each overlap */
2782: n = isz;
2783: for (; k < n; k++) { /* do only those rows in nidx[k], which are not done yet */
2784: for (ll = 0; ll < bs; ll++) {
2785: row = bs * nidx[k] + ll;
2786: start = ai[row];
2787: end = ai[row + 1];
2788: for (l = start; l < end; l++) {
2789: val = aj[l] / bs;
2790: if (!PetscBTLookupSet(table, val)) nidx[isz++] = val;
2791: }
2792: }
2793: }
2794: }
2795: PetscCall(ISCreateBlock(PETSC_COMM_SELF, bs, isz, nidx, PETSC_COPY_VALUES, is + i));
2796: } else {
2797: /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
2798: for (j = 0; j < n; ++j) {
2799: if (!PetscBTLookupSet(table, idx[j])) nidx[isz++] = idx[j];
2800: }
2801: PetscCall(ISRestoreIndices(is[i], &idx));
2802: PetscCall(ISDestroy(&is[i]));
2804: k = 0;
2805: for (j = 0; j < ov; j++) { /* for each overlap */
2806: n = isz;
2807: for (; k < n; k++) { /* do only those rows in nidx[k], which are not done yet */
2808: row = nidx[k];
2809: start = ai[row];
2810: end = ai[row + 1];
2811: for (l = start; l < end; l++) {
2812: val = aj[l];
2813: if (!PetscBTLookupSet(table, val)) nidx[isz++] = val;
2814: }
2815: }
2816: }
2817: PetscCall(ISCreateGeneral(PETSC_COMM_SELF, isz, nidx, PETSC_COPY_VALUES, is + i));
2818: }
2819: }
2820: PetscCall(PetscBTDestroy(&table));
2821: PetscCall(PetscFree(nidx));
2822: PetscFunctionReturn(PETSC_SUCCESS);
2823: }
2825: static PetscErrorCode MatPermute_SeqAIJ(Mat A, IS rowp, IS colp, Mat *B)
2826: {
2827: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
2828: PetscInt i, nz = 0, m = A->rmap->n, n = A->cmap->n;
2829: const PetscInt *row, *col;
2830: PetscInt *cnew, j, *lens;
2831: IS icolp, irowp;
2832: PetscInt *cwork = NULL;
2833: PetscScalar *vwork = NULL;
2835: PetscFunctionBegin;
2836: PetscCall(ISInvertPermutation(rowp, PETSC_DECIDE, &irowp));
2837: PetscCall(ISGetIndices(irowp, &row));
2838: PetscCall(ISInvertPermutation(colp, PETSC_DECIDE, &icolp));
2839: PetscCall(ISGetIndices(icolp, &col));
2841: /* determine lengths of permuted rows */
2842: PetscCall(PetscMalloc1(m + 1, &lens));
2843: for (i = 0; i < m; i++) lens[row[i]] = a->i[i + 1] - a->i[i];
2844: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), B));
2845: PetscCall(MatSetSizes(*B, m, n, m, n));
2846: PetscCall(MatSetBlockSizesFromMats(*B, A, A));
2847: PetscCall(MatSetType(*B, ((PetscObject)A)->type_name));
2848: PetscCall(MatSeqAIJSetPreallocation_SeqAIJ(*B, 0, lens));
2849: PetscCall(PetscFree(lens));
2851: PetscCall(PetscMalloc1(n, &cnew));
2852: for (i = 0; i < m; i++) {
2853: PetscCall(MatGetRow_SeqAIJ(A, i, &nz, &cwork, &vwork));
2854: for (j = 0; j < nz; j++) cnew[j] = col[cwork[j]];
2855: PetscCall(MatSetValues_SeqAIJ(*B, 1, &row[i], nz, cnew, vwork, INSERT_VALUES));
2856: PetscCall(MatRestoreRow_SeqAIJ(A, i, &nz, &cwork, &vwork));
2857: }
2858: PetscCall(PetscFree(cnew));
2860: (*B)->assembled = PETSC_FALSE;
2862: #if defined(PETSC_HAVE_DEVICE)
2863: PetscCall(MatBindToCPU(*B, A->boundtocpu));
2864: #endif
2865: PetscCall(MatAssemblyBegin(*B, MAT_FINAL_ASSEMBLY));
2866: PetscCall(MatAssemblyEnd(*B, MAT_FINAL_ASSEMBLY));
2867: PetscCall(ISRestoreIndices(irowp, &row));
2868: PetscCall(ISRestoreIndices(icolp, &col));
2869: PetscCall(ISDestroy(&irowp));
2870: PetscCall(ISDestroy(&icolp));
2871: if (rowp == colp) PetscCall(MatPropagateSymmetryOptions(A, *B));
2872: PetscFunctionReturn(PETSC_SUCCESS);
2873: }
2875: PetscErrorCode MatCopy_SeqAIJ(Mat A, Mat B, MatStructure str)
2876: {
2877: PetscFunctionBegin;
2878: /* If the two matrices have the same copy implementation, use fast copy. */
2879: if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
2880: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
2881: Mat_SeqAIJ *b = (Mat_SeqAIJ *)B->data;
2882: const PetscScalar *aa;
2883: PetscScalar *bb;
2885: PetscCall(MatSeqAIJGetArrayRead(A, &aa));
2886: PetscCall(MatSeqAIJGetArrayWrite(B, &bb));
2888: 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]);
2889: PetscCall(PetscArraycpy(bb, aa, a->i[A->rmap->n]));
2890: PetscCall(PetscObjectStateIncrease((PetscObject)B));
2891: PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
2892: PetscCall(MatSeqAIJRestoreArrayWrite(B, &bb));
2893: } else {
2894: PetscCall(MatCopy_Basic(A, B, str));
2895: }
2896: PetscFunctionReturn(PETSC_SUCCESS);
2897: }
2899: PETSC_INTERN PetscErrorCode MatSeqAIJGetArray_SeqAIJ(Mat A, PetscScalar *array[])
2900: {
2901: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
2903: PetscFunctionBegin;
2904: *array = a->a;
2905: PetscFunctionReturn(PETSC_SUCCESS);
2906: }
2908: PETSC_INTERN PetscErrorCode MatSeqAIJRestoreArray_SeqAIJ(Mat A, PetscScalar *array[])
2909: {
2910: PetscFunctionBegin;
2911: *array = NULL;
2912: PetscFunctionReturn(PETSC_SUCCESS);
2913: }
2915: /*
2916: Computes the number of nonzeros per row needed for preallocation when X and Y
2917: have different nonzero structure.
2918: */
2919: PetscErrorCode MatAXPYGetPreallocation_SeqX_private(PetscInt m, const PetscInt *xi, const PetscInt *xj, const PetscInt *yi, const PetscInt *yj, PetscInt *nnz)
2920: {
2921: PetscInt i, j, k, nzx, nzy;
2923: PetscFunctionBegin;
2924: /* Set the number of nonzeros in the new matrix */
2925: for (i = 0; i < m; i++) {
2926: const PetscInt *xjj = PetscSafePointerPlusOffset(xj, xi[i]), *yjj = PetscSafePointerPlusOffset(yj, yi[i]);
2927: nzx = xi[i + 1] - xi[i];
2928: nzy = yi[i + 1] - yi[i];
2929: nnz[i] = 0;
2930: for (j = 0, k = 0; j < nzx; j++) { /* Point in X */
2931: for (; k < nzy && yjj[k] < xjj[j]; k++) nnz[i]++; /* Catch up to X */
2932: if (k < nzy && yjj[k] == xjj[j]) k++; /* Skip duplicate */
2933: nnz[i]++;
2934: }
2935: for (; k < nzy; k++) nnz[i]++;
2936: }
2937: PetscFunctionReturn(PETSC_SUCCESS);
2938: }
2940: PetscErrorCode MatAXPYGetPreallocation_SeqAIJ(Mat Y, Mat X, PetscInt *nnz)
2941: {
2942: PetscInt m = Y->rmap->N;
2943: Mat_SeqAIJ *x = (Mat_SeqAIJ *)X->data;
2944: Mat_SeqAIJ *y = (Mat_SeqAIJ *)Y->data;
2946: PetscFunctionBegin;
2947: /* Set the number of nonzeros in the new matrix */
2948: PetscCall(MatAXPYGetPreallocation_SeqX_private(m, x->i, x->j, y->i, y->j, nnz));
2949: PetscFunctionReturn(PETSC_SUCCESS);
2950: }
2952: PetscErrorCode MatAXPY_SeqAIJ(Mat Y, PetscScalar a, Mat X, MatStructure str)
2953: {
2954: Mat_SeqAIJ *x = (Mat_SeqAIJ *)X->data, *y = (Mat_SeqAIJ *)Y->data;
2956: PetscFunctionBegin;
2957: if (str == UNKNOWN_NONZERO_PATTERN || (PetscDefined(USE_DEBUG) && str == SAME_NONZERO_PATTERN)) {
2958: PetscBool e = x->nz == y->nz ? PETSC_TRUE : PETSC_FALSE;
2959: if (e) {
2960: PetscCall(PetscArraycmp(x->i, y->i, Y->rmap->n + 1, &e));
2961: if (e) {
2962: PetscCall(PetscArraycmp(x->j, y->j, y->nz, &e));
2963: if (e) str = SAME_NONZERO_PATTERN;
2964: }
2965: }
2966: if (!e) PetscCheck(str != SAME_NONZERO_PATTERN, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "MatStructure is not SAME_NONZERO_PATTERN");
2967: }
2968: if (str == SAME_NONZERO_PATTERN) {
2969: const PetscScalar *xa;
2970: PetscScalar *ya, alpha = a;
2971: PetscBLASInt one = 1, bnz;
2973: PetscCall(PetscBLASIntCast(x->nz, &bnz));
2974: PetscCall(MatSeqAIJGetArray(Y, &ya));
2975: PetscCall(MatSeqAIJGetArrayRead(X, &xa));
2976: PetscCallBLAS("BLASaxpy", BLASaxpy_(&bnz, &alpha, xa, &one, ya, &one));
2977: PetscCall(MatSeqAIJRestoreArrayRead(X, &xa));
2978: PetscCall(MatSeqAIJRestoreArray(Y, &ya));
2979: PetscCall(PetscLogFlops(2.0 * bnz));
2980: PetscCall(PetscObjectStateIncrease((PetscObject)Y));
2981: } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2982: PetscCall(MatAXPY_Basic(Y, a, X, str));
2983: } else {
2984: Mat B;
2985: PetscInt *nnz;
2986: PetscCall(PetscMalloc1(Y->rmap->N, &nnz));
2987: PetscCall(MatCreate(PetscObjectComm((PetscObject)Y), &B));
2988: PetscCall(PetscObjectSetName((PetscObject)B, ((PetscObject)Y)->name));
2989: PetscCall(MatSetLayouts(B, Y->rmap, Y->cmap));
2990: PetscCall(MatSetType(B, ((PetscObject)Y)->type_name));
2991: PetscCall(MatAXPYGetPreallocation_SeqAIJ(Y, X, nnz));
2992: PetscCall(MatSeqAIJSetPreallocation(B, 0, nnz));
2993: PetscCall(MatAXPY_BasicWithPreallocation(B, Y, a, X, str));
2994: PetscCall(MatHeaderMerge(Y, &B));
2995: PetscCall(MatSeqAIJCheckInode(Y));
2996: PetscCall(PetscFree(nnz));
2997: }
2998: PetscFunctionReturn(PETSC_SUCCESS);
2999: }
3001: PETSC_INTERN PetscErrorCode MatConjugate_SeqAIJ(Mat mat)
3002: {
3003: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
3004: PetscInt i, nz = aij->nz;
3005: PetscScalar *a;
3007: PetscFunctionBegin;
3008: PetscCall(MatSeqAIJGetArray(mat, &a));
3009: for (i = 0; i < nz; i++) a[i] = PetscConj(a[i]);
3010: PetscCall(MatSeqAIJRestoreArray(mat, &a));
3011: PetscFunctionReturn(PETSC_SUCCESS);
3012: }
3014: static PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A, Vec v, PetscInt idx[])
3015: {
3016: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
3017: PetscInt i, j, m = A->rmap->n, *ai, *aj, ncols, n;
3018: PetscReal atmp;
3019: PetscScalar *x;
3020: const MatScalar *aa, *av;
3022: PetscFunctionBegin;
3023: PetscCheck(!A->factortype, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
3024: PetscCall(MatSeqAIJGetArrayRead(A, &av));
3025: aa = av;
3026: ai = a->i;
3027: aj = a->j;
3029: PetscCall(VecGetArrayWrite(v, &x));
3030: PetscCall(VecGetLocalSize(v, &n));
3031: PetscCheck(n == A->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nonconforming matrix and vector");
3032: for (i = 0; i < m; i++) {
3033: ncols = ai[1] - ai[0];
3034: ai++;
3035: x[i] = 0;
3036: for (j = 0; j < ncols; j++) {
3037: atmp = PetscAbsScalar(*aa);
3038: if (PetscAbsScalar(x[i]) < atmp) {
3039: x[i] = atmp;
3040: if (idx) idx[i] = *aj;
3041: }
3042: aa++;
3043: aj++;
3044: }
3045: }
3046: PetscCall(VecRestoreArrayWrite(v, &x));
3047: PetscCall(MatSeqAIJRestoreArrayRead(A, &av));
3048: PetscFunctionReturn(PETSC_SUCCESS);
3049: }
3051: static PetscErrorCode MatGetRowSumAbs_SeqAIJ(Mat A, Vec v)
3052: {
3053: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
3054: PetscInt i, j, m = A->rmap->n, *ai, ncols, n;
3055: PetscScalar *x;
3056: const MatScalar *aa, *av;
3058: PetscFunctionBegin;
3059: PetscCheck(!A->factortype, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
3060: PetscCall(MatSeqAIJGetArrayRead(A, &av));
3061: aa = av;
3062: ai = a->i;
3064: PetscCall(VecGetArrayWrite(v, &x));
3065: PetscCall(VecGetLocalSize(v, &n));
3066: PetscCheck(n == A->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nonconforming matrix and vector");
3067: for (i = 0; i < m; i++) {
3068: ncols = ai[1] - ai[0];
3069: ai++;
3070: x[i] = 0;
3071: for (j = 0; j < ncols; j++) {
3072: x[i] += PetscAbsScalar(*aa);
3073: aa++;
3074: }
3075: }
3076: PetscCall(VecRestoreArrayWrite(v, &x));
3077: PetscCall(MatSeqAIJRestoreArrayRead(A, &av));
3078: PetscFunctionReturn(PETSC_SUCCESS);
3079: }
3081: static PetscErrorCode MatGetRowMax_SeqAIJ(Mat A, Vec v, PetscInt idx[])
3082: {
3083: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
3084: PetscInt i, j, m = A->rmap->n, *ai, *aj, ncols, n;
3085: PetscScalar *x;
3086: const MatScalar *aa, *av;
3088: PetscFunctionBegin;
3089: PetscCheck(!A->factortype, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
3090: PetscCall(MatSeqAIJGetArrayRead(A, &av));
3091: aa = av;
3092: ai = a->i;
3093: aj = a->j;
3095: PetscCall(VecGetArrayWrite(v, &x));
3096: PetscCall(VecGetLocalSize(v, &n));
3097: PetscCheck(n == A->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nonconforming matrix and vector");
3098: for (i = 0; i < m; i++) {
3099: ncols = ai[1] - ai[0];
3100: ai++;
3101: if (ncols == A->cmap->n) { /* row is dense */
3102: x[i] = *aa;
3103: if (idx) idx[i] = 0;
3104: } else { /* row is sparse so already KNOW maximum is 0.0 or higher */
3105: x[i] = 0.0;
3106: if (idx) {
3107: for (j = 0; j < ncols; j++) { /* find first implicit 0.0 in the row */
3108: if (aj[j] > j) {
3109: idx[i] = j;
3110: break;
3111: }
3112: }
3113: /* in case first implicit 0.0 in the row occurs at ncols-th column */
3114: if (j == ncols && j < A->cmap->n) idx[i] = j;
3115: }
3116: }
3117: for (j = 0; j < ncols; j++) {
3118: if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {
3119: x[i] = *aa;
3120: if (idx) idx[i] = *aj;
3121: }
3122: aa++;
3123: aj++;
3124: }
3125: }
3126: PetscCall(VecRestoreArrayWrite(v, &x));
3127: PetscCall(MatSeqAIJRestoreArrayRead(A, &av));
3128: PetscFunctionReturn(PETSC_SUCCESS);
3129: }
3131: static PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A, Vec v, PetscInt idx[])
3132: {
3133: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
3134: PetscInt i, j, m = A->rmap->n, *ai, *aj, ncols, n;
3135: PetscScalar *x;
3136: const MatScalar *aa, *av;
3138: PetscFunctionBegin;
3139: PetscCall(MatSeqAIJGetArrayRead(A, &av));
3140: aa = av;
3141: ai = a->i;
3142: aj = a->j;
3144: PetscCall(VecGetArrayWrite(v, &x));
3145: PetscCall(VecGetLocalSize(v, &n));
3146: PetscCheck(n == m, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nonconforming matrix and vector, %" PetscInt_FMT " vs. %" PetscInt_FMT " rows", m, n);
3147: for (i = 0; i < m; i++) {
3148: ncols = ai[1] - ai[0];
3149: ai++;
3150: if (ncols == A->cmap->n) { /* row is dense */
3151: x[i] = *aa;
3152: if (idx) idx[i] = 0;
3153: } else { /* row is sparse so already KNOW minimum is 0.0 or higher */
3154: x[i] = 0.0;
3155: if (idx) { /* find first implicit 0.0 in the row */
3156: for (j = 0; j < ncols; j++) {
3157: if (aj[j] > j) {
3158: idx[i] = j;
3159: break;
3160: }
3161: }
3162: /* in case first implicit 0.0 in the row occurs at ncols-th column */
3163: if (j == ncols && j < A->cmap->n) idx[i] = j;
3164: }
3165: }
3166: for (j = 0; j < ncols; j++) {
3167: if (PetscAbsScalar(x[i]) > PetscAbsScalar(*aa)) {
3168: x[i] = *aa;
3169: if (idx) idx[i] = *aj;
3170: }
3171: aa++;
3172: aj++;
3173: }
3174: }
3175: PetscCall(VecRestoreArrayWrite(v, &x));
3176: PetscCall(MatSeqAIJRestoreArrayRead(A, &av));
3177: PetscFunctionReturn(PETSC_SUCCESS);
3178: }
3180: static PetscErrorCode MatGetRowMin_SeqAIJ(Mat A, Vec v, PetscInt idx[])
3181: {
3182: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
3183: PetscInt i, j, m = A->rmap->n, ncols, n;
3184: const PetscInt *ai, *aj;
3185: PetscScalar *x;
3186: const MatScalar *aa, *av;
3188: PetscFunctionBegin;
3189: PetscCheck(!A->factortype, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
3190: PetscCall(MatSeqAIJGetArrayRead(A, &av));
3191: aa = av;
3192: ai = a->i;
3193: aj = a->j;
3195: PetscCall(VecGetArrayWrite(v, &x));
3196: PetscCall(VecGetLocalSize(v, &n));
3197: PetscCheck(n == m, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nonconforming matrix and vector");
3198: for (i = 0; i < m; i++) {
3199: ncols = ai[1] - ai[0];
3200: ai++;
3201: if (ncols == A->cmap->n) { /* row is dense */
3202: x[i] = *aa;
3203: if (idx) idx[i] = 0;
3204: } else { /* row is sparse so already KNOW minimum is 0.0 or lower */
3205: x[i] = 0.0;
3206: if (idx) { /* find first implicit 0.0 in the row */
3207: for (j = 0; j < ncols; j++) {
3208: if (aj[j] > j) {
3209: idx[i] = j;
3210: break;
3211: }
3212: }
3213: /* in case first implicit 0.0 in the row occurs at ncols-th column */
3214: if (j == ncols && j < A->cmap->n) idx[i] = j;
3215: }
3216: }
3217: for (j = 0; j < ncols; j++) {
3218: if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {
3219: x[i] = *aa;
3220: if (idx) idx[i] = *aj;
3221: }
3222: aa++;
3223: aj++;
3224: }
3225: }
3226: PetscCall(VecRestoreArrayWrite(v, &x));
3227: PetscCall(MatSeqAIJRestoreArrayRead(A, &av));
3228: PetscFunctionReturn(PETSC_SUCCESS);
3229: }
3231: static PetscErrorCode MatInvertBlockDiagonal_SeqAIJ(Mat A, const PetscScalar **values)
3232: {
3233: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
3234: PetscInt i, bs = A->rmap->bs, mbs = A->rmap->n / bs, ipvt[5], bs2 = bs * bs, *v_pivots, ij[7], *IJ, j;
3235: MatScalar *diag, work[25], *v_work;
3236: const PetscReal shift = 0.0;
3237: PetscBool allowzeropivot, zeropivotdetected = PETSC_FALSE;
3239: PetscFunctionBegin;
3240: allowzeropivot = PetscNot(A->erroriffailure);
3241: if (a->ibdiagvalid) {
3242: if (values) *values = a->ibdiag;
3243: PetscFunctionReturn(PETSC_SUCCESS);
3244: }
3245: if (!a->ibdiag) PetscCall(PetscMalloc1(bs2 * mbs, &a->ibdiag));
3246: diag = a->ibdiag;
3247: if (values) *values = a->ibdiag;
3248: /* factor and invert each block */
3249: switch (bs) {
3250: case 1:
3251: for (i = 0; i < mbs; i++) {
3252: PetscCall(MatGetValues(A, 1, &i, 1, &i, diag + i));
3253: if (PetscAbsScalar(diag[i] + shift) < PETSC_MACHINE_EPSILON) {
3254: 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);
3255: A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3256: A->factorerror_zeropivot_value = PetscAbsScalar(diag[i]);
3257: A->factorerror_zeropivot_row = i;
3258: PetscCall(PetscInfo(A, "Zero pivot, row %" PetscInt_FMT " pivot %g tolerance %g\n", i, (double)PetscAbsScalar(diag[i]), (double)PETSC_MACHINE_EPSILON));
3259: }
3260: diag[i] = (PetscScalar)1.0 / (diag[i] + shift);
3261: }
3262: break;
3263: case 2:
3264: for (i = 0; i < mbs; i++) {
3265: ij[0] = 2 * i;
3266: ij[1] = 2 * i + 1;
3267: PetscCall(MatGetValues(A, 2, ij, 2, ij, diag));
3268: PetscCall(PetscKernel_A_gets_inverse_A_2(diag, shift, allowzeropivot, &zeropivotdetected));
3269: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3270: PetscCall(PetscKernel_A_gets_transpose_A_2(diag));
3271: diag += 4;
3272: }
3273: break;
3274: case 3:
3275: for (i = 0; i < mbs; i++) {
3276: ij[0] = 3 * i;
3277: ij[1] = 3 * i + 1;
3278: ij[2] = 3 * i + 2;
3279: PetscCall(MatGetValues(A, 3, ij, 3, ij, diag));
3280: PetscCall(PetscKernel_A_gets_inverse_A_3(diag, shift, allowzeropivot, &zeropivotdetected));
3281: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3282: PetscCall(PetscKernel_A_gets_transpose_A_3(diag));
3283: diag += 9;
3284: }
3285: break;
3286: case 4:
3287: for (i = 0; i < mbs; i++) {
3288: ij[0] = 4 * i;
3289: ij[1] = 4 * i + 1;
3290: ij[2] = 4 * i + 2;
3291: ij[3] = 4 * i + 3;
3292: PetscCall(MatGetValues(A, 4, ij, 4, ij, diag));
3293: PetscCall(PetscKernel_A_gets_inverse_A_4(diag, shift, allowzeropivot, &zeropivotdetected));
3294: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3295: PetscCall(PetscKernel_A_gets_transpose_A_4(diag));
3296: diag += 16;
3297: }
3298: break;
3299: case 5:
3300: for (i = 0; i < mbs; i++) {
3301: ij[0] = 5 * i;
3302: ij[1] = 5 * i + 1;
3303: ij[2] = 5 * i + 2;
3304: ij[3] = 5 * i + 3;
3305: ij[4] = 5 * i + 4;
3306: PetscCall(MatGetValues(A, 5, ij, 5, ij, diag));
3307: PetscCall(PetscKernel_A_gets_inverse_A_5(diag, ipvt, work, shift, allowzeropivot, &zeropivotdetected));
3308: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3309: PetscCall(PetscKernel_A_gets_transpose_A_5(diag));
3310: diag += 25;
3311: }
3312: break;
3313: case 6:
3314: for (i = 0; i < mbs; i++) {
3315: ij[0] = 6 * i;
3316: ij[1] = 6 * i + 1;
3317: ij[2] = 6 * i + 2;
3318: ij[3] = 6 * i + 3;
3319: ij[4] = 6 * i + 4;
3320: ij[5] = 6 * i + 5;
3321: PetscCall(MatGetValues(A, 6, ij, 6, ij, diag));
3322: PetscCall(PetscKernel_A_gets_inverse_A_6(diag, shift, allowzeropivot, &zeropivotdetected));
3323: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3324: PetscCall(PetscKernel_A_gets_transpose_A_6(diag));
3325: diag += 36;
3326: }
3327: break;
3328: case 7:
3329: for (i = 0; i < mbs; i++) {
3330: ij[0] = 7 * i;
3331: ij[1] = 7 * i + 1;
3332: ij[2] = 7 * i + 2;
3333: ij[3] = 7 * i + 3;
3334: ij[4] = 7 * i + 4;
3335: ij[5] = 7 * i + 5;
3336: ij[6] = 7 * i + 6;
3337: PetscCall(MatGetValues(A, 7, ij, 7, ij, diag));
3338: PetscCall(PetscKernel_A_gets_inverse_A_7(diag, shift, allowzeropivot, &zeropivotdetected));
3339: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3340: PetscCall(PetscKernel_A_gets_transpose_A_7(diag));
3341: diag += 49;
3342: }
3343: break;
3344: default:
3345: PetscCall(PetscMalloc3(bs, &v_work, bs, &v_pivots, bs, &IJ));
3346: for (i = 0; i < mbs; i++) {
3347: for (j = 0; j < bs; j++) IJ[j] = bs * i + j;
3348: PetscCall(MatGetValues(A, bs, IJ, bs, IJ, diag));
3349: PetscCall(PetscKernel_A_gets_inverse_A(bs, diag, v_pivots, v_work, allowzeropivot, &zeropivotdetected));
3350: if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3351: PetscCall(PetscKernel_A_gets_transpose_A_N(diag, bs));
3352: diag += bs2;
3353: }
3354: PetscCall(PetscFree3(v_work, v_pivots, IJ));
3355: }
3356: a->ibdiagvalid = PETSC_TRUE;
3357: PetscFunctionReturn(PETSC_SUCCESS);
3358: }
3360: static PetscErrorCode MatSetRandom_SeqAIJ(Mat x, PetscRandom rctx)
3361: {
3362: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)x->data;
3363: PetscScalar a, *aa;
3364: PetscInt m, n, i, j, col;
3366: PetscFunctionBegin;
3367: if (!x->assembled) {
3368: PetscCall(MatGetSize(x, &m, &n));
3369: for (i = 0; i < m; i++) {
3370: for (j = 0; j < aij->imax[i]; j++) {
3371: PetscCall(PetscRandomGetValue(rctx, &a));
3372: col = (PetscInt)(n * PetscRealPart(a));
3373: PetscCall(MatSetValues(x, 1, &i, 1, &col, &a, ADD_VALUES));
3374: }
3375: }
3376: } else {
3377: PetscCall(MatSeqAIJGetArrayWrite(x, &aa));
3378: for (i = 0; i < aij->nz; i++) PetscCall(PetscRandomGetValue(rctx, aa + i));
3379: PetscCall(MatSeqAIJRestoreArrayWrite(x, &aa));
3380: }
3381: PetscCall(MatAssemblyBegin(x, MAT_FINAL_ASSEMBLY));
3382: PetscCall(MatAssemblyEnd(x, MAT_FINAL_ASSEMBLY));
3383: PetscFunctionReturn(PETSC_SUCCESS);
3384: }
3386: /* Like MatSetRandom_SeqAIJ, but do not set values on columns in range of [low, high) */
3387: PetscErrorCode MatSetRandomSkipColumnRange_SeqAIJ_Private(Mat x, PetscInt low, PetscInt high, PetscRandom rctx)
3388: {
3389: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)x->data;
3390: PetscScalar a;
3391: PetscInt m, n, i, j, col, nskip;
3393: PetscFunctionBegin;
3394: nskip = high - low;
3395: PetscCall(MatGetSize(x, &m, &n));
3396: n -= nskip; /* shrink number of columns where nonzeros can be set */
3397: for (i = 0; i < m; i++) {
3398: for (j = 0; j < aij->imax[i]; j++) {
3399: PetscCall(PetscRandomGetValue(rctx, &a));
3400: col = (PetscInt)(n * PetscRealPart(a));
3401: if (col >= low) col += nskip; /* shift col rightward to skip the hole */
3402: PetscCall(MatSetValues(x, 1, &i, 1, &col, &a, ADD_VALUES));
3403: }
3404: }
3405: PetscCall(MatAssemblyBegin(x, MAT_FINAL_ASSEMBLY));
3406: PetscCall(MatAssemblyEnd(x, MAT_FINAL_ASSEMBLY));
3407: PetscFunctionReturn(PETSC_SUCCESS);
3408: }
3410: static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
3411: MatGetRow_SeqAIJ,
3412: MatRestoreRow_SeqAIJ,
3413: MatMult_SeqAIJ,
3414: /* 4*/ MatMultAdd_SeqAIJ,
3415: MatMultTranspose_SeqAIJ,
3416: MatMultTransposeAdd_SeqAIJ,
3417: NULL,
3418: NULL,
3419: NULL,
3420: /* 10*/ NULL,
3421: MatLUFactor_SeqAIJ,
3422: NULL,
3423: MatSOR_SeqAIJ,
3424: MatTranspose_SeqAIJ,
3425: /* 15*/ MatGetInfo_SeqAIJ,
3426: MatEqual_SeqAIJ,
3427: MatGetDiagonal_SeqAIJ,
3428: MatDiagonalScale_SeqAIJ,
3429: MatNorm_SeqAIJ,
3430: /* 20*/ NULL,
3431: MatAssemblyEnd_SeqAIJ,
3432: MatSetOption_SeqAIJ,
3433: MatZeroEntries_SeqAIJ,
3434: /* 24*/ MatZeroRows_SeqAIJ,
3435: NULL,
3436: NULL,
3437: NULL,
3438: NULL,
3439: /* 29*/ MatSetUp_Seq_Hash,
3440: NULL,
3441: NULL,
3442: NULL,
3443: NULL,
3444: /* 34*/ MatDuplicate_SeqAIJ,
3445: NULL,
3446: NULL,
3447: MatILUFactor_SeqAIJ,
3448: NULL,
3449: /* 39*/ MatAXPY_SeqAIJ,
3450: MatCreateSubMatrices_SeqAIJ,
3451: MatIncreaseOverlap_SeqAIJ,
3452: MatGetValues_SeqAIJ,
3453: MatCopy_SeqAIJ,
3454: /* 44*/ MatGetRowMax_SeqAIJ,
3455: MatScale_SeqAIJ,
3456: MatShift_SeqAIJ,
3457: MatDiagonalSet_SeqAIJ,
3458: MatZeroRowsColumns_SeqAIJ,
3459: /* 49*/ MatSetRandom_SeqAIJ,
3460: MatGetRowIJ_SeqAIJ,
3461: MatRestoreRowIJ_SeqAIJ,
3462: MatGetColumnIJ_SeqAIJ,
3463: MatRestoreColumnIJ_SeqAIJ,
3464: /* 54*/ MatFDColoringCreate_SeqXAIJ,
3465: NULL,
3466: NULL,
3467: MatPermute_SeqAIJ,
3468: NULL,
3469: /* 59*/ NULL,
3470: MatDestroy_SeqAIJ,
3471: MatView_SeqAIJ,
3472: NULL,
3473: NULL,
3474: /* 64*/ MatMatMatMultNumeric_SeqAIJ_SeqAIJ_SeqAIJ,
3475: NULL,
3476: NULL,
3477: NULL,
3478: MatGetRowMaxAbs_SeqAIJ,
3479: /* 69*/ MatGetRowMinAbs_SeqAIJ,
3480: NULL,
3481: NULL,
3482: MatFDColoringApply_AIJ,
3483: NULL,
3484: /* 74*/ MatFindZeroDiagonals_SeqAIJ,
3485: NULL,
3486: NULL,
3487: NULL,
3488: MatLoad_SeqAIJ,
3489: /* 79*/ NULL,
3490: NULL,
3491: NULL,
3492: NULL,
3493: NULL,
3494: /* 84*/ NULL,
3495: MatMatMultNumeric_SeqAIJ_SeqAIJ,
3496: MatPtAPNumeric_SeqAIJ_SeqAIJ_SparseAxpy,
3497: NULL,
3498: MatMatTransposeMultNumeric_SeqAIJ_SeqAIJ,
3499: /* 90*/ NULL,
3500: MatProductSetFromOptions_SeqAIJ,
3501: NULL,
3502: NULL,
3503: MatConjugate_SeqAIJ,
3504: /* 94*/ NULL,
3505: MatSetValuesRow_SeqAIJ,
3506: MatRealPart_SeqAIJ,
3507: MatImaginaryPart_SeqAIJ,
3508: NULL,
3509: /* 99*/ NULL,
3510: MatMatSolve_SeqAIJ,
3511: NULL,
3512: MatGetRowMin_SeqAIJ,
3513: NULL,
3514: /*104*/ NULL,
3515: NULL,
3516: NULL,
3517: NULL,
3518: NULL,
3519: /*109*/ NULL,
3520: NULL,
3521: NULL,
3522: NULL,
3523: MatGetMultiProcBlock_SeqAIJ,
3524: /*114*/ MatFindNonzeroRows_SeqAIJ,
3525: MatGetColumnReductions_SeqAIJ,
3526: MatInvertBlockDiagonal_SeqAIJ,
3527: MatInvertVariableBlockDiagonal_SeqAIJ,
3528: NULL,
3529: /*119*/ NULL,
3530: NULL,
3531: MatTransposeMatMultNumeric_SeqAIJ_SeqAIJ,
3532: MatTransposeColoringCreate_SeqAIJ,
3533: MatTransColoringApplySpToDen_SeqAIJ,
3534: /*124*/ MatTransColoringApplyDenToSp_SeqAIJ,
3535: MatRARtNumeric_SeqAIJ_SeqAIJ,
3536: NULL,
3537: NULL,
3538: MatFDColoringSetUp_SeqXAIJ,
3539: /*129*/ MatFindOffBlockDiagonalEntries_SeqAIJ,
3540: MatCreateMPIMatConcatenateSeqMat_SeqAIJ,
3541: MatDestroySubMatrices_SeqAIJ,
3542: NULL,
3543: NULL,
3544: /*134*/ MatCreateGraph_Simple_AIJ,
3545: MatTransposeSymbolic_SeqAIJ,
3546: MatEliminateZeros_SeqAIJ,
3547: MatGetRowSumAbs_SeqAIJ,
3548: NULL,
3549: /*139*/ NULL,
3550: NULL,
3551: MatCopyHashToXAIJ_Seq_Hash,
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: /*MC
4287: MATAIJCRL - MATAIJCRL = "aijcrl" - A matrix type to be used for sparse matrices.
4289: Options Database Key:
4290: . -mat_type aijcrl - sets the matrix type to "aijcrl" during a call to `MatSetFromOptions()`
4292: Level: beginner
4294: Note:
4295: This matrix type is identical to `MATSEQAIJCRL` when constructed with a single process communicator,
4296: and `MATMPIAIJCRL` otherwise. As a result, for single process communicators,
4297: `MatSeqAIJSetPreallocation()` is supported, and similarly `MatMPIAIJSetPreallocation()` is supported
4298: for communicators controlling multiple processes. It is recommended that you call both of
4299: the above preallocation routines for simplicity.
4301: .seealso: [](ch_matrices), `Mat`, `MatCreateMPIAIJCRL`, `MATSEQAIJCRL`, `MATMPIAIJCRL`, `MATSEQAIJCRL`, `MATMPIAIJCRL`
4302: M*/
4304: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCRL(Mat, MatType, MatReuse, Mat *);
4305: #if defined(PETSC_HAVE_ELEMENTAL)
4306: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_Elemental(Mat, MatType, MatReuse, Mat *);
4307: #endif
4308: #if defined(PETSC_HAVE_SCALAPACK) && (defined(PETSC_USE_REAL_SINGLE) || defined(PETSC_USE_REAL_DOUBLE))
4309: PETSC_INTERN PetscErrorCode MatConvert_AIJ_ScaLAPACK(Mat, MatType, MatReuse, Mat *);
4310: #endif
4311: #if defined(PETSC_HAVE_HYPRE)
4312: PETSC_INTERN PetscErrorCode MatConvert_AIJ_HYPRE(Mat A, MatType, MatReuse, Mat *);
4313: #endif
4315: PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_SeqSELL(Mat, MatType, MatReuse, Mat *);
4316: PETSC_INTERN PetscErrorCode MatConvert_XAIJ_IS(Mat, MatType, MatReuse, Mat *);
4317: PETSC_INTERN PetscErrorCode MatProductSetFromOptions_IS_XAIJ(Mat);
4319: /*@C
4320: MatSeqAIJGetArray - gives read/write access to the array where the data for a `MATSEQAIJ` matrix is stored
4322: Not Collective
4324: Input Parameter:
4325: . A - a `MATSEQAIJ` matrix
4327: Output Parameter:
4328: . array - pointer to the data
4330: Level: intermediate
4332: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJRestoreArray()`
4333: @*/
4334: PetscErrorCode MatSeqAIJGetArray(Mat A, PetscScalar *array[])
4335: {
4336: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;
4338: PetscFunctionBegin;
4339: if (aij->ops->getarray) {
4340: PetscCall((*aij->ops->getarray)(A, array));
4341: } else {
4342: *array = aij->a;
4343: }
4344: PetscFunctionReturn(PETSC_SUCCESS);
4345: }
4347: /*@C
4348: MatSeqAIJRestoreArray - returns access to the array where the data for a `MATSEQAIJ` matrix is stored obtained by `MatSeqAIJGetArray()`
4350: Not Collective
4352: Input Parameters:
4353: + A - a `MATSEQAIJ` matrix
4354: - array - pointer to the data
4356: Level: intermediate
4358: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJGetArray()`
4359: @*/
4360: PetscErrorCode MatSeqAIJRestoreArray(Mat A, PetscScalar *array[])
4361: {
4362: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;
4364: PetscFunctionBegin;
4365: if (aij->ops->restorearray) {
4366: PetscCall((*aij->ops->restorearray)(A, array));
4367: } else {
4368: *array = NULL;
4369: }
4370: PetscCall(PetscObjectStateIncrease((PetscObject)A));
4371: PetscFunctionReturn(PETSC_SUCCESS);
4372: }
4374: /*@C
4375: MatSeqAIJGetArrayRead - gives read-only access to the array where the data for a `MATSEQAIJ` matrix is stored
4377: Not Collective; No Fortran Support
4379: Input Parameter:
4380: . A - a `MATSEQAIJ` matrix
4382: Output Parameter:
4383: . array - pointer to the data
4385: Level: intermediate
4387: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJGetArray()`, `MatSeqAIJRestoreArrayRead()`
4388: @*/
4389: PetscErrorCode MatSeqAIJGetArrayRead(Mat A, const PetscScalar *array[])
4390: {
4391: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;
4393: PetscFunctionBegin;
4394: if (aij->ops->getarrayread) {
4395: PetscCall((*aij->ops->getarrayread)(A, array));
4396: } else {
4397: *array = aij->a;
4398: }
4399: PetscFunctionReturn(PETSC_SUCCESS);
4400: }
4402: /*@C
4403: MatSeqAIJRestoreArrayRead - restore the read-only access array obtained from `MatSeqAIJGetArrayRead()`
4405: Not Collective; No Fortran Support
4407: Input Parameter:
4408: . A - a `MATSEQAIJ` matrix
4410: Output Parameter:
4411: . array - pointer to the data
4413: Level: intermediate
4415: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJGetArray()`, `MatSeqAIJGetArrayRead()`
4416: @*/
4417: PetscErrorCode MatSeqAIJRestoreArrayRead(Mat A, const PetscScalar *array[])
4418: {
4419: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;
4421: PetscFunctionBegin;
4422: if (aij->ops->restorearrayread) {
4423: PetscCall((*aij->ops->restorearrayread)(A, array));
4424: } else {
4425: *array = NULL;
4426: }
4427: PetscFunctionReturn(PETSC_SUCCESS);
4428: }
4430: /*@C
4431: MatSeqAIJGetArrayWrite - gives write-only access to the array where the data for a `MATSEQAIJ` matrix is stored
4433: Not Collective; No Fortran Support
4435: Input Parameter:
4436: . A - a `MATSEQAIJ` matrix
4438: Output Parameter:
4439: . array - pointer to the data
4441: Level: intermediate
4443: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJGetArray()`, `MatSeqAIJRestoreArrayRead()`
4444: @*/
4445: PetscErrorCode MatSeqAIJGetArrayWrite(Mat A, PetscScalar *array[])
4446: {
4447: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;
4449: PetscFunctionBegin;
4450: if (aij->ops->getarraywrite) {
4451: PetscCall((*aij->ops->getarraywrite)(A, array));
4452: } else {
4453: *array = aij->a;
4454: }
4455: PetscCall(PetscObjectStateIncrease((PetscObject)A));
4456: PetscFunctionReturn(PETSC_SUCCESS);
4457: }
4459: /*@C
4460: MatSeqAIJRestoreArrayWrite - restore the read-only access array obtained from MatSeqAIJGetArrayRead
4462: Not Collective; No Fortran Support
4464: Input Parameter:
4465: . A - a MATSEQAIJ matrix
4467: Output Parameter:
4468: . array - pointer to the data
4470: Level: intermediate
4472: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJGetArray()`, `MatSeqAIJGetArrayRead()`
4473: @*/
4474: PetscErrorCode MatSeqAIJRestoreArrayWrite(Mat A, PetscScalar *array[])
4475: {
4476: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;
4478: PetscFunctionBegin;
4479: if (aij->ops->restorearraywrite) {
4480: PetscCall((*aij->ops->restorearraywrite)(A, array));
4481: } else {
4482: *array = NULL;
4483: }
4484: PetscFunctionReturn(PETSC_SUCCESS);
4485: }
4487: /*@C
4488: MatSeqAIJGetCSRAndMemType - Get the CSR arrays and the memory type of the `MATSEQAIJ` matrix
4490: Not Collective; No Fortran Support
4492: Input Parameter:
4493: . mat - a matrix of type `MATSEQAIJ` or its subclasses
4495: Output Parameters:
4496: + i - row map array of the matrix
4497: . j - column index array of the matrix
4498: . a - data array of the matrix
4499: - mtype - memory type of the arrays
4501: Level: developer
4503: Notes:
4504: Any of the output parameters can be `NULL`, in which case the corresponding value is not returned.
4505: If mat is a device matrix, the arrays are on the device. Otherwise, they are on the host.
4507: One can call this routine on a preallocated but not assembled matrix to just get the memory of the CSR underneath the matrix.
4508: If the matrix is assembled, the data array `a` is guaranteed to have the latest values of the matrix.
4510: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJGetArray()`, `MatSeqAIJGetArrayRead()`
4511: @*/
4512: PetscErrorCode MatSeqAIJGetCSRAndMemType(Mat mat, const PetscInt *i[], const PetscInt *j[], PetscScalar *a[], PetscMemType *mtype)
4513: {
4514: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
4516: PetscFunctionBegin;
4517: PetscCheck(mat->preallocated, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "matrix is not preallocated");
4518: if (aij->ops->getcsrandmemtype) {
4519: PetscCall((*aij->ops->getcsrandmemtype)(mat, i, j, a, mtype));
4520: } else {
4521: if (i) *i = aij->i;
4522: if (j) *j = aij->j;
4523: if (a) *a = aij->a;
4524: if (mtype) *mtype = PETSC_MEMTYPE_HOST;
4525: }
4526: PetscFunctionReturn(PETSC_SUCCESS);
4527: }
4529: /*@
4530: MatSeqAIJGetMaxRowNonzeros - returns the maximum number of nonzeros in any row
4532: Not Collective
4534: Input Parameter:
4535: . A - a `MATSEQAIJ` matrix
4537: Output Parameter:
4538: . nz - the maximum number of nonzeros in any row
4540: Level: intermediate
4542: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJRestoreArray()`
4543: @*/
4544: PetscErrorCode MatSeqAIJGetMaxRowNonzeros(Mat A, PetscInt *nz)
4545: {
4546: Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;
4548: PetscFunctionBegin;
4549: *nz = aij->rmax;
4550: PetscFunctionReturn(PETSC_SUCCESS);
4551: }
4553: static PetscErrorCode MatCOOStructDestroy_SeqAIJ(PetscCtxRt data)
4554: {
4555: MatCOOStruct_SeqAIJ *coo = *(MatCOOStruct_SeqAIJ **)data;
4557: PetscFunctionBegin;
4558: PetscCall(PetscFree(coo->perm));
4559: PetscCall(PetscFree(coo->jmap));
4560: PetscCall(PetscFree(coo));
4561: PetscFunctionReturn(PETSC_SUCCESS);
4562: }
4564: PetscErrorCode MatSetPreallocationCOO_SeqAIJ(Mat mat, PetscCount coo_n, PetscInt coo_i[], PetscInt coo_j[])
4565: {
4566: MPI_Comm comm;
4567: PetscInt *i, *j;
4568: PetscInt M, N, row, iprev;
4569: PetscCount k, p, q, nneg, nnz, start, end; /* Index the coo array, so use PetscCount as their type */
4570: PetscInt *Ai; /* Change to PetscCount once we use it for row pointers */
4571: PetscInt *Aj;
4572: PetscScalar *Aa;
4573: Mat_SeqAIJ *seqaij = (Mat_SeqAIJ *)mat->data;
4574: MatType rtype;
4575: PetscCount *perm, *jmap;
4576: MatCOOStruct_SeqAIJ *coo;
4577: PetscBool isorted;
4578: PetscBool hypre;
4580: PetscFunctionBegin;
4581: PetscCall(PetscObjectGetComm((PetscObject)mat, &comm));
4582: PetscCall(MatGetSize(mat, &M, &N));
4583: i = coo_i;
4584: j = coo_j;
4585: PetscCall(PetscMalloc1(coo_n, &perm));
4587: /* 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) */
4588: isorted = PETSC_TRUE;
4589: iprev = PETSC_INT_MIN;
4590: for (k = 0; k < coo_n; k++) {
4591: if (j[k] < 0) i[k] = -1;
4592: if (isorted) {
4593: if (i[k] < iprev) isorted = PETSC_FALSE;
4594: else iprev = i[k];
4595: }
4596: perm[k] = k;
4597: }
4599: /* Sort by row if not already */
4600: if (!isorted) PetscCall(PetscSortIntWithIntCountArrayPair(coo_n, i, j, perm));
4601: 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);
4603: /* Advance k to the first row with a non-negative index */
4604: for (k = 0; k < coo_n; k++)
4605: if (i[k] >= 0) break;
4606: nneg = k;
4607: 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 */
4608: nnz = 0; /* Total number of unique nonzeros to be counted */
4609: jmap++; /* Inc jmap by 1 for convenience */
4611: PetscCall(PetscShmgetAllocateArray(M + 1, sizeof(PetscInt), (void **)&Ai)); /* CSR of A */
4612: PetscCall(PetscArrayzero(Ai, M + 1));
4613: PetscCall(PetscShmgetAllocateArray(coo_n - nneg, sizeof(PetscInt), (void **)&Aj)); /* We have at most coo_n-nneg unique nonzeros */
4615: PetscCall(PetscStrcmp("_internal_COO_mat_for_hypre", ((PetscObject)mat)->name, &hypre));
4617: /* In each row, sort by column, then unique column indices to get row length */
4618: Ai++; /* Inc by 1 for convenience */
4619: q = 0; /* q-th unique nonzero, with q starting from 0 */
4620: while (k < coo_n) {
4621: PetscBool strictly_sorted; // this row is strictly sorted?
4622: PetscInt jprev;
4624: /* get [start,end) indices for this row; also check if cols in this row are strictly sorted */
4625: row = i[k];
4626: start = k;
4627: jprev = PETSC_INT_MIN;
4628: strictly_sorted = PETSC_TRUE;
4629: while (k < coo_n && i[k] == row) {
4630: if (strictly_sorted) {
4631: if (j[k] <= jprev) strictly_sorted = PETSC_FALSE;
4632: else jprev = j[k];
4633: }
4634: k++;
4635: }
4636: end = k;
4638: /* hack for HYPRE: swap min column to diag so that diagonal values will go first */
4639: if (hypre) {
4640: PetscInt minj = PETSC_INT_MAX;
4641: PetscBool hasdiag = PETSC_FALSE;
4643: if (strictly_sorted) { // fast path to swap the first and the diag
4644: PetscCount tmp;
4645: for (p = start; p < end; p++) {
4646: if (j[p] == row && p != start) {
4647: j[p] = j[start]; // swap j[], so that the diagonal value will go first (manipulated by perm[])
4648: j[start] = row;
4649: tmp = perm[start];
4650: perm[start] = perm[p]; // also swap perm[] so we can save the call to PetscSortIntWithCountArray() below
4651: perm[p] = tmp;
4652: break;
4653: }
4654: }
4655: } else {
4656: for (p = start; p < end; p++) {
4657: hasdiag = (PetscBool)(hasdiag || (j[p] == row));
4658: minj = PetscMin(minj, j[p]);
4659: }
4661: if (hasdiag) {
4662: for (p = start; p < end; p++) {
4663: if (j[p] == minj) j[p] = row;
4664: else if (j[p] == row) j[p] = minj;
4665: }
4666: }
4667: }
4668: }
4669: // sort by columns in a row. perm[] indicates their original order
4670: if (!strictly_sorted) PetscCall(PetscSortIntWithCountArray(end - start, j + start, perm + start));
4671: 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);
4673: if (strictly_sorted) { // fast path to set Aj[], jmap[], Ai[], nnz, q
4674: for (p = start; p < end; p++, q++) {
4675: Aj[q] = j[p];
4676: jmap[q] = 1;
4677: }
4678: PetscCall(PetscIntCast(end - start, Ai + row));
4679: nnz += Ai[row]; // q is already advanced
4680: } else {
4681: /* Find number of unique col entries in this row */
4682: Aj[q] = j[start]; /* Log the first nonzero in this row */
4683: jmap[q] = 1; /* Number of repeats of this nonzero entry */
4684: Ai[row] = 1;
4685: nnz++;
4687: for (p = start + 1; p < end; p++) { /* Scan remaining nonzero in this row */
4688: if (j[p] != j[p - 1]) { /* Meet a new nonzero */
4689: q++;
4690: jmap[q] = 1;
4691: Aj[q] = j[p];
4692: Ai[row]++;
4693: nnz++;
4694: } else {
4695: jmap[q]++;
4696: }
4697: }
4698: q++; /* Move to next row and thus next unique nonzero */
4699: }
4700: }
4702: Ai--; /* Back to the beginning of Ai[] */
4703: for (k = 0; k < M; k++) Ai[k + 1] += Ai[k];
4704: jmap--; // Back to the beginning of jmap[]
4705: jmap[0] = 0;
4706: for (k = 0; k < nnz; k++) jmap[k + 1] += jmap[k];
4708: if (nnz < coo_n - nneg) { /* Reallocate with actual number of unique nonzeros */
4709: PetscCount *jmap_new;
4710: PetscInt *Aj_new;
4712: PetscCall(PetscMalloc1(nnz + 1, &jmap_new));
4713: PetscCall(PetscArraycpy(jmap_new, jmap, nnz + 1));
4714: PetscCall(PetscFree(jmap));
4715: jmap = jmap_new;
4717: PetscCall(PetscShmgetAllocateArray(nnz, sizeof(PetscInt), (void **)&Aj_new));
4718: PetscCall(PetscArraycpy(Aj_new, Aj, nnz));
4719: PetscCall(PetscShmgetDeallocateArray((void **)&Aj));
4720: Aj = Aj_new;
4721: }
4723: if (nneg) { /* Discard heading entries with negative indices in perm[], as we'll access it from index 0 in MatSetValuesCOO */
4724: PetscCount *perm_new;
4726: PetscCall(PetscMalloc1(coo_n - nneg, &perm_new));
4727: PetscCall(PetscArraycpy(perm_new, perm + nneg, coo_n - nneg));
4728: PetscCall(PetscFree(perm));
4729: perm = perm_new;
4730: }
4732: PetscCall(MatGetRootType_Private(mat, &rtype));
4733: PetscCall(PetscShmgetAllocateArray(nnz, sizeof(PetscScalar), (void **)&Aa));
4734: PetscCall(PetscArrayzero(Aa, nnz));
4735: PetscCall(MatSetSeqAIJWithArrays_private(PETSC_COMM_SELF, M, N, Ai, Aj, Aa, rtype, mat));
4737: seqaij->free_a = seqaij->free_ij = PETSC_TRUE; /* Let newmat own Ai, Aj and Aa */
4739: // Put the COO struct in a container and then attach that to the matrix
4740: PetscCall(PetscMalloc1(1, &coo));
4741: PetscCall(PetscIntCast(nnz, &coo->nz));
4742: coo->n = coo_n;
4743: coo->Atot = coo_n - nneg; // Annz is seqaij->nz, so no need to record that again
4744: coo->jmap = jmap; // of length nnz+1
4745: coo->perm = perm;
4746: PetscCall(PetscObjectContainerCompose((PetscObject)mat, "__PETSc_MatCOOStruct_Host", coo, MatCOOStructDestroy_SeqAIJ));
4747: PetscFunctionReturn(PETSC_SUCCESS);
4748: }
4750: static PetscErrorCode MatSetValuesCOO_SeqAIJ(Mat A, const PetscScalar v[], InsertMode imode)
4751: {
4752: Mat_SeqAIJ *aseq = (Mat_SeqAIJ *)A->data;
4753: PetscCount i, j, Annz = aseq->nz;
4754: PetscCount *perm, *jmap;
4755: PetscScalar *Aa;
4756: PetscContainer container;
4757: MatCOOStruct_SeqAIJ *coo;
4759: PetscFunctionBegin;
4760: PetscCall(PetscObjectQuery((PetscObject)A, "__PETSc_MatCOOStruct_Host", (PetscObject *)&container));
4761: PetscCheck(container, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Not found MatCOOStruct on this matrix");
4762: PetscCall(PetscContainerGetPointer(container, &coo));
4763: perm = coo->perm;
4764: jmap = coo->jmap;
4765: PetscCall(MatSeqAIJGetArray(A, &Aa));
4766: for (i = 0; i < Annz; i++) {
4767: PetscScalar sum = 0.0;
4768: for (j = jmap[i]; j < jmap[i + 1]; j++) sum += v[perm[j]];
4769: Aa[i] = (imode == INSERT_VALUES ? 0.0 : Aa[i]) + sum;
4770: }
4771: PetscCall(MatSeqAIJRestoreArray(A, &Aa));
4772: PetscFunctionReturn(PETSC_SUCCESS);
4773: }
4775: #if defined(PETSC_HAVE_CUDA)
4776: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCUSPARSE(Mat, MatType, MatReuse, Mat *);
4777: #endif
4778: #if defined(PETSC_HAVE_HIP)
4779: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJHIPSPARSE(Mat, MatType, MatReuse, Mat *);
4780: #endif
4781: #if defined(PETSC_HAVE_KOKKOS_KERNELS)
4782: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJKokkos(Mat, MatType, MatReuse, Mat *);
4783: #endif
4785: PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJ(Mat B)
4786: {
4787: Mat_SeqAIJ *b;
4788: PetscMPIInt size;
4790: PetscFunctionBegin;
4791: PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)B), &size));
4792: PetscCheck(size <= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Comm must be of size 1");
4794: PetscCall(PetscNew(&b));
4796: B->data = (void *)b;
4797: B->ops[0] = MatOps_Values;
4798: if (B->sortedfull) B->ops->setvalues = MatSetValues_SeqAIJ_SortedFull;
4800: b->row = NULL;
4801: b->col = NULL;
4802: b->icol = NULL;
4803: b->reallocs = 0;
4804: b->ignorezeroentries = PETSC_FALSE;
4805: b->roworiented = PETSC_TRUE;
4806: b->nonew = 0;
4807: b->diag = NULL;
4808: b->solve_work = NULL;
4809: B->spptr = NULL;
4810: b->saved_values = NULL;
4811: b->idiag = NULL;
4812: b->mdiag = NULL;
4813: b->ssor_work = NULL;
4814: b->omega = 1.0;
4815: b->fshift = 0.0;
4816: b->ibdiagvalid = PETSC_FALSE;
4817: b->keepnonzeropattern = PETSC_FALSE;
4819: PetscCall(PetscObjectChangeTypeName((PetscObject)B, MATSEQAIJ));
4820: #if defined(PETSC_HAVE_MATLAB)
4821: PetscCall(PetscObjectComposeFunction((PetscObject)B, "PetscMatlabEnginePut_C", MatlabEnginePut_SeqAIJ));
4822: PetscCall(PetscObjectComposeFunction((PetscObject)B, "PetscMatlabEngineGet_C", MatlabEngineGet_SeqAIJ));
4823: #endif
4824: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSeqAIJSetColumnIndices_C", MatSeqAIJSetColumnIndices_SeqAIJ));
4825: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatStoreValues_C", MatStoreValues_SeqAIJ));
4826: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatRetrieveValues_C", MatRetrieveValues_SeqAIJ));
4827: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqsbaij_C", MatConvert_SeqAIJ_SeqSBAIJ));
4828: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqbaij_C", MatConvert_SeqAIJ_SeqBAIJ));
4829: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqaijperm_C", MatConvert_SeqAIJ_SeqAIJPERM));
4830: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqaijsell_C", MatConvert_SeqAIJ_SeqAIJSELL));
4831: #if defined(PETSC_HAVE_MKL_SPARSE)
4832: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqaijmkl_C", MatConvert_SeqAIJ_SeqAIJMKL));
4833: #endif
4834: #if defined(PETSC_HAVE_CUDA)
4835: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqaijcusparse_C", MatConvert_SeqAIJ_SeqAIJCUSPARSE));
4836: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqaijcusparse_seqaij_C", MatProductSetFromOptions_SeqAIJ));
4837: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqaij_seqaijcusparse_C", MatProductSetFromOptions_SeqAIJ));
4838: #endif
4839: #if defined(PETSC_HAVE_HIP)
4840: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqaijhipsparse_C", MatConvert_SeqAIJ_SeqAIJHIPSPARSE));
4841: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqaijhipsparse_seqaij_C", MatProductSetFromOptions_SeqAIJ));
4842: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqaij_seqaijhipsparse_C", MatProductSetFromOptions_SeqAIJ));
4843: #endif
4844: #if defined(PETSC_HAVE_KOKKOS_KERNELS)
4845: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqaijkokkos_C", MatConvert_SeqAIJ_SeqAIJKokkos));
4846: #endif
4847: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqaijcrl_C", MatConvert_SeqAIJ_SeqAIJCRL));
4848: #if defined(PETSC_HAVE_ELEMENTAL)
4849: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_elemental_C", MatConvert_SeqAIJ_Elemental));
4850: #endif
4851: #if defined(PETSC_HAVE_SCALAPACK) && (defined(PETSC_USE_REAL_SINGLE) || defined(PETSC_USE_REAL_DOUBLE))
4852: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_scalapack_C", MatConvert_AIJ_ScaLAPACK));
4853: #endif
4854: #if defined(PETSC_HAVE_HYPRE)
4855: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_hypre_C", MatConvert_AIJ_HYPRE));
4856: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_transpose_seqaij_seqaij_C", MatProductSetFromOptions_Transpose_AIJ_AIJ));
4857: #endif
4858: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqdense_C", MatConvert_SeqAIJ_SeqDense));
4859: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqsell_C", MatConvert_SeqAIJ_SeqSELL));
4860: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_is_C", MatConvert_XAIJ_IS));
4861: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatIsTranspose_C", MatIsTranspose_SeqAIJ));
4862: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatIsHermitianTranspose_C", MatIsHermitianTranspose_SeqAIJ));
4863: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSeqAIJSetPreallocation_C", MatSeqAIJSetPreallocation_SeqAIJ));
4864: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatResetPreallocation_C", MatResetPreallocation_SeqAIJ));
4865: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatResetHash_C", MatResetHash_SeqAIJ));
4866: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSeqAIJSetPreallocationCSR_C", MatSeqAIJSetPreallocationCSR_SeqAIJ));
4867: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatReorderForNonzeroDiagonal_C", MatReorderForNonzeroDiagonal_SeqAIJ));
4868: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_is_seqaij_C", MatProductSetFromOptions_IS_XAIJ));
4869: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqdense_seqaij_C", MatProductSetFromOptions_SeqDense_SeqAIJ));
4870: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqaij_seqaij_C", MatProductSetFromOptions_SeqAIJ));
4871: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSeqAIJKron_C", MatSeqAIJKron_SeqAIJ));
4872: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSetPreallocationCOO_C", MatSetPreallocationCOO_SeqAIJ));
4873: PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSetValuesCOO_C", MatSetValuesCOO_SeqAIJ));
4874: PetscCall(MatCreate_SeqAIJ_Inode(B));
4875: PetscCall(PetscObjectChangeTypeName((PetscObject)B, MATSEQAIJ));
4876: PetscCall(MatSeqAIJSetTypeFromOptions(B)); /* this allows changing the matrix subtype to say MATSEQAIJPERM */
4877: PetscFunctionReturn(PETSC_SUCCESS);
4878: }
4880: /*
4881: Given a matrix generated with MatGetFactor() duplicates all the information in A into C
4882: */
4883: PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C, Mat A, MatDuplicateOption cpvalues, PetscBool mallocmatspace)
4884: {
4885: Mat_SeqAIJ *c = (Mat_SeqAIJ *)C->data, *a = (Mat_SeqAIJ *)A->data;
4886: PetscInt m = A->rmap->n, i;
4888: PetscFunctionBegin;
4889: PetscCheck(A->assembled || cpvalues == MAT_DO_NOT_COPY_VALUES, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Cannot duplicate unassembled matrix");
4891: C->factortype = A->factortype;
4892: c->row = NULL;
4893: c->col = NULL;
4894: c->icol = NULL;
4895: c->reallocs = 0;
4896: C->assembled = A->assembled;
4898: if (A->preallocated) {
4899: PetscCall(PetscLayoutReference(A->rmap, &C->rmap));
4900: PetscCall(PetscLayoutReference(A->cmap, &C->cmap));
4902: if (!A->hash_active) {
4903: PetscCall(PetscMalloc1(m, &c->imax));
4904: PetscCall(PetscArraycpy(c->imax, a->imax, m));
4905: PetscCall(PetscMalloc1(m, &c->ilen));
4906: PetscCall(PetscArraycpy(c->ilen, a->ilen, m));
4908: /* allocate the matrix space */
4909: if (mallocmatspace) {
4910: PetscCall(PetscShmgetAllocateArray(a->i[m], sizeof(PetscScalar), (void **)&c->a));
4911: PetscCall(PetscShmgetAllocateArray(a->i[m], sizeof(PetscInt), (void **)&c->j));
4912: PetscCall(PetscShmgetAllocateArray(m + 1, sizeof(PetscInt), (void **)&c->i));
4913: PetscCall(PetscArraycpy(c->i, a->i, m + 1));
4914: c->free_a = PETSC_TRUE;
4915: c->free_ij = PETSC_TRUE;
4916: if (m > 0) {
4917: PetscCall(PetscArraycpy(c->j, a->j, a->i[m]));
4918: if (cpvalues == MAT_COPY_VALUES) {
4919: const PetscScalar *aa;
4921: PetscCall(MatSeqAIJGetArrayRead(A, &aa));
4922: PetscCall(PetscArraycpy(c->a, aa, a->i[m]));
4923: PetscCall(MatSeqAIJGetArrayRead(A, &aa));
4924: } else {
4925: PetscCall(PetscArrayzero(c->a, a->i[m]));
4926: }
4927: }
4928: }
4929: C->preallocated = PETSC_TRUE;
4930: } else {
4931: PetscCheck(mallocmatspace, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONGSTATE, "Cannot malloc matrix memory from a non-preallocated matrix");
4932: PetscCall(MatSetUp(C));
4933: }
4935: c->ignorezeroentries = a->ignorezeroentries;
4936: c->roworiented = a->roworiented;
4937: c->nonew = a->nonew;
4938: c->solve_work = NULL;
4939: c->saved_values = NULL;
4940: c->idiag = NULL;
4941: c->ssor_work = NULL;
4942: c->keepnonzeropattern = a->keepnonzeropattern;
4944: c->rmax = a->rmax;
4945: c->nz = a->nz;
4946: c->maxnz = a->nz; /* Since we allocate exactly the right amount */
4948: c->compressedrow.use = a->compressedrow.use;
4949: c->compressedrow.nrows = a->compressedrow.nrows;
4950: if (a->compressedrow.use) {
4951: i = a->compressedrow.nrows;
4952: PetscCall(PetscMalloc2(i + 1, &c->compressedrow.i, i, &c->compressedrow.rindex));
4953: PetscCall(PetscArraycpy(c->compressedrow.i, a->compressedrow.i, i + 1));
4954: PetscCall(PetscArraycpy(c->compressedrow.rindex, a->compressedrow.rindex, i));
4955: } else {
4956: c->compressedrow.use = PETSC_FALSE;
4957: c->compressedrow.i = NULL;
4958: c->compressedrow.rindex = NULL;
4959: }
4960: c->nonzerorowcnt = a->nonzerorowcnt;
4961: C->nonzerostate = A->nonzerostate;
4963: PetscCall(MatDuplicate_SeqAIJ_Inode(A, cpvalues, &C));
4964: }
4965: PetscCall(PetscFunctionListDuplicate(((PetscObject)A)->qlist, &((PetscObject)C)->qlist));
4966: PetscFunctionReturn(PETSC_SUCCESS);
4967: }
4969: PetscErrorCode MatDuplicate_SeqAIJ(Mat A, MatDuplicateOption cpvalues, Mat *B)
4970: {
4971: PetscFunctionBegin;
4972: PetscCall(MatCreate(PetscObjectComm((PetscObject)A), B));
4973: PetscCall(MatSetSizes(*B, A->rmap->n, A->cmap->n, A->rmap->n, A->cmap->n));
4974: if (!(A->rmap->n % A->rmap->bs) && !(A->cmap->n % A->cmap->bs)) PetscCall(MatSetBlockSizesFromMats(*B, A, A));
4975: PetscCall(MatSetType(*B, ((PetscObject)A)->type_name));
4976: PetscCall(MatDuplicateNoCreate_SeqAIJ(*B, A, cpvalues, PETSC_TRUE));
4977: PetscFunctionReturn(PETSC_SUCCESS);
4978: }
4980: PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer)
4981: {
4982: PetscBool isbinary, ishdf5;
4984: PetscFunctionBegin;
4987: /* force binary viewer to load .info file if it has not yet done so */
4988: PetscCall(PetscViewerSetUp(viewer));
4989: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
4990: PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
4991: if (isbinary) {
4992: PetscCall(MatLoad_SeqAIJ_Binary(newMat, viewer));
4993: } else if (ishdf5) {
4994: #if defined(PETSC_HAVE_HDF5)
4995: PetscCall(MatLoad_AIJ_HDF5(newMat, viewer));
4996: #else
4997: SETERRQ(PetscObjectComm((PetscObject)newMat), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
4998: #endif
4999: } else {
5000: 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);
5001: }
5002: PetscFunctionReturn(PETSC_SUCCESS);
5003: }
5005: PetscErrorCode MatLoad_SeqAIJ_Binary(Mat mat, PetscViewer viewer)
5006: {
5007: Mat_SeqAIJ *a = (Mat_SeqAIJ *)mat->data;
5008: PetscInt header[4], *rowlens, M, N, nz, sum, rows, cols, i;
5010: PetscFunctionBegin;
5011: PetscCall(PetscViewerSetUp(viewer));
5013: /* read in matrix header */
5014: PetscCall(PetscViewerBinaryRead(viewer, header, 4, NULL, PETSC_INT));
5015: PetscCheck(header[0] == MAT_FILE_CLASSID, PETSC_COMM_SELF, PETSC_ERR_FILE_UNEXPECTED, "Not a matrix object in file");
5016: M = header[1];
5017: N = header[2];
5018: nz = header[3];
5019: PetscCheck(M >= 0, PetscObjectComm((PetscObject)viewer), PETSC_ERR_FILE_UNEXPECTED, "Matrix row size (%" PetscInt_FMT ") in file is negative", M);
5020: PetscCheck(N >= 0, PetscObjectComm((PetscObject)viewer), PETSC_ERR_FILE_UNEXPECTED, "Matrix column size (%" PetscInt_FMT ") in file is negative", N);
5021: PetscCheck(nz >= 0, PETSC_COMM_SELF, PETSC_ERR_FILE_UNEXPECTED, "Matrix stored in special format on disk, cannot load as SeqAIJ");
5023: /* set block sizes from the viewer's .info file */
5024: PetscCall(MatLoad_Binary_BlockSizes(mat, viewer));
5025: /* set local and global sizes if not set already */
5026: if (mat->rmap->n < 0) mat->rmap->n = M;
5027: if (mat->cmap->n < 0) mat->cmap->n = N;
5028: if (mat->rmap->N < 0) mat->rmap->N = M;
5029: if (mat->cmap->N < 0) mat->cmap->N = N;
5030: PetscCall(PetscLayoutSetUp(mat->rmap));
5031: PetscCall(PetscLayoutSetUp(mat->cmap));
5033: /* check if the matrix sizes are correct */
5034: PetscCall(MatGetSize(mat, &rows, &cols));
5035: 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);
5037: /* read in row lengths */
5038: PetscCall(PetscMalloc1(M, &rowlens));
5039: PetscCall(PetscViewerBinaryRead(viewer, rowlens, M, NULL, PETSC_INT));
5040: /* check if sum(rowlens) is same as nz */
5041: sum = 0;
5042: for (i = 0; i < M; i++) sum += rowlens[i];
5043: 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);
5044: /* preallocate and check sizes */
5045: PetscCall(MatSeqAIJSetPreallocation_SeqAIJ(mat, 0, rowlens));
5046: PetscCall(MatGetSize(mat, &rows, &cols));
5047: 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);
5048: /* store row lengths */
5049: PetscCall(PetscArraycpy(a->ilen, rowlens, M));
5050: PetscCall(PetscFree(rowlens));
5052: /* fill in "i" row pointers */
5053: a->i[0] = 0;
5054: for (i = 0; i < M; i++) a->i[i + 1] = a->i[i] + a->ilen[i];
5055: /* read in "j" column indices */
5056: PetscCall(PetscViewerBinaryRead(viewer, a->j, nz, NULL, PETSC_INT));
5057: /* read in "a" nonzero values */
5058: PetscCall(PetscViewerBinaryRead(viewer, a->a, nz, NULL, PETSC_SCALAR));
5060: PetscCall(MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY));
5061: PetscCall(MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY));
5062: PetscFunctionReturn(PETSC_SUCCESS);
5063: }
5065: PetscErrorCode MatEqual_SeqAIJ(Mat A, Mat B, PetscBool *flg)
5066: {
5067: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data, *b = (Mat_SeqAIJ *)B->data;
5068: const PetscScalar *aa, *ba;
5070: PetscFunctionBegin;
5071: /* If the matrix dimensions are not equal,or no of nonzeros */
5072: if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) || (a->nz != b->nz)) {
5073: *flg = PETSC_FALSE;
5074: PetscFunctionReturn(PETSC_SUCCESS);
5075: }
5077: /* if the a->i are the same */
5078: PetscCall(PetscArraycmp(a->i, b->i, A->rmap->n + 1, flg));
5079: if (!*flg) PetscFunctionReturn(PETSC_SUCCESS);
5081: /* if a->j are the same */
5082: PetscCall(PetscArraycmp(a->j, b->j, a->nz, flg));
5083: if (!*flg) PetscFunctionReturn(PETSC_SUCCESS);
5085: PetscCall(MatSeqAIJGetArrayRead(A, &aa));
5086: PetscCall(MatSeqAIJGetArrayRead(B, &ba));
5087: /* if a->a are the same */
5088: PetscCall(PetscArraycmp(aa, ba, a->nz, flg));
5089: PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
5090: PetscCall(MatSeqAIJRestoreArrayRead(B, &ba));
5091: PetscFunctionReturn(PETSC_SUCCESS);
5092: }
5094: /*@
5095: MatCreateSeqAIJWithArrays - Creates an sequential `MATSEQAIJ` matrix using matrix elements (in CSR format)
5096: provided by the user.
5098: Collective
5100: Input Parameters:
5101: + comm - must be an MPI communicator of size 1
5102: . m - number of rows
5103: . n - number of columns
5104: . i - row indices; that is i[0] = 0, i[row] = i[row-1] + number of elements in that row of the matrix
5105: . j - column indices
5106: - a - matrix values
5108: Output Parameter:
5109: . mat - the matrix
5111: Level: intermediate
5113: Notes:
5114: The `i`, `j`, and `a` arrays are not copied by this routine, the user must free these arrays
5115: once the matrix is destroyed and not before
5117: You cannot set new nonzero locations into this matrix, that will generate an error.
5119: The `i` and `j` indices are 0 based
5121: The format which is used for the sparse matrix input, is equivalent to a
5122: row-major ordering.. i.e for the following matrix, the input data expected is
5123: as shown
5124: .vb
5125: 1 0 0
5126: 2 0 3
5127: 4 5 6
5129: i = {0,1,3,6} [size = nrow+1 = 3+1]
5130: j = {0,0,2,0,1,2} [size = 6]; values must be sorted for each row
5131: v = {1,2,3,4,5,6} [size = 6]
5132: .ve
5134: .seealso: [](ch_matrices), `Mat`, `MatCreate()`, `MatCreateAIJ()`, `MatCreateSeqAIJ()`, `MatCreateMPIAIJWithArrays()`, `MatMPIAIJSetPreallocationCSR()`
5135: @*/
5136: PetscErrorCode MatCreateSeqAIJWithArrays(MPI_Comm comm, PetscInt m, PetscInt n, PetscInt i[], PetscInt j[], PetscScalar a[], Mat *mat)
5137: {
5138: PetscInt ii;
5139: Mat_SeqAIJ *aij;
5140: PetscInt jj;
5142: PetscFunctionBegin;
5143: PetscCheck(m <= 0 || i[0] == 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "i (row indices) must start with 0");
5144: PetscCall(MatCreate(comm, mat));
5145: PetscCall(MatSetSizes(*mat, m, n, m, n));
5146: /* PetscCall(MatSetBlockSizes(*mat,,)); */
5147: PetscCall(MatSetType(*mat, MATSEQAIJ));
5148: PetscCall(MatSeqAIJSetPreallocation_SeqAIJ(*mat, MAT_SKIP_ALLOCATION, NULL));
5149: aij = (Mat_SeqAIJ *)(*mat)->data;
5150: PetscCall(PetscMalloc1(m, &aij->imax));
5151: PetscCall(PetscMalloc1(m, &aij->ilen));
5153: aij->i = i;
5154: aij->j = j;
5155: aij->a = a;
5156: aij->nonew = -1; /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
5157: aij->free_a = PETSC_FALSE;
5158: aij->free_ij = PETSC_FALSE;
5160: for (ii = 0, aij->nonzerorowcnt = 0, aij->rmax = 0; ii < m; ii++) {
5161: aij->ilen[ii] = aij->imax[ii] = i[ii + 1] - i[ii];
5162: if (PetscDefined(USE_DEBUG)) {
5163: 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]);
5164: for (jj = i[ii] + 1; jj < i[ii + 1]; jj++) {
5165: 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);
5166: 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);
5167: }
5168: }
5169: }
5170: if (PetscDefined(USE_DEBUG)) {
5171: for (ii = 0; ii < aij->i[m]; ii++) {
5172: PetscCheck(j[ii] >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Negative column index at location = %" PetscInt_FMT " index = %" PetscInt_FMT, ii, j[ii]);
5173: 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);
5174: }
5175: }
5177: PetscCall(MatAssemblyBegin(*mat, MAT_FINAL_ASSEMBLY));
5178: PetscCall(MatAssemblyEnd(*mat, MAT_FINAL_ASSEMBLY));
5179: PetscFunctionReturn(PETSC_SUCCESS);
5180: }
5182: /*@
5183: MatCreateSeqAIJFromTriple - Creates an sequential `MATSEQAIJ` matrix using matrix elements (in COO format)
5184: provided by the user.
5186: Collective
5188: Input Parameters:
5189: + comm - must be an MPI communicator of size 1
5190: . m - number of rows
5191: . n - number of columns
5192: . i - row indices
5193: . j - column indices
5194: . a - matrix values
5195: . nz - number of nonzeros
5196: - idx - if the `i` and `j` indices start with 1 use `PETSC_TRUE` otherwise use `PETSC_FALSE`
5198: Output Parameter:
5199: . mat - the matrix
5201: Level: intermediate
5203: Example:
5204: For the following matrix, the input data expected is as shown (using 0 based indexing)
5205: .vb
5206: 1 0 0
5207: 2 0 3
5208: 4 5 6
5210: i = {0,1,1,2,2,2}
5211: j = {0,0,2,0,1,2}
5212: v = {1,2,3,4,5,6}
5213: .ve
5215: Note:
5216: Instead of using this function, users should also consider `MatSetPreallocationCOO()` and `MatSetValuesCOO()`, which allow repeated or remote entries,
5217: and are particularly useful in iterative applications.
5219: .seealso: [](ch_matrices), `Mat`, `MatCreate()`, `MatCreateAIJ()`, `MatCreateSeqAIJ()`, `MatCreateSeqAIJWithArrays()`, `MatMPIAIJSetPreallocationCSR()`, `MatSetValuesCOO()`, `MatSetPreallocationCOO()`
5220: @*/
5221: PetscErrorCode MatCreateSeqAIJFromTriple(MPI_Comm comm, PetscInt m, PetscInt n, PetscInt i[], PetscInt j[], PetscScalar a[], Mat *mat, PetscCount nz, PetscBool idx)
5222: {
5223: PetscInt ii, *nnz, one = 1, row, col;
5225: PetscFunctionBegin;
5226: PetscCall(PetscCalloc1(m, &nnz));
5227: for (ii = 0; ii < nz; ii++) nnz[i[ii] - !!idx] += 1;
5228: PetscCall(MatCreate(comm, mat));
5229: PetscCall(MatSetSizes(*mat, m, n, m, n));
5230: PetscCall(MatSetType(*mat, MATSEQAIJ));
5231: PetscCall(MatSeqAIJSetPreallocation_SeqAIJ(*mat, 0, nnz));
5232: for (ii = 0; ii < nz; ii++) {
5233: if (idx) {
5234: row = i[ii] - 1;
5235: col = j[ii] - 1;
5236: } else {
5237: row = i[ii];
5238: col = j[ii];
5239: }
5240: PetscCall(MatSetValues(*mat, one, &row, one, &col, &a[ii], ADD_VALUES));
5241: }
5242: PetscCall(MatAssemblyBegin(*mat, MAT_FINAL_ASSEMBLY));
5243: PetscCall(MatAssemblyEnd(*mat, MAT_FINAL_ASSEMBLY));
5244: PetscCall(PetscFree(nnz));
5245: PetscFunctionReturn(PETSC_SUCCESS);
5246: }
5248: PetscErrorCode MatCreateMPIMatConcatenateSeqMat_SeqAIJ(MPI_Comm comm, Mat inmat, PetscInt n, MatReuse scall, Mat *outmat)
5249: {
5250: PetscFunctionBegin;
5251: PetscCall(MatCreateMPIMatConcatenateSeqMat_MPIAIJ(comm, inmat, n, scall, outmat));
5252: PetscFunctionReturn(PETSC_SUCCESS);
5253: }
5255: /*
5256: Permute A into C's *local* index space using rowemb,colemb.
5257: The embedding are supposed to be injections and the above implies that the range of rowemb is a subset
5258: of [0,m), colemb is in [0,n).
5259: If pattern == DIFFERENT_NONZERO_PATTERN, C is preallocated according to A.
5260: */
5261: PetscErrorCode MatSetSeqMat_SeqAIJ(Mat C, IS rowemb, IS colemb, MatStructure pattern, Mat B)
5262: {
5263: /* If making this function public, change the error returned in this function away from _PLIB. */
5264: Mat_SeqAIJ *Baij;
5265: PetscBool seqaij;
5266: PetscInt m, n, *nz, i, j, count;
5267: PetscScalar v;
5268: const PetscInt *rowindices, *colindices;
5270: PetscFunctionBegin;
5271: if (!B) PetscFunctionReturn(PETSC_SUCCESS);
5272: /* Check to make sure the target matrix (and embeddings) are compatible with C and each other. */
5273: PetscCall(PetscObjectBaseTypeCompare((PetscObject)B, MATSEQAIJ, &seqaij));
5274: PetscCheck(seqaij, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Input matrix is of wrong type");
5275: if (rowemb) {
5276: PetscCall(ISGetLocalSize(rowemb, &m));
5277: 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);
5278: } else {
5279: PetscCheck(C->rmap->n == B->rmap->n, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Input matrix is row-incompatible with the target matrix");
5280: }
5281: if (colemb) {
5282: PetscCall(ISGetLocalSize(colemb, &n));
5283: 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);
5284: } else {
5285: PetscCheck(C->cmap->n == B->cmap->n, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Input matrix is col-incompatible with the target matrix");
5286: }
5288: Baij = (Mat_SeqAIJ *)B->data;
5289: if (pattern == DIFFERENT_NONZERO_PATTERN) {
5290: PetscCall(PetscMalloc1(B->rmap->n, &nz));
5291: for (i = 0; i < B->rmap->n; i++) nz[i] = Baij->i[i + 1] - Baij->i[i];
5292: PetscCall(MatSeqAIJSetPreallocation(C, 0, nz));
5293: PetscCall(PetscFree(nz));
5294: }
5295: if (pattern == SUBSET_NONZERO_PATTERN) PetscCall(MatZeroEntries(C));
5296: count = 0;
5297: rowindices = NULL;
5298: colindices = NULL;
5299: if (rowemb) PetscCall(ISGetIndices(rowemb, &rowindices));
5300: if (colemb) PetscCall(ISGetIndices(colemb, &colindices));
5301: for (i = 0; i < B->rmap->n; i++) {
5302: PetscInt row;
5303: row = i;
5304: if (rowindices) row = rowindices[i];
5305: for (j = Baij->i[i]; j < Baij->i[i + 1]; j++) {
5306: PetscInt col;
5307: col = Baij->j[count];
5308: if (colindices) col = colindices[col];
5309: v = Baij->a[count];
5310: PetscCall(MatSetValues(C, 1, &row, 1, &col, &v, INSERT_VALUES));
5311: ++count;
5312: }
5313: }
5314: /* FIXME: set C's nonzerostate correctly. */
5315: /* Assembly for C is necessary. */
5316: C->preallocated = PETSC_TRUE;
5317: C->assembled = PETSC_TRUE;
5318: C->was_assembled = PETSC_FALSE;
5319: PetscFunctionReturn(PETSC_SUCCESS);
5320: }
5322: PetscErrorCode MatEliminateZeros_SeqAIJ(Mat A, PetscBool keep)
5323: {
5324: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
5325: MatScalar *aa = a->a;
5326: PetscInt m = A->rmap->n, fshift = 0, fshift_prev = 0, i, k;
5327: PetscInt *ailen = a->ilen, *imax = a->imax, *ai = a->i, *aj = a->j, rmax = 0;
5329: PetscFunctionBegin;
5330: PetscCheck(A->assembled, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Cannot eliminate zeros for unassembled matrix");
5331: if (m) rmax = ailen[0]; /* determine row with most nonzeros */
5332: for (i = 1; i <= m; i++) {
5333: /* move each nonzero entry back by the amount of zero slots (fshift) before it*/
5334: for (k = ai[i - 1]; k < ai[i]; k++) {
5335: if (aa[k] == 0 && (aj[k] != i - 1 || !keep)) fshift++;
5336: else {
5337: if (aa[k] == 0 && aj[k] == i - 1) PetscCall(PetscInfo(A, "Keep the diagonal zero at row %" PetscInt_FMT "\n", i - 1));
5338: aa[k - fshift] = aa[k];
5339: aj[k - fshift] = aj[k];
5340: }
5341: }
5342: ai[i - 1] -= fshift_prev; // safe to update ai[i-1] now since it will not be used in the next iteration
5343: fshift_prev = fshift;
5344: /* reset ilen and imax for each row */
5345: ailen[i - 1] = imax[i - 1] = ai[i] - fshift - ai[i - 1];
5346: a->nonzerorowcnt += ((ai[i] - fshift - ai[i - 1]) > 0);
5347: rmax = PetscMax(rmax, ailen[i - 1]);
5348: }
5349: if (fshift) {
5350: if (m) {
5351: ai[m] -= fshift;
5352: a->nz = ai[m];
5353: }
5354: 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));
5355: A->nonzerostate++;
5356: A->info.nz_unneeded += (PetscReal)fshift;
5357: a->rmax = rmax;
5358: if (a->inode.use && a->inode.checked) PetscCall(MatSeqAIJCheckInode(A));
5359: PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
5360: PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
5361: }
5362: PetscFunctionReturn(PETSC_SUCCESS);
5363: }
5365: PetscFunctionList MatSeqAIJList = NULL;
5367: /*@
5368: MatSeqAIJSetType - Converts a `MATSEQAIJ` matrix to a subtype
5370: Collective
5372: Input Parameters:
5373: + mat - the matrix object
5374: - matype - matrix type
5376: Options Database Key:
5377: . -mat_seqaij_type <method> - for example seqaijcrl
5379: Level: intermediate
5381: .seealso: [](ch_matrices), `Mat`, `PCSetType()`, `VecSetType()`, `MatCreate()`, `MatType`
5382: @*/
5383: PetscErrorCode MatSeqAIJSetType(Mat mat, MatType matype)
5384: {
5385: PetscBool sametype;
5386: PetscErrorCode (*r)(Mat, MatType, MatReuse, Mat *);
5388: PetscFunctionBegin;
5390: PetscCall(PetscObjectTypeCompare((PetscObject)mat, matype, &sametype));
5391: if (sametype) PetscFunctionReturn(PETSC_SUCCESS);
5393: PetscCall(PetscFunctionListFind(MatSeqAIJList, matype, &r));
5394: PetscCheck(r, PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown Mat type given: %s", matype);
5395: PetscCall((*r)(mat, matype, MAT_INPLACE_MATRIX, &mat));
5396: PetscFunctionReturn(PETSC_SUCCESS);
5397: }
5399: /*@C
5400: MatSeqAIJRegister - - Adds a new sub-matrix type for sequential `MATSEQAIJ` matrices
5402: Not Collective, No Fortran Support
5404: Input Parameters:
5405: + sname - name of a new user-defined matrix type, for example `MATSEQAIJCRL`
5406: - function - routine to convert to subtype
5408: Level: advanced
5410: Notes:
5411: `MatSeqAIJRegister()` may be called multiple times to add several user-defined solvers.
5413: Then, your matrix can be chosen with the procedural interface at runtime via the option
5414: .vb
5415: -mat_seqaij_type my_mat
5416: .ve
5418: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJRegisterAll()`
5419: @*/
5420: PetscErrorCode MatSeqAIJRegister(const char sname[], PetscErrorCode (*function)(Mat, MatType, MatReuse, Mat *))
5421: {
5422: PetscFunctionBegin;
5423: PetscCall(MatInitializePackage());
5424: PetscCall(PetscFunctionListAdd(&MatSeqAIJList, sname, function));
5425: PetscFunctionReturn(PETSC_SUCCESS);
5426: }
5428: PetscBool MatSeqAIJRegisterAllCalled = PETSC_FALSE;
5430: /*@C
5431: MatSeqAIJRegisterAll - Registers all of the matrix subtypes of `MATSSEQAIJ`
5433: Not Collective
5435: Level: advanced
5437: Note:
5438: This registers the versions of `MATSEQAIJ` for GPUs
5440: .seealso: [](ch_matrices), `Mat`, `MatRegisterAll()`, `MatSeqAIJRegister()`
5441: @*/
5442: PetscErrorCode MatSeqAIJRegisterAll(void)
5443: {
5444: PetscFunctionBegin;
5445: if (MatSeqAIJRegisterAllCalled) PetscFunctionReturn(PETSC_SUCCESS);
5446: MatSeqAIJRegisterAllCalled = PETSC_TRUE;
5448: PetscCall(MatSeqAIJRegister(MATSEQAIJCRL, MatConvert_SeqAIJ_SeqAIJCRL));
5449: PetscCall(MatSeqAIJRegister(MATSEQAIJPERM, MatConvert_SeqAIJ_SeqAIJPERM));
5450: PetscCall(MatSeqAIJRegister(MATSEQAIJSELL, MatConvert_SeqAIJ_SeqAIJSELL));
5451: #if defined(PETSC_HAVE_MKL_SPARSE)
5452: PetscCall(MatSeqAIJRegister(MATSEQAIJMKL, MatConvert_SeqAIJ_SeqAIJMKL));
5453: #endif
5454: #if defined(PETSC_HAVE_CUDA)
5455: PetscCall(MatSeqAIJRegister(MATSEQAIJCUSPARSE, MatConvert_SeqAIJ_SeqAIJCUSPARSE));
5456: #endif
5457: #if defined(PETSC_HAVE_HIP)
5458: PetscCall(MatSeqAIJRegister(MATSEQAIJHIPSPARSE, MatConvert_SeqAIJ_SeqAIJHIPSPARSE));
5459: #endif
5460: #if defined(PETSC_HAVE_KOKKOS_KERNELS)
5461: PetscCall(MatSeqAIJRegister(MATSEQAIJKOKKOS, MatConvert_SeqAIJ_SeqAIJKokkos));
5462: #endif
5463: #if defined(PETSC_HAVE_VIENNACL) && defined(PETSC_HAVE_VIENNACL_NO_CUDA)
5464: PetscCall(MatSeqAIJRegister(MATMPIAIJVIENNACL, MatConvert_SeqAIJ_SeqAIJViennaCL));
5465: #endif
5466: PetscFunctionReturn(PETSC_SUCCESS);
5467: }
5469: /*
5470: Special version for direct calls from Fortran
5471: */
5472: #if defined(PETSC_HAVE_FORTRAN_CAPS)
5473: #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
5474: #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
5475: #define matsetvaluesseqaij_ matsetvaluesseqaij
5476: #endif
5478: /* Change these macros so can be used in void function */
5480: /* Change these macros so can be used in void function */
5481: /* Identical to PetscCallVoid, except it assigns to *_ierr */
5482: #undef PetscCall
5483: #define PetscCall(...) \
5484: do { \
5485: PetscErrorCode ierr_msv_mpiaij = __VA_ARGS__; \
5486: if (PetscUnlikely(ierr_msv_mpiaij)) { \
5487: *_ierr = PetscError(PETSC_COMM_SELF, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr_msv_mpiaij, PETSC_ERROR_REPEAT, " "); \
5488: return; \
5489: } \
5490: } while (0)
5492: #undef SETERRQ
5493: #define SETERRQ(comm, ierr, ...) \
5494: do { \
5495: *_ierr = PetscError(comm, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr, PETSC_ERROR_INITIAL, __VA_ARGS__); \
5496: return; \
5497: } while (0)
5499: PETSC_EXTERN void matsetvaluesseqaij_(Mat *AA, PetscInt *mm, const PetscInt im[], PetscInt *nn, const PetscInt in[], const PetscScalar v[], InsertMode *isis, PetscErrorCode *_ierr)
5500: {
5501: Mat A = *AA;
5502: PetscInt m = *mm, n = *nn;
5503: InsertMode is = *isis;
5504: Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
5505: PetscInt *rp, k, low, high, t, ii, row, nrow, i, col, l, rmax, N;
5506: PetscInt *imax, *ai, *ailen;
5507: PetscInt *aj, nonew = a->nonew, lastcol = -1;
5508: MatScalar *ap, value, *aa;
5509: PetscBool ignorezeroentries = a->ignorezeroentries;
5510: PetscBool roworiented = a->roworiented;
5512: PetscFunctionBegin;
5513: MatCheckPreallocated(A, 1);
5514: imax = a->imax;
5515: ai = a->i;
5516: ailen = a->ilen;
5517: aj = a->j;
5518: aa = a->a;
5520: for (k = 0; k < m; k++) { /* loop over added rows */
5521: row = im[k];
5522: if (row < 0) continue;
5523: PetscCheck(row < A->rmap->n, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_OUTOFRANGE, "Row too large");
5524: rp = aj + ai[row];
5525: ap = aa + ai[row];
5526: rmax = imax[row];
5527: nrow = ailen[row];
5528: low = 0;
5529: high = nrow;
5530: for (l = 0; l < n; l++) { /* loop over added columns */
5531: if (in[l] < 0) continue;
5532: PetscCheck(in[l] < A->cmap->n, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_OUTOFRANGE, "Column too large");
5533: col = in[l];
5534: if (roworiented) value = v[l + k * n];
5535: else value = v[k + l * m];
5537: if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;
5539: if (col <= lastcol) low = 0;
5540: else high = nrow;
5541: lastcol = col;
5542: while (high - low > 5) {
5543: t = (low + high) / 2;
5544: if (rp[t] > col) high = t;
5545: else low = t;
5546: }
5547: for (i = low; i < high; i++) {
5548: if (rp[i] > col) break;
5549: if (rp[i] == col) {
5550: if (is == ADD_VALUES) ap[i] += value;
5551: else ap[i] = value;
5552: goto noinsert;
5553: }
5554: }
5555: if (value == 0.0 && ignorezeroentries) goto noinsert;
5556: if (nonew == 1) goto noinsert;
5557: PetscCheck(nonew != -1, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_OUTOFRANGE, "Inserting a new nonzero in the matrix");
5558: MatSeqXAIJReallocateAIJ(A, A->rmap->n, 1, nrow, row, col, rmax, aa, ai, aj, rp, ap, imax, nonew, MatScalar);
5559: N = nrow++ - 1;
5560: a->nz++;
5561: high++;
5562: /* shift up all the later entries in this row */
5563: for (ii = N; ii >= i; ii--) {
5564: rp[ii + 1] = rp[ii];
5565: ap[ii + 1] = ap[ii];
5566: }
5567: rp[i] = col;
5568: ap[i] = value;
5569: noinsert:;
5570: low = i + 1;
5571: }
5572: ailen[row] = nrow;
5573: }
5574: PetscFunctionReturnVoid();
5575: }
5576: /* Undefining these here since they were redefined from their original definition above! No
5577: * other PETSc functions should be defined past this point, as it is impossible to recover the
5578: * original definitions */
5579: #undef PetscCall
5580: #undef SETERRQ