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, sizeof(type), &flg));
 29:   if (flg) PetscCall(MatSeqAIJSetType(A, type));
 30:   PetscOptionsEnd();
 31:   PetscFunctionReturn(PETSC_SUCCESS);
 32: }

 34: static PetscErrorCode MatGetColumnReductions_SeqAIJ(Mat A, PetscInt type, PetscReal *reductions)
 35: {
 36:   PetscInt    i, m, n;
 37:   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;

 39:   PetscFunctionBegin;
 40:   PetscCall(MatGetSize(A, &m, &n));
 41:   PetscCall(PetscArrayzero(reductions, n));
 42:   if (type == NORM_2) {
 43:     for (i = 0; i < aij->i[m]; i++) reductions[aij->j[i]] += PetscAbsScalar(aij->a[i] * aij->a[i]);
 44:   } else if (type == NORM_1) {
 45:     for (i = 0; i < aij->i[m]; i++) reductions[aij->j[i]] += PetscAbsScalar(aij->a[i]);
 46:   } else if (type == NORM_INFINITY) {
 47:     for (i = 0; i < aij->i[m]; i++) reductions[aij->j[i]] = PetscMax(PetscAbsScalar(aij->a[i]), reductions[aij->j[i]]);
 48:   } else if (type == REDUCTION_SUM_REALPART || type == REDUCTION_MEAN_REALPART) {
 49:     for (i = 0; i < aij->i[m]; i++) reductions[aij->j[i]] += PetscRealPart(aij->a[i]);
 50:   } else if (type == REDUCTION_SUM_IMAGINARYPART || type == REDUCTION_MEAN_IMAGINARYPART) {
 51:     for (i = 0; i < aij->i[m]; i++) reductions[aij->j[i]] += PetscImaginaryPart(aij->a[i]);
 52:   } else SETERRQ(PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Unknown reduction type");

 54:   if (type == NORM_2) {
 55:     for (i = 0; i < n; i++) reductions[i] = PetscSqrtReal(reductions[i]);
 56:   } else if (type == REDUCTION_MEAN_REALPART || type == REDUCTION_MEAN_IMAGINARYPART) {
 57:     for (i = 0; i < n; i++) reductions[i] /= m;
 58:   }
 59:   PetscFunctionReturn(PETSC_SUCCESS);
 60: }

 62: static PetscErrorCode MatFindOffBlockDiagonalEntries_SeqAIJ(Mat A, IS *is)
 63: {
 64:   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data;
 65:   PetscInt        i, m = A->rmap->n, cnt = 0, bs = A->rmap->bs;
 66:   const PetscInt *jj = a->j, *ii = a->i;
 67:   PetscInt       *rows;

 69:   PetscFunctionBegin;
 70:   for (i = 0; i < m; i++) {
 71:     if ((ii[i] != ii[i + 1]) && ((jj[ii[i]] < bs * (i / bs)) || (jj[ii[i + 1] - 1] > bs * ((i + bs) / bs) - 1))) cnt++;
 72:   }
 73:   PetscCall(PetscMalloc1(cnt, &rows));
 74:   cnt = 0;
 75:   for (i = 0; i < m; i++) {
 76:     if ((ii[i] != ii[i + 1]) && ((jj[ii[i]] < bs * (i / bs)) || (jj[ii[i + 1] - 1] > bs * ((i + bs) / bs) - 1))) {
 77:       rows[cnt] = i;
 78:       cnt++;
 79:     }
 80:   }
 81:   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, cnt, rows, PETSC_OWN_POINTER, is));
 82:   PetscFunctionReturn(PETSC_SUCCESS);
 83: }

 85: PetscErrorCode MatFindZeroDiagonals_SeqAIJ_Private(Mat A, PetscInt *nrows, PetscInt **zrows)
 86: {
 87:   Mat_SeqAIJ      *a = (Mat_SeqAIJ *)A->data;
 88:   const MatScalar *aa;
 89:   PetscInt         i, m = A->rmap->n, cnt = 0;
 90:   const PetscInt  *ii = a->i, *jj = a->j, *diag;
 91:   PetscInt        *rows;

 93:   PetscFunctionBegin;
 94:   PetscCall(MatSeqAIJGetArrayRead(A, &aa));
 95:   PetscCall(MatGetDiagonalMarkers_SeqAIJ(A, &diag, NULL));
 96:   for (i = 0; i < m; i++) {
 97:     if ((diag[i] >= ii[i + 1]) || (jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) cnt++;
 98:   }
 99:   PetscCall(PetscMalloc1(cnt, &rows));
100:   cnt = 0;
101:   for (i = 0; i < m; i++) {
102:     if ((diag[i] >= ii[i + 1]) || (jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) rows[cnt++] = i;
103:   }
104:   *nrows = cnt;
105:   *zrows = rows;
106:   PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
107:   PetscFunctionReturn(PETSC_SUCCESS);
108: }

110: static PetscErrorCode MatFindZeroDiagonals_SeqAIJ(Mat A, IS *zrows)
111: {
112:   PetscInt nrows, *rows;

114:   PetscFunctionBegin;
115:   *zrows = NULL;
116:   PetscCall(MatFindZeroDiagonals_SeqAIJ_Private(A, &nrows, &rows));
117:   PetscCall(ISCreateGeneral(PetscObjectComm((PetscObject)A), nrows, rows, PETSC_OWN_POINTER, zrows));
118:   PetscFunctionReturn(PETSC_SUCCESS);
119: }

121: static PetscErrorCode MatFindNonzeroRows_SeqAIJ(Mat A, IS *keptrows)
122: {
123:   Mat_SeqAIJ      *a = (Mat_SeqAIJ *)A->data;
124:   const MatScalar *aa;
125:   PetscInt         m = A->rmap->n, cnt = 0;
126:   const PetscInt  *ii;
127:   PetscInt         n, i, j, *rows;

129:   PetscFunctionBegin;
130:   PetscCall(MatSeqAIJGetArrayRead(A, &aa));
131:   *keptrows = NULL;
132:   ii        = a->i;
133:   for (i = 0; i < m; i++) {
134:     n = ii[i + 1] - ii[i];
135:     if (!n) {
136:       cnt++;
137:       goto ok1;
138:     }
139:     for (j = ii[i]; j < ii[i + 1]; j++) {
140:       if (aa[j] != 0.0) goto ok1;
141:     }
142:     cnt++;
143:   ok1:;
144:   }
145:   if (!cnt) {
146:     PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
147:     PetscFunctionReturn(PETSC_SUCCESS);
148:   }
149:   PetscCall(PetscMalloc1(A->rmap->n - cnt, &rows));
150:   cnt = 0;
151:   for (i = 0; i < m; i++) {
152:     n = ii[i + 1] - ii[i];
153:     if (!n) continue;
154:     for (j = ii[i]; j < ii[i + 1]; j++) {
155:       if (aa[j] != 0.0) {
156:         rows[cnt++] = i;
157:         break;
158:       }
159:     }
160:   }
161:   PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
162:   PetscCall(ISCreateGeneral(PETSC_COMM_SELF, cnt, rows, PETSC_OWN_POINTER, keptrows));
163:   PetscFunctionReturn(PETSC_SUCCESS);
164: }

166: PetscErrorCode MatDiagonalSet_SeqAIJ(Mat Y, Vec D, InsertMode is)
167: {
168:   PetscInt           i, m = Y->rmap->n;
169:   const PetscInt    *diag;
170:   MatScalar         *aa;
171:   const PetscScalar *v;
172:   PetscBool          diagDense;

174:   PetscFunctionBegin;
175:   if (Y->assembled) {
176:     PetscCall(MatGetDiagonalMarkers_SeqAIJ(Y, &diag, &diagDense));
177:     if (diagDense) {
178:       PetscCall(VecGetArrayRead(D, &v));
179:       PetscCall(MatSeqAIJGetArray(Y, &aa));
180:       if (is == INSERT_VALUES) {
181:         for (i = 0; i < m; i++) aa[diag[i]] = v[i];
182:       } else {
183:         for (i = 0; i < m; i++) aa[diag[i]] += v[i];
184:       }
185:       PetscCall(MatSeqAIJRestoreArray(Y, &aa));
186:       PetscCall(VecRestoreArrayRead(D, &v));
187:       PetscFunctionReturn(PETSC_SUCCESS);
188:     }
189:   }
190:   PetscCall(MatDiagonalSet_Default(Y, D, is));
191:   PetscFunctionReturn(PETSC_SUCCESS);
192: }

194: PetscErrorCode MatGetRowIJ_SeqAIJ(Mat A, PetscInt oshift, PetscBool symmetric, PetscBool inodecompressed, PetscInt *m, const PetscInt *ia[], const PetscInt *ja[], PetscBool *done)
195: {
196:   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
197:   PetscInt    i, ishift;

199:   PetscFunctionBegin;
200:   if (m) *m = A->rmap->n;
201:   if (!ia) PetscFunctionReturn(PETSC_SUCCESS);
202:   ishift = 0;
203:   if (symmetric && A->structurally_symmetric != PETSC_BOOL3_TRUE) {
204:     PetscCall(MatToSymmetricIJ_SeqAIJ(A->rmap->n, a->i, a->j, PETSC_TRUE, ishift, oshift, (PetscInt **)ia, (PetscInt **)ja));
205:   } else if (oshift == 1) {
206:     PetscInt *tia;
207:     PetscInt  nz = a->i[A->rmap->n];

209:     /* malloc space and  add 1 to i and j indices */
210:     PetscCall(PetscMalloc1(A->rmap->n + 1, &tia));
211:     for (i = 0; i < A->rmap->n + 1; i++) tia[i] = a->i[i] + 1;
212:     *ia = tia;
213:     if (ja) {
214:       PetscInt *tja;

216:       PetscCall(PetscMalloc1(nz + 1, &tja));
217:       for (i = 0; i < nz; i++) tja[i] = a->j[i] + 1;
218:       *ja = tja;
219:     }
220:   } else {
221:     *ia = a->i;
222:     if (ja) *ja = a->j;
223:   }
224:   PetscFunctionReturn(PETSC_SUCCESS);
225: }

227: PetscErrorCode MatRestoreRowIJ_SeqAIJ(Mat A, PetscInt oshift, PetscBool symmetric, PetscBool inodecompressed, PetscInt *n, const PetscInt *ia[], const PetscInt *ja[], PetscBool *done)
228: {
229:   PetscFunctionBegin;
230:   if (!ia) PetscFunctionReturn(PETSC_SUCCESS);
231:   if ((symmetric && A->structurally_symmetric != PETSC_BOOL3_TRUE) || oshift == 1) {
232:     PetscCall(PetscFree(*ia));
233:     if (ja) PetscCall(PetscFree(*ja));
234:   }
235:   PetscFunctionReturn(PETSC_SUCCESS);
236: }

238: PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat A, PetscInt oshift, PetscBool symmetric, PetscBool inodecompressed, PetscInt *nn, const PetscInt *ia[], const PetscInt *ja[], PetscBool *done)
239: {
240:   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
241:   PetscInt    i, *collengths, *cia, *cja, n = A->cmap->n, m = A->rmap->n;
242:   PetscInt    nz = a->i[m], row, *jj, mr, col;

244:   PetscFunctionBegin;
245:   *nn = n;
246:   if (!ia) PetscFunctionReturn(PETSC_SUCCESS);
247:   if (symmetric) {
248:     PetscCall(MatToSymmetricIJ_SeqAIJ(A->rmap->n, a->i, a->j, PETSC_TRUE, 0, oshift, (PetscInt **)ia, (PetscInt **)ja));
249:   } else {
250:     PetscCall(PetscCalloc1(n, &collengths));
251:     PetscCall(PetscMalloc1(n + 1, &cia));
252:     PetscCall(PetscMalloc1(nz, &cja));
253:     jj = a->j;
254:     for (i = 0; i < nz; i++) collengths[jj[i]]++;
255:     cia[0] = oshift;
256:     for (i = 0; i < n; i++) cia[i + 1] = cia[i] + collengths[i];
257:     PetscCall(PetscArrayzero(collengths, n));
258:     jj = a->j;
259:     for (row = 0; row < m; row++) {
260:       mr = a->i[row + 1] - a->i[row];
261:       for (i = 0; i < mr; i++) {
262:         col = *jj++;

264:         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
265:       }
266:     }
267:     PetscCall(PetscFree(collengths));
268:     *ia = cia;
269:     *ja = cja;
270:   }
271:   PetscFunctionReturn(PETSC_SUCCESS);
272: }

274: PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat A, PetscInt oshift, PetscBool symmetric, PetscBool inodecompressed, PetscInt *n, const PetscInt *ia[], const PetscInt *ja[], PetscBool *done)
275: {
276:   PetscFunctionBegin;
277:   if (!ia) PetscFunctionReturn(PETSC_SUCCESS);

279:   PetscCall(PetscFree(*ia));
280:   PetscCall(PetscFree(*ja));
281:   PetscFunctionReturn(PETSC_SUCCESS);
282: }

284: /*
285:  MatGetColumnIJ_SeqAIJ_Color() and MatRestoreColumnIJ_SeqAIJ_Color() are customized from
286:  MatGetColumnIJ_SeqAIJ() and MatRestoreColumnIJ_SeqAIJ() by adding an output
287:  spidx[], index of a->a, to be used in MatTransposeColoringCreate_SeqAIJ() and MatFDColoringCreate_SeqXAIJ()
288: */
289: PetscErrorCode MatGetColumnIJ_SeqAIJ_Color(Mat A, PetscInt oshift, PetscBool symmetric, PetscBool inodecompressed, PetscInt *nn, const PetscInt *ia[], const PetscInt *ja[], PetscInt *spidx[], PetscBool *done)
290: {
291:   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data;
292:   PetscInt        i, *collengths, *cia, *cja, n = A->cmap->n, m = A->rmap->n;
293:   PetscInt        nz = a->i[m], row, mr, col, tmp;
294:   PetscInt       *cspidx;
295:   const PetscInt *jj;

297:   PetscFunctionBegin;
298:   *nn = n;
299:   if (!ia) PetscFunctionReturn(PETSC_SUCCESS);

301:   PetscCall(PetscCalloc1(n, &collengths));
302:   PetscCall(PetscMalloc1(n + 1, &cia));
303:   PetscCall(PetscMalloc1(nz, &cja));
304:   PetscCall(PetscMalloc1(nz, &cspidx));
305:   jj = a->j;
306:   for (i = 0; i < nz; i++) collengths[jj[i]]++;
307:   cia[0] = oshift;
308:   for (i = 0; i < n; i++) cia[i + 1] = cia[i] + collengths[i];
309:   PetscCall(PetscArrayzero(collengths, n));
310:   jj = a->j;
311:   for (row = 0; row < m; row++) {
312:     mr = a->i[row + 1] - a->i[row];
313:     for (i = 0; i < mr; i++) {
314:       col         = *jj++;
315:       tmp         = cia[col] + collengths[col]++ - oshift;
316:       cspidx[tmp] = a->i[row] + i; /* index of a->j */
317:       cja[tmp]    = row + oshift;
318:     }
319:   }
320:   PetscCall(PetscFree(collengths));
321:   *ia    = cia;
322:   *ja    = cja;
323:   *spidx = cspidx;
324:   PetscFunctionReturn(PETSC_SUCCESS);
325: }

327: PetscErrorCode MatRestoreColumnIJ_SeqAIJ_Color(Mat A, PetscInt oshift, PetscBool symmetric, PetscBool inodecompressed, PetscInt *n, const PetscInt *ia[], const PetscInt *ja[], PetscInt *spidx[], PetscBool *done)
328: {
329:   PetscFunctionBegin;
330:   PetscCall(MatRestoreColumnIJ_SeqAIJ(A, oshift, symmetric, inodecompressed, n, ia, ja, done));
331:   PetscCall(PetscFree(*spidx));
332:   PetscFunctionReturn(PETSC_SUCCESS);
333: }

335: static PetscErrorCode MatSetValuesRow_SeqAIJ(Mat A, PetscInt row, const PetscScalar v[])
336: {
337:   Mat_SeqAIJ  *a  = (Mat_SeqAIJ *)A->data;
338:   PetscInt    *ai = a->i;
339:   PetscScalar *aa;

341:   PetscFunctionBegin;
342:   PetscCall(MatSeqAIJGetArray(A, &aa));
343:   PetscCall(PetscArraycpy(aa + ai[row], v, ai[row + 1] - ai[row]));
344:   PetscCall(MatSeqAIJRestoreArray(A, &aa));
345:   PetscFunctionReturn(PETSC_SUCCESS);
346: }

348: #include <petsc/private/isimpl.h>

350: /*@
351:   MatSeqAIJSetValuesLocalFast - An optimized version of `MatSetValuesLocal()` for `MATSEQAIJ` matrices, valid under
352:   several restrictive assumptions.

354:   Not Collective

356:   Input Parameters:
357: + A  - the `MATSEQAIJ` matrix
358: . m  - the number of rows being set (must be 1)
359: . im - array of length `m` giving the local row index
360: . n  - the number of columns being set
361: . in - array of length `n` giving the local column indices
362: . v  - array of length `n` of values to add
363: - is - the insert mode (must be `ADD_VALUES`)

365:   Level: developer

367:   Notes:
368:   This routine requires that a single row of values is set with each call, that no row or column
369:   index is negative or larger than the number of rows or columns, that values are always added
370:   (not inserted), and that no new nonzero locations are introduced.

372:   The global column indices are not assumed to be sorted.

374: .seealso: `Mat`, `MATSEQAIJ`, `MatSetValuesLocal()`, `MatSetValues()`
375: @*/
376: PetscErrorCode MatSeqAIJSetValuesLocalFast(Mat A, PetscInt m, const PetscInt im[], PetscInt n, const PetscInt in[], const PetscScalar v[], InsertMode is)
377: {
378:   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data;
379:   PetscInt        low, high, t, row, nrow, i, col, l;
380:   const PetscInt *rp, *ai = a->i, *ailen = a->ilen, *aj = a->j;
381:   PetscInt        lastcol = -1;
382:   MatScalar      *ap, value, *aa;
383:   const PetscInt *ridx = A->rmap->mapping->indices, *cidx = A->cmap->mapping->indices;

385:   PetscFunctionBegin;
386:   PetscCall(MatSeqAIJGetArray(A, &aa));
387:   row  = ridx[im[0]];
388:   rp   = aj + ai[row];
389:   ap   = aa + ai[row];
390:   nrow = ailen[row];
391:   low  = 0;
392:   high = nrow;
393:   for (l = 0; l < n; l++) { /* loop over added columns */
394:     col   = cidx[in[l]];
395:     value = v[l];

397:     if (col <= lastcol) low = 0;
398:     else high = nrow;
399:     lastcol = col;
400:     while (high - low > 5) {
401:       t = (low + high) / 2;
402:       if (rp[t] > col) high = t;
403:       else low = t;
404:     }
405:     for (i = low; i < high; i++) {
406:       if (rp[i] == col) {
407:         ap[i] += value;
408:         low = i + 1;
409:         break;
410:       }
411:     }
412:   }
413:   PetscCall(MatSeqAIJRestoreArray(A, &aa));
414:   return PETSC_SUCCESS;
415: }

417: PetscErrorCode MatSetValues_SeqAIJ(Mat A, PetscInt m, const PetscInt im[], PetscInt n, const PetscInt in[], const PetscScalar v[], InsertMode is)
418: {
419:   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
420:   PetscInt   *rp, k, low, high, t, ii, row, nrow, i, col, l, rmax, N;
421:   PetscInt   *imax = a->imax, *ai = a->i, *ailen = a->ilen;
422:   PetscInt   *aj = a->j, nonew = a->nonew, lastcol = -1;
423:   MatScalar  *ap = NULL, value = 0.0, *aa;
424:   PetscBool   ignorezeroentries = a->ignorezeroentries;
425:   PetscBool   roworiented       = a->roworiented;

427:   PetscFunctionBegin;
428:   PetscCall(MatSeqAIJGetArray(A, &aa));
429:   for (k = 0; k < m; k++) { /* loop over added rows */
430:     row = im[k];
431:     if (row < 0) continue;
432:     PetscCheck(row < A->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Row too large: row %" PetscInt_FMT " max %" PetscInt_FMT, row, A->rmap->n - 1);
433:     rp = PetscSafePointerPlusOffset(aj, ai[row]);
434:     if (!A->structure_only) ap = PetscSafePointerPlusOffset(aa, ai[row]);
435:     rmax = imax[row];
436:     nrow = ailen[row];
437:     low  = 0;
438:     high = nrow;
439:     for (l = 0; l < n; l++) { /* loop over added columns */
440:       if (in[l] < 0) continue;
441:       PetscCheck(in[l] < A->cmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Column too large: col %" PetscInt_FMT " max %" PetscInt_FMT, in[l], A->cmap->n - 1);
442:       col = in[l];
443:       if (v && !A->structure_only) value = roworiented ? v[l + k * n] : v[k + l * m];
444:       if (!A->structure_only && value == 0.0 && ignorezeroentries && is == ADD_VALUES && row != col) continue;

446:       if (col <= lastcol) low = 0;
447:       else high = nrow;
448:       lastcol = col;
449:       while (high - low > 5) {
450:         t = (low + high) / 2;
451:         if (rp[t] > col) high = t;
452:         else low = t;
453:       }
454:       for (i = low; i < high; i++) {
455:         if (rp[i] > col) break;
456:         if (rp[i] == col) {
457:           if (!A->structure_only) {
458:             if (is == ADD_VALUES) {
459:               ap[i] += value;
460:               (void)PetscLogFlops(1.0);
461:             } else ap[i] = value;
462:           }
463:           low = i + 1;
464:           goto noinsert;
465:         }
466:       }
467:       if ((!A->structure_only && value == 0.0 && ignorezeroentries && row != col) || nonew == 1) goto noinsert;
468:       PetscCheck(nonew != -1, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Inserting a new nonzero at (%" PetscInt_FMT ",%" PetscInt_FMT ") in the matrix", row, col);
469:       if (A->structure_only) {
470:         MatSeqXAIJReallocateAIJ_structure_only(A, A->rmap->n, 1, nrow, row, col, rmax, ai, aj, rp, imax, nonew, MatScalar);
471:       } else {
472:         MatSeqXAIJReallocateAIJ(A, A->rmap->n, 1, nrow, row, col, rmax, aa, ai, aj, rp, ap, imax, nonew, MatScalar);
473:       }
474:       N = nrow++ - 1;
475:       a->nz++;
476:       high++;
477:       /* shift up all the later entries in this row */
478:       PetscCall(PetscArraymove(rp + i + 1, rp + i, N - i + 1));
479:       rp[i] = col;
480:       if (!A->structure_only) {
481:         PetscCall(PetscArraymove(ap + i + 1, ap + i, N - i + 1));
482:         ap[i] = value;
483:       }
484:       low = i + 1;
485:     noinsert:;
486:     }
487:     ailen[row] = nrow;
488:   }
489:   PetscCall(MatSeqAIJRestoreArray(A, &aa));
490:   PetscFunctionReturn(PETSC_SUCCESS);
491: }

493: static PetscErrorCode MatSetValues_SeqAIJ_SortedFullNoPreallocation(Mat A, PetscInt m, const PetscInt im[], PetscInt n, const PetscInt in[], const PetscScalar v[], InsertMode is)
494: {
495:   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
496:   PetscInt   *rp, k, row;
497:   PetscInt   *ai = a->i;
498:   PetscInt   *aj = a->j;
499:   MatScalar  *aa, *ap;

501:   PetscFunctionBegin;
502:   PetscCheck(!A->was_assembled, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Cannot call on assembled matrix.");
503:   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);

505:   PetscCall(MatSeqAIJGetArray(A, &aa));
506:   for (k = 0; k < m; k++) { /* loop over added rows */
507:     row = im[k];
508:     rp  = aj + ai[row];
509:     ap  = PetscSafePointerPlusOffset(aa, ai[row]);

511:     PetscCall(PetscArraycpy(rp, in, n));
512:     if (!A->structure_only) {
513:       if (v) {
514:         PetscCall(PetscArraycpy(ap, v, n));
515:         v += n;
516:       } else {
517:         PetscCall(PetscMemzero(ap, n * sizeof(PetscScalar)));
518:       }
519:     }
520:     a->ilen[row]  = n;
521:     a->imax[row]  = n;
522:     a->i[row + 1] = a->i[row] + n;
523:     a->nz += n;
524:   }
525:   PetscCall(MatSeqAIJRestoreArray(A, &aa));
526:   PetscFunctionReturn(PETSC_SUCCESS);
527: }

529: /*@
530:   MatSeqAIJSetTotalPreallocation - Sets an upper bound on the total number of expected nonzeros in the matrix.

532:   Input Parameters:
533: + A       - the `MATSEQAIJ` matrix
534: - nztotal - bound on the number of nonzeros

536:   Level: advanced

538:   Notes:
539:   This can be called if you will be provided the matrix row by row (from row zero) with sorted column indices for each row.
540:   Simply call `MatSetValues()` after this call to provide the matrix entries in the usual manner. This matrix may be used
541:   as always with multiple matrix assemblies.

543: .seealso: [](ch_matrices), `Mat`, `MatSetOption()`, `MAT_SORTED_FULL`, `MatSetValues()`, `MatSeqAIJSetPreallocation()`
544: @*/
545: PetscErrorCode MatSeqAIJSetTotalPreallocation(Mat A, PetscInt nztotal)
546: {
547:   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;

549:   PetscFunctionBegin;
550:   PetscCall(PetscLayoutSetUp(A->rmap));
551:   PetscCall(PetscLayoutSetUp(A->cmap));
552:   a->maxnz = nztotal;
553:   if (!a->imax) PetscCall(PetscMalloc1(A->rmap->n, &a->imax));
554:   if (!a->ilen) {
555:     PetscCall(PetscMalloc1(A->rmap->n, &a->ilen));
556:   } else {
557:     PetscCall(PetscMemzero(a->ilen, A->rmap->n * sizeof(PetscInt)));
558:   }

560:   /* allocate the matrix space */
561:   PetscCall(PetscShmgetAllocateArray(A->rmap->n + 1, sizeof(PetscInt), (void **)&a->i));
562:   PetscCall(PetscShmgetAllocateArray(nztotal, sizeof(PetscInt), (void **)&a->j));
563:   a->free_ij = PETSC_TRUE;
564:   if (A->structure_only) {
565:     a->free_a = PETSC_FALSE;
566:   } else {
567:     PetscCall(PetscShmgetAllocateArray(nztotal, sizeof(PetscScalar), (void **)&a->a));
568:     a->free_a = PETSC_TRUE;
569:   }
570:   a->i[0]           = 0;
571:   A->ops->setvalues = MatSetValues_SeqAIJ_SortedFullNoPreallocation;
572:   A->preallocated   = PETSC_TRUE;
573:   PetscFunctionReturn(PETSC_SUCCESS);
574: }

576: static PetscErrorCode MatSetValues_SeqAIJ_SortedFull(Mat A, PetscInt m, const PetscInt im[], PetscInt n, const PetscInt in[], const PetscScalar v[], InsertMode is)
577: {
578:   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
579:   PetscInt   *rp, k, row;
580:   PetscInt   *ai = a->i, *ailen = a->ilen;
581:   PetscInt   *aj = a->j;
582:   MatScalar  *aa, *ap;

584:   PetscFunctionBegin;
585:   PetscCall(MatSeqAIJGetArray(A, &aa));
586:   for (k = 0; k < m; k++) { /* loop over added rows */
587:     row = im[k];
588:     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);
589:     rp = aj + ai[row];
590:     ap = aa + ai[row];
591:     if (!A->was_assembled) PetscCall(PetscArraycpy(rp, in, n));
592:     if (!A->structure_only) {
593:       if (v) {
594:         PetscCall(PetscArraycpy(ap, v, n));
595:         v += n;
596:       } else {
597:         PetscCall(PetscMemzero(ap, n * sizeof(PetscScalar)));
598:       }
599:     }
600:     ailen[row] = n;
601:     a->nz += n;
602:   }
603:   PetscCall(MatSeqAIJRestoreArray(A, &aa));
604:   PetscFunctionReturn(PETSC_SUCCESS);
605: }

607: static PetscErrorCode MatGetValues_SeqAIJ(Mat A, PetscInt m, const PetscInt im[], PetscInt n, const PetscInt in[], PetscScalar v[])
608: {
609:   Mat_SeqAIJ      *a = (Mat_SeqAIJ *)A->data;
610:   PetscInt        *rp, k, low, high, t, row, nrow, i, col, l, *aj = a->j;
611:   PetscInt        *ai = a->i, *ailen = a->ilen;
612:   const MatScalar *ap, *aa;
613:   PetscBool        hyprecoo;
614:   PetscBool        roworiented = a->roworiented;
615:   PetscScalar     *value;

617:   PetscFunctionBegin;
618:   PetscCall(PetscStrcmp("_internal_COO_mat_for_hypre", ((PetscObject)A)->name, &hyprecoo));

620:   PetscCall(MatSeqAIJGetArrayRead(A, &aa));
621:   for (k = 0; k < m; k++) { /* loop over rows */
622:     row = im[k];
623:     if (row < 0) continue; /* negative row */
624:     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);
625:     rp   = PetscSafePointerPlusOffset(aj, ai[row]);
626:     ap   = PetscSafePointerPlusOffset(aa, ai[row]);
627:     nrow = ailen[row];
628:     for (l = 0; l < n; l++) {  /* loop over columns */
629:       if (in[l] < 0) continue; /* negative column */
630:       value = roworiented ? &v[l + k * n] : &v[k + l * m];
631:       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);
632:       col = in[l];
633:       /* hypre coo mat stores its diagonal at the front, out of sort */
634:       if (hyprecoo) {
635:         if (col == rp[0]) {
636:           *value = ap[0];
637:           goto finished;
638:         }
639:         low = 1;
640:       } else low = 0;
641:       high = nrow;
642:       /* assume sorted */
643:       while (high - low > 5) {
644:         t = (low + high) / 2;
645:         if (rp[t] > col) high = t;
646:         else low = t;
647:       }
648:       for (i = low; i < high; i++) {
649:         if (rp[i] > col) break;
650:         if (rp[i] == col) {
651:           *value = ap[i];
652:           goto finished;
653:         }
654:       }
655:       *value = 0.0;
656:     finished:;
657:     }
658:   }
659:   PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
660:   PetscFunctionReturn(PETSC_SUCCESS);
661: }

663: static PetscErrorCode MatView_SeqAIJ_Binary(Mat mat, PetscViewer viewer)
664: {
665:   Mat_SeqAIJ        *A = (Mat_SeqAIJ *)mat->data;
666:   const PetscScalar *av;
667:   PetscInt           header[4], M, N, m, nz, i;
668:   PetscInt          *rowlens;

670:   PetscFunctionBegin;
671:   PetscCall(PetscViewerSetUp(viewer));

673:   M  = mat->rmap->N;
674:   N  = mat->cmap->N;
675:   m  = mat->rmap->n;
676:   nz = A->nz;

678:   /* write matrix header */
679:   header[0] = MAT_FILE_CLASSID;
680:   header[1] = M;
681:   header[2] = N;
682:   header[3] = nz;
683:   PetscCall(PetscViewerBinaryWrite(viewer, header, 4, PETSC_INT));

685:   /* fill in and store row lengths */
686:   PetscCall(PetscMalloc1(m, &rowlens));
687:   for (i = 0; i < m; i++) rowlens[i] = A->i[i + 1] - A->i[i];
688:   if (PetscDefined(USE_DEBUG)) {
689:     PetscInt mnz = 0;

691:     for (i = 0; i < m; i++) mnz += rowlens[i];
692:     PetscCheck(nz == mnz, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Row lens %" PetscInt_FMT " do not sum to nz %" PetscInt_FMT, mnz, nz);
693:   }
694:   PetscCall(PetscViewerBinaryWrite(viewer, rowlens, m, PETSC_INT));
695:   PetscCall(PetscFree(rowlens));
696:   /* store column indices */
697:   PetscCall(PetscViewerBinaryWrite(viewer, A->j, nz, PETSC_INT));
698:   /* store nonzero values */
699:   PetscCall(MatSeqAIJGetArrayRead(mat, &av));
700:   PetscCall(PetscViewerBinaryWrite(viewer, av, nz, PETSC_SCALAR));
701:   PetscCall(MatSeqAIJRestoreArrayRead(mat, &av));

703:   /* write block size option to the viewer's .info file */
704:   PetscCall(MatView_Binary_BlockSizes(mat, viewer));
705:   PetscFunctionReturn(PETSC_SUCCESS);
706: }

708: static PetscErrorCode MatView_SeqAIJ_ASCII_structonly(Mat A, PetscViewer viewer)
709: {
710:   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
711:   PetscInt    i, k, m = A->rmap->N;

713:   PetscFunctionBegin;
714:   PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_FALSE));
715:   for (i = 0; i < m; i++) {
716:     PetscCall(PetscViewerASCIIPrintf(viewer, "row %" PetscInt_FMT ":", i));
717:     for (k = a->i[i]; k < a->i[i + 1]; k++) PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ") ", a->j[k]));
718:     PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
719:   }
720:   PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_TRUE));
721:   PetscFunctionReturn(PETSC_SUCCESS);
722: }

