Actual source code: aij.c

petsc-master 2019-07-15
Report Typos and Errors

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


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

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

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

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

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

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

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

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

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

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

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

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

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

135:   *keptrows = 0;
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:     aa = a->a + ii[i];
144:     for (j=0; j<n; j++) {
145:       if (aa[j] != 0.0) goto ok1;
146:     }
147:     cnt++;
148: ok1:;
149:   }
150:   if (!cnt) return(0);
151:   PetscMalloc1(A->rmap->n-cnt,&rows);
152:   cnt  = 0;
153:   for (i=0; i<m; i++) {
154:     n = ii[i+1] - ii[i];
155:     if (!n) continue;
156:     aa = a->a + ii[i];
157:     for (j=0; j<n; j++) {
158:       if (aa[j] != 0.0) {
159:         rows[cnt++] = i;
160:         break;
161:       }
162:     }
163:   }
164:   ISCreateGeneral(PETSC_COMM_SELF,cnt,rows,PETSC_OWN_POINTER,keptrows);
165:   return(0);
166: }

168: PetscErrorCode  MatDiagonalSet_SeqAIJ(Mat Y,Vec D,InsertMode is)
169: {
170:   PetscErrorCode    ierr;
171:   Mat_SeqAIJ        *aij = (Mat_SeqAIJ*) Y->data;
172:   PetscInt          i,m = Y->rmap->n;
173:   const PetscInt    *diag;
174:   MatScalar         *aa = aij->a;
175:   const PetscScalar *v;
176:   PetscBool         missing;

179:   if (Y->assembled) {
180:     MatMissingDiagonal_SeqAIJ(Y,&missing,NULL);
181:     if (!missing) {
182:       diag = aij->diag;
183:       VecGetArrayRead(D,&v);
184:       if (is == INSERT_VALUES) {
185:         for (i=0; i<m; i++) {
186:           aa[diag[i]] = v[i];
187:         }
188:       } else {
189:         for (i=0; i<m; i++) {
190:           aa[diag[i]] += v[i];
191:         }
192:       }
193:       VecRestoreArrayRead(D,&v);
194:       return(0);
195:     }
196:     MatSeqAIJInvalidateDiagonal(Y);
197:   }
198:   MatDiagonalSet_Default(Y,D,is);
199:   return(0);
200: }

202: PetscErrorCode MatGetRowIJ_SeqAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *m,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
203: {
204:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
206:   PetscInt       i,ishift;

209:   *m = A->rmap->n;
210:   if (!ia) return(0);
211:   ishift = 0;
212:   if (symmetric && !A->structurally_symmetric) {
213:     MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,PETSC_TRUE,ishift,oshift,(PetscInt**)ia,(PetscInt**)ja);
214:   } else if (oshift == 1) {
215:     PetscInt *tia;
216:     PetscInt nz = a->i[A->rmap->n];
217:     /* malloc space and  add 1 to i and j indices */
218:     PetscMalloc1(A->rmap->n+1,&tia);
219:     for (i=0; i<A->rmap->n+1; i++) tia[i] = a->i[i] + 1;
220:     *ia = tia;
221:     if (ja) {
222:       PetscInt *tja;
223:       PetscMalloc1(nz+1,&tja);
224:       for (i=0; i<nz; i++) tja[i] = a->j[i] + 1;
225:       *ja = tja;
226:     }
227:   } else {
228:     *ia = a->i;
229:     if (ja) *ja = a->j;
230:   }
231:   return(0);
232: }

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

239:   if (!ia) return(0);
240:   if ((symmetric && !A->structurally_symmetric) || oshift == 1) {
241:     PetscFree(*ia);
242:     if (ja) {PetscFree(*ja);}
243:   }
244:   return(0);
245: }

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

255:   *nn = n;
256:   if (!ia) return(0);
257:   if (symmetric) {
258:     MatToSymmetricIJ_SeqAIJ(A->rmap->n,a->i,a->j,PETSC_TRUE,0,oshift,(PetscInt**)ia,(PetscInt**)ja);
259:   } else {
260:     PetscCalloc1(n,&collengths);
261:     PetscMalloc1(n+1,&cia);
262:     PetscMalloc1(nz,&cja);
263:     jj   = a->j;
264:     for (i=0; i<nz; i++) {
265:       collengths[jj[i]]++;
266:     }
267:     cia[0] = oshift;
268:     for (i=0; i<n; i++) {
269:       cia[i+1] = cia[i] + collengths[i];
270:     }
271:     PetscArrayzero(collengths,n);
272:     jj   = a->j;
273:     for (row=0; row<m; row++) {
274:       mr = a->i[row+1] - a->i[row];
275:       for (i=0; i<mr; i++) {
276:         col = *jj++;

278:         cja[cia[col] + collengths[col]++ - oshift] = row + oshift;
279:       }
280:     }
281:     PetscFree(collengths);
282:     *ia  = cia; *ja = cja;
283:   }
284:   return(0);
285: }

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

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

294:   PetscFree(*ia);
295:   PetscFree(*ja);
296:   return(0);
297: }

299: /*
300:  MatGetColumnIJ_SeqAIJ_Color() and MatRestoreColumnIJ_SeqAIJ_Color() are customized from
301:  MatGetColumnIJ_SeqAIJ() and MatRestoreColumnIJ_SeqAIJ() by adding an output
302:  spidx[], index of a->a, to be used in MatTransposeColoringCreate_SeqAIJ() and MatFDColoringCreate_SeqXAIJ()
303: */
304: PetscErrorCode MatGetColumnIJ_SeqAIJ_Color(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool inodecompressed,PetscInt *nn,const PetscInt *ia[],const PetscInt *ja[],PetscInt *spidx[],PetscBool  *done)
305: {
306:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
308:   PetscInt       i,*collengths,*cia,*cja,n = A->cmap->n,m = A->rmap->n;
309:   PetscInt       nz = a->i[m],row,mr,col,tmp;
310:   PetscInt       *cspidx;
311:   const PetscInt *jj;

314:   *nn = n;
315:   if (!ia) return(0);

317:   PetscCalloc1(n,&collengths);
318:   PetscMalloc1(n+1,&cia);
319:   PetscMalloc1(nz,&cja);
320:   PetscMalloc1(nz,&cspidx);
321:   jj   = a->j;
322:   for (i=0; i<nz; i++) {
323:     collengths[jj[i]]++;
324:   }
325:   cia[0] = oshift;
326:   for (i=0; i<n; i++) {
327:     cia[i+1] = cia[i] + collengths[i];
328:   }
329:   PetscArrayzero(collengths,n);
330:   jj   = a->j;
331:   for (row=0; row<m; row++) {
332:     mr = a->i[row+1] - a->i[row];
333:     for (i=0; i<mr; i++) {
334:       col         = *jj++;
335:       tmp         = cia[col] + collengths[col]++ - oshift;
336:       cspidx[tmp] = a->i[row] + i; /* index of a->j */
337:       cja[tmp]    = row + oshift;
338:     }
339:   }
340:   PetscFree(collengths);
341:   *ia    = cia;
342:   *ja    = cja;
343:   *spidx = cspidx;
344:   return(0);
345: }

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

352:   MatRestoreColumnIJ_SeqAIJ(A,oshift,symmetric,inodecompressed,n,ia,ja,done);
353:   PetscFree(*spidx);
354:   return(0);
355: }

357: PetscErrorCode MatSetValuesRow_SeqAIJ(Mat A,PetscInt row,const PetscScalar v[])
358: {
359:   Mat_SeqAIJ     *a  = (Mat_SeqAIJ*)A->data;
360:   PetscInt       *ai = a->i;

364:   PetscArraycpy(a->a+ai[row],v,ai[row+1]-ai[row]);
365:   return(0);
366: }

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

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

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

378: */

380:  #include <petsc/private/isimpl.h>
381: PetscErrorCode MatSeqAIJSetValuesLocalFast(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
382: {
383:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
384:   PetscInt       low,high,t,row,nrow,i,col,l;
385:   const PetscInt *rp,*ai = a->i,*ailen = a->ilen,*aj = a->j;
386:   PetscInt       lastcol = -1;
387:   MatScalar      *ap,value,*aa = a->a;
388:   const PetscInt *ridx = A->rmap->mapping->indices,*cidx = A->cmap->mapping->indices;

390:   row = ridx[im[0]];
391:   rp   = aj + ai[row];
392:   ap = aa + ai[row];
393:   nrow = ailen[row];
394:   low  = 0;
395:   high = nrow;
396:   for (l=0; l<n; l++) { /* loop over added columns */
397:     col = cidx[in[l]];
398:     value = v[l];

400:     if (col <= lastcol) low = 0;
401:     else high = nrow;
402:     lastcol = col;
403:     while (high-low > 5) {
404:       t = (low+high)/2;
405:       if (rp[t] > col) high = t;
406:       else low = t;
407:     }
408:     for (i=low; i<high; i++) {
409:       if (rp[i] == col) {
410:         ap[i] += value;
411:         low = i + 1;
412:         break;
413:       }
414:     }
415:   }
416:   return 0;
417: }

419: PetscErrorCode MatSetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
420: {
421:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
422:   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
423:   PetscInt       *imax = a->imax,*ai = a->i,*ailen = a->ilen;
425:   PetscInt       *aj = a->j,nonew = a->nonew,lastcol = -1;
426:   MatScalar      *ap=NULL,value=0.0,*aa = a->a;
427:   PetscBool      ignorezeroentries = a->ignorezeroentries;
428:   PetscBool      roworiented       = a->roworiented;

431:   for (k=0; k<m; k++) { /* loop over added rows */
432:     row = im[k];
433:     if (row < 0) continue;
434: #if defined(PETSC_USE_DEBUG)
435:     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);
436: #endif
437:     rp   = aj + ai[row];
438:     if (!A->structure_only) ap = aa + ai[row];
439:     rmax = imax[row]; nrow = ailen[row];
440:     low  = 0;
441:     high = nrow;
442:     for (l=0; l<n; l++) { /* loop over added columns */
443:       if (in[l] < 0) continue;
444: #if defined(PETSC_USE_DEBUG)
445:       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);
446: #endif
447:       col = in[l];
448:       if (v && !A->structure_only) value = roworiented ? v[l + k*n] : v[k + l*m];
449:       if (!A->structure_only && value == 0.0 && ignorezeroentries && is == ADD_VALUES && row != col) continue;

451:       if (col <= lastcol) low = 0;
452:       else high = nrow;
453:       lastcol = col;
454:       while (high-low > 5) {
455:         t = (low+high)/2;
456:         if (rp[t] > col) high = t;
457:         else low = t;
458:       }
459:       for (i=low; i<high; i++) {
460:         if (rp[i] > col) break;
461:         if (rp[i] == col) {
462:           if (!A->structure_only) {
463:             if (is == ADD_VALUES) {
464:               ap[i] += value;
465:               (void)PetscLogFlops(1.0);
466:             }
467:             else ap[i] = value;
468:           }
469:           low = i + 1;
470:           goto noinsert;
471:         }
472:       }
473:       if (value == 0.0 && ignorezeroentries && row != col) goto noinsert;
474:       if (nonew == 1) goto noinsert;
475:       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero at (%D,%D) in the matrix",row,col);
476:       if (A->structure_only) {
477:         MatSeqXAIJReallocateAIJ_structure_only(A,A->rmap->n,1,nrow,row,col,rmax,ai,aj,rp,imax,nonew,MatScalar);
478:       } else {
479:         MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
480:       }
481:       N = nrow++ - 1; a->nz++; high++;
482:       /* shift up all the later entries in this row */
483:       PetscArraymove(rp+i+1,rp+i,N-i+1);
484:       rp[i] = col;
485:       if (!A->structure_only){
486:         PetscArraymove(ap+i+1,ap+i,N-i+1);
487:         ap[i] = value;
488:       }
489:       low = i + 1;
490:       A->nonzerostate++;
491: noinsert:;
492:     }
493:     ailen[row] = nrow;
494:   }
495:   return(0);
496: }

498: PetscErrorCode MatSetValues_SeqAIJ_SortedFull(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
499: {
500:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
501:   PetscInt       *rp,k,row;
502:   PetscInt       *ai = a->i,*ailen = a->ilen;
504:   PetscInt       *aj = a->j;
505:   MatScalar      *aa = a->a,*ap;

508:   for (k=0; k<m; k++) { /* loop over added rows */
509:     row  = im[k];
510:     rp   = aj + ai[row];
511:     ap   = aa + ai[row];
512:     if (!A->was_assembled) {
513:       PetscMemcpy(rp,in,n*sizeof(PetscInt));
514:     }
515:     if (!A->structure_only) {
516:       if (v) {
517:         PetscMemcpy(ap,v,n*sizeof(PetscScalar));
518:         v   += n;
519:       } else {
520:         PetscMemzero(ap,n*sizeof(PetscScalar));
521:       }
522:     }
523:     ailen[row] = n;
524:     a->nz      += n;
525:   }
526:   return(0);
527: }


530: PetscErrorCode MatGetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
531: {
532:   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
533:   PetscInt   *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
534:   PetscInt   *ai = a->i,*ailen = a->ilen;
535:   MatScalar  *ap,*aa = a->a;

538:   for (k=0; k<m; k++) { /* loop over rows */
539:     row = im[k];
540:     if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */
541:     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);
542:     rp   = aj + ai[row]; ap = aa + ai[row];
543:     nrow = ailen[row];
544:     for (l=0; l<n; l++) { /* loop over columns */
545:       if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */
546:       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);
547:       col  = in[l];
548:       high = nrow; low = 0; /* assume unsorted */
549:       while (high-low > 5) {
550:         t = (low+high)/2;
551:         if (rp[t] > col) high = t;
552:         else low = t;
553:       }
554:       for (i=low; i<high; i++) {
555:         if (rp[i] > col) break;
556:         if (rp[i] == col) {
557:           *v++ = ap[i];
558:           goto finished;
559:         }
560:       }
561:       *v++ = 0.0;
562: finished:;
563:     }
564:   }
565:   return(0);
566: }


569: PetscErrorCode MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer)
570: {
571:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
573:   PetscInt       i,*col_lens;
574:   int            fd;
575:   FILE           *file;

578:   PetscViewerBinaryGetDescriptor(viewer,&fd);
579:   PetscMalloc1(4+A->rmap->n,&col_lens);

581:   col_lens[0] = MAT_FILE_CLASSID;
582:   col_lens[1] = A->rmap->n;
583:   col_lens[2] = A->cmap->n;
584:   col_lens[3] = a->nz;

586:   /* store lengths of each row and write (including header) to file */
587:   for (i=0; i<A->rmap->n; i++) {
588:     col_lens[4+i] = a->i[i+1] - a->i[i];
589:   }
590:   PetscBinaryWrite(fd,col_lens,4+A->rmap->n,PETSC_INT,PETSC_TRUE);
591:   PetscFree(col_lens);

593:   /* store column indices (zero start index) */
594:   PetscBinaryWrite(fd,a->j,a->nz,PETSC_INT,PETSC_FALSE);

596:   /* store nonzero values */
597:   PetscBinaryWrite(fd,a->a,a->nz,PETSC_SCALAR,PETSC_FALSE);

599:   PetscViewerBinaryGetInfoPointer(viewer,&file);
600:   if (file) {
601:     fprintf(file,"-matload_block_size %d\n",(int)PetscAbs(A->rmap->bs));
602:   }
603:   return(0);
604: }

606: static PetscErrorCode MatView_SeqAIJ_ASCII_structonly(Mat A,PetscViewer viewer)
607: {
609:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
610:   PetscInt       i,k,m=A->rmap->N;

613:   PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
614:   for (i=0; i<m; i++) {
615:     PetscViewerASCIIPrintf(viewer,"row %D:",i);
616:     for (k=a->i[i]; k<a->i[i+1]; k++) {
617:       PetscViewerASCIIPrintf(viewer," (%D) ",a->j[k]);
618:     }
619:     PetscViewerASCIIPrintf(viewer,"\n");
620:   }
621:   PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
622:   return(0);
623: }

625: extern PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);

627: PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer)
628: {
629:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
630:   PetscErrorCode    ierr;
631:   PetscInt          i,j,m = A->rmap->n;
632:   const char        *name;
633:   PetscViewerFormat format;

636:   if (A->structure_only) {
637:     MatView_SeqAIJ_ASCII_structonly(A,viewer);
638:     return(0);
639:   }

641:   PetscViewerGetFormat(viewer,&format);
642:   if (format == PETSC_VIEWER_ASCII_MATLAB) {
643:     PetscInt nofinalvalue = 0;
644:     if (m && ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->cmap->n-1))) {
645:       /* Need a dummy value to ensure the dimension of the matrix. */
646:       nofinalvalue = 1;
647:     }
648:     PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
649:     PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->cmap->n);
650:     PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);
651: #if defined(PETSC_USE_COMPLEX)
652:     PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,4);\n",a->nz+nofinalvalue);
653: #else
654:     PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);
655: #endif
656:     PetscViewerASCIIPrintf(viewer,"zzz = [\n");

658:     for (i=0; i<m; i++) {
659:       for (j=a->i[i]; j<a->i[i+1]; j++) {
660: #if defined(PETSC_USE_COMPLEX)
661:         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]));
662: #else
663:         PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",i+1,a->j[j]+1,(double)a->a[j]);
664: #endif
665:       }
666:     }
667:     if (nofinalvalue) {
668: #if defined(PETSC_USE_COMPLEX)
669:       PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e %18.16e\n",m,A->cmap->n,0.,0.);
670: #else
671:       PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",m,A->cmap->n,0.0);
672: #endif
673:     }
674:     PetscObjectGetName((PetscObject)A,&name);
675:     PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);
676:     PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
677:   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO) {
678:     return(0);
679:   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
680:     PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
681:     for (i=0; i<m; i++) {
682:       PetscViewerASCIIPrintf(viewer,"row %D:",i);
683:       for (j=a->i[i]; j<a->i[i+1]; j++) {
684: #if defined(PETSC_USE_COMPLEX)
685:         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
686:           PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));
687:         } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
688:           PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)-PetscImaginaryPart(a->a[j]));
689:         } else if (PetscRealPart(a->a[j]) != 0.0) {
690:           PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));
691:         }
692: #else
693:         if (a->a[j] != 0.0) {PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);}
694: #endif
695:       }
696:       PetscViewerASCIIPrintf(viewer,"\n");
697:     }
698:     PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
699:   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
700:     PetscInt nzd=0,fshift=1,*sptr;
701:     PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
702:     PetscMalloc1(m+1,&sptr);
703:     for (i=0; i<m; i++) {
704:       sptr[i] = nzd+1;
705:       for (j=a->i[i]; j<a->i[i+1]; j++) {
706:         if (a->j[j] >= i) {
707: #if defined(PETSC_USE_COMPLEX)
708:           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
709: #else
710:           if (a->a[j] != 0.0) nzd++;
711: #endif
712:         }
713:       }
714:     }
715:     sptr[m] = nzd+1;
716:     PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);
717:     for (i=0; i<m+1; i+=6) {
718:       if (i+4<m) {
719:         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]);
720:       } else if (i+3<m) {
721:         PetscViewerASCIIPrintf(viewer," %D %D %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3],sptr[i+4]);
722:       } else if (i+2<m) {
723:         PetscViewerASCIIPrintf(viewer," %D %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3]);
724:       } else if (i+1<m) {
725:         PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);
726:       } else if (i<m) {
727:         PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);
728:       } else {
729:         PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);
730:       }
731:     }
732:     PetscViewerASCIIPrintf(viewer,"\n");
733:     PetscFree(sptr);
734:     for (i=0; i<m; i++) {
735:       for (j=a->i[i]; j<a->i[i+1]; j++) {
736:         if (a->j[j] >= i) {PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);}
737:       }
738:       PetscViewerASCIIPrintf(viewer,"\n");
739:     }
740:     PetscViewerASCIIPrintf(viewer,"\n");
741:     for (i=0; i<m; i++) {
742:       for (j=a->i[i]; j<a->i[i+1]; j++) {
743:         if (a->j[j] >= i) {
744: #if defined(PETSC_USE_COMPLEX)
745:           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) {
746:             PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));
747:           }
748: #else
749:           if (a->a[j] != 0.0) {PetscViewerASCIIPrintf(viewer," %18.16e ",(double)a->a[j]);}
750: #endif
751:         }
752:       }
753:       PetscViewerASCIIPrintf(viewer,"\n");
754:     }
755:     PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
756:   } else if (format == PETSC_VIEWER_ASCII_DENSE) {
757:     PetscInt    cnt = 0,jcnt;
758:     PetscScalar value;
759: #if defined(PETSC_USE_COMPLEX)
760:     PetscBool   realonly = PETSC_TRUE;

762:     for (i=0; i<a->i[m]; i++) {
763:       if (PetscImaginaryPart(a->a[i]) != 0.0) {
764:         realonly = PETSC_FALSE;
765:         break;
766:       }
767:     }
768: #endif

770:     PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
771:     for (i=0; i<m; i++) {
772:       jcnt = 0;
773:       for (j=0; j<A->cmap->n; j++) {
774:         if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
775:           value = a->a[cnt++];
776:           jcnt++;
777:         } else {
778:           value = 0.0;
779:         }
780: #if defined(PETSC_USE_COMPLEX)
781:         if (realonly) {
782:           PetscViewerASCIIPrintf(viewer," %7.5e ",(double)PetscRealPart(value));
783:         } else {
784:           PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",(double)PetscRealPart(value),(double)PetscImaginaryPart(value));
785:         }
786: #else
787:         PetscViewerASCIIPrintf(viewer," %7.5e ",(double)value);
788: #endif
789:       }
790:       PetscViewerASCIIPrintf(viewer,"\n");
791:     }
792:     PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
793:   } else if (format == PETSC_VIEWER_ASCII_MATRIXMARKET) {
794:     PetscInt fshift=1;
795:     PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
796: #if defined(PETSC_USE_COMPLEX)
797:     PetscViewerASCIIPrintf(viewer,"%%%%MatrixMarket matrix coordinate complex general\n");
798: #else
799:     PetscViewerASCIIPrintf(viewer,"%%%%MatrixMarket matrix coordinate real general\n");
800: #endif
801:     PetscViewerASCIIPrintf(viewer,"%D %D %D\n", m, A->cmap->n, a->nz);
802:     for (i=0; i<m; i++) {
803:       for (j=a->i[i]; j<a->i[i+1]; j++) {
804: #if defined(PETSC_USE_COMPLEX)
805:         PetscViewerASCIIPrintf(viewer,"%D %D %g %g\n", i+fshift,a->j[j]+fshift,(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));
806: #else
807:         PetscViewerASCIIPrintf(viewer,"%D %D %g\n", i+fshift, a->j[j]+fshift, (double)a->a[j]);
808: #endif
809:       }
810:     }
811:     PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
812:   } else {
813:     PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
814:     if (A->factortype) {
815:       for (i=0; i<m; i++) {
816:         PetscViewerASCIIPrintf(viewer,"row %D:",i);
817:         /* L part */
818:         for (j=a->i[i]; j<a->i[i+1]; j++) {
819: #if defined(PETSC_USE_COMPLEX)
820:           if (PetscImaginaryPart(a->a[j]) > 0.0) {
821:             PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));
822:           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
823:             PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)(-PetscImaginaryPart(a->a[j])));
824:           } else {
825:             PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));
826:           }
827: #else
828:           PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);
829: #endif
830:         }
831:         /* diagonal */
832:         j = a->diag[i];
833: #if defined(PETSC_USE_COMPLEX)
834:         if (PetscImaginaryPart(a->a[j]) > 0.0) {
835:           PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(1.0/a->a[j]),(double)PetscImaginaryPart(1.0/a->a[j]));
836:         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
837:           PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(1.0/a->a[j]),(double)(-PetscImaginaryPart(1.0/a->a[j])));
838:         } else {
839:           PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(1.0/a->a[j]));
840:         }
841: #else
842:         PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)(1.0/a->a[j]));
843: #endif

