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 (value == 0.0 && ignorezeroentries && row != col) goto noinsert;
468:       if (nonew == 1) goto noinsert;
469:       PetscCheck(nonew != -1, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Inserting a new nonzero at (%" PetscInt_FMT ",%" PetscInt_FMT ") in the matrix", row, col);
470:       if (A->structure_only) {
471:         MatSeqXAIJReallocateAIJ_structure_only(A, A->rmap->n, 1, nrow, row, col, rmax, ai, aj, rp, imax, nonew, MatScalar);
472:       } else {
473:         MatSeqXAIJReallocateAIJ(A, A->rmap->n, 1, nrow, row, col, rmax, aa, ai, aj, rp, ap, imax, nonew, MatScalar);
474:       }
475:       N = nrow++ - 1;
476:       a->nz++;
477:       high++;
478:       /* shift up all the later entries in this row */
479:       PetscCall(PetscArraymove(rp + i + 1, rp + i, N - i + 1));
480:       rp[i] = col;
481:       if (!A->structure_only) {
482:         PetscCall(PetscArraymove(ap + i + 1, ap + i, N - i + 1));
483:         ap[i] = value;
484:       }
485:       low = i + 1;
486:     noinsert:;
487:     }
488:     ailen[row] = nrow;
489:   }
490:   PetscCall(MatSeqAIJRestoreArray(A, &aa));
491:   PetscFunctionReturn(PETSC_SUCCESS);
492: }

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

502:   PetscFunctionBegin;
503:   PetscCheck(!A->was_assembled, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Cannot call on assembled matrix.");
504:   PetscCheck(m * n + a->nz <= a->maxnz, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of entries in matrix will be larger than maximum nonzeros allocated for %" PetscInt_FMT " in MatSeqAIJSetTotalPreallocation()", a->maxnz);

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

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

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

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

537:   Level: advanced

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

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

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

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

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

585:   PetscFunctionBegin;
586:   PetscCall(MatSeqAIJGetArray(A, &aa));
587:   for (k = 0; k < m; k++) { /* loop over added rows */
588:     row = im[k];
589:     PetscCheck(n <= a->imax[row], PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Preallocation for row %" PetscInt_FMT " does not match number of columns provided", n);
590:     rp = aj + ai[row];
591:     ap = aa + ai[row];
592:     if (!A->was_assembled) PetscCall(PetscArraycpy(rp, in, n));
593:     if (!A->structure_only) {
594:       if (v) {
595:         PetscCall(PetscArraycpy(ap, v, n));
596:         v += n;
597:       } else {
598:         PetscCall(PetscMemzero(ap, n * sizeof(PetscScalar)));
599:       }
600:     }
601:     ailen[row] = n;
602:     a->nz += n;
603:   }
604:   PetscCall(MatSeqAIJRestoreArray(A, &aa));
605:   PetscFunctionReturn(PETSC_SUCCESS);
606: }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

758:     for (i = 0; i < m; i++) {
759:       for (j = a->i[i]; j < a->i[i + 1]; j++) {
760: #if PetscDefined(USE_COMPLEX)
761:         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])));
762: #else
763:         PetscCall(PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT " %" PetscInt_FMT "  %18.16e\n", i + 1, a->j[j] + 1, (double)a->a[j]));
764: #endif
765:       }
766:     }
767:     if (nofinalvalue) {
768:       if (PetscDefined(USE_COMPLEX)) PetscCall(PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT " %" PetscInt_FMT "  %18.16e %18.16e\n", m, A->cmap->n, 0., 0.));
769:       else PetscCall(PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT " %" PetscInt_FMT "  %18.16e\n", m, A->cmap->n, 0.0));
770:     }
771:     PetscCall(PetscObjectGetName((PetscObject)A, &name));
772:     PetscCall(PetscViewerASCIIPrintf(viewer, "];\n %s = spconvert(zzz);\n", name));
773:     PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_TRUE));
774:   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
775:     PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_FALSE));
776:     for (i = 0; i < m; i++) {
777:       PetscCall(PetscViewerASCIIPrintf(viewer, "row %" PetscInt_FMT ":", i));
778:       for (j = a->i[i]; j < a->i[i + 1]; j++) {
779: #if PetscDefined(USE_COMPLEX)
780:         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
781:           PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g + %g i)", a->j[j], (double)PetscRealPart(a->a[j]), (double)PetscImaginaryPart(a->a[j])));
782:         } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
783:           PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g - %g i)", a->j[j], (double)PetscRealPart(a->a[j]), (double)-PetscImaginaryPart(a->a[j])));
784:         } else if (PetscRealPart(a->a[j]) != 0.0) {
785:           PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g) ", a->j[j], (double)PetscRealPart(a->a[j])));
786:         }
787: #else
788:         if (a->a[j] != 0.0) PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g) ", a->j[j], (double)a->a[j]));
789: #endif
790:       }
791:       PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
792:     }
793:     PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_TRUE));
794:   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
795:     PetscInt nzd = 0, fshift = 1, *sptr;
796:     PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_FALSE));
797:     PetscCall(PetscMalloc1(m + 1, &sptr));
798:     for (i = 0; i < m; i++) {
799:       sptr[i] = nzd + 1;
800:       for (j = a->i[i]; j < a->i[i + 1]; j++) {
801:         if (a->j[j] >= i) {
802:           if (PetscDefined(USE_COMPLEX) ? (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) : a->a[j] != 0.0) nzd++;
803:         }
804:       }
805:     }
806:     sptr[m] = nzd + 1;
807:     PetscCall(PetscViewerASCIIPrintf(viewer, " %" PetscInt_FMT " %" PetscInt_FMT "\n\n", m, nzd));
808:     for (i = 0; i < m + 1; i += 6) {
809:       if (i + 4 < m) {
810:         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]));
811:       } else if (i + 3 < m) {
812:         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]));
813:       } else if (i + 2 < m) {
814:         PetscCall(PetscViewerASCIIPrintf(viewer, " %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT "\n", sptr[i], sptr[i + 1], sptr[i + 2], sptr[i + 3]));
815:       } else if (i + 1 < m) {
816:         PetscCall(PetscViewerASCIIPrintf(viewer, " %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT "\n", sptr[i], sptr[i + 1], sptr[i + 2]));
817:       } else if (i < m) {
818:         PetscCall(PetscViewerASCIIPrintf(viewer, " %" PetscInt_FMT " %" PetscInt_FMT "\n", sptr[i], sptr[i + 1]));
819:       } else {
820:         PetscCall(PetscViewerASCIIPrintf(viewer, " %" PetscInt_FMT "\n", sptr[i]));
821:       }
822:     }
823:     PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
824:     PetscCall(PetscFree(sptr));
825:     for (i = 0; i < m; i++) {
826:       for (j = a->i[i]; j < a->i[i + 1]; j++) {
827:         if (a->j[j] >= i) PetscCall(PetscViewerASCIIPrintf(viewer, " %" PetscInt_FMT " ", a->j[j] + fshift));
828:       }
829:       PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
830:     }
831:     PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
832:     for (i = 0; i < m; i++) {
833:       for (j = a->i[i]; j < a->i[i + 1]; j++) {
834:         if (a->j[j] >= i) {
835: #if PetscDefined(USE_COMPLEX)
836:           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])));
837: #else
838:           if (a->a[j] != 0.0) PetscCall(PetscViewerASCIIPrintf(viewer, " %18.16e ", (double)a->a[j]));
839: #endif
840:         }
841:       }
842:       PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
843:     }
844:     PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_TRUE));
845:   } else if (format == PETSC_VIEWER_ASCII_DENSE) {
846:     PetscInt    cnt = 0, jcnt;
847:     PetscScalar value;
848:     PetscBool   realonly = PETSC_TRUE;

850:     if (PetscDefined(USE_COMPLEX)) {
851:       for (i = 0; i < a->i[m]; i++) {
852:         if (PetscImaginaryPart(a->a[i]) != 0.0) {
853:           realonly = PETSC_FALSE;
854:           break;
855:         }
856:       }
857:     }

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

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

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

954: #include <petscdraw.h>
955: static PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw, void *Aa)
956: {
957:   Mat                A = (Mat)Aa;
958:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data;
959:   PetscInt           i, j, m = A->rmap->n;
960:   int                color;
961:   PetscReal          xl, yl, xr, yr, x_l, x_r, y_l, y_r;
962:   PetscViewer        viewer;
963:   PetscViewerFormat  format;
964:   const PetscScalar *aa;

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

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

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

1024:     PetscDrawCollectiveBegin(draw);
1025:     for (i = 0; i < m; i++) {
1026:       y_l = m - i - 1.0;
1027:       y_r = y_l + 1.0;
1028:       for (j = a->i[i]; j < a->i[i + 1]; j++) {
1029:         x_l   = a->j[j];
1030:         x_r   = x_l + 1.0;
1031:         color = PetscDrawRealToColor(PetscAbsScalar(aa[count]), minv, maxv);
1032:         PetscCall(PetscDrawRectangle(draw, x_l, y_l, x_r, y_r, color, color, color, color));
1033:         count++;
1034:       }
1035:     }
1036:     PetscDrawCollectiveEnd(draw);
1037:   }
1038:   PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
1039:   PetscFunctionReturn(PETSC_SUCCESS);
1040: }

1042: #include <petscdraw.h>
1043: static PetscErrorCode MatView_SeqAIJ_Draw(Mat A, PetscViewer viewer)
1044: {
1045:   PetscDraw draw;
1046:   PetscReal xr, yr, xl, yl, h, w;
1047:   PetscBool isnull;

1049:   PetscFunctionBegin;
1050:   PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw));
1051:   PetscCall(PetscDrawIsNull(draw, &isnull));
1052:   if (isnull) PetscFunctionReturn(PETSC_SUCCESS);

1054:   xr = A->cmap->n;
1055:   yr = A->rmap->n;
1056:   h  = yr / 10.0;
1057:   w  = xr / 10.0;
1058:   xr += w;
1059:   yr += h;
1060:   xl = -w;
1061:   yl = -h;
1062:   PetscCall(PetscDrawSetCoordinates(draw, xl, yl, xr, yr));
1063:   PetscCall(PetscObjectCompose((PetscObject)A, "Zoomviewer", (PetscObject)viewer));
1064:   PetscCall(PetscDrawZoom(draw, MatView_SeqAIJ_Draw_Zoom, A));
1065:   PetscCall(PetscObjectCompose((PetscObject)A, "Zoomviewer", NULL));
1066:   PetscCall(PetscDrawSave(draw));
1067:   PetscFunctionReturn(PETSC_SUCCESS);
1068: }

1070: PetscErrorCode MatView_SeqAIJ(Mat A, PetscViewer viewer)
1071: {
1072:   PetscBool isascii, isbinary, isdraw;

1074:   PetscFunctionBegin;
1075:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
1076:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
1077:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
1078:   if (isascii) PetscCall(MatView_SeqAIJ_ASCII(A, viewer));
1079:   else if (isbinary) PetscCall(MatView_SeqAIJ_Binary(A, viewer));
1080:   else if (isdraw) PetscCall(MatView_SeqAIJ_Draw(A, viewer));
1081:   PetscCall(MatView_SeqAIJ_Inode(A, viewer));
1082:   PetscFunctionReturn(PETSC_SUCCESS);
1083: }

