Actual source code: aij.c

petsc-main 2021-03-03
Report Typos and Errors
  1: /*
  2:     Defines the basic matrix operations for the AIJ (compressed row)
  3:   matrix storage format.
  4: */


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

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

 19:   PetscObjectOptionsBegin((PetscObject)A);
 20:   PetscOptionsFList("-mat_seqaij_type","Matrix SeqAIJ type","MatSeqAIJSetType",MatSeqAIJList,"seqaij",type,256,&flg);
 21:   if (flg) {
 22:     MatSeqAIJSetType(A,type);
 23:   }
 24:   PetscOptionsEnd();
 25:   return(0);
 26: }

 28: PetscErrorCode MatGetColumnNorms_SeqAIJ(Mat A,NormType type,PetscReal *norms)
 29: {
 31:   PetscInt       i,m,n;
 32:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)A->data;

 35:   MatGetSize(A,&m,&n);
 36:   PetscArrayzero(norms,n);
 37:   if (type == NORM_2) {
 38:     for (i=0; i<aij->i[m]; i++) {
 39:       norms[aij->j[i]] += PetscAbsScalar(aij->a[i]*aij->a[i]);
 40:     }
 41:   } else if (type == NORM_1) {
 42:     for (i=0; i<aij->i[m]; i++) {
 43:       norms[aij->j[i]] += PetscAbsScalar(aij->a[i]);
 44:     }
 45:   } else if (type == NORM_INFINITY) {
 46:     for (i=0; i<aij->i[m]; i++) {
 47:       norms[aij->j[i]] = PetscMax(PetscAbsScalar(aij->a[i]),norms[aij->j[i]]);
 48:     }
 49:   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Unknown NormType");

 51:   if (type == NORM_2) {
 52:     for (i=0; i<n; i++) norms[i] = PetscSqrtReal(norms[i]);
 53:   }
 54:   return(0);
 55: }

 57: PetscErrorCode MatFindOffBlockDiagonalEntries_SeqAIJ(Mat A,IS *is)
 58: {
 59:   Mat_SeqAIJ      *a  = (Mat_SeqAIJ*)A->data;
 60:   PetscInt        i,m=A->rmap->n,cnt = 0, bs = A->rmap->bs;
 61:   const PetscInt  *jj = a->j,*ii = a->i;
 62:   PetscInt        *rows;
 63:   PetscErrorCode  ierr;

 66:   for (i=0; i<m; i++) {
 67:     if ((ii[i] != ii[i+1]) && ((jj[ii[i]] < bs*(i/bs)) || (jj[ii[i+1]-1] > bs*((i+bs)/bs)-1))) {
 68:       cnt++;
 69:     }
 70:   }
 71:   PetscMalloc1(cnt,&rows);
 72:   cnt  = 0;
 73:   for (i=0; i<m; i++) {
 74:     if ((ii[i] != ii[i+1]) && ((jj[ii[i]] < bs*(i/bs)) || (jj[ii[i+1]-1] > bs*((i+bs)/bs)-1))) {
 75:       rows[cnt] = i;
 76:       cnt++;
 77:     }
 78:   }
 79:   ISCreateGeneral(PETSC_COMM_SELF,cnt,rows,PETSC_OWN_POINTER,is);
 80:   return(0);
 81: }

 83: PetscErrorCode MatFindZeroDiagonals_SeqAIJ_Private(Mat A,PetscInt *nrows,PetscInt **zrows)
 84: {
 85:   Mat_SeqAIJ      *a  = (Mat_SeqAIJ*)A->data;
 86:   const MatScalar *aa = a->a;
 87:   PetscInt        i,m=A->rmap->n,cnt = 0;
 88:   const PetscInt  *ii = a->i,*jj = a->j,*diag;
 89:   PetscInt        *rows;
 90:   PetscErrorCode  ierr;

 93:   MatMarkDiagonal_SeqAIJ(A);
 94:   diag = a->diag;
 95:   for (i=0; i<m; i++) {
 96:     if ((diag[i] >= ii[i+1]) || (jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) {
 97:       cnt++;
 98:     }
 99:   }
100:   PetscMalloc1(cnt,&rows);
101:   cnt  = 0;
102:   for (i=0; i<m; i++) {
103:     if ((diag[i] >= ii[i+1]) || (jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) {
104:       rows[cnt++] = i;
105:     }
106:   }
107:   *nrows = cnt;
108:   *zrows = rows;
109:   return(0);
110: }

112: PetscErrorCode MatFindZeroDiagonals_SeqAIJ(Mat A,IS *zrows)
113: {
114:   PetscInt       nrows,*rows;

118:   *zrows = NULL;
119:   MatFindZeroDiagonals_SeqAIJ_Private(A,&nrows,&rows);
120:   ISCreateGeneral(PetscObjectComm((PetscObject)A),nrows,rows,PETSC_OWN_POINTER,zrows);
121:   return(0);
122: }

124: PetscErrorCode MatFindNonzeroRows_SeqAIJ(Mat A,IS *keptrows)
125: {
126:   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
127:   const MatScalar *aa;
128:   PetscInt        m=A->rmap->n,cnt = 0;
129:   const PetscInt  *ii;
130:   PetscInt        n,i,j,*rows;
131:   PetscErrorCode  ierr;

134:   MatSeqAIJGetArrayRead(A,&aa);
135:   *keptrows = NULL;
136:   ii        = a->i;
137:   for (i=0; i<m; i++) {
138:     n = ii[i+1] - ii[i];
139:     if (!n) {
140:       cnt++;
141:       goto ok1;
142:     }
143:     for (j=ii[i]; j<ii[i+1]; j++) {
144:       if (aa[j] != 0.0) goto ok1;
145:     }
146:     cnt++;
147: ok1:;
148:   }
149:   if (!cnt) {
150:     MatSeqAIJRestoreArrayRead(A,&aa);
151:     return(0);
152:   }
153:   PetscMalloc1(A->rmap->n-cnt,&rows);
154:   cnt  = 0;
155:   for (i=0; i<m; i++) {
156:     n = ii[i+1] - ii[i];
157:     if (!n) continue;
158:     for (j=ii[i]; j<ii[i+1]; j++) {
159:       if (aa[j] != 0.0) {
160:         rows[cnt++] = i;
161:         break;
162:       }
163:     }
164:   }
165:   MatSeqAIJRestoreArrayRead(A,&aa);
166:   ISCreateGeneral(PETSC_COMM_SELF,cnt,rows,PETSC_OWN_POINTER,keptrows);
167:   return(0);
168: }

170: PetscErrorCode  MatDiagonalSet_SeqAIJ(Mat Y,Vec D,InsertMode is)
171: {
172:   PetscErrorCode    ierr;
173:   Mat_SeqAIJ        *aij = (Mat_SeqAIJ*) Y->data;
174:   PetscInt          i,m = Y->rmap->n;
175:   const PetscInt    *diag;
176:   MatScalar         *aa;
177:   const PetscScalar *v;
178:   PetscBool         missing;
179: #if defined(PETSC_HAVE_DEVICE)
180:   PetscBool         inserted = PETSC_FALSE;
181: #endif

184:   if (Y->assembled) {
185:     MatMissingDiagonal_SeqAIJ(Y,&missing,NULL);
186:     if (!missing) {
187:       diag = aij->diag;
188:       VecGetArrayRead(D,&v);
189:       MatSeqAIJGetArray(Y,&aa);
190:       if (is == INSERT_VALUES) {
191: #if defined(PETSC_HAVE_DEVICE)
192:         inserted = PETSC_TRUE;
193: #endif
194:         for (i=0; i<m; i++) {
195:           aa[diag[i]] = v[i];
196:         }
197:       } else {
198:         for (i=0; i<m; i++) {
199: #if defined(PETSC_HAVE_DEVICE)
200:           if (v[i] != 0.0) inserted = PETSC_TRUE;
201: #endif
202:           aa[diag[i]] += v[i];
203:         }
204:       }
205:       MatSeqAIJRestoreArray(Y,&aa);
206: #if defined(PETSC_HAVE_DEVICE)
207:       if (inserted) Y->offloadmask = PETSC_OFFLOAD_CPU;
208: #endif
209:       VecRestoreArrayRead(D,&v);
210:       return(0);
211:     }
212:     MatSeqAIJInvalidateDiagonal(Y);
213:   }
214:   MatDiagonalSet_Default(Y,D,is);
215:   return(0);
216: }

218: PetscErrorCode MatGetRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *m,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
219: {
220:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
222:   PetscInt       i,ishift;

225:   *m = A->rmap->n;
226:   if (!ia) return(0);
227:   ishift = 0;
228:   if (symmetric && !A->structurally_symmetric) {
229:     MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,PETSC_TRUE,ishift,oshift,(PetscInt**)ia,(PetscInt**)ja);
230:   } else if (oshift == 1) {
231:     PetscInt *tia;
232:     PetscInt nz = a->i[A->rmap->n];
233:     /* malloc space and  add 1 to i and j indices */
234:     PetscMalloc1(A->rmap->n+1,&tia);
235:     for (i=0; i<A->rmap->n+1; i++) tia[i] = a->i[i] + 1;
236:     *ia = tia;
237:     if (ja) {
238:       PetscInt *tja;
239:       PetscMalloc1(nz+1,&tja);
240:       for (i=0; i<nz; i++) tja[i] = a->j[i] + 1;
241:       *ja = tja;
242:     }
243:   } else {
244:     *ia = a->i;
245:     if (ja) *ja = a->j;
246:   }
247:   return(0);
248: }

250: PetscErrorCode MatRestoreRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
251: {

255:   if (!ia) return(0);
256:   if ((symmetric && !A->structurally_symmetric) || oshift == 1) {
257:     PetscFree(*ia);
258:     if (ja) {PetscFree(*ja);}
259:   }
260:   return(0);
261: }

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

271:   *nn = n;
272:   if (!ia) return(0);
273:   if (symmetric) {
274:     MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,PETSC_TRUE,0,oshift,(PetscInt**)ia,(PetscInt**)ja);
275:   } else {
276:     PetscCalloc1(n,&collengths);
277:     PetscMalloc1(n+1,&cia);
278:     PetscMalloc1(nz,&cja);
279:     jj   = a->j;
280:     for (i=0; i<nz; i++) {
281:       collengths[jj[i]]++;
282:     }
283:     cia[0] = oshift;
284:     for (i=0; i<n; i++) {
285:       cia[i+1] = cia[i] + collengths[i];
286:     }
287:     PetscArrayzero(collengths,n);
288:     jj   = a->j;
289:     for (row=0; row<m; row++) {
290:       mr = a->i[row+1] - a->i[row];
291:       for (i=0; i<mr; i++) {
292:         col = *jj++;

294:         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
295:       }
296:     }
297:     PetscFree(collengths);
298:     *ia  = cia; *ja = cja;
299:   }
300:   return(0);
301: }

303: PetscErrorCode MatRestoreColumnIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
304: {

308:   if (!ia) return(0);

310:   PetscFree(*ia);
311:   PetscFree(*ja);
312:   return(0);
313: }

315: /*
316:  MatGetColumnIJ_SeqAIJ_Color() and MatRestoreColumnIJ_SeqAIJ_Color() are customized from
317:  MatGetColumnIJ_SeqAIJ() and MatRestoreColumnIJ_SeqAIJ() by adding an output
318:  spidx[], index of a->a, to be used in MatTransposeColoringCreate_SeqAIJ() and MatFDColoringCreate_SeqXAIJ()
319: */
320: PetscErrorCode MatGetColumnIJ_SeqAIJ_Color(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *nn,const PetscInt *ia[],const PetscInt *ja[],PetscInt *spidx[],PetscBool  *done)
321: {
322:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
324:   PetscInt       i,*collengths,*cia,*cja,n = A->cmap->n,m = A->rmap->n;
325:   PetscInt       nz = a->i[m],row,mr,col,tmp;
326:   PetscInt       *cspidx;
327:   const PetscInt *jj;

330:   *nn = n;
331:   if (!ia) return(0);

333:   PetscCalloc1(n,&collengths);
334:   PetscMalloc1(n+1,&cia);
335:   PetscMalloc1(nz,&cja);
336:   PetscMalloc1(nz,&cspidx);
337:   jj   = a->j;
338:   for (i=0; i<nz; i++) {
339:     collengths[jj[i]]++;
340:   }
341:   cia[0] = oshift;
342:   for (i=0; i<n; i++) {
343:     cia[i+1] = cia[i] + collengths[i];
344:   }
345:   PetscArrayzero(collengths,n);
346:   jj   = a->j;
347:   for (row=0; row<m; row++) {
348:     mr = a->i[row+1] - a->i[row];
349:     for (i=0; i<mr; i++) {
350:       col         = *jj++;
351:       tmp         = cia[col] + collengths[col]++ - oshift;
352:       cspidx[tmp] = a->i[row] + i; /* index of a->j */
353:       cja[tmp]    = row + oshift;
354:     }
355:   }
356:   PetscFree(collengths);
357:   *ia    = cia;
358:   *ja    = cja;
359:   *spidx = cspidx;
360:   return(0);
361: }

363: PetscErrorCode MatRestoreColumnIJ_SeqAIJ_Color(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *n,const PetscInt *ia[],const PetscInt *ja[],PetscInt *spidx[],PetscBool  *done)
364: {

368:   MatRestoreColumnIJ_SeqAIJ(A,oshift,symmetric,inodecompressed,n,ia,ja,done);
369:   PetscFree(*spidx);
370:   return(0);
371: }

373: PetscErrorCode MatSetValuesRow_SeqAIJ(Mat A,PetscInt row,const PetscScalar v[])
374: {
375:   Mat_SeqAIJ     *a  = (Mat_SeqAIJ*)A->data;
376:   PetscInt       *ai = a->i;

380:   PetscArraycpy(a->a+ai[row],v,ai[row+1]-ai[row]);
381: #if defined(PETSC_HAVE_DEVICE)
382:   if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED && ai[row+1]-ai[row]) A->offloadmask = PETSC_OFFLOAD_CPU;
383: #endif
384:   return(0);
385: }

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

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

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

397: */

399: #include <petsc/private/isimpl.h>
400: PetscErrorCode MatSeqAIJSetValuesLocalFast(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
401: {
402:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
403:   PetscInt       low,high,t,row,nrow,i,col,l;
404:   const PetscInt *rp,*ai = a->i,*ailen = a->ilen,*aj = a->j;
405:   PetscInt       lastcol = -1;
406:   MatScalar      *ap,value,*aa = a->a;
407:   const PetscInt *ridx = A->rmap->mapping->indices,*cidx = A->cmap->mapping->indices;

409:   row  = ridx[im[0]];
410:   rp   = aj + ai[row];
411:   ap   = aa + ai[row];
412:   nrow = ailen[row];
413:   low  = 0;
414:   high = nrow;
415:   for (l=0; l<n; l++) { /* loop over added columns */
416:     col = cidx[in[l]];
417:     value = v[l];

419:     if (col <= lastcol) low = 0;
420:     else high = nrow;
421:     lastcol = col;
422:     while (high-low > 5) {
423:       t = (low+high)/2;
424:       if (rp[t] > col) high = t;
425:       else low = t;
426:     }
427:     for (i=low; i<high; i++) {
428:       if (rp[i] == col) {
429:         ap[i] += value;
430:         low = i + 1;
431:         break;
432:       }
433:     }
434:   }
435: #if defined(PETSC_HAVE_DEVICE)
436:   if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED && m && n) A->offloadmask = PETSC_OFFLOAD_CPU;
437: #endif
438:   return 0;
439: }

441: PetscErrorCode MatSetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
442: {
443:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
444:   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
445:   PetscInt       *imax = a->imax,*ai = a->i,*ailen = a->ilen;
447:   PetscInt       *aj = a->j,nonew = a->nonew,lastcol = -1;
448:   MatScalar      *ap=NULL,value=0.0,*aa;
449:   PetscBool      ignorezeroentries = a->ignorezeroentries;
450:   PetscBool      roworiented       = a->roworiented;
451: #if defined(PETSC_HAVE_DEVICE)
452:   PetscBool      inserted          = PETSC_FALSE;
453: #endif

456: #if defined(PETSC_HAVE_DEVICE)
457:   if (A->offloadmask == PETSC_OFFLOAD_GPU) {
458:     const PetscScalar *dummy;
459:     MatSeqAIJGetArrayRead(A,&dummy);
460:     MatSeqAIJRestoreArrayRead(A,&dummy);
461:   }
462: #endif
463:   aa = a->a;
464:   for (k=0; k<m; k++) { /* loop over added rows */
465:     row = im[k];
466:     if (row < 0) continue;
467:     if (PetscUnlikelyDebug(row >= A->rmap->n)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->rmap->n-1);
468:     rp   = aj + ai[row];
469:     if (!A->structure_only) ap = aa + ai[row];
470:     rmax = imax[row]; nrow = ailen[row];
471:     low  = 0;
472:     high = nrow;
473:     for (l=0; l<n; l++) { /* loop over added columns */
474:       if (in[l] < 0) continue;
475:       if (PetscUnlikelyDebug(in[l] >= A->cmap->n)) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->cmap->n-1);
476:       col = in[l];
477:       if (v && !A->structure_only) value = roworiented ? v[l + k*n] : v[k + l*m];
478:       if (!A->structure_only && value == 0.0 && ignorezeroentries && is == ADD_VALUES && row != col) continue;

480:       if (col <= lastcol) low = 0;
481:       else high = nrow;
482:       lastcol = col;
483:       while (high-low > 5) {
484:         t = (low+high)/2;
485:         if (rp[t] > col) high = t;
486:         else low = t;
487:       }
488:       for (i=low; i<high; i++) {
489:         if (rp[i] > col) break;
490:         if (rp[i] == col) {
491:           if (!A->structure_only) {
492:             if (is == ADD_VALUES) {
493:               ap[i] += value;
494:               (void)PetscLogFlops(1.0);
495:             }
496:             else ap[i] = value;
497: #if defined(PETSC_HAVE_DEVICE)
498:             inserted = PETSC_TRUE;
499: #endif
500:           }
501:           low = i + 1;
502:           goto noinsert;
503:         }
504:       }
505:       if (value == 0.0 && ignorezeroentries && row != col) goto noinsert;
506:       if (nonew == 1) goto noinsert;
507:       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%D,%D) in the matrix",row,col);
508:       if (A->structure_only) {
509:         MatSeqXAIJReallocateAIJ_structure_only(A,A->rmap->n,1,nrow,row,col,rmax,ai,aj,rp,imax,nonew,MatScalar);
510:       } else {
511:         MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
512:       }
513:       N = nrow++ - 1; a->nz++; high++;
514:       /* shift up all the later entries in this row */
515:       PetscArraymove(rp+i+1,rp+i,N-i+1);
516:       rp[i] = col;
517:       if (!A->structure_only){
518:         PetscArraymove(ap+i+1,ap+i,N-i+1);
519:         ap[i] = value;
520:       }
521:       low = i + 1;
522:       A->nonzerostate++;
523: #if defined(PETSC_HAVE_DEVICE)
524:       inserted = PETSC_TRUE;
525: #endif
526: noinsert:;
527:     }
528:     ailen[row] = nrow;
529:   }
530: #if defined(PETSC_HAVE_DEVICE)
531:   if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED && inserted) A->offloadmask = PETSC_OFFLOAD_CPU;
532: #endif
533:   return(0);
534: }


537: PetscErrorCode MatSetValues_SeqAIJ_SortedFullNoPreallocation(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
538: {
539:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
540:   PetscInt       *rp,k,row;
541:   PetscInt       *ai = a->i;
543:   PetscInt       *aj = a->j;
544:   MatScalar      *aa = a->a,*ap;

547:   if (A->was_assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot call on assembled matrix.");
548:   if (m*n+a->nz > a->maxnz) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Number of entries in matrix will be larger than maximum nonzeros allocated for %D in MatSeqAIJSetTotalPreallocation()",a->maxnz);
549:   for (k=0; k<m; k++) { /* loop over added rows */
550:     row  = im[k];
551:     rp   = aj + ai[row];
552:     ap   = aa + ai[row];

554:     PetscMemcpy(rp,in,n*sizeof(PetscInt));
555:     if (!A->structure_only) {
556:       if (v) {
557:         PetscMemcpy(ap,v,n*sizeof(PetscScalar));
558:         v   += n;
559:       } else {
560:         PetscMemzero(ap,n*sizeof(PetscScalar));
561:       }
562:     }
563:     a->ilen[row] = n;
564:     a->imax[row] = n;
565:     a->i[row+1]  = a->i[row]+n;
566:     a->nz       += n;
567:   }
568: #if defined(PETSC_HAVE_DEVICE)
569:   if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED && m && n) A->offloadmask = PETSC_OFFLOAD_CPU;
570: #endif
571:   return(0);
572: }

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

577:   Input Parameters:
578: +  A - the SeqAIJ matrix
579: -  nztotal - bound on the number of nonzeros

581:   Level: advanced

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

588: .seealso: MatSetOption(), MAT_SORTED_FULL, MatSetValues(), MatSeqAIJSetPreallocation()
589: @*/

591: PetscErrorCode MatSeqAIJSetTotalPreallocation(Mat A,PetscInt nztotal)
592: {
594:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;

597:   PetscLayoutSetUp(A->rmap);
598:   PetscLayoutSetUp(A->cmap);
599:   a->maxnz  = nztotal;
600:   if (!a->imax) {
601:     PetscMalloc1(A->rmap->n,&a->imax);
602:     PetscLogObjectMemory((PetscObject)A,A->rmap->n*sizeof(PetscInt));
603:   }
604:   if (!a->ilen) {
605:     PetscMalloc1(A->rmap->n,&a->ilen);
606:     PetscLogObjectMemory((PetscObject)A,A->rmap->n*sizeof(PetscInt));
607:   } else {
608:     PetscMemzero(a->ilen,A->rmap->n*sizeof(PetscInt));
609:   }

611:   /* allocate the matrix space */
612:   if (A->structure_only) {
613:     PetscMalloc1(nztotal,&a->j);
614:     PetscMalloc1(A->rmap->n+1,&a->i);
615:     PetscLogObjectMemory((PetscObject)A,(A->rmap->n+1)*sizeof(PetscInt)+nztotal*sizeof(PetscInt));
616:   } else {
617:     PetscMalloc3(nztotal,&a->a,nztotal,&a->j,A->rmap->n+1,&a->i);
618:     PetscLogObjectMemory((PetscObject)A,(A->rmap->n+1)*sizeof(PetscInt)+nztotal*(sizeof(PetscScalar)+sizeof(PetscInt)));
619:   }
620:   a->i[0] = 0;
621:   if (A->structure_only) {
622:     a->singlemalloc = PETSC_FALSE;
623:     a->free_a       = PETSC_FALSE;
624:   } else {
625:     a->singlemalloc = PETSC_TRUE;
626:     a->free_a       = PETSC_TRUE;
627:   }
628:   a->free_ij         = PETSC_TRUE;
629:   A->ops->setvalues = MatSetValues_SeqAIJ_SortedFullNoPreallocation;
630:   A->preallocated   = PETSC_TRUE;
631:   return(0);
632: }

634: PetscErrorCode MatSetValues_SeqAIJ_SortedFull(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
635: {
636:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
637:   PetscInt       *rp,k,row;
638:   PetscInt       *ai = a->i,*ailen = a->ilen;
640:   PetscInt       *aj = a->j;
641:   MatScalar      *aa = a->a,*ap;

644:   for (k=0; k<m; k++) { /* loop over added rows */
645:     row  = im[k];
646:     if (PetscUnlikelyDebug(n > a->imax[row])) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Preallocation for row %D does not match number of columns provided",n);
647:     rp   = aj + ai[row];
648:     ap   = aa + ai[row];
649:     if (!A->was_assembled) {
650:       PetscMemcpy(rp,in,n*sizeof(PetscInt));
651:     }
652:     if (!A->structure_only) {
653:       if (v) {
654:         PetscMemcpy(ap,v,n*sizeof(PetscScalar));
655:         v   += n;
656:       } else {
657:         PetscMemzero(ap,n*sizeof(PetscScalar));
658:       }
659:     }
660:     ailen[row] = n;
661:     a->nz      += n;
662:   }
663: #if defined(PETSC_HAVE_DEVICE)
664:   if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED && m && n) A->offloadmask = PETSC_OFFLOAD_CPU;
665: #endif
666:   return(0);
667: }


670: PetscErrorCode MatGetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
671: {
672:   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
673:   PetscInt   *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
674:   PetscInt   *ai = a->i,*ailen = a->ilen;
675:   MatScalar  *ap,*aa = a->a;

678:   for (k=0; k<m; k++) { /* loop over rows */
679:     row = im[k];
680:     if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */
681:     if (row >= A->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->rmap->n-1);
682:     rp   = aj + ai[row]; ap = aa + ai[row];
683:     nrow = ailen[row];
684:     for (l=0; l<n; l++) { /* loop over columns */
685:       if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */
686:       if (in[l] >= A->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->cmap->n-1);
687:       col  = in[l];
688:       high = nrow; low = 0; /* assume unsorted */
689:       while (high-low > 5) {
690:         t = (low+high)/2;
691:         if (rp[t] > col) high = t;
692:         else low = t;
693:       }
694:       for (i=low; i<high; i++) {
695:         if (rp[i] > col) break;
696:         if (rp[i] == col) {
697:           *v++ = ap[i];
698:           goto finished;
699:         }
700:       }
701:       *v++ = 0.0;
702: finished:;
703:     }
704:   }
705:   return(0);
706: }

708: PetscErrorCode MatView_SeqAIJ_Binary(Mat mat,PetscViewer viewer)
709: {
710:   Mat_SeqAIJ        *A = (Mat_SeqAIJ*)mat->data;
711:   const PetscScalar *av;
712:   PetscInt          header[4],M,N,m,nz,i;
713:   PetscInt          *rowlens;
714:   PetscErrorCode    ierr;

717:   PetscViewerSetUp(viewer);

719:   M  = mat->rmap->N;
720:   N  = mat->cmap->N;
721:   m  = mat->rmap->n;
722:   nz = A->nz;

724:   /* write matrix header */
725:   header[0] = MAT_FILE_CLASSID;
726:   header[1] = M; header[2] = N; header[3] = nz;
727:   PetscViewerBinaryWrite(viewer,header,4,PETSC_INT);

729:   /* fill in and store row lengths */
730:   PetscMalloc1(m,&rowlens);
731:   for (i=0; i<m; i++) rowlens[i] = A->i[i+1] - A->i[i];
732:   PetscViewerBinaryWrite(viewer,rowlens,m,PETSC_INT);
733:   PetscFree(rowlens);
734:   /* store column indices */
735:   PetscViewerBinaryWrite(viewer,A->j,nz,PETSC_INT);
736:   /* store nonzero values */
737:   MatSeqAIJGetArrayRead(mat,&av);
738:   PetscViewerBinaryWrite(viewer,av,nz,PETSC_SCALAR);
739:   MatSeqAIJRestoreArrayRead(mat,&av);

741:   /* write block size option to the viewer's .info file */
742:   MatView_Binary_BlockSizes(mat,viewer);
743:   return(0);
744: }

746: static PetscErrorCode MatView_SeqAIJ_ASCII_structonly(Mat A,PetscViewer viewer)
747: {
749:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
750:   PetscInt       i,k,m=A->rmap->N;

753:   PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
754:   for (i=0; i<m; i++) {
755:     PetscViewerASCIIPrintf(viewer,"row %D:",i);
756:     for (k=a->i[i]; k<a->i[i+1]; k++) {
757:       PetscViewerASCIIPrintf(viewer," (%D) ",a->j[k]);
758:     }
759:     PetscViewerASCIIPrintf(viewer,"\n");
760:   }
761:   PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
762:   return(0);
763: }

765: extern PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);