845:         /* U part */
846:         for (j=a->diag[i+1]+1; j<a->diag[i]; j++) {
847: #if defined(PETSC_USE_COMPLEX)
848:           if (PetscImaginaryPart(a->a[j]) > 0.0) {
849:             PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));
850:           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
851:             PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)(-PetscImaginaryPart(a->a[j])));
852:           } else {
853:             PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));
854:           }
855: #else
856:           PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);
857: #endif
858:         }
859:         PetscViewerASCIIPrintf(viewer,"\n");
860:       }
861:     } else {
862:       for (i=0; i<m; i++) {
863:         PetscViewerASCIIPrintf(viewer,"row %D:",i);
864:         for (j=a->i[i]; j<a->i[i+1]; j++) {
865: #if defined(PETSC_USE_COMPLEX)
866:           if (PetscImaginaryPart(a->a[j]) > 0.0) {
867:             PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));
868:           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
869:             PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)-PetscImaginaryPart(a->a[j]));
870:           } else {
871:             PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));
872:           }
873: #else
874:           PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);
875: #endif
876:         }
877:         PetscViewerASCIIPrintf(viewer,"\n");
878:       }
879:     }
880:     PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
881:   }
882:   PetscViewerFlush(viewer);
883:   return(0);
884: }

886:  #include <petscdraw.h>
887: PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
888: {
889:   Mat               A  = (Mat) Aa;
890:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
891:   PetscErrorCode    ierr;
892:   PetscInt          i,j,m = A->rmap->n;
893:   int               color;
894:   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r;
895:   PetscViewer       viewer;
896:   PetscViewerFormat format;

899:   PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);
900:   PetscViewerGetFormat(viewer,&format);
901:   PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);

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

905:   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
906:     PetscDrawCollectiveBegin(draw);
907:     /* Blue for negative, Cyan for zero and  Red for positive */
908:     color = PETSC_DRAW_BLUE;
909:     for (i=0; i<m; i++) {
910:       y_l = m - i - 1.0; y_r = y_l + 1.0;
911:       for (j=a->i[i]; j<a->i[i+1]; j++) {
912:         x_l = a->j[j]; x_r = x_l + 1.0;
913:         if (PetscRealPart(a->a[j]) >=  0.) continue;
914:         PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);
915:       }
916:     }
917:     color = PETSC_DRAW_CYAN;
918:     for (i=0; i<m; i++) {
919:       y_l = m - i - 1.0; y_r = y_l + 1.0;
920:       for (j=a->i[i]; j<a->i[i+1]; j++) {
921:         x_l = a->j[j]; x_r = x_l + 1.0;
922:         if (a->a[j] !=  0.) continue;
923:         PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);
924:       }
925:     }
926:     color = PETSC_DRAW_RED;
927:     for (i=0; i<m; i++) {
928:       y_l = m - i - 1.0; y_r = y_l + 1.0;
929:       for (j=a->i[i]; j<a->i[i+1]; j++) {
930:         x_l = a->j[j]; x_r = x_l + 1.0;
931:         if (PetscRealPart(a->a[j]) <=  0.) continue;
932:         PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);
933:       }
934:     }
935:     PetscDrawCollectiveEnd(draw);
936:   } else {
937:     /* use contour shading to indicate magnitude of values */
938:     /* first determine max of all nonzero values */
939:     PetscReal minv = 0.0, maxv = 0.0;
940:     PetscInt  nz = a->nz, count = 0;
941:     PetscDraw popup;

943:     for (i=0; i<nz; i++) {
944:       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
945:     }
946:     if (minv >= maxv) maxv = minv + PETSC_SMALL;
947:     PetscDrawGetPopup(draw,&popup);
948:     PetscDrawScalePopup(popup,minv,maxv);

950:     PetscDrawCollectiveBegin(draw);
951:     for (i=0; i<m; i++) {
952:       y_l = m - i - 1.0;
953:       y_r = y_l + 1.0;
954:       for (j=a->i[i]; j<a->i[i+1]; j++) {
955:         x_l = a->j[j];
956:         x_r = x_l + 1.0;
957:         color = PetscDrawRealToColor(PetscAbsScalar(a->a[count]),minv,maxv);
958:         PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);
959:         count++;
960:       }
961:     }
962:     PetscDrawCollectiveEnd(draw);
963:   }
964:   return(0);
965: }

967:  #include <petscdraw.h>
968: PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
969: {
971:   PetscDraw      draw;
972:   PetscReal      xr,yr,xl,yl,h,w;
973:   PetscBool      isnull;

976:   PetscViewerDrawGetDraw(viewer,0,&draw);
977:   PetscDrawIsNull(draw,&isnull);
978:   if (isnull) return(0);

980:   xr   = A->cmap->n; yr  = A->rmap->n; h = yr/10.0; w = xr/10.0;
981:   xr  += w;          yr += h;         xl = -w;     yl = -h;
982:   PetscDrawSetCoordinates(draw,xl,yl,xr,yr);
983:   PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);
984:   PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);
985:   PetscObjectCompose((PetscObject)A,"Zoomviewer",NULL);
986:   PetscDrawSave(draw);
987:   return(0);
988: }

990: PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer)
991: {
993:   PetscBool      iascii,isbinary,isdraw;

996:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
997:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
998:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
999:   if (iascii) {
1000:     MatView_SeqAIJ_ASCII(A,viewer);
1001:   } else if (isbinary) {
1002:     MatView_SeqAIJ_Binary(A,viewer);
1003:   } else if (isdraw) {
1004:     MatView_SeqAIJ_Draw(A,viewer);
1005:   }
1006:   MatView_SeqAIJ_Inode(A,viewer);
1007:   return(0);
1008: }

1010: PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
1011: {
1012:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1014:   PetscInt       fshift = 0,i,*ai = a->i,*aj = a->j,*imax = a->imax;
1015:   PetscInt       m      = A->rmap->n,*ip,N,*ailen = a->ilen,rmax = 0;
1016:   MatScalar      *aa    = a->a,*ap;
1017:   PetscReal      ratio  = 0.6;

1020:   if (mode == MAT_FLUSH_ASSEMBLY) return(0);
1021:   MatSeqAIJInvalidateDiagonal(A);
1022:   if (A->was_assembled && A->ass_nonzerostate == A->nonzerostate) return(0);

1024:   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
1025:   for (i=1; i<m; i++) {
1026:     /* move each row back by the amount of empty slots (fshift) before it*/
1027:     fshift += imax[i-1] - ailen[i-1];
1028:     rmax    = PetscMax(rmax,ailen[i]);
1029:     if (fshift) {
1030:       ip = aj + ai[i];
1031:       ap = aa + ai[i];
1032:       N  = ailen[i];
1033:       PetscArraymove(ip-fshift,ip,N);
1034:       if (!A->structure_only) {
1035:         PetscArraymove(ap-fshift,ap,N);
1036:       }
1037:     }
1038:     ai[i] = ai[i-1] + ailen[i-1];
1039:   }
1040:   if (m) {
1041:     fshift += imax[m-1] - ailen[m-1];
1042:     ai[m]   = ai[m-1] + ailen[m-1];
1043:   }

1045:   /* reset ilen and imax for each row */
1046:   a->nonzerorowcnt = 0;
1047:   if (A->structure_only) {
1048:     PetscFree(a->imax);
1049:     PetscFree(a->ilen);
1050:   } else { /* !A->structure_only */
1051:     for (i=0; i<m; i++) {
1052:       ailen[i] = imax[i] = ai[i+1] - ai[i];
1053:       a->nonzerorowcnt += ((ai[i+1] - ai[i]) > 0);
1054:     }
1055:   }
1056:   a->nz = ai[m];
1057:   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);

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

1064:   A->info.mallocs    += a->reallocs;
1065:   a->reallocs         = 0;
1066:   A->info.nz_unneeded = (PetscReal)fshift;
1067:   a->rmax             = rmax;

1069:   if (!A->structure_only) {
1070:     MatCheckCompressedRow(A,a->nonzerorowcnt,&a->compressedrow,a->i,m,ratio);
1071:   }
1072:   MatAssemblyEnd_SeqAIJ_Inode(A,mode);
1073:   return(0);
1074: }

1076: PetscErrorCode MatRealPart_SeqAIJ(Mat A)
1077: {
1078:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1079:   PetscInt       i,nz = a->nz;
1080:   MatScalar      *aa = a->a;

1084:   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
1085:   MatSeqAIJInvalidateDiagonal(A);
1086:   return(0);
1087: }

1089: PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
1090: {
1091:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1092:   PetscInt       i,nz = a->nz;
1093:   MatScalar      *aa = a->a;

1097:   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
1098:   MatSeqAIJInvalidateDiagonal(A);
1099:   return(0);
1100: }

1102: PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
1103: {
1104:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;

1108:   PetscArrayzero(a->a,a->i[A->rmap->n]);
1109:   MatSeqAIJInvalidateDiagonal(A);
1110:   return(0);
1111: }

1113: PetscErrorCode MatDestroy_SeqAIJ(Mat A)
1114: {
1115:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;

1119: #if defined(PETSC_USE_LOG)
1120:   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz);
1121: #endif
1122:   MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);
1123:   ISDestroy(&a->row);
1124:   ISDestroy(&a->col);
1125:   PetscFree(a->diag);
1126:   PetscFree(a->ibdiag);
1127:   PetscFree(a->imax);
1128:   PetscFree(a->ilen);
1129:   PetscFree(a->ipre);
1130:   PetscFree3(a->idiag,a->mdiag,a->ssor_work);
1131:   PetscFree(a->solve_work);
1132:   ISDestroy(&a->icol);
1133:   PetscFree(a->saved_values);
1134:   ISColoringDestroy(&a->coloring);
1135:   PetscFree2(a->compressedrow.i,a->compressedrow.rindex);
1136:   PetscFree(a->matmult_abdense);

1138:   MatDestroy_SeqAIJ_Inode(A);
1139:   PetscFree(A->data);

1141:   PetscObjectChangeTypeName((PetscObject)A,0);
1142:   PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetColumnIndices_C",NULL);
1143:   PetscObjectComposeFunction((PetscObject)A,"MatStoreValues_C",NULL);
1144:   PetscObjectComposeFunction((PetscObject)A,"MatRetrieveValues_C",NULL);
1145:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqsbaij_C",NULL);
1146:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqbaij_C",NULL);
1147:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqaijperm_C",NULL);
1148: #if defined(PETSC_HAVE_ELEMENTAL)
1149:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_elemental_C",NULL);
1150: #endif
1151: #if defined(PETSC_HAVE_HYPRE)
1152:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_hypre_C",NULL);
1153:   PetscObjectComposeFunction((PetscObject)A,"MatMatMatMult_transpose_seqaij_seqaij_C",NULL);
1154: #endif
1155:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqdense_C",NULL);
1156:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqsell_C",NULL);
1157:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_is_C",NULL);
1158:   PetscObjectComposeFunction((PetscObject)A,"MatIsTranspose_C",NULL);
1159:   PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocation_C",NULL);
1160:   PetscObjectComposeFunction((PetscObject)A,"MatResetPreallocation_C",NULL);
1161:   PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C",NULL);
1162:   PetscObjectComposeFunction((PetscObject)A,"MatReorderForNonzeroDiagonal_C",NULL);
1163:   PetscObjectComposeFunction((PetscObject)A,"MatPtAP_is_seqaij_C",NULL);
1164:   return(0);
1165: }

1167: PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscBool flg)
1168: {
1169:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;

1173:   switch (op) {
1174:   case MAT_ROW_ORIENTED:
1175:     a->roworiented = flg;
1176:     break;
1177:   case MAT_KEEP_NONZERO_PATTERN:
1178:     a->keepnonzeropattern = flg;
1179:     break;
1180:   case MAT_NEW_NONZERO_LOCATIONS:
1181:     a->nonew = (flg ? 0 : 1);
1182:     break;
1183:   case MAT_NEW_NONZERO_LOCATION_ERR:
1184:     a->nonew = (flg ? -1 : 0);
1185:     break;
1186:   case MAT_NEW_NONZERO_ALLOCATION_ERR:
1187:     a->nonew = (flg ? -2 : 0);
1188:     break;
1189:   case MAT_UNUSED_NONZERO_LOCATION_ERR:
1190:     a->nounused = (flg ? -1 : 0);
1191:     break;
1192:   case MAT_IGNORE_ZERO_ENTRIES:
1193:     a->ignorezeroentries = flg;
1194:     break;
1195:   case MAT_SPD:
1196:   case MAT_SYMMETRIC:
1197:   case MAT_STRUCTURALLY_SYMMETRIC:
1198:   case MAT_HERMITIAN:
1199:   case MAT_SYMMETRY_ETERNAL:
1200:   case MAT_STRUCTURE_ONLY:
1201:     /* These options are handled directly by MatSetOption() */
1202:     break;
1203:   case MAT_NEW_DIAGONALS:
1204:   case MAT_IGNORE_OFF_PROC_ENTRIES:
1205:   case MAT_USE_HASH_TABLE:
1206:     PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);
1207:     break;
1208:   case MAT_USE_INODES:
1209:     /* Not an error because MatSetOption_SeqAIJ_Inode handles this one */
1210:     break;
1211:   case MAT_SUBMAT_SINGLEIS:
1212:     A->submat_singleis = flg;
1213:     break;
1214:   case MAT_SORTED_FULL:
1215:     if (flg) A->ops->setvalues = MatSetValues_SeqAIJ_SortedFull;
1216:     else     A->ops->setvalues = MatSetValues_SeqAIJ;
1217:     break;
1218:   default:
1219:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
1220:   }
1221:   MatSetOption_SeqAIJ_Inode(A,op,flg);
1222:   return(0);
1223: }

1225: PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
1226: {
1227:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1229:   PetscInt       i,j,n,*ai=a->i,*aj=a->j;
1230:   PetscScalar    *aa=a->a,*x;

1233:   VecGetLocalSize(v,&n);
1234:   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");

1236:   if (A->factortype == MAT_FACTOR_ILU || A->factortype == MAT_FACTOR_LU) {
1237:     PetscInt *diag=a->diag;
1238:     VecGetArrayWrite(v,&x);
1239:     for (i=0; i<n; i++) x[i] = 1.0/aa[diag[i]];
1240:     VecRestoreArrayWrite(v,&x);
1241:     return(0);
1242:   }

1244:   VecGetArrayWrite(v,&x);
1245:   for (i=0; i<n; i++) {
1246:     x[i] = 0.0;
1247:     for (j=ai[i]; j<ai[i+1]; j++) {
1248:       if (aj[j] == i) {
1249:         x[i] = aa[j];
1250:         break;
1251:       }
1252:     }
1253:   }
1254:   VecRestoreArrayWrite(v,&x);
1255:   return(0);
1256: }

1258: #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
1259: PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
1260: {
1261:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1262:   PetscScalar       *y;
1263:   const PetscScalar *x;
1264:   PetscErrorCode    ierr;
1265:   PetscInt          m = A->rmap->n;
1266: #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1267:   const MatScalar   *v;
1268:   PetscScalar       alpha;
1269:   PetscInt          n,i,j;
1270:   const PetscInt    *idx,*ii,*ridx=NULL;
1271:   Mat_CompressedRow cprow    = a->compressedrow;
1272:   PetscBool         usecprow = cprow.use;
1273: #endif

1276:   if (zz != yy) {VecCopy(zz,yy);}
1277:   VecGetArrayRead(xx,&x);
1278:   VecGetArray(yy,&y);

1280: #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1281:   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
1282: #else
1283:   if (usecprow) {
1284:     m    = cprow.nrows;
1285:     ii   = cprow.i;
1286:     ridx = cprow.rindex;
1287:   } else {
1288:     ii = a->i;
1289:   }
1290:   for (i=0; i<m; i++) {
1291:     idx = a->j + ii[i];
1292:     v   = a->a + ii[i];
1293:     n   = ii[i+1] - ii[i];
1294:     if (usecprow) {
1295:       alpha = x[ridx[i]];
1296:     } else {
1297:       alpha = x[i];
1298:     }
1299:     for (j=0; j<n; j++) y[idx[j]] += alpha*v[j];
1300:   }
1301: #endif
1302:   PetscLogFlops(2.0*a->nz);
1303:   VecRestoreArrayRead(xx,&x);
1304:   VecRestoreArray(yy,&y);
1305:   return(0);
1306: }

1308: PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
1309: {

1313:   VecSet(yy,0.0);
1314:   MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);
1315:   return(0);
1316: }

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

1320: PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
1321: {
1322:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1323:   PetscScalar       *y;
1324:   const PetscScalar *x;
1325:   const MatScalar   *aa;
1326:   PetscErrorCode    ierr;
1327:   PetscInt          m=A->rmap->n;
1328:   const PetscInt    *aj,*ii,*ridx=NULL;
1329:   PetscInt          n,i;
1330:   PetscScalar       sum;
1331:   PetscBool         usecprow=a->compressedrow.use;

1333: #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
1334: #pragma disjoint(*x,*y,*aa)
1335: #endif

1338:   VecGetArrayRead(xx,&x);
1339:   VecGetArray(yy,&y);
1340:   ii   = a->i;
1341:   if (usecprow) { /* use compressed row format */
1342:     PetscArrayzero(y,m);
1343:     m    = a->compressedrow.nrows;
1344:     ii   = a->compressedrow.i;
1345:     ridx = a->compressedrow.rindex;
1346:     for (i=0; i<m; i++) {
1347:       n           = ii[i+1] - ii[i];
1348:       aj          = a->j + ii[i];
1349:       aa          = a->a + ii[i];
1350:       sum         = 0.0;
1351:       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1352:       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
1353:       y[*ridx++] = sum;
1354:     }
1355:   } else { /* do not use compressed row format */
1356: #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
1357:     aj   = a->j;
1358:     aa   = a->a;
1359:     fortranmultaij_(&m,x,ii,aj,aa,y);
1360: #else
1361:     for (i=0; i<m; i++) {
1362:       n           = ii[i+1] - ii[i];
1363:       aj          = a->j + ii[i];
1364:       aa          = a->a + ii[i];
1365:       sum         = 0.0;
1366:       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1367:       y[i] = sum;
1368:     }
1369: #endif
1370:   }
1371:   PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);
1372:   VecRestoreArrayRead(xx,&x);
1373:   VecRestoreArray(yy,&y);
1374:   return(0);
1375: }

