xref: /petsc/src/mat/impls/sbaij/seq/sbaij.c (revision 21604f62fddfd00a143407caac518db3de88a88a)
1 /*
2     Defines the basic matrix operations for the SBAIJ (compressed row)
3   matrix storage format.
4 */
5 #include <../src/mat/impls/baij/seq/baij.h> /*I "petscmat.h" I*/
6 #include <../src/mat/impls/sbaij/seq/sbaij.h>
7 #include <petscblaslapack.h>
8 
9 #include <../src/mat/impls/sbaij/seq/relax.h>
10 #define USESHORT
11 #include <../src/mat/impls/sbaij/seq/relax.h>
12 
13 /* defines MatSetValues_Seq_Hash(), MatAssemblyEnd_Seq_Hash(), MatSetUp_Seq_Hash() */
14 #define TYPE SBAIJ
15 #define TYPE_SBAIJ
16 #define TYPE_BS
17 #include "../src/mat/impls/aij/seq/seqhashmatsetvalues.h"
18 #undef TYPE_BS
19 #define TYPE_BS _BS
20 #define TYPE_BS_ON
21 #include "../src/mat/impls/aij/seq/seqhashmatsetvalues.h"
22 #undef TYPE_BS
23 #undef TYPE_SBAIJ
24 #include "../src/mat/impls/aij/seq/seqhashmat.h"
25 #undef TYPE
26 #undef TYPE_BS_ON
27 
28 #if defined(PETSC_HAVE_ELEMENTAL)
29 PETSC_INTERN PetscErrorCode MatConvert_SeqSBAIJ_Elemental(Mat, MatType, MatReuse, Mat *);
30 #endif
31 #if defined(PETSC_HAVE_SCALAPACK)
32 PETSC_INTERN PetscErrorCode MatConvert_SBAIJ_ScaLAPACK(Mat, MatType, MatReuse, Mat *);
33 #endif
34 PETSC_INTERN PetscErrorCode MatConvert_MPISBAIJ_Basic(Mat, MatType, MatReuse, Mat *);
35 
36 /*
37      Checks for missing diagonals
38 */
39 static PetscErrorCode MatMissingDiagonal_SeqSBAIJ(Mat A, PetscBool *missing, PetscInt *dd)
40 {
41   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ *)A->data;
42   PetscInt     *diag, *ii = a->i, i;
43 
44   PetscFunctionBegin;
45   PetscCall(MatMarkDiagonal_SeqSBAIJ(A));
46   *missing = PETSC_FALSE;
47   if (A->rmap->n > 0 && !ii) {
48     *missing = PETSC_TRUE;
49     if (dd) *dd = 0;
50     PetscCall(PetscInfo(A, "Matrix has no entries therefore is missing diagonal\n"));
51   } else {
52     diag = a->diag;
53     for (i = 0; i < a->mbs; i++) {
54       if (diag[i] >= ii[i + 1]) {
55         *missing = PETSC_TRUE;
56         if (dd) *dd = i;
57         break;
58       }
59     }
60   }
61   PetscFunctionReturn(PETSC_SUCCESS);
62 }
63 
64 PetscErrorCode MatMarkDiagonal_SeqSBAIJ(Mat A)
65 {
66   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ *)A->data;
67   PetscInt      i, j;
68 
69   PetscFunctionBegin;
70   if (!a->diag) {
71     PetscCall(PetscMalloc1(a->mbs, &a->diag));
72     a->free_diag = PETSC_TRUE;
73   }
74   for (i = 0; i < a->mbs; i++) {
75     a->diag[i] = a->i[i + 1];
76     for (j = a->i[i]; j < a->i[i + 1]; j++) {
77       if (a->j[j] == i) {
78         a->diag[i] = j;
79         break;
80       }
81     }
82   }
83   PetscFunctionReturn(PETSC_SUCCESS);
84 }
85 
86 static PetscErrorCode MatGetRowIJ_SeqSBAIJ(Mat A, PetscInt oshift, PetscBool symmetric, PetscBool blockcompressed, PetscInt *nn, const PetscInt *inia[], const PetscInt *inja[], PetscBool *done)
87 {
88   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ *)A->data;
89   PetscInt      i, j, n = a->mbs, nz = a->i[n], *tia, *tja, bs = A->rmap->bs, k, l, cnt;
90   PetscInt    **ia = (PetscInt **)inia, **ja = (PetscInt **)inja;
91 
92   PetscFunctionBegin;
93   *nn = n;
94   if (!ia) PetscFunctionReturn(PETSC_SUCCESS);
95   if (symmetric) {
96     PetscCall(MatToSymmetricIJ_SeqAIJ(n, a->i, a->j, PETSC_FALSE, 0, 0, &tia, &tja));
97     nz = tia[n];
98   } else {
99     tia = a->i;
100     tja = a->j;
101   }
102 
103   if (!blockcompressed && bs > 1) {
104     (*nn) *= bs;
105     /* malloc & create the natural set of indices */
106     PetscCall(PetscMalloc1((n + 1) * bs, ia));
107     if (n) {
108       (*ia)[0] = oshift;
109       for (j = 1; j < bs; j++) (*ia)[j] = (tia[1] - tia[0]) * bs + (*ia)[j - 1];
110     }
111 
112     for (i = 1; i < n; i++) {
113       (*ia)[i * bs] = (tia[i] - tia[i - 1]) * bs + (*ia)[i * bs - 1];
114       for (j = 1; j < bs; j++) (*ia)[i * bs + j] = (tia[i + 1] - tia[i]) * bs + (*ia)[i * bs + j - 1];
115     }
116     if (n) (*ia)[n * bs] = (tia[n] - tia[n - 1]) * bs + (*ia)[n * bs - 1];
117 
118     if (inja) {
119       PetscCall(PetscMalloc1(nz * bs * bs, ja));
120       cnt = 0;
121       for (i = 0; i < n; i++) {
122         for (j = 0; j < bs; j++) {
123           for (k = tia[i]; k < tia[i + 1]; k++) {
124             for (l = 0; l < bs; l++) (*ja)[cnt++] = bs * tja[k] + l;
125           }
126         }
127       }
128     }
129 
130     if (symmetric) { /* deallocate memory allocated in MatToSymmetricIJ_SeqAIJ() */
131       PetscCall(PetscFree(tia));
132       PetscCall(PetscFree(tja));
133     }
134   } else if (oshift == 1) {
135     if (symmetric) {
136       nz = tia[A->rmap->n / bs];
137       /*  add 1 to i and j indices */
138       for (i = 0; i < A->rmap->n / bs + 1; i++) tia[i] = tia[i] + 1;
139       *ia = tia;
140       if (ja) {
141         for (i = 0; i < nz; i++) tja[i] = tja[i] + 1;
142         *ja = tja;
143       }
144     } else {
145       nz = a->i[A->rmap->n / bs];
146       /* malloc space and  add 1 to i and j indices */
147       PetscCall(PetscMalloc1(A->rmap->n / bs + 1, ia));
148       for (i = 0; i < A->rmap->n / bs + 1; i++) (*ia)[i] = a->i[i] + 1;
149       if (ja) {
150         PetscCall(PetscMalloc1(nz, ja));
151         for (i = 0; i < nz; i++) (*ja)[i] = a->j[i] + 1;
152       }
153     }
154   } else {
155     *ia = tia;
156     if (ja) *ja = tja;
157   }
158   PetscFunctionReturn(PETSC_SUCCESS);
159 }
160 
161 static PetscErrorCode MatRestoreRowIJ_SeqSBAIJ(Mat A, PetscInt oshift, PetscBool symmetric, PetscBool blockcompressed, PetscInt *nn, const PetscInt *ia[], const PetscInt *ja[], PetscBool *done)
162 {
163   PetscFunctionBegin;
164   if (!ia) PetscFunctionReturn(PETSC_SUCCESS);
165   if ((!blockcompressed && A->rmap->bs > 1) || (symmetric || oshift == 1)) {
166     PetscCall(PetscFree(*ia));
167     if (ja) PetscCall(PetscFree(*ja));
168   }
169   PetscFunctionReturn(PETSC_SUCCESS);
170 }
171 
172 PetscErrorCode MatDestroy_SeqSBAIJ(Mat A)
173 {
174   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ *)A->data;
175 
176   PetscFunctionBegin;
177   if (A->hash_active) {
178     PetscInt bs;
179     A->ops[0] = a->cops;
180     PetscCall(PetscHMapIJVDestroy(&a->ht));
181     PetscCall(MatGetBlockSize(A, &bs));
182     if (bs > 1) PetscCall(PetscHSetIJDestroy(&a->bht));
183     PetscCall(PetscFree(a->dnz));
184     PetscCall(PetscFree(a->bdnz));
185     A->hash_active = PETSC_FALSE;
186   }
187   PetscCall(PetscLogObjectState((PetscObject)A, "Rows=%" PetscInt_FMT ", NZ=%" PetscInt_FMT, A->rmap->N, a->nz));
188   PetscCall(MatSeqXAIJFreeAIJ(A, &a->a, &a->j, &a->i));
189   if (a->free_diag) PetscCall(PetscFree(a->diag));
190   PetscCall(ISDestroy(&a->row));
191   PetscCall(ISDestroy(&a->col));
192   PetscCall(ISDestroy(&a->icol));
193   PetscCall(PetscFree(a->idiag));
194   PetscCall(PetscFree(a->inode.size));
195   if (a->free_imax_ilen) PetscCall(PetscFree2(a->imax, a->ilen));
196   PetscCall(PetscFree(a->solve_work));
197   PetscCall(PetscFree(a->sor_work));
198   PetscCall(PetscFree(a->solves_work));
199   PetscCall(PetscFree(a->mult_work));
200   PetscCall(PetscFree(a->saved_values));
201   if (a->free_jshort) PetscCall(PetscFree(a->jshort));
202   PetscCall(PetscFree(a->inew));
203   PetscCall(MatDestroy(&a->parent));
204   PetscCall(PetscFree(A->data));
205 
206   PetscCall(PetscObjectChangeTypeName((PetscObject)A, NULL));
207   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatSeqSBAIJGetArray_C", NULL));
208   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatSeqSBAIJRestoreArray_C", NULL));
209   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatStoreValues_C", NULL));
210   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatRetrieveValues_C", NULL));
211   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatSeqSBAIJSetColumnIndices_C", NULL));
212   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqsbaij_seqaij_C", NULL));
213   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqsbaij_seqbaij_C", NULL));
214   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatSeqSBAIJSetPreallocation_C", NULL));
215   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatSeqSBAIJSetPreallocationCSR_C", NULL));
216 #if defined(PETSC_HAVE_ELEMENTAL)
217   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqsbaij_elemental_C", NULL));
218 #endif
219 #if defined(PETSC_HAVE_SCALAPACK)
220   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqsbaij_scalapack_C", NULL));
221 #endif
222   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatFactorGetSolverType_C", NULL));
223   PetscFunctionReturn(PETSC_SUCCESS);
224 }
225 
226 static PetscErrorCode MatSetOption_SeqSBAIJ(Mat A, MatOption op, PetscBool flg)
227 {
228   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ *)A->data;
229 #if defined(PETSC_USE_COMPLEX)
230   PetscInt bs;
231 #endif
232 
233   PetscFunctionBegin;
234 #if defined(PETSC_USE_COMPLEX)
235   PetscCall(MatGetBlockSize(A, &bs));
236 #endif
237   switch (op) {
238   case MAT_ROW_ORIENTED:
239     a->roworiented = flg;
240     break;
241   case MAT_KEEP_NONZERO_PATTERN:
242     a->keepnonzeropattern = flg;
243     break;
244   case MAT_NEW_NONZERO_LOCATIONS:
245     a->nonew = (flg ? 0 : 1);
246     break;
247   case MAT_NEW_NONZERO_LOCATION_ERR:
248     a->nonew = (flg ? -1 : 0);
249     break;
250   case MAT_NEW_NONZERO_ALLOCATION_ERR:
251     a->nonew = (flg ? -2 : 0);
252     break;
253   case MAT_UNUSED_NONZERO_LOCATION_ERR:
254     a->nounused = (flg ? -1 : 0);
255     break;
256   case MAT_FORCE_DIAGONAL_ENTRIES:
257   case MAT_IGNORE_OFF_PROC_ENTRIES:
258   case MAT_USE_HASH_TABLE:
259   case MAT_SORTED_FULL:
260   case MAT_SUBMAT_SINGLEIS:
261     PetscCall(PetscInfo(A, "Option %s ignored\n", MatOptions[op]));
262     break;
263   case MAT_HERMITIAN:
264 #if defined(PETSC_USE_COMPLEX)
265     if (flg) { /* disable transpose ops */
266       PetscCheck(bs <= 1, PETSC_COMM_SELF, PETSC_ERR_SUP, "No support for Hermitian with block size greater than 1");
267       A->ops->multtranspose    = NULL;
268       A->ops->multtransposeadd = NULL;
269       A->symmetric             = PETSC_BOOL3_FALSE;
270     }
271 #endif
272     break;
273   case MAT_SYMMETRIC:
274   case MAT_SPD:
275 #if defined(PETSC_USE_COMPLEX)
276     if (flg) { /* An hermitian and symmetric matrix has zero imaginary part (restore back transpose ops) */
277       A->ops->multtranspose    = A->ops->mult;
278       A->ops->multtransposeadd = A->ops->multadd;
279     }
280 #endif
281     break;
282     /* These options are handled directly by MatSetOption() */
283   case MAT_STRUCTURALLY_SYMMETRIC:
284   case MAT_SYMMETRY_ETERNAL:
285   case MAT_STRUCTURAL_SYMMETRY_ETERNAL:
286   case MAT_STRUCTURE_ONLY:
287   case MAT_SPD_ETERNAL:
288     /* These options are handled directly by MatSetOption() */
289     break;
290   case MAT_IGNORE_LOWER_TRIANGULAR:
291     a->ignore_ltriangular = flg;
292     break;
293   case MAT_ERROR_LOWER_TRIANGULAR:
294     a->ignore_ltriangular = flg;
295     break;
296   case MAT_GETROW_UPPERTRIANGULAR:
297     a->getrow_utriangular = flg;
298     break;
299   default:
300     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "unknown option %d", op);
301   }
302   PetscFunctionReturn(PETSC_SUCCESS);
303 }
304 
305 PetscErrorCode MatGetRow_SeqSBAIJ(Mat A, PetscInt row, PetscInt *nz, PetscInt **idx, PetscScalar **v)
306 {
307   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ *)A->data;
308 
309   PetscFunctionBegin;
310   PetscCheck(!A || a->getrow_utriangular, PETSC_COMM_SELF, PETSC_ERR_SUP, "MatGetRow is not supported for SBAIJ matrix format. Getting the upper triangular part of row, run with -mat_getrow_uppertriangular, call MatSetOption(mat,MAT_GETROW_UPPERTRIANGULAR,PETSC_TRUE) or MatGetRowUpperTriangular()");
311 
312   /* Get the upper triangular part of the row */
313   PetscCall(MatGetRow_SeqBAIJ_private(A, row, nz, idx, v, a->i, a->j, a->a));
314   PetscFunctionReturn(PETSC_SUCCESS);
315 }
316 
317 PetscErrorCode MatRestoreRow_SeqSBAIJ(Mat A, PetscInt row, PetscInt *nz, PetscInt **idx, PetscScalar **v)
318 {
319   PetscFunctionBegin;
320   if (idx) PetscCall(PetscFree(*idx));
321   if (v) PetscCall(PetscFree(*v));
322   PetscFunctionReturn(PETSC_SUCCESS);
323 }
324 
325 static PetscErrorCode MatGetRowUpperTriangular_SeqSBAIJ(Mat A)
326 {
327   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ *)A->data;
328 
329   PetscFunctionBegin;
330   a->getrow_utriangular = PETSC_TRUE;
331   PetscFunctionReturn(PETSC_SUCCESS);
332 }
333 
334 static PetscErrorCode MatRestoreRowUpperTriangular_SeqSBAIJ(Mat A)
335 {
336   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ *)A->data;
337 
338   PetscFunctionBegin;
339   a->getrow_utriangular = PETSC_FALSE;
340   PetscFunctionReturn(PETSC_SUCCESS);
341 }
342 
343 static PetscErrorCode MatTranspose_SeqSBAIJ(Mat A, MatReuse reuse, Mat *B)
344 {
345   PetscFunctionBegin;
346   if (reuse == MAT_REUSE_MATRIX) PetscCall(MatTransposeCheckNonzeroState_Private(A, *B));
347   if (reuse == MAT_INITIAL_MATRIX) {
348     PetscCall(MatDuplicate(A, MAT_COPY_VALUES, B));
349   } else if (reuse == MAT_REUSE_MATRIX) {
350     PetscCall(MatCopy(A, *B, SAME_NONZERO_PATTERN));
351   }
352   PetscFunctionReturn(PETSC_SUCCESS);
353 }
354 
355 static PetscErrorCode MatView_SeqSBAIJ_ASCII(Mat A, PetscViewer viewer)
356 {
357   Mat_SeqSBAIJ     *a = (Mat_SeqSBAIJ *)A->data;
358   PetscInt          i, j, bs = A->rmap->bs, k, l, bs2 = a->bs2;
359   PetscViewerFormat format;
360   PetscInt         *diag;
361   const char       *matname;
362 
363   PetscFunctionBegin;
364   PetscCall(PetscViewerGetFormat(viewer, &format));
365   if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
366     PetscCall(PetscViewerASCIIPrintf(viewer, "  block size is %" PetscInt_FMT "\n", bs));
367   } else if (format == PETSC_VIEWER_ASCII_MATLAB) {
368     Mat aij;
369 
370     if (A->factortype && bs > 1) {
371       PetscCall(PetscPrintf(PETSC_COMM_SELF, "Warning: matrix is factored with bs>1. MatView() with PETSC_VIEWER_ASCII_MATLAB is not supported and ignored!\n"));
372       PetscFunctionReturn(PETSC_SUCCESS);
373     }
374     PetscCall(MatConvert(A, MATSEQAIJ, MAT_INITIAL_MATRIX, &aij));
375     if (((PetscObject)A)->name) PetscCall(PetscObjectGetName((PetscObject)A, &matname));
376     if (((PetscObject)A)->name) PetscCall(PetscObjectSetName((PetscObject)aij, matname));
377     PetscCall(MatView_SeqAIJ(aij, viewer));
378     PetscCall(MatDestroy(&aij));
379   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
380     Mat B;
381 
382     PetscCall(MatConvert(A, MATSEQAIJ, MAT_INITIAL_MATRIX, &B));
383     if (((PetscObject)A)->name) PetscCall(PetscObjectGetName((PetscObject)A, &matname));
384     if (((PetscObject)A)->name) PetscCall(PetscObjectSetName((PetscObject)B, matname));
385     PetscCall(MatView_SeqAIJ(B, viewer));
386     PetscCall(MatDestroy(&B));
387   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
388     PetscFunctionReturn(PETSC_SUCCESS);
389   } else {
390     PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_FALSE));
391     if (A->factortype) { /* for factored matrix */
392       PetscCheck(bs <= 1, PETSC_COMM_SELF, PETSC_ERR_SUP, "matrix is factored with bs>1. Not implemented yet");
393 
394       diag = a->diag;
395       for (i = 0; i < a->mbs; i++) { /* for row block i */
396         PetscCall(PetscViewerASCIIPrintf(viewer, "row %" PetscInt_FMT ":", i));
397         /* diagonal entry */
398 #if defined(PETSC_USE_COMPLEX)
399         if (PetscImaginaryPart(a->a[diag[i]]) > 0.0) {
400           PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g + %g i) ", a->j[diag[i]], (double)PetscRealPart(1.0 / a->a[diag[i]]), (double)PetscImaginaryPart(1.0 / a->a[diag[i]])));
401         } else if (PetscImaginaryPart(a->a[diag[i]]) < 0.0) {
402           PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g - %g i) ", a->j[diag[i]], (double)PetscRealPart(1.0 / a->a[diag[i]]), -(double)PetscImaginaryPart(1.0 / a->a[diag[i]])));
403         } else {
404           PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g) ", a->j[diag[i]], (double)PetscRealPart(1.0 / a->a[diag[i]])));
405         }
406 #else
407         PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g) ", a->j[diag[i]], (double)(1 / a->a[diag[i]])));
408 #endif
409         /* off-diagonal entries */
410         for (k = a->i[i]; k < a->i[i + 1] - 1; k++) {
411 #if defined(PETSC_USE_COMPLEX)
412           if (PetscImaginaryPart(a->a[k]) > 0.0) {
413             PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g + %g i) ", bs * a->j[k], (double)PetscRealPart(a->a[k]), (double)PetscImaginaryPart(a->a[k])));
414           } else if (PetscImaginaryPart(a->a[k]) < 0.0) {
415             PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g - %g i) ", bs * a->j[k], (double)PetscRealPart(a->a[k]), -(double)PetscImaginaryPart(a->a[k])));
416           } else {
417             PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g) ", bs * a->j[k], (double)PetscRealPart(a->a[k])));
418           }
419 #else
420           PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g) ", a->j[k], (double)a->a[k]));
421 #endif
422         }
423         PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
424       }
425 
426     } else {                         /* for non-factored matrix */
427       for (i = 0; i < a->mbs; i++) { /* for row block i */
428         for (j = 0; j < bs; j++) {   /* for row bs*i + j */
429           PetscCall(PetscViewerASCIIPrintf(viewer, "row %" PetscInt_FMT ":", i * bs + j));
430           for (k = a->i[i]; k < a->i[i + 1]; k++) { /* for column block */
431             for (l = 0; l < bs; l++) {              /* for column */
432 #if defined(PETSC_USE_COMPLEX)
433               if (PetscImaginaryPart(a->a[bs2 * k + l * bs + j]) > 0.0) {
434                 PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g + %g i) ", bs * a->j[k] + l, (double)PetscRealPart(a->a[bs2 * k + l * bs + j]), (double)PetscImaginaryPart(a->a[bs2 * k + l * bs + j])));
435               } else if (PetscImaginaryPart(a->a[bs2 * k + l * bs + j]) < 0.0) {
436                 PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g - %g i) ", bs * a->j[k] + l, (double)PetscRealPart(a->a[bs2 * k + l * bs + j]), -(double)PetscImaginaryPart(a->a[bs2 * k + l * bs + j])));
437               } else {
438                 PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g) ", bs * a->j[k] + l, (double)PetscRealPart(a->a[bs2 * k + l * bs + j])));
439               }
440 #else
441               PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g) ", bs * a->j[k] + l, (double)a->a[bs2 * k + l * bs + j]));
442 #endif
443             }
444           }
445           PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
446         }
447       }
448     }
449     PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_TRUE));
450   }
451   PetscCall(PetscViewerFlush(viewer));
452   PetscFunctionReturn(PETSC_SUCCESS);
453 }
454 
455 #include <petscdraw.h>
456 static PetscErrorCode MatView_SeqSBAIJ_Draw_Zoom(PetscDraw draw, void *Aa)
457 {
458   Mat           A = (Mat)Aa;
459   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ *)A->data;
460   PetscInt      row, i, j, k, l, mbs = a->mbs, bs = A->rmap->bs, bs2 = a->bs2;
461   PetscReal     xl, yl, xr, yr, x_l, x_r, y_l, y_r;
462   MatScalar    *aa;
463   PetscViewer   viewer;
464   int           color;
465 
466   PetscFunctionBegin;
467   PetscCall(PetscObjectQuery((PetscObject)A, "Zoomviewer", (PetscObject *)&viewer));
468   PetscCall(PetscDrawGetCoordinates(draw, &xl, &yl, &xr, &yr));
469 
470   /* loop over matrix elements drawing boxes */
471 
472   PetscDrawCollectiveBegin(draw);
473   PetscCall(PetscDrawString(draw, .3 * (xl + xr), .3 * (yl + yr), PETSC_DRAW_BLACK, "symmetric"));
474   /* Blue for negative, Cyan for zero and  Red for positive */
475   color = PETSC_DRAW_BLUE;
476   for (i = 0, row = 0; i < mbs; i++, row += bs) {
477     for (j = a->i[i]; j < a->i[i + 1]; j++) {
478       y_l = A->rmap->N - row - 1.0;
479       y_r = y_l + 1.0;
480       x_l = a->j[j] * bs;
481       x_r = x_l + 1.0;
482       aa  = a->a + j * bs2;
483       for (k = 0; k < bs; k++) {
484         for (l = 0; l < bs; l++) {
485           if (PetscRealPart(*aa++) >= 0.) continue;
486           PetscCall(PetscDrawRectangle(draw, x_l + k, y_l - l, x_r + k, y_r - l, color, color, color, color));
487         }
488       }
489     }
490   }
491   color = PETSC_DRAW_CYAN;
492   for (i = 0, row = 0; i < mbs; i++, row += bs) {
493     for (j = a->i[i]; j < a->i[i + 1]; j++) {
494       y_l = A->rmap->N - row - 1.0;
495       y_r = y_l + 1.0;
496       x_l = a->j[j] * bs;
497       x_r = x_l + 1.0;
498       aa  = a->a + j * bs2;
499       for (k = 0; k < bs; k++) {
500         for (l = 0; l < bs; l++) {
501           if (PetscRealPart(*aa++) != 0.) continue;
502           PetscCall(PetscDrawRectangle(draw, x_l + k, y_l - l, x_r + k, y_r - l, color, color, color, color));
503         }
504       }
505     }
506   }
507   color = PETSC_DRAW_RED;
508   for (i = 0, row = 0; i < mbs; i++, row += bs) {
509     for (j = a->i[i]; j < a->i[i + 1]; j++) {
510       y_l = A->rmap->N - row - 1.0;
511       y_r = y_l + 1.0;
512       x_l = a->j[j] * bs;
513       x_r = x_l + 1.0;
514       aa  = a->a + j * bs2;
515       for (k = 0; k < bs; k++) {
516         for (l = 0; l < bs; l++) {
517           if (PetscRealPart(*aa++) <= 0.) continue;
518           PetscCall(PetscDrawRectangle(draw, x_l + k, y_l - l, x_r + k, y_r - l, color, color, color, color));
519         }
520       }
521     }
522   }
523   PetscDrawCollectiveEnd(draw);
524   PetscFunctionReturn(PETSC_SUCCESS);
525 }
526 
527 static PetscErrorCode MatView_SeqSBAIJ_Draw(Mat A, PetscViewer viewer)
528 {
529   PetscReal xl, yl, xr, yr, w, h;
530   PetscDraw draw;
531   PetscBool isnull;
532 
533   PetscFunctionBegin;
534   PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw));
535   PetscCall(PetscDrawIsNull(draw, &isnull));
536   if (isnull) PetscFunctionReturn(PETSC_SUCCESS);
537 
538   xr = A->rmap->N;
539   yr = A->rmap->N;
540   h  = yr / 10.0;
541   w  = xr / 10.0;
542   xr += w;
543   yr += h;
544   xl = -w;
545   yl = -h;
546   PetscCall(PetscDrawSetCoordinates(draw, xl, yl, xr, yr));
547   PetscCall(PetscObjectCompose((PetscObject)A, "Zoomviewer", (PetscObject)viewer));
548   PetscCall(PetscDrawZoom(draw, MatView_SeqSBAIJ_Draw_Zoom, A));
549   PetscCall(PetscObjectCompose((PetscObject)A, "Zoomviewer", NULL));
550   PetscCall(PetscDrawSave(draw));
551   PetscFunctionReturn(PETSC_SUCCESS);
552 }
553 
554 /* Used for both MPIBAIJ and MPISBAIJ matrices */
555 #define MatView_SeqSBAIJ_Binary MatView_SeqBAIJ_Binary
556 
557 PetscErrorCode MatView_SeqSBAIJ(Mat A, PetscViewer viewer)
558 {
559   PetscBool iascii, isbinary, isdraw;
560 
561   PetscFunctionBegin;
562   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii));
563   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
564   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
565   if (iascii) {
566     PetscCall(MatView_SeqSBAIJ_ASCII(A, viewer));
567   } else if (isbinary) {
568     PetscCall(MatView_SeqSBAIJ_Binary(A, viewer));
569   } else if (isdraw) {
570     PetscCall(MatView_SeqSBAIJ_Draw(A, viewer));
571   } else {
572     Mat         B;
573     const char *matname;
574     PetscCall(MatConvert(A, MATSEQAIJ, MAT_INITIAL_MATRIX, &B));
575     if (((PetscObject)A)->name) PetscCall(PetscObjectGetName((PetscObject)A, &matname));
576     if (((PetscObject)A)->name) PetscCall(PetscObjectSetName((PetscObject)B, matname));
577     PetscCall(MatView(B, viewer));
578     PetscCall(MatDestroy(&B));
579   }
580   PetscFunctionReturn(PETSC_SUCCESS);
581 }
582 
583 PetscErrorCode MatGetValues_SeqSBAIJ(Mat A, PetscInt m, const PetscInt im[], PetscInt n, const PetscInt in[], PetscScalar v[])
584 {
585   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ *)A->data;
586   PetscInt     *rp, k, low, high, t, row, nrow, i, col, l, *aj = a->j;
587   PetscInt     *ai = a->i, *ailen = a->ilen;
588   PetscInt      brow, bcol, ridx, cidx, bs = A->rmap->bs, bs2 = a->bs2;
589   MatScalar    *ap, *aa = a->a;
590 
591   PetscFunctionBegin;
592   for (k = 0; k < m; k++) { /* loop over rows */
593     row  = im[k];
594     brow = row / bs;
595     if (row < 0) {
596       v += n;
597       continue;
598     } /* negative row */
599     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);
600     rp   = aj + ai[brow];
601     ap   = aa + bs2 * ai[brow];
602     nrow = ailen[brow];
603     for (l = 0; l < n; l++) { /* loop over columns */
604       if (in[l] < 0) {
605         v++;
606         continue;
607       } /* negative column */
608       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);
609       col  = in[l];
610       bcol = col / bs;
611       cidx = col % bs;
612       ridx = row % bs;
613       high = nrow;
614       low  = 0; /* assume unsorted */
615       while (high - low > 5) {
616         t = (low + high) / 2;
617         if (rp[t] > bcol) high = t;
618         else low = t;
619       }
620       for (i = low; i < high; i++) {
621         if (rp[i] > bcol) break;
622         if (rp[i] == bcol) {
623           *v++ = ap[bs2 * i + bs * cidx + ridx];
624           goto finished;
625         }
626       }
627       *v++ = 0.0;
628     finished:;
629     }
630   }
631   PetscFunctionReturn(PETSC_SUCCESS);
632 }
633 
634 static PetscErrorCode MatPermute_SeqSBAIJ(Mat A, IS rowp, IS colp, Mat *B)
635 {
636   Mat       C;
637   PetscBool flg = (PetscBool)(rowp == colp);
638 
639   PetscFunctionBegin;
640   PetscCall(MatConvert(A, MATSEQBAIJ, MAT_INITIAL_MATRIX, &C));
641   PetscCall(MatPermute(C, rowp, colp, B));
642   PetscCall(MatDestroy(&C));
643   if (!flg) PetscCall(ISEqual(rowp, colp, &flg));
644   if (flg) PetscCall(MatConvert(*B, MATSEQSBAIJ, MAT_INPLACE_MATRIX, B));
645   PetscFunctionReturn(PETSC_SUCCESS);
646 }
647 
648 PetscErrorCode MatSetValuesBlocked_SeqSBAIJ(Mat A, PetscInt m, const PetscInt im[], PetscInt n, const PetscInt in[], const PetscScalar v[], InsertMode is)
649 {
650   Mat_SeqSBAIJ      *a = (Mat_SeqSBAIJ *)A->data;
651   PetscInt          *rp, k, low, high, t, ii, jj, row, nrow, i, col, l, rmax, N, lastcol = -1;
652   PetscInt          *imax = a->imax, *ai = a->i, *ailen = a->ilen;
653   PetscInt          *aj = a->j, nonew = a->nonew, bs2 = a->bs2, bs = A->rmap->bs, stepval;
654   PetscBool          roworiented = a->roworiented;
655   const PetscScalar *value       = v;
656   MatScalar         *ap, *aa = a->a, *bap;
657 
658   PetscFunctionBegin;
659   if (roworiented) stepval = (n - 1) * bs;
660   else stepval = (m - 1) * bs;
661   for (k = 0; k < m; k++) { /* loop over added rows */
662     row = im[k];
663     if (row < 0) continue;
664     PetscCheck(row < a->mbs, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Block index row too large %" PetscInt_FMT " max %" PetscInt_FMT, row, a->mbs - 1);
665     rp   = aj + ai[row];
666     ap   = aa + bs2 * ai[row];
667     rmax = imax[row];
668     nrow = ailen[row];
669     low  = 0;
670     high = nrow;
671     for (l = 0; l < n; l++) { /* loop over added columns */
672       if (in[l] < 0) continue;
673       col = in[l];
674       PetscCheck(col < a->nbs, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Block index column too large %" PetscInt_FMT " max %" PetscInt_FMT, col, a->nbs - 1);
675       if (col < row) {
676         if (a->ignore_ltriangular) continue; /* ignore lower triangular block */
677         else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_USER, "Lower triangular value cannot be set for sbaij format. Ignoring these values, run with -mat_ignore_lower_triangular or call MatSetOption(mat,MAT_IGNORE_LOWER_TRIANGULAR,PETSC_TRUE)");
678       }
679       if (roworiented) value = v + k * (stepval + bs) * bs + l * bs;
680       else value = v + l * (stepval + bs) * bs + k * bs;
681 
682       if (col <= lastcol) low = 0;
683       else high = nrow;
684 
685       lastcol = col;
686       while (high - low > 7) {
687         t = (low + high) / 2;
688         if (rp[t] > col) high = t;
689         else low = t;
690       }
691       for (i = low; i < high; i++) {
692         if (rp[i] > col) break;
693         if (rp[i] == col) {
694           bap = ap + bs2 * i;
695           if (roworiented) {
696             if (is == ADD_VALUES) {
697               for (ii = 0; ii < bs; ii++, value += stepval) {
698                 for (jj = ii; jj < bs2; jj += bs) bap[jj] += *value++;
699               }
700             } else {
701               for (ii = 0; ii < bs; ii++, value += stepval) {
702                 for (jj = ii; jj < bs2; jj += bs) bap[jj] = *value++;
703               }
704             }
705           } else {
706             if (is == ADD_VALUES) {
707               for (ii = 0; ii < bs; ii++, value += stepval) {
708                 for (jj = 0; jj < bs; jj++) *bap++ += *value++;
709               }
710             } else {
711               for (ii = 0; ii < bs; ii++, value += stepval) {
712                 for (jj = 0; jj < bs; jj++) *bap++ = *value++;
713               }
714             }
715           }
716           goto noinsert2;
717         }
718       }
719       if (nonew == 1) goto noinsert2;
720       PetscCheck(nonew != -1, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Inserting a new block index nonzero block (%" PetscInt_FMT ", %" PetscInt_FMT ") in the matrix", row, col);
721       MatSeqXAIJReallocateAIJ(A, a->mbs, bs2, nrow, row, col, rmax, aa, ai, aj, rp, ap, imax, nonew, MatScalar);
722       N = nrow++ - 1;
723       high++;
724       /* shift up all the later entries in this row */
725       PetscCall(PetscArraymove(rp + i + 1, rp + i, N - i + 1));
726       PetscCall(PetscArraymove(ap + bs2 * (i + 1), ap + bs2 * i, bs2 * (N - i + 1)));
727       PetscCall(PetscArrayzero(ap + bs2 * i, bs2));
728       rp[i] = col;
729       bap   = ap + bs2 * i;
730       if (roworiented) {
731         for (ii = 0; ii < bs; ii++, value += stepval) {
732           for (jj = ii; jj < bs2; jj += bs) bap[jj] = *value++;
733         }
734       } else {
735         for (ii = 0; ii < bs; ii++, value += stepval) {
736           for (jj = 0; jj < bs; jj++) *bap++ = *value++;
737         }
738       }
739     noinsert2:;
740       low = i;
741     }
742     ailen[row] = nrow;
743   }
744   PetscFunctionReturn(PETSC_SUCCESS);
745 }
746 
747 static PetscErrorCode MatAssemblyEnd_SeqSBAIJ(Mat A, MatAssemblyType mode)
748 {
749   Mat_SeqSBAIJ *a      = (Mat_SeqSBAIJ *)A->data;
750   PetscInt      fshift = 0, i, *ai = a->i, *aj = a->j, *imax = a->imax;
751   PetscInt      m = A->rmap->N, *ip, N, *ailen = a->ilen;
752   PetscInt      mbs = a->mbs, bs2 = a->bs2, rmax = 0;
753   MatScalar    *aa = a->a, *ap;
754 
755   PetscFunctionBegin;
756   if (mode == MAT_FLUSH_ASSEMBLY || (A->was_assembled && A->ass_nonzerostate == A->nonzerostate)) PetscFunctionReturn(PETSC_SUCCESS);
757 
758   if (m) rmax = ailen[0];
759   for (i = 1; i < mbs; i++) {
760     /* move each row back by the amount of empty slots (fshift) before it*/
761     fshift += imax[i - 1] - ailen[i - 1];
762     rmax = PetscMax(rmax, ailen[i]);
763     if (fshift) {
764       ip = aj + ai[i];
765       ap = aa + bs2 * ai[i];
766       N  = ailen[i];
767       PetscCall(PetscArraymove(ip - fshift, ip, N));
768       PetscCall(PetscArraymove(ap - bs2 * fshift, ap, bs2 * N));
769     }
770     ai[i] = ai[i - 1] + ailen[i - 1];
771   }
772   if (mbs) {
773     fshift += imax[mbs - 1] - ailen[mbs - 1];
774     ai[mbs] = ai[mbs - 1] + ailen[mbs - 1];
775   }
776   /* reset ilen and imax for each row */
777   for (i = 0; i < mbs; i++) ailen[i] = imax[i] = ai[i + 1] - ai[i];
778   a->nz = ai[mbs];
779 
780   /* diagonals may have moved, reset it */
781   if (a->diag) PetscCall(PetscArraycpy(a->diag, ai, mbs));
782   PetscCheck(!fshift || a->nounused != -1, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Unused space detected in matrix: %" PetscInt_FMT " X %" PetscInt_FMT " block size %" PetscInt_FMT ", %" PetscInt_FMT " unneeded", m, A->cmap->n, A->rmap->bs, fshift * bs2);
783 
784   PetscCall(PetscInfo(A, "Matrix size: %" PetscInt_FMT " X %" PetscInt_FMT ", block size %" PetscInt_FMT "; storage space: %" PetscInt_FMT " unneeded, %" PetscInt_FMT " used\n", m, A->rmap->N, A->rmap->bs, fshift * bs2, a->nz * bs2));
785   PetscCall(PetscInfo(A, "Number of mallocs during MatSetValues is %" PetscInt_FMT "\n", a->reallocs));
786   PetscCall(PetscInfo(A, "Most nonzeros blocks in any row is %" PetscInt_FMT "\n", rmax));
787 
788   A->info.mallocs += a->reallocs;
789   a->reallocs         = 0;
790   A->info.nz_unneeded = (PetscReal)fshift * bs2;
791   a->idiagvalid       = PETSC_FALSE;
792   a->rmax             = rmax;
793 
794   if (A->cmap->n < 65536 && A->cmap->bs == 1) {
795     if (a->jshort && a->free_jshort) {
796       /* when matrix data structure is changed, previous jshort must be replaced */
797       PetscCall(PetscFree(a->jshort));
798     }
799     PetscCall(PetscMalloc1(a->i[A->rmap->n], &a->jshort));
800     for (i = 0; i < a->i[A->rmap->n]; i++) a->jshort[i] = (short)a->j[i];
801     A->ops->mult   = MatMult_SeqSBAIJ_1_ushort;
802     A->ops->sor    = MatSOR_SeqSBAIJ_ushort;
803     a->free_jshort = PETSC_TRUE;
804   }
805   PetscFunctionReturn(PETSC_SUCCESS);
806 }
807 
808 /* Only add/insert a(i,j) with i<=j (blocks).
809    Any a(i,j) with i>j input by user is ignored.
810 */
811 
812 PetscErrorCode MatSetValues_SeqSBAIJ(Mat A, PetscInt m, const PetscInt im[], PetscInt n, const PetscInt in[], const PetscScalar v[], InsertMode is)
813 {
814   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ *)A->data;
815   PetscInt     *rp, k, low, high, t, ii, row, nrow, i, col, l, rmax, N, lastcol = -1;
816   PetscInt     *imax = a->imax, *ai = a->i, *ailen = a->ilen, roworiented = a->roworiented;
817   PetscInt     *aj = a->j, nonew = a->nonew, bs = A->rmap->bs, brow, bcol;
818   PetscInt      ridx, cidx, bs2                 = a->bs2;
819   MatScalar    *ap, value, *aa                  = a->a, *bap;
820 
821   PetscFunctionBegin;
822   for (k = 0; k < m; k++) { /* loop over added rows */
823     row  = im[k];           /* row number */
824     brow = row / bs;        /* block row number */
825     if (row < 0) continue;
826     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);
827     rp   = aj + ai[brow];       /*ptr to beginning of column value of the row block*/
828     ap   = aa + bs2 * ai[brow]; /*ptr to beginning of element value of the row block*/
829     rmax = imax[brow];          /* maximum space allocated for this row */
830     nrow = ailen[brow];         /* actual length of this row */
831     low  = 0;
832     high = nrow;
833     for (l = 0; l < n; l++) { /* loop over added columns */
834       if (in[l] < 0) continue;
835       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);
836       col  = in[l];
837       bcol = col / bs; /* block col number */
838 
839       if (brow > bcol) {
840         if (a->ignore_ltriangular) continue; /* ignore lower triangular values */
841         else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_USER, "Lower triangular value cannot be set for sbaij format. Ignoring these values, run with -mat_ignore_lower_triangular or call MatSetOption(mat,MAT_IGNORE_LOWER_TRIANGULAR,PETSC_TRUE)");
842       }
843 
844       ridx = row % bs;
845       cidx = col % bs; /*row and col index inside the block */
846       if ((brow == bcol && ridx <= cidx) || (brow < bcol)) {
847         /* element value a(k,l) */
848         if (roworiented) value = v[l + k * n];
849         else value = v[k + l * m];
850 
851         /* move pointer bap to a(k,l) quickly and add/insert value */
852         if (col <= lastcol) low = 0;
853         else high = nrow;
854 
855         lastcol = col;
856         while (high - low > 7) {
857           t = (low + high) / 2;
858           if (rp[t] > bcol) high = t;
859           else low = t;
860         }
861         for (i = low; i < high; i++) {
862           if (rp[i] > bcol) break;
863           if (rp[i] == bcol) {
864             bap = ap + bs2 * i + bs * cidx + ridx;
865             if (is == ADD_VALUES) *bap += value;
866             else *bap = value;
867             /* for diag block, add/insert its symmetric element a(cidx,ridx) */
868             if (brow == bcol && ridx < cidx) {
869               bap = ap + bs2 * i + bs * ridx + cidx;
870               if (is == ADD_VALUES) *bap += value;
871               else *bap = value;
872             }
873             goto noinsert1;
874           }
875         }
876 
877         if (nonew == 1) goto noinsert1;
878         PetscCheck(nonew != -1, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Inserting a new nonzero (%" PetscInt_FMT ", %" PetscInt_FMT ") in the matrix", row, col);
879         MatSeqXAIJReallocateAIJ(A, a->mbs, bs2, nrow, brow, bcol, rmax, aa, ai, aj, rp, ap, imax, nonew, MatScalar);
880 
881         N = nrow++ - 1;
882         high++;
883         /* shift up all the later entries in this row */
884         PetscCall(PetscArraymove(rp + i + 1, rp + i, N - i + 1));
885         PetscCall(PetscArraymove(ap + bs2 * (i + 1), ap + bs2 * i, bs2 * (N - i + 1)));
886         PetscCall(PetscArrayzero(ap + bs2 * i, bs2));
887         rp[i]                          = bcol;
888         ap[bs2 * i + bs * cidx + ridx] = value;
889         /* for diag block, add/insert its symmetric element a(cidx,ridx) */
890         if (brow == bcol && ridx < cidx) ap[bs2 * i + bs * ridx + cidx] = value;
891       noinsert1:;
892         low = i;
893       }
894     } /* end of loop over added columns */
895     ailen[brow] = nrow;
896   } /* end of loop over added rows */
897   PetscFunctionReturn(PETSC_SUCCESS);
898 }
899 
900 static PetscErrorCode MatICCFactor_SeqSBAIJ(Mat inA, IS row, const MatFactorInfo *info)
901 {
902   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ *)inA->data;
903   Mat           outA;
904   PetscBool     row_identity;
905 
906   PetscFunctionBegin;
907   PetscCheck(info->levels == 0, PETSC_COMM_SELF, PETSC_ERR_SUP, "Only levels=0 is supported for in-place icc");
908   PetscCall(ISIdentity(row, &row_identity));
909   PetscCheck(row_identity, PETSC_COMM_SELF, PETSC_ERR_SUP, "Matrix reordering is not supported");
910   PetscCheck(inA->rmap->bs == 1, PETSC_COMM_SELF, PETSC_ERR_SUP, "Matrix block size %" PetscInt_FMT " is not supported", inA->rmap->bs); /* Need to replace MatCholeskyFactorSymbolic_SeqSBAIJ_MSR()! */
911 
912   outA            = inA;
913   inA->factortype = MAT_FACTOR_ICC;
914   PetscCall(PetscFree(inA->solvertype));
915   PetscCall(PetscStrallocpy(MATSOLVERPETSC, &inA->solvertype));
916 
917   PetscCall(MatMarkDiagonal_SeqSBAIJ(inA));
918   PetscCall(MatSeqSBAIJSetNumericFactorization_inplace(inA, row_identity));
919 
920   PetscCall(PetscObjectReference((PetscObject)row));
921   PetscCall(ISDestroy(&a->row));
922   a->row = row;
923   PetscCall(PetscObjectReference((PetscObject)row));
924   PetscCall(ISDestroy(&a->col));
925   a->col = row;
926 
927   /* Create the invert permutation so that it can be used in MatCholeskyFactorNumeric() */
928   if (a->icol) PetscCall(ISInvertPermutation(row, PETSC_DECIDE, &a->icol));
929 
930   if (!a->solve_work) PetscCall(PetscMalloc1(inA->rmap->N + inA->rmap->bs, &a->solve_work));
931 
932   PetscCall(MatCholeskyFactorNumeric(outA, inA, info));
933   PetscFunctionReturn(PETSC_SUCCESS);
934 }
935 
936 static PetscErrorCode MatSeqSBAIJSetColumnIndices_SeqSBAIJ(Mat mat, PetscInt *indices)
937 {
938   Mat_SeqSBAIJ *baij = (Mat_SeqSBAIJ *)mat->data;
939   PetscInt      i, nz, n;
940 
941   PetscFunctionBegin;
942   nz = baij->maxnz;
943   n  = mat->cmap->n;
944   for (i = 0; i < nz; i++) baij->j[i] = indices[i];
945 
946   baij->nz = nz;
947   for (i = 0; i < n; i++) baij->ilen[i] = baij->imax[i];
948 
949   PetscCall(MatSetOption(mat, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
950   PetscFunctionReturn(PETSC_SUCCESS);
951 }
952 
953 /*@
954   MatSeqSBAIJSetColumnIndices - Set the column indices for all the rows
955   in a `MATSEQSBAIJ` matrix.
956 
957   Input Parameters:
958 + mat     - the `MATSEQSBAIJ` matrix
959 - indices - the column indices
960 
961   Level: advanced
962 
963   Notes:
964   This can be called if you have precomputed the nonzero structure of the
965   matrix and want to provide it to the matrix object to improve the performance
966   of the `MatSetValues()` operation.
967 
968   You MUST have set the correct numbers of nonzeros per row in the call to
969   `MatCreateSeqSBAIJ()`, and the columns indices MUST be sorted.
970 
971   MUST be called before any calls to `MatSetValues()`
972 
973 .seealso: [](ch_matrices), `Mat`, `MATSEQSBAIJ`, `MatCreateSeqSBAIJ`
974 @*/
975 PetscErrorCode MatSeqSBAIJSetColumnIndices(Mat mat, PetscInt *indices)
976 {
977   PetscFunctionBegin;
978   PetscValidHeaderSpecific(mat, MAT_CLASSID, 1);
979   PetscAssertPointer(indices, 2);
980   PetscUseMethod(mat, "MatSeqSBAIJSetColumnIndices_C", (Mat, PetscInt *), (mat, indices));
981   PetscFunctionReturn(PETSC_SUCCESS);
982 }
983 
984 static PetscErrorCode MatCopy_SeqSBAIJ(Mat A, Mat B, MatStructure str)
985 {
986   PetscBool isbaij;
987 
988   PetscFunctionBegin;
989   PetscCall(PetscObjectTypeCompareAny((PetscObject)B, &isbaij, MATSEQSBAIJ, MATMPISBAIJ, ""));
990   PetscCheck(isbaij, PetscObjectComm((PetscObject)B), PETSC_ERR_SUP, "Not for matrix type %s", ((PetscObject)B)->type_name);
991   /* If the two matrices have the same copy implementation and nonzero pattern, use fast copy. */
992   if (str == SAME_NONZERO_PATTERN && A->ops->copy == B->ops->copy) {
993     Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ *)A->data;
994     Mat_SeqSBAIJ *b = (Mat_SeqSBAIJ *)B->data;
995 
996     PetscCheck(a->i[a->mbs] == b->i[b->mbs], PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Number of nonzeros in two matrices are different");
997     PetscCheck(a->mbs == b->mbs, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Number of rows in two matrices are different");
998     PetscCheck(a->bs2 == b->bs2, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Different block size");
999     PetscCall(PetscArraycpy(b->a, a->a, a->bs2 * a->i[a->mbs]));
1000     PetscCall(PetscObjectStateIncrease((PetscObject)B));
1001   } else {
1002     PetscCall(MatGetRowUpperTriangular(A));
1003     PetscCall(MatCopy_Basic(A, B, str));
1004     PetscCall(MatRestoreRowUpperTriangular(A));
1005   }
1006   PetscFunctionReturn(PETSC_SUCCESS);
1007 }
1008 
1009 static PetscErrorCode MatSeqSBAIJGetArray_SeqSBAIJ(Mat A, PetscScalar *array[])
1010 {
1011   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ *)A->data;
1012 
1013   PetscFunctionBegin;
1014   *array = a->a;
1015   PetscFunctionReturn(PETSC_SUCCESS);
1016 }
1017 
1018 static PetscErrorCode MatSeqSBAIJRestoreArray_SeqSBAIJ(Mat A, PetscScalar *array[])
1019 {
1020   PetscFunctionBegin;
1021   *array = NULL;
1022   PetscFunctionReturn(PETSC_SUCCESS);
1023 }
1024 
1025 PetscErrorCode MatAXPYGetPreallocation_SeqSBAIJ(Mat Y, Mat X, PetscInt *nnz)
1026 {
1027   PetscInt      bs = Y->rmap->bs, mbs = Y->rmap->N / bs;
1028   Mat_SeqSBAIJ *x = (Mat_SeqSBAIJ *)X->data;
1029   Mat_SeqSBAIJ *y = (Mat_SeqSBAIJ *)Y->data;
1030 
1031   PetscFunctionBegin;
1032   /* Set the number of nonzeros in the new matrix */
1033   PetscCall(MatAXPYGetPreallocation_SeqX_private(mbs, x->i, x->j, y->i, y->j, nnz));
1034   PetscFunctionReturn(PETSC_SUCCESS);
1035 }
1036 
1037 static PetscErrorCode MatAXPY_SeqSBAIJ(Mat Y, PetscScalar a, Mat X, MatStructure str)
1038 {
1039   Mat_SeqSBAIJ *x = (Mat_SeqSBAIJ *)X->data, *y = (Mat_SeqSBAIJ *)Y->data;
1040   PetscInt      bs = Y->rmap->bs, bs2 = bs * bs;
1041   PetscBLASInt  one = 1;
1042 
1043   PetscFunctionBegin;
1044   if (str == UNKNOWN_NONZERO_PATTERN || (PetscDefined(USE_DEBUG) && str == SAME_NONZERO_PATTERN)) {
1045     PetscBool e = x->nz == y->nz && x->mbs == y->mbs ? PETSC_TRUE : PETSC_FALSE;
1046     if (e) {
1047       PetscCall(PetscArraycmp(x->i, y->i, x->mbs + 1, &e));
1048       if (e) {
1049         PetscCall(PetscArraycmp(x->j, y->j, x->i[x->mbs], &e));
1050         if (e) str = SAME_NONZERO_PATTERN;
1051       }
1052     }
1053     if (!e) PetscCheck(str != SAME_NONZERO_PATTERN, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "MatStructure is not SAME_NONZERO_PATTERN");
1054   }
1055   if (str == SAME_NONZERO_PATTERN) {
1056     PetscScalar  alpha = a;
1057     PetscBLASInt bnz;
1058     PetscCall(PetscBLASIntCast(x->nz * bs2, &bnz));
1059     PetscCallBLAS("BLASaxpy", BLASaxpy_(&bnz, &alpha, x->a, &one, y->a, &one));
1060     PetscCall(PetscObjectStateIncrease((PetscObject)Y));
1061   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
1062     PetscCall(MatSetOption(X, MAT_GETROW_UPPERTRIANGULAR, PETSC_TRUE));
1063     PetscCall(MatAXPY_Basic(Y, a, X, str));
1064     PetscCall(MatSetOption(X, MAT_GETROW_UPPERTRIANGULAR, PETSC_FALSE));
1065   } else {
1066     Mat       B;
1067     PetscInt *nnz;
1068     PetscCheck(bs == X->rmap->bs, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Matrices must have same block size");
1069     PetscCall(MatGetRowUpperTriangular(X));
1070     PetscCall(MatGetRowUpperTriangular(Y));
1071     PetscCall(PetscMalloc1(Y->rmap->N, &nnz));
1072     PetscCall(MatCreate(PetscObjectComm((PetscObject)Y), &B));
1073     PetscCall(PetscObjectSetName((PetscObject)B, ((PetscObject)Y)->name));
1074     PetscCall(MatSetSizes(B, Y->rmap->n, Y->cmap->n, Y->rmap->N, Y->cmap->N));
1075     PetscCall(MatSetBlockSizesFromMats(B, Y, Y));
1076     PetscCall(MatSetType(B, ((PetscObject)Y)->type_name));
1077     PetscCall(MatAXPYGetPreallocation_SeqSBAIJ(Y, X, nnz));
1078     PetscCall(MatSeqSBAIJSetPreallocation(B, bs, 0, nnz));
1079 
1080     PetscCall(MatAXPY_BasicWithPreallocation(B, Y, a, X, str));
1081 
1082     PetscCall(MatHeaderMerge(Y, &B));
1083     PetscCall(PetscFree(nnz));
1084     PetscCall(MatRestoreRowUpperTriangular(X));
1085     PetscCall(MatRestoreRowUpperTriangular(Y));
1086   }
1087   PetscFunctionReturn(PETSC_SUCCESS);
1088 }
1089 
1090 static PetscErrorCode MatIsStructurallySymmetric_SeqSBAIJ(Mat A, PetscBool *flg)
1091 {
1092   PetscFunctionBegin;
1093   *flg = PETSC_TRUE;
1094   PetscFunctionReturn(PETSC_SUCCESS);
1095 }
1096 
1097 static PetscErrorCode MatConjugate_SeqSBAIJ(Mat A)
1098 {
1099 #if defined(PETSC_USE_COMPLEX)
1100   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ *)A->data;
1101   PetscInt      i, nz = a->bs2 * a->i[a->mbs];
1102   MatScalar    *aa = a->a;
1103 
1104   PetscFunctionBegin;
1105   for (i = 0; i < nz; i++) aa[i] = PetscConj(aa[i]);
1106 #else
1107   PetscFunctionBegin;
1108 #endif
1109   PetscFunctionReturn(PETSC_SUCCESS);
1110 }
1111 
1112 static PetscErrorCode MatRealPart_SeqSBAIJ(Mat A)
1113 {
1114   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ *)A->data;
1115   PetscInt      i, nz = a->bs2 * a->i[a->mbs];
1116   MatScalar    *aa = a->a;
1117 
1118   PetscFunctionBegin;
1119   for (i = 0; i < nz; i++) aa[i] = PetscRealPart(aa[i]);
1120   PetscFunctionReturn(PETSC_SUCCESS);
1121 }
1122 
1123 static PetscErrorCode MatImaginaryPart_SeqSBAIJ(Mat A)
1124 {
1125   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ *)A->data;
1126   PetscInt      i, nz = a->bs2 * a->i[a->mbs];
1127   MatScalar    *aa = a->a;
1128 
1129   PetscFunctionBegin;
1130   for (i = 0; i < nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
1131   PetscFunctionReturn(PETSC_SUCCESS);
1132 }
1133 
1134 static PetscErrorCode MatZeroRowsColumns_SeqSBAIJ(Mat A, PetscInt is_n, const PetscInt is_idx[], PetscScalar diag, Vec x, Vec b)
1135 {
1136   Mat_SeqSBAIJ      *baij = (Mat_SeqSBAIJ *)A->data;
1137   PetscInt           i, j, k, count;
1138   PetscInt           bs = A->rmap->bs, bs2 = baij->bs2, row, col;
1139   PetscScalar        zero = 0.0;
1140   MatScalar         *aa;
1141   const PetscScalar *xx;
1142   PetscScalar       *bb;
1143   PetscBool         *zeroed, vecs = PETSC_FALSE;
1144 
1145   PetscFunctionBegin;
1146   /* fix right-hand side if needed */
1147   if (x && b) {
1148     PetscCall(VecGetArrayRead(x, &xx));
1149     PetscCall(VecGetArray(b, &bb));
1150     vecs = PETSC_TRUE;
1151   }
1152 
1153   /* zero the columns */
1154   PetscCall(PetscCalloc1(A->rmap->n, &zeroed));
1155   for (i = 0; i < is_n; i++) {
1156     PetscCheck(is_idx[i] >= 0 && is_idx[i] < A->rmap->N, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "row %" PetscInt_FMT " out of range", is_idx[i]);
1157     zeroed[is_idx[i]] = PETSC_TRUE;
1158   }
1159   if (vecs) {
1160     for (i = 0; i < A->rmap->N; i++) {
1161       row = i / bs;
1162       for (j = baij->i[row]; j < baij->i[row + 1]; j++) {
1163         for (k = 0; k < bs; k++) {
1164           col = bs * baij->j[j] + k;
1165           if (col <= i) continue;
1166           aa = baij->a + j * bs2 + (i % bs) + bs * k;
1167           if (!zeroed[i] && zeroed[col]) bb[i] -= aa[0] * xx[col];
1168           if (zeroed[i] && !zeroed[col]) bb[col] -= aa[0] * xx[i];
1169         }
1170       }
1171     }
1172     for (i = 0; i < is_n; i++) bb[is_idx[i]] = diag * xx[is_idx[i]];
1173   }
1174 
1175   for (i = 0; i < A->rmap->N; i++) {
1176     if (!zeroed[i]) {
1177       row = i / bs;
1178       for (j = baij->i[row]; j < baij->i[row + 1]; j++) {
1179         for (k = 0; k < bs; k++) {
1180           col = bs * baij->j[j] + k;
1181           if (zeroed[col]) {
1182             aa    = baij->a + j * bs2 + (i % bs) + bs * k;
1183             aa[0] = 0.0;
1184           }
1185         }
1186       }
1187     }
1188   }
1189   PetscCall(PetscFree(zeroed));
1190   if (vecs) {
1191     PetscCall(VecRestoreArrayRead(x, &xx));
1192     PetscCall(VecRestoreArray(b, &bb));
1193   }
1194 
1195   /* zero the rows */
1196   for (i = 0; i < is_n; i++) {
1197     row   = is_idx[i];
1198     count = (baij->i[row / bs + 1] - baij->i[row / bs]) * bs;
1199     aa    = baij->a + baij->i[row / bs] * bs2 + (row % bs);
1200     for (k = 0; k < count; k++) {
1201       aa[0] = zero;
1202       aa += bs;
1203     }
1204     if (diag != 0.0) PetscUseTypeMethod(A, setvalues, 1, &row, 1, &row, &diag, INSERT_VALUES);
1205   }
1206   PetscCall(MatAssemblyEnd_SeqSBAIJ(A, MAT_FINAL_ASSEMBLY));
1207   PetscFunctionReturn(PETSC_SUCCESS);
1208 }
1209 
1210 static PetscErrorCode MatShift_SeqSBAIJ(Mat Y, PetscScalar a)
1211 {
1212   Mat_SeqSBAIJ *aij = (Mat_SeqSBAIJ *)Y->data;
1213 
1214   PetscFunctionBegin;
1215   if (!Y->preallocated || !aij->nz) PetscCall(MatSeqSBAIJSetPreallocation(Y, Y->rmap->bs, 1, NULL));
1216   PetscCall(MatShift_Basic(Y, a));
1217   PetscFunctionReturn(PETSC_SUCCESS);
1218 }
1219 
1220 PetscErrorCode MatEliminateZeros_SeqSBAIJ(Mat A, PetscBool keep)
1221 {
1222   Mat_SeqSBAIJ *a      = (Mat_SeqSBAIJ *)A->data;
1223   PetscInt      fshift = 0, fshift_prev = 0, i, *ai = a->i, *aj = a->j, *imax = a->imax, j, k;
1224   PetscInt      m = A->rmap->N, *ailen = a->ilen;
1225   PetscInt      mbs = a->mbs, bs2 = a->bs2, rmax = 0;
1226   MatScalar    *aa = a->a, *ap;
1227   PetscBool     zero;
1228 
1229   PetscFunctionBegin;
1230   PetscCheck(A->assembled, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Cannot eliminate zeros for unassembled matrix");
1231   if (m) rmax = ailen[0];
1232   for (i = 1; i <= mbs; i++) {
1233     for (k = ai[i - 1]; k < ai[i]; k++) {
1234       zero = PETSC_TRUE;
1235       ap   = aa + bs2 * k;
1236       for (j = 0; j < bs2 && zero; j++) {
1237         if (ap[j] != 0.0) zero = PETSC_FALSE;
1238       }
1239       if (zero && (aj[k] != i - 1 || !keep)) fshift++;
1240       else {
1241         if (zero && aj[k] == i - 1) PetscCall(PetscInfo(A, "Keep the diagonal block at row %" PetscInt_FMT "\n", i - 1));
1242         aj[k - fshift] = aj[k];
1243         PetscCall(PetscArraymove(ap - bs2 * fshift, ap, bs2));
1244       }
1245     }
1246     ai[i - 1] -= fshift_prev;
1247     fshift_prev  = fshift;
1248     ailen[i - 1] = imax[i - 1] = ai[i] - fshift - ai[i - 1];
1249     a->nonzerorowcnt += ((ai[i] - fshift - ai[i - 1]) > 0);
1250     rmax = PetscMax(rmax, ailen[i - 1]);
1251   }
1252   if (fshift) {
1253     if (mbs) {
1254       ai[mbs] -= fshift;
1255       a->nz = ai[mbs];
1256     }
1257     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));
1258     A->nonzerostate++;
1259     A->info.nz_unneeded += (PetscReal)fshift;
1260     a->rmax = rmax;
1261     PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
1262     PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
1263   }
1264   PetscFunctionReturn(PETSC_SUCCESS);
1265 }
1266 
1267 static struct _MatOps MatOps_Values = {MatSetValues_SeqSBAIJ,
1268                                        MatGetRow_SeqSBAIJ,
1269                                        MatRestoreRow_SeqSBAIJ,
1270                                        MatMult_SeqSBAIJ_N,
1271                                        /*  4*/ MatMultAdd_SeqSBAIJ_N,
1272                                        MatMult_SeqSBAIJ_N, /* transpose versions are same as non-transpose versions */
1273                                        MatMultAdd_SeqSBAIJ_N,
1274                                        NULL,
1275                                        NULL,
1276                                        NULL,
1277                                        /* 10*/ NULL,
1278                                        NULL,
1279                                        MatCholeskyFactor_SeqSBAIJ,
1280                                        MatSOR_SeqSBAIJ,
1281                                        MatTranspose_SeqSBAIJ,
1282                                        /* 15*/ MatGetInfo_SeqSBAIJ,
1283                                        MatEqual_SeqSBAIJ,
1284                                        MatGetDiagonal_SeqSBAIJ,
1285                                        MatDiagonalScale_SeqSBAIJ,
1286                                        MatNorm_SeqSBAIJ,
1287                                        /* 20*/ NULL,
1288                                        MatAssemblyEnd_SeqSBAIJ,
1289                                        MatSetOption_SeqSBAIJ,
1290                                        MatZeroEntries_SeqSBAIJ,
1291                                        /* 24*/ NULL,
1292                                        NULL,
1293                                        NULL,
1294                                        NULL,
1295                                        NULL,
1296                                        /* 29*/ MatSetUp_Seq_Hash,
1297                                        NULL,
1298                                        NULL,
1299                                        NULL,
1300                                        NULL,
1301                                        /* 34*/ MatDuplicate_SeqSBAIJ,
1302                                        NULL,
1303                                        NULL,
1304                                        NULL,
1305                                        MatICCFactor_SeqSBAIJ,
1306                                        /* 39*/ MatAXPY_SeqSBAIJ,
1307                                        MatCreateSubMatrices_SeqSBAIJ,
1308                                        MatIncreaseOverlap_SeqSBAIJ,
1309                                        MatGetValues_SeqSBAIJ,
1310                                        MatCopy_SeqSBAIJ,
1311                                        /* 44*/ NULL,
1312                                        MatScale_SeqSBAIJ,
1313                                        MatShift_SeqSBAIJ,
1314                                        NULL,
1315                                        MatZeroRowsColumns_SeqSBAIJ,
1316                                        /* 49*/ NULL,
1317                                        MatGetRowIJ_SeqSBAIJ,
1318                                        MatRestoreRowIJ_SeqSBAIJ,
1319                                        NULL,
1320                                        NULL,
1321                                        /* 54*/ NULL,
1322                                        NULL,
1323                                        NULL,
1324                                        MatPermute_SeqSBAIJ,
1325                                        MatSetValuesBlocked_SeqSBAIJ,
1326                                        /* 59*/ MatCreateSubMatrix_SeqSBAIJ,
1327                                        NULL,
1328                                        NULL,
1329                                        NULL,
1330                                        NULL,
1331                                        /* 64*/ NULL,
1332                                        NULL,
1333                                        NULL,
1334                                        NULL,
1335                                        NULL,
1336                                        /* 69*/ MatGetRowMaxAbs_SeqSBAIJ,
1337                                        NULL,
1338                                        MatConvert_MPISBAIJ_Basic,
1339                                        NULL,
1340                                        NULL,
1341                                        /* 74*/ NULL,
1342                                        NULL,
1343                                        NULL,
1344                                        NULL,
1345                                        NULL,
1346                                        /* 79*/ NULL,
1347                                        NULL,
1348                                        NULL,
1349                                        MatGetInertia_SeqSBAIJ,
1350                                        MatLoad_SeqSBAIJ,
1351                                        /* 84*/ NULL,
1352                                        NULL,
1353                                        MatIsStructurallySymmetric_SeqSBAIJ,
1354                                        NULL,
1355                                        NULL,
1356                                        /* 89*/ NULL,
1357                                        NULL,
1358                                        NULL,
1359                                        NULL,
1360                                        NULL,
1361                                        /* 94*/ NULL,
1362                                        NULL,
1363                                        NULL,
1364                                        NULL,
1365                                        NULL,
1366                                        /* 99*/ NULL,
1367                                        NULL,
1368                                        NULL,
1369                                        MatConjugate_SeqSBAIJ,
1370                                        NULL,
1371                                        /*104*/ NULL,
1372                                        MatRealPart_SeqSBAIJ,
1373                                        MatImaginaryPart_SeqSBAIJ,
1374                                        MatGetRowUpperTriangular_SeqSBAIJ,
1375                                        MatRestoreRowUpperTriangular_SeqSBAIJ,
1376                                        /*109*/ NULL,
1377                                        NULL,
1378                                        NULL,
1379                                        NULL,
1380                                        MatMissingDiagonal_SeqSBAIJ,
1381                                        /*114*/ NULL,
1382                                        NULL,
1383                                        NULL,
1384                                        NULL,
1385                                        NULL,
1386                                        /*119*/ NULL,
1387                                        NULL,
1388                                        NULL,
1389                                        NULL,
1390                                        NULL,
1391                                        /*124*/ NULL,
1392                                        NULL,
1393                                        NULL,
1394                                        NULL,
1395                                        NULL,
1396                                        /*129*/ NULL,
1397                                        NULL,
1398                                        NULL,
1399                                        NULL,
1400                                        NULL,
1401                                        /*134*/ NULL,
1402                                        NULL,
1403                                        NULL,
1404                                        NULL,
1405                                        NULL,
1406                                        /*139*/ MatSetBlockSizes_Default,
1407                                        NULL,
1408                                        NULL,
1409                                        NULL,
1410                                        NULL,
1411                                        /*144*/ MatCreateMPIMatConcatenateSeqMat_SeqSBAIJ,
1412                                        NULL,
1413                                        NULL,
1414                                        NULL,
1415                                        NULL,
1416                                        NULL,
1417                                        /*150*/ NULL,
1418                                        MatEliminateZeros_SeqSBAIJ,
1419                                        NULL,
1420                                        NULL,
1421                                        NULL,
1422                                        /*155*/ NULL,
1423                                        MatCopyHashToXAIJ_Seq_Hash};
1424 
1425 static PetscErrorCode MatStoreValues_SeqSBAIJ(Mat mat)
1426 {
1427   Mat_SeqSBAIJ *aij = (Mat_SeqSBAIJ *)mat->data;
1428   PetscInt      nz  = aij->i[mat->rmap->N] * mat->rmap->bs * aij->bs2;
1429 
1430   PetscFunctionBegin;
1431   PetscCheck(aij->nonew == 1, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
1432 
1433   /* allocate space for values if not already there */
1434   if (!aij->saved_values) PetscCall(PetscMalloc1(nz + 1, &aij->saved_values));
1435 
1436   /* copy values over */
1437   PetscCall(PetscArraycpy(aij->saved_values, aij->a, nz));
1438   PetscFunctionReturn(PETSC_SUCCESS);
1439 }
1440 
1441 static PetscErrorCode MatRetrieveValues_SeqSBAIJ(Mat mat)
1442 {
1443   Mat_SeqSBAIJ *aij = (Mat_SeqSBAIJ *)mat->data;
1444   PetscInt      nz  = aij->i[mat->rmap->N] * mat->rmap->bs * aij->bs2;
1445 
1446   PetscFunctionBegin;
1447   PetscCheck(aij->nonew == 1, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
1448   PetscCheck(aij->saved_values, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Must call MatStoreValues(A);first");
1449 
1450   /* copy values over */
1451   PetscCall(PetscArraycpy(aij->a, aij->saved_values, nz));
1452   PetscFunctionReturn(PETSC_SUCCESS);
1453 }
1454 
1455 static PetscErrorCode MatSeqSBAIJSetPreallocation_SeqSBAIJ(Mat B, PetscInt bs, PetscInt nz, const PetscInt nnz[])
1456 {
1457   Mat_SeqSBAIJ *b = (Mat_SeqSBAIJ *)B->data;
1458   PetscInt      i, mbs, nbs, bs2;
1459   PetscBool     skipallocation = PETSC_FALSE, flg = PETSC_FALSE, realalloc = PETSC_FALSE;
1460 
1461   PetscFunctionBegin;
1462   if (B->hash_active) {
1463     PetscInt bs;
1464     B->ops[0] = b->cops;
1465     PetscCall(PetscHMapIJVDestroy(&b->ht));
1466     PetscCall(MatGetBlockSize(B, &bs));
1467     if (bs > 1) PetscCall(PetscHSetIJDestroy(&b->bht));
1468     PetscCall(PetscFree(b->dnz));
1469     PetscCall(PetscFree(b->bdnz));
1470     B->hash_active = PETSC_FALSE;
1471   }
1472   if (nz >= 0 || nnz) realalloc = PETSC_TRUE;
1473 
1474   PetscCall(MatSetBlockSize(B, PetscAbs(bs)));
1475   PetscCall(PetscLayoutSetUp(B->rmap));
1476   PetscCall(PetscLayoutSetUp(B->cmap));
1477   PetscCheck(B->rmap->N <= B->cmap->N, PETSC_COMM_SELF, PETSC_ERR_SUP, "SEQSBAIJ matrix cannot have more rows %" PetscInt_FMT " than columns %" PetscInt_FMT, B->rmap->N, B->cmap->N);
1478   PetscCall(PetscLayoutGetBlockSize(B->rmap, &bs));
1479 
1480   B->preallocated = PETSC_TRUE;
1481 
1482   mbs = B->rmap->N / bs;
1483   nbs = B->cmap->n / bs;
1484   bs2 = bs * bs;
1485 
1486   PetscCheck(mbs * bs == B->rmap->N && nbs * bs == B->cmap->n, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Number rows, cols must be divisible by blocksize");
1487 
1488   if (nz == MAT_SKIP_ALLOCATION) {
1489     skipallocation = PETSC_TRUE;
1490     nz             = 0;
1491   }
1492 
1493   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 3;
1494   PetscCheck(nz >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "nz cannot be less than 0: value %" PetscInt_FMT, nz);
1495   if (nnz) {
1496     for (i = 0; i < mbs; i++) {
1497       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]);
1498       PetscCheck(nnz[i] <= nbs, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "nnz cannot be greater than block row length: local row %" PetscInt_FMT " value %" PetscInt_FMT " block rowlength %" PetscInt_FMT, i, nnz[i], nbs);
1499     }
1500   }
1501 
1502   B->ops->mult             = MatMult_SeqSBAIJ_N;
1503   B->ops->multadd          = MatMultAdd_SeqSBAIJ_N;
1504   B->ops->multtranspose    = MatMult_SeqSBAIJ_N;
1505   B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_N;
1506 
1507   PetscCall(PetscOptionsGetBool(((PetscObject)B)->options, ((PetscObject)B)->prefix, "-mat_no_unroll", &flg, NULL));
1508   if (!flg) {
1509     switch (bs) {
1510     case 1:
1511       B->ops->mult             = MatMult_SeqSBAIJ_1;
1512       B->ops->multadd          = MatMultAdd_SeqSBAIJ_1;
1513       B->ops->multtranspose    = MatMult_SeqSBAIJ_1;
1514       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_1;
1515       break;
1516     case 2:
1517       B->ops->mult             = MatMult_SeqSBAIJ_2;
1518       B->ops->multadd          = MatMultAdd_SeqSBAIJ_2;
1519       B->ops->multtranspose    = MatMult_SeqSBAIJ_2;
1520       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_2;
1521       break;
1522     case 3:
1523       B->ops->mult             = MatMult_SeqSBAIJ_3;
1524       B->ops->multadd          = MatMultAdd_SeqSBAIJ_3;
1525       B->ops->multtranspose    = MatMult_SeqSBAIJ_3;
1526       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_3;
1527       break;
1528     case 4:
1529       B->ops->mult             = MatMult_SeqSBAIJ_4;
1530       B->ops->multadd          = MatMultAdd_SeqSBAIJ_4;
1531       B->ops->multtranspose    = MatMult_SeqSBAIJ_4;
1532       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_4;
1533       break;
1534     case 5:
1535       B->ops->mult             = MatMult_SeqSBAIJ_5;
1536       B->ops->multadd          = MatMultAdd_SeqSBAIJ_5;
1537       B->ops->multtranspose    = MatMult_SeqSBAIJ_5;
1538       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_5;
1539       break;
1540     case 6:
1541       B->ops->mult             = MatMult_SeqSBAIJ_6;
1542       B->ops->multadd          = MatMultAdd_SeqSBAIJ_6;
1543       B->ops->multtranspose    = MatMult_SeqSBAIJ_6;
1544       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_6;
1545       break;
1546     case 7:
1547       B->ops->mult             = MatMult_SeqSBAIJ_7;
1548       B->ops->multadd          = MatMultAdd_SeqSBAIJ_7;
1549       B->ops->multtranspose    = MatMult_SeqSBAIJ_7;
1550       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_7;
1551       break;
1552     }
1553   }
1554 
1555   b->mbs = mbs;
1556   b->nbs = nbs;
1557   if (!skipallocation) {
1558     if (!b->imax) {
1559       PetscCall(PetscMalloc2(mbs, &b->imax, mbs, &b->ilen));
1560 
1561       b->free_imax_ilen = PETSC_TRUE;
1562     }
1563     if (!nnz) {
1564       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
1565       else if (nz <= 0) nz = 1;
1566       nz = PetscMin(nbs, nz);
1567       for (i = 0; i < mbs; i++) b->imax[i] = nz;
1568       PetscCall(PetscIntMultError(nz, mbs, &nz));
1569     } else {
1570       PetscInt64 nz64 = 0;
1571       for (i = 0; i < mbs; i++) {
1572         b->imax[i] = nnz[i];
1573         nz64 += nnz[i];
1574       }
1575       PetscCall(PetscIntCast(nz64, &nz));
1576     }
1577     /* b->ilen will count nonzeros in each block row so far. */
1578     for (i = 0; i < mbs; i++) b->ilen[i] = 0;
1579     /* nz=(nz+mbs)/2; */ /* total diagonal and superdiagonal nonzero blocks */
1580 
1581     /* allocate the matrix space */
1582     PetscCall(MatSeqXAIJFreeAIJ(B, &b->a, &b->j, &b->i));
1583     PetscCall(PetscShmgetAllocateArray(bs2 * nz, sizeof(PetscScalar), (void **)&b->a));
1584     PetscCall(PetscShmgetAllocateArray(nz, sizeof(PetscInt), (void **)&b->j));
1585     PetscCall(PetscShmgetAllocateArray(B->rmap->n + 1, sizeof(PetscInt), (void **)&b->i));
1586     b->free_a  = PETSC_TRUE;
1587     b->free_ij = PETSC_TRUE;
1588     PetscCall(PetscArrayzero(b->a, nz * bs2));
1589     PetscCall(PetscArrayzero(b->j, nz));
1590     b->free_a  = PETSC_TRUE;
1591     b->free_ij = PETSC_TRUE;
1592 
1593     /* pointer to beginning of each row */
1594     b->i[0] = 0;
1595     for (i = 1; i < mbs + 1; i++) b->i[i] = b->i[i - 1] + b->imax[i - 1];
1596 
1597   } else {
1598     b->free_a  = PETSC_FALSE;
1599     b->free_ij = PETSC_FALSE;
1600   }
1601 
1602   b->bs2     = bs2;
1603   b->nz      = 0;
1604   b->maxnz   = nz;
1605   b->inew    = NULL;
1606   b->jnew    = NULL;
1607   b->anew    = NULL;
1608   b->a2anew  = NULL;
1609   b->permute = PETSC_FALSE;
1610 
1611   B->was_assembled = PETSC_FALSE;
1612   B->assembled     = PETSC_FALSE;
1613   if (realalloc) PetscCall(MatSetOption(B, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_TRUE));
1614   PetscFunctionReturn(PETSC_SUCCESS);
1615 }
1616 
1617 static PetscErrorCode MatSeqSBAIJSetPreallocationCSR_SeqSBAIJ(Mat B, PetscInt bs, const PetscInt ii[], const PetscInt jj[], const PetscScalar V[])
1618 {
1619   PetscInt      i, j, m, nz, anz, nz_max = 0, *nnz;
1620   PetscScalar  *values      = NULL;
1621   Mat_SeqSBAIJ *b           = (Mat_SeqSBAIJ *)B->data;
1622   PetscBool     roworiented = b->roworiented;
1623   PetscBool     ilw         = b->ignore_ltriangular;
1624 
1625   PetscFunctionBegin;
1626   PetscCheck(bs >= 1, PetscObjectComm((PetscObject)B), PETSC_ERR_ARG_OUTOFRANGE, "Invalid block size specified, must be positive but it is %" PetscInt_FMT, bs);
1627   PetscCall(PetscLayoutSetBlockSize(B->rmap, bs));
1628   PetscCall(PetscLayoutSetBlockSize(B->cmap, bs));
1629   PetscCall(PetscLayoutSetUp(B->rmap));
1630   PetscCall(PetscLayoutSetUp(B->cmap));
1631   PetscCall(PetscLayoutGetBlockSize(B->rmap, &bs));
1632   m = B->rmap->n / bs;
1633 
1634   PetscCheck(!ii[0], PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "ii[0] must be 0 but it is %" PetscInt_FMT, ii[0]);
1635   PetscCall(PetscMalloc1(m + 1, &nnz));
1636   for (i = 0; i < m; i++) {
1637     nz = ii[i + 1] - ii[i];
1638     PetscCheck(nz >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Row %" PetscInt_FMT " has a negative number of columns %" PetscInt_FMT, i, nz);
1639     PetscCheckSorted(nz, jj + ii[i]);
1640     anz = 0;
1641     for (j = 0; j < nz; j++) {
1642       /* count only values on the diagonal or above */
1643       if (jj[ii[i] + j] >= i) {
1644         anz = nz - j;
1645         break;
1646       }
1647     }
1648     nz_max = PetscMax(nz_max, nz);
1649     nnz[i] = anz;
1650   }
1651   PetscCall(MatSeqSBAIJSetPreallocation(B, bs, 0, nnz));
1652   PetscCall(PetscFree(nnz));
1653 
1654   values = (PetscScalar *)V;
1655   if (!values) PetscCall(PetscCalloc1(bs * bs * nz_max, &values));
1656   b->ignore_ltriangular = PETSC_TRUE;
1657   for (i = 0; i < m; i++) {
1658     PetscInt        ncols = ii[i + 1] - ii[i];
1659     const PetscInt *icols = jj + ii[i];
1660 
1661     if (!roworiented || bs == 1) {
1662       const PetscScalar *svals = values + (V ? (bs * bs * ii[i]) : 0);
1663       PetscCall(MatSetValuesBlocked_SeqSBAIJ(B, 1, &i, ncols, icols, svals, INSERT_VALUES));
1664     } else {
1665       for (j = 0; j < ncols; j++) {
1666         const PetscScalar *svals = values + (V ? (bs * bs * (ii[i] + j)) : 0);
1667         PetscCall(MatSetValuesBlocked_SeqSBAIJ(B, 1, &i, 1, &icols[j], svals, INSERT_VALUES));
1668       }
1669     }
1670   }
1671   if (!V) PetscCall(PetscFree(values));
1672   PetscCall(MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY));
1673   PetscCall(MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY));
1674   PetscCall(MatSetOption(B, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
1675   b->ignore_ltriangular = ilw;
1676   PetscFunctionReturn(PETSC_SUCCESS);
1677 }
1678 
1679 /*
1680    This is used to set the numeric factorization for both Cholesky and ICC symbolic factorization
1681 */
1682 PetscErrorCode MatSeqSBAIJSetNumericFactorization_inplace(Mat B, PetscBool natural)
1683 {
1684   PetscBool flg = PETSC_FALSE;
1685   PetscInt  bs  = B->rmap->bs;
1686 
1687   PetscFunctionBegin;
1688   PetscCall(PetscOptionsGetBool(((PetscObject)B)->options, ((PetscObject)B)->prefix, "-mat_no_unroll", &flg, NULL));
1689   if (flg) bs = 8;
1690 
1691   if (!natural) {
1692     switch (bs) {
1693     case 1:
1694       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_1_inplace;
1695       break;
1696     case 2:
1697       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_2;
1698       break;
1699     case 3:
1700       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_3;
1701       break;
1702     case 4:
1703       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_4;
1704       break;
1705     case 5:
1706       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_5;
1707       break;
1708     case 6:
1709       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_6;
1710       break;
1711     case 7:
1712       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_7;
1713       break;
1714     default:
1715       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_N;
1716       break;
1717     }
1718   } else {
1719     switch (bs) {
1720     case 1:
1721       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_1_NaturalOrdering_inplace;
1722       break;
1723     case 2:
1724       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_2_NaturalOrdering;
1725       break;
1726     case 3:
1727       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_3_NaturalOrdering;
1728       break;
1729     case 4:
1730       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_4_NaturalOrdering;
1731       break;
1732     case 5:
1733       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_5_NaturalOrdering;
1734       break;
1735     case 6:
1736       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_6_NaturalOrdering;
1737       break;
1738     case 7:
1739       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_7_NaturalOrdering;
1740       break;
1741     default:
1742       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_N_NaturalOrdering;
1743       break;
1744     }
1745   }
1746   PetscFunctionReturn(PETSC_SUCCESS);
1747 }
1748 
1749 PETSC_INTERN PetscErrorCode MatConvert_SeqSBAIJ_SeqAIJ(Mat, MatType, MatReuse, Mat *);
1750 PETSC_INTERN PetscErrorCode MatConvert_SeqSBAIJ_SeqBAIJ(Mat, MatType, MatReuse, Mat *);
1751 static PetscErrorCode       MatFactorGetSolverType_petsc(Mat A, MatSolverType *type)
1752 {
1753   PetscFunctionBegin;
1754   *type = MATSOLVERPETSC;
1755   PetscFunctionReturn(PETSC_SUCCESS);
1756 }
1757 
1758 PETSC_INTERN PetscErrorCode MatGetFactor_seqsbaij_petsc(Mat A, MatFactorType ftype, Mat *B)
1759 {
1760   PetscInt n = A->rmap->n;
1761 
1762   PetscFunctionBegin;
1763 #if defined(PETSC_USE_COMPLEX)
1764   if ((ftype == MAT_FACTOR_CHOLESKY || ftype == MAT_FACTOR_ICC) && A->hermitian == PETSC_BOOL3_TRUE && A->symmetric != PETSC_BOOL3_TRUE) {
1765     PetscCall(PetscInfo(A, "Hermitian MAT_FACTOR_CHOLESKY or MAT_FACTOR_ICC are not supported. Use MAT_FACTOR_LU instead.\n"));
1766     *B = NULL;
1767     PetscFunctionReturn(PETSC_SUCCESS);
1768   }
1769 #endif
1770 
1771   PetscCall(MatCreate(PetscObjectComm((PetscObject)A), B));
1772   PetscCall(MatSetSizes(*B, n, n, n, n));
1773   if (ftype == MAT_FACTOR_CHOLESKY || ftype == MAT_FACTOR_ICC) {
1774     PetscCall(MatSetType(*B, MATSEQSBAIJ));
1775     PetscCall(MatSeqSBAIJSetPreallocation(*B, A->rmap->bs, MAT_SKIP_ALLOCATION, NULL));
1776 
1777     (*B)->ops->choleskyfactorsymbolic = MatCholeskyFactorSymbolic_SeqSBAIJ;
1778     (*B)->ops->iccfactorsymbolic      = MatICCFactorSymbolic_SeqSBAIJ;
1779     PetscCall(PetscStrallocpy(MATORDERINGNATURAL, (char **)&(*B)->preferredordering[MAT_FACTOR_CHOLESKY]));
1780     PetscCall(PetscStrallocpy(MATORDERINGNATURAL, (char **)&(*B)->preferredordering[MAT_FACTOR_ICC]));
1781   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Factor type not supported");
1782 
1783   (*B)->factortype     = ftype;
1784   (*B)->canuseordering = PETSC_TRUE;
1785   PetscCall(PetscFree((*B)->solvertype));
1786   PetscCall(PetscStrallocpy(MATSOLVERPETSC, &(*B)->solvertype));
1787   PetscCall(PetscObjectComposeFunction((PetscObject)*B, "MatFactorGetSolverType_C", MatFactorGetSolverType_petsc));
1788   PetscFunctionReturn(PETSC_SUCCESS);
1789 }
1790 
1791 /*@C
1792   MatSeqSBAIJGetArray - gives access to the array where the numerical data for a `MATSEQSBAIJ` matrix is stored
1793 
1794   Not Collective
1795 
1796   Input Parameter:
1797 . A - a `MATSEQSBAIJ` matrix
1798 
1799   Output Parameter:
1800 . array - pointer to the data
1801 
1802   Level: intermediate
1803 
1804 .seealso: [](ch_matrices), `Mat`, `MATSEQSBAIJ`, `MatSeqSBAIJRestoreArray()`, `MatSeqAIJGetArray()`, `MatSeqAIJRestoreArray()`
1805 @*/
1806 PetscErrorCode MatSeqSBAIJGetArray(Mat A, PetscScalar *array[])
1807 {
1808   PetscFunctionBegin;
1809   PetscUseMethod(A, "MatSeqSBAIJGetArray_C", (Mat, PetscScalar **), (A, array));
1810   PetscFunctionReturn(PETSC_SUCCESS);
1811 }
1812 
1813 /*@C
1814   MatSeqSBAIJRestoreArray - returns access to the array where the numerical data for a `MATSEQSBAIJ` matrix is stored obtained by `MatSeqSBAIJGetArray()`
1815 
1816   Not Collective
1817 
1818   Input Parameters:
1819 + A     - a `MATSEQSBAIJ` matrix
1820 - array - pointer to the data
1821 
1822   Level: intermediate
1823 
1824 .seealso: [](ch_matrices), `Mat`, `MATSEQSBAIJ`, `MatSeqSBAIJGetArray()`, `MatSeqAIJGetArray()`, `MatSeqAIJRestoreArray()`
1825 @*/
1826 PetscErrorCode MatSeqSBAIJRestoreArray(Mat A, PetscScalar *array[])
1827 {
1828   PetscFunctionBegin;
1829   PetscUseMethod(A, "MatSeqSBAIJRestoreArray_C", (Mat, PetscScalar **), (A, array));
1830   PetscFunctionReturn(PETSC_SUCCESS);
1831 }
1832 
1833 /*MC
1834   MATSEQSBAIJ - MATSEQSBAIJ = "seqsbaij" - A matrix type to be used for sequential symmetric block sparse matrices,
1835   based on block compressed sparse row format.  Only the upper triangular portion of the matrix is stored.
1836 
1837   For complex numbers by default this matrix is symmetric, NOT Hermitian symmetric. To make it Hermitian symmetric you
1838   can call `MatSetOption`(`Mat`, `MAT_HERMITIAN`).
1839 
1840   Options Database Key:
1841   . -mat_type seqsbaij - sets the matrix type to "seqsbaij" during a call to `MatSetFromOptions()`
1842 
1843   Level: beginner
1844 
1845   Notes:
1846   By default if you insert values into the lower triangular part of the matrix they are simply ignored (since they are not
1847   stored and it is assumed they symmetric to the upper triangular). If you call `MatSetOption`(`Mat`,`MAT_IGNORE_LOWER_TRIANGULAR`,`PETSC_FALSE`) or use
1848   the options database `-mat_ignore_lower_triangular` false it will generate an error if you try to set a value in the lower triangular portion.
1849 
1850   The number of rows in the matrix must be less than or equal to the number of columns
1851 
1852 .seealso: [](ch_matrices), `Mat`, `MATSEQSBAIJ`, `MatCreateSeqSBAIJ()`, `MatType`, `MATMPISBAIJ`
1853 M*/
1854 PETSC_EXTERN PetscErrorCode MatCreate_SeqSBAIJ(Mat B)
1855 {
1856   Mat_SeqSBAIJ *b;
1857   PetscMPIInt   size;
1858   PetscBool     no_unroll = PETSC_FALSE, no_inode = PETSC_FALSE;
1859 
1860   PetscFunctionBegin;
1861   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)B), &size));
1862   PetscCheck(size <= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Comm must be of size 1");
1863 
1864   PetscCall(PetscNew(&b));
1865   B->data   = (void *)b;
1866   B->ops[0] = MatOps_Values;
1867 
1868   B->ops->destroy    = MatDestroy_SeqSBAIJ;
1869   B->ops->view       = MatView_SeqSBAIJ;
1870   b->row             = NULL;
1871   b->icol            = NULL;
1872   b->reallocs        = 0;
1873   b->saved_values    = NULL;
1874   b->inode.limit     = 5;
1875   b->inode.max_limit = 5;
1876 
1877   b->roworiented        = PETSC_TRUE;
1878   b->nonew              = 0;
1879   b->diag               = NULL;
1880   b->solve_work         = NULL;
1881   b->mult_work          = NULL;
1882   B->spptr              = NULL;
1883   B->info.nz_unneeded   = (PetscReal)b->maxnz * b->bs2;
1884   b->keepnonzeropattern = PETSC_FALSE;
1885 
1886   b->inew    = NULL;
1887   b->jnew    = NULL;
1888   b->anew    = NULL;
1889   b->a2anew  = NULL;
1890   b->permute = PETSC_FALSE;
1891 
1892   b->ignore_ltriangular = PETSC_TRUE;
1893 
1894   PetscCall(PetscOptionsGetBool(((PetscObject)B)->options, ((PetscObject)B)->prefix, "-mat_ignore_lower_triangular", &b->ignore_ltriangular, NULL));
1895 
1896   b->getrow_utriangular = PETSC_FALSE;
1897 
1898   PetscCall(PetscOptionsGetBool(((PetscObject)B)->options, ((PetscObject)B)->prefix, "-mat_getrow_uppertriangular", &b->getrow_utriangular, NULL));
1899 
1900   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSeqSBAIJGetArray_C", MatSeqSBAIJGetArray_SeqSBAIJ));
1901   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSeqSBAIJRestoreArray_C", MatSeqSBAIJRestoreArray_SeqSBAIJ));
1902   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatStoreValues_C", MatStoreValues_SeqSBAIJ));
1903   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatRetrieveValues_C", MatRetrieveValues_SeqSBAIJ));
1904   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSeqSBAIJSetColumnIndices_C", MatSeqSBAIJSetColumnIndices_SeqSBAIJ));
1905   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqsbaij_seqaij_C", MatConvert_SeqSBAIJ_SeqAIJ));
1906   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqsbaij_seqbaij_C", MatConvert_SeqSBAIJ_SeqBAIJ));
1907   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSeqSBAIJSetPreallocation_C", MatSeqSBAIJSetPreallocation_SeqSBAIJ));
1908   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSeqSBAIJSetPreallocationCSR_C", MatSeqSBAIJSetPreallocationCSR_SeqSBAIJ));
1909 #if defined(PETSC_HAVE_ELEMENTAL)
1910   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqsbaij_elemental_C", MatConvert_SeqSBAIJ_Elemental));
1911 #endif
1912 #if defined(PETSC_HAVE_SCALAPACK)
1913   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqsbaij_scalapack_C", MatConvert_SBAIJ_ScaLAPACK));
1914 #endif
1915 
1916   B->symmetry_eternal            = PETSC_TRUE;
1917   B->structural_symmetry_eternal = PETSC_TRUE;
1918   B->symmetric                   = PETSC_BOOL3_TRUE;
1919   B->structurally_symmetric      = PETSC_BOOL3_TRUE;
1920 #if defined(PETSC_USE_COMPLEX)
1921   B->hermitian = PETSC_BOOL3_FALSE;
1922 #else
1923   B->hermitian = PETSC_BOOL3_TRUE;
1924 #endif
1925 
1926   PetscCall(PetscObjectChangeTypeName((PetscObject)B, MATSEQSBAIJ));
1927 
1928   PetscOptionsBegin(PetscObjectComm((PetscObject)B), ((PetscObject)B)->prefix, "Options for SEQSBAIJ matrix", "Mat");
1929   PetscCall(PetscOptionsBool("-mat_no_unroll", "Do not optimize for inodes (slower)", NULL, no_unroll, &no_unroll, NULL));
1930   if (no_unroll) PetscCall(PetscInfo(B, "Not using Inode routines due to -mat_no_unroll\n"));
1931   PetscCall(PetscOptionsBool("-mat_no_inode", "Do not optimize for inodes (slower)", NULL, no_inode, &no_inode, NULL));
1932   if (no_inode) PetscCall(PetscInfo(B, "Not using Inode routines due to -mat_no_inode\n"));
1933   PetscCall(PetscOptionsInt("-mat_inode_limit", "Do not use inodes larger then this value", NULL, b->inode.limit, &b->inode.limit, NULL));
1934   PetscOptionsEnd();
1935   b->inode.use = (PetscBool)(!(no_unroll || no_inode));
1936   if (b->inode.limit > b->inode.max_limit) b->inode.limit = b->inode.max_limit;
1937   PetscFunctionReturn(PETSC_SUCCESS);
1938 }
1939 
1940 /*@
1941   MatSeqSBAIJSetPreallocation - Creates a sparse symmetric matrix in block AIJ (block
1942   compressed row) `MATSEQSBAIJ` format.  For good matrix assembly performance the
1943   user should preallocate the matrix storage by setting the parameter `nz`
1944   (or the array `nnz`).
1945 
1946   Collective
1947 
1948   Input Parameters:
1949 + B   - the symmetric matrix
1950 . bs  - size of block, the blocks are ALWAYS square. One can use `MatSetBlockSizes()` to set a different row and column blocksize but the row
1951         blocksize always defines the size of the blocks. The column blocksize sets the blocksize of the vectors obtained with `MatCreateVecs()`
1952 . nz  - number of block nonzeros per block row (same for all rows)
1953 - nnz - array containing the number of block nonzeros in the upper triangular plus
1954         diagonal portion of each block (possibly different for each block row) or `NULL`
1955 
1956   Options Database Keys:
1957 + -mat_no_unroll  - uses code that does not unroll the loops in the block calculations (much slower)
1958 - -mat_block_size - size of the blocks to use (only works if a negative bs is passed in
1959 
1960   Level: intermediate
1961 
1962   Notes:
1963   Specify the preallocated storage with either `nz` or `nnz` (not both).
1964   Set `nz` = `PETSC_DEFAULT` and `nnz` = `NULL` for PETSc to control dynamic memory
1965   allocation.  See [Sparse Matrices](sec_matsparse) for details.
1966 
1967   You can call `MatGetInfo()` to get information on how effective the preallocation was;
1968   for example the fields mallocs,nz_allocated,nz_used,nz_unneeded;
1969   You can also run with the option `-info` and look for messages with the string
1970   malloc in them to see if additional memory allocation was needed.
1971 
1972   If the `nnz` parameter is given then the `nz` parameter is ignored
1973 
1974 .seealso: [](ch_matrices), `Mat`, [Sparse Matrices](sec_matsparse), `MATSEQSBAIJ`, `MatCreate()`, `MatCreateSeqAIJ()`, `MatSetValues()`, `MatCreateSBAIJ()`
1975 @*/
1976 PetscErrorCode MatSeqSBAIJSetPreallocation(Mat B, PetscInt bs, PetscInt nz, const PetscInt nnz[])
1977 {
1978   PetscFunctionBegin;
1979   PetscValidHeaderSpecific(B, MAT_CLASSID, 1);
1980   PetscValidType(B, 1);
1981   PetscValidLogicalCollectiveInt(B, bs, 2);
1982   PetscTryMethod(B, "MatSeqSBAIJSetPreallocation_C", (Mat, PetscInt, PetscInt, const PetscInt[]), (B, bs, nz, nnz));
1983   PetscFunctionReturn(PETSC_SUCCESS);
1984 }
1985 
1986 /*@C
1987   MatSeqSBAIJSetPreallocationCSR - Creates a sparse parallel matrix in `MATSEQSBAIJ` format using the given nonzero structure and (optional) numerical values
1988 
1989   Input Parameters:
1990 + B  - the matrix
1991 . bs - size of block, the blocks are ALWAYS square.
1992 . i  - the indices into `j` for the start of each local row (indices start with zero)
1993 . j  - the column indices for each local row (indices start with zero) these must be sorted for each row
1994 - v  - optional values in the matrix, use `NULL` if not provided
1995 
1996   Level: advanced
1997 
1998   Notes:
1999   The `i`,`j`,`v` values are COPIED with this routine; to avoid the copy use `MatCreateSeqSBAIJWithArrays()`
2000 
2001   The order of the entries in values is specified by the `MatOption` `MAT_ROW_ORIENTED`.  For example, C programs
2002   may want to use the default `MAT_ROW_ORIENTED` = `PETSC_TRUE` and use an array v[nnz][bs][bs] where the second index is
2003   over rows within a block and the last index is over columns within a block row.  Fortran programs will likely set
2004   `MAT_ROW_ORIENTED` = `PETSC_FALSE` and use a Fortran array v(bs,bs,nnz) in which the first index is over rows within a
2005   block column and the second index is over columns within a block.
2006 
2007   Any entries provided that lie below the diagonal are ignored
2008 
2009   Though this routine has Preallocation() in the name it also sets the exact nonzero locations of the matrix entries
2010   and usually the numerical values as well
2011 
2012 .seealso: [](ch_matrices), `Mat`, `MATSEQSBAIJ`, `MatCreate()`, `MatCreateSeqSBAIJ()`, `MatSetValuesBlocked()`, `MatSeqSBAIJSetPreallocation()`
2013 @*/
2014 PetscErrorCode MatSeqSBAIJSetPreallocationCSR(Mat B, PetscInt bs, const PetscInt i[], const PetscInt j[], const PetscScalar v[])
2015 {
2016   PetscFunctionBegin;
2017   PetscValidHeaderSpecific(B, MAT_CLASSID, 1);
2018   PetscValidType(B, 1);
2019   PetscValidLogicalCollectiveInt(B, bs, 2);
2020   PetscTryMethod(B, "MatSeqSBAIJSetPreallocationCSR_C", (Mat, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[]), (B, bs, i, j, v));
2021   PetscFunctionReturn(PETSC_SUCCESS);
2022 }
2023 
2024 /*@
2025   MatCreateSeqSBAIJ - Creates a sparse symmetric matrix in (block
2026   compressed row) `MATSEQSBAIJ` format.  For good matrix assembly performance the
2027   user should preallocate the matrix storage by setting the parameter `nz`
2028   (or the array `nnz`).
2029 
2030   Collective
2031 
2032   Input Parameters:
2033 + comm - MPI communicator, set to `PETSC_COMM_SELF`
2034 . bs   - size of block, the blocks are ALWAYS square. One can use `MatSetBlockSizes()` to set a different row and column blocksize but the row
2035           blocksize always defines the size of the blocks. The column blocksize sets the blocksize of the vectors obtained with MatCreateVecs()
2036 . m    - number of rows
2037 . n    - number of columns
2038 . nz   - number of block nonzeros per block row (same for all rows)
2039 - nnz  - array containing the number of block nonzeros in the upper triangular plus
2040          diagonal portion of each block (possibly different for each block row) or `NULL`
2041 
2042   Output Parameter:
2043 . A - the symmetric matrix
2044 
2045   Options Database Keys:
2046 + -mat_no_unroll  - uses code that does not unroll the loops in the block calculations (much slower)
2047 - -mat_block_size - size of the blocks to use
2048 
2049   Level: intermediate
2050 
2051   Notes:
2052   It is recommended that one use `MatCreateFromOptions()` or the `MatCreate()`, `MatSetType()` and/or `MatSetFromOptions()`,
2053   MatXXXXSetPreallocation() paradigm instead of this routine directly.
2054   [MatXXXXSetPreallocation() is, for example, `MatSeqAIJSetPreallocation()`]
2055 
2056   The number of rows and columns must be divisible by blocksize.
2057   This matrix type does not support complex Hermitian operation.
2058 
2059   Specify the preallocated storage with either `nz` or `nnz` (not both).
2060   Set `nz` = `PETSC_DEFAULT` and `nnz` = `NULL` for PETSc to control dynamic memory
2061   allocation.  See [Sparse Matrices](sec_matsparse) for details.
2062 
2063   If the `nnz` parameter is given then the `nz` parameter is ignored
2064 
2065 .seealso: [](ch_matrices), `Mat`, [Sparse Matrices](sec_matsparse), `MATSEQSBAIJ`, `MatCreate()`, `MatCreateSeqAIJ()`, `MatSetValues()`, `MatCreateSBAIJ()`
2066 @*/
2067 PetscErrorCode MatCreateSeqSBAIJ(MPI_Comm comm, PetscInt bs, PetscInt m, PetscInt n, PetscInt nz, const PetscInt nnz[], Mat *A)
2068 {
2069   PetscFunctionBegin;
2070   PetscCall(MatCreate(comm, A));
2071   PetscCall(MatSetSizes(*A, m, n, m, n));
2072   PetscCall(MatSetType(*A, MATSEQSBAIJ));
2073   PetscCall(MatSeqSBAIJSetPreallocation(*A, bs, nz, (PetscInt *)nnz));
2074   PetscFunctionReturn(PETSC_SUCCESS);
2075 }
2076 
2077 PetscErrorCode MatDuplicate_SeqSBAIJ(Mat A, MatDuplicateOption cpvalues, Mat *B)
2078 {
2079   Mat           C;
2080   Mat_SeqSBAIJ *c, *a  = (Mat_SeqSBAIJ *)A->data;
2081   PetscInt      i, mbs = a->mbs, nz = a->nz, bs2 = a->bs2;
2082 
2083   PetscFunctionBegin;
2084   PetscCheck(A->assembled, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONGSTATE, "Cannot duplicate unassembled matrix");
2085   PetscCheck(a->i[mbs] == nz, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Corrupt matrix");
2086 
2087   *B = NULL;
2088   PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &C));
2089   PetscCall(MatSetSizes(C, A->rmap->N, A->cmap->n, A->rmap->N, A->cmap->n));
2090   PetscCall(MatSetBlockSizesFromMats(C, A, A));
2091   PetscCall(MatSetType(C, MATSEQSBAIJ));
2092   c = (Mat_SeqSBAIJ *)C->data;
2093 
2094   C->preallocated       = PETSC_TRUE;
2095   C->factortype         = A->factortype;
2096   c->row                = NULL;
2097   c->icol               = NULL;
2098   c->saved_values       = NULL;
2099   c->keepnonzeropattern = a->keepnonzeropattern;
2100   C->assembled          = PETSC_TRUE;
2101 
2102   PetscCall(PetscLayoutReference(A->rmap, &C->rmap));
2103   PetscCall(PetscLayoutReference(A->cmap, &C->cmap));
2104   c->bs2 = a->bs2;
2105   c->mbs = a->mbs;
2106   c->nbs = a->nbs;
2107 
2108   if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
2109     c->imax           = a->imax;
2110     c->ilen           = a->ilen;
2111     c->free_imax_ilen = PETSC_FALSE;
2112   } else {
2113     PetscCall(PetscMalloc2(mbs + 1, &c->imax, mbs + 1, &c->ilen));
2114     for (i = 0; i < mbs; i++) {
2115       c->imax[i] = a->imax[i];
2116       c->ilen[i] = a->ilen[i];
2117     }
2118     c->free_imax_ilen = PETSC_TRUE;
2119   }
2120 
2121   /* allocate the matrix space */
2122   PetscCall(PetscShmgetAllocateArray(bs2 * nz, sizeof(PetscScalar), (void **)&c->a));
2123   c->free_a = PETSC_TRUE;
2124   if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
2125     PetscCall(PetscArrayzero(c->a, bs2 * nz));
2126     c->i       = a->i;
2127     c->j       = a->j;
2128     c->free_ij = PETSC_FALSE;
2129     c->parent  = A;
2130     PetscCall(PetscObjectReference((PetscObject)A));
2131     PetscCall(MatSetOption(A, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
2132     PetscCall(MatSetOption(C, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
2133   } else {
2134     PetscCall(PetscShmgetAllocateArray(nz, sizeof(PetscInt), (void **)&c->j));
2135     PetscCall(PetscShmgetAllocateArray(mbs + 1, sizeof(PetscInt), (void **)&c->i));
2136     PetscCall(PetscArraycpy(c->i, a->i, mbs + 1));
2137     c->free_ij = PETSC_TRUE;
2138   }
2139   if (mbs > 0) {
2140     if (cpvalues != MAT_SHARE_NONZERO_PATTERN) PetscCall(PetscArraycpy(c->j, a->j, nz));
2141     if (cpvalues == MAT_COPY_VALUES) {
2142       PetscCall(PetscArraycpy(c->a, a->a, bs2 * nz));
2143     } else {
2144       PetscCall(PetscArrayzero(c->a, bs2 * nz));
2145     }
2146     if (a->jshort) {
2147       /* cannot share jshort, it is reallocated in MatAssemblyEnd_SeqSBAIJ() */
2148       /* if the parent matrix is reassembled, this child matrix will never notice */
2149       PetscCall(PetscMalloc1(nz, &c->jshort));
2150       PetscCall(PetscArraycpy(c->jshort, a->jshort, nz));
2151 
2152       c->free_jshort = PETSC_TRUE;
2153     }
2154   }
2155 
2156   c->roworiented = a->roworiented;
2157   c->nonew       = a->nonew;
2158 
2159   if (a->diag) {
2160     if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
2161       c->diag      = a->diag;
2162       c->free_diag = PETSC_FALSE;
2163     } else {
2164       PetscCall(PetscMalloc1(mbs, &c->diag));
2165       for (i = 0; i < mbs; i++) c->diag[i] = a->diag[i];
2166       c->free_diag = PETSC_TRUE;
2167     }
2168   }
2169   c->nz         = a->nz;
2170   c->maxnz      = a->nz; /* Since we allocate exactly the right amount */
2171   c->solve_work = NULL;
2172   c->mult_work  = NULL;
2173 
2174   *B = C;
2175   PetscCall(PetscFunctionListDuplicate(((PetscObject)A)->qlist, &((PetscObject)C)->qlist));
2176   PetscFunctionReturn(PETSC_SUCCESS);
2177 }
2178 
2179 /* Used for both SeqBAIJ and SeqSBAIJ matrices */
2180 #define MatLoad_SeqSBAIJ_Binary MatLoad_SeqBAIJ_Binary
2181 
2182 PetscErrorCode MatLoad_SeqSBAIJ(Mat mat, PetscViewer viewer)
2183 {
2184   PetscBool isbinary;
2185 
2186   PetscFunctionBegin;
2187   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
2188   PetscCheck(isbinary, PetscObjectComm((PetscObject)viewer), PETSC_ERR_SUP, "Viewer type %s not yet supported for reading %s matrices", ((PetscObject)viewer)->type_name, ((PetscObject)mat)->type_name);
2189   PetscCall(MatLoad_SeqSBAIJ_Binary(mat, viewer));
2190   PetscFunctionReturn(PETSC_SUCCESS);
2191 }
2192 
2193 /*@
2194   MatCreateSeqSBAIJWithArrays - Creates an sequential `MATSEQSBAIJ` matrix using matrix elements
2195   (upper triangular entries in CSR format) provided by the user.
2196 
2197   Collective
2198 
2199   Input Parameters:
2200 + comm - must be an MPI communicator of size 1
2201 . bs   - size of block
2202 . m    - number of rows
2203 . n    - number of columns
2204 . i    - row indices; that is i[0] = 0, i[row] = i[row-1] + number of block elements in that row block row of the matrix
2205 . j    - column indices
2206 - a    - matrix values
2207 
2208   Output Parameter:
2209 . mat - the matrix
2210 
2211   Level: advanced
2212 
2213   Notes:
2214   The `i`, `j`, and `a` arrays are not copied by this routine, the user must free these arrays
2215   once the matrix is destroyed
2216 
2217   You cannot set new nonzero locations into this matrix, that will generate an error.
2218 
2219   The `i` and `j` indices are 0 based
2220 
2221   When block size is greater than 1 the matrix values must be stored using the `MATSBAIJ` storage format. For block size of 1
2222   it is the regular CSR format excluding the lower triangular elements.
2223 
2224 .seealso: [](ch_matrices), `Mat`, `MATSEQSBAIJ`, `MatCreate()`, `MatCreateSBAIJ()`, `MatCreateSeqSBAIJ()`
2225 @*/
2226 PetscErrorCode MatCreateSeqSBAIJWithArrays(MPI_Comm comm, PetscInt bs, PetscInt m, PetscInt n, PetscInt i[], PetscInt j[], PetscScalar a[], Mat *mat)
2227 {
2228   PetscInt      ii;
2229   Mat_SeqSBAIJ *sbaij;
2230 
2231   PetscFunctionBegin;
2232   PetscCheck(bs == 1, PETSC_COMM_SELF, PETSC_ERR_SUP, "block size %" PetscInt_FMT " > 1 is not supported yet", bs);
2233   PetscCheck(m == 0 || i[0] == 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "i (row indices) must start with 0");
2234 
2235   PetscCall(MatCreate(comm, mat));
2236   PetscCall(MatSetSizes(*mat, m, n, m, n));
2237   PetscCall(MatSetType(*mat, MATSEQSBAIJ));
2238   PetscCall(MatSeqSBAIJSetPreallocation(*mat, bs, MAT_SKIP_ALLOCATION, NULL));
2239   sbaij = (Mat_SeqSBAIJ *)(*mat)->data;
2240   PetscCall(PetscMalloc2(m, &sbaij->imax, m, &sbaij->ilen));
2241 
2242   sbaij->i = i;
2243   sbaij->j = j;
2244   sbaij->a = a;
2245 
2246   sbaij->nonew          = -1; /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
2247   sbaij->free_a         = PETSC_FALSE;
2248   sbaij->free_ij        = PETSC_FALSE;
2249   sbaij->free_imax_ilen = PETSC_TRUE;
2250 
2251   for (ii = 0; ii < m; ii++) {
2252     sbaij->ilen[ii] = sbaij->imax[ii] = i[ii + 1] - i[ii];
2253     PetscCheck(i[ii + 1] >= i[ii], 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]);
2254   }
2255   if (PetscDefined(USE_DEBUG)) {
2256     for (ii = 0; ii < sbaij->i[m]; ii++) {
2257       PetscCheck(j[ii] >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Negative column index at location = %" PetscInt_FMT " index = %" PetscInt_FMT, ii, j[ii]);
2258       PetscCheck(j[ii] < n, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Column index too large at location = %" PetscInt_FMT " index = %" PetscInt_FMT, ii, j[ii]);
2259     }
2260   }
2261 
2262   PetscCall(MatAssemblyBegin(*mat, MAT_FINAL_ASSEMBLY));
2263   PetscCall(MatAssemblyEnd(*mat, MAT_FINAL_ASSEMBLY));
2264   PetscFunctionReturn(PETSC_SUCCESS);
2265 }
2266 
2267 PetscErrorCode MatCreateMPIMatConcatenateSeqMat_SeqSBAIJ(MPI_Comm comm, Mat inmat, PetscInt n, MatReuse scall, Mat *outmat)
2268 {
2269   PetscFunctionBegin;
2270   PetscCall(MatCreateMPIMatConcatenateSeqMat_MPISBAIJ(comm, inmat, n, scall, outmat));
2271   PetscFunctionReturn(PETSC_SUCCESS);
2272 }
2273