767: PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer)
768: {
769:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
770:   const PetscScalar *av;
771:   PetscErrorCode    ierr;
772:   PetscInt          i,j,m = A->rmap->n;
773:   const char        *name;
774:   PetscViewerFormat format;

777:   if (A->structure_only) {
778:     MatView_SeqAIJ_ASCII_structonly(A,viewer);
779:     return(0);
780:   }

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

785:   /* trigger copy to CPU if needed */
786:   MatSeqAIJGetArrayRead(A,&av);
787:   MatSeqAIJRestoreArrayRead(A,&av);
788:   if (format == PETSC_VIEWER_ASCII_MATLAB) {
789:     PetscInt nofinalvalue = 0;
790:     if (m && ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->cmap->n-1))) {
791:       /* Need a dummy value to ensure the dimension of the matrix. */
792:       nofinalvalue = 1;
793:     }
794:     PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
795:     PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->cmap->n);
796:     PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);
797: #if defined(PETSC_USE_COMPLEX)
798:     PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,4);\n",a->nz+nofinalvalue);
799: #else
800:     PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);
801: #endif
802:     PetscViewerASCIIPrintf(viewer,"zzz = [\n");

804:     for (i=0; i<m; i++) {
805:       for (j=a->i[i]; j<a->i[i+1]; j++) {
806: #if defined(PETSC_USE_COMPLEX)
807:         PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e %18.16e\n",i+1,a->j[j]+1,(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));
808: #else
809:         PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",i+1,a->j[j]+1,(double)a->a[j]);
810: #endif
811:       }
812:     }
813:     if (nofinalvalue) {
814: #if defined(PETSC_USE_COMPLEX)
815:       PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e %18.16e\n",m,A->cmap->n,0.,0.);
816: #else
817:       PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",m,A->cmap->n,0.0);
818: #endif
819:     }
820:     PetscObjectGetName((PetscObject)A,&name);
821:     PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);
822:     PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
823:   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
824:     PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
825:     for (i=0; i<m; i++) {
826:       PetscViewerASCIIPrintf(viewer,"row %D:",i);
827:       for (j=a->i[i]; j<a->i[i+1]; j++) {
828: #if defined(PETSC_USE_COMPLEX)
829:         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
830:           PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));
831:         } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
832:           PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)-PetscImaginaryPart(a->a[j]));
833:         } else if (PetscRealPart(a->a[j]) != 0.0) {
834:           PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));
835:         }
836: #else
837:         if (a->a[j] != 0.0) {PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);}
838: #endif
839:       }
840:       PetscViewerASCIIPrintf(viewer,"\n");
841:     }
842:     PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
843:   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
844:     PetscInt nzd=0,fshift=1,*sptr;
845:     PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
846:     PetscMalloc1(m+1,&sptr);
847:     for (i=0; i<m; i++) {
848:       sptr[i] = nzd+1;
849:       for (j=a->i[i]; j<a->i[i+1]; j++) {
850:         if (a->j[j] >= i) {
851: #if defined(PETSC_USE_COMPLEX)
852:           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
853: #else
854:           if (a->a[j] != 0.0) nzd++;
855: #endif
856:         }
857:       }
858:     }
859:     sptr[m] = nzd+1;
860:     PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);
861:     for (i=0; i<m+1; i+=6) {
862:       if (i+4<m) {
863:         PetscViewerASCIIPrintf(viewer," %D %D %D %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3],sptr[i+4],sptr[i+5]);
864:       } else if (i+3<m) {
865:         PetscViewerASCIIPrintf(viewer," %D %D %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3],sptr[i+4]);
866:       } else if (i+2<m) {
867:         PetscViewerASCIIPrintf(viewer," %D %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3]);
868:       } else if (i+1<m) {
869:         PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);
870:       } else if (i<m) {
871:         PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);
872:       } else {
873:         PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);
874:       }
875:     }
876:     PetscViewerASCIIPrintf(viewer,"\n");
877:     PetscFree(sptr);
878:     for (i=0; i<m; i++) {
879:       for (j=a->i[i]; j<a->i[i+1]; j++) {
880:         if (a->j[j] >= i) {PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);}
881:       }
882:       PetscViewerASCIIPrintf(viewer,"\n");
883:     }
884:     PetscViewerASCIIPrintf(viewer,"\n");
885:     for (i=0; i<m; i++) {
886:       for (j=a->i[i]; j<a->i[i+1]; j++) {
887:         if (a->j[j] >= i) {
888: #if defined(PETSC_USE_COMPLEX)
889:           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) {
890:             PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));
891:           }
892: #else
893:           if (a->a[j] != 0.0) {PetscViewerASCIIPrintf(viewer," %18.16e ",(double)a->a[j]);}
894: #endif
895:         }
896:       }
897:       PetscViewerASCIIPrintf(viewer,"\n");
898:     }
899:     PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
900:   } else if (format == PETSC_VIEWER_ASCII_DENSE) {
901:     PetscInt    cnt = 0,jcnt;
902:     PetscScalar value;
903: #if defined(PETSC_USE_COMPLEX)
904:     PetscBool   realonly = PETSC_TRUE;

906:     for (i=0; i<a->i[m]; i++) {
907:       if (PetscImaginaryPart(a->a[i]) != 0.0) {
908:         realonly = PETSC_FALSE;
909:         break;
910:       }
911:     }
912: #endif

914:     PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
915:     for (i=0; i<m; i++) {
916:       jcnt = 0;
917:       for (j=0; j<A->cmap->n; j++) {
918:         if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
919:           value = a->a[cnt++];
920:           jcnt++;
921:         } else {
922:           value = 0.0;
923:         }
924: #if defined(PETSC_USE_COMPLEX)
925:         if (realonly) {
926:           PetscViewerASCIIPrintf(viewer," %7.5e ",(double)PetscRealPart(value));
927:         } else {
928:           PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",(double)PetscRealPart(value),(double)PetscImaginaryPart(value));
929:         }
930: #else
931:         PetscViewerASCIIPrintf(viewer," %7.5e ",(double)value);
932: #endif
933:       }
934:       PetscViewerASCIIPrintf(viewer,"\n");
935:     }
936:     PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
937:   } else if (format == PETSC_VIEWER_ASCII_MATRIXMARKET) {
938:     PetscInt fshift=1;
939:     PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
940: #if defined(PETSC_USE_COMPLEX)
941:     PetscViewerASCIIPrintf(viewer,"%%%%MatrixMarket matrix coordinate complex general\n");
942: #else
943:     PetscViewerASCIIPrintf(viewer,"%%%%MatrixMarket matrix coordinate real general\n");
944: #endif
945:     PetscViewerASCIIPrintf(viewer,"%D %D %D\n", m, A->cmap->n, a->nz);
946:     for (i=0; i<m; i++) {
947:       for (j=a->i[i]; j<a->i[i+1]; j++) {
948: #if defined(PETSC_USE_COMPLEX)
949:         PetscViewerASCIIPrintf(viewer,"%D %D %g %g\n", i+fshift,a->j[j]+fshift,(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));
950: #else
951:         PetscViewerASCIIPrintf(viewer,"%D %D %g\n", i+fshift, a->j[j]+fshift, (double)a->a[j]);
952: #endif
953:       }
954:     }
955:     PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
956:   } else {
957:     PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
958:     if (A->factortype) {
959:       for (i=0; i<m; i++) {
960:         PetscViewerASCIIPrintf(viewer,"row %D:",i);
961:         /* L part */
962:         for (j=a->i[i]; j<a->i[i+1]; j++) {
963: #if defined(PETSC_USE_COMPLEX)
964:           if (PetscImaginaryPart(a->a[j]) > 0.0) {
965:             PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));
966:           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
967:             PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)(-PetscImaginaryPart(a->a[j])));
968:           } else {
969:             PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));
970:           }
971: #else
972:           PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);
973: #endif
974:         }
975:         /* diagonal */
976:         j = a->diag[i];
977: #if defined(PETSC_USE_COMPLEX)
978:         if (PetscImaginaryPart(a->a[j]) > 0.0) {
979:           PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(1.0/a->a[j]),(double)PetscImaginaryPart(1.0/a->a[j]));
980:         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
981:           PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(1.0/a->a[j]),(double)(-PetscImaginaryPart(1.0/a->a[j])));
982:         } else {
983:           PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(1.0/a->a[j]));
984:         }
985: #else
986:         PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)(1.0/a->a[j]));
987: #endif

989:         /* U part */
990:         for (j=a->diag[i+1]+1; j<a->diag[i]; j++) {
991: #if defined(PETSC_USE_COMPLEX)
992:           if (PetscImaginaryPart(a->a[j]) > 0.0) {
993:             PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));
994:           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
995:             PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)(-PetscImaginaryPart(a->a[j])));
996:           } else {
997:             PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));
998:           }
999: #else
1000:           PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);
1001: #endif
1002:         }
1003:         PetscViewerASCIIPrintf(viewer,"\n");
1004:       }
1005:     } else {
1006:       for (i=0; i<m; i++) {
1007:         PetscViewerASCIIPrintf(viewer,"row %D:",i);
1008:         for (j=a->i[i]; j<a->i[i+1]; j++) {
1009: #if defined(PETSC_USE_COMPLEX)
1010:           if (PetscImaginaryPart(a->a[j]) > 0.0) {
1011:             PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));
1012:           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
1013:             PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)-PetscImaginaryPart(a->a[j]));
1014:           } else {
1015:             PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));
1016:           }
1017: #else
1018:           PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);
1019: #endif
1020:         }
1021:         PetscViewerASCIIPrintf(viewer,"\n");
1022:       }
1023:     }
1024:     PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
1025:   }
1026:   PetscViewerFlush(viewer);
1027:   return(0);
1028: }

1030: #include <petscdraw.h>
1031: PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
1032: {
1033:   Mat               A  = (Mat) Aa;
1034:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1035:   PetscErrorCode    ierr;
1036:   PetscInt          i,j,m = A->rmap->n;
1037:   int               color;
1038:   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r;
1039:   PetscViewer       viewer;
1040:   PetscViewerFormat format;

1043:   PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);
1044:   PetscViewerGetFormat(viewer,&format);
1045:   PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);

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

1049:   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
1050:     PetscDrawCollectiveBegin(draw);
1051:     /* Blue for negative, Cyan for zero and  Red for positive */
1052:     color = PETSC_DRAW_BLUE;
1053:     for (i=0; i<m; i++) {
1054:       y_l = m - i - 1.0; y_r = y_l + 1.0;
1055:       for (j=a->i[i]; j<a->i[i+1]; j++) {
1056:         x_l = a->j[j]; x_r = x_l + 1.0;
1057:         if (PetscRealPart(a->a[j]) >=  0.) continue;
1058:         PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);
1059:       }
1060:     }
1061:     color = PETSC_DRAW_CYAN;
1062:     for (i=0; i<m; i++) {
1063:       y_l = m - i - 1.0; y_r = y_l + 1.0;
1064:       for (j=a->i[i]; j<a->i[i+1]; j++) {
1065:         x_l = a->j[j]; x_r = x_l + 1.0;
1066:         if (a->a[j] !=  0.) continue;
1067:         PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);
1068:       }
1069:     }
1070:     color = PETSC_DRAW_RED;
1071:     for (i=0; i<m; i++) {
1072:       y_l = m - i - 1.0; y_r = y_l + 1.0;
1073:       for (j=a->i[i]; j<a->i[i+1]; j++) {
1074:         x_l = a->j[j]; x_r = x_l + 1.0;
1075:         if (PetscRealPart(a->a[j]) <=  0.) continue;
1076:         PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);
1077:       }
1078:     }
1079:     PetscDrawCollectiveEnd(draw);
1080:   } else {
1081:     /* use contour shading to indicate magnitude of values */
1082:     /* first determine max of all nonzero values */
1083:     PetscReal minv = 0.0, maxv = 0.0;
1084:     PetscInt  nz = a->nz, count = 0;
1085:     PetscDraw popup;

1087:     for (i=0; i<nz; i++) {
1088:       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
1089:     }
1090:     if (minv >= maxv) maxv = minv + PETSC_SMALL;
1091:     PetscDrawGetPopup(draw,&popup);
1092:     PetscDrawScalePopup(popup,minv,maxv);

1094:     PetscDrawCollectiveBegin(draw);
1095:     for (i=0; i<m; i++) {
1096:       y_l = m - i - 1.0;
1097:       y_r = y_l + 1.0;
1098:       for (j=a->i[i]; j<a->i[i+1]; j++) {
1099:         x_l = a->j[j];
1100:         x_r = x_l + 1.0;
1101:         color = PetscDrawRealToColor(PetscAbsScalar(a->a[count]),minv,maxv);
1102:         PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);
1103:         count++;
1104:       }
1105:     }
1106:     PetscDrawCollectiveEnd(draw);
1107:   }
1108:   return(0);
1109: }

1111: #include <petscdraw.h>
1112: PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
1113: {
1115:   PetscDraw      draw;
1116:   PetscReal      xr,yr,xl,yl,h,w;
1117:   PetscBool      isnull;

1120:   PetscViewerDrawGetDraw(viewer,0,&draw);
1121:   PetscDrawIsNull(draw,&isnull);
1122:   if (isnull) return(0);

1124:   xr   = A->cmap->n; yr  = A->rmap->n; h = yr/10.0; w = xr/10.0;
1125:   xr  += w;          yr += h;         xl = -w;     yl = -h;
1126:   PetscDrawSetCoordinates(draw,xl,yl,xr,yr);
1127:   PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);
1128:   PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);
1129:   PetscObjectCompose((PetscObject)A,"Zoomviewer",NULL);
1130:   PetscDrawSave(draw);
1131:   return(0);
1132: }

1134: PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer)
1135: {
1137:   PetscBool      iascii,isbinary,isdraw;

1140:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
1141:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
1142:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
1143:   if (iascii) {
1144:     MatView_SeqAIJ_ASCII(A,viewer);
1145:   } else if (isbinary) {
1146:     MatView_SeqAIJ_Binary(A,viewer);
1147:   } else if (isdraw) {
1148:     MatView_SeqAIJ_Draw(A,viewer);
1149:   }
1150:   MatView_SeqAIJ_Inode(A,viewer);
1151:   return(0);
1152: }

1154: PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
1155: {
1156:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1158:   PetscInt       fshift = 0,i,*ai = a->i,*aj = a->j,*imax = a->imax;
1159:   PetscInt       m      = A->rmap->n,*ip,N,*ailen = a->ilen,rmax = 0;
1160:   MatScalar      *aa    = a->a,*ap;
1161:   PetscReal      ratio  = 0.6;

1164:   if (mode == MAT_FLUSH_ASSEMBLY) return(0);
1165:   MatSeqAIJInvalidateDiagonal(A);
1166:   if (A->was_assembled && A->ass_nonzerostate == A->nonzerostate) {
1167:     /* we need to respect users asking to use or not the inodes routine in between matrix assemblies */
1168:     MatAssemblyEnd_SeqAIJ_Inode(A,mode);
1169:     return(0);
1170:   }

1172:   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
1173:   for (i=1; i<m; i++) {
1174:     /* move each row back by the amount of empty slots (fshift) before it*/
1175:     fshift += imax[i-1] - ailen[i-1];
1176:     rmax    = PetscMax(rmax,ailen[i]);
1177:     if (fshift) {
1178:       ip = aj + ai[i];
1179:       ap = aa + ai[i];
1180:       N  = ailen[i];
1181:       PetscArraymove(ip-fshift,ip,N);
1182:       if (!A->structure_only) {
1183:         PetscArraymove(ap-fshift,ap,N);
1184:       }
1185:     }
1186:     ai[i] = ai[i-1] + ailen[i-1];
1187:   }
1188:   if (m) {
1189:     fshift += imax[m-1] - ailen[m-1];
1190:     ai[m]   = ai[m-1] + ailen[m-1];
1191:   }

1193:   /* reset ilen and imax for each row */
1194:   a->nonzerorowcnt = 0;
1195:   if (A->structure_only) {
1196:     PetscFree(a->imax);
1197:     PetscFree(a->ilen);
1198:   } else { /* !A->structure_only */
1199:     for (i=0; i<m; i++) {
1200:       ailen[i] = imax[i] = ai[i+1] - ai[i];
1201:       a->nonzerorowcnt += ((ai[i+1] - ai[i]) > 0);
1202:     }
1203:   }
1204:   a->nz = ai[m];
1205:   if (fshift && a->nounused == -1) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_PLIB, "Unused space detected in matrix: %D X %D, %D unneeded", m, A->cmap->n, fshift);

1207:   MatMarkDiagonal_SeqAIJ(A);
1208:   PetscInfo4(A,"Matrix size: %D X %D; storage space: %D unneeded,%D used\n",m,A->cmap->n,fshift,a->nz);
1209:   PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);
1210:   PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);

1212:   A->info.mallocs    += a->reallocs;
1213:   a->reallocs         = 0;
1214:   A->info.nz_unneeded = (PetscReal)fshift;
1215:   a->rmax             = rmax;

1217:   if (!A->structure_only) {
1218:     MatCheckCompressedRow(A,a->nonzerorowcnt,&a->compressedrow,a->i,m,ratio);
1219:   }
1220:   MatAssemblyEnd_SeqAIJ_Inode(A,mode);
1221:   return(0);
1222: }

1224: PetscErrorCode MatRealPart_SeqAIJ(Mat A)
1225: {
1226:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1227:   PetscInt       i,nz = a->nz;
1228:   MatScalar      *aa;

1232:   MatSeqAIJGetArray(A,&aa);
1233:   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
1234:   MatSeqAIJRestoreArray(A,&aa);
1235:   MatSeqAIJInvalidateDiagonal(A);
1236: #if defined(PETSC_HAVE_DEVICE)
1237:   if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) A->offloadmask = PETSC_OFFLOAD_CPU;
1238: #endif
1239:   return(0);
1240: }

1242: PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
1243: {
1244:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1245:   PetscInt       i,nz = a->nz;
1246:   MatScalar      *aa;

1250:   MatSeqAIJGetArray(A,&aa);
1251:   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
1252:   MatSeqAIJRestoreArray(A,&aa);
1253:   MatSeqAIJInvalidateDiagonal(A);
1254: #if defined(PETSC_HAVE_DEVICE)
1255:   if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) A->offloadmask = PETSC_OFFLOAD_CPU;
1256: #endif
1257:   return(0);
1258: }

1260: PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
1261: {
1262:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;

1266:   PetscArrayzero(a->a,a->i[A->rmap->n]);
1267:   MatSeqAIJInvalidateDiagonal(A);
1268: #if defined(PETSC_HAVE_DEVICE)
1269:   if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) A->offloadmask = PETSC_OFFLOAD_CPU;
1270: #endif
1271:   return(0);
1272: }

1274: PetscErrorCode MatDestroy_SeqAIJ(Mat A)
1275: {
1276:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;

1280: #if defined(PETSC_USE_LOG)
1281:   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz);
1282: #endif
1283:   MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);
1284:   ISDestroy(&a->row);
1285:   ISDestroy(&a->col);
1286:   PetscFree(a->diag);
1287:   PetscFree(a->ibdiag);
1288:   PetscFree(a->imax);
1289:   PetscFree(a->ilen);
1290:   PetscFree(a->ipre);
1291:   PetscFree3(a->idiag,a->mdiag,a->ssor_work);
1292:   PetscFree(a->solve_work);
1293:   ISDestroy(&a->icol);
1294:   PetscFree(a->saved_values);
1295:   PetscFree2(a->compressedrow.i,a->compressedrow.rindex);

1297:   MatDestroy_SeqAIJ_Inode(A);
1298:   PetscFree(A->data);

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

1307:   PetscObjectChangeTypeName((PetscObject)A,NULL);
1308:   PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetColumnIndices_C",NULL);
1309:   PetscObjectComposeFunction((PetscObject)A,"MatStoreValues_C",NULL);
1310:   PetscObjectComposeFunction((PetscObject)A,"MatRetrieveValues_C",NULL);
1311:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqsbaij_C",NULL);
1312:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqbaij_C",NULL);
1313:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqaijperm_C",NULL);
1314: #if defined(PETSC_HAVE_CUDA)
1315:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqaijcusparse_C",NULL);
1316:   PetscObjectComposeFunction((PetscObject)A,"MatProductSetFromOptions_seqaijcusparse_seqaij_C",NULL);
1317:   PetscObjectComposeFunction((PetscObject)A,"MatProductSetFromOptions_seqaij_seqaijcusparse_C",NULL);
1318: #endif
1319: #if defined(PETSC_HAVE_KOKKOS_KERNELS)
1320:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqaijkokkos_C",NULL);
1321: #endif
1322:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqaijcrl_C",NULL);
1323: #if defined(PETSC_HAVE_ELEMENTAL)
1324:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_elemental_C",NULL);
1325: #endif
1326: #if defined(PETSC_HAVE_SCALAPACK)
1327:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_scalapack_C",NULL);
1328: #endif
1329: #if defined(PETSC_HAVE_HYPRE)
1330:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_hypre_C",NULL);
1331:   PetscObjectComposeFunction((PetscObject)A,"MatProductSetFromOptions_transpose_seqaij_seqaij_C",NULL);
1332: #endif
1333:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqdense_C",NULL);
1334:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqsell_C",NULL);
1335:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_is_C",NULL);
1336:   PetscObjectComposeFunction((PetscObject)A,"MatIsTranspose_C",NULL);
1337:   PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocation_C",NULL);
1338:   PetscObjectComposeFunction((PetscObject)A,"MatResetPreallocation_C",NULL);
1339:   PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C",NULL);
1340:   PetscObjectComposeFunction((PetscObject)A,"MatReorderForNonzeroDiagonal_C",NULL);
1341:   PetscObjectComposeFunction((PetscObject)A,"MatProductSetFromOptions_is_seqaij_C",NULL);
1342:   PetscObjectComposeFunction((PetscObject)A,"MatProductSetFromOptions_seqdense_seqaij_C",NULL);
1343:   PetscObjectComposeFunction((PetscObject)A,"MatProductSetFromOptions_seqaij_seqaij_C",NULL);
1344:   return(0);
1345: }