1377: PetscErrorCode MatMultMax_SeqAIJ(Mat A,Vec xx,Vec yy)
1378: {
1379:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1380:   PetscScalar       *y;
1381:   const PetscScalar *x;
1382:   const MatScalar   *aa;
1383:   PetscErrorCode    ierr;
1384:   PetscInt          m=A->rmap->n;
1385:   const PetscInt    *aj,*ii,*ridx=NULL;
1386:   PetscInt          n,i,nonzerorow=0;
1387:   PetscScalar       sum;
1388:   PetscBool         usecprow=a->compressedrow.use;

1390: #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
1391: #pragma disjoint(*x,*y,*aa)
1392: #endif

1395:   VecGetArrayRead(xx,&x);
1396:   VecGetArray(yy,&y);
1397:   if (usecprow) { /* use compressed row format */
1398:     m    = a->compressedrow.nrows;
1399:     ii   = a->compressedrow.i;
1400:     ridx = a->compressedrow.rindex;
1401:     for (i=0; i<m; i++) {
1402:       n           = ii[i+1] - ii[i];
1403:       aj          = a->j + ii[i];
1404:       aa          = a->a + ii[i];
1405:       sum         = 0.0;
1406:       nonzerorow += (n>0);
1407:       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1408:       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
1409:       y[*ridx++] = sum;
1410:     }
1411:   } else { /* do not use compressed row format */
1412:     ii = a->i;
1413:     for (i=0; i<m; i++) {
1414:       n           = ii[i+1] - ii[i];
1415:       aj          = a->j + ii[i];
1416:       aa          = a->a + ii[i];
1417:       sum         = 0.0;
1418:       nonzerorow += (n>0);
1419:       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1420:       y[i] = sum;
1421:     }
1422:   }
1423:   PetscLogFlops(2.0*a->nz - nonzerorow);
1424:   VecRestoreArrayRead(xx,&x);
1425:   VecRestoreArray(yy,&y);
1426:   return(0);
1427: }

1429: PetscErrorCode MatMultAddMax_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1430: {
1431:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1432:   PetscScalar       *y,*z;
1433:   const PetscScalar *x;
1434:   const MatScalar   *aa;
1435:   PetscErrorCode    ierr;
1436:   PetscInt          m = A->rmap->n,*aj,*ii;
1437:   PetscInt          n,i,*ridx=NULL;
1438:   PetscScalar       sum;
1439:   PetscBool         usecprow=a->compressedrow.use;

1442:   VecGetArrayRead(xx,&x);
1443:   VecGetArrayPair(yy,zz,&y,&z);
1444:   if (usecprow) { /* use compressed row format */
1445:     if (zz != yy) {
1446:       PetscArraycpy(z,y,m);
1447:     }
1448:     m    = a->compressedrow.nrows;
1449:     ii   = a->compressedrow.i;
1450:     ridx = a->compressedrow.rindex;
1451:     for (i=0; i<m; i++) {
1452:       n   = ii[i+1] - ii[i];
1453:       aj  = a->j + ii[i];
1454:       aa  = a->a + ii[i];
1455:       sum = y[*ridx];
1456:       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1457:       z[*ridx++] = sum;
1458:     }
1459:   } else { /* do not use compressed row format */
1460:     ii = a->i;
1461:     for (i=0; i<m; i++) {
1462:       n   = ii[i+1] - ii[i];
1463:       aj  = a->j + ii[i];
1464:       aa  = a->a + ii[i];
1465:       sum = y[i];
1466:       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1467:       z[i] = sum;
1468:     }
1469:   }
1470:   PetscLogFlops(2.0*a->nz);
1471:   VecRestoreArrayRead(xx,&x);
1472:   VecRestoreArrayPair(yy,zz,&y,&z);
1473:   return(0);
1474: }

1476: #include <../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h>
1477: PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1478: {
1479:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1480:   PetscScalar       *y,*z;
1481:   const PetscScalar *x;
1482:   const MatScalar   *aa;
1483:   PetscErrorCode    ierr;
1484:   const PetscInt    *aj,*ii,*ridx=NULL;
1485:   PetscInt          m = A->rmap->n,n,i;
1486:   PetscScalar       sum;
1487:   PetscBool         usecprow=a->compressedrow.use;

1490:   VecGetArrayRead(xx,&x);
1491:   VecGetArrayPair(yy,zz,&y,&z);
1492:   if (usecprow) { /* use compressed row format */
1493:     if (zz != yy) {
1494:       PetscArraycpy(z,y,m);
1495:     }
1496:     m    = a->compressedrow.nrows;
1497:     ii   = a->compressedrow.i;
1498:     ridx = a->compressedrow.rindex;
1499:     for (i=0; i<m; i++) {
1500:       n   = ii[i+1] - ii[i];
1501:       aj  = a->j + ii[i];
1502:       aa  = a->a + ii[i];
1503:       sum = y[*ridx];
1504:       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1505:       z[*ridx++] = sum;
1506:     }
1507:   } else { /* do not use compressed row format */
1508:     ii = a->i;
1509: #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
1510:     aj = a->j;
1511:     aa = a->a;
1512:     fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
1513: #else
1514:     for (i=0; i<m; i++) {
1515:       n   = ii[i+1] - ii[i];
1516:       aj  = a->j + ii[i];
1517:       aa  = a->a + ii[i];
1518:       sum = y[i];
1519:       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1520:       z[i] = sum;
1521:     }
1522: #endif
1523:   }
1524:   PetscLogFlops(2.0*a->nz);
1525:   VecRestoreArrayRead(xx,&x);
1526:   VecRestoreArrayPair(yy,zz,&y,&z);
1527:   return(0);
1528: }

1530: /*
1531:      Adds diagonal pointers to sparse matrix structure.
1532: */
1533: PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
1534: {
1535:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1537:   PetscInt       i,j,m = A->rmap->n;

1540:   if (!a->diag) {
1541:     PetscMalloc1(m,&a->diag);
1542:     PetscLogObjectMemory((PetscObject)A, m*sizeof(PetscInt));
1543:   }
1544:   for (i=0; i<A->rmap->n; i++) {
1545:     a->diag[i] = a->i[i+1];
1546:     for (j=a->i[i]; j<a->i[i+1]; j++) {
1547:       if (a->j[j] == i) {
1548:         a->diag[i] = j;
1549:         break;
1550:       }
1551:     }
1552:   }
1553:   return(0);
1554: }

1556: PetscErrorCode MatShift_SeqAIJ(Mat A,PetscScalar v)
1557: {
1558:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1559:   const PetscInt    *diag = (const PetscInt*)a->diag;
1560:   const PetscInt    *ii = (const PetscInt*) a->i;
1561:   PetscInt          i,*mdiag = NULL;
1562:   PetscErrorCode    ierr;
1563:   PetscInt          cnt = 0; /* how many diagonals are missing */

1566:   if (!A->preallocated || !a->nz) {
1567:     MatSeqAIJSetPreallocation(A,1,NULL);
1568:     MatShift_Basic(A,v);
1569:     return(0);
1570:   }

1572:   if (a->diagonaldense) {
1573:     cnt = 0;
1574:   } else {
1575:     PetscCalloc1(A->rmap->n,&mdiag);
1576:     for (i=0; i<A->rmap->n; i++) {
1577:       if (diag[i] >= ii[i+1]) {
1578:         cnt++;
1579:         mdiag[i] = 1;
1580:       }
1581:     }
1582:   }
1583:   if (!cnt) {
1584:     MatShift_Basic(A,v);
1585:   } else {
1586:     PetscScalar *olda = a->a;  /* preserve pointers to current matrix nonzeros structure and values */
1587:     PetscInt    *oldj = a->j, *oldi = a->i;
1588:     PetscBool   singlemalloc = a->singlemalloc,free_a = a->free_a,free_ij = a->free_ij;

1590:     a->a = NULL;
1591:     a->j = NULL;
1592:     a->i = NULL;
1593:     /* increase the values in imax for each row where a diagonal is being inserted then reallocate the matrix data structures */
1594:     for (i=0; i<A->rmap->n; i++) {
1595:       a->imax[i] += mdiag[i];
1596:       a->imax[i] = PetscMin(a->imax[i],A->cmap->n);
1597:     }
1598:     MatSeqAIJSetPreallocation_SeqAIJ(A,0,a->imax);

1600:     /* copy old values into new matrix data structure */
1601:     for (i=0; i<A->rmap->n; i++) {
1602:       MatSetValues(A,1,&i,a->imax[i] - mdiag[i],&oldj[oldi[i]],&olda[oldi[i]],ADD_VALUES);
1603:       if (i < A->cmap->n) {
1604:         MatSetValue(A,i,i,v,ADD_VALUES);
1605:       }
1606:     }
1607:     MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
1608:     MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
1609:     if (singlemalloc) {
1610:       PetscFree3(olda,oldj,oldi);
1611:     } else {
1612:       if (free_a)  {PetscFree(olda);}
1613:       if (free_ij) {PetscFree(oldj);}
1614:       if (free_ij) {PetscFree(oldi);}
1615:     }
1616:   }
1617:   PetscFree(mdiag);
1618:   a->diagonaldense = PETSC_TRUE;
1619:   return(0);
1620: }

1622: /*
1623:      Checks for missing diagonals
1624: */
1625: PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscBool  *missing,PetscInt *d)
1626: {
1627:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1628:   PetscInt       *diag,*ii = a->i,i;

1632:   *missing = PETSC_FALSE;
1633:   if (A->rmap->n > 0 && !ii) {
1634:     *missing = PETSC_TRUE;
1635:     if (d) *d = 0;
1636:     PetscInfo(A,"Matrix has no entries therefore is missing diagonal\n");
1637:   } else {
1638:     PetscInt n;
1639:     n = PetscMin(A->rmap->n, A->cmap->n);
1640:     diag = a->diag;
1641:     for (i=0; i<n; i++) {
1642:       if (diag[i] >= ii[i+1]) {
1643:         *missing = PETSC_TRUE;
1644:         if (d) *d = i;
1645:         PetscInfo1(A,"Matrix is missing diagonal number %D\n",i);
1646:         break;
1647:       }
1648:     }
1649:   }
1650:   return(0);
1651: }

1653:  #include <petscblaslapack.h>
1654:  #include <petsc/private/kernels/blockinvert.h>

1656: /*
1657:     Note that values is allocated externally by the PC and then passed into this routine
1658: */
1659: PetscErrorCode MatInvertVariableBlockDiagonal_SeqAIJ(Mat A,PetscInt nblocks,const PetscInt *bsizes,PetscScalar *diag)
1660: {
1661:   PetscErrorCode  ierr;
1662:   PetscInt        n = A->rmap->n, i, ncnt = 0, *indx,j,bsizemax = 0,*v_pivots;
1663:   PetscBool       allowzeropivot,zeropivotdetected=PETSC_FALSE;
1664:   const PetscReal shift = 0.0;
1665:   PetscInt        ipvt[5];
1666:   PetscScalar     work[25],*v_work;

1669:   allowzeropivot = PetscNot(A->erroriffailure);
1670:   for (i=0; i<nblocks; i++) ncnt += bsizes[i];
1671:   if (ncnt != n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Total blocksizes %D doesn't match number matrix rows %D",ncnt,n);
1672:   for (i=0; i<nblocks; i++) {
1673:     bsizemax = PetscMax(bsizemax,bsizes[i]);
1674:   }
1675:   PetscMalloc1(bsizemax,&indx);
1676:   if (bsizemax > 7) {
1677:     PetscMalloc2(bsizemax,&v_work,bsizemax,&v_pivots);
1678:   }
1679:   ncnt = 0;
1680:   for (i=0; i<nblocks; i++) {
1681:     for (j=0; j<bsizes[i]; j++) indx[j] = ncnt+j;
1682:     MatGetValues(A,bsizes[i],indx,bsizes[i],indx,diag);
1683:     switch (bsizes[i]) {
1684:     case 1:
1685:       *diag = 1.0/(*diag);
1686:       break;
1687:     case 2:
1688:       PetscKernel_A_gets_inverse_A_2(diag,shift,allowzeropivot,&zeropivotdetected);
1689:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1690:       PetscKernel_A_gets_transpose_A_2(diag);
1691:       break;
1692:     case 3:
1693:       PetscKernel_A_gets_inverse_A_3(diag,shift,allowzeropivot,&zeropivotdetected);
1694:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1695:       PetscKernel_A_gets_transpose_A_3(diag);
1696:       break;
1697:     case 4:
1698:       PetscKernel_A_gets_inverse_A_4(diag,shift,allowzeropivot,&zeropivotdetected);
1699:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1700:       PetscKernel_A_gets_transpose_A_4(diag);
1701:       break;
1702:     case 5:
1703:       PetscKernel_A_gets_inverse_A_5(diag,ipvt,work,shift,allowzeropivot,&zeropivotdetected);
1704:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1705:       PetscKernel_A_gets_transpose_A_5(diag);
1706:       break;
1707:     case 6:
1708:       PetscKernel_A_gets_inverse_A_6(diag,shift,allowzeropivot,&zeropivotdetected);
1709:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1710:       PetscKernel_A_gets_transpose_A_6(diag);
1711:       break;
1712:     case 7:
1713:       PetscKernel_A_gets_inverse_A_7(diag,shift,allowzeropivot,&zeropivotdetected);
1714:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1715:       PetscKernel_A_gets_transpose_A_7(diag);
1716:       break;
1717:     default:
1718:       PetscKernel_A_gets_inverse_A(bsizes[i],diag,v_pivots,v_work,allowzeropivot,&zeropivotdetected);
1719:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1720:       PetscKernel_A_gets_transpose_A_N(diag,bsizes[i]);
1721:     }
1722:     ncnt   += bsizes[i];
1723:     diag += bsizes[i]*bsizes[i];
1724:   }
1725:   if (bsizemax > 7) {
1726:     PetscFree2(v_work,v_pivots);
1727:   }
1728:   PetscFree(indx);
1729:   return(0);
1730: }

1732: /*
1733:    Negative shift indicates do not generate an error if there is a zero diagonal, just invert it anyways
1734: */
1735: PetscErrorCode  MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
1736: {
1737:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
1739:   PetscInt       i,*diag,m = A->rmap->n;
1740:   MatScalar      *v = a->a;
1741:   PetscScalar    *idiag,*mdiag;

1744:   if (a->idiagvalid) return(0);
1745:   MatMarkDiagonal_SeqAIJ(A);
1746:   diag = a->diag;
1747:   if (!a->idiag) {
1748:     PetscMalloc3(m,&a->idiag,m,&a->mdiag,m,&a->ssor_work);
1749:     PetscLogObjectMemory((PetscObject)A, 3*m*sizeof(PetscScalar));
1750:     v    = a->a;
1751:   }
1752:   mdiag = a->mdiag;
1753:   idiag = a->idiag;

1755:   if (omega == 1.0 && PetscRealPart(fshift) <= 0.0) {
1756:     for (i=0; i<m; i++) {
1757:       mdiag[i] = v[diag[i]];
1758:       if (!PetscAbsScalar(mdiag[i])) { /* zero diagonal */
1759:         if (PetscRealPart(fshift)) {
1760:           PetscInfo1(A,"Zero diagonal on row %D\n",i);
1761:           A->factorerrortype             = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1762:           A->factorerror_zeropivot_value = 0.0;
1763:           A->factorerror_zeropivot_row   = i;
1764:         } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
1765:       }
1766:       idiag[i] = 1.0/v[diag[i]];
1767:     }
1768:     PetscLogFlops(m);
1769:   } else {
1770:     for (i=0; i<m; i++) {
1771:       mdiag[i] = v[diag[i]];
1772:       idiag[i] = omega/(fshift + v[diag[i]]);
1773:     }
1774:     PetscLogFlops(2.0*m);
1775:   }
1776:   a->idiagvalid = PETSC_TRUE;
1777:   return(0);
1778: }

1780: #include <../src/mat/impls/aij/seq/ftn-kernels/frelax.h>
1781: PetscErrorCode MatSOR_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
1782: {
1783:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1784:   PetscScalar       *x,d,sum,*t,scale;
1785:   const MatScalar   *v,*idiag=0,*mdiag;
1786:   const PetscScalar *b, *bs,*xb, *ts;
1787:   PetscErrorCode    ierr;
1788:   PetscInt          n,m = A->rmap->n,i;
1789:   const PetscInt    *idx,*diag;

1792:   its = its*lits;

1794:   if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
1795:   if (!a->idiagvalid) {MatInvertDiagonal_SeqAIJ(A,omega,fshift);}
1796:   a->fshift = fshift;
1797:   a->omega  = omega;

1799:   diag  = a->diag;
1800:   t     = a->ssor_work;
1801:   idiag = a->idiag;
1802:   mdiag = a->mdiag;

1804:   VecGetArray(xx,&x);
1805:   VecGetArrayRead(bb,&b);
1806:   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
1807:   if (flag == SOR_APPLY_UPPER) {
1808:     /* apply (U + D/omega) to the vector */
1809:     bs = b;
1810:     for (i=0; i<m; i++) {
1811:       d   = fshift + mdiag[i];
1812:       n   = a->i[i+1] - diag[i] - 1;
1813:       idx = a->j + diag[i] + 1;
1814:       v   = a->a + diag[i] + 1;
1815:       sum = b[i]*d/omega;
1816:       PetscSparseDensePlusDot(sum,bs,v,idx,n);
1817:       x[i] = sum;
1818:     }
1819:     VecRestoreArray(xx,&x);
1820:     VecRestoreArrayRead(bb,&b);
1821:     PetscLogFlops(a->nz);
1822:     return(0);
1823:   }

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

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

1832:     to a vector efficiently using Eisenstat's trick.
1833:     */
1834:     scale = (2.0/omega) - 1.0;

1836:     /*  x = (E + U)^{-1} b */
1837:     for (i=m-1; i>=0; i--) {
1838:       n   = a->i[i+1] - diag[i] - 1;
1839:       idx = a->j + diag[i] + 1;
1840:       v   = a->a + diag[i] + 1;
1841:       sum = b[i];
1842:       PetscSparseDenseMinusDot(sum,x,v,idx,n);
1843:       x[i] = sum*idiag[i];
1844:     }

1846:     /*  t = b - (2*E - D)x */
1847:     v = a->a;
1848:     for (i=0; i<m; i++) t[i] = b[i] - scale*(v[*diag++])*x[i];

1850:     /*  t = (E + L)^{-1}t */
1851:     ts   = t;
1852:     diag = a->diag;
1853:     for (i=0; i<m; i++) {
1854:       n   = diag[i] - a->i[i];
1855:       idx = a->j + a->i[i];
1856:       v   = a->a + a->i[i];
1857:       sum = t[i];
1858:       PetscSparseDenseMinusDot(sum,ts,v,idx,n);
1859:       t[i] = sum*idiag[i];
1860:       /*  x = x + t */
1861:       x[i] += t[i];
1862:     }

1864:     PetscLogFlops(6.0*m-1 + 2.0*a->nz);
1865:     VecRestoreArray(xx,&x);
1866:     VecRestoreArrayRead(bb,&b);
1867:     return(0);
1868:   }
1869:   if (flag & SOR_ZERO_INITIAL_GUESS) {
1870:     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
1871:       for (i=0; i<m; i++) {
1872:         n   = diag[i] - a->i[i];
1873:         idx = a->j + a->i[i];
1874:         v   = a->a + a->i[i];
1875:         sum = b[i];
1876:         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1877:         t[i] = sum;
1878:         x[i] = sum*idiag[i];
1879:       }
1880:       xb   = t;
1881:       PetscLogFlops(a->nz);
1882:     } else xb = b;
1883:     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
1884:       for (i=m-1; i>=0; i--) {
1885:         n   = a->i[i+1] - diag[i] - 1;
1886:         idx = a->j + diag[i] + 1;
1887:         v   = a->a + diag[i] + 1;
1888:         sum = xb[i];
1889:         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1890:         if (xb == b) {
1891:           x[i] = sum*idiag[i];
1892:         } else {
1893:           x[i] = (1-omega)*x[i] + sum*idiag[i];  /* omega in idiag */
1894:         }
1895:       }
1896:       PetscLogFlops(a->nz); /* assumes 1/2 in upper */
1897:     }
1898:     its--;
1899:   }
1900:   while (its--) {
1901:     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
1902:       for (i=0; i<m; i++) {
1903:         /* lower */
1904:         n   = diag[i] - a->i[i];
1905:         idx = a->j + a->i[i];
1906:         v   = a->a + a->i[i];
1907:         sum = b[i];
1908:         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1909:         t[i] = sum;             /* save application of the lower-triangular part */
1910:         /* upper */
1911:         n   = a->i[i+1] - diag[i] - 1;
1912:         idx = a->j + diag[i] + 1;
1913:         v   = a->a + diag[i] + 1;
1914:         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1915:         x[i] = (1. - omega)*x[i] + sum*idiag[i]; /* omega in idiag */
1916:       }
1917:       xb   = t;
1918:       PetscLogFlops(2.0*a->nz);
1919:     } else xb = b;
1920:     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
1921:       for (i=m-1; i>=0; i--) {
1922:         sum = xb[i];
1923:         if (xb == b) {
1924:           /* whole matrix (no checkpointing available) */
1925:           n   = a->i[i+1] - a->i[i];
1926:           idx = a->j + a->i[i];
1927:           v   = a->a + a->i[i];
1928:           PetscSparseDenseMinusDot(sum,x,v,idx,n);
1929:           x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
1930:         } else { /* lower-triangular part has been saved, so only apply upper-triangular */
1931:           n   = a->i[i+1] - diag[i] - 1;
1932:           idx = a->j + diag[i] + 1;
1933:           v   = a->a + diag[i] + 1;
1934:           PetscSparseDenseMinusDot(sum,x,v,idx,n);
1935:           x[i] = (1. - omega)*x[i] + sum*idiag[i];  /* omega in idiag */
1936:         }
1937:       }
1938:       if (xb == b) {
1939:         PetscLogFlops(2.0*a->nz);
1940:       } else {
1941:         PetscLogFlops(a->nz); /* assumes 1/2 in upper */
1942:       }
1943:     }
1944:   }
1945:   VecRestoreArray(xx,&x);
1946:   VecRestoreArrayRead(bb,&b);
1947:   return(0);
1948: }