724: static PetscErrorCode MatView_SeqAIJ_ASCII(Mat A, PetscViewer viewer)
725: {
726:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data;
727:   const PetscScalar *av;
728:   PetscInt           i, j, m = A->rmap->n;
729:   const char        *name;
730:   PetscViewerFormat  format;

732:   PetscFunctionBegin;
733:   if (A->structure_only) {
734:     PetscCall(MatView_SeqAIJ_ASCII_structonly(A, viewer));
735:     PetscFunctionReturn(PETSC_SUCCESS);
736:   }

738:   PetscCall(PetscViewerGetFormat(viewer, &format));
739:   // By petsc's rule, even PETSC_VIEWER_ASCII_INFO_DETAIL doesn't print matrix entries
740:   if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) PetscFunctionReturn(PETSC_SUCCESS);

742:   /* trigger copy to CPU if needed */
743:   PetscCall(MatSeqAIJGetArrayRead(A, &av));
744:   PetscCall(MatSeqAIJRestoreArrayRead(A, &av));
745:   if (format == PETSC_VIEWER_ASCII_MATLAB) {
746:     PetscInt nofinalvalue = 0;
747:     if (m && ((a->i[m] == a->i[m - 1]) || (a->j[a->nz - 1] != A->cmap->n - 1))) {
748:       /* Need a dummy value to ensure the dimension of the matrix. */
749:       nofinalvalue = 1;
750:     }
751:     PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_FALSE));
752:     PetscCall(PetscViewerASCIIPrintf(viewer, "%% Size = %" PetscInt_FMT " %" PetscInt_FMT " \n", m, A->cmap->n));
753:     PetscCall(PetscViewerASCIIPrintf(viewer, "%% Nonzeros = %" PetscInt_FMT " \n", a->nz));
754:     PetscCall(PetscViewerASCIIPrintf(viewer, "zzz = zeros(%" PetscInt_FMT ",%d);\n", a->nz + nofinalvalue, PetscDefined(USE_COMPLEX) ? 4 : 3));
755:     PetscCall(PetscViewerASCIIPrintf(viewer, "zzz = [\n"));

757:     for (i = 0; i < m; i++) {
758:       for (j = a->i[i]; j < a->i[i + 1]; j++) {
759: #if PetscDefined(USE_COMPLEX)
760:         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])));
761: #else
762:         PetscCall(PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT " %" PetscInt_FMT "  %18.16e\n", i + 1, a->j[j] + 1, (double)a->a[j]));
763: #endif
764:       }
765:     }
766:     if (nofinalvalue) {
767:       if (PetscDefined(USE_COMPLEX)) PetscCall(PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT " %" PetscInt_FMT "  %18.16e %18.16e\n", m, A->cmap->n, 0., 0.));
768:       else PetscCall(PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT " %" PetscInt_FMT "  %18.16e\n", m, A->cmap->n, 0.0));
769:     }
770:     PetscCall(PetscObjectGetName((PetscObject)A, &name));
771:     PetscCall(PetscViewerASCIIPrintf(viewer, "];\n %s = spconvert(zzz);\n", name));
772:     PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_TRUE));
773:   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
774:     PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_FALSE));
775:     for (i = 0; i < m; i++) {
776:       PetscCall(PetscViewerASCIIPrintf(viewer, "row %" PetscInt_FMT ":", i));
777:       for (j = a->i[i]; j < a->i[i + 1]; j++) {
778: #if PetscDefined(USE_COMPLEX)
779:         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
780:           PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g + %g i)", a->j[j], (double)PetscRealPart(a->a[j]), (double)PetscImaginaryPart(a->a[j])));
781:         } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
782:           PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g - %g i)", a->j[j], (double)PetscRealPart(a->a[j]), (double)-PetscImaginaryPart(a->a[j])));
783:         } else if (PetscRealPart(a->a[j]) != 0.0) {
784:           PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g) ", a->j[j], (double)PetscRealPart(a->a[j])));
785:         }
786: #else
787:         if (a->a[j] != 0.0) PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g) ", a->j[j], (double)a->a[j]));
788: #endif
789:       }
790:       PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
791:     }
792:     PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_TRUE));
793:   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
794:     PetscInt nzd = 0, fshift = 1, *sptr;
795:     PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_FALSE));
796:     PetscCall(PetscMalloc1(m + 1, &sptr));
797:     for (i = 0; i < m; i++) {
798:       sptr[i] = nzd + 1;
799:       for (j = a->i[i]; j < a->i[i + 1]; j++) {
800:         if (a->j[j] >= i) {
801:           if (PetscDefined(USE_COMPLEX) ? (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) : a->a[j] != 0.0) nzd++;
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 PetscDefined(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:     PetscBool   realonly = PETSC_TRUE;

849:     if (PetscDefined(USE_COMPLEX)) {
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:     }

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 (!PetscDefined(USE_COMPLEX) || realonly) {
869:           PetscCall(PetscViewerASCIIPrintf(viewer, " %7.5e ", (double)PetscRealPart(value)));
870:         } else {
871:           PetscCall(PetscViewerASCIIPrintf(viewer, " %7.5e+%7.5e i ", (double)PetscRealPart(value), (double)PetscImaginaryPart(value)));
872:         }
873:       }
874:       PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
875:     }
876:     PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_TRUE));
877:   } else if (format == PETSC_VIEWER_ASCII_MATRIXMARKET) {
878:     PetscInt fshift = 1;
879:     PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_FALSE));
880:     PetscCall(PetscViewerASCIIPrintf(viewer, "%%%%MatrixMarket matrix coordinate %s general\n", PetscDefined(USE_COMPLEX) ? "complex" : "real"));
881:     PetscCall(PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT "\n", m, A->cmap->n, a->nz));
882:     for (i = 0; i < m; i++) {
883:       for (j = a->i[i]; j < a->i[i + 1]; j++) {
884: #if PetscDefined(USE_COMPLEX)
885:         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])));
886: #else
887:         PetscCall(PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT " %" PetscInt_FMT " %g\n", i + fshift, a->j[j] + fshift, (double)a->a[j]));
888: #endif
889:       }
890:     }
891:     PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_TRUE));
892:   } else {
893:     PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_FALSE));
894:     if (A->factortype) {
895:       const PetscInt *adiag;

897:       PetscCall(MatGetDiagonalMarkers_SeqAIJ(A, &adiag, NULL));
898:       for (i = 0; i < m; i++) {
899:         PetscCall(PetscViewerASCIIPrintf(viewer, "row %" PetscInt_FMT ":", i));
900:         /* L part */
901:         for (j = a->i[i]; j < a->i[i + 1]; j++) {
902:           if (PetscDefined(USE_COMPLEX) && PetscImaginaryPart(a->a[j]) > 0.0) {
903:             PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g + %g i)", a->j[j], (double)PetscRealPart(a->a[j]), (double)PetscImaginaryPart(a->a[j])));
904:           } else if (PetscDefined(USE_COMPLEX) && PetscImaginaryPart(a->a[j]) < 0.0) {
905:             PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g - %g i)", a->j[j], (double)PetscRealPart(a->a[j]), (double)(-PetscImaginaryPart(a->a[j]))));
906:           } else {
907:             PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g) ", a->j[j], (double)PetscRealPart(a->a[j])));
908:           }
909:         }
910:         /* diagonal */
911:         j = adiag[i];
912:         if (PetscDefined(USE_COMPLEX) && PetscImaginaryPart(a->a[j]) > 0.0) {
913:           PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g + %g i)", a->j[j], (double)PetscRealPart(1 / a->a[j]), (double)PetscImaginaryPart(1 / a->a[j])));
914:         } else if (PetscDefined(USE_COMPLEX) && PetscImaginaryPart(a->a[j]) < 0.0) {
915:           PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g - %g i)", a->j[j], (double)PetscRealPart(1 / a->a[j]), (double)(-PetscImaginaryPart(1 / a->a[j]))));
916:         } else {
917:           PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g) ", a->j[j], (double)PetscRealPart(1 / a->a[j])));
918:         }

920:         /* U part */
921:         for (j = adiag[i + 1] + 1; j < adiag[i]; j++) {
922:           if (PetscDefined(USE_COMPLEX) && PetscImaginaryPart(a->a[j]) > 0.0) {
923:             PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g + %g i)", a->j[j], (double)PetscRealPart(a->a[j]), (double)PetscImaginaryPart(a->a[j])));
924:           } else if (PetscDefined(USE_COMPLEX) && PetscImaginaryPart(a->a[j]) < 0.0) {
925:             PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g - %g i)", a->j[j], (double)PetscRealPart(a->a[j]), (double)(-PetscImaginaryPart(a->a[j]))));
926:           } else {
927:             PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g) ", a->j[j], (double)PetscRealPart(a->a[j])));
928:           }
929:         }
930:         PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
931:       }
932:     } else {
933:       for (i = 0; i < m; i++) {
934:         PetscCall(PetscViewerASCIIPrintf(viewer, "row %" PetscInt_FMT ":", i));
935:         for (j = a->i[i]; j < a->i[i + 1]; j++) {
936:           if (PetscDefined(USE_COMPLEX) && PetscImaginaryPart(a->a[j]) > 0.0) {
937:             PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g + %g i)", a->j[j], (double)PetscRealPart(a->a[j]), (double)PetscImaginaryPart(a->a[j])));
938:           } else if (PetscDefined(USE_COMPLEX) && PetscImaginaryPart(a->a[j]) < 0.0) {
939:             PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g - %g i)", a->j[j], (double)PetscRealPart(a->a[j]), (double)-PetscImaginaryPart(a->a[j])));
940:           } else {
941:             PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g) ", a->j[j], (double)PetscRealPart(a->a[j])));
942:           }
943:         }
944:         PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
945:       }
946:     }
947:     PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_TRUE));
948:   }
949:   PetscCall(PetscViewerFlush(viewer));
950:   PetscFunctionReturn(PETSC_SUCCESS);
951: }

953: #include <petscdraw.h>
954: #if defined(__GNUC__) && !defined(__clang__)
955:   #pragma GCC diagnostic push
956:   #pragma GCC diagnostic ignored "-Wclobbered"
957: #endif
958: static PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw, void *Aa)
959: {
960:   Mat                A = (Mat)Aa;
961:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data;
962:   PetscInt           i, j, m = A->rmap->n;
963:   int                color;
964:   PetscReal          xl, yl, xr, yr, x_l, x_r, y_l, y_r;
965:   PetscViewer        viewer;
966:   PetscViewerFormat  format;
967:   const PetscScalar *aa;

969:   PetscFunctionBegin;
970:   PetscCall(PetscObjectQuery((PetscObject)A, "Zoomviewer", (PetscObject *)&viewer));
971:   PetscCall(PetscViewerGetFormat(viewer, &format));
972:   PetscCall(PetscDrawGetCoordinates(draw, &xl, &yl, &xr, &yr));

974:   /* loop over matrix elements drawing boxes */
975:   PetscCall(MatSeqAIJGetArrayRead(A, &aa));
976:   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
977:     PetscDrawCollectiveBegin(draw);
978:     /* Blue for negative, Cyan for zero and  Red for positive */
979:     color = PETSC_DRAW_BLUE;
980:     for (i = 0; i < m; i++) {
981:       y_l = m - i - 1.0;
982:       y_r = y_l + 1.0;
983:       for (j = a->i[i]; j < a->i[i + 1]; j++) {
984:         x_l = a->j[j];
985:         x_r = x_l + 1.0;
986:         if (PetscRealPart(aa[j]) >= 0.) continue;
987:         PetscCall(PetscDrawRectangle(draw, x_l, y_l, x_r, y_r, color, color, color, color));
988:       }
989:     }
990:     color = PETSC_DRAW_CYAN;
991:     for (i = 0; i < m; i++) {
992:       y_l = m - i - 1.0;
993:       y_r = y_l + 1.0;
994:       for (j = a->i[i]; j < a->i[i + 1]; j++) {
995:         x_l = a->j[j];
996:         x_r = x_l + 1.0;
997:         if (aa[j] != 0.) continue;
998:         PetscCall(PetscDrawRectangle(draw, x_l, y_l, x_r, y_r, color, color, color, color));
999:       }
1000:     }
1001:     color = PETSC_DRAW_RED;
1002:     for (i = 0; i < m; i++) {
1003:       y_l = m - i - 1.0;
1004:       y_r = y_l + 1.0;
1005:       for (j = a->i[i]; j < a->i[i + 1]; j++) {
1006:         x_l = a->j[j];
1007:         x_r = x_l + 1.0;
1008:         if (PetscRealPart(aa[j]) <= 0.) continue;
1009:         PetscCall(PetscDrawRectangle(draw, x_l, y_l, x_r, y_r, color, color, color, color));
1010:       }
1011:     }
1012:     PetscDrawCollectiveEnd(draw);
1013:   } else {
1014:     /* use contour shading to indicate magnitude of values */
1015:     /* first determine max of all nonzero values */
1016:     PetscReal minv = 0.0, maxv = 0.0;
1017:     PetscInt  nz = a->nz, count = 0;
1018:     PetscDraw popup;

1020:     for (i = 0; i < nz; i++) {
1021:       if (PetscAbsScalar(aa[i]) > maxv) maxv = PetscAbsScalar(aa[i]);
1022:     }
1023:     if (minv >= maxv) maxv = minv + PETSC_SMALL;
1024:     PetscCall(PetscDrawGetPopup(draw, &popup));
1025:     PetscCall(PetscDrawScalePopup(popup, minv, maxv));

1027:     PetscDrawCollectiveBegin(draw);
1028:     for (i = 0; i < m; i++) {
1029:       y_l = m - i - 1.0;
1030:       y_r = y_l + 1.0;
1031:       for (j = a->i[i]; j < a->i[i + 1]; j++) {
1032:         x_l   = a->j[j];
1033:         x_r   = x_l + 1.0;
1034:         color = PetscDrawRealToColor(PetscAbsScalar(aa[count]), minv, maxv);
1035:         PetscCall(PetscDrawRectangle(draw, x_l, y_l, x_r, y_r, color, color, color, color));
1036:         count++;
1037:       }
1038:     }
1039:     PetscDrawCollectiveEnd(draw);
1040:   }
1041:   PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
1042:   PetscFunctionReturn(PETSC_SUCCESS);
1043: }
1044: #if defined(__GNUC__) && !defined(__clang__)
1045:   #pragma GCC diagnostic pop
1046: #endif

1048: #include <petscdraw.h>
1049: static PetscErrorCode MatView_SeqAIJ_Draw(Mat A, PetscViewer viewer)
1050: {
1051:   PetscDraw draw;
1052:   PetscReal xr, yr, xl, yl, h, w;
1053:   PetscBool isnull;

1055:   PetscFunctionBegin;
1056:   PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw));
1057:   PetscCall(PetscDrawIsNull(draw, &isnull));
1058:   if (isnull) PetscFunctionReturn(PETSC_SUCCESS);

1060:   xr = A->cmap->n;
1061:   yr = A->rmap->n;
1062:   h  = yr / 10.0;
1063:   w  = xr / 10.0;
1064:   xr += w;
1065:   yr += h;
1066:   xl = -w;
1067:   yl = -h;
1068:   PetscCall(PetscDrawSetCoordinates(draw, xl, yl, xr, yr));
1069:   PetscCall(PetscObjectCompose((PetscObject)A, "Zoomviewer", (PetscObject)viewer));
1070:   PetscCall(PetscDrawZoom(draw, MatView_SeqAIJ_Draw_Zoom, A));
1071:   PetscCall(PetscObjectCompose((PetscObject)A, "Zoomviewer", NULL));
1072:   PetscCall(PetscDrawSave(draw));
1073:   PetscFunctionReturn(PETSC_SUCCESS);
1074: }

1076: PetscErrorCode MatView_SeqAIJ(Mat A, PetscViewer viewer)
1077: {
1078:   PetscBool isascii, isbinary, isdraw;

1080:   PetscFunctionBegin;
1081:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
1082:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
1083:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
1084:   if (isascii) PetscCall(MatView_SeqAIJ_ASCII(A, viewer));
1085:   else if (isbinary) PetscCall(MatView_SeqAIJ_Binary(A, viewer));
1086:   else if (isdraw) PetscCall(MatView_SeqAIJ_Draw(A, viewer));
1087:   PetscCall(MatView_SeqAIJ_Inode(A, viewer));
1088:   PetscFunctionReturn(PETSC_SUCCESS);
1089: }

1091: PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A, MatAssemblyType mode)
1092: {
1093:   Mat_SeqAIJ *a      = (Mat_SeqAIJ *)A->data;
1094:   PetscInt    fshift = 0, i, *ai = a->i, *aj = a->j, *imax = a->imax;
1095:   PetscInt    m = A->rmap->n, *ip, N, *ailen = a->ilen, rmax = 0;
1096:   MatScalar  *aa    = a->a, *ap;
1097:   PetscReal   ratio = 0.6;

1099:   PetscFunctionBegin;
1100:   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(PETSC_SUCCESS);
1101:   if (A->was_assembled && A->ass_nonzerostate == A->nonzerostate) {
1102:     /* 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) */
1103:     PetscCall(MatAssemblyEnd_SeqAIJ_Inode(A, mode)); /* read the sparsity pattern */
1104:     PetscFunctionReturn(PETSC_SUCCESS);
1105:   }

1107:   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
1108:   for (i = 1; i < m; i++) {
1109:     /* move each row back by the amount of empty slots (fshift) before it*/
1110:     fshift += imax[i - 1] - ailen[i - 1];
1111:     rmax = PetscMax(rmax, ailen[i]);
1112:     if (fshift) {
1113:       ip = aj + ai[i];
1114:       N  = ailen[i];
1115:       PetscCall(PetscArraymove(ip - fshift, ip, N));
1116:       if (!A->structure_only) {
1117:         ap = aa + ai[i];
1118:         PetscCall(PetscArraymove(ap - fshift, ap, N));
1119:       }
1120:     }
1121:     ai[i] = ai[i - 1] + ailen[i - 1];
1122:   }
1123:   if (m) {
1124:     fshift += imax[m - 1] - ailen[m - 1];
1125:     ai[m] = ai[m - 1] + ailen[m - 1];
1126:   }
1127:   /* reset ilen and imax for each row */
1128:   a->nonzerorowcnt = 0;
1129:   for (i = 0; i < m; i++) {
1130:     ailen[i] = imax[i] = ai[i + 1] - ai[i];
1131:     a->nonzerorowcnt += ((ai[i + 1] - ai[i]) > 0);
1132:   }
1133:   a->nz = ai[m];
1134:   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);
1135:   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));
1136:   PetscCall(PetscInfo(A, "Number of mallocs during MatSetValues() is %" PetscInt_FMT "\n", a->reallocs));
1137:   PetscCall(PetscInfo(A, "Maximum nonzeros in any row is %" PetscInt_FMT "\n", rmax));

1139:   A->info.mallocs += a->reallocs;
1140:   a->reallocs         = 0;
1141:   A->info.nz_unneeded = (PetscReal)fshift;
1142:   a->rmax             = rmax;

1144:   if (!A->structure_only) PetscCall(MatCheckCompressedRow(A, a->nonzerorowcnt, &a->compressedrow, a->i, m, ratio));
1145:   PetscCall(MatAssemblyEnd_SeqAIJ_Inode(A, mode));
1146:   PetscFunctionReturn(PETSC_SUCCESS);
1147: }

1149: static PetscErrorCode MatRealPart_SeqAIJ(Mat A)
1150: {
1151:   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
1152:   PetscInt    i, nz = a->nz;
1153:   MatScalar  *aa;

1155:   PetscFunctionBegin;
1156:   PetscCall(MatSeqAIJGetArray(A, &aa));
1157:   for (i = 0; i < nz; i++) aa[i] = PetscRealPart(aa[i]);
1158:   PetscCall(MatSeqAIJRestoreArray(A, &aa));
1159:   PetscFunctionReturn(PETSC_SUCCESS);
1160: }

1162: static PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
1163: {
1164:   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
1165:   PetscInt    i, nz = a->nz;
1166:   MatScalar  *aa;

1168:   PetscFunctionBegin;
1169:   PetscCall(MatSeqAIJGetArray(A, &aa));
1170:   for (i = 0; i < nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
1171:   PetscCall(MatSeqAIJRestoreArray(A, &aa));
1172:   PetscFunctionReturn(PETSC_SUCCESS);
1173: }

1175: PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
1176: {
1177:   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
1178:   MatScalar  *aa;

1180:   PetscFunctionBegin;
1181:   PetscCall(MatSeqAIJGetArrayWrite(A, &aa));
1182:   PetscCall(PetscArrayzero(aa, a->i[A->rmap->n]));
1183:   PetscCall(MatSeqAIJRestoreArrayWrite(A, &aa));
1184:   PetscFunctionReturn(PETSC_SUCCESS);
1185: }

1187: static PetscErrorCode MatReset_SeqAIJ(Mat A)
1188: {
1189:   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;

1191:   PetscFunctionBegin;
1192:   if (A->hash_active) {
1193:     A->ops[0] = a->cops;
1194:     PetscCall(PetscHMapIJVDestroy(&a->ht));
1195:     PetscCall(PetscFree(a->dnz));
1196:     A->hash_active = PETSC_FALSE;
1197:   }

1199:   PetscCall(PetscLogObjectState((PetscObject)A, "Rows=%" PetscInt_FMT ", Cols=%" PetscInt_FMT ", NZ=%" PetscInt_FMT, A->rmap->n, A->cmap->n, a->nz));
1200:   PetscCall(MatSeqXAIJFreeAIJ(A, &a->a, &a->j, &a->i));
1201:   PetscCall(ISDestroy(&a->row));
1202:   PetscCall(ISDestroy(&a->col));
1203:   PetscCall(PetscFree(a->diag));
1204:   PetscCall(PetscFree(a->ibdiag));
1205:   a->ibdiagsize = 0;
1206:   PetscCall(PetscFree(a->imax));
1207:   PetscCall(PetscFree(a->ilen));
1208:   PetscCall(PetscFree(a->ipre));
1209:   PetscCall(PetscFree3(a->idiag, a->mdiag, a->ssor_work));
1210:   PetscCall(PetscFree(a->solve_work));
1211:   PetscCall(ISDestroy(&a->icol));
1212:   PetscCall(PetscFree(a->saved_values));
1213:   a->compressedrow.use = PETSC_FALSE;
1214:   PetscCall(PetscFree2(a->compressedrow.i, a->compressedrow.rindex));
1215:   PetscCall(MatDestroy_SeqAIJ_Inode(A));
1216:   PetscFunctionReturn(PETSC_SUCCESS);
1217: }

1219: static PetscErrorCode MatResetHash_SeqAIJ(Mat A)
1220: {
1221:   PetscFunctionBegin;
1222:   PetscCall(MatReset_SeqAIJ(A));
1223:   PetscCall(MatCreate_SeqAIJ_Inode(A));
1224:   PetscCall(MatSetUp_Seq_Hash(A));
1225:   A->nonzerostate++;
1226:   PetscFunctionReturn(PETSC_SUCCESS);
1227: }

1229: PetscErrorCode MatDestroy_SeqAIJ(Mat A)
1230: {
1231:   PetscFunctionBegin;
1232:   PetscCall(MatReset_SeqAIJ(A));
1233:   PetscCall(PetscFree(A->data));

1235:   /* MatMatMultNumeric_SeqAIJ_SeqAIJ_Sorted may allocate this.
1236:      That function is so heavily used (sometimes in an hidden way through multnumeric function pointers)
1237:      that is hard to properly add this data to the MatProduct data. We free it here to avoid
1238:      users reusing the matrix object with different data to incur in obscure segmentation faults
1239:      due to different matrix sizes */
1240:   PetscCall(PetscObjectCompose((PetscObject)A, "__PETSc__ab_dense", NULL));

1242:   PetscCall(PetscObjectChangeTypeName((PetscObject)A, NULL));
1243:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "PetscMatlabEnginePut_C", NULL));
1244:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "PetscMatlabEngineGet_C", NULL));
1245:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatSeqAIJSetColumnIndices_C", NULL));
1246:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatStoreValues_C", NULL));
1247:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatRetrieveValues_C", NULL));
1248:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_seqsbaij_C", NULL));
1249:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_seqbaij_C", NULL));
1250:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_seqaijperm_C", NULL));
1251:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_seqaijsell_C", NULL));
1252: #if PetscDefined(HAVE_MKL_SPARSE)
1253:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_seqaijmkl_C", NULL));
1254: #endif
1255: #if PetscDefined(HAVE_CUDA)
1256:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_seqaijcusparse_C", NULL));
1257:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatProductSetFromOptions_seqaijcusparse_seqaij_C", NULL));
1258:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatProductSetFromOptions_seqaij_seqaijcusparse_C", NULL));
1259: #endif
1260: #if PetscDefined(HAVE_HIP)
1261:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_seqaijhipsparse_C", NULL));
1262:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatProductSetFromOptions_seqaijhipsparse_seqaij_C", NULL));
1263:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatProductSetFromOptions_seqaij_seqaijhipsparse_C", NULL));
1264: #endif
1265: #if PetscDefined(HAVE_KOKKOS_KERNELS)
1266:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_seqaijkokkos_C", NULL));
1267: #endif
1268:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_seqaijcrl_C", NULL));
1269: #if PetscDefined(HAVE_ELEMENTAL)
1270:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_elemental_C", NULL));
1271: #endif
1272: #if PetscDefined(HAVE_SCALAPACK) && (PetscDefined(USE_REAL_SINGLE) || PetscDefined(USE_REAL_DOUBLE))
1273:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_scalapack_C", NULL));
1274: #endif
1275: #if PetscDefined(HAVE_HYPRE)
1276:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_hypre_C", NULL));
1277:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatProductSetFromOptions_transpose_seqaij_seqaij_C", NULL));
1278: #endif
1279:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_seqdense_C", NULL));
1280:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_seqsell_C", NULL));
1281:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_is_C", NULL));
1282:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatIsTranspose_C", NULL));
1283:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatIsHermitianTranspose_C", NULL));
1284:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatSeqAIJSetPreallocation_C", NULL));
1285:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatResetPreallocation_C", NULL));
1286:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatResetHash_C", NULL));
1287:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatSeqAIJSetPreallocationCSR_C", NULL));
1288:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatReorderForNonzeroDiagonal_C", NULL));
1289:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatProductSetFromOptions_is_seqaij_C", NULL));
1290:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatProductSetFromOptions_seqdense_seqaij_C", NULL));
1291:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatProductSetFromOptions_seqaij_seqaij_C", NULL));
1292:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatSeqAIJKron_C", NULL));
1293:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatSetPreallocationCOO_C", NULL));
1294:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatSetValuesCOO_C", NULL));
1295:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatFactorGetSolverType_C", NULL));
1296:   /* these calls do not belong here: the subclasses Duplicate/Destroy are wrong */
1297:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaijsell_seqaij_C", NULL));
1298:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaijperm_seqaij_C", NULL));
1299:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_seqaijviennacl_C", NULL));
1300:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatProductSetFromOptions_seqaijviennacl_seqdense_C", NULL));
1301:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatProductSetFromOptions_seqaijviennacl_seqaij_C", NULL));
1302:   PetscFunctionReturn(PETSC_SUCCESS);
1303: }

1305: PetscErrorCode MatSetOption_SeqAIJ(Mat A, MatOption op, PetscBool flg)
1306: {
1307:   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;

1309:   PetscFunctionBegin;
1310:   switch (op) {
1311:   case MAT_ROW_ORIENTED:
1312:     a->roworiented = flg;
1313:     break;
1314:   case MAT_KEEP_NONZERO_PATTERN:
1315:     a->keepnonzeropattern = flg;
1316:     break;
1317:   case MAT_NEW_NONZERO_LOCATIONS:
1318:     a->nonew = (flg ? 0 : 1);
1319:     break;
1320:   case MAT_NEW_NONZERO_LOCATION_ERR:
1321:     a->nonew = (flg ? -1 : 0);
1322:     break;
1323:   case MAT_NEW_NONZERO_ALLOCATION_ERR:
1324:     a->nonew = (flg ? -2 : 0);
1325:     break;
1326:   case MAT_UNUSED_NONZERO_LOCATION_ERR:
1327:     a->nounused = (flg ? -1 : 0);
1328:     break;
1329:   case MAT_IGNORE_ZERO_ENTRIES:
1330:     a->ignorezeroentries = flg;
1331:     break;
1332:   case MAT_USE_INODES:
1333:     PetscCall(MatSetOption_SeqAIJ_Inode(A, MAT_USE_INODES, flg));
1334:     break;
1335:   case MAT_SUBMAT_SINGLEIS:
1336:     A->submat_singleis = flg;
1337:     break;
1338:   case MAT_SORTED_FULL:
1339:     if (flg) A->ops->setvalues = MatSetValues_SeqAIJ_SortedFull;
1340:     else A->ops->setvalues = MatSetValues_SeqAIJ;
1341:     break;
1342:   case MAT_FORM_EXPLICIT_TRANSPOSE:
1343:     A->form_explicit_transpose = flg;
1344:     break;
1345:   case MAT_STRUCTURE_ONLY:
1346:     if (flg) {
1347:       PetscCall(MatXAIJDeallocatea(A, &a->a));
1348:       a->a = NULL;
1349:     }
1350:     break;
1351:   default:
1352:     break;
1353:   }
1354:   PetscFunctionReturn(PETSC_SUCCESS);
1355: }

