Actual source code: aij.c

  1: /*
  2:     Defines the basic matrix operations for the AIJ (compressed row)
  3:   matrix storage format.
  4: */

  6: #include <../src/mat/impls/aij/seq/aij.h>
  7: #include <petscblaslapack.h>
  8: #include <petscbt.h>
  9: #include <petsc/private/kernels/blocktranspose.h>

 11: PetscErrorCode MatSeqAIJSetTypeFromOptions(Mat A)
 12: {
 13:   PetscBool flg;
 14:   char      type[256];

 16:   PetscObjectOptionsBegin((PetscObject)A);
 17:   PetscOptionsFList("-mat_seqaij_type", "Matrix SeqAIJ type", "MatSeqAIJSetType", MatSeqAIJList, "seqaij", type, 256, &flg);
 18:   if (flg) MatSeqAIJSetType(A, type);
 19:   PetscOptionsEnd();
 20:   return 0;
 21: }

 23: PetscErrorCode MatGetColumnReductions_SeqAIJ(Mat A, PetscInt type, PetscReal *reductions)
 24: {
 25:   PetscInt    i, m, n;
 26:   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;

 28:   MatGetSize(A, &m, &n);
 29:   PetscArrayzero(reductions, n);
 30:   if (type == NORM_2) {
 31:     for (i = 0; i < aij->i[m]; i++) reductions[aij->j[i]] += PetscAbsScalar(aij->a[i] * aij->a[i]);
 32:   } else if (type == NORM_1) {
 33:     for (i = 0; i < aij->i[m]; i++) reductions[aij->j[i]] += PetscAbsScalar(aij->a[i]);
 34:   } else if (type == NORM_INFINITY) {
 35:     for (i = 0; i < aij->i[m]; i++) reductions[aij->j[i]] = PetscMax(PetscAbsScalar(aij->a[i]), reductions[aij->j[i]]);
 36:   } else if (type == REDUCTION_SUM_REALPART || type == REDUCTION_MEAN_REALPART) {
 37:     for (i = 0; i < aij->i[m]; i++) reductions[aij->j[i]] += PetscRealPart(aij->a[i]);
 38:   } else if (type == REDUCTION_SUM_IMAGINARYPART || type == REDUCTION_MEAN_IMAGINARYPART) {
 39:     for (i = 0; i < aij->i[m]; i++) reductions[aij->j[i]] += PetscImaginaryPart(aij->a[i]);
 40:   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Unknown reduction type");

 42:   if (type == NORM_2) {
 43:     for (i = 0; i < n; i++) reductions[i] = PetscSqrtReal(reductions[i]);
 44:   } else if (type == REDUCTION_MEAN_REALPART || type == REDUCTION_MEAN_IMAGINARYPART) {
 45:     for (i = 0; i < n; i++) reductions[i] /= m;
 46:   }
 47:   return 0;
 48: }

 50: PetscErrorCode MatFindOffBlockDiagonalEntries_SeqAIJ(Mat A, IS *is)
 51: {
 52:   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data;
 53:   PetscInt        i, m = A->rmap->n, cnt = 0, bs = A->rmap->bs;
 54:   const PetscInt *jj = a->j, *ii = a->i;
 55:   PetscInt       *rows;

 57:   for (i = 0; i < m; i++) {
 58:     if ((ii[i] != ii[i + 1]) && ((jj[ii[i]] < bs * (i / bs)) || (jj[ii[i + 1] - 1] > bs * ((i + bs) / bs) - 1))) cnt++;
 59:   }
 60:   PetscMalloc1(cnt, &rows);
 61:   cnt = 0;
 62:   for (i = 0; i < m; i++) {
 63:     if ((ii[i] != ii[i + 1]) && ((jj[ii[i]] < bs * (i / bs)) || (jj[ii[i + 1] - 1] > bs * ((i + bs) / bs) - 1))) {
 64:       rows[cnt] = i;
 65:       cnt++;
 66:     }
 67:   }
 68:   ISCreateGeneral(PETSC_COMM_SELF, cnt, rows, PETSC_OWN_POINTER, is);
 69:   return 0;
 70: }

 72: PetscErrorCode MatFindZeroDiagonals_SeqAIJ_Private(Mat A, PetscInt *nrows, PetscInt **zrows)
 73: {
 74:   Mat_SeqAIJ      *a = (Mat_SeqAIJ *)A->data;
 75:   const MatScalar *aa;
 76:   PetscInt         i, m = A->rmap->n, cnt = 0;
 77:   const PetscInt  *ii = a->i, *jj = a->j, *diag;
 78:   PetscInt        *rows;

 80:   MatSeqAIJGetArrayRead(A, &aa);
 81:   MatMarkDiagonal_SeqAIJ(A);
 82:   diag = a->diag;
 83:   for (i = 0; i < m; i++) {
 84:     if ((diag[i] >= ii[i + 1]) || (jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) cnt++;
 85:   }
 86:   PetscMalloc1(cnt, &rows);
 87:   cnt = 0;
 88:   for (i = 0; i < m; i++) {
 89:     if ((diag[i] >= ii[i + 1]) || (jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) rows[cnt++] = i;
 90:   }
 91:   *nrows = cnt;
 92:   *zrows = rows;
 93:   MatSeqAIJRestoreArrayRead(A, &aa);
 94:   return 0;
 95: }

 97: PetscErrorCode MatFindZeroDiagonals_SeqAIJ(Mat A, IS *zrows)
 98: {
 99:   PetscInt nrows, *rows;

101:   *zrows = NULL;
102:   MatFindZeroDiagonals_SeqAIJ_Private(A, &nrows, &rows);
103:   ISCreateGeneral(PetscObjectComm((PetscObject)A), nrows, rows, PETSC_OWN_POINTER, zrows);
104:   return 0;
105: }

107: PetscErrorCode MatFindNonzeroRows_SeqAIJ(Mat A, IS *keptrows)
108: {
109:   Mat_SeqAIJ      *a = (Mat_SeqAIJ *)A->data;
110:   const MatScalar *aa;
111:   PetscInt         m = A->rmap->n, cnt = 0;
112:   const PetscInt  *ii;
113:   PetscInt         n, i, j, *rows;

115:   MatSeqAIJGetArrayRead(A, &aa);
116:   *keptrows = NULL;
117:   ii        = a->i;
118:   for (i = 0; i < m; i++) {
119:     n = ii[i + 1] - ii[i];
120:     if (!n) {
121:       cnt++;
122:       goto ok1;
123:     }
124:     for (j = ii[i]; j < ii[i + 1]; j++) {
125:       if (aa[j] != 0.0) goto ok1;
126:     }
127:     cnt++;
128:   ok1:;
129:   }
130:   if (!cnt) {
131:     MatSeqAIJRestoreArrayRead(A, &aa);
132:     return 0;
133:   }
134:   PetscMalloc1(A->rmap->n - cnt, &rows);
135:   cnt = 0;
136:   for (i = 0; i < m; i++) {
137:     n = ii[i + 1] - ii[i];
138:     if (!n) continue;
139:     for (j = ii[i]; j < ii[i + 1]; j++) {
140:       if (aa[j] != 0.0) {
141:         rows[cnt++] = i;
142:         break;
143:       }
144:     }
145:   }
146:   MatSeqAIJRestoreArrayRead(A, &aa);
147:   ISCreateGeneral(PETSC_COMM_SELF, cnt, rows, PETSC_OWN_POINTER, keptrows);
148:   return 0;
149: }

151: PetscErrorCode MatDiagonalSet_SeqAIJ(Mat Y, Vec D, InsertMode is)
152: {
153:   Mat_SeqAIJ        *aij = (Mat_SeqAIJ *)Y->data;
154:   PetscInt           i, m = Y->rmap->n;
155:   const PetscInt    *diag;
156:   MatScalar         *aa;
157:   const PetscScalar *v;
158:   PetscBool          missing;

160:   if (Y->assembled) {
161:     MatMissingDiagonal_SeqAIJ(Y, &missing, NULL);
162:     if (!missing) {
163:       diag = aij->diag;
164:       VecGetArrayRead(D, &v);
165:       MatSeqAIJGetArray(Y, &aa);
166:       if (is == INSERT_VALUES) {
167:         for (i = 0; i < m; i++) aa[diag[i]] = v[i];
168:       } else {
169:         for (i = 0; i < m; i++) aa[diag[i]] += v[i];
170:       }
171:       MatSeqAIJRestoreArray(Y, &aa);
172:       VecRestoreArrayRead(D, &v);
173:       return 0;
174:     }
175:     MatSeqAIJInvalidateDiagonal(Y);
176:   }
177:   MatDiagonalSet_Default(Y, D, is);
178:   return 0;
179: }

181: PetscErrorCode MatGetRowIJ_SeqAIJ(Mat A, PetscInt oshift, PetscBool symmetric, PetscBool inodecompressed, PetscInt *m, const PetscInt *ia[], const PetscInt *ja[], PetscBool *done)
182: {
183:   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
184:   PetscInt    i, ishift;

186:   if (m) *m = A->rmap->n;
187:   if (!ia) return 0;
188:   ishift = 0;
189:   if (symmetric && A->structurally_symmetric != PETSC_BOOL3_TRUE) {
190:     MatToSymmetricIJ_SeqAIJ(A->rmap->n, a->i, a->j, PETSC_TRUE, ishift, oshift, (PetscInt **)ia, (PetscInt **)ja);
191:   } else if (oshift == 1) {
192:     PetscInt *tia;
193:     PetscInt  nz = a->i[A->rmap->n];
194:     /* malloc space and  add 1 to i and j indices */
195:     PetscMalloc1(A->rmap->n + 1, &tia);
196:     for (i = 0; i < A->rmap->n + 1; i++) tia[i] = a->i[i] + 1;
197:     *ia = tia;
198:     if (ja) {
199:       PetscInt *tja;
200:       PetscMalloc1(nz + 1, &tja);
201:       for (i = 0; i < nz; i++) tja[i] = a->j[i] + 1;
202:       *ja = tja;
203:     }
204:   } else {
205:     *ia = a->i;
206:     if (ja) *ja = a->j;
207:   }
208:   return 0;
209: }

211: PetscErrorCode MatRestoreRowIJ_SeqAIJ(Mat A, PetscInt oshift, PetscBool symmetric, PetscBool inodecompressed, PetscInt *n, const PetscInt *ia[], const PetscInt *ja[], PetscBool *done)
212: {
213:   if (!ia) return 0;
214:   if ((symmetric && A->structurally_symmetric != PETSC_BOOL3_TRUE) || oshift == 1) {
215:     PetscFree(*ia);
216:     if (ja) PetscFree(*ja);
217:   }
218:   return 0;
219: }

221: PetscErrorCode MatGetColumnIJ_SeqAIJ(Mat A, PetscInt oshift, PetscBool symmetric, PetscBool inodecompressed, PetscInt *nn, const PetscInt *ia[], const PetscInt *ja[], PetscBool *done)
222: {
223:   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
224:   PetscInt    i, *collengths, *cia, *cja, n = A->cmap->n, m = A->rmap->n;
225:   PetscInt    nz = a->i[m], row, *jj, mr, col;

227:   *nn = n;
228:   if (!ia) return 0;
229:   if (symmetric) {
230:     MatToSymmetricIJ_SeqAIJ(A->rmap->n, a->i, a->j, PETSC_TRUE, 0, oshift, (PetscInt **)ia, (PetscInt **)ja);
231:   } else {
232:     PetscCalloc1(n, &collengths);
233:     PetscMalloc1(n + 1, &cia);
234:     PetscMalloc1(nz, &cja);
235:     jj = a->j;
236:     for (i = 0; i < nz; i++) collengths[jj[i]]++;
237:     cia[0] = oshift;
238:     for (i = 0; i < n; i++) cia[i + 1] = cia[i] + collengths[i];
239:     PetscArrayzero(collengths, n);
240:     jj = a->j;
241:     for (row = 0; row < m; row++) {
242:       mr = a->i[row + 1] - a->i[row];
243:       for (i = 0; i < mr; i++) {
244:         col = *jj++;

246:         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
247:       }
248:     }
249:     PetscFree(collengths);
250:     *ia = cia;
251:     *ja = cja;
252:   }
253:   return 0;
254: }

256: PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat A, PetscInt oshift, PetscBool symmetric, PetscBool inodecompressed, PetscInt *n, const PetscInt *ia[], const PetscInt *ja[], PetscBool *done)
257: {
258:   if (!ia) return 0;

260:   PetscFree(*ia);
261:   PetscFree(*ja);
262:   return 0;
263: }

265: /*
266:  MatGetColumnIJ_SeqAIJ_Color() and MatRestoreColumnIJ_SeqAIJ_Color() are customized from
267:  MatGetColumnIJ_SeqAIJ() and MatRestoreColumnIJ_SeqAIJ() by adding an output
268:  spidx[], index of a->a, to be used in MatTransposeColoringCreate_SeqAIJ() and MatFDColoringCreate_SeqXAIJ()
269: */
270: PetscErrorCode MatGetColumnIJ_SeqAIJ_Color(Mat A, PetscInt oshift, PetscBool symmetric, PetscBool inodecompressed, PetscInt *nn, const PetscInt *ia[], const PetscInt *ja[], PetscInt *spidx[], PetscBool *done)
271: {
272:   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data;
273:   PetscInt        i, *collengths, *cia, *cja, n = A->cmap->n, m = A->rmap->n;
274:   PetscInt        nz = a->i[m], row, mr, col, tmp;
275:   PetscInt       *cspidx;
276:   const PetscInt *jj;

278:   *nn = n;
279:   if (!ia) return 0;

281:   PetscCalloc1(n, &collengths);
282:   PetscMalloc1(n + 1, &cia);
283:   PetscMalloc1(nz, &cja);
284:   PetscMalloc1(nz, &cspidx);
285:   jj = a->j;
286:   for (i = 0; i < nz; i++) collengths[jj[i]]++;
287:   cia[0] = oshift;
288:   for (i = 0; i < n; i++) cia[i + 1] = cia[i] + collengths[i];
289:   PetscArrayzero(collengths, n);
290:   jj = a->j;
291:   for (row = 0; row < m; row++) {
292:     mr = a->i[row + 1] - a->i[row];
293:     for (i = 0; i < mr; i++) {
294:       col         = *jj++;
295:       tmp         = cia[col] + collengths[col]++ - oshift;
296:       cspidx[tmp] = a->i[row] + i; /* index of a->j */
297:       cja[tmp]    = row + oshift;
298:     }
299:   }
300:   PetscFree(collengths);
301:   *ia    = cia;
302:   *ja    = cja;
303:   *spidx = cspidx;
304:   return 0;
305: }

307: PetscErrorCode MatRestoreColumnIJ_SeqAIJ_Color(Mat A, PetscInt oshift, PetscBool symmetric, PetscBool inodecompressed, PetscInt *n, const PetscInt *ia[], const PetscInt *ja[], PetscInt *spidx[], PetscBool *done)
308: {
309:   MatRestoreColumnIJ_SeqAIJ(A, oshift, symmetric, inodecompressed, n, ia, ja, done);
310:   PetscFree(*spidx);
311:   return 0;
312: }

314: PetscErrorCode MatSetValuesRow_SeqAIJ(Mat A, PetscInt row, const PetscScalar v[])
315: {
316:   Mat_SeqAIJ  *a  = (Mat_SeqAIJ *)A->data;
317:   PetscInt    *ai = a->i;
318:   PetscScalar *aa;

320:   MatSeqAIJGetArray(A, &aa);
321:   PetscArraycpy(aa + ai[row], v, ai[row + 1] - ai[row]);
322:   MatSeqAIJRestoreArray(A, &aa);
323:   return 0;
324: }

326: /*
327:     MatSeqAIJSetValuesLocalFast - An optimized version of MatSetValuesLocal() for SeqAIJ matrices with several assumptions

329:       -   a single row of values is set with each call
330:       -   no row or column indices are negative or (in error) larger than the number of rows or columns
331:       -   the values are always added to the matrix, not set
332:       -   no new locations are introduced in the nonzero structure of the matrix

334:      This does NOT assume the global column indices are sorted

336: */

338: #include <petsc/private/isimpl.h>
339: PetscErrorCode MatSeqAIJSetValuesLocalFast(Mat A, PetscInt m, const PetscInt im[], PetscInt n, const PetscInt in[], const PetscScalar v[], InsertMode is)
340: {
341:   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data;
342:   PetscInt        low, high, t, row, nrow, i, col, l;
343:   const PetscInt *rp, *ai = a->i, *ailen = a->ilen, *aj = a->j;
344:   PetscInt        lastcol = -1;
345:   MatScalar      *ap, value, *aa;
346:   const PetscInt *ridx = A->rmap->mapping->indices, *cidx = A->cmap->mapping->indices;

348:   MatSeqAIJGetArray(A, &aa);
349:   row  = ridx[im[0]];
350:   rp   = aj + ai[row];
351:   ap   = aa + ai[row];
352:   nrow = ailen[row];
353:   low  = 0;
354:   high = nrow;
355:   for (l = 0; l < n; l++) { /* loop over added columns */
356:     col   = cidx[in[l]];
357:     value = v[l];

359:     if (col <= lastcol) low = 0;
360:     else high = nrow;
361:     lastcol = col;
362:     while (high - low > 5) {
363:       t = (low + high) / 2;
364:       if (rp[t] > col) high = t;
365:       else low = t;
366:     }
367:     for (i = low; i < high; i++) {
368:       if (rp[i] == col) {
369:         ap[i] += value;
370:         low = i + 1;
371:         break;
372:       }
373:     }
374:   }
375:   MatSeqAIJRestoreArray(A, &aa);
376:   return 0;
377: }

379: PetscErrorCode MatSetValues_SeqAIJ(Mat A, PetscInt m, const PetscInt im[], PetscInt n, const PetscInt in[], const PetscScalar v[], InsertMode is)
380: {
381:   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
382:   PetscInt   *rp, k, low, high, t, ii, row, nrow, i, col, l, rmax, N;
383:   PetscInt   *imax = a->imax, *ai = a->i, *ailen = a->ilen;
384:   PetscInt   *aj = a->j, nonew = a->nonew, lastcol = -1;
385:   MatScalar  *ap = NULL, value = 0.0, *aa;
386:   PetscBool   ignorezeroentries = a->ignorezeroentries;
387:   PetscBool   roworiented       = a->roworiented;

389:   MatSeqAIJGetArray(A, &aa);
390:   for (k = 0; k < m; k++) { /* loop over added rows */
391:     row = im[k];
392:     if (row < 0) continue;
394:     rp = aj + ai[row];
395:     if (!A->structure_only) ap = aa + ai[row];
396:     rmax = imax[row];
397:     nrow = ailen[row];
398:     low  = 0;
399:     high = nrow;
400:     for (l = 0; l < n; l++) { /* loop over added columns */
401:       if (in[l] < 0) continue;
403:       col = in[l];
404:       if (v && !A->structure_only) value = roworiented ? v[l + k * n] : v[k + l * m];
405:       if (!A->structure_only && value == 0.0 && ignorezeroentries && is == ADD_VALUES && row != col) continue;

407:       if (col <= lastcol) low = 0;
408:       else high = nrow;
409:       lastcol = col;
410:       while (high - low > 5) {
411:         t = (low + high) / 2;
412:         if (rp[t] > col) high = t;
413:         else low = t;
414:       }
415:       for (i = low; i < high; i++) {
416:         if (rp[i] > col) break;
417:         if (rp[i] == col) {
418:           if (!A->structure_only) {
419:             if (is == ADD_VALUES) {
420:               ap[i] += value;
421:               (void)PetscLogFlops(1.0);
422:             } else ap[i] = value;
423:           }
424:           low = i + 1;
425:           goto noinsert;
426:         }
427:       }
428:       if (value == 0.0 && ignorezeroentries && row != col) goto noinsert;
429:       if (nonew == 1) goto noinsert;
431:       if (A->structure_only) {
432:         MatSeqXAIJReallocateAIJ_structure_only(A, A->rmap->n, 1, nrow, row, col, rmax, ai, aj, rp, imax, nonew, MatScalar);
433:       } else {
434:         MatSeqXAIJReallocateAIJ(A, A->rmap->n, 1, nrow, row, col, rmax, aa, ai, aj, rp, ap, imax, nonew, MatScalar);
435:       }
436:       N = nrow++ - 1;
437:       a->nz++;
438:       high++;
439:       /* shift up all the later entries in this row */
440:       PetscArraymove(rp + i + 1, rp + i, N - i + 1);
441:       rp[i] = col;
442:       if (!A->structure_only) {
443:         PetscArraymove(ap + i + 1, ap + i, N - i + 1);
444:         ap[i] = value;
445:       }
446:       low = i + 1;
447:       A->nonzerostate++;
448:     noinsert:;
449:     }
450:     ailen[row] = nrow;
451:   }
452:   MatSeqAIJRestoreArray(A, &aa);
453:   return 0;
454: }

456: PetscErrorCode MatSetValues_SeqAIJ_SortedFullNoPreallocation(Mat A, PetscInt m, const PetscInt im[], PetscInt n, const PetscInt in[], const PetscScalar v[], InsertMode is)
457: {
458:   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
459:   PetscInt   *rp, k, row;
460:   PetscInt   *ai = a->i;
461:   PetscInt   *aj = a->j;
462:   MatScalar  *aa, *ap;


467:   MatSeqAIJGetArray(A, &aa);
468:   for (k = 0; k < m; k++) { /* loop over added rows */
469:     row = im[k];
470:     rp  = aj + ai[row];
471:     ap  = aa + ai[row];

473:     PetscMemcpy(rp, in, n * sizeof(PetscInt));
474:     if (!A->structure_only) {
475:       if (v) {
476:         PetscMemcpy(ap, v, n * sizeof(PetscScalar));
477:         v += n;
478:       } else {
479:         PetscMemzero(ap, n * sizeof(PetscScalar));
480:       }
481:     }
482:     a->ilen[row]  = n;
483:     a->imax[row]  = n;
484:     a->i[row + 1] = a->i[row] + n;
485:     a->nz += n;
486:   }
487:   MatSeqAIJRestoreArray(A, &aa);
488:   return 0;
489: }

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

494:   Input Parameters:
495: +  A - the `MATSEQAIJ` matrix
496: -  nztotal - bound on the number of nonzeros

498:   Level: advanced

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

505: .seealso: `MatSetOption()`, `MAT_SORTED_FULL`, `MatSetValues()`, `MatSeqAIJSetPreallocation()`
506: @*/

508: PetscErrorCode MatSeqAIJSetTotalPreallocation(Mat A, PetscInt nztotal)
509: {
510:   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;

512:   PetscLayoutSetUp(A->rmap);
513:   PetscLayoutSetUp(A->cmap);
514:   a->maxnz = nztotal;
515:   if (!a->imax) { PetscMalloc1(A->rmap->n, &a->imax); }
516:   if (!a->ilen) {
517:     PetscMalloc1(A->rmap->n, &a->ilen);
518:   } else {
519:     PetscMemzero(a->ilen, A->rmap->n * sizeof(PetscInt));
520:   }

522:   /* allocate the matrix space */
523:   if (A->structure_only) {
524:     PetscMalloc1(nztotal, &a->j);
525:     PetscMalloc1(A->rmap->n + 1, &a->i);
526:   } else {
527:     PetscMalloc3(nztotal, &a->a, nztotal, &a->j, A->rmap->n + 1, &a->i);
528:   }
529:   a->i[0] = 0;
530:   if (A->structure_only) {
531:     a->singlemalloc = PETSC_FALSE;
532:     a->free_a       = PETSC_FALSE;
533:   } else {
534:     a->singlemalloc = PETSC_TRUE;
535:     a->free_a       = PETSC_TRUE;
536:   }
537:   a->free_ij        = PETSC_TRUE;
538:   A->ops->setvalues = MatSetValues_SeqAIJ_SortedFullNoPreallocation;
539:   A->preallocated   = PETSC_TRUE;
540:   return 0;
541: }

543: PetscErrorCode MatSetValues_SeqAIJ_SortedFull(Mat A, PetscInt m, const PetscInt im[], PetscInt n, const PetscInt in[], const PetscScalar v[], InsertMode is)
544: {
545:   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
546:   PetscInt   *rp, k, row;
547:   PetscInt   *ai = a->i, *ailen = a->ilen;
548:   PetscInt   *aj = a->j;
549:   MatScalar  *aa, *ap;

551:   MatSeqAIJGetArray(A, &aa);
552:   for (k = 0; k < m; k++) { /* loop over added rows */
553:     row = im[k];
555:     rp = aj + ai[row];
556:     ap = aa + ai[row];
557:     if (!A->was_assembled) PetscMemcpy(rp, in, n * sizeof(PetscInt));
558:     if (!A->structure_only) {
559:       if (v) {
560:         PetscMemcpy(ap, v, n * sizeof(PetscScalar));
561:         v += n;
562:       } else {
563:         PetscMemzero(ap, n * sizeof(PetscScalar));
564:       }
565:     }
566:     ailen[row] = n;
567:     a->nz += n;
568:   }
569:   MatSeqAIJRestoreArray(A, &aa);
570:   return 0;
571: }

573: PetscErrorCode MatGetValues_SeqAIJ(Mat A, PetscInt m, const PetscInt im[], PetscInt n, const PetscInt in[], PetscScalar v[])
574: {
575:   Mat_SeqAIJ      *a = (Mat_SeqAIJ *)A->data;
576:   PetscInt        *rp, k, low, high, t, row, nrow, i, col, l, *aj = a->j;
577:   PetscInt        *ai = a->i, *ailen = a->ilen;
578:   const MatScalar *ap, *aa;

580:   MatSeqAIJGetArrayRead(A, &aa);
581:   for (k = 0; k < m; k++) { /* loop over rows */
582:     row = im[k];
583:     if (row < 0) {
584:       v += n;
585:       continue;
586:     } /* negative row */
588:     rp   = aj + ai[row];
589:     ap   = aa + ai[row];
590:     nrow = ailen[row];
591:     for (l = 0; l < n; l++) { /* loop over columns */
592:       if (in[l] < 0) {
593:         v++;
594:         continue;
595:       } /* negative column */
597:       col  = in[l];
598:       high = nrow;
599:       low  = 0; /* assume unsorted */
600:       while (high - low > 5) {
601:         t = (low + high) / 2;
602:         if (rp[t] > col) high = t;
603:         else low = t;
604:       }
605:       for (i = low; i < high; i++) {
606:         if (rp[i] > col) break;
607:         if (rp[i] == col) {
608:           *v++ = ap[i];
609:           goto finished;
610:         }
611:       }
612:       *v++ = 0.0;
613:     finished:;
614:     }
615:   }
616:   MatSeqAIJRestoreArrayRead(A, &aa);
617:   return 0;
618: }

620: PetscErrorCode MatView_SeqAIJ_Binary(Mat mat, PetscViewer viewer)
621: {
622:   Mat_SeqAIJ        *A = (Mat_SeqAIJ *)mat->data;
623:   const PetscScalar *av;
624:   PetscInt           header[4], M, N, m, nz, i;
625:   PetscInt          *rowlens;

627:   PetscViewerSetUp(viewer);

629:   M  = mat->rmap->N;
630:   N  = mat->cmap->N;
631:   m  = mat->rmap->n;
632:   nz = A->nz;

634:   /* write matrix header */
635:   header[0] = MAT_FILE_CLASSID;
636:   header[1] = M;
637:   header[2] = N;
638:   header[3] = nz;
639:   PetscViewerBinaryWrite(viewer, header, 4, PETSC_INT);

641:   /* fill in and store row lengths */
642:   PetscMalloc1(m, &rowlens);
643:   for (i = 0; i < m; i++) rowlens[i] = A->i[i + 1] - A->i[i];
644:   PetscViewerBinaryWrite(viewer, rowlens, m, PETSC_INT);
645:   PetscFree(rowlens);
646:   /* store column indices */
647:   PetscViewerBinaryWrite(viewer, A->j, nz, PETSC_INT);
648:   /* store nonzero values */
649:   MatSeqAIJGetArrayRead(mat, &av);
650:   PetscViewerBinaryWrite(viewer, av, nz, PETSC_SCALAR);
651:   MatSeqAIJRestoreArrayRead(mat, &av);

653:   /* write block size option to the viewer's .info file */
654:   MatView_Binary_BlockSizes(mat, viewer);
655:   return 0;
656: }

658: static PetscErrorCode MatView_SeqAIJ_ASCII_structonly(Mat A, PetscViewer viewer)
659: {
660:   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
661:   PetscInt    i, k, m = A->rmap->N;

663:   PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);
664:   for (i = 0; i < m; i++) {
665:     PetscViewerASCIIPrintf(viewer, "row %" PetscInt_FMT ":", i);
666:     for (k = a->i[i]; k < a->i[i + 1]; k++) PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ") ", a->j[k]);
667:     PetscViewerASCIIPrintf(viewer, "\n");
668:   }
669:   PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);
670:   return 0;
671: }