1951: PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
1952: {
1953:   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;

1956:   info->block_size   = 1.0;
1957:   info->nz_allocated = (double)a->maxnz;
1958:   info->nz_used      = (double)a->nz;
1959:   info->nz_unneeded  = (double)(a->maxnz - a->nz);
1960:   info->assemblies   = (double)A->num_ass;
1961:   info->mallocs      = (double)A->info.mallocs;
1962:   info->memory       = ((PetscObject)A)->mem;
1963:   if (A->factortype) {
1964:     info->fill_ratio_given  = A->info.fill_ratio_given;
1965:     info->fill_ratio_needed = A->info.fill_ratio_needed;
1966:     info->factor_mallocs    = A->info.factor_mallocs;
1967:   } else {
1968:     info->fill_ratio_given  = 0;
1969:     info->fill_ratio_needed = 0;
1970:     info->factor_mallocs    = 0;
1971:   }
1972:   return(0);
1973: }

1975: PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
1976: {
1977:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1978:   PetscInt          i,m = A->rmap->n - 1;
1979:   PetscErrorCode    ierr;
1980:   const PetscScalar *xx;
1981:   PetscScalar       *bb;
1982:   PetscInt          d = 0;

1985:   if (x && b) {
1986:     VecGetArrayRead(x,&xx);
1987:     VecGetArray(b,&bb);
1988:     for (i=0; i<N; i++) {
1989:       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1990:       if (rows[i] >= A->cmap->n) continue;
1991:       bb[rows[i]] = diag*xx[rows[i]];
1992:     }
1993:     VecRestoreArrayRead(x,&xx);
1994:     VecRestoreArray(b,&bb);
1995:   }

1997:   if (a->keepnonzeropattern) {
1998:     for (i=0; i<N; i++) {
1999:       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
2000:       PetscArrayzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]);
2001:     }
2002:     if (diag != 0.0) {
2003:       for (i=0; i<N; i++) {
2004:         d = rows[i];
2005:         if (rows[i] >= A->cmap->n) continue;
2006:         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);
2007:       }
2008:       for (i=0; i<N; i++) {
2009:         if (rows[i] >= A->cmap->n) continue;
2010:         a->a[a->diag[rows[i]]] = diag;
2011:       }
2012:     }
2013:   } else {
2014:     if (diag != 0.0) {
2015:       for (i=0; i<N; i++) {
2016:         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
2017:         if (a->ilen[rows[i]] > 0) {
2018:           if (rows[i] >= A->cmap->n) {
2019:             a->ilen[rows[i]] = 0;
2020:           } else {
2021:             a->ilen[rows[i]]    = 1;
2022:             a->a[a->i[rows[i]]] = diag;
2023:             a->j[a->i[rows[i]]] = rows[i];
2024:           }
2025:         } else if (rows[i] < A->cmap->n) { /* in case row was completely empty */
2026:           MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);
2027:         }
2028:       }
2029:     } else {
2030:       for (i=0; i<N; i++) {
2031:         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
2032:         a->ilen[rows[i]] = 0;
2033:       }
2034:     }
2035:     A->nonzerostate++;
2036:   }
2037:   (*A->ops->assemblyend)(A,MAT_FINAL_ASSEMBLY);
2038:   return(0);
2039: }

2041: PetscErrorCode MatZeroRowsColumns_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
2042: {
2043:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
2044:   PetscInt          i,j,m = A->rmap->n - 1,d = 0;
2045:   PetscErrorCode    ierr;
2046:   PetscBool         missing,*zeroed,vecs = PETSC_FALSE;
2047:   const PetscScalar *xx;
2048:   PetscScalar       *bb;

2051:   if (x && b) {
2052:     VecGetArrayRead(x,&xx);
2053:     VecGetArray(b,&bb);
2054:     vecs = PETSC_TRUE;
2055:   }
2056:   PetscCalloc1(A->rmap->n,&zeroed);
2057:   for (i=0; i<N; i++) {
2058:     if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
2059:     PetscArrayzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]);

2061:     zeroed[rows[i]] = PETSC_TRUE;
2062:   }
2063:   for (i=0; i<A->rmap->n; i++) {
2064:     if (!zeroed[i]) {
2065:       for (j=a->i[i]; j<a->i[i+1]; j++) {
2066:         if (a->j[j] < A->rmap->n && zeroed[a->j[j]]) {
2067:           if (vecs) bb[i] -= a->a[j]*xx[a->j[j]];
2068:           a->a[j] = 0.0;
2069:         }
2070:       }
2071:     } else if (vecs && i < A->cmap->N) bb[i] = diag*xx[i];
2072:   }
2073:   if (x && b) {
2074:     VecRestoreArrayRead(x,&xx);
2075:     VecRestoreArray(b,&bb);
2076:   }
2077:   PetscFree(zeroed);
2078:   if (diag != 0.0) {
2079:     MatMissingDiagonal_SeqAIJ(A,&missing,&d);
2080:     if (missing) {
2081:       for (i=0; i<N; i++) {
2082:         if (rows[i] >= A->cmap->N) continue;
2083:         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]);
2084:         MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);
2085:       }
2086:     } else {
2087:       for (i=0; i<N; i++) {
2088:         a->a[a->diag[rows[i]]] = diag;
2089:       }
2090:     }
2091:   }
2092:   (*A->ops->assemblyend)(A,MAT_FINAL_ASSEMBLY);
2093:   return(0);
2094: }

2096: PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
2097: {
2098:   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2099:   PetscInt   *itmp;

2102:   if (row < 0 || row >= A->rmap->n) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D out of range",row);

2104:   *nz = a->i[row+1] - a->i[row];
2105:   if (v) *v = a->a + a->i[row];
2106:   if (idx) {
2107:     itmp = a->j + a->i[row];
2108:     if (*nz) *idx = itmp;
2109:     else *idx = 0;
2110:   }
2111:   return(0);
2112: }

2114: /* remove this function? */
2115: PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
2116: {
2118:   return(0);
2119: }

2121: PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
2122: {
2123:   Mat_SeqAIJ     *a  = (Mat_SeqAIJ*)A->data;
2124:   MatScalar      *v  = a->a;
2125:   PetscReal      sum = 0.0;
2127:   PetscInt       i,j;

2130:   if (type == NORM_FROBENIUS) {
2131: #if defined(PETSC_USE_REAL___FP16)
2132:     PetscBLASInt one = 1,nz = a->nz;
2133:     *nrm = BLASnrm2_(&nz,v,&one);
2134: #else
2135:     for (i=0; i<a->nz; i++) {
2136:       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
2137:     }
2138:     *nrm = PetscSqrtReal(sum);
2139: #endif
2140:     PetscLogFlops(2*a->nz);
2141:   } else if (type == NORM_1) {
2142:     PetscReal *tmp;
2143:     PetscInt  *jj = a->j;
2144:     PetscCalloc1(A->cmap->n+1,&tmp);
2145:     *nrm = 0.0;
2146:     for (j=0; j<a->nz; j++) {
2147:       tmp[*jj++] += PetscAbsScalar(*v);  v++;
2148:     }
2149:     for (j=0; j<A->cmap->n; j++) {
2150:       if (tmp[j] > *nrm) *nrm = tmp[j];
2151:     }
2152:     PetscFree(tmp);
2153:     PetscLogFlops(PetscMax(a->nz-1,0));
2154:   } else if (type == NORM_INFINITY) {
2155:     *nrm = 0.0;
2156:     for (j=0; j<A->rmap->n; j++) {
2157:       v   = a->a + a->i[j];
2158:       sum = 0.0;
2159:       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
2160:         sum += PetscAbsScalar(*v); v++;
2161:       }
2162:       if (sum > *nrm) *nrm = sum;
2163:     }
2164:     PetscLogFlops(PetscMax(a->nz-1,0));
2165:   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for two norm");
2166:   return(0);
2167: }

2169: /* Merged from MatGetSymbolicTranspose_SeqAIJ() - replace MatGetSymbolicTranspose_SeqAIJ()? */
2170: PetscErrorCode MatTransposeSymbolic_SeqAIJ(Mat A,Mat *B)
2171: {
2173:   PetscInt       i,j,anzj;
2174:   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data,*b;
2175:   PetscInt       an=A->cmap->N,am=A->rmap->N;
2176:   PetscInt       *ati,*atj,*atfill,*ai=a->i,*aj=a->j;

2179:   /* Allocate space for symbolic transpose info and work array */
2180:   PetscCalloc1(an+1,&ati);
2181:   PetscMalloc1(ai[am],&atj);
2182:   PetscMalloc1(an,&atfill);

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

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

2193:   /* Walk through A row-wise and mark nonzero entries of A^T. */
2194:   for (i=0;i<am;i++) {
2195:     anzj = ai[i+1] - ai[i];
2196:     for (j=0;j<anzj;j++) {
2197:       atj[atfill[*aj]] = i;
2198:       atfill[*aj++]   += 1;
2199:     }
2200:   }

2202:   /* Clean up temporary space and complete requests. */
2203:   PetscFree(atfill);
2204:   MatCreateSeqAIJWithArrays(PetscObjectComm((PetscObject)A),an,am,ati,atj,NULL,B);
2205:   MatSetBlockSizes(*B,PetscAbs(A->cmap->bs),PetscAbs(A->rmap->bs));

2207:   b          = (Mat_SeqAIJ*)((*B)->data);
2208:   b->free_a  = PETSC_FALSE;
2209:   b->free_ij = PETSC_TRUE;
2210:   b->nonew   = 0;
2211:   return(0);
2212: }

2214: PetscErrorCode  MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
2215: {
2216:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) B->data;
2217:   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
2218:   MatScalar      *va,*vb;
2220:   PetscInt       ma,na,mb,nb, i;

2223:   MatGetSize(A,&ma,&na);
2224:   MatGetSize(B,&mb,&nb);
2225:   if (ma!=nb || na!=mb) {
2226:     *f = PETSC_FALSE;
2227:     return(0);
2228:   }
2229:   aii  = aij->i; bii = bij->i;
2230:   adx  = aij->j; bdx = bij->j;
2231:   va   = aij->a; vb = bij->a;
2232:   PetscMalloc1(ma,&aptr);
2233:   PetscMalloc1(mb,&bptr);
2234:   for (i=0; i<ma; i++) aptr[i] = aii[i];
2235:   for (i=0; i<mb; i++) bptr[i] = bii[i];

2237:   *f = PETSC_TRUE;
2238:   for (i=0; i<ma; i++) {
2239:     while (aptr[i]<aii[i+1]) {
2240:       PetscInt    idc,idr;
2241:       PetscScalar vc,vr;
2242:       /* column/row index/value */
2243:       idc = adx[aptr[i]];
2244:       idr = bdx[bptr[idc]];
2245:       vc  = va[aptr[i]];
2246:       vr  = vb[bptr[idc]];
2247:       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
2248:         *f = PETSC_FALSE;
2249:         goto done;
2250:       } else {
2251:         aptr[i]++;
2252:         if (B || i!=idc) bptr[idc]++;
2253:       }
2254:     }
2255:   }
2256: done:
2257:   PetscFree(aptr);
2258:   PetscFree(bptr);
2259:   return(0);
2260: }

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

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

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

2310: PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
2311: {

2315:   MatIsTranspose_SeqAIJ(A,A,tol,f);
2316:   return(0);
2317: }

2319: PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
2320: {

2324:   MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);
2325:   return(0);
2326: }

2328: PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
2329: {
2330:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
2331:   const PetscScalar *l,*r;
2332:   PetscScalar       x;
2333:   MatScalar         *v;
2334:   PetscErrorCode    ierr;
2335:   PetscInt          i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz;
2336:   const PetscInt    *jj;

2339:   if (ll) {
2340:     /* The local size is used so that VecMPI can be passed to this routine
2341:        by MatDiagonalScale_MPIAIJ */
2342:     VecGetLocalSize(ll,&m);
2343:     if (m != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
2344:     VecGetArrayRead(ll,&l);
2345:     v    = a->a;
2346:     for (i=0; i<m; i++) {
2347:       x = l[i];
2348:       M = a->i[i+1] - a->i[i];
2349:       for (j=0; j<M; j++) (*v++) *= x;
2350:     }
2351:     VecRestoreArrayRead(ll,&l);
2352:     PetscLogFlops(nz);
2353:   }
2354:   if (rr) {
2355:     VecGetLocalSize(rr,&n);
2356:     if (n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
2357:     VecGetArrayRead(rr,&r);
2358:     v    = a->a; jj = a->j;
2359:     for (i=0; i<nz; i++) (*v++) *= r[*jj++];
2360:     VecRestoreArrayRead(rr,&r);
2361:     PetscLogFlops(nz);
2362:   }
2363:   MatSeqAIJInvalidateDiagonal(A);
2364:   return(0);
2365: }

2367: PetscErrorCode MatCreateSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
2368: {
2369:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
2371:   PetscInt       *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens;
2372:   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
2373:   const PetscInt *irow,*icol;
2374:   PetscInt       nrows,ncols;
2375:   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
2376:   MatScalar      *a_new,*mat_a;
2377:   Mat            C;
2378:   PetscBool      stride;


2382:   ISGetIndices(isrow,&irow);
2383:   ISGetLocalSize(isrow,&nrows);
2384:   ISGetLocalSize(iscol,&ncols);

2386:   PetscObjectTypeCompare((PetscObject)iscol,ISSTRIDE,&stride);
2387:   if (stride) {
2388:     ISStrideGetInfo(iscol,&first,&step);
2389:   } else {
2390:     first = 0;
2391:     step  = 0;
2392:   }
2393:   if (stride && step == 1) {
2394:     /* special case of contiguous rows */
2395:     PetscMalloc2(nrows,&lens,nrows,&starts);
2396:     /* loop over new rows determining lens and starting points */
2397:     for (i=0; i<nrows; i++) {
2398:       kstart = ai[irow[i]];
2399:       kend   = kstart + ailen[irow[i]];
2400:       starts[i] = kstart;
2401:       for (k=kstart; k<kend; k++) {
2402:         if (aj[k] >= first) {
2403:           starts[i] = k;
2404:           break;
2405:         }
2406:       }
2407:       sum = 0;
2408:       while (k < kend) {
2409:         if (aj[k++] >= first+ncols) break;
2410:         sum++;
2411:       }
2412:       lens[i] = sum;
2413:     }
2414:     /* create submatrix */
2415:     if (scall == MAT_REUSE_MATRIX) {
2416:       PetscInt n_cols,n_rows;
2417:       MatGetSize(*B,&n_rows,&n_cols);
2418:       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
2419:       MatZeroEntries(*B);
2420:       C    = *B;
2421:     } else {
2422:       PetscInt rbs,cbs;
2423:       MatCreate(PetscObjectComm((PetscObject)A),&C);
2424:       MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);
2425:       ISGetBlockSize(isrow,&rbs);
2426:       ISGetBlockSize(iscol,&cbs);
2427:       MatSetBlockSizes(C,rbs,cbs);
2428:       MatSetType(C,((PetscObject)A)->type_name);
2429:       MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);
2430:     }
2431:     c = (Mat_SeqAIJ*)C->data;

2433:     /* loop over rows inserting into submatrix */
2434:     a_new = c->a;
2435:     j_new = c->j;
2436:     i_new = c->i;

2438:     for (i=0; i<nrows; i++) {
2439:       ii    = starts[i];
2440:       lensi = lens[i];
2441:       for (k=0; k<lensi; k++) {
2442:         *j_new++ = aj[ii+k] - first;
2443:       }
2444:       PetscArraycpy(a_new,a->a + starts[i],lensi);
2445:       a_new     += lensi;
2446:       i_new[i+1] = i_new[i] + lensi;
2447:       c->ilen[i] = lensi;
2448:     }
2449:     PetscFree2(lens,starts);
2450:   } else {
2451:     ISGetIndices(iscol,&icol);
2452:     PetscCalloc1(oldcols,&smap);
2453:     PetscMalloc1(1+nrows,&lens);
2454:     for (i=0; i<ncols; i++) {
2455: #if defined(PETSC_USE_DEBUG)
2456:       if (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);
2457: #endif
2458:       smap[icol[i]] = i+1;
2459:     }

2461:     /* determine lens of each row */
2462:     for (i=0; i<nrows; i++) {
2463:       kstart  = ai[irow[i]];
2464:       kend    = kstart + a->ilen[irow[i]];
2465:       lens[i] = 0;
2466:       for (k=kstart; k<kend; k++) {
2467:         if (smap[aj[k]]) {
2468:           lens[i]++;
2469:         }
2470:       }
2471:     }
2472:     /* Create and fill new matrix */
2473:     if (scall == MAT_REUSE_MATRIX) {
2474:       PetscBool equal;

2476:       c = (Mat_SeqAIJ*)((*B)->data);
2477:       if ((*B)->rmap->n  != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
2478:       PetscArraycmp(c->ilen,lens,(*B)->rmap->n,&equal);
2479:       if (!equal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
2480:       PetscArrayzero(c->ilen,(*B)->rmap->n);
2481:       C    = *B;
2482:     } else {
2483:       PetscInt rbs,cbs;
2484:       MatCreate(PetscObjectComm((PetscObject)A),&C);
2485:       MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);
2486:       ISGetBlockSize(isrow,&rbs);
2487:       ISGetBlockSize(iscol,&cbs);
2488:       MatSetBlockSizes(C,rbs,cbs);
2489:       MatSetType(C,((PetscObject)A)->type_name);
2490:       MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);
2491:     }
2492:     c = (Mat_SeqAIJ*)(C->data);
2493:     for (i=0; i<nrows; i++) {
2494:       row      = irow[i];
2495:       kstart   = ai[row];
2496:       kend     = kstart + a->ilen[row];
2497:       mat_i    = c->i[i];
2498:       mat_j    = c->j + mat_i;
2499:       mat_a    = c->a + mat_i;
2500:       mat_ilen = c->ilen + i;
2501:       for (k=kstart; k<kend; k++) {
2502:         if ((tcol=smap[a->j[k]])) {
2503:           *mat_j++ = tcol - 1;
2504:           *mat_a++ = a->a[k];
2505:           (*mat_ilen)++;

2507:         }
2508:       }
2509:     }
2510:     /* Free work space */
2511:     ISRestoreIndices(iscol,&icol);
2512:     PetscFree(smap);
2513:     PetscFree(lens);
2514:     /* sort */
2515:     for (i = 0; i < nrows; i++) {
2516:       PetscInt ilen;

2518:       mat_i = c->i[i];
2519:       mat_j = c->j + mat_i;
2520:       mat_a = c->a + mat_i;
2521:       ilen  = c->ilen[i];
2522:       PetscSortIntWithScalarArray(ilen,mat_j,mat_a);
2523:     }
2524:   }
2525:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
2526:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);

2528:   ISRestoreIndices(isrow,&irow);
2529:   *B   = C;
2530:   return(0);
2531: }

2533: PetscErrorCode  MatGetMultiProcBlock_SeqAIJ(Mat mat,MPI_Comm subComm,MatReuse scall,Mat *subMat)
2534: {
2536:   Mat            B;

2539:   if (scall == MAT_INITIAL_MATRIX) {
2540:     MatCreate(subComm,&B);
2541:     MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->n,mat->cmap->n);
2542:     MatSetBlockSizesFromMats(B,mat,mat);
2543:     MatSetType(B,MATSEQAIJ);
2544:     MatDuplicateNoCreate_SeqAIJ(B,mat,MAT_COPY_VALUES,PETSC_TRUE);
2545:     *subMat = B;
2546:   } else {
2547:     MatCopy_SeqAIJ(mat,*subMat,SAME_NONZERO_PATTERN);
2548:   }
2549:   return(0);
2550: }