1357: PETSC_INTERN PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A, Vec v)
1358: {
1359:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data;
1360:   PetscInt           n, *ai = a->i;
1361:   PetscScalar       *x;
1362:   const PetscScalar *aa;
1363:   const PetscInt    *diag;
1364:   PetscBool          diagDense;

1366:   PetscFunctionBegin;
1367:   PetscCall(VecGetLocalSize(v, &n));
1368:   PetscCheck(n == A->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nonconforming matrix and vector");
1369:   PetscCall(MatSeqAIJGetArrayRead(A, &aa));
1370:   if (A->factortype == MAT_FACTOR_ILU || A->factortype == MAT_FACTOR_LU) {
1371:     PetscCall(MatGetDiagonalMarkers_SeqAIJ(A, &diag, NULL));
1372:     PetscCall(VecGetArrayWrite(v, &x));
1373:     for (PetscInt i = 0; i < n; i++) x[i] = 1.0 / aa[diag[i]];
1374:     PetscCall(VecRestoreArrayWrite(v, &x));
1375:     PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
1376:     PetscFunctionReturn(PETSC_SUCCESS);
1377:   }

1379:   PetscCheck(A->factortype == MAT_FACTOR_NONE, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Not for factor matrices that are not ILU or LU");
1380:   PetscCall(MatGetDiagonalMarkers_SeqAIJ(A, &diag, &diagDense));
1381:   PetscCall(VecGetArrayWrite(v, &x));
1382:   if (diagDense) {
1383:     for (PetscInt i = 0; i < n; i++) x[i] = aa[diag[i]];
1384:   } else {
1385:     for (PetscInt i = 0; i < n; i++) x[i] = (diag[i] == ai[i + 1]) ? 0.0 : aa[diag[i]];
1386:   }
1387:   PetscCall(VecRestoreArrayWrite(v, &x));
1388:   PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
1389:   PetscFunctionReturn(PETSC_SUCCESS);
1390: }

1392: #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
1393: PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A, Vec xx, Vec zz, Vec yy)
1394: {
1395:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data;
1396:   const MatScalar   *aa;
1397:   PetscScalar       *y;
1398:   const PetscScalar *x;
1399:   PetscInt           m = A->rmap->n;
1400: #if !PetscDefined(USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1401:   const MatScalar  *v;
1402:   PetscScalar       alpha;
1403:   PetscInt          n, i, j;
1404:   const PetscInt   *idx, *ii, *ridx = NULL;
1405:   Mat_CompressedRow cprow    = a->compressedrow;
1406:   PetscBool         usecprow = cprow.use;
1407: #endif

1409:   PetscFunctionBegin;
1410:   if (zz != yy) PetscCall(VecCopy(zz, yy));
1411:   PetscCall(VecGetArrayRead(xx, &x));
1412:   PetscCall(VecGetArray(yy, &y));
1413:   PetscCall(MatSeqAIJGetArrayRead(A, &aa));

1415: #if PetscDefined(USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1416:   fortranmulttransposeaddaij_(&m, x, a->i, a->j, aa, y);
1417: #else
1418:   if (usecprow) {
1419:     m    = cprow.nrows;
1420:     ii   = cprow.i;
1421:     ridx = cprow.rindex;
1422:   } else {
1423:     ii = a->i;
1424:   }
1425:   for (i = 0; i < m; i++) {
1426:     idx = a->j + ii[i];
1427:     v   = aa + ii[i];
1428:     n   = ii[i + 1] - ii[i];
1429:     if (usecprow) {
1430:       alpha = x[ridx[i]];
1431:     } else {
1432:       alpha = x[i];
1433:     }
1434:     for (j = 0; j < n; j++) y[idx[j]] += alpha * v[j];
1435:   }
1436: #endif
1437:   PetscCall(PetscLogFlops(2.0 * a->nz));
1438:   PetscCall(VecRestoreArrayRead(xx, &x));
1439:   PetscCall(VecRestoreArray(yy, &y));
1440:   PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
1441:   PetscFunctionReturn(PETSC_SUCCESS);
1442: }

1444: PetscErrorCode MatMultTranspose_SeqAIJ(Mat A, Vec xx, Vec yy)
1445: {
1446:   PetscFunctionBegin;
1447:   PetscCall(VecSet(yy, 0.0));
1448:   PetscCall(MatMultTransposeAdd_SeqAIJ(A, xx, yy, yy));
1449:   PetscFunctionReturn(PETSC_SUCCESS);
1450: }

1452: #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>

1454: PetscErrorCode MatMult_SeqAIJ(Mat A, Vec xx, Vec yy)
1455: {
1456:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data;
1457:   PetscScalar       *y;
1458:   const PetscScalar *x;
1459:   const MatScalar   *a_a;
1460:   PetscInt           m = A->rmap->n;
1461:   const PetscInt    *ii, *ridx = NULL;
1462:   PetscBool          usecprow = a->compressedrow.use;

1464: #if PetscDefined(HAVE_PRAGMA_DISJOINT)
1465:   #pragma disjoint(*x, *y, *aa)
1466: #endif

1468:   PetscFunctionBegin;
1469:   if (a->inode.use && a->inode.checked) {
1470:     PetscCall(MatMult_SeqAIJ_Inode(A, xx, yy));
1471:     PetscFunctionReturn(PETSC_SUCCESS);
1472:   }
1473:   PetscCall(MatSeqAIJGetArrayRead(A, &a_a));
1474:   PetscCall(VecGetArrayRead(xx, &x));
1475:   PetscCall(VecGetArray(yy, &y));
1476:   ii = a->i;
1477:   if (usecprow) { /* use compressed row format */
1478:     PetscCall(PetscArrayzero(y, m));
1479:     m    = a->compressedrow.nrows;
1480:     ii   = a->compressedrow.i;
1481:     ridx = a->compressedrow.rindex;
1482:     PetscPragmaUseOMPKernels(parallel for)
1483:     for (PetscInt i = 0; i < m; i++) {
1484:       PetscInt           n   = ii[i + 1] - ii[i];
1485:       const PetscInt    *aj  = a->j + ii[i];
1486:       const PetscScalar *aa  = a_a + ii[i];
1487:       PetscScalar        sum = 0.0;
1488:       PetscSparseDensePlusDot(sum, x, aa, aj, n);
1489:       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
1490:       y[ridx[i]] = sum;
1491:     }
1492:   } else { /* do not use compressed row format */
1493: #if PetscDefined(USE_FORTRAN_KERNEL_MULTAIJ)
1494:     fortranmultaij_(&m, x, ii, a->j, a_a, y);
1495: #else
1496:     PetscPragmaUseOMPKernels(parallel for)
1497:     for (PetscInt i = 0; i < m; i++) {
1498:       PetscInt           n   = ii[i + 1] - ii[i];
1499:       const PetscInt    *aj  = a->j + ii[i];
1500:       const PetscScalar *aa  = a_a + ii[i];
1501:       PetscScalar        sum = 0.0;
1502:       PetscSparseDensePlusDot(sum, x, aa, aj, n);
1503:       y[i] = sum;
1504:     }
1505: #endif
1506:   }
1507:   PetscCall(PetscLogFlops(2.0 * a->nz - a->nonzerorowcnt));
1508:   PetscCall(VecRestoreArrayRead(xx, &x));
1509:   PetscCall(VecRestoreArray(yy, &y));
1510:   PetscCall(MatSeqAIJRestoreArrayRead(A, &a_a));
1511:   PetscFunctionReturn(PETSC_SUCCESS);
1512: }

1514: // HACK!!!!! Used by src/mat/tests/ex170.c
1515: PETSC_EXTERN PetscErrorCode MatMultMax_SeqAIJ(Mat A, Vec xx, Vec yy)
1516: {
1517:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data;
1518:   PetscScalar       *y;
1519:   const PetscScalar *x;
1520:   const MatScalar   *aa, *a_a;
1521:   PetscInt           m = A->rmap->n;
1522:   const PetscInt    *aj, *ii, *ridx   = NULL;
1523:   PetscInt           n, i, nonzerorow = 0;
1524:   PetscScalar        sum;
1525:   PetscBool          usecprow = a->compressedrow.use;

1527: #if PetscDefined(HAVE_PRAGMA_DISJOINT)
1528:   #pragma disjoint(*x, *y, *aa)
1529: #endif

1531:   PetscFunctionBegin;
1532:   PetscCall(MatSeqAIJGetArrayRead(A, &a_a));
1533:   PetscCall(VecGetArrayRead(xx, &x));
1534:   PetscCall(VecGetArray(yy, &y));
1535:   if (usecprow) { /* use compressed row format */
1536:     m    = a->compressedrow.nrows;
1537:     ii   = a->compressedrow.i;
1538:     ridx = a->compressedrow.rindex;
1539:     for (i = 0; i < m; i++) {
1540:       n   = ii[i + 1] - ii[i];
1541:       aj  = a->j + ii[i];
1542:       aa  = a_a + ii[i];
1543:       sum = 0.0;
1544:       nonzerorow += (n > 0);
1545:       PetscSparseDenseMaxDot(sum, x, aa, aj, n);
1546:       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
1547:       y[*ridx++] = sum;
1548:     }
1549:   } else { /* do not use compressed row format */
1550:     ii = a->i;
1551:     for (i = 0; i < m; i++) {
1552:       n   = ii[i + 1] - ii[i];
1553:       aj  = a->j + ii[i];
1554:       aa  = a_a + ii[i];
1555:       sum = 0.0;
1556:       nonzerorow += (n > 0);
1557:       PetscSparseDenseMaxDot(sum, x, aa, aj, n);
1558:       y[i] = sum;
1559:     }
1560:   }
1561:   PetscCall(PetscLogFlops(2.0 * a->nz - nonzerorow));
1562:   PetscCall(VecRestoreArrayRead(xx, &x));
1563:   PetscCall(VecRestoreArray(yy, &y));
1564:   PetscCall(MatSeqAIJRestoreArrayRead(A, &a_a));
1565:   PetscFunctionReturn(PETSC_SUCCESS);
1566: }

1568: // HACK!!!!! Used by src/mat/tests/ex170.c
1569: PETSC_EXTERN PetscErrorCode MatMultAddMax_SeqAIJ(Mat A, Vec xx, Vec yy, Vec zz)
1570: {
1571:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data;
1572:   PetscScalar       *y, *z;
1573:   const PetscScalar *x;
1574:   const MatScalar   *aa, *a_a;
1575:   PetscInt           m = A->rmap->n, *aj, *ii;
1576:   PetscInt           n, i, *ridx = NULL;
1577:   PetscScalar        sum;
1578:   PetscBool          usecprow = a->compressedrow.use;

1580:   PetscFunctionBegin;
1581:   PetscCall(MatSeqAIJGetArrayRead(A, &a_a));
1582:   PetscCall(VecGetArrayRead(xx, &x));
1583:   PetscCall(VecGetArrayPair(yy, zz, &y, &z));
1584:   if (usecprow) { /* use compressed row format */
1585:     if (zz != yy) PetscCall(PetscArraycpy(z, y, m));
1586:     m    = a->compressedrow.nrows;
1587:     ii   = a->compressedrow.i;
1588:     ridx = a->compressedrow.rindex;
1589:     for (i = 0; i < m; i++) {
1590:       n   = ii[i + 1] - ii[i];
1591:       aj  = a->j + ii[i];
1592:       aa  = a_a + ii[i];
1593:       sum = y[*ridx];
1594:       PetscSparseDenseMaxDot(sum, x, aa, aj, n);
1595:       z[*ridx++] = sum;
1596:     }
1597:   } else { /* do not use compressed row format */
1598:     ii = a->i;
1599:     for (i = 0; i < m; i++) {
1600:       n   = ii[i + 1] - ii[i];
1601:       aj  = a->j + ii[i];
1602:       aa  = a_a + ii[i];
1603:       sum = y[i];
1604:       PetscSparseDenseMaxDot(sum, x, aa, aj, n);
1605:       z[i] = sum;
1606:     }
1607:   }
1608:   PetscCall(PetscLogFlops(2.0 * a->nz));
1609:   PetscCall(VecRestoreArrayRead(xx, &x));
1610:   PetscCall(VecRestoreArrayPair(yy, zz, &y, &z));
1611:   PetscCall(MatSeqAIJRestoreArrayRead(A, &a_a));
1612:   PetscFunctionReturn(PETSC_SUCCESS);
1613: }

1615: #include <../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h>
1616: PetscErrorCode MatMultAdd_SeqAIJ(Mat A, Vec xx, Vec yy, Vec zz)
1617: {
1618:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data;
1619:   PetscScalar       *y, *z;
1620:   const PetscScalar *x;
1621:   const MatScalar   *a_a;
1622:   const PetscInt    *ii, *ridx = NULL;
1623:   PetscInt           m        = A->rmap->n;
1624:   PetscBool          usecprow = a->compressedrow.use;

1626:   PetscFunctionBegin;
1627:   if (a->inode.use && a->inode.checked) {
1628:     PetscCall(MatMultAdd_SeqAIJ_Inode(A, xx, yy, zz));
1629:     PetscFunctionReturn(PETSC_SUCCESS);
1630:   }
1631:   PetscCall(MatSeqAIJGetArrayRead(A, &a_a));
1632:   PetscCall(VecGetArrayRead(xx, &x));
1633:   PetscCall(VecGetArrayPair(yy, zz, &y, &z));
1634:   if (usecprow) { /* use compressed row format */
1635:     if (zz != yy) PetscCall(PetscArraycpy(z, y, m));
1636:     m    = a->compressedrow.nrows;
1637:     ii   = a->compressedrow.i;
1638:     ridx = a->compressedrow.rindex;
1639:     for (PetscInt i = 0; i < m; i++) {
1640:       PetscInt           n   = ii[i + 1] - ii[i];
1641:       const PetscInt    *aj  = a->j + ii[i];
1642:       const PetscScalar *aa  = a_a + ii[i];
1643:       PetscScalar        sum = y[*ridx];
1644:       PetscSparseDensePlusDot(sum, x, aa, aj, n);
1645:       z[*ridx++] = sum;
1646:     }
1647:   } else { /* do not use compressed row format */
1648:     ii = a->i;
1649: #if PetscDefined(USE_FORTRAN_KERNEL_MULTADDAIJ)
1650:     fortranmultaddaij_(&m, x, ii, a->j, a_a, y, z);
1651: #else
1652:     PetscPragmaUseOMPKernels(parallel for)
1653:     for (PetscInt i = 0; i < m; i++) {
1654:       PetscInt           n   = ii[i + 1] - ii[i];
1655:       const PetscInt    *aj  = a->j + ii[i];
1656:       const PetscScalar *aa  = a_a + ii[i];
1657:       PetscScalar        sum = y[i];
1658:       PetscSparseDensePlusDot(sum, x, aa, aj, n);
1659:       z[i] = sum;
1660:     }
1661: #endif
1662:   }
1663:   PetscCall(PetscLogFlops(2.0 * a->nz));
1664:   PetscCall(VecRestoreArrayRead(xx, &x));
1665:   PetscCall(VecRestoreArrayPair(yy, zz, &y, &z));
1666:   PetscCall(MatSeqAIJRestoreArrayRead(A, &a_a));
1667:   PetscFunctionReturn(PETSC_SUCCESS);
1668: }

1670: static PetscErrorCode MatShift_SeqAIJ(Mat A, PetscScalar v)
1671: {
1672:   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data;
1673:   const PetscInt *diag;
1674:   const PetscInt *ii    = (const PetscInt *)a->i;
1675:   const PetscInt  ndiag = PetscMin(A->rmap->n, A->cmap->n);
1676:   PetscBool       diagDense, hasDiag;

1678:   PetscFunctionBegin;
1679:   /* If A is preallocated but has no nonzeros, preallocate it again only if it is not 0 x n or m x 0, otherwise the nonzero state
1680:      would increase even though the shift creates no new nonzeros */
1681:   if (!A->preallocated || (!a->nz && ndiag > 0)) {
1682:     PetscCall(MatSeqAIJSetPreallocation(A, 1, NULL));
1683:     PetscCall(MatShift_Basic(A, v));
1684:     PetscFunctionReturn(PETSC_SUCCESS);
1685:   }

1687:   PetscCall(MatGetDiagonalMarkers_SeqAIJ(A, &diag, &diagDense));
1688:   /* Reallocate A only if a diagonal entry is missing, otherwise the nonzero state would increase even though the shift creates no
1689:      new nonzeros. diagDense alone cannot tell, as it also counts the rows past the last column, which cannot have a diagonal entry */
1690:   hasDiag = diagDense;
1691:   if (!hasDiag) {
1692:     hasDiag = PETSC_TRUE;
1693:     for (PetscInt i = 0; i < ndiag && hasDiag; i++) hasDiag = (PetscBool)(diag[i] < ii[i + 1]);
1694:   }
1695:   if (hasDiag) {
1696:     PetscScalar *Aa;

1698:     PetscCall(MatSeqAIJGetArray(A, &Aa));
1699:     for (PetscInt i = 0; i < ndiag; i++) Aa[diag[i]] += v;
1700:     PetscCall(MatSeqAIJRestoreArray(A, &Aa));
1701:   } else {
1702:     PetscScalar       *olda = a->a; /* preserve pointers to current matrix nonzeros structure and values */
1703:     PetscInt          *oldj = a->j, *oldi = a->i;
1704:     PetscBool          free_a = a->free_a, free_ij = a->free_ij;
1705:     const PetscScalar *Aa;
1706:     PetscInt          *mdiag = NULL;

1708:     PetscCall(PetscCalloc1(A->rmap->n, &mdiag));
1709:     for (PetscInt i = 0; i < A->rmap->n; i++) {
1710:       if (i < A->cmap->n && diag[i] >= ii[i + 1]) { /* 'out of range' rows never have diagonals */
1711:         mdiag[i] = 1;
1712:       }
1713:     }
1714:     PetscCall(MatSeqAIJGetArrayRead(A, &Aa)); // sync the host
1715:     PetscCall(MatSeqAIJRestoreArrayRead(A, &Aa));

1717:     a->a = NULL;
1718:     a->j = NULL;
1719:     a->i = NULL;
1720:     /* increase the values in imax for each row where a diagonal is being inserted then reallocate the matrix data structures */
1721:     for (PetscInt i = 0; i < PetscMin(A->rmap->n, A->cmap->n); i++) a->imax[i] += mdiag[i];
1722:     PetscCall(MatSeqAIJSetPreallocation_SeqAIJ(A, 0, a->imax));

1724:     /* copy old values into new matrix data structure */
1725:     for (PetscInt i = 0; i < A->rmap->n; i++) {
1726:       PetscCall(MatSetValues(A, 1, &i, a->imax[i] - mdiag[i], &oldj[oldi[i]], &olda[oldi[i]], ADD_VALUES));
1727:       if (i < A->cmap->n) PetscCall(MatSetValue(A, i, i, v, ADD_VALUES));
1728:     }
1729:     PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
1730:     PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
1731:     if (free_a) PetscCall(PetscShmgetDeallocateArray((void **)&olda));
1732:     if (free_ij) PetscCall(PetscShmgetDeallocateArray((void **)&oldj));
1733:     if (free_ij) PetscCall(PetscShmgetDeallocateArray((void **)&oldi));
1734:     PetscCall(PetscFree(mdiag));
1735:   }
1736:   PetscFunctionReturn(PETSC_SUCCESS);
1737: }

1739: #include <petscblaslapack.h>
1740: #include <petsc/private/kernels/blockinvert.h>

1742: /*
1743:     Note that values is allocated externally by the PC and then passed into this routine
1744: */
1745: static PetscErrorCode MatInvertVariableBlockDiagonal_SeqAIJ(Mat A, PetscInt nblocks, const PetscInt *bsizes, PetscScalar *diag)
1746: {
1747:   PetscInt        n = A->rmap->n, i, ncnt = 0, *indx, j, bsizemax = 0, *v_pivots;
1748:   PetscBool       allowzeropivot, zeropivotdetected = PETSC_FALSE;
1749:   const PetscReal shift = 0.0;
1750:   PetscInt        ipvt[5];
1751:   PetscCount      flops = 0;
1752:   PetscScalar     work[25], *v_work;

1754:   PetscFunctionBegin;
1755:   allowzeropivot = PetscNot(A->erroriffailure);
1756:   for (i = 0; i < nblocks; i++) ncnt += bsizes[i];
1757:   PetscCheck(ncnt == n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Total blocksizes %" PetscInt_FMT " doesn't match number matrix rows %" PetscInt_FMT, ncnt, n);
1758:   for (i = 0; i < nblocks; i++) bsizemax = PetscMax(bsizemax, bsizes[i]);
1759:   PetscCall(PetscMalloc1(bsizemax, &indx));
1760:   if (bsizemax > 7) PetscCall(PetscMalloc2(bsizemax, &v_work, bsizemax, &v_pivots));
1761:   ncnt = 0;
1762:   for (i = 0; i < nblocks; i++) {
1763:     for (j = 0; j < bsizes[i]; j++) indx[j] = ncnt + j;
1764:     PetscCall(MatGetValues(A, bsizes[i], indx, bsizes[i], indx, diag));
1765:     switch (bsizes[i]) {
1766:     case 1:
1767:       *diag = 1.0 / (*diag);
1768:       break;
1769:     case 2:
1770:       PetscCall(PetscKernel_A_gets_inverse_A_2(diag, shift, allowzeropivot, &zeropivotdetected));
1771:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1772:       PetscCall(PetscKernel_A_gets_transpose_A_2(diag));
1773:       break;
1774:     case 3:
1775:       PetscCall(PetscKernel_A_gets_inverse_A_3(diag, shift, allowzeropivot, &zeropivotdetected));
1776:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1777:       PetscCall(PetscKernel_A_gets_transpose_A_3(diag));
1778:       break;
1779:     case 4:
1780:       PetscCall(PetscKernel_A_gets_inverse_A_4(diag, shift, allowzeropivot, &zeropivotdetected));
1781:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1782:       PetscCall(PetscKernel_A_gets_transpose_A_4(diag));
1783:       break;
1784:     case 5:
1785:       PetscCall(PetscKernel_A_gets_inverse_A_5(diag, ipvt, work, shift, allowzeropivot, &zeropivotdetected));
1786:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1787:       PetscCall(PetscKernel_A_gets_transpose_A_5(diag));
1788:       break;
1789:     case 6:
1790:       PetscCall(PetscKernel_A_gets_inverse_A_6(diag, shift, allowzeropivot, &zeropivotdetected));
1791:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1792:       PetscCall(PetscKernel_A_gets_transpose_A_6(diag));
1793:       break;
1794:     case 7:
1795:       PetscCall(PetscKernel_A_gets_inverse_A_7(diag, shift, allowzeropivot, &zeropivotdetected));
1796:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1797:       PetscCall(PetscKernel_A_gets_transpose_A_7(diag));
1798:       break;
1799:     default:
1800:       PetscCall(PetscKernel_A_gets_inverse_A(bsizes[i], diag, v_pivots, v_work, allowzeropivot, &zeropivotdetected));
1801:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1802:       PetscCall(PetscKernel_A_gets_transpose_A_N(diag, bsizes[i]));
1803:     }
1804:     ncnt += bsizes[i];
1805:     diag += bsizes[i] * bsizes[i];
1806:     flops += 2 * PetscPowInt64(bsizes[i], 3) / 3;
1807:   }
1808:   PetscCall(PetscLogFlops(flops));
1809:   if (bsizemax > 7) PetscCall(PetscFree2(v_work, v_pivots));
1810:   PetscCall(PetscFree(indx));
1811:   PetscFunctionReturn(PETSC_SUCCESS);
1812: }

1814: /*
1815:    Negative shift indicates do not generate an error if there is a zero diagonal, just invert it anyways
1816: */
1817: static PetscErrorCode MatInvertDiagonalForSOR_SeqAIJ(Mat A, PetscScalar omega, PetscScalar fshift)
1818: {
1819:   Mat_SeqAIJ      *a = (Mat_SeqAIJ *)A->data;
1820:   PetscInt         i, m = A->rmap->n;
1821:   const MatScalar *v;
1822:   PetscScalar     *idiag, *mdiag;
1823:   PetscBool        diagDense;
1824:   const PetscInt  *diag;

1826:   PetscFunctionBegin;
1827:   if (a->idiagState == ((PetscObject)A)->state && a->omega == omega && a->fshift == fshift) PetscFunctionReturn(PETSC_SUCCESS);
1828:   PetscCall(MatGetDiagonalMarkers_SeqAIJ(A, &diag, &diagDense));
1829:   PetscCheck(diagDense, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Matrix must have all diagonal locations to invert them");
1830:   if (!a->idiag) PetscCall(PetscMalloc3(m, &a->idiag, m, &a->mdiag, m, &a->ssor_work));

1832:   mdiag = a->mdiag;
1833:   idiag = a->idiag;
1834:   PetscCall(MatSeqAIJGetArrayRead(A, &v));
1835:   if (omega == 1.0 && PetscRealPart(fshift) <= 0.0) {
1836:     for (i = 0; i < m; i++) {
1837:       mdiag[i] = v[diag[i]];
1838:       if (!PetscAbsScalar(mdiag[i])) { /* zero diagonal */
1839:         PetscCheck(PetscRealPart(fshift), PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Zero diagonal on row %" PetscInt_FMT, i);
1840:         PetscCall(PetscInfo(A, "Zero diagonal on row %" PetscInt_FMT "\n", i));
1841:         A->factorerrortype             = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1842:         A->factorerror_zeropivot_value = 0.0;
1843:         A->factorerror_zeropivot_row   = i;
1844:       }
1845:       idiag[i] = 1.0 / v[diag[i]];
1846:     }
1847:     PetscCall(PetscLogFlops(m));
1848:   } else {
1849:     for (i = 0; i < m; i++) {
1850:       mdiag[i] = v[diag[i]];
1851:       idiag[i] = omega / (fshift + v[diag[i]]);
1852:     }
1853:     PetscCall(PetscLogFlops(2.0 * m));
1854:   }
1855:   PetscCall(MatSeqAIJRestoreArrayRead(A, &v));
1856:   a->idiagState = ((PetscObject)A)->state;
1857:   a->omega      = omega;
1858:   a->fshift     = fshift;
1859:   PetscFunctionReturn(PETSC_SUCCESS);
1860: }

1862: PetscErrorCode MatSOR_SeqAIJ(Mat A, Vec bb, PetscReal omega, MatSORType flag, PetscReal fshift, PetscInt its, PetscInt lits, Vec xx)
1863: {
1864:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data;
1865:   PetscScalar       *x, d, sum, *t, scale;
1866:   const MatScalar   *v, *idiag = NULL, *mdiag, *aa;
1867:   const PetscScalar *b, *bs, *xb, *ts;
1868:   PetscInt           n, m = A->rmap->n, i;
1869:   const PetscInt    *idx, *diag;

1871:   PetscFunctionBegin;
1872:   if (a->inode.use && a->inode.checked && omega == 1.0 && fshift == 0.0) {
1873:     PetscCall(MatSOR_SeqAIJ_Inode(A, bb, omega, flag, fshift, its, lits, xx));
1874:     PetscFunctionReturn(PETSC_SUCCESS);
1875:   }
1876:   its = its * lits;
1877:   PetscCall(MatInvertDiagonalForSOR_SeqAIJ(A, omega, fshift));
1878:   PetscCall(MatGetDiagonalMarkers_SeqAIJ(A, &diag, NULL));
1879:   t     = a->ssor_work;
1880:   idiag = a->idiag;
1881:   mdiag = a->mdiag;

1883:   PetscCall(MatSeqAIJGetArrayRead(A, &aa));
1884:   PetscCall(VecGetArray(xx, &x));
1885:   PetscCall(VecGetArrayRead(bb, &b));
1886:   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
1887:   if (flag == SOR_APPLY_UPPER) {
1888:     /* apply (U + D/omega) to the vector */
1889:     bs = b;
1890:     for (i = 0; i < m; i++) {
1891:       d   = fshift + mdiag[i];
1892:       n   = a->i[i + 1] - diag[i] - 1;
1893:       idx = a->j + diag[i] + 1;
1894:       v   = aa + diag[i] + 1;
1895:       sum = b[i] * d / omega;
1896:       PetscSparseDensePlusDot(sum, bs, v, idx, n);
1897:       x[i] = sum;
1898:     }
1899:     PetscCall(VecRestoreArray(xx, &x));
1900:     PetscCall(VecRestoreArrayRead(bb, &b));
1901:     PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
1902:     PetscCall(PetscLogFlops(a->nz));
1903:     PetscFunctionReturn(PETSC_SUCCESS);
1904:   }

1906:   PetscCheck(flag != SOR_APPLY_LOWER, PETSC_COMM_SELF, PETSC_ERR_SUP, "SOR_APPLY_LOWER is not implemented");
1907:   if (flag & SOR_EISENSTAT) {
1908:     /* Let  A = L + U + D; where L is lower triangular,
1909:     U is upper triangular, E = D/omega; This routine applies

1911:             (L + E)^{-1} A (U + E)^{-1}

1913:     to a vector efficiently using Eisenstat's trick.
1914:     */
1915:     scale = (2.0 / omega) - 1.0;

1917:     /*  x = (E + U)^{-1} b */
1918:     for (i = m - 1; i >= 0; i--) {
1919:       n   = a->i[i + 1] - diag[i] - 1;
1920:       idx = a->j + diag[i] + 1;
1921:       v   = aa + diag[i] + 1;
1922:       sum = b[i];
1923:       PetscSparseDenseMinusDot(sum, x, v, idx, n);
1924:       x[i] = sum * idiag[i];
1925:     }

1927:     /*  t = b - (2*E - D)x */
1928:     v = aa;
1929:     for (i = 0; i < m; i++) t[i] = b[i] - scale * v[*diag++] * x[i];

1931:     /*  t = (E + L)^{-1}t */
1932:     ts   = t;
1933:     diag = a->diag;
1934:     for (i = 0; i < m; i++) {
1935:       n   = diag[i] - a->i[i];
1936:       idx = a->j + a->i[i];
1937:       v   = aa + a->i[i];
1938:       sum = t[i];
1939:       PetscSparseDenseMinusDot(sum, ts, v, idx, n);
1940:       t[i] = sum * idiag[i];
1941:       /*  x = x + t */
1942:       x[i] += t[i];
1943:     }

1945:     PetscCall(PetscLogFlops(6.0 * m - 1 + 2.0 * a->nz));
1946:     PetscCall(VecRestoreArray(xx, &x));
1947:     PetscCall(VecRestoreArrayRead(bb, &b));
1948:     PetscFunctionReturn(PETSC_SUCCESS);
1949:   }
1950:   if (flag & SOR_ZERO_INITIAL_GUESS) {
1951:     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
1952:       for (i = 0; i < m; i++) {
1953:         n   = diag[i] - a->i[i];
1954:         idx = a->j + a->i[i];
1955:         v   = aa + a->i[i];
1956:         sum = b[i];
1957:         PetscSparseDenseMinusDot(sum, x, v, idx, n);
1958:         t[i] = sum;
1959:         x[i] = sum * idiag[i];
1960:       }
1961:       xb = t;
1962:       PetscCall(PetscLogFlops(a->nz));
1963:     } else xb = b;
1964:     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
1965:       for (i = m - 1; i >= 0; i--) {
1966:         n   = a->i[i + 1] - diag[i] - 1;
1967:         idx = a->j + diag[i] + 1;
1968:         v   = aa + diag[i] + 1;
1969:         sum = xb[i];
1970:         PetscSparseDenseMinusDot(sum, x, v, idx, n);
1971:         if (xb == b) {
1972:           x[i] = sum * idiag[i];
1973:         } else {
1974:           x[i] = (1 - omega) * x[i] + sum * idiag[i]; /* omega in idiag */
1975:         }
1976:       }
1977:       PetscCall(PetscLogFlops(a->nz)); /* assumes 1/2 in upper */
1978:     }
1979:     its--;
1980:   }
1981:   while (its--) {
1982:     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
1983:       for (i = 0; i < m; i++) {
1984:         /* lower */
1985:         n   = diag[i] - a->i[i];
1986:         idx = a->j + a->i[i];
1987:         v   = aa + a->i[i];
1988:         sum = b[i];
1989:         PetscSparseDenseMinusDot(sum, x, v, idx, n);
1990:         t[i] = sum; /* save application of the lower-triangular part */
1991:         /* upper */
1992:         n   = a->i[i + 1] - diag[i] - 1;
1993:         idx = a->j + diag[i] + 1;
1994:         v   = aa + diag[i] + 1;
1995:         PetscSparseDenseMinusDot(sum, x, v, idx, n);
1996:         x[i] = (1. - omega) * x[i] + sum * idiag[i]; /* omega in idiag */
1997:       }
1998:       xb = t;
1999:       PetscCall(PetscLogFlops(2.0 * a->nz));
2000:     } else xb = b;
2001:     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
2002:       for (i = m - 1; i >= 0; i--) {
2003:         sum = xb[i];
2004:         if (xb == b) {
2005:           /* whole matrix (no checkpointing available) */
2006:           n   = a->i[i + 1] - a->i[i];
2007:           idx = a->j + a->i[i];
2008:           v   = aa + a->i[i];
2009:           PetscSparseDenseMinusDot(sum, x, v, idx, n);
2010:           x[i] = (1. - omega) * x[i] + (sum + mdiag[i] * x[i]) * idiag[i];
2011:         } else { /* lower-triangular part has been saved, so only apply upper-triangular */
2012:           n   = a->i[i + 1] - diag[i] - 1;
2013:           idx = a->j + diag[i] + 1;
2014:           v   = aa + diag[i] + 1;
2015:           PetscSparseDenseMinusDot(sum, x, v, idx, n);
2016:           x[i] = (1. - omega) * x[i] + sum * idiag[i]; /* omega in idiag */
2017:         }
2018:       }
2019:       if (xb == b) PetscCall(PetscLogFlops(2.0 * a->nz));
2020:       else PetscCall(PetscLogFlops(a->nz)); /* assumes 1/2 in upper */
2021:     }
2022:   }
2023:   PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
2024:   PetscCall(VecRestoreArray(xx, &x));
2025:   PetscCall(VecRestoreArrayRead(bb, &b));
2026:   PetscFunctionReturn(PETSC_SUCCESS);
2027: }