1085: PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A, MatAssemblyType mode)
1086: {
1087:   Mat_SeqAIJ *a      = (Mat_SeqAIJ *)A->data;
1088:   PetscInt    fshift = 0, i, *ai = a->i, *aj = a->j, *imax = a->imax;
1089:   PetscInt    m = A->rmap->n, *ip, N, *ailen = a->ilen, rmax = 0;
1090:   MatScalar  *aa    = a->a, *ap;
1091:   PetscReal   ratio = 0.6;

1093:   PetscFunctionBegin;
1094:   if (mode == MAT_FLUSH_ASSEMBLY) PetscFunctionReturn(PETSC_SUCCESS);
1095:   if (A->was_assembled && A->ass_nonzerostate == A->nonzerostate) {
1096:     /* 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) */
1097:     PetscCall(MatAssemblyEnd_SeqAIJ_Inode(A, mode)); /* read the sparsity pattern */
1098:     PetscFunctionReturn(PETSC_SUCCESS);
1099:   }

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

1136:   A->info.mallocs += a->reallocs;
1137:   a->reallocs         = 0;
1138:   A->info.nz_unneeded = (PetscReal)fshift;
1139:   a->rmax             = rmax;

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

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

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

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

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

1172: PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
1173: {
1174:   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
1175:   MatScalar  *aa;

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

1184: static PetscErrorCode MatReset_SeqAIJ(Mat A)
1185: {
1186:   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;

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

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

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

1226: PetscErrorCode MatDestroy_SeqAIJ(Mat A)
1227: {
1228:   PetscFunctionBegin;
1229:   PetscCall(MatReset_SeqAIJ(A));
1230:   PetscCall(PetscFree(A->data));

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

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

1302: PetscErrorCode MatSetOption_SeqAIJ(Mat A, MatOption op, PetscBool flg)
1303: {
1304:   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;

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

1348: PETSC_INTERN PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A, Vec v)
1349: {
1350:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data;
1351:   PetscInt           n, *ai = a->i;
1352:   PetscScalar       *x;
1353:   const PetscScalar *aa;
1354:   const PetscInt    *diag;
1355:   PetscBool          diagDense;

1357:   PetscFunctionBegin;
1358:   PetscCall(VecGetLocalSize(v, &n));
1359:   PetscCheck(n == A->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nonconforming matrix and vector");
1360:   PetscCall(MatSeqAIJGetArrayRead(A, &aa));
1361:   if (A->factortype == MAT_FACTOR_ILU || A->factortype == MAT_FACTOR_LU) {
1362:     PetscCall(MatGetDiagonalMarkers_SeqAIJ(A, &diag, NULL));
1363:     PetscCall(VecGetArrayWrite(v, &x));
1364:     for (PetscInt i = 0; i < n; i++) x[i] = 1.0 / aa[diag[i]];
1365:     PetscCall(VecRestoreArrayWrite(v, &x));
1366:     PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
1367:     PetscFunctionReturn(PETSC_SUCCESS);
1368:   }

1370:   PetscCheck(A->factortype == MAT_FACTOR_NONE, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Not for factor matrices that are not ILU or LU");
1371:   PetscCall(MatGetDiagonalMarkers_SeqAIJ(A, &diag, &diagDense));
1372:   PetscCall(VecGetArrayWrite(v, &x));
1373:   if (diagDense) {
1374:     for (PetscInt i = 0; i < n; i++) x[i] = aa[diag[i]];
1375:   } else {
1376:     for (PetscInt i = 0; i < n; i++) x[i] = (diag[i] == ai[i + 1]) ? 0.0 : aa[diag[i]];
1377:   }
1378:   PetscCall(VecRestoreArrayWrite(v, &x));
1379:   PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
1380:   PetscFunctionReturn(PETSC_SUCCESS);
1381: }

1383: #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
1384: PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A, Vec xx, Vec zz, Vec yy)
1385: {
1386:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data;
1387:   const MatScalar   *aa;
1388:   PetscScalar       *y;
1389:   const PetscScalar *x;
1390:   PetscInt           m = A->rmap->n;
1391: #if !PetscDefined(USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1392:   const MatScalar  *v;
1393:   PetscScalar       alpha;
1394:   PetscInt          n, i, j;
1395:   const PetscInt   *idx, *ii, *ridx = NULL;
1396:   Mat_CompressedRow cprow    = a->compressedrow;
1397:   PetscBool         usecprow = cprow.use;
1398: #endif

1400:   PetscFunctionBegin;
1401:   if (zz != yy) PetscCall(VecCopy(zz, yy));
1402:   PetscCall(VecGetArrayRead(xx, &x));
1403:   PetscCall(VecGetArray(yy, &y));
1404:   PetscCall(MatSeqAIJGetArrayRead(A, &aa));

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

1435: PetscErrorCode MatMultTranspose_SeqAIJ(Mat A, Vec xx, Vec yy)
1436: {
1437:   PetscFunctionBegin;
1438:   PetscCall(VecSet(yy, 0.0));
1439:   PetscCall(MatMultTransposeAdd_SeqAIJ(A, xx, yy, yy));
1440:   PetscFunctionReturn(PETSC_SUCCESS);
1441: }

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

1445: PetscErrorCode MatMult_SeqAIJ(Mat A, Vec xx, Vec yy)
1446: {
1447:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data;
1448:   PetscScalar       *y;
1449:   const PetscScalar *x;
1450:   const MatScalar   *a_a;
1451:   PetscInt           m = A->rmap->n;
1452:   const PetscInt    *ii, *ridx = NULL;
1453:   PetscBool          usecprow = a->compressedrow.use;

1455: #if PetscDefined(HAVE_PRAGMA_DISJOINT)
1456:   #pragma disjoint(*x, *y, *aa)
1457: #endif

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

1505: // HACK!!!!! Used by src/mat/tests/ex170.c
1506: PETSC_EXTERN PetscErrorCode MatMultMax_SeqAIJ(Mat A, Vec xx, Vec yy)
1507: {
1508:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data;
1509:   PetscScalar       *y;
1510:   const PetscScalar *x;
1511:   const MatScalar   *aa, *a_a;
1512:   PetscInt           m = A->rmap->n;
1513:   const PetscInt    *aj, *ii, *ridx   = NULL;
1514:   PetscInt           n, i, nonzerorow = 0;
1515:   PetscScalar        sum;
1516:   PetscBool          usecprow = a->compressedrow.use;

1518: #if PetscDefined(HAVE_PRAGMA_DISJOINT)
1519:   #pragma disjoint(*x, *y, *aa)
1520: #endif

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

1559: // HACK!!!!! Used by src/mat/tests/ex170.c
1560: PETSC_EXTERN PetscErrorCode MatMultAddMax_SeqAIJ(Mat A, Vec xx, Vec yy, Vec zz)
1561: {
1562:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data;
1563:   PetscScalar       *y, *z;
1564:   const PetscScalar *x;
1565:   const MatScalar   *aa, *a_a;
1566:   PetscInt           m = A->rmap->n, *aj, *ii;
1567:   PetscInt           n, i, *ridx = NULL;
1568:   PetscScalar        sum;
1569:   PetscBool          usecprow = a->compressedrow.use;

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

1606: #include <../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h>
1607: PetscErrorCode MatMultAdd_SeqAIJ(Mat A, Vec xx, Vec yy, Vec zz)
1608: {
1609:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data;
1610:   PetscScalar       *y, *z;
1611:   const PetscScalar *x;
1612:   const MatScalar   *a_a;
1613:   const PetscInt    *ii, *ridx = NULL;
1614:   PetscInt           m        = A->rmap->n;
1615:   PetscBool          usecprow = a->compressedrow.use;

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

1661: static PetscErrorCode MatShift_SeqAIJ(Mat A, PetscScalar v)
1662: {
1663:   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data;
1664:   const PetscInt *diag;
1665:   const PetscInt *ii = (const PetscInt *)a->i;
1666:   PetscBool       diagDense;

1668:   PetscFunctionBegin;
1669:   if (!A->preallocated || !a->nz) {
1670:     PetscCall(MatSeqAIJSetPreallocation(A, 1, NULL));
1671:     PetscCall(MatShift_Basic(A, v));
1672:     PetscFunctionReturn(PETSC_SUCCESS);
1673:   }

1675:   PetscCall(MatGetDiagonalMarkers_SeqAIJ(A, &diag, &diagDense));
1676:   if (diagDense) {
1677:     PetscScalar *Aa;

1679:     PetscCall(MatSeqAIJGetArray(A, &Aa));
1680:     for (PetscInt i = 0; i < A->rmap->n; i++) Aa[diag[i]] += v;
1681:     PetscCall(MatSeqAIJRestoreArray(A, &Aa));
1682:   } else {
1683:     PetscScalar       *olda = a->a; /* preserve pointers to current matrix nonzeros structure and values */
1684:     PetscInt          *oldj = a->j, *oldi = a->i;
1685:     PetscBool          free_a = a->free_a, free_ij = a->free_ij;
1686:     const PetscScalar *Aa;
1687:     PetscInt          *mdiag = NULL;

1689:     PetscCall(PetscCalloc1(A->rmap->n, &mdiag));
1690:     for (PetscInt i = 0; i < A->rmap->n; i++) {
1691:       if (i < A->cmap->n && diag[i] >= ii[i + 1]) { /* 'out of range' rows never have diagonals */
1692:         mdiag[i] = 1;
1693:       }
1694:     }
1695:     PetscCall(MatSeqAIJGetArrayRead(A, &Aa)); // sync the host
1696:     PetscCall(MatSeqAIJRestoreArrayRead(A, &Aa));

1698:     a->a = NULL;
1699:     a->j = NULL;
1700:     a->i = NULL;
1701:     /* increase the values in imax for each row where a diagonal is being inserted then reallocate the matrix data structures */
1702:     for (PetscInt i = 0; i < PetscMin(A->rmap->n, A->cmap->n); i++) a->imax[i] += mdiag[i];
1703:     PetscCall(MatSeqAIJSetPreallocation_SeqAIJ(A, 0, a->imax));

1705:     /* copy old values into new matrix data structure */
1706:     for (PetscInt i = 0; i < A->rmap->n; i++) {
1707:       PetscCall(MatSetValues(A, 1, &i, a->imax[i] - mdiag[i], &oldj[oldi[i]], &olda[oldi[i]], ADD_VALUES));
1708:       if (i < A->cmap->n) PetscCall(MatSetValue(A, i, i, v, ADD_VALUES));
1709:     }
1710:     PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
1711:     PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
1712:     if (free_a) PetscCall(PetscShmgetDeallocateArray((void **)&olda));
1713:     if (free_ij) PetscCall(PetscShmgetDeallocateArray((void **)&oldj));
1714:     if (free_ij) PetscCall(PetscShmgetDeallocateArray((void **)&oldi));
1715:     PetscCall(PetscFree(mdiag));
1716:   }
1717:   PetscFunctionReturn(PETSC_SUCCESS);
1718: }

1720: #include <petscblaslapack.h>
1721: #include <petsc/private/kernels/blockinvert.h>

1723: /*
1724:     Note that values is allocated externally by the PC and then passed into this routine
1725: */
1726: static PetscErrorCode MatInvertVariableBlockDiagonal_SeqAIJ(Mat A, PetscInt nblocks, const PetscInt *bsizes, PetscScalar *diag)
1727: {
1728:   PetscInt        n = A->rmap->n, i, ncnt = 0, *indx, j, bsizemax = 0, *v_pivots;
1729:   PetscBool       allowzeropivot, zeropivotdetected = PETSC_FALSE;
1730:   const PetscReal shift = 0.0;
1731:   PetscInt        ipvt[5];
1732:   PetscCount      flops = 0;
1733:   PetscScalar     work[25], *v_work;

1735:   PetscFunctionBegin;
1736:   allowzeropivot = PetscNot(A->erroriffailure);
1737:   for (i = 0; i < nblocks; i++) ncnt += bsizes[i];
1738:   PetscCheck(ncnt == n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Total blocksizes %" PetscInt_FMT " doesn't match number matrix rows %" PetscInt_FMT, ncnt, n);
1739:   for (i = 0; i < nblocks; i++) bsizemax = PetscMax(bsizemax, bsizes[i]);
1740:   PetscCall(PetscMalloc1(bsizemax, &indx));
1741:   if (bsizemax > 7) PetscCall(PetscMalloc2(bsizemax, &v_work, bsizemax, &v_pivots));
1742:   ncnt = 0;
1743:   for (i = 0; i < nblocks; i++) {
1744:     for (j = 0; j < bsizes[i]; j++) indx[j] = ncnt + j;
1745:     PetscCall(MatGetValues(A, bsizes[i], indx, bsizes[i], indx, diag));
1746:     switch (bsizes[i]) {
1747:     case 1:
1748:       *diag = 1.0 / (*diag);
1749:       break;
1750:     case 2:
1751:       PetscCall(PetscKernel_A_gets_inverse_A_2(diag, shift, allowzeropivot, &zeropivotdetected));
1752:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1753:       PetscCall(PetscKernel_A_gets_transpose_A_2(diag));
1754:       break;
1755:     case 3:
1756:       PetscCall(PetscKernel_A_gets_inverse_A_3(diag, shift, allowzeropivot, &zeropivotdetected));
1757:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1758:       PetscCall(PetscKernel_A_gets_transpose_A_3(diag));
1759:       break;
1760:     case 4:
1761:       PetscCall(PetscKernel_A_gets_inverse_A_4(diag, shift, allowzeropivot, &zeropivotdetected));
1762:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1763:       PetscCall(PetscKernel_A_gets_transpose_A_4(diag));
1764:       break;
1765:     case 5:
1766:       PetscCall(PetscKernel_A_gets_inverse_A_5(diag, ipvt, work, shift, allowzeropivot, &zeropivotdetected));
1767:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1768:       PetscCall(PetscKernel_A_gets_transpose_A_5(diag));
1769:       break;
1770:     case 6:
1771:       PetscCall(PetscKernel_A_gets_inverse_A_6(diag, shift, allowzeropivot, &zeropivotdetected));
1772:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1773:       PetscCall(PetscKernel_A_gets_transpose_A_6(diag));
1774:       break;
1775:     case 7:
1776:       PetscCall(PetscKernel_A_gets_inverse_A_7(diag, shift, allowzeropivot, &zeropivotdetected));
1777:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1778:       PetscCall(PetscKernel_A_gets_transpose_A_7(diag));
1779:       break;
1780:     default:
1781:       PetscCall(PetscKernel_A_gets_inverse_A(bsizes[i], diag, v_pivots, v_work, allowzeropivot, &zeropivotdetected));
1782:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1783:       PetscCall(PetscKernel_A_gets_transpose_A_N(diag, bsizes[i]));
1784:     }
1785:     ncnt += bsizes[i];
1786:     diag += bsizes[i] * bsizes[i];
1787:     flops += 2 * PetscPowInt64(bsizes[i], 3) / 3;
1788:   }
1789:   PetscCall(PetscLogFlops(flops));
1790:   if (bsizemax > 7) PetscCall(PetscFree2(v_work, v_pivots));
1791:   PetscCall(PetscFree(indx));
1792:   PetscFunctionReturn(PETSC_SUCCESS);
1793: }

1795: /*
1796:    Negative shift indicates do not generate an error if there is a zero diagonal, just invert it anyways
1797: */
1798: static PetscErrorCode MatInvertDiagonalForSOR_SeqAIJ(Mat A, PetscScalar omega, PetscScalar fshift)
1799: {
1800:   Mat_SeqAIJ      *a = (Mat_SeqAIJ *)A->data;
1801:   PetscInt         i, m = A->rmap->n;
1802:   const MatScalar *v;
1803:   PetscScalar     *idiag, *mdiag;
1804:   PetscBool        diagDense;
1805:   const PetscInt  *diag;

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

1813:   mdiag = a->mdiag;
1814:   idiag = a->idiag;
1815:   PetscCall(MatSeqAIJGetArrayRead(A, &v));
1816:   if (omega == 1.0 && PetscRealPart(fshift) <= 0.0) {
1817:     for (i = 0; i < m; i++) {
1818:       mdiag[i] = v[diag[i]];
1819:       if (!PetscAbsScalar(mdiag[i])) { /* zero diagonal */
1820:         PetscCheck(PetscRealPart(fshift), PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Zero diagonal on row %" PetscInt_FMT, i);
1821:         PetscCall(PetscInfo(A, "Zero diagonal on row %" PetscInt_FMT "\n", i));
1822:         A->factorerrortype             = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1823:         A->factorerror_zeropivot_value = 0.0;
1824:         A->factorerror_zeropivot_row   = i;
1825:       }
1826:       idiag[i] = 1.0 / v[diag[i]];
1827:     }
1828:     PetscCall(PetscLogFlops(m));
1829:   } else {
1830:     for (i = 0; i < m; i++) {
1831:       mdiag[i] = v[diag[i]];
1832:       idiag[i] = omega / (fshift + v[diag[i]]);
1833:     }
1834:     PetscCall(PetscLogFlops(2.0 * m));
1835:   }
1836:   PetscCall(MatSeqAIJRestoreArrayRead(A, &v));
1837:   a->idiagState = ((PetscObject)A)->state;
1838:   a->omega      = omega;
1839:   a->fshift     = fshift;
1840:   PetscFunctionReturn(PETSC_SUCCESS);
1841: }

1843: PetscErrorCode MatSOR_SeqAIJ(Mat A, Vec bb, PetscReal omega, MatSORType flag, PetscReal fshift, PetscInt its, PetscInt lits, Vec xx)
1844: {
1845:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data;
1846:   PetscScalar       *x, d, sum, *t, scale;
1847:   const MatScalar   *v, *idiag = NULL, *mdiag, *aa;
1848:   const PetscScalar *b, *bs, *xb, *ts;
1849:   PetscInt           n, m = A->rmap->n, i;
1850:   const PetscInt    *idx, *diag;

1852:   PetscFunctionBegin;
1853:   if (a->inode.use && a->inode.checked && omega == 1.0 && fshift == 0.0) {
1854:     PetscCall(MatSOR_SeqAIJ_Inode(A, bb, omega, flag, fshift, its, lits, xx));
1855:     PetscFunctionReturn(PETSC_SUCCESS);
1856:   }
1857:   its = its * lits;
1858:   PetscCall(MatInvertDiagonalForSOR_SeqAIJ(A, omega, fshift));
1859:   PetscCall(MatGetDiagonalMarkers_SeqAIJ(A, &diag, NULL));
1860:   t     = a->ssor_work;
1861:   idiag = a->idiag;
1862:   mdiag = a->mdiag;

1864:   PetscCall(MatSeqAIJGetArrayRead(A, &aa));
1865:   PetscCall(VecGetArray(xx, &x));
1866:   PetscCall(VecGetArrayRead(bb, &b));
1867:   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
1868:   if (flag == SOR_APPLY_UPPER) {
1869:     /* apply (U + D/omega) to the vector */
1870:     bs = b;
1871:     for (i = 0; i < m; i++) {
1872:       d   = fshift + mdiag[i];
1873:       n   = a->i[i + 1] - diag[i] - 1;
1874:       idx = a->j + diag[i] + 1;
1875:       v   = aa + diag[i] + 1;
1876:       sum = b[i] * d / omega;
1877:       PetscSparseDensePlusDot(sum, bs, v, idx, n);
1878:       x[i] = sum;
1879:     }
1880:     PetscCall(VecRestoreArray(xx, &x));
1881:     PetscCall(VecRestoreArrayRead(bb, &b));
1882:     PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
1883:     PetscCall(PetscLogFlops(a->nz));
1884:     PetscFunctionReturn(PETSC_SUCCESS);
1885:   }

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

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

1894:     to a vector efficiently using Eisenstat's trick.
1895:     */
1896:     scale = (2.0 / omega) - 1.0;

1898:     /*  x = (E + U)^{-1} b */
1899:     for (i = m - 1; i >= 0; i--) {
1900:       n   = a->i[i + 1] - diag[i] - 1;
1901:       idx = a->j + diag[i] + 1;
1902:       v   = aa + diag[i] + 1;
1903:       sum = b[i];
1904:       PetscSparseDenseMinusDot(sum, x, v, idx, n);
1905:       x[i] = sum * idiag[i];
1906:     }

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

1912:     /*  t = (E + L)^{-1}t */
1913:     ts   = t;
1914:     diag = a->diag;
1915:     for (i = 0; i < m; i++) {
1916:       n   = diag[i] - a->i[i];
1917:       idx = a->j + a->i[i];
1918:       v   = aa + a->i[i];
1919:       sum = t[i];
1920:       PetscSparseDenseMinusDot(sum, ts, v, idx, n);
1921:       t[i] = sum * idiag[i];
1922:       /*  x = x + t */
1923:       x[i] += t[i];
1924:     }

1926:     PetscCall(PetscLogFlops(6.0 * m - 1 + 2.0 * a->nz));
1927:     PetscCall(VecRestoreArray(xx, &x));
1928:     PetscCall(VecRestoreArrayRead(bb, &b));
1929:     PetscFunctionReturn(PETSC_SUCCESS);
1930:   }
1931:   if (flag & SOR_ZERO_INITIAL_GUESS) {
1932:     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
1933:       for (i = 0; i < m; i++) {
1934:         n   = diag[i] - a->i[i];
1935:         idx = a->j + a->i[i];
1936:         v   = aa + a->i[i];
1937:         sum = b[i];
1938:         PetscSparseDenseMinusDot(sum, x, v, idx, n);
1939:         t[i] = sum;
1940:         x[i] = sum * idiag[i];
1941:       }
1942:       xb = t;
1943:       PetscCall(PetscLogFlops(a->nz));
1944:     } else xb = b;
1945:     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
1946:       for (i = m - 1; i >= 0; i--) {
1947:         n   = a->i[i + 1] - diag[i] - 1;
1948:         idx = a->j + diag[i] + 1;
1949:         v   = aa + diag[i] + 1;
1950:         sum = xb[i];
1951:         PetscSparseDenseMinusDot(sum, x, v, idx, n);
1952:         if (xb == b) {
1953:           x[i] = sum * idiag[i];
1954:         } else {
1955:           x[i] = (1 - omega) * x[i] + sum * idiag[i]; /* omega in idiag */
1956:         }
1957:       }
1958:       PetscCall(PetscLogFlops(a->nz)); /* assumes 1/2 in upper */
1959:     }
1960:     its--;
1961:   }
1962:   while (its--) {
1963:     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
1964:       for (i = 0; i < m; i++) {
1965:         /* lower */
1966:         n   = diag[i] - a->i[i];
1967:         idx = a->j + a->i[i];
1968:         v   = aa + a->i[i];
1969:         sum = b[i];
1970:         PetscSparseDenseMinusDot(sum, x, v, idx, n);
1971:         t[i] = sum; /* save application of the lower-triangular part */
1972:         /* upper */
1973:         n   = a->i[i + 1] - diag[i] - 1;
1974:         idx = a->j + diag[i] + 1;
1975:         v   = aa + diag[i] + 1;
1976:         PetscSparseDenseMinusDot(sum, x, v, idx, n);
1977:         x[i] = (1. - omega) * x[i] + sum * idiag[i]; /* omega in idiag */
1978:       }
1979:       xb = t;
1980:       PetscCall(PetscLogFlops(2.0 * a->nz));
1981:     } else xb = b;
1982:     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
1983:       for (i = m - 1; i >= 0; i--) {
1984:         sum = xb[i];
1985:         if (xb == b) {
1986:           /* whole matrix (no checkpointing available) */
1987:           n   = a->i[i + 1] - a->i[i];
1988:           idx = a->j + a->i[i];
1989:           v   = aa + a->i[i];
1990:           PetscSparseDenseMinusDot(sum, x, v, idx, n);
1991:           x[i] = (1. - omega) * x[i] + (sum + mdiag[i] * x[i]) * idiag[i];
1992:         } else { /* lower-triangular part has been saved, so only apply upper-triangular */
1993:           n   = a->i[i + 1] - diag[i] - 1;
1994:           idx = a->j + diag[i] + 1;
1995:           v   = aa + diag[i] + 1;
1996:           PetscSparseDenseMinusDot(sum, x, v, idx, n);
1997:           x[i] = (1. - omega) * x[i] + sum * idiag[i]; /* omega in idiag */
1998:         }
1999:       }
2000:       if (xb == b) PetscCall(PetscLogFlops(2.0 * a->nz));
2001:       else PetscCall(PetscLogFlops(a->nz)); /* assumes 1/2 in upper */
2002:     }
2003:   }
2004:   PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
2005:   PetscCall(VecRestoreArray(xx, &x));
2006:   PetscCall(VecRestoreArrayRead(bb, &b));
2007:   PetscFunctionReturn(PETSC_SUCCESS);
2008: }

2010: static PetscErrorCode MatGetInfo_SeqAIJ(Mat A, MatInfoType flag, MatInfo *info)
2011: {
2012:   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;

2014:   PetscFunctionBegin;
2015:   info->block_size   = 1.0;
2016:   info->nz_allocated = a->maxnz;
2017:   info->nz_used      = a->nz;
2018:   info->nz_unneeded  = (a->maxnz - a->nz);
2019:   info->assemblies   = A->num_ass;
2020:   info->mallocs      = A->info.mallocs;
2021:   info->memory       = 0; /* REVIEW ME */
2022:   if (A->factortype) {
2023:     info->fill_ratio_given  = A->info.fill_ratio_given;
2024:     info->fill_ratio_needed = A->info.fill_ratio_needed;
2025:     info->factor_mallocs    = A->info.factor_mallocs;
2026:   } else {
2027:     info->fill_ratio_given  = 0;
2028:     info->fill_ratio_needed = 0;
2029:     info->factor_mallocs    = 0;
2030:   }
2031:   PetscFunctionReturn(PETSC_SUCCESS);
2032: }

2034: static PetscErrorCode MatZeroRows_SeqAIJ(Mat A, PetscInt N, const PetscInt rows[], PetscScalar diagv, Vec x, Vec b)
2035: {
2036:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data;
2037:   PetscInt           i, m = A->rmap->n - 1;
2038:   const PetscScalar *xx;
2039:   PetscScalar       *bb, *aa;
2040:   PetscInt           d = 0;
2041:   const PetscInt    *diag;

2043:   PetscFunctionBegin;
2044:   if (x && b) {
2045:     PetscCall(VecGetArrayRead(x, &xx));
2046:     PetscCall(VecGetArray(b, &bb));
2047:     for (i = 0; i < N; i++) {
2048:       PetscCheck(rows[i] >= 0 && rows[i] <= m, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "row %" PetscInt_FMT " out of range", rows[i]);
2049:       if (rows[i] >= A->cmap->n) continue;
2050:       bb[rows[i]] = diagv * xx[rows[i]];
2051:     }
2052:     PetscCall(VecRestoreArrayRead(x, &xx));
2053:     PetscCall(VecRestoreArray(b, &bb));
2054:   }

2056:   PetscCall(MatGetDiagonalMarkers_SeqAIJ(A, &diag, NULL));
2057:   PetscCall(MatSeqAIJGetArray(A, &aa));
2058:   if (a->keepnonzeropattern) {
2059:     for (i = 0; i < N; i++) {
2060:       PetscCheck(rows[i] >= 0 && rows[i] <= m, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "row %" PetscInt_FMT " out of range", rows[i]);
2061:       PetscCall(PetscArrayzero(&aa[a->i[rows[i]]], a->ilen[rows[i]]));
2062:     }
2063:     if (diagv != 0.0) {
2064:       for (i = 0; i < N; i++) {
2065:         d = rows[i];
2066:         if (d >= A->cmap->n) continue;
2067:         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);
2068:         aa[diag[d]] = diagv;
2069:       }
2070:     }
2071:   } else {
2072:     if (diagv != 0.0) {
2073:       for (i = 0; i < N; i++) {
2074:         PetscCheck(rows[i] >= 0 && rows[i] <= m, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "row %" PetscInt_FMT " out of range", rows[i]);
2075:         if (a->ilen[rows[i]] > 0) {
2076:           if (rows[i] >= A->cmap->n) {
2077:             a->ilen[rows[i]] = 0;
2078:           } else {
2079:             a->ilen[rows[i]]    = 1;
2080:             aa[a->i[rows[i]]]   = diagv;
2081:             a->j[a->i[rows[i]]] = rows[i];
2082:           }
2083:         } else if (rows[i] < A->cmap->n) { /* in case row was completely empty */
2084:           PetscCall(MatSetValues_SeqAIJ(A, 1, &rows[i], 1, &rows[i], &diagv, INSERT_VALUES));
2085:         }
2086:       }
2087:     } else {
2088:       for (i = 0; i < N; i++) {
2089:         PetscCheck(rows[i] >= 0 && rows[i] <= m, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "row %" PetscInt_FMT " out of range", rows[i]);
2090:         a->ilen[rows[i]] = 0;
2091:       }
2092:     }
2093:     A->nonzerostate++;
2094:   }
2095:   PetscCall(MatSeqAIJRestoreArray(A, &aa));
2096:   PetscUseTypeMethod(A, assemblyend, MAT_FINAL_ASSEMBLY);
2097:   PetscFunctionReturn(PETSC_SUCCESS);
2098: }

2100: static PetscErrorCode MatZeroRowsColumns_SeqAIJ(Mat A, PetscInt N, const PetscInt rows[], PetscScalar diagv, Vec x, Vec b)
2101: {
2102:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data;
2103:   PetscInt           i, j, m = A->rmap->n - 1, d = 0;
2104:   PetscBool         *zeroed, vecs = PETSC_FALSE;
2105:   const PetscScalar *xx;
2106:   PetscScalar       *bb, *aa;
2107:   const PetscInt    *diag;
2108:   PetscBool          diagDense;

2110:   PetscFunctionBegin;
2111:   if (!N) PetscFunctionReturn(PETSC_SUCCESS);
2112:   PetscCall(MatGetDiagonalMarkers_SeqAIJ(A, &diag, &diagDense));
2113:   PetscCall(MatSeqAIJGetArray(A, &aa));
2114:   if (x && b) {
2115:     PetscCall(VecGetArrayRead(x, &xx));
2116:     PetscCall(VecGetArray(b, &bb));
2117:     vecs = PETSC_TRUE;
2118:   }
2119:   PetscCall(PetscCalloc1(A->rmap->n, &zeroed));
2120:   for (i = 0; i < N; i++) {
2121:     PetscCheck(rows[i] >= 0 && rows[i] <= m, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "row %" PetscInt_FMT " out of range", rows[i]);
2122:     PetscCall(PetscArrayzero(PetscSafePointerPlusOffset(aa, a->i[rows[i]]), a->ilen[rows[i]]));

2124:     zeroed[rows[i]] = PETSC_TRUE;
2125:   }
2126:   for (i = 0; i < A->rmap->n; i++) {
2127:     if (!zeroed[i]) {
2128:       for (j = a->i[i]; j < a->i[i + 1]; j++) {
2129:         if (a->j[j] < A->rmap->n && zeroed[a->j[j]]) {
2130:           if (vecs) bb[i] -= aa[j] * xx[a->j[j]];
2131:           aa[j] = 0.0;
2132:         }
2133:       }
2134:     } else if (vecs && i < A->cmap->N) bb[i] = diagv * xx[i];
2135:   }
2136:   if (x && b) {
2137:     PetscCall(VecRestoreArrayRead(x, &xx));
2138:     PetscCall(VecRestoreArray(b, &bb));
2139:   }
2140:   PetscCall(PetscFree(zeroed));
2141:   if (diagv != 0.0) {
2142:     if (!diagDense) {
2143:       for (i = 0; i < N; i++) {
2144:         if (rows[i] >= A->cmap->N) continue;
2145:         PetscCheck(!a->nonew || rows[i] < d, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Matrix is missing diagonal entry in row %" PetscInt_FMT " (%" PetscInt_FMT ")", d, rows[i]);
2146:         PetscCall(MatSetValues_SeqAIJ(A, 1, &rows[i], 1, &rows[i], &diagv, INSERT_VALUES));
2147:       }
2148:     } else {
2149:       for (i = 0; i < N; i++) aa[diag[rows[i]]] = diagv;
2150:     }
2151:   }
2152:   PetscCall(MatSeqAIJRestoreArray(A, &aa));
2153:   if (!diagDense) PetscUseTypeMethod(A, assemblyend, MAT_FINAL_ASSEMBLY);
2154:   PetscFunctionReturn(PETSC_SUCCESS);
2155: }

2157: PetscErrorCode MatGetRow_SeqAIJ(Mat A, PetscInt row, PetscInt *nz, PetscInt **idx, PetscScalar **v)
2158: {
2159:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data;
2160:   const PetscScalar *aa;

2162:   PetscFunctionBegin;
2163:   PetscCall(MatSeqAIJGetArrayRead(A, &aa));
2164:   *nz = a->i[row + 1] - a->i[row];
2165:   if (v) *v = PetscSafePointerPlusOffset((PetscScalar *)aa, a->i[row]);
2166:   if (idx) {
2167:     if (*nz && a->j) *idx = a->j + a->i[row];
2168:     else *idx = NULL;
2169:   }
2170:   PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
2171:   PetscFunctionReturn(PETSC_SUCCESS);
2172: }

2174: PetscErrorCode MatRestoreRow_SeqAIJ(Mat A, PetscInt row, PetscInt *nz, PetscInt **idx, PetscScalar **v)
2175: {
2176:   PetscFunctionBegin;
2177:   PetscFunctionReturn(PETSC_SUCCESS);
2178: }

2180: static PetscErrorCode MatNorm_SeqAIJ(Mat A, NormType type, PetscReal *nrm)
2181: {
2182:   Mat_SeqAIJ      *a = (Mat_SeqAIJ *)A->data;
2183:   const MatScalar *v;
2184:   PetscReal        sum = 0.0;
2185:   PetscInt         i, j;

2187:   PetscFunctionBegin;
2188:   PetscCall(MatSeqAIJGetArrayRead(A, &v));
2189:   if (type == NORM_FROBENIUS) {
2190: #if PetscDefined(USE_REAL___FP16)
2191:     PetscBLASInt one = 1, nz = a->nz;
2192:     PetscCallBLAS("BLASnrm2", *nrm = BLASnrm2_(&nz, v, &one));
2193: #else
2194:     for (i = 0; i < a->nz; i++) {
2195:       sum += PetscRealPart(PetscConj(*v) * (*v));
2196:       v++;
2197:     }
2198:     *nrm = PetscSqrtReal(sum);
2199: #endif
2200:     PetscCall(PetscLogFlops(2.0 * a->nz));
2201:   } else if (type == NORM_1) {
2202:     PetscReal *tmp;
2203:     PetscInt  *jj = a->j;
2204:     PetscCall(PetscCalloc1(A->cmap->n, &tmp));
2205:     *nrm = 0.0;
2206:     for (j = 0; j < a->nz; j++) {
2207:       tmp[*jj++] += PetscAbsScalar(*v);
2208:       v++;
2209:     }
2210:     for (j = 0; j < A->cmap->n; j++) {
2211:       if (tmp[j] > *nrm) *nrm = tmp[j];
2212:     }
2213:     PetscCall(PetscFree(tmp));
2214:     PetscCall(PetscLogFlops(a->nz));
2215:   } else if (type == NORM_INFINITY) {
2216:     *nrm = 0.0;
2217:     for (j = 0; j < A->rmap->n; j++) {
2218:       const PetscScalar *v2 = PetscSafePointerPlusOffset(v, a->i[j]);
2219:       sum                   = 0.0;
2220:       for (i = 0; i < a->i[j + 1] - a->i[j]; i++) {
2221:         sum += PetscAbsScalar(*v2);
2222:         v2++;
2223:       }
2224:       if (sum > *nrm) *nrm = sum;
2225:     }
2226:     PetscCall(PetscLogFlops(a->nz));
2227:   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "No support for two norm");
2228:   PetscCall(MatSeqAIJRestoreArrayRead(A, &v));
2229:   PetscFunctionReturn(PETSC_SUCCESS);
2230: }

2232: static PetscErrorCode MatIsTranspose_SeqAIJ(Mat A, Mat B, PetscReal tol, PetscBool *f)
2233: {
2234:   Mat_SeqAIJ      *aij = (Mat_SeqAIJ *)A->data, *bij = (Mat_SeqAIJ *)B->data;
2235:   PetscInt        *adx, *bdx, *aii, *bii, *aptr, *bptr;
2236:   const MatScalar *va, *vb;
2237:   PetscInt         ma, na, mb, nb, i;

2239:   PetscFunctionBegin;
2240:   PetscCall(MatGetSize(A, &ma, &na));
2241:   PetscCall(MatGetSize(B, &mb, &nb));
2242:   if (ma != nb || na != mb) {
2243:     *f = PETSC_FALSE;
2244:     PetscFunctionReturn(PETSC_SUCCESS);
2245:   }
2246:   PetscCall(MatSeqAIJGetArrayRead(A, &va));
2247:   PetscCall(MatSeqAIJGetArrayRead(B, &vb));
2248:   aii = aij->i;
2249:   bii = bij->i;
2250:   adx = aij->j;
2251:   bdx = bij->j;
2252:   PetscCall(PetscMalloc1(ma, &aptr));
2253:   PetscCall(PetscMalloc1(mb, &bptr));
2254:   for (i = 0; i < ma; i++) aptr[i] = aii[i];
2255:   for (i = 0; i < mb; i++) bptr[i] = bii[i];

2257:   *f = PETSC_TRUE;
2258:   for (i = 0; i < ma; i++) {
2259:     while (aptr[i] < aii[i + 1]) {
2260:       PetscInt    idc, idr;
2261:       PetscScalar vc, vr;
2262:       /* column/row index/value */
2263:       idc = adx[aptr[i]];
2264:       idr = bdx[bptr[idc]];
2265:       vc  = va[aptr[i]];
2266:       vr  = vb[bptr[idc]];
2267:       if (i != idr || PetscAbsScalar(vc - vr) > tol) {
2268:         *f = PETSC_FALSE;
2269:         goto done;
2270:       } else {
2271:         aptr[i]++;
2272:         if (B || i != idc) bptr[idc]++;
2273:       }
2274:     }
2275:   }
2276: done:
2277:   PetscCall(PetscFree(aptr));
2278:   PetscCall(PetscFree(bptr));
2279:   PetscCall(MatSeqAIJRestoreArrayRead(A, &va));
2280:   PetscCall(MatSeqAIJRestoreArrayRead(B, &vb));
2281:   PetscFunctionReturn(PETSC_SUCCESS);
2282: }

2284: static PetscErrorCode MatIsHermitianTranspose_SeqAIJ(Mat A, Mat B, PetscReal tol, PetscBool *f)
2285: {
2286:   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data, *bij = (Mat_SeqAIJ *)B->data;
2287:   PetscInt   *adx, *bdx, *aii, *bii, *aptr, *bptr;
2288:   MatScalar  *va, *vb;
2289:   PetscInt    ma, na, mb, nb, i;

2291:   PetscFunctionBegin;
2292:   PetscCall(MatGetSize(A, &ma, &na));
2293:   PetscCall(MatGetSize(B, &mb, &nb));
2294:   if (ma != nb || na != mb) {
2295:     *f = PETSC_FALSE;
2296:     PetscFunctionReturn(PETSC_SUCCESS);
2297:   }
2298:   aii = aij->i;
2299:   bii = bij->i;
2300:   adx = aij->j;
2301:   bdx = bij->j;
2302:   va  = aij->a;
2303:   vb  = bij->a;
2304:   PetscCall(PetscMalloc1(ma, &aptr));
2305:   PetscCall(PetscMalloc1(mb, &bptr));
2306:   for (i = 0; i < ma; i++) aptr[i] = aii[i];
2307:   for (i = 0; i < mb; i++) bptr[i] = bii[i];

2309:   *f = PETSC_TRUE;
2310:   for (i = 0; i < ma; i++) {
2311:     while (aptr[i] < aii[i + 1]) {
2312:       PetscInt    idc, idr;
2313:       PetscScalar vc, vr;
2314:       /* column/row index/value */
2315:       idc = adx[aptr[i]];
2316:       idr = bdx[bptr[idc]];
2317:       vc  = va[aptr[i]];
2318:       vr  = vb[bptr[idc]];
2319:       if (i != idr || PetscAbsScalar(vc - PetscConj(vr)) > tol) {
2320:         *f = PETSC_FALSE;
2321:         goto done;
2322:       } else {
2323:         aptr[i]++;
2324:         if (B || i != idc) bptr[idc]++;
2325:       }
2326:     }
2327:   }
2328: done:
2329:   PetscCall(PetscFree(aptr));
2330:   PetscCall(PetscFree(bptr));
2331:   PetscFunctionReturn(PETSC_SUCCESS);
2332: }

2334: PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A, Vec ll, Vec rr)
2335: {
2336:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data;
2337:   const PetscScalar *l, *r;
2338:   PetscScalar        x;
2339:   MatScalar         *v;
2340:   PetscInt           i, j, m = A->rmap->n, n = A->cmap->n, M, nz = a->nz;
2341:   const PetscInt    *jj;

2343:   PetscFunctionBegin;
2344:   if (ll) {
2345:     /* The local size is used so that VecMPI can be passed to this routine
2346:        by MatDiagonalScale_MPIAIJ */
2347:     PetscCall(VecGetLocalSize(ll, &m));
2348:     PetscCheck(m == A->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Left scaling vector wrong length");
2349:     PetscCall(VecGetArrayRead(ll, &l));
2350:     PetscCall(MatSeqAIJGetArray(A, &v));
2351:     for (i = 0; i < m; i++) {
2352:       x = l[i];
2353:       M = a->i[i + 1] - a->i[i];
2354:       for (j = 0; j < M; j++) (*v++) *= x;
2355:     }
2356:     PetscCall(VecRestoreArrayRead(ll, &l));
2357:     PetscCall(PetscLogFlops(nz));
2358:     PetscCall(MatSeqAIJRestoreArray(A, &v));
2359:   }
2360:   if (rr) {
2361:     PetscCall(VecGetLocalSize(rr, &n));
2362:     PetscCheck(n == A->cmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Right scaling vector wrong length");
2363:     PetscCall(VecGetArrayRead(rr, &r));
2364:     PetscCall(MatSeqAIJGetArray(A, &v));
2365:     jj = a->j;
2366:     for (i = 0; i < nz; i++) (*v++) *= r[*jj++];
2367:     PetscCall(MatSeqAIJRestoreArray(A, &v));
2368:     PetscCall(VecRestoreArrayRead(rr, &r));
2369:     PetscCall(PetscLogFlops(nz));
2370:   }
2371:   PetscFunctionReturn(PETSC_SUCCESS);
2372: }

2374: PetscErrorCode MatCreateSubMatrix_SeqAIJ(Mat A, IS isrow, IS iscol, PetscInt csize, MatReuse scall, Mat *B)
2375: {
2376:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data, *c;
2377:   PetscInt          *smap, i, k, kstart, kend, oldcols = A->cmap->n, *lens;
2378:   PetscInt           row, mat_i, *mat_j, tcol, first, step, *mat_ilen, sum, lensi;
2379:   const PetscInt    *irow, *icol;
2380:   const PetscScalar *aa;
2381:   PetscInt           nrows, ncols;
2382:   PetscInt          *starts, *j_new, *i_new, *aj = a->j, *ai = a->i, ii, *ailen = a->ilen;
2383:   MatScalar         *a_new, *mat_a, *c_a;
2384:   Mat                C;
2385:   PetscBool          stride;

2387:   PetscFunctionBegin;
2388:   PetscCall(ISGetIndices(isrow, &irow));
2389:   PetscCall(ISGetLocalSize(isrow, &nrows));
2390:   PetscCall(ISGetLocalSize(iscol, &ncols));

2392:   PetscCall(PetscObjectTypeCompare((PetscObject)iscol, ISSTRIDE, &stride));
2393:   if (stride) {
2394:     PetscCall(ISStrideGetInfo(iscol, &first, &step));
2395:   } else {
2396:     first = 0;
2397:     step  = 0;
2398:   }
2399:   if (stride && step == 1) {
2400:     /* special case of contiguous rows */
2401:     PetscCall(PetscMalloc2(nrows, &lens, nrows, &starts));
2402:     /* loop over new rows determining lens and starting points */
2403:     for (i = 0; i < nrows; i++) {
2404:       kstart    = ai[irow[i]];
2405:       kend      = kstart + ailen[irow[i]];
2406:       starts[i] = kstart;
2407:       for (k = kstart; k < kend; k++) {
2408:         if (aj[k] >= first) {
2409:           starts[i] = k;
2410:           break;
2411:         }
2412:       }
2413:       sum = 0;
2414:       while (k < kend) {
2415:         if (aj[k++] >= first + ncols) break;
2416:         sum++;
2417:       }
2418:       lens[i] = sum;
2419:     }
2420:     /* create submatrix */
2421:     if (scall == MAT_REUSE_MATRIX) {
2422:       PetscInt n_cols, n_rows;
2423:       PetscCall(MatGetSize(*B, &n_rows, &n_cols));
2424:       PetscCheck(n_rows == nrows && n_cols == ncols, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Reused submatrix wrong size");
2425:       PetscCall(MatZeroEntries(*B));
2426:       C = *B;
2427:     } else {
2428:       PetscInt rbs, cbs;
2429:       PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &C));
2430:       PetscCall(MatSetSizes(C, nrows, ncols, PETSC_DETERMINE, PETSC_DETERMINE));
2431:       PetscCall(ISGetBlockSize(isrow, &rbs));
2432:       PetscCall(ISGetBlockSize(iscol, &cbs));
2433:       PetscCall(MatSetBlockSizes(C, rbs, cbs));
2434:       PetscCall(MatSetType(C, ((PetscObject)A)->type_name));
2435:       PetscCall(MatSeqAIJSetPreallocation_SeqAIJ(C, 0, lens));
2436:     }
2437:     c = (Mat_SeqAIJ *)C->data;

2439:     /* loop over rows inserting into submatrix */
2440:     PetscCall(MatSeqAIJGetArrayWrite(C, &a_new)); // Not 'a_new = c->a-new', since that raw usage ignores offload state of C
2441:     j_new = c->j;
2442:     i_new = c->i;
2443:     PetscCall(MatSeqAIJGetArrayRead(A, &aa));
2444:     for (i = 0; i < nrows; i++) {
2445:       ii    = starts[i];
2446:       lensi = lens[i];
2447:       if (lensi) {
2448:         for (k = 0; k < lensi; k++) *j_new++ = aj[ii + k] - first;
2449:         PetscCall(PetscArraycpy(a_new, aa + starts[i], lensi));
2450:         a_new += lensi;
2451:       }
2452:       i_new[i + 1] = i_new[i] + lensi;
2453:       c->ilen[i]   = lensi;
2454:     }
2455:     PetscCall(MatSeqAIJRestoreArrayWrite(C, &a_new)); // Set C's offload state properly
2456:     PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
2457:     PetscCall(PetscFree2(lens, starts));
2458:   } else {
2459:     PetscCall(ISGetIndices(iscol, &icol));
2460:     PetscCall(PetscCalloc1(oldcols, &smap));
2461:     PetscCall(PetscMalloc1(1 + nrows, &lens));
2462:     for (i = 0; i < ncols; i++) {
2463:       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);
2464:       smap[icol[i]] = i + 1;
2465:     }

2467:     /* determine lens of each row */
2468:     for (i = 0; i < nrows; i++) {
2469:       kstart  = ai[irow[i]];
2470:       kend    = kstart + a->ilen[irow[i]];
2471:       lens[i] = 0;
2472:       for (k = kstart; k < kend; k++) {
2473:         if (smap[aj[k]]) lens[i]++;
2474:       }
2475:     }
2476:     /* Create and fill new matrix */
2477:     if (scall == MAT_REUSE_MATRIX) {
2478:       PetscBool equal;

2480:       c = (Mat_SeqAIJ *)((*B)->data);
2481:       PetscCheck((*B)->rmap->n == nrows && (*B)->cmap->n == ncols, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Cannot reuse matrix. wrong size");
2482:       PetscCall(PetscArraycmp(c->ilen, lens, (*B)->rmap->n, &equal));
2483:       PetscCheck(equal, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Cannot reuse matrix. wrong number of nonzeros");
2484:       PetscCall(PetscArrayzero(c->ilen, (*B)->rmap->n));
2485:       C = *B;
2486:     } else {
2487:       PetscInt rbs, cbs;
2488:       PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &C));
2489:       PetscCall(MatSetSizes(C, nrows, ncols, PETSC_DETERMINE, PETSC_DETERMINE));
2490:       PetscCall(ISGetBlockSize(isrow, &rbs));
2491:       PetscCall(ISGetBlockSize(iscol, &cbs));
2492:       if (rbs > 1 || cbs > 1) PetscCall(MatSetBlockSizes(C, rbs, cbs));
2493:       PetscCall(MatSetType(C, ((PetscObject)A)->type_name));
2494:       PetscCall(MatSeqAIJSetPreallocation_SeqAIJ(C, 0, lens));
2495:     }
2496:     PetscCall(MatSeqAIJGetArrayRead(A, &aa));

2498:     c = (Mat_SeqAIJ *)C->data;
2499:     PetscCall(MatSeqAIJGetArrayWrite(C, &c_a)); // Not 'c->a', since that raw usage ignores offload state of C
2500:     for (i = 0; i < nrows; i++) {
2501:       row      = irow[i];
2502:       kstart   = ai[row];
2503:       kend     = kstart + a->ilen[row];
2504:       mat_i    = c->i[i];
2505:       mat_j    = PetscSafePointerPlusOffset(c->j, mat_i);
2506:       mat_a    = PetscSafePointerPlusOffset(c_a, mat_i);
2507:       mat_ilen = c->ilen + i;
2508:       for (k = kstart; k < kend; k++) {
2509:         if ((tcol = smap[a->j[k]])) {
2510:           *mat_j++ = tcol - 1;
2511:           *mat_a++ = aa[k];
2512:           (*mat_ilen)++;
2513:         }
2514:       }
2515:     }
2516:     PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
2517:     /* Free work space */
2518:     PetscCall(ISRestoreIndices(iscol, &icol));
2519:     PetscCall(PetscFree(smap));
2520:     PetscCall(PetscFree(lens));
2521:     /* sort */
2522:     for (i = 0; i < nrows; i++) {
2523:       PetscInt ilen;

2525:       mat_i = c->i[i];
2526:       mat_j = PetscSafePointerPlusOffset(c->j, mat_i);
2527:       mat_a = PetscSafePointerPlusOffset(c_a, mat_i);
2528:       ilen  = c->ilen[i];
2529:       PetscCall(PetscSortIntWithScalarArray(ilen, mat_j, mat_a));
2530:     }
2531:     PetscCall(MatSeqAIJRestoreArrayWrite(C, &c_a));
2532:   }
2533: #if PetscDefined(HAVE_DEVICE)
2534:   PetscCall(MatBindToCPU(C, A->boundtocpu));
2535: #endif
2536:   PetscCall(MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY));
2537:   PetscCall(MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY));

2539:   PetscCall(ISRestoreIndices(isrow, &irow));
2540:   *B = C;
2541:   PetscFunctionReturn(PETSC_SUCCESS);
2542: }

2544: static PetscErrorCode MatGetMultiProcBlock_SeqAIJ(Mat mat, MPI_Comm subComm, MatReuse scall, Mat *subMat)
2545: {
2546:   Mat B;

2548:   PetscFunctionBegin;
2549:   if (scall == MAT_INITIAL_MATRIX) {
2550:     PetscCall(MatCreate(subComm, &B));
2551:     PetscCall(MatSetSizes(B, mat->rmap->n, mat->cmap->n, mat->rmap->n, mat->cmap->n));
2552:     PetscCall(MatSetBlockSizesFromMats(B, mat, mat));
2553:     PetscCall(MatSetType(B, MATSEQAIJ));
2554:     PetscCall(MatDuplicateNoCreate_SeqAIJ(B, mat, MAT_COPY_VALUES, PETSC_TRUE));
2555:     *subMat = B;
2556:   } else {
2557:     PetscCall(MatCopy_SeqAIJ(mat, *subMat, SAME_NONZERO_PATTERN));
2558:   }
2559:   PetscFunctionReturn(PETSC_SUCCESS);
2560: }

2562: static PetscErrorCode MatILUFactor_SeqAIJ(Mat inA, IS row, IS col, const MatFactorInfo *info)
2563: {
2564:   Mat_SeqAIJ *a = (Mat_SeqAIJ *)inA->data;
2565:   Mat         outA;
2566:   PetscBool   row_identity, col_identity;

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

2571:   PetscCall(ISIdentity(row, &row_identity));
2572:   PetscCall(ISIdentity(col, &col_identity));

2574:   outA = inA;
2575:   PetscCall(PetscFree(inA->solvertype));
2576:   PetscCall(PetscStrallocpy(MATSOLVERPETSC, &inA->solvertype));

2578:   PetscCall(PetscObjectReference((PetscObject)row));
2579:   PetscCall(ISDestroy(&a->row));

2581:   a->row = row;

2583:   PetscCall(PetscObjectReference((PetscObject)col));
2584:   PetscCall(ISDestroy(&a->col));

2586:   a->col = col;

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

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

2596:   if (row_identity && col_identity) {
2597:     PetscCall(MatLUFactorNumeric_SeqAIJ_inplace(outA, inA, info));
2598:   } else {
2599:     PetscCall(MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA, inA, info));
2600:   }
2601:   outA->factortype = MAT_FACTOR_LU;
2602:   PetscFunctionReturn(PETSC_SUCCESS);
2603: }

2605: PetscErrorCode MatScale_SeqAIJ(Mat inA, PetscScalar alpha)
2606: {
2607:   Mat_SeqAIJ  *a = (Mat_SeqAIJ *)inA->data;
2608:   PetscScalar *v;
2609:   PetscBLASInt one = 1, bnz;

2611:   PetscFunctionBegin;
2612:   PetscCall(MatSeqAIJGetArray(inA, &v));
2613:   PetscCall(PetscBLASIntCast(a->nz, &bnz));
2614:   PetscCallBLAS("BLASscal", BLASscal_(&bnz, &alpha, v, &one));
2615:   PetscCall(PetscLogFlops(a->nz));
2616:   PetscCall(MatSeqAIJRestoreArray(inA, &v));
2617:   PetscFunctionReturn(PETSC_SUCCESS);
2618: }

2620: PetscErrorCode MatDestroySubMatrix_Private(Mat_SubSppt *submatj)
2621: {
2622:   PetscInt i;

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

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

2631:     if (submatj->rbuf1) {
2632:       PetscCall(PetscFree(submatj->rbuf1[0]));
2633:       PetscCall(PetscFree(submatj->rbuf1));
2634:     }

2636:     for (i = 0; i < submatj->nrqs; ++i) PetscCall(PetscFree(submatj->rbuf3[i]));
2637:     PetscCall(PetscFree3(submatj->req_source2, submatj->rbuf2, submatj->rbuf3));
2638:     PetscCall(PetscFree(submatj->pa));
2639:   }

2641: #if PetscDefined(USE_CTABLE)
2642:   PetscCall(PetscHMapIDestroy(&submatj->rmap));
2643:   PetscCall(PetscFree(submatj->cmap_loc));
2644:   PetscCall(PetscFree(submatj->rmap_loc));
2645: #else
2646:   PetscCall(PetscFree(submatj->rmap));
2647: #endif

2649:   if (!submatj->allcolumns) {
2650: #if PetscDefined(USE_CTABLE)
2651:     PetscCall(PetscHMapIDestroy(&submatj->cmap));
2652: #else
2653:     PetscCall(PetscFree(submatj->cmap));
2654: #endif
2655:   }
2656:   PetscCall(PetscFree(submatj->row2proc));

2658:   PetscCall(PetscFree(submatj));
2659:   PetscFunctionReturn(PETSC_SUCCESS);
2660: }

2662: PetscErrorCode MatDestroySubMatrix_SeqAIJ(Mat C)
2663: {
2664:   Mat_SeqAIJ  *c       = (Mat_SeqAIJ *)C->data;
2665:   Mat_SubSppt *submatj = c->submatis1;

2667:   PetscFunctionBegin;
2668:   PetscCall((*submatj->destroy)(C));
2669:   PetscCall(MatDestroySubMatrix_Private(submatj));
2670:   PetscFunctionReturn(PETSC_SUCCESS);
2671: }

2673: /* Note this has code duplication with MatDestroySubMatrices_SeqBAIJ() */
2674: static PetscErrorCode MatDestroySubMatrices_SeqAIJ(PetscInt n, Mat *mat[])
2675: {
2676:   PetscInt     i;
2677:   Mat          C;
2678:   Mat_SeqAIJ  *c;
2679:   Mat_SubSppt *submatj;

2681:   PetscFunctionBegin;
2682:   for (i = 0; i < n; i++) {
2683:     C       = (*mat)[i];
2684:     c       = (Mat_SeqAIJ *)C->data;
2685:     submatj = c->submatis1;
2686:     if (submatj) {
2687:       if (--((PetscObject)C)->refct <= 0) {
2688:         PetscCall(PetscFree(C->factorprefix));
2689:         PetscCall((*submatj->destroy)(C));
2690:         PetscCall(MatDestroySubMatrix_Private(submatj));
2691:         PetscCall(PetscFree(C->defaultvectype));
2692:         PetscCall(PetscFree(C->defaultrandtype));
2693:         PetscCall(PetscLayoutDestroy(&C->rmap));
2694:         PetscCall(PetscLayoutDestroy(&C->cmap));
2695:         PetscCall(PetscHeaderDestroy(&C));
2696:       }
2697:     } else {
2698:       PetscCall(MatDestroy(&C));
2699:     }
2700:   }

2702:   /* Destroy Dummy submatrices created for reuse */
2703:   PetscCall(MatDestroySubMatrices_Dummy(n, mat));

2705:   PetscCall(PetscFree(*mat));
2706:   PetscFunctionReturn(PETSC_SUCCESS);
2707: }

2709: static PetscErrorCode MatCreateSubMatrices_SeqAIJ(Mat A, PetscInt n, const IS irow[], const IS icol[], MatReuse scall, Mat *B[])
2710: {
2711:   PetscInt i;

2713:   PetscFunctionBegin;
2714:   if (scall == MAT_INITIAL_MATRIX) PetscCall(PetscCalloc1(n + 1, B));

2716:   for (i = 0; i < n; i++) PetscCall(MatCreateSubMatrix_SeqAIJ(A, irow[i], icol[i], PETSC_DECIDE, scall, &(*B)[i]));
2717:   PetscFunctionReturn(PETSC_SUCCESS);
2718: }

2720: static PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A, PetscInt is_max, IS is[], PetscInt ov)
2721: {
2722:   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data;
2723:   PetscInt        row, i, j, k, l, ll, m, n, *nidx, isz, val;
2724:   const PetscInt *idx;
2725:   PetscInt        start, end, *ai, *aj, bs = A->rmap->bs == A->cmap->bs ? A->rmap->bs : 1;
2726:   PetscBT         table;

2728:   PetscFunctionBegin;
2729:   m  = A->rmap->n / bs;
2730:   ai = a->i;
2731:   aj = a->j;

2733:   PetscCheck(ov >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "illegal negative overlap value used");

2735:   PetscCall(PetscMalloc1(m + 1, &nidx));
2736:   PetscCall(PetscBTCreate(m, &table));

2738:   for (i = 0; i < is_max; i++) {
2739:     /* Initialize the two local arrays */
2740:     isz = 0;
2741:     PetscCall(PetscBTMemzero(m, table));

2743:     /* Extract the indices, assume there can be duplicate entries */
2744:     PetscCall(ISGetIndices(is[i], &idx));
2745:     PetscCall(ISGetLocalSize(is[i], &n));

2747:     if (bs > 1) {
2748:       /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
2749:       for (j = 0; j < n; ++j) {
2750:         if (!PetscBTLookupSet(table, idx[j] / bs)) nidx[isz++] = idx[j] / bs;
2751:       }
2752:       PetscCall(ISRestoreIndices(is[i], &idx));
2753:       PetscCall(ISDestroy(&is[i]));

2755:       k = 0;
2756:       for (j = 0; j < ov; j++) { /* for each overlap */
2757:         n = isz;
2758:         for (; k < n; k++) { /* do only those rows in nidx[k], which are not done yet */
2759:           for (ll = 0; ll < bs; ll++) {
2760:             row   = bs * nidx[k] + ll;
2761:             start = ai[row];
2762:             end   = ai[row + 1];
2763:             for (l = start; l < end; l++) {
2764:               val = aj[l] / bs;
2765:               if (!PetscBTLookupSet(table, val)) nidx[isz++] = val;
2766:             }
2767:           }
2768:         }
2769:       }
2770:       PetscCall(ISCreateBlock(PETSC_COMM_SELF, bs, isz, nidx, PETSC_COPY_VALUES, is + i));
2771:     } else {
2772:       /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
2773:       for (j = 0; j < n; ++j) {
2774:         if (!PetscBTLookupSet(table, idx[j])) nidx[isz++] = idx[j];
2775:       }
2776:       PetscCall(ISRestoreIndices(is[i], &idx));
2777:       PetscCall(ISDestroy(&is[i]));

2779:       k = 0;
2780:       for (j = 0; j < ov; j++) { /* for each overlap */
2781:         n = isz;
2782:         for (; k < n; k++) { /* do only those rows in nidx[k], which are not done yet */
2783:           row   = nidx[k];
2784:           start = ai[row];
2785:           end   = ai[row + 1];
2786:           for (l = start; l < end; l++) {
2787:             val = aj[l];
2788:             if (!PetscBTLookupSet(table, val)) nidx[isz++] = val;
2789:           }
2790:         }
2791:       }
2792:       PetscCall(ISCreateGeneral(PETSC_COMM_SELF, isz, nidx, PETSC_COPY_VALUES, is + i));
2793:     }
2794:   }
2795:   PetscCall(PetscBTDestroy(&table));
2796:   PetscCall(PetscFree(nidx));
2797:   PetscFunctionReturn(PETSC_SUCCESS);
2798: }

2800: static PetscErrorCode MatPermute_SeqAIJ(Mat A, IS rowp, IS colp, Mat *B)
2801: {
2802:   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data;
2803:   PetscInt        i, nz = 0, m = A->rmap->n, n = A->cmap->n;
2804:   const PetscInt *row, *col;
2805:   PetscInt       *cnew, j, *lens;
2806:   IS              icolp, irowp;
2807:   PetscInt       *cwork = NULL;
2808:   PetscScalar    *vwork = NULL;

2810:   PetscFunctionBegin;
2811:   PetscCall(ISInvertPermutation(rowp, PETSC_DECIDE, &irowp));
2812:   PetscCall(ISGetIndices(irowp, &row));
2813:   PetscCall(ISInvertPermutation(colp, PETSC_DECIDE, &icolp));
2814:   PetscCall(ISGetIndices(icolp, &col));

2816:   /* determine lengths of permuted rows */
2817:   PetscCall(PetscMalloc1(m + 1, &lens));
2818:   for (i = 0; i < m; i++) lens[row[i]] = a->i[i + 1] - a->i[i];
2819:   PetscCall(MatCreate(PetscObjectComm((PetscObject)A), B));
2820:   PetscCall(MatSetSizes(*B, m, n, m, n));
2821:   PetscCall(MatSetBlockSizesFromMats(*B, A, A));
2822:   PetscCall(MatSetType(*B, ((PetscObject)A)->type_name));
2823:   PetscCall(MatSeqAIJSetPreallocation_SeqAIJ(*B, 0, lens));
2824:   PetscCall(PetscFree(lens));

2826:   PetscCall(PetscMalloc1(n, &cnew));
2827:   for (i = 0; i < m; i++) {
2828:     PetscCall(MatGetRow_SeqAIJ(A, i, &nz, &cwork, &vwork));
2829:     for (j = 0; j < nz; j++) cnew[j] = col[cwork[j]];
2830:     PetscCall(MatSetValues_SeqAIJ(*B, 1, &row[i], nz, cnew, vwork, INSERT_VALUES));
2831:     PetscCall(MatRestoreRow_SeqAIJ(A, i, &nz, &cwork, &vwork));
2832:   }
2833:   PetscCall(PetscFree(cnew));

2835:   (*B)->assembled = PETSC_FALSE;

2837: #if PetscDefined(HAVE_DEVICE)
2838:   PetscCall(MatBindToCPU(*B, A->boundtocpu));
2839: #endif
2840:   PetscCall(MatAssemblyBegin(*B, MAT_FINAL_ASSEMBLY));
2841:   PetscCall(MatAssemblyEnd(*B, MAT_FINAL_ASSEMBLY));
2842:   PetscCall(ISRestoreIndices(irowp, &row));
2843:   PetscCall(ISRestoreIndices(icolp, &col));
2844:   PetscCall(ISDestroy(&irowp));
2845:   PetscCall(ISDestroy(&icolp));
2846:   if (rowp == colp) PetscCall(MatPropagateSymmetryOptions(A, *B));
2847:   PetscFunctionReturn(PETSC_SUCCESS);
2848: }

2850: PetscErrorCode MatCopy_SeqAIJ(Mat A, Mat B, MatStructure str)
2851: {
2852:   PetscFunctionBegin;
2853:   /* If the two matrices have the same copy implementation, use fast copy. */
2854:   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
2855:     Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data;
2856:     Mat_SeqAIJ        *b = (Mat_SeqAIJ *)B->data;
2857:     const PetscScalar *aa;
2858:     PetscScalar       *bb;

2860:     PetscCall(MatSeqAIJGetArrayRead(A, &aa));
2861:     PetscCall(MatSeqAIJGetArrayWrite(B, &bb));

2863:     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]);
2864:     PetscCall(PetscArraycpy(bb, aa, a->i[A->rmap->n]));
2865:     PetscCall(PetscObjectStateIncrease((PetscObject)B));
2866:     PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
2867:     PetscCall(MatSeqAIJRestoreArrayWrite(B, &bb));
2868:   } else {
2869:     PetscCall(MatCopy_Basic(A, B, str));
2870:   }
2871:   PetscFunctionReturn(PETSC_SUCCESS);
2872: }