673: extern PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat, PetscViewer);

675: PetscErrorCode MatView_SeqAIJ_ASCII(Mat A, PetscViewer viewer)
676: {
677:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data;
678:   const PetscScalar *av;
679:   PetscInt           i, j, m = A->rmap->n;
680:   const char        *name;
681:   PetscViewerFormat  format;

683:   if (A->structure_only) {
684:     MatView_SeqAIJ_ASCII_structonly(A, viewer);
685:     return 0;
686:   }

688:   PetscViewerGetFormat(viewer, &format);
689:   if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) return 0;

691:   /* trigger copy to CPU if needed */
692:   MatSeqAIJGetArrayRead(A, &av);
693:   MatSeqAIJRestoreArrayRead(A, &av);
694:   if (format == PETSC_VIEWER_ASCII_MATLAB) {
695:     PetscInt nofinalvalue = 0;
696:     if (m && ((a->i[m] == a->i[m - 1]) || (a->j[a->nz - 1] != A->cmap->n - 1))) {
697:       /* Need a dummy value to ensure the dimension of the matrix. */
698:       nofinalvalue = 1;
699:     }
700:     PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);
701:     PetscViewerASCIIPrintf(viewer, "%% Size = %" PetscInt_FMT " %" PetscInt_FMT " \n", m, A->cmap->n);
702:     PetscViewerASCIIPrintf(viewer, "%% Nonzeros = %" PetscInt_FMT " \n", a->nz);
703: #if defined(PETSC_USE_COMPLEX)
704:     PetscViewerASCIIPrintf(viewer, "zzz = zeros(%" PetscInt_FMT ",4);\n", a->nz + nofinalvalue);
705: #else
706:     PetscViewerASCIIPrintf(viewer, "zzz = zeros(%" PetscInt_FMT ",3);\n", a->nz + nofinalvalue);
707: #endif
708:     PetscViewerASCIIPrintf(viewer, "zzz = [\n");

710:     for (i = 0; i < m; i++) {
711:       for (j = a->i[i]; j < a->i[i + 1]; j++) {
712: #if defined(PETSC_USE_COMPLEX)
713:         PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT " %" PetscInt_FMT "  %18.16e %18.16e\n", i + 1, a->j[j] + 1, (double)PetscRealPart(a->a[j]), (double)PetscImaginaryPart(a->a[j]));
714: #else
715:         PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT " %" PetscInt_FMT "  %18.16e\n", i + 1, a->j[j] + 1, (double)a->a[j]);
716: #endif
717:       }
718:     }
719:     if (nofinalvalue) {
720: #if defined(PETSC_USE_COMPLEX)
721:       PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT " %" PetscInt_FMT "  %18.16e %18.16e\n", m, A->cmap->n, 0., 0.);
722: #else
723:       PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT " %" PetscInt_FMT "  %18.16e\n", m, A->cmap->n, 0.0);
724: #endif
725:     }
726:     PetscObjectGetName((PetscObject)A, &name);
727:     PetscViewerASCIIPrintf(viewer, "];\n %s = spconvert(zzz);\n", name);
728:     PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);
729:   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
730:     PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);
731:     for (i = 0; i < m; i++) {
732:       PetscViewerASCIIPrintf(viewer, "row %" PetscInt_FMT ":", i);
733:       for (j = a->i[i]; j < a->i[i + 1]; j++) {
734: #if defined(PETSC_USE_COMPLEX)
735:         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
736:           PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g + %g i)", a->j[j], (double)PetscRealPart(a->a[j]), (double)PetscImaginaryPart(a->a[j]));
737:         } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
738:           PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g - %g i)", a->j[j], (double)PetscRealPart(a->a[j]), (double)-PetscImaginaryPart(a->a[j]));
739:         } else if (PetscRealPart(a->a[j]) != 0.0) {
740:           PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g) ", a->j[j], (double)PetscRealPart(a->a[j]));
741:         }
742: #else
743:         if (a->a[j] != 0.0) PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g) ", a->j[j], (double)a->a[j]);
744: #endif
745:       }
746:       PetscViewerASCIIPrintf(viewer, "\n");
747:     }
748:     PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);
749:   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
750:     PetscInt nzd = 0, fshift = 1, *sptr;
751:     PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);
752:     PetscMalloc1(m + 1, &sptr);
753:     for (i = 0; i < m; i++) {
754:       sptr[i] = nzd + 1;
755:       for (j = a->i[i]; j < a->i[i + 1]; j++) {
756:         if (a->j[j] >= i) {
757: #if defined(PETSC_USE_COMPLEX)
758:           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
759: #else
760:           if (a->a[j] != 0.0) nzd++;
761: #endif
762:         }
763:       }
764:     }
765:     sptr[m] = nzd + 1;
766:     PetscViewerASCIIPrintf(viewer, " %" PetscInt_FMT " %" PetscInt_FMT "\n\n", m, nzd);
767:     for (i = 0; i < m + 1; i += 6) {
768:       if (i + 4 < m) {
769:         PetscViewerASCIIPrintf(viewer, " %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT "\n", sptr[i], sptr[i + 1], sptr[i + 2], sptr[i + 3], sptr[i + 4], sptr[i + 5]);
770:       } else if (i + 3 < m) {
771:         PetscViewerASCIIPrintf(viewer, " %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT "\n", sptr[i], sptr[i + 1], sptr[i + 2], sptr[i + 3], sptr[i + 4]);
772:       } else if (i + 2 < m) {
773:         PetscViewerASCIIPrintf(viewer, " %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT "\n", sptr[i], sptr[i + 1], sptr[i + 2], sptr[i + 3]);
774:       } else if (i + 1 < m) {
775:         PetscViewerASCIIPrintf(viewer, " %" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT "\n", sptr[i], sptr[i + 1], sptr[i + 2]);
776:       } else if (i < m) {
777:         PetscViewerASCIIPrintf(viewer, " %" PetscInt_FMT " %" PetscInt_FMT "\n", sptr[i], sptr[i + 1]);
778:       } else {
779:         PetscViewerASCIIPrintf(viewer, " %" PetscInt_FMT "\n", sptr[i]);
780:       }
781:     }
782:     PetscViewerASCIIPrintf(viewer, "\n");
783:     PetscFree(sptr);
784:     for (i = 0; i < m; i++) {
785:       for (j = a->i[i]; j < a->i[i + 1]; j++) {
786:         if (a->j[j] >= i) PetscViewerASCIIPrintf(viewer, " %" PetscInt_FMT " ", a->j[j] + fshift);
787:       }
788:       PetscViewerASCIIPrintf(viewer, "\n");
789:     }
790:     PetscViewerASCIIPrintf(viewer, "\n");
791:     for (i = 0; i < m; i++) {
792:       for (j = a->i[i]; j < a->i[i + 1]; j++) {
793:         if (a->j[j] >= i) {
794: #if defined(PETSC_USE_COMPLEX)
795:           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) PetscViewerASCIIPrintf(viewer, " %18.16e %18.16e ", (double)PetscRealPart(a->a[j]), (double)PetscImaginaryPart(a->a[j]));
796: #else
797:           if (a->a[j] != 0.0) PetscViewerASCIIPrintf(viewer, " %18.16e ", (double)a->a[j]);
798: #endif
799:         }
800:       }
801:       PetscViewerASCIIPrintf(viewer, "\n");
802:     }
803:     PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);
804:   } else if (format == PETSC_VIEWER_ASCII_DENSE) {
805:     PetscInt    cnt = 0, jcnt;
806:     PetscScalar value;
807: #if defined(PETSC_USE_COMPLEX)
808:     PetscBool realonly = PETSC_TRUE;

810:     for (i = 0; i < a->i[m]; i++) {
811:       if (PetscImaginaryPart(a->a[i]) != 0.0) {
812:         realonly = PETSC_FALSE;
813:         break;
814:       }
815:     }
816: #endif

818:     PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);
819:     for (i = 0; i < m; i++) {
820:       jcnt = 0;
821:       for (j = 0; j < A->cmap->n; j++) {
822:         if (jcnt < a->i[i + 1] - a->i[i] && j == a->j[cnt]) {
823:           value = a->a[cnt++];
824:           jcnt++;
825:         } else {
826:           value = 0.0;
827:         }
828: #if defined(PETSC_USE_COMPLEX)
829:         if (realonly) {
830:           PetscViewerASCIIPrintf(viewer, " %7.5e ", (double)PetscRealPart(value));
831:         } else {
832:           PetscViewerASCIIPrintf(viewer, " %7.5e+%7.5e i ", (double)PetscRealPart(value), (double)PetscImaginaryPart(value));
833:         }
834: #else
835:         PetscViewerASCIIPrintf(viewer, " %7.5e ", (double)value);
836: #endif
837:       }
838:       PetscViewerASCIIPrintf(viewer, "\n");
839:     }
840:     PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);
841:   } else if (format == PETSC_VIEWER_ASCII_MATRIXMARKET) {
842:     PetscInt fshift = 1;
843:     PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);
844: #if defined(PETSC_USE_COMPLEX)
845:     PetscViewerASCIIPrintf(viewer, "%%%%MatrixMarket matrix coordinate complex general\n");
846: #else
847:     PetscViewerASCIIPrintf(viewer, "%%%%MatrixMarket matrix coordinate real general\n");
848: #endif
849:     PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT " %" PetscInt_FMT " %" PetscInt_FMT "\n", m, A->cmap->n, a->nz);
850:     for (i = 0; i < m; i++) {
851:       for (j = a->i[i]; j < a->i[i + 1]; j++) {
852: #if defined(PETSC_USE_COMPLEX)
853:         PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT " %" PetscInt_FMT " %g %g\n", i + fshift, a->j[j] + fshift, (double)PetscRealPart(a->a[j]), (double)PetscImaginaryPart(a->a[j]));
854: #else
855:         PetscViewerASCIIPrintf(viewer, "%" PetscInt_FMT " %" PetscInt_FMT " %g\n", i + fshift, a->j[j] + fshift, (double)a->a[j]);
856: #endif
857:       }
858:     }
859:     PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);
860:   } else {
861:     PetscViewerASCIIUseTabs(viewer, PETSC_FALSE);
862:     if (A->factortype) {
863:       for (i = 0; i < m; i++) {
864:         PetscViewerASCIIPrintf(viewer, "row %" PetscInt_FMT ":", i);
865:         /* L part */
866:         for (j = a->i[i]; j < a->i[i + 1]; j++) {
867: #if defined(PETSC_USE_COMPLEX)
868:           if (PetscImaginaryPart(a->a[j]) > 0.0) {
869:             PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g + %g i)", a->j[j], (double)PetscRealPart(a->a[j]), (double)PetscImaginaryPart(a->a[j]));
870:           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
871:             PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g - %g i)", a->j[j], (double)PetscRealPart(a->a[j]), (double)(-PetscImaginaryPart(a->a[j])));
872:           } else {
873:             PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g) ", a->j[j], (double)PetscRealPart(a->a[j]));
874:           }
875: #else
876:           PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g) ", a->j[j], (double)a->a[j]);
877: #endif
878:         }
879:         /* diagonal */
880:         j = a->diag[i];
881: #if defined(PETSC_USE_COMPLEX)
882:         if (PetscImaginaryPart(a->a[j]) > 0.0) {
883:           PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g + %g i)", a->j[j], (double)PetscRealPart(1.0 / a->a[j]), (double)PetscImaginaryPart(1.0 / a->a[j]));
884:         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
885:           PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g - %g i)", a->j[j], (double)PetscRealPart(1.0 / a->a[j]), (double)(-PetscImaginaryPart(1.0 / a->a[j])));
886:         } else {
887:           PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g) ", a->j[j], (double)PetscRealPart(1.0 / a->a[j]));
888:         }
889: #else
890:         PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g) ", a->j[j], (double)(1.0 / a->a[j]));
891: #endif

893:         /* U part */
894:         for (j = a->diag[i + 1] + 1; j < a->diag[i]; j++) {
895: #if defined(PETSC_USE_COMPLEX)
896:           if (PetscImaginaryPart(a->a[j]) > 0.0) {
897:             PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g + %g i)", a->j[j], (double)PetscRealPart(a->a[j]), (double)PetscImaginaryPart(a->a[j]));
898:           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
899:             PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g - %g i)", a->j[j], (double)PetscRealPart(a->a[j]), (double)(-PetscImaginaryPart(a->a[j])));
900:           } else {
901:             PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g) ", a->j[j], (double)PetscRealPart(a->a[j]));
902:           }
903: #else
904:           PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g) ", a->j[j], (double)a->a[j]);
905: #endif
906:         }
907:         PetscViewerASCIIPrintf(viewer, "\n");
908:       }
909:     } else {
910:       for (i = 0; i < m; i++) {
911:         PetscViewerASCIIPrintf(viewer, "row %" PetscInt_FMT ":", i);
912:         for (j = a->i[i]; j < a->i[i + 1]; j++) {
913: #if defined(PETSC_USE_COMPLEX)
914:           if (PetscImaginaryPart(a->a[j]) > 0.0) {
915:             PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g + %g i)", a->j[j], (double)PetscRealPart(a->a[j]), (double)PetscImaginaryPart(a->a[j]));
916:           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
917:             PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g - %g i)", a->j[j], (double)PetscRealPart(a->a[j]), (double)-PetscImaginaryPart(a->a[j]));
918:           } else {
919:             PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g) ", a->j[j], (double)PetscRealPart(a->a[j]));
920:           }
921: #else
922:           PetscViewerASCIIPrintf(viewer, " (%" PetscInt_FMT ", %g) ", a->j[j], (double)a->a[j]);
923: #endif
924:         }
925:         PetscViewerASCIIPrintf(viewer, "\n");
926:       }
927:     }
928:     PetscViewerASCIIUseTabs(viewer, PETSC_TRUE);
929:   }
930:   PetscViewerFlush(viewer);
931:   return 0;
932: }

934: #include <petscdraw.h>
935: PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw, void *Aa)
936: {
937:   Mat                A = (Mat)Aa;
938:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data;
939:   PetscInt           i, j, m = A->rmap->n;
940:   int                color;
941:   PetscReal          xl, yl, xr, yr, x_l, x_r, y_l, y_r;
942:   PetscViewer        viewer;
943:   PetscViewerFormat  format;
944:   const PetscScalar *aa;

946:   PetscObjectQuery((PetscObject)A, "Zoomviewer", (PetscObject *)&viewer);
947:   PetscViewerGetFormat(viewer, &format);
948:   PetscDrawGetCoordinates(draw, &xl, &yl, &xr, &yr);

950:   /* loop over matrix elements drawing boxes */
951:   MatSeqAIJGetArrayRead(A, &aa);
952:   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
953:     PetscDrawCollectiveBegin(draw);
954:     /* Blue for negative, Cyan for zero and  Red for positive */
955:     color = PETSC_DRAW_BLUE;
956:     for (i = 0; i < m; i++) {
957:       y_l = m - i - 1.0;
958:       y_r = y_l + 1.0;
959:       for (j = a->i[i]; j < a->i[i + 1]; j++) {
960:         x_l = a->j[j];
961:         x_r = x_l + 1.0;
962:         if (PetscRealPart(aa[j]) >= 0.) continue;
963:         PetscDrawRectangle(draw, x_l, y_l, x_r, y_r, color, color, color, color);
964:       }
965:     }
966:     color = PETSC_DRAW_CYAN;
967:     for (i = 0; i < m; i++) {
968:       y_l = m - i - 1.0;
969:       y_r = y_l + 1.0;
970:       for (j = a->i[i]; j < a->i[i + 1]; j++) {
971:         x_l = a->j[j];
972:         x_r = x_l + 1.0;
973:         if (aa[j] != 0.) continue;
974:         PetscDrawRectangle(draw, x_l, y_l, x_r, y_r, color, color, color, color);
975:       }
976:     }
977:     color = PETSC_DRAW_RED;
978:     for (i = 0; i < m; i++) {
979:       y_l = m - i - 1.0;
980:       y_r = y_l + 1.0;
981:       for (j = a->i[i]; j < a->i[i + 1]; j++) {
982:         x_l = a->j[j];
983:         x_r = x_l + 1.0;
984:         if (PetscRealPart(aa[j]) <= 0.) continue;
985:         PetscDrawRectangle(draw, x_l, y_l, x_r, y_r, color, color, color, color);
986:       }
987:     }
988:     PetscDrawCollectiveEnd(draw);
989:   } else {
990:     /* use contour shading to indicate magnitude of values */
991:     /* first determine max of all nonzero values */
992:     PetscReal minv = 0.0, maxv = 0.0;
993:     PetscInt  nz = a->nz, count = 0;
994:     PetscDraw popup;

996:     for (i = 0; i < nz; i++) {
997:       if (PetscAbsScalar(aa[i]) > maxv) maxv = PetscAbsScalar(aa[i]);
998:     }
999:     if (minv >= maxv) maxv = minv + PETSC_SMALL;
1000:     PetscDrawGetPopup(draw, &popup);
1001:     PetscDrawScalePopup(popup, minv, maxv);

1003:     PetscDrawCollectiveBegin(draw);
1004:     for (i = 0; i < m; i++) {
1005:       y_l = m - i - 1.0;
1006:       y_r = y_l + 1.0;
1007:       for (j = a->i[i]; j < a->i[i + 1]; j++) {
1008:         x_l   = a->j[j];
1009:         x_r   = x_l + 1.0;
1010:         color = PetscDrawRealToColor(PetscAbsScalar(aa[count]), minv, maxv);
1011:         PetscDrawRectangle(draw, x_l, y_l, x_r, y_r, color, color, color, color);
1012:         count++;
1013:       }
1014:     }
1015:     PetscDrawCollectiveEnd(draw);
1016:   }
1017:   MatSeqAIJRestoreArrayRead(A, &aa);
1018:   return 0;
1019: }

1021: #include <petscdraw.h>
1022: PetscErrorCode MatView_SeqAIJ_Draw(Mat A, PetscViewer viewer)
1023: {
1024:   PetscDraw draw;
1025:   PetscReal xr, yr, xl, yl, h, w;
1026:   PetscBool isnull;

1028:   PetscViewerDrawGetDraw(viewer, 0, &draw);
1029:   PetscDrawIsNull(draw, &isnull);
1030:   if (isnull) return 0;

1032:   xr = A->cmap->n;
1033:   yr = A->rmap->n;
1034:   h  = yr / 10.0;
1035:   w  = xr / 10.0;
1036:   xr += w;
1037:   yr += h;
1038:   xl = -w;
1039:   yl = -h;
1040:   PetscDrawSetCoordinates(draw, xl, yl, xr, yr);
1041:   PetscObjectCompose((PetscObject)A, "Zoomviewer", (PetscObject)viewer);
1042:   PetscDrawZoom(draw, MatView_SeqAIJ_Draw_Zoom, A);
1043:   PetscObjectCompose((PetscObject)A, "Zoomviewer", NULL);
1044:   PetscDrawSave(draw);
1045:   return 0;
1046: }

1048: PetscErrorCode MatView_SeqAIJ(Mat A, PetscViewer viewer)
1049: {
1050:   PetscBool iascii, isbinary, isdraw;

1052:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii);
1053:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary);
1054:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERDRAW, &isdraw);
1055:   if (iascii) MatView_SeqAIJ_ASCII(A, viewer);
1056:   else if (isbinary) MatView_SeqAIJ_Binary(A, viewer);
1057:   else if (isdraw) MatView_SeqAIJ_Draw(A, viewer);
1058:   MatView_SeqAIJ_Inode(A, viewer);
1059:   return 0;
1060: }

1062: PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A, MatAssemblyType mode)
1063: {
1064:   Mat_SeqAIJ *a      = (Mat_SeqAIJ *)A->data;
1065:   PetscInt    fshift = 0, i, *ai = a->i, *aj = a->j, *imax = a->imax;
1066:   PetscInt    m = A->rmap->n, *ip, N, *ailen = a->ilen, rmax = 0;
1067:   MatScalar  *aa    = a->a, *ap;
1068:   PetscReal   ratio = 0.6;

1070:   if (mode == MAT_FLUSH_ASSEMBLY) return 0;
1071:   MatSeqAIJInvalidateDiagonal(A);
1072:   if (A->was_assembled && A->ass_nonzerostate == A->nonzerostate) {
1073:     /* we need to respect users asking to use or not the inodes routine in between matrix assemblies */
1074:     MatAssemblyEnd_SeqAIJ_Inode(A, mode);
1075:     return 0;
1076:   }

1078:   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
1079:   for (i = 1; i < m; i++) {
1080:     /* move each row back by the amount of empty slots (fshift) before it*/
1081:     fshift += imax[i - 1] - ailen[i - 1];
1082:     rmax = PetscMax(rmax, ailen[i]);
1083:     if (fshift) {
1084:       ip = aj + ai[i];
1085:       ap = aa + ai[i];
1086:       N  = ailen[i];
1087:       PetscArraymove(ip - fshift, ip, N);
1088:       if (!A->structure_only) PetscArraymove(ap - fshift, ap, N);
1089:     }
1090:     ai[i] = ai[i - 1] + ailen[i - 1];
1091:   }
1092:   if (m) {
1093:     fshift += imax[m - 1] - ailen[m - 1];
1094:     ai[m] = ai[m - 1] + ailen[m - 1];
1095:   }

1097:   /* reset ilen and imax for each row */
1098:   a->nonzerorowcnt = 0;
1099:   if (A->structure_only) {
1100:     PetscFree(a->imax);
1101:     PetscFree(a->ilen);
1102:   } else { /* !A->structure_only */
1103:     for (i = 0; i < m; i++) {
1104:       ailen[i] = imax[i] = ai[i + 1] - ai[i];
1105:       a->nonzerorowcnt += ((ai[i + 1] - ai[i]) > 0);
1106:     }
1107:   }
1108:   a->nz = ai[m];

1111:   MatMarkDiagonal_SeqAIJ(A);
1112:   PetscInfo(A, "Matrix size: %" PetscInt_FMT " X %" PetscInt_FMT "; storage space: %" PetscInt_FMT " unneeded,%" PetscInt_FMT " used\n", m, A->cmap->n, fshift, a->nz);
1113:   PetscInfo(A, "Number of mallocs during MatSetValues() is %" PetscInt_FMT "\n", a->reallocs);
1114:   PetscInfo(A, "Maximum nonzeros in any row is %" PetscInt_FMT "\n", rmax);

1116:   A->info.mallocs += a->reallocs;
1117:   a->reallocs         = 0;
1118:   A->info.nz_unneeded = (PetscReal)fshift;
1119:   a->rmax             = rmax;

1121:   if (!A->structure_only) MatCheckCompressedRow(A, a->nonzerorowcnt, &a->compressedrow, a->i, m, ratio);
1122:   MatAssemblyEnd_SeqAIJ_Inode(A, mode);
1123:   return 0;
1124: }

1126: PetscErrorCode MatRealPart_SeqAIJ(Mat A)
1127: {
1128:   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
1129:   PetscInt    i, nz = a->nz;
1130:   MatScalar  *aa;

1132:   MatSeqAIJGetArray(A, &aa);
1133:   for (i = 0; i < nz; i++) aa[i] = PetscRealPart(aa[i]);
1134:   MatSeqAIJRestoreArray(A, &aa);
1135:   MatSeqAIJInvalidateDiagonal(A);
1136:   return 0;
1137: }