2029: static PetscErrorCode MatGetInfo_SeqAIJ(Mat A, MatInfoType flag, MatInfo *info)
2030: {
2031:   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;

2033:   PetscFunctionBegin;
2034:   info->block_size   = 1.0;
2035:   info->nz_allocated = a->maxnz;
2036:   info->nz_used      = a->nz;
2037:   info->nz_unneeded  = (a->maxnz - a->nz);
2038:   info->assemblies   = A->num_ass;
2039:   info->mallocs      = A->info.mallocs;
2040:   info->memory       = 0; /* REVIEW ME */
2041:   if (A->factortype) {
2042:     info->fill_ratio_given  = A->info.fill_ratio_given;
2043:     info->fill_ratio_needed = A->info.fill_ratio_needed;
2044:     info->factor_mallocs    = A->info.factor_mallocs;
2045:   } else {
2046:     info->fill_ratio_given  = 0;
2047:     info->fill_ratio_needed = 0;
2048:     info->factor_mallocs    = 0;
2049:   }
2050:   PetscFunctionReturn(PETSC_SUCCESS);
2051: }

2053: static PetscErrorCode MatZeroRows_SeqAIJ(Mat A, PetscInt N, const PetscInt rows[], PetscScalar diagv, Vec x, Vec b)
2054: {
2055:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data;
2056:   PetscInt           i, m = A->rmap->n - 1;
2057:   const PetscScalar *xx;
2058:   PetscScalar       *bb, *aa;
2059:   PetscInt           d = 0;
2060:   const PetscInt    *diag;

2062:   PetscFunctionBegin;
2063:   if (x && b) {
2064:     PetscCall(VecGetArrayRead(x, &xx));
2065:     PetscCall(VecGetArray(b, &bb));
2066:     for (i = 0; i < N; i++) {
2067:       PetscCheck(rows[i] >= 0 && rows[i] <= m, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "row %" PetscInt_FMT " out of range", rows[i]);
2068:       if (rows[i] >= A->cmap->n) continue;
2069:       bb[rows[i]] = diagv * xx[rows[i]];
2070:     }
2071:     PetscCall(VecRestoreArrayRead(x, &xx));
2072:     PetscCall(VecRestoreArray(b, &bb));
2073:   }

2075:   PetscCall(MatGetDiagonalMarkers_SeqAIJ(A, &diag, NULL));
2076:   PetscCall(MatSeqAIJGetArray(A, &aa));
2077:   if (a->keepnonzeropattern) {
2078:     for (i = 0; i < N; i++) {
2079:       PetscCheck(rows[i] >= 0 && rows[i] <= m, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "row %" PetscInt_FMT " out of range", rows[i]);
2080:       PetscCall(PetscArrayzero(&aa[a->i[rows[i]]], a->ilen[rows[i]]));
2081:     }
2082:     if (diagv != 0.0) {
2083:       for (i = 0; i < N; i++) {
2084:         d = rows[i];
2085:         if (d >= A->cmap->n) continue;
2086:         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);
2087:         aa[diag[d]] = diagv;
2088:       }
2089:     }
2090:   } else {
2091:     if (diagv != 0.0) {
2092:       for (i = 0; i < N; i++) {
2093:         PetscCheck(rows[i] >= 0 && rows[i] <= m, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "row %" PetscInt_FMT " out of range", rows[i]);
2094:         if (a->ilen[rows[i]] > 0) {
2095:           if (rows[i] >= A->cmap->n) {
2096:             a->ilen[rows[i]] = 0;
2097:           } else {
2098:             a->ilen[rows[i]]    = 1;
2099:             aa[a->i[rows[i]]]   = diagv;
2100:             a->j[a->i[rows[i]]] = rows[i];
2101:           }
2102:         } else if (rows[i] < A->cmap->n) { /* in case row was completely empty */
2103:           PetscCall(MatSetValues_SeqAIJ(A, 1, &rows[i], 1, &rows[i], &diagv, INSERT_VALUES));
2104:         }
2105:       }
2106:     } else {
2107:       for (i = 0; i < N; i++) {
2108:         PetscCheck(rows[i] >= 0 && rows[i] <= m, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "row %" PetscInt_FMT " out of range", rows[i]);
2109:         a->ilen[rows[i]] = 0;
2110:       }
2111:     }
2112:     if (N) A->nonzerostate++;
2113:   }
2114:   PetscCall(MatSeqAIJRestoreArray(A, &aa));
2115:   PetscUseTypeMethod(A, assemblyend, MAT_FINAL_ASSEMBLY);
2116:   PetscFunctionReturn(PETSC_SUCCESS);
2117: }

2119: static PetscErrorCode MatZeroRowsColumns_SeqAIJ(Mat A, PetscInt N, const PetscInt rows[], PetscScalar diagv, Vec x, Vec b)
2120: {
2121:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data;
2122:   PetscInt           i, j, m = A->rmap->n - 1;
2123:   PetscBool         *zeroed, vecs = PETSC_FALSE;
2124:   const PetscScalar *xx;
2125:   PetscScalar       *bb, *aa;
2126:   const PetscInt    *diag;
2127:   PetscBool          diagDense;

2129:   PetscFunctionBegin;
2130:   if (!N) PetscFunctionReturn(PETSC_SUCCESS);
2131:   PetscCall(MatGetDiagonalMarkers_SeqAIJ(A, &diag, &diagDense));
2132:   PetscCall(MatSeqAIJGetArray(A, &aa));
2133:   if (x && b) {
2134:     PetscCall(VecGetArrayRead(x, &xx));
2135:     PetscCall(VecGetArray(b, &bb));
2136:     vecs = PETSC_TRUE;
2137:   }
2138:   PetscCall(PetscCalloc1(A->rmap->n, &zeroed));
2139:   for (i = 0; i < N; i++) {
2140:     PetscCheck(rows[i] >= 0 && rows[i] <= m, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "row %" PetscInt_FMT " out of range", rows[i]);
2141:     PetscCall(PetscArrayzero(PetscSafePointerPlusOffset(aa, a->i[rows[i]]), a->ilen[rows[i]]));

2143:     zeroed[rows[i]] = PETSC_TRUE;
2144:   }
2145:   for (i = 0; i < A->rmap->n; i++) {
2146:     if (!zeroed[i]) {
2147:       for (j = a->i[i]; j < a->i[i + 1]; j++) {
2148:         if (a->j[j] < A->rmap->n && zeroed[a->j[j]]) {
2149:           if (vecs) bb[i] -= aa[j] * xx[a->j[j]];
2150:           aa[j] = 0.0;
2151:         }
2152:       }
2153:     } else if (vecs && i < A->cmap->N) bb[i] = diagv * xx[i];
2154:   }
2155:   if (x && b) {
2156:     PetscCall(VecRestoreArrayRead(x, &xx));
2157:     PetscCall(VecRestoreArray(b, &bb));
2158:   }
2159:   PetscCall(PetscFree(zeroed));
2160:   if (diagv != 0.0) {
2161:     if (!diagDense) {
2162:       for (i = 0; i < N; i++) {
2163:         if (rows[i] >= A->cmap->N || rows[i] < 0) continue;
2164:         PetscCall(MatSetValues_SeqAIJ(A, 1, &rows[i], 1, &rows[i], &diagv, INSERT_VALUES));
2165:       }
2166:     } else {
2167:       for (i = 0; i < N; i++) aa[diag[rows[i]]] = diagv;
2168:     }
2169:   }
2170:   PetscCall(MatSeqAIJRestoreArray(A, &aa));
2171:   if (!diagDense) PetscUseTypeMethod(A, assemblyend, MAT_FINAL_ASSEMBLY);
2172:   PetscFunctionReturn(PETSC_SUCCESS);
2173: }

2175: PetscErrorCode MatGetRow_SeqAIJ(Mat A, PetscInt row, PetscInt *nz, PetscInt **idx, PetscScalar **v)
2176: {
2177:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data;
2178:   const PetscScalar *aa;

2180:   PetscFunctionBegin;
2181:   PetscCall(MatSeqAIJGetArrayRead(A, &aa));
2182:   *nz = a->i[row + 1] - a->i[row];
2183:   if (v) *v = PetscSafePointerPlusOffset((PetscScalar *)aa, a->i[row]);
2184:   if (idx) {
2185:     if (*nz && a->j) *idx = a->j + a->i[row];
2186:     else *idx = NULL;
2187:   }
2188:   PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
2189:   PetscFunctionReturn(PETSC_SUCCESS);
2190: }

2192: PetscErrorCode MatRestoreRow_SeqAIJ(Mat A, PetscInt row, PetscInt *nz, PetscInt **idx, PetscScalar **v)
2193: {
2194:   PetscFunctionBegin;
2195:   PetscFunctionReturn(PETSC_SUCCESS);
2196: }

2198: static PetscErrorCode MatNorm_SeqAIJ(Mat A, NormType type, PetscReal *nrm)
2199: {
2200:   Mat_SeqAIJ      *a = (Mat_SeqAIJ *)A->data;
2201:   const MatScalar *v;
2202:   PetscReal        sum = 0.0;
2203:   PetscInt         i, j;

2205:   PetscFunctionBegin;
2206:   PetscCall(MatSeqAIJGetArrayRead(A, &v));
2207:   if (type == NORM_FROBENIUS) {
2208: #if PetscDefined(USE_REAL___FP16)
2209:     PetscBLASInt one = 1, nz = a->nz;
2210:     PetscCallBLAS("BLASnrm2", *nrm = BLASnrm2_(&nz, v, &one));
2211: #else
2212:     for (i = 0; i < a->nz; i++) {
2213:       sum += PetscRealPart(PetscConj(*v) * (*v));
2214:       v++;
2215:     }
2216:     *nrm = PetscSqrtReal(sum);
2217: #endif
2218:     PetscCall(PetscLogFlops(2.0 * a->nz));
2219:   } else if (type == NORM_1) {
2220:     PetscReal *tmp;
2221:     PetscInt  *jj = a->j;
2222:     PetscCall(PetscCalloc1(A->cmap->n, &tmp));
2223:     *nrm = 0.0;
2224:     for (j = 0; j < a->nz; j++) {
2225:       tmp[*jj++] += PetscAbsScalar(*v);
2226:       v++;
2227:     }
2228:     for (j = 0; j < A->cmap->n; j++) {
2229:       if (tmp[j] > *nrm) *nrm = tmp[j];
2230:     }
2231:     PetscCall(PetscFree(tmp));
2232:     PetscCall(PetscLogFlops(a->nz));
2233:   } else if (type == NORM_INFINITY) {
2234:     *nrm = 0.0;
2235:     for (j = 0; j < A->rmap->n; j++) {
2236:       const PetscScalar *v2 = PetscSafePointerPlusOffset(v, a->i[j]);
2237:       sum                   = 0.0;
2238:       for (i = 0; i < a->i[j + 1] - a->i[j]; i++) {
2239:         sum += PetscAbsScalar(*v2);
2240:         v2++;
2241:       }
2242:       if (sum > *nrm) *nrm = sum;
2243:     }
2244:     PetscCall(PetscLogFlops(a->nz));
2245:   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "No support for two norm");
2246:   PetscCall(MatSeqAIJRestoreArrayRead(A, &v));
2247:   PetscFunctionReturn(PETSC_SUCCESS);
2248: }

2250: static PetscErrorCode MatIsTranspose_SeqAIJ(Mat A, Mat B, PetscReal tol, PetscBool *f)
2251: {
2252:   Mat_SeqAIJ      *aij = (Mat_SeqAIJ *)A->data, *bij = (Mat_SeqAIJ *)B->data;
2253:   PetscInt        *adx, *bdx, *aii, *bii, *aptr, *bptr;
2254:   const MatScalar *va, *vb;
2255:   PetscInt         ma, na, mb, nb, i;

2257:   PetscFunctionBegin;
2258:   PetscCall(MatGetSize(A, &ma, &na));
2259:   PetscCall(MatGetSize(B, &mb, &nb));
2260:   if (ma != nb || na != mb) {
2261:     *f = PETSC_FALSE;
2262:     PetscFunctionReturn(PETSC_SUCCESS);
2263:   }
2264:   PetscCall(MatSeqAIJGetArrayRead(A, &va));
2265:   PetscCall(MatSeqAIJGetArrayRead(B, &vb));
2266:   aii = aij->i;
2267:   bii = bij->i;
2268:   adx = aij->j;
2269:   bdx = bij->j;
2270:   PetscCall(PetscMalloc1(ma, &aptr));
2271:   PetscCall(PetscMalloc1(mb, &bptr));
2272:   for (i = 0; i < ma; i++) aptr[i] = aii[i];
2273:   for (i = 0; i < mb; i++) bptr[i] = bii[i];

2275:   *f = PETSC_TRUE;
2276:   for (i = 0; i < ma; i++) {
2277:     while (aptr[i] < aii[i + 1]) {
2278:       PetscInt    idc, idr;
2279:       PetscScalar vc, vr;
2280:       /* column/row index/value */
2281:       idc = adx[aptr[i]];
2282:       idr = bdx[bptr[idc]];
2283:       vc  = va[aptr[i]];
2284:       vr  = vb[bptr[idc]];
2285:       if (i != idr || PetscAbsScalar(vc - vr) > tol) {
2286:         *f = PETSC_FALSE;
2287:         goto done;
2288:       } else {
2289:         aptr[i]++;
2290:         if (B || i != idc) bptr[idc]++;
2291:       }
2292:     }
2293:   }
2294: done:
2295:   PetscCall(PetscFree(aptr));
2296:   PetscCall(PetscFree(bptr));
2297:   PetscCall(MatSeqAIJRestoreArrayRead(A, &va));
2298:   PetscCall(MatSeqAIJRestoreArrayRead(B, &vb));
2299:   PetscFunctionReturn(PETSC_SUCCESS);
2300: }

2302: static PetscErrorCode MatIsHermitianTranspose_SeqAIJ(Mat A, Mat B, PetscReal tol, PetscBool *f)
2303: {
2304:   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data, *bij = (Mat_SeqAIJ *)B->data;
2305:   PetscInt   *adx, *bdx, *aii, *bii, *aptr, *bptr;
2306:   MatScalar  *va, *vb;
2307:   PetscInt    ma, na, mb, nb, i;

2309:   PetscFunctionBegin;
2310:   PetscCall(MatGetSize(A, &ma, &na));
2311:   PetscCall(MatGetSize(B, &mb, &nb));
2312:   if (ma != nb || na != mb) {
2313:     *f = PETSC_FALSE;
2314:     PetscFunctionReturn(PETSC_SUCCESS);
2315:   }
2316:   aii = aij->i;
2317:   bii = bij->i;
2318:   adx = aij->j;
2319:   bdx = bij->j;
2320:   va  = aij->a;
2321:   vb  = bij->a;
2322:   PetscCall(PetscMalloc1(ma, &aptr));
2323:   PetscCall(PetscMalloc1(mb, &bptr));
2324:   for (i = 0; i < ma; i++) aptr[i] = aii[i];
2325:   for (i = 0; i < mb; i++) bptr[i] = bii[i];

2327:   *f = PETSC_TRUE;
2328:   for (i = 0; i < ma; i++) {
2329:     while (aptr[i] < aii[i + 1]) {
2330:       PetscInt    idc, idr;
2331:       PetscScalar vc, vr;
2332:       /* column/row index/value */
2333:       idc = adx[aptr[i]];
2334:       idr = bdx[bptr[idc]];
2335:       vc  = va[aptr[i]];
2336:       vr  = vb[bptr[idc]];
2337:       if (i != idr || PetscAbsScalar(vc - PetscConj(vr)) > tol) {
2338:         *f = PETSC_FALSE;
2339:         goto done;
2340:       } else {
2341:         aptr[i]++;
2342:         if (B || i != idc) bptr[idc]++;
2343:       }
2344:     }
2345:   }
2346: done:
2347:   PetscCall(PetscFree(aptr));
2348:   PetscCall(PetscFree(bptr));
2349:   PetscFunctionReturn(PETSC_SUCCESS);
2350: }

2352: PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A, Vec ll, Vec rr)
2353: {
2354:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data;
2355:   const PetscScalar *l, *r;
2356:   PetscScalar        x;
2357:   MatScalar         *v;
2358:   PetscInt           i, j, m = A->rmap->n, n = A->cmap->n, M, nz = a->nz;
2359:   const PetscInt    *jj;

2361:   PetscFunctionBegin;
2362:   if (ll) {
2363:     /* The local size is used so that VecMPI can be passed to this routine
2364:        by MatDiagonalScale_MPIAIJ */
2365:     PetscCall(VecGetLocalSize(ll, &m));
2366:     PetscCheck(m == A->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Left scaling vector wrong length");
2367:     PetscCall(VecGetArrayRead(ll, &l));
2368:     PetscCall(MatSeqAIJGetArray(A, &v));
2369:     for (i = 0; i < m; i++) {
2370:       x = l[i];
2371:       M = a->i[i + 1] - a->i[i];
2372:       for (j = 0; j < M; j++) (*v++) *= x;
2373:     }
2374:     PetscCall(VecRestoreArrayRead(ll, &l));
2375:     PetscCall(PetscLogFlops(nz));
2376:     PetscCall(MatSeqAIJRestoreArray(A, &v));
2377:   }
2378:   if (rr) {
2379:     PetscCall(VecGetLocalSize(rr, &n));
2380:     PetscCheck(n == A->cmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Right scaling vector wrong length");
2381:     PetscCall(VecGetArrayRead(rr, &r));
2382:     PetscCall(MatSeqAIJGetArray(A, &v));
2383:     jj = a->j;
2384:     for (i = 0; i < nz; i++) (*v++) *= r[*jj++];
2385:     PetscCall(MatSeqAIJRestoreArray(A, &v));
2386:     PetscCall(VecRestoreArrayRead(rr, &r));
2387:     PetscCall(PetscLogFlops(nz));
2388:   }
2389:   PetscFunctionReturn(PETSC_SUCCESS);
2390: }

2392: PetscErrorCode MatCreateSubMatrix_SeqAIJ(Mat A, IS isrow, IS iscol, PetscInt csize, MatReuse scall, Mat *B)
2393: {
2394:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data, *c;
2395:   PetscInt          *smap, i, k, kstart, kend, oldcols = A->cmap->n, *lens;
2396:   PetscInt           row, mat_i, *mat_j, tcol, first, step, *mat_ilen, sum, lensi;
2397:   const PetscInt    *irow, *icol;
2398:   const PetscScalar *aa;
2399:   PetscInt           nrows, ncols;
2400:   PetscInt          *starts, *j_new, *i_new, *aj = a->j, *ai = a->i, ii, *ailen = a->ilen;
2401:   MatScalar         *a_new, *mat_a, *c_a;
2402:   Mat                C;
2403:   PetscBool          stride;

2405:   PetscFunctionBegin;
2406:   PetscCall(ISGetIndices(isrow, &irow));
2407:   PetscCall(ISGetLocalSize(isrow, &nrows));
2408:   PetscCall(ISGetLocalSize(iscol, &ncols));

2410:   PetscCall(PetscObjectTypeCompare((PetscObject)iscol, ISSTRIDE, &stride));
2411:   if (stride) {
2412:     PetscCall(ISStrideGetInfo(iscol, &first, &step));
2413:   } else {
2414:     first = 0;
2415:     step  = 0;
2416:   }
2417:   if (stride && step == 1) {
2418:     /* special case of contiguous rows */
2419:     PetscCall(PetscMalloc2(nrows, &lens, nrows, &starts));
2420:     /* loop over new rows determining lens and starting points */
2421:     for (i = 0; i < nrows; i++) {
2422:       kstart    = ai[irow[i]];
2423:       kend      = kstart + ailen[irow[i]];
2424:       starts[i] = kstart;
2425:       for (k = kstart; k < kend; k++) {
2426:         if (aj[k] >= first) {
2427:           starts[i] = k;
2428:           break;
2429:         }
2430:       }
2431:       sum = 0;
2432:       while (k < kend) {
2433:         if (aj[k++] >= first + ncols) break;
2434:         sum++;
2435:       }
2436:       lens[i] = sum;
2437:     }
2438:     /* create submatrix */
2439:     if (scall == MAT_REUSE_MATRIX) {
2440:       PetscInt n_cols, n_rows;
2441:       PetscCall(MatGetSize(*B, &n_rows, &n_cols));
2442:       PetscCheck(n_rows == nrows && n_cols == ncols, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Reused submatrix wrong size");
2443:       PetscCall(MatZeroEntries(*B));
2444:       C = *B;
2445:     } else {
2446:       PetscInt rbs, cbs;
2447:       PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &C));
2448:       PetscCall(MatSetSizes(C, nrows, ncols, PETSC_DETERMINE, PETSC_DETERMINE));
2449:       PetscCall(ISGetBlockSize(isrow, &rbs));
2450:       PetscCall(ISGetBlockSize(iscol, &cbs));
2451:       PetscCall(MatSetBlockSizes(C, rbs, cbs));
2452:       PetscCall(MatSetType(C, ((PetscObject)A)->type_name));
2453:       PetscCall(MatSetOption(C, MAT_STRUCTURE_ONLY, A->structure_only));
2454:       PetscCall(MatSeqAIJSetPreallocation_SeqAIJ(C, 0, lens));
2455:     }
2456:     c = (Mat_SeqAIJ *)C->data;

2458:     /* loop over rows inserting into submatrix */
2459:     j_new = c->j;
2460:     i_new = c->i;
2461:     PetscCall(MatSeqAIJGetArrayWrite(C, &a_new)); // Not 'a_new = c->a-new', since that raw usage ignores offload state of C
2462:     PetscCall(MatSeqAIJGetArrayRead(A, &aa));
2463:     for (i = 0; i < nrows; i++) {
2464:       ii    = starts[i];
2465:       lensi = lens[i];
2466:       if (lensi) {
2467:         for (k = 0; k < lensi; k++) *j_new++ = aj[ii + k] - first;
2468:         if (!A->structure_only) {
2469:           PetscCall(PetscArraycpy(a_new, aa + starts[i], lensi));
2470:           a_new += lensi;
2471:         }
2472:       }
2473:       i_new[i + 1] = i_new[i] + lensi;
2474:       c->ilen[i]   = lensi;
2475:     }
2476:     PetscCall(MatSeqAIJRestoreArrayWrite(C, &a_new)); // Set C's offload state properly
2477:     PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
2478:     PetscCall(PetscFree2(lens, starts));
2479:   } else {
2480:     PetscCall(ISGetIndices(iscol, &icol));
2481:     PetscCall(PetscCalloc1(oldcols, &smap));
2482:     PetscCall(PetscMalloc1(1 + nrows, &lens));
2483:     for (i = 0; i < ncols; i++) {
2484:       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);
2485:       smap[icol[i]] = i + 1;
2486:     }

2488:     /* determine lens of each row */
2489:     for (i = 0; i < nrows; i++) {
2490:       kstart  = ai[irow[i]];
2491:       kend    = kstart + a->ilen[irow[i]];
2492:       lens[i] = 0;
2493:       for (k = kstart; k < kend; k++) {
2494:         if (smap[aj[k]]) lens[i]++;
2495:       }
2496:     }
2497:     /* Create and fill new matrix */
2498:     if (scall == MAT_REUSE_MATRIX) {
2499:       PetscBool equal;

2501:       c = (Mat_SeqAIJ *)(*B)->data;
2502:       PetscCheck((*B)->rmap->n == nrows && (*B)->cmap->n == ncols, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Cannot reuse matrix. wrong size");
2503:       PetscCall(PetscArraycmp(c->ilen, lens, (*B)->rmap->n, &equal));
2504:       PetscCheck(equal, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Cannot reuse matrix. wrong number of nonzeros");
2505:       PetscCall(PetscArrayzero(c->ilen, (*B)->rmap->n));
2506:       C = *B;
2507:     } else {
2508:       PetscInt rbs, cbs;
2509:       PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &C));
2510:       PetscCall(MatSetSizes(C, nrows, ncols, PETSC_DETERMINE, PETSC_DETERMINE));
2511:       PetscCall(ISGetBlockSize(isrow, &rbs));
2512:       PetscCall(ISGetBlockSize(iscol, &cbs));
2513:       if (rbs > 1 || cbs > 1) PetscCall(MatSetBlockSizes(C, rbs, cbs));
2514:       PetscCall(MatSetType(C, ((PetscObject)A)->type_name));
2515:       PetscCall(MatSetOption(C, MAT_STRUCTURE_ONLY, A->structure_only));
2516:       PetscCall(MatSeqAIJSetPreallocation_SeqAIJ(C, 0, lens));
2517:     }
2518:     c = (Mat_SeqAIJ *)C->data;
2519:     PetscCall(MatSeqAIJGetArrayRead(A, &aa));
2520:     PetscCall(MatSeqAIJGetArrayWrite(C, &c_a)); // Not 'c->a', since that raw usage ignores offload state of C
2521:     for (i = 0; i < nrows; i++) {
2522:       row      = irow[i];
2523:       kstart   = ai[row];
2524:       kend     = kstart + a->ilen[row];
2525:       mat_i    = c->i[i];
2526:       mat_j    = PetscSafePointerPlusOffset(c->j, mat_i);
2527:       mat_a    = PetscSafePointerPlusOffset(c_a, mat_i);
2528:       mat_ilen = c->ilen + i;
2529:       for (k = kstart; k < kend; k++) {
2530:         if ((tcol = smap[a->j[k]])) {
2531:           *mat_j++ = tcol - 1;
2532:           if (!A->structure_only) *mat_a++ = aa[k];
2533:           (*mat_ilen)++;
2534:         }
2535:       }
2536:     }
2537:     PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
2538:     /* Free work space */
2539:     PetscCall(ISRestoreIndices(iscol, &icol));
2540:     PetscCall(PetscFree(smap));
2541:     PetscCall(PetscFree(lens));
2542:     /* sort */
2543:     for (i = 0; i < nrows; i++) {
2544:       PetscInt ilen;

2546:       mat_i = c->i[i];
2547:       mat_j = PetscSafePointerPlusOffset(c->j, mat_i);
2548:       mat_a = PetscSafePointerPlusOffset(c_a, mat_i);
2549:       ilen  = c->ilen[i];
2550:       if (A->structure_only) PetscCall(PetscSortInt(ilen, mat_j));
2551:       else PetscCall(PetscSortIntWithScalarArray(ilen, mat_j, mat_a));
2552:     }
2553:     PetscCall(MatSeqAIJRestoreArrayWrite(C, &c_a));
2554:   }
2555: #if PetscDefined(HAVE_DEVICE)
2556:   PetscCall(MatBindToCPU(C, A->boundtocpu));
2557: #endif
2558:   PetscCall(MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY));
2559:   PetscCall(MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY));

2561:   PetscCall(ISRestoreIndices(isrow, &irow));
2562:   *B = C;
2563:   PetscFunctionReturn(PETSC_SUCCESS);
2564: }

2566: static PetscErrorCode MatGetMultiProcBlock_SeqAIJ(Mat mat, MPI_Comm subComm, MatReuse scall, Mat *subMat)
2567: {
2568:   Mat B;

2570:   PetscFunctionBegin;
2571:   if (scall == MAT_INITIAL_MATRIX) {
2572:     PetscCall(MatCreate(subComm, &B));
2573:     PetscCall(MatSetSizes(B, mat->rmap->n, mat->cmap->n, mat->rmap->n, mat->cmap->n));
2574:     PetscCall(MatSetBlockSizesFromMats(B, mat, mat));
2575:     PetscCall(MatSetType(B, MATSEQAIJ));
2576:     PetscCall(MatDuplicateNoCreate_SeqAIJ(B, mat, MAT_COPY_VALUES, PETSC_TRUE));
2577:     *subMat = B;
2578:   } else {
2579:     PetscCall(MatCopy_SeqAIJ(mat, *subMat, SAME_NONZERO_PATTERN));
2580:   }
2581:   PetscFunctionReturn(PETSC_SUCCESS);
2582: }

2584: static PetscErrorCode MatILUFactor_SeqAIJ(Mat inA, IS row, IS col, const MatFactorInfo *info)
2585: {
2586:   Mat_SeqAIJ *a = (Mat_SeqAIJ *)inA->data;
2587:   Mat         outA;
2588:   PetscBool   row_identity, col_identity;

2590:   PetscFunctionBegin;
2591:   PetscCheck(info->levels == 0, PETSC_COMM_SELF, PETSC_ERR_SUP, "Only levels=0 supported for in-place ilu");

2593:   PetscCall(ISIdentity(row, &row_identity));
2594:   PetscCall(ISIdentity(col, &col_identity));

2596:   outA = inA;
2597:   PetscCall(PetscFree(inA->solvertype));
2598:   PetscCall(PetscStrallocpy(MATSOLVERPETSC, &inA->solvertype));

2600:   PetscCall(PetscObjectReference((PetscObject)row));
2601:   PetscCall(ISDestroy(&a->row));

2603:   a->row = row;

2605:   PetscCall(PetscObjectReference((PetscObject)col));
2606:   PetscCall(ISDestroy(&a->col));

2608:   a->col = col;

2610:   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
2611:   PetscCall(ISDestroy(&a->icol));
2612:   PetscCall(ISInvertPermutation(col, PETSC_DECIDE, &a->icol));

2614:   if (!a->solve_work) { /* this matrix may have been factored before */
2615:     PetscCall(PetscMalloc1(inA->rmap->n, &a->solve_work));
2616:   }

2618:   if (row_identity && col_identity) {
2619:     PetscCall(MatLUFactorNumeric_SeqAIJ_inplace(outA, inA, info));
2620:   } else {
2621:     PetscCall(MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA, inA, info));
2622:   }
2623:   outA->factortype = MAT_FACTOR_LU;
2624:   PetscFunctionReturn(PETSC_SUCCESS);
2625: }

2627: PetscErrorCode MatScale_SeqAIJ(Mat inA, PetscScalar alpha)
2628: {
2629:   Mat_SeqAIJ  *a = (Mat_SeqAIJ *)inA->data;
2630:   PetscScalar *v;
2631:   PetscBLASInt one = 1, bnz;

2633:   PetscFunctionBegin;
2634:   PetscCall(MatSeqAIJGetArray(inA, &v));
2635:   PetscCall(PetscBLASIntCast(a->nz, &bnz));
2636:   PetscCallBLAS("BLASscal", BLASscal_(&bnz, &alpha, v, &one));
2637:   PetscCall(PetscLogFlops(a->nz));
2638:   PetscCall(MatSeqAIJRestoreArray(inA, &v));
2639:   PetscFunctionReturn(PETSC_SUCCESS);
2640: }