2552: PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
2553: {
2554:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
2556:   Mat            outA;
2557:   PetscBool      row_identity,col_identity;

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

2562:   ISIdentity(row,&row_identity);
2563:   ISIdentity(col,&col_identity);

2565:   outA             = inA;
2566:   outA->factortype = MAT_FACTOR_LU;
2567:   PetscFree(inA->solvertype);
2568:   PetscStrallocpy(MATSOLVERPETSC,&inA->solvertype);

2570:   PetscObjectReference((PetscObject)row);
2571:   ISDestroy(&a->row);

2573:   a->row = row;

2575:   PetscObjectReference((PetscObject)col);
2576:   ISDestroy(&a->col);

2578:   a->col = col;

2580:   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
2581:   ISDestroy(&a->icol);
2582:   ISInvertPermutation(col,PETSC_DECIDE,&a->icol);
2583:   PetscLogObjectParent((PetscObject)inA,(PetscObject)a->icol);

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

2590:   MatMarkDiagonal_SeqAIJ(inA);
2591:   if (row_identity && col_identity) {
2592:     MatLUFactorNumeric_SeqAIJ_inplace(outA,inA,info);
2593:   } else {
2594:     MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);
2595:   }
2596:   return(0);
2597: }

2599: PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
2600: {
2601:   Mat_SeqAIJ     *a     = (Mat_SeqAIJ*)inA->data;
2602:   PetscScalar    oalpha = alpha;
2604:   PetscBLASInt   one = 1,bnz;

2607:   PetscBLASIntCast(a->nz,&bnz);
2608:   PetscStackCallBLAS("BLASscal",BLASscal_(&bnz,&oalpha,a->a,&one));
2609:   PetscLogFlops(a->nz);
2610:   MatSeqAIJInvalidateDiagonal(inA);
2611:   return(0);
2612: }

2614: PetscErrorCode MatDestroySubMatrix_Private(Mat_SubSppt *submatj)
2615: {
2617:   PetscInt       i;

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

2623:     for (i=0; i<submatj->nrqr; ++i) {
2624:       PetscFree(submatj->sbuf2[i]);
2625:     }
2626:     PetscFree3(submatj->sbuf2,submatj->req_size,submatj->req_source1);

2628:     if (submatj->rbuf1) {
2629:       PetscFree(submatj->rbuf1[0]);
2630:       PetscFree(submatj->rbuf1);
2631:     }

2633:     for (i=0; i<submatj->nrqs; ++i) {
2634:       PetscFree(submatj->rbuf3[i]);
2635:     }
2636:     PetscFree3(submatj->req_source2,submatj->rbuf2,submatj->rbuf3);
2637:     PetscFree(submatj->pa);
2638:   }

2640: #if defined(PETSC_USE_CTABLE)
2641:   PetscTableDestroy((PetscTable*)&submatj->rmap);
2642:   if (submatj->cmap_loc) {PetscFree(submatj->cmap_loc);}
2643:   PetscFree(submatj->rmap_loc);
2644: #else
2645:   PetscFree(submatj->rmap);
2646: #endif

2648:   if (!submatj->allcolumns) {
2649: #if defined(PETSC_USE_CTABLE)
2650:     PetscTableDestroy((PetscTable*)&submatj->cmap);
2651: #else
2652:     PetscFree(submatj->cmap);
2653: #endif
2654:   }
2655:   PetscFree(submatj->row2proc);

2657:   PetscFree(submatj);
2658:   return(0);
2659: }

2661: PetscErrorCode MatDestroySubMatrix_SeqAIJ(Mat C)
2662: {
2664:   Mat_SeqAIJ     *c = (Mat_SeqAIJ*)C->data;
2665:   Mat_SubSppt    *submatj = c->submatis1;

2668:   (*submatj->destroy)(C);
2669:   MatDestroySubMatrix_Private(submatj);
2670:   return(0);
2671: }

2673: PetscErrorCode MatDestroySubMatrices_SeqAIJ(PetscInt n,Mat *mat[])
2674: {
2676:   PetscInt       i;
2677:   Mat            C;
2678:   Mat_SeqAIJ     *c;
2679:   Mat_SubSppt    *submatj;

2682:   for (i=0; i<n; i++) {
2683:     C       = (*mat)[i];
2684:     c       = (Mat_SeqAIJ*)C->data;
2685:     submatj = c->submatis1;
2686:     if (submatj) {
2687:       if (--((PetscObject)C)->refct <= 0) {
2688:         (*submatj->destroy)(C);
2689:         MatDestroySubMatrix_Private(submatj);
2690:         PetscFree(C->defaultvectype);
2691:         PetscLayoutDestroy(&C->rmap);
2692:         PetscLayoutDestroy(&C->cmap);
2693:         PetscHeaderDestroy(&C);
2694:       }
2695:     } else {
2696:       MatDestroy(&C);
2697:     }
2698:   }

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

2703:   PetscFree(*mat);
2704:   return(0);
2705: }

2707: PetscErrorCode MatCreateSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
2708: {
2710:   PetscInt       i;

2713:   if (scall == MAT_INITIAL_MATRIX) {
2714:     PetscCalloc1(n+1,B);
2715:   }

2717:   for (i=0; i<n; i++) {
2718:     MatCreateSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);
2719:   }
2720:   return(0);
2721: }

2723: PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
2724: {
2725:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2727:   PetscInt       row,i,j,k,l,m,n,*nidx,isz,val;
2728:   const PetscInt *idx;
2729:   PetscInt       start,end,*ai,*aj;
2730:   PetscBT        table;

2733:   m  = A->rmap->n;
2734:   ai = a->i;
2735:   aj = a->j;

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

2739:   PetscMalloc1(m+1,&nidx);
2740:   PetscBTCreate(m,&table);

2742:   for (i=0; i<is_max; i++) {
2743:     /* Initialize the two local arrays */
2744:     isz  = 0;
2745:     PetscBTMemzero(m,table);

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

2751:     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
2752:     for (j=0; j<n; ++j) {
2753:       if (!PetscBTLookupSet(table,idx[j])) nidx[isz++] = idx[j];
2754:     }
2755:     ISRestoreIndices(is[i],&idx);
2756:     ISDestroy(&is[i]);

2758:     k = 0;
2759:     for (j=0; j<ov; j++) { /* for each overlap */
2760:       n = isz;
2761:       for (; k<n; k++) { /* do only those rows in nidx[k], which are not done yet */
2762:         row   = nidx[k];
2763:         start = ai[row];
2764:         end   = ai[row+1];
2765:         for (l = start; l<end; l++) {
2766:           val = aj[l];
2767:           if (!PetscBTLookupSet(table,val)) nidx[isz++] = val;
2768:         }
2769:       }
2770:     }
2771:     ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,PETSC_COPY_VALUES,(is+i));
2772:   }
2773:   PetscBTDestroy(&table);
2774:   PetscFree(nidx);
2775:   return(0);
2776: }

2778: /* -------------------------------------------------------------- */
2779: PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
2780: {
2781:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2783:   PetscInt       i,nz = 0,m = A->rmap->n,n = A->cmap->n;
2784:   const PetscInt *row,*col;
2785:   PetscInt       *cnew,j,*lens;
2786:   IS             icolp,irowp;
2787:   PetscInt       *cwork = NULL;
2788:   PetscScalar    *vwork = NULL;

2791:   ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);
2792:   ISGetIndices(irowp,&row);
2793:   ISInvertPermutation(colp,PETSC_DECIDE,&icolp);
2794:   ISGetIndices(icolp,&col);

2796:   /* determine lengths of permuted rows */
2797:   PetscMalloc1(m+1,&lens);
2798:   for (i=0; i<m; i++) lens[row[i]] = a->i[i+1] - a->i[i];
2799:   MatCreate(PetscObjectComm((PetscObject)A),B);
2800:   MatSetSizes(*B,m,n,m,n);
2801:   MatSetBlockSizesFromMats(*B,A,A);
2802:   MatSetType(*B,((PetscObject)A)->type_name);
2803:   MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);
2804:   PetscFree(lens);

2806:   PetscMalloc1(n,&cnew);
2807:   for (i=0; i<m; i++) {
2808:     MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);
2809:     for (j=0; j<nz; j++) cnew[j] = col[cwork[j]];
2810:     MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);
2811:     MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);
2812:   }
2813:   PetscFree(cnew);

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

2817:   MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);
2818:   MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);
2819:   ISRestoreIndices(irowp,&row);
2820:   ISRestoreIndices(icolp,&col);
2821:   ISDestroy(&irowp);
2822:   ISDestroy(&icolp);
2823:   return(0);
2824: }

2826: PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
2827: {

2831:   /* If the two matrices have the same copy implementation, use fast copy. */
2832:   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
2833:     Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2834:     Mat_SeqAIJ *b = (Mat_SeqAIJ*)B->data;

2836:     if (a->i[A->rmap->n] != b->i[B->rmap->n]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Number of nonzeros in two matrices are different");
2837:     PetscArraycpy(b->a,a->a,a->i[A->rmap->n]);
2838:     PetscObjectStateIncrease((PetscObject)B);
2839:   } else {
2840:     MatCopy_Basic(A,B,str);
2841:   }
2842:   return(0);
2843: }

2845: PetscErrorCode MatSetUp_SeqAIJ(Mat A)
2846: {

2850:    MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);
2851:   return(0);
2852: }

2854: PetscErrorCode MatSeqAIJGetArray_SeqAIJ(Mat A,PetscScalar *array[])
2855: {
2856:   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;

2859:   *array = a->a;
2860:   return(0);
2861: }

2863: PetscErrorCode MatSeqAIJRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
2864: {
2866:   return(0);
2867: }

2869: /*
2870:    Computes the number of nonzeros per row needed for preallocation when X and Y
2871:    have different nonzero structure.
2872: */
2873: PetscErrorCode MatAXPYGetPreallocation_SeqX_private(PetscInt m,const PetscInt *xi,const PetscInt *xj,const PetscInt *yi,const PetscInt *yj,PetscInt *nnz)
2874: {
2875:   PetscInt       i,j,k,nzx,nzy;

2878:   /* Set the number of nonzeros in the new matrix */
2879:   for (i=0; i<m; i++) {
2880:     const PetscInt *xjj = xj+xi[i],*yjj = yj+yi[i];
2881:     nzx = xi[i+1] - xi[i];
2882:     nzy = yi[i+1] - yi[i];
2883:     nnz[i] = 0;
2884:     for (j=0,k=0; j<nzx; j++) {                   /* Point in X */
2885:       for (; k<nzy && yjj[k]<xjj[j]; k++) nnz[i]++; /* Catch up to X */
2886:       if (k<nzy && yjj[k]==xjj[j]) k++;             /* Skip duplicate */
2887:       nnz[i]++;
2888:     }
2889:     for (; k<nzy; k++) nnz[i]++;
2890:   }
2891:   return(0);
2892: }

2894: PetscErrorCode MatAXPYGetPreallocation_SeqAIJ(Mat Y,Mat X,PetscInt *nnz)
2895: {
2896:   PetscInt       m = Y->rmap->N;
2897:   Mat_SeqAIJ     *x = (Mat_SeqAIJ*)X->data;
2898:   Mat_SeqAIJ     *y = (Mat_SeqAIJ*)Y->data;

2902:   /* Set the number of nonzeros in the new matrix */
2903:   MatAXPYGetPreallocation_SeqX_private(m,x->i,x->j,y->i,y->j,nnz);
2904:   return(0);
2905: }

2907: PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2908: {
2910:   Mat_SeqAIJ     *x = (Mat_SeqAIJ*)X->data,*y = (Mat_SeqAIJ*)Y->data;
2911:   PetscBLASInt   one=1,bnz;

2914:   PetscBLASIntCast(x->nz,&bnz);
2915:   if (str == SAME_NONZERO_PATTERN) {
2916:     PetscScalar alpha = a;
2917:     PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
2918:     MatSeqAIJInvalidateDiagonal(Y);
2919:     PetscObjectStateIncrease((PetscObject)Y);
2920:   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2921:     MatAXPY_Basic(Y,a,X,str);
2922:   } else {
2923:     Mat      B;
2924:     PetscInt *nnz;
2925:     PetscMalloc1(Y->rmap->N,&nnz);
2926:     MatCreate(PetscObjectComm((PetscObject)Y),&B);
2927:     PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);
2928:     MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);
2929:     MatSetBlockSizesFromMats(B,Y,Y);
2930:     MatSetType(B,(MatType) ((PetscObject)Y)->type_name);
2931:     MatAXPYGetPreallocation_SeqAIJ(Y,X,nnz);
2932:     MatSeqAIJSetPreallocation(B,0,nnz);
2933:     MatAXPY_BasicWithPreallocation(B,Y,a,X,str);
2934:     MatHeaderReplace(Y,&B);
2935:     PetscFree(nnz);
2936:   }
2937:   return(0);
2938: }

2940: PetscErrorCode  MatConjugate_SeqAIJ(Mat mat)
2941: {
2942: #if defined(PETSC_USE_COMPLEX)
2943:   Mat_SeqAIJ  *aij = (Mat_SeqAIJ*)mat->data;
2944:   PetscInt    i,nz;
2945:   PetscScalar *a;

2948:   nz = aij->nz;
2949:   a  = aij->a;
2950:   for (i=0; i<nz; i++) a[i] = PetscConj(a[i]);
2951: #else
2953: #endif
2954:   return(0);
2955: }

2957: PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2958: {
2959:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2961:   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2962:   PetscReal      atmp;
2963:   PetscScalar    *x;
2964:   MatScalar      *aa;

2967:   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2968:   aa = a->a;
2969:   ai = a->i;
2970:   aj = a->j;

2972:   VecSet(v,0.0);
2973:   VecGetArray(v,&x);
2974:   VecGetLocalSize(v,&n);
2975:   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2976:   for (i=0; i<m; i++) {
2977:     ncols = ai[1] - ai[0]; ai++;
2978:     x[i]  = 0.0;
2979:     for (j=0; j<ncols; j++) {
2980:       atmp = PetscAbsScalar(*aa);
2981:       if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2982:       aa++; aj++;
2983:     }
2984:   }
2985:   VecRestoreArray(v,&x);
2986:   return(0);
2987: }

2989: PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2990: {
2991:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2993:   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2994:   PetscScalar    *x;
2995:   MatScalar      *aa;

2998:   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2999:   aa = a->a;
3000:   ai = a->i;
3001:   aj = a->j;

3003:   VecSet(v,0.0);
3004:   VecGetArray(v,&x);
3005:   VecGetLocalSize(v,&n);
3006:   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
3007:   for (i=0; i<m; i++) {
3008:     ncols = ai[1] - ai[0]; ai++;
3009:     if (ncols == A->cmap->n) { /* row is dense */
3010:       x[i] = *aa; if (idx) idx[i] = 0;
3011:     } else {  /* row is sparse so already KNOW maximum is 0.0 or higher */
3012:       x[i] = 0.0;
3013:       if (idx) {
3014:         idx[i] = 0; /* in case ncols is zero */
3015:         for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */
3016:           if (aj[j] > j) {
3017:             idx[i] = j;
3018:             break;
3019:           }
3020:         }
3021:       }
3022:     }
3023:     for (j=0; j<ncols; j++) {
3024:       if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
3025:       aa++; aj++;
3026:     }
3027:   }
3028:   VecRestoreArray(v,&x);
3029:   return(0);
3030: }

3032: PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
3033: {
3034:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
3036:   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
3037:   PetscReal      atmp;
3038:   PetscScalar    *x;
3039:   MatScalar      *aa;

3042:   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3043:   aa = a->a;
3044:   ai = a->i;
3045:   aj = a->j;

3047:   VecSet(v,0.0);
3048:   VecGetArray(v,&x);
3049:   VecGetLocalSize(v,&n);
3050:   if (n != A->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector, %D vs. %D rows", A->rmap->n, n);
3051:   for (i=0; i<m; i++) {
3052:     ncols = ai[1] - ai[0]; ai++;
3053:     if (ncols) {
3054:       /* Get first nonzero */
3055:       for (j = 0; j < ncols; j++) {
3056:         atmp = PetscAbsScalar(aa[j]);
3057:         if (atmp > 1.0e-12) {
3058:           x[i] = atmp;
3059:           if (idx) idx[i] = aj[j];
3060:           break;
3061:         }
3062:       }
3063:       if (j == ncols) {x[i] = PetscAbsScalar(*aa); if (idx) idx[i] = *aj;}
3064:     } else {
3065:       x[i] = 0.0; if (idx) idx[i] = 0;
3066:     }
3067:     for (j = 0; j < ncols; j++) {
3068:       atmp = PetscAbsScalar(*aa);
3069:       if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
3070:       aa++; aj++;
3071:     }
3072:   }
3073:   VecRestoreArray(v,&x);
3074:   return(0);
3075: }

3077: PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[])
3078: {
3079:   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3080:   PetscErrorCode  ierr;
3081:   PetscInt        i,j,m = A->rmap->n,ncols,n;
3082:   const PetscInt  *ai,*aj;
3083:   PetscScalar     *x;
3084:   const MatScalar *aa;

3087:   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3088:   aa = a->a;
3089:   ai = a->i;
3090:   aj = a->j;

3092:   VecSet(v,0.0);
3093:   VecGetArray(v,&x);
3094:   VecGetLocalSize(v,&n);
3095:   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
3096:   for (i=0; i<m; i++) {
3097:     ncols = ai[1] - ai[0]; ai++;
3098:     if (ncols == A->cmap->n) { /* row is dense */
3099:       x[i] = *aa; if (idx) idx[i] = 0;
3100:     } else {  /* row is sparse so already KNOW minimum is 0.0 or lower */
3101:       x[i] = 0.0;
3102:       if (idx) {   /* find first implicit 0.0 in the row */
3103:         idx[i] = 0; /* in case ncols is zero */
3104:         for (j=0; j<ncols; j++) {
3105:           if (aj[j] > j) {
3106:             idx[i] = j;
3107:             break;
3108:           }
3109:         }
3110:       }
3111:     }
3112:     for (j=0; j<ncols; j++) {
3113:       if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
3114:       aa++; aj++;
3115:     }
3116:   }
3117:   VecRestoreArray(v,&x);
3118:   return(0);
3119: }

3121: PetscErrorCode MatInvertBlockDiagonal_SeqAIJ(Mat A,const PetscScalar **values)
3122: {
3123:   Mat_SeqAIJ      *a = (Mat_SeqAIJ*) A->data;
3124:   PetscErrorCode  ierr;
3125:   PetscInt        i,bs = PetscAbs(A->rmap->bs),mbs = A->rmap->n/bs,ipvt[5],bs2 = bs*bs,*v_pivots,ij[7],*IJ,j;
3126:   MatScalar       *diag,work[25],*v_work;
3127:   const PetscReal shift = 0.0;
3128:   PetscBool       allowzeropivot,zeropivotdetected=PETSC_FALSE;

3131:   allowzeropivot = PetscNot(A->erroriffailure);
3132:   if (a->ibdiagvalid) {
3133:     if (values) *values = a->ibdiag;
3134:     return(0);
3135:   }
3136:   MatMarkDiagonal_SeqAIJ(A);
3137:   if (!a->ibdiag) {
3138:     PetscMalloc1(bs2*mbs,&a->ibdiag);
3139:     PetscLogObjectMemory((PetscObject)A,bs2*mbs*sizeof(PetscScalar));
3140:   }
3141:   diag = a->ibdiag;
3142:   if (values) *values = a->ibdiag;
3143:   /* factor and invert each block */
3144:   switch (bs) {
3145:   case 1:
3146:     for (i=0; i<mbs; i++) {
3147:       MatGetValues(A,1,&i,1,&i,diag+i);
3148:       if (PetscAbsScalar(diag[i] + shift) < PETSC_MACHINE_EPSILON) {
3149:         if (allowzeropivot) {
3150:           A->factorerrortype             = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3151:           A->factorerror_zeropivot_value = PetscAbsScalar(diag[i]);
3152:           A->factorerror_zeropivot_row   = i;
3153:           PetscInfo3(A,"Zero pivot, row %D pivot %g tolerance %g\n",i,(double)PetscAbsScalar(diag[i]),(double)PETSC_MACHINE_EPSILON);
3154:         } 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);
3155:       }
3156:       diag[i] = (PetscScalar)1.0 / (diag[i] + shift);
3157:     }
3158:     break;
3159:   case 2:
3160:     for (i=0; i<mbs; i++) {
3161:       ij[0] = 2*i; ij[1] = 2*i + 1;
3162:       MatGetValues(A,2,ij,2,ij,diag);
3163:       PetscKernel_A_gets_inverse_A_2(diag,shift,allowzeropivot,&zeropivotdetected);
3164:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3165:       PetscKernel_A_gets_transpose_A_2(diag);
3166:       diag += 4;
3167:     }
3168:     break;
3169:   case 3:
3170:     for (i=0; i<mbs; i++) {
3171:       ij[0] = 3*i; ij[1] = 3*i + 1; ij[2] = 3*i + 2;
3172:       MatGetValues(A,3,ij,3,ij,diag);
3173:       PetscKernel_A_gets_inverse_A_3(diag,shift,allowzeropivot,&zeropivotdetected);
3174:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3175:       PetscKernel_A_gets_transpose_A_3(diag);
3176:       diag += 9;
3177:     }
3178:     break;
3179:   case 4:
3180:     for (i=0; i<mbs; i++) {
3181:       ij[0] = 4*i; ij[1] = 4*i + 1; ij[2] = 4*i + 2; ij[3] = 4*i + 3;
3182:       MatGetValues(A,4,ij,4,ij,diag);
3183:       PetscKernel_A_gets_inverse_A_4(diag,shift,allowzeropivot,&zeropivotdetected);
3184:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3185:       PetscKernel_A_gets_transpose_A_4(diag);
3186:       diag += 16;
3187:     }
3188:     break;
3189:   case 5:
3190:     for (i=0; i<mbs; i++) {
3191:       ij[0] = 5*i; ij[1] = 5*i + 1; ij[2] = 5*i + 2; ij[3] = 5*i + 3; ij[4] = 5*i + 4;
3192:       MatGetValues(A,5,ij,5,ij,diag);
3193:       PetscKernel_A_gets_inverse_A_5(diag,ipvt,work,shift,allowzeropivot,&zeropivotdetected);
3194:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3195:       PetscKernel_A_gets_transpose_A_5(diag);
3196:       diag += 25;
3197:     }
3198:     break;
3199:   case 6:
3200:     for (i=0; i<mbs; i++) {
3201:       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;
3202:       MatGetValues(A,6,ij,6,ij,diag);
3203:       PetscKernel_A_gets_inverse_A_6(diag,shift,allowzeropivot,&zeropivotdetected);
3204:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3205:       PetscKernel_A_gets_transpose_A_6(diag);
3206:       diag += 36;
3207:     }
3208:     break;
3209:   case 7:
3210:     for (i=0; i<mbs; i++) {
3211:       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;
3212:       MatGetValues(A,7,ij,7,ij,diag);
3213:       PetscKernel_A_gets_inverse_A_7(diag,shift,allowzeropivot,&zeropivotdetected);
3214:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3215:       PetscKernel_A_gets_transpose_A_7(diag);
3216:       diag += 49;
3217:     }
3218:     break;
3219:   default:
3220:     PetscMalloc3(bs,&v_work,bs,&v_pivots,bs,&IJ);
3221:     for (i=0; i<mbs; i++) {
3222:       for (j=0; j<bs; j++) {
3223:         IJ[j] = bs*i + j;
3224:       }
3225:       MatGetValues(A,bs,IJ,bs,IJ,diag);
3226:       PetscKernel_A_gets_inverse_A(bs,diag,v_pivots,v_work,allowzeropivot,&zeropivotdetected);
3227:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3228:       PetscKernel_A_gets_transpose_A_N(diag,bs);
3229:       diag += bs2;
3230:     }
3231:     PetscFree3(v_work,v_pivots,IJ);
3232:   }
3233:   a->ibdiagvalid = PETSC_TRUE;
3234:   return(0);
3235: }

