Actual source code: sbaij.c

  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>
  6: #include <../src/mat/impls/sbaij/seq/sbaij.h>
  7: #include <petsc/private/kernels/blocktranspose.h>
  8: #include <petscblaslapack.h>

 10: #include <../src/mat/impls/sbaij/seq/relax.h>
 11: #define USESHORT
 12: #include <../src/mat/impls/sbaij/seq/relax.h>

 14: /* defines MatSetValues_Seq_Hash(), MatAssemblyEnd_Seq_Hash(), MatSetUp_Seq_Hash() */
 15: #define TYPE SBAIJ
 16: #define TYPE_SBAIJ
 17: #define TYPE_BS
 18: #include "../src/mat/impls/aij/seq/seqhashmatsetvalues.h"
 19: #undef TYPE_BS
 20: #define TYPE_BS _BS
 21: #define TYPE_BS_ON
 22: #include "../src/mat/impls/aij/seq/seqhashmatsetvalues.h"
 23: #undef TYPE_BS
 24: #undef TYPE_SBAIJ
 25: #include "../src/mat/impls/aij/seq/seqhashmat.h"
 26: #undef TYPE
 27: #undef TYPE_BS_ON

 29: #if PetscDefined(HAVE_ELEMENTAL)
 30: PETSC_INTERN PetscErrorCode MatConvert_SeqSBAIJ_Elemental(Mat, MatType, MatReuse, Mat *);
 31: #endif
 32: #if PetscDefined(HAVE_SCALAPACK) && (PetscDefined(USE_REAL_SINGLE) || PetscDefined(USE_REAL_DOUBLE))
 33: PETSC_INTERN PetscErrorCode MatConvert_SBAIJ_ScaLAPACK(Mat, MatType, MatReuse, Mat *);
 34: #endif
 35: PETSC_INTERN PetscErrorCode MatConvert_MPISBAIJ_Basic(Mat, MatType, MatReuse, Mat *);

 37: MatGetDiagonalMarkers(SeqSBAIJ, A->rmap->bs)

 39: static PetscErrorCode MatGetColumnReductions_SeqSBAIJ(Mat A, PetscInt type, PetscReal *reductions)
 40: {
 41:   Mat_SeqSBAIJ    *aij = (Mat_SeqSBAIJ *)A->data;
 42:   PetscInt         m, n, bs = A->rmap->bs, bs2 = aij->bs2;
 43:   PetscBool        implicit, hermitian;
 44:   const PetscInt  *ai = aij->i, *aj = aij->j;
 45:   const MatScalar *aa = aij->a;

 47:   PetscFunctionBegin;
 48:   PetscCall(MatGetSize(A, &m, &n));
 49:   PetscCheck(type == NORM_2 || type == NORM_1 || type == NORM_INFINITY || type == REDUCTION_SUM_REALPART || type == REDUCTION_MEAN_REALPART || type == REDUCTION_SUM_IMAGINARYPART || type == REDUCTION_MEAN_IMAGINARYPART, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Unknown reduction type");
 50:   PetscCall(PetscArrayzero(reductions, n));
 51:   implicit  = (PetscBool)(m == n && (A->symmetric == PETSC_BOOL3_TRUE || A->hermitian == PETSC_BOOL3_TRUE));
 52:   hermitian = (PetscBool)(implicit && PetscDefined(USE_COMPLEX) && A->hermitian == PETSC_BOOL3_TRUE);
 53:   for (PetscInt i = 0; i < aij->mbs; i++) {
 54:     for (PetscInt k = ai[i]; k < ai[i + 1]; k++) {
 55:       const PetscBool offdiag = (PetscBool)(implicit && aj[k] != i);

 57:       for (PetscInt jb = 0; jb < bs; jb++) {
 58:         for (PetscInt ib = 0; ib < bs; ib++) {
 59:           const MatScalar value = aa[k * bs2 + jb * bs + ib];
 60:           const PetscInt  col = aj[k] * bs + jb, row = i * bs + ib;
 61:           PetscReal       reduction;

 63:           if (type == NORM_2) reduction = PetscAbsScalar(value * value);
 64:           else if (type == NORM_1 || type == NORM_INFINITY) reduction = PetscAbsScalar(value);
 65:           else if (type == REDUCTION_SUM_REALPART || type == REDUCTION_MEAN_REALPART) reduction = PetscRealPart(value);
 66:           else reduction = PetscImaginaryPart(value);
 67:           if (type == NORM_INFINITY) reductions[col] = PetscMax(reduction, reductions[col]);
 68:           else reductions[col] += reduction;
 69:           if (offdiag) {
 70:             if (hermitian && (type == REDUCTION_SUM_IMAGINARYPART || type == REDUCTION_MEAN_IMAGINARYPART)) reduction = -reduction;
 71:             if (type == NORM_INFINITY) reductions[row] = PetscMax(reduction, reductions[row]);
 72:             else reductions[row] += reduction;
 73:           }
 74:         }
 75:       }
 76:     }
 77:   }
 78:   if (type == NORM_2) {
 79:     for (PetscInt i = 0; i < n; i++) reductions[i] = PetscSqrtReal(reductions[i]);
 80:   } else if (type == REDUCTION_MEAN_REALPART || type == REDUCTION_MEAN_IMAGINARYPART) {
 81:     for (PetscInt i = 0; i < n; i++) reductions[i] /= m;
 82:   }
 83:   PetscFunctionReturn(PETSC_SUCCESS);
 84: }

 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;

 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:   }

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:     }

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];

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:     }

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: }

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: }

172: PetscErrorCode MatDestroy_SeqSBAIJ(Mat A)
173: {
174:   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ *)A->data;

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

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 PetscDefined(HAVE_ELEMENTAL)
217:   PetscCall(PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqsbaij_elemental_C", NULL));
218: #endif
219: #if PetscDefined(HAVE_SCALAPACK) && (PetscDefined(USE_REAL_SINGLE) || PetscDefined(USE_REAL_DOUBLE))
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: }

226: static PetscErrorCode MatSetOption_SeqSBAIJ(Mat A, MatOption op, PetscBool flg)
227: {
228:   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ *)A->data;

230:   PetscFunctionBegin;
231:   switch (op) {
232:   case MAT_ROW_ORIENTED:
233:     a->roworiented = flg;
234:     break;
235:   case MAT_KEEP_NONZERO_PATTERN:
236:     a->keepnonzeropattern = flg;
237:     break;
238:   case MAT_NEW_NONZERO_LOCATIONS:
239:     a->nonew = (flg ? 0 : 1);
240:     break;
241:   case MAT_NEW_NONZERO_LOCATION_ERR:
242:     a->nonew = (flg ? -1 : 0);
243:     break;
244:   case MAT_NEW_NONZERO_ALLOCATION_ERR:
245:     a->nonew = (flg ? -2 : 0);
246:     break;
247:   case MAT_UNUSED_NONZERO_LOCATION_ERR:
248:     a->nounused = (flg ? -1 : 0);
249:     break;
250:   case MAT_HERMITIAN:
251:     if (PetscDefined(USE_COMPLEX) && flg) { /* disable transpose ops */
252:       PetscInt bs;

254:       PetscCall(MatGetBlockSize(A, &bs));
255:       PetscCheck(bs <= 1, PETSC_COMM_SELF, PETSC_ERR_SUP, "No support for Hermitian with block size greater than 1");
256:       A->ops->multtranspose    = NULL;
257:       A->ops->multtransposeadd = NULL;
258:     }
259:     break;
260:   case MAT_SYMMETRIC:
261:   case MAT_SPD:
262:     if (PetscDefined(USE_COMPLEX) && flg) { /* An Hermitian and symmetric matrix has zero imaginary part (restore back transpose ops) */
263:       A->ops->multtranspose    = A->ops->mult;
264:       A->ops->multtransposeadd = A->ops->multadd;
265:     }
266:     break;
267:   case MAT_IGNORE_LOWER_TRIANGULAR:
268:   case MAT_ERROR_LOWER_TRIANGULAR:
269:     a->ignore_ltriangular = flg;
270:     break;
271:   case MAT_GETROW_UPPERTRIANGULAR:
272:     a->getrow_utriangular = flg;
273:     break;
274:   default:
275:     break;
276:   }
277:   PetscFunctionReturn(PETSC_SUCCESS);
278: }

280: PetscErrorCode MatGetRow_SeqSBAIJ(Mat A, PetscInt row, PetscInt *nz, PetscInt **idx, PetscScalar **v)
281: {
282:   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ *)A->data;

284:   PetscFunctionBegin;
285:   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()");

287:   /* Get the upper triangular part of the row */
288:   PetscCall(MatGetRow_SeqBAIJ_private(A, row, nz, idx, v, a->i, a->j, a->a));
289:   PetscFunctionReturn(PETSC_SUCCESS);
290: }

292: PetscErrorCode MatRestoreRow_SeqSBAIJ(Mat A, PetscInt row, PetscInt *nz, PetscInt **idx, PetscScalar **v)
293: {
294:   PetscFunctionBegin;
295:   if (idx) PetscCall(PetscFree(*idx));
296:   if (v) PetscCall(PetscFree(*v));
297:   PetscFunctionReturn(PETSC_SUCCESS);
298: }

300: static PetscErrorCode MatGetRowUpperTriangular_SeqSBAIJ(Mat A)
301: {
302:   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ *)A->data;

304:   PetscFunctionBegin;
305:   a->getrow_utriangular = PETSC_TRUE;
306:   PetscFunctionReturn(PETSC_SUCCESS);
307: }

309: static PetscErrorCode MatRestoreRowUpperTriangular_SeqSBAIJ(Mat A)
310: {
311:   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ *)A->data;

313:   PetscFunctionBegin;
314:   a->getrow_utriangular = PETSC_FALSE;
315:   PetscFunctionReturn(PETSC_SUCCESS);
316: }

318: static PetscErrorCode MatTranspose_SeqSBAIJ(Mat A, MatReuse reuse, Mat *B)
319: {
320:   PetscFunctionBegin;
321:   if (reuse == MAT_REUSE_MATRIX) PetscCall(MatTransposeCheckNonzeroState_Private(A, *B));
322:   if (reuse == MAT_INITIAL_MATRIX) {
323:     PetscCall(MatDuplicate(A, MAT_COPY_VALUES, B));
324:   } else if (reuse == MAT_REUSE_MATRIX) {
325:     PetscCall(MatCopy(A, *B, SAME_NONZERO_PATTERN));
326:   }
327:   PetscFunctionReturn(PETSC_SUCCESS);
328: }

330: static PetscErrorCode MatView_SeqSBAIJ_ASCII(Mat A, PetscViewer viewer)
331: {
332:   Mat_SeqSBAIJ     *a = (Mat_SeqSBAIJ *)A->data;
333:   PetscInt          i, j, bs = A->rmap->bs, k, l, bs2 = a->bs2;
334:   PetscViewerFormat format;
335:   const PetscInt   *diag;
336:   const char       *matname;

338:   PetscFunctionBegin;
339:   PetscCall(PetscViewerGetFormat(viewer, &format));
340:   if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
341:   } else if (format == PETSC_VIEWER_ASCII_MATLAB) {
342:     Mat aij;

344:     if (A->factortype && bs > 1) {
345:       PetscCall(PetscPrintf(PETSC_COMM_SELF, "Warning: matrix is factored with bs>1. MatView() with PETSC_VIEWER_ASCII_MATLAB is not supported and ignored!\n"));
346:       PetscFunctionReturn(PETSC_SUCCESS);
347:     }
348:     PetscCall(MatConvert(A, MATSEQAIJ, MAT_INITIAL_MATRIX, &aij));
349:     if (((PetscObject)A)->name) PetscCall(PetscObjectGetName((PetscObject)A, &matname));
350:     if (((PetscObject)A)->name) PetscCall(PetscObjectSetName((PetscObject)aij, matname));
351:     PetscCall(MatView_SeqAIJ(aij, viewer));
352:     PetscCall(MatDestroy(&aij));
353:   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
354:     Mat B;

356:     PetscCall(MatConvert(A, MATSEQAIJ, MAT_INITIAL_MATRIX, &B));
357:     if (((PetscObject)A)->name) PetscCall(PetscObjectGetName((PetscObject)A, &matname));
358:     if (((PetscObject)A)->name) PetscCall(PetscObjectSetName((PetscObject)B, matname));
359:     PetscCall(MatView_SeqAIJ(B, viewer));
360:     PetscCall(MatDestroy(&B));
361:   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
362:     PetscFunctionReturn(PETSC_SUCCESS);
363:   } else {
364:     PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_FALSE));
365:     if (A->factortype) { /* for factored matrix */
366:       PetscCheck(bs <= 1, PETSC_COMM_SELF, PETSC_ERR_SUP, "matrix is factored with bs>1. Not implemented yet");
367:       PetscCall(MatGetDiagonalMarkers_SeqSBAIJ(A, &diag, NULL));
368:       for (i = 0; i < a->mbs; i++) { /* for row block i */
369:         PetscCall(PetscViewerASCIIPrintf(viewer, "row %" PetscInt_FMT ":", i));
370:         /* diagonal entry */
371:         if (PetscDefined(USE_COMPLEX) && PetscImaginaryPart(a->a[diag[i]]) > 0.0) {
372:           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]])));
373:         } else if (PetscDefined(USE_COMPLEX) && PetscImaginaryPart(a->a[diag[i]]) < 0.0) {
374:           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]])));
375:         } else {
376:           PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g) ", a->j[diag[i]], (double)PetscRealPart(1.0 / a->a[diag[i]])));
377:         }
378:         /* off-diagonal entries */
379:         for (k = a->i[i]; k < a->i[i + 1] - 1; k++) {
380:           if (PetscDefined(USE_COMPLEX) && PetscImaginaryPart(a->a[k]) > 0.0) {
381:             PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g + %g i) ", bs * a->j[k], (double)PetscRealPart(a->a[k]), (double)PetscImaginaryPart(a->a[k])));
382:           } else if (PetscDefined(USE_COMPLEX) && PetscImaginaryPart(a->a[k]) < 0.0) {
383:             PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g - %g i) ", bs * a->j[k], (double)PetscRealPart(a->a[k]), -(double)PetscImaginaryPart(a->a[k])));
384:           } else {
385:             PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g) ", bs * a->j[k], (double)PetscRealPart(a->a[k])));
386:           }
387:         }
388:         PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
389:       }

