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