3237: static PetscErrorCode  MatSetRandom_SeqAIJ(Mat x,PetscRandom rctx)
3238: {
3240:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)x->data;
3241:   PetscScalar    a;
3242:   PetscInt       m,n,i,j,col;

3245:   if (!x->assembled) {
3246:     MatGetSize(x,&m,&n);
3247:     for (i=0; i<m; i++) {
3248:       for (j=0; j<aij->imax[i]; j++) {
3249:         PetscRandomGetValue(rctx,&a);
3250:         col  = (PetscInt)(n*PetscRealPart(a));
3251:         MatSetValues(x,1,&i,1,&col,&a,ADD_VALUES);
3252:       }
3253:     }
3254:   } else {
3255:     for (i=0; i<aij->nz; i++) {PetscRandomGetValue(rctx,aij->a+i);}
3256:   }
3257:   MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);
3258:   MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);
3259:   return(0);
3260: }

3262: /* Like MatSetRandom_SeqAIJ, but do not set values on columns in range of [low, high) */
3263: PetscErrorCode  MatSetRandomSkipColumnRange_SeqAIJ_Private(Mat x,PetscInt low,PetscInt high,PetscRandom rctx)
3264: {
3266:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)x->data;
3267:   PetscScalar    a;
3268:   PetscInt       m,n,i,j,col,nskip;

3271:   nskip = high - low;
3272:   MatGetSize(x,&m,&n);
3273:   n    -= nskip; /* shrink number of columns where nonzeros can be set */
3274:   for (i=0; i<m; i++) {
3275:     for (j=0; j<aij->imax[i]; j++) {
3276:       PetscRandomGetValue(rctx,&a);
3277:       col  = (PetscInt)(n*PetscRealPart(a));
3278:       if (col >= low) col += nskip; /* shift col rightward to skip the hole */
3279:       MatSetValues(x,1,&i,1,&col,&a,ADD_VALUES);
3280:     }
3281:   }
3282:   MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);
3283:   MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);
3284:   return(0);
3285: }


3288: /* -------------------------------------------------------------------*/
3289: static struct _MatOps MatOps_Values = { MatSetValues_SeqAIJ,
3290:                                         MatGetRow_SeqAIJ,
3291:                                         MatRestoreRow_SeqAIJ,
3292:                                         MatMult_SeqAIJ,
3293:                                 /*  4*/ MatMultAdd_SeqAIJ,
3294:                                         MatMultTranspose_SeqAIJ,
3295:                                         MatMultTransposeAdd_SeqAIJ,
3296:                                         0,
3297:                                         0,
3298:                                         0,
3299:                                 /* 10*/ 0,
3300:                                         MatLUFactor_SeqAIJ,
3301:                                         0,
3302:                                         MatSOR_SeqAIJ,
3303:                                         MatTranspose_SeqAIJ,
3304:                                 /*1 5*/ MatGetInfo_SeqAIJ,
3305:                                         MatEqual_SeqAIJ,
3306:                                         MatGetDiagonal_SeqAIJ,
3307:                                         MatDiagonalScale_SeqAIJ,
3308:                                         MatNorm_SeqAIJ,
3309:                                 /* 20*/ 0,
3310:                                         MatAssemblyEnd_SeqAIJ,
3311:                                         MatSetOption_SeqAIJ,
3312:                                         MatZeroEntries_SeqAIJ,
3313:                                 /* 24*/ MatZeroRows_SeqAIJ,
3314:                                         0,
3315:                                         0,
3316:                                         0,
3317:                                         0,
3318:                                 /* 29*/ MatSetUp_SeqAIJ,
3319:                                         0,
3320:                                         0,
3321:                                         0,
3322:                                         0,
3323:                                 /* 34*/ MatDuplicate_SeqAIJ,
3324:                                         0,
3325:                                         0,
3326:                                         MatILUFactor_SeqAIJ,
3327:                                         0,
3328:                                 /* 39*/ MatAXPY_SeqAIJ,
3329:                                         MatCreateSubMatrices_SeqAIJ,
3330:                                         MatIncreaseOverlap_SeqAIJ,
3331:                                         MatGetValues_SeqAIJ,
3332:                                         MatCopy_SeqAIJ,
3333:                                 /* 44*/ MatGetRowMax_SeqAIJ,
3334:                                         MatScale_SeqAIJ,
3335:                                         MatShift_SeqAIJ,
3336:                                         MatDiagonalSet_SeqAIJ,
3337:                                         MatZeroRowsColumns_SeqAIJ,
3338:                                 /* 49*/ MatSetRandom_SeqAIJ,
3339:                                         MatGetRowIJ_SeqAIJ,
3340:                                         MatRestoreRowIJ_SeqAIJ,
3341:                                         MatGetColumnIJ_SeqAIJ,
3342:                                         MatRestoreColumnIJ_SeqAIJ,
3343:                                 /* 54*/ MatFDColoringCreate_SeqXAIJ,
3344:                                         0,
3345:                                         0,
3346:                                         MatPermute_SeqAIJ,
3347:                                         0,
3348:                                 /* 59*/ 0,
3349:                                         MatDestroy_SeqAIJ,
3350:                                         MatView_SeqAIJ,
3351:                                         0,
3352:                                         MatMatMatMult_SeqAIJ_SeqAIJ_SeqAIJ,
3353:                                 /* 64*/ MatMatMatMultSymbolic_SeqAIJ_SeqAIJ_SeqAIJ,
3354:                                         MatMatMatMultNumeric_SeqAIJ_SeqAIJ_SeqAIJ,
3355:                                         0,
3356:                                         0,
3357:                                         0,
3358:                                 /* 69*/ MatGetRowMaxAbs_SeqAIJ,
3359:                                         MatGetRowMinAbs_SeqAIJ,
3360:                                         0,
3361:                                         0,
3362:                                         0,
3363:                                 /* 74*/ 0,
3364:                                         MatFDColoringApply_AIJ,
3365:                                         0,
3366:                                         0,
3367:                                         0,
3368:                                 /* 79*/ MatFindZeroDiagonals_SeqAIJ,
3369:                                         0,
3370:                                         0,
3371:                                         0,
3372:                                         MatLoad_SeqAIJ,
3373:                                 /* 84*/ MatIsSymmetric_SeqAIJ,
3374:                                         MatIsHermitian_SeqAIJ,
3375:                                         0,
3376:                                         0,
3377:                                         0,
3378:                                 /* 89*/ MatMatMult_SeqAIJ_SeqAIJ,
3379:                                         MatMatMultSymbolic_SeqAIJ_SeqAIJ,
3380:                                         MatMatMultNumeric_SeqAIJ_SeqAIJ,
3381:                                         MatPtAP_SeqAIJ_SeqAIJ,
3382:                                         MatPtAPSymbolic_SeqAIJ_SeqAIJ_SparseAxpy,
3383:                                 /* 94*/ MatPtAPNumeric_SeqAIJ_SeqAIJ_SparseAxpy,
3384:                                         MatMatTransposeMult_SeqAIJ_SeqAIJ,
3385:                                         MatMatTransposeMultSymbolic_SeqAIJ_SeqAIJ,
3386:                                         MatMatTransposeMultNumeric_SeqAIJ_SeqAIJ,
3387:                                         0,
3388:                                 /* 99*/ 0,
3389:                                         0,
3390:                                         0,
3391:                                         MatConjugate_SeqAIJ,
3392:                                         0,
3393:                                 /*104*/ MatSetValuesRow_SeqAIJ,
3394:                                         MatRealPart_SeqAIJ,
3395:                                         MatImaginaryPart_SeqAIJ,
3396:                                         0,
3397:                                         0,
3398:                                 /*109*/ MatMatSolve_SeqAIJ,
3399:                                         0,
3400:                                         MatGetRowMin_SeqAIJ,
3401:                                         0,
3402:                                         MatMissingDiagonal_SeqAIJ,
3403:                                 /*114*/ 0,
3404:                                         0,
3405:                                         0,
3406:                                         0,
3407:                                         0,
3408:                                 /*119*/ 0,
3409:                                         0,
3410:                                         0,
3411:                                         0,
3412:                                         MatGetMultiProcBlock_SeqAIJ,
3413:                                 /*124*/ MatFindNonzeroRows_SeqAIJ,
3414:                                         MatGetColumnNorms_SeqAIJ,
3415:                                         MatInvertBlockDiagonal_SeqAIJ,
3416:                                         MatInvertVariableBlockDiagonal_SeqAIJ,
3417:                                         0,
3418:                                 /*129*/ 0,
3419:                                         MatTransposeMatMult_SeqAIJ_SeqAIJ,
3420:                                         MatTransposeMatMultSymbolic_SeqAIJ_SeqAIJ,
3421:                                         MatTransposeMatMultNumeric_SeqAIJ_SeqAIJ,
3422:                                         MatTransposeColoringCreate_SeqAIJ,
3423:                                 /*134*/ MatTransColoringApplySpToDen_SeqAIJ,
3424:                                         MatTransColoringApplyDenToSp_SeqAIJ,
3425:                                         MatRARt_SeqAIJ_SeqAIJ,
3426:                                         MatRARtSymbolic_SeqAIJ_SeqAIJ,
3427:                                         MatRARtNumeric_SeqAIJ_SeqAIJ,
3428:                                  /*139*/0,
3429:                                         0,
3430:                                         0,
3431:                                         MatFDColoringSetUp_SeqXAIJ,
3432:                                         MatFindOffBlockDiagonalEntries_SeqAIJ,
3433:                                  /*144*/MatCreateMPIMatConcatenateSeqMat_SeqAIJ,
3434:                                         MatDestroySubMatrices_SeqAIJ
3435: };

3437: PetscErrorCode  MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
3438: {
3439:   Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data;
3440:   PetscInt   i,nz,n;

3443:   nz = aij->maxnz;
3444:   n  = mat->rmap->n;
3445:   for (i=0; i<nz; i++) {
3446:     aij->j[i] = indices[i];
3447:   }
3448:   aij->nz = nz;
3449:   for (i=0; i<n; i++) {
3450:     aij->ilen[i] = aij->imax[i];
3451:   }
3452:   return(0);
3453: }

3455: /*
3456:  * When a sparse matrix has many zero columns, we should compact them out to save the space
3457:  * This happens in MatPtAPSymbolic_MPIAIJ_MPIAIJ_scalable()
3458:  * */
3459: PetscErrorCode  MatSeqAIJCompactOutExtraColumns_SeqAIJ(Mat mat, ISLocalToGlobalMapping *mapping)
3460: {
3461:   Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data;
3462:   PetscTable         gid1_lid1;
3463:   PetscTablePosition tpos;
3464:   PetscInt           gid,lid,i,j,ncols,ec;
3465:   PetscInt           *garray;
3466:   PetscErrorCode  ierr;

3471:   /* use a table */
3472:   PetscTableCreate(mat->rmap->n,mat->cmap->N+1,&gid1_lid1);
3473:   ec = 0;
3474:   for (i=0; i<mat->rmap->n; i++) {
3475:     ncols = aij->i[i+1] - aij->i[i];
3476:     for (j=0; j<ncols; j++) {
3477:       PetscInt data,gid1 = aij->j[aij->i[i] + j] + 1;
3478:       PetscTableFind(gid1_lid1,gid1,&data);
3479:       if (!data) {
3480:         /* one based table */
3481:         PetscTableAdd(gid1_lid1,gid1,++ec,INSERT_VALUES);
3482:       }
3483:     }
3484:   }
3485:   /* form array of columns we need */
3486:   PetscMalloc1(ec+1,&garray);
3487:   PetscTableGetHeadPosition(gid1_lid1,&tpos);
3488:   while (tpos) {
3489:     PetscTableGetNext(gid1_lid1,&tpos,&gid,&lid);
3490:     gid--;
3491:     lid--;
3492:     garray[lid] = gid;
3493:   }
3494:   PetscSortInt(ec,garray); /* sort, and rebuild */
3495:   PetscTableRemoveAll(gid1_lid1);
3496:   for (i=0; i<ec; i++) {
3497:     PetscTableAdd(gid1_lid1,garray[i]+1,i+1,INSERT_VALUES);
3498:   }
3499:   /* compact out the extra columns in B */
3500:   for (i=0; i<mat->rmap->n; i++) {
3501:         ncols = aij->i[i+1] - aij->i[i];
3502:     for (j=0; j<ncols; j++) {
3503:       PetscInt gid1 = aij->j[aij->i[i] + j] + 1;
3504:       PetscTableFind(gid1_lid1,gid1,&lid);
3505:       lid--;
3506:       aij->j[aij->i[i] + j] = lid;
3507:     }
3508:   }
3509:   mat->cmap->n = mat->cmap->N = ec;
3510:   mat->cmap->bs = 1;

3512:   PetscTableDestroy(&gid1_lid1);
3513:   PetscLayoutSetUp((mat->cmap));
3514:   ISLocalToGlobalMappingCreate(PETSC_COMM_SELF,mat->cmap->bs,mat->cmap->n,garray,PETSC_OWN_POINTER,mapping);
3515:   ISLocalToGlobalMappingSetType(*mapping,ISLOCALTOGLOBALMAPPINGHASH);
3516:   return(0);
3517: }

3519: /*@
3520:     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
3521:        in the matrix.

3523:   Input Parameters:
3524: +  mat - the SeqAIJ matrix
3525: -  indices - the column indices

3527:   Level: advanced

3529:   Notes:
3530:     This can be called if you have precomputed the nonzero structure of the
3531:   matrix and want to provide it to the matrix object to improve the performance
3532:   of the MatSetValues() operation.

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

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

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

3541: @*/
3542: PetscErrorCode  MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
3543: {

3549:   PetscUseMethod(mat,"MatSeqAIJSetColumnIndices_C",(Mat,PetscInt*),(mat,indices));
3550:   return(0);
3551: }

3553: /* ----------------------------------------------------------------------------------------*/

3555: PetscErrorCode  MatStoreValues_SeqAIJ(Mat mat)
3556: {
3557:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)mat->data;
3559:   size_t         nz = aij->i[mat->rmap->n];

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

3564:   /* allocate space for values if not already there */
3565:   if (!aij->saved_values) {
3566:     PetscMalloc1(nz+1,&aij->saved_values);
3567:     PetscLogObjectMemory((PetscObject)mat,(nz+1)*sizeof(PetscScalar));
3568:   }

3570:   /* copy values over */
3571:   PetscArraycpy(aij->saved_values,aij->a,nz);
3572:   return(0);
3573: }

3575: /*@
3576:     MatStoreValues - Stashes a copy of the matrix values; this allows, for
3577:        example, reuse of the linear part of a Jacobian, while recomputing the
3578:        nonlinear portion.

3580:    Collect on Mat

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

3585:   Level: advanced

3587:   Common Usage, with SNESSolve():
3588: $    Create Jacobian matrix
3589: $    Set linear terms into matrix
3590: $    Apply boundary conditions to matrix, at this time matrix must have
3591: $      final nonzero structure (i.e. setting the nonlinear terms and applying
3592: $      boundary conditions again will not change the nonzero structure
3593: $    MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3594: $    MatStoreValues(mat);
3595: $    Call SNESSetJacobian() with matrix
3596: $    In your Jacobian routine
3597: $      MatRetrieveValues(mat);
3598: $      Set nonlinear terms in matrix

3600:   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
3601: $    // build linear portion of Jacobian
3602: $    MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3603: $    MatStoreValues(mat);
3604: $    loop over nonlinear iterations
3605: $       MatRetrieveValues(mat);
3606: $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
3607: $       // call MatAssemblyBegin/End() on matrix
3608: $       Solve linear system with Jacobian
3609: $    endloop

3611:   Notes:
3612:     Matrix must already be assemblied before calling this routine
3613:     Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
3614:     calling this routine.

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

3619: .seealso: MatRetrieveValues()

3621: @*/
3622: PetscErrorCode  MatStoreValues(Mat mat)
3623: {

3628:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3629:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3630:   PetscUseMethod(mat,"MatStoreValues_C",(Mat),(mat));
3631:   return(0);
3632: }

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

3641:   if (!aij->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3642:   if (!aij->saved_values) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
3643:   /* copy values over */
3644:   PetscArraycpy(aij->a,aij->saved_values,nz);
3645:   return(0);
3646: }

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

3653:    Collect on Mat

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

3658:   Level: advanced

3660: .seealso: MatStoreValues()

3662: @*/
3663: PetscErrorCode  MatRetrieveValues(Mat mat)
3664: {

3669:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3670:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3671:   PetscUseMethod(mat,"MatRetrieveValues_C",(Mat),(mat));
3672:   return(0);
3673: }


3676: /* --------------------------------------------------------------------------------*/
3677: /*@C
3678:    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
3679:    (the default parallel PETSc format).  For good matrix assembly performance
3680:    the user should preallocate the matrix storage by setting the parameter nz
3681:    (or the array nnz).  By setting these parameters accurately, performance
3682:    during matrix assembly can be increased by more than a factor of 50.

3684:    Collective

3686:    Input Parameters:
3687: +  comm - MPI communicator, set to PETSC_COMM_SELF
3688: .  m - number of rows
3689: .  n - number of columns
3690: .  nz - number of nonzeros per row (same for all rows)
3691: -  nnz - array containing the number of nonzeros in the various rows
3692:          (possibly different for each row) or NULL

3694:    Output Parameter:
3695: .  A - the matrix

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

3701:    Notes:
3702:    If nnz is given then nz is ignored

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

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

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

3719:    Options Database Keys:
3720: +  -mat_no_inode  - Do not use inodes
3721: -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)

3723:    Level: intermediate

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

3727: @*/
3728: PetscErrorCode  MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
3729: {

3733:   MatCreate(comm,A);
3734:   MatSetSizes(*A,m,n,m,n);
3735:   MatSetType(*A,MATSEQAIJ);
3736:   MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);
3737:   return(0);
3738: }

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

3746:    Collective

3748:    Input Parameters:
3749: +  B - The matrix
3750: .  nz - number of nonzeros per row (same for all rows)
3751: -  nnz - array containing the number of nonzeros in the various rows
3752:          (possibly different for each row) or NULL

3754:    Notes:
3755:      If nnz is given then nz is ignored

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

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

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

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

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

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

3784:    Level: intermediate

3786: .seealso: MatCreate(), MatCreateAIJ(), MatSetValues(), MatSeqAIJSetColumnIndices(), MatCreateSeqAIJWithArrays(), MatGetInfo()