1139: PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
1140: {
1141:   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
1142:   PetscInt    i, nz = a->nz;
1143:   MatScalar  *aa;

1145:   MatSeqAIJGetArray(A, &aa);
1146:   for (i = 0; i < nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
1147:   MatSeqAIJRestoreArray(A, &aa);
1148:   MatSeqAIJInvalidateDiagonal(A);
1149:   return 0;
1150: }

1152: PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
1153: {
1154:   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
1155:   MatScalar  *aa;

1157:   MatSeqAIJGetArrayWrite(A, &aa);
1158:   PetscArrayzero(aa, a->i[A->rmap->n]);
1159:   MatSeqAIJRestoreArrayWrite(A, &aa);
1160:   MatSeqAIJInvalidateDiagonal(A);
1161:   return 0;
1162: }

1164: PETSC_INTERN PetscErrorCode MatResetPreallocationCOO_SeqAIJ(Mat A)
1165: {
1166:   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;

1168:   PetscFree(a->perm);
1169:   PetscFree(a->jmap);
1170:   return 0;
1171: }

1173: PetscErrorCode MatDestroy_SeqAIJ(Mat A)
1174: {
1175:   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;

1177: #if defined(PETSC_USE_LOG)
1178:   PetscLogObjectState((PetscObject)A, "Rows=%" PetscInt_FMT ", Cols=%" PetscInt_FMT ", NZ=%" PetscInt_FMT, A->rmap->n, A->cmap->n, a->nz);
1179: #endif
1180:   MatSeqXAIJFreeAIJ(A, &a->a, &a->j, &a->i);
1181:   MatResetPreallocationCOO_SeqAIJ(A);
1182:   ISDestroy(&a->row);
1183:   ISDestroy(&a->col);
1184:   PetscFree(a->diag);
1185:   PetscFree(a->ibdiag);
1186:   PetscFree(a->imax);
1187:   PetscFree(a->ilen);
1188:   PetscFree(a->ipre);
1189:   PetscFree3(a->idiag, a->mdiag, a->ssor_work);
1190:   PetscFree(a->solve_work);
1191:   ISDestroy(&a->icol);
1192:   PetscFree(a->saved_values);
1193:   PetscFree2(a->compressedrow.i, a->compressedrow.rindex);
1194:   MatDestroy_SeqAIJ_Inode(A);
1195:   PetscFree(A->data);

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

1204:   PetscObjectChangeTypeName((PetscObject)A, NULL);
1205:   PetscObjectComposeFunction((PetscObject)A, "PetscMatlabEnginePut_C", NULL);
1206:   PetscObjectComposeFunction((PetscObject)A, "PetscMatlabEngineGet_C", NULL);
1207:   PetscObjectComposeFunction((PetscObject)A, "MatSeqAIJSetColumnIndices_C", NULL);
1208:   PetscObjectComposeFunction((PetscObject)A, "MatStoreValues_C", NULL);
1209:   PetscObjectComposeFunction((PetscObject)A, "MatRetrieveValues_C", NULL);
1210:   PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_seqsbaij_C", NULL);
1211:   PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_seqbaij_C", NULL);
1212:   PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_seqaijperm_C", NULL);
1213:   PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_seqaijsell_C", NULL);
1214: #if defined(PETSC_HAVE_MKL_SPARSE)
1215:   PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_seqaijmkl_C", NULL);
1216: #endif
1217: #if defined(PETSC_HAVE_CUDA)
1218:   PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_seqaijcusparse_C", NULL);
1219:   PetscObjectComposeFunction((PetscObject)A, "MatProductSetFromOptions_seqaijcusparse_seqaij_C", NULL);
1220:   PetscObjectComposeFunction((PetscObject)A, "MatProductSetFromOptions_seqaij_seqaijcusparse_C", NULL);
1221: #endif
1222: #if defined(PETSC_HAVE_KOKKOS_KERNELS)
1223:   PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_seqaijkokkos_C", NULL);
1224: #endif
1225:   PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_seqaijcrl_C", NULL);
1226: #if defined(PETSC_HAVE_ELEMENTAL)
1227:   PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_elemental_C", NULL);
1228: #endif
1229: #if defined(PETSC_HAVE_SCALAPACK)
1230:   PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_scalapack_C", NULL);
1231: #endif
1232: #if defined(PETSC_HAVE_HYPRE)
1233:   PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_hypre_C", NULL);
1234:   PetscObjectComposeFunction((PetscObject)A, "MatProductSetFromOptions_transpose_seqaij_seqaij_C", NULL);
1235: #endif
1236:   PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_seqdense_C", NULL);
1237:   PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_seqsell_C", NULL);
1238:   PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_is_C", NULL);
1239:   PetscObjectComposeFunction((PetscObject)A, "MatIsTranspose_C", NULL);
1240:   PetscObjectComposeFunction((PetscObject)A, "MatIsHermitianTranspose_C", NULL);
1241:   PetscObjectComposeFunction((PetscObject)A, "MatSeqAIJSetPreallocation_C", NULL);
1242:   PetscObjectComposeFunction((PetscObject)A, "MatResetPreallocation_C", NULL);
1243:   PetscObjectComposeFunction((PetscObject)A, "MatSeqAIJSetPreallocationCSR_C", NULL);
1244:   PetscObjectComposeFunction((PetscObject)A, "MatReorderForNonzeroDiagonal_C", NULL);
1245:   PetscObjectComposeFunction((PetscObject)A, "MatProductSetFromOptions_is_seqaij_C", NULL);
1246:   PetscObjectComposeFunction((PetscObject)A, "MatProductSetFromOptions_seqdense_seqaij_C", NULL);
1247:   PetscObjectComposeFunction((PetscObject)A, "MatProductSetFromOptions_seqaij_seqaij_C", NULL);
1248:   PetscObjectComposeFunction((PetscObject)A, "MatSeqAIJKron_C", NULL);
1249:   PetscObjectComposeFunction((PetscObject)A, "MatSetPreallocationCOO_C", NULL);
1250:   PetscObjectComposeFunction((PetscObject)A, "MatSetValuesCOO_C", NULL);
1251:   PetscObjectComposeFunction((PetscObject)A, "MatFactorGetSolverType_C", NULL);
1252:   /* these calls do not belong here: the subclasses Duplicate/Destroy are wrong */
1253:   PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaijsell_seqaij_C", NULL);
1254:   PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaijperm_seqaij_C", NULL);
1255:   PetscObjectComposeFunction((PetscObject)A, "MatConvert_seqaij_seqaijviennacl_C", NULL);
1256:   PetscObjectComposeFunction((PetscObject)A, "MatProductSetFromOptions_seqaijviennacl_seqdense_C", NULL);
1257:   PetscObjectComposeFunction((PetscObject)A, "MatProductSetFromOptions_seqaijviennacl_seqaij_C", NULL);
1258:   return 0;
1259: }

1261: PetscErrorCode MatSetOption_SeqAIJ(Mat A, MatOption op, PetscBool flg)
1262: {
1263:   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;

1265:   switch (op) {
1266:   case MAT_ROW_ORIENTED:
1267:     a->roworiented = flg;
1268:     break;
1269:   case MAT_KEEP_NONZERO_PATTERN:
1270:     a->keepnonzeropattern = flg;
1271:     break;
1272:   case MAT_NEW_NONZERO_LOCATIONS:
1273:     a->nonew = (flg ? 0 : 1);
1274:     break;
1275:   case MAT_NEW_NONZERO_LOCATION_ERR:
1276:     a->nonew = (flg ? -1 : 0);
1277:     break;
1278:   case MAT_NEW_NONZERO_ALLOCATION_ERR:
1279:     a->nonew = (flg ? -2 : 0);
1280:     break;
1281:   case MAT_UNUSED_NONZERO_LOCATION_ERR:
1282:     a->nounused = (flg ? -1 : 0);
1283:     break;
1284:   case MAT_IGNORE_ZERO_ENTRIES:
1285:     a->ignorezeroentries = flg;
1286:     break;
1287:   case MAT_SPD:
1288:   case MAT_SYMMETRIC:
1289:   case MAT_STRUCTURALLY_SYMMETRIC:
1290:   case MAT_HERMITIAN:
1291:   case MAT_SYMMETRY_ETERNAL:
1292:   case MAT_STRUCTURE_ONLY:
1293:   case MAT_STRUCTURAL_SYMMETRY_ETERNAL:
1294:   case MAT_SPD_ETERNAL:
1295:     /* if the diagonal matrix is square it inherits some of the properties above */
1296:     break;
1297:   case MAT_FORCE_DIAGONAL_ENTRIES:
1298:   case MAT_IGNORE_OFF_PROC_ENTRIES:
1299:   case MAT_USE_HASH_TABLE:
1300:     PetscInfo(A, "Option %s ignored\n", MatOptions[op]);
1301:     break;
1302:   case MAT_USE_INODES:
1303:     MatSetOption_SeqAIJ_Inode(A, MAT_USE_INODES, flg);
1304:     break;
1305:   case MAT_SUBMAT_SINGLEIS:
1306:     A->submat_singleis = flg;
1307:     break;
1308:   case MAT_SORTED_FULL:
1309:     if (flg) A->ops->setvalues = MatSetValues_SeqAIJ_SortedFull;
1310:     else A->ops->setvalues = MatSetValues_SeqAIJ;
1311:     break;
1312:   case MAT_FORM_EXPLICIT_TRANSPOSE:
1313:     A->form_explicit_transpose = flg;
1314:     break;
1315:   default:
1316:     SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "unknown option %d", op);
1317:   }
1318:   return 0;
1319: }

1321: PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A, Vec v)
1322: {
1323:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data;
1324:   PetscInt           i, j, n, *ai = a->i, *aj = a->j;
1325:   PetscScalar       *x;
1326:   const PetscScalar *aa;

1328:   VecGetLocalSize(v, &n);
1330:   MatSeqAIJGetArrayRead(A, &aa);
1331:   if (A->factortype == MAT_FACTOR_ILU || A->factortype == MAT_FACTOR_LU) {
1332:     PetscInt *diag = a->diag;
1333:     VecGetArrayWrite(v, &x);
1334:     for (i = 0; i < n; i++) x[i] = 1.0 / aa[diag[i]];
1335:     VecRestoreArrayWrite(v, &x);
1336:     MatSeqAIJRestoreArrayRead(A, &aa);
1337:     return 0;
1338:   }

1340:   VecGetArrayWrite(v, &x);
1341:   for (i = 0; i < n; i++) {
1342:     x[i] = 0.0;
1343:     for (j = ai[i]; j < ai[i + 1]; j++) {
1344:       if (aj[j] == i) {
1345:         x[i] = aa[j];
1346:         break;
1347:       }
1348:     }
1349:   }
1350:   VecRestoreArrayWrite(v, &x);
1351:   MatSeqAIJRestoreArrayRead(A, &aa);
1352:   return 0;
1353: }

1355: #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
1356: PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A, Vec xx, Vec zz, Vec yy)
1357: {
1358:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data;
1359:   const MatScalar   *aa;
1360:   PetscScalar       *y;
1361:   const PetscScalar *x;
1362:   PetscInt           m = A->rmap->n;
1363: #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1364:   const MatScalar  *v;
1365:   PetscScalar       alpha;
1366:   PetscInt          n, i, j;
1367:   const PetscInt   *idx, *ii, *ridx = NULL;
1368:   Mat_CompressedRow cprow    = a->compressedrow;
1369:   PetscBool         usecprow = cprow.use;
1370: #endif

1372:   if (zz != yy) VecCopy(zz, yy);
1373:   VecGetArrayRead(xx, &x);
1374:   VecGetArray(yy, &y);
1375:   MatSeqAIJGetArrayRead(A, &aa);

1377: #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1378:   fortranmulttransposeaddaij_(&m, x, a->i, a->j, aa, y);
1379: #else
1380:   if (usecprow) {
1381:     m = cprow.nrows;
1382:     ii = cprow.i;
1383:     ridx = cprow.rindex;
1384:   } else {
1385:     ii = a->i;
1386:   }
1387:   for (i = 0; i < m; i++) {
1388:     idx = a->j + ii[i];
1389:     v = aa + ii[i];
1390:     n = ii[i + 1] - ii[i];
1391:     if (usecprow) {
1392:       alpha = x[ridx[i]];
1393:     } else {
1394:       alpha = x[i];
1395:     }
1396:     for (j = 0; j < n; j++) y[idx[j]] += alpha * v[j];
1397:   }
1398: #endif
1399:   PetscLogFlops(2.0 * a->nz);
1400:   VecRestoreArrayRead(xx, &x);
1401:   VecRestoreArray(yy, &y);
1402:   MatSeqAIJRestoreArrayRead(A, &aa);
1403:   return 0;
1404: }

1406: PetscErrorCode MatMultTranspose_SeqAIJ(Mat A, Vec xx, Vec yy)
1407: {
1408:   VecSet(yy, 0.0);
1409:   MatMultTransposeAdd_SeqAIJ(A, xx, yy, yy);
1410:   return 0;
1411: }

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

1415: PetscErrorCode MatMult_SeqAIJ(Mat A, Vec xx, Vec yy)
1416: {
1417:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data;
1418:   PetscScalar       *y;
1419:   const PetscScalar *x;
1420:   const MatScalar   *aa, *a_a;
1421:   PetscInt           m = A->rmap->n;
1422:   const PetscInt    *aj, *ii, *ridx = NULL;
1423:   PetscInt           n, i;
1424:   PetscScalar        sum;
1425:   PetscBool          usecprow = a->compressedrow.use;

1427: #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
1428:   #pragma disjoint(*x, *y, *aa)
1429: #endif

1431:   if (a->inode.use && a->inode.checked) {
1432:     MatMult_SeqAIJ_Inode(A, xx, yy);
1433:     return 0;
1434:   }
1435:   MatSeqAIJGetArrayRead(A, &a_a);
1436:   VecGetArrayRead(xx, &x);
1437:   VecGetArray(yy, &y);
1438:   ii = a->i;
1439:   if (usecprow) { /* use compressed row format */
1440:     PetscArrayzero(y, m);
1441:     m    = a->compressedrow.nrows;
1442:     ii   = a->compressedrow.i;
1443:     ridx = a->compressedrow.rindex;
1444:     for (i = 0; i < m; i++) {
1445:       n   = ii[i + 1] - ii[i];
1446:       aj  = a->j + ii[i];
1447:       aa  = a_a + ii[i];
1448:       sum = 0.0;
1449:       PetscSparseDensePlusDot(sum, x, aa, aj, n);
1450:       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
1451:       y[*ridx++] = sum;
1452:     }
1453:   } else { /* do not use compressed row format */
1454: #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
1455:     aj = a->j;
1456:     aa = a_a;
1457:     fortranmultaij_(&m, x, ii, aj, aa, y);
1458: #else
1459:     for (i = 0; i < m; i++) {
1460:       n = ii[i + 1] - ii[i];
1461:       aj = a->j + ii[i];
1462:       aa = a_a + ii[i];
1463:       sum = 0.0;
1464:       PetscSparseDensePlusDot(sum, x, aa, aj, n);
1465:       y[i] = sum;
1466:     }
1467: #endif
1468:   }
1469:   PetscLogFlops(2.0 * a->nz - a->nonzerorowcnt);
1470:   VecRestoreArrayRead(xx, &x);
1471:   VecRestoreArray(yy, &y);
1472:   MatSeqAIJRestoreArrayRead(A, &a_a);
1473:   return 0;
1474: }

1476: PetscErrorCode MatMultMax_SeqAIJ(Mat A, Vec xx, Vec yy)
1477: {
1478:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data;
1479:   PetscScalar       *y;
1480:   const PetscScalar *x;
1481:   const MatScalar   *aa, *a_a;
1482:   PetscInt           m = A->rmap->n;
1483:   const PetscInt    *aj, *ii, *ridx   = NULL;
1484:   PetscInt           n, i, nonzerorow = 0;
1485:   PetscScalar        sum;
1486:   PetscBool          usecprow = a->compressedrow.use;

1488: #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
1489:   #pragma disjoint(*x, *y, *aa)
1490: #endif

1492:   MatSeqAIJGetArrayRead(A, &a_a);
1493:   VecGetArrayRead(xx, &x);
1494:   VecGetArray(yy, &y);
1495:   if (usecprow) { /* use compressed row format */
1496:     m    = a->compressedrow.nrows;
1497:     ii   = a->compressedrow.i;
1498:     ridx = a->compressedrow.rindex;
1499:     for (i = 0; i < m; i++) {
1500:       n   = ii[i + 1] - ii[i];
1501:       aj  = a->j + ii[i];
1502:       aa  = a_a + ii[i];
1503:       sum = 0.0;
1504:       nonzerorow += (n > 0);
1505:       PetscSparseDenseMaxDot(sum, x, aa, aj, n);
1506:       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
1507:       y[*ridx++] = sum;
1508:     }
1509:   } else { /* do not use compressed row format */
1510:     ii = a->i;
1511:     for (i = 0; i < m; i++) {
1512:       n   = ii[i + 1] - ii[i];
1513:       aj  = a->j + ii[i];
1514:       aa  = a_a + ii[i];
1515:       sum = 0.0;
1516:       nonzerorow += (n > 0);
1517:       PetscSparseDenseMaxDot(sum, x, aa, aj, n);
1518:       y[i] = sum;
1519:     }
1520:   }
1521:   PetscLogFlops(2.0 * a->nz - nonzerorow);
1522:   VecRestoreArrayRead(xx, &x);
1523:   VecRestoreArray(yy, &y);
1524:   MatSeqAIJRestoreArrayRead(A, &a_a);
1525:   return 0;
1526: }

1528: PetscErrorCode MatMultAddMax_SeqAIJ(Mat A, Vec xx, Vec yy, Vec zz)
1529: {
1530:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data;
1531:   PetscScalar       *y, *z;
1532:   const PetscScalar *x;
1533:   const MatScalar   *aa, *a_a;
1534:   PetscInt           m = A->rmap->n, *aj, *ii;
1535:   PetscInt           n, i, *ridx = NULL;
1536:   PetscScalar        sum;
1537:   PetscBool          usecprow = a->compressedrow.use;

1539:   MatSeqAIJGetArrayRead(A, &a_a);
1540:   VecGetArrayRead(xx, &x);
1541:   VecGetArrayPair(yy, zz, &y, &z);
1542:   if (usecprow) { /* use compressed row format */
1543:     if (zz != yy) PetscArraycpy(z, y, m);
1544:     m    = a->compressedrow.nrows;
1545:     ii   = a->compressedrow.i;
1546:     ridx = a->compressedrow.rindex;
1547:     for (i = 0; i < m; i++) {
1548:       n   = ii[i + 1] - ii[i];
1549:       aj  = a->j + ii[i];
1550:       aa  = a_a + ii[i];
1551:       sum = y[*ridx];
1552:       PetscSparseDenseMaxDot(sum, x, aa, aj, n);
1553:       z[*ridx++] = sum;
1554:     }
1555:   } else { /* do not use compressed row format */
1556:     ii = a->i;
1557:     for (i = 0; i < m; i++) {
1558:       n   = ii[i + 1] - ii[i];
1559:       aj  = a->j + ii[i];
1560:       aa  = a_a + ii[i];
1561:       sum = y[i];
1562:       PetscSparseDenseMaxDot(sum, x, aa, aj, n);
1563:       z[i] = sum;
1564:     }
1565:   }
1566:   PetscLogFlops(2.0 * a->nz);
1567:   VecRestoreArrayRead(xx, &x);
1568:   VecRestoreArrayPair(yy, zz, &y, &z);
1569:   MatSeqAIJRestoreArrayRead(A, &a_a);
1570:   return 0;
1571: }

1573: #include <../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h>
1574: PetscErrorCode MatMultAdd_SeqAIJ(Mat A, Vec xx, Vec yy, Vec zz)
1575: {
1576:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data;
1577:   PetscScalar       *y, *z;
1578:   const PetscScalar *x;
1579:   const MatScalar   *aa, *a_a;
1580:   const PetscInt    *aj, *ii, *ridx = NULL;
1581:   PetscInt           m = A->rmap->n, n, i;
1582:   PetscScalar        sum;
1583:   PetscBool          usecprow = a->compressedrow.use;

1585:   if (a->inode.use && a->inode.checked) {
1586:     MatMultAdd_SeqAIJ_Inode(A, xx, yy, zz);
1587:     return 0;
1588:   }
1589:   MatSeqAIJGetArrayRead(A, &a_a);
1590:   VecGetArrayRead(xx, &x);
1591:   VecGetArrayPair(yy, zz, &y, &z);
1592:   if (usecprow) { /* use compressed row format */
1593:     if (zz != yy) PetscArraycpy(z, y, m);
1594:     m    = a->compressedrow.nrows;
1595:     ii   = a->compressedrow.i;
1596:     ridx = a->compressedrow.rindex;
1597:     for (i = 0; i < m; i++) {
1598:       n   = ii[i + 1] - ii[i];
1599:       aj  = a->j + ii[i];
1600:       aa  = a_a + ii[i];
1601:       sum = y[*ridx];
1602:       PetscSparseDensePlusDot(sum, x, aa, aj, n);
1603:       z[*ridx++] = sum;
1604:     }
1605:   } else { /* do not use compressed row format */
1606:     ii = a->i;
1607: #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
1608:     aj = a->j;
1609:     aa = a_a;
1610:     fortranmultaddaij_(&m, x, ii, aj, aa, y, z);
1611: #else
1612:     for (i = 0; i < m; i++) {
1613:       n = ii[i + 1] - ii[i];
1614:       aj = a->j + ii[i];
1615:       aa = a_a + ii[i];
1616:       sum = y[i];
1617:       PetscSparseDensePlusDot(sum, x, aa, aj, n);
1618:       z[i] = sum;
1619:     }
1620: #endif
1621:   }
1622:   PetscLogFlops(2.0 * a->nz);
1623:   VecRestoreArrayRead(xx, &x);
1624:   VecRestoreArrayPair(yy, zz, &y, &z);
1625:   MatSeqAIJRestoreArrayRead(A, &a_a);
1626:   return 0;
1627: }

1629: /*
1630:      Adds diagonal pointers to sparse matrix structure.
1631: */
1632: PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
1633: {
1634:   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
1635:   PetscInt    i, j, m = A->rmap->n;
1636:   PetscBool   alreadySet = PETSC_TRUE;

1638:   if (!a->diag) {
1639:     PetscMalloc1(m, &a->diag);
1640:     alreadySet = PETSC_FALSE;
1641:   }
1642:   for (i = 0; i < A->rmap->n; i++) {
1643:     /* If A's diagonal is already correctly set, this fast track enables cheap and repeated MatMarkDiagonal_SeqAIJ() calls */
1644:     if (alreadySet) {
1645:       PetscInt pos = a->diag[i];
1646:       if (pos >= a->i[i] && pos < a->i[i + 1] && a->j[pos] == i) continue;
1647:     }

1649:     a->diag[i] = a->i[i + 1];
1650:     for (j = a->i[i]; j < a->i[i + 1]; j++) {
1651:       if (a->j[j] == i) {
1652:         a->diag[i] = j;
1653:         break;
1654:       }
1655:     }
1656:   }
1657:   return 0;
1658: }

1660: PetscErrorCode MatShift_SeqAIJ(Mat A, PetscScalar v)
1661: {
1662:   Mat_SeqAIJ     *a    = (Mat_SeqAIJ *)A->data;
1663:   const PetscInt *diag = (const PetscInt *)a->diag;
1664:   const PetscInt *ii   = (const PetscInt *)a->i;
1665:   PetscInt        i, *mdiag = NULL;
1666:   PetscInt        cnt = 0; /* how many diagonals are missing */

1668:   if (!A->preallocated || !a->nz) {
1669:     MatSeqAIJSetPreallocation(A, 1, NULL);
1670:     MatShift_Basic(A, v);
1671:     return 0;
1672:   }

1674:   if (a->diagonaldense) {
1675:     cnt = 0;
1676:   } else {
1677:     PetscCalloc1(A->rmap->n, &mdiag);
1678:     for (i = 0; i < A->rmap->n; i++) {
1679:       if (i < A->cmap->n && diag[i] >= ii[i + 1]) { /* 'out of range' rows never have diagonals */
1680:         cnt++;
1681:         mdiag[i] = 1;
1682:       }
1683:     }
1684:   }
1685:   if (!cnt) {
1686:     MatShift_Basic(A, v);
1687:   } else {
1688:     PetscScalar *olda = a->a; /* preserve pointers to current matrix nonzeros structure and values */
1689:     PetscInt    *oldj = a->j, *oldi = a->i;
1690:     PetscBool    singlemalloc = a->singlemalloc, free_a = a->free_a, free_ij = a->free_ij;

1692:     a->a = NULL;
1693:     a->j = NULL;
1694:     a->i = NULL;
1695:     /* increase the values in imax for each row where a diagonal is being inserted then reallocate the matrix data structures */
1696:     for (i = 0; i < PetscMin(A->rmap->n, A->cmap->n); i++) a->imax[i] += mdiag[i];
1697:     MatSeqAIJSetPreallocation_SeqAIJ(A, 0, a->imax);

1699:     /* copy old values into new matrix data structure */
1700:     for (i = 0; i < A->rmap->n; i++) {
1701:       MatSetValues(A, 1, &i, a->imax[i] - mdiag[i], &oldj[oldi[i]], &olda[oldi[i]], ADD_VALUES);
1702:       if (i < A->cmap->n) MatSetValue(A, i, i, v, ADD_VALUES);
1703:     }
1704:     MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY);
1705:     MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY);
1706:     if (singlemalloc) {
1707:       PetscFree3(olda, oldj, oldi);
1708:     } else {
1709:       if (free_a) PetscFree(olda);
1710:       if (free_ij) PetscFree(oldj);
1711:       if (free_ij) PetscFree(oldi);
1712:     }
1713:   }
1714:   PetscFree(mdiag);
1715:   a->diagonaldense = PETSC_TRUE;
1716:   return 0;
1717: }

1719: /*
1720:      Checks for missing diagonals
1721: */
1722: PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A, PetscBool *missing, PetscInt *d)
1723: {
1724:   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;
1725:   PetscInt   *diag, *ii = a->i, i;

1727:   *missing = PETSC_FALSE;
1728:   if (A->rmap->n > 0 && !ii) {
1729:     *missing = PETSC_TRUE;
1730:     if (d) *d = 0;
1731:     PetscInfo(A, "Matrix has no entries therefore is missing diagonal\n");
1732:   } else {
1733:     PetscInt n;
1734:     n    = PetscMin(A->rmap->n, A->cmap->n);
1735:     diag = a->diag;
1736:     for (i = 0; i < n; i++) {
1737:       if (diag[i] >= ii[i + 1]) {
1738:         *missing = PETSC_TRUE;
1739:         if (d) *d = i;
1740:         PetscInfo(A, "Matrix is missing diagonal number %" PetscInt_FMT "\n", i);
1741:         break;
1742:       }
1743:     }
1744:   }
1745:   return 0;
1746: }

1748: #include <petscblaslapack.h>
1749: #include <petsc/private/kernels/blockinvert.h>