1347: PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscBool flg)
1348: {
1349:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;

1353:   switch (op) {
1354:   case MAT_ROW_ORIENTED:
1355:     a->roworiented = flg;
1356:     break;
1357:   case MAT_KEEP_NONZERO_PATTERN:
1358:     a->keepnonzeropattern = flg;
1359:     break;
1360:   case MAT_NEW_NONZERO_LOCATIONS:
1361:     a->nonew = (flg ? 0 : 1);
1362:     break;
1363:   case MAT_NEW_NONZERO_LOCATION_ERR:
1364:     a->nonew = (flg ? -1 : 0);
1365:     break;
1366:   case MAT_NEW_NONZERO_ALLOCATION_ERR:
1367:     a->nonew = (flg ? -2 : 0);
1368:     break;
1369:   case MAT_UNUSED_NONZERO_LOCATION_ERR:
1370:     a->nounused = (flg ? -1 : 0);
1371:     break;
1372:   case MAT_IGNORE_ZERO_ENTRIES:
1373:     a->ignorezeroentries = flg;
1374:     break;
1375:   case MAT_SPD:
1376:   case MAT_SYMMETRIC:
1377:   case MAT_STRUCTURALLY_SYMMETRIC:
1378:   case MAT_HERMITIAN:
1379:   case MAT_SYMMETRY_ETERNAL:
1380:   case MAT_STRUCTURE_ONLY:
1381:     /* These options are handled directly by MatSetOption() */
1382:     break;
1383:   case MAT_FORCE_DIAGONAL_ENTRIES:
1384:   case MAT_IGNORE_OFF_PROC_ENTRIES:
1385:   case MAT_USE_HASH_TABLE:
1386:     PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);
1387:     break;
1388:   case MAT_USE_INODES:
1389:     MatSetOption_SeqAIJ_Inode(A,MAT_USE_INODES,flg);
1390:     break;
1391:   case MAT_SUBMAT_SINGLEIS:
1392:     A->submat_singleis = flg;
1393:     break;
1394:   case MAT_SORTED_FULL:
1395:     if (flg) A->ops->setvalues = MatSetValues_SeqAIJ_SortedFull;
1396:     else     A->ops->setvalues = MatSetValues_SeqAIJ;
1397:     break;
1398:   default:
1399:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
1400:   }
1401:   return(0);
1402: }

1404: PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
1405: {
1406:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1407:   PetscErrorCode    ierr;
1408:   PetscInt          i,j,n,*ai=a->i,*aj=a->j;
1409:   PetscScalar       *x;
1410:   const PetscScalar *aa;

1413:   VecGetLocalSize(v,&n);
1414:   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
1415:   MatSeqAIJGetArrayRead(A,&aa);
1416:   if (A->factortype == MAT_FACTOR_ILU || A->factortype == MAT_FACTOR_LU) {
1417:     PetscInt *diag=a->diag;
1418:     VecGetArrayWrite(v,&x);
1419:     for (i=0; i<n; i++) x[i] = 1.0/aa[diag[i]];
1420:     VecRestoreArrayWrite(v,&x);
1421:     MatSeqAIJRestoreArrayRead(A,&aa);
1422:     return(0);
1423:   }

1425:   VecGetArrayWrite(v,&x);
1426:   for (i=0; i<n; i++) {
1427:     x[i] = 0.0;
1428:     for (j=ai[i]; j<ai[i+1]; j++) {
1429:       if (aj[j] == i) {
1430:         x[i] = aa[j];
1431:         break;
1432:       }
1433:     }
1434:   }
1435:   VecRestoreArrayWrite(v,&x);
1436:   MatSeqAIJRestoreArrayRead(A,&aa);
1437:   return(0);
1438: }

1440: #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
1441: PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
1442: {
1443:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1444:   PetscScalar       *y;
1445:   const PetscScalar *x;
1446:   PetscErrorCode    ierr;
1447:   PetscInt          m = A->rmap->n;
1448: #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1449:   const MatScalar   *v;
1450:   PetscScalar       alpha;
1451:   PetscInt          n,i,j;
1452:   const PetscInt    *idx,*ii,*ridx=NULL;
1453:   Mat_CompressedRow cprow    = a->compressedrow;
1454:   PetscBool         usecprow = cprow.use;
1455: #endif

1458:   if (zz != yy) {VecCopy(zz,yy);}
1459:   VecGetArrayRead(xx,&x);
1460:   VecGetArray(yy,&y);

1462: #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1463:   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
1464: #else
1465:   if (usecprow) {
1466:     m    = cprow.nrows;
1467:     ii   = cprow.i;
1468:     ridx = cprow.rindex;
1469:   } else {
1470:     ii = a->i;
1471:   }
1472:   for (i=0; i<m; i++) {
1473:     idx = a->j + ii[i];
1474:     v   = a->a + ii[i];
1475:     n   = ii[i+1] - ii[i];
1476:     if (usecprow) {
1477:       alpha = x[ridx[i]];
1478:     } else {
1479:       alpha = x[i];
1480:     }
1481:     for (j=0; j<n; j++) y[idx[j]] += alpha*v[j];
1482:   }
1483: #endif
1484:   PetscLogFlops(2.0*a->nz);
1485:   VecRestoreArrayRead(xx,&x);
1486:   VecRestoreArray(yy,&y);
1487:   return(0);
1488: }

1490: PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
1491: {

1495:   VecSet(yy,0.0);
1496:   MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);
1497:   return(0);
1498: }

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

1502: PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
1503: {
1504:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1505:   PetscScalar       *y;
1506:   const PetscScalar *x;
1507:   const MatScalar   *aa;
1508:   PetscErrorCode    ierr;
1509:   PetscInt          m=A->rmap->n;
1510:   const PetscInt    *aj,*ii,*ridx=NULL;
1511:   PetscInt          n,i;
1512:   PetscScalar       sum;
1513:   PetscBool         usecprow=a->compressedrow.use;

1515: #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
1516: #pragma disjoint(*x,*y,*aa)
1517: #endif

1520:   if (a->inode.use && a->inode.checked) {
1521:     MatMult_SeqAIJ_Inode(A,xx,yy);
1522:     return(0);
1523:   }
1524:   VecGetArrayRead(xx,&x);
1525:   VecGetArray(yy,&y);
1526:   ii   = a->i;
1527:   if (usecprow) { /* use compressed row format */
1528:     PetscArrayzero(y,m);
1529:     m    = a->compressedrow.nrows;
1530:     ii   = a->compressedrow.i;
1531:     ridx = a->compressedrow.rindex;
1532:     for (i=0; i<m; i++) {
1533:       n           = ii[i+1] - ii[i];
1534:       aj          = a->j + ii[i];
1535:       aa          = a->a + ii[i];
1536:       sum         = 0.0;
1537:       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1538:       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
1539:       y[*ridx++] = sum;
1540:     }
1541:   } else { /* do not use compressed row format */
1542: #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
1543:     aj   = a->j;
1544:     aa   = a->a;
1545:     fortranmultaij_(&m,x,ii,aj,aa,y);
1546: #else
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         = 0.0;
1552:       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1553:       y[i] = sum;
1554:     }
1555: #endif
1556:   }
1557:   PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);
1558:   VecRestoreArrayRead(xx,&x);
1559:   VecRestoreArray(yy,&y);
1560:   return(0);
1561: }

1563: PetscErrorCode MatMultMax_SeqAIJ(Mat A,Vec xx,Vec yy)
1564: {
1565:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1566:   PetscScalar       *y;
1567:   const PetscScalar *x;
1568:   const MatScalar   *aa;
1569:   PetscErrorCode    ierr;
1570:   PetscInt          m=A->rmap->n;
1571:   const PetscInt    *aj,*ii,*ridx=NULL;
1572:   PetscInt          n,i,nonzerorow=0;
1573:   PetscScalar       sum;
1574:   PetscBool         usecprow=a->compressedrow.use;

1576: #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
1577: #pragma disjoint(*x,*y,*aa)
1578: #endif

1581:   VecGetArrayRead(xx,&x);
1582:   VecGetArray(yy,&y);
1583:   if (usecprow) { /* use compressed row format */
1584:     m    = a->compressedrow.nrows;
1585:     ii   = a->compressedrow.i;
1586:     ridx = a->compressedrow.rindex;
1587:     for (i=0; i<m; i++) {
1588:       n           = ii[i+1] - ii[i];
1589:       aj          = a->j + ii[i];
1590:       aa          = a->a + ii[i];
1591:       sum         = 0.0;
1592:       nonzerorow += (n>0);
1593:       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1594:       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
1595:       y[*ridx++] = sum;
1596:     }
1597:   } else { /* do not use compressed row format */
1598:     ii = a->i;
1599:     for (i=0; i<m; i++) {
1600:       n           = ii[i+1] - ii[i];
1601:       aj          = a->j + ii[i];
1602:       aa          = a->a + ii[i];
1603:       sum         = 0.0;
1604:       nonzerorow += (n>0);
1605:       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1606:       y[i] = sum;
1607:     }
1608:   }
1609:   PetscLogFlops(2.0*a->nz - nonzerorow);
1610:   VecRestoreArrayRead(xx,&x);
1611:   VecRestoreArray(yy,&y);
1612:   return(0);
1613: }

1615: PetscErrorCode MatMultAddMax_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1616: {
1617:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1618:   PetscScalar       *y,*z;
1619:   const PetscScalar *x;
1620:   const MatScalar   *aa;
1621:   PetscErrorCode    ierr;
1622:   PetscInt          m = A->rmap->n,*aj,*ii;
1623:   PetscInt          n,i,*ridx=NULL;
1624:   PetscScalar       sum;
1625:   PetscBool         usecprow=a->compressedrow.use;

1628:   VecGetArrayRead(xx,&x);
1629:   VecGetArrayPair(yy,zz,&y,&z);
1630:   if (usecprow) { /* use compressed row format */
1631:     if (zz != yy) {
1632:       PetscArraycpy(z,y,m);
1633:     }
1634:     m    = a->compressedrow.nrows;
1635:     ii   = a->compressedrow.i;
1636:     ridx = a->compressedrow.rindex;
1637:     for (i=0; i<m; i++) {
1638:       n   = ii[i+1] - ii[i];
1639:       aj  = a->j + ii[i];
1640:       aa  = a->a + ii[i];
1641:       sum = y[*ridx];
1642:       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1643:       z[*ridx++] = sum;
1644:     }
1645:   } else { /* do not use compressed row format */
1646:     ii = a->i;
1647:     for (i=0; i<m; i++) {
1648:       n   = ii[i+1] - ii[i];
1649:       aj  = a->j + ii[i];
1650:       aa  = a->a + ii[i];
1651:       sum = y[i];
1652:       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1653:       z[i] = sum;
1654:     }
1655:   }
1656:   PetscLogFlops(2.0*a->nz);
1657:   VecRestoreArrayRead(xx,&x);
1658:   VecRestoreArrayPair(yy,zz,&y,&z);
1659:   return(0);
1660: }

1662: #include <../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h>
1663: PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1664: {
1665:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1666:   PetscScalar       *y,*z;
1667:   const PetscScalar *x;
1668:   const MatScalar   *aa;
1669:   PetscErrorCode    ierr;
1670:   const PetscInt    *aj,*ii,*ridx=NULL;
1671:   PetscInt          m = A->rmap->n,n,i;
1672:   PetscScalar       sum;
1673:   PetscBool         usecprow=a->compressedrow.use;

1676:   if (a->inode.use && a->inode.checked) {
1677:     MatMultAdd_SeqAIJ_Inode(A,xx,yy,zz);
1678:     return(0);
1679:   }
1680:   VecGetArrayRead(xx,&x);
1681:   VecGetArrayPair(yy,zz,&y,&z);
1682:   if (usecprow) { /* use compressed row format */
1683:     if (zz != yy) {
1684:       PetscArraycpy(z,y,m);
1685:     }
1686:     m    = a->compressedrow.nrows;
1687:     ii   = a->compressedrow.i;
1688:     ridx = a->compressedrow.rindex;
1689:     for (i=0; i<m; i++) {
1690:       n   = ii[i+1] - ii[i];
1691:       aj  = a->j + ii[i];
1692:       aa  = a->a + ii[i];
1693:       sum = y[*ridx];
1694:       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1695:       z[*ridx++] = sum;
1696:     }
1697:   } else { /* do not use compressed row format */
1698:     ii = a->i;
1699: #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
1700:     aj = a->j;
1701:     aa = a->a;
1702:     fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
1703: #else
1704:     for (i=0; i<m; i++) {
1705:       n   = ii[i+1] - ii[i];
1706:       aj  = a->j + ii[i];
1707:       aa  = a->a + ii[i];
1708:       sum = y[i];
1709:       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1710:       z[i] = sum;
1711:     }
1712: #endif
1713:   }
1714:   PetscLogFlops(2.0*a->nz);
1715:   VecRestoreArrayRead(xx,&x);
1716:   VecRestoreArrayPair(yy,zz,&y,&z);
1717:   return(0);
1718: }

1720: /*
1721:      Adds diagonal pointers to sparse matrix structure.
1722: */
1723: PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
1724: {
1725:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1727:   PetscInt       i,j,m = A->rmap->n;

1730:   if (!a->diag) {
1731:     PetscMalloc1(m,&a->diag);
1732:     PetscLogObjectMemory((PetscObject)A, m*sizeof(PetscInt));
1733:   }
1734:   for (i=0; i<A->rmap->n; i++) {
1735:     a->diag[i] = a->i[i+1];
1736:     for (j=a->i[i]; j<a->i[i+1]; j++) {
1737:       if (a->j[j] == i) {
1738:         a->diag[i] = j;
1739:         break;
1740:       }
1741:     }
1742:   }
1743:   return(0);
1744: }

1746: PetscErrorCode MatShift_SeqAIJ(Mat A,PetscScalar v)
1747: {
1748:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1749:   const PetscInt    *diag = (const PetscInt*)a->diag;
1750:   const PetscInt    *ii = (const PetscInt*) a->i;
1751:   PetscInt          i,*mdiag = NULL;
1752:   PetscErrorCode    ierr;
1753:   PetscInt          cnt = 0; /* how many diagonals are missing */

1756:   if (!A->preallocated || !a->nz) {
1757:     MatSeqAIJSetPreallocation(A,1,NULL);
1758:     MatShift_Basic(A,v);
1759:     return(0);
1760:   }

1762:   if (a->diagonaldense) {
1763:     cnt = 0;
1764:   } else {
1765:     PetscCalloc1(A->rmap->n,&mdiag);
1766:     for (i=0; i<A->rmap->n; i++) {
1767:       if (diag[i] >= ii[i+1]) {
1768:         cnt++;
1769:         mdiag[i] = 1;
1770:       }
1771:     }
1772:   }
1773:   if (!cnt) {
1774:     MatShift_Basic(A,v);
1775:   } else {
1776:     PetscScalar *olda = a->a;  /* preserve pointers to current matrix nonzeros structure and values */
1777:     PetscInt    *oldj = a->j, *oldi = a->i;
1778:     PetscBool   singlemalloc = a->singlemalloc,free_a = a->free_a,free_ij = a->free_ij;

1780:     a->a = NULL;
1781:     a->j = NULL;
1782:     a->i = NULL;
1783:     /* increase the values in imax for each row where a diagonal is being inserted then reallocate the matrix data structures */
1784:     for (i=0; i<A->rmap->n; i++) {
1785:       a->imax[i] += mdiag[i];
1786:       a->imax[i] = PetscMin(a->imax[i],A->cmap->n);
1787:     }
1788:     MatSeqAIJSetPreallocation_SeqAIJ(A,0,a->imax);

1790:     /* copy old values into new matrix data structure */
1791:     for (i=0; i<A->rmap->n; i++) {
1792:       MatSetValues(A,1,&i,a->imax[i] - mdiag[i],&oldj[oldi[i]],&olda[oldi[i]],ADD_VALUES);
1793:       if (i < A->cmap->n) {
1794:         MatSetValue(A,i,i,v,ADD_VALUES);
1795:       }
1796:     }
1797:     MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
1798:     MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
1799:     if (singlemalloc) {
1800:       PetscFree3(olda,oldj,oldi);
1801:     } else {
1802:       if (free_a)  {PetscFree(olda);}
1803:       if (free_ij) {PetscFree(oldj);}
1804:       if (free_ij) {PetscFree(oldi);}
1805:     }
1806:   }
1807:   PetscFree(mdiag);
1808:   a->diagonaldense = PETSC_TRUE;
1809:   return(0);
1810: }

1812: /*
1813:      Checks for missing diagonals
1814: */
1815: PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscBool  *missing,PetscInt *d)
1816: {
1817:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1818:   PetscInt       *diag,*ii = a->i,i;

1822:   *missing = PETSC_FALSE;
1823:   if (A->rmap->n > 0 && !ii) {
1824:     *missing = PETSC_TRUE;
1825:     if (d) *d = 0;
1826:     PetscInfo(A,"Matrix has no entries therefore is missing diagonal\n");
1827:   } else {
1828:     PetscInt n;
1829:     n = PetscMin(A->rmap->n, A->cmap->n);
1830:     diag = a->diag;
1831:     for (i=0; i<n; i++) {
1832:       if (diag[i] >= ii[i+1]) {
1833:         *missing = PETSC_TRUE;
1834:         if (d) *d = i;
1835:         PetscInfo1(A,"Matrix is missing diagonal number %D\n",i);
1836:         break;
1837:       }
1838:     }
1839:   }
1840:   return(0);
1841: }

1843: #include <petscblaslapack.h>
1844: #include <petsc/private/kernels/blockinvert.h>

1846: /*
1847:     Note that values is allocated externally by the PC and then passed into this routine
1848: */
1849: PetscErrorCode MatInvertVariableBlockDiagonal_SeqAIJ(Mat A,PetscInt nblocks,const PetscInt *bsizes,PetscScalar *diag)
1850: {
1851:   PetscErrorCode  ierr;
1852:   PetscInt        n = A->rmap->n, i, ncnt = 0, *indx,j,bsizemax = 0,*v_pivots;
1853:   PetscBool       allowzeropivot,zeropivotdetected=PETSC_FALSE;
1854:   const PetscReal shift = 0.0;
1855:   PetscInt        ipvt[5];
1856:   PetscScalar     work[25],*v_work;

1859:   allowzeropivot = PetscNot(A->erroriffailure);
1860:   for (i=0; i<nblocks; i++) ncnt += bsizes[i];
1861:   if (ncnt != n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Total blocksizes %D doesn't match number matrix rows %D",ncnt,n);
1862:   for (i=0; i<nblocks; i++) {
1863:     bsizemax = PetscMax(bsizemax,bsizes[i]);
1864:   }
1865:   PetscMalloc1(bsizemax,&indx);
1866:   if (bsizemax > 7) {
1867:     PetscMalloc2(bsizemax,&v_work,bsizemax,&v_pivots);
1868:   }
1869:   ncnt = 0;
1870:   for (i=0; i<nblocks; i++) {
1871:     for (j=0; j<bsizes[i]; j++) indx[j] = ncnt+j;
1872:     MatGetValues(A,bsizes[i],indx,bsizes[i],indx,diag);
1873:     switch (bsizes[i]) {
1874:     case 1:
1875:       *diag = 1.0/(*diag);
1876:       break;
1877:     case 2:
1878:       PetscKernel_A_gets_inverse_A_2(diag,shift,allowzeropivot,&zeropivotdetected);
1879:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1880:       PetscKernel_A_gets_transpose_A_2(diag);
1881:       break;
1882:     case 3:
1883:       PetscKernel_A_gets_inverse_A_3(diag,shift,allowzeropivot,&zeropivotdetected);
1884:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1885:       PetscKernel_A_gets_transpose_A_3(diag);
1886:       break;
1887:     case 4:
1888:       PetscKernel_A_gets_inverse_A_4(diag,shift,allowzeropivot,&zeropivotdetected);
1889:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1890:       PetscKernel_A_gets_transpose_A_4(diag);
1891:       break;
1892:     case 5:
1893:       PetscKernel_A_gets_inverse_A_5(diag,ipvt,work,shift,allowzeropivot,&zeropivotdetected);
1894:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1895:       PetscKernel_A_gets_transpose_A_5(diag);
1896:       break;
1897:     case 6:
1898:       PetscKernel_A_gets_inverse_A_6(diag,shift,allowzeropivot,&zeropivotdetected);
1899:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1900:       PetscKernel_A_gets_transpose_A_6(diag);
1901:       break;
1902:     case 7:
1903:       PetscKernel_A_gets_inverse_A_7(diag,shift,allowzeropivot,&zeropivotdetected);
1904:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1905:       PetscKernel_A_gets_transpose_A_7(diag);
1906:       break;
1907:     default:
1908:       PetscKernel_A_gets_inverse_A(bsizes[i],diag,v_pivots,v_work,allowzeropivot,&zeropivotdetected);
1909:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1910:       PetscKernel_A_gets_transpose_A_N(diag,bsizes[i]);
1911:     }
1912:     ncnt   += bsizes[i];
1913:     diag += bsizes[i]*bsizes[i];
1914:   }
1915:   if (bsizemax > 7) {
1916:     PetscFree2(v_work,v_pivots);
1917:   }
1918:   PetscFree(indx);
1919:   return(0);
1920: }

1922: /*
1923:    Negative shift indicates do not generate an error if there is a zero diagonal, just invert it anyways
1924: */
1925: PetscErrorCode  MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
1926: {
1927:   Mat_SeqAIJ      *a = (Mat_SeqAIJ*) A->data;
1928:   PetscErrorCode  ierr;
1929:   PetscInt        i,*diag,m = A->rmap->n;
1930:   const MatScalar *v;
1931:   PetscScalar     *idiag,*mdiag;

1934:   if (a->idiagvalid) return(0);
1935:   MatMarkDiagonal_SeqAIJ(A);
1936:   diag = a->diag;
1937:   if (!a->idiag) {
1938:     PetscMalloc3(m,&a->idiag,m,&a->mdiag,m,&a->ssor_work);
1939:     PetscLogObjectMemory((PetscObject)A,3*m*sizeof(PetscScalar));
1940:   }

1942:   mdiag = a->mdiag;
1943:   idiag = a->idiag;
1944:   MatSeqAIJGetArrayRead(A,&v);
1945:   if (omega == 1.0 && PetscRealPart(fshift) <= 0.0) {
1946:     for (i=0; i<m; i++) {
1947:       mdiag[i] = v[diag[i]];
1948:       if (!PetscAbsScalar(mdiag[i])) { /* zero diagonal */
1949:         if (PetscRealPart(fshift)) {
1950:           PetscInfo1(A,"Zero diagonal on row %D\n",i);
1951:           A->factorerrortype             = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1952:           A->factorerror_zeropivot_value = 0.0;
1953:           A->factorerror_zeropivot_row   = i;
1954:         } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
1955:       }
1956:       idiag[i] = 1.0/v[diag[i]];
1957:     }
1958:     PetscLogFlops(m);
1959:   } else {
1960:     for (i=0; i<m; i++) {
1961:       mdiag[i] = v[diag[i]];
1962:       idiag[i] = omega/(fshift + v[diag[i]]);
1963:     }
1964:     PetscLogFlops(2.0*m);
1965:   }
1966:   a->idiagvalid = PETSC_TRUE;
1967:   MatSeqAIJRestoreArrayRead(A,&v);
1968:   return(0);
1969: }

1971: #include <../src/mat/impls/aij/seq/ftn-kernels/frelax.h>
1972: PetscErrorCode MatSOR_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
1973: {
1974:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1975:   PetscScalar       *x,d,sum,*t,scale;
1976:   const MatScalar   *v,*idiag=NULL,*mdiag,*aa;
1977:   const PetscScalar *b, *bs,*xb, *ts;
1978:   PetscErrorCode    ierr;
1979:   PetscInt          n,m = A->rmap->n,i;
1980:   const PetscInt    *idx,*diag;

1983:   if (a->inode.use && a->inode.checked && omega == 1.0 && fshift == 0.0) {
1984:     MatSOR_SeqAIJ_Inode(A,bb,omega,flag,fshift,its,lits,xx);
1985:     return(0);
1986:   }
1987:   its = its*lits;

1989:   if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
1990:   if (!a->idiagvalid) {MatInvertDiagonal_SeqAIJ(A,omega,fshift);}
1991:   a->fshift = fshift;
1992:   a->omega  = omega;

1994:   diag  = a->diag;
1995:   t     = a->ssor_work;
1996:   idiag = a->idiag;
1997:   mdiag = a->mdiag;

1999:   MatSeqAIJGetArrayRead(A,&aa);
2000:   VecGetArray(xx,&x);
2001:   VecGetArrayRead(bb,&b);
2002:   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
2003:   if (flag == SOR_APPLY_UPPER) {
2004:     /* apply (U + D/omega) to the vector */
2005:     bs = b;
2006:     for (i=0; i<m; i++) {
2007:       d   = fshift + mdiag[i];
2008:       n   = a->i[i+1] - diag[i] - 1;
2009:       idx = a->j + diag[i] + 1;
2010:       v   = aa + diag[i] + 1;
2011:       sum = b[i]*d/omega;
2012:       PetscSparseDensePlusDot(sum,bs,v,idx,n);
2013:       x[i] = sum;
2014:     }
2015:     VecRestoreArray(xx,&x);
2016:     VecRestoreArrayRead(bb,&b);
2017:     MatSeqAIJRestoreArrayRead(A,&aa);
2018:     PetscLogFlops(a->nz);
2019:     return(0);
2020:   }

2022:   if (flag == SOR_APPLY_LOWER) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"SOR_APPLY_LOWER is not implemented");
2023:   else if (flag & SOR_EISENSTAT) {
2024:     /* Let  A = L + U + D; where L is lower triangular,
2025:     U is upper triangular, E = D/omega; This routine applies

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

2029:     to a vector efficiently using Eisenstat's trick.
2030:     */
2031:     scale = (2.0/omega) - 1.0;

2033:     /*  x = (E + U)^{-1} b */
2034:     for (i=m-1; i>=0; i--) {
2035:       n   = a->i[i+1] - diag[i] - 1;
2036:       idx = a->j + diag[i] + 1;
2037:       v   = aa + diag[i] + 1;
2038:       sum = b[i];
2039:       PetscSparseDenseMinusDot(sum,x,v,idx,n);
2040:       x[i] = sum*idiag[i];
2041:     }

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

2047:     /*  t = (E + L)^{-1}t */
2048:     ts   = t;
2049:     diag = a->diag;
2050:     for (i=0; i<m; i++) {
2051:       n   = diag[i] - a->i[i];
2052:       idx = a->j + a->i[i];
2053:       v   = aa + a->i[i];
2054:       sum = t[i];
2055:       PetscSparseDenseMinusDot(sum,ts,v,idx,n);
2056:       t[i] = sum*idiag[i];
2057:       /*  x = x + t */
2058:       x[i] += t[i];
2059:     }

2061:     PetscLogFlops(6.0*m-1 + 2.0*a->nz);
2062:     VecRestoreArray(xx,&x);
2063:     VecRestoreArrayRead(bb,&b);
2064:     return(0);
2065:   }
2066:   if (flag & SOR_ZERO_INITIAL_GUESS) {
2067:     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
2068:       for (i=0; i<m; i++) {
2069:         n   = diag[i] - a->i[i];
2070:         idx = a->j + a->i[i];
2071:         v   = aa + a->i[i];
2072:         sum = b[i];
2073:         PetscSparseDenseMinusDot(sum,x,v,idx,n);
2074:         t[i] = sum;
2075:         x[i] = sum*idiag[i];
2076:       }
2077:       xb   = t;
2078:       PetscLogFlops(a->nz);
2079:     } else xb = b;
2080:     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
2081:       for (i=m-1; i>=0; i--) {
2082:         n   = a->i[i+1] - diag[i] - 1;
2083:         idx = a->j + diag[i] + 1;
2084:         v   = aa + diag[i] + 1;
2085:         sum = xb[i];
2086:         PetscSparseDenseMinusDot(sum,x,v,idx,n);
2087:         if (xb == b) {
2088:           x[i] = sum*idiag[i];
2089:         } else {
2090:           x[i] = (1-omega)*x[i] + sum*idiag[i];  /* omega in idiag */
2091:         }
2092:       }
2093:       PetscLogFlops(a->nz); /* assumes 1/2 in upper */
2094:     }
2095:     its--;
2096:   }
2097:   while (its--) {
2098:     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
2099:       for (i=0; i<m; i++) {
2100:         /* lower */
2101:         n   = diag[i] - a->i[i];
2102:         idx = a->j + a->i[i];
2103:         v   = aa + a->i[i];
2104:         sum = b[i];
2105:         PetscSparseDenseMinusDot(sum,x,v,idx,n);
2106:         t[i] = sum;             /* save application of the lower-triangular part */
2107:         /* upper */
2108:         n   = a->i[i+1] - diag[i] - 1;
2109:         idx = a->j + diag[i] + 1;
2110:         v   = aa + diag[i] + 1;
2111:         PetscSparseDenseMinusDot(sum,x,v,idx,n);
2112:         x[i] = (1. - omega)*x[i] + sum*idiag[i]; /* omega in idiag */
2113:       }
2114:       xb   = t;
2115:       PetscLogFlops(2.0*a->nz);
2116:     } else xb = b;
2117:     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
2118:       for (i=m-1; i>=0; i--) {
2119:         sum = xb[i];
2120:         if (xb == b) {
2121:           /* whole matrix (no checkpointing available) */
2122:           n   = a->i[i+1] - a->i[i];
2123:           idx = a->j + a->i[i];
2124:           v   = aa + a->i[i];
2125:           PetscSparseDenseMinusDot(sum,x,v,idx,n);
2126:           x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
2127:         } else { /* lower-triangular part has been saved, so only apply upper-triangular */
2128:           n   = a->i[i+1] - diag[i] - 1;
2129:           idx = a->j + diag[i] + 1;
2130:           v   = aa + diag[i] + 1;
2131:           PetscSparseDenseMinusDot(sum,x,v,idx,n);
2132:           x[i] = (1. - omega)*x[i] + sum*idiag[i];  /* omega in idiag */
2133:         }
2134:       }
2135:       if (xb == b) {
2136:         PetscLogFlops(2.0*a->nz);
2137:       } else {
2138:         PetscLogFlops(a->nz); /* assumes 1/2 in upper */
2139:       }
2140:     }
2141:   }
2142:   MatSeqAIJRestoreArrayRead(A,&aa);
2143:   VecRestoreArray(xx,&x);
2144:   VecRestoreArrayRead(bb,&b);
2145:   return(0);
2146: }