3788: @*/
3789: PetscErrorCode  MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
3790: {

3796:   PetscTryMethod(B,"MatSeqAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[]),(B,nz,nnz));
3797:   return(0);
3798: }

3800: PetscErrorCode  MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,const PetscInt *nnz)
3801: {
3802:   Mat_SeqAIJ     *b;
3803:   PetscBool      skipallocation = PETSC_FALSE,realalloc = PETSC_FALSE;
3805:   PetscInt       i;

3808:   if (nz >= 0 || nnz) realalloc = PETSC_TRUE;
3809:   if (nz == MAT_SKIP_ALLOCATION) {
3810:     skipallocation = PETSC_TRUE;
3811:     nz             = 0;
3812:   }
3813:   PetscLayoutSetUp(B->rmap);
3814:   PetscLayoutSetUp(B->cmap);

3816:   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
3817:   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %D",nz);
3818: #if defined(PETSC_USE_DEBUG)
3819:   if (nnz) {
3820:     for (i=0; i<B->rmap->n; i++) {
3821:       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]);
3822:       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);
3823:     }
3824:   }
3825: #endif

3827:   B->preallocated = PETSC_TRUE;

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

3831:   if (!skipallocation) {
3832:     if (!b->imax) {
3833:       PetscMalloc1(B->rmap->n,&b->imax);
3834:       PetscLogObjectMemory((PetscObject)B,B->rmap->n*sizeof(PetscInt));
3835:     }
3836:     if (!b->ilen) {
3837:       /* b->ilen will count nonzeros in each row so far. */
3838:       PetscCalloc1(B->rmap->n,&b->ilen);
3839:       PetscLogObjectMemory((PetscObject)B,B->rmap->n*sizeof(PetscInt));
3840:     } else {
3841:       PetscMemzero(b->ilen,B->rmap->n*sizeof(PetscInt));
3842:     }
3843:     if (!b->ipre) {
3844:       PetscMalloc1(B->rmap->n,&b->ipre);
3845:       PetscLogObjectMemory((PetscObject)B,B->rmap->n*sizeof(PetscInt));
3846:     }
3847:     if (!nnz) {
3848:       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
3849:       else if (nz < 0) nz = 1;
3850:       nz = PetscMin(nz,B->cmap->n);
3851:       for (i=0; i<B->rmap->n; i++) b->imax[i] = nz;
3852:       nz = nz*B->rmap->n;
3853:     } else {
3854:       nz = 0;
3855:       for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
3856:     }

3858:     /* allocate the matrix space */
3859:     /* FIXME: should B's old memory be unlogged? */
3860:     MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);
3861:     if (B->structure_only) {
3862:       PetscMalloc1(nz,&b->j);
3863:       PetscMalloc1(B->rmap->n+1,&b->i);
3864:       PetscLogObjectMemory((PetscObject)B,(B->rmap->n+1)*sizeof(PetscInt)+nz*sizeof(PetscInt));
3865:     } else {
3866:       PetscMalloc3(nz,&b->a,nz,&b->j,B->rmap->n+1,&b->i);
3867:       PetscLogObjectMemory((PetscObject)B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));
3868:     }
3869:     b->i[0] = 0;
3870:     for (i=1; i<B->rmap->n+1; i++) {
3871:       b->i[i] = b->i[i-1] + b->imax[i-1];
3872:     }
3873:     if (B->structure_only) {
3874:       b->singlemalloc = PETSC_FALSE;
3875:       b->free_a       = PETSC_FALSE;
3876:     } else {
3877:       b->singlemalloc = PETSC_TRUE;
3878:       b->free_a       = PETSC_TRUE;
3879:     }
3880:     b->free_ij      = PETSC_TRUE;
3881:   } else {
3882:     b->free_a  = PETSC_FALSE;
3883:     b->free_ij = PETSC_FALSE;
3884:   }

3886:   if (b->ipre && nnz != b->ipre  && b->imax) {
3887:     /* reserve user-requested sparsity */
3888:     PetscArraycpy(b->ipre,b->imax,B->rmap->n);
3889:   }


3892:   b->nz               = 0;
3893:   b->maxnz            = nz;
3894:   B->info.nz_unneeded = (double)b->maxnz;
3895:   if (realalloc) {
3896:     MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);
3897:   }
3898:   B->was_assembled = PETSC_FALSE;
3899:   B->assembled     = PETSC_FALSE;
3900:   return(0);
3901: }


3904: PetscErrorCode MatResetPreallocation_SeqAIJ(Mat A)
3905: {
3906:   Mat_SeqAIJ     *a;
3907:   PetscInt       i;


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

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

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

3922:   PetscArraycpy(a->imax,a->ipre,A->rmap->n);
3923:   PetscArrayzero(a->ilen,A->rmap->n);
3924:   a->i[0] = 0;
3925:   for (i=1; i<A->rmap->n+1; i++) {
3926:     a->i[i] = a->i[i-1] + a->imax[i-1];
3927:   }
3928:   A->preallocated     = PETSC_TRUE;
3929:   a->nz               = 0;
3930:   a->maxnz            = a->i[A->rmap->n];
3931:   A->info.nz_unneeded = (double)a->maxnz;
3932:   A->was_assembled    = PETSC_FALSE;
3933:   A->assembled        = PETSC_FALSE;
3934:   return(0);
3935: }

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

3940:    Input Parameters:
3941: +  B - the matrix
3942: .  i - the indices into j for the start of each row (starts with zero)
3943: .  j - the column indices for each row (starts with zero) these must be sorted for each row
3944: -  v - optional values in the matrix

3946:    Level: developer

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

3950: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), MATSEQAIJ
3951: @*/
3952: PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
3953: {

3959:   PetscTryMethod(B,"MatSeqAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));
3960:   return(0);
3961: }

3963: PetscErrorCode  MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3964: {
3965:   PetscInt       i;
3966:   PetscInt       m,n;
3967:   PetscInt       nz;
3968:   PetscInt       *nnz, nz_max = 0;

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

3974:   PetscLayoutSetUp(B->rmap);
3975:   PetscLayoutSetUp(B->cmap);

3977:   MatGetSize(B, &m, &n);
3978:   PetscMalloc1(m+1, &nnz);
3979:   for (i = 0; i < m; i++) {
3980:     nz     = Ii[i+1]- Ii[i];
3981:     nz_max = PetscMax(nz_max, nz);
3982:     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
3983:     nnz[i] = nz;
3984:   }
3985:   MatSeqAIJSetPreallocation(B, 0, nnz);
3986:   PetscFree(nnz);

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

3992:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
3993:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);

3995:   MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
3996:   return(0);
3997: }

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

4002: /*
4003:     Computes (B'*A')' since computing B*A directly is untenable

4005:                n                       p                          p
4006:         (              )       (              )         (                  )
4007:       m (      A       )  *  n (       B      )   =   m (         C        )
4008:         (              )       (              )         (                  )

4010: */
4011: PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
4012: {
4013:   PetscErrorCode    ierr;
4014:   Mat_SeqDense      *sub_a = (Mat_SeqDense*)A->data;
4015:   Mat_SeqAIJ        *sub_b = (Mat_SeqAIJ*)B->data;
4016:   Mat_SeqDense      *sub_c = (Mat_SeqDense*)C->data;
4017:   PetscInt          i,n,m,q,p;
4018:   const PetscInt    *ii,*idx;
4019:   const PetscScalar *b,*a,*a_q;
4020:   PetscScalar       *c,*c_q;

4023:   m    = A->rmap->n;
4024:   n    = A->cmap->n;
4025:   p    = B->cmap->n;
4026:   a    = sub_a->v;
4027:   b    = sub_b->a;
4028:   c    = sub_c->v;
4029:   PetscArrayzero(c,m*p);

4031:   ii  = sub_b->i;
4032:   idx = sub_b->j;
4033:   for (i=0; i<n; i++) {
4034:     q = ii[i+1] - ii[i];
4035:     while (q-->0) {
4036:       c_q = c + m*(*idx);
4037:       a_q = a + m*i;
4038:       PetscKernelAXPY(c_q,*b,a_q,m);
4039:       idx++;
4040:       b++;
4041:     }
4042:   }
4043:   return(0);
4044: }

4046: PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
4047: {
4049:   PetscInt       m=A->rmap->n,n=B->cmap->n;
4050:   Mat            Cmat;

4053:   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);
4054:   MatCreate(PetscObjectComm((PetscObject)A),&Cmat);
4055:   MatSetSizes(Cmat,m,n,m,n);
4056:   MatSetBlockSizesFromMats(Cmat,A,B);
4057:   MatSetType(Cmat,MATSEQDENSE);
4058:   MatSeqDenseSetPreallocation(Cmat,NULL);

4060:   Cmat->ops->matmultnumeric = MatMatMultNumeric_SeqDense_SeqAIJ;

4062:   *C = Cmat;
4063:   return(0);
4064: }

4066: /* ----------------------------------------------------------------*/
4067: PETSC_INTERN PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
4068: {

4072:   if (scall == MAT_INITIAL_MATRIX) {
4073:     PetscLogEventBegin(MAT_MatMultSymbolic,A,B,0,0);
4074:     MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);
4075:     PetscLogEventEnd(MAT_MatMultSymbolic,A,B,0,0);
4076:   }
4077:   PetscLogEventBegin(MAT_MatMultNumeric,A,B,0,0);
4078:   MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);
4079:   PetscLogEventEnd(MAT_MatMultNumeric,A,B,0,0);
4080:   return(0);
4081: }


4084: /*MC
4085:    MATSEQAIJ - MATSEQAIJ = "seqaij" - A matrix type to be used for sequential sparse matrices,
4086:    based on compressed sparse row format.

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

4091:   Level: beginner

4093: .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
4094: M*/

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

4099:    This matrix type is identical to MATSEQAIJ when constructed with a single process communicator,
4100:    and MATMPIAIJ otherwise.  As a result, for single process communicators,
4101:   MatSeqAIJSetPreallocation is supported, and similarly MatMPIAIJSetPreallocation is supported
4102:   for communicators controlling multiple processes.  It is recommended that you call both of
4103:   the above preallocation routines for simplicity.

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

4108:   Developer Notes:
4109:     Subclasses include MATAIJCUSPARSE, MATAIJPERM, MATAIJSELL, MATAIJMKL, MATAIJCRL, and also automatically switches over to use inodes when
4110:    enough exist.

4112:   Level: beginner

4114: .seealso: MatCreateAIJ(), MatCreateSeqAIJ(), MATSEQAIJ,MATMPIAIJ
4115: M*/

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

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

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

4129:   Level: beginner

4131: .seealso: MatCreateMPIAIJCRL,MATSEQAIJCRL,MATMPIAIJCRL, MATSEQAIJCRL, MATMPIAIJCRL
4132: M*/

4134: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCRL(Mat,MatType,MatReuse,Mat*);
4135: #if defined(PETSC_HAVE_ELEMENTAL)
4136: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_Elemental(Mat,MatType,MatReuse,Mat*);
4137: #endif
4138: #if defined(PETSC_HAVE_HYPRE)
4139: PETSC_INTERN PetscErrorCode MatConvert_AIJ_HYPRE(Mat A,MatType,MatReuse,Mat*);
4140: PETSC_INTERN PetscErrorCode MatMatMatMult_Transpose_AIJ_AIJ(Mat,Mat,Mat,MatReuse,PetscReal,Mat*);
4141: #endif
4142: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqDense(Mat,MatType,MatReuse,Mat*);

4144: PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_SeqSELL(Mat,MatType,MatReuse,Mat*);
4145: PETSC_INTERN PetscErrorCode MatConvert_XAIJ_IS(Mat,MatType,MatReuse,Mat*);
4146: PETSC_INTERN PetscErrorCode MatPtAP_IS_XAIJ(Mat,Mat,MatReuse,PetscReal,Mat*);

4148: /*@C
4149:    MatSeqAIJGetArray - gives access to the array where the data for a MATSEQAIJ matrix is stored

4151:    Not Collective

4153:    Input Parameter:
4154: .  mat - a MATSEQAIJ matrix

4156:    Output Parameter:
4157: .   array - pointer to the data

4159:    Level: intermediate

4161: .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90()
4162: @*/
4163: PetscErrorCode  MatSeqAIJGetArray(Mat A,PetscScalar **array)
4164: {

4168:   PetscUseMethod(A,"MatSeqAIJGetArray_C",(Mat,PetscScalar**),(A,array));
4169:   return(0);
4170: }

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

4175:    Not Collective

4177:    Input Parameter:
4178: .  mat - a MATSEQAIJ matrix

4180:    Output Parameter:
4181: .   nz - the maximum number of nonzeros in any row

4183:    Level: intermediate

4185: .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90()
4186: @*/
4187: PetscErrorCode  MatSeqAIJGetMaxRowNonzeros(Mat A,PetscInt *nz)
4188: {
4189:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)A->data;

4192:   *nz = aij->rmax;
4193:   return(0);
4194: }

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

4199:    Not Collective

4201:    Input Parameters:
4202: +  mat - a MATSEQAIJ matrix
4203: -  array - pointer to the data

4205:    Level: intermediate

4207: .seealso: MatSeqAIJGetArray(), MatSeqAIJRestoreArrayF90()
4208: @*/
4209: PetscErrorCode  MatSeqAIJRestoreArray(Mat A,PetscScalar **array)
4210: {

4214:   PetscUseMethod(A,"MatSeqAIJRestoreArray_C",(Mat,PetscScalar**),(A,array));
4215:   return(0);
4216: }

4218: #if defined(PETSC_HAVE_CUDA)
4219: PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCUSPARSE(Mat);
4220: #endif

4222: PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJ(Mat B)
4223: {
4224:   Mat_SeqAIJ     *b;
4226:   PetscMPIInt    size;

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

4232:   PetscNewLog(B,&b);

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

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

4239:   b->row                = 0;
4240:   b->col                = 0;
4241:   b->icol               = 0;
4242:   b->reallocs           = 0;
4243:   b->ignorezeroentries  = PETSC_FALSE;
4244:   b->roworiented        = PETSC_TRUE;
4245:   b->nonew              = 0;
4246:   b->diag               = 0;
4247:   b->solve_work         = 0;
4248:   B->spptr              = 0;
4249:   b->saved_values       = 0;
4250:   b->idiag              = 0;
4251:   b->mdiag              = 0;
4252:   b->ssor_work          = 0;
4253:   b->omega              = 1.0;
4254:   b->fshift             = 0.0;
4255:   b->idiagvalid         = PETSC_FALSE;
4256:   b->ibdiagvalid        = PETSC_FALSE;
4257:   b->keepnonzeropattern = PETSC_FALSE;

4259:   PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);
4260:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJGetArray_C",MatSeqAIJGetArray_SeqAIJ);
4261:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJRestoreArray_C",MatSeqAIJRestoreArray_SeqAIJ);

4263: #if defined(PETSC_HAVE_MATLAB_ENGINE)
4264:   PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEnginePut_C",MatlabEnginePut_SeqAIJ);
4265:   PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEngineGet_C",MatlabEngineGet_SeqAIJ);
4266: #endif

4268:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetColumnIndices_C",MatSeqAIJSetColumnIndices_SeqAIJ);
4269:   PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_SeqAIJ);
4270:   PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_SeqAIJ);
4271:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",MatConvert_SeqAIJ_SeqSBAIJ);
4272:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqbaij_C",MatConvert_SeqAIJ_SeqBAIJ);
4273:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijperm_C",MatConvert_SeqAIJ_SeqAIJPERM);
4274:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijsell_C",MatConvert_SeqAIJ_SeqAIJSELL);
4275: #if defined(PETSC_HAVE_MKL_SPARSE)
4276:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijmkl_C",MatConvert_SeqAIJ_SeqAIJMKL);
4277: #endif
4278: #if defined(PETSC_HAVE_CUDA)
4279:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijcusparse_C",MatConvert_SeqAIJ_SeqAIJCUSPARSE);
4280: #endif
4281:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijcrl_C",MatConvert_SeqAIJ_SeqAIJCRL);
4282: #if defined(PETSC_HAVE_ELEMENTAL)
4283:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_elemental_C",MatConvert_SeqAIJ_Elemental);
4284: #endif
4285: #if defined(PETSC_HAVE_HYPRE)
4286:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_hypre_C",MatConvert_AIJ_HYPRE);
4287:   PetscObjectComposeFunction((PetscObject)B,"MatMatMatMult_transpose_seqaij_seqaij_C",MatMatMatMult_Transpose_AIJ_AIJ);
4288: #endif
4289:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqdense_C",MatConvert_SeqAIJ_SeqDense);
4290:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqsell_C",MatConvert_SeqAIJ_SeqSELL);
4291:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_is_C",MatConvert_XAIJ_IS);
4292:   PetscObjectComposeFunction((PetscObject)B,"MatIsTranspose_C",MatIsTranspose_SeqAIJ);
4293:   PetscObjectComposeFunction((PetscObject)B,"MatIsHermitianTranspose_C",MatIsTranspose_SeqAIJ);
4294:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",MatSeqAIJSetPreallocation_SeqAIJ);
4295:   PetscObjectComposeFunction((PetscObject)B,"MatResetPreallocation_C",MatResetPreallocation_SeqAIJ);
4296:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",MatSeqAIJSetPreallocationCSR_SeqAIJ);
4297:   PetscObjectComposeFunction((PetscObject)B,"MatReorderForNonzeroDiagonal_C",MatReorderForNonzeroDiagonal_SeqAIJ);
4298:   PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqdense_seqaij_C",MatMatMult_SeqDense_SeqAIJ);
4299:   PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C",MatMatMultSymbolic_SeqDense_SeqAIJ);
4300:   PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C",MatMatMultNumeric_SeqDense_SeqAIJ);
4301:   PetscObjectComposeFunction((PetscObject)B,"MatPtAP_is_seqaij_C",MatPtAP_IS_XAIJ);
4302:   MatCreate_SeqAIJ_Inode(B);
4303:   PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);
4304:   MatSeqAIJSetTypeFromOptions(B);  /* this allows changing the matrix subtype to say MATSEQAIJPERM */
4305:   return(0);
4306: }

4308: /*
4309:     Given a matrix generated with MatGetFactor() duplicates all the information in A into B
4310: */
4311: PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscBool mallocmatspace)
4312: {
4313:   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
4315:   PetscInt       m = A->rmap->n,i;

4318:   c = (Mat_SeqAIJ*)C->data;

4320:   C->factortype = A->factortype;
4321:   c->row        = 0;
4322:   c->col        = 0;
4323:   c->icol       = 0;
4324:   c->reallocs   = 0;

4326:   C->assembled = PETSC_TRUE;

4328:   PetscLayoutReference(A->rmap,&C->rmap);
4329:   PetscLayoutReference(A->cmap,&C->cmap);

4331:   PetscMalloc1(m,&c->imax);
4332:   PetscMemcpy(c->imax,a->imax,m*sizeof(PetscInt));
4333:   PetscMalloc1(m,&c->ilen);
4334:   PetscMemcpy(c->ilen,a->ilen,m*sizeof(PetscInt));
4335:   PetscLogObjectMemory((PetscObject)C, 2*m*sizeof(PetscInt));

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

4342:     c->singlemalloc = PETSC_TRUE;

4344:     PetscArraycpy(c->i,a->i,m+1);
4345:     if (m > 0) {
4346:       PetscArraycpy(c->j,a->j,a->i[m]);
4347:       if (cpvalues == MAT_COPY_VALUES) {
4348:         PetscArraycpy(c->a,a->a,a->i[m]);
4349:       } else {
4350:         PetscArrayzero(c->a,a->i[m]);
4351:       }
4352:     }
4353:   }

4355:   c->ignorezeroentries = a->ignorezeroentries;
4356:   c->roworiented       = a->roworiented;
4357:   c->nonew             = a->nonew;
4358:   if (a->diag) {
4359:     PetscMalloc1(m+1,&c->diag);
4360:     PetscMemcpy(c->diag,a->diag,m*sizeof(PetscInt));
4361:     PetscLogObjectMemory((PetscObject)C,(m+1)*sizeof(PetscInt));
4362:   } else c->diag = NULL;

4364:   c->solve_work         = 0;
4365:   c->saved_values       = 0;
4366:   c->idiag              = 0;
4367:   c->ssor_work          = 0;
4368:   c->keepnonzeropattern = a->keepnonzeropattern;
4369:   c->free_a             = PETSC_TRUE;
4370:   c->free_ij            = PETSC_TRUE;

4372:   c->rmax         = a->rmax;
4373:   c->nz           = a->nz;
4374:   c->maxnz        = a->nz;       /* Since we allocate exactly the right amount */
4375:   C->preallocated = PETSC_TRUE;

4377:   c->compressedrow.use   = a->compressedrow.use;
4378:   c->compressedrow.nrows = a->compressedrow.nrows;
4379:   if (a->compressedrow.use) {
4380:     i    = a->compressedrow.nrows;
4381:     PetscMalloc2(i+1,&c->compressedrow.i,i,&c->compressedrow.rindex);
4382:     PetscArraycpy(c->compressedrow.i,a->compressedrow.i,i+1);
4383:     PetscArraycpy(c->compressedrow.rindex,a->compressedrow.rindex,i);
4384:   } else {
4385:     c->compressedrow.use    = PETSC_FALSE;
4386:     c->compressedrow.i      = NULL;
4387:     c->compressedrow.rindex = NULL;
4388:   }
4389:   c->nonzerorowcnt = a->nonzerorowcnt;
4390:   C->nonzerostate  = A->nonzerostate;