391:     } else {                         /* for non-factored matrix */
392:       for (i = 0; i < a->mbs; i++) { /* for row block i */
393:         for (j = 0; j < bs; j++) {   /* for row bs*i + j */
394:           PetscCall(PetscViewerASCIIPrintf(viewer, "row %" PetscInt_FMT ":", i * bs + j));
395:           for (k = a->i[i]; k < a->i[i + 1]; k++) { /* for column block */
396:             for (l = 0; l < bs; l++) {              /* for column */
397:               if (PetscDefined(USE_COMPLEX) && PetscImaginaryPart(a->a[bs2 * k + l * bs + j]) > 0.0) {
398:                 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])));
399:               } else if (PetscDefined(USE_COMPLEX) && PetscImaginaryPart(a->a[bs2 * k + l * bs + j]) < 0.0) {
400:                 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])));
401:               } else {
402:                 PetscCall(PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g) ", bs * a->j[k] + l, (double)PetscRealPart(a->a[bs2 * k + l * bs + j])));
403:               }
404:             }
405:           }
406:           PetscCall(PetscViewerASCIIPrintf(viewer, "\n"));
407:         }
408:       }
409:     }
410:     PetscCall(PetscViewerASCIIUseTabs(viewer, PETSC_TRUE));
411:   }
412:   PetscCall(PetscViewerFlush(viewer));
413:   PetscFunctionReturn(PETSC_SUCCESS);
414: }

416: #include <petscdraw.h>
417: static PetscErrorCode MatView_SeqSBAIJ_Draw_Zoom(PetscDraw draw, void *Aa)
418: {
419:   Mat           A = (Mat)Aa;
420:   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ *)A->data;
421:   PetscInt      row, i, j, k, l, mbs = a->mbs, bs = A->rmap->bs, bs2 = a->bs2;
422:   PetscReal     xl, yl, xr, yr, x_l, x_r, y_l, y_r;
423:   MatScalar    *aa;
424:   PetscViewer   viewer;
425:   int           color;

427:   PetscFunctionBegin;
428:   PetscCall(PetscObjectQuery((PetscObject)A, "Zoomviewer", (PetscObject *)&viewer));
429:   PetscCall(PetscDrawGetCoordinates(draw, &xl, &yl, &xr, &yr));

431:   /* loop over matrix elements drawing boxes */

433:   PetscDrawCollectiveBegin(draw);
434:   PetscCall(PetscDrawString(draw, .3 * (xl + xr), .3 * (yl + yr), PETSC_DRAW_BLACK, "symmetric"));
435:   /* Blue for negative, Cyan for zero and  Red for positive */
436:   color = PETSC_DRAW_BLUE;
437:   for (i = 0, row = 0; i < mbs; i++, row += bs) {
438:     for (j = a->i[i]; j < a->i[i + 1]; j++) {
439:       y_l = A->rmap->N - row - 1.0;
440:       y_r = y_l + 1.0;
441:       x_l = a->j[j] * bs;
442:       x_r = x_l + 1.0;
443:       aa  = a->a + j * bs2;
444:       for (k = 0; k < bs; k++) {
445:         for (l = 0; l < bs; l++) {
446:           if (PetscRealPart(*aa++) >= 0.) continue;
447:           PetscCall(PetscDrawRectangle(draw, x_l + k, y_l - l, x_r + k, y_r - l, color, color, color, color));
448:         }
449:       }
450:     }
451:   }
452:   color = PETSC_DRAW_CYAN;
453:   for (i = 0, row = 0; i < mbs; i++, row += bs) {
454:     for (j = a->i[i]; j < a->i[i + 1]; j++) {
455:       y_l = A->rmap->N - row - 1.0;
456:       y_r = y_l + 1.0;
457:       x_l = a->j[j] * bs;
458:       x_r = x_l + 1.0;
459:       aa  = a->a + j * bs2;
460:       for (k = 0; k < bs; k++) {
461:         for (l = 0; l < bs; l++) {
462:           if (PetscRealPart(*aa++) != 0.) continue;
463:           PetscCall(PetscDrawRectangle(draw, x_l + k, y_l - l, x_r + k, y_r - l, color, color, color, color));
464:         }
465:       }
466:     }
467:   }
468:   color = PETSC_DRAW_RED;
469:   for (i = 0, row = 0; i < mbs; i++, row += bs) {
470:     for (j = a->i[i]; j < a->i[i + 1]; j++) {
471:       y_l = A->rmap->N - row - 1.0;
472:       y_r = y_l + 1.0;
473:       x_l = a->j[j] * bs;
474:       x_r = x_l + 1.0;
475:       aa  = a->a + j * bs2;
476:       for (k = 0; k < bs; k++) {
477:         for (l = 0; l < bs; l++) {
478:           if (PetscRealPart(*aa++) <= 0.) continue;
479:           PetscCall(PetscDrawRectangle(draw, x_l + k, y_l - l, x_r + k, y_r - l, color, color, color, color));
480:         }
481:       }
482:     }
483:   }
484:   PetscDrawCollectiveEnd(draw);
485:   PetscFunctionReturn(PETSC_SUCCESS);
486: }

488: static PetscErrorCode MatView_SeqSBAIJ_Draw(Mat A, PetscViewer viewer)
489: {
490:   PetscReal xl, yl, xr, yr, w, h;
491:   PetscDraw draw;
492:   PetscBool isnull;

494:   PetscFunctionBegin;
495:   PetscCall(PetscViewerDrawGetDraw(viewer, 0, &draw));
496:   PetscCall(PetscDrawIsNull(draw, &isnull));
497:   if (isnull) PetscFunctionReturn(PETSC_SUCCESS);

499:   xr = A->rmap->N;
500:   yr = A->rmap->N;
501:   h  = yr / 10.0;
502:   w  = xr / 10.0;
503:   xr += w;
504:   yr += h;
505:   xl = -w;
506:   yl = -h;
507:   PetscCall(PetscDrawSetCoordinates(draw, xl, yl, xr, yr));
508:   PetscCall(PetscObjectCompose((PetscObject)A, "Zoomviewer", (PetscObject)viewer));
509:   PetscCall(PetscDrawZoom(draw, MatView_SeqSBAIJ_Draw_Zoom, A));
510:   PetscCall(PetscObjectCompose((PetscObject)A, "Zoomviewer", NULL));
511:   PetscCall(PetscDrawSave(draw));
512:   PetscFunctionReturn(PETSC_SUCCESS);
513: }

515: /* Used for both MPIBAIJ and MPISBAIJ matrices */
516: #define MatView_SeqSBAIJ_Binary MatView_SeqBAIJ_Binary

518: PetscErrorCode MatView_SeqSBAIJ(Mat A, PetscViewer viewer)
519: {
520:   PetscBool isascii, isbinary, isdraw;

522:   PetscFunctionBegin;
523:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &isascii));
524:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
525:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw));
526:   if (isascii) {
527:     PetscCall(MatView_SeqSBAIJ_ASCII(A, viewer));
528:   } else if (isbinary) {
529:     PetscCall(MatView_SeqSBAIJ_Binary(A, viewer));
530:   } else if (isdraw) {
531:     PetscCall(MatView_SeqSBAIJ_Draw(A, viewer));
532:   } else {
533:     Mat         B;
534:     const char *matname;
535:     PetscCall(MatConvert(A, MATSEQAIJ, MAT_INITIAL_MATRIX, &B));
536:     if (((PetscObject)A)->name) PetscCall(PetscObjectGetName((PetscObject)A, &matname));
537:     if (((PetscObject)A)->name) PetscCall(PetscObjectSetName((PetscObject)B, matname));
538:     PetscCall(MatView(B, viewer));
539:     PetscCall(MatDestroy(&B));
540:   }
541:   PetscFunctionReturn(PETSC_SUCCESS);
542: }

544: PetscErrorCode MatGetValues_SeqSBAIJ(Mat A, PetscInt m, const PetscInt im[], PetscInt n, const PetscInt in[], PetscScalar v[])
545: {
546:   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ *)A->data;
547:   PetscInt     *rp, k, low, high, t, row, nrow, i, col, l, *aj = a->j;
548:   PetscInt     *ai = a->i, *ailen = a->ilen;
549:   PetscInt      brow, bcol, ridx, cidx, bs = A->rmap->bs, bs2 = a->bs2;
550:   MatScalar    *ap, *aa = a->a;
551:   PetscBool     roworiented = a->roworiented;
552:   PetscScalar  *value;

554:   PetscFunctionBegin;
555:   for (k = 0; k < m; k++) { /* loop over rows */
556:     row = im[k];
557:     if (row < 0) continue; /* negative row */
558:     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);
559:     brow = row / bs;
560:     rp   = aj + ai[brow];
561:     ap   = aa + bs2 * ai[brow];
562:     nrow = ailen[brow];
563:     for (l = 0; l < n; l++) {  /* loop over columns */
564:       if (in[l] < 0) continue; /* negative column */
565:       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);
566:       value = roworiented ? &v[l + k * n] : &v[k + l * m];
567:       col   = in[l];
568:       bcol  = col / bs;
569:       cidx  = col % bs;
570:       ridx  = row % bs;
571:       high  = nrow;
572:       low   = 0; /* assume unsorted */
573:       while (high - low > 5) {
574:         t = (low + high) / 2;
575:         if (rp[t] > bcol) high = t;
576:         else low = t;
577:       }
578:       for (i = low; i < high; i++) {
579:         if (rp[i] > bcol) break;
580:         if (rp[i] == bcol) {
581:           *value = ap[bs2 * i + bs * cidx + ridx];
582:           goto finished;
583:         }
584:       }
585:       *value = 0.0;
586:     finished:;
587:     }
588:   }
589:   PetscFunctionReturn(PETSC_SUCCESS);
590: }

592: static PetscErrorCode MatPermute_SeqSBAIJ(Mat A, IS rowp, IS colp, Mat *B)
593: {
594:   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ *)A->data, *b = NULL;
595:   Mat_SeqBAIJ    *c = NULL;
596:   Mat             C;
597:   IS              browp, bcolp;
598:   const PetscInt *row, *col;
599:   PetscInt       *irow, *icol, *lens, *bi, *bj, *bilen;
600:   MatScalar      *ba, *work;
601:   PetscInt        mbs, nbs, bs = A->rmap->bs, bs2 = a->bs2;
602:   PetscBool       same = (PetscBool)(rowp == colp), implicit = (PetscBool)(A->rmap->N == A->cmap->N && (A->symmetric == PETSC_BOOL3_TRUE || A->hermitian == PETSC_BOOL3_TRUE)), hermitian;

604:   PetscFunctionBegin;
605:   if (same == PETSC_FALSE) PetscCall(ISEqualUnsorted(rowp, colp, &same));
606:   hermitian = (PetscBool)(implicit == PETSC_TRUE && PetscDefined(USE_COMPLEX) && A->hermitian == PETSC_BOOL3_TRUE);
607:   same      = (PetscBool)(implicit == PETSC_TRUE && same == PETSC_TRUE);
608:   PetscCall(ISCompressIndicesGeneral(A->rmap->N, A->rmap->n, bs, 1, &rowp, &browp));
609:   if (rowp == colp) {
610:     bcolp = browp;
611:     PetscCall(PetscObjectReference((PetscObject)bcolp));
612:   } else PetscCall(ISCompressIndicesGeneral(A->cmap->N, A->cmap->n, bs, 1, &colp, &bcolp));
613:   PetscCall(ISGetLocalSize(browp, &mbs));
614:   PetscCall(ISGetLocalSize(bcolp, &nbs));
615:   PetscCheck(mbs == a->mbs && nbs == a->nbs, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Row and column index sets must be block permutations");
616:   PetscCall(ISGetIndices(browp, &row));
617:   PetscCall(ISGetIndices(bcolp, &col));
618:   PetscCall(PetscMalloc2(mbs, &irow, nbs, &icol));
619:   for (PetscInt i = 0; i < mbs; i++) {
620:     PetscCheck(row[i] >= 0 && row[i] < mbs, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Row index set is not a block permutation");
621:     irow[row[i]] = i;
622:   }
623:   for (PetscInt i = 0; i < nbs; i++) {
624:     PetscCheck(col[i] >= 0 && col[i] < nbs, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Column index set is not a block permutation");
625:     icol[col[i]] = i;
626:   }
627:   PetscCall(PetscCalloc1(mbs, &lens));
628:   for (PetscInt i = 0; i < a->mbs; i++) {
629:     for (PetscInt k = a->i[i]; k < a->i[i + 1]; k++) {
630:       const PetscInt j = a->j[k];

632:       if (same == PETSC_TRUE) lens[PetscMin(irow[i], icol[j])]++;
633:       else {
634:         lens[irow[i]]++;
635:         if (implicit == PETSC_TRUE && i != j) lens[irow[j]]++;
636:       }
637:     }
638:   }
639:   PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &C));
640:   PetscCall(MatSetSizes(C, mbs * bs, nbs * bs, mbs * bs, nbs * bs));
641:   PetscCall(MatSetType(C, same == PETSC_TRUE ? ((PetscObject)A)->type_name : MATSEQBAIJ));
642:   if (same == PETSC_TRUE) {
643:     PetscCall(MatSeqSBAIJSetPreallocation(C, bs, 0, lens));
644:     b     = (Mat_SeqSBAIJ *)C->data;
645:     bi    = b->i;
646:     bj    = b->j;
647:     ba    = b->a;
648:     bilen = b->ilen;
649:   } else {
650:     PetscCall(MatSeqBAIJSetPreallocation(C, bs, 0, lens));
651:     c     = (Mat_SeqBAIJ *)C->data;
652:     bi    = c->i;
653:     bj    = c->j;
654:     ba    = c->a;
655:     bilen = c->ilen;
656:   }
657:   PetscCall(PetscFree(lens));
658:   for (PetscInt i = 0; i < a->mbs; i++) {
659:     for (PetscInt k = a->i[i]; k < a->i[i + 1]; k++) {
660:       const PetscInt j = a->j[k];
661:       PetscInt       r = irow[i], colidx = icol[j], pos;
662:       MatScalar     *block;

664:       if (same == PETSC_TRUE && r > colidx) {
665:         pos    = r;
666:         r      = colidx;
667:         colidx = pos;
668:       }
669:       pos     = bi[r] + bilen[r]++;
670:       bj[pos] = colidx;
671:       block   = ba + pos * bs2;
672:       PetscCall(PetscArraycpy(block, a->a + k * bs2, bs2));
673:       if (same == PETSC_TRUE && irow[i] > icol[j]) {
674:         PetscCall(PetscKernel_A_gets_transpose_A_N(block, bs));
675:         if (hermitian == PETSC_TRUE) {
676:           for (PetscInt q = 0; q < bs2; q++) block[q] = PetscConj(block[q]);
677:         }
678:       }
679:       if (same == PETSC_FALSE && implicit == PETSC_TRUE && i != j) {
680:         r       = irow[j];
681:         colidx  = icol[i];
682:         pos     = bi[r] + bilen[r]++;
683:         bj[pos] = colidx;
684:         block   = ba + pos * bs2;
685:         PetscCall(PetscArraycpy(block, a->a + k * bs2, bs2));
686:         PetscCall(PetscKernel_A_gets_transpose_A_N(block, bs));
687:         if (hermitian == PETSC_TRUE) {
688:           for (PetscInt q = 0; q < bs2; q++) block[q] = PetscConj(block[q]);
689:         }
690:       }
691:     }
692:   }
693:   PetscCall(PetscMalloc1(bs2, &work));
694:   for (PetscInt i = 0; i < mbs; i++) PetscCall(PetscSortIntWithDataArray(bilen[i], PetscSafePointerPlusOffset(bj, bi[i]), PetscSafePointerPlusOffset(ba, bi[i] * bs2), bs2 * sizeof(MatScalar), work));
695:   PetscCall(PetscFree(work));
696:   PetscCall(MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY));
697:   PetscCall(MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY));
698:   if (same == PETSC_TRUE) PetscCall(MatPropagateSymmetryOptions(A, C));
699:   PetscCall(PetscFree2(irow, icol));
700:   PetscCall(ISRestoreIndices(browp, &row));
701:   PetscCall(ISRestoreIndices(bcolp, &col));
702:   PetscCall(ISDestroy(&browp));
703:   PetscCall(ISDestroy(&bcolp));
704:   *B = C;
705:   PetscFunctionReturn(PETSC_SUCCESS);
706: }