2149: PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
2150: {
2151:   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;

2154:   info->block_size   = 1.0;
2155:   info->nz_allocated = a->maxnz;
2156:   info->nz_used      = a->nz;
2157:   info->nz_unneeded  = (a->maxnz - a->nz);
2158:   info->assemblies   = A->num_ass;
2159:   info->mallocs      = A->info.mallocs;
2160:   info->memory       = ((PetscObject)A)->mem;
2161:   if (A->factortype) {
2162:     info->fill_ratio_given  = A->info.fill_ratio_given;
2163:     info->fill_ratio_needed = A->info.fill_ratio_needed;
2164:     info->factor_mallocs    = A->info.factor_mallocs;
2165:   } else {
2166:     info->fill_ratio_given  = 0;
2167:     info->fill_ratio_needed = 0;
2168:     info->factor_mallocs    = 0;
2169:   }
2170:   return(0);
2171: }

2173: PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
2174: {
2175:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
2176:   PetscInt          i,m = A->rmap->n - 1;
2177:   PetscErrorCode    ierr;
2178:   const PetscScalar *xx;
2179:   PetscScalar       *bb,*aa;
2180:   PetscInt          d = 0;

2183:   if (x && b) {
2184:     VecGetArrayRead(x,&xx);
2185:     VecGetArray(b,&bb);
2186:     for (i=0; i<N; i++) {
2187:       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
2188:       if (rows[i] >= A->cmap->n) continue;
2189:       bb[rows[i]] = diag*xx[rows[i]];
2190:     }
2191:     VecRestoreArrayRead(x,&xx);
2192:     VecRestoreArray(b,&bb);
2193:   }

2195:   MatSeqAIJGetArray(A,&aa);
2196:   if (a->keepnonzeropattern) {
2197:     for (i=0; i<N; i++) {
2198:       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
2199:       PetscArrayzero(&aa[a->i[rows[i]]],a->ilen[rows[i]]);
2200:     }
2201:     if (diag != 0.0) {
2202:       for (i=0; i<N; i++) {
2203:         d = rows[i];
2204:         if (rows[i] >= A->cmap->n) continue;
2205:         if (a->diag[d] >= a->i[d+1]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in the zeroed row %D",d);
2206:       }
2207:       for (i=0; i<N; i++) {
2208:         if (rows[i] >= A->cmap->n) continue;
2209:         aa[a->diag[rows[i]]] = diag;
2210:       }
2211:     }
2212:   } else {
2213:     if (diag != 0.0) {
2214:       for (i=0; i<N; i++) {
2215:         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
2216:         if (a->ilen[rows[i]] > 0) {
2217:           if (rows[i] >= A->cmap->n) {
2218:             a->ilen[rows[i]] = 0;
2219:           } else {
2220:             a->ilen[rows[i]]    = 1;
2221:             aa[a->i[rows[i]]]   = diag;
2222:             a->j[a->i[rows[i]]] = rows[i];
2223:           }
2224:         } else if (rows[i] < A->cmap->n) { /* in case row was completely empty */
2225:           MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);
2226:         }
2227:       }
2228:     } else {
2229:       for (i=0; i<N; i++) {
2230:         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
2231:         a->ilen[rows[i]] = 0;
2232:       }
2233:     }
2234:     A->nonzerostate++;
2235:   }
2236:   MatSeqAIJRestoreArray(A,&aa);
2237: #if defined(PETSC_HAVE_DEVICE)
2238:   if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) A->offloadmask = PETSC_OFFLOAD_CPU;
2239: #endif
2240:   (*A->ops->assemblyend)(A,MAT_FINAL_ASSEMBLY);
2241:   return(0);
2242: }

2244: PetscErrorCode MatZeroRowsColumns_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
2245: {
2246:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
2247:   PetscInt          i,j,m = A->rmap->n - 1,d = 0;
2248:   PetscErrorCode    ierr;
2249:   PetscBool         missing,*zeroed,vecs = PETSC_FALSE;
2250:   const PetscScalar *xx;
2251:   PetscScalar       *bb,*aa;

2254:   if (!N) return(0);
2255:   MatSeqAIJGetArray(A,&aa);
2256:   if (x && b) {
2257:     VecGetArrayRead(x,&xx);
2258:     VecGetArray(b,&bb);
2259:     vecs = PETSC_TRUE;
2260:   }
2261:   PetscCalloc1(A->rmap->n,&zeroed);
2262:   for (i=0; i<N; i++) {
2263:     if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
2264:     PetscArrayzero(&aa[a->i[rows[i]]],a->ilen[rows[i]]);

2266:     zeroed[rows[i]] = PETSC_TRUE;
2267:   }
2268:   for (i=0; i<A->rmap->n; i++) {
2269:     if (!zeroed[i]) {
2270:       for (j=a->i[i]; j<a->i[i+1]; j++) {
2271:         if (a->j[j] < A->rmap->n && zeroed[a->j[j]]) {
2272:           if (vecs) bb[i] -= aa[j]*xx[a->j[j]];
2273:           aa[j] = 0.0;
2274:         }
2275:       }
2276:     } else if (vecs && i < A->cmap->N) bb[i] = diag*xx[i];
2277:   }
2278:   if (x && b) {
2279:     VecRestoreArrayRead(x,&xx);
2280:     VecRestoreArray(b,&bb);
2281:   }
2282:   PetscFree(zeroed);
2283:   if (diag != 0.0) {
2284:     MatMissingDiagonal_SeqAIJ(A,&missing,&d);
2285:     if (missing) {
2286:       for (i=0; i<N; i++) {
2287:         if (rows[i] >= A->cmap->N) continue;
2288:         if (a->nonew && rows[i] >= d) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D (%D)",d,rows[i]);
2289:         MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);
2290:       }
2291:     } else {
2292:       for (i=0; i<N; i++) {
2293:         aa[a->diag[rows[i]]] = diag;
2294:       }
2295:     }
2296:   }
2297:   MatSeqAIJRestoreArray(A,&aa);
2298: #if defined(PETSC_HAVE_DEVICE)
2299:   if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) A->offloadmask = PETSC_OFFLOAD_CPU;
2300: #endif
2301:   (*A->ops->assemblyend)(A,MAT_FINAL_ASSEMBLY);
2302:   return(0);
2303: }

2305: PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
2306: {
2307:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
2308:   const PetscScalar *aa = a->a;
2309:   PetscInt          *itmp;
2310: #if defined(PETSC_HAVE_DEVICE)
2311:   PetscErrorCode    ierr;
2312:   PetscBool         rest = PETSC_FALSE;
2313: #endif

2316: #if defined(PETSC_HAVE_DEVICE)
2317:   if (v && A->offloadmask == PETSC_OFFLOAD_GPU) {
2318:     /* triggers copy to CPU */
2319:     rest = PETSC_TRUE;
2320:     MatSeqAIJGetArrayRead(A,&aa);
2321:   } else aa = a->a;
2322: #endif
2323:   *nz = a->i[row+1] - a->i[row];
2324:   if (v) *v = (PetscScalar*)(aa + a->i[row]);
2325:   if (idx) {
2326:     itmp = a->j + a->i[row];
2327:     if (*nz) *idx = itmp;
2328:     else *idx = NULL;
2329:   }
2330: #if defined(PETSC_HAVE_DEVICE)
2331:   if (rest) {
2332:     MatSeqAIJRestoreArrayRead(A,&aa);
2333:   }
2334: #endif
2335:   return(0);
2336: }

2338: PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
2339: {
2341:   *nz = 0;
2342:   if (idx) *idx = NULL;
2343:   if (v) *v = NULL;
2344:   return(0);
2345: }

2347: PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
2348: {
2349:   Mat_SeqAIJ      *a  = (Mat_SeqAIJ*)A->data;
2350:   const MatScalar *v;
2351:   PetscReal       sum = 0.0;
2352:   PetscErrorCode  ierr;
2353:   PetscInt        i,j;

2356:   MatSeqAIJGetArrayRead(A,&v);
2357:   if (type == NORM_FROBENIUS) {
2358: #if defined(PETSC_USE_REAL___FP16)
2359:     PetscBLASInt one = 1,nz = a->nz;
2360:     PetscStackCallBLAS("BLASnrm2",*nrm = BLASnrm2_(&nz,v,&one));
2361: #else
2362:     for (i=0; i<a->nz; i++) {
2363:       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
2364:     }
2365:     *nrm = PetscSqrtReal(sum);
2366: #endif
2367:     PetscLogFlops(2.0*a->nz);
2368:   } else if (type == NORM_1) {
2369:     PetscReal *tmp;
2370:     PetscInt  *jj = a->j;
2371:     PetscCalloc1(A->cmap->n+1,&tmp);
2372:     *nrm = 0.0;
2373:     for (j=0; j<a->nz; j++) {
2374:       tmp[*jj++] += PetscAbsScalar(*v);  v++;
2375:     }
2376:     for (j=0; j<A->cmap->n; j++) {
2377:       if (tmp[j] > *nrm) *nrm = tmp[j];
2378:     }
2379:     PetscFree(tmp);
2380:     PetscLogFlops(PetscMax(a->nz-1,0));
2381:   } else if (type == NORM_INFINITY) {
2382:     *nrm = 0.0;
2383:     for (j=0; j<A->rmap->n; j++) {
2384:       const PetscScalar *v2 = v + a->i[j];
2385:       sum = 0.0;
2386:       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
2387:         sum += PetscAbsScalar(*v2); v2++;
2388:       }
2389:       if (sum > *nrm) *nrm = sum;
2390:     }
2391:     PetscLogFlops(PetscMax(a->nz-1,0));
2392:   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for two norm");
2393:   MatSeqAIJRestoreArrayRead(A,&v);
2394:   return(0);
2395: }

2397: /* Merged from MatGetSymbolicTranspose_SeqAIJ() - replace MatGetSymbolicTranspose_SeqAIJ()? */
2398: PetscErrorCode MatTransposeSymbolic_SeqAIJ(Mat A,Mat *B)
2399: {
2401:   PetscInt       i,j,anzj;
2402:   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data,*b;
2403:   PetscInt       an=A->cmap->N,am=A->rmap->N;
2404:   PetscInt       *ati,*atj,*atfill,*ai=a->i,*aj=a->j;

2407:   /* Allocate space for symbolic transpose info and work array */
2408:   PetscCalloc1(an+1,&ati);
2409:   PetscMalloc1(ai[am],&atj);
2410:   PetscMalloc1(an,&atfill);

2412:   /* Walk through aj and count ## of non-zeros in each row of A^T. */
2413:   /* Note: offset by 1 for fast conversion into csr format. */
2414:   for (i=0;i<ai[am];i++) ati[aj[i]+1] += 1;
2415:   /* Form ati for csr format of A^T. */
2416:   for (i=0;i<an;i++) ati[i+1] += ati[i];

2418:   /* Copy ati into atfill so we have locations of the next free space in atj */
2419:   PetscArraycpy(atfill,ati,an);

2421:   /* Walk through A row-wise and mark nonzero entries of A^T. */
2422:   for (i=0;i<am;i++) {
2423:     anzj = ai[i+1] - ai[i];
2424:     for (j=0;j<anzj;j++) {
2425:       atj[atfill[*aj]] = i;
2426:       atfill[*aj++]   += 1;
2427:     }
2428:   }

2430:   /* Clean up temporary space and complete requests. */
2431:   PetscFree(atfill);
2432:   MatCreateSeqAIJWithArrays(PetscObjectComm((PetscObject)A),an,am,ati,atj,NULL,B);
2433:   MatSetBlockSizes(*B,PetscAbs(A->cmap->bs),PetscAbs(A->rmap->bs));
2434:   MatSetType(*B,((PetscObject)A)->type_name);

2436:   b          = (Mat_SeqAIJ*)((*B)->data);
2437:   b->free_a  = PETSC_FALSE;
2438:   b->free_ij = PETSC_TRUE;
2439:   b->nonew   = 0;
2440:   return(0);
2441: }

2443: PetscErrorCode  MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
2444: {
2445:   Mat_SeqAIJ      *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) B->data;
2446:   PetscInt        *adx,*bdx,*aii,*bii,*aptr,*bptr;
2447:   const MatScalar *va,*vb;
2448:   PetscErrorCode  ierr;
2449:   PetscInt        ma,na,mb,nb, i;

2452:   MatGetSize(A,&ma,&na);
2453:   MatGetSize(B,&mb,&nb);
2454:   if (ma!=nb || na!=mb) {
2455:     *f = PETSC_FALSE;
2456:     return(0);
2457:   }
2458:   MatSeqAIJGetArrayRead(A,&va);
2459:   MatSeqAIJGetArrayRead(B,&vb);
2460:   aii  = aij->i; bii = bij->i;
2461:   adx  = aij->j; bdx = bij->j;
2462:   PetscMalloc1(ma,&aptr);
2463:   PetscMalloc1(mb,&bptr);
2464:   for (i=0; i<ma; i++) aptr[i] = aii[i];
2465:   for (i=0; i<mb; i++) bptr[i] = bii[i];

2467:   *f = PETSC_TRUE;
2468:   for (i=0; i<ma; i++) {
2469:     while (aptr[i]<aii[i+1]) {
2470:       PetscInt    idc,idr;
2471:       PetscScalar vc,vr;
2472:       /* column/row index/value */
2473:       idc = adx[aptr[i]];
2474:       idr = bdx[bptr[idc]];
2475:       vc  = va[aptr[i]];
2476:       vr  = vb[bptr[idc]];
2477:       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
2478:         *f = PETSC_FALSE;
2479:         goto done;
2480:       } else {
2481:         aptr[i]++;
2482:         if (B || i!=idc) bptr[idc]++;
2483:       }
2484:     }
2485:   }
2486: done:
2487:   PetscFree(aptr);
2488:   PetscFree(bptr);
2489:   MatSeqAIJRestoreArrayRead(A,&va);
2490:   MatSeqAIJRestoreArrayRead(B,&vb);
2491:   return(0);
2492: }

2494: PetscErrorCode  MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
2495: {
2496:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) B->data;
2497:   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
2498:   MatScalar      *va,*vb;
2500:   PetscInt       ma,na,mb,nb, i;

2503:   MatGetSize(A,&ma,&na);
2504:   MatGetSize(B,&mb,&nb);
2505:   if (ma!=nb || na!=mb) {
2506:     *f = PETSC_FALSE;
2507:     return(0);
2508:   }
2509:   aii  = aij->i; bii = bij->i;
2510:   adx  = aij->j; bdx = bij->j;
2511:   va   = aij->a; vb = bij->a;
2512:   PetscMalloc1(ma,&aptr);
2513:   PetscMalloc1(mb,&bptr);
2514:   for (i=0; i<ma; i++) aptr[i] = aii[i];
2515:   for (i=0; i<mb; i++) bptr[i] = bii[i];

2517:   *f = PETSC_TRUE;
2518:   for (i=0; i<ma; i++) {
2519:     while (aptr[i]<aii[i+1]) {
2520:       PetscInt    idc,idr;
2521:       PetscScalar vc,vr;
2522:       /* column/row index/value */
2523:       idc = adx[aptr[i]];
2524:       idr = bdx[bptr[idc]];
2525:       vc  = va[aptr[i]];
2526:       vr  = vb[bptr[idc]];
2527:       if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) {
2528:         *f = PETSC_FALSE;
2529:         goto done;
2530:       } else {
2531:         aptr[i]++;
2532:         if (B || i!=idc) bptr[idc]++;
2533:       }
2534:     }
2535:   }
2536: done:
2537:   PetscFree(aptr);
2538:   PetscFree(bptr);
2539:   return(0);
2540: }

2542: PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
2543: {

2547:   MatIsTranspose_SeqAIJ(A,A,tol,f);
2548:   return(0);
2549: }

2551: PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
2552: {

2556:   MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);
2557:   return(0);
2558: }

