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