2642: PetscErrorCode MatDestroySubMatrix_Private(Mat_SubSppt *submatj)
2643: {
2644:   PetscInt i;

2646:   PetscFunctionBegin;
2647:   if (!submatj->id) { /* delete data that are linked only to submats[id=0] */
2648:     PetscCall(PetscFree4(submatj->sbuf1, submatj->ptr, submatj->tmp, submatj->ctr));

2650:     for (i = 0; i < submatj->nrqr; ++i) PetscCall(PetscFree(submatj->sbuf2[i]));
2651:     PetscCall(PetscFree3(submatj->sbuf2, submatj->req_size, submatj->req_source1));

2653:     if (submatj->rbuf1) {
2654:       PetscCall(PetscFree(submatj->rbuf1[0]));
2655:       PetscCall(PetscFree(submatj->rbuf1));
2656:     }

2658:     for (i = 0; i < submatj->nrqs; ++i) PetscCall(PetscFree(submatj->rbuf3[i]));
2659:     PetscCall(PetscFree3(submatj->req_source2, submatj->rbuf2, submatj->rbuf3));
2660:     PetscCall(PetscFree(submatj->pa));
2661:     PetscCall(PetscFree2(submatj->local_a_parent, submatj->local_a_sub));
2662:     PetscCall(PetscFree2(submatj->local_b_parent, submatj->local_b_sub));
2663:   }

2665: #if PetscDefined(USE_CTABLE)
2666:   PetscCall(PetscHMapIDestroy(&submatj->rmap));
2667:   PetscCall(PetscFree(submatj->cmap_loc));
2668:   PetscCall(PetscFree(submatj->rmap_loc));
2669: #else
2670:   PetscCall(PetscFree(submatj->rmap));
2671: #endif

2673:   if (!submatj->allcolumns) {
2674: #if PetscDefined(USE_CTABLE)
2675:     PetscCall(PetscHMapIDestroy(&submatj->cmap));
2676: #else
2677:     PetscCall(PetscFree(submatj->cmap));
2678: #endif
2679:   }
2680:   PetscCall(PetscFree(submatj->row2proc));

2682:   PetscCall(PetscFree(submatj));
2683:   PetscFunctionReturn(PETSC_SUCCESS);
2684: }

2686: PetscErrorCode MatDestroySubMatrix_SeqAIJ(Mat C)
2687: {
2688:   Mat_SeqAIJ  *c       = (Mat_SeqAIJ *)C->data;
2689:   Mat_SubSppt *submatj = c->submatis1;

2691:   PetscFunctionBegin;
2692:   PetscCall((*submatj->destroy)(C));
2693:   PetscCall(MatDestroySubMatrix_Private(submatj));
2694:   PetscFunctionReturn(PETSC_SUCCESS);
2695: }