1751: /*
1752:     Note that values is allocated externally by the PC and then passed into this routine
1753: */
1754: PetscErrorCode MatInvertVariableBlockDiagonal_SeqAIJ(Mat A, PetscInt nblocks, const PetscInt *bsizes, PetscScalar *diag)
1755: {
1756:   PetscInt        n = A->rmap->n, i, ncnt = 0, *indx, j, bsizemax = 0, *v_pivots;
1757:   PetscBool       allowzeropivot, zeropivotdetected = PETSC_FALSE;
1758:   const PetscReal shift = 0.0;
1759:   PetscInt        ipvt[5];
1760:   PetscCount      flops = 0;
1761:   PetscScalar     work[25], *v_work;

1763:   allowzeropivot = PetscNot(A->erroriffailure);
1764:   for (i = 0; i < nblocks; i++) ncnt += bsizes[i];
1766:   for (i = 0; i < nblocks; i++) bsizemax = PetscMax(bsizemax, bsizes[i]);
1767:   PetscMalloc1(bsizemax, &indx);
1768:   if (bsizemax > 7) PetscMalloc2(bsizemax, &v_work, bsizemax, &v_pivots);
1769:   ncnt = 0;
1770:   for (i = 0; i < nblocks; i++) {
1771:     for (j = 0; j < bsizes[i]; j++) indx[j] = ncnt + j;
1772:     MatGetValues(A, bsizes[i], indx, bsizes[i], indx, diag);
1773:     switch (bsizes[i]) {
1774:     case 1:
1775:       *diag = 1.0 / (*diag);
1776:       break;
1777:     case 2:
1778:       PetscKernel_A_gets_inverse_A_2(diag, shift, allowzeropivot, &zeropivotdetected);
1779:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1780:       PetscKernel_A_gets_transpose_A_2(diag);
1781:       break;
1782:     case 3:
1783:       PetscKernel_A_gets_inverse_A_3(diag, shift, allowzeropivot, &zeropivotdetected);
1784:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1785:       PetscKernel_A_gets_transpose_A_3(diag);
1786:       break;
1787:     case 4:
1788:       PetscKernel_A_gets_inverse_A_4(diag, shift, allowzeropivot, &zeropivotdetected);
1789:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1790:       PetscKernel_A_gets_transpose_A_4(diag);
1791:       break;
1792:     case 5:
1793:       PetscKernel_A_gets_inverse_A_5(diag, ipvt, work, shift, allowzeropivot, &zeropivotdetected);
1794:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1795:       PetscKernel_A_gets_transpose_A_5(diag);
1796:       break;
1797:     case 6:
1798:       PetscKernel_A_gets_inverse_A_6(diag, shift, allowzeropivot, &zeropivotdetected);
1799:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1800:       PetscKernel_A_gets_transpose_A_6(diag);
1801:       break;
1802:     case 7:
1803:       PetscKernel_A_gets_inverse_A_7(diag, shift, allowzeropivot, &zeropivotdetected);
1804:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1805:       PetscKernel_A_gets_transpose_A_7(diag);
1806:       break;
1807:     default:
1808:       PetscKernel_A_gets_inverse_A(bsizes[i], diag, v_pivots, v_work, allowzeropivot, &zeropivotdetected);
1809:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1810:       PetscKernel_A_gets_transpose_A_N(diag, bsizes[i]);
1811:     }
1812:     ncnt += bsizes[i];
1813:     diag += bsizes[i] * bsizes[i];
1814:     flops += 2 * PetscPowInt(bsizes[i], 3) / 3;
1815:   }
1816:   PetscLogFlops(flops);
1817:   if (bsizemax > 7) PetscFree2(v_work, v_pivots);
1818:   PetscFree(indx);
1819:   return 0;
1820: }

1822: /*
1823:    Negative shift indicates do not generate an error if there is a zero diagonal, just invert it anyways
1824: */
1825: PetscErrorCode MatInvertDiagonal_SeqAIJ(Mat A, PetscScalar omega, PetscScalar fshift)
1826: {
1827:   Mat_SeqAIJ      *a = (Mat_SeqAIJ *)A->data;
1828:   PetscInt         i, *diag, m = A->rmap->n;
1829:   const MatScalar *v;
1830:   PetscScalar     *idiag, *mdiag;

1832:   if (a->idiagvalid) return 0;
1833:   MatMarkDiagonal_SeqAIJ(A);
1834:   diag = a->diag;
1835:   if (!a->idiag) { PetscMalloc3(m, &a->idiag, m, &a->mdiag, m, &a->ssor_work); }

1837:   mdiag = a->mdiag;
1838:   idiag = a->idiag;
1839:   MatSeqAIJGetArrayRead(A, &v);
1840:   if (omega == 1.0 && PetscRealPart(fshift) <= 0.0) {
1841:     for (i = 0; i < m; i++) {
1842:       mdiag[i] = v[diag[i]];
1843:       if (!PetscAbsScalar(mdiag[i])) { /* zero diagonal */
1844:         if (PetscRealPart(fshift)) {
1845:           PetscInfo(A, "Zero diagonal on row %" PetscInt_FMT "\n", i);
1846:           A->factorerrortype             = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1847:           A->factorerror_zeropivot_value = 0.0;
1848:           A->factorerror_zeropivot_row   = i;
1849:         } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Zero diagonal on row %" PetscInt_FMT, i);
1850:       }
1851:       idiag[i] = 1.0 / v[diag[i]];
1852:     }
1853:     PetscLogFlops(m);
1854:   } else {
1855:     for (i = 0; i < m; i++) {
1856:       mdiag[i] = v[diag[i]];
1857:       idiag[i] = omega / (fshift + v[diag[i]]);
1858:     }
1859:     PetscLogFlops(2.0 * m);
1860:   }
1861:   a->idiagvalid = PETSC_TRUE;
1862:   MatSeqAIJRestoreArrayRead(A, &v);
1863:   return 0;
1864: }

1866: #include <../src/mat/impls/aij/seq/ftn-kernels/frelax.h>
1867: PetscErrorCode MatSOR_SeqAIJ(Mat A, Vec bb, PetscReal omega, MatSORType flag, PetscReal fshift, PetscInt its, PetscInt lits, Vec xx)
1868: {
1869:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data;
1870:   PetscScalar       *x, d, sum, *t, scale;
1871:   const MatScalar   *v, *idiag = NULL, *mdiag, *aa;
1872:   const PetscScalar *b, *bs, *xb, *ts;
1873:   PetscInt           n, m = A->rmap->n, i;
1874:   const PetscInt    *idx, *diag;

1876:   if (a->inode.use && a->inode.checked && omega == 1.0 && fshift == 0.0) {
1877:     MatSOR_SeqAIJ_Inode(A, bb, omega, flag, fshift, its, lits, xx);
1878:     return 0;
1879:   }
1880:   its = its * lits;

1882:   if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
1883:   if (!a->idiagvalid) MatInvertDiagonal_SeqAIJ(A, omega, fshift);
1884:   a->fshift = fshift;
1885:   a->omega  = omega;

1887:   diag  = a->diag;
1888:   t     = a->ssor_work;
1889:   idiag = a->idiag;
1890:   mdiag = a->mdiag;

1892:   MatSeqAIJGetArrayRead(A, &aa);
1893:   VecGetArray(xx, &x);
1894:   VecGetArrayRead(bb, &b);
1895:   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
1896:   if (flag == SOR_APPLY_UPPER) {
1897:     /* apply (U + D/omega) to the vector */
1898:     bs = b;
1899:     for (i = 0; i < m; i++) {
1900:       d   = fshift + mdiag[i];
1901:       n   = a->i[i + 1] - diag[i] - 1;
1902:       idx = a->j + diag[i] + 1;
1903:       v   = aa + diag[i] + 1;
1904:       sum = b[i] * d / omega;
1905:       PetscSparseDensePlusDot(sum, bs, v, idx, n);
1906:       x[i] = sum;
1907:     }
1908:     VecRestoreArray(xx, &x);
1909:     VecRestoreArrayRead(bb, &b);
1910:     MatSeqAIJRestoreArrayRead(A, &aa);
1911:     PetscLogFlops(a->nz);
1912:     return 0;
1913:   }

1916:   if (flag & SOR_EISENSTAT) {
1917:     /* Let  A = L + U + D; where L is lower triangular,
1918:     U is upper triangular, E = D/omega; This routine applies

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

1922:     to a vector efficiently using Eisenstat's trick.
1923:     */
1924:     scale = (2.0 / omega) - 1.0;

1926:     /*  x = (E + U)^{-1} b */
1927:     for (i = m - 1; i >= 0; i--) {
1928:       n   = a->i[i + 1] - diag[i] - 1;
1929:       idx = a->j + diag[i] + 1;
1930:       v   = aa + diag[i] + 1;
1931:       sum = b[i];
1932:       PetscSparseDenseMinusDot(sum, x, v, idx, n);
1933:       x[i] = sum * idiag[i];
1934:     }

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

1940:     /*  t = (E + L)^{-1}t */
1941:     ts   = t;
1942:     diag = a->diag;
1943:     for (i = 0; i < m; i++) {
1944:       n   = diag[i] - a->i[i];
1945:       idx = a->j + a->i[i];
1946:       v   = aa + a->i[i];
1947:       sum = t[i];
1948:       PetscSparseDenseMinusDot(sum, ts, v, idx, n);
1949:       t[i] = sum * idiag[i];
1950:       /*  x = x + t */
1951:       x[i] += t[i];
1952:     }

1954:     PetscLogFlops(6.0 * m - 1 + 2.0 * a->nz);
1955:     VecRestoreArray(xx, &x);
1956:     VecRestoreArrayRead(bb, &b);
1957:     return 0;
1958:   }
1959:   if (flag & SOR_ZERO_INITIAL_GUESS) {
1960:     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
1961:       for (i = 0; i < m; i++) {
1962:         n   = diag[i] - a->i[i];
1963:         idx = a->j + a->i[i];
1964:         v   = aa + a->i[i];
1965:         sum = b[i];
1966:         PetscSparseDenseMinusDot(sum, x, v, idx, n);
1967:         t[i] = sum;
1968:         x[i] = sum * idiag[i];
1969:       }
1970:       xb = t;
1971:       PetscLogFlops(a->nz);
1972:     } else xb = b;
1973:     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
1974:       for (i = m - 1; i >= 0; i--) {
1975:         n   = a->i[i + 1] - diag[i] - 1;
1976:         idx = a->j + diag[i] + 1;
1977:         v   = aa + diag[i] + 1;
1978:         sum = xb[i];
1979:         PetscSparseDenseMinusDot(sum, x, v, idx, n);
1980:         if (xb == b) {
1981:           x[i] = sum * idiag[i];
1982:         } else {
1983:           x[i] = (1 - omega) * x[i] + sum * idiag[i]; /* omega in idiag */
1984:         }
1985:       }
1986:       PetscLogFlops(a->nz); /* assumes 1/2 in upper */
1987:     }
1988:     its--;
1989:   }
1990:   while (its--) {
1991:     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
1992:       for (i = 0; i < m; i++) {
1993:         /* lower */
1994:         n   = diag[i] - a->i[i];
1995:         idx = a->j + a->i[i];
1996:         v   = aa + a->i[i];
1997:         sum = b[i];
1998:         PetscSparseDenseMinusDot(sum, x, v, idx, n);
1999:         t[i] = sum; /* save application of the lower-triangular part */
2000:         /* upper */
2001:         n   = a->i[i + 1] - diag[i] - 1;
2002:         idx = a->j + diag[i] + 1;
2003:         v   = aa + diag[i] + 1;
2004:         PetscSparseDenseMinusDot(sum, x, v, idx, n);
2005:         x[i] = (1. - omega) * x[i] + sum * idiag[i]; /* omega in idiag */
2006:       }
2007:       xb = t;
2008:       PetscLogFlops(2.0 * a->nz);
2009:     } else xb = b;
2010:     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
2011:       for (i = m - 1; i >= 0; i--) {
2012:         sum = xb[i];
2013:         if (xb == b) {
2014:           /* whole matrix (no checkpointing available) */
2015:           n   = a->i[i + 1] - a->i[i];
2016:           idx = a->j + a->i[i];
2017:           v   = aa + a->i[i];
2018:           PetscSparseDenseMinusDot(sum, x, v, idx, n);
2019:           x[i] = (1. - omega) * x[i] + (sum + mdiag[i] * x[i]) * idiag[i];
2020:         } else { /* lower-triangular part has been saved, so only apply upper-triangular */
2021:           n   = a->i[i + 1] - diag[i] - 1;
2022:           idx = a->j + diag[i] + 1;
2023:           v   = aa + diag[i] + 1;
2024:           PetscSparseDenseMinusDot(sum, x, v, idx, n);
2025:           x[i] = (1. - omega) * x[i] + sum * idiag[i]; /* omega in idiag */
2026:         }
2027:       }
2028:       if (xb == b) {
2029:         PetscLogFlops(2.0 * a->nz);
2030:       } else {
2031:         PetscLogFlops(a->nz); /* assumes 1/2 in upper */
2032:       }
2033:     }
2034:   }
2035:   MatSeqAIJRestoreArrayRead(A, &aa);
2036:   VecRestoreArray(xx, &x);
2037:   VecRestoreArrayRead(bb, &b);
2038:   return 0;
2039: }

2041: PetscErrorCode MatGetInfo_SeqAIJ(Mat A, MatInfoType flag, MatInfo *info)
2042: {
2043:   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;

2045:   info->block_size   = 1.0;
2046:   info->nz_allocated = a->maxnz;
2047:   info->nz_used      = a->nz;
2048:   info->nz_unneeded  = (a->maxnz - a->nz);
2049:   info->assemblies   = A->num_ass;
2050:   info->mallocs      = A->info.mallocs;
2051:   info->memory       = 0; /* REVIEW ME */
2052:   if (A->factortype) {
2053:     info->fill_ratio_given  = A->info.fill_ratio_given;
2054:     info->fill_ratio_needed = A->info.fill_ratio_needed;
2055:     info->factor_mallocs    = A->info.factor_mallocs;
2056:   } else {
2057:     info->fill_ratio_given  = 0;
2058:     info->fill_ratio_needed = 0;
2059:     info->factor_mallocs    = 0;
2060:   }
2061:   return 0;
2062: }

2064: PetscErrorCode MatZeroRows_SeqAIJ(Mat A, PetscInt N, const PetscInt rows[], PetscScalar diag, Vec x, Vec b)
2065: {
2066:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data;
2067:   PetscInt           i, m = A->rmap->n - 1;
2068:   const PetscScalar *xx;
2069:   PetscScalar       *bb, *aa;
2070:   PetscInt           d = 0;

2072:   if (x && b) {
2073:     VecGetArrayRead(x, &xx);
2074:     VecGetArray(b, &bb);
2075:     for (i = 0; i < N; i++) {
2077:       if (rows[i] >= A->cmap->n) continue;
2078:       bb[rows[i]] = diag * xx[rows[i]];
2079:     }
2080:     VecRestoreArrayRead(x, &xx);
2081:     VecRestoreArray(b, &bb);
2082:   }

2084:   MatSeqAIJGetArray(A, &aa);
2085:   if (a->keepnonzeropattern) {
2086:     for (i = 0; i < N; i++) {
2088:       PetscArrayzero(&aa[a->i[rows[i]]], a->ilen[rows[i]]);
2089:     }
2090:     if (diag != 0.0) {
2091:       for (i = 0; i < N; i++) {
2092:         d = rows[i];
2093:         if (rows[i] >= A->cmap->n) continue;
2095:       }
2096:       for (i = 0; i < N; i++) {
2097:         if (rows[i] >= A->cmap->n) continue;
2098:         aa[a->diag[rows[i]]] = diag;
2099:       }
2100:     }
2101:   } else {
2102:     if (diag != 0.0) {
2103:       for (i = 0; i < N; i++) {
2105:         if (a->ilen[rows[i]] > 0) {
2106:           if (rows[i] >= A->cmap->n) {
2107:             a->ilen[rows[i]] = 0;
2108:           } else {
2109:             a->ilen[rows[i]]    = 1;
2110:             aa[a->i[rows[i]]]   = diag;
2111:             a->j[a->i[rows[i]]] = rows[i];
2112:           }
2113:         } else if (rows[i] < A->cmap->n) { /* in case row was completely empty */
2114:           MatSetValues_SeqAIJ(A, 1, &rows[i], 1, &rows[i], &diag, INSERT_VALUES);
2115:         }
2116:       }
2117:     } else {
2118:       for (i = 0; i < N; i++) {
2120:         a->ilen[rows[i]] = 0;
2121:       }
2122:     }
2123:     A->nonzerostate++;
2124:   }
2125:   MatSeqAIJRestoreArray(A, &aa);
2126:   PetscUseTypeMethod(A, assemblyend, MAT_FINAL_ASSEMBLY);
2127:   return 0;
2128: }

2130: PetscErrorCode MatZeroRowsColumns_SeqAIJ(Mat A, PetscInt N, const PetscInt rows[], PetscScalar diag, Vec x, Vec b)
2131: {
2132:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data;
2133:   PetscInt           i, j, m = A->rmap->n - 1, d = 0;
2134:   PetscBool          missing, *zeroed, vecs = PETSC_FALSE;
2135:   const PetscScalar *xx;
2136:   PetscScalar       *bb, *aa;

2138:   if (!N) return 0;
2139:   MatSeqAIJGetArray(A, &aa);
2140:   if (x && b) {
2141:     VecGetArrayRead(x, &xx);
2142:     VecGetArray(b, &bb);
2143:     vecs = PETSC_TRUE;
2144:   }
2145:   PetscCalloc1(A->rmap->n, &zeroed);
2146:   for (i = 0; i < N; i++) {
2148:     PetscArrayzero(&aa[a->i[rows[i]]], a->ilen[rows[i]]);

2150:     zeroed[rows[i]] = PETSC_TRUE;
2151:   }
2152:   for (i = 0; i < A->rmap->n; i++) {
2153:     if (!zeroed[i]) {
2154:       for (j = a->i[i]; j < a->i[i + 1]; j++) {
2155:         if (a->j[j] < A->rmap->n && zeroed[a->j[j]]) {
2156:           if (vecs) bb[i] -= aa[j] * xx[a->j[j]];
2157:           aa[j] = 0.0;
2158:         }
2159:       }
2160:     } else if (vecs && i < A->cmap->N) bb[i] = diag * xx[i];
2161:   }
2162:   if (x && b) {
2163:     VecRestoreArrayRead(x, &xx);
2164:     VecRestoreArray(b, &bb);
2165:   }
2166:   PetscFree(zeroed);
2167:   if (diag != 0.0) {
2168:     MatMissingDiagonal_SeqAIJ(A, &missing, &d);
2169:     if (missing) {
2170:       for (i = 0; i < N; i++) {
2171:         if (rows[i] >= A->cmap->N) continue;
2173:         MatSetValues_SeqAIJ(A, 1, &rows[i], 1, &rows[i], &diag, INSERT_VALUES);
2174:       }
2175:     } else {
2176:       for (i = 0; i < N; i++) aa[a->diag[rows[i]]] = diag;
2177:     }
2178:   }
2179:   MatSeqAIJRestoreArray(A, &aa);
2180:   PetscUseTypeMethod(A, assemblyend, MAT_FINAL_ASSEMBLY);
2181:   return 0;
2182: }

2184: PetscErrorCode MatGetRow_SeqAIJ(Mat A, PetscInt row, PetscInt *nz, PetscInt **idx, PetscScalar **v)
2185: {
2186:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data;
2187:   const PetscScalar *aa;
2188:   PetscInt          *itmp;

2190:   MatSeqAIJGetArrayRead(A, &aa);
2191:   *nz = a->i[row + 1] - a->i[row];
2192:   if (v) *v = (PetscScalar *)(aa + a->i[row]);
2193:   if (idx) {
2194:     itmp = a->j + a->i[row];
2195:     if (*nz) *idx = itmp;
2196:     else *idx = NULL;
2197:   }
2198:   MatSeqAIJRestoreArrayRead(A, &aa);
2199:   return 0;
2200: }

2202: PetscErrorCode MatRestoreRow_SeqAIJ(Mat A, PetscInt row, PetscInt *nz, PetscInt **idx, PetscScalar **v)
2203: {
2204:   if (nz) *nz = 0;
2205:   if (idx) *idx = NULL;
2206:   if (v) *v = NULL;
2207:   return 0;
2208: }

2210: PetscErrorCode MatNorm_SeqAIJ(Mat A, NormType type, PetscReal *nrm)
2211: {
2212:   Mat_SeqAIJ      *a = (Mat_SeqAIJ *)A->data;
2213:   const MatScalar *v;
2214:   PetscReal        sum = 0.0;
2215:   PetscInt         i, j;

2217:   MatSeqAIJGetArrayRead(A, &v);
2218:   if (type == NORM_FROBENIUS) {
2219: #if defined(PETSC_USE_REAL___FP16)
2220:     PetscBLASInt one = 1, nz = a->nz;
2221:     PetscCallBLAS("BLASnrm2", *nrm = BLASnrm2_(&nz, v, &one));
2222: #else
2223:     for (i = 0; i < a->nz; i++) {
2224:       sum += PetscRealPart(PetscConj(*v) * (*v));
2225:       v++;
2226:     }
2227:     *nrm = PetscSqrtReal(sum);
2228: #endif
2229:     PetscLogFlops(2.0 * a->nz);
2230:   } else if (type == NORM_1) {
2231:     PetscReal *tmp;
2232:     PetscInt  *jj = a->j;
2233:     PetscCalloc1(A->cmap->n + 1, &tmp);
2234:     *nrm = 0.0;
2235:     for (j = 0; j < a->nz; j++) {
2236:       tmp[*jj++] += PetscAbsScalar(*v);
2237:       v++;
2238:     }
2239:     for (j = 0; j < A->cmap->n; j++) {
2240:       if (tmp[j] > *nrm) *nrm = tmp[j];
2241:     }
2242:     PetscFree(tmp);
2243:     PetscLogFlops(PetscMax(a->nz - 1, 0));
2244:   } else if (type == NORM_INFINITY) {
2245:     *nrm = 0.0;
2246:     for (j = 0; j < A->rmap->n; j++) {
2247:       const PetscScalar *v2 = v + a->i[j];
2248:       sum                   = 0.0;
2249:       for (i = 0; i < a->i[j + 1] - a->i[j]; i++) {
2250:         sum += PetscAbsScalar(*v2);
2251:         v2++;
2252:       }
2253:       if (sum > *nrm) *nrm = sum;
2254:     }
2255:     PetscLogFlops(PetscMax(a->nz - 1, 0));
2256:   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "No support for two norm");
2257:   MatSeqAIJRestoreArrayRead(A, &v);
2258:   return 0;
2259: }

2261: PetscErrorCode MatIsTranspose_SeqAIJ(Mat A, Mat B, PetscReal tol, PetscBool *f)
2262: {
2263:   Mat_SeqAIJ      *aij = (Mat_SeqAIJ *)A->data, *bij = (Mat_SeqAIJ *)B->data;
2264:   PetscInt        *adx, *bdx, *aii, *bii, *aptr, *bptr;
2265:   const MatScalar *va, *vb;
2266:   PetscInt         ma, na, mb, nb, i;

2268:   MatGetSize(A, &ma, &na);
2269:   MatGetSize(B, &mb, &nb);
2270:   if (ma != nb || na != mb) {
2271:     *f = PETSC_FALSE;
2272:     return 0;
2273:   }
2274:   MatSeqAIJGetArrayRead(A, &va);
2275:   MatSeqAIJGetArrayRead(B, &vb);
2276:   aii = aij->i;
2277:   bii = bij->i;
2278:   adx = aij->j;
2279:   bdx = bij->j;
2280:   PetscMalloc1(ma, &aptr);
2281:   PetscMalloc1(mb, &bptr);
2282:   for (i = 0; i < ma; i++) aptr[i] = aii[i];
2283:   for (i = 0; i < mb; i++) bptr[i] = bii[i];

2285:   *f = PETSC_TRUE;
2286:   for (i = 0; i < ma; i++) {
2287:     while (aptr[i] < aii[i + 1]) {
2288:       PetscInt    idc, idr;
2289:       PetscScalar vc, vr;
2290:       /* column/row index/value */
2291:       idc = adx[aptr[i]];
2292:       idr = bdx[bptr[idc]];
2293:       vc  = va[aptr[i]];
2294:       vr  = vb[bptr[idc]];
2295:       if (i != idr || PetscAbsScalar(vc - vr) > tol) {
2296:         *f = PETSC_FALSE;
2297:         goto done;
2298:       } else {
2299:         aptr[i]++;
2300:         if (B || i != idc) bptr[idc]++;
2301:       }
2302:     }
2303:   }
2304: done:
2305:   PetscFree(aptr);
2306:   PetscFree(bptr);
2307:   MatSeqAIJRestoreArrayRead(A, &va);
2308:   MatSeqAIJRestoreArrayRead(B, &vb);
2309:   return 0;
2310: }

2312: PetscErrorCode MatIsHermitianTranspose_SeqAIJ(Mat A, Mat B, PetscReal tol, PetscBool *f)
2313: {
2314:   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data, *bij = (Mat_SeqAIJ *)B->data;
2315:   PetscInt   *adx, *bdx, *aii, *bii, *aptr, *bptr;
2316:   MatScalar  *va, *vb;
2317:   PetscInt    ma, na, mb, nb, i;

2319:   MatGetSize(A, &ma, &na);
2320:   MatGetSize(B, &mb, &nb);
2321:   if (ma != nb || na != mb) {
2322:     *f = PETSC_FALSE;
2323:     return 0;
2324:   }
2325:   aii = aij->i;
2326:   bii = bij->i;
2327:   adx = aij->j;
2328:   bdx = bij->j;
2329:   va  = aij->a;
2330:   vb  = bij->a;
2331:   PetscMalloc1(ma, &aptr);
2332:   PetscMalloc1(mb, &bptr);
2333:   for (i = 0; i < ma; i++) aptr[i] = aii[i];
2334:   for (i = 0; i < mb; i++) bptr[i] = bii[i];

2336:   *f = PETSC_TRUE;
2337:   for (i = 0; i < ma; i++) {
2338:     while (aptr[i] < aii[i + 1]) {
2339:       PetscInt    idc, idr;
2340:       PetscScalar vc, vr;
2341:       /* column/row index/value */
2342:       idc = adx[aptr[i]];
2343:       idr = bdx[bptr[idc]];
2344:       vc  = va[aptr[i]];
2345:       vr  = vb[bptr[idc]];
2346:       if (i != idr || PetscAbsScalar(vc - PetscConj(vr)) > tol) {
2347:         *f = PETSC_FALSE;
2348:         goto done;
2349:       } else {
2350:         aptr[i]++;
2351:         if (B || i != idc) bptr[idc]++;
2352:       }
2353:     }
2354:   }
2355: done:
2356:   PetscFree(aptr);
2357:   PetscFree(bptr);
2358:   return 0;
2359: }

2361: PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A, PetscReal tol, PetscBool *f)
2362: {
2363:   MatIsTranspose_SeqAIJ(A, A, tol, f);
2364:   return 0;
2365: }

2367: PetscErrorCode MatIsHermitian_SeqAIJ(Mat A, PetscReal tol, PetscBool *f)
2368: {
2369:   MatIsHermitianTranspose_SeqAIJ(A, A, tol, f);
2370:   return 0;
2371: }