4392:   MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);
4393:   PetscFunctionListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);
4394:   return(0);
4395: }

4397: PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
4398: {

4402:   MatCreate(PetscObjectComm((PetscObject)A),B);
4403:   MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);
4404:   if (!(A->rmap->n % A->rmap->bs) && !(A->cmap->n % A->cmap->bs)) {
4405:     MatSetBlockSizesFromMats(*B,A,A);
4406:   }
4407:   MatSetType(*B,((PetscObject)A)->type_name);
4408:   MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);
4409:   return(0);
4410: }

4412: PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer)
4413: {
4414:   PetscBool      isbinary, ishdf5;

4420:   /* force binary viewer to load .info file if it has not yet done so */
4421:   PetscViewerSetUp(viewer);
4422:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
4423:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,  &ishdf5);
4424:   if (isbinary) {
4425:     MatLoad_SeqAIJ_Binary(newMat,viewer);
4426:   } else if (ishdf5) {
4427: #if defined(PETSC_HAVE_HDF5)
4428:     MatLoad_AIJ_HDF5(newMat,viewer);
4429: #else
4430:     SETERRQ(PetscObjectComm((PetscObject)newMat),PETSC_ERR_SUP,"HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
4431: #endif
4432:   } else {
4433:     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);
4434:   }
4435:   return(0);
4436: }

4438: PetscErrorCode MatLoad_SeqAIJ_Binary(Mat newMat, PetscViewer viewer)
4439: {
4440:   Mat_SeqAIJ     *a;
4442:   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N,rows,cols;
4443:   int            fd;
4444:   PetscMPIInt    size;
4445:   MPI_Comm       comm;
4446:   PetscInt       bs = newMat->rmap->bs;

4449:   PetscObjectGetComm((PetscObject)viewer,&comm);
4450:   MPI_Comm_size(comm,&size);
4451:   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"view must have one processor");

4453:   PetscOptionsBegin(comm,NULL,"Options for loading SEQAIJ matrix","Mat");
4454:   PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,NULL);
4455:   PetscOptionsEnd();
4456:   if (bs < 0) bs = 1;
4457:   MatSetBlockSize(newMat,bs);

4459:   PetscViewerBinaryGetDescriptor(viewer,&fd);
4460:   PetscBinaryRead(fd,header,4,NULL,PETSC_INT);
4461:   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
4462:   M = header[1]; N = header[2]; nz = header[3];

4464:   if (nz < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format on disk,cannot load as SeqAIJ");

4466:   /* read in row lengths */
4467:   PetscMalloc1(M,&rowlengths);
4468:   PetscBinaryRead(fd,rowlengths,M,NULL,PETSC_INT);

4470:   /* check if sum of rowlengths is same as nz */
4471:   for (i=0,sum=0; i< M; i++) sum +=rowlengths[i];
4472:   if (sum != nz) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_FILE_READ,"Inconsistant matrix data in file. no-nonzeros = %dD, sum-row-lengths = %D\n",nz,sum);

4474:   /* set global size if not set already*/
4475:   if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) {
4476:     MatSetSizes(newMat,PETSC_DECIDE,PETSC_DECIDE,M,N);
4477:   } else {
4478:     /* if sizes and type are already set, check if the matrix  global sizes are correct */
4479:     MatGetSize(newMat,&rows,&cols);
4480:     if (rows < 0 && cols < 0) { /* user might provide local size instead of global size */
4481:       MatGetLocalSize(newMat,&rows,&cols);
4482:     }
4483:     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);
4484:   }
4485:   MatSeqAIJSetPreallocation_SeqAIJ(newMat,0,rowlengths);
4486:   a    = (Mat_SeqAIJ*)newMat->data;

4488:   PetscBinaryRead(fd,a->j,nz,NULL,PETSC_INT);

4490:   /* read in nonzero values */
4491:   PetscBinaryRead(fd,a->a,nz,NULL,PETSC_SCALAR);

4493:   /* set matrix "i" values */
4494:   a->i[0] = 0;
4495:   for (i=1; i<= M; i++) {
4496:     a->i[i]      = a->i[i-1] + rowlengths[i-1];
4497:     a->ilen[i-1] = rowlengths[i-1];
4498:   }
4499:   PetscFree(rowlengths);

4501:   MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);
4502:   MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);
4503:   return(0);
4504: }

4506: PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscBool * flg)
4507: {
4508:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*b = (Mat_SeqAIJ*)B->data;
4510: #if defined(PETSC_USE_COMPLEX)
4511:   PetscInt k;
4512: #endif

4515:   /* If the  matrix dimensions are not equal,or no of nonzeros */
4516:   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
4517:     *flg = PETSC_FALSE;
4518:     return(0);
4519:   }

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

4525:   /* if a->j are the same */
4526:   PetscArraycmp(a->j,b->j,a->nz,flg);
4527:   if (!*flg) return(0);

4529:   /* if a->a are the same */
4530: #if defined(PETSC_USE_COMPLEX)
4531:   for (k=0; k<a->nz; k++) {
4532:     if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])) {
4533:       *flg = PETSC_FALSE;
4534:       return(0);
4535:     }
4536:   }
4537: #else
4538:   PetscArraycmp(a->a,b->a,a->nz,flg);
4539: #endif
4540:   return(0);
4541: }

4543: /*@
4544:      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
4545:               provided by the user.

4547:       Collective

4549:    Input Parameters:
4550: +   comm - must be an MPI communicator of size 1
4551: .   m - number of rows
4552: .   n - number of columns
4553: .   i - row indices; that is i[0] = 0, i[row] = i[row-1] + number of elements in that row of the matrix
4554: .   j - column indices
4555: -   a - matrix values

4557:    Output Parameter:
4558: .   mat - the matrix

4560:    Level: intermediate

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

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

4568:        The i and j indices are 0 based

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

4574: $        1 0 0
4575: $        2 0 3
4576: $        4 5 6
4577: $
4578: $        i =  {0,1,3,6}  [size = nrow+1  = 3+1]
4579: $        j =  {0,0,2,0,1,2}  [size = 6]; values must be sorted for each row
4580: $        v =  {1,2,3,4,5,6}  [size = 6]


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

4585: @*/
4586: PetscErrorCode  MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt i[],PetscInt j[],PetscScalar a[],Mat *mat)
4587: {
4589:   PetscInt       ii;
4590:   Mat_SeqAIJ     *aij;
4591: #if defined(PETSC_USE_DEBUG)
4592:   PetscInt jj;
4593: #endif

4596:   if (m > 0 && i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
4597:   MatCreate(comm,mat);
4598:   MatSetSizes(*mat,m,n,m,n);
4599:   /* MatSetBlockSizes(*mat,,); */
4600:   MatSetType(*mat,MATSEQAIJ);
4601:   MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);
4602:   aij  = (Mat_SeqAIJ*)(*mat)->data;
4603:   PetscMalloc1(m,&aij->imax);
4604:   PetscMalloc1(m,&aij->ilen);

4606:   aij->i            = i;
4607:   aij->j            = j;
4608:   aij->a            = a;
4609:   aij->singlemalloc = PETSC_FALSE;
4610:   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
4611:   aij->free_a       = PETSC_FALSE;
4612:   aij->free_ij      = PETSC_FALSE;

4614:   for (ii=0; ii<m; ii++) {
4615:     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
4616: #if defined(PETSC_USE_DEBUG)
4617:     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]);
4618:     for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
4619:       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);
4620:       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);
4621:     }
4622: #endif
4623:   }
4624: #if defined(PETSC_USE_DEBUG)
4625:   for (ii=0; ii<aij->i[m]; ii++) {
4626:     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %D index = %D",ii,j[ii]);
4627:     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]);
4628:   }
4629: #endif

4631:   MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);
4632:   MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);
4633:   return(0);
4634: }
4635: /*@C
4636:      MatCreateSeqAIJFromTriple - Creates an sequential AIJ matrix using matrix elements (in COO format)
4637:               provided by the user.

4639:       Collective

4641:    Input Parameters:
4642: +   comm - must be an MPI communicator of size 1
4643: .   m   - number of rows
4644: .   n   - number of columns
4645: .   i   - row indices
4646: .   j   - column indices
4647: .   a   - matrix values
4648: .   nz  - number of nonzeros
4649: -   idx - 0 or 1 based

4651:    Output Parameter:
4652: .   mat - the matrix

4654:    Level: intermediate

4656:    Notes:
4657:        The i and j indices are 0 based

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

4663:         1 0 0
4664:         2 0 3
4665:         4 5 6

4667:         i =  {0,1,1,2,2,2}
4668:         j =  {0,0,2,0,1,2}
4669:         v =  {1,2,3,4,5,6}


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

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


4682:   PetscCalloc1(m,&nnz);
4683:   for (ii = 0; ii < nz; ii++) {
4684:     nnz[i[ii] - !!idx] += 1;
4685:   }
4686:   MatCreate(comm,mat);
4687:   MatSetSizes(*mat,m,n,m,n);
4688:   MatSetType(*mat,MATSEQAIJ);
4689:   MatSeqAIJSetPreallocation_SeqAIJ(*mat,0,nnz);
4690:   for (ii = 0; ii < nz; ii++) {
4691:     if (idx) {
4692:       row = i[ii] - 1;
4693:       col = j[ii] - 1;
4694:     } else {
4695:       row = i[ii];
4696:       col = j[ii];
4697:     }
4698:     MatSetValues(*mat,one,&row,one,&col,&a[ii],ADD_VALUES);
4699:   }
4700:   MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);
4701:   MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);
4702:   PetscFree(nnz);
4703:   return(0);
4704: }

4706: PetscErrorCode MatSeqAIJInvalidateDiagonal(Mat A)
4707: {
4708:   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data;

4712:   a->idiagvalid  = PETSC_FALSE;
4713:   a->ibdiagvalid = PETSC_FALSE;

4715:   MatSeqAIJInvalidateDiagonal_Inode(A);
4716:   return(0);
4717: }

4719: PetscErrorCode MatCreateMPIMatConcatenateSeqMat_SeqAIJ(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat)
4720: {
4722:   PetscMPIInt    size;

4725:   MPI_Comm_size(comm,&size);
4726:   if (size == 1) {
4727:     if (scall == MAT_INITIAL_MATRIX) {
4728:       MatDuplicate(inmat,MAT_COPY_VALUES,outmat);
4729:     } else {
4730:       MatCopy(inmat,*outmat,SAME_NONZERO_PATTERN);
4731:     }
4732:   } else {
4733:     MatCreateMPIMatConcatenateSeqMat_MPIAIJ(comm,inmat,n,scall,outmat);
4734:   }
4735:   return(0);
4736: }

4738: /*
4739:  Permute A into C's *local* index space using rowemb,colemb.
4740:  The embedding are supposed to be injections and the above implies that the range of rowemb is a subset
4741:  of [0,m), colemb is in [0,n).
4742:  If pattern == DIFFERENT_NONZERO_PATTERN, C is preallocated according to A.
4743:  */
4744: PetscErrorCode MatSetSeqMat_SeqAIJ(Mat C,IS rowemb,IS colemb,MatStructure pattern,Mat B)
4745: {
4746:   /* If making this function public, change the error returned in this function away from _PLIB. */
4748:   Mat_SeqAIJ     *Baij;
4749:   PetscBool      seqaij;
4750:   PetscInt       m,n,*nz,i,j,count;
4751:   PetscScalar    v;
4752:   const PetscInt *rowindices,*colindices;

4755:   if (!B) return(0);
4756:   /* Check to make sure the target matrix (and embeddings) are compatible with C and each other. */
4757:   PetscObjectBaseTypeCompare((PetscObject)B,MATSEQAIJ,&seqaij);
4758:   if (!seqaij) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is of wrong type");
4759:   if (rowemb) {
4760:     ISGetLocalSize(rowemb,&m);
4761:     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);
4762:   } else {
4763:     if (C->rmap->n != B->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is row-incompatible with the target matrix");
4764:   }
4765:   if (colemb) {
4766:     ISGetLocalSize(colemb,&n);
4767:     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);
4768:   } else {
4769:     if (C->cmap->n != B->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is col-incompatible with the target matrix");
4770:   }

4772:   Baij = (Mat_SeqAIJ*)(B->data);
4773:   if (pattern == DIFFERENT_NONZERO_PATTERN) {
4774:     PetscMalloc1(B->rmap->n,&nz);
4775:     for (i=0; i<B->rmap->n; i++) {
4776:       nz[i] = Baij->i[i+1] - Baij->i[i];
4777:     }
4778:     MatSeqAIJSetPreallocation(C,0,nz);
4779:     PetscFree(nz);
4780:   }
4781:   if (pattern == SUBSET_NONZERO_PATTERN) {
4782:     MatZeroEntries(C);
4783:   }
4784:   count = 0;
4785:   rowindices = NULL;
4786:   colindices = NULL;
4787:   if (rowemb) {
4788:     ISGetIndices(rowemb,&rowindices);
4789:   }
4790:   if (colemb) {
4791:     ISGetIndices(colemb,&colindices);
4792:   }
4793:   for (i=0; i<B->rmap->n; i++) {
4794:     PetscInt row;
4795:     row = i;
4796:     if (rowindices) row = rowindices[i];
4797:     for (j=Baij->i[i]; j<Baij->i[i+1]; j++) {
4798:       PetscInt col;
4799:       col  = Baij->j[count];
4800:       if (colindices) col = colindices[col];
4801:       v    = Baij->a[count];
4802:       MatSetValues(C,1,&row,1,&col,&v,INSERT_VALUES);
4803:       ++count;
4804:     }
4805:   }
4806:   /* FIXME: set C's nonzerostate correctly. */
4807:   /* Assembly for C is necessary. */
4808:   C->preallocated = PETSC_TRUE;
4809:   C->assembled     = PETSC_TRUE;
4810:   C->was_assembled = PETSC_FALSE;
4811:   return(0);
4812: }

4814: PetscFunctionList MatSeqAIJList = NULL;

4816: /*@C
4817:    MatSeqAIJSetType - Converts a MATSEQAIJ matrix to a subtype

4819:    Collective on Mat

4821:    Input Parameters:
4822: +  mat      - the matrix object
4823: -  matype   - matrix type

4825:    Options Database Key:
4826: .  -mat_seqai_type  <method> - for example seqaijcrl


4829:   Level: intermediate

4831: .seealso: PCSetType(), VecSetType(), MatCreate(), MatType, Mat
4832: @*/
4833: PetscErrorCode  MatSeqAIJSetType(Mat mat, MatType matype)
4834: {
4835:   PetscErrorCode ierr,(*r)(Mat,MatType,MatReuse,Mat*);
4836:   PetscBool      sametype;

4840:   PetscObjectTypeCompare((PetscObject)mat,matype,&sametype);
4841:   if (sametype) return(0);

4843:    PetscFunctionListFind(MatSeqAIJList,matype,&r);
4844:   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown Mat type given: %s",matype);
4845:   (*r)(mat,matype,MAT_INPLACE_MATRIX,&mat);
4846:   return(0);
4847: }


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

4853:    Not Collective

4855:    Input Parameters:
4856: +  name - name of a new user-defined matrix type, for example MATSEQAIJCRL
4857: -  function - routine to convert to subtype

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


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

4866:    Level: advanced

4868: .seealso: MatSeqAIJRegisterAll()


4871:   Level: advanced
4872: @*/
4873: PetscErrorCode  MatSeqAIJRegister(const char sname[],PetscErrorCode (*function)(Mat,MatType,MatReuse,Mat *))
4874: {

4878:   MatInitializePackage();
4879:   PetscFunctionListAdd(&MatSeqAIJList,sname,function);
4880:   return(0);
4881: }

4883: PetscBool MatSeqAIJRegisterAllCalled = PETSC_FALSE;

4885: /*@C
4886:   MatSeqAIJRegisterAll - Registers all of the matrix subtypes of SeqAIJ

4888:   Not Collective

4890:   Level: advanced

4892:   Developers Note: CUSP and CUSPARSE do not yet support the  MatConvert_SeqAIJ..() paradigm and thus cannot be registered here

4894: .seealso:  MatRegisterAll(), MatSeqAIJRegister()
4895: @*/
4896: PetscErrorCode  MatSeqAIJRegisterAll(void)
4897: {

4901:   if (MatSeqAIJRegisterAllCalled) return(0);
4902:   MatSeqAIJRegisterAllCalled = PETSC_TRUE;

4904:   MatSeqAIJRegister(MATSEQAIJCRL,      MatConvert_SeqAIJ_SeqAIJCRL);
4905:   MatSeqAIJRegister(MATSEQAIJPERM,     MatConvert_SeqAIJ_SeqAIJPERM);
4906:   MatSeqAIJRegister(MATSEQAIJSELL,     MatConvert_SeqAIJ_SeqAIJSELL);
4907: #if defined(PETSC_HAVE_MKL_SPARSE)
4908:   MatSeqAIJRegister(MATSEQAIJMKL,      MatConvert_SeqAIJ_SeqAIJMKL);
4909: #endif
4910: #if defined(PETSC_HAVE_VIENNACL) && defined(PETSC_HAVE_VIENNACL_NO_CUDA)
4911:   MatSeqAIJRegister(MATMPIAIJVIENNACL, MatConvert_SeqAIJ_SeqAIJViennaCL);
4912: #endif
4913:   return(0);
4914: }

4916: /*
4917:     Special version for direct calls from Fortran
4918: */
4919:  #include <petsc/private/fortranimpl.h>
4920: #if defined(PETSC_HAVE_FORTRAN_CAPS)
4921: #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
4922: #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
4923: #define matsetvaluesseqaij_ matsetvaluesseqaij
4924: #endif

4926: /* Change these macros so can be used in void function */
4927: #undef CHKERRQ
4928: #define CHKERRQ(ierr) CHKERRABORT(PetscObjectComm((PetscObject)A),ierr)
4929: #undef SETERRQ2
4930: #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
4931: #undef SETERRQ3
4932: #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr)

4934: PETSC_EXTERN void PETSC_STDCALL matsetvaluesseqaij_(Mat *AA,PetscInt *mm,const PetscInt im[],PetscInt *nn,const PetscInt in[],const PetscScalar v[],InsertMode *isis, PetscErrorCode *_ierr)
4935: {
4936:   Mat            A  = *AA;
4937:   PetscInt       m  = *mm, n = *nn;
4938:   InsertMode     is = *isis;
4939:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
4940:   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
4941:   PetscInt       *imax,*ai,*ailen;
4943:   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
4944:   MatScalar      *ap,value,*aa;
4945:   PetscBool      ignorezeroentries = a->ignorezeroentries;
4946:   PetscBool      roworiented       = a->roworiented;

4949:   MatCheckPreallocated(A,1);
4950:   imax  = a->imax;
4951:   ai    = a->i;
4952:   ailen = a->ilen;
4953:   aj    = a->j;
4954:   aa    = a->a;

4956:   for (k=0; k<m; k++) { /* loop over added rows */
4957:     row = im[k];
4958:     if (row < 0) continue;
4959: #if defined(PETSC_USE_DEBUG)
4960:     if (row >= A->rmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
4961: #endif
4962:     rp   = aj + ai[row]; ap = aa + ai[row];
4963:     rmax = imax[row]; nrow = ailen[row];
4964:     low  = 0;
4965:     high = nrow;
4966:     for (l=0; l<n; l++) { /* loop over added columns */
4967:       if (in[l] < 0) continue;
4968: #if defined(PETSC_USE_DEBUG)
4969:       if (in[l] >= A->cmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
4970: #endif
4971:       col = in[l];
4972:       if (roworiented) value = v[l + k*n];
4973:       else value = v[k + l*m];

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

4977:       if (col <= lastcol) low = 0;
4978:       else high = nrow;
4979:       lastcol = col;
4980:       while (high-low > 5) {
4981:         t = (low+high)/2;
4982:         if (rp[t] > col) high = t;
4983:         else             low  = t;
4984:       }
4985:       for (i=low; i<high; i++) {
4986:         if (rp[i] > col) break;
4987:         if (rp[i] == col) {
4988:           if (is == ADD_VALUES) ap[i] += value;
4989:           else                  ap[i] = value;
4990:           goto noinsert;
4991:         }
4992:       }
4993:       if (value == 0.0 && ignorezeroentries) goto noinsert;
4994:       if (nonew == 1) goto noinsert;
4995:       if (nonew == -1) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
4996:       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
4997:       N = nrow++ - 1; a->nz++; high++;
4998:       /* shift up all the later entries in this row */
4999:       for (ii=N; ii>=i; ii--) {
5000:         rp[ii+1] = rp[ii];
5001:         ap[ii+1] = ap[ii];
5002:       }
5003:       rp[i] = col;
5004:       ap[i] = value;
5005:       A->nonzerostate++;
5006: noinsert:;
5007:       low = i + 1;
5008:     }
5009:     ailen[row] = nrow;
5010:   }
5011:   PetscFunctionReturnVoid();
5012: }