2560: PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
2561: {
2562:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
2563:   const PetscScalar *l,*r;
2564:   PetscScalar       x;
2565:   MatScalar         *v;
2566:   PetscErrorCode    ierr;
2567:   PetscInt          i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz;
2568:   const PetscInt    *jj;

2571:   if (ll) {
2572:     /* The local size is used so that VecMPI can be passed to this routine
2573:        by MatDiagonalScale_MPIAIJ */
2574:     VecGetLocalSize(ll,&m);
2575:     if (m != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
2576:     VecGetArrayRead(ll,&l);
2577:     MatSeqAIJGetArray(A,&v);
2578:     for (i=0; i<m; i++) {
2579:       x = l[i];
2580:       M = a->i[i+1] - a->i[i];
2581:       for (j=0; j<M; j++) (*v++) *= x;
2582:     }
2583:     VecRestoreArrayRead(ll,&l);
2584:     PetscLogFlops(nz);
2585:     MatSeqAIJRestoreArray(A,&v);
2586:   }
2587:   if (rr) {
2588:     VecGetLocalSize(rr,&n);
2589:     if (n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
2590:     VecGetArrayRead(rr,&r);
2591:     MatSeqAIJGetArray(A,&v);
2592:     jj = a->j;
2593:     for (i=0; i<nz; i++) (*v++) *= r[*jj++];
2594:     MatSeqAIJRestoreArray(A,&v);
2595:     VecRestoreArrayRead(rr,&r);
2596:     PetscLogFlops(nz);
2597:   }
2598:   MatSeqAIJInvalidateDiagonal(A);
2599: #if defined(PETSC_HAVE_DEVICE)
2600:   if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) A->offloadmask = PETSC_OFFLOAD_CPU;
2601: #endif
2602:   return(0);
2603: }

2605: PetscErrorCode MatCreateSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
2606: {
2607:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data,*c;
2608:   PetscErrorCode    ierr;
2609:   PetscInt          *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens;
2610:   PetscInt          row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
2611:   const PetscInt    *irow,*icol;
2612:   const PetscScalar *aa;
2613:   PetscInt          nrows,ncols;
2614:   PetscInt          *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
2615:   MatScalar         *a_new,*mat_a;
2616:   Mat               C;
2617:   PetscBool         stride;

2620:   ISGetIndices(isrow,&irow);
2621:   ISGetLocalSize(isrow,&nrows);
2622:   ISGetLocalSize(iscol,&ncols);

2624:   PetscObjectTypeCompare((PetscObject)iscol,ISSTRIDE,&stride);
2625:   if (stride) {
2626:     ISStrideGetInfo(iscol,&first,&step);
2627:   } else {
2628:     first = 0;
2629:     step  = 0;
2630:   }
2631:   if (stride && step == 1) {
2632:     /* special case of contiguous rows */
2633:     PetscMalloc2(nrows,&lens,nrows,&starts);
2634:     /* loop over new rows determining lens and starting points */
2635:     for (i=0; i<nrows; i++) {
2636:       kstart = ai[irow[i]];
2637:       kend   = kstart + ailen[irow[i]];
2638:       starts[i] = kstart;
2639:       for (k=kstart; k<kend; k++) {
2640:         if (aj[k] >= first) {
2641:           starts[i] = k;
2642:           break;
2643:         }
2644:       }
2645:       sum = 0;
2646:       while (k < kend) {
2647:         if (aj[k++] >= first+ncols) break;
2648:         sum++;
2649:       }
2650:       lens[i] = sum;
2651:     }
2652:     /* create submatrix */
2653:     if (scall == MAT_REUSE_MATRIX) {
2654:       PetscInt n_cols,n_rows;
2655:       MatGetSize(*B,&n_rows,&n_cols);
2656:       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
2657:       MatZeroEntries(*B);
2658:       C    = *B;
2659:     } else {
2660:       PetscInt rbs,cbs;
2661:       MatCreate(PetscObjectComm((PetscObject)A),&C);
2662:       MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);
2663:       ISGetBlockSize(isrow,&rbs);
2664:       ISGetBlockSize(iscol,&cbs);
2665:       MatSetBlockSizes(C,rbs,cbs);
2666:       MatSetType(C,((PetscObject)A)->type_name);
2667:       MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);
2668:     }
2669:     c = (Mat_SeqAIJ*)C->data;

2671:     /* loop over rows inserting into submatrix */
2672:     a_new = c->a;
2673:     j_new = c->j;
2674:     i_new = c->i;
2675:     MatSeqAIJGetArrayRead(A,&aa);
2676:     for (i=0; i<nrows; i++) {
2677:       ii    = starts[i];
2678:       lensi = lens[i];
2679:       for (k=0; k<lensi; k++) {
2680:         *j_new++ = aj[ii+k] - first;
2681:       }
2682:       PetscArraycpy(a_new,aa + starts[i],lensi);
2683:       a_new     += lensi;
2684:       i_new[i+1] = i_new[i] + lensi;
2685:       c->ilen[i] = lensi;
2686:     }
2687:     MatSeqAIJRestoreArrayRead(A,&aa);
2688:     PetscFree2(lens,starts);
2689:   } else {
2690:     ISGetIndices(iscol,&icol);
2691:     PetscCalloc1(oldcols,&smap);
2692:     PetscMalloc1(1+nrows,&lens);
2693:     for (i=0; i<ncols; i++) {
2694:       if (PetscUnlikelyDebug(icol[i] >= oldcols)) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Requesting column beyond largest column icol[%D] %D >= A->cmap->n %D",i,icol[i],oldcols);
2695:       smap[icol[i]] = i+1;
2696:     }

2698:     /* determine lens of each row */
2699:     for (i=0; i<nrows; i++) {
2700:       kstart  = ai[irow[i]];
2701:       kend    = kstart + a->ilen[irow[i]];
2702:       lens[i] = 0;
2703:       for (k=kstart; k<kend; k++) {
2704:         if (smap[aj[k]]) {
2705:           lens[i]++;
2706:         }
2707:       }
2708:     }
2709:     /* Create and fill new matrix */
2710:     if (scall == MAT_REUSE_MATRIX) {
2711:       PetscBool equal;

2713:       c = (Mat_SeqAIJ*)((*B)->data);
2714:       if ((*B)->rmap->n  != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
2715:       PetscArraycmp(c->ilen,lens,(*B)->rmap->n,&equal);
2716:       if (!equal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
2717:       PetscArrayzero(c->ilen,(*B)->rmap->n);
2718:       C    = *B;
2719:     } else {
2720:       PetscInt rbs,cbs;
2721:       MatCreate(PetscObjectComm((PetscObject)A),&C);
2722:       MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);
2723:       ISGetBlockSize(isrow,&rbs);
2724:       ISGetBlockSize(iscol,&cbs);
2725:       MatSetBlockSizes(C,rbs,cbs);
2726:       MatSetType(C,((PetscObject)A)->type_name);
2727:       MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);
2728:     }
2729:     MatSeqAIJGetArrayRead(A,&aa);
2730:     c = (Mat_SeqAIJ*)(C->data);
2731:     for (i=0; i<nrows; i++) {
2732:       row      = irow[i];
2733:       kstart   = ai[row];
2734:       kend     = kstart + a->ilen[row];
2735:       mat_i    = c->i[i];
2736:       mat_j    = c->j + mat_i;
2737:       mat_a    = c->a + mat_i;
2738:       mat_ilen = c->ilen + i;
2739:       for (k=kstart; k<kend; k++) {
2740:         if ((tcol=smap[a->j[k]])) {
2741:           *mat_j++ = tcol - 1;
2742:           *mat_a++ = aa[k];
2743:           (*mat_ilen)++;

2745:         }
2746:       }
2747:     }
2748:     MatSeqAIJRestoreArrayRead(A,&aa);
2749:     /* Free work space */
2750:     ISRestoreIndices(iscol,&icol);
2751:     PetscFree(smap);
2752:     PetscFree(lens);
2753:     /* sort */
2754:     for (i = 0; i < nrows; i++) {
2755:       PetscInt ilen;

2757:       mat_i = c->i[i];
2758:       mat_j = c->j + mat_i;
2759:       mat_a = c->a + mat_i;
2760:       ilen  = c->ilen[i];
2761:       PetscSortIntWithScalarArray(ilen,mat_j,mat_a);
2762:     }
2763:   }
2764: #if defined(PETSC_HAVE_DEVICE)
2765:   MatBindToCPU(C,A->boundtocpu);
2766: #endif
2767:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
2768:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);

2770:   ISRestoreIndices(isrow,&irow);
2771:   *B   = C;
2772:   return(0);
2773: }

2775: PetscErrorCode  MatGetMultiProcBlock_SeqAIJ(Mat mat,MPI_Comm subComm,MatReuse scall,Mat *subMat)
2776: {
2778:   Mat            B;

2781:   if (scall == MAT_INITIAL_MATRIX) {
2782:     MatCreate(subComm,&B);
2783:     MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->n,mat->cmap->n);
2784:     MatSetBlockSizesFromMats(B,mat,mat);
2785:     MatSetType(B,MATSEQAIJ);
2786:     MatDuplicateNoCreate_SeqAIJ(B,mat,MAT_COPY_VALUES,PETSC_TRUE);
2787:     *subMat = B;
2788:   } else {
2789:     MatCopy_SeqAIJ(mat,*subMat,SAME_NONZERO_PATTERN);
2790:   }
2791:   return(0);
2792: }

2794: PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
2795: {
2796:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
2798:   Mat            outA;
2799:   PetscBool      row_identity,col_identity;

2802:   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels=0 supported for in-place ilu");

2804:   ISIdentity(row,&row_identity);
2805:   ISIdentity(col,&col_identity);

2807:   outA             = inA;
2808:   outA->factortype = MAT_FACTOR_LU;
2809:   PetscFree(inA->solvertype);
2810:   PetscStrallocpy(MATSOLVERPETSC,&inA->solvertype);

2812:   PetscObjectReference((PetscObject)row);
2813:   ISDestroy(&a->row);

2815:   a->row = row;

2817:   PetscObjectReference((PetscObject)col);
2818:   ISDestroy(&a->col);

2820:   a->col = col;

2822:   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
2823:   ISDestroy(&a->icol);
2824:   ISInvertPermutation(col,PETSC_DECIDE,&a->icol);
2825:   PetscLogObjectParent((PetscObject)inA,(PetscObject)a->icol);

2827:   if (!a->solve_work) { /* this matrix may have been factored before */
2828:     PetscMalloc1(inA->rmap->n+1,&a->solve_work);
2829:     PetscLogObjectMemory((PetscObject)inA, (inA->rmap->n+1)*sizeof(PetscScalar));
2830:   }

2832:   MatMarkDiagonal_SeqAIJ(inA);
2833:   if (row_identity && col_identity) {
2834:     MatLUFactorNumeric_SeqAIJ_inplace(outA,inA,info);
2835:   } else {
2836:     MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);
2837:   }
2838:   return(0);
2839: }

2841: PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
2842: {
2843:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
2844:   PetscScalar    *v;
2846:   PetscBLASInt   one = 1,bnz;

2849:   MatSeqAIJGetArray(inA,&v);
2850:   PetscBLASIntCast(a->nz,&bnz);
2851:   PetscStackCallBLAS("BLASscal",BLASscal_(&bnz,&alpha,v,&one));
2852:   PetscLogFlops(a->nz);
2853:   MatSeqAIJRestoreArray(inA,&v);
2854:   MatSeqAIJInvalidateDiagonal(inA);
2855:   return(0);
2856: }

2858: PetscErrorCode MatDestroySubMatrix_Private(Mat_SubSppt *submatj)
2859: {
2861:   PetscInt       i;

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

2867:     for (i=0; i<submatj->nrqr; ++i) {
2868:       PetscFree(submatj->sbuf2[i]);
2869:     }
2870:     PetscFree3(submatj->sbuf2,submatj->req_size,submatj->req_source1);

2872:     if (submatj->rbuf1) {
2873:       PetscFree(submatj->rbuf1[0]);
2874:       PetscFree(submatj->rbuf1);
2875:     }

2877:     for (i=0; i<submatj->nrqs; ++i) {
2878:       PetscFree(submatj->rbuf3[i]);
2879:     }
2880:     PetscFree3(submatj->req_source2,submatj->rbuf2,submatj->rbuf3);
2881:     PetscFree(submatj->pa);
2882:   }

2884: #if defined(PETSC_USE_CTABLE)
2885:   PetscTableDestroy((PetscTable*)&submatj->rmap);
2886:   if (submatj->cmap_loc) {PetscFree(submatj->cmap_loc);}
2887:   PetscFree(submatj->rmap_loc);
2888: #else
2889:   PetscFree(submatj->rmap);
2890: #endif

2892:   if (!submatj->allcolumns) {
2893: #if defined(PETSC_USE_CTABLE)
2894:     PetscTableDestroy((PetscTable*)&submatj->cmap);
2895: #else
2896:     PetscFree(submatj->cmap);
2897: #endif
2898:   }
2899:   PetscFree(submatj->row2proc);

2901:   PetscFree(submatj);
2902:   return(0);
2903: }

2905: PetscErrorCode MatDestroySubMatrix_SeqAIJ(Mat C)
2906: {
2908:   Mat_SeqAIJ     *c = (Mat_SeqAIJ*)C->data;
2909:   Mat_SubSppt    *submatj = c->submatis1;

2912:   (*submatj->destroy)(C);
2913:   MatDestroySubMatrix_Private(submatj);
2914:   return(0);
2915: }

2917: PetscErrorCode MatDestroySubMatrices_SeqAIJ(PetscInt n,Mat *mat[])
2918: {
2920:   PetscInt       i;
2921:   Mat            C;
2922:   Mat_SeqAIJ     *c;
2923:   Mat_SubSppt    *submatj;

2926:   for (i=0; i<n; i++) {
2927:     C       = (*mat)[i];
2928:     c       = (Mat_SeqAIJ*)C->data;
2929:     submatj = c->submatis1;
2930:     if (submatj) {
2931:       if (--((PetscObject)C)->refct <= 0) {
2932:         (*submatj->destroy)(C);
2933:         MatDestroySubMatrix_Private(submatj);
2934:         PetscFree(C->defaultvectype);
2935:         PetscLayoutDestroy(&C->rmap);
2936:         PetscLayoutDestroy(&C->cmap);
2937:         PetscHeaderDestroy(&C);
2938:       }
2939:     } else {
2940:       MatDestroy(&C);
2941:     }
2942:   }

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

2947:   PetscFree(*mat);
2948:   return(0);
2949: }

2951: PetscErrorCode MatCreateSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
2952: {
2954:   PetscInt       i;

2957:   if (scall == MAT_INITIAL_MATRIX) {
2958:     PetscCalloc1(n+1,B);
2959:   }

2961:   for (i=0; i<n; i++) {
2962:     MatCreateSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);
2963:   }
2964:   return(0);
2965: }

2967: PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
2968: {
2969:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2971:   PetscInt       row,i,j,k,l,m,n,*nidx,isz,val;
2972:   const PetscInt *idx;
2973:   PetscInt       start,end,*ai,*aj;
2974:   PetscBT        table;

2977:   m  = A->rmap->n;
2978:   ai = a->i;
2979:   aj = a->j;

2981:   if (ov < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"illegal negative overlap value used");

2983:   PetscMalloc1(m+1,&nidx);
2984:   PetscBTCreate(m,&table);

2986:   for (i=0; i<is_max; i++) {
2987:     /* Initialize the two local arrays */
2988:     isz  = 0;
2989:     PetscBTMemzero(m,table);

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

2995:     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
2996:     for (j=0; j<n; ++j) {
2997:       if (!PetscBTLookupSet(table,idx[j])) nidx[isz++] = idx[j];
2998:     }
2999:     ISRestoreIndices(is[i],&idx);
3000:     ISDestroy(&is[i]);

3002:     k = 0;
3003:     for (j=0; j<ov; j++) { /* for each overlap */
3004:       n = isz;
3005:       for (; k<n; k++) { /* do only those rows in nidx[k], which are not done yet */
3006:         row   = nidx[k];
3007:         start = ai[row];
3008:         end   = ai[row+1];
3009:         for (l = start; l<end; l++) {
3010:           val = aj[l];
3011:           if (!PetscBTLookupSet(table,val)) nidx[isz++] = val;
3012:         }
3013:       }
3014:     }
3015:     ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,PETSC_COPY_VALUES,(is+i));
3016:   }
3017:   PetscBTDestroy(&table);
3018:   PetscFree(nidx);
3019:   return(0);
3020: }

3022: /* -------------------------------------------------------------- */
3023: PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
3024: {
3025:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
3027:   PetscInt       i,nz = 0,m = A->rmap->n,n = A->cmap->n;
3028:   const PetscInt *row,*col;
3029:   PetscInt       *cnew,j,*lens;
3030:   IS             icolp,irowp;
3031:   PetscInt       *cwork = NULL;
3032:   PetscScalar    *vwork = NULL;

3035:   ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);
3036:   ISGetIndices(irowp,&row);
3037:   ISInvertPermutation(colp,PETSC_DECIDE,&icolp);
3038:   ISGetIndices(icolp,&col);

3040:   /* determine lengths of permuted rows */
3041:   PetscMalloc1(m+1,&lens);
3042:   for (i=0; i<m; i++) lens[row[i]] = a->i[i+1] - a->i[i];
3043:   MatCreate(PetscObjectComm((PetscObject)A),B);
3044:   MatSetSizes(*B,m,n,m,n);
3045:   MatSetBlockSizesFromMats(*B,A,A);
3046:   MatSetType(*B,((PetscObject)A)->type_name);
3047:   MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);
3048:   PetscFree(lens);

3050:   PetscMalloc1(n,&cnew);
3051:   for (i=0; i<m; i++) {
3052:     MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);
3053:     for (j=0; j<nz; j++) cnew[j] = col[cwork[j]];
3054:     MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);
3055:     MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);
3056:   }
3057:   PetscFree(cnew);

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

3061: #if defined(PETSC_HAVE_DEVICE)
3062:   MatBindToCPU(*B,A->boundtocpu);
3063: #endif
3064:   MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);
3065:   MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);
3066:   ISRestoreIndices(irowp,&row);
3067:   ISRestoreIndices(icolp,&col);
3068:   ISDestroy(&irowp);
3069:   ISDestroy(&icolp);
3070:   if (rowp == colp) {
3071:     MatPropagateSymmetryOptions(A,*B);
3072:   }
3073:   return(0);
3074: }

3076: PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
3077: {

3081:   /* If the two matrices have the same copy implementation, use fast copy. */
3082:   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
3083:     Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
3084:     Mat_SeqAIJ        *b = (Mat_SeqAIJ*)B->data;
3085:     const PetscScalar *aa;

3087:     MatSeqAIJGetArrayRead(A,&aa);
3088:     if (a->i[A->rmap->n] != b->i[B->rmap->n]) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Number of nonzeros in two matrices are different %D != %D",a->i[A->rmap->n],b->i[B->rmap->n]);
3089:     PetscArraycpy(b->a,aa,a->i[A->rmap->n]);
3090:     PetscObjectStateIncrease((PetscObject)B);
3091:     MatSeqAIJRestoreArrayRead(A,&aa);
3092:   } else {
3093:     MatCopy_Basic(A,B,str);
3094:   }
3095:   return(0);
3096: }

3098: PetscErrorCode MatSetUp_SeqAIJ(Mat A)
3099: {

3103:   MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,NULL);
3104:   return(0);
3105: }

3107: PETSC_INTERN PetscErrorCode MatSeqAIJGetArray_SeqAIJ(Mat A,PetscScalar *array[])
3108: {
3109:   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;

3112:   *array = a->a;
3113:   return(0);
3114: }

3116: PETSC_INTERN PetscErrorCode MatSeqAIJRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
3117: {
3119:   *array = NULL;
3120:   return(0);
3121: }

3123: /*
3124:    Computes the number of nonzeros per row needed for preallocation when X and Y
3125:    have different nonzero structure.
3126: */
3127: PetscErrorCode MatAXPYGetPreallocation_SeqX_private(PetscInt m,const PetscInt *xi,const PetscInt *xj,const PetscInt *yi,const PetscInt *yj,PetscInt *nnz)
3128: {
3129:   PetscInt       i,j,k,nzx,nzy;

3132:   /* Set the number of nonzeros in the new matrix */
3133:   for (i=0; i<m; i++) {
3134:     const PetscInt *xjj = xj+xi[i],*yjj = yj+yi[i];
3135:     nzx = xi[i+1] - xi[i];
3136:     nzy = yi[i+1] - yi[i];
3137:     nnz[i] = 0;
3138:     for (j=0,k=0; j<nzx; j++) {                   /* Point in X */
3139:       for (; k<nzy && yjj[k]<xjj[j]; k++) nnz[i]++; /* Catch up to X */
3140:       if (k<nzy && yjj[k]==xjj[j]) k++;             /* Skip duplicate */
3141:       nnz[i]++;
3142:     }
3143:     for (; k<nzy; k++) nnz[i]++;
3144:   }
3145:   return(0);
3146: }

3148: PetscErrorCode MatAXPYGetPreallocation_SeqAIJ(Mat Y,Mat X,PetscInt *nnz)
3149: {
3150:   PetscInt       m = Y->rmap->N;
3151:   Mat_SeqAIJ     *x = (Mat_SeqAIJ*)X->data;
3152:   Mat_SeqAIJ     *y = (Mat_SeqAIJ*)Y->data;

3156:   /* Set the number of nonzeros in the new matrix */
3157:   MatAXPYGetPreallocation_SeqX_private(m,x->i,x->j,y->i,y->j,nnz);
3158:   return(0);
3159: }

3161: PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
3162: {
3164:   Mat_SeqAIJ     *x = (Mat_SeqAIJ*)X->data,*y = (Mat_SeqAIJ*)Y->data;

3167:   if (str == UNKNOWN_NONZERO_PATTERN && x->nz == y->nz) {
3168:     PetscBool e;
3169:     PetscArraycmp(x->i,y->i,Y->rmap->n+1,&e);
3170:     if (e) {
3171:       PetscArraycmp(x->j,y->j,y->nz,&e);
3172:       if (e) {
3173:         str = SAME_NONZERO_PATTERN;
3174:       }
3175:     }
3176:   }
3177:   if (str == SAME_NONZERO_PATTERN) {
3178:     const PetscScalar *xa;
3179:     PetscScalar       *ya,alpha = a;
3180:     PetscBLASInt      one = 1,bnz;

3182:     PetscBLASIntCast(x->nz,&bnz);
3183:     MatSeqAIJGetArray(Y,&ya);
3184:     MatSeqAIJGetArrayRead(X,&xa);
3185:     PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,xa,&one,ya,&one));
3186:     MatSeqAIJRestoreArrayRead(X,&xa);
3187:     MatSeqAIJRestoreArray(Y,&ya);
3188:     PetscLogFlops(2.0*bnz);
3189:     MatSeqAIJInvalidateDiagonal(Y);
3190:     PetscObjectStateIncrease((PetscObject)Y);
3191:   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
3192:     MatAXPY_Basic(Y,a,X,str);
3193:   } else {
3194:     Mat      B;
3195:     PetscInt *nnz;
3196:     PetscMalloc1(Y->rmap->N,&nnz);
3197:     MatCreate(PetscObjectComm((PetscObject)Y),&B);
3198:     PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);
3199:     MatSetLayouts(B,Y->rmap,Y->cmap);
3200:     MatSetType(B,((PetscObject)Y)->type_name);
3201:     MatAXPYGetPreallocation_SeqAIJ(Y,X,nnz);
3202:     MatSeqAIJSetPreallocation(B,0,nnz);
3203:     MatAXPY_BasicWithPreallocation(B,Y,a,X,str);
3204:     MatHeaderReplace(Y,&B);
3205:     PetscFree(nnz);
3206:   }
3207:   return(0);
3208: }

3210: PETSC_INTERN PetscErrorCode MatConjugate_SeqAIJ(Mat mat)
3211: {
3212: #if defined(PETSC_USE_COMPLEX)
3213:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)mat->data;
3214:   PetscInt       i,nz;
3215:   PetscScalar    *a;

3219:   nz = aij->nz;
3220:   MatSeqAIJGetArray(mat,&a);
3221:   for (i=0; i<nz; i++) a[i] = PetscConj(a[i]);
3222:   MatSeqAIJRestoreArray(mat,&a);
3223: #else
3225: #endif
3226:   return(0);
3227: }

3229: PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
3230: {
3231:   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3232:   PetscErrorCode  ierr;
3233:   PetscInt        i,j,m = A->rmap->n,*ai,*aj,ncols,n;
3234:   PetscReal       atmp;
3235:   PetscScalar     *x;
3236:   const MatScalar *aa,*av;

3239:   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3240:   MatSeqAIJGetArrayRead(A,&av);
3241:   aa = av;
3242:   ai = a->i;
3243:   aj = a->j;

3245:   VecSet(v,0.0);
3246:   VecGetArrayWrite(v,&x);
3247:   VecGetLocalSize(v,&n);
3248:   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
3249:   for (i=0; i<m; i++) {
3250:     ncols = ai[1] - ai[0]; ai++;
3251:     for (j=0; j<ncols; j++) {
3252:       atmp = PetscAbsScalar(*aa);
3253:       if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
3254:       aa++; aj++;
3255:     }
3256:   }
3257:   VecRestoreArrayWrite(v,&x);
3258:   MatSeqAIJRestoreArrayRead(A,&av);
3259:   return(0);
3260: }

3262: PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[])
3263: {
3264:   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3265:   PetscErrorCode  ierr;
3266:   PetscInt        i,j,m = A->rmap->n,*ai,*aj,ncols,n;
3267:   PetscScalar     *x;
3268:   const MatScalar *aa,*av;

3271:   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3272:   MatSeqAIJGetArrayRead(A,&av);
3273:   aa = av;
3274:   ai = a->i;
3275:   aj = a->j;

3277:   VecSet(v,0.0);
3278:   VecGetArrayWrite(v,&x);
3279:   VecGetLocalSize(v,&n);
3280:   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
3281:   for (i=0; i<m; i++) {
3282:     ncols = ai[1] - ai[0]; ai++;
3283:     if (ncols == A->cmap->n) { /* row is dense */
3284:       x[i] = *aa; if (idx) idx[i] = 0;
3285:     } else {  /* row is sparse so already KNOW maximum is 0.0 or higher */
3286:       x[i] = 0.0;
3287:       if (idx) {
3288:         for (j=0; j<ncols; j++) { /* find first implicit 0.0 in the row */
3289:           if (aj[j] > j) {
3290:             idx[i] = j;
3291:             break;
3292:           }
3293:         }
3294:         /* in case first implicit 0.0 in the row occurs at ncols-th column */
3295:         if (j==ncols && j < A->cmap->n) idx[i] = j;
3296:       }
3297:     }
3298:     for (j=0; j<ncols; j++) {
3299:       if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
3300:       aa++; aj++;
3301:     }
3302:   }
3303:   VecRestoreArrayWrite(v,&x);
3304:   MatSeqAIJRestoreArrayRead(A,&av);
3305:   return(0);
3306: }