2874: PETSC_INTERN PetscErrorCode MatSeqAIJGetArray_SeqAIJ(Mat A, PetscScalar *array[])
2875: {
2876:   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;

2878:   PetscFunctionBegin;
2879:   *array = a->a;
2880:   PetscFunctionReturn(PETSC_SUCCESS);
2881: }

2883: PETSC_INTERN PetscErrorCode MatSeqAIJRestoreArray_SeqAIJ(Mat A, PetscScalar *array[])
2884: {
2885:   PetscFunctionBegin;
2886:   *array = NULL;
2887:   PetscFunctionReturn(PETSC_SUCCESS);
2888: }

2890: /*
2891:    Computes the number of nonzeros per row needed for preallocation when X and Y
2892:    have different nonzero structure.
2893: */
2894: PetscErrorCode MatAXPYGetPreallocation_SeqX_private(PetscInt m, const PetscInt *xi, const PetscInt *xj, const PetscInt *yi, const PetscInt *yj, PetscInt *nnz)
2895: {
2896:   PetscInt i, j, k, nzx, nzy;

2898:   PetscFunctionBegin;
2899:   /* Set the number of nonzeros in the new matrix */
2900:   for (i = 0; i < m; i++) {
2901:     const PetscInt *xjj = PetscSafePointerPlusOffset(xj, xi[i]), *yjj = PetscSafePointerPlusOffset(yj, yi[i]);
2902:     nzx    = xi[i + 1] - xi[i];
2903:     nzy    = yi[i + 1] - yi[i];
2904:     nnz[i] = 0;
2905:     for (j = 0, k = 0; j < nzx; j++) {                  /* Point in X */
2906:       for (; k < nzy && yjj[k] < xjj[j]; k++) nnz[i]++; /* Catch up to X */
2907:       if (k < nzy && yjj[k] == xjj[j]) k++;             /* Skip duplicate */
2908:       nnz[i]++;
2909:     }
2910:     for (; k < nzy; k++) nnz[i]++;
2911:   }
2912:   PetscFunctionReturn(PETSC_SUCCESS);
2913: }