2697: /* Note this has code duplication with MatDestroySubMatrices_SeqBAIJ() */
2698: static PetscErrorCode MatDestroySubMatrices_SeqAIJ(PetscInt n, Mat *mat[])
2699: {
2700:   PetscInt     i;
2701:   Mat          C;
2702:   Mat_SeqAIJ  *c;
2703:   Mat_SubSppt *submatj;

2705:   PetscFunctionBegin;
2706:   for (i = 0; i < n; i++) {
2707:     C       = (*mat)[i];
2708:     c       = (Mat_SeqAIJ *)C->data;
2709:     submatj = c->submatis1;
2710:     if (submatj) {
2711:       if (--((PetscObject)C)->refct <= 0) {
2712:         PetscCall(PetscFree(C->factorprefix));
2713:         PetscCall((*submatj->destroy)(C));
2714:         PetscCall(MatDestroySubMatrix_Private(submatj));
2715:         PetscCall(PetscFree(C->defaultvectype));
2716:         PetscCall(PetscFree(C->defaultrandtype));
2717:         PetscCall(PetscFree(C->solvertype));
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 PetscDefined(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:     PetscBool subset = PETSC_TRUE;

2988:     PetscCall(PetscMalloc1(Y->rmap->N, &nnz));
2989:     PetscCall(MatAXPYGetPreallocation_SeqAIJ(Y, X, nnz));
2990:     /* nnz[i] is the size of the union of row i of X and Y, so the pattern of X is contained in that of Y when every nnz[i] equals
2991:        the length of row i of Y. Adding in place then keeps the nonzero pattern of Y, and its nonzero state, unchanged. */
2992:     for (PetscInt i = 0; i < Y->rmap->N && subset; i++) subset = (PetscBool)(nnz[i] == y->i[i + 1] - y->i[i]);
2993:     if (subset) PetscCall(MatAXPY_Basic(Y, a, X, SUBSET_NONZERO_PATTERN));
2994:     else {
2995:       PetscCall(MatCreate(PetscObjectComm((PetscObject)Y), &B));
2996:       PetscCall(PetscObjectSetName((PetscObject)B, ((PetscObject)Y)->name));
2997:       PetscCall(MatSetLayouts(B, Y->rmap, Y->cmap));
2998:       PetscCall(MatSetType(B, ((PetscObject)Y)->type_name));
2999:       PetscCall(MatSeqAIJSetPreallocation(B, 0, nnz));
3000:       PetscCall(MatAXPY_BasicWithPreallocation(B, Y, a, X, str));
3001:       PetscCall(MatHeaderMerge(Y, &B));
3002:       PetscCall(MatSeqAIJCheckInode(Y));
3003:     }
3004:     PetscCall(PetscFree(nnz));
3005:   }
3006:   PetscFunctionReturn(PETSC_SUCCESS);
3007: }

3009: PETSC_INTERN PetscErrorCode MatConjugate_SeqAIJ(Mat mat)
3010: {
3011:   Mat_SeqAIJ  *aij = (Mat_SeqAIJ *)mat->data;
3012:   PetscInt     i, nz = aij->nz;
3013:   PetscScalar *a;

3015:   PetscFunctionBegin;
3016:   PetscCall(MatSeqAIJGetArray(mat, &a));
3017:   for (i = 0; i < nz; i++) a[i] = PetscConj(a[i]);
3018:   PetscCall(MatSeqAIJRestoreArray(mat, &a));
3019:   PetscFunctionReturn(PETSC_SUCCESS);
3020: }

3022: static PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A, Vec v, PetscInt idx[])
3023: {
3024:   Mat_SeqAIJ      *a = (Mat_SeqAIJ *)A->data;
3025:   PetscInt         i, j, m = A->rmap->n, *ai, *aj, ncols, n;
3026:   PetscReal        atmp;
3027:   PetscScalar     *x;
3028:   const MatScalar *aa, *av;

3030:   PetscFunctionBegin;
3031:   PetscCheck(!A->factortype, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
3032:   PetscCall(MatSeqAIJGetArrayRead(A, &av));
3033:   aa = av;
3034:   ai = a->i;
3035:   aj = a->j;

3037:   PetscCall(VecGetArrayWrite(v, &x));
3038:   PetscCall(VecGetLocalSize(v, &n));
3039:   PetscCheck(n == A->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nonconforming matrix and vector");
3040:   for (i = 0; i < m; i++) {
3041:     ncols = ai[1] - ai[0];
3042:     ai++;
3043:     x[i] = 0;
3044:     for (j = 0; j < ncols; j++) {
3045:       atmp = PetscAbsScalar(*aa);
3046:       if (PetscAbsScalar(x[i]) < atmp) {
3047:         x[i] = atmp;
3048:         if (idx) idx[i] = *aj;
3049:       }
3050:       aa++;
3051:       aj++;
3052:     }
3053:   }
3054:   PetscCall(VecRestoreArrayWrite(v, &x));
3055:   PetscCall(MatSeqAIJRestoreArrayRead(A, &av));
3056:   PetscFunctionReturn(PETSC_SUCCESS);
3057: }

3059: PETSC_INTERN PetscErrorCode MatGetRowSumAbs_SeqAIJ(Mat A, Vec v)
3060: {
3061:   Mat_SeqAIJ      *a = (Mat_SeqAIJ *)A->data;
3062:   PetscInt         i, j, m = A->rmap->n, *ai, ncols, n;
3063:   PetscScalar     *x;
3064:   const MatScalar *aa, *av;

3066:   PetscFunctionBegin;
3067:   PetscCheck(!A->factortype, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
3068:   PetscCall(MatSeqAIJGetArrayRead(A, &av));
3069:   aa = av;
3070:   ai = a->i;

3072:   PetscCall(VecGetArrayWrite(v, &x));
3073:   PetscCall(VecGetLocalSize(v, &n));
3074:   PetscCheck(n == A->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nonconforming matrix and vector");
3075:   for (i = 0; i < m; i++) {
3076:     ncols = ai[1] - ai[0];
3077:     ai++;
3078:     x[i] = 0;
3079:     for (j = 0; j < ncols; j++) {
3080:       x[i] += PetscAbsScalar(*aa);
3081:       aa++;
3082:     }
3083:   }
3084:   PetscCall(VecRestoreArrayWrite(v, &x));
3085:   PetscCall(MatSeqAIJRestoreArrayRead(A, &av));
3086:   PetscFunctionReturn(PETSC_SUCCESS);
3087: }

3089: static PetscErrorCode MatGetRowMax_SeqAIJ(Mat A, Vec v, PetscInt idx[])
3090: {
3091:   Mat_SeqAIJ      *a = (Mat_SeqAIJ *)A->data;
3092:   PetscInt         i, j, m = A->rmap->n, *ai, *aj, ncols, n;
3093:   PetscScalar     *x;
3094:   const MatScalar *aa, *av;

3096:   PetscFunctionBegin;
3097:   PetscCheck(!A->factortype, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
3098:   PetscCall(MatSeqAIJGetArrayRead(A, &av));
3099:   aa = av;
3100:   ai = a->i;
3101:   aj = a->j;

3103:   PetscCall(VecGetArrayWrite(v, &x));
3104:   PetscCall(VecGetLocalSize(v, &n));
3105:   PetscCheck(n == A->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nonconforming matrix and vector");
3106:   for (i = 0; i < m; i++) {
3107:     ncols = ai[1] - ai[0];
3108:     ai++;
3109:     if (ncols == A->cmap->n) { /* row is dense */
3110:       x[i] = *aa;
3111:       if (idx) idx[i] = 0;
3112:     } else { /* row is sparse so already KNOW maximum is 0.0 or higher */
3113:       x[i] = 0.0;
3114:       if (idx) {
3115:         for (j = 0; j < ncols; j++) { /* find first implicit 0.0 in the row */
3116:           if (aj[j] > j) {
3117:             idx[i] = j;
3118:             break;
3119:           }
3120:         }
3121:         /* in case first implicit 0.0 in the row occurs at ncols-th column */
3122:         if (j == ncols && j < A->cmap->n) idx[i] = j;
3123:       }
3124:     }
3125:     for (j = 0; j < ncols; j++) {
3126:       if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {
3127:         x[i] = *aa;
3128:         if (idx) idx[i] = *aj;
3129:       }
3130:       aa++;
3131:       aj++;
3132:     }
3133:   }
3134:   PetscCall(VecRestoreArrayWrite(v, &x));
3135:   PetscCall(MatSeqAIJRestoreArrayRead(A, &av));
3136:   PetscFunctionReturn(PETSC_SUCCESS);
3137: }

3139: static PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A, Vec v, PetscInt idx[])
3140: {
3141:   Mat_SeqAIJ      *a = (Mat_SeqAIJ *)A->data;
3142:   PetscInt         i, j, m = A->rmap->n, *ai, *aj, ncols, n;
3143:   PetscScalar     *x;
3144:   const MatScalar *aa, *av;

3146:   PetscFunctionBegin;
3147:   PetscCall(MatSeqAIJGetArrayRead(A, &av));
3148:   aa = av;
3149:   ai = a->i;
3150:   aj = a->j;

3152:   PetscCall(VecGetArrayWrite(v, &x));
3153:   PetscCall(VecGetLocalSize(v, &n));
3154:   PetscCheck(n == m, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nonconforming matrix and vector, %" PetscInt_FMT " vs. %" PetscInt_FMT " rows", m, n);
3155:   for (i = 0; i < m; i++) {
3156:     ncols = ai[1] - ai[0];
3157:     ai++;
3158:     if (ncols == A->cmap->n) { /* row is dense */
3159:       x[i] = *aa;
3160:       if (idx) idx[i] = 0;
3161:     } else { /* row is sparse so already KNOW minimum is 0.0 or higher */
3162:       x[i] = 0.0;
3163:       if (idx) { /* find first implicit 0.0 in the row */
3164:         for (j = 0; j < ncols; j++) {
3165:           if (aj[j] > j) {
3166:             idx[i] = j;
3167:             break;
3168:           }
3169:         }
3170:         /* in case first implicit 0.0 in the row occurs at ncols-th column */
3171:         if (j == ncols && j < A->cmap->n) idx[i] = j;
3172:       }
3173:     }
3174:     for (j = 0; j < ncols; j++) {
3175:       if (PetscAbsScalar(x[i]) > PetscAbsScalar(*aa)) {
3176:         x[i] = *aa;
3177:         if (idx) idx[i] = *aj;
3178:       }
3179:       aa++;
3180:       aj++;
3181:     }
3182:   }
3183:   PetscCall(VecRestoreArrayWrite(v, &x));
3184:   PetscCall(MatSeqAIJRestoreArrayRead(A, &av));
3185:   PetscFunctionReturn(PETSC_SUCCESS);
3186: }

3188: static PetscErrorCode MatGetRowMin_SeqAIJ(Mat A, Vec v, PetscInt idx[])
3189: {
3190:   Mat_SeqAIJ      *a = (Mat_SeqAIJ *)A->data;
3191:   PetscInt         i, j, m = A->rmap->n, ncols, n;
3192:   const PetscInt  *ai, *aj;
3193:   PetscScalar     *x;
3194:   const MatScalar *aa, *av;

3196:   PetscFunctionBegin;
3197:   PetscCheck(!A->factortype, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
3198:   PetscCall(MatSeqAIJGetArrayRead(A, &av));
3199:   aa = av;
3200:   ai = a->i;
3201:   aj = a->j;

3203:   PetscCall(VecGetArrayWrite(v, &x));
3204:   PetscCall(VecGetLocalSize(v, &n));
3205:   PetscCheck(n == m, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nonconforming matrix and vector");
3206:   for (i = 0; i < m; i++) {
3207:     ncols = ai[1] - ai[0];
3208:     ai++;
3209:     if (ncols == A->cmap->n) { /* row is dense */
3210:       x[i] = *aa;
3211:       if (idx) idx[i] = 0;
3212:     } else { /* row is sparse so already KNOW minimum is 0.0 or lower */
3213:       x[i] = 0.0;
3214:       if (idx) { /* find first implicit 0.0 in the row */
3215:         for (j = 0; j < ncols; j++) {
3216:           if (aj[j] > j) {
3217:             idx[i] = j;
3218:             break;
3219:           }
3220:         }
3221:         /* in case first implicit 0.0 in the row occurs at ncols-th column */
3222:         if (j == ncols && j < A->cmap->n) idx[i] = j;
3223:       }
3224:     }
3225:     for (j = 0; j < ncols; j++) {
3226:       if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {
3227:         x[i] = *aa;
3228:         if (idx) idx[i] = *aj;
3229:       }
3230:       aa++;
3231:       aj++;
3232:     }
3233:   }
3234:   PetscCall(VecRestoreArrayWrite(v, &x));
3235:   PetscCall(MatSeqAIJRestoreArrayRead(A, &av));
3236:   PetscFunctionReturn(PETSC_SUCCESS);
3237: }

3239: static PetscErrorCode MatInvertBlockDiagonal_SeqAIJ(Mat A, const PetscScalar **values)
3240: {
3241:   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data;
3242:   PetscInt        i, bs = A->rmap->bs, mbs = A->rmap->n / bs, ipvt[5], bs2 = bs * bs, *v_pivots, ij[7], *IJ, j;
3243:   MatScalar      *diag, work[25], *v_work;
3244:   const PetscReal shift = 0.0;
3245:   PetscBool       allowzeropivot, zeropivotdetected = PETSC_FALSE;

3247:   PetscFunctionBegin;
3248:   allowzeropivot = PetscNot(A->erroriffailure);
3249:   if (a->ibdiag && a->ibdiagsize == bs2 * mbs && a->ibdiagState == ((PetscObject)A)->state) {
3250:     if (values) *values = a->ibdiag;
3251:     PetscFunctionReturn(PETSC_SUCCESS);
3252:   }
3253:   /* reallocate only when the length changes, so that the pointer stays valid for callers that
3254:      hold on to it, such as PCSetUp_PBJacobi_Host() */
3255:   if (!a->ibdiag || a->ibdiagsize != bs2 * mbs) {
3256:     PetscCall(PetscFree(a->ibdiag));
3257:     PetscCall(PetscMalloc1(bs2 * mbs, &a->ibdiag));
3258:     a->ibdiagsize = bs2 * mbs;
3259:   }
3260:   diag = a->ibdiag;
3261:   if (values) *values = a->ibdiag;
3262:   /* factor and invert each block */
3263:   switch (bs) {
3264:   case 1:
3265:     for (i = 0; i < mbs; i++) {
3266:       PetscCall(MatGetValues(A, 1, &i, 1, &i, diag + i));
3267:       if (PetscAbsScalar(diag[i] + shift) < PETSC_MACHINE_EPSILON) {
3268:         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);
3269:         A->factorerrortype             = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3270:         A->factorerror_zeropivot_value = PetscAbsScalar(diag[i]);
3271:         A->factorerror_zeropivot_row   = i;
3272:         PetscCall(PetscInfo(A, "Zero pivot, row %" PetscInt_FMT " pivot %g tolerance %g\n", i, (double)PetscAbsScalar(diag[i]), (double)PETSC_MACHINE_EPSILON));
3273:       }
3274:       diag[i] = (PetscScalar)1.0 / (diag[i] + shift);
3275:     }
3276:     break;
3277:   case 2:
3278:     for (i = 0; i < mbs; i++) {
3279:       ij[0] = 2 * i;
3280:       ij[1] = 2 * i + 1;
3281:       PetscCall(MatGetValues(A, 2, ij, 2, ij, diag));
3282:       PetscCall(PetscKernel_A_gets_inverse_A_2(diag, shift, allowzeropivot, &zeropivotdetected));
3283:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3284:       PetscCall(PetscKernel_A_gets_transpose_A_2(diag));
3285:       diag += 4;
3286:     }
3287:     break;
3288:   case 3:
3289:     for (i = 0; i < mbs; i++) {
3290:       ij[0] = 3 * i;
3291:       ij[1] = 3 * i + 1;
3292:       ij[2] = 3 * i + 2;
3293:       PetscCall(MatGetValues(A, 3, ij, 3, ij, diag));
3294:       PetscCall(PetscKernel_A_gets_inverse_A_3(diag, shift, allowzeropivot, &zeropivotdetected));
3295:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3296:       PetscCall(PetscKernel_A_gets_transpose_A_3(diag));
3297:       diag += 9;
3298:     }
3299:     break;
3300:   case 4:
3301:     for (i = 0; i < mbs; i++) {
3302:       ij[0] = 4 * i;
3303:       ij[1] = 4 * i + 1;
3304:       ij[2] = 4 * i + 2;
3305:       ij[3] = 4 * i + 3;
3306:       PetscCall(MatGetValues(A, 4, ij, 4, ij, diag));
3307:       PetscCall(PetscKernel_A_gets_inverse_A_4(diag, shift, allowzeropivot, &zeropivotdetected));
3308:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3309:       PetscCall(PetscKernel_A_gets_transpose_A_4(diag));
3310:       diag += 16;
3311:     }
3312:     break;
3313:   case 5:
3314:     for (i = 0; i < mbs; i++) {
3315:       ij[0] = 5 * i;
3316:       ij[1] = 5 * i + 1;
3317:       ij[2] = 5 * i + 2;
3318:       ij[3] = 5 * i + 3;
3319:       ij[4] = 5 * i + 4;
3320:       PetscCall(MatGetValues(A, 5, ij, 5, ij, diag));
3321:       PetscCall(PetscKernel_A_gets_inverse_A_5(diag, ipvt, work, shift, allowzeropivot, &zeropivotdetected));
3322:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3323:       PetscCall(PetscKernel_A_gets_transpose_A_5(diag));
3324:       diag += 25;
3325:     }
3326:     break;
3327:   case 6:
3328:     for (i = 0; i < mbs; i++) {
3329:       ij[0] = 6 * i;
3330:       ij[1] = 6 * i + 1;
3331:       ij[2] = 6 * i + 2;
3332:       ij[3] = 6 * i + 3;
3333:       ij[4] = 6 * i + 4;
3334:       ij[5] = 6 * i + 5;
3335:       PetscCall(MatGetValues(A, 6, ij, 6, ij, diag));
3336:       PetscCall(PetscKernel_A_gets_inverse_A_6(diag, shift, allowzeropivot, &zeropivotdetected));
3337:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3338:       PetscCall(PetscKernel_A_gets_transpose_A_6(diag));
3339:       diag += 36;
3340:     }
3341:     break;
3342:   case 7:
3343:     for (i = 0; i < mbs; i++) {
3344:       ij[0] = 7 * i;
3345:       ij[1] = 7 * i + 1;
3346:       ij[2] = 7 * i + 2;
3347:       ij[3] = 7 * i + 3;
3348:       ij[4] = 7 * i + 4;
3349:       ij[5] = 7 * i + 5;
3350:       ij[6] = 7 * i + 6;
3351:       PetscCall(MatGetValues(A, 7, ij, 7, ij, diag));
3352:       PetscCall(PetscKernel_A_gets_inverse_A_7(diag, shift, allowzeropivot, &zeropivotdetected));
3353:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3354:       PetscCall(PetscKernel_A_gets_transpose_A_7(diag));
3355:       diag += 49;
3356:     }
3357:     break;
3358:   default:
3359:     PetscCall(PetscMalloc3(bs, &v_work, bs, &v_pivots, bs, &IJ));
3360:     for (i = 0; i < mbs; i++) {
3361:       for (j = 0; j < bs; j++) IJ[j] = bs * i + j;
3362:       PetscCall(MatGetValues(A, bs, IJ, bs, IJ, diag));
3363:       PetscCall(PetscKernel_A_gets_inverse_A(bs, diag, v_pivots, v_work, allowzeropivot, &zeropivotdetected));
3364:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3365:       PetscCall(PetscKernel_A_gets_transpose_A_N(diag, bs));
3366:       diag += bs2;
3367:     }
3368:     PetscCall(PetscFree3(v_work, v_pivots, IJ));
3369:   }
3370:   a->ibdiagState = ((PetscObject)A)->state;
3371:   PetscFunctionReturn(PETSC_SUCCESS);
3372: }

3374: static PetscErrorCode MatSetRandom_SeqAIJ(Mat x, PetscRandom rctx)
3375: {
3376:   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)x->data;
3377:   PetscScalar a, *aa;
3378:   PetscInt    m, n, i, j, col;

3380:   PetscFunctionBegin;
3381:   if (!x->assembled) {
3382:     PetscCall(MatGetSize(x, &m, &n));
3383:     for (i = 0; i < m; i++) {
3384:       for (j = 0; j < aij->imax[i]; j++) {
3385:         PetscCall(PetscRandomGetValue(rctx, &a));
3386:         col = (PetscInt)(n * PetscRealPart(a));
3387:         PetscCall(MatSetValues(x, 1, &i, 1, &col, &a, ADD_VALUES));
3388:       }
3389:     }
3390:   } else {
3391:     PetscCall(MatSeqAIJGetArrayWrite(x, &aa));
3392:     for (i = 0; i < aij->nz; i++) PetscCall(PetscRandomGetValue(rctx, aa + i));
3393:     PetscCall(MatSeqAIJRestoreArrayWrite(x, &aa));
3394:   }
3395:   PetscCall(MatAssemblyBegin(x, MAT_FINAL_ASSEMBLY));
3396:   PetscCall(MatAssemblyEnd(x, MAT_FINAL_ASSEMBLY));
3397:   PetscFunctionReturn(PETSC_SUCCESS);
3398: }

3400: /* Like MatSetRandom_SeqAIJ, but do not set values on columns in range of [low, high) */
3401: PetscErrorCode MatSetRandomSkipColumnRange_SeqAIJ_Private(Mat x, PetscInt low, PetscInt high, PetscRandom rctx)
3402: {
3403:   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)x->data;
3404:   PetscScalar a;
3405:   PetscInt    m, n, i, j, col, nskip;

3407:   PetscFunctionBegin;
3408:   nskip = high - low;
3409:   PetscCall(MatGetSize(x, &m, &n));
3410:   n -= nskip; /* shrink number of columns where nonzeros can be set */
3411:   for (i = 0; i < m; i++) {
3412:     for (j = 0; j < aij->imax[i]; j++) {
3413:       PetscCall(PetscRandomGetValue(rctx, &a));
3414:       col = (PetscInt)(n * PetscRealPart(a));
3415:       if (col >= low) col += nskip; /* shift col rightward to skip the hole */
3416:       PetscCall(MatSetValues(x, 1, &i, 1, &col, &a, ADD_VALUES));
3417:     }
3418:   }
3419:   PetscCall(MatAssemblyBegin(x, MAT_FINAL_ASSEMBLY));
3420:   PetscCall(MatAssemblyEnd(x, MAT_FINAL_ASSEMBLY));
3421:   PetscFunctionReturn(PETSC_SUCCESS);
3422: }

3424: static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
3425:                                        MatGetRow_SeqAIJ,
3426:                                        MatRestoreRow_SeqAIJ,
3427:                                        MatMult_SeqAIJ,
3428:                                        /*  4*/ MatMultAdd_SeqAIJ,
3429:                                        MatMultTranspose_SeqAIJ,
3430:                                        MatMultTransposeAdd_SeqAIJ,
3431:                                        NULL,
3432:                                        NULL,
3433:                                        NULL,
3434:                                        /* 10*/ NULL,
3435:                                        MatLUFactor_SeqAIJ,
3436:                                        NULL,
3437:                                        MatSOR_SeqAIJ,
3438:                                        MatTranspose_SeqAIJ,
3439:                                        /* 15*/ MatGetInfo_SeqAIJ,
3440:                                        MatEqual_SeqAIJ,
3441:                                        MatGetDiagonal_SeqAIJ,
3442:                                        MatDiagonalScale_SeqAIJ,
3443:                                        MatNorm_SeqAIJ,
3444:                                        /* 20*/ NULL,
3445:                                        MatAssemblyEnd_SeqAIJ,
3446:                                        MatSetOption_SeqAIJ,
3447:                                        MatZeroEntries_SeqAIJ,
3448:                                        /* 24*/ MatZeroRows_SeqAIJ,
3449:                                        NULL,
3450:                                        NULL,
3451:                                        NULL,
3452:                                        NULL,
3453:                                        /* 29*/ MatSetUp_Seq_Hash,
3454:                                        NULL,
3455:                                        NULL,
3456:                                        NULL,
3457:                                        NULL,
3458:                                        /* 34*/ MatDuplicate_SeqAIJ,
3459:                                        NULL,
3460:                                        NULL,
3461:                                        MatILUFactor_SeqAIJ,
3462:                                        NULL,
3463:                                        /* 39*/ MatAXPY_SeqAIJ,
3464:                                        MatCreateSubMatrices_SeqAIJ,
3465:                                        MatIncreaseOverlap_SeqAIJ,
3466:                                        MatGetValues_SeqAIJ,
3467:                                        MatCopy_SeqAIJ,
3468:                                        /* 44*/ MatGetRowMax_SeqAIJ,
3469:                                        MatScale_SeqAIJ,
3470:                                        MatShift_SeqAIJ,
3471:                                        MatDiagonalSet_SeqAIJ,
3472:                                        MatZeroRowsColumns_SeqAIJ,
3473:                                        /* 49*/ MatSetRandom_SeqAIJ,
3474:                                        MatGetRowIJ_SeqAIJ,
3475:                                        MatRestoreRowIJ_SeqAIJ,
3476:                                        MatGetColumnIJ_SeqAIJ,
3477:                                        MatRestoreColumnIJ_SeqAIJ,
3478:                                        /* 54*/ MatFDColoringCreate_SeqXAIJ,
3479:                                        NULL,
3480:                                        NULL,
3481:                                        MatPermute_SeqAIJ,
3482:                                        NULL,
3483:                                        /* 59*/ NULL,
3484:                                        MatDestroy_SeqAIJ,
3485:                                        MatView_SeqAIJ,
3486:                                        NULL,
3487:                                        NULL,
3488:                                        /* 64*/ MatMatMatMultNumeric_SeqAIJ_SeqAIJ_SeqAIJ,
3489:                                        NULL,
3490:                                        NULL,
3491:                                        NULL,
3492:                                        MatGetRowMaxAbs_SeqAIJ,
3493:                                        /* 69*/ MatGetRowMinAbs_SeqAIJ,
3494:                                        NULL,
3495:                                        NULL,
3496:                                        MatFDColoringApply_AIJ,
3497:                                        NULL,
3498:                                        /* 74*/ MatFindZeroDiagonals_SeqAIJ,
3499:                                        NULL,
3500:                                        NULL,
3501:                                        NULL,
3502:                                        MatLoad_SeqAIJ,
3503:                                        /* 79*/ NULL,
3504:                                        NULL,
3505:                                        NULL,
3506:                                        NULL,
3507:                                        NULL,
3508:                                        /* 84*/ NULL,
3509:                                        MatMatMultNumeric_SeqAIJ_SeqAIJ,
3510:                                        MatPtAPNumeric_SeqAIJ_SeqAIJ_SparseAxpy,
3511:                                        NULL,
3512:                                        MatMatTransposeMultNumeric_SeqAIJ_SeqAIJ,
3513:                                        /* 90*/ NULL,
3514:                                        MatProductSetFromOptions_SeqAIJ,
3515:                                        NULL,
3516:                                        NULL,
3517:                                        MatConjugate_SeqAIJ,
3518:                                        /* 94*/ NULL,
3519:                                        MatSetValuesRow_SeqAIJ,
3520:                                        MatRealPart_SeqAIJ,
3521:                                        MatImaginaryPart_SeqAIJ,
3522:                                        NULL,
3523:                                        /* 99*/ NULL,
3524:                                        MatMatSolve_SeqAIJ,
3525:                                        NULL,
3526:                                        MatGetRowMin_SeqAIJ,
3527:                                        NULL,
3528:                                        /*104*/ NULL,
3529:                                        NULL,
3530:                                        NULL,
3531:                                        NULL,
3532:                                        NULL,
3533:                                        /*109*/ NULL,
3534:                                        NULL,
3535:                                        NULL,
3536:                                        NULL,
3537:                                        MatGetMultiProcBlock_SeqAIJ,
3538:                                        /*114*/ MatFindNonzeroRows_SeqAIJ,
3539:                                        MatGetColumnReductions_SeqAIJ,
3540:                                        MatInvertBlockDiagonal_SeqAIJ,
3541:                                        MatInvertVariableBlockDiagonal_SeqAIJ,
3542:                                        NULL,
3543:                                        /*119*/ NULL,
3544:                                        MatTransposeMatMultNumeric_SeqAIJ_SeqAIJ,
3545:                                        MatTransposeColoringCreate_SeqAIJ,
3546:                                        MatTransColoringApplySpToDen_SeqAIJ,
3547:                                        MatTransColoringApplyDenToSp_SeqAIJ,
3548:                                        /*124*/ MatRARtNumeric_SeqAIJ_SeqAIJ,
3549:                                        NULL,
3550:                                        NULL,
3551:                                        MatFDColoringSetUp_SeqXAIJ,
3552:                                        MatFindOffBlockDiagonalEntries_SeqAIJ,
3553:                                        /*129*/ MatCreateMPIMatConcatenateSeqMat_SeqAIJ,
3554:                                        MatDestroySubMatrices_SeqAIJ,
3555:                                        NULL,
3556:                                        NULL,
3557:                                        MatCreateGraph_Simple_AIJ,
3558:                                        /*134*/ MatTransposeSymbolic_SeqAIJ,
3559:                                        MatEliminateZeros_SeqAIJ,
3560:                                        MatGetRowSumAbs_SeqAIJ,
3561:                                        NULL,
3562:                                        NULL,
3563:                                        /*139*/ NULL,
3564:                                        MatCopyHashToXAIJ_Seq_Hash,
3565:                                        NULL,
3566:                                        NULL,
3567:                                        NULL,
3568:                                        /*144*/ NULL,
3569:                                        NULL,
3570:                                        NULL,
3571:                                        NULL};

3573: static PetscErrorCode MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat, PetscInt *indices)
3574: {
3575:   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
3576:   PetscInt    i, nz, n;

3578:   PetscFunctionBegin;
3579:   nz = aij->maxnz;
3580:   n  = mat->rmap->n;
3581:   for (i = 0; i < nz; i++) aij->j[i] = indices[i];
3582:   aij->nz = nz;
3583:   for (i = 0; i < n; i++) aij->ilen[i] = aij->imax[i];
3584:   PetscFunctionReturn(PETSC_SUCCESS);
3585: }

3587: /*
3588:  * Given a sparse matrix with global column indices, compact it by using a local column space.
3589:  * The result matrix helps saving memory in other algorithms, such as MatPtAPSymbolic_MPIAIJ_MPIAIJ_scalable()
3590:  */
3591: PetscErrorCode MatSeqAIJCompactOutExtraColumns_SeqAIJ(Mat mat, ISLocalToGlobalMapping *mapping)
3592: {
3593:   Mat_SeqAIJ   *aij = (Mat_SeqAIJ *)mat->data;
3594:   PetscHMapI    gid1_lid1;
3595:   PetscHashIter tpos;
3596:   PetscInt      gid, lid, i, ec, nz = aij->nz;
3597:   PetscInt     *garray, *jj = aij->j;

3599:   PetscFunctionBegin;
3601:   PetscAssertPointer(mapping, 2);
3602:   /* use a table */
3603:   PetscCall(PetscHMapICreateWithSize(mat->rmap->n, &gid1_lid1));
3604:   ec = 0;
3605:   for (i = 0; i < nz; i++) {
3606:     PetscInt data, gid1 = jj[i] + 1;
3607:     PetscCall(PetscHMapIGetWithDefault(gid1_lid1, gid1, 0, &data));
3608:     if (!data) {
3609:       /* one based table */
3610:       PetscCall(PetscHMapISet(gid1_lid1, gid1, ++ec));
3611:     }
3612:   }
3613:   /* form array of columns we need */
3614:   PetscCall(PetscMalloc1(ec, &garray));
3615:   PetscHashIterBegin(gid1_lid1, tpos);
3616:   while (!PetscHashIterAtEnd(gid1_lid1, tpos)) {
3617:     PetscHashIterGetKey(gid1_lid1, tpos, gid);
3618:     PetscHashIterGetVal(gid1_lid1, tpos, lid);
3619:     PetscHashIterNext(gid1_lid1, tpos);
3620:     gid--;
3621:     lid--;
3622:     garray[lid] = gid;
3623:   }
3624:   PetscCall(PetscSortInt(ec, garray)); /* sort, and rebuild */
3625:   PetscCall(PetscHMapIClear(gid1_lid1));
3626:   for (i = 0; i < ec; i++) PetscCall(PetscHMapISet(gid1_lid1, garray[i] + 1, i + 1));
3627:   /* compact out the extra columns in B */
3628:   for (i = 0; i < nz; i++) {
3629:     PetscInt gid1 = jj[i] + 1;
3630:     PetscCall(PetscHMapIGetWithDefault(gid1_lid1, gid1, 0, &lid));
3631:     lid--;
3632:     jj[i] = lid;
3633:   }
3634:   PetscCall(PetscLayoutDestroy(&mat->cmap));
3635:   PetscCall(PetscHMapIDestroy(&gid1_lid1));
3636:   PetscCall(PetscLayoutCreateFromSizes(PetscObjectComm((PetscObject)mat), ec, ec, 1, &mat->cmap));
3637:   PetscCall(ISLocalToGlobalMappingCreate(PETSC_COMM_SELF, mat->cmap->bs, mat->cmap->n, garray, PETSC_OWN_POINTER, mapping));
3638:   PetscCall(ISLocalToGlobalMappingSetType(*mapping, ISLOCALTOGLOBALMAPPINGHASH));
3639:   PetscFunctionReturn(PETSC_SUCCESS);
3640: }

3642: /*@
3643:   MatSeqAIJSetColumnIndices - Set the column indices for all the rows
3644:   in the matrix.

3646:   Input Parameters:
3647: + mat     - the `MATSEQAIJ` matrix
3648: - indices - the column indices

3650:   Level: advanced

3652:   Notes:
3653:   This can be called if you have precomputed the nonzero structure of the
3654:   matrix and want to provide it to the matrix object to improve the performance
3655:   of the `MatSetValues()` operation.

3657:   You MUST have set the correct numbers of nonzeros per row in the call to
3658:   `MatCreateSeqAIJ()`, and the columns indices MUST be sorted.

3660:   MUST be called before any calls to `MatSetValues()`

3662:   The indices should start with zero, not one.

3664: .seealso: [](ch_matrices), `Mat`, `MATSEQAIJ`
3665: @*/
3666: PetscErrorCode MatSeqAIJSetColumnIndices(Mat mat, PetscInt *indices)
3667: {
3668:   PetscFunctionBegin;
3670:   PetscAssertPointer(indices, 2);
3671:   PetscUseMethod(mat, "MatSeqAIJSetColumnIndices_C", (Mat, PetscInt *), (mat, indices));
3672:   PetscFunctionReturn(PETSC_SUCCESS);
3673: }

3675: static PetscErrorCode MatStoreValues_SeqAIJ(Mat mat)
3676: {
3677:   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
3678:   size_t      nz  = aij->i[mat->rmap->n];

3680:   PetscFunctionBegin;
3681:   PetscCheck(aij->nonew, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");

3683:   /* allocate space for values if not already there */
3684:   if (!aij->saved_values) PetscCall(PetscMalloc1(nz + 1, &aij->saved_values));

3686:   /* copy values over */
3687:   PetscCall(PetscArraycpy(aij->saved_values, aij->a, nz));
3688:   PetscFunctionReturn(PETSC_SUCCESS);
3689: }

3691: /*@
3692:   MatStoreValues - Stashes a copy of the matrix values; this allows reusing of the linear part of a Jacobian, while recomputing only the
3693:   nonlinear portion.

3695:   Logically Collect

3697:   Input Parameter:
3698: . mat - the matrix (currently only `MATAIJ` matrices support this option)

3700:   Level: advanced

3702:   Example Usage:
3703: .vb
3704:     Using SNES
3705:     Create Jacobian matrix
3706:     Set linear terms into matrix
3707:     Apply boundary conditions to matrix, at this time matrix must have
3708:       final nonzero structure (i.e. setting the nonlinear terms and applying
3709:       boundary conditions again will not change the nonzero structure
3710:     MatSetOption(mat, MAT_NEW_NONZERO_LOCATIONS, PETSC_FALSE);
3711:     MatStoreValues(mat);
3712:     Call SNESSetJacobian() with matrix
3713:     In your Jacobian routine
3714:       MatRetrieveValues(mat);
3715:       Set nonlinear terms in matrix

3717:     Without `SNESSolve()`, i.e. when you handle nonlinear solve yourself:
3718:     // build linear portion of Jacobian
3719:     MatSetOption(mat, MAT_NEW_NONZERO_LOCATIONS, PETSC_FALSE);
3720:     MatStoreValues(mat);
3721:     loop over nonlinear iterations
3722:        MatRetrieveValues(mat);
3723:        // call MatSetValues(mat,...) to set nonliner portion of Jacobian
3724:        // call MatAssemblyBegin/End() on matrix
3725:        Solve linear system with Jacobian
3726:     endloop
3727: .ve

3729:   Notes:
3730:   Matrix must already be assembled before calling this routine
3731:   Must set the matrix option `MatSetOption`(mat,`MAT_NEW_NONZERO_LOCATIONS`,`PETSC_FALSE`); before
3732:   calling this routine.

3734:   When this is called multiple times it overwrites the previous set of stored values
3735:   and does not allocated additional space.

3737: .seealso: [](ch_matrices), `Mat`, `MatRetrieveValues()`
3738: @*/
3739: PetscErrorCode MatStoreValues(Mat mat)
3740: {
3741:   PetscFunctionBegin;
3743:   PetscCheck(mat->assembled, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for unassembled matrix");
3744:   PetscCheck(!mat->factortype, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
3745:   PetscUseMethod(mat, "MatStoreValues_C", (Mat), (mat));
3746:   PetscFunctionReturn(PETSC_SUCCESS);
3747: }

3749: static PetscErrorCode MatRetrieveValues_SeqAIJ(Mat mat)
3750: {
3751:   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
3752:   PetscInt    nz  = aij->i[mat->rmap->n];

3754:   PetscFunctionBegin;
3755:   PetscCheck(aij->nonew, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3756:   PetscCheck(aij->saved_values, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Must call MatStoreValues(A);first");
3757:   /* copy values over */
3758:   PetscCall(PetscArraycpy(aij->a, aij->saved_values, nz));
3759:   PetscFunctionReturn(PETSC_SUCCESS);
3760: }

3762: /*@
3763:   MatRetrieveValues - Retrieves the copy of the matrix values that was stored with `MatStoreValues()`

3765:   Logically Collect

3767:   Input Parameter:
3768: . mat - the matrix (currently only `MATAIJ` matrices support this option)

3770:   Level: advanced

3772: .seealso: [](ch_matrices), `Mat`, `MatStoreValues()`
3773: @*/
3774: PetscErrorCode MatRetrieveValues(Mat mat)
3775: {
3776:   PetscFunctionBegin;
3778:   PetscCheck(mat->assembled, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for unassembled matrix");
3779:   PetscCheck(!mat->factortype, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
3780:   PetscUseMethod(mat, "MatRetrieveValues_C", (Mat), (mat));
3781:   PetscCall(PetscObjectStateIncrease((PetscObject)mat));
3782:   PetscFunctionReturn(PETSC_SUCCESS);
3783: }

3785: /*@
3786:   MatCreateSeqAIJ - Creates a sparse matrix in `MATSEQAIJ` (compressed row) format
3787:   (the default parallel PETSc format).  For good matrix assembly performance
3788:   the user should preallocate the matrix storage by setting the parameter `nz`
3789:   (or the array `nnz`).

3791:   Collective

3793:   Input Parameters:
3794: + comm - MPI communicator, set to `PETSC_COMM_SELF`
3795: . m    - number of rows
3796: . n    - number of columns
3797: . nz   - number of nonzeros per row (same for all rows)
3798: - nnz  - array containing the number of nonzeros in the various rows
3799:          (possibly different for each row) or NULL

3801:   Output Parameter:
3802: . A - the matrix

3804:   Options Database Keys:
3805: + -mat_no_inode          - Do not use inodes
3806: - -mat_inode_limit limit - Sets inode limit (max limit=5)

3808:   Level: intermediate

3810:   Notes:
3811:   It is recommend to use `MatCreateFromOptions()` instead of this routine

3813:   If `nnz` is given then `nz` is ignored

3815:   The `MATSEQAIJ` format, also called
3816:   compressed row storage, is fully compatible with standard Fortran
3817:   storage.  That is, the stored row and column indices can begin at
3818:   either one (as in Fortran) or zero.

3820:   Specify the preallocated storage with either `nz` or `nnz` (not both).
3821:   Set `nz` = `PETSC_DEFAULT` and `nnz` = `NULL` for PETSc to control dynamic memory
3822:   allocation.

3824:   By default, this format uses inodes (identical nodes) when possible, to
3825:   improve numerical efficiency of matrix-vector products and solves. We
3826:   search for consecutive rows with the same nonzero structure, thereby
3827:   reusing matrix information to achieve increased efficiency.

3829: .seealso: [](ch_matrices), `Mat`, [Sparse Matrix Creation](sec_matsparse), `MatCreate()`, `MatCreateAIJ()`, `MatSetValues()`, `MatSeqAIJSetColumnIndices()`, `MatCreateSeqAIJWithArrays()`
3830: @*/
3831: PetscErrorCode MatCreateSeqAIJ(MPI_Comm comm, PetscInt m, PetscInt n, PetscInt nz, const PetscInt nnz[], Mat *A)
3832: {
3833:   PetscFunctionBegin;
3834:   PetscCall(MatCreate(comm, A));
3835:   PetscCall(MatSetSizes(*A, m, n, m, n));
3836:   PetscCall(MatSetType(*A, MATSEQAIJ));
3837:   PetscCall(MatSeqAIJSetPreallocation_SeqAIJ(*A, nz, nnz));
3838:   PetscFunctionReturn(PETSC_SUCCESS);
3839: }

3841: /*@
3842:   MatSeqAIJSetPreallocation - For good matrix assembly performance
3843:   the user should preallocate the matrix storage by setting the parameter nz
3844:   (or the array nnz).  By setting these parameters accurately, performance
3845:   during matrix assembly can be increased by more than a factor of 50.

3847:   Collective

3849:   Input Parameters:
3850: + B   - The matrix
3851: . nz  - number of nonzeros per row (same for all rows)
3852: - nnz - array containing the number of nonzeros in the various rows
3853:          (possibly different for each row) or NULL

3855:   Options Database Keys:
3856: + -mat_no_inode          - Do not use inodes
3857: - -mat_inode_limit limit - Sets inode limit (max limit=5)

3859:   Level: intermediate

3861:   Notes:
3862:   If `nnz` is given then `nz` is ignored

3864:   The `MATSEQAIJ` format also called
3865:   compressed row storage, is fully compatible with standard Fortran
3866:   storage.  That is, the stored row and column indices can begin at
3867:   either one (as in Fortran) or zero.  See the users' manual for details.

3869:   Specify the preallocated storage with either `nz` or `nnz` (not both).
3870:   Set nz = `PETSC_DEFAULT` and `nnz` = `NULL` for PETSc to control dynamic memory
3871:   allocation.

3873:   You can call `MatGetInfo()` to get information on how effective the preallocation was;
3874:   for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
3875:   You can also run with the option -info and look for messages with the string
3876:   malloc in them to see if additional memory allocation was needed.

3878:   Developer Notes:
3879:   Use nz of `MAT_SKIP_ALLOCATION` to not allocate any space for the matrix
3880:   entries or columns indices

3882:   By default, this format uses inodes (identical nodes) when possible, to
3883:   improve numerical efficiency of matrix-vector products and solves. We
3884:   search for consecutive rows with the same nonzero structure, thereby
3885:   reusing matrix information to achieve increased efficiency.

3887: .seealso: [](ch_matrices), `Mat`, `MatCreate()`, `MatCreateAIJ()`, `MatSetValues()`, `MatSeqAIJSetColumnIndices()`, `MatCreateSeqAIJWithArrays()`, `MatGetInfo()`,
3888:           `MatSeqAIJSetTotalPreallocation()`
3889: @*/
3890: PetscErrorCode MatSeqAIJSetPreallocation(Mat B, PetscInt nz, const PetscInt nnz[])
3891: {
3892:   PetscFunctionBegin;
3895:   PetscTryMethod(B, "MatSeqAIJSetPreallocation_C", (Mat, PetscInt, const PetscInt[]), (B, nz, nnz));
3896:   PetscFunctionReturn(PETSC_SUCCESS);
3897: }

3899: PetscErrorCode MatSeqAIJSetPreallocation_SeqAIJ(Mat B, PetscInt nz, const PetscInt *nnz)
3900: {
3901:   Mat_SeqAIJ *b              = (Mat_SeqAIJ *)B->data;
3902:   PetscBool   skipallocation = PETSC_FALSE, realalloc = PETSC_FALSE;
3903:   PetscInt    i;

3905:   PetscFunctionBegin;
3906:   if (B->hash_active) {
3907:     B->ops[0] = b->cops;
3908:     PetscCall(PetscHMapIJVDestroy(&b->ht));
3909:     PetscCall(PetscFree(b->dnz));
3910:     B->hash_active = PETSC_FALSE;
3911:   }
3912:   if (nz >= 0 || nnz) realalloc = PETSC_TRUE;
3913:   if (nz == MAT_SKIP_ALLOCATION) {
3914:     skipallocation = PETSC_TRUE;
3915:     nz             = 0;
3916:   }
3917:   PetscCall(PetscLayoutSetUp(B->rmap));
3918:   PetscCall(PetscLayoutSetUp(B->cmap));

3920:   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
3921:   PetscCheck(nz >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "nz cannot be less than 0: value %" PetscInt_FMT, nz);
3922:   if (nnz) {
3923:     for (i = 0; i < B->rmap->n; i++) {
3924:       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]);
3925:       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);
3926:     }
3927:   }

3929:   B->preallocated = PETSC_TRUE;
3930:   if (!skipallocation) {
3931:     if (!b->imax) PetscCall(PetscMalloc1(B->rmap->n, &b->imax));
3932:     if (!b->ilen) {
3933:       /* b->ilen will count nonzeros in each row so far. */
3934:       PetscCall(PetscCalloc1(B->rmap->n, &b->ilen));
3935:     } else {
3936:       PetscCall(PetscMemzero(b->ilen, B->rmap->n * sizeof(PetscInt)));
3937:     }
3938:     if (!b->ipre) PetscCall(PetscMalloc1(B->rmap->n, &b->ipre));
3939:     if (!nnz) {
3940:       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
3941:       else if (nz < 0) nz = 1;
3942:       nz = PetscMin(nz, B->cmap->n);
3943:       for (i = 0; i < B->rmap->n; i++) b->imax[i] = nz;
3944:       PetscCall(PetscIntMultError(nz, B->rmap->n, &nz));
3945:     } else {
3946:       PetscInt64 nz64 = 0;
3947:       for (i = 0; i < B->rmap->n; i++) {
3948:         b->imax[i] = nnz[i];
3949:         nz64 += nnz[i];
3950:       }
3951:       PetscCall(PetscIntCast(nz64, &nz));
3952:     }

3954:     /* allocate the matrix space */
3955:     PetscCall(MatSeqXAIJFreeAIJ(B, &b->a, &b->j, &b->i));
3956:     PetscCall(PetscShmgetAllocateArray(nz, sizeof(PetscInt), (void **)&b->j));
3957:     PetscCall(PetscShmgetAllocateArray(B->rmap->n + 1, sizeof(PetscInt), (void **)&b->i));
3958:     b->free_ij = PETSC_TRUE;
3959:     if (B->structure_only) {
3960:       b->free_a = PETSC_FALSE;
3961:     } else {
3962:       PetscCall(PetscShmgetAllocateArray(nz, sizeof(PetscScalar), (void **)&b->a));
3963:       b->free_a = PETSC_TRUE;
3964:     }
3965:     b->i[0] = 0;
3966:     for (i = 1; i < B->rmap->n + 1; i++) b->i[i] = b->i[i - 1] + b->imax[i - 1];
3967:   } else {
3968:     b->free_a  = PETSC_FALSE;
3969:     b->free_ij = PETSC_FALSE;
3970:   }

3972:   if (b->ipre && nnz != b->ipre && b->imax) {
3973:     /* reserve user-requested sparsity */
3974:     PetscCall(PetscArraycpy(b->ipre, b->imax, B->rmap->n));
3975:   }

3977:   b->nz               = 0;
3978:   b->maxnz            = nz;
3979:   B->info.nz_unneeded = (double)b->maxnz;
3980:   if (realalloc) PetscCall(MatSetOption(B, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_TRUE));
3981:   B->was_assembled = PETSC_FALSE;
3982:   B->assembled     = PETSC_FALSE;
3983:   /* We simply deem preallocation has changed nonzero state. Updating the state
3984:      will give clients (like AIJKokkos) a chance to know something has happened.
3985:   */
3986:   B->nonzerostate++;
3987:   PetscFunctionReturn(PETSC_SUCCESS);
3988: }

3990: PetscErrorCode MatResetPreallocation_SeqAIJ_Private(Mat A, PetscBool *memoryreset)
3991: {
3992:   Mat_SeqAIJ *a;
3993:   PetscInt    i;
3994:   PetscBool   skipreset;

3996:   PetscFunctionBegin;

3999:   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()");
4000:   if (A->num_ass == 0) PetscFunctionReturn(PETSC_SUCCESS);

4002:   /* Check local size. If zero, then return */
4003:   if (!A->rmap->n) PetscFunctionReturn(PETSC_SUCCESS);

4005:   a = (Mat_SeqAIJ *)A->data;
4006:   /* if no saved info, we error out */
4007:   PetscCheck(a->ipre, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "No saved preallocation info ");

4009:   PetscCheck(a->i && a->imax && a->ilen, PETSC_COMM_SELF, PETSC_ERR_ARG_NULL, "Memory info is incomplete, and cannot reset preallocation ");

4011:   PetscCall(PetscArraycmp(a->ipre, a->ilen, A->rmap->n, &skipreset));
4012:   if (skipreset) PetscCall(MatZeroEntries(A));
4013:   else {
4014:     PetscCall(PetscArraycpy(a->imax, a->ipre, A->rmap->n));
4015:     PetscCall(PetscArrayzero(a->ilen, A->rmap->n));
4016:     a->i[0] = 0;
4017:     for (i = 1; i < A->rmap->n + 1; i++) a->i[i] = a->i[i - 1] + a->imax[i - 1];
4018:     A->preallocated     = PETSC_TRUE;
4019:     a->nz               = 0;
4020:     a->maxnz            = a->i[A->rmap->n];
4021:     A->info.nz_unneeded = (double)a->maxnz;
4022:     A->was_assembled    = PETSC_FALSE;
4023:     A->assembled        = PETSC_FALSE;
4024:     A->nonzerostate++;
4025:     /* Log that the state of this object has changed; this will help guarantee that preconditioners get re-setup */
4026:     PetscCall(PetscObjectStateIncrease((PetscObject)A));
4027:   }
4028:   if (memoryreset) *memoryreset = (PetscBool)!skipreset;
4029:   PetscFunctionReturn(PETSC_SUCCESS);
4030: }

4032: static PetscErrorCode MatResetPreallocation_SeqAIJ(Mat A)
4033: {
4034:   PetscFunctionBegin;
4035:   PetscCall(MatResetPreallocation_SeqAIJ_Private(A, NULL));
4036:   PetscFunctionReturn(PETSC_SUCCESS);
4037: }

4039: /*@
4040:   MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in `MATSEQAIJ` format.

4042:   Input Parameters:
4043: + B - the matrix
4044: . i - the indices into `j` for the start of each row (indices start with zero)
4045: . j - the column indices for each row (indices start with zero) these must be sorted for each row
4046: - v - optional values in the matrix, use `NULL` if not provided

4048:   Level: developer

4050:   Notes:
4051:   The `i`,`j`,`v` values are COPIED with this routine; to avoid the copy use `MatCreateSeqAIJWithArrays()`

4053:   This routine may be called multiple times with different nonzero patterns (or the same nonzero pattern). The nonzero
4054:   structure will be the union of all the previous nonzero structures.

4056:   Developer Notes:
4057:   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
4058:   then just copies the `v` values directly with `PetscMemcpy()`.

4060:   This routine could also take a `PetscCopyMode` argument to allow sharing the values instead of always copying them.

4062: .seealso: [](ch_matrices), `Mat`, `MatCreate()`, `MatCreateSeqAIJ()`, `MatSetValues()`, `MatSeqAIJSetPreallocation()`, `MATSEQAIJ`, `MatResetPreallocation()`
4063: @*/
4064: PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B, const PetscInt i[], const PetscInt j[], const PetscScalar v[])
4065: {
4066:   PetscFunctionBegin;
4069:   PetscTryMethod(B, "MatSeqAIJSetPreallocationCSR_C", (Mat, const PetscInt[], const PetscInt[], const PetscScalar[]), (B, i, j, v));
4070:   PetscFunctionReturn(PETSC_SUCCESS);
4071: }

4073: static PetscErrorCode MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B, const PetscInt Ii[], const PetscInt J[], const PetscScalar v[])
4074: {
4075:   PetscInt  i;
4076:   PetscInt  m, n;
4077:   PetscInt  nz;
4078:   PetscInt *nnz;

4080:   PetscFunctionBegin;
4081:   PetscCheck(Ii[0] == 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %" PetscInt_FMT, Ii[0]);

4083:   PetscCall(PetscLayoutSetUp(B->rmap));
4084:   PetscCall(PetscLayoutSetUp(B->cmap));

4086:   PetscCall(MatGetSize(B, &m, &n));
4087:   PetscCall(PetscMalloc1(m + 1, &nnz));
4088:   for (i = 0; i < m; i++) {
4089:     nz = Ii[i + 1] - Ii[i];
4090:     PetscCheck(nz >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Local row %" PetscInt_FMT " has a negative number of columns %" PetscInt_FMT, i, nz);
4091:     nnz[i] = nz;
4092:   }
4093:   PetscCall(MatSeqAIJSetPreallocation(B, 0, nnz));
4094:   PetscCall(PetscFree(nnz));

4096:   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));

4098:   PetscCall(MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY));
4099:   PetscCall(MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY));

4101:   PetscCall(MatSetOption(B, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
4102:   PetscFunctionReturn(PETSC_SUCCESS);
4103: }

4105: /*@
4106:   MatSeqAIJKron - Computes `C`, the Kronecker product of `A` and `B`.

4108:   Input Parameters:
4109: + A     - left-hand side matrix
4110: . B     - right-hand side matrix
4111: - reuse - either `MAT_INITIAL_MATRIX` or `MAT_REUSE_MATRIX`

4113:   Output Parameter:
4114: . C - Kronecker product of `A` and `B`

4116:   Level: intermediate

4118:   Note:
4119:   `MAT_REUSE_MATRIX` can only be used when the nonzero structure of the product matrix has not changed from that last call to `MatSeqAIJKron()`.

4121: .seealso: [](ch_matrices), `Mat`, `MatCreateSeqAIJ()`, `MATSEQAIJ`, `MATKAIJ`, `MatReuse`
4122: @*/
4123: PetscErrorCode MatSeqAIJKron(Mat A, Mat B, MatReuse reuse, Mat *C)
4124: {
4125:   PetscFunctionBegin;
4130:   PetscAssertPointer(C, 4);
4131:   if (reuse == MAT_REUSE_MATRIX) {
4134:   }
4135:   PetscTryMethod(A, "MatSeqAIJKron_C", (Mat, Mat, MatReuse, Mat *), (A, B, reuse, C));
4136:   PetscFunctionReturn(PETSC_SUCCESS);
4137: }

4139: static PetscErrorCode MatSeqAIJKron_SeqAIJ(Mat A, Mat B, MatReuse reuse, Mat *C)
4140: {
4141:   Mat                newmat;
4142:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data;
4143:   Mat_SeqAIJ        *b = (Mat_SeqAIJ *)B->data;
4144:   PetscScalar       *v;
4145:   const PetscScalar *aa, *ba;
4146:   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;
4147:   PetscBool          flg;

4149:   PetscFunctionBegin;
4150:   PetscCheck(!A->factortype, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
4151:   PetscCheck(A->assembled, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for unassembled matrix");
4152:   PetscCheck(!B->factortype, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
4153:   PetscCheck(B->assembled, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for unassembled matrix");
4154:   PetscCall(PetscObjectTypeCompare((PetscObject)B, MATSEQAIJ, &flg));
4155:   PetscCheck(flg, PETSC_COMM_SELF, PETSC_ERR_SUP, "MatType %s", ((PetscObject)B)->type_name);
4156:   PetscCheck(reuse == MAT_INITIAL_MATRIX || reuse == MAT_REUSE_MATRIX, PETSC_COMM_SELF, PETSC_ERR_SUP, "MatReuse %d", (int)reuse);
4157:   if (reuse == MAT_INITIAL_MATRIX) {
4158:     PetscCall(PetscMalloc2(am * bm + 1, &i, a->i[am] * b->i[bm], &j));
4159:     PetscCall(MatCreate(PETSC_COMM_SELF, &newmat));
4160:     PetscCall(MatSetSizes(newmat, am * bm, an * bn, am * bm, an * bn));
4161:     PetscCall(MatSetType(newmat, MATAIJ));
4162:     i[0] = 0;
4163:     for (m = 0; m < am; ++m) {
4164:       for (p = 0; p < bm; ++p) {
4165:         i[m * bm + p + 1] = i[m * bm + p] + (a->i[m + 1] - a->i[m]) * (b->i[p + 1] - b->i[p]);
4166:         for (n = a->i[m]; n < a->i[m + 1]; ++n) {
4167:           for (q = b->i[p]; q < b->i[p + 1]; ++q) j[nnz++] = a->j[n] * bn + b->j[q];
4168:         }
4169:       }
4170:     }
4171:     PetscCall(MatSeqAIJSetPreallocationCSR(newmat, i, j, NULL));
4172:     *C = newmat;
4173:     PetscCall(PetscFree2(i, j));
4174:     nnz = 0;
4175:   }
4176:   PetscCall(MatSeqAIJGetArray(*C, &v));
4177:   PetscCall(MatSeqAIJGetArrayRead(A, &aa));
4178:   PetscCall(MatSeqAIJGetArrayRead(B, &ba));
4179:   for (m = 0; m < am; ++m) {
4180:     for (p = 0; p < bm; ++p) {
4181:       for (n = a->i[m]; n < a->i[m + 1]; ++n) {
4182:         for (q = b->i[p]; q < b->i[p + 1]; ++q) v[nnz++] = aa[n] * ba[q];
4183:       }
4184:     }
4185:   }
4186:   PetscCall(MatSeqAIJRestoreArray(*C, &v));
4187:   PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
4188:   PetscCall(MatSeqAIJRestoreArrayRead(B, &ba));
4189:   PetscFunctionReturn(PETSC_SUCCESS);
4190: }

4192: #include <../src/mat/impls/dense/seq/dense.h>
4193: #include <petsc/private/kernels/petscaxpy.h>

4195: /*
4196:     Computes (B'*A')' since computing B*A directly is untenable

4198:                n                       p                          p
4199:         [             ]       [             ]         [                 ]
4200:       m [      A      ]  *  n [       B     ]   =   m [         C       ]
4201:         [             ]       [             ]         [                 ]

4203: */
4204: PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A, Mat B, Mat C)
4205: {
4206:   Mat_SeqDense      *sub_a = (Mat_SeqDense *)A->data;
4207:   Mat_SeqAIJ        *sub_b = (Mat_SeqAIJ *)B->data;
4208:   Mat_SeqDense      *sub_c = (Mat_SeqDense *)C->data;
4209:   PetscInt           i, j, n, m, q, p;
4210:   const PetscInt    *ii, *idx;
4211:   const PetscScalar *b, *a, *a_q;
4212:   PetscScalar       *c, *c_q;
4213:   PetscInt           clda = sub_c->lda;
4214:   PetscInt           alda = sub_a->lda;

4216:   PetscFunctionBegin;
4217:   m = A->rmap->n;
4218:   n = A->cmap->n;
4219:   p = B->cmap->n;
4220:   a = sub_a->v;
4221:   b = sub_b->a;
4222:   c = sub_c->v;
4223:   if (clda == m) {
4224:     PetscCall(PetscArrayzero(c, m * p));
4225:   } else {
4226:     for (j = 0; j < p; j++)
4227:       for (i = 0; i < m; i++) c[j * clda + i] = 0.0;
4228:   }
4229:   ii  = sub_b->i;
4230:   idx = sub_b->j;
4231:   for (i = 0; i < n; i++) {
4232:     q = ii[i + 1] - ii[i];
4233:     while (q-- > 0) {
4234:       c_q = c + clda * (*idx);
4235:       a_q = a + alda * i;
4236:       PetscKernelAXPY(c_q, *b, a_q, m);
4237:       idx++;
4238:       b++;
4239:     }
4240:   }
4241:   PetscFunctionReturn(PETSC_SUCCESS);
4242: }

4244: PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A, Mat B, PetscReal fill, Mat C)
4245: {
4246:   PetscInt  m = A->rmap->n, n = B->cmap->n;
4247:   PetscBool cisdense;

4249:   PetscFunctionBegin;
4250:   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);
4251:   PetscCall(MatSetSizes(C, m, n, m, n));
4252:   PetscCall(MatSetBlockSizesFromMats(C, A, B));
4253:   PetscCall(PetscObjectTypeCompareAny((PetscObject)C, &cisdense, MATSEQDENSE, MATSEQDENSECUDA, MATSEQDENSEHIP, ""));
4254:   if (!cisdense) {
4255:     PetscCall(MatSetType(C, MATDENSE));
4256:     PetscCall(MatSetVecType(C, A->defaultvectype));
4257:   }
4258:   PetscCall(MatSetUp(C));

4260:   C->ops->matmultnumeric = MatMatMultNumeric_SeqDense_SeqAIJ;
4261:   PetscFunctionReturn(PETSC_SUCCESS);
4262: }

4264: /*MC
4265:    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
4266:    based on compressed sparse row format.

4268:    Options Database Key:
4269: . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()

4271:    Level: beginner

4273:    Notes:
4274:     `MatSetValues()` may be called with a `NULL` argument for the numerical values to insert zeros at the supplied row and column indices.

4276:     Call `MatSetOption(A, MAT_STRUCTURE_ONLY, PETSC_TRUE)` before preallocation or `MatSetUp()` to store only the nonzero pattern.
4277:     The assembled matrix has no numerical value array. Row and column indices supplied during insertion are retained, while numerical values are ignored.
4278:     Such matrices can be used for structural operations, but not for numerical operations.

4280:   Developer Note:
4281:     It would be nice if all matrix formats supported passing `NULL` in for the numerical values

4283: .seealso: [](ch_matrices), `Mat`, `MatCreateSeqAIJ()`, `MatSetFromOptions()`, `MatSetType()`, `MatCreate()`, `MatType`, `MATSELL`, `MATSEQSELL`, `MATMPISELL`
4284: M*/

4286: /*MC
4287:    MATAIJ - MATAIJ = "aij" - A matrix type to be used for sparse matrices.

4289:    This matrix type is identical to `MATSEQAIJ` when constructed with a single process communicator,
4290:    and `MATMPIAIJ` otherwise.  As a result, for single process communicators,
4291:    `MatSeqAIJSetPreallocation()` is supported, and similarly `MatMPIAIJSetPreallocation()` is supported
4292:    for communicators controlling multiple processes.  It is recommended that you call both of
4293:    the above preallocation routines for simplicity.

4295:    Options Database Key:
4296: . -mat_type aij - sets the matrix type to "aij" during a call to `MatSetFromOptions()`

4298:   Level: beginner

4300:    Notes:
4301:    Call `MatSetOption(A, MAT_STRUCTURE_ONLY, PETSC_TRUE)` before preallocation or `MatSetUp()` to store only the nonzero pattern.
4302:    The assembled matrix has no numerical value array. Row and column indices supplied during insertion are retained, while numerical values are ignored.
4303:    Such matrices can be used for structural operations, but not for numerical operations.

4305:    Subclasses include `MATAIJCUSPARSE`, `MATAIJPERM`, `MATAIJSELL`, `MATAIJMKL`, `MATAIJCRL`, and also automatically switches over to use inodes when
4306:    enough exist.

4308: .seealso: [](ch_matrices), `Mat`, `MatCreateAIJ()`, `MatCreateSeqAIJ()`, `MATSEQAIJ`, `MATMPIAIJ`, `MATSELL`, `MATSEQSELL`, `MATMPISELL`
4309: M*/

4311: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCRL(Mat, MatType, MatReuse, Mat *);
4312: #if PetscDefined(HAVE_ELEMENTAL)
4313: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_Elemental(Mat, MatType, MatReuse, Mat *);
4314: #endif
4315: #if PetscDefined(HAVE_SCALAPACK) && (PetscDefined(USE_REAL_SINGLE) || PetscDefined(USE_REAL_DOUBLE))
4316: PETSC_INTERN PetscErrorCode MatConvert_AIJ_ScaLAPACK(Mat, MatType, MatReuse, Mat *);
4317: #endif
4318: #if PetscDefined(HAVE_HYPRE)
4319: PETSC_INTERN PetscErrorCode MatConvert_AIJ_HYPRE(Mat A, MatType, MatReuse, Mat *);
4320: #endif

4322: PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_SeqSELL(Mat, MatType, MatReuse, Mat *);
4323: PETSC_INTERN PetscErrorCode MatConvert_XAIJ_IS(Mat, MatType, MatReuse, Mat *);
4324: PETSC_INTERN PetscErrorCode MatProductSetFromOptions_IS_XAIJ(Mat);

4326: /*@
4327:   MatSeqAIJGetArray - gives read/write access to the array where the data for a `MATSEQAIJ` matrix is stored

4329:   Not Collective

4331:   Input Parameter:
4332: . A - a `MATSEQAIJ` matrix

4334:   Output Parameter:
4335: . array - pointer to the data

4337:   Level: intermediate

4339: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJRestoreArray()`
4340: @*/
4341: PetscErrorCode MatSeqAIJGetArray(Mat A, PetscScalar *array[])
4342: {
4343:   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;

4345:   PetscFunctionBegin;
4346:   if (A->structure_only || aij->ops->getarray == NULL) *array = aij->a;
4347:   else PetscCall((*aij->ops->getarray)(A, array));
4348:   PetscFunctionReturn(PETSC_SUCCESS);
4349: }

4351: /*@
4352:   MatSeqAIJRestoreArray - returns access to the array where the data for a `MATSEQAIJ` matrix is stored obtained by `MatSeqAIJGetArray()`

4354:   Not Collective

4356:   Input Parameters:
4357: + A     - a `MATSEQAIJ` matrix
4358: - array - pointer to the data

4360:   Level: intermediate

4362: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJGetArray()`
4363: @*/
4364: PetscErrorCode MatSeqAIJRestoreArray(Mat A, PetscScalar *array[])
4365: {
4366:   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;

4368:   PetscFunctionBegin;
4369:   if (A->structure_only || aij->ops->restorearray == NULL) *array = NULL;
4370:   else PetscCall((*aij->ops->restorearray)(A, array));
4371:   PetscCall(PetscObjectStateIncrease((PetscObject)A));
4372:   PetscFunctionReturn(PETSC_SUCCESS);
4373: }

4375: /*@
4376:   MatSeqAIJGetArrayRead - gives read-only access to the array where the data for a `MATSEQAIJ` matrix is stored

4378:   Not Collective

4380:   Input Parameter:
4381: . A - a `MATSEQAIJ` matrix

4383:   Output Parameter:
4384: . array - pointer to the data

4386:   Level: intermediate

4388: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJGetArray()`, `MatSeqAIJRestoreArrayRead()`
4389: @*/
4390: PetscErrorCode MatSeqAIJGetArrayRead(Mat A, const PetscScalar *array[])
4391: {
4392:   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;

4394:   PetscFunctionBegin;
4395:   if (A->structure_only || aij->ops->getarrayread == NULL) *array = aij->a;
4396:   else PetscCall((*aij->ops->getarrayread)(A, array));
4397:   PetscFunctionReturn(PETSC_SUCCESS);
4398: }

4400: /*@
4401:   MatSeqAIJRestoreArrayRead - restore the read-only access array obtained from `MatSeqAIJGetArrayRead()`

4403:   Not Collective

4405:   Input Parameter:
4406: . A - a `MATSEQAIJ` matrix

4408:   Output Parameter:
4409: . array - pointer to the data

4411:   Level: intermediate

4413: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJGetArray()`, `MatSeqAIJGetArrayRead()`
4414: @*/
4415: PetscErrorCode MatSeqAIJRestoreArrayRead(Mat A, const PetscScalar *array[])
4416: {
4417:   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;

4419:   PetscFunctionBegin;
4420:   if (A->structure_only || aij->ops->restorearrayread == NULL) *array = NULL;
4421:   else PetscCall((*aij->ops->restorearrayread)(A, array));
4422:   PetscFunctionReturn(PETSC_SUCCESS);
4423: }

4425: /*@
4426:   MatSeqAIJGetArrayWrite - gives write-only access to the array where the data for a `MATSEQAIJ` matrix is stored

4428:   Not Collective

4430:   Input Parameter:
4431: . A - a `MATSEQAIJ` matrix

4433:   Output Parameter:
4434: . array - pointer to the data

4436:   Level: intermediate

4438: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJGetArray()`, `MatSeqAIJRestoreArrayWrite()`
4439: @*/
4440: PetscErrorCode MatSeqAIJGetArrayWrite(Mat A, PetscScalar *array[])
4441: {
4442:   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;

4444:   PetscFunctionBegin;
4445:   if (A->structure_only || aij->ops->getarraywrite == NULL) *array = aij->a;
4446:   else PetscCall((*aij->ops->getarraywrite)(A, array));
4447:   PetscCall(PetscObjectStateIncrease((PetscObject)A));
4448:   PetscFunctionReturn(PETSC_SUCCESS);
4449: }

4451: /*@
4452:   MatSeqAIJRestoreArrayWrite - restore the write-only access array obtained from `MatSeqAIJGetArrayWrite()`

4454:   Not Collective

4456:   Input Parameter:
4457: . A - a `MATSEQAIJ` matrix

4459:   Output Parameter:
4460: . array - pointer to the data

4462:   Level: intermediate

4464: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJGetArray()`, `MatSeqAIJGetArrayWrite()`
4465: @*/
4466: PetscErrorCode MatSeqAIJRestoreArrayWrite(Mat A, PetscScalar *array[])
4467: {
4468:   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;

4470:   PetscFunctionBegin;
4471:   if (A->structure_only || aij->ops->restorearraywrite == NULL) *array = NULL;
4472:   else PetscCall((*aij->ops->restorearraywrite)(A, array));
4473:   PetscFunctionReturn(PETSC_SUCCESS);
4474: }

4476: /*@
4477:   MatSeqAIJGetCSRAndMemType - Get the CSR arrays and the memory type of the `MATSEQAIJ` matrix

4479:   Not Collective; No Fortran Support

4481:   Input Parameter:
4482: . mat - a matrix of type `MATSEQAIJ` or its subclasses

4484:   Output Parameters:
4485: + i     - row map array of the matrix
4486: . j     - column index array of the matrix
4487: . a     - data array of the matrix
4488: - mtype - memory type of the arrays

4490:   Level: developer

4492:   Notes:
4493:   Any of the output parameters can be `NULL`, in which case the corresponding value is not returned.
4494:   If mat is a device matrix, the arrays are on the device. Otherwise, they are on the host.

4496:   One can call this routine on a preallocated but not assembled matrix to just get the memory of the CSR underneath the matrix.
4497:   If the matrix is assembled, the data array `a` is guaranteed to have the latest values of the matrix.

4499: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJGetArray()`, `MatSeqAIJGetArrayRead()`
4500: @*/
4501: PetscErrorCode MatSeqAIJGetCSRAndMemType(Mat mat, const PetscInt *i[], const PetscInt *j[], PetscScalar *a[], PetscMemType *mtype)
4502: {
4503:   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;

4505:   PetscFunctionBegin;
4506:   PetscCheck(mat->preallocated, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "matrix is not preallocated");
4507:   if (aij->ops->getcsrandmemtype) {
4508:     PetscCall((*aij->ops->getcsrandmemtype)(mat, i, j, a, mtype));
4509:   } else {
4510:     if (i) *i = aij->i;
4511:     if (j) *j = aij->j;
4512:     if (a) *a = aij->a;
4513:     if (mtype) *mtype = PETSC_MEMTYPE_HOST;
4514:   }
4515:   PetscFunctionReturn(PETSC_SUCCESS);
4516: }

4518: /*@
4519:   MatSeqAIJGetMaxRowNonzeros - returns the maximum number of nonzeros in any row

4521:   Not Collective

4523:   Input Parameter:
4524: . A - a `MATSEQAIJ` matrix

4526:   Output Parameter:
4527: . nz - the maximum number of nonzeros in any row

4529:   Level: intermediate

4531: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJRestoreArray()`
4532: @*/
4533: PetscErrorCode MatSeqAIJGetMaxRowNonzeros(Mat A, PetscInt *nz)
4534: {
4535:   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;

4537:   PetscFunctionBegin;
4538:   *nz = aij->rmax;
4539:   PetscFunctionReturn(PETSC_SUCCESS);
4540: }

4542: static PetscErrorCode MatCOOStructDestroy_SeqAIJ(PetscCtxRt data)
4543: {
4544:   MatCOOStruct_SeqAIJ *coo = *(MatCOOStruct_SeqAIJ **)data;

4546:   PetscFunctionBegin;
4547:   PetscCall(PetscFree(coo->perm));
4548:   PetscCall(PetscFree(coo->jmap));
4549:   PetscCall(PetscFree(coo));
4550:   PetscFunctionReturn(PETSC_SUCCESS);
4551: }

4553: PetscErrorCode MatSetPreallocationCOO_SeqAIJ(Mat mat, PetscCount coo_n, PetscInt coo_i[], PetscInt coo_j[])
4554: {
4555:   MPI_Comm             comm;
4556:   PetscInt            *i, *j;
4557:   PetscInt             M, N, row, iprev;
4558:   PetscCount           k, p, q, nneg, nnz, start, end; /* Index the coo array, so use PetscCount as their type */
4559:   PetscInt            *Ai;                             /* Change to PetscCount once we use it for row pointers */
4560:   PetscInt            *Aj;
4561:   PetscScalar         *Aa     = NULL;
4562:   Mat_SeqAIJ          *seqaij = (Mat_SeqAIJ *)mat->data;
4563:   MatType              rtype;
4564:   PetscCount          *perm, *jmap;
4565:   MatCOOStruct_SeqAIJ *coo;
4566:   PetscBool            isorted;
4567:   PetscBool            hypre;

4569:   PetscFunctionBegin;
4570:   // The CSR storage built below replaces the hash table, so MatSetValues() must use the regular SeqAIJ operations
4571:   if (mat->hash_active) {
4572:     mat->ops[0] = seqaij->cops;
4573:     PetscCall(PetscHMapIJVDestroy(&seqaij->ht));
4574:     PetscCall(PetscFree(seqaij->dnz));
4575:     mat->hash_active = PETSC_FALSE;
4576:   }
4577:   PetscCall(PetscObjectGetComm((PetscObject)mat, &comm));
4578:   PetscCall(MatGetSize(mat, &M, &N));
4579:   i = coo_i;
4580:   j = coo_j;
4581:   PetscCall(PetscMalloc1(coo_n, &perm));

4583:   /* 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) */
4584:   isorted = PETSC_TRUE;
4585:   iprev   = PETSC_INT_MIN;
4586:   for (k = 0; k < coo_n; k++) {
4587:     if (j[k] < 0) i[k] = -1;
4588:     if (isorted) {
4589:       if (i[k] < iprev) isorted = PETSC_FALSE;
4590:       else iprev = i[k];
4591:     }
4592:     perm[k] = k;
4593:   }

4595:   /* Sort by row if not already */
4596:   if (!isorted) PetscCall(PetscSortIntWithIntCountArrayPair(coo_n, i, j, perm));
4597:   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);

4599:   /* Advance k to the first row with a non-negative index */
4600:   for (k = 0; k < coo_n; k++)
4601:     if (i[k] >= 0) break;
4602:   nneg = k;
4603:   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 */
4604:   nnz = 0;                                          /* Total number of unique nonzeros to be counted */
4605:   jmap++;                                           /* Inc jmap by 1 for convenience */

4607:   PetscCall(PetscShmgetAllocateArray(M + 1, sizeof(PetscInt), (void **)&Ai)); /* CSR of A */
4608:   PetscCall(PetscArrayzero(Ai, M + 1));
4609:   PetscCall(PetscShmgetAllocateArray(coo_n - nneg, sizeof(PetscInt), (void **)&Aj)); /* We have at most coo_n-nneg unique nonzeros */

4611:   PetscCall(PetscStrcmp("_internal_COO_mat_for_hypre", ((PetscObject)mat)->name, &hypre));

4613:   /* In each row, sort by column, then unique column indices to get row length */
4614:   Ai++;  /* Inc by 1 for convenience */
4615:   q = 0; /* q-th unique nonzero, with q starting from 0 */
4616:   while (k < coo_n) {
4617:     PetscBool strictly_sorted; // this row is strictly sorted?
4618:     PetscInt  jprev;

4620:     /* get [start,end) indices for this row; also check if cols in this row are strictly sorted */
4621:     row             = i[k];
4622:     start           = k;
4623:     jprev           = PETSC_INT_MIN;
4624:     strictly_sorted = PETSC_TRUE;
4625:     while (k < coo_n && i[k] == row) {
4626:       if (strictly_sorted) {
4627:         if (j[k] <= jprev) strictly_sorted = PETSC_FALSE;
4628:         else jprev = j[k];
4629:       }
4630:       k++;
4631:     }
4632:     end = k;

4634:     /* hack for HYPRE: swap min column to diag so that diagonal values will go first */
4635:     if (hypre) {
4636:       PetscInt  minj    = PETSC_INT_MAX;
4637:       PetscBool hasdiag = PETSC_FALSE;

4639:       if (strictly_sorted) { // fast path to swap the first and the diag
4640:         PetscCount tmp;
4641:         for (p = start; p < end; p++) {
4642:           if (j[p] == row && p != start) {
4643:             j[p]        = j[start]; // swap j[], so that the diagonal value will go first (manipulated by perm[])
4644:             j[start]    = row;
4645:             tmp         = perm[start];
4646:             perm[start] = perm[p]; // also swap perm[] so we can save the call to PetscSortIntWithCountArray() below
4647:             perm[p]     = tmp;
4648:             break;
4649:           }
4650:         }
4651:       } else {
4652:         for (p = start; p < end; p++) {
4653:           hasdiag = (PetscBool)(hasdiag || (j[p] == row));
4654:           minj    = PetscMin(minj, j[p]);
4655:         }

4657:         if (hasdiag) {
4658:           for (p = start; p < end; p++) {
4659:             if (j[p] == minj) j[p] = row;
4660:             else if (j[p] == row) j[p] = minj;
4661:           }
4662:         }
4663:       }
4664:     }
4665:     // sort by columns in a row. perm[] indicates their original order
4666:     if (!strictly_sorted) PetscCall(PetscSortIntWithCountArray(end - start, j + start, perm + start));
4667:     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);

4669:     if (strictly_sorted) { // fast path to set Aj[], jmap[], Ai[], nnz, q
4670:       for (p = start; p < end; p++, q++) {
4671:         Aj[q]   = j[p];
4672:         jmap[q] = 1;
4673:       }
4674:       PetscCall(PetscIntCast(end - start, Ai + row));
4675:       nnz += Ai[row]; // q is already advanced
4676:     } else {
4677:       /* Find number of unique col entries in this row */
4678:       Aj[q]   = j[start]; /* Log the first nonzero in this row */
4679:       jmap[q] = 1;        /* Number of repeats of this nonzero entry */
4680:       Ai[row] = 1;
4681:       nnz++;

4683:       for (p = start + 1; p < end; p++) { /* Scan remaining nonzero in this row */
4684:         if (j[p] != j[p - 1]) {           /* Meet a new nonzero */
4685:           q++;
4686:           jmap[q] = 1;
4687:           Aj[q]   = j[p];
4688:           Ai[row]++;
4689:           nnz++;
4690:         } else {
4691:           jmap[q]++;
4692:         }
4693:       }
4694:       q++; /* Move to next row and thus next unique nonzero */
4695:     }
4696:   }

4698:   Ai--; /* Back to the beginning of Ai[] */
4699:   for (k = 0; k < M; k++) Ai[k + 1] += Ai[k];
4700:   jmap--; // Back to the beginning of jmap[]
4701:   jmap[0] = 0;
4702:   for (k = 0; k < nnz; k++) jmap[k + 1] += jmap[k];

4704:   if (nnz < coo_n - nneg) { /* Reallocate with actual number of unique nonzeros */
4705:     PetscCount *jmap_new;
4706:     PetscInt   *Aj_new;

4708:     PetscCall(PetscMalloc1(nnz + 1, &jmap_new));
4709:     PetscCall(PetscArraycpy(jmap_new, jmap, nnz + 1));
4710:     PetscCall(PetscFree(jmap));
4711:     jmap = jmap_new;

4713:     PetscCall(PetscShmgetAllocateArray(nnz, sizeof(PetscInt), (void **)&Aj_new));
4714:     PetscCall(PetscArraycpy(Aj_new, Aj, nnz));
4715:     PetscCall(PetscShmgetDeallocateArray((void **)&Aj));
4716:     Aj = Aj_new;
4717:   }

4719:   if (nneg) { /* Discard heading entries with negative indices in perm[], as we'll access it from index 0 in MatSetValuesCOO */
4720:     PetscCount *perm_new;

4722:     PetscCall(PetscMalloc1(coo_n - nneg, &perm_new));
4723:     PetscCall(PetscArraycpy(perm_new, perm + nneg, coo_n - nneg));
4724:     PetscCall(PetscFree(perm));
4725:     perm = perm_new;
4726:   }

4728:   PetscCall(MatGetRootType_Private(mat, &rtype));
4729:   if (!mat->structure_only) {
4730:     PetscCall(PetscShmgetAllocateArray(nnz, sizeof(PetscScalar), (void **)&Aa));
4731:     PetscCall(PetscArrayzero(Aa, nnz));
4732:   }
4733:   PetscCall(MatSetSeqAIJWithArrays_private(PETSC_COMM_SELF, M, N, Ai, Aj, Aa, rtype, mat));

4735:   seqaij->free_a  = (PetscBool)!mat->structure_only;
4736:   seqaij->free_ij = PETSC_TRUE; /* Let mat own Ai, Aj and any allocated Aa */

4738:   // Put the COO struct in a container and then attach that to the matrix
4739:   PetscCall(PetscMalloc1(1, &coo));
4740:   PetscCall(PetscIntCast(nnz, &coo->nz));
4741:   coo->n    = coo_n;
4742:   coo->Atot = coo_n - nneg; // Annz is seqaij->nz, so no need to record that again
4743:   coo->jmap = jmap;         // of length nnz+1
4744:   coo->perm = perm;
4745:   // MatSetPreallocationCOO() increments the nonzero state once more after this routine returns; if that changes, every
4746:   // MatSetValuesCOO() fails the check in MatSeqAIJCheckCOONonzeroState(), so a mismatch cannot go unnoticed
4747:   coo->nonzerostate = mat->nonzerostate + 1;
4748:   PetscCall(PetscObjectContainerCompose((PetscObject)mat, "__PETSc_MatCOOStruct_Host", coo, MatCOOStructDestroy_SeqAIJ));
4749:   PetscFunctionReturn(PETSC_SUCCESS);
4750: }

4752: // PetscClangLinter pragma disable: -fdoc-internal-linkage
4753: /*@
4754:   MatSeqAIJCheckCOONonzeroState - Checks that the COO maps built by `MatSetPreallocationCOO()` can still be used by
4755:   `MatSetValuesCOO()` on a `MATSEQAIJ` matrix

4757:   Not Collective

4759:   Input Parameter:
4760: . A - the `MATSEQAIJ` matrix

4762:   Level: developer

4764:   Notes:
4765:   The maps address the nonzeros of the matrix by position, so they are only valid while its nonzero pattern is the one built by
4766:   `MatSetPreallocationCOO()`. Any change to the pattern, for example a new nonzero inserted with `MatSetValues()`, would make
4767:   `MatSetValuesCOO()` write values into the wrong locations, so this routine generates an error instead. It also generates an
4768:   error when `MatSetPreallocationCOO()` has not been called.

4770:   The `MatSetValuesCOO()` implementations of `MATSEQAIJ` and its device subtypes call this routine before using the maps.

4772: .seealso: [](ch_matrices), `Mat`, `MATSEQAIJ`, `MatSetPreallocationCOO()`, `MatSetValuesCOO()`, `MatMPIAIJCheckCOONonzeroState()`
4773: @*/
4774: PetscErrorCode MatSeqAIJCheckCOONonzeroState(Mat A)
4775: {
4776:   PetscContainer       container;
4777:   MatCOOStruct_SeqAIJ *coo;

4779:   PetscFunctionBegin;
4780:   PetscCall(PetscObjectQuery((PetscObject)A, "__PETSc_MatCOOStruct_Host", (PetscObject *)&container));
4781:   PetscCheck(container, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "MatSetPreallocationCOO() must be called before MatSetValuesCOO()");
4782:   PetscCall(PetscContainerGetPointer(container, &coo));
4783:   PetscCheck(coo->nonzerostate == A->nonzerostate, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "The nonzero pattern of the matrix has changed since MatSetPreallocationCOO(), for example because MatSetValues() inserted a new nonzero; call MatSetPreallocationCOO() again before MatSetValuesCOO(), or stop inserting new nonzeros with MatSetValues()");
4784:   PetscFunctionReturn(PETSC_SUCCESS);
4785: }

4787: static PetscErrorCode MatSetValuesCOO_SeqAIJ(Mat A, const PetscScalar v[], InsertMode imode)
4788: {
4789:   Mat_SeqAIJ          *aseq = (Mat_SeqAIJ *)A->data;
4790:   PetscCount           i, j, Annz = aseq->nz;
4791:   PetscCount          *perm, *jmap;
4792:   PetscScalar         *Aa;
4793:   PetscContainer       container;
4794:   MatCOOStruct_SeqAIJ *coo;

4796:   PetscFunctionBegin;
4797:   PetscCall(MatSeqAIJCheckCOONonzeroState(A));
4798:   PetscCall(PetscObjectQuery((PetscObject)A, "__PETSc_MatCOOStruct_Host", (PetscObject *)&container));
4799:   PetscCall(PetscContainerGetPointer(container, &coo));
4800:   perm = coo->perm;
4801:   jmap = coo->jmap;
4802:   PetscCall(MatSeqAIJGetArray(A, &Aa));
4803:   for (i = 0; i < Annz; i++) {
4804:     PetscScalar sum = 0.0;
4805:     for (j = jmap[i]; j < jmap[i + 1]; j++) sum += v[perm[j]];
4806:     Aa[i] = (imode == INSERT_VALUES ? 0.0 : Aa[i]) + sum;
4807:   }
4808:   PetscCall(MatSeqAIJRestoreArray(A, &Aa));
4809:   PetscFunctionReturn(PETSC_SUCCESS);
4810: }

4812: #if PetscDefined(HAVE_CUDA)
4813: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCUSPARSE(Mat, MatType, MatReuse, Mat *);
4814: #endif
4815: #if PetscDefined(HAVE_HIP)
4816: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJHIPSPARSE(Mat, MatType, MatReuse, Mat *);
4817: #endif
4818: #if PetscDefined(HAVE_KOKKOS_KERNELS)
4819: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJKokkos(Mat, MatType, MatReuse, Mat *);
4820: #endif

4822: PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJ(Mat B)
4823: {
4824:   Mat_SeqAIJ *b;
4825:   PetscMPIInt size;

4827:   PetscFunctionBegin;
4828:   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)B), &size));
4829:   PetscCheck(size <= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Comm must be of size 1");

4831:   PetscCall(PetscNew(&b));

4833:   B->data   = (void *)b;
4834:   B->ops[0] = MatOps_Values;
4835:   if (B->sortedfull) B->ops->setvalues = MatSetValues_SeqAIJ_SortedFull;

4837:   b->row                = NULL;
4838:   b->col                = NULL;
4839:   b->icol               = NULL;
4840:   b->reallocs           = 0;
4841:   b->ignorezeroentries  = PETSC_FALSE;
4842:   b->roworiented        = PETSC_TRUE;
4843:   b->nonew              = 0;
4844:   b->diag               = NULL;
4845:   b->solve_work         = NULL;
4846:   B->spptr              = NULL;
4847:   b->saved_values       = NULL;
4848:   b->idiag              = NULL;
4849:   b->mdiag              = NULL;
4850:   b->ssor_work          = NULL;
4851:   b->omega              = 1.0;
4852:   b->fshift             = 0.0;
4853:   b->ibdiag             = NULL;
4854:   b->keepnonzeropattern = PETSC_FALSE;

4856:   PetscCall(PetscObjectChangeTypeName((PetscObject)B, MATSEQAIJ));
4857: #if PetscDefined(HAVE_MATLAB)
4858:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "PetscMatlabEnginePut_C", MatlabEnginePut_SeqAIJ));
4859:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "PetscMatlabEngineGet_C", MatlabEngineGet_SeqAIJ));
4860: #endif
4861:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSeqAIJSetColumnIndices_C", MatSeqAIJSetColumnIndices_SeqAIJ));
4862:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatStoreValues_C", MatStoreValues_SeqAIJ));
4863:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatRetrieveValues_C", MatRetrieveValues_SeqAIJ));
4864:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqsbaij_C", MatConvert_SeqAIJ_SeqSBAIJ));
4865:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqbaij_C", MatConvert_SeqAIJ_SeqBAIJ));
4866:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqaijperm_C", MatConvert_SeqAIJ_SeqAIJPERM));
4867:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqaijsell_C", MatConvert_SeqAIJ_SeqAIJSELL));
4868: #if PetscDefined(HAVE_MKL_SPARSE)
4869:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqaijmkl_C", MatConvert_SeqAIJ_SeqAIJMKL));
4870: #endif
4871: #if PetscDefined(HAVE_CUDA)
4872:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqaijcusparse_C", MatConvert_SeqAIJ_SeqAIJCUSPARSE));
4873:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqaijcusparse_seqaij_C", MatProductSetFromOptions_SeqAIJ));
4874:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqaij_seqaijcusparse_C", MatProductSetFromOptions_SeqAIJ));
4875: #endif
4876: #if PetscDefined(HAVE_HIP)
4877:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqaijhipsparse_C", MatConvert_SeqAIJ_SeqAIJHIPSPARSE));
4878:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqaijhipsparse_seqaij_C", MatProductSetFromOptions_SeqAIJ));
4879:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqaij_seqaijhipsparse_C", MatProductSetFromOptions_SeqAIJ));
4880: #endif
4881: #if PetscDefined(HAVE_KOKKOS_KERNELS)
4882:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqaijkokkos_C", MatConvert_SeqAIJ_SeqAIJKokkos));
4883: #endif
4884:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqaijcrl_C", MatConvert_SeqAIJ_SeqAIJCRL));
4885: #if PetscDefined(HAVE_ELEMENTAL)
4886:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_elemental_C", MatConvert_SeqAIJ_Elemental));
4887: #endif
4888: #if PetscDefined(HAVE_SCALAPACK) && (PetscDefined(USE_REAL_SINGLE) || PetscDefined(USE_REAL_DOUBLE))
4889:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_scalapack_C", MatConvert_AIJ_ScaLAPACK));
4890: #endif
4891: #if PetscDefined(HAVE_HYPRE)
4892:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_hypre_C", MatConvert_AIJ_HYPRE));
4893:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_transpose_seqaij_seqaij_C", MatProductSetFromOptions_Transpose_AIJ_AIJ));
4894: #endif
4895:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqdense_C", MatConvert_SeqAIJ_SeqDense));
4896:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqsell_C", MatConvert_SeqAIJ_SeqSELL));
4897:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_is_C", MatConvert_XAIJ_IS));
4898:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatIsTranspose_C", MatIsTranspose_SeqAIJ));
4899:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatIsHermitianTranspose_C", MatIsHermitianTranspose_SeqAIJ));
4900:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSeqAIJSetPreallocation_C", MatSeqAIJSetPreallocation_SeqAIJ));
4901:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatResetPreallocation_C", MatResetPreallocation_SeqAIJ));
4902:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatResetHash_C", MatResetHash_SeqAIJ));
4903:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSeqAIJSetPreallocationCSR_C", MatSeqAIJSetPreallocationCSR_SeqAIJ));
4904:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatReorderForNonzeroDiagonal_C", MatReorderForNonzeroDiagonal_SeqAIJ));
4905:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_is_seqaij_C", MatProductSetFromOptions_IS_XAIJ));
4906:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqdense_seqaij_C", MatProductSetFromOptions_SeqDense_SeqAIJ));
4907:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqaij_seqaij_C", MatProductSetFromOptions_SeqAIJ));
4908:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSeqAIJKron_C", MatSeqAIJKron_SeqAIJ));
4909:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSetPreallocationCOO_C", MatSetPreallocationCOO_SeqAIJ));
4910:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSetValuesCOO_C", MatSetValuesCOO_SeqAIJ));
4911:   PetscCall(MatCreate_SeqAIJ_Inode(B));
4912:   PetscCall(PetscObjectChangeTypeName((PetscObject)B, MATSEQAIJ));
4913:   PetscCall(MatSeqAIJSetTypeFromOptions(B)); /* this allows changing the matrix subtype to say MATSEQAIJPERM */
4914:   PetscFunctionReturn(PETSC_SUCCESS);
4915: }