708: PetscErrorCode MatSetValuesBlocked_SeqSBAIJ(Mat A, PetscInt m, const PetscInt im[], PetscInt n, const PetscInt in[], const PetscScalar v[], InsertMode is)
709: {
710:   Mat_SeqSBAIJ      *a = (Mat_SeqSBAIJ *)A->data;
711:   PetscInt          *rp, k, low, high, t, ii, jj, row, nrow, i, col, l, rmax, N, lastcol = -1;
712:   PetscInt          *imax = a->imax, *ai = a->i, *ailen = a->ilen;
713:   PetscInt          *aj = a->j, nonew = a->nonew, bs2 = a->bs2, bs = A->rmap->bs, stepval;
714:   PetscBool          roworiented = a->roworiented;
715:   const PetscScalar *value       = v;
716:   MatScalar         *ap, *aa = a->a, *bap;

718:   PetscFunctionBegin;
719:   if (roworiented) stepval = (n - 1) * bs;
720:   else stepval = (m - 1) * bs;
721:   for (k = 0; k < m; k++) { /* loop over added rows */
722:     row = im[k];
723:     if (row < 0) continue;
724:     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);
725:     rp   = aj + ai[row];
726:     ap   = aa + bs2 * ai[row];
727:     rmax = imax[row];
728:     nrow = ailen[row];
729:     low  = 0;
730:     high = nrow;
731:     for (l = 0; l < n; l++) { /* loop over added columns */
732:       if (in[l] < 0) continue;
733:       col = in[l];
734:       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);
735:       if (col < row) {
736:         PetscCheck(a->ignore_ltriangular, 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)");
737:         continue; /* ignore lower triangular block */
738:       }
739:       if (roworiented) value = v + k * (stepval + bs) * bs + l * bs;
740:       else value = v + l * (stepval + bs) * bs + k * bs;

742:       if (col <= lastcol) low = 0;
743:       else high = nrow;

745:       lastcol = col;
746:       while (high - low > 7) {
747:         t = (low + high) / 2;
748:         if (rp[t] > col) high = t;
749:         else low = t;
750:       }
751:       for (i = low; i < high; i++) {
752:         if (rp[i] > col) break;
753:         if (rp[i] == col) {
754:           bap = ap + bs2 * i;
755:           if (roworiented) {
756:             if (is == ADD_VALUES) {
757:               for (ii = 0; ii < bs; ii++, value += stepval) {
758:                 for (jj = ii; jj < bs2; jj += bs) bap[jj] += *value++;
759:               }
760:             } else {
761:               for (ii = 0; ii < bs; ii++, value += stepval) {
762:                 for (jj = ii; jj < bs2; jj += bs) bap[jj] = *value++;
763:               }
764:             }
765:           } else {
766:             if (is == ADD_VALUES) {
767:               for (ii = 0; ii < bs; ii++, value += stepval) {
768:                 for (jj = 0; jj < bs; jj++) *bap++ += *value++;
769:               }
770:             } else {
771:               for (ii = 0; ii < bs; ii++, value += stepval) {
772:                 for (jj = 0; jj < bs; jj++) *bap++ = *value++;
773:               }
774:             }
775:           }
776:           goto noinsert2;
777:         }
778:       }
779:       if (nonew == 1) goto noinsert2;
780:       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);
781:       MatSeqXAIJReallocateAIJ(A, a->mbs, bs2, nrow, row, col, rmax, aa, ai, aj, rp, ap, imax, nonew, MatScalar);
782:       N = nrow++ - 1;
783:       high++;
784:       /* shift up all the later entries in this row */
785:       PetscCall(PetscArraymove(rp + i + 1, rp + i, N - i + 1));
786:       PetscCall(PetscArraymove(ap + bs2 * (i + 1), ap + bs2 * i, bs2 * (N - i + 1)));
787:       PetscCall(PetscArrayzero(ap + bs2 * i, bs2));
788:       rp[i] = col;
789:       bap   = ap + bs2 * i;
790:       if (roworiented) {
791:         for (ii = 0; ii < bs; ii++, value += stepval) {
792:           for (jj = ii; jj < bs2; jj += bs) bap[jj] = *value++;
793:         }
794:       } else {
795:         for (ii = 0; ii < bs; ii++, value += stepval) {
796:           for (jj = 0; jj < bs; jj++) *bap++ = *value++;
797:         }
798:       }
799:     noinsert2:;
800:       low = i;
801:     }
802:     ailen[row] = nrow;
803:   }
804:   PetscFunctionReturn(PETSC_SUCCESS);
805: }

807: static PetscErrorCode MatAssemblyEnd_SeqSBAIJ(Mat A, MatAssemblyType mode)
808: {
809:   Mat_SeqSBAIJ *a      = (Mat_SeqSBAIJ *)A->data;
810:   PetscInt      fshift = 0, i, *ai = a->i, *aj = a->j, *imax = a->imax;
811:   PetscInt      m = A->rmap->N, *ip, N, *ailen = a->ilen;
812:   PetscInt      mbs = a->mbs, bs2 = a->bs2, rmax = 0;
813:   MatScalar    *aa = a->a, *ap;

815:   PetscFunctionBegin;
816:   if (mode == MAT_FLUSH_ASSEMBLY || (A->was_assembled && A->ass_nonzerostate == A->nonzerostate)) PetscFunctionReturn(PETSC_SUCCESS);

818:   if (m) rmax = ailen[0];
819:   for (i = 1; i < mbs; i++) {
820:     /* move each row back by the amount of empty slots (fshift) before it*/
821:     fshift += imax[i - 1] - ailen[i - 1];
822:     rmax = PetscMax(rmax, ailen[i]);
823:     if (fshift) {
824:       ip = aj + ai[i];
825:       ap = aa + bs2 * ai[i];
826:       N  = ailen[i];
827:       PetscCall(PetscArraymove(ip - fshift, ip, N));
828:       PetscCall(PetscArraymove(ap - bs2 * fshift, ap, bs2 * N));
829:     }
830:     ai[i] = ai[i - 1] + ailen[i - 1];
831:   }
832:   if (mbs) {
833:     fshift += imax[mbs - 1] - ailen[mbs - 1];
834:     ai[mbs] = ai[mbs - 1] + ailen[mbs - 1];
835:   }
836:   /* reset ilen and imax for each row */
837:   for (i = 0; i < mbs; i++) ailen[i] = imax[i] = ai[i + 1] - ai[i];
838:   a->nz = ai[mbs];

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

842:   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));
843:   PetscCall(PetscInfo(A, "Number of mallocs during MatSetValues is %" PetscInt_FMT "\n", a->reallocs));
844:   PetscCall(PetscInfo(A, "Most nonzeros blocks in any row is %" PetscInt_FMT "\n", rmax));

846:   A->info.mallocs += a->reallocs;
847:   a->reallocs         = 0;
848:   A->info.nz_unneeded = (PetscReal)fshift * bs2;
849:   a->rmax             = rmax;

851:   if (A->cmap->n < 65536 && A->cmap->bs == 1) {
852:     if (a->jshort && a->free_jshort) {
853:       /* when matrix data structure is changed, previous jshort must be replaced */
854:       PetscCall(PetscFree(a->jshort));
855:     }
856:     PetscCall(PetscMalloc1(a->i[A->rmap->n], &a->jshort));
857:     for (i = 0; i < a->i[A->rmap->n]; i++) a->jshort[i] = (short)a->j[i];
858:     A->ops->mult   = MatMult_SeqSBAIJ_1_ushort;
859:     A->ops->sor    = MatSOR_SeqSBAIJ_ushort;
860:     a->free_jshort = PETSC_TRUE;
861:   }
862:   PetscFunctionReturn(PETSC_SUCCESS);
863: }

865: /* Only add/insert a(i,j) with i<=j (blocks).
866:    Any a(i,j) with i>j input by user is ignored.
867: */

869: PetscErrorCode MatSetValues_SeqSBAIJ(Mat A, PetscInt m, const PetscInt im[], PetscInt n, const PetscInt in[], const PetscScalar v[], InsertMode is)
870: {
871:   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ *)A->data;
872:   PetscInt     *rp, k, low, high, t, ii, row, nrow, i, col, l, rmax, N, lastcol = -1;
873:   PetscInt     *imax = a->imax, *ai = a->i, *ailen = a->ilen, roworiented = a->roworiented;
874:   PetscInt     *aj = a->j, nonew = a->nonew, bs = A->rmap->bs, brow, bcol;
875:   PetscInt      ridx, cidx, bs2                 = a->bs2;
876:   MatScalar    *ap, value, *aa                  = a->a, *bap;

878:   PetscFunctionBegin;
879:   for (k = 0; k < m; k++) { /* loop over added rows */
880:     row  = im[k];           /* row number */
881:     brow = row / bs;        /* block row number */
882:     if (row < 0) continue;
883:     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);
884:     rp   = aj + ai[brow];       /*ptr to beginning of column value of the row block*/
885:     ap   = aa + bs2 * ai[brow]; /*ptr to beginning of element value of the row block*/
886:     rmax = imax[brow];          /* maximum space allocated for this row */
887:     nrow = ailen[brow];         /* actual length of this row */
888:     low  = 0;
889:     high = nrow;
890:     for (l = 0; l < n; l++) { /* loop over added columns */
891:       if (in[l] < 0) continue;
892:       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);
893:       col  = in[l];
894:       bcol = col / bs; /* block col number */

896:       if (brow > bcol) {
897:         PetscCheck(a->ignore_ltriangular, 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)");
898:         continue; /* ignore lower triangular values */
899:       }

901:       ridx = row % bs;
902:       cidx = col % bs; /*row and col index inside the block */
903:       if ((brow == bcol && ridx <= cidx) || (brow < bcol)) {
904:         /* element value a(k,l) */
905:         if (roworiented) value = v[l + k * n];
906:         else value = v[k + l * m];

908:         /* move pointer bap to a(k,l) quickly and add/insert value */
909:         if (col <= lastcol) low = 0;
910:         else high = nrow;

912:         lastcol = col;
913:         while (high - low > 7) {
914:           t = (low + high) / 2;
915:           if (rp[t] > bcol) high = t;
916:           else low = t;
917:         }
918:         for (i = low; i < high; i++) {
919:           if (rp[i] > bcol) break;
920:           if (rp[i] == bcol) {
921:             bap = ap + bs2 * i + bs * cidx + ridx;
922:             if (is == ADD_VALUES) *bap += value;
923:             else *bap = value;
924:             /* for diag block, add/insert its symmetric element a(cidx,ridx) */
925:             if (brow == bcol && ridx < cidx) {
926:               bap = ap + bs2 * i + bs * ridx + cidx;
927:               if (is == ADD_VALUES) *bap += value;
928:               else *bap = value;
929:             }
930:             goto noinsert1;
931:           }
932:         }

934:         if (nonew == 1) goto noinsert1;
935:         PetscCheck(nonew != -1, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Inserting a new nonzero (%" PetscInt_FMT ", %" PetscInt_FMT ") in the matrix", row, col);
936:         MatSeqXAIJReallocateAIJ(A, a->mbs, bs2, nrow, brow, bcol, rmax, aa, ai, aj, rp, ap, imax, nonew, MatScalar);

938:         N = nrow++ - 1;
939:         high++;
940:         /* shift up all the later entries in this row */
941:         PetscCall(PetscArraymove(rp + i + 1, rp + i, N - i + 1));
942:         PetscCall(PetscArraymove(ap + bs2 * (i + 1), ap + bs2 * i, bs2 * (N - i + 1)));
943:         PetscCall(PetscArrayzero(ap + bs2 * i, bs2));
944:         rp[i]                          = bcol;
945:         ap[bs2 * i + bs * cidx + ridx] = value;
946:         /* for diag block, add/insert its symmetric element a(cidx,ridx) */
947:         if (brow == bcol && ridx < cidx) ap[bs2 * i + bs * ridx + cidx] = value;
948:       noinsert1:;
949:         low = i;
950:       }
951:     } /* end of loop over added columns */
952:     ailen[brow] = nrow;
953:   } /* end of loop over added rows */
954:   PetscFunctionReturn(PETSC_SUCCESS);
955: }