2915: PetscErrorCode MatAXPYGetPreallocation_SeqAIJ(Mat Y, Mat X, PetscInt *nnz)
2916: {
2917:   PetscInt    m = Y->rmap->N;
2918:   Mat_SeqAIJ *x = (Mat_SeqAIJ *)X->data;
2919:   Mat_SeqAIJ *y = (Mat_SeqAIJ *)Y->data;

2921:   PetscFunctionBegin;
2922:   /* Set the number of nonzeros in the new matrix */
2923:   PetscCall(MatAXPYGetPreallocation_SeqX_private(m, x->i, x->j, y->i, y->j, nnz));
2924:   PetscFunctionReturn(PETSC_SUCCESS);
2925: }

2927: PetscErrorCode MatAXPY_SeqAIJ(Mat Y, PetscScalar a, Mat X, MatStructure str)
2928: {
2929:   Mat_SeqAIJ *x = (Mat_SeqAIJ *)X->data, *y = (Mat_SeqAIJ *)Y->data;

2931:   PetscFunctionBegin;
2932:   if (str == UNKNOWN_NONZERO_PATTERN || (PetscDefined(USE_DEBUG) && str == SAME_NONZERO_PATTERN)) {
2933:     PetscBool e = x->nz == y->nz ? PETSC_TRUE : PETSC_FALSE;
2934:     if (e) {
2935:       PetscCall(PetscArraycmp(x->i, y->i, Y->rmap->n + 1, &e));
2936:       if (e) {
2937:         PetscCall(PetscArraycmp(x->j, y->j, y->nz, &e));
2938:         if (e) str = SAME_NONZERO_PATTERN;
2939:       }
2940:     }
2941:     if (!e) PetscCheck(str != SAME_NONZERO_PATTERN, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "MatStructure is not SAME_NONZERO_PATTERN");
2942:   }
2943:   if (str == SAME_NONZERO_PATTERN) {
2944:     const PetscScalar *xa;
2945:     PetscScalar       *ya, alpha = a;
2946:     PetscBLASInt       one = 1, bnz;

2948:     PetscCall(PetscBLASIntCast(x->nz, &bnz));
2949:     PetscCall(MatSeqAIJGetArray(Y, &ya));
2950:     PetscCall(MatSeqAIJGetArrayRead(X, &xa));
2951:     PetscCallBLAS("BLASaxpy", BLASaxpy_(&bnz, &alpha, xa, &one, ya, &one));
2952:     PetscCall(MatSeqAIJRestoreArrayRead(X, &xa));
2953:     PetscCall(MatSeqAIJRestoreArray(Y, &ya));
2954:     PetscCall(PetscLogFlops(2.0 * bnz));
2955:     PetscCall(PetscObjectStateIncrease((PetscObject)Y));
2956:   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2957:     PetscCall(MatAXPY_Basic(Y, a, X, str));
2958:   } else {
2959:     Mat       B;
2960:     PetscInt *nnz;
2961:     PetscCall(PetscMalloc1(Y->rmap->N, &nnz));
2962:     PetscCall(MatCreate(PetscObjectComm((PetscObject)Y), &B));
2963:     PetscCall(PetscObjectSetName((PetscObject)B, ((PetscObject)Y)->name));
2964:     PetscCall(MatSetLayouts(B, Y->rmap, Y->cmap));
2965:     PetscCall(MatSetType(B, ((PetscObject)Y)->type_name));
2966:     PetscCall(MatAXPYGetPreallocation_SeqAIJ(Y, X, nnz));
2967:     PetscCall(MatSeqAIJSetPreallocation(B, 0, nnz));
2968:     PetscCall(MatAXPY_BasicWithPreallocation(B, Y, a, X, str));
2969:     PetscCall(MatHeaderMerge(Y, &B));
2970:     PetscCall(MatSeqAIJCheckInode(Y));
2971:     PetscCall(PetscFree(nnz));
2972:   }
2973:   PetscFunctionReturn(PETSC_SUCCESS);
2974: }

2976: PETSC_INTERN PetscErrorCode MatConjugate_SeqAIJ(Mat mat)
2977: {
2978:   Mat_SeqAIJ  *aij = (Mat_SeqAIJ *)mat->data;
2979:   PetscInt     i, nz = aij->nz;
2980:   PetscScalar *a;

2982:   PetscFunctionBegin;
2983:   PetscCall(MatSeqAIJGetArray(mat, &a));
2984:   for (i = 0; i < nz; i++) a[i] = PetscConj(a[i]);
2985:   PetscCall(MatSeqAIJRestoreArray(mat, &a));
2986:   PetscFunctionReturn(PETSC_SUCCESS);
2987: }