2373: PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A, Vec ll, Vec rr)
2374: {
2375:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data;
2376:   const PetscScalar *l, *r;
2377:   PetscScalar        x;
2378:   MatScalar         *v;
2379:   PetscInt           i, j, m = A->rmap->n, n = A->cmap->n, M, nz = a->nz;
2380:   const PetscInt    *jj;

2382:   if (ll) {
2383:     /* The local size is used so that VecMPI can be passed to this routine
2384:        by MatDiagonalScale_MPIAIJ */
2385:     VecGetLocalSize(ll, &m);
2387:     VecGetArrayRead(ll, &l);
2388:     MatSeqAIJGetArray(A, &v);
2389:     for (i = 0; i < m; i++) {
2390:       x = l[i];
2391:       M = a->i[i + 1] - a->i[i];
2392:       for (j = 0; j < M; j++) (*v++) *= x;
2393:     }
2394:     VecRestoreArrayRead(ll, &l);
2395:     PetscLogFlops(nz);
2396:     MatSeqAIJRestoreArray(A, &v);
2397:   }
2398:   if (rr) {
2399:     VecGetLocalSize(rr, &n);
2401:     VecGetArrayRead(rr, &r);
2402:     MatSeqAIJGetArray(A, &v);
2403:     jj = a->j;
2404:     for (i = 0; i < nz; i++) (*v++) *= r[*jj++];
2405:     MatSeqAIJRestoreArray(A, &v);
2406:     VecRestoreArrayRead(rr, &r);
2407:     PetscLogFlops(nz);
2408:   }
2409:   MatSeqAIJInvalidateDiagonal(A);
2410:   return 0;
2411: }

2413: PetscErrorCode MatCreateSubMatrix_SeqAIJ(Mat A, IS isrow, IS iscol, PetscInt csize, MatReuse scall, Mat *B)
2414: {
2415:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data, *c;
2416:   PetscInt          *smap, i, k, kstart, kend, oldcols = A->cmap->n, *lens;
2417:   PetscInt           row, mat_i, *mat_j, tcol, first, step, *mat_ilen, sum, lensi;
2418:   const PetscInt    *irow, *icol;
2419:   const PetscScalar *aa;
2420:   PetscInt           nrows, ncols;
2421:   PetscInt          *starts, *j_new, *i_new, *aj = a->j, *ai = a->i, ii, *ailen = a->ilen;
2422:   MatScalar         *a_new, *mat_a, *c_a;
2423:   Mat                C;
2424:   PetscBool          stride;

2426:   ISGetIndices(isrow, &irow);
2427:   ISGetLocalSize(isrow, &nrows);
2428:   ISGetLocalSize(iscol, &ncols);

2430:   PetscObjectTypeCompare((PetscObject)iscol, ISSTRIDE, &stride);
2431:   if (stride) {
2432:     ISStrideGetInfo(iscol, &first, &step);
2433:   } else {
2434:     first = 0;
2435:     step  = 0;
2436:   }
2437:   if (stride && step == 1) {
2438:     /* special case of contiguous rows */
2439:     PetscMalloc2(nrows, &lens, nrows, &starts);
2440:     /* loop over new rows determining lens and starting points */
2441:     for (i = 0; i < nrows; i++) {
2442:       kstart    = ai[irow[i]];
2443:       kend      = kstart + ailen[irow[i]];
2444:       starts[i] = kstart;
2445:       for (k = kstart; k < kend; k++) {
2446:         if (aj[k] >= first) {
2447:           starts[i] = k;
2448:           break;
2449:         }
2450:       }
2451:       sum = 0;
2452:       while (k < kend) {
2453:         if (aj[k++] >= first + ncols) break;
2454:         sum++;
2455:       }
2456:       lens[i] = sum;
2457:     }
2458:     /* create submatrix */
2459:     if (scall == MAT_REUSE_MATRIX) {
2460:       PetscInt n_cols, n_rows;
2461:       MatGetSize(*B, &n_rows, &n_cols);
2463:       MatZeroEntries(*B);
2464:       C = *B;
2465:     } else {
2466:       PetscInt rbs, cbs;
2467:       MatCreate(PetscObjectComm((PetscObject)A), &C);
2468:       MatSetSizes(C, nrows, ncols, PETSC_DETERMINE, PETSC_DETERMINE);
2469:       ISGetBlockSize(isrow, &rbs);
2470:       ISGetBlockSize(iscol, &cbs);
2471:       MatSetBlockSizes(C, rbs, cbs);
2472:       MatSetType(C, ((PetscObject)A)->type_name);
2473:       MatSeqAIJSetPreallocation_SeqAIJ(C, 0, lens);
2474:     }
2475:     c = (Mat_SeqAIJ *)C->data;

2477:     /* loop over rows inserting into submatrix */
2478:     MatSeqAIJGetArrayWrite(C, &a_new); // Not 'a_new = c->a-new', since that raw usage ignores offload state of C
2479:     j_new = c->j;
2480:     i_new = c->i;
2481:     MatSeqAIJGetArrayRead(A, &aa);
2482:     for (i = 0; i < nrows; i++) {
2483:       ii    = starts[i];
2484:       lensi = lens[i];
2485:       for (k = 0; k < lensi; k++) *j_new++ = aj[ii + k] - first;
2486:       PetscArraycpy(a_new, aa + starts[i], lensi);
2487:       a_new += lensi;
2488:       i_new[i + 1] = i_new[i] + lensi;
2489:       c->ilen[i]   = lensi;
2490:     }
2491:     MatSeqAIJRestoreArrayWrite(C, &a_new); // Set C's offload state properly
2492:     MatSeqAIJRestoreArrayRead(A, &aa);
2493:     PetscFree2(lens, starts);
2494:   } else {
2495:     ISGetIndices(iscol, &icol);
2496:     PetscCalloc1(oldcols, &smap);
2497:     PetscMalloc1(1 + nrows, &lens);
2498:     for (i = 0; i < ncols; i++) {
2500:       smap[icol[i]] = i + 1;
2501:     }

2503:     /* determine lens of each row */
2504:     for (i = 0; i < nrows; i++) {
2505:       kstart  = ai[irow[i]];
2506:       kend    = kstart + a->ilen[irow[i]];
2507:       lens[i] = 0;
2508:       for (k = kstart; k < kend; k++) {
2509:         if (smap[aj[k]]) lens[i]++;
2510:       }
2511:     }
2512:     /* Create and fill new matrix */
2513:     if (scall == MAT_REUSE_MATRIX) {
2514:       PetscBool equal;

2516:       c = (Mat_SeqAIJ *)((*B)->data);
2518:       PetscArraycmp(c->ilen, lens, (*B)->rmap->n, &equal);
2520:       PetscArrayzero(c->ilen, (*B)->rmap->n);
2521:       C = *B;
2522:     } else {
2523:       PetscInt rbs, cbs;
2524:       MatCreate(PetscObjectComm((PetscObject)A), &C);
2525:       MatSetSizes(C, nrows, ncols, PETSC_DETERMINE, PETSC_DETERMINE);
2526:       ISGetBlockSize(isrow, &rbs);
2527:       ISGetBlockSize(iscol, &cbs);
2528:       MatSetBlockSizes(C, rbs, cbs);
2529:       MatSetType(C, ((PetscObject)A)->type_name);
2530:       MatSeqAIJSetPreallocation_SeqAIJ(C, 0, lens);
2531:     }
2532:     MatSeqAIJGetArrayRead(A, &aa);

2534:     c = (Mat_SeqAIJ *)(C->data);
2535:     MatSeqAIJGetArrayWrite(C, &c_a); // Not 'c->a', since that raw usage ignores offload state of C
2536:     for (i = 0; i < nrows; i++) {
2537:       row      = irow[i];
2538:       kstart   = ai[row];
2539:       kend     = kstart + a->ilen[row];
2540:       mat_i    = c->i[i];
2541:       mat_j    = c->j + mat_i;
2542:       mat_a    = c_a + mat_i;
2543:       mat_ilen = c->ilen + i;
2544:       for (k = kstart; k < kend; k++) {
2545:         if ((tcol = smap[a->j[k]])) {
2546:           *mat_j++ = tcol - 1;
2547:           *mat_a++ = aa[k];
2548:           (*mat_ilen)++;
2549:         }
2550:       }
2551:     }
2552:     MatSeqAIJRestoreArrayRead(A, &aa);
2553:     /* Free work space */
2554:     ISRestoreIndices(iscol, &icol);
2555:     PetscFree(smap);
2556:     PetscFree(lens);
2557:     /* sort */
2558:     for (i = 0; i < nrows; i++) {
2559:       PetscInt ilen;

2561:       mat_i = c->i[i];
2562:       mat_j = c->j + mat_i;
2563:       mat_a = c_a + mat_i;
2564:       ilen  = c->ilen[i];
2565:       PetscSortIntWithScalarArray(ilen, mat_j, mat_a);
2566:     }
2567:     MatSeqAIJRestoreArrayWrite(C, &c_a);
2568:   }
2569: #if defined(PETSC_HAVE_DEVICE)
2570:   MatBindToCPU(C, A->boundtocpu);
2571: #endif
2572:   MatAssemblyBegin(C, MAT_FINAL_ASSEMBLY);
2573:   MatAssemblyEnd(C, MAT_FINAL_ASSEMBLY);

2575:   ISRestoreIndices(isrow, &irow);
2576:   *B = C;
2577:   return 0;
2578: }

2580: PetscErrorCode MatGetMultiProcBlock_SeqAIJ(Mat mat, MPI_Comm subComm, MatReuse scall, Mat *subMat)
2581: {
2582:   Mat B;

2584:   if (scall == MAT_INITIAL_MATRIX) {
2585:     MatCreate(subComm, &B);
2586:     MatSetSizes(B, mat->rmap->n, mat->cmap->n, mat->rmap->n, mat->cmap->n);
2587:     MatSetBlockSizesFromMats(B, mat, mat);
2588:     MatSetType(B, MATSEQAIJ);
2589:     MatDuplicateNoCreate_SeqAIJ(B, mat, MAT_COPY_VALUES, PETSC_TRUE);
2590:     *subMat = B;
2591:   } else {
2592:     MatCopy_SeqAIJ(mat, *subMat, SAME_NONZERO_PATTERN);
2593:   }
2594:   return 0;
2595: }

2597: PetscErrorCode MatILUFactor_SeqAIJ(Mat inA, IS row, IS col, const MatFactorInfo *info)
2598: {
2599:   Mat_SeqAIJ *a = (Mat_SeqAIJ *)inA->data;
2600:   Mat         outA;
2601:   PetscBool   row_identity, col_identity;


2605:   ISIdentity(row, &row_identity);
2606:   ISIdentity(col, &col_identity);

2608:   outA             = inA;
2609:   outA->factortype = MAT_FACTOR_LU;
2610:   PetscFree(inA->solvertype);
2611:   PetscStrallocpy(MATSOLVERPETSC, &inA->solvertype);

2613:   PetscObjectReference((PetscObject)row);
2614:   ISDestroy(&a->row);

2616:   a->row = row;

2618:   PetscObjectReference((PetscObject)col);
2619:   ISDestroy(&a->col);

2621:   a->col = col;

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

2627:   if (!a->solve_work) { /* this matrix may have been factored before */
2628:     PetscMalloc1(inA->rmap->n + 1, &a->solve_work);
2629:   }

2631:   MatMarkDiagonal_SeqAIJ(inA);
2632:   if (row_identity && col_identity) {
2633:     MatLUFactorNumeric_SeqAIJ_inplace(outA, inA, info);
2634:   } else {
2635:     MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA, inA, info);
2636:   }
2637:   return 0;
2638: }

2640: PetscErrorCode MatScale_SeqAIJ(Mat inA, PetscScalar alpha)
2641: {
2642:   Mat_SeqAIJ  *a = (Mat_SeqAIJ *)inA->data;
2643:   PetscScalar *v;
2644:   PetscBLASInt one = 1, bnz;

2646:   MatSeqAIJGetArray(inA, &v);
2647:   PetscBLASIntCast(a->nz, &bnz);
2648:   PetscCallBLAS("BLASscal", BLASscal_(&bnz, &alpha, v, &one));
2649:   PetscLogFlops(a->nz);
2650:   MatSeqAIJRestoreArray(inA, &v);
2651:   MatSeqAIJInvalidateDiagonal(inA);
2652:   return 0;
2653: }

2655: PetscErrorCode MatDestroySubMatrix_Private(Mat_SubSppt *submatj)
2656: {
2657:   PetscInt i;

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

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

2665:     if (submatj->rbuf1) {
2666:       PetscFree(submatj->rbuf1[0]);
2667:       PetscFree(submatj->rbuf1);
2668:     }

2670:     for (i = 0; i < submatj->nrqs; ++i) PetscFree(submatj->rbuf3[i]);
2671:     PetscFree3(submatj->req_source2, submatj->rbuf2, submatj->rbuf3);
2672:     PetscFree(submatj->pa);
2673:   }

2675: #if defined(PETSC_USE_CTABLE)
2676:   PetscTableDestroy((PetscTable *)&submatj->rmap);
2677:   if (submatj->cmap_loc) PetscFree(submatj->cmap_loc);
2678:   PetscFree(submatj->rmap_loc);
2679: #else
2680:   PetscFree(submatj->rmap);
2681: #endif

2683:   if (!submatj->allcolumns) {
2684: #if defined(PETSC_USE_CTABLE)
2685:     PetscTableDestroy((PetscTable *)&submatj->cmap);
2686: #else
2687:     PetscFree(submatj->cmap);
2688: #endif
2689:   }
2690:   PetscFree(submatj->row2proc);

2692:   PetscFree(submatj);
2693:   return 0;
2694: }

2696: PetscErrorCode MatDestroySubMatrix_SeqAIJ(Mat C)
2697: {
2698:   Mat_SeqAIJ  *c       = (Mat_SeqAIJ *)C->data;
2699:   Mat_SubSppt *submatj = c->submatis1;

2701:   (*submatj->destroy)(C);
2702:   MatDestroySubMatrix_Private(submatj);
2703:   return 0;
2704: }

2706: /* Note this has code duplication with MatDestroySubMatrices_SeqBAIJ() */
2707: PetscErrorCode MatDestroySubMatrices_SeqAIJ(PetscInt n, Mat *mat[])
2708: {
2709:   PetscInt     i;
2710:   Mat          C;
2711:   Mat_SeqAIJ  *c;
2712:   Mat_SubSppt *submatj;

2714:   for (i = 0; i < n; i++) {
2715:     C       = (*mat)[i];
2716:     c       = (Mat_SeqAIJ *)C->data;
2717:     submatj = c->submatis1;
2718:     if (submatj) {
2719:       if (--((PetscObject)C)->refct <= 0) {
2720:         PetscFree(C->factorprefix);
2721:         (*submatj->destroy)(C);
2722:         MatDestroySubMatrix_Private(submatj);
2723:         PetscFree(C->defaultvectype);
2724:         PetscFree(C->defaultrandtype);
2725:         PetscLayoutDestroy(&C->rmap);
2726:         PetscLayoutDestroy(&C->cmap);
2727:         PetscHeaderDestroy(&C);
2728:       }
2729:     } else {
2730:       MatDestroy(&C);
2731:     }
2732:   }

2734:   /* Destroy Dummy submatrices created for reuse */
2735:   MatDestroySubMatrices_Dummy(n, mat);

2737:   PetscFree(*mat);
2738:   return 0;
2739: }

2741: PetscErrorCode MatCreateSubMatrices_SeqAIJ(Mat A, PetscInt n, const IS irow[], const IS icol[], MatReuse scall, Mat *B[])
2742: {
2743:   PetscInt i;

2745:   if (scall == MAT_INITIAL_MATRIX) PetscCalloc1(n + 1, B);

2747:   for (i = 0; i < n; i++) MatCreateSubMatrix_SeqAIJ(A, irow[i], icol[i], PETSC_DECIDE, scall, &(*B)[i]);
2748:   return 0;
2749: }

2751: PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A, PetscInt is_max, IS is[], PetscInt ov)
2752: {
2753:   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data;
2754:   PetscInt        row, i, j, k, l, ll, m, n, *nidx, isz, val;
2755:   const PetscInt *idx;
2756:   PetscInt        start, end, *ai, *aj, bs = (A->rmap->bs > 0 && A->rmap->bs == A->cmap->bs) ? A->rmap->bs : 1;
2757:   PetscBT         table;

2759:   m  = A->rmap->n / bs;
2760:   ai = a->i;
2761:   aj = a->j;


2765:   PetscMalloc1(m + 1, &nidx);
2766:   PetscBTCreate(m, &table);

2768:   for (i = 0; i < is_max; i++) {
2769:     /* Initialize the two local arrays */
2770:     isz = 0;
2771:     PetscBTMemzero(m, table);

2773:     /* Extract the indices, assume there can be duplicate entries */
2774:     ISGetIndices(is[i], &idx);
2775:     ISGetLocalSize(is[i], &n);

2777:     if (bs > 1) {
2778:       /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
2779:       for (j = 0; j < n; ++j) {
2780:         if (!PetscBTLookupSet(table, idx[j] / bs)) nidx[isz++] = idx[j] / bs;
2781:       }
2782:       ISRestoreIndices(is[i], &idx);
2783:       ISDestroy(&is[i]);

2785:       k = 0;
2786:       for (j = 0; j < ov; j++) { /* for each overlap */
2787:         n = isz;
2788:         for (; k < n; k++) { /* do only those rows in nidx[k], which are not done yet */
2789:           for (ll = 0; ll < bs; ll++) {
2790:             row   = bs * nidx[k] + ll;
2791:             start = ai[row];
2792:             end   = ai[row + 1];
2793:             for (l = start; l < end; l++) {
2794:               val = aj[l] / bs;
2795:               if (!PetscBTLookupSet(table, val)) nidx[isz++] = val;
2796:             }
2797:           }
2798:         }
2799:       }
2800:       ISCreateBlock(PETSC_COMM_SELF, bs, isz, nidx, PETSC_COPY_VALUES, (is + i));
2801:     } else {
2802:       /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
2803:       for (j = 0; j < n; ++j) {
2804:         if (!PetscBTLookupSet(table, idx[j])) nidx[isz++] = idx[j];
2805:       }
2806:       ISRestoreIndices(is[i], &idx);
2807:       ISDestroy(&is[i]);

2809:       k = 0;
2810:       for (j = 0; j < ov; j++) { /* for each overlap */
2811:         n = isz;
2812:         for (; k < n; k++) { /* do only those rows in nidx[k], which are not done yet */
2813:           row   = nidx[k];
2814:           start = ai[row];
2815:           end   = ai[row + 1];
2816:           for (l = start; l < end; l++) {
2817:             val = aj[l];
2818:             if (!PetscBTLookupSet(table, val)) nidx[isz++] = val;
2819:           }
2820:         }
2821:       }
2822:       ISCreateGeneral(PETSC_COMM_SELF, isz, nidx, PETSC_COPY_VALUES, (is + i));
2823:     }
2824:   }
2825:   PetscBTDestroy(&table);
2826:   PetscFree(nidx);
2827:   return 0;
2828: }

2830: /* -------------------------------------------------------------- */
2831: PetscErrorCode MatPermute_SeqAIJ(Mat A, IS rowp, IS colp, Mat *B)
2832: {
2833:   Mat_SeqAIJ     *a = (Mat_SeqAIJ *)A->data;
2834:   PetscInt        i, nz = 0, m = A->rmap->n, n = A->cmap->n;
2835:   const PetscInt *row, *col;
2836:   PetscInt       *cnew, j, *lens;
2837:   IS              icolp, irowp;
2838:   PetscInt       *cwork = NULL;
2839:   PetscScalar    *vwork = NULL;

2841:   ISInvertPermutation(rowp, PETSC_DECIDE, &irowp);
2842:   ISGetIndices(irowp, &row);
2843:   ISInvertPermutation(colp, PETSC_DECIDE, &icolp);
2844:   ISGetIndices(icolp, &col);

2846:   /* determine lengths of permuted rows */
2847:   PetscMalloc1(m + 1, &lens);
2848:   for (i = 0; i < m; i++) lens[row[i]] = a->i[i + 1] - a->i[i];
2849:   MatCreate(PetscObjectComm((PetscObject)A), B);
2850:   MatSetSizes(*B, m, n, m, n);
2851:   MatSetBlockSizesFromMats(*B, A, A);
2852:   MatSetType(*B, ((PetscObject)A)->type_name);
2853:   MatSeqAIJSetPreallocation_SeqAIJ(*B, 0, lens);
2854:   PetscFree(lens);

2856:   PetscMalloc1(n, &cnew);
2857:   for (i = 0; i < m; i++) {
2858:     MatGetRow_SeqAIJ(A, i, &nz, &cwork, &vwork);
2859:     for (j = 0; j < nz; j++) cnew[j] = col[cwork[j]];
2860:     MatSetValues_SeqAIJ(*B, 1, &row[i], nz, cnew, vwork, INSERT_VALUES);
2861:     MatRestoreRow_SeqAIJ(A, i, &nz, &cwork, &vwork);
2862:   }
2863:   PetscFree(cnew);

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

2867: #if defined(PETSC_HAVE_DEVICE)
2868:   MatBindToCPU(*B, A->boundtocpu);
2869: #endif
2870:   MatAssemblyBegin(*B, MAT_FINAL_ASSEMBLY);
2871:   MatAssemblyEnd(*B, MAT_FINAL_ASSEMBLY);
2872:   ISRestoreIndices(irowp, &row);
2873:   ISRestoreIndices(icolp, &col);
2874:   ISDestroy(&irowp);
2875:   ISDestroy(&icolp);
2876:   if (rowp == colp) MatPropagateSymmetryOptions(A, *B);
2877:   return 0;
2878: }

2880: PetscErrorCode MatCopy_SeqAIJ(Mat A, Mat B, MatStructure str)
2881: {
2882:   /* If the two matrices have the same copy implementation, use fast copy. */
2883:   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
2884:     Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data;
2885:     Mat_SeqAIJ        *b = (Mat_SeqAIJ *)B->data;
2886:     const PetscScalar *aa;

2888:     MatSeqAIJGetArrayRead(A, &aa);
2890:     PetscArraycpy(b->a, aa, a->i[A->rmap->n]);
2891:     PetscObjectStateIncrease((PetscObject)B);
2892:     MatSeqAIJRestoreArrayRead(A, &aa);
2893:   } else {
2894:     MatCopy_Basic(A, B, str);
2895:   }
2896:   return 0;
2897: }

2899: PetscErrorCode MatSetUp_SeqAIJ(Mat A)
2900: {
2901:   MatSeqAIJSetPreallocation_SeqAIJ(A, PETSC_DEFAULT, NULL);
2902:   return 0;
2903: }

2905: PETSC_INTERN PetscErrorCode MatSeqAIJGetArray_SeqAIJ(Mat A, PetscScalar *array[])
2906: {
2907:   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;

2909:   *array = a->a;
2910:   return 0;
2911: }

2913: PETSC_INTERN PetscErrorCode MatSeqAIJRestoreArray_SeqAIJ(Mat A, PetscScalar *array[])
2914: {
2915:   *array = NULL;
2916:   return 0;
2917: }

2919: /*
2920:    Computes the number of nonzeros per row needed for preallocation when X and Y
2921:    have different nonzero structure.
2922: */
2923: PetscErrorCode MatAXPYGetPreallocation_SeqX_private(PetscInt m, const PetscInt *xi, const PetscInt *xj, const PetscInt *yi, const PetscInt *yj, PetscInt *nnz)
2924: {
2925:   PetscInt i, j, k, nzx, nzy;

2927:   /* Set the number of nonzeros in the new matrix */
2928:   for (i = 0; i < m; i++) {
2929:     const PetscInt *xjj = xj + xi[i], *yjj = yj + yi[i];
2930:     nzx    = xi[i + 1] - xi[i];
2931:     nzy    = yi[i + 1] - yi[i];
2932:     nnz[i] = 0;
2933:     for (j = 0, k = 0; j < nzx; j++) {                  /* Point in X */
2934:       for (; k < nzy && yjj[k] < xjj[j]; k++) nnz[i]++; /* Catch up to X */
2935:       if (k < nzy && yjj[k] == xjj[j]) k++;             /* Skip duplicate */
2936:       nnz[i]++;
2937:     }
2938:     for (; k < nzy; k++) nnz[i]++;
2939:   }
2940:   return 0;
2941: }

2943: PetscErrorCode MatAXPYGetPreallocation_SeqAIJ(Mat Y, Mat X, PetscInt *nnz)
2944: {
2945:   PetscInt    m = Y->rmap->N;
2946:   Mat_SeqAIJ *x = (Mat_SeqAIJ *)X->data;
2947:   Mat_SeqAIJ *y = (Mat_SeqAIJ *)Y->data;

2949:   /* Set the number of nonzeros in the new matrix */
2950:   MatAXPYGetPreallocation_SeqX_private(m, x->i, x->j, y->i, y->j, nnz);
2951:   return 0;
2952: }

2954: PetscErrorCode MatAXPY_SeqAIJ(Mat Y, PetscScalar a, Mat X, MatStructure str)
2955: {
2956:   Mat_SeqAIJ *x = (Mat_SeqAIJ *)X->data, *y = (Mat_SeqAIJ *)Y->data;

2958:   if (str == UNKNOWN_NONZERO_PATTERN || (PetscDefined(USE_DEBUG) && str == SAME_NONZERO_PATTERN)) {
2959:     PetscBool e = x->nz == y->nz ? PETSC_TRUE : PETSC_FALSE;
2960:     if (e) {
2961:       PetscArraycmp(x->i, y->i, Y->rmap->n + 1, &e);
2962:       if (e) {
2963:         PetscArraycmp(x->j, y->j, y->nz, &e);
2964:         if (e) str = SAME_NONZERO_PATTERN;
2965:       }
2966:     }
2968:   }
2969:   if (str == SAME_NONZERO_PATTERN) {
2970:     const PetscScalar *xa;
2971:     PetscScalar       *ya, alpha = a;
2972:     PetscBLASInt       one = 1, bnz;

2974:     PetscBLASIntCast(x->nz, &bnz);
2975:     MatSeqAIJGetArray(Y, &ya);
2976:     MatSeqAIJGetArrayRead(X, &xa);
2977:     PetscCallBLAS("BLASaxpy", BLASaxpy_(&bnz, &alpha, xa, &one, ya, &one));
2978:     MatSeqAIJRestoreArrayRead(X, &xa);
2979:     MatSeqAIJRestoreArray(Y, &ya);
2980:     PetscLogFlops(2.0 * bnz);
2981:     MatSeqAIJInvalidateDiagonal(Y);
2982:     PetscObjectStateIncrease((PetscObject)Y);
2983:   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2984:     MatAXPY_Basic(Y, a, X, str);
2985:   } else {
2986:     Mat       B;
2987:     PetscInt *nnz;
2988:     PetscMalloc1(Y->rmap->N, &nnz);
2989:     MatCreate(PetscObjectComm((PetscObject)Y), &B);
2990:     PetscObjectSetName((PetscObject)B, ((PetscObject)Y)->name);
2991:     MatSetLayouts(B, Y->rmap, Y->cmap);
2992:     MatSetType(B, ((PetscObject)Y)->type_name);
2993:     MatAXPYGetPreallocation_SeqAIJ(Y, X, nnz);
2994:     MatSeqAIJSetPreallocation(B, 0, nnz);
2995:     MatAXPY_BasicWithPreallocation(B, Y, a, X, str);
2996:     MatHeaderMerge(Y, &B);
2997:     MatSeqAIJCheckInode(Y);
2998:     PetscFree(nnz);
2999:   }
3000:   return 0;
3001: }