957: static PetscErrorCode MatICCFactor_SeqSBAIJ(Mat inA, IS row, const MatFactorInfo *info)
958: {
959:   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ *)inA->data;
960:   Mat           outA;
961:   PetscBool     row_identity;

963:   PetscFunctionBegin;
964:   PetscCheck(info->levels == 0, PETSC_COMM_SELF, PETSC_ERR_SUP, "Only levels=0 is supported for in-place icc");
965:   PetscCall(ISIdentity(row, &row_identity));
966:   PetscCheck(row_identity, PETSC_COMM_SELF, PETSC_ERR_SUP, "Matrix reordering is not supported");
967:   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()! */

969:   outA = inA;
970:   PetscCall(PetscFree(inA->solvertype));
971:   PetscCall(PetscStrallocpy(MATSOLVERPETSC, &inA->solvertype));

973:   inA->factortype = MAT_FACTOR_ICC;
974:   PetscCall(MatSeqSBAIJSetNumericFactorization_inplace(inA, row_identity));

976:   PetscCall(PetscObjectReference((PetscObject)row));
977:   PetscCall(ISDestroy(&a->row));
978:   a->row = row;
979:   PetscCall(PetscObjectReference((PetscObject)row));
980:   PetscCall(ISDestroy(&a->col));
981:   a->col = row;

983:   /* Create the invert permutation so that it can be used in MatCholeskyFactorNumeric() */
984:   if (a->icol) PetscCall(ISInvertPermutation(row, PETSC_DECIDE, &a->icol));

986:   if (!a->solve_work) PetscCall(PetscMalloc1(inA->rmap->N + inA->rmap->bs, &a->solve_work));

988:   PetscCall(MatCholeskyFactorNumeric(outA, inA, info));
989:   PetscFunctionReturn(PETSC_SUCCESS);
990: }

992: static PetscErrorCode MatSeqSBAIJSetColumnIndices_SeqSBAIJ(Mat mat, PetscInt *indices)
993: {
994:   Mat_SeqSBAIJ *baij = (Mat_SeqSBAIJ *)mat->data;
995:   PetscInt      i, nz, n;

997:   PetscFunctionBegin;
998:   nz = baij->maxnz;
999:   n  = mat->cmap->n;
1000:   for (i = 0; i < nz; i++) baij->j[i] = indices[i];

1002:   baij->nz = nz;
1003:   for (i = 0; i < n; i++) baij->ilen[i] = baij->imax[i];

1005:   PetscCall(MatSetOption(mat, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
1006:   PetscFunctionReturn(PETSC_SUCCESS);
1007: }

1009: /*@
1010:   MatSeqSBAIJSetColumnIndices - Set the column indices for all the rows
1011:   in a `MATSEQSBAIJ` matrix.

1013:   Input Parameters:
1014: + mat     - the `MATSEQSBAIJ` matrix
1015: - indices - the column indices

1017:   Level: advanced

1019:   Notes:
1020:   This can be called if you have precomputed the nonzero structure of the
1021:   matrix and want to provide it to the matrix object to improve the performance
1022:   of the `MatSetValues()` operation.

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

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

1029: .seealso: [](ch_matrices), `Mat`, `MATSEQSBAIJ`, `MatCreateSeqSBAIJ`
1030: @*/
1031: PetscErrorCode MatSeqSBAIJSetColumnIndices(Mat mat, PetscInt *indices)
1032: {
1033:   PetscFunctionBegin;
1035:   PetscAssertPointer(indices, 2);
1036:   PetscUseMethod(mat, "MatSeqSBAIJSetColumnIndices_C", (Mat, PetscInt *), (mat, indices));
1037:   PetscFunctionReturn(PETSC_SUCCESS);
1038: }

1040: static PetscErrorCode MatCopy_SeqSBAIJ(Mat A, Mat B, MatStructure str)
1041: {
1042:   PetscBool isbaij;

1044:   PetscFunctionBegin;
1045:   PetscCall(PetscObjectTypeCompareAny((PetscObject)B, &isbaij, MATSEQSBAIJ, MATMPISBAIJ, ""));
1046:   PetscCheck(isbaij, PetscObjectComm((PetscObject)B), PETSC_ERR_SUP, "Not for matrix type %s", ((PetscObject)B)->type_name);
1047:   /* If the two matrices have the same copy implementation and nonzero pattern, use fast copy. */
1048:   if (str == SAME_NONZERO_PATTERN && A->ops->copy == B->ops->copy) {
1049:     Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ *)A->data;
1050:     Mat_SeqSBAIJ *b = (Mat_SeqSBAIJ *)B->data;

1052:     PetscCheck(a->i[a->mbs] == b->i[b->mbs], PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Number of nonzeros in two matrices are different");
1053:     PetscCheck(a->mbs == b->mbs, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Number of rows in two matrices are different");
1054:     PetscCheck(a->bs2 == b->bs2, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Different block size");
1055:     PetscCall(PetscArraycpy(b->a, a->a, a->bs2 * a->i[a->mbs]));
1056:     PetscCall(PetscObjectStateIncrease((PetscObject)B));
1057:   } else {
1058:     PetscCall(MatGetRowUpperTriangular(A));
1059:     PetscCall(MatCopy_Basic(A, B, str));
1060:     PetscCall(MatRestoreRowUpperTriangular(A));
1061:   }
1062:   PetscFunctionReturn(PETSC_SUCCESS);
1063: }

1065: static PetscErrorCode MatSeqSBAIJGetArray_SeqSBAIJ(Mat A, PetscScalar *array[])
1066: {
1067:   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ *)A->data;

1069:   PetscFunctionBegin;
1070:   *array = a->a;
1071:   PetscFunctionReturn(PETSC_SUCCESS);
1072: }

1074: static PetscErrorCode MatSeqSBAIJRestoreArray_SeqSBAIJ(Mat A, PetscScalar *array[])
1075: {
1076:   PetscFunctionBegin;
1077:   *array = NULL;
1078:   PetscFunctionReturn(PETSC_SUCCESS);
1079: }

1081: PetscErrorCode MatAXPYGetPreallocation_SeqSBAIJ(Mat Y, Mat X, PetscInt *nnz)
1082: {
1083:   PetscInt      bs = Y->rmap->bs, mbs = Y->rmap->N / bs;
1084:   Mat_SeqSBAIJ *x = (Mat_SeqSBAIJ *)X->data;
1085:   Mat_SeqSBAIJ *y = (Mat_SeqSBAIJ *)Y->data;

1087:   PetscFunctionBegin;
1088:   /* Set the number of nonzeros in the new matrix */
1089:   PetscCall(MatAXPYGetPreallocation_SeqX_private(mbs, x->i, x->j, y->i, y->j, nnz));
1090:   PetscFunctionReturn(PETSC_SUCCESS);
1091: }

1093: static PetscErrorCode MatAXPY_SeqSBAIJ(Mat Y, PetscScalar a, Mat X, MatStructure str)
1094: {
1095:   Mat_SeqSBAIJ *x = (Mat_SeqSBAIJ *)X->data, *y = (Mat_SeqSBAIJ *)Y->data;
1096:   PetscInt      bs = Y->rmap->bs, bs2 = bs * bs;
1097:   PetscBLASInt  one = 1;

1099:   PetscFunctionBegin;
1100:   if (str == UNKNOWN_NONZERO_PATTERN || (PetscDefined(USE_DEBUG) && str == SAME_NONZERO_PATTERN)) {
1101:     PetscBool e = x->nz == y->nz && x->mbs == y->mbs ? PETSC_TRUE : PETSC_FALSE;
1102:     if (e) {
1103:       PetscCall(PetscArraycmp(x->i, y->i, x->mbs + 1, &e));
1104:       if (e) {
1105:         PetscCall(PetscArraycmp(x->j, y->j, x->i[x->mbs], &e));
1106:         if (e) str = SAME_NONZERO_PATTERN;
1107:       }
1108:     }
1109:     if (!e) PetscCheck(str != SAME_NONZERO_PATTERN, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "MatStructure is not SAME_NONZERO_PATTERN");
1110:   }
1111:   if (str == SAME_NONZERO_PATTERN) {
1112:     PetscScalar  alpha = a;
1113:     PetscBLASInt bnz;
1114:     PetscCall(PetscBLASIntCast(x->nz * bs2, &bnz));
1115:     PetscCallBLAS("BLASaxpy", BLASaxpy_(&bnz, &alpha, x->a, &one, y->a, &one));
1116:     PetscCall(PetscObjectStateIncrease((PetscObject)Y));
1117:   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
1118:     PetscCall(MatSetOption(X, MAT_GETROW_UPPERTRIANGULAR, PETSC_TRUE));
1119:     PetscCall(MatAXPY_Basic(Y, a, X, str));
1120:     PetscCall(MatSetOption(X, MAT_GETROW_UPPERTRIANGULAR, PETSC_FALSE));
1121:   } else {
1122:     Mat       B;
1123:     PetscInt *nnz;
1124:     PetscCheck(bs == X->rmap->bs, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Matrices must have same block size");
1125:     PetscCall(MatGetRowUpperTriangular(X));
1126:     PetscCall(MatGetRowUpperTriangular(Y));
1127:     PetscCall(PetscMalloc1(Y->rmap->N, &nnz));
1128:     PetscCall(MatCreate(PetscObjectComm((PetscObject)Y), &B));
1129:     PetscCall(PetscObjectSetName((PetscObject)B, ((PetscObject)Y)->name));
1130:     PetscCall(MatSetSizes(B, Y->rmap->n, Y->cmap->n, Y->rmap->N, Y->cmap->N));
1131:     PetscCall(MatSetBlockSizesFromMats(B, Y, Y));
1132:     PetscCall(MatSetType(B, ((PetscObject)Y)->type_name));
1133:     PetscCall(MatAXPYGetPreallocation_SeqSBAIJ(Y, X, nnz));
1134:     PetscCall(MatSeqSBAIJSetPreallocation(B, bs, 0, nnz));

1136:     PetscCall(MatAXPY_BasicWithPreallocation(B, Y, a, X, str));

1138:     PetscCall(MatHeaderMerge(Y, &B));
1139:     PetscCall(PetscFree(nnz));
1140:     PetscCall(MatRestoreRowUpperTriangular(X));
1141:     PetscCall(MatRestoreRowUpperTriangular(Y));
1142:   }
1143:   PetscFunctionReturn(PETSC_SUCCESS);
1144: }

1146: static PetscErrorCode MatIsStructurallySymmetric_SeqSBAIJ(Mat A, PetscBool *flg)
1147: {
1148:   PetscFunctionBegin;
1149:   *flg = PETSC_TRUE;
1150:   PetscFunctionReturn(PETSC_SUCCESS);
1151: }

1153: static PetscErrorCode MatConjugate_SeqSBAIJ(Mat A)
1154: {
1155:   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ *)A->data;
1156:   PetscInt      i, nz = a->bs2 * a->i[a->mbs];
1157:   MatScalar    *aa = a->a;

1159:   PetscFunctionBegin;
1160:   for (i = 0; i < nz; i++) aa[i] = PetscConj(aa[i]);
1161:   PetscFunctionReturn(PETSC_SUCCESS);
1162: }

1164: static PetscErrorCode MatRealPart_SeqSBAIJ(Mat A)
1165: {
1166:   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ *)A->data;
1167:   PetscInt      i, nz = a->bs2 * a->i[a->mbs];
1168:   MatScalar    *aa = a->a;

1170:   PetscFunctionBegin;
1171:   for (i = 0; i < nz; i++) aa[i] = PetscRealPart(aa[i]);
1172:   PetscFunctionReturn(PETSC_SUCCESS);
1173: }

1175: static PetscErrorCode MatImaginaryPart_SeqSBAIJ(Mat A)
1176: {
1177:   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ *)A->data;
1178:   PetscInt      i, nz = a->bs2 * a->i[a->mbs];
1179:   MatScalar    *aa = a->a;

1181:   PetscFunctionBegin;
1182:   for (i = 0; i < nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
1183:   PetscFunctionReturn(PETSC_SUCCESS);
1184: }

1186: static PetscErrorCode MatZeroRowsColumns_SeqSBAIJ(Mat A, PetscInt is_n, const PetscInt is_idx[], PetscScalar diag, Vec x, Vec b)
1187: {
1188:   Mat_SeqSBAIJ      *baij = (Mat_SeqSBAIJ *)A->data;
1189:   PetscInt           i, j, k, count;
1190:   PetscInt           bs = A->rmap->bs, bs2 = baij->bs2, row, col;
1191:   PetscScalar        zero = 0.0;
1192:   MatScalar         *aa;
1193:   const PetscScalar *xx;
1194:   PetscScalar       *bb;
1195:   PetscBool         *zeroed, vecs = PETSC_FALSE;

1197:   PetscFunctionBegin;
1198:   /* fix right-hand side if needed */
1199:   if (x && b) {
1200:     PetscCall(VecGetArrayRead(x, &xx));
1201:     PetscCall(VecGetArray(b, &bb));
1202:     vecs = PETSC_TRUE;
1203:   }

1205:   /* zero the columns */
1206:   PetscCall(PetscCalloc1(A->rmap->n, &zeroed));
1207:   for (i = 0; i < is_n; i++) {
1208:     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]);
1209:     zeroed[is_idx[i]] = PETSC_TRUE;
1210:   }
1211:   if (vecs) {
1212:     for (i = 0; i < A->rmap->N; i++) {
1213:       row = i / bs;
1214:       for (j = baij->i[row]; j < baij->i[row + 1]; j++) {
1215:         for (k = 0; k < bs; k++) {
1216:           col = bs * baij->j[j] + k;
1217:           if (col <= i) continue;
1218:           aa = baij->a + j * bs2 + (i % bs) + bs * k;
1219:           if (!zeroed[i] && zeroed[col]) bb[i] -= aa[0] * xx[col];
1220:           if (zeroed[i] && !zeroed[col]) bb[col] -= aa[0] * xx[i];
1221:         }
1222:       }
1223:     }
1224:     for (i = 0; i < is_n; i++) bb[is_idx[i]] = diag * xx[is_idx[i]];
1225:   }

1227:   for (i = 0; i < A->rmap->N; i++) {
1228:     if (!zeroed[i]) {
1229:       row = i / bs;
1230:       for (j = baij->i[row]; j < baij->i[row + 1]; j++) {
1231:         for (k = 0; k < bs; k++) {
1232:           col = bs * baij->j[j] + k;
1233:           if (zeroed[col]) {
1234:             aa    = baij->a + j * bs2 + (i % bs) + bs * k;
1235:             aa[0] = 0.0;
1236:           }
1237:         }
1238:       }
1239:     }
1240:   }
1241:   PetscCall(PetscFree(zeroed));
1242:   if (vecs) {
1243:     PetscCall(VecRestoreArrayRead(x, &xx));
1244:     PetscCall(VecRestoreArray(b, &bb));
1245:   }

1247:   /* zero the rows */
1248:   for (i = 0; i < is_n; i++) {
1249:     row   = is_idx[i];
1250:     count = (baij->i[row / bs + 1] - baij->i[row / bs]) * bs;
1251:     aa    = PetscSafePointerPlusOffset(baij->a, baij->i[row / bs] * bs2 + (row % bs));
1252:     for (k = 0; k < count; k++) {
1253:       aa[0] = zero;
1254:       aa += bs;
1255:     }
1256:     if (diag != 0.0) PetscUseTypeMethod(A, setvalues, 1, &row, 1, &row, &diag, INSERT_VALUES);
1257:   }
1258:   PetscCall(MatAssemblyEnd_SeqSBAIJ(A, MAT_FINAL_ASSEMBLY));
1259:   PetscFunctionReturn(PETSC_SUCCESS);
1260: }