2989: static PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A, Vec v, PetscInt idx[])
2990: {
2991:   Mat_SeqAIJ      *a = (Mat_SeqAIJ *)A->data;
2992:   PetscInt         i, j, m = A->rmap->n, *ai, *aj, ncols, n;
2993:   PetscReal        atmp;
2994:   PetscScalar     *x;
2995:   const MatScalar *aa, *av;

2997:   PetscFunctionBegin;
2998:   PetscCheck(!A->factortype, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
2999:   PetscCall(MatSeqAIJGetArrayRead(A, &av));
3000:   aa = av;
3001:   ai = a->i;
3002:   aj = a->j;

3004:   PetscCall(VecGetArrayWrite(v, &x));
3005:   PetscCall(VecGetLocalSize(v, &n));
3006:   PetscCheck(n == A->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nonconforming matrix and vector");
3007:   for (i = 0; i < m; i++) {
3008:     ncols = ai[1] - ai[0];
3009:     ai++;
3010:     x[i] = 0;
3011:     for (j = 0; j < ncols; j++) {
3012:       atmp = PetscAbsScalar(*aa);
3013:       if (PetscAbsScalar(x[i]) < atmp) {
3014:         x[i] = atmp;
3015:         if (idx) idx[i] = *aj;
3016:       }
3017:       aa++;
3018:       aj++;
3019:     }
3020:   }
3021:   PetscCall(VecRestoreArrayWrite(v, &x));
3022:   PetscCall(MatSeqAIJRestoreArrayRead(A, &av));
3023:   PetscFunctionReturn(PETSC_SUCCESS);
3024: }

3026: static PetscErrorCode MatGetRowSumAbs_SeqAIJ(Mat A, Vec v)
3027: {
3028:   Mat_SeqAIJ      *a = (Mat_SeqAIJ *)A->data;
3029:   PetscInt         i, j, m = A->rmap->n, *ai, ncols, n;
3030:   PetscScalar     *x;
3031:   const MatScalar *aa, *av;

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

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

3056: static PetscErrorCode MatGetRowMax_SeqAIJ(Mat A, Vec v, PetscInt idx[])
3057: {
3058:   Mat_SeqAIJ      *a = (Mat_SeqAIJ *)A->data;
3059:   PetscInt         i, j, m = A->rmap->n, *ai, *aj, ncols, n;
3060:   PetscScalar     *x;
3061:   const MatScalar *aa, *av;

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

3070:   PetscCall(VecGetArrayWrite(v, &x));
3071:   PetscCall(VecGetLocalSize(v, &n));
3072:   PetscCheck(n == A->rmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nonconforming matrix and vector");
3073:   for (i = 0; i < m; i++) {
3074:     ncols = ai[1] - ai[0];
3075:     ai++;
3076:     if (ncols == A->cmap->n) { /* row is dense */
3077:       x[i] = *aa;
3078:       if (idx) idx[i] = 0;
3079:     } else { /* row is sparse so already KNOW maximum is 0.0 or higher */
3080:       x[i] = 0.0;
3081:       if (idx) {
3082:         for (j = 0; j < ncols; j++) { /* find first implicit 0.0 in the row */
3083:           if (aj[j] > j) {
3084:             idx[i] = j;
3085:             break;
3086:           }
3087:         }
3088:         /* in case first implicit 0.0 in the row occurs at ncols-th column */
3089:         if (j == ncols && j < A->cmap->n) idx[i] = j;
3090:       }
3091:     }
3092:     for (j = 0; j < ncols; j++) {
3093:       if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {
3094:         x[i] = *aa;
3095:         if (idx) idx[i] = *aj;
3096:       }
3097:       aa++;
3098:       aj++;
3099:     }
3100:   }
3101:   PetscCall(VecRestoreArrayWrite(v, &x));
3102:   PetscCall(MatSeqAIJRestoreArrayRead(A, &av));
3103:   PetscFunctionReturn(PETSC_SUCCESS);
3104: }

3106: static PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A, Vec v, PetscInt idx[])
3107: {
3108:   Mat_SeqAIJ      *a = (Mat_SeqAIJ *)A->data;
3109:   PetscInt         i, j, m = A->rmap->n, *ai, *aj, ncols, n;
3110:   PetscScalar     *x;
3111:   const MatScalar *aa, *av;

3113:   PetscFunctionBegin;
3114:   PetscCall(MatSeqAIJGetArrayRead(A, &av));
3115:   aa = av;
3116:   ai = a->i;
3117:   aj = a->j;

3119:   PetscCall(VecGetArrayWrite(v, &x));
3120:   PetscCall(VecGetLocalSize(v, &n));
3121:   PetscCheck(n == m, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nonconforming matrix and vector, %" PetscInt_FMT " vs. %" PetscInt_FMT " rows", m, n);
3122:   for (i = 0; i < m; i++) {
3123:     ncols = ai[1] - ai[0];
3124:     ai++;
3125:     if (ncols == A->cmap->n) { /* row is dense */
3126:       x[i] = *aa;
3127:       if (idx) idx[i] = 0;
3128:     } else { /* row is sparse so already KNOW minimum is 0.0 or higher */
3129:       x[i] = 0.0;
3130:       if (idx) { /* find first implicit 0.0 in the row */
3131:         for (j = 0; j < ncols; j++) {
3132:           if (aj[j] > j) {
3133:             idx[i] = j;
3134:             break;
3135:           }
3136:         }
3137:         /* in case first implicit 0.0 in the row occurs at ncols-th column */
3138:         if (j == ncols && j < A->cmap->n) idx[i] = j;
3139:       }
3140:     }
3141:     for (j = 0; j < ncols; j++) {
3142:       if (PetscAbsScalar(x[i]) > PetscAbsScalar(*aa)) {
3143:         x[i] = *aa;
3144:         if (idx) idx[i] = *aj;
3145:       }
3146:       aa++;
3147:       aj++;
3148:     }
3149:   }
3150:   PetscCall(VecRestoreArrayWrite(v, &x));
3151:   PetscCall(MatSeqAIJRestoreArrayRead(A, &av));
3152:   PetscFunctionReturn(PETSC_SUCCESS);
3153: }

3155: static PetscErrorCode MatGetRowMin_SeqAIJ(Mat A, Vec v, PetscInt idx[])
3156: {
3157:   Mat_SeqAIJ      *a = (Mat_SeqAIJ *)A->data;
3158:   PetscInt         i, j, m = A->rmap->n, ncols, n;
3159:   const PetscInt  *ai, *aj;
3160:   PetscScalar     *x;
3161:   const MatScalar *aa, *av;

3163:   PetscFunctionBegin;
3164:   PetscCheck(!A->factortype, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
3165:   PetscCall(MatSeqAIJGetArrayRead(A, &av));
3166:   aa = av;
3167:   ai = a->i;
3168:   aj = a->j;

3170:   PetscCall(VecGetArrayWrite(v, &x));
3171:   PetscCall(VecGetLocalSize(v, &n));
3172:   PetscCheck(n == m, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Nonconforming matrix and vector");
3173:   for (i = 0; i < m; i++) {
3174:     ncols = ai[1] - ai[0];
3175:     ai++;
3176:     if (ncols == A->cmap->n) { /* row is dense */
3177:       x[i] = *aa;
3178:       if (idx) idx[i] = 0;
3179:     } else { /* row is sparse so already KNOW minimum is 0.0 or lower */
3180:       x[i] = 0.0;
3181:       if (idx) { /* find first implicit 0.0 in the row */
3182:         for (j = 0; j < ncols; j++) {
3183:           if (aj[j] > j) {
3184:             idx[i] = j;
3185:             break;
3186:           }
3187:         }
3188:         /* in case first implicit 0.0 in the row occurs at ncols-th column */
3189:         if (j == ncols && j < A->cmap->n) idx[i] = j;
3190:       }
3191:     }
3192:     for (j = 0; j < ncols; j++) {
3193:       if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {
3194:         x[i] = *aa;
3195:         if (idx) idx[i] = *aj;
3196:       }
3197:       aa++;
3198:       aj++;
3199:     }
3200:   }
3201:   PetscCall(VecRestoreArrayWrite(v, &x));
3202:   PetscCall(MatSeqAIJRestoreArrayRead(A, &av));
3203:   PetscFunctionReturn(PETSC_SUCCESS);
3204: }

3206: static PetscErrorCode MatInvertBlockDiagonal_SeqAIJ(Mat A, const PetscScalar **values)
3207: {
3208:   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data;
3209:   PetscInt        i, bs = A->rmap->bs, mbs = A->rmap->n / bs, ipvt[5], bs2 = bs * bs, *v_pivots, ij[7], *IJ, j;
3210:   MatScalar      *diag, work[25], *v_work;
3211:   const PetscReal shift = 0.0;
3212:   PetscBool       allowzeropivot, zeropivotdetected = PETSC_FALSE;

3214:   PetscFunctionBegin;
3215:   allowzeropivot = PetscNot(A->erroriffailure);
3216:   if (a->ibdiag && a->ibdiagsize == bs2 * mbs && a->ibdiagState == ((PetscObject)A)->state) {
3217:     if (values) *values = a->ibdiag;
3218:     PetscFunctionReturn(PETSC_SUCCESS);
3219:   }
3220:   /* reallocate only when the length changes, so that the pointer stays valid for callers that
3221:      hold on to it, such as PCSetUp_PBJacobi_Host() */
3222:   if (!a->ibdiag || a->ibdiagsize != bs2 * mbs) {
3223:     PetscCall(PetscFree(a->ibdiag));
3224:     PetscCall(PetscMalloc1(bs2 * mbs, &a->ibdiag));
3225:     a->ibdiagsize = bs2 * mbs;
3226:   }
3227:   diag = a->ibdiag;
3228:   if (values) *values = a->ibdiag;
3229:   /* factor and invert each block */
3230:   switch (bs) {
3231:   case 1:
3232:     for (i = 0; i < mbs; i++) {
3233:       PetscCall(MatGetValues(A, 1, &i, 1, &i, diag + i));
3234:       if (PetscAbsScalar(diag[i] + shift) < PETSC_MACHINE_EPSILON) {
3235:         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);
3236:         A->factorerrortype             = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3237:         A->factorerror_zeropivot_value = PetscAbsScalar(diag[i]);
3238:         A->factorerror_zeropivot_row   = i;
3239:         PetscCall(PetscInfo(A, "Zero pivot, row %" PetscInt_FMT " pivot %g tolerance %g\n", i, (double)PetscAbsScalar(diag[i]), (double)PETSC_MACHINE_EPSILON));
3240:       }
3241:       diag[i] = (PetscScalar)1.0 / (diag[i] + shift);
3242:     }
3243:     break;
3244:   case 2:
3245:     for (i = 0; i < mbs; i++) {
3246:       ij[0] = 2 * i;
3247:       ij[1] = 2 * i + 1;
3248:       PetscCall(MatGetValues(A, 2, ij, 2, ij, diag));
3249:       PetscCall(PetscKernel_A_gets_inverse_A_2(diag, shift, allowzeropivot, &zeropivotdetected));
3250:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3251:       PetscCall(PetscKernel_A_gets_transpose_A_2(diag));
3252:       diag += 4;
3253:     }
3254:     break;
3255:   case 3:
3256:     for (i = 0; i < mbs; i++) {
3257:       ij[0] = 3 * i;
3258:       ij[1] = 3 * i + 1;
3259:       ij[2] = 3 * i + 2;
3260:       PetscCall(MatGetValues(A, 3, ij, 3, ij, diag));
3261:       PetscCall(PetscKernel_A_gets_inverse_A_3(diag, shift, allowzeropivot, &zeropivotdetected));
3262:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3263:       PetscCall(PetscKernel_A_gets_transpose_A_3(diag));
3264:       diag += 9;
3265:     }
3266:     break;
3267:   case 4:
3268:     for (i = 0; i < mbs; i++) {
3269:       ij[0] = 4 * i;
3270:       ij[1] = 4 * i + 1;
3271:       ij[2] = 4 * i + 2;
3272:       ij[3] = 4 * i + 3;
3273:       PetscCall(MatGetValues(A, 4, ij, 4, ij, diag));
3274:       PetscCall(PetscKernel_A_gets_inverse_A_4(diag, shift, allowzeropivot, &zeropivotdetected));
3275:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3276:       PetscCall(PetscKernel_A_gets_transpose_A_4(diag));
3277:       diag += 16;
3278:     }
3279:     break;
3280:   case 5:
3281:     for (i = 0; i < mbs; i++) {
3282:       ij[0] = 5 * i;
3283:       ij[1] = 5 * i + 1;
3284:       ij[2] = 5 * i + 2;
3285:       ij[3] = 5 * i + 3;
3286:       ij[4] = 5 * i + 4;
3287:       PetscCall(MatGetValues(A, 5, ij, 5, ij, diag));
3288:       PetscCall(PetscKernel_A_gets_inverse_A_5(diag, ipvt, work, shift, allowzeropivot, &zeropivotdetected));
3289:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3290:       PetscCall(PetscKernel_A_gets_transpose_A_5(diag));
3291:       diag += 25;
3292:     }
3293:     break;
3294:   case 6:
3295:     for (i = 0; i < mbs; i++) {
3296:       ij[0] = 6 * i;
3297:       ij[1] = 6 * i + 1;
3298:       ij[2] = 6 * i + 2;
3299:       ij[3] = 6 * i + 3;
3300:       ij[4] = 6 * i + 4;
3301:       ij[5] = 6 * i + 5;
3302:       PetscCall(MatGetValues(A, 6, ij, 6, ij, diag));
3303:       PetscCall(PetscKernel_A_gets_inverse_A_6(diag, shift, allowzeropivot, &zeropivotdetected));
3304:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3305:       PetscCall(PetscKernel_A_gets_transpose_A_6(diag));
3306:       diag += 36;
3307:     }
3308:     break;
3309:   case 7:
3310:     for (i = 0; i < mbs; i++) {
3311:       ij[0] = 7 * i;
3312:       ij[1] = 7 * i + 1;
3313:       ij[2] = 7 * i + 2;
3314:       ij[3] = 7 * i + 3;
3315:       ij[4] = 7 * i + 4;
3316:       ij[5] = 7 * i + 5;
3317:       ij[6] = 7 * i + 6;
3318:       PetscCall(MatGetValues(A, 7, ij, 7, ij, diag));
3319:       PetscCall(PetscKernel_A_gets_inverse_A_7(diag, shift, allowzeropivot, &zeropivotdetected));
3320:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3321:       PetscCall(PetscKernel_A_gets_transpose_A_7(diag));
3322:       diag += 49;
3323:     }
3324:     break;
3325:   default:
3326:     PetscCall(PetscMalloc3(bs, &v_work, bs, &v_pivots, bs, &IJ));
3327:     for (i = 0; i < mbs; i++) {
3328:       for (j = 0; j < bs; j++) IJ[j] = bs * i + j;
3329:       PetscCall(MatGetValues(A, bs, IJ, bs, IJ, diag));
3330:       PetscCall(PetscKernel_A_gets_inverse_A(bs, diag, v_pivots, v_work, allowzeropivot, &zeropivotdetected));
3331:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3332:       PetscCall(PetscKernel_A_gets_transpose_A_N(diag, bs));
3333:       diag += bs2;
3334:     }
3335:     PetscCall(PetscFree3(v_work, v_pivots, IJ));
3336:   }
3337:   a->ibdiagState = ((PetscObject)A)->state;
3338:   PetscFunctionReturn(PETSC_SUCCESS);
3339: }

3341: static PetscErrorCode MatSetRandom_SeqAIJ(Mat x, PetscRandom rctx)
3342: {
3343:   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)x->data;
3344:   PetscScalar a, *aa;
3345:   PetscInt    m, n, i, j, col;

3347:   PetscFunctionBegin;
3348:   if (!x->assembled) {
3349:     PetscCall(MatGetSize(x, &m, &n));
3350:     for (i = 0; i < m; i++) {
3351:       for (j = 0; j < aij->imax[i]; j++) {
3352:         PetscCall(PetscRandomGetValue(rctx, &a));
3353:         col = (PetscInt)(n * PetscRealPart(a));
3354:         PetscCall(MatSetValues(x, 1, &i, 1, &col, &a, ADD_VALUES));
3355:       }
3356:     }
3357:   } else {
3358:     PetscCall(MatSeqAIJGetArrayWrite(x, &aa));
3359:     for (i = 0; i < aij->nz; i++) PetscCall(PetscRandomGetValue(rctx, aa + i));
3360:     PetscCall(MatSeqAIJRestoreArrayWrite(x, &aa));
3361:   }
3362:   PetscCall(MatAssemblyBegin(x, MAT_FINAL_ASSEMBLY));
3363:   PetscCall(MatAssemblyEnd(x, MAT_FINAL_ASSEMBLY));
3364:   PetscFunctionReturn(PETSC_SUCCESS);
3365: }

3367: /* Like MatSetRandom_SeqAIJ, but do not set values on columns in range of [low, high) */
3368: PetscErrorCode MatSetRandomSkipColumnRange_SeqAIJ_Private(Mat x, PetscInt low, PetscInt high, PetscRandom rctx)
3369: {
3370:   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)x->data;
3371:   PetscScalar a;
3372:   PetscInt    m, n, i, j, col, nskip;

3374:   PetscFunctionBegin;
3375:   nskip = high - low;
3376:   PetscCall(MatGetSize(x, &m, &n));
3377:   n -= nskip; /* shrink number of columns where nonzeros can be set */
3378:   for (i = 0; i < m; i++) {
3379:     for (j = 0; j < aij->imax[i]; j++) {
3380:       PetscCall(PetscRandomGetValue(rctx, &a));
3381:       col = (PetscInt)(n * PetscRealPart(a));
3382:       if (col >= low) col += nskip; /* shift col rightward to skip the hole */
3383:       PetscCall(MatSetValues(x, 1, &i, 1, &col, &a, ADD_VALUES));
3384:     }
3385:   }
3386:   PetscCall(MatAssemblyBegin(x, MAT_FINAL_ASSEMBLY));
3387:   PetscCall(MatAssemblyEnd(x, MAT_FINAL_ASSEMBLY));
3388:   PetscFunctionReturn(PETSC_SUCCESS);
3389: }

3391: static struct _MatOps MatOps_Values = {MatSetValues_SeqAIJ,
3392:                                        MatGetRow_SeqAIJ,
3393:                                        MatRestoreRow_SeqAIJ,
3394:                                        MatMult_SeqAIJ,
3395:                                        /*  4*/ MatMultAdd_SeqAIJ,
3396:                                        MatMultTranspose_SeqAIJ,
3397:                                        MatMultTransposeAdd_SeqAIJ,
3398:                                        NULL,
3399:                                        NULL,
3400:                                        NULL,
3401:                                        /* 10*/ NULL,
3402:                                        MatLUFactor_SeqAIJ,
3403:                                        NULL,
3404:                                        MatSOR_SeqAIJ,
3405:                                        MatTranspose_SeqAIJ,
3406:                                        /* 15*/ MatGetInfo_SeqAIJ,
3407:                                        MatEqual_SeqAIJ,
3408:                                        MatGetDiagonal_SeqAIJ,
3409:                                        MatDiagonalScale_SeqAIJ,
3410:                                        MatNorm_SeqAIJ,
3411:                                        /* 20*/ NULL,
3412:                                        MatAssemblyEnd_SeqAIJ,
3413:                                        MatSetOption_SeqAIJ,
3414:                                        MatZeroEntries_SeqAIJ,
3415:                                        /* 24*/ MatZeroRows_SeqAIJ,
3416:                                        NULL,
3417:                                        NULL,
3418:                                        NULL,
3419:                                        NULL,
3420:                                        /* 29*/ MatSetUp_Seq_Hash,
3421:                                        NULL,
3422:                                        NULL,
3423:                                        NULL,
3424:                                        NULL,
3425:                                        /* 34*/ MatDuplicate_SeqAIJ,
3426:                                        NULL,
3427:                                        NULL,
3428:                                        MatILUFactor_SeqAIJ,
3429:                                        NULL,
3430:                                        /* 39*/ MatAXPY_SeqAIJ,
3431:                                        MatCreateSubMatrices_SeqAIJ,
3432:                                        MatIncreaseOverlap_SeqAIJ,
3433:                                        MatGetValues_SeqAIJ,
3434:                                        MatCopy_SeqAIJ,
3435:                                        /* 44*/ MatGetRowMax_SeqAIJ,
3436:                                        MatScale_SeqAIJ,
3437:                                        MatShift_SeqAIJ,
3438:                                        MatDiagonalSet_SeqAIJ,
3439:                                        MatZeroRowsColumns_SeqAIJ,
3440:                                        /* 49*/ MatSetRandom_SeqAIJ,
3441:                                        MatGetRowIJ_SeqAIJ,
3442:                                        MatRestoreRowIJ_SeqAIJ,
3443:                                        MatGetColumnIJ_SeqAIJ,
3444:                                        MatRestoreColumnIJ_SeqAIJ,
3445:                                        /* 54*/ MatFDColoringCreate_SeqXAIJ,
3446:                                        NULL,
3447:                                        NULL,
3448:                                        MatPermute_SeqAIJ,
3449:                                        NULL,
3450:                                        /* 59*/ NULL,
3451:                                        MatDestroy_SeqAIJ,
3452:                                        MatView_SeqAIJ,
3453:                                        NULL,
3454:                                        NULL,
3455:                                        /* 64*/ MatMatMatMultNumeric_SeqAIJ_SeqAIJ_SeqAIJ,
3456:                                        NULL,
3457:                                        NULL,
3458:                                        NULL,
3459:                                        MatGetRowMaxAbs_SeqAIJ,
3460:                                        /* 69*/ MatGetRowMinAbs_SeqAIJ,
3461:                                        NULL,
3462:                                        NULL,
3463:                                        MatFDColoringApply_AIJ,
3464:                                        NULL,
3465:                                        /* 74*/ MatFindZeroDiagonals_SeqAIJ,
3466:                                        NULL,
3467:                                        NULL,
3468:                                        NULL,
3469:                                        MatLoad_SeqAIJ,
3470:                                        /* 79*/ NULL,
3471:                                        NULL,
3472:                                        NULL,
3473:                                        NULL,
3474:                                        NULL,
3475:                                        /* 84*/ NULL,
3476:                                        MatMatMultNumeric_SeqAIJ_SeqAIJ,
3477:                                        MatPtAPNumeric_SeqAIJ_SeqAIJ_SparseAxpy,
3478:                                        NULL,
3479:                                        MatMatTransposeMultNumeric_SeqAIJ_SeqAIJ,
3480:                                        /* 90*/ NULL,
3481:                                        MatProductSetFromOptions_SeqAIJ,
3482:                                        NULL,
3483:                                        NULL,
3484:                                        MatConjugate_SeqAIJ,
3485:                                        /* 94*/ NULL,
3486:                                        MatSetValuesRow_SeqAIJ,
3487:                                        MatRealPart_SeqAIJ,
3488:                                        MatImaginaryPart_SeqAIJ,
3489:                                        NULL,
3490:                                        /* 99*/ NULL,
3491:                                        MatMatSolve_SeqAIJ,
3492:                                        NULL,
3493:                                        MatGetRowMin_SeqAIJ,
3494:                                        NULL,
3495:                                        /*104*/ NULL,
3496:                                        NULL,
3497:                                        NULL,
3498:                                        NULL,
3499:                                        NULL,
3500:                                        /*109*/ NULL,
3501:                                        NULL,
3502:                                        NULL,
3503:                                        NULL,
3504:                                        MatGetMultiProcBlock_SeqAIJ,
3505:                                        /*114*/ MatFindNonzeroRows_SeqAIJ,
3506:                                        MatGetColumnReductions_SeqAIJ,
3507:                                        MatInvertBlockDiagonal_SeqAIJ,
3508:                                        MatInvertVariableBlockDiagonal_SeqAIJ,
3509:                                        NULL,
3510:                                        /*119*/ NULL,
3511:                                        MatTransposeMatMultNumeric_SeqAIJ_SeqAIJ,
3512:                                        MatTransposeColoringCreate_SeqAIJ,
3513:                                        MatTransColoringApplySpToDen_SeqAIJ,
3514:                                        MatTransColoringApplyDenToSp_SeqAIJ,
3515:                                        /*124*/ MatRARtNumeric_SeqAIJ_SeqAIJ,
3516:                                        NULL,
3517:                                        NULL,
3518:                                        MatFDColoringSetUp_SeqXAIJ,
3519:                                        MatFindOffBlockDiagonalEntries_SeqAIJ,
3520:                                        /*129*/ MatCreateMPIMatConcatenateSeqMat_SeqAIJ,
3521:                                        MatDestroySubMatrices_SeqAIJ,
3522:                                        NULL,
3523:                                        NULL,
3524:                                        MatCreateGraph_Simple_AIJ,
3525:                                        /*134*/ MatTransposeSymbolic_SeqAIJ,
3526:                                        MatEliminateZeros_SeqAIJ,
3527:                                        MatGetRowSumAbs_SeqAIJ,
3528:                                        NULL,
3529:                                        NULL,
3530:                                        /*139*/ NULL,
3531:                                        MatCopyHashToXAIJ_Seq_Hash,
3532:                                        NULL,
3533:                                        NULL,
3534:                                        NULL,
3535:                                        /*144*/ NULL,
3536:                                        NULL,
3537:                                        NULL,
3538:                                        NULL};

3540: static PetscErrorCode MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat, PetscInt *indices)
3541: {
3542:   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
3543:   PetscInt    i, nz, n;

3545:   PetscFunctionBegin;
3546:   nz = aij->maxnz;
3547:   n  = mat->rmap->n;
3548:   for (i = 0; i < nz; i++) aij->j[i] = indices[i];
3549:   aij->nz = nz;
3550:   for (i = 0; i < n; i++) aij->ilen[i] = aij->imax[i];
3551:   PetscFunctionReturn(PETSC_SUCCESS);
3552: }

3554: /*
3555:  * Given a sparse matrix with global column indices, compact it by using a local column space.
3556:  * The result matrix helps saving memory in other algorithms, such as MatPtAPSymbolic_MPIAIJ_MPIAIJ_scalable()
3557:  */
3558: PetscErrorCode MatSeqAIJCompactOutExtraColumns_SeqAIJ(Mat mat, ISLocalToGlobalMapping *mapping)
3559: {
3560:   Mat_SeqAIJ   *aij = (Mat_SeqAIJ *)mat->data;
3561:   PetscHMapI    gid1_lid1;
3562:   PetscHashIter tpos;
3563:   PetscInt      gid, lid, i, ec, nz = aij->nz;
3564:   PetscInt     *garray, *jj = aij->j;

3566:   PetscFunctionBegin;
3568:   PetscAssertPointer(mapping, 2);
3569:   /* use a table */
3570:   PetscCall(PetscHMapICreateWithSize(mat->rmap->n, &gid1_lid1));
3571:   ec = 0;
3572:   for (i = 0; i < nz; i++) {
3573:     PetscInt data, gid1 = jj[i] + 1;
3574:     PetscCall(PetscHMapIGetWithDefault(gid1_lid1, gid1, 0, &data));
3575:     if (!data) {
3576:       /* one based table */
3577:       PetscCall(PetscHMapISet(gid1_lid1, gid1, ++ec));
3578:     }
3579:   }
3580:   /* form array of columns we need */
3581:   PetscCall(PetscMalloc1(ec, &garray));
3582:   PetscHashIterBegin(gid1_lid1, tpos);
3583:   while (!PetscHashIterAtEnd(gid1_lid1, tpos)) {
3584:     PetscHashIterGetKey(gid1_lid1, tpos, gid);
3585:     PetscHashIterGetVal(gid1_lid1, tpos, lid);
3586:     PetscHashIterNext(gid1_lid1, tpos);
3587:     gid--;
3588:     lid--;
3589:     garray[lid] = gid;
3590:   }
3591:   PetscCall(PetscSortInt(ec, garray)); /* sort, and rebuild */
3592:   PetscCall(PetscHMapIClear(gid1_lid1));
3593:   for (i = 0; i < ec; i++) PetscCall(PetscHMapISet(gid1_lid1, garray[i] + 1, i + 1));
3594:   /* compact out the extra columns in B */
3595:   for (i = 0; i < nz; i++) {
3596:     PetscInt gid1 = jj[i] + 1;
3597:     PetscCall(PetscHMapIGetWithDefault(gid1_lid1, gid1, 0, &lid));
3598:     lid--;
3599:     jj[i] = lid;
3600:   }
3601:   PetscCall(PetscLayoutDestroy(&mat->cmap));
3602:   PetscCall(PetscHMapIDestroy(&gid1_lid1));
3603:   PetscCall(PetscLayoutCreateFromSizes(PetscObjectComm((PetscObject)mat), ec, ec, 1, &mat->cmap));
3604:   PetscCall(ISLocalToGlobalMappingCreate(PETSC_COMM_SELF, mat->cmap->bs, mat->cmap->n, garray, PETSC_OWN_POINTER, mapping));
3605:   PetscCall(ISLocalToGlobalMappingSetType(*mapping, ISLOCALTOGLOBALMAPPINGHASH));
3606:   PetscFunctionReturn(PETSC_SUCCESS);
3607: }

3609: /*@
3610:   MatSeqAIJSetColumnIndices - Set the column indices for all the rows
3611:   in the matrix.

3613:   Input Parameters:
3614: + mat     - the `MATSEQAIJ` matrix
3615: - indices - the column indices

3617:   Level: advanced

3619:   Notes:
3620:   This can be called if you have precomputed the nonzero structure of the
3621:   matrix and want to provide it to the matrix object to improve the performance
3622:   of the `MatSetValues()` operation.

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

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

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

3631: .seealso: [](ch_matrices), `Mat`, `MATSEQAIJ`
3632: @*/
3633: PetscErrorCode MatSeqAIJSetColumnIndices(Mat mat, PetscInt *indices)
3634: {
3635:   PetscFunctionBegin;
3637:   PetscAssertPointer(indices, 2);
3638:   PetscUseMethod(mat, "MatSeqAIJSetColumnIndices_C", (Mat, PetscInt *), (mat, indices));
3639:   PetscFunctionReturn(PETSC_SUCCESS);
3640: }

3642: static PetscErrorCode MatStoreValues_SeqAIJ(Mat mat)
3643: {
3644:   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
3645:   size_t      nz  = aij->i[mat->rmap->n];

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

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

3653:   /* copy values over */
3654:   PetscCall(PetscArraycpy(aij->saved_values, aij->a, nz));
3655:   PetscFunctionReturn(PETSC_SUCCESS);
3656: }

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

3662:   Logically Collect

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

3667:   Level: advanced

3669:   Example Usage:
3670: .vb
3671:     Using SNES
3672:     Create Jacobian matrix
3673:     Set linear terms into matrix
3674:     Apply boundary conditions to matrix, at this time matrix must have
3675:       final nonzero structure (i.e. setting the nonlinear terms and applying
3676:       boundary conditions again will not change the nonzero structure
3677:     MatSetOption(mat, MAT_NEW_NONZERO_LOCATIONS, PETSC_FALSE);
3678:     MatStoreValues(mat);
3679:     Call SNESSetJacobian() with matrix
3680:     In your Jacobian routine
3681:       MatRetrieveValues(mat);
3682:       Set nonlinear terms in matrix

3684:     Without `SNESSolve()`, i.e. when you handle nonlinear solve yourself:
3685:     // build linear portion of Jacobian
3686:     MatSetOption(mat, MAT_NEW_NONZERO_LOCATIONS, PETSC_FALSE);
3687:     MatStoreValues(mat);
3688:     loop over nonlinear iterations
3689:        MatRetrieveValues(mat);
3690:        // call MatSetValues(mat,...) to set nonliner portion of Jacobian
3691:        // call MatAssemblyBegin/End() on matrix
3692:        Solve linear system with Jacobian
3693:     endloop
3694: .ve

3696:   Notes:
3697:   Matrix must already be assembled before calling this routine
3698:   Must set the matrix option `MatSetOption`(mat,`MAT_NEW_NONZERO_LOCATIONS`,`PETSC_FALSE`); before
3699:   calling this routine.

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

3704: .seealso: [](ch_matrices), `Mat`, `MatRetrieveValues()`
3705: @*/
3706: PetscErrorCode MatStoreValues(Mat mat)
3707: {
3708:   PetscFunctionBegin;
3710:   PetscCheck(mat->assembled, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for unassembled matrix");
3711:   PetscCheck(!mat->factortype, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
3712:   PetscUseMethod(mat, "MatStoreValues_C", (Mat), (mat));
3713:   PetscFunctionReturn(PETSC_SUCCESS);
3714: }

3716: static PetscErrorCode MatRetrieveValues_SeqAIJ(Mat mat)
3717: {
3718:   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
3719:   PetscInt    nz  = aij->i[mat->rmap->n];

3721:   PetscFunctionBegin;
3722:   PetscCheck(aij->nonew, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3723:   PetscCheck(aij->saved_values, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Must call MatStoreValues(A);first");
3724:   /* copy values over */
3725:   PetscCall(PetscArraycpy(aij->a, aij->saved_values, nz));
3726:   PetscFunctionReturn(PETSC_SUCCESS);
3727: }

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

3732:   Logically Collect

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

3737:   Level: advanced

3739: .seealso: [](ch_matrices), `Mat`, `MatStoreValues()`
3740: @*/
3741: PetscErrorCode MatRetrieveValues(Mat mat)
3742: {
3743:   PetscFunctionBegin;
3745:   PetscCheck(mat->assembled, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for unassembled matrix");
3746:   PetscCheck(!mat->factortype, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
3747:   PetscUseMethod(mat, "MatRetrieveValues_C", (Mat), (mat));
3748:   PetscCall(PetscObjectStateIncrease((PetscObject)mat));
3749:   PetscFunctionReturn(PETSC_SUCCESS);
3750: }

3752: /*@
3753:   MatCreateSeqAIJ - Creates a sparse matrix in `MATSEQAIJ` (compressed row) format
3754:   (the default parallel PETSc format).  For good matrix assembly performance
3755:   the user should preallocate the matrix storage by setting the parameter `nz`
3756:   (or the array `nnz`).

3758:   Collective

3760:   Input Parameters:
3761: + comm - MPI communicator, set to `PETSC_COMM_SELF`
3762: . m    - number of rows
3763: . n    - number of columns
3764: . nz   - number of nonzeros per row (same for all rows)
3765: - nnz  - array containing the number of nonzeros in the various rows
3766:          (possibly different for each row) or NULL

3768:   Output Parameter:
3769: . A - the matrix

3771:   Options Database Keys:
3772: + -mat_no_inode          - Do not use inodes
3773: - -mat_inode_limit limit - Sets inode limit (max limit=5)

3775:   Level: intermediate

3777:   Notes:
3778:   It is recommend to use `MatCreateFromOptions()` instead of this routine

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

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

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

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

3796: .seealso: [](ch_matrices), `Mat`, [Sparse Matrix Creation](sec_matsparse), `MatCreate()`, `MatCreateAIJ()`, `MatSetValues()`, `MatSeqAIJSetColumnIndices()`, `MatCreateSeqAIJWithArrays()`
3797: @*/
3798: PetscErrorCode MatCreateSeqAIJ(MPI_Comm comm, PetscInt m, PetscInt n, PetscInt nz, const PetscInt nnz[], Mat *A)
3799: {
3800:   PetscFunctionBegin;
3801:   PetscCall(MatCreate(comm, A));
3802:   PetscCall(MatSetSizes(*A, m, n, m, n));
3803:   PetscCall(MatSetType(*A, MATSEQAIJ));
3804:   PetscCall(MatSeqAIJSetPreallocation_SeqAIJ(*A, nz, nnz));
3805:   PetscFunctionReturn(PETSC_SUCCESS);
3806: }

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

3814:   Collective

3816:   Input Parameters:
3817: + B   - The matrix
3818: . nz  - number of nonzeros per row (same for all rows)
3819: - nnz - array containing the number of nonzeros in the various rows
3820:          (possibly different for each row) or NULL

3822:   Options Database Keys:
3823: + -mat_no_inode          - Do not use inodes
3824: - -mat_inode_limit limit - Sets inode limit (max limit=5)

3826:   Level: intermediate

3828:   Notes:
3829:   If `nnz` is given then `nz` is ignored

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

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

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

3845:   Developer Notes:
3846:   Use nz of `MAT_SKIP_ALLOCATION` to not allocate any space for the matrix
3847:   entries or columns indices

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

3854: .seealso: [](ch_matrices), `Mat`, `MatCreate()`, `MatCreateAIJ()`, `MatSetValues()`, `MatSeqAIJSetColumnIndices()`, `MatCreateSeqAIJWithArrays()`, `MatGetInfo()`,
3855:           `MatSeqAIJSetTotalPreallocation()`
3856: @*/
3857: PetscErrorCode MatSeqAIJSetPreallocation(Mat B, PetscInt nz, const PetscInt nnz[])
3858: {
3859:   PetscFunctionBegin;
3862:   PetscTryMethod(B, "MatSeqAIJSetPreallocation_C", (Mat, PetscInt, const PetscInt[]), (B, nz, nnz));
3863:   PetscFunctionReturn(PETSC_SUCCESS);
3864: }

3866: PetscErrorCode MatSeqAIJSetPreallocation_SeqAIJ(Mat B, PetscInt nz, const PetscInt *nnz)
3867: {
3868:   Mat_SeqAIJ *b              = (Mat_SeqAIJ *)B->data;
3869:   PetscBool   skipallocation = PETSC_FALSE, realalloc = PETSC_FALSE;
3870:   PetscInt    i;

3872:   PetscFunctionBegin;
3873:   if (B->hash_active) {
3874:     B->ops[0] = b->cops;
3875:     PetscCall(PetscHMapIJVDestroy(&b->ht));
3876:     PetscCall(PetscFree(b->dnz));
3877:     B->hash_active = PETSC_FALSE;
3878:   }
3879:   if (nz >= 0 || nnz) realalloc = PETSC_TRUE;
3880:   if (nz == MAT_SKIP_ALLOCATION) {
3881:     skipallocation = PETSC_TRUE;
3882:     nz             = 0;
3883:   }
3884:   PetscCall(PetscLayoutSetUp(B->rmap));
3885:   PetscCall(PetscLayoutSetUp(B->cmap));

3887:   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
3888:   PetscCheck(nz >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "nz cannot be less than 0: value %" PetscInt_FMT, nz);
3889:   if (nnz) {
3890:     for (i = 0; i < B->rmap->n; i++) {
3891:       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]);
3892:       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);
3893:     }
3894:   }

3896:   B->preallocated = PETSC_TRUE;
3897:   if (!skipallocation) {
3898:     if (!b->imax) PetscCall(PetscMalloc1(B->rmap->n, &b->imax));
3899:     if (!b->ilen) {
3900:       /* b->ilen will count nonzeros in each row so far. */
3901:       PetscCall(PetscCalloc1(B->rmap->n, &b->ilen));
3902:     } else {
3903:       PetscCall(PetscMemzero(b->ilen, B->rmap->n * sizeof(PetscInt)));
3904:     }
3905:     if (!b->ipre) PetscCall(PetscMalloc1(B->rmap->n, &b->ipre));
3906:     if (!nnz) {
3907:       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
3908:       else if (nz < 0) nz = 1;
3909:       nz = PetscMin(nz, B->cmap->n);
3910:       for (i = 0; i < B->rmap->n; i++) b->imax[i] = nz;
3911:       PetscCall(PetscIntMultError(nz, B->rmap->n, &nz));
3912:     } else {
3913:       PetscInt64 nz64 = 0;
3914:       for (i = 0; i < B->rmap->n; i++) {
3915:         b->imax[i] = nnz[i];
3916:         nz64 += nnz[i];
3917:       }
3918:       PetscCall(PetscIntCast(nz64, &nz));
3919:     }

3921:     /* allocate the matrix space */
3922:     PetscCall(MatSeqXAIJFreeAIJ(B, &b->a, &b->j, &b->i));
3923:     PetscCall(PetscShmgetAllocateArray(nz, sizeof(PetscInt), (void **)&b->j));
3924:     PetscCall(PetscShmgetAllocateArray(B->rmap->n + 1, sizeof(PetscInt), (void **)&b->i));
3925:     b->free_ij = PETSC_TRUE;
3926:     if (B->structure_only) {
3927:       b->free_a = PETSC_FALSE;
3928:     } else {
3929:       PetscCall(PetscShmgetAllocateArray(nz, sizeof(PetscScalar), (void **)&b->a));
3930:       b->free_a = PETSC_TRUE;
3931:     }
3932:     b->i[0] = 0;
3933:     for (i = 1; i < B->rmap->n + 1; i++) b->i[i] = b->i[i - 1] + b->imax[i - 1];
3934:   } else {
3935:     b->free_a  = PETSC_FALSE;
3936:     b->free_ij = PETSC_FALSE;
3937:   }

3939:   if (b->ipre && nnz != b->ipre && b->imax) {
3940:     /* reserve user-requested sparsity */
3941:     PetscCall(PetscArraycpy(b->ipre, b->imax, B->rmap->n));
3942:   }

3944:   b->nz               = 0;
3945:   b->maxnz            = nz;
3946:   B->info.nz_unneeded = (double)b->maxnz;
3947:   if (realalloc) PetscCall(MatSetOption(B, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_TRUE));
3948:   B->was_assembled = PETSC_FALSE;
3949:   B->assembled     = PETSC_FALSE;
3950:   /* We simply deem preallocation has changed nonzero state. Updating the state
3951:      will give clients (like AIJKokkos) a chance to know something has happened.
3952:   */
3953:   B->nonzerostate++;
3954:   PetscFunctionReturn(PETSC_SUCCESS);
3955: }

3957: PetscErrorCode MatResetPreallocation_SeqAIJ_Private(Mat A, PetscBool *memoryreset)
3958: {
3959:   Mat_SeqAIJ *a;
3960:   PetscInt    i;
3961:   PetscBool   skipreset;

3963:   PetscFunctionBegin;

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

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

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

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

3978:   PetscCall(PetscArraycmp(a->ipre, a->ilen, A->rmap->n, &skipreset));
3979:   if (skipreset) PetscCall(MatZeroEntries(A));
3980:   else {
3981:     PetscCall(PetscArraycpy(a->imax, a->ipre, A->rmap->n));
3982:     PetscCall(PetscArrayzero(a->ilen, A->rmap->n));
3983:     a->i[0] = 0;
3984:     for (i = 1; i < A->rmap->n + 1; i++) a->i[i] = a->i[i - 1] + a->imax[i - 1];
3985:     A->preallocated     = PETSC_TRUE;
3986:     a->nz               = 0;
3987:     a->maxnz            = a->i[A->rmap->n];
3988:     A->info.nz_unneeded = (double)a->maxnz;
3989:     A->was_assembled    = PETSC_FALSE;
3990:     A->assembled        = PETSC_FALSE;
3991:     A->nonzerostate++;
3992:     /* Log that the state of this object has changed; this will help guarantee that preconditioners get re-setup */
3993:     PetscCall(PetscObjectStateIncrease((PetscObject)A));
3994:   }
3995:   if (memoryreset) *memoryreset = (PetscBool)!skipreset;
3996:   PetscFunctionReturn(PETSC_SUCCESS);
3997: }

3999: static PetscErrorCode MatResetPreallocation_SeqAIJ(Mat A)
4000: {
4001:   PetscFunctionBegin;
4002:   PetscCall(MatResetPreallocation_SeqAIJ_Private(A, NULL));
4003:   PetscFunctionReturn(PETSC_SUCCESS);
4004: }

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

4009:   Input Parameters:
4010: + B - the matrix
4011: . i - the indices into `j` for the start of each row (indices start with zero)
4012: . j - the column indices for each row (indices start with zero) these must be sorted for each row
4013: - v - optional values in the matrix, use `NULL` if not provided

4015:   Level: developer

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

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

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

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

4029: .seealso: [](ch_matrices), `Mat`, `MatCreate()`, `MatCreateSeqAIJ()`, `MatSetValues()`, `MatSeqAIJSetPreallocation()`, `MATSEQAIJ`, `MatResetPreallocation()`
4030: @*/
4031: PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B, const PetscInt i[], const PetscInt j[], const PetscScalar v[])
4032: {
4033:   PetscFunctionBegin;
4036:   PetscTryMethod(B, "MatSeqAIJSetPreallocationCSR_C", (Mat, const PetscInt[], const PetscInt[], const PetscScalar[]), (B, i, j, v));
4037:   PetscFunctionReturn(PETSC_SUCCESS);
4038: }

4040: static PetscErrorCode MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B, const PetscInt Ii[], const PetscInt J[], const PetscScalar v[])
4041: {
4042:   PetscInt  i;
4043:   PetscInt  m, n;
4044:   PetscInt  nz;
4045:   PetscInt *nnz;

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

4050:   PetscCall(PetscLayoutSetUp(B->rmap));
4051:   PetscCall(PetscLayoutSetUp(B->cmap));

4053:   PetscCall(MatGetSize(B, &m, &n));
4054:   PetscCall(PetscMalloc1(m + 1, &nnz));
4055:   for (i = 0; i < m; i++) {
4056:     nz = Ii[i + 1] - Ii[i];
4057:     PetscCheck(nz >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Local row %" PetscInt_FMT " has a negative number of columns %" PetscInt_FMT, i, nz);
4058:     nnz[i] = nz;
4059:   }
4060:   PetscCall(MatSeqAIJSetPreallocation(B, 0, nnz));
4061:   PetscCall(PetscFree(nnz));

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

4065:   PetscCall(MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY));
4066:   PetscCall(MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY));

4068:   PetscCall(MatSetOption(B, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
4069:   PetscFunctionReturn(PETSC_SUCCESS);
4070: }

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

4075:   Input Parameters:
4076: + A     - left-hand side matrix
4077: . B     - right-hand side matrix
4078: - reuse - either `MAT_INITIAL_MATRIX` or `MAT_REUSE_MATRIX`

4080:   Output Parameter:
4081: . C - Kronecker product of `A` and `B`

4083:   Level: intermediate

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

4088: .seealso: [](ch_matrices), `Mat`, `MatCreateSeqAIJ()`, `MATSEQAIJ`, `MATKAIJ`, `MatReuse`
4089: @*/
4090: PetscErrorCode MatSeqAIJKron(Mat A, Mat B, MatReuse reuse, Mat *C)
4091: {
4092:   PetscFunctionBegin;
4097:   PetscAssertPointer(C, 4);
4098:   if (reuse == MAT_REUSE_MATRIX) {
4101:   }
4102:   PetscTryMethod(A, "MatSeqAIJKron_C", (Mat, Mat, MatReuse, Mat *), (A, B, reuse, C));
4103:   PetscFunctionReturn(PETSC_SUCCESS);
4104: }

4106: static PetscErrorCode MatSeqAIJKron_SeqAIJ(Mat A, Mat B, MatReuse reuse, Mat *C)
4107: {
4108:   Mat                newmat;
4109:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data;
4110:   Mat_SeqAIJ        *b = (Mat_SeqAIJ *)B->data;
4111:   PetscScalar       *v;
4112:   const PetscScalar *aa, *ba;
4113:   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;
4114:   PetscBool          flg;

4116:   PetscFunctionBegin;
4117:   PetscCheck(!A->factortype, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
4118:   PetscCheck(A->assembled, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for unassembled matrix");
4119:   PetscCheck(!B->factortype, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for factored matrix");
4120:   PetscCheck(B->assembled, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Not for unassembled matrix");
4121:   PetscCall(PetscObjectTypeCompare((PetscObject)B, MATSEQAIJ, &flg));
4122:   PetscCheck(flg, PETSC_COMM_SELF, PETSC_ERR_SUP, "MatType %s", ((PetscObject)B)->type_name);
4123:   PetscCheck(reuse == MAT_INITIAL_MATRIX || reuse == MAT_REUSE_MATRIX, PETSC_COMM_SELF, PETSC_ERR_SUP, "MatReuse %d", (int)reuse);
4124:   if (reuse == MAT_INITIAL_MATRIX) {
4125:     PetscCall(PetscMalloc2(am * bm + 1, &i, a->i[am] * b->i[bm], &j));
4126:     PetscCall(MatCreate(PETSC_COMM_SELF, &newmat));
4127:     PetscCall(MatSetSizes(newmat, am * bm, an * bn, am * bm, an * bn));
4128:     PetscCall(MatSetType(newmat, MATAIJ));
4129:     i[0] = 0;
4130:     for (m = 0; m < am; ++m) {
4131:       for (p = 0; p < bm; ++p) {
4132:         i[m * bm + p + 1] = i[m * bm + p] + (a->i[m + 1] - a->i[m]) * (b->i[p + 1] - b->i[p]);
4133:         for (n = a->i[m]; n < a->i[m + 1]; ++n) {
4134:           for (q = b->i[p]; q < b->i[p + 1]; ++q) j[nnz++] = a->j[n] * bn + b->j[q];
4135:         }
4136:       }
4137:     }
4138:     PetscCall(MatSeqAIJSetPreallocationCSR(newmat, i, j, NULL));
4139:     *C = newmat;
4140:     PetscCall(PetscFree2(i, j));
4141:     nnz = 0;
4142:   }
4143:   PetscCall(MatSeqAIJGetArray(*C, &v));
4144:   PetscCall(MatSeqAIJGetArrayRead(A, &aa));
4145:   PetscCall(MatSeqAIJGetArrayRead(B, &ba));
4146:   for (m = 0; m < am; ++m) {
4147:     for (p = 0; p < bm; ++p) {
4148:       for (n = a->i[m]; n < a->i[m + 1]; ++n) {
4149:         for (q = b->i[p]; q < b->i[p + 1]; ++q) v[nnz++] = aa[n] * ba[q];
4150:       }
4151:     }
4152:   }
4153:   PetscCall(MatSeqAIJRestoreArray(*C, &v));
4154:   PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
4155:   PetscCall(MatSeqAIJRestoreArrayRead(B, &ba));
4156:   PetscFunctionReturn(PETSC_SUCCESS);
4157: }

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

4162: /*
4163:     Computes (B'*A')' since computing B*A directly is untenable

4165:                n                       p                          p
4166:         [             ]       [             ]         [                 ]
4167:       m [      A      ]  *  n [       B     ]   =   m [         C       ]
4168:         [             ]       [             ]         [                 ]

4170: */
4171: PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A, Mat B, Mat C)
4172: {
4173:   Mat_SeqDense      *sub_a = (Mat_SeqDense *)A->data;
4174:   Mat_SeqAIJ        *sub_b = (Mat_SeqAIJ *)B->data;
4175:   Mat_SeqDense      *sub_c = (Mat_SeqDense *)C->data;
4176:   PetscInt           i, j, n, m, q, p;
4177:   const PetscInt    *ii, *idx;
4178:   const PetscScalar *b, *a, *a_q;
4179:   PetscScalar       *c, *c_q;
4180:   PetscInt           clda = sub_c->lda;
4181:   PetscInt           alda = sub_a->lda;

4183:   PetscFunctionBegin;
4184:   m = A->rmap->n;
4185:   n = A->cmap->n;
4186:   p = B->cmap->n;
4187:   a = sub_a->v;
4188:   b = sub_b->a;
4189:   c = sub_c->v;
4190:   if (clda == m) {
4191:     PetscCall(PetscArrayzero(c, m * p));
4192:   } else {
4193:     for (j = 0; j < p; j++)
4194:       for (i = 0; i < m; i++) c[j * clda + i] = 0.0;
4195:   }
4196:   ii  = sub_b->i;
4197:   idx = sub_b->j;
4198:   for (i = 0; i < n; i++) {
4199:     q = ii[i + 1] - ii[i];
4200:     while (q-- > 0) {
4201:       c_q = c + clda * (*idx);
4202:       a_q = a + alda * i;
4203:       PetscKernelAXPY(c_q, *b, a_q, m);
4204:       idx++;
4205:       b++;
4206:     }
4207:   }
4208:   PetscFunctionReturn(PETSC_SUCCESS);
4209: }

4211: PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A, Mat B, PetscReal fill, Mat C)
4212: {
4213:   PetscInt  m = A->rmap->n, n = B->cmap->n;
4214:   PetscBool cisdense;

4216:   PetscFunctionBegin;
4217:   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);
4218:   PetscCall(MatSetSizes(C, m, n, m, n));
4219:   PetscCall(MatSetBlockSizesFromMats(C, A, B));
4220:   PetscCall(PetscObjectTypeCompareAny((PetscObject)C, &cisdense, MATSEQDENSE, MATSEQDENSECUDA, MATSEQDENSEHIP, ""));
4221:   if (!cisdense) PetscCall(MatSetType(C, MATDENSE));
4222:   PetscCall(MatSetUp(C));

4224:   C->ops->matmultnumeric = MatMatMultNumeric_SeqDense_SeqAIJ;
4225:   PetscFunctionReturn(PETSC_SUCCESS);
4226: }

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

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

4235:    Level: beginner

4237:    Notes:
4238:     `MatSetValues()` may be called for this matrix type with a `NULL` argument for the numerical values,
4239:     in this case the values associated with the rows and columns one passes in are set to zero
4240:     in the matrix

4242:     `MatSetOptions`(,`MAT_STRUCTURE_ONLY`,`PETSC_TRUE`) may be called for this matrix type. In this no
4243:     space is allocated for the nonzero entries and any entries passed with `MatSetValues()` are ignored

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

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

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

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

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

4263:   Level: beginner

4265:    Note:
4266:    Subclasses include `MATAIJCUSPARSE`, `MATAIJPERM`, `MATAIJSELL`, `MATAIJMKL`, `MATAIJCRL`, and also automatically switches over to use inodes when
4267:    enough exist.

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

4272: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCRL(Mat, MatType, MatReuse, Mat *);
4273: #if PetscDefined(HAVE_ELEMENTAL)
4274: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_Elemental(Mat, MatType, MatReuse, Mat *);
4275: #endif
4276: #if PetscDefined(HAVE_SCALAPACK) && (PetscDefined(USE_REAL_SINGLE) || PetscDefined(USE_REAL_DOUBLE))
4277: PETSC_INTERN PetscErrorCode MatConvert_AIJ_ScaLAPACK(Mat, MatType, MatReuse, Mat *);
4278: #endif
4279: #if PetscDefined(HAVE_HYPRE)
4280: PETSC_INTERN PetscErrorCode MatConvert_AIJ_HYPRE(Mat A, MatType, MatReuse, Mat *);
4281: #endif

4283: PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_SeqSELL(Mat, MatType, MatReuse, Mat *);
4284: PETSC_INTERN PetscErrorCode MatConvert_XAIJ_IS(Mat, MatType, MatReuse, Mat *);
4285: PETSC_INTERN PetscErrorCode MatProductSetFromOptions_IS_XAIJ(Mat);

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

4290:   Not Collective

4292:   Input Parameter:
4293: . A - a `MATSEQAIJ` matrix

4295:   Output Parameter:
4296: . array - pointer to the data

4298:   Level: intermediate

4300: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJRestoreArray()`
4301: @*/
4302: PetscErrorCode MatSeqAIJGetArray(Mat A, PetscScalar *array[])
4303: {
4304:   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;

4306:   PetscFunctionBegin;
4307:   if (aij->ops->getarray) {
4308:     PetscCall((*aij->ops->getarray)(A, array));
4309:   } else {
4310:     *array = aij->a;
4311:   }
4312:   PetscFunctionReturn(PETSC_SUCCESS);
4313: }

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

4318:   Not Collective

4320:   Input Parameters:
4321: + A     - a `MATSEQAIJ` matrix
4322: - array - pointer to the data

4324:   Level: intermediate

4326: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJGetArray()`
4327: @*/
4328: PetscErrorCode MatSeqAIJRestoreArray(Mat A, PetscScalar *array[])
4329: {
4330:   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;

4332:   PetscFunctionBegin;
4333:   if (aij->ops->restorearray) {
4334:     PetscCall((*aij->ops->restorearray)(A, array));
4335:   } else {
4336:     *array = NULL;
4337:   }
4338:   PetscCall(PetscObjectStateIncrease((PetscObject)A));
4339:   PetscFunctionReturn(PETSC_SUCCESS);
4340: }

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

4345:   Not Collective

4347:   Input Parameter:
4348: . A - a `MATSEQAIJ` matrix

4350:   Output Parameter:
4351: . array - pointer to the data

4353:   Level: intermediate

4355: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJGetArray()`, `MatSeqAIJRestoreArrayRead()`
4356: @*/
4357: PetscErrorCode MatSeqAIJGetArrayRead(Mat A, const PetscScalar *array[])
4358: {
4359:   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;

4361:   PetscFunctionBegin;
4362:   if (aij->ops->getarrayread) {
4363:     PetscCall((*aij->ops->getarrayread)(A, array));
4364:   } else {
4365:     *array = aij->a;
4366:   }
4367:   PetscFunctionReturn(PETSC_SUCCESS);
4368: }

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

4373:   Not Collective

4375:   Input Parameter:
4376: . A - a `MATSEQAIJ` matrix

4378:   Output Parameter:
4379: . array - pointer to the data

4381:   Level: intermediate

4383: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJGetArray()`, `MatSeqAIJGetArrayRead()`
4384: @*/
4385: PetscErrorCode MatSeqAIJRestoreArrayRead(Mat A, const PetscScalar *array[])
4386: {
4387:   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;

4389:   PetscFunctionBegin;
4390:   if (aij->ops->restorearrayread) {
4391:     PetscCall((*aij->ops->restorearrayread)(A, array));
4392:   } else {
4393:     *array = NULL;
4394:   }
4395:   PetscFunctionReturn(PETSC_SUCCESS);
4396: }

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

4401:   Not Collective

4403:   Input Parameter:
4404: . A - a `MATSEQAIJ` matrix

4406:   Output Parameter:
4407: . array - pointer to the data

4409:   Level: intermediate

4411: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJGetArray()`, `MatSeqAIJRestoreArrayWrite()`
4412: @*/
4413: PetscErrorCode MatSeqAIJGetArrayWrite(Mat A, PetscScalar *array[])
4414: {
4415:   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;

4417:   PetscFunctionBegin;
4418:   if (aij->ops->getarraywrite) {
4419:     PetscCall((*aij->ops->getarraywrite)(A, array));
4420:   } else {
4421:     *array = aij->a;
4422:   }
4423:   PetscCall(PetscObjectStateIncrease((PetscObject)A));
4424:   PetscFunctionReturn(PETSC_SUCCESS);
4425: }

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

4430:   Not Collective

4432:   Input Parameter:
4433: . A - a `MATSEQAIJ` matrix

4435:   Output Parameter:
4436: . array - pointer to the data

4438:   Level: intermediate

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

4446:   PetscFunctionBegin;
4447:   if (aij->ops->restorearraywrite) {
4448:     PetscCall((*aij->ops->restorearraywrite)(A, array));
4449:   } else {
4450:     *array = NULL;
4451:   }
4452:   PetscFunctionReturn(PETSC_SUCCESS);
4453: }

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

4458:   Not Collective; No Fortran Support

4460:   Input Parameter:
4461: . mat - a matrix of type `MATSEQAIJ` or its subclasses

4463:   Output Parameters:
4464: + i     - row map array of the matrix
4465: . j     - column index array of the matrix
4466: . a     - data array of the matrix
4467: - mtype - memory type of the arrays

4469:   Level: developer

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

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

4478: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJGetArray()`, `MatSeqAIJGetArrayRead()`
4479: @*/
4480: PetscErrorCode MatSeqAIJGetCSRAndMemType(Mat mat, const PetscInt *i[], const PetscInt *j[], PetscScalar *a[], PetscMemType *mtype)
4481: {
4482:   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;

4484:   PetscFunctionBegin;
4485:   PetscCheck(mat->preallocated, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "matrix is not preallocated");
4486:   if (aij->ops->getcsrandmemtype) {
4487:     PetscCall((*aij->ops->getcsrandmemtype)(mat, i, j, a, mtype));
4488:   } else {
4489:     if (i) *i = aij->i;
4490:     if (j) *j = aij->j;
4491:     if (a) *a = aij->a;
4492:     if (mtype) *mtype = PETSC_MEMTYPE_HOST;
4493:   }
4494:   PetscFunctionReturn(PETSC_SUCCESS);
4495: }

4497: /*@
4498:   MatSeqAIJGetMaxRowNonzeros - returns the maximum number of nonzeros in any row

4500:   Not Collective

4502:   Input Parameter:
4503: . A - a `MATSEQAIJ` matrix

4505:   Output Parameter:
4506: . nz - the maximum number of nonzeros in any row

4508:   Level: intermediate

4510: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJRestoreArray()`
4511: @*/
4512: PetscErrorCode MatSeqAIJGetMaxRowNonzeros(Mat A, PetscInt *nz)
4513: {
4514:   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;

4516:   PetscFunctionBegin;
4517:   *nz = aij->rmax;
4518:   PetscFunctionReturn(PETSC_SUCCESS);
4519: }

4521: static PetscErrorCode MatCOOStructDestroy_SeqAIJ(PetscCtxRt data)
4522: {
4523:   MatCOOStruct_SeqAIJ *coo = *(MatCOOStruct_SeqAIJ **)data;

4525:   PetscFunctionBegin;
4526:   PetscCall(PetscFree(coo->perm));
4527:   PetscCall(PetscFree(coo->jmap));
4528:   PetscCall(PetscFree(coo));
4529:   PetscFunctionReturn(PETSC_SUCCESS);
4530: }

4532: PetscErrorCode MatSetPreallocationCOO_SeqAIJ(Mat mat, PetscCount coo_n, PetscInt coo_i[], PetscInt coo_j[])
4533: {
4534:   MPI_Comm             comm;
4535:   PetscInt            *i, *j;
4536:   PetscInt             M, N, row, iprev;
4537:   PetscCount           k, p, q, nneg, nnz, start, end; /* Index the coo array, so use PetscCount as their type */
4538:   PetscInt            *Ai;                             /* Change to PetscCount once we use it for row pointers */
4539:   PetscInt            *Aj;
4540:   PetscScalar         *Aa;
4541:   Mat_SeqAIJ          *seqaij = (Mat_SeqAIJ *)mat->data;
4542:   MatType              rtype;
4543:   PetscCount          *perm, *jmap;
4544:   MatCOOStruct_SeqAIJ *coo;
4545:   PetscBool            isorted;
4546:   PetscBool            hypre;

4548:   PetscFunctionBegin;
4549:   PetscCall(PetscObjectGetComm((PetscObject)mat, &comm));
4550:   PetscCall(MatGetSize(mat, &M, &N));
4551:   i = coo_i;
4552:   j = coo_j;
4553:   PetscCall(PetscMalloc1(coo_n, &perm));

4555:   /* 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) */
4556:   isorted = PETSC_TRUE;
4557:   iprev   = PETSC_INT_MIN;
4558:   for (k = 0; k < coo_n; k++) {
4559:     if (j[k] < 0) i[k] = -1;
4560:     if (isorted) {
4561:       if (i[k] < iprev) isorted = PETSC_FALSE;
4562:       else iprev = i[k];
4563:     }
4564:     perm[k] = k;
4565:   }

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

4571:   /* Advance k to the first row with a non-negative index */
4572:   for (k = 0; k < coo_n; k++)
4573:     if (i[k] >= 0) break;
4574:   nneg = k;
4575:   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 */
4576:   nnz = 0;                                          /* Total number of unique nonzeros to be counted */
4577:   jmap++;                                           /* Inc jmap by 1 for convenience */

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

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

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

4592:     /* get [start,end) indices for this row; also check if cols in this row are strictly sorted */
4593:     row             = i[k];
4594:     start           = k;
4595:     jprev           = PETSC_INT_MIN;
4596:     strictly_sorted = PETSC_TRUE;
4597:     while (k < coo_n && i[k] == row) {
4598:       if (strictly_sorted) {
4599:         if (j[k] <= jprev) strictly_sorted = PETSC_FALSE;
4600:         else jprev = j[k];
4601:       }
4602:       k++;
4603:     }
4604:     end = k;

4606:     /* hack for HYPRE: swap min column to diag so that diagonal values will go first */
4607:     if (hypre) {
4608:       PetscInt  minj    = PETSC_INT_MAX;
4609:       PetscBool hasdiag = PETSC_FALSE;

4611:       if (strictly_sorted) { // fast path to swap the first and the diag
4612:         PetscCount tmp;
4613:         for (p = start; p < end; p++) {
4614:           if (j[p] == row && p != start) {
4615:             j[p]        = j[start]; // swap j[], so that the diagonal value will go first (manipulated by perm[])
4616:             j[start]    = row;
4617:             tmp         = perm[start];
4618:             perm[start] = perm[p]; // also swap perm[] so we can save the call to PetscSortIntWithCountArray() below
4619:             perm[p]     = tmp;
4620:             break;
4621:           }
4622:         }
4623:       } else {
4624:         for (p = start; p < end; p++) {
4625:           hasdiag = (PetscBool)(hasdiag || (j[p] == row));
4626:           minj    = PetscMin(minj, j[p]);
4627:         }

4629:         if (hasdiag) {
4630:           for (p = start; p < end; p++) {
4631:             if (j[p] == minj) j[p] = row;
4632:             else if (j[p] == row) j[p] = minj;
4633:           }
4634:         }
4635:       }
4636:     }
4637:     // sort by columns in a row. perm[] indicates their original order
4638:     if (!strictly_sorted) PetscCall(PetscSortIntWithCountArray(end - start, j + start, perm + start));
4639:     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);

4641:     if (strictly_sorted) { // fast path to set Aj[], jmap[], Ai[], nnz, q
4642:       for (p = start; p < end; p++, q++) {
4643:         Aj[q]   = j[p];
4644:         jmap[q] = 1;
4645:       }
4646:       PetscCall(PetscIntCast(end - start, Ai + row));
4647:       nnz += Ai[row]; // q is already advanced
4648:     } else {
4649:       /* Find number of unique col entries in this row */
4650:       Aj[q]   = j[start]; /* Log the first nonzero in this row */
4651:       jmap[q] = 1;        /* Number of repeats of this nonzero entry */
4652:       Ai[row] = 1;
4653:       nnz++;

4655:       for (p = start + 1; p < end; p++) { /* Scan remaining nonzero in this row */
4656:         if (j[p] != j[p - 1]) {           /* Meet a new nonzero */
4657:           q++;
4658:           jmap[q] = 1;
4659:           Aj[q]   = j[p];
4660:           Ai[row]++;
4661:           nnz++;
4662:         } else {
4663:           jmap[q]++;
4664:         }
4665:       }
4666:       q++; /* Move to next row and thus next unique nonzero */
4667:     }
4668:   }

4670:   Ai--; /* Back to the beginning of Ai[] */
4671:   for (k = 0; k < M; k++) Ai[k + 1] += Ai[k];
4672:   jmap--; // Back to the beginning of jmap[]
4673:   jmap[0] = 0;
4674:   for (k = 0; k < nnz; k++) jmap[k + 1] += jmap[k];

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

4680:     PetscCall(PetscMalloc1(nnz + 1, &jmap_new));
4681:     PetscCall(PetscArraycpy(jmap_new, jmap, nnz + 1));
4682:     PetscCall(PetscFree(jmap));
4683:     jmap = jmap_new;

4685:     PetscCall(PetscShmgetAllocateArray(nnz, sizeof(PetscInt), (void **)&Aj_new));
4686:     PetscCall(PetscArraycpy(Aj_new, Aj, nnz));
4687:     PetscCall(PetscShmgetDeallocateArray((void **)&Aj));
4688:     Aj = Aj_new;
4689:   }

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

4694:     PetscCall(PetscMalloc1(coo_n - nneg, &perm_new));
4695:     PetscCall(PetscArraycpy(perm_new, perm + nneg, coo_n - nneg));
4696:     PetscCall(PetscFree(perm));
4697:     perm = perm_new;
4698:   }

4700:   PetscCall(MatGetRootType_Private(mat, &rtype));
4701:   PetscCall(PetscShmgetAllocateArray(nnz, sizeof(PetscScalar), (void **)&Aa));
4702:   PetscCall(PetscArrayzero(Aa, nnz));
4703:   PetscCall(MatSetSeqAIJWithArrays_private(PETSC_COMM_SELF, M, N, Ai, Aj, Aa, rtype, mat));

4705:   seqaij->free_a = seqaij->free_ij = PETSC_TRUE; /* Let newmat own Ai, Aj and Aa */

4707:   // Put the COO struct in a container and then attach that to the matrix
4708:   PetscCall(PetscMalloc1(1, &coo));
4709:   PetscCall(PetscIntCast(nnz, &coo->nz));
4710:   coo->n    = coo_n;
4711:   coo->Atot = coo_n - nneg; // Annz is seqaij->nz, so no need to record that again
4712:   coo->jmap = jmap;         // of length nnz+1
4713:   coo->perm = perm;
4714:   PetscCall(PetscObjectContainerCompose((PetscObject)mat, "__PETSc_MatCOOStruct_Host", coo, MatCOOStructDestroy_SeqAIJ));
4715:   PetscFunctionReturn(PETSC_SUCCESS);
4716: }

4718: static PetscErrorCode MatSetValuesCOO_SeqAIJ(Mat A, const PetscScalar v[], InsertMode imode)
4719: {
4720:   Mat_SeqAIJ          *aseq = (Mat_SeqAIJ *)A->data;
4721:   PetscCount           i, j, Annz = aseq->nz;
4722:   PetscCount          *perm, *jmap;
4723:   PetscScalar         *Aa;
4724:   PetscContainer       container;
4725:   MatCOOStruct_SeqAIJ *coo;

4727:   PetscFunctionBegin;
4728:   PetscCall(PetscObjectQuery((PetscObject)A, "__PETSc_MatCOOStruct_Host", (PetscObject *)&container));
4729:   PetscCheck(container, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Not found MatCOOStruct on this matrix");
4730:   PetscCall(PetscContainerGetPointer(container, &coo));
4731:   perm = coo->perm;
4732:   jmap = coo->jmap;
4733:   PetscCall(MatSeqAIJGetArray(A, &Aa));
4734:   for (i = 0; i < Annz; i++) {
4735:     PetscScalar sum = 0.0;
4736:     for (j = jmap[i]; j < jmap[i + 1]; j++) sum += v[perm[j]];
4737:     Aa[i] = (imode == INSERT_VALUES ? 0.0 : Aa[i]) + sum;
4738:   }
4739:   PetscCall(MatSeqAIJRestoreArray(A, &Aa));
4740:   PetscFunctionReturn(PETSC_SUCCESS);
4741: }

4743: #if PetscDefined(HAVE_CUDA)
4744: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCUSPARSE(Mat, MatType, MatReuse, Mat *);
4745: #endif
4746: #if PetscDefined(HAVE_HIP)
4747: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJHIPSPARSE(Mat, MatType, MatReuse, Mat *);
4748: #endif
4749: #if PetscDefined(HAVE_KOKKOS_KERNELS)
4750: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJKokkos(Mat, MatType, MatReuse, Mat *);
4751: #endif

4753: PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJ(Mat B)
4754: {
4755:   Mat_SeqAIJ *b;
4756:   PetscMPIInt size;

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

4762:   PetscCall(PetscNew(&b));

4764:   B->data   = (void *)b;
4765:   B->ops[0] = MatOps_Values;
4766:   if (B->sortedfull) B->ops->setvalues = MatSetValues_SeqAIJ_SortedFull;

4768:   b->row                = NULL;
4769:   b->col                = NULL;
4770:   b->icol               = NULL;
4771:   b->reallocs           = 0;
4772:   b->ignorezeroentries  = PETSC_FALSE;
4773:   b->roworiented        = PETSC_TRUE;
4774:   b->nonew              = 0;
4775:   b->diag               = NULL;
4776:   b->solve_work         = NULL;
4777:   B->spptr              = NULL;
4778:   b->saved_values       = NULL;
4779:   b->idiag              = NULL;
4780:   b->mdiag              = NULL;
4781:   b->ssor_work          = NULL;
4782:   b->omega              = 1.0;
4783:   b->fshift             = 0.0;
4784:   b->ibdiag             = NULL;
4785:   b->keepnonzeropattern = PETSC_FALSE;

4787:   PetscCall(PetscObjectChangeTypeName((PetscObject)B, MATSEQAIJ));
4788: #if PetscDefined(HAVE_MATLAB)
4789:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "PetscMatlabEnginePut_C", MatlabEnginePut_SeqAIJ));
4790:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "PetscMatlabEngineGet_C", MatlabEngineGet_SeqAIJ));
4791: #endif
4792:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSeqAIJSetColumnIndices_C", MatSeqAIJSetColumnIndices_SeqAIJ));
4793:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatStoreValues_C", MatStoreValues_SeqAIJ));
4794:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatRetrieveValues_C", MatRetrieveValues_SeqAIJ));
4795:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqsbaij_C", MatConvert_SeqAIJ_SeqSBAIJ));
4796:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqbaij_C", MatConvert_SeqAIJ_SeqBAIJ));
4797:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqaijperm_C", MatConvert_SeqAIJ_SeqAIJPERM));
4798:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqaijsell_C", MatConvert_SeqAIJ_SeqAIJSELL));
4799: #if PetscDefined(HAVE_MKL_SPARSE)
4800:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqaijmkl_C", MatConvert_SeqAIJ_SeqAIJMKL));
4801: #endif
4802: #if PetscDefined(HAVE_CUDA)
4803:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqaijcusparse_C", MatConvert_SeqAIJ_SeqAIJCUSPARSE));
4804:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqaijcusparse_seqaij_C", MatProductSetFromOptions_SeqAIJ));
4805:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqaij_seqaijcusparse_C", MatProductSetFromOptions_SeqAIJ));
4806: #endif
4807: #if PetscDefined(HAVE_HIP)
4808:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqaijhipsparse_C", MatConvert_SeqAIJ_SeqAIJHIPSPARSE));
4809:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqaijhipsparse_seqaij_C", MatProductSetFromOptions_SeqAIJ));
4810:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqaij_seqaijhipsparse_C", MatProductSetFromOptions_SeqAIJ));
4811: #endif
4812: #if PetscDefined(HAVE_KOKKOS_KERNELS)
4813:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqaijkokkos_C", MatConvert_SeqAIJ_SeqAIJKokkos));
4814: #endif
4815:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqaijcrl_C", MatConvert_SeqAIJ_SeqAIJCRL));
4816: #if PetscDefined(HAVE_ELEMENTAL)
4817:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_elemental_C", MatConvert_SeqAIJ_Elemental));
4818: #endif
4819: #if PetscDefined(HAVE_SCALAPACK) && (PetscDefined(USE_REAL_SINGLE) || PetscDefined(USE_REAL_DOUBLE))
4820:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_scalapack_C", MatConvert_AIJ_ScaLAPACK));
4821: #endif
4822: #if PetscDefined(HAVE_HYPRE)
4823:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_hypre_C", MatConvert_AIJ_HYPRE));
4824:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_transpose_seqaij_seqaij_C", MatProductSetFromOptions_Transpose_AIJ_AIJ));
4825: #endif
4826:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqdense_C", MatConvert_SeqAIJ_SeqDense));
4827:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqsell_C", MatConvert_SeqAIJ_SeqSELL));
4828:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_is_C", MatConvert_XAIJ_IS));
4829:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatIsTranspose_C", MatIsTranspose_SeqAIJ));
4830:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatIsHermitianTranspose_C", MatIsHermitianTranspose_SeqAIJ));
4831:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSeqAIJSetPreallocation_C", MatSeqAIJSetPreallocation_SeqAIJ));
4832:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatResetPreallocation_C", MatResetPreallocation_SeqAIJ));
4833:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatResetHash_C", MatResetHash_SeqAIJ));
4834:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSeqAIJSetPreallocationCSR_C", MatSeqAIJSetPreallocationCSR_SeqAIJ));
4835:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatReorderForNonzeroDiagonal_C", MatReorderForNonzeroDiagonal_SeqAIJ));
4836:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_is_seqaij_C", MatProductSetFromOptions_IS_XAIJ));
4837:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqdense_seqaij_C", MatProductSetFromOptions_SeqDense_SeqAIJ));
4838:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqaij_seqaij_C", MatProductSetFromOptions_SeqAIJ));
4839:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSeqAIJKron_C", MatSeqAIJKron_SeqAIJ));
4840:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSetPreallocationCOO_C", MatSetPreallocationCOO_SeqAIJ));
4841:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSetValuesCOO_C", MatSetValuesCOO_SeqAIJ));
4842:   PetscCall(MatCreate_SeqAIJ_Inode(B));
4843:   PetscCall(PetscObjectChangeTypeName((PetscObject)B, MATSEQAIJ));
4844:   PetscCall(MatSeqAIJSetTypeFromOptions(B)); /* this allows changing the matrix subtype to say MATSEQAIJPERM */
4845:   PetscFunctionReturn(PETSC_SUCCESS);
4846: }