3308: PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
3309: {
3310:   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3311:   PetscErrorCode  ierr;
3312:   PetscInt        i,j,m = A->rmap->n,*ai,*aj,ncols,n;
3313:   PetscScalar     *x;
3314:   const MatScalar *aa,*av;

3317:   MatSeqAIJGetArrayRead(A,&av);
3318:   aa = av;
3319:   ai = a->i;
3320:   aj = a->j;

3322:   VecSet(v,0.0);
3323:   VecGetArrayWrite(v,&x);
3324:   VecGetLocalSize(v,&n);
3325:   if (n != m) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector, %D vs. %D rows", m, n);
3326:   for (i=0; i<m; i++) {
3327:     ncols = ai[1] - ai[0]; ai++;
3328:     if (ncols == A->cmap->n) { /* row is dense */
3329:       x[i] = *aa; if (idx) idx[i] = 0;
3330:     } else {  /* row is sparse so already KNOW minimum is 0.0 or higher */
3331:       x[i] = 0.0;
3332:       if (idx) {   /* find first implicit 0.0 in the row */
3333:         for (j=0; j<ncols; j++) {
3334:           if (aj[j] > j) {
3335:             idx[i] = j;
3336:             break;
3337:           }
3338:         }
3339:         /* in case first implicit 0.0 in the row occurs at ncols-th column */
3340:         if (j==ncols && j < A->cmap->n) idx[i] = j;
3341:       }
3342:     }
3343:     for (j=0; j<ncols; j++) {
3344:       if (PetscAbsScalar(x[i]) > PetscAbsScalar(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
3345:       aa++; aj++;
3346:     }
3347:   }
3348:   VecRestoreArrayWrite(v,&x);
3349:   MatSeqAIJRestoreArrayRead(A,&av);
3350:   return(0);
3351: }

3353: PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[])
3354: {
3355:   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3356:   PetscErrorCode  ierr;
3357:   PetscInt        i,j,m = A->rmap->n,ncols,n;
3358:   const PetscInt  *ai,*aj;
3359:   PetscScalar     *x;
3360:   const MatScalar *aa,*av;

3363:   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3364:   MatSeqAIJGetArrayRead(A,&av);
3365:   aa = av;
3366:   ai = a->i;
3367:   aj = a->j;

3369:   VecSet(v,0.0);
3370:   VecGetArrayWrite(v,&x);
3371:   VecGetLocalSize(v,&n);
3372:   if (n != m) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
3373:   for (i=0; i<m; i++) {
3374:     ncols = ai[1] - ai[0]; ai++;
3375:     if (ncols == A->cmap->n) { /* row is dense */
3376:       x[i] = *aa; if (idx) idx[i] = 0;
3377:     } else {  /* row is sparse so already KNOW minimum is 0.0 or lower */
3378:       x[i] = 0.0;
3379:       if (idx) {   /* find first implicit 0.0 in the row */
3380:         for (j=0; j<ncols; j++) {
3381:           if (aj[j] > j) {
3382:             idx[i] = j;
3383:             break;
3384:           }
3385:         }
3386:         /* in case first implicit 0.0 in the row occurs at ncols-th column */
3387:         if (j==ncols && j < A->cmap->n) idx[i] = j;
3388:       }
3389:     }
3390:     for (j=0; j<ncols; j++) {
3391:       if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
3392:       aa++; aj++;
3393:     }
3394:   }
3395:   VecRestoreArrayWrite(v,&x);
3396:   MatSeqAIJRestoreArrayRead(A,&av);
3397:   return(0);
3398: }

3400: PetscErrorCode MatInvertBlockDiagonal_SeqAIJ(Mat A,const PetscScalar **values)
3401: {
3402:   Mat_SeqAIJ      *a = (Mat_SeqAIJ*) A->data;
3403:   PetscErrorCode  ierr;
3404:   PetscInt        i,bs = PetscAbs(A->rmap->bs),mbs = A->rmap->n/bs,ipvt[5],bs2 = bs*bs,*v_pivots,ij[7],*IJ,j;
3405:   MatScalar       *diag,work[25],*v_work;
3406:   const PetscReal shift = 0.0;
3407:   PetscBool       allowzeropivot,zeropivotdetected=PETSC_FALSE;

3410:   allowzeropivot = PetscNot(A->erroriffailure);
3411:   if (a->ibdiagvalid) {
3412:     if (values) *values = a->ibdiag;
3413:     return(0);
3414:   }
3415:   MatMarkDiagonal_SeqAIJ(A);
3416:   if (!a->ibdiag) {
3417:     PetscMalloc1(bs2*mbs,&a->ibdiag);
3418:     PetscLogObjectMemory((PetscObject)A,bs2*mbs*sizeof(PetscScalar));
3419:   }
3420:   diag = a->ibdiag;
3421:   if (values) *values = a->ibdiag;
3422:   /* factor and invert each block */
3423:   switch (bs) {
3424:   case 1:
3425:     for (i=0; i<mbs; i++) {
3426:       MatGetValues(A,1,&i,1,&i,diag+i);
3427:       if (PetscAbsScalar(diag[i] + shift) < PETSC_MACHINE_EPSILON) {
3428:         if (allowzeropivot) {
3429:           A->factorerrortype             = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3430:           A->factorerror_zeropivot_value = PetscAbsScalar(diag[i]);
3431:           A->factorerror_zeropivot_row   = i;
3432:           PetscInfo3(A,"Zero pivot, row %D pivot %g tolerance %g\n",i,(double)PetscAbsScalar(diag[i]),(double)PETSC_MACHINE_EPSILON);
3433:         } else SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_MAT_LU_ZRPVT,"Zero pivot, row %D pivot %g tolerance %g",i,(double)PetscAbsScalar(diag[i]),(double)PETSC_MACHINE_EPSILON);
3434:       }
3435:       diag[i] = (PetscScalar)1.0 / (diag[i] + shift);
3436:     }
3437:     break;
3438:   case 2:
3439:     for (i=0; i<mbs; i++) {
3440:       ij[0] = 2*i; ij[1] = 2*i + 1;
3441:       MatGetValues(A,2,ij,2,ij,diag);
3442:       PetscKernel_A_gets_inverse_A_2(diag,shift,allowzeropivot,&zeropivotdetected);
3443:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3444:       PetscKernel_A_gets_transpose_A_2(diag);
3445:       diag += 4;
3446:     }
3447:     break;
3448:   case 3:
3449:     for (i=0; i<mbs; i++) {
3450:       ij[0] = 3*i; ij[1] = 3*i + 1; ij[2] = 3*i + 2;
3451:       MatGetValues(A,3,ij,3,ij,diag);
3452:       PetscKernel_A_gets_inverse_A_3(diag,shift,allowzeropivot,&zeropivotdetected);
3453:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3454:       PetscKernel_A_gets_transpose_A_3(diag);
3455:       diag += 9;
3456:     }
3457:     break;
3458:   case 4:
3459:     for (i=0; i<mbs; i++) {
3460:       ij[0] = 4*i; ij[1] = 4*i + 1; ij[2] = 4*i + 2; ij[3] = 4*i + 3;
3461:       MatGetValues(A,4,ij,4,ij,diag);
3462:       PetscKernel_A_gets_inverse_A_4(diag,shift,allowzeropivot,&zeropivotdetected);
3463:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3464:       PetscKernel_A_gets_transpose_A_4(diag);
3465:       diag += 16;
3466:     }
3467:     break;
3468:   case 5:
3469:     for (i=0; i<mbs; i++) {
3470:       ij[0] = 5*i; ij[1] = 5*i + 1; ij[2] = 5*i + 2; ij[3] = 5*i + 3; ij[4] = 5*i + 4;
3471:       MatGetValues(A,5,ij,5,ij,diag);
3472:       PetscKernel_A_gets_inverse_A_5(diag,ipvt,work,shift,allowzeropivot,&zeropivotdetected);
3473:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3474:       PetscKernel_A_gets_transpose_A_5(diag);
3475:       diag += 25;
3476:     }
3477:     break;
3478:   case 6:
3479:     for (i=0; i<mbs; i++) {
3480:       ij[0] = 6*i; ij[1] = 6*i + 1; ij[2] = 6*i + 2; ij[3] = 6*i + 3; ij[4] = 6*i + 4; ij[5] = 6*i + 5;
3481:       MatGetValues(A,6,ij,6,ij,diag);
3482:       PetscKernel_A_gets_inverse_A_6(diag,shift,allowzeropivot,&zeropivotdetected);
3483:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3484:       PetscKernel_A_gets_transpose_A_6(diag);
3485:       diag += 36;
3486:     }
3487:     break;
3488:   case 7:
3489:     for (i=0; i<mbs; i++) {
3490:       ij[0] = 7*i; ij[1] = 7*i + 1; ij[2] = 7*i + 2; ij[3] = 7*i + 3; ij[4] = 7*i + 4; ij[5] = 7*i + 5; ij[5] = 7*i + 6;
3491:       MatGetValues(A,7,ij,7,ij,diag);
3492:       PetscKernel_A_gets_inverse_A_7(diag,shift,allowzeropivot,&zeropivotdetected);
3493:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3494:       PetscKernel_A_gets_transpose_A_7(diag);
3495:       diag += 49;
3496:     }
3497:     break;
3498:   default:
3499:     PetscMalloc3(bs,&v_work,bs,&v_pivots,bs,&IJ);
3500:     for (i=0; i<mbs; i++) {
3501:       for (j=0; j<bs; j++) {
3502:         IJ[j] = bs*i + j;
3503:       }
3504:       MatGetValues(A,bs,IJ,bs,IJ,diag);
3505:       PetscKernel_A_gets_inverse_A(bs,diag,v_pivots,v_work,allowzeropivot,&zeropivotdetected);
3506:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3507:       PetscKernel_A_gets_transpose_A_N(diag,bs);
3508:       diag += bs2;
3509:     }
3510:     PetscFree3(v_work,v_pivots,IJ);
3511:   }
3512:   a->ibdiagvalid = PETSC_TRUE;
3513:   return(0);
3514: }

3516: static PetscErrorCode  MatSetRandom_SeqAIJ(Mat x,PetscRandom rctx)
3517: {
3519:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)x->data;
3520:   PetscScalar    a;
3521:   PetscInt       m,n,i,j,col;

3524:   if (!x->assembled) {
3525:     MatGetSize(x,&m,&n);
3526:     for (i=0; i<m; i++) {
3527:       for (j=0; j<aij->imax[i]; j++) {
3528:         PetscRandomGetValue(rctx,&a);
3529:         col  = (PetscInt)(n*PetscRealPart(a));
3530:         MatSetValues(x,1,&i,1,&col,&a,ADD_VALUES);
3531:       }
3532:     }
3533:   } else {
3534:     for (i=0; i<aij->nz; i++) {PetscRandomGetValue(rctx,aij->a+i);}
3535:   }
3536: #if defined(PETSC_HAVE_DEVICE)
3537:   if (x->offloadmask != PETSC_OFFLOAD_UNALLOCATED) x->offloadmask = PETSC_OFFLOAD_CPU;
3538: #endif
3539:   MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);
3540:   MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);
3541:   return(0);
3542: }

3544: /* Like MatSetRandom_SeqAIJ, but do not set values on columns in range of [low, high) */
3545: PetscErrorCode  MatSetRandomSkipColumnRange_SeqAIJ_Private(Mat x,PetscInt low,PetscInt high,PetscRandom rctx)
3546: {
3548:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)x->data;
3549:   PetscScalar    a;
3550:   PetscInt       m,n,i,j,col,nskip;

3553:   nskip = high - low;
3554:   MatGetSize(x,&m,&n);
3555:   n    -= nskip; /* shrink number of columns where nonzeros can be set */
3556:   for (i=0; i<m; i++) {
3557:     for (j=0; j<aij->imax[i]; j++) {
3558:       PetscRandomGetValue(rctx,&a);
3559:       col  = (PetscInt)(n*PetscRealPart(a));
3560:       if (col >= low) col += nskip; /* shift col rightward to skip the hole */
3561:       MatSetValues(x,1,&i,1,&col,&a,ADD_VALUES);
3562:     }
3563:   }
3564:   MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);
3565:   MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);
3566:   return(0);
3567: }


3570: /* -------------------------------------------------------------------*/
3571: static struct _MatOps MatOps_Values = { MatSetValues_SeqAIJ,
3572:                                         MatGetRow_SeqAIJ,
3573:                                         MatRestoreRow_SeqAIJ,
3574:                                         MatMult_SeqAIJ,
3575:                                 /*  4*/ MatMultAdd_SeqAIJ,
3576:                                         MatMultTranspose_SeqAIJ,
3577:                                         MatMultTransposeAdd_SeqAIJ,
3578:                                         NULL,
3579:                                         NULL,
3580:                                         NULL,
3581:                                 /* 10*/ NULL,
3582:                                         MatLUFactor_SeqAIJ,
3583:                                         NULL,
3584:                                         MatSOR_SeqAIJ,
3585:                                         MatTranspose_SeqAIJ,
3586:                                 /*1 5*/ MatGetInfo_SeqAIJ,
3587:                                         MatEqual_SeqAIJ,
3588:                                         MatGetDiagonal_SeqAIJ,
3589:                                         MatDiagonalScale_SeqAIJ,
3590:                                         MatNorm_SeqAIJ,
3591:                                 /* 20*/ NULL,
3592:                                         MatAssemblyEnd_SeqAIJ,
3593:                                         MatSetOption_SeqAIJ,
3594:                                         MatZeroEntries_SeqAIJ,
3595:                                 /* 24*/ MatZeroRows_SeqAIJ,
3596:                                         NULL,
3597:                                         NULL,
3598:                                         NULL,
3599:                                         NULL,
3600:                                 /* 29*/ MatSetUp_SeqAIJ,
3601:                                         NULL,
3602:                                         NULL,
3603:                                         NULL,
3604:                                         NULL,
3605:                                 /* 34*/ MatDuplicate_SeqAIJ,
3606:                                         NULL,
3607:                                         NULL,
3608:                                         MatILUFactor_SeqAIJ,
3609:                                         NULL,
3610:                                 /* 39*/ MatAXPY_SeqAIJ,
3611:                                         MatCreateSubMatrices_SeqAIJ,
3612:                                         MatIncreaseOverlap_SeqAIJ,
3613:                                         MatGetValues_SeqAIJ,
3614:                                         MatCopy_SeqAIJ,
3615:                                 /* 44*/ MatGetRowMax_SeqAIJ,
3616:                                         MatScale_SeqAIJ,
3617:                                         MatShift_SeqAIJ,
3618:                                         MatDiagonalSet_SeqAIJ,
3619:                                         MatZeroRowsColumns_SeqAIJ,
3620:                                 /* 49*/ MatSetRandom_SeqAIJ,
3621:                                         MatGetRowIJ_SeqAIJ,
3622:                                         MatRestoreRowIJ_SeqAIJ,
3623:                                         MatGetColumnIJ_SeqAIJ,
3624:                                         MatRestoreColumnIJ_SeqAIJ,
3625:                                 /* 54*/ MatFDColoringCreate_SeqXAIJ,
3626:                                         NULL,
3627:                                         NULL,
3628:                                         MatPermute_SeqAIJ,
3629:                                         NULL,
3630:                                 /* 59*/ NULL,
3631:                                         MatDestroy_SeqAIJ,
3632:                                         MatView_SeqAIJ,
3633:                                         NULL,
3634:                                         NULL,
3635:                                 /* 64*/ NULL,
3636:                                         MatMatMatMultNumeric_SeqAIJ_SeqAIJ_SeqAIJ,
3637:                                         NULL,
3638:                                         NULL,
3639:                                         NULL,
3640:                                 /* 69*/ MatGetRowMaxAbs_SeqAIJ,
3641:                                         MatGetRowMinAbs_SeqAIJ,
3642:                                         NULL,
3643:                                         NULL,
3644:                                         NULL,
3645:                                 /* 74*/ NULL,
3646:                                         MatFDColoringApply_AIJ,
3647:                                         NULL,
3648:                                         NULL,
3649:                                         NULL,
3650:                                 /* 79*/ MatFindZeroDiagonals_SeqAIJ,
3651:                                         NULL,
3652:                                         NULL,
3653:                                         NULL,
3654:                                         MatLoad_SeqAIJ,
3655:                                 /* 84*/ MatIsSymmetric_SeqAIJ,
3656:                                         MatIsHermitian_SeqAIJ,
3657:                                         NULL,
3658:                                         NULL,
3659:                                         NULL,
3660:                                 /* 89*/ NULL,
3661:                                         NULL,
3662:                                         MatMatMultNumeric_SeqAIJ_SeqAIJ,
3663:                                         NULL,
3664:                                         NULL,
3665:                                 /* 94*/ MatPtAPNumeric_SeqAIJ_SeqAIJ_SparseAxpy,
3666:                                         NULL,
3667:                                         NULL,
3668:                                         MatMatTransposeMultNumeric_SeqAIJ_SeqAIJ,
3669:                                         NULL,
3670:                                 /* 99*/ MatProductSetFromOptions_SeqAIJ,
3671:                                         NULL,
3672:                                         NULL,
3673:                                         MatConjugate_SeqAIJ,
3674:                                         NULL,
3675:                                 /*104*/ MatSetValuesRow_SeqAIJ,
3676:                                         MatRealPart_SeqAIJ,
3677:                                         MatImaginaryPart_SeqAIJ,
3678:                                         NULL,
3679:                                         NULL,
3680:                                 /*109*/ MatMatSolve_SeqAIJ,
3681:                                         NULL,
3682:                                         MatGetRowMin_SeqAIJ,
3683:                                         NULL,
3684:                                         MatMissingDiagonal_SeqAIJ,
3685:                                 /*114*/ NULL,
3686:                                         NULL,
3687:                                         NULL,
3688:                                         NULL,
3689:                                         NULL,
3690:                                 /*119*/ NULL,
3691:                                         NULL,
3692:                                         NULL,
3693:                                         NULL,
3694:                                         MatGetMultiProcBlock_SeqAIJ,
3695:                                 /*124*/ MatFindNonzeroRows_SeqAIJ,
3696:                                         MatGetColumnNorms_SeqAIJ,
3697:                                         MatInvertBlockDiagonal_SeqAIJ,
3698:                                         MatInvertVariableBlockDiagonal_SeqAIJ,
3699:                                         NULL,
3700:                                 /*129*/ NULL,
3701:                                         NULL,
3702:                                         NULL,
3703:                                         MatTransposeMatMultNumeric_SeqAIJ_SeqAIJ,
3704:                                         MatTransposeColoringCreate_SeqAIJ,
3705:                                 /*134*/ MatTransColoringApplySpToDen_SeqAIJ,
3706:                                         MatTransColoringApplyDenToSp_SeqAIJ,
3707:                                         NULL,
3708:                                         NULL,
3709:                                         MatRARtNumeric_SeqAIJ_SeqAIJ,
3710:                                  /*139*/NULL,
3711:                                         NULL,
3712:                                         NULL,
3713:                                         MatFDColoringSetUp_SeqXAIJ,
3714:                                         MatFindOffBlockDiagonalEntries_SeqAIJ,
3715:                                         MatCreateMPIMatConcatenateSeqMat_SeqAIJ,
3716:                                  /*145*/MatDestroySubMatrices_SeqAIJ,
3717:                                         NULL,
3718:                                         NULL
3719: };

3721: PetscErrorCode  MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
3722: {
3723:   Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data;
3724:   PetscInt   i,nz,n;

3727:   nz = aij->maxnz;
3728:   n  = mat->rmap->n;
3729:   for (i=0; i<nz; i++) {
3730:     aij->j[i] = indices[i];
3731:   }
3732:   aij->nz = nz;
3733:   for (i=0; i<n; i++) {
3734:     aij->ilen[i] = aij->imax[i];
3735:   }
3736:   return(0);
3737: }

3739: /*
3740:  * When a sparse matrix has many zero columns, we should compact them out to save the space
3741:  * This happens in MatPtAPSymbolic_MPIAIJ_MPIAIJ_scalable()
3742:  * */
3743: PetscErrorCode  MatSeqAIJCompactOutExtraColumns_SeqAIJ(Mat mat, ISLocalToGlobalMapping *mapping)
3744: {
3745:   Mat_SeqAIJ         *aij = (Mat_SeqAIJ*)mat->data;
3746:   PetscTable         gid1_lid1;
3747:   PetscTablePosition tpos;
3748:   PetscInt           gid,lid,i,ec,nz = aij->nz;
3749:   PetscInt           *garray,*jj = aij->j;
3750:   PetscErrorCode     ierr;

3755:   /* use a table */
3756:   PetscTableCreate(mat->rmap->n,mat->cmap->N+1,&gid1_lid1);
3757:   ec = 0;
3758:   for (i=0; i<nz; i++) {
3759:     PetscInt data,gid1 = jj[i] + 1;
3760:     PetscTableFind(gid1_lid1,gid1,&data);
3761:     if (!data) {
3762:       /* one based table */
3763:       PetscTableAdd(gid1_lid1,gid1,++ec,INSERT_VALUES);
3764:     }
3765:   }
3766:   /* form array of columns we need */
3767:   PetscMalloc1(ec+1,&garray);
3768:   PetscTableGetHeadPosition(gid1_lid1,&tpos);
3769:   while (tpos) {
3770:     PetscTableGetNext(gid1_lid1,&tpos,&gid,&lid);
3771:     gid--;
3772:     lid--;
3773:     garray[lid] = gid;
3774:   }
3775:   PetscSortInt(ec,garray); /* sort, and rebuild */
3776:   PetscTableRemoveAll(gid1_lid1);
3777:   for (i=0; i<ec; i++) {
3778:     PetscTableAdd(gid1_lid1,garray[i]+1,i+1,INSERT_VALUES);
3779:   }
3780:   /* compact out the extra columns in B */
3781:   for (i=0; i<nz; i++) {
3782:     PetscInt gid1 = jj[i] + 1;
3783:     PetscTableFind(gid1_lid1,gid1,&lid);
3784:     lid--;
3785:     jj[i] = lid;
3786:   }
3787:   PetscLayoutDestroy(&mat->cmap);
3788:   PetscTableDestroy(&gid1_lid1);
3789:   PetscLayoutCreateFromSizes(PetscObjectComm((PetscObject)mat),ec,ec,1,&mat->cmap);
3790:   ISLocalToGlobalMappingCreate(PETSC_COMM_SELF,mat->cmap->bs,mat->cmap->n,garray,PETSC_OWN_POINTER,mapping);
3791:   ISLocalToGlobalMappingSetType(*mapping,ISLOCALTOGLOBALMAPPINGHASH);
3792:   return(0);
3793: }

3795: /*@
3796:     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
3797:        in the matrix.

3799:   Input Parameters:
3800: +  mat - the SeqAIJ matrix
3801: -  indices - the column indices

3803:   Level: advanced

3805:   Notes:
3806:     This can be called if you have precomputed the nonzero structure of the
3807:   matrix and want to provide it to the matrix object to improve the performance
3808:   of the MatSetValues() operation.

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

3813:     MUST be called before any calls to MatSetValues();

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

3817: @*/
3818: PetscErrorCode  MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
3819: {

3825:   PetscUseMethod(mat,"MatSeqAIJSetColumnIndices_C",(Mat,PetscInt*),(mat,indices));
3826:   return(0);
3827: }

3829: /* ----------------------------------------------------------------------------------------*/

3831: PetscErrorCode  MatStoreValues_SeqAIJ(Mat mat)
3832: {
3833:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)mat->data;
3835:   size_t         nz = aij->i[mat->rmap->n];

3838:   if (!aij->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");

3840:   /* allocate space for values if not already there */
3841:   if (!aij->saved_values) {
3842:     PetscMalloc1(nz+1,&aij->saved_values);
3843:     PetscLogObjectMemory((PetscObject)mat,(nz+1)*sizeof(PetscScalar));
3844:   }

3846:   /* copy values over */
3847:   PetscArraycpy(aij->saved_values,aij->a,nz);
3848:   return(0);
3849: }

3851: /*@
3852:     MatStoreValues - Stashes a copy of the matrix values; this allows, for
3853:        example, reuse of the linear part of a Jacobian, while recomputing the
3854:        nonlinear portion.

3856:    Collect on Mat

3858:   Input Parameters:
3859: .  mat - the matrix (currently only AIJ matrices support this option)

3861:   Level: advanced

3863:   Common Usage, with SNESSolve():
3864: $    Create Jacobian matrix
3865: $    Set linear terms into matrix
3866: $    Apply boundary conditions to matrix, at this time matrix must have
3867: $      final nonzero structure (i.e. setting the nonlinear terms and applying
3868: $      boundary conditions again will not change the nonzero structure
3869: $    MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3870: $    MatStoreValues(mat);
3871: $    Call SNESSetJacobian() with matrix
3872: $    In your Jacobian routine
3873: $      MatRetrieveValues(mat);
3874: $      Set nonlinear terms in matrix

3876:   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
3877: $    // build linear portion of Jacobian
3878: $    MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3879: $    MatStoreValues(mat);
3880: $    loop over nonlinear iterations
3881: $       MatRetrieveValues(mat);
3882: $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
3883: $       // call MatAssemblyBegin/End() on matrix
3884: $       Solve linear system with Jacobian
3885: $    endloop

3887:   Notes:
3888:     Matrix must already be assemblied before calling this routine
3889:     Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
3890:     calling this routine.

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

3895: .seealso: MatRetrieveValues()

3897: @*/
3898: PetscErrorCode  MatStoreValues(Mat mat)
3899: {

3904:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3905:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3906:   PetscUseMethod(mat,"MatStoreValues_C",(Mat),(mat));
3907:   return(0);
3908: }

3910: PetscErrorCode  MatRetrieveValues_SeqAIJ(Mat mat)
3911: {
3912:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)mat->data;
3914:   PetscInt       nz = aij->i[mat->rmap->n];

3917:   if (!aij->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3918:   if (!aij->saved_values) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
3919:   /* copy values over */
3920:   PetscArraycpy(aij->a,aij->saved_values,nz);
3921:   return(0);
3922: }

3924: /*@
3925:     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
3926:        example, reuse of the linear part of a Jacobian, while recomputing the
3927:        nonlinear portion.

3929:    Collect on Mat

3931:   Input Parameters:
3932: .  mat - the matrix (currently only AIJ matrices support this option)

3934:   Level: advanced

3936: .seealso: MatStoreValues()

3938: @*/
3939: PetscErrorCode  MatRetrieveValues(Mat mat)
3940: {

3945:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3946:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3947:   PetscUseMethod(mat,"MatRetrieveValues_C",(Mat),(mat));
3948:   return(0);
3949: }