4917: /*
4918:     Given a matrix generated with MatGetFactor() duplicates all the information in A into C
4919: */
4920: PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C, Mat A, MatDuplicateOption cpvalues, PetscBool mallocmatspace)
4921: {
4922:   Mat_SeqAIJ *c = (Mat_SeqAIJ *)C->data, *a = (Mat_SeqAIJ *)A->data;
4923:   PetscInt    m = A->rmap->n, i;

4925:   PetscFunctionBegin;
4926:   PetscCheck(A->assembled || cpvalues == MAT_DO_NOT_COPY_VALUES, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Cannot duplicate unassembled matrix");
4927:   PetscCall(MatSetOption(C, MAT_STRUCTURE_ONLY, A->structure_only));

4929:   C->factortype = A->factortype;
4930:   c->row        = NULL;
4931:   c->col        = NULL;
4932:   c->icol       = NULL;
4933:   c->reallocs   = 0;
4934:   C->assembled  = A->assembled;

4936:   if (A->preallocated) {
4937:     PetscCall(PetscLayoutReference(A->rmap, &C->rmap));
4938:     PetscCall(PetscLayoutReference(A->cmap, &C->cmap));

4940:     if (!A->hash_active) {
4941:       PetscCall(PetscMalloc1(m, &c->imax));
4942:       PetscCall(PetscArraycpy(c->imax, a->imax, m));
4943:       PetscCall(PetscMalloc1(m, &c->ilen));
4944:       PetscCall(PetscArraycpy(c->ilen, a->ilen, m));

4946:       /* allocate the matrix space */
4947:       if (mallocmatspace) {
4948:         if (!A->structure_only) PetscCall(PetscShmgetAllocateArray(a->i[m], sizeof(PetscScalar), (void **)&c->a));
4949:         PetscCall(PetscShmgetAllocateArray(a->i[m], sizeof(PetscInt), (void **)&c->j));
4950:         PetscCall(PetscShmgetAllocateArray(m + 1, sizeof(PetscInt), (void **)&c->i));
4951:         PetscCall(PetscArraycpy(c->i, a->i, m + 1));
4952:         c->free_a  = PETSC_TRUE;
4953:         c->free_ij = PETSC_TRUE;
4954:         if (m > 0) {
4955:           PetscCall(PetscArraycpy(c->j, a->j, a->i[m]));
4956:           if (!A->structure_only) {
4957:             if (cpvalues == MAT_COPY_VALUES) {
4958:               const PetscScalar *aa;

4960:               PetscCall(MatSeqAIJGetArrayRead(A, &aa));
4961:               PetscCall(PetscArraycpy(c->a, aa, a->i[m]));
4962:               PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
4963:             } else PetscCall(PetscArrayzero(c->a, a->i[m]));
4964:           }
4965:         }
4966:       }
4967:       C->preallocated = PETSC_TRUE;
4968:     } else {
4969:       PetscCheck(mallocmatspace, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONGSTATE, "Cannot malloc matrix memory from a non-preallocated matrix");
4970:       PetscCall(MatSetUp(C));
4971:     }

4973:     c->ignorezeroentries  = a->ignorezeroentries;
4974:     c->roworiented        = a->roworiented;
4975:     c->nonew              = a->nonew;
4976:     c->solve_work         = NULL;
4977:     c->saved_values       = NULL;
4978:     c->idiag              = NULL;
4979:     c->ssor_work          = NULL;
4980:     c->keepnonzeropattern = a->keepnonzeropattern;

4982:     c->rmax  = a->rmax;
4983:     c->nz    = a->nz;
4984:     c->maxnz = a->nz; /* Since we allocate exactly the right amount */

4986:     c->compressedrow.use   = a->compressedrow.use;
4987:     c->compressedrow.nrows = a->compressedrow.nrows;
4988:     if (a->compressedrow.use) {
4989:       i = a->compressedrow.nrows;
4990:       PetscCall(PetscMalloc2(i + 1, &c->compressedrow.i, i, &c->compressedrow.rindex));
4991:       PetscCall(PetscArraycpy(c->compressedrow.i, a->compressedrow.i, i + 1));
4992:       PetscCall(PetscArraycpy(c->compressedrow.rindex, a->compressedrow.rindex, i));
4993:     } else {
4994:       c->compressedrow.use    = PETSC_FALSE;
4995:       c->compressedrow.i      = NULL;
4996:       c->compressedrow.rindex = NULL;
4997:     }
4998:     c->nonzerorowcnt = a->nonzerorowcnt;
4999:     C->nonzerostate  = A->nonzerostate;

5001:     PetscCall(MatDuplicate_SeqAIJ_Inode(A, cpvalues, &C));
5002:   }
5003:   PetscCall(PetscFunctionListDuplicate(((PetscObject)A)->qlist, &((PetscObject)C)->qlist));
5004:   PetscFunctionReturn(PETSC_SUCCESS);
5005: }

5007: PetscErrorCode MatDuplicate_SeqAIJ(Mat A, MatDuplicateOption cpvalues, Mat *B)
5008: {
5009:   PetscFunctionBegin;
5010:   PetscCall(MatCreate(PetscObjectComm((PetscObject)A), B));
5011:   PetscCall(MatSetSizes(*B, A->rmap->n, A->cmap->n, A->rmap->n, A->cmap->n));
5012:   if (!(A->rmap->n % A->rmap->bs) && !(A->cmap->n % A->cmap->bs)) PetscCall(MatSetBlockSizesFromMats(*B, A, A));
5013:   PetscCall(MatSetType(*B, ((PetscObject)A)->type_name));
5014:   PetscCall(MatDuplicateNoCreate_SeqAIJ(*B, A, cpvalues, PETSC_TRUE));
5015:   PetscFunctionReturn(PETSC_SUCCESS);
5016: }

5018: PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer)
5019: {
5020:   PetscBool isbinary, ishdf5;

5022:   PetscFunctionBegin;
5025:   /* force binary viewer to load .info file if it has not yet done so */
5026:   PetscCall(PetscViewerSetUp(viewer));
5027:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
5028:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
5029:   if (isbinary) {
5030:     PetscCall(MatLoad_SeqAIJ_Binary(newMat, viewer));
5031:   } else if (ishdf5) {
5032: #if PetscDefined(HAVE_HDF5)
5033:     PetscCall(MatLoad_AIJ_HDF5(newMat, viewer));
5034: #else
5035:     SETERRQ(PetscObjectComm((PetscObject)newMat), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
5036: #endif
5037:   } else {
5038:     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);
5039:   }
5040:   PetscFunctionReturn(PETSC_SUCCESS);
5041: }