4848: /*
4849:     Given a matrix generated with MatGetFactor() duplicates all the information in A into C
4850: */
4851: PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C, Mat A, MatDuplicateOption cpvalues, PetscBool mallocmatspace)
4852: {
4853:   Mat_SeqAIJ *c = (Mat_SeqAIJ *)C->data, *a = (Mat_SeqAIJ *)A->data;
4854:   PetscInt    m = A->rmap->n, i;

4856:   PetscFunctionBegin;
4857:   PetscCheck(A->assembled || cpvalues == MAT_DO_NOT_COPY_VALUES, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Cannot duplicate unassembled matrix");

4859:   C->factortype = A->factortype;
4860:   c->row        = NULL;
4861:   c->col        = NULL;
4862:   c->icol       = NULL;
4863:   c->reallocs   = 0;
4864:   C->assembled  = A->assembled;

4866:   if (A->preallocated) {
4867:     PetscCall(PetscLayoutReference(A->rmap, &C->rmap));
4868:     PetscCall(PetscLayoutReference(A->cmap, &C->cmap));

4870:     if (!A->hash_active) {
4871:       PetscCall(PetscMalloc1(m, &c->imax));
4872:       PetscCall(PetscArraycpy(c->imax, a->imax, m));
4873:       PetscCall(PetscMalloc1(m, &c->ilen));
4874:       PetscCall(PetscArraycpy(c->ilen, a->ilen, m));

4876:       /* allocate the matrix space */
4877:       if (mallocmatspace) {
4878:         PetscCall(PetscShmgetAllocateArray(a->i[m], sizeof(PetscScalar), (void **)&c->a));
4879:         PetscCall(PetscShmgetAllocateArray(a->i[m], sizeof(PetscInt), (void **)&c->j));
4880:         PetscCall(PetscShmgetAllocateArray(m + 1, sizeof(PetscInt), (void **)&c->i));
4881:         PetscCall(PetscArraycpy(c->i, a->i, m + 1));
4882:         c->free_a  = PETSC_TRUE;
4883:         c->free_ij = PETSC_TRUE;
4884:         if (m > 0) {
4885:           PetscCall(PetscArraycpy(c->j, a->j, a->i[m]));
4886:           if (cpvalues == MAT_COPY_VALUES) {
4887:             const PetscScalar *aa;

4889:             PetscCall(MatSeqAIJGetArrayRead(A, &aa));
4890:             PetscCall(PetscArraycpy(c->a, aa, a->i[m]));
4891:             PetscCall(MatSeqAIJGetArrayRead(A, &aa));
4892:           } else {
4893:             PetscCall(PetscArrayzero(c->a, a->i[m]));
4894:           }
4895:         }
4896:       }
4897:       C->preallocated = PETSC_TRUE;
4898:     } else {
4899:       PetscCheck(mallocmatspace, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONGSTATE, "Cannot malloc matrix memory from a non-preallocated matrix");
4900:       PetscCall(MatSetUp(C));
4901:     }

4903:     c->ignorezeroentries  = a->ignorezeroentries;
4904:     c->roworiented        = a->roworiented;
4905:     c->nonew              = a->nonew;
4906:     c->solve_work         = NULL;
4907:     c->saved_values       = NULL;
4908:     c->idiag              = NULL;
4909:     c->ssor_work          = NULL;
4910:     c->keepnonzeropattern = a->keepnonzeropattern;

4912:     c->rmax  = a->rmax;
4913:     c->nz    = a->nz;
4914:     c->maxnz = a->nz; /* Since we allocate exactly the right amount */

4916:     c->compressedrow.use   = a->compressedrow.use;
4917:     c->compressedrow.nrows = a->compressedrow.nrows;
4918:     if (a->compressedrow.use) {
4919:       i = a->compressedrow.nrows;
4920:       PetscCall(PetscMalloc2(i + 1, &c->compressedrow.i, i, &c->compressedrow.rindex));
4921:       PetscCall(PetscArraycpy(c->compressedrow.i, a->compressedrow.i, i + 1));
4922:       PetscCall(PetscArraycpy(c->compressedrow.rindex, a->compressedrow.rindex, i));
4923:     } else {
4924:       c->compressedrow.use    = PETSC_FALSE;
4925:       c->compressedrow.i      = NULL;
4926:       c->compressedrow.rindex = NULL;
4927:     }
4928:     c->nonzerorowcnt = a->nonzerorowcnt;
4929:     C->nonzerostate  = A->nonzerostate;

4931:     PetscCall(MatDuplicate_SeqAIJ_Inode(A, cpvalues, &C));
4932:   }
4933:   PetscCall(PetscFunctionListDuplicate(((PetscObject)A)->qlist, &((PetscObject)C)->qlist));
4934:   PetscFunctionReturn(PETSC_SUCCESS);
4935: }

4937: PetscErrorCode MatDuplicate_SeqAIJ(Mat A, MatDuplicateOption cpvalues, Mat *B)
4938: {
4939:   PetscFunctionBegin;
4940:   PetscCall(MatCreate(PetscObjectComm((PetscObject)A), B));
4941:   PetscCall(MatSetSizes(*B, A->rmap->n, A->cmap->n, A->rmap->n, A->cmap->n));
4942:   if (!(A->rmap->n % A->rmap->bs) && !(A->cmap->n % A->cmap->bs)) PetscCall(MatSetBlockSizesFromMats(*B, A, A));
4943:   PetscCall(MatSetType(*B, ((PetscObject)A)->type_name));
4944:   PetscCall(MatDuplicateNoCreate_SeqAIJ(*B, A, cpvalues, PETSC_TRUE));
4945:   PetscFunctionReturn(PETSC_SUCCESS);
4946: }

4948: PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer)
4949: {
4950:   PetscBool isbinary, ishdf5;

4952:   PetscFunctionBegin;
4955:   /* force binary viewer to load .info file if it has not yet done so */
4956:   PetscCall(PetscViewerSetUp(viewer));
4957:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
4958:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5));
4959:   if (isbinary) {
4960:     PetscCall(MatLoad_SeqAIJ_Binary(newMat, viewer));
4961:   } else if (ishdf5) {
4962: #if PetscDefined(HAVE_HDF5)
4963:     PetscCall(MatLoad_AIJ_HDF5(newMat, viewer));
4964: #else
4965:     SETERRQ(PetscObjectComm((PetscObject)newMat), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
4966: #endif
4967:   } else {
4968:     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);
4969:   }
4970:   PetscFunctionReturn(PETSC_SUCCESS);
4971: }

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