3952: /* --------------------------------------------------------------------------------*/
3953: /*@C
3954:    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
3955:    (the default parallel PETSc format).  For good matrix assembly performance
3956:    the user should preallocate the matrix storage by setting the parameter nz
3957:    (or the array nnz).  By setting these parameters accurately, performance
3958:    during matrix assembly can be increased by more than a factor of 50.

3960:    Collective

3962:    Input Parameters:
3963: +  comm - MPI communicator, set to PETSC_COMM_SELF
3964: .  m - number of rows
3965: .  n - number of columns
3966: .  nz - number of nonzeros per row (same for all rows)
3967: -  nnz - array containing the number of nonzeros in the various rows
3968:          (possibly different for each row) or NULL

3970:    Output Parameter:
3971: .  A - the matrix

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

3977:    Notes:
3978:    If nnz is given then nz is ignored

3980:    The AIJ format (also called the Yale sparse matrix format or
3981:    compressed row storage), is fully compatible with standard Fortran 77
3982:    storage.  That is, the stored row and column indices can begin at
3983:    either one (as in Fortran) or zero.  See the users' manual for details.

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

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

3995:    Options Database Keys:
3996: +  -mat_no_inode  - Do not use inodes
3997: -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)

3999:    Level: intermediate

4001: .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays()

4003: @*/
4004: PetscErrorCode  MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
4005: {

4009:   MatCreate(comm,A);
4010:   MatSetSizes(*A,m,n,m,n);
4011:   MatSetType(*A,MATSEQAIJ);
4012:   MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);
4013:   return(0);
4014: }

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

4022:    Collective

4024:    Input Parameters:
4025: +  B - The matrix
4026: .  nz - number of nonzeros per row (same for all rows)
4027: -  nnz - array containing the number of nonzeros in the various rows
4028:          (possibly different for each row) or NULL

4030:    Notes:
4031:      If nnz is given then nz is ignored

4033:     The AIJ format (also called the Yale sparse matrix format or
4034:    compressed row storage), is fully compatible with standard Fortran 77
4035:    storage.  That is, the stored row and column indices can begin at
4036:    either one (as in Fortran) or zero.  See the users' manual for details.

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

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

4048:    Developers: Use nz of MAT_SKIP_ALLOCATION to not allocate any space for the matrix
4049:    entries or columns indices

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

4056:    Options Database Keys:
4057: +  -mat_no_inode  - Do not use inodes
4058: -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)

4060:    Level: intermediate

4062: .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo(),
4063:           MatSeqAIJSetTotalPreallocation()

4065: @*/
4066: PetscErrorCode  MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
4067: {

4073:   PetscTryMethod(B,"MatSeqAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[]),(B,nz,nnz));
4074:   return(0);
4075: }

4077: PetscErrorCode  MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,const PetscInt *nnz)
4078: {
4079:   Mat_SeqAIJ     *b;
4080:   PetscBool      skipallocation = PETSC_FALSE,realalloc = PETSC_FALSE;
4082:   PetscInt       i;

4085:   if (nz >= 0 || nnz) realalloc = PETSC_TRUE;
4086:   if (nz == MAT_SKIP_ALLOCATION) {
4087:     skipallocation = PETSC_TRUE;
4088:     nz             = 0;
4089:   }
4090:   PetscLayoutSetUp(B->rmap);
4091:   PetscLayoutSetUp(B->cmap);

4093:   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
4094:   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %D",nz);
4095:   if (PetscUnlikelyDebug(nnz)) {
4096:     for (i=0; i<B->rmap->n; i++) {
4097:       if (nnz[i] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be less than 0: local row %D value %D",i,nnz[i]);
4098:       if (nnz[i] > B->cmap->n) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be greater than row length: local row %D value %d rowlength %D",i,nnz[i],B->cmap->n);
4099:     }
4100:   }

4102:   B->preallocated = PETSC_TRUE;

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

4106:   if (!skipallocation) {
4107:     if (!b->imax) {
4108:       PetscMalloc1(B->rmap->n,&b->imax);
4109:       PetscLogObjectMemory((PetscObject)B,B->rmap->n*sizeof(PetscInt));
4110:     }
4111:     if (!b->ilen) {
4112:       /* b->ilen will count nonzeros in each row so far. */
4113:       PetscCalloc1(B->rmap->n,&b->ilen);
4114:       PetscLogObjectMemory((PetscObject)B,B->rmap->n*sizeof(PetscInt));
4115:     } else {
4116:       PetscMemzero(b->ilen,B->rmap->n*sizeof(PetscInt));
4117:     }
4118:     if (!b->ipre) {
4119:       PetscMalloc1(B->rmap->n,&b->ipre);
4120:       PetscLogObjectMemory((PetscObject)B,B->rmap->n*sizeof(PetscInt));
4121:     }
4122:     if (!nnz) {
4123:       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
4124:       else if (nz < 0) nz = 1;
4125:       nz = PetscMin(nz,B->cmap->n);
4126:       for (i=0; i<B->rmap->n; i++) b->imax[i] = nz;
4127:       nz = nz*B->rmap->n;
4128:     } else {
4129:       PetscInt64 nz64 = 0;
4130:       for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz64 += nnz[i];}
4131:       PetscIntCast(nz64,&nz);
4132:     }

4134:     /* allocate the matrix space */
4135:     /* FIXME: should B's old memory be unlogged? */
4136:     MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);
4137:     if (B->structure_only) {
4138:       PetscMalloc1(nz,&b->j);
4139:       PetscMalloc1(B->rmap->n+1,&b->i);
4140:       PetscLogObjectMemory((PetscObject)B,(B->rmap->n+1)*sizeof(PetscInt)+nz*sizeof(PetscInt));
4141:     } else {
4142:       PetscMalloc3(nz,&b->a,nz,&b->j,B->rmap->n+1,&b->i);
4143:       PetscLogObjectMemory((PetscObject)B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));
4144:     }
4145:     b->i[0] = 0;
4146:     for (i=1; i<B->rmap->n+1; i++) {
4147:       b->i[i] = b->i[i-1] + b->imax[i-1];
4148:     }
4149:     if (B->structure_only) {
4150:       b->singlemalloc = PETSC_FALSE;
4151:       b->free_a       = PETSC_FALSE;
4152:     } else {
4153:       b->singlemalloc = PETSC_TRUE;
4154:       b->free_a       = PETSC_TRUE;
4155:     }
4156:     b->free_ij      = PETSC_TRUE;
4157:   } else {
4158:     b->free_a  = PETSC_FALSE;
4159:     b->free_ij = PETSC_FALSE;
4160:   }

4162:   if (b->ipre && nnz != b->ipre  && b->imax) {
4163:     /* reserve user-requested sparsity */
4164:     PetscArraycpy(b->ipre,b->imax,B->rmap->n);
4165:   }


4168:   b->nz               = 0;
4169:   b->maxnz            = nz;
4170:   B->info.nz_unneeded = (double)b->maxnz;
4171:   if (realalloc) {
4172:     MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);
4173:   }
4174:   B->was_assembled = PETSC_FALSE;
4175:   B->assembled     = PETSC_FALSE;
4176:   return(0);
4177: }


4180: PetscErrorCode MatResetPreallocation_SeqAIJ(Mat A)
4181: {
4182:   Mat_SeqAIJ     *a;
4183:   PetscInt       i;


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

4192:   a = (Mat_SeqAIJ*)A->data;
4193:   /* if no saved info, we error out */
4194:   if (!a->ipre) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"No saved preallocation info \n");

4196:   if (!a->i || !a->j || !a->a || !a->imax || !a->ilen) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Memory info is incomplete, and can not reset preallocation \n");

4198:   PetscArraycpy(a->imax,a->ipre,A->rmap->n);
4199:   PetscArrayzero(a->ilen,A->rmap->n);
4200:   a->i[0] = 0;
4201:   for (i=1; i<A->rmap->n+1; i++) {
4202:     a->i[i] = a->i[i-1] + a->imax[i-1];
4203:   }
4204:   A->preallocated     = PETSC_TRUE;
4205:   a->nz               = 0;
4206:   a->maxnz            = a->i[A->rmap->n];
4207:   A->info.nz_unneeded = (double)a->maxnz;
4208:   A->was_assembled    = PETSC_FALSE;
4209:   A->assembled        = PETSC_FALSE;
4210:   return(0);
4211: }

4213: /*@
4214:    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.

4216:    Input Parameters:
4217: +  B - the matrix
4218: .  i - the indices into j for the start of each row (starts with zero)
4219: .  j - the column indices for each row (starts with zero) these must be sorted for each row
4220: -  v - optional values in the matrix

4222:    Level: developer

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

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

4230:     Developer Notes:
4231:       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
4232:       then just copies the v values directly with PetscMemcpy().

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

4236: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), MATSEQAIJ, MatResetPreallocation()
4237: @*/
4238: PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
4239: {

4245:   PetscTryMethod(B,"MatSeqAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));
4246:   return(0);
4247: }

4249: PetscErrorCode  MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
4250: {
4251:   PetscInt       i;
4252:   PetscInt       m,n;
4253:   PetscInt       nz;
4254:   PetscInt       *nnz;

4258:   if (Ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Ii[0] must be 0 it is %D", Ii[0]);

4260:   PetscLayoutSetUp(B->rmap);
4261:   PetscLayoutSetUp(B->cmap);

4263:   MatGetSize(B, &m, &n);
4264:   PetscMalloc1(m+1, &nnz);
4265:   for (i = 0; i < m; i++) {
4266:     nz     = Ii[i+1]- Ii[i];
4267:     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
4268:     nnz[i] = nz;
4269:   }
4270:   MatSeqAIJSetPreallocation(B, 0, nnz);
4271:   PetscFree(nnz);

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

4277:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
4278:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);

4280:   MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
4281:   return(0);
4282: }

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

4287: /*
4288:     Computes (B'*A')' since computing B*A directly is untenable

4290:                n                       p                          p
4291:         [             ]       [             ]         [                 ]
4292:       m [      A      ]  *  n [       B     ]   =   m [         C       ]
4293:         [             ]       [             ]         [                 ]

4295: */
4296: PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
4297: {
4298:   PetscErrorCode    ierr;
4299:   Mat_SeqDense      *sub_a = (Mat_SeqDense*)A->data;
4300:   Mat_SeqAIJ        *sub_b = (Mat_SeqAIJ*)B->data;
4301:   Mat_SeqDense      *sub_c = (Mat_SeqDense*)C->data;
4302:   PetscInt          i,j,n,m,q,p;
4303:   const PetscInt    *ii,*idx;
4304:   const PetscScalar *b,*a,*a_q;
4305:   PetscScalar       *c,*c_q;
4306:   PetscInt          clda = sub_c->lda;
4307:   PetscInt          alda = sub_a->lda;

4310:   m    = A->rmap->n;
4311:   n    = A->cmap->n;
4312:   p    = B->cmap->n;
4313:   a    = sub_a->v;
4314:   b    = sub_b->a;
4315:   c    = sub_c->v;
4316:   if (clda == m) {
4317:     PetscArrayzero(c,m*p);
4318:   } else {
4319:     for (j=0;j<p;j++)
4320:       for (i=0;i<m;i++)
4321:         c[j*clda + i] = 0.0;
4322:   }
4323:   ii  = sub_b->i;
4324:   idx = sub_b->j;
4325:   for (i=0; i<n; i++) {
4326:     q = ii[i+1] - ii[i];
4327:     while (q-->0) {
4328:       c_q = c + clda*(*idx);
4329:       a_q = a + alda*i;
4330:       PetscKernelAXPY(c_q,*b,a_q,m);
4331:       idx++;
4332:       b++;
4333:     }
4334:   }
4335:   return(0);
4336: }

4338: PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat C)
4339: {
4341:   PetscInt       m=A->rmap->n,n=B->cmap->n;
4342:   PetscBool      cisdense;

4345:   if (A->cmap->n != B->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"A->cmap->n %D != B->rmap->n %D\n",A->cmap->n,B->rmap->n);
4346:   MatSetSizes(C,m,n,m,n);
4347:   MatSetBlockSizesFromMats(C,A,B);
4348:   PetscObjectTypeCompareAny((PetscObject)C,&cisdense,MATSEQDENSE,MATSEQDENSECUDA,"");
4349:   if (!cisdense) {
4350:     MatSetType(C,MATDENSE);
4351:   }
4352:   MatSetUp(C);

4354:   C->ops->matmultnumeric = MatMatMultNumeric_SeqDense_SeqAIJ;
4355:   return(0);
4356: }

4358: /* ----------------------------------------------------------------*/
4359: /*MC
4360:    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
4361:    based on compressed sparse row format.

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

4366:    Level: beginner

4368:    Notes:
4369:     MatSetValues() may be called for this matrix type with a NULL argument for the numerical values,
4370:     in this case the values associated with the rows and columns one passes in are set to zero
4371:     in the matrix

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

4376:   Developer Notes:
4377:     It would be nice if all matrix formats supported passing NULL in for the numerical values

4379: .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
4380: M*/

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

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

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

4394:   Developer Notes:
4395:     Subclasses include MATAIJCUSPARSE, MATAIJPERM, MATAIJSELL, MATAIJMKL, MATAIJCRL, and also automatically switches over to use inodes when
4396:    enough exist.

4398:   Level: beginner

4400: .seealso: MatCreateAIJ(), MatCreateSeqAIJ(), MATSEQAIJ,MATMPIAIJ
4401: M*/

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

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

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

4415:   Level: beginner

4417: .seealso: MatCreateMPIAIJCRL,MATSEQAIJCRL,MATMPIAIJCRL, MATSEQAIJCRL, MATMPIAIJCRL
4418: M*/

4420: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCRL(Mat,MatType,MatReuse,Mat*);
4421: #if defined(PETSC_HAVE_ELEMENTAL)
4422: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_Elemental(Mat,MatType,MatReuse,Mat*);
4423: #endif
4424: #if defined(PETSC_HAVE_SCALAPACK)
4425: PETSC_INTERN PetscErrorCode MatConvert_AIJ_ScaLAPACK(Mat,MatType,MatReuse,Mat*);
4426: #endif
4427: #if defined(PETSC_HAVE_HYPRE)
4428: PETSC_INTERN PetscErrorCode MatConvert_AIJ_HYPRE(Mat A,MatType,MatReuse,Mat*);
4429: #endif

4431: PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_SeqSELL(Mat,MatType,MatReuse,Mat*);
4432: PETSC_INTERN PetscErrorCode MatConvert_XAIJ_IS(Mat,MatType,MatReuse,Mat*);
4433: PETSC_INTERN PetscErrorCode MatProductSetFromOptions_IS_XAIJ(Mat);

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

4438:    Not Collective

4440:    Input Parameter:
4441: .  mat - a MATSEQAIJ matrix

4443:    Output Parameter:
4444: .   array - pointer to the data

4446:    Level: intermediate

4448: .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90()
4449: @*/
4450: PetscErrorCode  MatSeqAIJGetArray(Mat A,PetscScalar **array)
4451: {

4455:   PetscUseMethod(A,"MatSeqAIJGetArray_C",(Mat,PetscScalar**),(A,array));
4456: #if defined(PETSC_HAVE_DEVICE)
4457:   if (A->offloadmask != PETSC_OFFLOAD_UNALLOCATED) A->offloadmask = PETSC_OFFLOAD_CPU;
4458: #endif
4459:   return(0);
4460: }

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

4465:    Not Collective

4467:    Input Parameter:
4468: .  mat - a MATSEQAIJ matrix

4470:    Output Parameter:
4471: .   array - pointer to the data

4473:    Level: intermediate

4475: .seealso: MatSeqAIJGetArray(), MatSeqAIJRestoreArrayRead()
4476: @*/
4477: PetscErrorCode  MatSeqAIJGetArrayRead(Mat A,const PetscScalar **array)
4478: {
4479: #if defined(PETSC_HAVE_DEVICE)
4480:   PetscOffloadMask oval;
4481: #endif

4485: #if defined(PETSC_HAVE_DEVICE)
4486:   oval = A->offloadmask;
4487: #endif
4488:   MatSeqAIJGetArray(A,(PetscScalar**)array);
4489: #if defined(PETSC_HAVE_DEVICE)
4490:   if (oval == PETSC_OFFLOAD_GPU || oval == PETSC_OFFLOAD_BOTH) A->offloadmask = PETSC_OFFLOAD_BOTH;
4491: #endif
4492:   return(0);
4493: }

4495: /*@C
4496:    MatSeqAIJRestoreArrayRead - restore the read-only access array obtained from MatSeqAIJGetArrayRead

4498:    Not Collective

4500:    Input Parameter:
4501: .  mat - a MATSEQAIJ matrix

4503:    Output Parameter:
4504: .   array - pointer to the data

4506:    Level: intermediate

4508: .seealso: MatSeqAIJGetArray(), MatSeqAIJGetArrayRead()
4509: @*/
4510: PetscErrorCode  MatSeqAIJRestoreArrayRead(Mat A,const PetscScalar **array)
4511: {
4512: #if defined(PETSC_HAVE_DEVICE)
4513:   PetscOffloadMask oval;
4514: #endif

4518: #if defined(PETSC_HAVE_DEVICE)
4519:   oval = A->offloadmask;
4520: #endif
4521:   MatSeqAIJRestoreArray(A,(PetscScalar**)array);
4522: #if defined(PETSC_HAVE_DEVICE)
4523:   A->offloadmask = oval;
4524: #endif
4525:   return(0);
4526: }

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

4531:    Not Collective

4533:    Input Parameter:
4534: .  mat - a MATSEQAIJ matrix

4536:    Output Parameter:
4537: .   nz - the maximum number of nonzeros in any row

4539:    Level: intermediate

4541: .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90()
4542: @*/
4543: PetscErrorCode  MatSeqAIJGetMaxRowNonzeros(Mat A,PetscInt *nz)
4544: {
4545:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)A->data;

4548:   *nz = aij->rmax;
4549:   return(0);
4550: }

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

4555:    Not Collective

4557:    Input Parameters:
4558: +  mat - a MATSEQAIJ matrix
4559: -  array - pointer to the data

4561:    Level: intermediate

4563: .seealso: MatSeqAIJGetArray(), MatSeqAIJRestoreArrayF90()
4564: @*/
4565: PetscErrorCode  MatSeqAIJRestoreArray(Mat A,PetscScalar **array)
4566: {

4570:   PetscUseMethod(A,"MatSeqAIJRestoreArray_C",(Mat,PetscScalar**),(A,array));
4571:   return(0);
4572: }

4574: #if defined(PETSC_HAVE_CUDA)
4575: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCUSPARSE(Mat);
4576: #endif
4577: #if defined(PETSC_HAVE_KOKKOS_KERNELS)
4578: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJKokkos(Mat);
4579: #endif

4581: PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJ(Mat B)
4582: {
4583:   Mat_SeqAIJ     *b;
4585:   PetscMPIInt    size;

4588:   MPI_Comm_size(PetscObjectComm((PetscObject)B),&size);
4589:   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Comm must be of size 1");

4591:   PetscNewLog(B,&b);

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

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

4598:   b->row                = NULL;
4599:   b->col                = NULL;
4600:   b->icol               = NULL;
4601:   b->reallocs           = 0;
4602:   b->ignorezeroentries  = PETSC_FALSE;
4603:   b->roworiented        = PETSC_TRUE;
4604:   b->nonew              = 0;
4605:   b->diag               = NULL;
4606:   b->solve_work         = NULL;
4607:   B->spptr              = NULL;
4608:   b->saved_values       = NULL;
4609:   b->idiag              = NULL;
4610:   b->mdiag              = NULL;
4611:   b->ssor_work          = NULL;
4612:   b->omega              = 1.0;
4613:   b->fshift             = 0.0;
4614:   b->idiagvalid         = PETSC_FALSE;
4615:   b->ibdiagvalid        = PETSC_FALSE;
4616:   b->keepnonzeropattern = PETSC_FALSE;

4618:   PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);
4619:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJGetArray_C",MatSeqAIJGetArray_SeqAIJ);
4620:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJRestoreArray_C",MatSeqAIJRestoreArray_SeqAIJ);

4622: #if defined(PETSC_HAVE_MATLAB_ENGINE)
4623:   PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEnginePut_C",MatlabEnginePut_SeqAIJ);
4624:   PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEngineGet_C",MatlabEngineGet_SeqAIJ);
4625: #endif

4627:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetColumnIndices_C",MatSeqAIJSetColumnIndices_SeqAIJ);
4628:   PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_SeqAIJ);
4629:   PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_SeqAIJ);
4630:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",MatConvert_SeqAIJ_SeqSBAIJ);
4631:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqbaij_C",MatConvert_SeqAIJ_SeqBAIJ);
4632:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijperm_C",MatConvert_SeqAIJ_SeqAIJPERM);
4633:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijsell_C",MatConvert_SeqAIJ_SeqAIJSELL);
4634: #if defined(PETSC_HAVE_MKL_SPARSE)
4635:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijmkl_C",MatConvert_SeqAIJ_SeqAIJMKL);
4636: #endif
4637: #if defined(PETSC_HAVE_CUDA)
4638:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijcusparse_C",MatConvert_SeqAIJ_SeqAIJCUSPARSE);
4639:   PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_seqaijcusparse_seqaij_C",MatProductSetFromOptions_SeqAIJ);
4640:   PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_seqaij_seqaijcusparse_C",MatProductSetFromOptions_SeqAIJ);
4641: #endif
4642: #if defined(PETSC_HAVE_KOKKOS_KERNELS)
4643:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijkokkos_C",MatConvert_SeqAIJ_SeqAIJKokkos);
4644: #endif
4645:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijcrl_C",MatConvert_SeqAIJ_SeqAIJCRL);
4646: #if defined(PETSC_HAVE_ELEMENTAL)
4647:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_elemental_C",MatConvert_SeqAIJ_Elemental);
4648: #endif
4649: #if defined(PETSC_HAVE_SCALAPACK)
4650:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_scalapack_C",MatConvert_AIJ_ScaLAPACK);
4651: #endif
4652: #if defined(PETSC_HAVE_HYPRE)
4653:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_hypre_C",MatConvert_AIJ_HYPRE);
4654:   PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_transpose_seqaij_seqaij_C",MatProductSetFromOptions_Transpose_AIJ_AIJ);
4655: #endif
4656:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqdense_C",MatConvert_SeqAIJ_SeqDense);
4657:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqsell_C",MatConvert_SeqAIJ_SeqSELL);
4658:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_is_C",MatConvert_XAIJ_IS);
4659:   PetscObjectComposeFunction((PetscObject)B,"MatIsTranspose_C",MatIsTranspose_SeqAIJ);
4660:   PetscObjectComposeFunction((PetscObject)B,"MatIsHermitianTranspose_C",MatIsTranspose_SeqAIJ);
4661:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",MatSeqAIJSetPreallocation_SeqAIJ);
4662:   PetscObjectComposeFunction((PetscObject)B,"MatResetPreallocation_C",MatResetPreallocation_SeqAIJ);
4663:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",MatSeqAIJSetPreallocationCSR_SeqAIJ);
4664:   PetscObjectComposeFunction((PetscObject)B,"MatReorderForNonzeroDiagonal_C",MatReorderForNonzeroDiagonal_SeqAIJ);
4665:   PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_is_seqaij_C",MatProductSetFromOptions_IS_XAIJ);
4666:   PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_seqdense_seqaij_C",MatProductSetFromOptions_SeqDense_SeqAIJ);
4667:   PetscObjectComposeFunction((PetscObject)B,"MatProductSetFromOptions_seqaij_seqaij_C",MatProductSetFromOptions_SeqAIJ);
4668:   MatCreate_SeqAIJ_Inode(B);
4669:   PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);
4670:   MatSeqAIJSetTypeFromOptions(B);  /* this allows changing the matrix subtype to say MATSEQAIJPERM */
4671:   return(0);
4672: }