3003: PETSC_INTERN PetscErrorCode MatConjugate_SeqAIJ(Mat mat)
3004: {
3005: #if defined(PETSC_USE_COMPLEX)
3006:   Mat_SeqAIJ  *aij = (Mat_SeqAIJ *)mat->data;
3007:   PetscInt     i, nz;
3008:   PetscScalar *a;

3010:   nz = aij->nz;
3011:   MatSeqAIJGetArray(mat, &a);
3012:   for (i = 0; i < nz; i++) a[i] = PetscConj(a[i]);
3013:   MatSeqAIJRestoreArray(mat, &a);
3014: #else
3015: #endif
3016:   return 0;
3017: }

3019: PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A, Vec v, PetscInt idx[])
3020: {
3021:   Mat_SeqAIJ      *a = (Mat_SeqAIJ *)A->data;
3022:   PetscInt         i, j, m = A->rmap->n, *ai, *aj, ncols, n;
3023:   PetscReal        atmp;
3024:   PetscScalar     *x;
3025:   const MatScalar *aa, *av;

3028:   MatSeqAIJGetArrayRead(A, &av);
3029:   aa = av;
3030:   ai = a->i;
3031:   aj = a->j;

3033:   VecSet(v, 0.0);
3034:   VecGetArrayWrite(v, &x);
3035:   VecGetLocalSize(v, &n);
3037:   for (i = 0; i < m; i++) {
3038:     ncols = ai[1] - ai[0];
3039:     ai++;
3040:     for (j = 0; j < ncols; j++) {
3041:       atmp = PetscAbsScalar(*aa);
3042:       if (PetscAbsScalar(x[i]) < atmp) {
3043:         x[i] = atmp;
3044:         if (idx) idx[i] = *aj;
3045:       }
3046:       aa++;
3047:       aj++;
3048:     }
3049:   }
3050:   VecRestoreArrayWrite(v, &x);
3051:   MatSeqAIJRestoreArrayRead(A, &av);
3052:   return 0;
3053: }

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

3063:   MatSeqAIJGetArrayRead(A, &av);
3064:   aa = av;
3065:   ai = a->i;
3066:   aj = a->j;

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

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

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

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

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

3163:   MatSeqAIJGetArrayRead(A, &av);
3164:   aa = av;
3165:   ai = a->i;
3166:   aj = a->j;

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

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

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

3335: static PetscErrorCode MatSetRandom_SeqAIJ(Mat x, PetscRandom rctx)
3336: {
3337:   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)x->data;
3338:   PetscScalar a, *aa;
3339:   PetscInt    m, n, i, j, col;

3341:   if (!x->assembled) {
3342:     MatGetSize(x, &m, &n);
3343:     for (i = 0; i < m; i++) {
3344:       for (j = 0; j < aij->imax[i]; j++) {
3345:         PetscRandomGetValue(rctx, &a);
3346:         col = (PetscInt)(n * PetscRealPart(a));
3347:         MatSetValues(x, 1, &i, 1, &col, &a, ADD_VALUES);
3348:       }
3349:     }
3350:   } else {
3351:     MatSeqAIJGetArrayWrite(x, &aa);
3352:     for (i = 0; i < aij->nz; i++) PetscRandomGetValue(rctx, aa + i);
3353:     MatSeqAIJRestoreArrayWrite(x, &aa);
3354:   }
3355:   MatAssemblyBegin(x, MAT_FINAL_ASSEMBLY);
3356:   MatAssemblyEnd(x, MAT_FINAL_ASSEMBLY);
3357:   return 0;
3358: }

3360: /* Like MatSetRandom_SeqAIJ, but do not set values on columns in range of [low, high) */
3361: PetscErrorCode MatSetRandomSkipColumnRange_SeqAIJ_Private(Mat x, PetscInt low, PetscInt high, PetscRandom rctx)
3362: {
3363:   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)x->data;
3364:   PetscScalar a;
3365:   PetscInt    m, n, i, j, col, nskip;

3367:   nskip = high - low;
3368:   MatGetSize(x, &m, &n);
3369:   n -= nskip; /* shrink number of columns where nonzeros can be set */
3370:   for (i = 0; i < m; i++) {
3371:     for (j = 0; j < aij->imax[i]; j++) {
3372:       PetscRandomGetValue(rctx, &a);
3373:       col = (PetscInt)(n * PetscRealPart(a));
3374:       if (col >= low) col += nskip; /* shift col rightward to skip the hole */
3375:       MatSetValues(x, 1, &i, 1, &col, &a, ADD_VALUES);
3376:     }
3377:   }
3378:   MatAssemblyBegin(x, MAT_FINAL_ASSEMBLY);
3379:   MatAssemblyEnd(x, MAT_FINAL_ASSEMBLY);
3380:   return 0;
3381: }

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

3536: PetscErrorCode MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat, PetscInt *indices)
3537: {
3538:   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
3539:   PetscInt    i, nz, n;

3541:   nz = aij->maxnz;
3542:   n  = mat->rmap->n;
3543:   for (i = 0; i < nz; i++) aij->j[i] = indices[i];
3544:   aij->nz = nz;
3545:   for (i = 0; i < n; i++) aij->ilen[i] = aij->imax[i];
3546:   return 0;
3547: }

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

3563:   /* use a table */
3564:   PetscTableCreate(mat->rmap->n, mat->cmap->N + 1, &gid1_lid1);
3565:   ec = 0;
3566:   for (i = 0; i < nz; i++) {
3567:     PetscInt data, gid1 = jj[i] + 1;
3568:     PetscTableFind(gid1_lid1, gid1, &data);
3569:     if (!data) {
3570:       /* one based table */
3571:       PetscTableAdd(gid1_lid1, gid1, ++ec, INSERT_VALUES);
3572:     }
3573:   }
3574:   /* form array of columns we need */
3575:   PetscMalloc1(ec, &garray);
3576:   PetscTableGetHeadPosition(gid1_lid1, &tpos);
3577:   while (tpos) {
3578:     PetscTableGetNext(gid1_lid1, &tpos, &gid, &lid);
3579:     gid--;
3580:     lid--;
3581:     garray[lid] = gid;
3582:   }
3583:   PetscSortInt(ec, garray); /* sort, and rebuild */
3584:   PetscTableRemoveAll(gid1_lid1);
3585:   for (i = 0; i < ec; i++) PetscTableAdd(gid1_lid1, garray[i] + 1, i + 1, INSERT_VALUES);
3586:   /* compact out the extra columns in B */
3587:   for (i = 0; i < nz; i++) {
3588:     PetscInt gid1 = jj[i] + 1;
3589:     PetscTableFind(gid1_lid1, gid1, &lid);
3590:     lid--;
3591:     jj[i] = lid;
3592:   }
3593:   PetscLayoutDestroy(&mat->cmap);
3594:   PetscTableDestroy(&gid1_lid1);
3595:   PetscLayoutCreateFromSizes(PetscObjectComm((PetscObject)mat), ec, ec, 1, &mat->cmap);
3596:   ISLocalToGlobalMappingCreate(PETSC_COMM_SELF, mat->cmap->bs, mat->cmap->n, garray, PETSC_OWN_POINTER, mapping);
3597:   ISLocalToGlobalMappingSetType(*mapping, ISLOCALTOGLOBALMAPPINGHASH);
3598:   return 0;
3599: }

3601: /*@
3602:     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
3603:        in the matrix.

3605:   Input Parameters:
3606: +  mat - the `MATSEQAIJ` matrix
3607: -  indices - the column indices

3609:   Level: advanced

3611:   Notes:
3612:     This can be called if you have precomputed the nonzero structure of the
3613:   matrix and want to provide it to the matrix object to improve the performance
3614:   of the `MatSetValues()` operation.

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

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

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

3623: @*/
3624: PetscErrorCode MatSeqAIJSetColumnIndices(Mat mat, PetscInt *indices)
3625: {
3628:   PetscUseMethod(mat, "MatSeqAIJSetColumnIndices_C", (Mat, PetscInt *), (mat, indices));
3629:   return 0;
3630: }

3632: /* ----------------------------------------------------------------------------------------*/

3634: PetscErrorCode MatStoreValues_SeqAIJ(Mat mat)
3635: {
3636:   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
3637:   size_t      nz  = aij->i[mat->rmap->n];


3641:   /* allocate space for values if not already there */
3642:   if (!aij->saved_values) { PetscMalloc1(nz + 1, &aij->saved_values); }

3644:   /* copy values over */
3645:   PetscArraycpy(aij->saved_values, aij->a, nz);
3646:   return 0;
3647: }

3649: /*@
3650:     MatStoreValues - Stashes a copy of the matrix values; this allows, for
3651:        example, reuse of the linear part of a Jacobian, while recomputing the
3652:        nonlinear portion.

3654:    Logically Collect

3656:   Input Parameters:
3657: .  mat - the matrix (currently only `MATAIJ` matrices support this option)

3659:   Level: advanced

3661:   Common Usage, with `SNESSolve()`:
3662: $    Create Jacobian matrix
3663: $    Set linear terms into matrix
3664: $    Apply boundary conditions to matrix, at this time matrix must have
3665: $      final nonzero structure (i.e. setting the nonlinear terms and applying
3666: $      boundary conditions again will not change the nonzero structure
3667: $    MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3668: $    MatStoreValues(mat);
3669: $    Call SNESSetJacobian() with matrix
3670: $    In your Jacobian routine
3671: $      MatRetrieveValues(mat);
3672: $      Set nonlinear terms in matrix

3674:   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
3675: $    // build linear portion of Jacobian
3676: $    MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3677: $    MatStoreValues(mat);
3678: $    loop over nonlinear iterations
3679: $       MatRetrieveValues(mat);
3680: $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
3681: $       // call MatAssemblyBegin/End() on matrix
3682: $       Solve linear system with Jacobian
3683: $    endloop

3685:   Notes:
3686:     Matrix must already be assembled before calling this routine
3687:     Must set the matrix option `MatSetOption`(mat,`MAT_NEW_NONZERO_LOCATIONS`,`PETSC_FALSE`); before
3688:     calling this routine.

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

3693: .seealso: `MatRetrieveValues()`
3694: @*/
3695: PetscErrorCode MatStoreValues(Mat mat)
3696: {
3700:   PetscUseMethod(mat, "MatStoreValues_C", (Mat), (mat));
3701:   return 0;
3702: }

3704: PetscErrorCode MatRetrieveValues_SeqAIJ(Mat mat)
3705: {
3706:   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;
3707:   PetscInt    nz  = aij->i[mat->rmap->n];

3711:   /* copy values over */
3712:   PetscArraycpy(aij->a, aij->saved_values, nz);
3713:   return 0;
3714: }

3716: /*@
3717:     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
3718:        example, reuse of the linear part of a Jacobian, while recomputing the
3719:        nonlinear portion.

3721:    Logically Collect

3723:   Input Parameters:
3724: .  mat - the matrix (currently only `MATAIJ` matrices support this option)

3726:   Level: advanced

3728: .seealso: `MatStoreValues()`
3729: @*/
3730: PetscErrorCode MatRetrieveValues(Mat mat)
3731: {
3735:   PetscUseMethod(mat, "MatRetrieveValues_C", (Mat), (mat));
3736:   return 0;
3737: }

3739: /* --------------------------------------------------------------------------------*/
3740: /*@C
3741:    MatCreateSeqAIJ - Creates a sparse matrix in `MATSEQAIJ` (compressed row) format
3742:    (the default parallel PETSc format).  For good matrix assembly performance
3743:    the user should preallocate the matrix storage by setting the parameter nz
3744:    (or the array nnz).  By setting these parameters accurately, performance
3745:    during matrix assembly can be increased by more than a factor of 50.

3747:    Collective

3749:    Input Parameters:
3750: +  comm - MPI communicator, set to `PETSC_COMM_SELF`
3751: .  m - number of rows
3752: .  n - number of columns
3753: .  nz - number of nonzeros per row (same for all rows)
3754: -  nnz - array containing the number of nonzeros in the various rows
3755:          (possibly different for each row) or NULL

3757:    Output Parameter:
3758: .  A - the matrix

3760:    It is recommended that one use the `MatCreate()`, `MatSetType()` and/or `MatSetFromOptions()`,
3761:    MatXXXXSetPreallocation() paradigm instead of this routine directly.
3762:    [MatXXXXSetPreallocation() is, for example, `MatSeqAIJSetPreallocation()`]

3764:    Notes:
3765:    If nnz is given then nz is ignored

3767:    The AIJ format, also called
3768:    compressed row storage, is fully compatible with standard Fortran 77
3769:    storage.  That is, the stored row and column indices can begin at
3770:    either one (as in Fortran) or zero.  See the users' manual for details.

3772:    Specify the preallocated storage with either nz or nnz (not both).
3773:    Set nz = `PETSC_DEFAULT` and nnz = NULL for PETSc to control dynamic memory
3774:    allocation.  For large problems you MUST preallocate memory or you
3775:    will get TERRIBLE performance, see the users' manual chapter on matrices.

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

3782:    Options Database Keys:
3783: +  -mat_no_inode  - Do not use inodes
3784: -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)

3786:    Level: intermediate

3788: .seealso: [Sparse Matrix Creation](sec_matsparse), `MatCreate()`, `MatCreateAIJ()`, `MatSetValues()`, `MatSeqAIJSetColumnIndices()`, `MatCreateSeqAIJWithArrays()`
3789: @*/
3790: PetscErrorCode MatCreateSeqAIJ(MPI_Comm comm, PetscInt m, PetscInt n, PetscInt nz, const PetscInt nnz[], Mat *A)
3791: {
3792:   MatCreate(comm, A);
3793:   MatSetSizes(*A, m, n, m, n);
3794:   MatSetType(*A, MATSEQAIJ);
3795:   MatSeqAIJSetPreallocation_SeqAIJ(*A, nz, nnz);
3796:   return 0;
3797: }

3799: /*@C
3800:    MatSeqAIJSetPreallocation - For good matrix assembly performance
3801:    the user should preallocate the matrix storage by setting the parameter nz
3802:    (or the array nnz).  By setting these parameters accurately, performance
3803:    during matrix assembly can be increased by more than a factor of 50.

3805:    Collective

3807:    Input Parameters:
3808: +  B - The matrix
3809: .  nz - number of nonzeros per row (same for all rows)
3810: -  nnz - array containing the number of nonzeros in the various rows
3811:          (possibly different for each row) or NULL

3813:    Notes:
3814:      If nnz is given then nz is ignored

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

3821:    Specify the preallocated storage with either nz or nnz (not both).
3822:    Set nz = `PETSC_DEFAULT` and nnz = NULL for PETSc to control dynamic memory
3823:    allocation.  For large problems you MUST preallocate memory or you
3824:    will get TERRIBLE performance, see the users' manual chapter on matrices.

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

3831:    Developer Notes:
3832:    Use nz of `MAT_SKIP_ALLOCATION` to not allocate any space for the matrix
3833:    entries or columns indices

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

3840:    Options Database Keys:
3841: +  -mat_no_inode  - Do not use inodes
3842: -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)

3844:    Level: intermediate

3846: .seealso: `MatCreate()`, `MatCreateAIJ()`, `MatSetValues()`, `MatSeqAIJSetColumnIndices()`, `MatCreateSeqAIJWithArrays()`, `MatGetInfo()`,
3847:           `MatSeqAIJSetTotalPreallocation()`
3848: @*/
3849: PetscErrorCode MatSeqAIJSetPreallocation(Mat B, PetscInt nz, const PetscInt nnz[])
3850: {
3853:   PetscTryMethod(B, "MatSeqAIJSetPreallocation_C", (Mat, PetscInt, const PetscInt[]), (B, nz, nnz));
3854:   return 0;
3855: }

3857: PetscErrorCode MatSeqAIJSetPreallocation_SeqAIJ(Mat B, PetscInt nz, const PetscInt *nnz)
3858: {
3859:   Mat_SeqAIJ *b;
3860:   PetscBool   skipallocation = PETSC_FALSE, realalloc = PETSC_FALSE;
3861:   PetscInt    i;

3863:   if (nz >= 0 || nnz) realalloc = PETSC_TRUE;
3864:   if (nz == MAT_SKIP_ALLOCATION) {
3865:     skipallocation = PETSC_TRUE;
3866:     nz             = 0;
3867:   }
3868:   PetscLayoutSetUp(B->rmap);
3869:   PetscLayoutSetUp(B->cmap);

3871:   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
3873:   if (PetscUnlikelyDebug(nnz)) {
3874:     for (i = 0; i < B->rmap->n; i++) {
3877:     }
3878:   }

3880:   B->preallocated = PETSC_TRUE;

3882:   b = (Mat_SeqAIJ *)B->data;

3884:   if (!skipallocation) {
3885:     if (!b->imax) { PetscMalloc1(B->rmap->n, &b->imax); }
3886:     if (!b->ilen) {
3887:       /* b->ilen will count nonzeros in each row so far. */
3888:       PetscCalloc1(B->rmap->n, &b->ilen);
3889:     } else {
3890:       PetscMemzero(b->ilen, B->rmap->n * sizeof(PetscInt));
3891:     }
3892:     if (!b->ipre) { PetscMalloc1(B->rmap->n, &b->ipre); }
3893:     if (!nnz) {
3894:       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
3895:       else if (nz < 0) nz = 1;
3896:       nz = PetscMin(nz, B->cmap->n);
3897:       for (i = 0; i < B->rmap->n; i++) b->imax[i] = nz;
3898:       nz = nz * B->rmap->n;
3899:     } else {
3900:       PetscInt64 nz64 = 0;
3901:       for (i = 0; i < B->rmap->n; i++) {
3902:         b->imax[i] = nnz[i];
3903:         nz64 += nnz[i];
3904:       }
3905:       PetscIntCast(nz64, &nz);
3906:     }

3908:     /* allocate the matrix space */
3909:     /* FIXME: should B's old memory be unlogged? */
3910:     MatSeqXAIJFreeAIJ(B, &b->a, &b->j, &b->i);
3911:     if (B->structure_only) {
3912:       PetscMalloc1(nz, &b->j);
3913:       PetscMalloc1(B->rmap->n + 1, &b->i);
3914:     } else {
3915:       PetscMalloc3(nz, &b->a, nz, &b->j, B->rmap->n + 1, &b->i);
3916:     }
3917:     b->i[0] = 0;
3918:     for (i = 1; i < B->rmap->n + 1; i++) b->i[i] = b->i[i - 1] + b->imax[i - 1];
3919:     if (B->structure_only) {
3920:       b->singlemalloc = PETSC_FALSE;
3921:       b->free_a       = PETSC_FALSE;
3922:     } else {
3923:       b->singlemalloc = PETSC_TRUE;
3924:       b->free_a       = PETSC_TRUE;
3925:     }
3926:     b->free_ij = PETSC_TRUE;
3927:   } else {
3928:     b->free_a  = PETSC_FALSE;
3929:     b->free_ij = PETSC_FALSE;
3930:   }

3932:   if (b->ipre && nnz != b->ipre && b->imax) {
3933:     /* reserve user-requested sparsity */
3934:     PetscArraycpy(b->ipre, b->imax, B->rmap->n);
3935:   }

3937:   b->nz               = 0;
3938:   b->maxnz            = nz;
3939:   B->info.nz_unneeded = (double)b->maxnz;
3940:   if (realalloc) MatSetOption(B, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_TRUE);
3941:   B->was_assembled = PETSC_FALSE;
3942:   B->assembled     = PETSC_FALSE;
3943:   /* We simply deem preallocation has changed nonzero state. Updating the state
3944:      will give clients (like AIJKokkos) a chance to know something has happened.
3945:   */
3946:   B->nonzerostate++;
3947:   return 0;
3948: }

3950: PetscErrorCode MatResetPreallocation_SeqAIJ(Mat A)
3951: {
3952:   Mat_SeqAIJ *a;
3953:   PetscInt    i;


3957:   /* Check local size. If zero, then return */
3958:   if (!A->rmap->n) return 0;

3960:   a = (Mat_SeqAIJ *)A->data;
3961:   /* if no saved info, we error out */


3966:   PetscArraycpy(a->imax, a->ipre, A->rmap->n);
3967:   PetscArrayzero(a->ilen, A->rmap->n);
3968:   a->i[0] = 0;
3969:   for (i = 1; i < A->rmap->n + 1; i++) a->i[i] = a->i[i - 1] + a->imax[i - 1];
3970:   A->preallocated     = PETSC_TRUE;
3971:   a->nz               = 0;
3972:   a->maxnz            = a->i[A->rmap->n];
3973:   A->info.nz_unneeded = (double)a->maxnz;
3974:   A->was_assembled    = PETSC_FALSE;
3975:   A->assembled        = PETSC_FALSE;
3976:   return 0;
3977: }

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

3982:    Input Parameters:
3983: +  B - the matrix
3984: .  i - the indices into j for the start of each row (starts with zero)
3985: .  j - the column indices for each row (starts with zero) these must be sorted for each row
3986: -  v - optional values in the matrix

3988:    Level: developer

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

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

3996:     Developer Notes:
3997:       An optimization could be added to the implementation where it checks if the i, and j are identical to the current i and j and
3998:       then just copies the v values directly with `PetscMemcpy()`.

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

4002: .seealso: `MatCreate()`, `MatCreateSeqAIJ()`, `MatSetValues()`, `MatSeqAIJSetPreallocation()`, `MatCreateSeqAIJ()`, `MATSEQAIJ`, `MatResetPreallocation()`
4003: @*/
4004: PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B, const PetscInt i[], const PetscInt j[], const PetscScalar v[])
4005: {
4008:   PetscTryMethod(B, "MatSeqAIJSetPreallocationCSR_C", (Mat, const PetscInt[], const PetscInt[], const PetscScalar[]), (B, i, j, v));
4009:   return 0;
4010: }

4012: PetscErrorCode MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B, const PetscInt Ii[], const PetscInt J[], const PetscScalar v[])
4013: {
4014:   PetscInt  i;
4015:   PetscInt  m, n;
4016:   PetscInt  nz;
4017:   PetscInt *nnz;


4021:   PetscLayoutSetUp(B->rmap);
4022:   PetscLayoutSetUp(B->cmap);

4024:   MatGetSize(B, &m, &n);
4025:   PetscMalloc1(m + 1, &nnz);
4026:   for (i = 0; i < m; i++) {
4027:     nz = Ii[i + 1] - Ii[i];
4029:     nnz[i] = nz;
4030:   }
4031:   MatSeqAIJSetPreallocation(B, 0, nnz);
4032:   PetscFree(nnz);

4034:   for (i = 0; i < m; i++) MatSetValues_SeqAIJ(B, 1, &i, Ii[i + 1] - Ii[i], J + Ii[i], v ? v + Ii[i] : NULL, INSERT_VALUES);

4036:   MatAssemblyBegin(B, MAT_FINAL_ASSEMBLY);
4037:   MatAssemblyEnd(B, MAT_FINAL_ASSEMBLY);

4039:   MatSetOption(B, MAT_NEW_NONZERO_LOCATION_ERR, PETSC_TRUE);
4040:   return 0;
4041: }

4043: /*@
4044:    MatSeqAIJKron - Computes C, the Kronecker product of A and B.

4046:    Input Parameters:
4047: +  A - left-hand side matrix
4048: .  B - right-hand side matrix
4049: -  reuse - either `MAT_INITIAL_MATRIX` or `MAT_REUSE_MATRIX`

4051:    Output Parameter:
4052: .  C - Kronecker product of A and B

4054:    Level: intermediate

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

4059: .seealso: `MatCreateSeqAIJ()`, `MATSEQAIJ`, `MATKAIJ`, `MatReuse`
4060: @*/
4061: PetscErrorCode MatSeqAIJKron(Mat A, Mat B, MatReuse reuse, Mat *C)
4062: {
4068:   if (reuse == MAT_REUSE_MATRIX) {
4071:   }
4072:   PetscTryMethod(A, "MatSeqAIJKron_C", (Mat, Mat, MatReuse, Mat *), (A, B, reuse, C));
4073:   return 0;
4074: }

4076: PetscErrorCode MatSeqAIJKron_SeqAIJ(Mat A, Mat B, MatReuse reuse, Mat *C)
4077: {
4078:   Mat                newmat;
4079:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data;
4080:   Mat_SeqAIJ        *b = (Mat_SeqAIJ *)B->data;
4081:   PetscScalar       *v;
4082:   const PetscScalar *aa, *ba;
4083:   PetscInt          *i, *j, m, n, p, q, nnz = 0, am = A->rmap->n, bm = B->rmap->n, an = A->cmap->n, bn = B->cmap->n;
4084:   PetscBool          flg;

4090:   PetscObjectTypeCompare((PetscObject)B, MATSEQAIJ, &flg);
4093:   if (reuse == MAT_INITIAL_MATRIX) {
4094:     PetscMalloc2(am * bm + 1, &i, a->i[am] * b->i[bm], &j);
4095:     MatCreate(PETSC_COMM_SELF, &newmat);
4096:     MatSetSizes(newmat, am * bm, an * bn, am * bm, an * bn);
4097:     MatSetType(newmat, MATAIJ);
4098:     i[0] = 0;
4099:     for (m = 0; m < am; ++m) {
4100:       for (p = 0; p < bm; ++p) {
4101:         i[m * bm + p + 1] = i[m * bm + p] + (a->i[m + 1] - a->i[m]) * (b->i[p + 1] - b->i[p]);
4102:         for (n = a->i[m]; n < a->i[m + 1]; ++n) {
4103:           for (q = b->i[p]; q < b->i[p + 1]; ++q) j[nnz++] = a->j[n] * bn + b->j[q];
4104:         }
4105:       }
4106:     }
4107:     MatSeqAIJSetPreallocationCSR(newmat, i, j, NULL);
4108:     *C = newmat;
4109:     PetscFree2(i, j);
4110:     nnz = 0;
4111:   }
4112:   MatSeqAIJGetArray(*C, &v);
4113:   MatSeqAIJGetArrayRead(A, &aa);
4114:   MatSeqAIJGetArrayRead(B, &ba);
4115:   for (m = 0; m < am; ++m) {
4116:     for (p = 0; p < bm; ++p) {
4117:       for (n = a->i[m]; n < a->i[m + 1]; ++n) {
4118:         for (q = b->i[p]; q < b->i[p + 1]; ++q) v[nnz++] = aa[n] * ba[q];
4119:       }
4120:     }
4121:   }
4122:   MatSeqAIJRestoreArray(*C, &v);
4123:   MatSeqAIJRestoreArrayRead(A, &aa);
4124:   MatSeqAIJRestoreArrayRead(B, &ba);
4125:   return 0;
4126: }

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