1262: static PetscErrorCode MatShift_SeqSBAIJ(Mat Y, PetscScalar a)
1263: {
1264:   Mat_SeqSBAIJ *aij = (Mat_SeqSBAIJ *)Y->data;

1266:   PetscFunctionBegin;
1267:   if (!Y->preallocated || !aij->nz) PetscCall(MatSeqSBAIJSetPreallocation(Y, Y->rmap->bs, 1, NULL));
1268:   PetscCall(MatShift_Basic(Y, a));
1269:   PetscFunctionReturn(PETSC_SUCCESS);
1270: }

1272: PetscErrorCode MatEliminateZeros_SeqSBAIJ(Mat A, PetscBool keep)
1273: {
1274:   Mat_SeqSBAIJ *a      = (Mat_SeqSBAIJ *)A->data;
1275:   PetscInt      fshift = 0, fshift_prev = 0, i, *ai = a->i, *aj = a->j, *imax = a->imax, j, k;
1276:   PetscInt      m = A->rmap->N, *ailen = a->ilen;
1277:   PetscInt      mbs = a->mbs, bs2 = a->bs2, rmax = 0;
1278:   MatScalar    *aa = a->a, *ap;
1279:   PetscBool     zero;

1281:   PetscFunctionBegin;
1282:   PetscCheck(A->assembled, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Cannot eliminate zeros for unassembled matrix");
1283:   if (m) rmax = ailen[0];
1284:   for (i = 1, a->nonzerorowcnt = 0; i <= mbs; i++) {
1285:     for (k = ai[i - 1]; k < ai[i]; k++) {
1286:       zero = PETSC_TRUE;
1287:       ap   = aa + bs2 * k;
1288:       for (j = 0; j < bs2 && zero; j++) {
1289:         if (ap[j] != 0.0) zero = PETSC_FALSE;
1290:       }
1291:       if (zero && (aj[k] != i - 1 || !keep)) fshift++;
1292:       else {
1293:         if (zero && aj[k] == i - 1) PetscCall(PetscInfo(A, "Keep the diagonal block at row %" PetscInt_FMT "\n", i - 1));
1294:         aj[k - fshift] = aj[k];
1295:         PetscCall(PetscArraymove(ap - bs2 * fshift, ap, bs2));
1296:       }
1297:     }
1298:     ai[i - 1] -= fshift_prev;
1299:     fshift_prev  = fshift;
1300:     ailen[i - 1] = imax[i - 1] = ai[i] - fshift - ai[i - 1];
1301:     a->nonzerorowcnt += ((ai[i] - fshift - ai[i - 1]) > 0);
1302:     rmax = PetscMax(rmax, ailen[i - 1]);
1303:   }
1304:   if (fshift) {
1305:     if (mbs) {
1306:       ai[mbs] -= fshift;
1307:       a->nz = ai[mbs];
1308:     }
1309:     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));
1310:     A->nonzerostate++;
1311:     A->info.nz_unneeded += (PetscReal)fshift;
1312:     a->rmax = rmax;
1313:     PetscCall(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY));
1314:     PetscCall(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY));
1315:   }
1316:   PetscFunctionReturn(PETSC_SUCCESS);
1317: }

1319: static struct _MatOps MatOps_Values = {MatSetValues_SeqSBAIJ,
1320:                                        MatGetRow_SeqSBAIJ,
1321:                                        MatRestoreRow_SeqSBAIJ,
1322:                                        MatMult_SeqSBAIJ_N,
1323:                                        /*  4*/ MatMultAdd_SeqSBAIJ_N,
1324:                                        MatMult_SeqSBAIJ_N, /* transpose versions are same as non-transpose versions */
1325:                                        MatMultAdd_SeqSBAIJ_N,
1326:                                        NULL,
1327:                                        NULL,
1328:                                        NULL,
1329:                                        /* 10*/ NULL,
1330:                                        NULL,
1331:                                        MatCholeskyFactor_SeqSBAIJ,
1332:                                        MatSOR_SeqSBAIJ,
1333:                                        MatTranspose_SeqSBAIJ,
1334:                                        /* 15*/ MatGetInfo_SeqSBAIJ,
1335:                                        MatEqual_SeqSBAIJ,
1336:                                        MatGetDiagonal_SeqSBAIJ,
1337:                                        MatDiagonalScale_SeqSBAIJ,
1338:                                        MatNorm_SeqSBAIJ,
1339:                                        /* 20*/ NULL,
1340:                                        MatAssemblyEnd_SeqSBAIJ,
1341:                                        MatSetOption_SeqSBAIJ,
1342:                                        MatZeroEntries_SeqSBAIJ,
1343:                                        /* 24*/ NULL,
1344:                                        NULL,
1345:                                        NULL,
1346:                                        NULL,
1347:                                        NULL,
1348:                                        /* 29*/ MatSetUp_Seq_Hash,
1349:                                        NULL,
1350:                                        NULL,
1351:                                        NULL,
1352:                                        NULL,
1353:                                        /* 34*/ MatDuplicate_SeqSBAIJ,
1354:                                        NULL,
1355:                                        NULL,
1356:                                        NULL,
1357:                                        MatICCFactor_SeqSBAIJ,
1358:                                        /* 39*/ MatAXPY_SeqSBAIJ,
1359:                                        MatCreateSubMatrices_SeqSBAIJ,
1360:                                        MatIncreaseOverlap_SeqSBAIJ,
1361:                                        MatGetValues_SeqSBAIJ,
1362:                                        MatCopy_SeqSBAIJ,
1363:                                        /* 44*/ NULL,
1364:                                        MatScale_SeqSBAIJ,
1365:                                        MatShift_SeqSBAIJ,
1366:                                        NULL,
1367:                                        MatZeroRowsColumns_SeqSBAIJ,
1368:                                        /* 49*/ NULL,
1369:                                        MatGetRowIJ_SeqSBAIJ,
1370:                                        MatRestoreRowIJ_SeqSBAIJ,
1371:                                        NULL,
1372:                                        NULL,
1373:                                        /* 54*/ NULL,
1374:                                        NULL,
1375:                                        NULL,
1376:                                        MatPermute_SeqSBAIJ,
1377:                                        MatSetValuesBlocked_SeqSBAIJ,
1378:                                        /* 59*/ MatCreateSubMatrix_SeqSBAIJ,
1379:                                        NULL,
1380:                                        NULL,
1381:                                        NULL,
1382:                                        NULL,
1383:                                        /* 64*/ NULL,
1384:                                        NULL,
1385:                                        NULL,
1386:                                        NULL,
1387:                                        MatGetRowMaxAbs_SeqSBAIJ,
1388:                                        /* 69*/ NULL,
1389:                                        MatConvert_MPISBAIJ_Basic,
1390:                                        NULL,
1391:                                        NULL,
1392:                                        NULL,
1393:                                        /* 74*/ NULL,
1394:                                        NULL,
1395:                                        NULL,
1396:                                        MatGetInertia_SeqSBAIJ,
1397:                                        MatLoad_SeqSBAIJ,
1398:                                        /* 79*/ NULL,
1399:                                        NULL,
1400:                                        MatIsStructurallySymmetric_SeqSBAIJ,
1401:                                        NULL,
1402:                                        NULL,
1403:                                        /* 84*/ NULL,
1404:                                        NULL,
1405:                                        NULL,
1406:                                        NULL,
1407:                                        NULL,
1408:                                        /* 89*/ NULL,
1409:                                        NULL,
1410:                                        NULL,
1411:                                        NULL,
1412:                                        MatConjugate_SeqSBAIJ,
1413:                                        /* 94*/ NULL,
1414:                                        NULL,
1415:                                        MatRealPart_SeqSBAIJ,
1416:                                        MatImaginaryPart_SeqSBAIJ,
1417:                                        MatGetRowUpperTriangular_SeqSBAIJ,
1418:                                        /* 99*/ MatRestoreRowUpperTriangular_SeqSBAIJ,
1419:                                        NULL,
1420:                                        NULL,
1421:                                        NULL,
1422:                                        NULL,
1423:                                        /*104*/ NULL,
1424:                                        NULL,
1425:                                        NULL,
1426:                                        NULL,
1427:                                        NULL,
1428:                                        /*109*/ NULL,
1429:                                        NULL,
1430:                                        NULL,
1431:                                        NULL,
1432:                                        NULL,
1433:                                        /*114*/ NULL,
1434:                                        MatGetColumnReductions_SeqSBAIJ,
1435:                                        NULL,
1436:                                        NULL,
1437:                                        NULL,
1438:                                        /*119*/ NULL,
1439:                                        NULL,
1440:                                        NULL,
1441:                                        NULL,
1442:                                        NULL,
1443:                                        /*124*/ NULL,
1444:                                        MatSetBlockSizes_Default,
1445:                                        NULL,
1446:                                        NULL,
1447:                                        NULL,
1448:                                        /*129*/ MatCreateMPIMatConcatenateSeqMat_SeqSBAIJ,
1449:                                        NULL,
1450:                                        NULL,
1451:                                        NULL,
1452:                                        NULL,
1453:                                        /*134*/ NULL,
1454:                                        MatEliminateZeros_SeqSBAIJ,
1455:                                        NULL,
1456:                                        NULL,
1457:                                        NULL,
1458:                                        /*139*/ NULL,
1459:                                        MatCopyHashToXAIJ_Seq_Hash,
1460:                                        NULL,
1461:                                        NULL,
1462:                                        NULL,
1463:                                        /*144*/ NULL,
1464:                                        NULL,
1465:                                        NULL,
1466:                                        NULL};