4674: /*
4675:     Given a matrix generated with MatGetFactor() duplicates all the information in A into B
4676: */
4677: PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscBool mallocmatspace)
4678: {
4679:   Mat_SeqAIJ     *c = (Mat_SeqAIJ*)C->data,*a = (Mat_SeqAIJ*)A->data;
4681:   PetscInt       m = A->rmap->n,i;

4684:   if (!A->assembled && cpvalues!=MAT_DO_NOT_COPY_VALUES) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Cannot duplicate unassembled matrix");

4686:   C->factortype = A->factortype;
4687:   c->row        = NULL;
4688:   c->col        = NULL;
4689:   c->icol       = NULL;
4690:   c->reallocs   = 0;

4692:   C->assembled = PETSC_TRUE;

4694:   PetscLayoutReference(A->rmap,&C->rmap);
4695:   PetscLayoutReference(A->cmap,&C->cmap);

4697:   PetscMalloc1(m,&c->imax);
4698:   PetscMemcpy(c->imax,a->imax,m*sizeof(PetscInt));
4699:   PetscMalloc1(m,&c->ilen);
4700:   PetscMemcpy(c->ilen,a->ilen,m*sizeof(PetscInt));
4701:   PetscLogObjectMemory((PetscObject)C, 2*m*sizeof(PetscInt));

4703:   /* allocate the matrix space */
4704:   if (mallocmatspace) {
4705:     PetscMalloc3(a->i[m],&c->a,a->i[m],&c->j,m+1,&c->i);
4706:     PetscLogObjectMemory((PetscObject)C, a->i[m]*(sizeof(PetscScalar)+sizeof(PetscInt))+(m+1)*sizeof(PetscInt));

4708:     c->singlemalloc = PETSC_TRUE;

4710:     PetscArraycpy(c->i,a->i,m+1);
4711:     if (m > 0) {
4712:       PetscArraycpy(c->j,a->j,a->i[m]);
4713:       if (cpvalues == MAT_COPY_VALUES) {
4714:         const PetscScalar *aa;

4716:         MatSeqAIJGetArrayRead(A,&aa);
4717:         PetscArraycpy(c->a,aa,a->i[m]);
4718:         MatSeqAIJGetArrayRead(A,&aa);
4719:       } else {
4720:         PetscArrayzero(c->a,a->i[m]);
4721:       }
4722:     }
4723:   }

4725:   c->ignorezeroentries = a->ignorezeroentries;
4726:   c->roworiented       = a->roworiented;
4727:   c->nonew             = a->nonew;
4728:   if (a->diag) {
4729:     PetscMalloc1(m+1,&c->diag);
4730:     PetscMemcpy(c->diag,a->diag,m*sizeof(PetscInt));
4731:     PetscLogObjectMemory((PetscObject)C,(m+1)*sizeof(PetscInt));
4732:   } else c->diag = NULL;

4734:   c->solve_work         = NULL;
4735:   c->saved_values       = NULL;
4736:   c->idiag              = NULL;
4737:   c->ssor_work          = NULL;
4738:   c->keepnonzeropattern = a->keepnonzeropattern;
4739:   c->free_a             = PETSC_TRUE;
4740:   c->free_ij            = PETSC_TRUE;

4742:   c->rmax         = a->rmax;
4743:   c->nz           = a->nz;
4744:   c->maxnz        = a->nz;       /* Since we allocate exactly the right amount */
4745:   C->preallocated = PETSC_TRUE;

4747:   c->compressedrow.use   = a->compressedrow.use;
4748:   c->compressedrow.nrows = a->compressedrow.nrows;
4749:   if (a->compressedrow.use) {
4750:     i    = a->compressedrow.nrows;
4751:     PetscMalloc2(i+1,&c->compressedrow.i,i,&c->compressedrow.rindex);
4752:     PetscArraycpy(c->compressedrow.i,a->compressedrow.i,i+1);
4753:     PetscArraycpy(c->compressedrow.rindex,a->compressedrow.rindex,i);
4754:   } else {
4755:     c->compressedrow.use    = PETSC_FALSE;
4756:     c->compressedrow.i      = NULL;
4757:     c->compressedrow.rindex = NULL;
4758:   }
4759:   c->nonzerorowcnt = a->nonzerorowcnt;
4760:   C->nonzerostate  = A->nonzerostate;

4762:   MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);
4763:   PetscFunctionListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);
4764:   return(0);
4765: }

4767: PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
4768: {

4772:   MatCreate(PetscObjectComm((PetscObject)A),B);
4773:   MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);
4774:   if (!(A->rmap->n % A->rmap->bs) && !(A->cmap->n % A->cmap->bs)) {
4775:     MatSetBlockSizesFromMats(*B,A,A);
4776:   }
4777:   MatSetType(*B,((PetscObject)A)->type_name);
4778:   MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);
4779:   return(0);
4780: }

4782: PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer)
4783: {
4784:   PetscBool      isbinary, ishdf5;

4790:   /* force binary viewer to load .info file if it has not yet done so */
4791:   PetscViewerSetUp(viewer);
4792:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
4793:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,  &ishdf5);
4794:   if (isbinary) {
4795:     MatLoad_SeqAIJ_Binary(newMat,viewer);
4796:   } else if (ishdf5) {
4797: #if defined(PETSC_HAVE_HDF5)
4798:     MatLoad_AIJ_HDF5(newMat,viewer);
4799: #else
4800:     SETERRQ(PetscObjectComm((PetscObject)newMat),PETSC_ERR_SUP,"HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
4801: #endif
4802:   } else {
4803:     SETERRQ2(PetscObjectComm((PetscObject)newMat),PETSC_ERR_SUP,"Viewer type %s not yet supported for reading %s matrices",((PetscObject)viewer)->type_name,((PetscObject)newMat)->type_name);
4804:   }
4805:   return(0);
4806: }

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

4815:   PetscViewerSetUp(viewer);

4817:   /* read in matrix header */
4818:   PetscViewerBinaryRead(viewer,header,4,NULL,PETSC_INT);
4819:   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Not a matrix object in file");
4820:   M = header[1]; N = header[2]; nz = header[3];
4821:   if (M < 0) SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_FILE_UNEXPECTED,"Matrix row size (%D) in file is negative",M);
4822:   if (N < 0) SETERRQ1(PetscObjectComm((PetscObject)viewer),PETSC_ERR_FILE_UNEXPECTED,"Matrix column size (%D) in file is negative",N);
4823:   if (nz < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk, cannot load as SeqAIJ");

4825:   /* set block sizes from the viewer's .info file */
4826:   MatLoad_Binary_BlockSizes(mat,viewer);
4827:   /* set local and global sizes if not set already */
4828:   if (mat->rmap->n < 0) mat->rmap->n = M;
4829:   if (mat->cmap->n < 0) mat->cmap->n = N;
4830:   if (mat->rmap->N < 0) mat->rmap->N = M;
4831:   if (mat->cmap->N < 0) mat->cmap->N = N;
4832:   PetscLayoutSetUp(mat->rmap);
4833:   PetscLayoutSetUp(mat->cmap);

4835:   /* check if the matrix sizes are correct */
4836:   MatGetSize(mat,&rows,&cols);
4837:   if (M != rows || N != cols) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED, "Matrix in file of different sizes (%D, %D) than the input matrix (%D, %D)",M,N,rows,cols);

4839:   /* read in row lengths */
4840:   PetscMalloc1(M,&rowlens);
4841:   PetscViewerBinaryRead(viewer,rowlens,M,NULL,PETSC_INT);
4842:   /* check if sum(rowlens) is same as nz */
4843:   sum = 0; for (i=0; i<M; i++) sum += rowlens[i];
4844:   if (sum != nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Inconsistent matrix data in file: nonzeros = %D, sum-row-lengths = %D\n",nz,sum);
4845:   /* preallocate and check sizes */
4846:   MatSeqAIJSetPreallocation_SeqAIJ(mat,0,rowlens);
4847:   MatGetSize(mat,&rows,&cols);
4848:   if (M != rows || N != cols) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED, "Matrix in file of different length (%D, %D) than the input matrix (%D, %D)",M,N,rows,cols);
4849:   /* store row lengths */
4850:   PetscArraycpy(a->ilen,rowlens,M);
4851:   PetscFree(rowlens);

4853:   /* fill in "i" row pointers */
4854:   a->i[0] = 0; for (i=0; i<M; i++) a->i[i+1] = a->i[i] + a->ilen[i];
4855:   /* read in "j" column indices */
4856:   PetscViewerBinaryRead(viewer,a->j,nz,NULL,PETSC_INT);
4857:   /* read in "a" nonzero values */
4858:   PetscViewerBinaryRead(viewer,a->a,nz,NULL,PETSC_SCALAR);

4860:   MatAssemblyBegin(mat,MAT_FINAL_ASSEMBLY);
4861:   MatAssemblyEnd(mat,MAT_FINAL_ASSEMBLY);
4862:   return(0);
4863: }

4865: PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscBool * flg)
4866: {
4867:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*b = (Mat_SeqAIJ*)B->data;
4869: #if defined(PETSC_USE_COMPLEX)
4870:   PetscInt k;
4871: #endif

4874:   /* If the  matrix dimensions are not equal,or no of nonzeros */
4875:   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
4876:     *flg = PETSC_FALSE;
4877:     return(0);
4878:   }

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

4884:   /* if a->j are the same */
4885:   PetscArraycmp(a->j,b->j,a->nz,flg);
4886:   if (!*flg) return(0);

4888:   /* if a->a are the same */
4889: #if defined(PETSC_USE_COMPLEX)
4890:   for (k=0; k<a->nz; k++) {
4891:     if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])) {
4892:       *flg = PETSC_FALSE;
4893:       return(0);
4894:     }
4895:   }
4896: #else
4897:   PetscArraycmp(a->a,b->a,a->nz,flg);
4898: #endif
4899:   return(0);
4900: }

4902: /*@
4903:      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
4904:               provided by the user.

4906:       Collective

4908:    Input Parameters:
4909: +   comm - must be an MPI communicator of size 1
4910: .   m - number of rows
4911: .   n - number of columns
4912: .   i - row indices; that is i[0] = 0, i[row] = i[row-1] + number of elements in that row of the matrix
4913: .   j - column indices
4914: -   a - matrix values

4916:    Output Parameter:
4917: .   mat - the matrix

4919:    Level: intermediate

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

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

4927:        The i and j indices are 0 based

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

4933: $        1 0 0
4934: $        2 0 3
4935: $        4 5 6
4936: $
4937: $        i =  {0,1,3,6}  [size = nrow+1  = 3+1]
4938: $        j =  {0,0,2,0,1,2}  [size = 6]; values must be sorted for each row
4939: $        v =  {1,2,3,4,5,6}  [size = 6]


4942: .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateMPIAIJWithArrays(), MatMPIAIJSetPreallocationCSR()

4944: @*/
4945: PetscErrorCode  MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt i[],PetscInt j[],PetscScalar a[],Mat *mat)
4946: {
4948:   PetscInt       ii;
4949:   Mat_SeqAIJ     *aij;
4950:   PetscInt jj;

4953:   if (m > 0 && i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
4954:   MatCreate(comm,mat);
4955:   MatSetSizes(*mat,m,n,m,n);
4956:   /* MatSetBlockSizes(*mat,,); */
4957:   MatSetType(*mat,MATSEQAIJ);
4958:   MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,NULL);
4959:   aij  = (Mat_SeqAIJ*)(*mat)->data;
4960:   PetscMalloc1(m,&aij->imax);
4961:   PetscMalloc1(m,&aij->ilen);

4963:   aij->i            = i;
4964:   aij->j            = j;
4965:   aij->a            = a;
4966:   aij->singlemalloc = PETSC_FALSE;
4967:   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
4968:   aij->free_a       = PETSC_FALSE;
4969:   aij->free_ij      = PETSC_FALSE;

4971:   for (ii=0; ii<m; ii++) {
4972:     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
4973:     if (PetscDefined(USE_DEBUG)) {
4974:       if (i[ii+1] - i[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row length in i (row indices) row = %D length = %D",ii,i[ii+1] - i[ii]);
4975:       for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
4976:         if (j[jj] < j[jj-1]) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column entry number %D (actual column %D) in row %D is not sorted",jj-i[ii],j[jj],ii);
4977:         if (j[jj] == j[jj-1]) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column entry number %D (actual column %D) in row %D is identical to previous entry",jj-i[ii],j[jj],ii);
4978:       }
4979:     }
4980:   }
4981:   if (PetscDefined(USE_DEBUG)) {
4982:     for (ii=0; ii<aij->i[m]; ii++) {
4983:       if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %D index = %D",ii,j[ii]);
4984:       if (j[ii] > n - 1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column index to large at location = %D index = %D",ii,j[ii]);
4985:     }
4986:   }

4988:   MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);
4989:   MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);
4990:   return(0);
4991: }
4992: /*@C
4993:      MatCreateSeqAIJFromTriple - Creates an sequential AIJ matrix using matrix elements (in COO format)
4994:               provided by the user.

4996:       Collective

4998:    Input Parameters:
4999: +   comm - must be an MPI communicator of size 1
5000: .   m   - number of rows
5001: .   n   - number of columns
5002: .   i   - row indices
5003: .   j   - column indices
5004: .   a   - matrix values
5005: .   nz  - number of nonzeros
5006: -   idx - 0 or 1 based

5008:    Output Parameter:
5009: .   mat - the matrix

5011:    Level: intermediate

5013:    Notes:
5014:        The i and j indices are 0 based

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

5020:         1 0 0
5021:         2 0 3
5022:         4 5 6

5024:         i =  {0,1,1,2,2,2}
5025:         j =  {0,0,2,0,1,2}
5026:         v =  {1,2,3,4,5,6}


5029: .seealso: MatCreate(), MatCreateAIJ(), MatCreateSeqAIJ(), MatCreateSeqAIJWithArrays(), MatMPIAIJSetPreallocationCSR()

5031: @*/
5032: PetscErrorCode  MatCreateSeqAIJFromTriple(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt i[],PetscInt j[],PetscScalar a[],Mat *mat,PetscInt nz,PetscBool idx)
5033: {
5035:   PetscInt       ii, *nnz, one = 1,row,col;


5039:   PetscCalloc1(m,&nnz);
5040:   for (ii = 0; ii < nz; ii++) {
5041:     nnz[i[ii] - !!idx] += 1;
5042:   }
5043:   MatCreate(comm,mat);
5044:   MatSetSizes(*mat,m,n,m,n);
5045:   MatSetType(*mat,MATSEQAIJ);
5046:   MatSeqAIJSetPreallocation_SeqAIJ(*mat,0,nnz);
5047:   for (ii = 0; ii < nz; ii++) {
5048:     if (idx) {
5049:       row = i[ii] - 1;
5050:       col = j[ii] - 1;
5051:     } else {
5052:       row = i[ii];
5053:       col = j[ii];
5054:     }
5055:     MatSetValues(*mat,one,&row,one,&col,&a[ii],ADD_VALUES);
5056:   }
5057:   MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);
5058:   MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);
5059:   PetscFree(nnz);
5060:   return(0);
5061: }

5063: PetscErrorCode MatSeqAIJInvalidateDiagonal(Mat A)
5064: {
5065:   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data;

5069:   a->idiagvalid  = PETSC_FALSE;
5070:   a->ibdiagvalid = PETSC_FALSE;

5072:   MatSeqAIJInvalidateDiagonal_Inode(A);
5073:   return(0);
5074: }

5076: PetscErrorCode MatCreateMPIMatConcatenateSeqMat_SeqAIJ(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat)
5077: {
5079:   PetscMPIInt    size;

5082:   MPI_Comm_size(comm,&size);
5083:   if (size == 1) {
5084:     if (scall == MAT_INITIAL_MATRIX) {
5085:       MatDuplicate(inmat,MAT_COPY_VALUES,outmat);
5086:     } else {
5087:       MatCopy(inmat,*outmat,SAME_NONZERO_PATTERN);
5088:     }
5089:   } else {
5090:     MatCreateMPIMatConcatenateSeqMat_MPIAIJ(comm,inmat,n,scall,outmat);
5091:   }
5092:   return(0);
5093: }

5095: /*
5096:  Permute A into C's *local* index space using rowemb,colemb.
5097:  The embedding are supposed to be injections and the above implies that the range of rowemb is a subset
5098:  of [0,m), colemb is in [0,n).
5099:  If pattern == DIFFERENT_NONZERO_PATTERN, C is preallocated according to A.
5100:  */
5101: PetscErrorCode MatSetSeqMat_SeqAIJ(Mat C,IS rowemb,IS colemb,MatStructure pattern,Mat B)
5102: {
5103:   /* If making this function public, change the error returned in this function away from _PLIB. */
5105:   Mat_SeqAIJ     *Baij;
5106:   PetscBool      seqaij;
5107:   PetscInt       m,n,*nz,i,j,count;
5108:   PetscScalar    v;
5109:   const PetscInt *rowindices,*colindices;

5112:   if (!B) return(0);
5113:   /* Check to make sure the target matrix (and embeddings) are compatible with C and each other. */
5114:   PetscObjectBaseTypeCompare((PetscObject)B,MATSEQAIJ,&seqaij);
5115:   if (!seqaij) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is of wrong type");
5116:   if (rowemb) {
5117:     ISGetLocalSize(rowemb,&m);
5118:     if (m != B->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Row IS of size %D is incompatible with matrix row size %D",m,B->rmap->n);
5119:   } else {
5120:     if (C->rmap->n != B->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is row-incompatible with the target matrix");
5121:   }
5122:   if (colemb) {
5123:     ISGetLocalSize(colemb,&n);
5124:     if (n != B->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Diag col IS of size %D is incompatible with input matrix col size %D",n,B->cmap->n);
5125:   } else {
5126:     if (C->cmap->n != B->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is col-incompatible with the target matrix");
5127:   }

5129:   Baij = (Mat_SeqAIJ*)(B->data);
5130:   if (pattern == DIFFERENT_NONZERO_PATTERN) {
5131:     PetscMalloc1(B->rmap->n,&nz);
5132:     for (i=0; i<B->rmap->n; i++) {
5133:       nz[i] = Baij->i[i+1] - Baij->i[i];
5134:     }
5135:     MatSeqAIJSetPreallocation(C,0,nz);
5136:     PetscFree(nz);
5137:   }
5138:   if (pattern == SUBSET_NONZERO_PATTERN) {
5139:     MatZeroEntries(C);
5140:   }
5141:   count = 0;
5142:   rowindices = NULL;
5143:   colindices = NULL;
5144:   if (rowemb) {
5145:     ISGetIndices(rowemb,&rowindices);
5146:   }
5147:   if (colemb) {
5148:     ISGetIndices(colemb,&colindices);
5149:   }
5150:   for (i=0; i<B->rmap->n; i++) {
5151:     PetscInt row;
5152:     row = i;
5153:     if (rowindices) row = rowindices[i];
5154:     for (j=Baij->i[i]; j<Baij->i[i+1]; j++) {
5155:       PetscInt col;
5156:       col  = Baij->j[count];
5157:       if (colindices) col = colindices[col];
5158:       v    = Baij->a[count];
5159:       MatSetValues(C,1,&row,1,&col,&v,INSERT_VALUES);
5160:       ++count;
5161:     }
5162:   }
5163:   /* FIXME: set C's nonzerostate correctly. */
5164:   /* Assembly for C is necessary. */
5165:   C->preallocated = PETSC_TRUE;
5166:   C->assembled     = PETSC_TRUE;
5167:   C->was_assembled = PETSC_FALSE;
5168:   return(0);
5169: }

5171: PetscFunctionList MatSeqAIJList = NULL;

5173: /*@C
5174:    MatSeqAIJSetType - Converts a MATSEQAIJ matrix to a subtype

5176:    Collective on Mat

5178:    Input Parameters:
5179: +  mat      - the matrix object
5180: -  matype   - matrix type

5182:    Options Database Key:
5183: .  -mat_seqai_type  <method> - for example seqaijcrl


5186:   Level: intermediate

5188: .seealso: PCSetType(), VecSetType(), MatCreate(), MatType, Mat
5189: @*/
5190: PetscErrorCode  MatSeqAIJSetType(Mat mat, MatType matype)
5191: {
5192:   PetscErrorCode ierr,(*r)(Mat,MatType,MatReuse,Mat*);
5193:   PetscBool      sametype;

5197:   PetscObjectTypeCompare((PetscObject)mat,matype,&sametype);
5198:   if (sametype) return(0);

5200:    PetscFunctionListFind(MatSeqAIJList,matype,&r);
5201:   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown Mat type given: %s",matype);
5202:   (*r)(mat,matype,MAT_INPLACE_MATRIX,&mat);
5203:   return(0);
5204: }


5207: /*@C
5208:   MatSeqAIJRegister -  - Adds a new sub-matrix type for sequential AIJ matrices

5210:    Not Collective

5212:    Input Parameters:
5213: +  name - name of a new user-defined matrix type, for example MATSEQAIJCRL
5214: -  function - routine to convert to subtype

5216:    Notes:
5217:    MatSeqAIJRegister() may be called multiple times to add several user-defined solvers.


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

5223:    Level: advanced

5225: .seealso: MatSeqAIJRegisterAll()


5228:   Level: advanced
5229: @*/
5230: PetscErrorCode  MatSeqAIJRegister(const char sname[],PetscErrorCode (*function)(Mat,MatType,MatReuse,Mat *))
5231: {

5235:   MatInitializePackage();
5236:   PetscFunctionListAdd(&MatSeqAIJList,sname,function);
5237:   return(0);
5238: }

5240: PetscBool MatSeqAIJRegisterAllCalled = PETSC_FALSE;

5242: /*@C
5243:   MatSeqAIJRegisterAll - Registers all of the matrix subtypes of SeqAIJ

5245:   Not Collective

5247:   Level: advanced

5249:   Developers Note: CUSPARSE does not yet support the MatConvert_SeqAIJ..() paradigm and thus cannot be registered here

5251: .seealso:  MatRegisterAll(), MatSeqAIJRegister()
5252: @*/
5253: PetscErrorCode  MatSeqAIJRegisterAll(void)
5254: {

5258:   if (MatSeqAIJRegisterAllCalled) return(0);
5259:   MatSeqAIJRegisterAllCalled = PETSC_TRUE;

5261:   MatSeqAIJRegister(MATSEQAIJCRL,      MatConvert_SeqAIJ_SeqAIJCRL);
5262:   MatSeqAIJRegister(MATSEQAIJPERM,     MatConvert_SeqAIJ_SeqAIJPERM);
5263:   MatSeqAIJRegister(MATSEQAIJSELL,     MatConvert_SeqAIJ_SeqAIJSELL);
5264: #if defined(PETSC_HAVE_MKL_SPARSE)
5265:   MatSeqAIJRegister(MATSEQAIJMKL,      MatConvert_SeqAIJ_SeqAIJMKL);
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 */
5284: #undef CHKERRQ
5285: #define CHKERRQ(ierr) CHKERRABORT(PetscObjectComm((PetscObject)A),ierr)
5286: #undef SETERRQ2
5287: #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
5288: #undef SETERRQ3
5289: #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr)

5291: PETSC_EXTERN void matsetvaluesseqaij_(Mat *AA,PetscInt *mm,const PetscInt im[],PetscInt *nn,const PetscInt in[],const PetscScalar v[],InsertMode *isis, PetscErrorCode *_ierr)
5292: {
5293:   Mat            A  = *AA;
5294:   PetscInt       m  = *mm, n = *nn;
5295:   InsertMode     is = *isis;
5296:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
5297:   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
5298:   PetscInt       *imax,*ai,*ailen;
5300:   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
5301:   MatScalar      *ap,value,*aa;
5302:   PetscBool      ignorezeroentries = a->ignorezeroentries;
5303:   PetscBool      roworiented       = a->roworiented;

5306:   MatCheckPreallocated(A,1);
5307:   imax  = a->imax;
5308:   ai    = a->i;
5309:   ailen = a->ilen;
5310:   aj    = a->j;
5311:   aa    = a->a;

5313:   for (k=0; k<m; k++) { /* loop over added rows */
5314:     row = im[k];
5315:     if (row < 0) continue;
5316:     if (PetscUnlikelyDebug(row >= A->rmap->n)) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
5317:     rp   = aj + ai[row]; ap = aa + ai[row];
5318:     rmax = imax[row]; nrow = ailen[row];
5319:     low  = 0;
5320:     high = nrow;
5321:     for (l=0; l<n; l++) { /* loop over added columns */
5322:       if (in[l] < 0) continue;
5323:       if (PetscUnlikelyDebug(in[l] >= A->cmap->n)) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
5324:       col = in[l];
5325:       if (roworiented) value = v[l + k*n];
5326:       else value = v[k + l*m];

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

5330:       if (col <= lastcol) low = 0;
5331:       else high = nrow;
5332:       lastcol = col;
5333:       while (high-low > 5) {
5334:         t = (low+high)/2;
5335:         if (rp[t] > col) high = t;
5336:         else             low  = t;
5337:       }
5338:       for (i=low; i<high; i++) {
5339:         if (rp[i] > col) break;
5340:         if (rp[i] == col) {
5341:           if (is == ADD_VALUES) ap[i] += value;
5342:           else                  ap[i] = value;
5343:           goto noinsert;
5344:         }
5345:       }
5346:       if (value == 0.0 && ignorezeroentries) goto noinsert;
5347:       if (nonew == 1) goto noinsert;
5348:       if (nonew == -1) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
5349:       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
5350:       N = nrow++ - 1; a->nz++; high++;
5351:       /* shift up all the later entries in this row */
5352:       for (ii=N; ii>=i; ii--) {
5353:         rp[ii+1] = rp[ii];
5354:         ap[ii+1] = ap[ii];
5355:       }
5356:       rp[i] = col;
5357:       ap[i] = value;
5358:       A->nonzerostate++;
5359: noinsert:;
5360:       low = i + 1;
5361:     }
5362:     ailen[row] = nrow;
5363:   }
5364:   PetscFunctionReturnVoid();
5365: }