5043: PetscErrorCode MatLoad_SeqAIJ_Binary(Mat mat, PetscViewer viewer)
5044: {
5045:   Mat_SeqAIJ *a = (Mat_SeqAIJ *)mat->data;
5046:   PetscInt    header[4], *rowlens, M, N, nz, sum, rows, cols, i;

5048:   PetscFunctionBegin;
5049:   PetscCall(PetscViewerSetUp(viewer));

5051:   /* read in matrix header */
5052:   PetscCall(PetscViewerBinaryRead(viewer, header, 4, NULL, PETSC_INT));
5053:   PetscCheck(header[0] == MAT_FILE_CLASSID, PETSC_COMM_SELF, PETSC_ERR_FILE_UNEXPECTED, "Not a matrix object in file");
5054:   M  = header[1];
5055:   N  = header[2];
5056:   nz = header[3];
5057:   PetscCheck(M >= 0, PetscObjectComm((PetscObject)viewer), PETSC_ERR_FILE_UNEXPECTED, "Matrix row size (%" PetscInt_FMT ") in file is negative", M);
5058:   PetscCheck(N >= 0, PetscObjectComm((PetscObject)viewer), PETSC_ERR_FILE_UNEXPECTED, "Matrix column size (%" PetscInt_FMT ") in file is negative", N);
5059:   PetscCheck(nz >= 0, PETSC_COMM_SELF, PETSC_ERR_FILE_UNEXPECTED, "Matrix stored in special format on disk, cannot load as SeqAIJ");

5061:   /* set block sizes from the viewer's .info file */
5062:   PetscCall(MatLoad_Binary_BlockSizes(mat, viewer));
5063:   /* set local and global sizes if not set already */
5064:   if (mat->rmap->n < 0) mat->rmap->n = M;
5065:   if (mat->cmap->n < 0) mat->cmap->n = N;
5066:   if (mat->rmap->N < 0) mat->rmap->N = M;
5067:   if (mat->cmap->N < 0) mat->cmap->N = N;
5068:   PetscCall(PetscLayoutSetUp(mat->rmap));
5069:   PetscCall(PetscLayoutSetUp(mat->cmap));

5071:   /* check if the matrix sizes are correct */
5072:   PetscCall(MatGetSize(mat, &rows, &cols));
5073:   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);

5075:   /* read in row lengths */
5076:   PetscCall(PetscMalloc1(M, &rowlens));
5077:   PetscCall(PetscViewerBinaryRead(viewer, rowlens, M, NULL, PETSC_INT));
5078:   /* check if sum(rowlens) is same as nz */
5079:   sum = 0;
5080:   for (i = 0; i < M; i++) sum += rowlens[i];
5081:   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);
5082:   /* preallocate and check sizes */
5083:   PetscCall(MatSeqAIJSetPreallocation_SeqAIJ(mat, 0, rowlens));
5084:   PetscCall(MatGetSize(mat, &rows, &cols));
5085:   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);
5086:   /* store row lengths */
5087:   PetscCall(PetscArraycpy(a->ilen, rowlens, M));
5088:   PetscCall(PetscFree(rowlens));

5090:   /* fill in "i" row pointers */
5091:   a->i[0] = 0;
5092:   for (i = 0; i < M; i++) a->i[i + 1] = a->i[i] + a->ilen[i];
5093:   /* read in "j" column indices */
5094:   PetscCall(PetscViewerBinaryRead(viewer, a->j, nz, NULL, PETSC_INT));
5095:   /* read in "a" nonzero values */
5096:   PetscCall(PetscViewerBinaryRead(viewer, a->a, nz, NULL, PETSC_SCALAR));

5098:   PetscCall(MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY));
5099:   PetscCall(MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY));
5100:   PetscFunctionReturn(PETSC_SUCCESS);
5101: }

5103: PetscErrorCode MatEqual_SeqAIJ(Mat A, Mat B, PetscBool *flg)
5104: {
5105:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data, *b = (Mat_SeqAIJ *)B->data;
5106:   const PetscScalar *aa, *ba;

5108:   PetscFunctionBegin;
5109:   /* If the  matrix dimensions are not equal,or no of nonzeros */
5110:   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) || (a->nz != b->nz)) {
5111:     *flg = PETSC_FALSE;
5112:     PetscFunctionReturn(PETSC_SUCCESS);
5113:   }

5115:   /* if the a->i are the same */
5116:   PetscCall(PetscArraycmp(a->i, b->i, A->rmap->n + 1, flg));
5117:   if (!*flg) PetscFunctionReturn(PETSC_SUCCESS);

5119:   /* if a->j are the same */
5120:   PetscCall(PetscArraycmp(a->j, b->j, a->nz, flg));
5121:   if (!*flg) PetscFunctionReturn(PETSC_SUCCESS);

5123:   PetscCall(MatSeqAIJGetArrayRead(A, &aa));
5124:   PetscCall(MatSeqAIJGetArrayRead(B, &ba));
5125:   /* if a->a are the same */
5126:   PetscCall(PetscArraycmp(aa, ba, a->nz, flg));
5127:   PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
5128:   PetscCall(MatSeqAIJRestoreArrayRead(B, &ba));
5129:   PetscFunctionReturn(PETSC_SUCCESS);
5130: }

5132: /*@
5133:   MatCreateSeqAIJWithArrays - Creates an sequential `MATSEQAIJ` matrix using matrix elements (in CSR format)
5134:   provided by the user.

5136:   Collective

5138:   Input Parameters:
5139: + comm - must be an MPI communicator of size 1
5140: . m    - number of rows
5141: . n    - number of columns
5142: . i    - row indices; that is i[0] = 0, i[row] = i[row-1] + number of elements in that row of the matrix
5143: . j    - column indices
5144: - a    - matrix values

5146:   Output Parameter:
5147: . mat - the matrix

5149:   Level: intermediate

5151:   Notes:
5152:   The `i`, `j`, and `a` arrays are not copied by this routine, the user must free these arrays
5153:   once the matrix is destroyed and not before

5155:   You cannot set new nonzero locations into this matrix, that will generate an error.

5157:   The `i` and `j` indices are 0 based

5159:   The format which is used for the sparse matrix input, is equivalent to a
5160:   row-major ordering.. i.e for the following matrix, the input data expected is
5161:   as shown
5162: .vb
5163:         1 0 0
5164:         2 0 3
5165:         4 5 6

5167:         i =  {0,1,3,6}  [size = nrow+1  = 3+1]
5168:         j =  {0,0,2,0,1,2}  [size = 6]; values must be sorted for each row
5169:         v =  {1,2,3,4,5,6}  [size = 6]
5170: .ve

5172: .seealso: [](ch_matrices), `Mat`, `MatCreate()`, `MatCreateAIJ()`, `MatCreateSeqAIJ()`, `MatCreateMPIAIJWithArrays()`, `MatMPIAIJSetPreallocationCSR()`
5173: @*/
5174: PetscErrorCode MatCreateSeqAIJWithArrays(MPI_Comm comm, PetscInt m, PetscInt n, PetscInt i[], PetscInt j[], PetscScalar a[], Mat *mat)
5175: {
5176:   PetscInt    ii;
5177:   Mat_SeqAIJ *aij;

5179:   PetscFunctionBegin;
5180:   PetscCheck(m <= 0 || i[0] == 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "i (row indices) must start with 0");
5181:   PetscCall(MatCreate(comm, mat));
5182:   PetscCall(MatSetSizes(*mat, m, n, m, n));
5183:   /* PetscCall(MatSetBlockSizes(*mat,,)); */
5184:   PetscCall(MatSetType(*mat, MATSEQAIJ));
5185:   PetscCall(MatSeqAIJSetPreallocation_SeqAIJ(*mat, MAT_SKIP_ALLOCATION, NULL));
5186:   aij = (Mat_SeqAIJ *)(*mat)->data;
5187:   PetscCall(PetscMalloc1(m, &aij->imax));
5188:   PetscCall(PetscMalloc1(m, &aij->ilen));

5190:   aij->i       = i;
5191:   aij->j       = j;
5192:   aij->a       = a;
5193:   aij->nonew   = -1; /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
5194:   aij->free_a  = PETSC_FALSE;
5195:   aij->free_ij = PETSC_FALSE;

5197:   for (ii = 0, aij->nonzerorowcnt = 0, aij->rmax = 0; ii < m; ii++) {
5198:     aij->ilen[ii] = aij->imax[ii] = i[ii + 1] - i[ii];
5199:     if (PetscDefined(USE_DEBUG)) {
5200:       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]);
5201:       for (PetscInt jj = i[ii] + 1; jj < i[ii + 1]; jj++) {
5202:         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);
5203:         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);
5204:       }
5205:     }
5206:   }
5207:   if (PetscDefined(USE_DEBUG)) {
5208:     for (ii = 0; ii < aij->i[m]; ii++) {
5209:       PetscCheck(j[ii] >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Negative column index at location = %" PetscInt_FMT " index = %" PetscInt_FMT, ii, j[ii]);
5210:       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);
5211:     }
5212:   }

5214:   PetscCall(MatAssemblyBegin(*mat, MAT_FINAL_ASSEMBLY));
5215:   PetscCall(MatAssemblyEnd(*mat, MAT_FINAL_ASSEMBLY));
5216:   PetscFunctionReturn(PETSC_SUCCESS);
5217: }

5219: /*@
5220:   MatCreateSeqAIJFromTriple - Creates an sequential `MATSEQAIJ` matrix using matrix elements (in COO format)
5221:   provided by the user.

5223:   Collective

5225:   Input Parameters:
5226: + comm - must be an MPI communicator of size 1
5227: . m    - number of rows
5228: . n    - number of columns
5229: . i    - row indices
5230: . j    - column indices
5231: . a    - matrix values
5232: . nz   - number of nonzeros
5233: - idx  - if the `i` and `j` indices start with 1 use `PETSC_TRUE` otherwise use `PETSC_FALSE`

5235:   Output Parameter:
5236: . mat - the matrix

5238:   Level: intermediate

5240:   Example:
5241:   For the following matrix, the input data expected is as shown (using 0 based indexing)
5242: .vb
5243:         1 0 0
5244:         2 0 3
5245:         4 5 6

5247:         i =  {0,1,1,2,2,2}
5248:         j =  {0,0,2,0,1,2}
5249:         v =  {1,2,3,4,5,6}
5250: .ve

5252:   Note:
5253:   Instead of using this function, users should also consider `MatSetPreallocationCOO()` and `MatSetValuesCOO()`, which allow repeated or remote entries,
5254:   and are particularly useful in iterative applications.

5256: .seealso: [](ch_matrices), `Mat`, `MatCreate()`, `MatCreateAIJ()`, `MatCreateSeqAIJ()`, `MatCreateSeqAIJWithArrays()`, `MatMPIAIJSetPreallocationCSR()`, `MatSetValuesCOO()`, `MatSetPreallocationCOO()`
5257: @*/
5258: PetscErrorCode MatCreateSeqAIJFromTriple(MPI_Comm comm, PetscInt m, PetscInt n, PetscInt i[], PetscInt j[], PetscScalar a[], Mat *mat, PetscCount nz, PetscBool idx)
5259: {
5260:   PetscInt ii, *nnz, one = 1, row, col;

5262:   PetscFunctionBegin;
5263:   PetscCall(PetscCalloc1(m, &nnz));
5264:   for (ii = 0; ii < nz; ii++) nnz[i[ii] - !!idx] += 1;
5265:   PetscCall(MatCreate(comm, mat));
5266:   PetscCall(MatSetSizes(*mat, m, n, m, n));
5267:   PetscCall(MatSetType(*mat, MATSEQAIJ));
5268:   PetscCall(MatSeqAIJSetPreallocation_SeqAIJ(*mat, 0, nnz));
5269:   for (ii = 0; ii < nz; ii++) {
5270:     if (idx) {
5271:       row = i[ii] - 1;
5272:       col = j[ii] - 1;
5273:     } else {
5274:       row = i[ii];
5275:       col = j[ii];
5276:     }
5277:     PetscCall(MatSetValues(*mat, one, &row, one, &col, &a[ii], ADD_VALUES));
5278:   }
5279:   PetscCall(MatAssemblyBegin(*mat, MAT_FINAL_ASSEMBLY));
5280:   PetscCall(MatAssemblyEnd(*mat, MAT_FINAL_ASSEMBLY));
5281:   PetscCall(PetscFree(nnz));
5282:   PetscFunctionReturn(PETSC_SUCCESS);
5283: }

5285: PetscErrorCode MatCreateMPIMatConcatenateSeqMat_SeqAIJ(MPI_Comm comm, Mat inmat, PetscInt n, MatReuse scall, Mat *outmat)
5286: {
5287:   PetscFunctionBegin;
5288:   PetscCall(MatCreateMPIMatConcatenateSeqMat_MPIAIJ(comm, inmat, n, scall, outmat));
5289:   PetscFunctionReturn(PETSC_SUCCESS);
5290: }

5292: /*
5293:  Permute A into C's *local* index space using rowemb,colemb.
5294:  The embedding are supposed to be injections and the above implies that the range of rowemb is a subset
5295:  of [0,m), colemb is in [0,n).
5296:  If pattern == DIFFERENT_NONZERO_PATTERN, C is preallocated according to A.
5297:  */
5298: PetscErrorCode MatSetSeqMat_SeqAIJ(Mat C, IS rowemb, IS colemb, MatStructure pattern, Mat B)
5299: {
5300:   /* If making this function public, change the error returned in this function away from _PLIB. */
5301:   Mat_SeqAIJ     *Baij;
5302:   PetscBool       seqaij;
5303:   PetscInt        m, n, *nz, i, j, count;
5304:   PetscScalar     v;
5305:   const PetscInt *rowindices, *colindices;

5307:   PetscFunctionBegin;
5308:   if (!B) PetscFunctionReturn(PETSC_SUCCESS);
5309:   /* Check to make sure the target matrix (and embeddings) are compatible with C and each other. */
5310:   PetscCall(PetscObjectBaseTypeCompare((PetscObject)B, MATSEQAIJ, &seqaij));
5311:   PetscCheck(seqaij, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Input matrix is of wrong type");
5312:   if (rowemb) {
5313:     PetscCall(ISGetLocalSize(rowemb, &m));
5314:     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);
5315:   } else PetscCheck(C->rmap->n == B->rmap->n, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Input matrix is row-incompatible with the target matrix");
5316:   if (colemb) {
5317:     PetscCall(ISGetLocalSize(colemb, &n));
5318:     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);
5319:   } else PetscCheck(C->cmap->n == B->cmap->n, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Input matrix is col-incompatible with the target matrix");

5321:   Baij       = (Mat_SeqAIJ *)B->data;
5322:   rowindices = NULL;
5323:   if (rowemb) PetscCall(ISGetIndices(rowemb, &rowindices));
5324:   if (pattern == DIFFERENT_NONZERO_PATTERN) {
5325:     PetscCall(PetscMalloc1(C->rmap->n, &nz));
5326:     if (rowemb) {
5327:       PetscCall(PetscArrayzero(nz, C->rmap->n));
5328:       for (i = 0; i < B->rmap->n; i++) nz[rowindices[i]] = Baij->i[i + 1] - Baij->i[i];
5329:     } else {
5330:       for (i = 0; i < B->rmap->n; i++) nz[i] = Baij->i[i + 1] - Baij->i[i];
5331:     }
5332:     PetscCall(MatSeqAIJSetPreallocation(C, 0, nz));
5333:     PetscCall(PetscFree(nz));
5334:   }
5335:   if (pattern == SUBSET_NONZERO_PATTERN) PetscCall(MatZeroEntries(C));
5336:   count      = 0;
5337:   colindices = NULL;
5338:   if (colemb) PetscCall(ISGetIndices(colemb, &colindices));
5339:   for (i = 0; i < B->rmap->n; i++) {
5340:     PetscInt row;
5341:     row = i;
5342:     if (rowindices) row = rowindices[i];
5343:     for (j = Baij->i[i]; j < Baij->i[i + 1]; j++) {
5344:       PetscInt col;
5345:       col = Baij->j[count];
5346:       if (colindices) col = colindices[col];
5347:       v = Baij->a[count];
5348:       PetscCall(MatSetValues(C, 1, &row, 1, &col, &v, INSERT_VALUES));
5349:       ++count;
5350:     }
5351:   }
5352:   if (colemb) PetscCall(ISRestoreIndices(colemb, &colindices));
5353:   if (rowemb) PetscCall(ISRestoreIndices(rowemb, &rowindices));
5354:   /* FIXME: set C's nonzerostate correctly. */
5355:   /* Assembly for C is necessary. */
5356:   C->preallocated  = PETSC_TRUE;
5357:   C->assembled     = PETSC_TRUE;
5358:   C->was_assembled = PETSC_FALSE;
5359:   PetscFunctionReturn(PETSC_SUCCESS);
5360: }

5362: PetscErrorCode MatEliminateZeros_SeqAIJ(Mat A, PetscBool keep)
5363: {
5364:   Mat_SeqAIJ *a  = (Mat_SeqAIJ *)A->data;
5365:   MatScalar  *aa = a->a;
5366:   PetscInt    m = A->rmap->n, fshift = 0, fshift_prev = 0, i, k;
5367:   PetscInt   *ailen = a->ilen, *imax = a->imax, *ai = a->i, *aj = a->j, rmax = 0;

5369:   PetscFunctionBegin;
5370:   PetscCheck(A->assembled, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Cannot eliminate zeros for unassembled matrix");
5371:   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
5372:   for (i = 1, a->nonzerorowcnt = 0; i <= m; i++) {
5373:     /* move each nonzero entry back by the amount of zero slots (fshift) before it*/
5374:     for (k = ai[i - 1]; k < ai[i]; k++) {
5375:       if (aa[k] == 0 && (aj[k] != i - 1 || !keep)) fshift++;
5376:       else {
5377:         if (aa[k] == 0 && aj[k] == i - 1) PetscCall(PetscInfo(A, "Keep the diagonal zero at row %" PetscInt_FMT "\n", i - 1));
5378:         aa[k - fshift] = aa[k];
5379:         aj[k - fshift] = aj[k];
5380:       }
5381:     }
5382:     ai[i - 1] -= fshift_prev; // safe to update ai[i-1] now since it will not be used in the next iteration
5383:     fshift_prev = fshift;
5384:     /* reset ilen and imax for each row */
5385:     ailen[i - 1] = imax[i - 1] = ai[i] - fshift - ai[i - 1];
5386:     a->nonzerorowcnt += ((ai[i] - fshift - ai[i - 1]) > 0);
5387:     rmax = PetscMax(rmax, ailen[i - 1]);
5388:   }
5389:   if (fshift) {
5390:     if (m) {
5391:       ai[m] -= fshift;
5392:       a->nz = ai[m];
5393:     }
5394:     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));
5395:     A->nonzerostate++;
5396:     A->info.nz_unneeded += (PetscReal)fshift;
5397:     a->rmax = rmax;
5398:     if (a->inode.use && a->inode.checked) PetscCall(MatSeqAIJCheckInode(A));
5399:     PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
5400:     PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
5401:   }
5402:   PetscFunctionReturn(PETSC_SUCCESS);
5403: }

5405: PetscFunctionList MatSeqAIJList = NULL;

5407: /*@
5408:   MatSeqAIJSetType - Converts a `MATSEQAIJ` matrix to a subtype

5410:   Collective

5412:   Input Parameters:
5413: + mat    - the matrix object
5414: - matype - matrix type

5416:   Options Database Key:
5417: . -mat_seqaij_type  method - for example seqaijcrl

5419:   Level: intermediate

5421: .seealso: [](ch_matrices), `Mat`, `PCSetType()`, `VecSetType()`, `MatCreate()`, `MatType`
5422: @*/
5423: PetscErrorCode MatSeqAIJSetType(Mat mat, MatType matype)
5424: {
5425:   PetscBool sametype;
5426:   PetscErrorCode (*r)(Mat, MatType, MatReuse, Mat *);

5428:   PetscFunctionBegin;
5430:   PetscCall(PetscObjectTypeCompare((PetscObject)mat, matype, &sametype));
5431:   if (sametype) PetscFunctionReturn(PETSC_SUCCESS);

5433:   PetscCall(PetscFunctionListFind(MatSeqAIJList, matype, &r));
5434:   PetscCheck(r, PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown Mat type given: %s", matype);
5435:   PetscCall((*r)(mat, matype, MAT_INPLACE_MATRIX, &mat));
5436:   PetscFunctionReturn(PETSC_SUCCESS);
5437: }

5439: /*@
5440:   MatSeqAIJRegister -  - Adds a new sub-matrix type for sequential `MATSEQAIJ` matrices

5442:   Not Collective, No Fortran Support

5444:   Input Parameters:
5445: + sname    - name of a new user-defined matrix type, for example `MATSEQAIJCRL`
5446: - function - routine to convert to subtype

5448:   Level: advanced

5450:   Notes:
5451:   `MatSeqAIJRegister()` may be called multiple times to add several user-defined solvers.

5453:   Then, your matrix can be chosen with the procedural interface at runtime via the option
5454: .vb
5455:   -mat_seqaij_type my_mat
5456: .ve

5458: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJRegisterAll()`
5459: @*/
5460: PetscErrorCode MatSeqAIJRegister(const char sname[], PetscErrorCode (*function)(Mat, MatType, MatReuse, Mat *))
5461: {
5462:   PetscFunctionBegin;
5463:   PetscCall(MatInitializePackage());
5464:   PetscCall(PetscFunctionListAdd(&MatSeqAIJList, sname, function));
5465:   PetscFunctionReturn(PETSC_SUCCESS);
5466: }

5468: PetscBool MatSeqAIJRegisterAllCalled = PETSC_FALSE;

5470: /*@
5471:   MatSeqAIJRegisterAll - Registers all of the matrix subtypes of `MATSSEQAIJ`

5473:   Not Collective

5475:   Level: advanced

5477:   Note:
5478:   This registers the versions of `MATSEQAIJ` for GPUs

5480: .seealso: [](ch_matrices), `Mat`, `MatRegisterAll()`, `MatSeqAIJRegister()`
5481: @*/
5482: PetscErrorCode MatSeqAIJRegisterAll(void)
5483: {
5484:   PetscFunctionBegin;
5485:   if (MatSeqAIJRegisterAllCalled) PetscFunctionReturn(PETSC_SUCCESS);
5486:   MatSeqAIJRegisterAllCalled = PETSC_TRUE;

5488:   PetscCall(MatSeqAIJRegister(MATSEQAIJCRL, MatConvert_SeqAIJ_SeqAIJCRL));
5489:   PetscCall(MatSeqAIJRegister(MATSEQAIJPERM, MatConvert_SeqAIJ_SeqAIJPERM));
5490:   PetscCall(MatSeqAIJRegister(MATSEQAIJSELL, MatConvert_SeqAIJ_SeqAIJSELL));
5491: #if PetscDefined(HAVE_MKL_SPARSE)
5492:   PetscCall(MatSeqAIJRegister(MATSEQAIJMKL, MatConvert_SeqAIJ_SeqAIJMKL));
5493: #endif
5494: #if PetscDefined(HAVE_CUDA)
5495:   PetscCall(MatSeqAIJRegister(MATSEQAIJCUSPARSE, MatConvert_SeqAIJ_SeqAIJCUSPARSE));
5496: #endif
5497: #if PetscDefined(HAVE_HIP)
5498:   PetscCall(MatSeqAIJRegister(MATSEQAIJHIPSPARSE, MatConvert_SeqAIJ_SeqAIJHIPSPARSE));
5499: #endif
5500: #if PetscDefined(HAVE_KOKKOS_KERNELS)
5501:   PetscCall(MatSeqAIJRegister(MATSEQAIJKOKKOS, MatConvert_SeqAIJ_SeqAIJKokkos));
5502: #endif
5503: #if PetscDefined(HAVE_VIENNACL) && PetscDefined(HAVE_VIENNACL_NO_CUDA)
5504:   PetscCall(MatSeqAIJRegister(MATMPIAIJVIENNACL, MatConvert_SeqAIJ_SeqAIJViennaCL));
5505: #endif
5506:   PetscFunctionReturn(PETSC_SUCCESS);
5507: }