1468: static PetscErrorCode MatStoreValues_SeqSBAIJ(Mat mat)
1469: {
1470:   Mat_SeqSBAIJ *aij = (Mat_SeqSBAIJ *)mat->data;
1471:   PetscInt      nz  = aij->i[mat->rmap->N] * mat->rmap->bs * aij->bs2;

1473:   PetscFunctionBegin;
1474:   PetscCheck(aij->nonew == 1, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");

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

1479:   /* copy values over */
1480:   PetscCall(PetscArraycpy(aij->saved_values, aij->a, nz));
1481:   PetscFunctionReturn(PETSC_SUCCESS);
1482: }

1484: static PetscErrorCode MatRetrieveValues_SeqSBAIJ(Mat mat)
1485: {
1486:   Mat_SeqSBAIJ *aij = (Mat_SeqSBAIJ *)mat->data;
1487:   PetscInt      nz  = aij->i[mat->rmap->N] * mat->rmap->bs * aij->bs2;

1489:   PetscFunctionBegin;
1490:   PetscCheck(aij->nonew == 1, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
1491:   PetscCheck(aij->saved_values, PETSC_COMM_SELF, PETSC_ERR_ORDER, "Must call MatStoreValues(A);first");

1493:   /* copy values over */
1494:   PetscCall(PetscArraycpy(aij->a, aij->saved_values, nz));
1495:   PetscFunctionReturn(PETSC_SUCCESS);
1496: }

1498: static PetscErrorCode MatSeqSBAIJSetPreallocation_SeqSBAIJ(Mat B, PetscInt bs, PetscInt nz, const PetscInt nnz[])
1499: {
1500:   Mat_SeqSBAIJ *b = (Mat_SeqSBAIJ *)B->data;
1501:   PetscInt      i, mbs, nbs, bs2;
1502:   PetscBool     skipallocation = PETSC_FALSE, flg = PETSC_FALSE, realalloc = PETSC_FALSE;

1504:   PetscFunctionBegin;
1505:   if (B->hash_active) {
1506:     PetscInt bs;
1507:     B->ops[0] = b->cops;
1508:     PetscCall(PetscHMapIJVDestroy(&b->ht));
1509:     PetscCall(MatGetBlockSize(B, &bs));
1510:     if (bs > 1) PetscCall(PetscHSetIJDestroy(&b->bht));
1511:     PetscCall(PetscFree(b->dnz));
1512:     PetscCall(PetscFree(b->bdnz));
1513:     B->hash_active = PETSC_FALSE;
1514:   }
1515:   if (nz >= 0 || nnz) realalloc = PETSC_TRUE;

1517:   PetscCall(MatSetBlockSize(B, bs));
1518:   PetscCall(PetscLayoutSetUp(B->rmap));
1519:   PetscCall(PetscLayoutSetUp(B->cmap));
1520:   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);
1521:   PetscCall(PetscLayoutGetBlockSize(B->rmap, &bs));

1523:   B->preallocated = PETSC_TRUE;

1525:   mbs = B->rmap->N / bs;
1526:   nbs = B->cmap->n / bs;
1527:   bs2 = bs * bs;

1529:   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");

1531:   if (nz == MAT_SKIP_ALLOCATION) {
1532:     skipallocation = PETSC_TRUE;
1533:     nz             = 0;
1534:   }

1536:   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 3;
1537:   PetscCheck(nz >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "nz cannot be less than 0: value %" PetscInt_FMT, nz);
1538:   if (nnz) {
1539:     for (i = 0; i < mbs; i++) {
1540:       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]);
1541:       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);
1542:     }
1543:   }

1545:   B->ops->mult             = MatMult_SeqSBAIJ_N;
1546:   B->ops->multadd          = MatMultAdd_SeqSBAIJ_N;
1547:   B->ops->multtranspose    = MatMult_SeqSBAIJ_N;
1548:   B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_N;

1550:   PetscCall(PetscOptionsGetBool(((PetscObject)B)->options, ((PetscObject)B)->prefix, "-mat_no_unroll", &flg, NULL));
1551:   if (!flg) {
1552:     switch (bs) {
1553:     case 1:
1554:       B->ops->mult             = MatMult_SeqSBAIJ_1;
1555:       B->ops->multadd          = MatMultAdd_SeqSBAIJ_1;
1556:       B->ops->multtranspose    = MatMult_SeqSBAIJ_1;
1557:       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_1;
1558:       break;
1559:     case 2:
1560:       B->ops->mult             = MatMult_SeqSBAIJ_2;
1561:       B->ops->multadd          = MatMultAdd_SeqSBAIJ_2;
1562:       B->ops->multtranspose    = MatMult_SeqSBAIJ_2;
1563:       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_2;
1564:       break;
1565:     case 3:
1566:       B->ops->mult             = MatMult_SeqSBAIJ_3;
1567:       B->ops->multadd          = MatMultAdd_SeqSBAIJ_3;
1568:       B->ops->multtranspose    = MatMult_SeqSBAIJ_3;
1569:       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_3;
1570:       break;
1571:     case 4:
1572:       B->ops->mult             = MatMult_SeqSBAIJ_4;
1573:       B->ops->multadd          = MatMultAdd_SeqSBAIJ_4;
1574:       B->ops->multtranspose    = MatMult_SeqSBAIJ_4;
1575:       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_4;
1576:       break;
1577:     case 5:
1578:       B->ops->mult             = MatMult_SeqSBAIJ_5;
1579:       B->ops->multadd          = MatMultAdd_SeqSBAIJ_5;
1580:       B->ops->multtranspose    = MatMult_SeqSBAIJ_5;
1581:       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_5;
1582:       break;
1583:     case 6:
1584:       B->ops->mult             = MatMult_SeqSBAIJ_6;
1585:       B->ops->multadd          = MatMultAdd_SeqSBAIJ_6;
1586:       B->ops->multtranspose    = MatMult_SeqSBAIJ_6;
1587:       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_6;
1588:       break;
1589:     case 7:
1590:       B->ops->mult             = MatMult_SeqSBAIJ_7;
1591:       B->ops->multadd          = MatMultAdd_SeqSBAIJ_7;
1592:       B->ops->multtranspose    = MatMult_SeqSBAIJ_7;
1593:       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_7;
1594:       break;
1595:     }
1596:   }

1598:   b->mbs = mbs;
1599:   b->nbs = nbs;
1600:   if (!skipallocation) {
1601:     if (!b->imax) {
1602:       PetscCall(PetscMalloc2(mbs, &b->imax, mbs, &b->ilen));
1603:       b->free_imax_ilen = PETSC_TRUE;
1604:     }
1605:     if (!nnz) {
1606:       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
1607:       else if (nz <= 0) nz = 1;
1608:       nz = PetscMin(nbs, nz);
1609:       for (i = 0; i < mbs; i++) b->imax[i] = nz;
1610:       PetscCall(PetscIntMultError(nz, mbs, &nz));
1611:     } else {
1612:       PetscInt64 nz64 = 0;
1613:       for (i = 0; i < mbs; i++) {
1614:         b->imax[i] = nnz[i];
1615:         nz64 += nnz[i];
1616:       }
1617:       PetscCall(PetscIntCast(nz64, &nz));
1618:     }
1619:     /* b->ilen will count nonzeros in each block row so far. */
1620:     for (i = 0; i < mbs; i++) b->ilen[i] = 0;
1621:     /* nz=(nz+mbs)/2; */ /* total diagonal and superdiagonal nonzero blocks */

1623:     /* allocate the matrix space */
1624:     PetscCall(MatSeqXAIJFreeAIJ(B, &b->a, &b->j, &b->i));
1625:     PetscCall(PetscShmgetAllocateArray(bs2 * nz, sizeof(PetscScalar), (void **)&b->a));
1626:     PetscCall(PetscShmgetAllocateArray(nz, sizeof(PetscInt), (void **)&b->j));
1627:     PetscCall(PetscShmgetAllocateArray(B->rmap->n + 1, sizeof(PetscInt), (void **)&b->i));
1628:     PetscCall(PetscArrayzero(b->a, nz * bs2));
1629:     PetscCall(PetscArrayzero(b->j, nz));
1630:     b->free_a  = PETSC_TRUE;
1631:     b->free_ij = PETSC_TRUE;

1633:     /* pointer to beginning of each row */
1634:     b->i[0] = 0;
1635:     for (i = 1; i < mbs + 1; i++) b->i[i] = b->i[i - 1] + b->imax[i - 1];
1636:   } else {
1637:     b->free_a  = PETSC_FALSE;
1638:     b->free_ij = PETSC_FALSE;
1639:   }

1641:   b->bs2     = bs2;
1642:   b->nz      = 0;
1643:   b->maxnz   = nz;
1644:   b->inew    = NULL;
1645:   b->jnew    = NULL;
1646:   b->anew    = NULL;
1647:   b->a2anew  = NULL;
1648:   b->permute = PETSC_FALSE;

1650:   B->was_assembled = PETSC_FALSE;
1651:   B->assembled     = PETSC_FALSE;
1652:   if (realalloc) PetscCall(MatSetOption(B, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_TRUE));
1653:   PetscFunctionReturn(PETSC_SUCCESS);
1654: }