4131: /*
4132:     Computes (B'*A')' since computing B*A directly is untenable

4134:                n                       p                          p
4135:         [             ]       [             ]         [                 ]
4136:       m [      A      ]  *  n [       B     ]   =   m [         C       ]
4137:         [             ]       [             ]         [                 ]

4139: */
4140: PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A, Mat B, Mat C)
4141: {
4142:   Mat_SeqDense      *sub_a = (Mat_SeqDense *)A->data;
4143:   Mat_SeqAIJ        *sub_b = (Mat_SeqAIJ *)B->data;
4144:   Mat_SeqDense      *sub_c = (Mat_SeqDense *)C->data;
4145:   PetscInt           i, j, n, m, q, p;
4146:   const PetscInt    *ii, *idx;
4147:   const PetscScalar *b, *a, *a_q;
4148:   PetscScalar       *c, *c_q;
4149:   PetscInt           clda = sub_c->lda;
4150:   PetscInt           alda = sub_a->lda;

4152:   m = A->rmap->n;
4153:   n = A->cmap->n;
4154:   p = B->cmap->n;
4155:   a = sub_a->v;
4156:   b = sub_b->a;
4157:   c = sub_c->v;
4158:   if (clda == m) {
4159:     PetscArrayzero(c, m * p);
4160:   } else {
4161:     for (j = 0; j < p; j++)
4162:       for (i = 0; i < m; i++) c[j * clda + i] = 0.0;
4163:   }
4164:   ii  = sub_b->i;
4165:   idx = sub_b->j;
4166:   for (i = 0; i < n; i++) {
4167:     q = ii[i + 1] - ii[i];
4168:     while (q-- > 0) {
4169:       c_q = c + clda * (*idx);
4170:       a_q = a + alda * i;
4171:       PetscKernelAXPY(c_q, *b, a_q, m);
4172:       idx++;
4173:       b++;
4174:     }
4175:   }
4176:   return 0;
4177: }

4179: PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A, Mat B, PetscReal fill, Mat C)
4180: {
4181:   PetscInt  m = A->rmap->n, n = B->cmap->n;
4182:   PetscBool cisdense;

4185:   MatSetSizes(C, m, n, m, n);
4186:   MatSetBlockSizesFromMats(C, A, B);
4187:   PetscObjectTypeCompareAny((PetscObject)C, &cisdense, MATSEQDENSE, MATSEQDENSECUDA, "");
4188:   if (!cisdense) MatSetType(C, MATDENSE);
4189:   MatSetUp(C);

4191:   C->ops->matmultnumeric = MatMatMultNumeric_SeqDense_SeqAIJ;
4192:   return 0;
4193: }

4195: /* ----------------------------------------------------------------*/
4196: /*MC
4197:    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
4198:    based on compressed sparse row format.

4200:    Options Database Keys:
4201: . -mat_type seqaij - sets the matrix type to "seqaij" during a call to MatSetFromOptions()

4203:    Level: beginner

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

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

4213:   Developer Note:
4214:     It would be nice if all matrix formats supported passing NULL in for the numerical values

4216: .seealso: `MatCreateSeqAIJ()`, `MatSetFromOptions()`, `MatSetType()`, `MatCreate()`, `MatType`, `MATSELL`, `MATSEQSELL`, `MATMPISELL`
4217: M*/

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

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

4228:    Options Database Keys:
4229: . -mat_type aij - sets the matrix type to "aij" during a call to `MatSetFromOptions()`

4231:    Note:
4232:    Subclasses include `MATAIJCUSPARSE`, `MATAIJPERM`, `MATAIJSELL`, `MATAIJMKL`, `MATAIJCRL`, and also automatically switches over to use inodes when
4233:    enough exist.

4235:   Level: beginner

4237: .seealso: `MatCreateAIJ()`, `MatCreateSeqAIJ()`, `MATSEQAIJ`, `MATMPIAIJ`, `MATSELL`, `MATSEQSELL`, `MATMPISELL`
4238: M*/

4240: /*MC
4241:    MATAIJCRL - MATAIJCRL = "aijcrl" - A matrix type to be used for sparse matrices.

4243:    This matrix type is identical to `MATSEQAIJCRL` when constructed with a single process communicator,
4244:    and `MATMPIAIJCRL` otherwise.  As a result, for single process communicators,
4245:    `MatSeqAIJSetPreallocation()` is supported, and similarly `MatMPIAIJSetPreallocation()` is supported
4246:    for communicators controlling multiple processes.  It is recommended that you call both of
4247:    the above preallocation routines for simplicity.

4249:    Options Database Keys:
4250: . -mat_type aijcrl - sets the matrix type to "aijcrl" during a call to `MatSetFromOptions()`

4252:   Level: beginner

4254: .seealso: `MatCreateMPIAIJCRL`, `MATSEQAIJCRL`, `MATMPIAIJCRL`, `MATSEQAIJCRL`, `MATMPIAIJCRL`
4255: M*/

4257: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCRL(Mat, MatType, MatReuse, Mat *);
4258: #if defined(PETSC_HAVE_ELEMENTAL)
4259: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_Elemental(Mat, MatType, MatReuse, Mat *);
4260: #endif
4261: #if defined(PETSC_HAVE_SCALAPACK)
4262: PETSC_INTERN PetscErrorCode MatConvert_AIJ_ScaLAPACK(Mat, MatType, MatReuse, Mat *);
4263: #endif
4264: #if defined(PETSC_HAVE_HYPRE)
4265: PETSC_INTERN PetscErrorCode MatConvert_AIJ_HYPRE(Mat A, MatType, MatReuse, Mat *);
4266: #endif

4268: PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_SeqSELL(Mat, MatType, MatReuse, Mat *);
4269: PETSC_INTERN PetscErrorCode MatConvert_XAIJ_IS(Mat, MatType, MatReuse, Mat *);
4270: PETSC_INTERN PetscErrorCode MatProductSetFromOptions_IS_XAIJ(Mat);

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

4275:    Not Collective

4277:    Input Parameter:
4278: .  mat - a `MATSEQAIJ` matrix

4280:    Output Parameter:
4281: .   array - pointer to the data

4283:    Level: intermediate

4285: .seealso: `MatSeqAIJRestoreArray()`, `MatSeqAIJGetArrayF90()`
4286: @*/
4287: PetscErrorCode MatSeqAIJGetArray(Mat A, PetscScalar **array)
4288: {
4289:   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;

4291:   if (aij->ops->getarray) {
4292:     (*aij->ops->getarray)(A, array);
4293:   } else {
4294:     *array = aij->a;
4295:   }
4296:   return 0;
4297: }

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

4302:    Not Collective

4304:    Input Parameters:
4305: +  mat - a `MATSEQAIJ` matrix
4306: -  array - pointer to the data

4308:    Level: intermediate

4310: .seealso: `MatSeqAIJGetArray()`, `MatSeqAIJRestoreArrayF90()`
4311: @*/
4312: PetscErrorCode MatSeqAIJRestoreArray(Mat A, PetscScalar **array)
4313: {
4314:   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;

4316:   if (aij->ops->restorearray) {
4317:     (*aij->ops->restorearray)(A, array);
4318:   } else {
4319:     *array = NULL;
4320:   }
4321:   MatSeqAIJInvalidateDiagonal(A);
4322:   PetscObjectStateIncrease((PetscObject)A);
4323:   return 0;
4324: }

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

4329:    Not Collective

4331:    Input Parameter:
4332: .  mat - a `MATSEQAIJ` matrix

4334:    Output Parameter:
4335: .   array - pointer to the data

4337:    Level: intermediate

4339: .seealso: `MatSeqAIJGetArray()`, `MatSeqAIJRestoreArrayRead()`
4340: @*/
4341: PetscErrorCode MatSeqAIJGetArrayRead(Mat A, const PetscScalar **array)
4342: {
4343:   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;

4345:   if (aij->ops->getarrayread) {
4346:     (*aij->ops->getarrayread)(A, array);
4347:   } else {
4348:     *array = aij->a;
4349:   }
4350:   return 0;
4351: }

4353: /*@C
4354:    MatSeqAIJRestoreArrayRead - restore the read-only access array obtained from `MatSeqAIJGetArrayRead()`

4356:    Not Collective

4358:    Input Parameter:
4359: .  mat - a `MATSEQAIJ` matrix

4361:    Output Parameter:
4362: .   array - pointer to the data

4364:    Level: intermediate

4366: .seealso: `MatSeqAIJGetArray()`, `MatSeqAIJGetArrayRead()`
4367: @*/
4368: PetscErrorCode MatSeqAIJRestoreArrayRead(Mat A, const PetscScalar **array)
4369: {
4370:   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;

4372:   if (aij->ops->restorearrayread) {
4373:     (*aij->ops->restorearrayread)(A, array);
4374:   } else {
4375:     *array = NULL;
4376:   }
4377:   return 0;
4378: }

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

4383:    Not Collective

4385:    Input Parameter:
4386: .  mat - a `MATSEQAIJ` matrix

4388:    Output Parameter:
4389: .   array - pointer to the data

4391:    Level: intermediate

4393: .seealso: `MatSeqAIJGetArray()`, `MatSeqAIJRestoreArrayRead()`
4394: @*/
4395: PetscErrorCode MatSeqAIJGetArrayWrite(Mat A, PetscScalar **array)
4396: {
4397:   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;

4399:   if (aij->ops->getarraywrite) {
4400:     (*aij->ops->getarraywrite)(A, array);
4401:   } else {
4402:     *array = aij->a;
4403:   }
4404:   MatSeqAIJInvalidateDiagonal(A);
4405:   PetscObjectStateIncrease((PetscObject)A);
4406:   return 0;
4407: }

4409: /*@C
4410:    MatSeqAIJRestoreArrayWrite - restore the read-only access array obtained from MatSeqAIJGetArrayRead

4412:    Not Collective

4414:    Input Parameter:
4415: .  mat - a MATSEQAIJ matrix

4417:    Output Parameter:
4418: .   array - pointer to the data

4420:    Level: intermediate

4422: .seealso: `MatSeqAIJGetArray()`, `MatSeqAIJGetArrayRead()`
4423: @*/
4424: PetscErrorCode MatSeqAIJRestoreArrayWrite(Mat A, PetscScalar **array)
4425: {
4426:   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;

4428:   if (aij->ops->restorearraywrite) {
4429:     (*aij->ops->restorearraywrite)(A, array);
4430:   } else {
4431:     *array = NULL;
4432:   }
4433:   return 0;
4434: }

4436: /*@C
4437:    MatSeqAIJGetCSRAndMemType - Get the CSR arrays and the memory type of the `MATSEQAIJ` matrix

4439:    Not Collective

4441:    Input Parameter:
4442: .  mat - a matrix of type `MATSEQAIJ` or its subclasses

4444:    Output Parameters:
4445: +  i - row map array of the matrix
4446: .  j - column index array of the matrix
4447: .  a - data array of the matrix
4448: -  memtype - memory type of the arrays

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

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

4457:    Level: Developer

4459: .seealso: `MatSeqAIJGetArray()`, `MatSeqAIJGetArrayRead()`
4460: @*/
4461: PetscErrorCode MatSeqAIJGetCSRAndMemType(Mat mat, const PetscInt **i, const PetscInt **j, PetscScalar **a, PetscMemType *mtype)
4462: {
4463:   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)mat->data;

4466:   if (aij->ops->getcsrandmemtype) {
4467:     (*aij->ops->getcsrandmemtype)(mat, i, j, a, mtype);
4468:   } else {
4469:     if (i) *i = aij->i;
4470:     if (j) *j = aij->j;
4471:     if (a) *a = aij->a;
4472:     if (mtype) *mtype = PETSC_MEMTYPE_HOST;
4473:   }
4474:   return 0;
4475: }

4477: /*@C
4478:    MatSeqAIJGetMaxRowNonzeros - returns the maximum number of nonzeros in any row

4480:    Not Collective

4482:    Input Parameter:
4483: .  mat - a `MATSEQAIJ` matrix

4485:    Output Parameter:
4486: .   nz - the maximum number of nonzeros in any row

4488:    Level: intermediate

4490: .seealso: `MatSeqAIJRestoreArray()`, `MatSeqAIJGetArrayF90()`
4491: @*/
4492: PetscErrorCode MatSeqAIJGetMaxRowNonzeros(Mat A, PetscInt *nz)
4493: {
4494:   Mat_SeqAIJ *aij = (Mat_SeqAIJ *)A->data;

4496:   *nz = aij->rmax;
4497:   return 0;
4498: }

4500: PetscErrorCode MatSetPreallocationCOO_SeqAIJ(Mat mat, PetscCount coo_n, PetscInt coo_i[], PetscInt coo_j[])
4501: {
4502:   MPI_Comm     comm;
4503:   PetscInt    *i, *j;
4504:   PetscInt     M, N, row;
4505:   PetscCount   k, p, q, nneg, nnz, start, end; /* Index the coo array, so use PetscCount as their type */
4506:   PetscInt    *Ai;                             /* Change to PetscCount once we use it for row pointers */
4507:   PetscInt    *Aj;
4508:   PetscScalar *Aa;
4509:   Mat_SeqAIJ  *seqaij = (Mat_SeqAIJ *)(mat->data);
4510:   MatType      rtype;
4511:   PetscCount  *perm, *jmap;

4513:   MatResetPreallocationCOO_SeqAIJ(mat);
4514:   PetscObjectGetComm((PetscObject)mat, &comm);
4515:   MatGetSize(mat, &M, &N);
4516:   i = coo_i;
4517:   j = coo_j;
4518:   PetscMalloc1(coo_n, &perm);
4519:   for (k = 0; k < coo_n; k++) { /* Ignore entries with negative row or col indices */
4520:     if (j[k] < 0) i[k] = -1;
4521:     perm[k] = k;
4522:   }

4524:   /* Sort by row */
4525:   PetscSortIntWithIntCountArrayPair(coo_n, i, j, perm);
4526:   for (k = 0; k < coo_n; k++) {
4527:     if (i[k] >= 0) break;
4528:   } /* Advance k to the first row with a non-negative index */
4529:   nneg = k;
4530:   PetscMalloc1(coo_n - nneg + 1, &jmap); /* +1 to make a CSR-like data structure. jmap[i] originally is the number of repeats for i-th nonzero */
4531:   nnz = 0;                                          /* Total number of unique nonzeros to be counted */
4532:   jmap++;                                           /* Inc jmap by 1 for convenience */

4534:   PetscCalloc1(M + 1, &Ai);        /* CSR of A */
4535:   PetscMalloc1(coo_n - nneg, &Aj); /* We have at most coo_n-nneg unique nonzeros */

4537:   /* In each row, sort by column, then unique column indices to get row length */
4538:   Ai++;  /* Inc by 1 for convenience */
4539:   q = 0; /* q-th unique nonzero, with q starting from 0 */
4540:   while (k < coo_n) {
4541:     row   = i[k];
4542:     start = k; /* [start,end) indices for this row */
4543:     while (k < coo_n && i[k] == row) k++;
4544:     end = k;
4545:     PetscSortIntWithCountArray(end - start, j + start, perm + start);
4546:     /* Find number of unique col entries in this row */
4547:     Aj[q]   = j[start]; /* Log the first nonzero in this row */
4548:     jmap[q] = 1;        /* Number of repeats of this nozero entry */
4549:     Ai[row] = 1;
4550:     nnz++;

4552:     for (p = start + 1; p < end; p++) { /* Scan remaining nonzero in this row */
4553:       if (j[p] != j[p - 1]) {           /* Meet a new nonzero */
4554:         q++;
4555:         jmap[q] = 1;
4556:         Aj[q]   = j[p];
4557:         Ai[row]++;
4558:         nnz++;
4559:       } else {
4560:         jmap[q]++;
4561:       }
4562:     }
4563:     q++; /* Move to next row and thus next unique nonzero */
4564:   }

4566:   Ai--; /* Back to the beginning of Ai[] */
4567:   for (k = 0; k < M; k++) Ai[k + 1] += Ai[k];
4568:   jmap--; /* Back to the beginning of jmap[] */
4569:   jmap[0] = 0;
4570:   for (k = 0; k < nnz; k++) jmap[k + 1] += jmap[k];
4571:   if (nnz < coo_n - nneg) { /* Realloc with actual number of unique nonzeros */
4572:     PetscCount *jmap_new;
4573:     PetscInt   *Aj_new;

4575:     PetscMalloc1(nnz + 1, &jmap_new);
4576:     PetscArraycpy(jmap_new, jmap, nnz + 1);
4577:     PetscFree(jmap);
4578:     jmap = jmap_new;

4580:     PetscMalloc1(nnz, &Aj_new);
4581:     PetscArraycpy(Aj_new, Aj, nnz);
4582:     PetscFree(Aj);
4583:     Aj = Aj_new;
4584:   }

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

4589:     PetscMalloc1(coo_n - nneg, &perm_new);
4590:     PetscArraycpy(perm_new, perm + nneg, coo_n - nneg);
4591:     PetscFree(perm);
4592:     perm = perm_new;
4593:   }

4595:   MatGetRootType_Private(mat, &rtype);
4596:   PetscCalloc1(nnz, &Aa); /* Zero the matrix */
4597:   MatSetSeqAIJWithArrays_private(PETSC_COMM_SELF, M, N, Ai, Aj, Aa, rtype, mat);

4599:   seqaij->singlemalloc = PETSC_FALSE;            /* Ai, Aj and Aa are not allocated in one big malloc */
4600:   seqaij->free_a = seqaij->free_ij = PETSC_TRUE; /* Let newmat own Ai, Aj and Aa */
4601:   /* Record COO fields */
4602:   seqaij->coo_n = coo_n;
4603:   seqaij->Atot  = coo_n - nneg; /* Annz is seqaij->nz, so no need to record that again */
4604:   seqaij->jmap  = jmap;         /* of length nnz+1 */
4605:   seqaij->perm  = perm;
4606:   return 0;
4607: }

4609: static PetscErrorCode MatSetValuesCOO_SeqAIJ(Mat A, const PetscScalar v[], InsertMode imode)
4610: {
4611:   Mat_SeqAIJ  *aseq = (Mat_SeqAIJ *)A->data;
4612:   PetscCount   i, j, Annz = aseq->nz;
4613:   PetscCount  *perm = aseq->perm, *jmap = aseq->jmap;
4614:   PetscScalar *Aa;

4616:   MatSeqAIJGetArray(A, &Aa);
4617:   for (i = 0; i < Annz; i++) {
4618:     PetscScalar sum = 0.0;
4619:     for (j = jmap[i]; j < jmap[i + 1]; j++) sum += v[perm[j]];
4620:     Aa[i] = (imode == INSERT_VALUES ? 0.0 : Aa[i]) + sum;
4621:   }
4622:   MatSeqAIJRestoreArray(A, &Aa);
4623:   return 0;
4624: }

4626: #if defined(PETSC_HAVE_CUDA)
4627: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCUSPARSE(Mat, MatType, MatReuse, Mat *);
4628: #endif
4629: #if defined(PETSC_HAVE_KOKKOS_KERNELS)
4630: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJKokkos(Mat, MatType, MatReuse, Mat *);
4631: #endif

4633: PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJ(Mat B)
4634: {
4635:   Mat_SeqAIJ *b;
4636:   PetscMPIInt size;

4638:   MPI_Comm_size(PetscObjectComm((PetscObject)B), &size);

4641:   PetscNew(&b);

4643:   B->data = (void *)b;

4645:   PetscMemcpy(B->ops, &MatOps_Values, sizeof(struct _MatOps));
4646:   if (B->sortedfull) B->ops->setvalues = MatSetValues_SeqAIJ_SortedFull;

4648:   b->row                = NULL;
4649:   b->col                = NULL;
4650:   b->icol               = NULL;
4651:   b->reallocs           = 0;
4652:   b->ignorezeroentries  = PETSC_FALSE;
4653:   b->roworiented        = PETSC_TRUE;
4654:   b->nonew              = 0;
4655:   b->diag               = NULL;
4656:   b->solve_work         = NULL;
4657:   B->spptr              = NULL;
4658:   b->saved_values       = NULL;
4659:   b->idiag              = NULL;
4660:   b->mdiag              = NULL;
4661:   b->ssor_work          = NULL;
4662:   b->omega              = 1.0;
4663:   b->fshift             = 0.0;
4664:   b->idiagvalid         = PETSC_FALSE;
4665:   b->ibdiagvalid        = PETSC_FALSE;
4666:   b->keepnonzeropattern = PETSC_FALSE;

4668:   PetscObjectChangeTypeName((PetscObject)B, MATSEQAIJ);
4669: #if defined(PETSC_HAVE_MATLAB)
4670:   PetscObjectComposeFunction((PetscObject)B, "PetscMatlabEnginePut_C", MatlabEnginePut_SeqAIJ);
4671:   PetscObjectComposeFunction((PetscObject)B, "PetscMatlabEngineGet_C", MatlabEngineGet_SeqAIJ);
4672: #endif
4673:   PetscObjectComposeFunction((PetscObject)B, "MatSeqAIJSetColumnIndices_C", MatSeqAIJSetColumnIndices_SeqAIJ);
4674:   PetscObjectComposeFunction((PetscObject)B, "MatStoreValues_C", MatStoreValues_SeqAIJ);
4675:   PetscObjectComposeFunction((PetscObject)B, "MatRetrieveValues_C", MatRetrieveValues_SeqAIJ);
4676:   PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqsbaij_C", MatConvert_SeqAIJ_SeqSBAIJ);
4677:   PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqbaij_C", MatConvert_SeqAIJ_SeqBAIJ);
4678:   PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqaijperm_C", MatConvert_SeqAIJ_SeqAIJPERM);
4679:   PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqaijsell_C", MatConvert_SeqAIJ_SeqAIJSELL);
4680: #if defined(PETSC_HAVE_MKL_SPARSE)
4681:   PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqaijmkl_C", MatConvert_SeqAIJ_SeqAIJMKL);
4682: #endif
4683: #if defined(PETSC_HAVE_CUDA)
4684:   PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqaijcusparse_C", MatConvert_SeqAIJ_SeqAIJCUSPARSE);
4685:   PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqaijcusparse_seqaij_C", MatProductSetFromOptions_SeqAIJ);
4686:   PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqaij_seqaijcusparse_C", MatProductSetFromOptions_SeqAIJ);
4687: #endif
4688: #if defined(PETSC_HAVE_KOKKOS_KERNELS)
4689:   PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqaijkokkos_C", MatConvert_SeqAIJ_SeqAIJKokkos);
4690: #endif
4691:   PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqaijcrl_C", MatConvert_SeqAIJ_SeqAIJCRL);
4692: #if defined(PETSC_HAVE_ELEMENTAL)
4693:   PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_elemental_C", MatConvert_SeqAIJ_Elemental);
4694: #endif
4695: #if defined(PETSC_HAVE_SCALAPACK)
4696:   PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_scalapack_C", MatConvert_AIJ_ScaLAPACK);
4697: #endif
4698: #if defined(PETSC_HAVE_HYPRE)
4699:   PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_hypre_C", MatConvert_AIJ_HYPRE);
4700:   PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_transpose_seqaij_seqaij_C", MatProductSetFromOptions_Transpose_AIJ_AIJ);
4701: #endif
4702:   PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqdense_C", MatConvert_SeqAIJ_SeqDense);
4703:   PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_seqsell_C", MatConvert_SeqAIJ_SeqSELL);
4704:   PetscObjectComposeFunction((PetscObject)B, "MatConvert_seqaij_is_C", MatConvert_XAIJ_IS);
4705:   PetscObjectComposeFunction((PetscObject)B, "MatIsTranspose_C", MatIsTranspose_SeqAIJ);
4706:   PetscObjectComposeFunction((PetscObject)B, "MatIsHermitianTranspose_C", MatIsTranspose_SeqAIJ);
4707:   PetscObjectComposeFunction((PetscObject)B, "MatSeqAIJSetPreallocation_C", MatSeqAIJSetPreallocation_SeqAIJ);
4708:   PetscObjectComposeFunction((PetscObject)B, "MatResetPreallocation_C", MatResetPreallocation_SeqAIJ);
4709:   PetscObjectComposeFunction((PetscObject)B, "MatSeqAIJSetPreallocationCSR_C", MatSeqAIJSetPreallocationCSR_SeqAIJ);
4710:   PetscObjectComposeFunction((PetscObject)B, "MatReorderForNonzeroDiagonal_C", MatReorderForNonzeroDiagonal_SeqAIJ);
4711:   PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_is_seqaij_C", MatProductSetFromOptions_IS_XAIJ);
4712:   PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqdense_seqaij_C", MatProductSetFromOptions_SeqDense_SeqAIJ);
4713:   PetscObjectComposeFunction((PetscObject)B, "MatProductSetFromOptions_seqaij_seqaij_C", MatProductSetFromOptions_SeqAIJ);
4714:   PetscObjectComposeFunction((PetscObject)B, "MatSeqAIJKron_C", MatSeqAIJKron_SeqAIJ);
4715:   PetscObjectComposeFunction((PetscObject)B, "MatSetPreallocationCOO_C", MatSetPreallocationCOO_SeqAIJ);
4716:   PetscObjectComposeFunction((PetscObject)B, "MatSetValuesCOO_C", MatSetValuesCOO_SeqAIJ);
4717:   MatCreate_SeqAIJ_Inode(B);
4718:   PetscObjectChangeTypeName((PetscObject)B, MATSEQAIJ);
4719:   MatSeqAIJSetTypeFromOptions(B); /* this allows changing the matrix subtype to say MATSEQAIJPERM */
4720:   return 0;
4721: }