4978:   PetscFunctionBegin;
4979:   PetscCall(PetscViewerSetUp(viewer));

4981:   /* read in matrix header */
4982:   PetscCall(PetscViewerBinaryRead(viewer, header, 4, NULL, PETSC_INT));
4983:   PetscCheck(header[0] == MAT_FILE_CLASSID, PETSC_COMM_SELF, PETSC_ERR_FILE_UNEXPECTED, "Not a matrix object in file");
4984:   M  = header[1];
4985:   N  = header[2];
4986:   nz = header[3];
4987:   PetscCheck(M >= 0, PetscObjectComm((PetscObject)viewer), PETSC_ERR_FILE_UNEXPECTED, "Matrix row size (%" PetscInt_FMT ") in file is negative", M);
4988:   PetscCheck(N >= 0, PetscObjectComm((PetscObject)viewer), PETSC_ERR_FILE_UNEXPECTED, "Matrix column size (%" PetscInt_FMT ") in file is negative", N);
4989:   PetscCheck(nz >= 0, PETSC_COMM_SELF, PETSC_ERR_FILE_UNEXPECTED, "Matrix stored in special format on disk, cannot load as SeqAIJ");

4991:   /* set block sizes from the viewer's .info file */
4992:   PetscCall(MatLoad_Binary_BlockSizes(mat, viewer));
4993:   /* set local and global sizes if not set already */
4994:   if (mat->rmap->n < 0) mat->rmap->n = M;
4995:   if (mat->cmap->n < 0) mat->cmap->n = N;
4996:   if (mat->rmap->N < 0) mat->rmap->N = M;
4997:   if (mat->cmap->N < 0) mat->cmap->N = N;
4998:   PetscCall(PetscLayoutSetUp(mat->rmap));
4999:   PetscCall(PetscLayoutSetUp(mat->cmap));

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

5005:   /* read in row lengths */
5006:   PetscCall(PetscMalloc1(M, &rowlens));
5007:   PetscCall(PetscViewerBinaryRead(viewer, rowlens, M, NULL, PETSC_INT));
5008:   /* check if sum(rowlens) is same as nz */
5009:   sum = 0;
5010:   for (i = 0; i < M; i++) sum += rowlens[i];
5011:   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);
5012:   /* preallocate and check sizes */
5013:   PetscCall(MatSeqAIJSetPreallocation_SeqAIJ(mat, 0, rowlens));
5014:   PetscCall(MatGetSize(mat, &rows, &cols));
5015:   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);
5016:   /* store row lengths */
5017:   PetscCall(PetscArraycpy(a->ilen, rowlens, M));
5018:   PetscCall(PetscFree(rowlens));