1656: static PetscErrorCode MatSeqSBAIJSetPreallocationCSR_SeqSBAIJ(Mat B, PetscInt bs, const PetscInt ii[], const PetscInt jj[], const PetscScalar V[])
1657: {
1658:   PetscInt      i, j, m, nz, anz, nz_max = 0, *nnz;
1659:   PetscScalar  *values      = NULL;
1660:   Mat_SeqSBAIJ *b           = (Mat_SeqSBAIJ *)B->data;
1661:   PetscBool     roworiented = b->roworiented;
1662:   PetscBool     ilw         = b->ignore_ltriangular;

1664:   PetscFunctionBegin;
1665:   PetscCheck(bs >= 1, PetscObjectComm((PetscObject)B), PETSC_ERR_ARG_OUTOFRANGE, "Invalid block size specified, must be positive but it is %" PetscInt_FMT, bs);
1666:   PetscCall(PetscLayoutSetBlockSize(B->rmap, bs));
1667:   PetscCall(PetscLayoutSetBlockSize(B->cmap, bs));
1668:   PetscCall(PetscLayoutSetUp(B->rmap));
1669:   PetscCall(PetscLayoutSetUp(B->cmap));
1670:   PetscCall(PetscLayoutGetBlockSize(B->rmap, &bs));
1671:   m = B->rmap->n / bs;

1673:   PetscCheck(!ii[0], PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "ii[0] must be 0 but it is %" PetscInt_FMT, ii[0]);
1674:   PetscCall(PetscMalloc1(m + 1, &nnz));
1675:   for (i = 0; i < m; i++) {
1676:     nz = ii[i + 1] - ii[i];
1677:     PetscCheck(nz >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Row %" PetscInt_FMT " has a negative number of columns %" PetscInt_FMT, i, nz);
1678:     PetscCheckSorted(nz, jj + ii[i]);
1679:     anz = 0;
1680:     for (j = 0; j < nz; j++) {
1681:       /* count only values on the diagonal or above */
1682:       if (jj[ii[i] + j] >= i) {
1683:         anz = nz - j;
1684:         break;
1685:       }
1686:     }
1687:     nz_max = PetscMax(nz_max, nz);
1688:     nnz[i] = anz;
1689:   }
1690:   PetscCall(MatSeqSBAIJSetPreallocation(B, bs, 0, nnz));
1691:   PetscCall(PetscFree(nnz));

1693:   values = (PetscScalar *)V;
1694:   if (!values) PetscCall(PetscCalloc1(bs * bs * nz_max, &values));
1695:   b->ignore_ltriangular = PETSC_TRUE;
1696:   for (i = 0; i < m; i++) {
1697:     PetscInt        ncols = ii[i + 1] - ii[i];
1698:     const PetscInt *icols = jj + ii[i];

1700:     if (!roworiented || bs == 1) {
1701:       const PetscScalar *svals = values + (V ? (bs * bs * ii[i]) : 0);
1702:       PetscCall(MatSetValuesBlocked_SeqSBAIJ(B, 1, &i, ncols, icols, svals, INSERT_VALUES));
1703:     } else {
1704:       for (j = 0; j < ncols; j++) {
1705:         const PetscScalar *svals = values + (V ? (bs * bs * (ii[i] + j)) : 0);
1706:         PetscCall(MatSetValuesBlocked_SeqSBAIJ(B, 1, &i, 1, &icols[j], svals, INSERT_VALUES));
1707:       }
1708:     }
1709:   }
1710:   if (!V) PetscCall(PetscFree(values));
1711:   PetscCall(MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY));
1712:   PetscCall(MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY));
1713:   PetscCall(MatSetOption(B, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
1714:   b->ignore_ltriangular = ilw;
1715:   PetscFunctionReturn(PETSC_SUCCESS);
1716: }

1718: /*
1719:    This is used to set the numeric factorization for both Cholesky and ICC symbolic factorization
1720: */
1721: PetscErrorCode MatSeqSBAIJSetNumericFactorization_inplace(Mat B, PetscBool natural)
1722: {
1723:   PetscBool flg = PETSC_FALSE;
1724:   PetscInt  bs  = B->rmap->bs;

1726:   PetscFunctionBegin;
1727:   PetscCall(PetscOptionsGetBool(((PetscObject)B)->options, ((PetscObject)B)->prefix, "-mat_no_unroll", &flg, NULL));
1728:   if (flg) bs = 8;

1730:   if (!natural) {
1731:     switch (bs) {
1732:     case 1:
1733:       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_1_inplace;
1734:       break;
1735:     case 2:
1736:       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_2;
1737:       break;
1738:     case 3:
1739:       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_3;
1740:       break;
1741:     case 4:
1742:       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_4;
1743:       break;
1744:     case 5:
1745:       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_5;
1746:       break;
1747:     case 6:
1748:       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_6;
1749:       break;
1750:     case 7:
1751:       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_7;
1752:       break;
1753:     default:
1754:       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_N;
1755:       break;
1756:     }
1757:   } else {
1758:     switch (bs) {
1759:     case 1:
1760:       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_1_NaturalOrdering_inplace;
1761:       break;
1762:     case 2:
1763:       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_2_NaturalOrdering;
1764:       break;
1765:     case 3:
1766:       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_3_NaturalOrdering;
1767:       break;
1768:     case 4:
1769:       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_4_NaturalOrdering;
1770:       break;
1771:     case 5:
1772:       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_5_NaturalOrdering;
1773:       break;
1774:     case 6:
1775:       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_6_NaturalOrdering;
1776:       break;
1777:     case 7:
1778:       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_7_NaturalOrdering;
1779:       break;
1780:     default:
1781:       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_N_NaturalOrdering;
1782:       break;
1783:     }
1784:   }
1785:   PetscFunctionReturn(PETSC_SUCCESS);
1786: }

1788: PETSC_INTERN PetscErrorCode MatConvert_SeqSBAIJ_SeqAIJ(Mat, MatType, MatReuse, Mat *);
1789: PETSC_INTERN PetscErrorCode MatConvert_SeqSBAIJ_SeqBAIJ(Mat, MatType, MatReuse, Mat *);
1790: static PetscErrorCode       MatFactorGetSolverType_petsc(Mat A, MatSolverType *type)
1791: {
1792:   PetscFunctionBegin;
1793:   *type = MATSOLVERPETSC;
1794:   PetscFunctionReturn(PETSC_SUCCESS);
1795: }

1797: PETSC_INTERN PetscErrorCode MatGetFactor_seqsbaij_petsc(Mat A, MatFactorType ftype, Mat *B)
1798: {
1799:   PetscInt n = A->rmap->n;

1801:   PetscFunctionBegin;
1802:   if (PetscDefined(USE_COMPLEX) && (ftype == MAT_FACTOR_CHOLESKY || ftype == MAT_FACTOR_ICC) && A->hermitian == PETSC_BOOL3_TRUE && A->symmetric != PETSC_BOOL3_TRUE) {
1803:     PetscCall(PetscInfo(A, "Hermitian MAT_FACTOR_CHOLESKY or MAT_FACTOR_ICC are not supported. Use MAT_FACTOR_LU instead.\n"));
1804:     *B = NULL;
1805:     PetscFunctionReturn(PETSC_SUCCESS);
1806:   }

1808:   PetscCall(MatCreate(PetscObjectComm((PetscObject)A), B));
1809:   PetscCall(MatSetSizes(*B, n, n, n, n));
1810:   PetscCheck(ftype == MAT_FACTOR_CHOLESKY || ftype == MAT_FACTOR_ICC, PETSC_COMM_SELF, PETSC_ERR_SUP, "Factor type not supported");
1811:   PetscCall(MatSetType(*B, MATSEQSBAIJ));
1812:   PetscCall(MatSeqSBAIJSetPreallocation(*B, A->rmap->bs, MAT_SKIP_ALLOCATION, NULL));

1814:   (*B)->ops->choleskyfactorsymbolic = MatCholeskyFactorSymbolic_SeqSBAIJ;
1815:   (*B)->ops->iccfactorsymbolic      = MatICCFactorSymbolic_SeqSBAIJ;
1816:   PetscCall(PetscStrallocpy(MATORDERINGNATURAL, (char **)&(*B)->preferredordering[MAT_FACTOR_CHOLESKY]));
1817:   PetscCall(PetscStrallocpy(MATORDERINGNATURAL, (char **)&(*B)->preferredordering[MAT_FACTOR_ICC]));

1819:   (*B)->factortype     = ftype;
1820:   (*B)->canuseordering = PETSC_TRUE;
1821:   PetscCall(PetscFree((*B)->solvertype));
1822:   PetscCall(PetscStrallocpy(MATSOLVERPETSC, &(*B)->solvertype));
1823:   PetscCall(PetscObjectComposeFunction((PetscObject)*B, "MatFactorGetSolverType_C", MatFactorGetSolverType_petsc));
1824:   PetscFunctionReturn(PETSC_SUCCESS);
1825: }

1827: /*@
1828:   MatSeqSBAIJGetArray - gives access to the array where the numerical data for a `MATSEQSBAIJ` matrix is stored

1830:   Not Collective

1832:   Input Parameter:
1833: . A - a `MATSEQSBAIJ` matrix

1835:   Output Parameter:
1836: . array - pointer to the data

1838:   Level: intermediate

1840: .seealso: [](ch_matrices), `Mat`, `MATSEQSBAIJ`, `MatSeqSBAIJRestoreArray()`, `MatSeqAIJGetArray()`, `MatSeqAIJRestoreArray()`
1841: @*/
1842: PetscErrorCode MatSeqSBAIJGetArray(Mat A, PetscScalar *array[])
1843: {
1844:   PetscFunctionBegin;
1845:   PetscUseMethod(A, "MatSeqSBAIJGetArray_C", (Mat, PetscScalar **), (A, array));
1846:   PetscFunctionReturn(PETSC_SUCCESS);
1847: }

1849: /*@
1850:   MatSeqSBAIJRestoreArray - returns access to the array where the numerical data for a `MATSEQSBAIJ` matrix is stored obtained by `MatSeqSBAIJGetArray()`

1852:   Not Collective

1854:   Input Parameters:
1855: + A     - a `MATSEQSBAIJ` matrix
1856: - array - pointer to the data

1858:   Level: intermediate

1860: .seealso: [](ch_matrices), `Mat`, `MATSEQSBAIJ`, `MatSeqSBAIJGetArray()`, `MatSeqAIJGetArray()`, `MatSeqAIJRestoreArray()`
1861: @*/
1862: PetscErrorCode MatSeqSBAIJRestoreArray(Mat A, PetscScalar *array[])
1863: {
1864:   PetscFunctionBegin;
1865:   PetscUseMethod(A, "MatSeqSBAIJRestoreArray_C", (Mat, PetscScalar **), (A, array));
1866:   PetscCall(PetscObjectStateIncrease((PetscObject)A));
1867:   PetscFunctionReturn(PETSC_SUCCESS);
1868: }

1870: /*MC
1871:   MATSEQSBAIJ - MATSEQSBAIJ = "seqsbaij" - A matrix type to be used for sequential symmetric block sparse matrices,
1872:   based on block compressed sparse row format.  Only the upper triangular portion of the matrix is stored.

1874:   For complex numbers by default this matrix is symmetric, NOT Hermitian symmetric. To make it Hermitian symmetric you
1875:   can call `MatSetOption`(`Mat`, `MAT_HERMITIAN`).

1877:   Options Database Key:
1878:   . -mat_type seqsbaij - sets the matrix type to "seqsbaij" during a call to `MatSetFromOptions()`

1880:   Level: beginner

1882:   Notes:
1883:   By default if you insert values into the lower triangular part of the matrix they are simply ignored (since they are not
1884:   stored and it is assumed they symmetric to the upper triangular). If you call `MatSetOption`(`Mat`,`MAT_IGNORE_LOWER_TRIANGULAR`,`PETSC_FALSE`) or use
1885:   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.

1887:   The number of rows in the matrix must be less than or equal to the number of columns

1889: .seealso: [](ch_matrices), `Mat`, `MATSEQSBAIJ`, `MatCreateSeqSBAIJ()`, `MatType`, `MATMPISBAIJ`
1890: M*/
1891: PETSC_EXTERN PetscErrorCode MatCreate_SeqSBAIJ(Mat B)
1892: {
1893:   Mat_SeqSBAIJ *b;
1894:   PetscMPIInt   size;
1895:   PetscBool     no_unroll = PETSC_FALSE, no_inode = PETSC_FALSE;

1897:   PetscFunctionBegin;
1898:   PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)B), &size));
1899:   PetscCheck(size <= 1, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Comm must be of size 1");

1901:   PetscCall(PetscNew(&b));
1902:   B->data   = (void *)b;
1903:   B->ops[0] = MatOps_Values;

1905:   B->ops->destroy    = MatDestroy_SeqSBAIJ;
1906:   B->ops->view       = MatView_SeqSBAIJ;
1907:   b->row             = NULL;
1908:   b->icol            = NULL;
1909:   b->reallocs        = 0;
1910:   b->saved_values    = NULL;
1911:   b->inode.limit     = 5;
1912:   b->inode.max_limit = 5;

1914:   b->roworiented        = PETSC_TRUE;
1915:   b->nonew              = 0;
1916:   b->diag               = NULL;
1917:   b->solve_work         = NULL;
1918:   b->mult_work          = NULL;
1919:   B->spptr              = NULL;
1920:   B->info.nz_unneeded   = (PetscReal)b->maxnz * b->bs2;
1921:   b->keepnonzeropattern = PETSC_FALSE;

1923:   b->inew    = NULL;
1924:   b->jnew    = NULL;
1925:   b->anew    = NULL;
1926:   b->a2anew  = NULL;
1927:   b->permute = PETSC_FALSE;

1929:   b->ignore_ltriangular = PETSC_TRUE;

1931:   PetscCall(PetscOptionsGetBool(((PetscObject)B)->options, ((PetscObject)B)->prefix, "-mat_ignore_lower_triangular", &b->ignore_ltriangular, NULL));

1933:   b->getrow_utriangular = PETSC_FALSE;

1935:   PetscCall(PetscOptionsGetBool(((PetscObject)B)->options, ((PetscObject)B)->prefix, "-mat_getrow_uppertriangular", &b->getrow_utriangular, NULL));

1937:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSeqSBAIJGetArray_C", MatSeqSBAIJGetArray_SeqSBAIJ));
1938:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSeqSBAIJRestoreArray_C", MatSeqSBAIJRestoreArray_SeqSBAIJ));
1939:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatStoreValues_C", MatStoreValues_SeqSBAIJ));
1940:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatRetrieveValues_C", MatRetrieveValues_SeqSBAIJ));
1941:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSeqSBAIJSetColumnIndices_C", MatSeqSBAIJSetColumnIndices_SeqSBAIJ));
1942:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqsbaij_seqaij_C", MatConvert_SeqSBAIJ_SeqAIJ));
1943:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqsbaij_seqbaij_C", MatConvert_SeqSBAIJ_SeqBAIJ));
1944:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSeqSBAIJSetPreallocation_C", MatSeqSBAIJSetPreallocation_SeqSBAIJ));
1945:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatSeqSBAIJSetPreallocationCSR_C", MatSeqSBAIJSetPreallocationCSR_SeqSBAIJ));
1946: #if PetscDefined(HAVE_ELEMENTAL)
1947:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqsbaij_elemental_C", MatConvert_SeqSBAIJ_Elemental));
1948: #endif
1949: #if PetscDefined(HAVE_SCALAPACK) && (PetscDefined(USE_REAL_SINGLE) || PetscDefined(USE_REAL_DOUBLE))
1950:   PetscCall(PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqsbaij_scalapack_C", MatConvert_SBAIJ_ScaLAPACK));
1951: #endif

1953:   B->symmetry_eternal            = PETSC_TRUE;
1954:   B->structural_symmetry_eternal = PETSC_TRUE;
1955:   B->symmetric                   = PETSC_BOOL3_TRUE;
1956:   B->structurally_symmetric      = PETSC_BOOL3_TRUE;
1957: #if !PetscDefined(USE_COMPLEX)
1958:   B->hermitian = PETSC_BOOL3_TRUE;
1959: #endif

1961:   PetscCall(PetscObjectChangeTypeName((PetscObject)B, MATSEQSBAIJ));

1963:   PetscOptionsBegin(PetscObjectComm((PetscObject)B), ((PetscObject)B)->prefix, "Options for SEQSBAIJ matrix", "Mat");
1964:   PetscCall(PetscOptionsBool("-mat_no_unroll", "Do not optimize for inodes (slower)", NULL, no_unroll, &no_unroll, NULL));
1965:   if (no_unroll) PetscCall(PetscInfo(B, "Not using Inode routines due to -mat_no_unroll\n"));
1966:   PetscCall(PetscOptionsBool("-mat_no_inode", "Do not optimize for inodes (slower)", NULL, no_inode, &no_inode, NULL));
1967:   if (no_inode) PetscCall(PetscInfo(B, "Not using Inode routines due to -mat_no_inode\n"));
1968:   PetscCall(PetscOptionsInt("-mat_inode_limit", "Do not use inodes larger than this value", NULL, b->inode.limit, &b->inode.limit, NULL));
1969:   PetscOptionsEnd();
1970:   b->inode.use = (PetscBool)(!(no_unroll || no_inode));
1971:   if (b->inode.limit > b->inode.max_limit) b->inode.limit = b->inode.max_limit;
1972:   PetscFunctionReturn(PETSC_SUCCESS);
1973: }

1975: /*@
1976:   MatSeqSBAIJSetPreallocation - Creates a sparse symmetric matrix in block AIJ (block
1977:   compressed row) `MATSEQSBAIJ` format.  For good matrix assembly performance the
1978:   user should preallocate the matrix storage by setting the parameter `nz`
1979:   (or the array `nnz`).

1981:   Collective

1983:   Input Parameters:
1984: + B   - the symmetric matrix
1985: . bs  - size of block, the blocks are ALWAYS square. One can use `MatSetBlockSizes()` to set a different row and column blocksize but the row
1986:         blocksize always defines the size of the blocks. The column blocksize sets the blocksize of the vectors obtained with `MatCreateVecs()`
1987: . nz  - number of block nonzeros per block row (same for all rows)
1988: - nnz - array containing the number of block nonzeros in the upper triangular plus
1989:         diagonal portion of each block (possibly different for each block row) or `NULL`

1991:   Options Database Keys:
1992: + -mat_no_unroll  - uses code that does not unroll the loops in the block calculations (much slower)
1993: - -mat_block_size - size of the blocks to use (only works if a negative bs is passed in

1995:   Level: intermediate

1997:   Notes:
1998:   Specify the preallocated storage with either `nz` or `nnz` (not both).
1999:   Set `nz` = `PETSC_DEFAULT` and `nnz` = `NULL` for PETSc to control dynamic memory
2000:   allocation.  See [Sparse Matrices](sec_matsparse) for details.

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

2007:   If the `nnz` parameter is given then the `nz` parameter is ignored

2009: .seealso: [](ch_matrices), `Mat`, [Sparse Matrices](sec_matsparse), `MATSEQSBAIJ`, `MatCreate()`, `MatCreateSeqAIJ()`, `MatSetValues()`, `MatCreateSBAIJ()`
2010: @*/
2011: PetscErrorCode MatSeqSBAIJSetPreallocation(Mat B, PetscInt bs, PetscInt nz, const PetscInt nnz[])
2012: {
2013:   PetscFunctionBegin;
2017:   PetscTryMethod(B, "MatSeqSBAIJSetPreallocation_C", (Mat, PetscInt, PetscInt, const PetscInt[]), (B, bs, nz, nnz));
2018:   PetscFunctionReturn(PETSC_SUCCESS);
2019: }

2021: /*@
2022:   MatSeqSBAIJSetPreallocationCSR - Creates a sparse parallel matrix in `MATSEQSBAIJ` format using the given nonzero structure and (optional) numerical values

2024:   Input Parameters:
2025: + B  - the matrix
2026: . bs - size of block, the blocks are ALWAYS square.
2027: . i  - the indices into `j` for the start of each local row (indices start with zero)
2028: . j  - the column indices for each local row (indices start with zero) these must be sorted for each row
2029: - v  - optional values in the matrix, use `NULL` if not provided

2031:   Level: advanced

2033:   Notes:
2034:   The `i`,`j`,`v` values are COPIED with this routine; to avoid the copy use `MatCreateSeqSBAIJWithArrays()`

2036:   The order of the entries in values is specified by the `MatOption` `MAT_ROW_ORIENTED`.  For example, C programs
2037:   may want to use the default `MAT_ROW_ORIENTED` = `PETSC_TRUE` and use an array v[nnz][bs][bs] where the second index is
2038:   over rows within a block and the last index is over columns within a block row.  Fortran programs will likely set
2039:   `MAT_ROW_ORIENTED` = `PETSC_FALSE` and use a Fortran array v(bs,bs,nnz) in which the first index is over rows within a
2040:   block column and the second index is over columns within a block.

2042:   Any entries provided that lie below the diagonal are ignored

2044:   Though this routine has Preallocation() in the name it also sets the exact nonzero locations of the matrix entries
2045:   and usually the numerical values as well

2047: .seealso: [](ch_matrices), `Mat`, `MATSEQSBAIJ`, `MatCreate()`, `MatCreateSeqSBAIJ()`, `MatSetValuesBlocked()`, `MatSeqSBAIJSetPreallocation()`
2048: @*/
2049: PetscErrorCode MatSeqSBAIJSetPreallocationCSR(Mat B, PetscInt bs, const PetscInt i[], const PetscInt j[], const PetscScalar v[])
2050: {
2051:   PetscFunctionBegin;
2055:   PetscTryMethod(B, "MatSeqSBAIJSetPreallocationCSR_C", (Mat, PetscInt, const PetscInt[], const PetscInt[], const PetscScalar[]), (B, bs, i, j, v));
2056:   PetscFunctionReturn(PETSC_SUCCESS);
2057: }

2059: /*@
2060:   MatCreateSeqSBAIJ - Creates a sparse symmetric matrix in (block
2061:   compressed row) `MATSEQSBAIJ` format.  For good matrix assembly performance the
2062:   user should preallocate the matrix storage by setting the parameter `nz`
2063:   (or the array `nnz`).

2065:   Collective

2067:   Input Parameters:
2068: + comm - MPI communicator, set to `PETSC_COMM_SELF`
2069: . bs   - size of block, the blocks are ALWAYS square. One can use `MatSetBlockSizes()` to set a different row and column blocksize but the row
2070:           blocksize always defines the size of the blocks. The column blocksize sets the blocksize of the vectors obtained with MatCreateVecs()
2071: . m    - number of rows
2072: . n    - number of columns
2073: . nz   - number of block nonzeros per block row (same for all rows)
2074: - nnz  - array containing the number of block nonzeros in the upper triangular plus
2075:          diagonal portion of each block (possibly different for each block row) or `NULL`

2077:   Output Parameter:
2078: . A - the symmetric matrix

2080:   Options Database Keys:
2081: + -mat_no_unroll  - uses code that does not unroll the loops in the block calculations (much slower)
2082: - -mat_block_size - size of the blocks to use

2084:   Level: intermediate

2086:   Notes:
2087:   It is recommended that one use `MatCreateFromOptions()` or the `MatCreate()`, `MatSetType()` and/or `MatSetFromOptions()`,
2088:   MatXXXXSetPreallocation() paradigm instead of this routine directly.
2089:   [MatXXXXSetPreallocation() is, for example, `MatSeqAIJSetPreallocation()`]

2091:   The number of rows and columns must be divisible by blocksize.
2092:   This matrix type does not support complex Hermitian operation.

2094:   Specify the preallocated storage with either `nz` or `nnz` (not both).
2095:   Set `nz` = `PETSC_DEFAULT` and `nnz` = `NULL` for PETSc to control dynamic memory
2096:   allocation.  See [Sparse Matrices](sec_matsparse) for details.

2098:   If the `nnz` parameter is given then the `nz` parameter is ignored

2100: .seealso: [](ch_matrices), `Mat`, [Sparse Matrices](sec_matsparse), `MATSEQSBAIJ`, `MatCreate()`, `MatCreateSeqAIJ()`, `MatSetValues()`, `MatCreateSBAIJ()`
2101: @*/
2102: PetscErrorCode MatCreateSeqSBAIJ(MPI_Comm comm, PetscInt bs, PetscInt m, PetscInt n, PetscInt nz, const PetscInt nnz[], Mat *A)
2103: {
2104:   PetscFunctionBegin;
2105:   PetscCall(MatCreate(comm, A));
2106:   PetscCall(MatSetSizes(*A, m, n, m, n));
2107:   PetscCall(MatSetType(*A, MATSEQSBAIJ));
2108:   PetscCall(MatSeqSBAIJSetPreallocation(*A, bs, nz, (PetscInt *)nnz));
2109:   PetscFunctionReturn(PETSC_SUCCESS);
2110: }

2112: PetscErrorCode MatDuplicate_SeqSBAIJ(Mat A, MatDuplicateOption cpvalues, Mat *B)
2113: {
2114:   Mat           C;
2115:   Mat_SeqSBAIJ *c, *a  = (Mat_SeqSBAIJ *)A->data;
2116:   PetscInt      i, mbs = a->mbs, nz = a->nz, bs2 = a->bs2;

2118:   PetscFunctionBegin;
2119:   PetscCheck(A->assembled, PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONGSTATE, "Cannot duplicate unassembled matrix");
2120:   PetscCheck(a->i[mbs] == nz, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Corrupt matrix");

2122:   *B = NULL;
2123:   PetscCall(MatCreate(PetscObjectComm((PetscObject)A), &C));
2124:   PetscCall(MatSetSizes(C, A->rmap->N, A->cmap->n, A->rmap->N, A->cmap->n));
2125:   PetscCall(MatSetBlockSizesFromMats(C, A, A));
2126:   PetscCall(MatSetType(C, MATSEQSBAIJ));
2127:   c = (Mat_SeqSBAIJ *)C->data;

2129:   C->preallocated       = PETSC_TRUE;
2130:   C->factortype         = A->factortype;
2131:   c->row                = NULL;
2132:   c->icol               = NULL;
2133:   c->saved_values       = NULL;
2134:   c->keepnonzeropattern = a->keepnonzeropattern;
2135:   C->assembled          = PETSC_TRUE;

2137:   PetscCall(PetscLayoutReference(A->rmap, &C->rmap));
2138:   PetscCall(PetscLayoutReference(A->cmap, &C->cmap));
2139:   c->bs2 = a->bs2;
2140:   c->mbs = a->mbs;
2141:   c->nbs = a->nbs;

2143:   if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
2144:     c->imax           = a->imax;
2145:     c->ilen           = a->ilen;
2146:     c->free_imax_ilen = PETSC_FALSE;
2147:   } else {
2148:     PetscCall(PetscMalloc2(mbs + 1, &c->imax, mbs + 1, &c->ilen));
2149:     for (i = 0; i < mbs; i++) {
2150:       c->imax[i] = a->imax[i];
2151:       c->ilen[i] = a->ilen[i];
2152:     }
2153:     c->free_imax_ilen = PETSC_TRUE;
2154:   }

2156:   /* allocate the matrix space */
2157:   PetscCall(PetscShmgetAllocateArray(bs2 * nz, sizeof(PetscScalar), (void **)&c->a));
2158:   c->free_a = PETSC_TRUE;
2159:   if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
2160:     PetscCall(PetscArrayzero(c->a, bs2 * nz));
2161:     c->i       = a->i;
2162:     c->j       = a->j;
2163:     c->free_ij = PETSC_FALSE;
2164:     c->parent  = A;
2165:     PetscCall(PetscObjectReference((PetscObject)A));
2166:     PetscCall(MatSetOption(A, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
2167:     PetscCall(MatSetOption(C, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE));
2168:   } else {
2169:     PetscCall(PetscShmgetAllocateArray(nz, sizeof(PetscInt), (void **)&c->j));
2170:     PetscCall(PetscShmgetAllocateArray(mbs + 1, sizeof(PetscInt), (void **)&c->i));
2171:     PetscCall(PetscArraycpy(c->i, a->i, mbs + 1));
2172:     c->free_ij = PETSC_TRUE;
2173:   }
2174:   if (mbs > 0) {
2175:     if (cpvalues != MAT_SHARE_NONZERO_PATTERN) PetscCall(PetscArraycpy(c->j, a->j, nz));
2176:     if (cpvalues == MAT_COPY_VALUES) {
2177:       PetscCall(PetscArraycpy(c->a, a->a, bs2 * nz));
2178:     } else {
2179:       PetscCall(PetscArrayzero(c->a, bs2 * nz));
2180:     }
2181:     if (a->jshort) {
2182:       /* cannot share jshort, it is reallocated in MatAssemblyEnd_SeqSBAIJ() */
2183:       /* if the parent matrix is reassembled, this child matrix will never notice */
2184:       PetscCall(PetscMalloc1(nz, &c->jshort));
2185:       PetscCall(PetscArraycpy(c->jshort, a->jshort, nz));

2187:       c->free_jshort = PETSC_TRUE;
2188:     }
2189:   }

2191:   c->roworiented = a->roworiented;
2192:   c->nonew       = a->nonew;
2193:   c->nz          = a->nz;
2194:   c->maxnz       = a->nz; /* Since we allocate exactly the right amount */
2195:   c->solve_work  = NULL;
2196:   c->mult_work   = NULL;

2198:   *B = C;
2199:   PetscCall(PetscFunctionListDuplicate(((PetscObject)A)->qlist, &((PetscObject)C)->qlist));
2200:   PetscFunctionReturn(PETSC_SUCCESS);
2201: }

2203: /* Used for both SeqBAIJ and SeqSBAIJ matrices */
2204: #define MatLoad_SeqSBAIJ_Binary MatLoad_SeqBAIJ_Binary

2206: PetscErrorCode MatLoad_SeqSBAIJ(Mat mat, PetscViewer viewer)
2207: {
2208:   PetscBool isbinary;

2210:   PetscFunctionBegin;
2211:   PetscCall(PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary));
2212:   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);
2213:   PetscCall(MatLoad_SeqSBAIJ_Binary(mat, viewer));
2214:   PetscFunctionReturn(PETSC_SUCCESS);
2215: }