4723: /*
4724:     Given a matrix generated with MatGetFactor() duplicates all the information in A into C
4725: */
4726: PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C, Mat A, MatDuplicateOption cpvalues, PetscBool mallocmatspace)
4727: {
4728:   Mat_SeqAIJ *c = (Mat_SeqAIJ *)C->data, *a = (Mat_SeqAIJ *)A->data;
4729:   PetscInt    m = A->rmap->n, i;


4733:   C->factortype = A->factortype;
4734:   c->row        = NULL;
4735:   c->col        = NULL;
4736:   c->icol       = NULL;
4737:   c->reallocs   = 0;

4739:   C->assembled    = A->assembled;
4740:   C->preallocated = A->preallocated;

4742:   if (A->preallocated) {
4743:     PetscLayoutReference(A->rmap, &C->rmap);
4744:     PetscLayoutReference(A->cmap, &C->cmap);

4746:     PetscMalloc1(m, &c->imax);
4747:     PetscMemcpy(c->imax, a->imax, m * sizeof(PetscInt));
4748:     PetscMalloc1(m, &c->ilen);
4749:     PetscMemcpy(c->ilen, a->ilen, m * sizeof(PetscInt));

4751:     /* allocate the matrix space */
4752:     if (mallocmatspace) {
4753:       PetscMalloc3(a->i[m], &c->a, a->i[m], &c->j, m + 1, &c->i);

4755:       c->singlemalloc = PETSC_TRUE;

4757:       PetscArraycpy(c->i, a->i, m + 1);
4758:       if (m > 0) {
4759:         PetscArraycpy(c->j, a->j, a->i[m]);
4760:         if (cpvalues == MAT_COPY_VALUES) {
4761:           const PetscScalar *aa;

4763:           MatSeqAIJGetArrayRead(A, &aa);
4764:           PetscArraycpy(c->a, aa, a->i[m]);
4765:           MatSeqAIJGetArrayRead(A, &aa);
4766:         } else {
4767:           PetscArrayzero(c->a, a->i[m]);
4768:         }
4769:       }
4770:     }

4772:     c->ignorezeroentries = a->ignorezeroentries;
4773:     c->roworiented       = a->roworiented;
4774:     c->nonew             = a->nonew;
4775:     if (a->diag) {
4776:       PetscMalloc1(m + 1, &c->diag);
4777:       PetscMemcpy(c->diag, a->diag, m * sizeof(PetscInt));
4778:     } else c->diag = NULL;

4780:     c->solve_work         = NULL;
4781:     c->saved_values       = NULL;
4782:     c->idiag              = NULL;
4783:     c->ssor_work          = NULL;
4784:     c->keepnonzeropattern = a->keepnonzeropattern;
4785:     c->free_a             = PETSC_TRUE;
4786:     c->free_ij            = PETSC_TRUE;

4788:     c->rmax  = a->rmax;
4789:     c->nz    = a->nz;
4790:     c->maxnz = a->nz; /* Since we allocate exactly the right amount */

4792:     c->compressedrow.use   = a->compressedrow.use;
4793:     c->compressedrow.nrows = a->compressedrow.nrows;
4794:     if (a->compressedrow.use) {
4795:       i = a->compressedrow.nrows;
4796:       PetscMalloc2(i + 1, &c->compressedrow.i, i, &c->compressedrow.rindex);
4797:       PetscArraycpy(c->compressedrow.i, a->compressedrow.i, i + 1);
4798:       PetscArraycpy(c->compressedrow.rindex, a->compressedrow.rindex, i);
4799:     } else {
4800:       c->compressedrow.use    = PETSC_FALSE;
4801:       c->compressedrow.i      = NULL;
4802:       c->compressedrow.rindex = NULL;
4803:     }
4804:     c->nonzerorowcnt = a->nonzerorowcnt;
4805:     C->nonzerostate  = A->nonzerostate;

4807:     MatDuplicate_SeqAIJ_Inode(A, cpvalues, &C);
4808:   }
4809:   PetscFunctionListDuplicate(((PetscObject)A)->qlist, &((PetscObject)C)->qlist);
4810:   return 0;
4811: }

4813: PetscErrorCode MatDuplicate_SeqAIJ(Mat A, MatDuplicateOption cpvalues, Mat *B)
4814: {
4815:   MatCreate(PetscObjectComm((PetscObject)A), B);
4816:   MatSetSizes(*B, A->rmap->n, A->cmap->n, A->rmap->n, A->cmap->n);
4817:   if (!(A->rmap->n % A->rmap->bs) && !(A->cmap->n % A->cmap->bs)) MatSetBlockSizesFromMats(*B, A, A);
4818:   MatSetType(*B, ((PetscObject)A)->type_name);
4819:   MatDuplicateNoCreate_SeqAIJ(*B, A, cpvalues, PETSC_TRUE);
4820:   return 0;
4821: }

4823: PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer)
4824: {
4825:   PetscBool isbinary, ishdf5;

4829:   /* force binary viewer to load .info file if it has not yet done so */
4830:   PetscViewerSetUp(viewer);
4831:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERBINARY, &isbinary);
4832:   PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERHDF5, &ishdf5);
4833:   if (isbinary) {
4834:     MatLoad_SeqAIJ_Binary(newMat, viewer);
4835:   } else if (ishdf5) {
4836: #if defined(PETSC_HAVE_HDF5)
4837:     MatLoad_AIJ_HDF5(newMat, viewer);
4838: #else
4839:     SETERRQ(PetscObjectComm((PetscObject)newMat), PETSC_ERR_SUP, "HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
4840: #endif
4841:   } else {
4842:     SETERRQ(PetscObjectComm((PetscObject)newMat), PETSC_ERR_SUP, "Viewer type %s not yet supported for reading %s matrices", ((PetscObject)viewer)->type_name, ((PetscObject)newMat)->type_name);
4843:   }
4844:   return 0;
4845: }

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

4852:   PetscViewerSetUp(viewer);

4854:   /* read in matrix header */
4855:   PetscViewerBinaryRead(viewer, header, 4, NULL, PETSC_INT);
4857:   M  = header[1];
4858:   N  = header[2];
4859:   nz = header[3];

4864:   /* set block sizes from the viewer's .info file */
4865:   MatLoad_Binary_BlockSizes(mat, viewer);
4866:   /* set local and global sizes if not set already */
4867:   if (mat->rmap->n < 0) mat->rmap->n = M;
4868:   if (mat->cmap->n < 0) mat->cmap->n = N;
4869:   if (mat->rmap->N < 0) mat->rmap->N = M;
4870:   if (mat->cmap->N < 0) mat->cmap->N = N;
4871:   PetscLayoutSetUp(mat->rmap);
4872:   PetscLayoutSetUp(mat->cmap);

4874:   /* check if the matrix sizes are correct */
4875:   MatGetSize(mat, &rows, &cols);

4878:   /* read in row lengths */
4879:   PetscMalloc1(M, &rowlens);
4880:   PetscViewerBinaryRead(viewer, rowlens, M, NULL, PETSC_INT);
4881:   /* check if sum(rowlens) is same as nz */
4882:   sum = 0;
4883:   for (i = 0; i < M; i++) sum += rowlens[i];
4885:   /* preallocate and check sizes */
4886:   MatSeqAIJSetPreallocation_SeqAIJ(mat, 0, rowlens);
4887:   MatGetSize(mat, &rows, &cols);
4889:   /* store row lengths */
4890:   PetscArraycpy(a->ilen, rowlens, M);
4891:   PetscFree(rowlens);

4893:   /* fill in "i" row pointers */
4894:   a->i[0] = 0;
4895:   for (i = 0; i < M; i++) a->i[i + 1] = a->i[i] + a->ilen[i];
4896:   /* read in "j" column indices */
4897:   PetscViewerBinaryRead(viewer, a->j, nz, NULL, PETSC_INT);
4898:   /* read in "a" nonzero values */
4899:   PetscViewerBinaryRead(viewer, a->a, nz, NULL, PETSC_SCALAR);

4901:   MatAssemblyBegin(mat, MAT_FINAL_ASSEMBLY);
4902:   MatAssemblyEnd(mat, MAT_FINAL_ASSEMBLY);
4903:   return 0;
4904: }

4906: PetscErrorCode MatEqual_SeqAIJ(Mat A, Mat B, PetscBool *flg)
4907: {
4908:   Mat_SeqAIJ        *a = (Mat_SeqAIJ *)A->data, *b = (Mat_SeqAIJ *)B->data;
4909:   const PetscScalar *aa, *ba;
4910: #if defined(PETSC_USE_COMPLEX)
4911:   PetscInt k;
4912: #endif

4914:   /* If the  matrix dimensions are not equal,or no of nonzeros */
4915:   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) || (a->nz != b->nz)) {
4916:     *flg = PETSC_FALSE;
4917:     return 0;
4918:   }

4920:   /* if the a->i are the same */
4921:   PetscArraycmp(a->i, b->i, A->rmap->n + 1, flg);
4922:   if (!*flg) return 0;

4924:   /* if a->j are the same */
4925:   PetscArraycmp(a->j, b->j, a->nz, flg);
4926:   if (!*flg) return 0;

4928:   MatSeqAIJGetArrayRead(A, &aa);
4929:   MatSeqAIJGetArrayRead(B, &ba);
4930:   /* if a->a are the same */
4931: #if defined(PETSC_USE_COMPLEX)
4932:   for (k = 0; k < a->nz; k++) {
4933:     if (PetscRealPart(aa[k]) != PetscRealPart(ba[k]) || PetscImaginaryPart(aa[k]) != PetscImaginaryPart(ba[k])) {
4934:       *flg = PETSC_FALSE;
4935:       return 0;
4936:     }
4937:   }
4938: #else
4939:   PetscArraycmp(aa, ba, a->nz, flg);
4940: #endif
4941:   MatSeqAIJRestoreArrayRead(A, &aa);
4942:   MatSeqAIJRestoreArrayRead(B, &ba);
4943:   return 0;
4944: }

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

4950:       Collective

4952:    Input Parameters:
4953: +   comm - must be an MPI communicator of size 1
4954: .   m - number of rows
4955: .   n - number of columns
4956: .   i - row indices; that is i[0] = 0, i[row] = i[row-1] + number of elements in that row of the matrix
4957: .   j - column indices
4958: -   a - matrix values

4960:    Output Parameter:
4961: .   mat - the matrix

4963:    Level: intermediate

4965:    Notes:
4966:        The i, j, and a arrays are not copied by this routine, the user must free these arrays
4967:     once the matrix is destroyed and not before

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

4971:        The i and j indices are 0 based

4973:        The format which is used for the sparse matrix input, is equivalent to a
4974:     row-major ordering.. i.e for the following matrix, the input data expected is
4975:     as shown

4977: $        1 0 0
4978: $        2 0 3
4979: $        4 5 6
4980: $
4981: $        i =  {0,1,3,6}  [size = nrow+1  = 3+1]
4982: $        j =  {0,0,2,0,1,2}  [size = 6]; values must be sorted for each row
4983: $        v =  {1,2,3,4,5,6}  [size = 6]

4985: .seealso: `MatCreate()`, `MatCreateAIJ()`, `MatCreateSeqAIJ()`, `MatCreateMPIAIJWithArrays()`, `MatMPIAIJSetPreallocationCSR()`
4986: @*/
4987: PetscErrorCode MatCreateSeqAIJWithArrays(MPI_Comm comm, PetscInt m, PetscInt n, PetscInt i[], PetscInt j[], PetscScalar a[], Mat *mat)
4988: {
4989:   PetscInt    ii;
4990:   Mat_SeqAIJ *aij;
4991:   PetscInt    jj;

4994:   MatCreate(comm, mat);
4995:   MatSetSizes(*mat, m, n, m, n);
4996:   /* MatSetBlockSizes(*mat,,); */
4997:   MatSetType(*mat, MATSEQAIJ);
4998:   MatSeqAIJSetPreallocation_SeqAIJ(*mat, MAT_SKIP_ALLOCATION, NULL);
4999:   aij = (Mat_SeqAIJ *)(*mat)->data;
5000:   PetscMalloc1(m, &aij->imax);
5001:   PetscMalloc1(m, &aij->ilen);

5003:   aij->i            = i;
5004:   aij->j            = j;
5005:   aij->a            = a;
5006:   aij->singlemalloc = PETSC_FALSE;
5007:   aij->nonew        = -1; /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
5008:   aij->free_a       = PETSC_FALSE;
5009:   aij->free_ij      = PETSC_FALSE;

5011:   for (ii = 0, aij->nonzerorowcnt = 0, aij->rmax = 0; ii < m; ii++) {
5012:     aij->ilen[ii] = aij->imax[ii] = i[ii + 1] - i[ii];
5013:     if (PetscDefined(USE_DEBUG)) {
5015:       for (jj = i[ii] + 1; jj < i[ii + 1]; jj++) {
5018:       }
5019:     }
5020:   }
5021:   if (PetscDefined(USE_DEBUG)) {
5022:     for (ii = 0; ii < aij->i[m]; ii++) {
5025:     }
5026:   }

5028:   MatAssemblyBegin(*mat, MAT_FINAL_ASSEMBLY);
5029:   MatAssemblyEnd(*mat, MAT_FINAL_ASSEMBLY);
5030:   return 0;
5031: }

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

5037:       Collective

5039:    Input Parameters:
5040: +   comm - must be an MPI communicator of size 1
5041: .   m   - number of rows
5042: .   n   - number of columns
5043: .   i   - row indices
5044: .   j   - column indices
5045: .   a   - matrix values
5046: .   nz  - number of nonzeros
5047: -   idx - if the i and j indices start with 1 use `PETSC_TRUE` otherwise use `PETSC_FALSE`

5049:    Output Parameter:
5050: .   mat - the matrix

5052:    Level: intermediate

5054:    Example:
5055:        For the following matrix, the input data expected is as shown (using 0 based indexing)
5056: .vb
5057:         1 0 0
5058:         2 0 3
5059:         4 5 6

5061:         i =  {0,1,1,2,2,2}
5062:         j =  {0,0,2,0,1,2}
5063:         v =  {1,2,3,4,5,6}
5064: .ve
5065:   Notes:
5066:     Instead of using this function, users should also consider `MatSetPreallocationCOO()` and `MatSetValuesCOO()`, which allow repeated or remote entries,
5067:     and are particularly useful in iterative applications.

5069: .seealso: `MatCreate()`, `MatCreateAIJ()`, `MatCreateSeqAIJ()`, `MatCreateSeqAIJWithArrays()`, `MatMPIAIJSetPreallocationCSR()`, `MatSetValuesCOO()`, `MatSetPreallocationCOO()`
5070: @*/
5071: PetscErrorCode MatCreateSeqAIJFromTriple(MPI_Comm comm, PetscInt m, PetscInt n, PetscInt i[], PetscInt j[], PetscScalar a[], Mat *mat, PetscInt nz, PetscBool idx)
5072: {
5073:   PetscInt ii, *nnz, one = 1, row, col;

5075:   PetscCalloc1(m, &nnz);
5076:   for (ii = 0; ii < nz; ii++) nnz[i[ii] - !!idx] += 1;
5077:   MatCreate(comm, mat);
5078:   MatSetSizes(*mat, m, n, m, n);
5079:   MatSetType(*mat, MATSEQAIJ);
5080:   MatSeqAIJSetPreallocation_SeqAIJ(*mat, 0, nnz);
5081:   for (ii = 0; ii < nz; ii++) {
5082:     if (idx) {
5083:       row = i[ii] - 1;
5084:       col = j[ii] - 1;
5085:     } else {
5086:       row = i[ii];
5087:       col = j[ii];
5088:     }
5089:     MatSetValues(*mat, one, &row, one, &col, &a[ii], ADD_VALUES);
5090:   }
5091:   MatAssemblyBegin(*mat, MAT_FINAL_ASSEMBLY);
5092:   MatAssemblyEnd(*mat, MAT_FINAL_ASSEMBLY);
5093:   PetscFree(nnz);
5094:   return 0;
5095: }

5097: PetscErrorCode MatSeqAIJInvalidateDiagonal(Mat A)
5098: {
5099:   Mat_SeqAIJ *a = (Mat_SeqAIJ *)A->data;

5101:   a->idiagvalid  = PETSC_FALSE;
5102:   a->ibdiagvalid = PETSC_FALSE;

5104:   MatSeqAIJInvalidateDiagonal_Inode(A);
5105:   return 0;
5106: }

5108: PetscErrorCode MatCreateMPIMatConcatenateSeqMat_SeqAIJ(MPI_Comm comm, Mat inmat, PetscInt n, MatReuse scall, Mat *outmat)
5109: {
5110:   MatCreateMPIMatConcatenateSeqMat_MPIAIJ(comm, inmat, n, scall, outmat);
5111:   return 0;
5112: }

5114: /*
5115:  Permute A into C's *local* index space using rowemb,colemb.
5116:  The embedding are supposed to be injections and the above implies that the range of rowemb is a subset
5117:  of [0,m), colemb is in [0,n).
5118:  If pattern == DIFFERENT_NONZERO_PATTERN, C is preallocated according to A.
5119:  */
5120: PetscErrorCode MatSetSeqMat_SeqAIJ(Mat C, IS rowemb, IS colemb, MatStructure pattern, Mat B)
5121: {
5122:   /* If making this function public, change the error returned in this function away from _PLIB. */
5123:   Mat_SeqAIJ     *Baij;
5124:   PetscBool       seqaij;
5125:   PetscInt        m, n, *nz, i, j, count;
5126:   PetscScalar     v;
5127:   const PetscInt *rowindices, *colindices;

5129:   if (!B) return 0;
5130:   /* Check to make sure the target matrix (and embeddings) are compatible with C and each other. */
5131:   PetscObjectBaseTypeCompare((PetscObject)B, MATSEQAIJ, &seqaij);
5133:   if (rowemb) {
5134:     ISGetLocalSize(rowemb, &m);
5136:   } else {
5138:   }
5139:   if (colemb) {
5140:     ISGetLocalSize(colemb, &n);
5142:   } else {
5144:   }

5146:   Baij = (Mat_SeqAIJ *)(B->data);
5147:   if (pattern == DIFFERENT_NONZERO_PATTERN) {
5148:     PetscMalloc1(B->rmap->n, &nz);
5149:     for (i = 0; i < B->rmap->n; i++) nz[i] = Baij->i[i + 1] - Baij->i[i];
5150:     MatSeqAIJSetPreallocation(C, 0, nz);
5151:     PetscFree(nz);
5152:   }
5153:   if (pattern == SUBSET_NONZERO_PATTERN) MatZeroEntries(C);
5154:   count      = 0;
5155:   rowindices = NULL;
5156:   colindices = NULL;
5157:   if (rowemb) ISGetIndices(rowemb, &rowindices);
5158:   if (colemb) ISGetIndices(colemb, &colindices);
5159:   for (i = 0; i < B->rmap->n; i++) {
5160:     PetscInt row;
5161:     row = i;
5162:     if (rowindices) row = rowindices[i];
5163:     for (j = Baij->i[i]; j < Baij->i[i + 1]; j++) {
5164:       PetscInt col;
5165:       col = Baij->j[count];
5166:       if (colindices) col = colindices[col];
5167:       v = Baij->a[count];
5168:       MatSetValues(C, 1, &row, 1, &col, &v, INSERT_VALUES);
5169:       ++count;
5170:     }
5171:   }
5172:   /* FIXME: set C's nonzerostate correctly. */
5173:   /* Assembly for C is necessary. */
5174:   C->preallocated  = PETSC_TRUE;
5175:   C->assembled     = PETSC_TRUE;
5176:   C->was_assembled = PETSC_FALSE;
5177:   return 0;
5178: }

5180: PetscFunctionList MatSeqAIJList = NULL;

5182: /*@C
5183:    MatSeqAIJSetType - Converts a `MATSEQAIJ` matrix to a subtype

5185:    Collective

5187:    Input Parameters:
5188: +  mat      - the matrix object
5189: -  matype   - matrix type

5191:    Options Database Key:
5192: .  -mat_seqaij_type  <method> - for example seqaijcrl

5194:   Level: intermediate

5196: .seealso: `PCSetType()`, `VecSetType()`, `MatCreate()`, `MatType`, `Mat`
5197: @*/
5198: PetscErrorCode MatSeqAIJSetType(Mat mat, MatType matype)
5199: {
5200:   PetscBool sametype;
5201:   PetscErrorCode (*r)(Mat, MatType, MatReuse, Mat *);

5204:   PetscObjectTypeCompare((PetscObject)mat, matype, &sametype);
5205:   if (sametype) return 0;

5207:   PetscFunctionListFind(MatSeqAIJList, matype, &r);
5209:   (*r)(mat, matype, MAT_INPLACE_MATRIX, &mat);
5210:   return 0;
5211: }

5213: /*@C
5214:   MatSeqAIJRegister -  - Adds a new sub-matrix type for sequential `MATSEQAIJ` matrices

5216:    Not Collective

5218:    Input Parameters:
5219: +  name - name of a new user-defined matrix type, for example `MATSEQAIJCRL`
5220: -  function - routine to convert to subtype

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

5225:    Then, your matrix can be chosen with the procedural interface at runtime via the option
5226: $     -mat_seqaij_type my_mat

5228:    Level: advanced

5230: .seealso: `MatSeqAIJRegisterAll()`
5231: @*/
5232: PetscErrorCode MatSeqAIJRegister(const char sname[], PetscErrorCode (*function)(Mat, MatType, MatReuse, Mat *))
5233: {
5234:   MatInitializePackage();
5235:   PetscFunctionListAdd(&MatSeqAIJList, sname, function);
5236:   return 0;
5237: }

5239: PetscBool MatSeqAIJRegisterAllCalled = PETSC_FALSE;

5241: /*@C
5242:   MatSeqAIJRegisterAll - Registers all of the matrix subtypes of `MATSSEQAIJ`

5244:   Not Collective

5246:   Level: advanced

5248: .seealso: `MatRegisterAll()`, `MatSeqAIJRegister()`
5249: @*/
5250: PetscErrorCode MatSeqAIJRegisterAll(void)
5251: {
5252:   if (MatSeqAIJRegisterAllCalled) return 0;
5253:   MatSeqAIJRegisterAllCalled = PETSC_TRUE;

5255:   MatSeqAIJRegister(MATSEQAIJCRL, MatConvert_SeqAIJ_SeqAIJCRL);
5256:   MatSeqAIJRegister(MATSEQAIJPERM, MatConvert_SeqAIJ_SeqAIJPERM);
5257:   MatSeqAIJRegister(MATSEQAIJSELL, MatConvert_SeqAIJ_SeqAIJSELL);
5258: #if defined(PETSC_HAVE_MKL_SPARSE)
5259:   MatSeqAIJRegister(MATSEQAIJMKL, MatConvert_SeqAIJ_SeqAIJMKL);
5260: #endif
5261: #if defined(PETSC_HAVE_CUDA)
5262:   MatSeqAIJRegister(MATSEQAIJCUSPARSE, MatConvert_SeqAIJ_SeqAIJCUSPARSE);
5263: #endif
5264: #if defined(PETSC_HAVE_KOKKOS_KERNELS)
5265:   MatSeqAIJRegister(MATSEQAIJKOKKOS, MatConvert_SeqAIJ_SeqAIJKokkos);
5266: #endif
5267: #if defined(PETSC_HAVE_VIENNACL) && defined(PETSC_HAVE_VIENNACL_NO_CUDA)
5268:   MatSeqAIJRegister(MATMPIAIJVIENNACL, MatConvert_SeqAIJ_SeqAIJViennaCL);
5269: #endif
5270:   return 0;
5271: }

5273: /*
5274:     Special version for direct calls from Fortran
5275: */
5276: #include <petsc/private/fortranimpl.h>
5277: #if defined(PETSC_HAVE_FORTRAN_CAPS)
5278:   #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
5279: #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
5280:   #define matsetvaluesseqaij_ matsetvaluesseqaij
5281: #endif

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

5285: /* Change these macros so can be used in void function */
5286: /* Identical to PetscCallVoid, except it assigns to *_ierr */
5287: #undef PetscCall
5288: #define PetscCall(...) \
5289:   do { \
5290:     PetscErrorCode ierr_msv_mpiaij = __VA_ARGS__; \
5291:     if (PetscUnlikely(ierr_msv_mpiaij)) { \
5292:       *_PetscError(PETSC_COMM_SELF, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr_msv_mpiaij, PETSC_ERROR_REPEAT, " "); \
5293:       return; \
5294:     } \
5295:   } while (0)

5297: #undef SETERRQ
5298: #define SETERRQ(comm, ierr, ...) \
5299:   do { \
5300:     *_PetscError(comm, __LINE__, PETSC_FUNCTION_NAME, __FILE__, ierr, PETSC_ERROR_INITIAL, __VA_ARGS__); \
5301:     return; \
5302:   } while (0)

5304: PETSC_EXTERN void matsetvaluesseqaij_(Mat *AA, PetscInt *mm, const PetscInt im[], PetscInt *nn, const PetscInt in[], const PetscScalar v[], InsertMode *isis, PetscErrorCode *_ierr)
5305: {
5306:   Mat         A = *AA;
5307:   PetscInt    m = *mm, n = *nn;
5308:   InsertMode  is = *isis;
5309:   Mat_SeqAIJ *a  = (Mat_SeqAIJ *)A->data;
5310:   PetscInt   *rp, k, low, high, t, ii, row, nrow, i, col, l, rmax, N;
5311:   PetscInt   *imax, *ai, *ailen;
5312:   PetscInt   *aj, nonew = a->nonew, lastcol = -1;
5313:   MatScalar  *ap, value, *aa;
5314:   PetscBool   ignorezeroentries = a->ignorezeroentries;
5315:   PetscBool   roworiented       = a->roworiented;

5317:   MatCheckPreallocated(A, 1);
5318:   imax  = a->imax;
5319:   ai    = a->i;
5320:   ailen = a->ilen;
5321:   aj    = a->j;
5322:   aa    = a->a;

5324:   for (k = 0; k < m; k++) { /* loop over added rows */
5325:     row = im[k];
5326:     if (row < 0) continue;
5328:     rp   = aj + ai[row];
5329:     ap   = aa + ai[row];
5330:     rmax = imax[row];
5331:     nrow = ailen[row];
5332:     low  = 0;
5333:     high = nrow;
5334:     for (l = 0; l < n; l++) { /* loop over added columns */
5335:       if (in[l] < 0) continue;
5337:       col = in[l];
5338:       if (roworiented) value = v[l + k * n];
5339:       else value = v[k + l * m];

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

5343:       if (col <= lastcol) low = 0;
5344:       else high = nrow;
5345:       lastcol = col;
5346:       while (high - low > 5) {
5347:         t = (low + high) / 2;
5348:         if (rp[t] > col) high = t;
5349:         else low = t;
5350:       }
5351:       for (i = low; i < high; i++) {
5352:         if (rp[i] > col) break;
5353:         if (rp[i] == col) {
5354:           if (is == ADD_VALUES) ap[i] += value;
5355:           else ap[i] = value;
5356:           goto noinsert;
5357:         }
5358:       }
5359:       if (value == 0.0 && ignorezeroentries) goto noinsert;
5360:       if (nonew == 1) goto noinsert;
5362:       MatSeqXAIJReallocateAIJ(A, A->rmap->n, 1, nrow, row, col, rmax, aa, ai, aj, rp, ap, imax, nonew, MatScalar);
5363:       N = nrow++ - 1;
5364:       a->nz++;
5365:       high++;
5366:       /* shift up all the later entries in this row */
5367:       for (ii = N; ii >= i; ii--) {
5368:         rp[ii + 1] = rp[ii];
5369:         ap[ii + 1] = ap[ii];
5370:       }
5371:       rp[i] = col;
5372:       ap[i] = value;
5373:       A->nonzerostate++;
5374:     noinsert:;
5375:       low = i + 1;
5376:     }
5377:     ailen[row] = nrow;
5378:   }
5379:   return;
5380: }
5381: /* Undefining these here since they were redefined from their original definition above! No
5382:  * other PETSc functions should be defined past this point, as it is impossible to recover the
5383:  * original definitions */
5384: #undef PetscCall
5385: #undef SETERRQ