5020:   /* fill in "i" row pointers */
5021:   a->i[0] = 0;
5022:   for (i = 0; i < M; i++) a->i[i + 1] = a->i[i] + a->ilen[i];
5023:   /* read in "j" column indices */
5024:   PetscCall(PetscViewerBinaryRead(viewer, a->j, nz, NULL, PETSC_INT));
5025:   /* read in "a" nonzero values */
5026:   PetscCall(PetscViewerBinaryRead(viewer, a->a, nz, NULL, PETSC_SCALAR));

5028:   PetscCall(MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY));
5029:   PetscCall(MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY));
5030:   PetscFunctionReturn(PETSC_SUCCESS);
5031: }

5033: PetscErrorCode MatEqual_SeqAIJ(Mat A, Mat B, PetscBool *flg)
5034: {
5035:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data, *b = (Mat_SeqAIJ *)B->data;
5036:   const PetscScalar *aa, *ba;

5038:   PetscFunctionBegin;
5039:   /* If the  matrix dimensions are not equal,or no of nonzeros */
5040:   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) || (a->nz != b->nz)) {
5041:     *flg = PETSC_FALSE;
5042:     PetscFunctionReturn(PETSC_SUCCESS);
5043:   }

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

5049:   /* if a->j are the same */
5050:   PetscCall(PetscArraycmp(a->j, b->j, a->nz, flg));
5051:   if (!*flg) PetscFunctionReturn(PETSC_SUCCESS);

5053:   PetscCall(MatSeqAIJGetArrayRead(A, &aa));
5054:   PetscCall(MatSeqAIJGetArrayRead(B, &ba));
5055:   /* if a->a are the same */
5056:   PetscCall(PetscArraycmp(aa, ba, a->nz, flg));
5057:   PetscCall(MatSeqAIJRestoreArrayRead(A, &aa));
5058:   PetscCall(MatSeqAIJRestoreArrayRead(B, &ba));
5059:   PetscFunctionReturn(PETSC_SUCCESS);
5060: }

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

5066:   Collective

5068:   Input Parameters:
5069: + comm - must be an MPI communicator of size 1
5070: . m    - number of rows
5071: . n    - number of columns
5072: . i    - row indices; that is i[0] = 0, i[row] = i[row-1] + number of elements in that row of the matrix
5073: . j    - column indices
5074: - a    - matrix values

5076:   Output Parameter:
5077: . mat - the matrix

5079:   Level: intermediate

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

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

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

5089:   The format which is used for the sparse matrix input, is equivalent to a
5090:   row-major ordering.. i.e for the following matrix, the input data expected is
5091:   as shown
5092: .vb
5093:         1 0 0
5094:         2 0 3
5095:         4 5 6

5097:         i =  {0,1,3,6}  [size = nrow+1  = 3+1]
5098:         j =  {0,0,2,0,1,2}  [size = 6]; values must be sorted for each row
5099:         v =  {1,2,3,4,5,6}  [size = 6]
5100: .ve

5102: .seealso: [](ch_matrices), `Mat`, `MatCreate()`, `MatCreateAIJ()`, `MatCreateSeqAIJ()`, `MatCreateMPIAIJWithArrays()`, `MatMPIAIJSetPreallocationCSR()`
5103: @*/
5104: PetscErrorCode MatCreateSeqAIJWithArrays(MPI_Comm comm, PetscInt m, PetscInt n, PetscInt i[], PetscInt j[], PetscScalar a[], Mat *mat)
5105: {
5106:   PetscInt    ii;
5107:   Mat_SeqAIJ *aij;

5109:   PetscFunctionBegin;
5110:   PetscCheck(m <= 0 || i[0] == 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "i (row indices) must start with 0");
5111:   PetscCall(MatCreate(comm, mat));
5112:   PetscCall(MatSetSizes(*mat, m, n, m, n));
5113:   /* PetscCall(MatSetBlockSizes(*mat,,)); */
5114:   PetscCall(MatSetType(*mat, MATSEQAIJ));
5115:   PetscCall(MatSeqAIJSetPreallocation_SeqAIJ(*mat, MAT_SKIP_ALLOCATION, NULL));
5116:   aij = (Mat_SeqAIJ *)(*mat)->data;
5117:   PetscCall(PetscMalloc1(m, &aij->imax));
5118:   PetscCall(PetscMalloc1(m, &aij->ilen));

5120:   aij->i       = i;
5121:   aij->j       = j;
5122:   aij->a       = a;
5123:   aij->nonew   = -1; /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
5124:   aij->free_a  = PETSC_FALSE;
5125:   aij->free_ij = PETSC_FALSE;

5127:   for (ii = 0, aij->nonzerorowcnt = 0, aij->rmax = 0; ii < m; ii++) {
5128:     aij->ilen[ii] = aij->imax[ii] = i[ii + 1] - i[ii];
5129:     if (PetscDefined(USE_DEBUG)) {
5130:       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]);
5131:       for (PetscInt jj = i[ii] + 1; jj < i[ii + 1]; jj++) {
5132:         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);
5133:         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);
5134:       }
5135:     }
5136:   }
5137:   if (PetscDefined(USE_DEBUG)) {
5138:     for (ii = 0; ii < aij->i[m]; ii++) {
5139:       PetscCheck(j[ii] >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Negative column index at location = %" PetscInt_FMT " index = %" PetscInt_FMT, ii, j[ii]);
5140:       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);
5141:     }
5142:   }

5144:   PetscCall(MatAssemblyBegin(*mat, MAT_FINAL_ASSEMBLY));
5145:   PetscCall(MatAssemblyEnd(*mat, MAT_FINAL_ASSEMBLY));
5146:   PetscFunctionReturn(PETSC_SUCCESS);
5147: }

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

5153:   Collective

5155:   Input Parameters:
5156: + comm - must be an MPI communicator of size 1
5157: . m    - number of rows
5158: . n    - number of columns
5159: . i    - row indices
5160: . j    - column indices
5161: . a    - matrix values
5162: . nz   - number of nonzeros
5163: - idx  - if the `i` and `j` indices start with 1 use `PETSC_TRUE` otherwise use `PETSC_FALSE`

5165:   Output Parameter:
5166: . mat - the matrix

5168:   Level: intermediate

5170:   Example:
5171:   For the following matrix, the input data expected is as shown (using 0 based indexing)
5172: .vb
5173:         1 0 0
5174:         2 0 3
5175:         4 5 6

5177:         i =  {0,1,1,2,2,2}
5178:         j =  {0,0,2,0,1,2}
5179:         v =  {1,2,3,4,5,6}
5180: .ve

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

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

5192:   PetscFunctionBegin;
5193:   PetscCall(PetscCalloc1(m, &nnz));
5194:   for (ii = 0; ii < nz; ii++) nnz[i[ii] - !!idx] += 1;
5195:   PetscCall(MatCreate(comm, mat));
5196:   PetscCall(MatSetSizes(*mat, m, n, m, n));
5197:   PetscCall(MatSetType(*mat, MATSEQAIJ));
5198:   PetscCall(MatSeqAIJSetPreallocation_SeqAIJ(*mat, 0, nnz));
5199:   for (ii = 0; ii < nz; ii++) {
5200:     if (idx) {
5201:       row = i[ii] - 1;
5202:       col = j[ii] - 1;
5203:     } else {
5204:       row = i[ii];
5205:       col = j[ii];
5206:     }
5207:     PetscCall(MatSetValues(*mat, one, &row, one, &col, &a[ii], ADD_VALUES));
5208:   }
5209:   PetscCall(MatAssemblyBegin(*mat, MAT_FINAL_ASSEMBLY));
5210:   PetscCall(MatAssemblyEnd(*mat, MAT_FINAL_ASSEMBLY));
5211:   PetscCall(PetscFree(nnz));
5212:   PetscFunctionReturn(PETSC_SUCCESS);
5213: }

5215: PetscErrorCode MatCreateMPIMatConcatenateSeqMat_SeqAIJ(MPI_Comm comm, Mat inmat, PetscInt n, MatReuse scall, Mat *outmat)
5216: {
5217:   PetscFunctionBegin;
5218:   PetscCall(MatCreateMPIMatConcatenateSeqMat_MPIAIJ(comm, inmat, n, scall, outmat));
5219:   PetscFunctionReturn(PETSC_SUCCESS);
5220: }

5222: /*
5223:  Permute A into C's *local* index space using rowemb,colemb.
5224:  The embedding are supposed to be injections and the above implies that the range of rowemb is a subset
5225:  of [0,m), colemb is in [0,n).
5226:  If pattern == DIFFERENT_NONZERO_PATTERN, C is preallocated according to A.
5227:  */
5228: PetscErrorCode MatSetSeqMat_SeqAIJ(Mat C, IS rowemb, IS colemb, MatStructure pattern, Mat B)
5229: {
5230:   /* If making this function public, change the error returned in this function away from _PLIB. */
5231:   Mat_SeqAIJ     *Baij;
5232:   PetscBool       seqaij;
5233:   PetscInt        m, n, *nz, i, j, count;
5234:   PetscScalar     v;
5235:   const PetscInt *rowindices, *colindices;

5237:   PetscFunctionBegin;
5238:   if (!B) PetscFunctionReturn(PETSC_SUCCESS);
5239:   /* Check to make sure the target matrix (and embeddings) are compatible with C and each other. */
5240:   PetscCall(PetscObjectBaseTypeCompare((PetscObject)B, MATSEQAIJ, &seqaij));
5241:   PetscCheck(seqaij, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Input matrix is of wrong type");
5242:   if (rowemb) {
5243:     PetscCall(ISGetLocalSize(rowemb, &m));
5244:     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);
5245:   } else PetscCheck(C->rmap->n == B->rmap->n, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Input matrix is row-incompatible with the target matrix");
5246:   if (colemb) {
5247:     PetscCall(ISGetLocalSize(colemb, &n));
5248:     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);
5249:   } else PetscCheck(C->cmap->n == B->cmap->n, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Input matrix is col-incompatible with the target matrix");

5251:   Baij       = (Mat_SeqAIJ *)B->data;
5252:   rowindices = NULL;
5253:   if (rowemb) PetscCall(ISGetIndices(rowemb, &rowindices));
5254:   if (pattern == DIFFERENT_NONZERO_PATTERN) {
5255:     PetscCall(PetscMalloc1(C->rmap->n, &nz));
5256:     if (rowemb) {
5257:       PetscCall(PetscArrayzero(nz, C->rmap->n));
5258:       for (i = 0; i < B->rmap->n; i++) nz[rowindices[i]] = Baij->i[i + 1] - Baij->i[i];
5259:     } else {
5260:       for (i = 0; i < B->rmap->n; i++) nz[i] = Baij->i[i + 1] - Baij->i[i];
5261:     }
5262:     PetscCall(MatSeqAIJSetPreallocation(C, 0, nz));
5263:     PetscCall(PetscFree(nz));
5264:   }
5265:   if (pattern == SUBSET_NONZERO_PATTERN) PetscCall(MatZeroEntries(C));
5266:   count      = 0;
5267:   colindices = NULL;
5268:   if (colemb) PetscCall(ISGetIndices(colemb, &colindices));
5269:   for (i = 0; i < B->rmap->n; i++) {
5270:     PetscInt row;
5271:     row = i;
5272:     if (rowindices) row = rowindices[i];
5273:     for (j = Baij->i[i]; j < Baij->i[i + 1]; j++) {
5274:       PetscInt col;
5275:       col = Baij->j[count];
5276:       if (colindices) col = colindices[col];
5277:       v = Baij->a[count];
5278:       PetscCall(MatSetValues(C, 1, &row, 1, &col, &v, INSERT_VALUES));
5279:       ++count;
5280:     }
5281:   }
5282:   if (colemb) PetscCall(ISRestoreIndices(colemb, &colindices));
5283:   if (rowemb) PetscCall(ISRestoreIndices(rowemb, &rowindices));
5284:   /* FIXME: set C's nonzerostate correctly. */
5285:   /* Assembly for C is necessary. */
5286:   C->preallocated  = PETSC_TRUE;
5287:   C->assembled     = PETSC_TRUE;
5288:   C->was_assembled = PETSC_FALSE;
5289:   PetscFunctionReturn(PETSC_SUCCESS);
5290: }

5292: PetscErrorCode MatEliminateZeros_SeqAIJ(Mat A, PetscBool keep)
5293: {
5294:   Mat_SeqAIJ *a  = (Mat_SeqAIJ *)A->data;
5295:   MatScalar  *aa = a->a;
5296:   PetscInt    m = A->rmap->n, fshift = 0, fshift_prev = 0, i, k;
5297:   PetscInt   *ailen = a->ilen, *imax = a->imax, *ai = a->i, *aj = a->j, rmax = 0;

5299:   PetscFunctionBegin;
5300:   PetscCheck(A->assembled, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Cannot eliminate zeros for unassembled matrix");
5301:   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
5302:   for (i = 1, a->nonzerorowcnt = 0; i <= m; i++) {
5303:     /* move each nonzero entry back by the amount of zero slots (fshift) before it*/
5304:     for (k = ai[i - 1]; k < ai[i]; k++) {
5305:       if (aa[k] == 0 && (aj[k] != i - 1 || !keep)) fshift++;
5306:       else {
5307:         if (aa[k] == 0 && aj[k] == i - 1) PetscCall(PetscInfo(A, "Keep the diagonal zero at row %" PetscInt_FMT "\n", i - 1));
5308:         aa[k - fshift] = aa[k];
5309:         aj[k - fshift] = aj[k];
5310:       }
5311:     }
5312:     ai[i - 1] -= fshift_prev; // safe to update ai[i-1] now since it will not be used in the next iteration
5313:     fshift_prev = fshift;
5314:     /* reset ilen and imax for each row */
5315:     ailen[i - 1] = imax[i - 1] = ai[i] - fshift - ai[i - 1];
5316:     a->nonzerorowcnt += ((ai[i] - fshift - ai[i - 1]) > 0);
5317:     rmax = PetscMax(rmax, ailen[i - 1]);
5318:   }
5319:   if (fshift) {
5320:     if (m) {
5321:       ai[m] -= fshift;
5322:       a->nz = ai[m];
5323:     }
5324:     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));
5325:     A->nonzerostate++;
5326:     A->info.nz_unneeded += (PetscReal)fshift;
5327:     a->rmax = rmax;
5328:     if (a->inode.use && a->inode.checked) PetscCall(MatSeqAIJCheckInode(A));
5329:     PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
5330:     PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
5331:   }
5332:   PetscFunctionReturn(PETSC_SUCCESS);
5333: }

5335: PetscFunctionList MatSeqAIJList = NULL;

5337: /*@
5338:   MatSeqAIJSetType - Converts a `MATSEQAIJ` matrix to a subtype

5340:   Collective

5342:   Input Parameters:
5343: + mat    - the matrix object
5344: - matype - matrix type

5346:   Options Database Key:
5347: . -mat_seqaij_type  method - for example seqaijcrl

5349:   Level: intermediate

5351: .seealso: [](ch_matrices), `Mat`, `PCSetType()`, `VecSetType()`, `MatCreate()`, `MatType`
5352: @*/
5353: PetscErrorCode MatSeqAIJSetType(Mat mat, MatType matype)
5354: {
5355:   PetscBool sametype;
5356:   PetscErrorCode (*r)(Mat, MatType, MatReuse, Mat *);

5358:   PetscFunctionBegin;
5360:   PetscCall(PetscObjectTypeCompare((PetscObject)mat, matype, &sametype));
5361:   if (sametype) PetscFunctionReturn(PETSC_SUCCESS);

5363:   PetscCall(PetscFunctionListFind(MatSeqAIJList, matype, &r));
5364:   PetscCheck(r, PetscObjectComm((PetscObject)mat), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown Mat type given: %s", matype);
5365:   PetscCall((*r)(mat, matype, MAT_INPLACE_MATRIX, &mat));
5366:   PetscFunctionReturn(PETSC_SUCCESS);
5367: }

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

5372:   Not Collective, No Fortran Support

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

5378:   Level: advanced

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

5383:   Then, your matrix can be chosen with the procedural interface at runtime via the option
5384: .vb
5385:   -mat_seqaij_type my_mat
5386: .ve

5388: .seealso: [](ch_matrices), `Mat`, `MatSeqAIJRegisterAll()`
5389: @*/
5390: PetscErrorCode MatSeqAIJRegister(const char sname[], PetscErrorCode (*function)(Mat, MatType, MatReuse, Mat *))
5391: {
5392:   PetscFunctionBegin;
5393:   PetscCall(MatInitializePackage());
5394:   PetscCall(PetscFunctionListAdd(&MatSeqAIJList, sname, function));
5395:   PetscFunctionReturn(PETSC_SUCCESS);
5396: }

5398: PetscBool MatSeqAIJRegisterAllCalled = PETSC_FALSE;

5400: /*@
5401:   MatSeqAIJRegisterAll - Registers all of the matrix subtypes of `MATSSEQAIJ`

5403:   Not Collective

5405:   Level: advanced

5407:   Note:
5408:   This registers the versions of `MATSEQAIJ` for GPUs

5410: .seealso: [](ch_matrices), `Mat`, `MatRegisterAll()`, `MatSeqAIJRegister()`
5411: @*/
5412: PetscErrorCode MatSeqAIJRegisterAll(void)
5413: {
5414:   PetscFunctionBegin;
5415:   if (MatSeqAIJRegisterAllCalled) PetscFunctionReturn(PETSC_SUCCESS);
5416:   MatSeqAIJRegisterAllCalled = PETSC_TRUE;

5418:   PetscCall(MatSeqAIJRegister(MATSEQAIJCRL, MatConvert_SeqAIJ_SeqAIJCRL));
5419:   PetscCall(MatSeqAIJRegister(MATSEQAIJPERM, MatConvert_SeqAIJ_SeqAIJPERM));
5420:   PetscCall(MatSeqAIJRegister(MATSEQAIJSELL, MatConvert_SeqAIJ_SeqAIJSELL));
5421: #if PetscDefined(HAVE_MKL_SPARSE)
5422:   PetscCall(MatSeqAIJRegister(MATSEQAIJMKL, MatConvert_SeqAIJ_SeqAIJMKL));
5423: #endif
5424: #if PetscDefined(HAVE_CUDA)
5425:   PetscCall(MatSeqAIJRegister(MATSEQAIJCUSPARSE, MatConvert_SeqAIJ_SeqAIJCUSPARSE));
5426: #endif
5427: #if PetscDefined(HAVE_HIP)
5428:   PetscCall(MatSeqAIJRegister(MATSEQAIJHIPSPARSE, MatConvert_SeqAIJ_SeqAIJHIPSPARSE));
5429: #endif
5430: #if PetscDefined(HAVE_KOKKOS_KERNELS)
5431:   PetscCall(MatSeqAIJRegister(MATSEQAIJKOKKOS, MatConvert_SeqAIJ_SeqAIJKokkos));
5432: #endif
5433: #if PetscDefined(HAVE_VIENNACL) && PetscDefined(HAVE_VIENNACL_NO_CUDA)
5434:   PetscCall(MatSeqAIJRegister(MATMPIAIJVIENNACL, MatConvert_SeqAIJ_SeqAIJViennaCL));
5435: #endif
5436:   PetscFunctionReturn(PETSC_SUCCESS);
5437: }

5439: /*
5440:     Special version for direct calls from Fortran
5441: */
5442: #if PetscDefined(HAVE_FORTRAN_CAPS)
5443:   #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
5444: #elif !PetscDefined(HAVE_FORTRAN_UNDERSCORE)
5445:   #define matsetvaluesseqaij_ matsetvaluesseqaij
5446: #endif

5448: /* Change these macros so can be used in void function */

5450: /* Change these macros so can be used in void function */
5451: /* Identical to PetscCallVoid, except it assigns to *_ierr */
5452: #undef PetscCall
5453: #define PetscCall(...) \
5454:   do { \
5455:     PetscErrorCode ierr_msv_mpiaij = __VA_ARGS__; \
5456:     if (PetscUnlikely(ierr_msv_mpiaij)) { \
5457:       *_ierr = PetscError(PETSC_COMM_SELF, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr_msv_mpiaij, PETSC_ERROR_REPEAT, " "); \
5458:       return; \
5459:     } \
5460:   } while (0)

5462: #undef SETERRQ
5463: #define SETERRQ(comm, ierr, ...) \
5464:   do { \
5465:     *_ierr = PetscError(comm, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr, PETSC_ERROR_INITIAL, __VA_ARGS__); \
5466:     return; \
5467:   } while (0)

5469: PETSC_EXTERN void matsetvaluesseqaij_(Mat *AA, PetscInt *mm, const PetscInt im[], PetscInt *nn, const PetscInt in[], const PetscScalar v[], InsertMode *isis, PetscErrorCode *_ierr)
5470: {
5471:   Mat         A = *AA;
5472:   PetscInt    m = *mm, n = *nn;
5473:   InsertMode  is = *isis;
5474:   Mat_SeqAIJ *a  = (Mat_SeqAIJ *)A->data;
5475:   PetscInt   *rp, k, low, high, t, ii, row, nrow, i, col, l, rmax, N;
5476:   PetscInt   *imax, *ai, *ailen;
5477:   PetscInt   *aj, nonew = a->nonew, lastcol = -1;
5478:   MatScalar  *ap, value, *aa;
5479:   PetscBool   ignorezeroentries = a->ignorezeroentries;
5480:   PetscBool   roworiented       = a->roworiented;

5482:   PetscFunctionBegin;
5483:   MatCheckPreallocated(A, 1);
5484:   imax  = a->imax;
5485:   ai    = a->i;
5486:   ailen = a->ilen;
5487:   aj    = a->j;
5488:   aa    = a->a;

5490:   for (k = 0; k < m; k++) { /* loop over added rows */
5491:     row = im[k];
5492:     if (row < 0) continue;
5493:     PetscCheck(row < A->rmap->n, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_OUTOFRANGE, "Row too large");
5494:     rp   = aj + ai[row];
5495:     ap   = aa + ai[row];
5496:     rmax = imax[row];
5497:     nrow = ailen[row];
5498:     low  = 0;
5499:     high = nrow;
5500:     for (l = 0; l < n; l++) { /* loop over added columns */
5501:       if (in[l] < 0) continue;
5502:       PetscCheck(in[l] < A->cmap->n, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_OUTOFRANGE, "Column too large");
5503:       col = in[l];
5504:       if (roworiented) value = v[l + k * n];
5505:       else value = v[k + l * m];

5507:       if (value == 0.0 && ignorezeroentries && (is == ADD_VALUES)) continue;

5509:       if (col <= lastcol) low = 0;
5510:       else high = nrow;
5511:       lastcol = col;
5512:       while (high - low > 5) {
5513:         t = (low + high) / 2;
5514:         if (rp[t] > col) high = t;
5515:         else low = t;
5516:       }
5517:       for (i = low; i < high; i++) {
5518:         if (rp[i] > col) break;
5519:         if (rp[i] == col) {
5520:           if (is == ADD_VALUES) ap[i] += value;
5521:           else ap[i] = value;
5522:           goto noinsert;
5523:         }
5524:       }
5525:       if (value == 0.0 && ignorezeroentries) goto noinsert;
5526:       if (nonew == 1) goto noinsert;
5527:       PetscCheck(nonew != -1, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_OUTOFRANGE, "Inserting a new nonzero in the matrix");
5528:       MatSeqXAIJReallocateAIJ(A, A->rmap->n, 1, nrow, row, col, rmax, aa, ai, aj, rp, ap, imax, nonew, MatScalar);
5529:       N = nrow++ - 1;
5530:       a->nz++;
5531:       high++;
5532:       /* shift up all the later entries in this row */
5533:       for (ii = N; ii >= i; ii--) {
5534:         rp[ii + 1] = rp[ii];
5535:         ap[ii + 1] = ap[ii];
5536:       }
5537:       rp[i] = col;
5538:       ap[i] = value;
5539:     noinsert:;
5540:       low = i + 1;
5541:     }
5542:     ailen[row] = nrow;
5543:   }
5544:   PetscFunctionReturnVoid();
5545: }
5546: /* Undefining these here since they were redefined from their original definition above! No
5547:  * other PETSc functions should be defined past this point, as it is impossible to recover the
5548:  * original definitions */
5549: #undef PetscCall
5550: #undef SETERRQ