2217: /*@
2218:   MatCreateSeqSBAIJWithArrays - Creates an sequential `MATSEQSBAIJ` matrix using matrix elements
2219:   (upper triangular entries in CSR format) provided by the user.

2221:   Collective

2223:   Input Parameters:
2224: + comm - must be an MPI communicator of size 1
2225: . bs   - size of block
2226: . m    - number of rows
2227: . n    - number of columns
2228: . 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
2229: . j    - column indices
2230: - a    - matrix values

2232:   Output Parameter:
2233: . mat - the matrix

2235:   Level: advanced

2237:   Notes:
2238:   The `i`, `j`, and `a` arrays are not copied by this routine, the user must free these arrays
2239:   once the matrix is destroyed

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

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

2245:   When block size is greater than 1 the matrix values must be stored using the `MATSBAIJ` storage format. For block size of 1
2246:   it is the regular CSR format excluding the lower triangular elements.

2248: .seealso: [](ch_matrices), `Mat`, `MATSEQSBAIJ`, `MatCreate()`, `MatCreateSBAIJ()`, `MatCreateSeqSBAIJ()`
2249: @*/
2250: PetscErrorCode MatCreateSeqSBAIJWithArrays(MPI_Comm comm, PetscInt bs, PetscInt m, PetscInt n, PetscInt i[], PetscInt j[], PetscScalar a[], Mat *mat)
2251: {
2252:   Mat_SeqSBAIJ *sbaij;

2254:   PetscFunctionBegin;
2255:   PetscCheck(bs == 1, PETSC_COMM_SELF, PETSC_ERR_SUP, "block size %" PetscInt_FMT " > 1 is not supported yet", bs);
2256:   PetscCheck(m == 0 || i[0] == 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "i (row indices) must start with 0");

2258:   PetscCall(MatCreate(comm, mat));
2259:   PetscCall(MatSetSizes(*mat, m, n, m, n));
2260:   PetscCall(MatSetType(*mat, MATSEQSBAIJ));
2261:   PetscCall(MatSeqSBAIJSetPreallocation(*mat, bs, MAT_SKIP_ALLOCATION, NULL));
2262:   sbaij = (Mat_SeqSBAIJ *)(*mat)->data;
2263:   PetscCall(PetscMalloc2(m, &sbaij->imax, m, &sbaij->ilen));

2265:   sbaij->i = i;
2266:   sbaij->j = j;
2267:   sbaij->a = a;

2269:   sbaij->nonew          = -1; /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
2270:   sbaij->free_a         = PETSC_FALSE;
2271:   sbaij->free_ij        = PETSC_FALSE;
2272:   sbaij->free_imax_ilen = PETSC_TRUE;

2274:   for (PetscInt ii = 0; ii < m; ii++) {
2275:     sbaij->ilen[ii] = sbaij->imax[ii] = i[ii + 1] - i[ii];
2276:     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]);
2277:   }
2278:   if (PetscDefined(USE_DEBUG)) {
2279:     for (PetscInt ii = 0; ii < sbaij->i[m]; ii++) {
2280:       PetscCheck(j[ii] >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Negative column index at location = %" PetscInt_FMT " index = %" PetscInt_FMT, ii, j[ii]);
2281:       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]);
2282:     }
2283:   }

2285:   PetscCall(MatAssemblyBegin(*mat, MAT_FINAL_ASSEMBLY));
2286:   PetscCall(MatAssemblyEnd(*mat, MAT_FINAL_ASSEMBLY));
2287:   PetscFunctionReturn(PETSC_SUCCESS);
2288: }

2290: PetscErrorCode MatCreateMPIMatConcatenateSeqMat_SeqSBAIJ(MPI_Comm comm, Mat inmat, PetscInt n, MatReuse scall, Mat *outmat)
2291: {
2292:   PetscFunctionBegin;
2293:   PetscCall(MatCreateMPIMatConcatenateSeqMat_MPISBAIJ(comm, inmat, n, scall, outmat));
2294:   PetscFunctionReturn(PETSC_SUCCESS);
2295: }