Actual source code: aij.c

petsc-master 2018-05-19
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:   PetscMemzero(norms,n*sizeof(PetscReal));
 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+1,&collengths);
261:     PetscMalloc1(n+1,&cia);
262:     PetscMalloc1(nz+1,&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:     PetscMemzero(collengths,n*sizeof(PetscInt));
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,*jj,mr,col;
310:   PetscInt       *cspidx;

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

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

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

349:   MatRestoreColumnIJ_SeqAIJ(A,oshift,symmetric,inodecompressed,n,ia,ja,done);
350:   PetscFree(*spidx);
351:   return(0);
352: }

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

361:   PetscMemcpy(a->a+ai[row],v,(ai[row+1]-ai[row])*sizeof(PetscScalar));
362:   return(0);
363: }

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

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

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

375: */

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

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

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

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

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

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


501: PetscErrorCode MatGetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
502: {
503:   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
504:   PetscInt   *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
505:   PetscInt   *ai = a->i,*ailen = a->ilen;
506:   MatScalar  *ap,*aa = a->a;

509:   for (k=0; k<m; k++) { /* loop over rows */
510:     row = im[k];
511:     if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */
512:     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);
513:     rp   = aj + ai[row]; ap = aa + ai[row];
514:     nrow = ailen[row];
515:     for (l=0; l<n; l++) { /* loop over columns */
516:       if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */
517:       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);
518:       col  = in[l];
519:       high = nrow; low = 0; /* assume unsorted */
520:       while (high-low > 5) {
521:         t = (low+high)/2;
522:         if (rp[t] > col) high = t;
523:         else low = t;
524:       }
525:       for (i=low; i<high; i++) {
526:         if (rp[i] > col) break;
527:         if (rp[i] == col) {
528:           *v++ = ap[i];
529:           goto finished;
530:         }
531:       }
532:       *v++ = 0.0;
533: finished:;
534:     }
535:   }
536:   return(0);
537: }


540: PetscErrorCode MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer)
541: {
542:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
544:   PetscInt       i,*col_lens;
545:   int            fd;
546:   FILE           *file;

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

552:   col_lens[0] = MAT_FILE_CLASSID;
553:   col_lens[1] = A->rmap->n;
554:   col_lens[2] = A->cmap->n;
555:   col_lens[3] = a->nz;

557:   /* store lengths of each row and write (including header) to file */
558:   for (i=0; i<A->rmap->n; i++) {
559:     col_lens[4+i] = a->i[i+1] - a->i[i];
560:   }
561:   PetscBinaryWrite(fd,col_lens,4+A->rmap->n,PETSC_INT,PETSC_TRUE);
562:   PetscFree(col_lens);

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

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

570:   PetscViewerBinaryGetInfoPointer(viewer,&file);
571:   if (file) {
572:     fprintf(file,"-matload_block_size %d\n",(int)PetscAbs(A->rmap->bs));
573:   }
574:   return(0);
575: }

577: static PetscErrorCode MatView_SeqAIJ_ASCII_structonly(Mat A,PetscViewer viewer)
578: {
580:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
581:   PetscInt       i,k,m=A->rmap->N;

584:   PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
585:   for (i=0; i<m; i++) {
586:     PetscViewerASCIIPrintf(viewer,"row %D:",i);
587:     for (k=a->i[i]; k<a->i[i+1]; k++) {
588:       PetscViewerASCIIPrintf(viewer," (%D) ",a->j[k]);
589:     }
590:     PetscViewerASCIIPrintf(viewer,"\n");
591:   }
592:   PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
593:   return(0);
594: }

596: extern PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);

598: PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer)
599: {
600:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
601:   PetscErrorCode    ierr;
602:   PetscInt          i,j,m = A->rmap->n;
603:   const char        *name;
604:   PetscViewerFormat format;

607:   if (A->structure_only) {
608:     MatView_SeqAIJ_ASCII_structonly(A,viewer);
609:     return(0);
610:   }

612:   PetscViewerGetFormat(viewer,&format);
613:   if (format == PETSC_VIEWER_ASCII_MATLAB) {
614:     PetscInt nofinalvalue = 0;
615:     if (m && ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->cmap->n-1))) {
616:       /* Need a dummy value to ensure the dimension of the matrix. */
617:       nofinalvalue = 1;
618:     }
619:     PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
620:     PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->cmap->n);
621:     PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);
622: #if defined(PETSC_USE_COMPLEX)
623:     PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,4);\n",a->nz+nofinalvalue);
624: #else
625:     PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);
626: #endif
627:     PetscViewerASCIIPrintf(viewer,"zzz = [\n");

629:     for (i=0; i<m; i++) {
630:       for (j=a->i[i]; j<a->i[i+1]; j++) {
631: #if defined(PETSC_USE_COMPLEX)
632:         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]));
633: #else
634:         PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",i+1,a->j[j]+1,(double)a->a[j]);
635: #endif
636:       }
637:     }
638:     if (nofinalvalue) {
639: #if defined(PETSC_USE_COMPLEX)
640:       PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e %18.16e\n",m,A->cmap->n,0.,0.);
641: #else
642:       PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",m,A->cmap->n,0.0);
643: #endif
644:     }
645:     PetscObjectGetName((PetscObject)A,&name);
646:     PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);
647:     PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
648:   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO) {
649:     return(0);
650:   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
651:     PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
652:     for (i=0; i<m; i++) {
653:       PetscViewerASCIIPrintf(viewer,"row %D:",i);
654:       for (j=a->i[i]; j<a->i[i+1]; j++) {
655: #if defined(PETSC_USE_COMPLEX)
656:         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
657:           PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));
658:         } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
659:           PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)-PetscImaginaryPart(a->a[j]));
660:         } else if (PetscRealPart(a->a[j]) != 0.0) {
661:           PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));
662:         }
663: #else
664:         if (a->a[j] != 0.0) {PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);}
665: #endif
666:       }
667:       PetscViewerASCIIPrintf(viewer,"\n");
668:     }
669:     PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
670:   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
671:     PetscInt nzd=0,fshift=1,*sptr;
672:     PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
673:     PetscMalloc1(m+1,&sptr);
674:     for (i=0; i<m; i++) {
675:       sptr[i] = nzd+1;
676:       for (j=a->i[i]; j<a->i[i+1]; j++) {
677:         if (a->j[j] >= i) {
678: #if defined(PETSC_USE_COMPLEX)
679:           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
680: #else
681:           if (a->a[j] != 0.0) nzd++;
682: #endif
683:         }
684:       }
685:     }
686:     sptr[m] = nzd+1;
687:     PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);
688:     for (i=0; i<m+1; i+=6) {
689:       if (i+4<m) {
690:         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]);
691:       } else if (i+3<m) {
692:         PetscViewerASCIIPrintf(viewer," %D %D %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3],sptr[i+4]);
693:       } else if (i+2<m) {
694:         PetscViewerASCIIPrintf(viewer," %D %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3]);
695:       } else if (i+1<m) {
696:         PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);
697:       } else if (i<m) {
698:         PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);
699:       } else {
700:         PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);
701:       }
702:     }
703:     PetscViewerASCIIPrintf(viewer,"\n");
704:     PetscFree(sptr);
705:     for (i=0; i<m; i++) {
706:       for (j=a->i[i]; j<a->i[i+1]; j++) {
707:         if (a->j[j] >= i) {PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);}
708:       }
709:       PetscViewerASCIIPrintf(viewer,"\n");
710:     }
711:     PetscViewerASCIIPrintf(viewer,"\n");
712:     for (i=0; i<m; i++) {
713:       for (j=a->i[i]; j<a->i[i+1]; j++) {
714:         if (a->j[j] >= i) {
715: #if defined(PETSC_USE_COMPLEX)
716:           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) {
717:             PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));
718:           }
719: #else
720:           if (a->a[j] != 0.0) {PetscViewerASCIIPrintf(viewer," %18.16e ",(double)a->a[j]);}
721: #endif
722:         }
723:       }
724:       PetscViewerASCIIPrintf(viewer,"\n");
725:     }
726:     PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
727:   } else if (format == PETSC_VIEWER_ASCII_DENSE) {
728:     PetscInt    cnt = 0,jcnt;
729:     PetscScalar value;
730: #if defined(PETSC_USE_COMPLEX)
731:     PetscBool   realonly = PETSC_TRUE;

733:     for (i=0; i<a->i[m]; i++) {
734:       if (PetscImaginaryPart(a->a[i]) != 0.0) {
735:         realonly = PETSC_FALSE;
736:         break;
737:       }
738:     }
739: #endif

741:     PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
742:     for (i=0; i<m; i++) {
743:       jcnt = 0;
744:       for (j=0; j<A->cmap->n; j++) {
745:         if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
746:           value = a->a[cnt++];
747:           jcnt++;
748:         } else {
749:           value = 0.0;
750:         }
751: #if defined(PETSC_USE_COMPLEX)
752:         if (realonly) {
753:           PetscViewerASCIIPrintf(viewer," %7.5e ",(double)PetscRealPart(value));
754:         } else {
755:           PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",(double)PetscRealPart(value),(double)PetscImaginaryPart(value));
756:         }
757: #else
758:         PetscViewerASCIIPrintf(viewer," %7.5e ",(double)value);
759: #endif
760:       }
761:       PetscViewerASCIIPrintf(viewer,"\n");
762:     }
763:     PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
764:   } else if (format == PETSC_VIEWER_ASCII_MATRIXMARKET) {
765:     PetscInt fshift=1;
766:     PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
767: #if defined(PETSC_USE_COMPLEX)
768:     PetscViewerASCIIPrintf(viewer,"%%%%MatrixMarket matrix coordinate complex general\n");
769: #else
770:     PetscViewerASCIIPrintf(viewer,"%%%%MatrixMarket matrix coordinate real general\n");
771: #endif
772:     PetscViewerASCIIPrintf(viewer,"%D %D %D\n", m, A->cmap->n, a->nz);
773:     for (i=0; i<m; i++) {
774:       for (j=a->i[i]; j<a->i[i+1]; j++) {
775: #if defined(PETSC_USE_COMPLEX)
776:         PetscViewerASCIIPrintf(viewer,"%D %D %g %g\n", i+fshift,a->j[j]+fshift,(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));
777: #else
778:         PetscViewerASCIIPrintf(viewer,"%D %D %g\n", i+fshift, a->j[j]+fshift, (double)a->a[j]);
779: #endif
780:       }
781:     }
782:     PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
783:   } else {
784:     PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
785:     if (A->factortype) {
786:       for (i=0; i<m; i++) {
787:         PetscViewerASCIIPrintf(viewer,"row %D:",i);
788:         /* L part */
789:         for (j=a->i[i]; j<a->i[i+1]; j++) {
790: #if defined(PETSC_USE_COMPLEX)
791:           if (PetscImaginaryPart(a->a[j]) > 0.0) {
792:             PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));
793:           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
794:             PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)(-PetscImaginaryPart(a->a[j])));
795:           } else {
796:             PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));
797:           }
798: #else
799:           PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);
800: #endif
801:         }
802:         /* diagonal */
803:         j = a->diag[i];
804: #if defined(PETSC_USE_COMPLEX)
805:         if (PetscImaginaryPart(a->a[j]) > 0.0) {
806:           PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(1.0/a->a[j]),(double)PetscImaginaryPart(1.0/a->a[j]));
807:         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
808:           PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(1.0/a->a[j]),(double)(-PetscImaginaryPart(1.0/a->a[j])));
809:         } else {
810:           PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(1.0/a->a[j]));
811:         }
812: #else
813:         PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)(1.0/a->a[j]));
814: #endif

816:         /* U part */
817:         for (j=a->diag[i+1]+1; j<a->diag[i]; j++) {
818: #if defined(PETSC_USE_COMPLEX)
819:           if (PetscImaginaryPart(a->a[j]) > 0.0) {
820:             PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));
821:           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
822:             PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)(-PetscImaginaryPart(a->a[j])));
823:           } else {
824:             PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));
825:           }
826: #else
827:           PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);
828: #endif
829:         }
830:         PetscViewerASCIIPrintf(viewer,"\n");
831:       }
832:     } else {
833:       for (i=0; i<m; i++) {
834:         PetscViewerASCIIPrintf(viewer,"row %D:",i);
835:         for (j=a->i[i]; j<a->i[i+1]; j++) {
836: #if defined(PETSC_USE_COMPLEX)
837:           if (PetscImaginaryPart(a->a[j]) > 0.0) {
838:             PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));
839:           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
840:             PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)-PetscImaginaryPart(a->a[j]));
841:           } else {
842:             PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));
843:           }
844: #else
845:           PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);
846: #endif
847:         }
848:         PetscViewerASCIIPrintf(viewer,"\n");
849:       }
850:     }
851:     PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
852:   }
853:   PetscViewerFlush(viewer);
854:   return(0);
855: }

857:  #include <petscdraw.h>
858: PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
859: {
860:   Mat               A  = (Mat) Aa;
861:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
862:   PetscErrorCode    ierr;
863:   PetscInt          i,j,m = A->rmap->n;
864:   int               color;
865:   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r;
866:   PetscViewer       viewer;
867:   PetscViewerFormat format;

870:   PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);
871:   PetscViewerGetFormat(viewer,&format);
872:   PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);

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

876:   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
877:     PetscDrawCollectiveBegin(draw);
878:     /* Blue for negative, Cyan for zero and  Red for positive */
879:     color = PETSC_DRAW_BLUE;
880:     for (i=0; i<m; i++) {
881:       y_l = m - i - 1.0; y_r = y_l + 1.0;
882:       for (j=a->i[i]; j<a->i[i+1]; j++) {
883:         x_l = a->j[j]; x_r = x_l + 1.0;
884:         if (PetscRealPart(a->a[j]) >=  0.) continue;
885:         PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);
886:       }
887:     }
888:     color = PETSC_DRAW_CYAN;
889:     for (i=0; i<m; i++) {
890:       y_l = m - i - 1.0; y_r = y_l + 1.0;
891:       for (j=a->i[i]; j<a->i[i+1]; j++) {
892:         x_l = a->j[j]; x_r = x_l + 1.0;
893:         if (a->a[j] !=  0.) continue;
894:         PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);
895:       }
896:     }
897:     color = PETSC_DRAW_RED;
898:     for (i=0; i<m; i++) {
899:       y_l = m - i - 1.0; y_r = y_l + 1.0;
900:       for (j=a->i[i]; j<a->i[i+1]; j++) {
901:         x_l = a->j[j]; x_r = x_l + 1.0;
902:         if (PetscRealPart(a->a[j]) <=  0.) continue;
903:         PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);
904:       }
905:     }
906:     PetscDrawCollectiveEnd(draw);
907:   } else {
908:     /* use contour shading to indicate magnitude of values */
909:     /* first determine max of all nonzero values */
910:     PetscReal minv = 0.0, maxv = 0.0;
911:     PetscInt  nz = a->nz, count = 0;
912:     PetscDraw popup;

914:     for (i=0; i<nz; i++) {
915:       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
916:     }
917:     if (minv >= maxv) maxv = minv + PETSC_SMALL;
918:     PetscDrawGetPopup(draw,&popup);
919:     PetscDrawScalePopup(popup,minv,maxv);

921:     PetscDrawCollectiveBegin(draw);
922:     for (i=0; i<m; i++) {
923:       y_l = m - i - 1.0;
924:       y_r = y_l + 1.0;
925:       for (j=a->i[i]; j<a->i[i+1]; j++) {
926:         x_l = a->j[j];
927:         x_r = x_l + 1.0;
928:         color = PetscDrawRealToColor(PetscAbsScalar(a->a[count]),minv,maxv);
929:         PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);
930:         count++;
931:       }
932:     }
933:     PetscDrawCollectiveEnd(draw);
934:   }
935:   return(0);
936: }

938:  #include <petscdraw.h>
939: PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
940: {
942:   PetscDraw      draw;
943:   PetscReal      xr,yr,xl,yl,h,w;
944:   PetscBool      isnull;

947:   PetscViewerDrawGetDraw(viewer,0,&draw);
948:   PetscDrawIsNull(draw,&isnull);
949:   if (isnull) return(0);

951:   xr   = A->cmap->n; yr  = A->rmap->n; h = yr/10.0; w = xr/10.0;
952:   xr  += w;          yr += h;         xl = -w;     yl = -h;
953:   PetscDrawSetCoordinates(draw,xl,yl,xr,yr);
954:   PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);
955:   PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);
956:   PetscObjectCompose((PetscObject)A,"Zoomviewer",NULL);
957:   PetscDrawSave(draw);
958:   return(0);
959: }

961: PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer)
962: {
964:   PetscBool      iascii,isbinary,isdraw;

967:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
968:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
969:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
970:   if (iascii) {
971:     MatView_SeqAIJ_ASCII(A,viewer);
972:   } else if (isbinary) {
973:     MatView_SeqAIJ_Binary(A,viewer);
974:   } else if (isdraw) {
975:     MatView_SeqAIJ_Draw(A,viewer);
976:   }
977:   MatView_SeqAIJ_Inode(A,viewer);
978:   return(0);
979: }

981: PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
982: {
983:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
985:   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
986:   PetscInt       m      = A->rmap->n,*ip,N,*ailen = a->ilen,rmax = 0;
987:   MatScalar      *aa    = a->a,*ap;
988:   PetscReal      ratio  = 0.6;

991:   if (mode == MAT_FLUSH_ASSEMBLY) return(0);

993:   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
994:   for (i=1; i<m; i++) {
995:     /* move each row back by the amount of empty slots (fshift) before it*/
996:     fshift += imax[i-1] - ailen[i-1];
997:     rmax    = PetscMax(rmax,ailen[i]);
998:     if (fshift) {
999:       ip = aj + ai[i];
1000:       ap = aa + ai[i];
1001:       N  = ailen[i];
1002:       for (j=0; j<N; j++) {
1003:         ip[j-fshift] = ip[j];
1004:         if (!A->structure_only) ap[j-fshift] = ap[j];
1005:       }
1006:     }
1007:     ai[i] = ai[i-1] + ailen[i-1];
1008:   }
1009:   if (m) {
1010:     fshift += imax[m-1] - ailen[m-1];
1011:     ai[m]   = ai[m-1] + ailen[m-1];
1012:   }

1014:   /* reset ilen and imax for each row */
1015:   a->nonzerorowcnt = 0;
1016:   if (A->structure_only) {
1017:     PetscFree2(a->imax,a->ilen);
1018:   } else { /* !A->structure_only */
1019:     for (i=0; i<m; i++) {
1020:       ailen[i] = imax[i] = ai[i+1] - ai[i];
1021:       a->nonzerorowcnt += ((ai[i+1] - ai[i]) > 0);
1022:     }
1023:   }
1024:   a->nz = ai[m];
1025:   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);

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

1032:   A->info.mallocs    += a->reallocs;
1033:   a->reallocs         = 0;
1034:   A->info.nz_unneeded = (PetscReal)fshift;
1035:   a->rmax             = rmax;

1037:   if (!A->structure_only) {
1038:     MatCheckCompressedRow(A,a->nonzerorowcnt,&a->compressedrow,a->i,m,ratio);
1039:   }
1040:   MatAssemblyEnd_SeqAIJ_Inode(A,mode);
1041:   MatSeqAIJInvalidateDiagonal(A);
1042:   return(0);
1043: }

1045: PetscErrorCode MatRealPart_SeqAIJ(Mat A)
1046: {
1047:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1048:   PetscInt       i,nz = a->nz;
1049:   MatScalar      *aa = a->a;

1053:   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
1054:   MatSeqAIJInvalidateDiagonal(A);
1055:   return(0);
1056: }

1058: PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
1059: {
1060:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1061:   PetscInt       i,nz = a->nz;
1062:   MatScalar      *aa = a->a;

1066:   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
1067:   MatSeqAIJInvalidateDiagonal(A);
1068:   return(0);
1069: }

1071: PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
1072: {
1073:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;

1077:   PetscMemzero(a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));
1078:   MatSeqAIJInvalidateDiagonal(A);
1079:   return(0);
1080: }

1082: PetscErrorCode MatDestroy_SeqAIJ(Mat A)
1083: {
1084:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;

1088: #if defined(PETSC_USE_LOG)
1089:   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz);
1090: #endif
1091:   MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);
1092:   ISDestroy(&a->row);
1093:   ISDestroy(&a->col);
1094:   PetscFree(a->diag);
1095:   PetscFree(a->ibdiag);
1096:   PetscFree2(a->imax,a->ilen);
1097:   PetscFree(a->ipre);
1098:   PetscFree3(a->idiag,a->mdiag,a->ssor_work);
1099:   PetscFree(a->solve_work);
1100:   ISDestroy(&a->icol);
1101:   PetscFree(a->saved_values);
1102:   ISColoringDestroy(&a->coloring);
1103:   PetscFree2(a->compressedrow.i,a->compressedrow.rindex);
1104:   PetscFree(a->matmult_abdense);

1106:   MatDestroy_SeqAIJ_Inode(A);
1107:   PetscFree(A->data);

1109:   PetscObjectChangeTypeName((PetscObject)A,0);
1110:   PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetColumnIndices_C",NULL);
1111:   PetscObjectComposeFunction((PetscObject)A,"MatStoreValues_C",NULL);
1112:   PetscObjectComposeFunction((PetscObject)A,"MatRetrieveValues_C",NULL);
1113:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqsbaij_C",NULL);
1114:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqbaij_C",NULL);
1115:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqaijperm_C",NULL);
1116: #if defined(PETSC_HAVE_ELEMENTAL)
1117:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_elemental_C",NULL);
1118: #endif
1119: #if defined(PETSC_HAVE_HYPRE)
1120:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_hypre_C",NULL);
1121:   PetscObjectComposeFunction((PetscObject)A,"MatMatMatMult_transpose_seqaij_seqaij_C",NULL);
1122: #endif
1123:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqdense_C",NULL);
1124:   PetscObjectComposeFunction((PetscObject)A,"MatIsTranspose_C",NULL);
1125:   PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocation_C",NULL);
1126:   PetscObjectComposeFunction((PetscObject)A,"MatResetPreallocation_C",NULL);
1127:   PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C",NULL);
1128:   PetscObjectComposeFunction((PetscObject)A,"MatReorderForNonzeroDiagonal_C",NULL);
1129:   PetscObjectComposeFunction((PetscObject)A,"MatPtAP_is_seqaij_C",NULL);
1130:   return(0);
1131: }

1133: PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscBool flg)
1134: {
1135:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;

1139:   switch (op) {
1140:   case MAT_ROW_ORIENTED:
1141:     a->roworiented = flg;
1142:     break;
1143:   case MAT_KEEP_NONZERO_PATTERN:
1144:     a->keepnonzeropattern = flg;
1145:     break;
1146:   case MAT_NEW_NONZERO_LOCATIONS:
1147:     a->nonew = (flg ? 0 : 1);
1148:     break;
1149:   case MAT_NEW_NONZERO_LOCATION_ERR:
1150:     a->nonew = (flg ? -1 : 0);
1151:     break;
1152:   case MAT_NEW_NONZERO_ALLOCATION_ERR:
1153:     a->nonew = (flg ? -2 : 0);
1154:     break;
1155:   case MAT_UNUSED_NONZERO_LOCATION_ERR:
1156:     a->nounused = (flg ? -1 : 0);
1157:     break;
1158:   case MAT_IGNORE_ZERO_ENTRIES:
1159:     a->ignorezeroentries = flg;
1160:     break;
1161:   case MAT_SPD:
1162:   case MAT_SYMMETRIC:
1163:   case MAT_STRUCTURALLY_SYMMETRIC:
1164:   case MAT_HERMITIAN:
1165:   case MAT_SYMMETRY_ETERNAL:
1166:   case MAT_STRUCTURE_ONLY:
1167:     /* These options are handled directly by MatSetOption() */
1168:     break;
1169:   case MAT_NEW_DIAGONALS:
1170:   case MAT_IGNORE_OFF_PROC_ENTRIES:
1171:   case MAT_USE_HASH_TABLE:
1172:     PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);
1173:     break;
1174:   case MAT_USE_INODES:
1175:     /* Not an error because MatSetOption_SeqAIJ_Inode handles this one */
1176:     break;
1177:   case MAT_SUBMAT_SINGLEIS:
1178:     A->submat_singleis = flg;
1179:     break;
1180:   default:
1181:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
1182:   }
1183:   MatSetOption_SeqAIJ_Inode(A,op,flg);
1184:   return(0);
1185: }

1187: PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
1188: {
1189:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1191:   PetscInt       i,j,n,*ai=a->i,*aj=a->j,nz;
1192:   PetscScalar    *aa=a->a,*x,zero=0.0;

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

1198:   if (A->factortype == MAT_FACTOR_ILU || A->factortype == MAT_FACTOR_LU) {
1199:     PetscInt *diag=a->diag;
1200:     VecGetArray(v,&x);
1201:     for (i=0; i<n; i++) x[i] = 1.0/aa[diag[i]];
1202:     VecRestoreArray(v,&x);
1203:     return(0);
1204:   }

1206:   VecSet(v,zero);
1207:   VecGetArray(v,&x);
1208:   for (i=0; i<n; i++) {
1209:     nz = ai[i+1] - ai[i];
1210:     if (!nz) x[i] = 0.0;
1211:     for (j=ai[i]; j<ai[i+1]; j++) {
1212:       if (aj[j] == i) {
1213:         x[i] = aa[j];
1214:         break;
1215:       }
1216:     }
1217:   }
1218:   VecRestoreArray(v,&x);
1219:   return(0);
1220: }

1222: #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
1223: PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
1224: {
1225:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1226:   PetscScalar       *y;
1227:   const PetscScalar *x;
1228:   PetscErrorCode    ierr;
1229:   PetscInt          m = A->rmap->n;
1230: #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1231:   const MatScalar   *v;
1232:   PetscScalar       alpha;
1233:   PetscInt          n,i,j;
1234:   const PetscInt    *idx,*ii,*ridx=NULL;
1235:   Mat_CompressedRow cprow    = a->compressedrow;
1236:   PetscBool         usecprow = cprow.use;
1237: #endif

1240:   if (zz != yy) {VecCopy(zz,yy);}
1241:   VecGetArrayRead(xx,&x);
1242:   VecGetArray(yy,&y);

1244: #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1245:   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
1246: #else
1247:   if (usecprow) {
1248:     m    = cprow.nrows;
1249:     ii   = cprow.i;
1250:     ridx = cprow.rindex;
1251:   } else {
1252:     ii = a->i;
1253:   }
1254:   for (i=0; i<m; i++) {
1255:     idx = a->j + ii[i];
1256:     v   = a->a + ii[i];
1257:     n   = ii[i+1] - ii[i];
1258:     if (usecprow) {
1259:       alpha = x[ridx[i]];
1260:     } else {
1261:       alpha = x[i];
1262:     }
1263:     for (j=0; j<n; j++) y[idx[j]] += alpha*v[j];
1264:   }
1265: #endif
1266:   PetscLogFlops(2.0*a->nz);
1267:   VecRestoreArrayRead(xx,&x);
1268:   VecRestoreArray(yy,&y);
1269:   return(0);
1270: }

1272: PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
1273: {

1277:   VecSet(yy,0.0);
1278:   MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);
1279:   return(0);
1280: }

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

1284: PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
1285: {
1286:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1287:   PetscScalar       *y;
1288:   const PetscScalar *x;
1289:   const MatScalar   *aa;
1290:   PetscErrorCode    ierr;
1291:   PetscInt          m=A->rmap->n;
1292:   const PetscInt    *aj,*ii,*ridx=NULL;
1293:   PetscInt          n,i;
1294:   PetscScalar       sum;
1295:   PetscBool         usecprow=a->compressedrow.use;

1297: #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
1298: #pragma disjoint(*x,*y,*aa)
1299: #endif

1302:   VecGetArrayRead(xx,&x);
1303:   VecGetArray(yy,&y);
1304:   ii   = a->i;
1305:   if (usecprow) { /* use compressed row format */
1306:     PetscMemzero(y,m*sizeof(PetscScalar));
1307:     m    = a->compressedrow.nrows;
1308:     ii   = a->compressedrow.i;
1309:     ridx = a->compressedrow.rindex;
1310:     for (i=0; i<m; i++) {
1311:       n           = ii[i+1] - ii[i];
1312:       aj          = a->j + ii[i];
1313:       aa          = a->a + ii[i];
1314:       sum         = 0.0;
1315:       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1316:       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
1317:       y[*ridx++] = sum;
1318:     }
1319:   } else { /* do not use compressed row format */
1320: #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
1321:     aj   = a->j;
1322:     aa   = a->a;
1323:     fortranmultaij_(&m,x,ii,aj,aa,y);
1324: #else
1325:     for (i=0; i<m; i++) {
1326:       n           = ii[i+1] - ii[i];
1327:       aj          = a->j + ii[i];
1328:       aa          = a->a + ii[i];
1329:       sum         = 0.0;
1330:       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1331:       y[i] = sum;
1332:     }
1333: #endif
1334:   }
1335:   PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);
1336:   VecRestoreArrayRead(xx,&x);
1337:   VecRestoreArray(yy,&y);
1338:   return(0);
1339: }

1341: PetscErrorCode MatMultMax_SeqAIJ(Mat A,Vec xx,Vec yy)
1342: {
1343:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1344:   PetscScalar       *y;
1345:   const PetscScalar *x;
1346:   const MatScalar   *aa;
1347:   PetscErrorCode    ierr;
1348:   PetscInt          m=A->rmap->n;
1349:   const PetscInt    *aj,*ii,*ridx=NULL;
1350:   PetscInt          n,i,nonzerorow=0;
1351:   PetscScalar       sum;
1352:   PetscBool         usecprow=a->compressedrow.use;

1354: #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
1355: #pragma disjoint(*x,*y,*aa)
1356: #endif

1359:   VecGetArrayRead(xx,&x);
1360:   VecGetArray(yy,&y);
1361:   if (usecprow) { /* use compressed row format */
1362:     m    = a->compressedrow.nrows;
1363:     ii   = a->compressedrow.i;
1364:     ridx = a->compressedrow.rindex;
1365:     for (i=0; i<m; i++) {
1366:       n           = ii[i+1] - ii[i];
1367:       aj          = a->j + ii[i];
1368:       aa          = a->a + ii[i];
1369:       sum         = 0.0;
1370:       nonzerorow += (n>0);
1371:       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1372:       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
1373:       y[*ridx++] = sum;
1374:     }
1375:   } else { /* do not use compressed row format */
1376:     ii = a->i;
1377:     for (i=0; i<m; i++) {
1378:       n           = ii[i+1] - ii[i];
1379:       aj          = a->j + ii[i];
1380:       aa          = a->a + ii[i];
1381:       sum         = 0.0;
1382:       nonzerorow += (n>0);
1383:       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1384:       y[i] = sum;
1385:     }
1386:   }
1387:   PetscLogFlops(2.0*a->nz - nonzerorow);
1388:   VecRestoreArrayRead(xx,&x);
1389:   VecRestoreArray(yy,&y);
1390:   return(0);
1391: }

1393: PetscErrorCode MatMultAddMax_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1394: {
1395:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1396:   PetscScalar       *y,*z;
1397:   const PetscScalar *x;
1398:   const MatScalar   *aa;
1399:   PetscErrorCode    ierr;
1400:   PetscInt          m = A->rmap->n,*aj,*ii;
1401:   PetscInt          n,i,*ridx=NULL;
1402:   PetscScalar       sum;
1403:   PetscBool         usecprow=a->compressedrow.use;

1406:   VecGetArrayRead(xx,&x);
1407:   VecGetArrayPair(yy,zz,&y,&z);
1408:   if (usecprow) { /* use compressed row format */
1409:     if (zz != yy) {
1410:       PetscMemcpy(z,y,m*sizeof(PetscScalar));
1411:     }
1412:     m    = a->compressedrow.nrows;
1413:     ii   = a->compressedrow.i;
1414:     ridx = a->compressedrow.rindex;
1415:     for (i=0; i<m; i++) {
1416:       n   = ii[i+1] - ii[i];
1417:       aj  = a->j + ii[i];
1418:       aa  = a->a + ii[i];
1419:       sum = y[*ridx];
1420:       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1421:       z[*ridx++] = sum;
1422:     }
1423:   } else { /* do not use compressed row format */
1424:     ii = a->i;
1425:     for (i=0; i<m; i++) {
1426:       n   = ii[i+1] - ii[i];
1427:       aj  = a->j + ii[i];
1428:       aa  = a->a + ii[i];
1429:       sum = y[i];
1430:       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1431:       z[i] = sum;
1432:     }
1433:   }
1434:   PetscLogFlops(2.0*a->nz);
1435:   VecRestoreArrayRead(xx,&x);
1436:   VecRestoreArrayPair(yy,zz,&y,&z);
1437:   return(0);
1438: }

1440: #include <../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h>
1441: PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1442: {
1443:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1444:   PetscScalar       *y,*z;
1445:   const PetscScalar *x;
1446:   const MatScalar   *aa;
1447:   PetscErrorCode    ierr;
1448:   const PetscInt    *aj,*ii,*ridx=NULL;
1449:   PetscInt          m = A->rmap->n,n,i;
1450:   PetscScalar       sum;
1451:   PetscBool         usecprow=a->compressedrow.use;

1454:   VecGetArrayRead(xx,&x);
1455:   VecGetArrayPair(yy,zz,&y,&z);
1456:   if (usecprow) { /* use compressed row format */
1457:     if (zz != yy) {
1458:       PetscMemcpy(z,y,m*sizeof(PetscScalar));
1459:     }
1460:     m    = a->compressedrow.nrows;
1461:     ii   = a->compressedrow.i;
1462:     ridx = a->compressedrow.rindex;
1463:     for (i=0; i<m; i++) {
1464:       n   = ii[i+1] - ii[i];
1465:       aj  = a->j + ii[i];
1466:       aa  = a->a + ii[i];
1467:       sum = y[*ridx];
1468:       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1469:       z[*ridx++] = sum;
1470:     }
1471:   } else { /* do not use compressed row format */
1472:     ii = a->i;
1473: #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
1474:     aj = a->j;
1475:     aa = a->a;
1476:     fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
1477: #else
1478:     for (i=0; i<m; i++) {
1479:       n   = ii[i+1] - ii[i];
1480:       aj  = a->j + ii[i];
1481:       aa  = a->a + ii[i];
1482:       sum = y[i];
1483:       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1484:       z[i] = sum;
1485:     }
1486: #endif
1487:   }
1488:   PetscLogFlops(2.0*a->nz);
1489:   VecRestoreArrayRead(xx,&x);
1490:   VecRestoreArrayPair(yy,zz,&y,&z);
1491:   return(0);
1492: }

1494: /*
1495:      Adds diagonal pointers to sparse matrix structure.
1496: */
1497: PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
1498: {
1499:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1501:   PetscInt       i,j,m = A->rmap->n;

1504:   if (!a->diag) {
1505:     PetscMalloc1(m,&a->diag);
1506:     PetscLogObjectMemory((PetscObject)A, m*sizeof(PetscInt));
1507:   }
1508:   for (i=0; i<A->rmap->n; i++) {
1509:     a->diag[i] = a->i[i+1];
1510:     for (j=a->i[i]; j<a->i[i+1]; j++) {
1511:       if (a->j[j] == i) {
1512:         a->diag[i] = j;
1513:         break;
1514:       }
1515:     }
1516:   }
1517:   return(0);
1518: }

1520: /*
1521:      Checks for missing diagonals
1522: */
1523: PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscBool  *missing,PetscInt *d)
1524: {
1525:   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1526:   PetscInt   *diag,*ii = a->i,i;

1529:   *missing = PETSC_FALSE;
1530:   if (A->rmap->n > 0 && !ii) {
1531:     *missing = PETSC_TRUE;
1532:     if (d) *d = 0;
1533:     PetscInfo(A,"Matrix has no entries therefore is missing diagonal\n");
1534:   } else {
1535:     diag = a->diag;
1536:     for (i=0; i<A->rmap->n; i++) {
1537:       if (diag[i] >= ii[i+1]) {
1538:         *missing = PETSC_TRUE;
1539:         if (d) *d = i;
1540:         PetscInfo1(A,"Matrix is missing diagonal number %D\n",i);
1541:         break;
1542:       }
1543:     }
1544:   }
1545:   return(0);
1546: }

1548: /*
1549:    Negative shift indicates do not generate an error if there is a zero diagonal, just invert it anyways
1550: */
1551: PetscErrorCode  MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
1552: {
1553:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
1555:   PetscInt       i,*diag,m = A->rmap->n;
1556:   MatScalar      *v = a->a;
1557:   PetscScalar    *idiag,*mdiag;

1560:   if (a->idiagvalid) return(0);
1561:   MatMarkDiagonal_SeqAIJ(A);
1562:   diag = a->diag;
1563:   if (!a->idiag) {
1564:     PetscMalloc3(m,&a->idiag,m,&a->mdiag,m,&a->ssor_work);
1565:     PetscLogObjectMemory((PetscObject)A, 3*m*sizeof(PetscScalar));
1566:     v    = a->a;
1567:   }
1568:   mdiag = a->mdiag;
1569:   idiag = a->idiag;

1571:   if (omega == 1.0 && PetscRealPart(fshift) <= 0.0) {
1572:     for (i=0; i<m; i++) {
1573:       mdiag[i] = v[diag[i]];
1574:       if (!PetscAbsScalar(mdiag[i])) { /* zero diagonal */
1575:         if (PetscRealPart(fshift)) {
1576:           PetscInfo1(A,"Zero diagonal on row %D\n",i);
1577:           A->factorerrortype             = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1578:           A->factorerror_zeropivot_value = 0.0;
1579:           A->factorerror_zeropivot_row   = i;
1580:         } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
1581:       }
1582:       idiag[i] = 1.0/v[diag[i]];
1583:     }
1584:     PetscLogFlops(m);
1585:   } else {
1586:     for (i=0; i<m; i++) {
1587:       mdiag[i] = v[diag[i]];
1588:       idiag[i] = omega/(fshift + v[diag[i]]);
1589:     }
1590:     PetscLogFlops(2.0*m);
1591:   }
1592:   a->idiagvalid = PETSC_TRUE;
1593:   return(0);
1594: }

1596: #include <../src/mat/impls/aij/seq/ftn-kernels/frelax.h>
1597: PetscErrorCode MatSOR_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
1598: {
1599:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1600:   PetscScalar       *x,d,sum,*t,scale;
1601:   const MatScalar   *v,*idiag=0,*mdiag;
1602:   const PetscScalar *b, *bs,*xb, *ts;
1603:   PetscErrorCode    ierr;
1604:   PetscInt          n,m = A->rmap->n,i;
1605:   const PetscInt    *idx,*diag;

1608:   its = its*lits;

1610:   if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
1611:   if (!a->idiagvalid) {MatInvertDiagonal_SeqAIJ(A,omega,fshift);}
1612:   a->fshift = fshift;
1613:   a->omega  = omega;

1615:   diag  = a->diag;
1616:   t     = a->ssor_work;
1617:   idiag = a->idiag;
1618:   mdiag = a->mdiag;

1620:   VecGetArray(xx,&x);
1621:   VecGetArrayRead(bb,&b);
1622:   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
1623:   if (flag == SOR_APPLY_UPPER) {
1624:     /* apply (U + D/omega) to the vector */
1625:     bs = b;
1626:     for (i=0; i<m; i++) {
1627:       d   = fshift + mdiag[i];
1628:       n   = a->i[i+1] - diag[i] - 1;
1629:       idx = a->j + diag[i] + 1;
1630:       v   = a->a + diag[i] + 1;
1631:       sum = b[i]*d/omega;
1632:       PetscSparseDensePlusDot(sum,bs,v,idx,n);
1633:       x[i] = sum;
1634:     }
1635:     VecRestoreArray(xx,&x);
1636:     VecRestoreArrayRead(bb,&b);
1637:     PetscLogFlops(a->nz);
1638:     return(0);
1639:   }

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

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

1648:     to a vector efficiently using Eisenstat's trick.
1649:     */
1650:     scale = (2.0/omega) - 1.0;

1652:     /*  x = (E + U)^{-1} b */
1653:     for (i=m-1; i>=0; i--) {
1654:       n   = a->i[i+1] - diag[i] - 1;
1655:       idx = a->j + diag[i] + 1;
1656:       v   = a->a + diag[i] + 1;
1657:       sum = b[i];
1658:       PetscSparseDenseMinusDot(sum,x,v,idx,n);
1659:       x[i] = sum*idiag[i];
1660:     }

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

1666:     /*  t = (E + L)^{-1}t */
1667:     ts   = t;
1668:     diag = a->diag;
1669:     for (i=0; i<m; i++) {
1670:       n   = diag[i] - a->i[i];
1671:       idx = a->j + a->i[i];
1672:       v   = a->a + a->i[i];
1673:       sum = t[i];
1674:       PetscSparseDenseMinusDot(sum,ts,v,idx,n);
1675:       t[i] = sum*idiag[i];
1676:       /*  x = x + t */
1677:       x[i] += t[i];
1678:     }

1680:     PetscLogFlops(6.0*m-1 + 2.0*a->nz);
1681:     VecRestoreArray(xx,&x);
1682:     VecRestoreArrayRead(bb,&b);
1683:     return(0);
1684:   }
1685:   if (flag & SOR_ZERO_INITIAL_GUESS) {
1686:     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
1687:       for (i=0; i<m; i++) {
1688:         n   = diag[i] - a->i[i];
1689:         idx = a->j + a->i[i];
1690:         v   = a->a + a->i[i];
1691:         sum = b[i];
1692:         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1693:         t[i] = sum;
1694:         x[i] = sum*idiag[i];
1695:       }
1696:       xb   = t;
1697:       PetscLogFlops(a->nz);
1698:     } else xb = b;
1699:     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
1700:       for (i=m-1; i>=0; i--) {
1701:         n   = a->i[i+1] - diag[i] - 1;
1702:         idx = a->j + diag[i] + 1;
1703:         v   = a->a + diag[i] + 1;
1704:         sum = xb[i];
1705:         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1706:         if (xb == b) {
1707:           x[i] = sum*idiag[i];
1708:         } else {
1709:           x[i] = (1-omega)*x[i] + sum*idiag[i];  /* omega in idiag */
1710:         }
1711:       }
1712:       PetscLogFlops(a->nz); /* assumes 1/2 in upper */
1713:     }
1714:     its--;
1715:   }
1716:   while (its--) {
1717:     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
1718:       for (i=0; i<m; i++) {
1719:         /* lower */
1720:         n   = diag[i] - a->i[i];
1721:         idx = a->j + a->i[i];
1722:         v   = a->a + a->i[i];
1723:         sum = b[i];
1724:         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1725:         t[i] = sum;             /* save application of the lower-triangular part */
1726:         /* upper */
1727:         n   = a->i[i+1] - diag[i] - 1;
1728:         idx = a->j + diag[i] + 1;
1729:         v   = a->a + diag[i] + 1;
1730:         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1731:         x[i] = (1. - omega)*x[i] + sum*idiag[i]; /* omega in idiag */
1732:       }
1733:       xb   = t;
1734:       PetscLogFlops(2.0*a->nz);
1735:     } else xb = b;
1736:     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
1737:       for (i=m-1; i>=0; i--) {
1738:         sum = xb[i];
1739:         if (xb == b) {
1740:           /* whole matrix (no checkpointing available) */
1741:           n   = a->i[i+1] - a->i[i];
1742:           idx = a->j + a->i[i];
1743:           v   = a->a + a->i[i];
1744:           PetscSparseDenseMinusDot(sum,x,v,idx,n);
1745:           x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
1746:         } else { /* lower-triangular part has been saved, so only apply upper-triangular */
1747:           n   = a->i[i+1] - diag[i] - 1;
1748:           idx = a->j + diag[i] + 1;
1749:           v   = a->a + diag[i] + 1;
1750:           PetscSparseDenseMinusDot(sum,x,v,idx,n);
1751:           x[i] = (1. - omega)*x[i] + sum*idiag[i];  /* omega in idiag */
1752:         }
1753:       }
1754:       if (xb == b) {
1755:         PetscLogFlops(2.0*a->nz);
1756:       } else {
1757:         PetscLogFlops(a->nz); /* assumes 1/2 in upper */
1758:       }
1759:     }
1760:   }
1761:   VecRestoreArray(xx,&x);
1762:   VecRestoreArrayRead(bb,&b);
1763:   return(0);
1764: }


1767: PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
1768: {
1769:   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;

1772:   info->block_size   = 1.0;
1773:   info->nz_allocated = (double)a->maxnz;
1774:   info->nz_used      = (double)a->nz;
1775:   info->nz_unneeded  = (double)(a->maxnz - a->nz);
1776:   info->assemblies   = (double)A->num_ass;
1777:   info->mallocs      = (double)A->info.mallocs;
1778:   info->memory       = ((PetscObject)A)->mem;
1779:   if (A->factortype) {
1780:     info->fill_ratio_given  = A->info.fill_ratio_given;
1781:     info->fill_ratio_needed = A->info.fill_ratio_needed;
1782:     info->factor_mallocs    = A->info.factor_mallocs;
1783:   } else {
1784:     info->fill_ratio_given  = 0;
1785:     info->fill_ratio_needed = 0;
1786:     info->factor_mallocs    = 0;
1787:   }
1788:   return(0);
1789: }

1791: PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
1792: {
1793:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1794:   PetscInt          i,m = A->rmap->n - 1;
1795:   PetscErrorCode    ierr;
1796:   const PetscScalar *xx;
1797:   PetscScalar       *bb;
1798:   PetscInt          d = 0;

1801:   if (x && b) {
1802:     VecGetArrayRead(x,&xx);
1803:     VecGetArray(b,&bb);
1804:     for (i=0; i<N; i++) {
1805:       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1806:       bb[rows[i]] = diag*xx[rows[i]];
1807:     }
1808:     VecRestoreArrayRead(x,&xx);
1809:     VecRestoreArray(b,&bb);
1810:   }

1812:   if (a->keepnonzeropattern) {
1813:     for (i=0; i<N; i++) {
1814:       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1815:       PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));
1816:     }
1817:     if (diag != 0.0) {
1818:       for (i=0; i<N; i++) {
1819:         d = rows[i];
1820:         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);
1821:       }
1822:       for (i=0; i<N; i++) {
1823:         a->a[a->diag[rows[i]]] = diag;
1824:       }
1825:     }
1826:   } else {
1827:     if (diag != 0.0) {
1828:       for (i=0; i<N; i++) {
1829:         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1830:         if (a->ilen[rows[i]] > 0) {
1831:           a->ilen[rows[i]]    = 1;
1832:           a->a[a->i[rows[i]]] = diag;
1833:           a->j[a->i[rows[i]]] = rows[i];
1834:         } else { /* in case row was completely empty */
1835:           MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);
1836:         }
1837:       }
1838:     } else {
1839:       for (i=0; i<N; i++) {
1840:         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1841:         a->ilen[rows[i]] = 0;
1842:       }
1843:     }
1844:     A->nonzerostate++;
1845:   }
1846:   (*A->ops->assemblyend)(A,MAT_FINAL_ASSEMBLY);
1847:   return(0);
1848: }

1850: PetscErrorCode MatZeroRowsColumns_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
1851: {
1852:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1853:   PetscInt          i,j,m = A->rmap->n - 1,d = 0;
1854:   PetscErrorCode    ierr;
1855:   PetscBool         missing,*zeroed,vecs = PETSC_FALSE;
1856:   const PetscScalar *xx;
1857:   PetscScalar       *bb;

1860:   if (x && b) {
1861:     VecGetArrayRead(x,&xx);
1862:     VecGetArray(b,&bb);
1863:     vecs = PETSC_TRUE;
1864:   }
1865:   PetscCalloc1(A->rmap->n,&zeroed);
1866:   for (i=0; i<N; i++) {
1867:     if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1868:     PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));

1870:     zeroed[rows[i]] = PETSC_TRUE;
1871:   }
1872:   for (i=0; i<A->rmap->n; i++) {
1873:     if (!zeroed[i]) {
1874:       for (j=a->i[i]; j<a->i[i+1]; j++) {
1875:         if (zeroed[a->j[j]]) {
1876:           if (vecs) bb[i] -= a->a[j]*xx[a->j[j]];
1877:           a->a[j] = 0.0;
1878:         }
1879:       }
1880:     } else if (vecs) bb[i] = diag*xx[i];
1881:   }
1882:   if (x && b) {
1883:     VecRestoreArrayRead(x,&xx);
1884:     VecRestoreArray(b,&bb);
1885:   }
1886:   PetscFree(zeroed);
1887:   if (diag != 0.0) {
1888:     MatMissingDiagonal_SeqAIJ(A,&missing,&d);
1889:     if (missing) {
1890:       if (a->nonew) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
1891:       else {
1892:         for (i=0; i<N; i++) {
1893:           MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);
1894:         }
1895:       }
1896:     } else {
1897:       for (i=0; i<N; i++) {
1898:         a->a[a->diag[rows[i]]] = diag;
1899:       }
1900:     }
1901:   }
1902:   (*A->ops->assemblyend)(A,MAT_FINAL_ASSEMBLY);
1903:   return(0);
1904: }

1906: PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
1907: {
1908:   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1909:   PetscInt   *itmp;

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

1914:   *nz = a->i[row+1] - a->i[row];
1915:   if (v) *v = a->a + a->i[row];
1916:   if (idx) {
1917:     itmp = a->j + a->i[row];
1918:     if (*nz) *idx = itmp;
1919:     else *idx = 0;
1920:   }
1921:   return(0);
1922: }

1924: /* remove this function? */
1925: PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
1926: {
1928:   return(0);
1929: }

1931: PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
1932: {
1933:   Mat_SeqAIJ     *a  = (Mat_SeqAIJ*)A->data;
1934:   MatScalar      *v  = a->a;
1935:   PetscReal      sum = 0.0;
1937:   PetscInt       i,j;

1940:   if (type == NORM_FROBENIUS) {
1941: #if defined(PETSC_USE_REAL___FP16)
1942:     PetscBLASInt one = 1,nz = a->nz;
1943:     *nrm = BLASnrm2_(&nz,v,&one);
1944: #else
1945:     for (i=0; i<a->nz; i++) {
1946:       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
1947:     }
1948:     *nrm = PetscSqrtReal(sum);
1949: #endif
1950:     PetscLogFlops(2*a->nz);
1951:   } else if (type == NORM_1) {
1952:     PetscReal *tmp;
1953:     PetscInt  *jj = a->j;
1954:     PetscCalloc1(A->cmap->n+1,&tmp);
1955:     *nrm = 0.0;
1956:     for (j=0; j<a->nz; j++) {
1957:       tmp[*jj++] += PetscAbsScalar(*v);  v++;
1958:     }
1959:     for (j=0; j<A->cmap->n; j++) {
1960:       if (tmp[j] > *nrm) *nrm = tmp[j];
1961:     }
1962:     PetscFree(tmp);
1963:     PetscLogFlops(PetscMax(a->nz-1,0));
1964:   } else if (type == NORM_INFINITY) {
1965:     *nrm = 0.0;
1966:     for (j=0; j<A->rmap->n; j++) {
1967:       v   = a->a + a->i[j];
1968:       sum = 0.0;
1969:       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
1970:         sum += PetscAbsScalar(*v); v++;
1971:       }
1972:       if (sum > *nrm) *nrm = sum;
1973:     }
1974:     PetscLogFlops(PetscMax(a->nz-1,0));
1975:   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for two norm");
1976:   return(0);
1977: }

1979: /* Merged from MatGetSymbolicTranspose_SeqAIJ() - replace MatGetSymbolicTranspose_SeqAIJ()? */
1980: PetscErrorCode MatTransposeSymbolic_SeqAIJ(Mat A,Mat *B)
1981: {
1983:   PetscInt       i,j,anzj;
1984:   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data,*b;
1985:   PetscInt       an=A->cmap->N,am=A->rmap->N;
1986:   PetscInt       *ati,*atj,*atfill,*ai=a->i,*aj=a->j;

1989:   /* Allocate space for symbolic transpose info and work array */
1990:   PetscCalloc1(an+1,&ati);
1991:   PetscMalloc1(ai[am],&atj);
1992:   PetscMalloc1(an,&atfill);

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

2000:   /* Copy ati into atfill so we have locations of the next free space in atj */
2001:   PetscMemcpy(atfill,ati,an*sizeof(PetscInt));

2003:   /* Walk through A row-wise and mark nonzero entries of A^T. */
2004:   for (i=0;i<am;i++) {
2005:     anzj = ai[i+1] - ai[i];
2006:     for (j=0;j<anzj;j++) {
2007:       atj[atfill[*aj]] = i;
2008:       atfill[*aj++]   += 1;
2009:     }
2010:   }

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

2017:   b          = (Mat_SeqAIJ*)((*B)->data);
2018:   b->free_a  = PETSC_FALSE;
2019:   b->free_ij = PETSC_TRUE;
2020:   b->nonew   = 0;
2021:   return(0);
2022: }

2024: PetscErrorCode MatTranspose_SeqAIJ(Mat A,MatReuse reuse,Mat *B)
2025: {
2026:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2027:   Mat            C;
2029:   PetscInt       i,*aj = a->j,*ai = a->i,m = A->rmap->n,len,*col;
2030:   MatScalar      *array = a->a;

2033:   if (reuse == MAT_INPLACE_MATRIX && m != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Square matrix only for in-place");

2035:   if (reuse == MAT_INITIAL_MATRIX || reuse == MAT_INPLACE_MATRIX) {
2036:     PetscCalloc1(1+A->cmap->n,&col);

2038:     for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
2039:     MatCreate(PetscObjectComm((PetscObject)A),&C);
2040:     MatSetSizes(C,A->cmap->n,m,A->cmap->n,m);
2041:     MatSetBlockSizes(C,PetscAbs(A->cmap->bs),PetscAbs(A->rmap->bs));
2042:     MatSetType(C,((PetscObject)A)->type_name);
2043:     MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);
2044:     PetscFree(col);
2045:   } else {
2046:     C = *B;
2047:   }

2049:   for (i=0; i<m; i++) {
2050:     len    = ai[i+1]-ai[i];
2051:     MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);
2052:     array += len;
2053:     aj    += len;
2054:   }
2055:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
2056:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);

2058:   if (reuse == MAT_INITIAL_MATRIX || reuse == MAT_REUSE_MATRIX) {
2059:     *B = C;
2060:   } else {
2061:     MatHeaderMerge(A,&C);
2062:   }
2063:   return(0);
2064: }

2066: PetscErrorCode  MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
2067: {
2068:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) B->data;
2069:   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
2070:   MatScalar      *va,*vb;
2072:   PetscInt       ma,na,mb,nb, i;

2075:   MatGetSize(A,&ma,&na);
2076:   MatGetSize(B,&mb,&nb);
2077:   if (ma!=nb || na!=mb) {
2078:     *f = PETSC_FALSE;
2079:     return(0);
2080:   }
2081:   aii  = aij->i; bii = bij->i;
2082:   adx  = aij->j; bdx = bij->j;
2083:   va   = aij->a; vb = bij->a;
2084:   PetscMalloc1(ma,&aptr);
2085:   PetscMalloc1(mb,&bptr);
2086:   for (i=0; i<ma; i++) aptr[i] = aii[i];
2087:   for (i=0; i<mb; i++) bptr[i] = bii[i];

2089:   *f = PETSC_TRUE;
2090:   for (i=0; i<ma; i++) {
2091:     while (aptr[i]<aii[i+1]) {
2092:       PetscInt    idc,idr;
2093:       PetscScalar vc,vr;
2094:       /* column/row index/value */
2095:       idc = adx[aptr[i]];
2096:       idr = bdx[bptr[idc]];
2097:       vc  = va[aptr[i]];
2098:       vr  = vb[bptr[idc]];
2099:       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
2100:         *f = PETSC_FALSE;
2101:         goto done;
2102:       } else {
2103:         aptr[i]++;
2104:         if (B || i!=idc) bptr[idc]++;
2105:       }
2106:     }
2107:   }
2108: done:
2109:   PetscFree(aptr);
2110:   PetscFree(bptr);
2111:   return(0);
2112: }

2114: PetscErrorCode  MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
2115: {
2116:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) B->data;
2117:   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
2118:   MatScalar      *va,*vb;
2120:   PetscInt       ma,na,mb,nb, i;

2123:   MatGetSize(A,&ma,&na);
2124:   MatGetSize(B,&mb,&nb);
2125:   if (ma!=nb || na!=mb) {
2126:     *f = PETSC_FALSE;
2127:     return(0);
2128:   }
2129:   aii  = aij->i; bii = bij->i;
2130:   adx  = aij->j; bdx = bij->j;
2131:   va   = aij->a; vb = bij->a;
2132:   PetscMalloc1(ma,&aptr);
2133:   PetscMalloc1(mb,&bptr);
2134:   for (i=0; i<ma; i++) aptr[i] = aii[i];
2135:   for (i=0; i<mb; i++) bptr[i] = bii[i];

2137:   *f = PETSC_TRUE;
2138:   for (i=0; i<ma; i++) {
2139:     while (aptr[i]<aii[i+1]) {
2140:       PetscInt    idc,idr;
2141:       PetscScalar vc,vr;
2142:       /* column/row index/value */
2143:       idc = adx[aptr[i]];
2144:       idr = bdx[bptr[idc]];
2145:       vc  = va[aptr[i]];
2146:       vr  = vb[bptr[idc]];
2147:       if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) {
2148:         *f = PETSC_FALSE;
2149:         goto done;
2150:       } else {
2151:         aptr[i]++;
2152:         if (B || i!=idc) bptr[idc]++;
2153:       }
2154:     }
2155:   }
2156: done:
2157:   PetscFree(aptr);
2158:   PetscFree(bptr);
2159:   return(0);
2160: }

2162: PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
2163: {

2167:   MatIsTranspose_SeqAIJ(A,A,tol,f);
2168:   return(0);
2169: }

2171: PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
2172: {

2176:   MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);
2177:   return(0);
2178: }

2180: PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
2181: {
2182:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
2183:   const PetscScalar *l,*r;
2184:   PetscScalar       x;
2185:   MatScalar         *v;
2186:   PetscErrorCode    ierr;
2187:   PetscInt          i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz;
2188:   const PetscInt    *jj;

2191:   if (ll) {
2192:     /* The local size is used so that VecMPI can be passed to this routine
2193:        by MatDiagonalScale_MPIAIJ */
2194:     VecGetLocalSize(ll,&m);
2195:     if (m != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
2196:     VecGetArrayRead(ll,&l);
2197:     v    = a->a;
2198:     for (i=0; i<m; i++) {
2199:       x = l[i];
2200:       M = a->i[i+1] - a->i[i];
2201:       for (j=0; j<M; j++) (*v++) *= x;
2202:     }
2203:     VecRestoreArrayRead(ll,&l);
2204:     PetscLogFlops(nz);
2205:   }
2206:   if (rr) {
2207:     VecGetLocalSize(rr,&n);
2208:     if (n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
2209:     VecGetArrayRead(rr,&r);
2210:     v    = a->a; jj = a->j;
2211:     for (i=0; i<nz; i++) (*v++) *= r[*jj++];
2212:     VecRestoreArrayRead(rr,&r);
2213:     PetscLogFlops(nz);
2214:   }
2215:   MatSeqAIJInvalidateDiagonal(A);
2216:   return(0);
2217: }

2219: PetscErrorCode MatCreateSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
2220: {
2221:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
2223:   PetscInt       *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens;
2224:   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
2225:   const PetscInt *irow,*icol;
2226:   PetscInt       nrows,ncols;
2227:   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
2228:   MatScalar      *a_new,*mat_a;
2229:   Mat            C;
2230:   PetscBool      stride;


2234:   ISGetIndices(isrow,&irow);
2235:   ISGetLocalSize(isrow,&nrows);
2236:   ISGetLocalSize(iscol,&ncols);

2238:   PetscObjectTypeCompare((PetscObject)iscol,ISSTRIDE,&stride);
2239:   if (stride) {
2240:     ISStrideGetInfo(iscol,&first,&step);
2241:   } else {
2242:     first = 0;
2243:     step  = 0;
2244:   }
2245:   if (stride && step == 1) {
2246:     /* special case of contiguous rows */
2247:     PetscMalloc2(nrows,&lens,nrows,&starts);
2248:     /* loop over new rows determining lens and starting points */
2249:     for (i=0; i<nrows; i++) {
2250:       kstart = ai[irow[i]];
2251:       kend   = kstart + ailen[irow[i]];
2252:       starts[i] = kstart;
2253:       for (k=kstart; k<kend; k++) {
2254:         if (aj[k] >= first) {
2255:           starts[i] = k;
2256:           break;
2257:         }
2258:       }
2259:       sum = 0;
2260:       while (k < kend) {
2261:         if (aj[k++] >= first+ncols) break;
2262:         sum++;
2263:       }
2264:       lens[i] = sum;
2265:     }
2266:     /* create submatrix */
2267:     if (scall == MAT_REUSE_MATRIX) {
2268:       PetscInt n_cols,n_rows;
2269:       MatGetSize(*B,&n_rows,&n_cols);
2270:       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
2271:       MatZeroEntries(*B);
2272:       C    = *B;
2273:     } else {
2274:       PetscInt rbs,cbs;
2275:       MatCreate(PetscObjectComm((PetscObject)A),&C);
2276:       MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);
2277:       ISGetBlockSize(isrow,&rbs);
2278:       ISGetBlockSize(iscol,&cbs);
2279:       MatSetBlockSizes(C,rbs,cbs);
2280:       MatSetType(C,((PetscObject)A)->type_name);
2281:       MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);
2282:     }
2283:     c = (Mat_SeqAIJ*)C->data;

2285:     /* loop over rows inserting into submatrix */
2286:     a_new = c->a;
2287:     j_new = c->j;
2288:     i_new = c->i;

2290:     for (i=0; i<nrows; i++) {
2291:       ii    = starts[i];
2292:       lensi = lens[i];
2293:       for (k=0; k<lensi; k++) {
2294:         *j_new++ = aj[ii+k] - first;
2295:       }
2296:       PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));
2297:       a_new     += lensi;
2298:       i_new[i+1] = i_new[i] + lensi;
2299:       c->ilen[i] = lensi;
2300:     }
2301:     PetscFree2(lens,starts);
2302:   } else {
2303:     ISGetIndices(iscol,&icol);
2304:     PetscCalloc1(oldcols,&smap);
2305:     PetscMalloc1(1+nrows,&lens);
2306:     for (i=0; i<ncols; i++) {
2307: #if defined(PETSC_USE_DEBUG)
2308:       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);
2309: #endif
2310:       smap[icol[i]] = i+1;
2311:     }

2313:     /* determine lens of each row */
2314:     for (i=0; i<nrows; i++) {
2315:       kstart  = ai[irow[i]];
2316:       kend    = kstart + a->ilen[irow[i]];
2317:       lens[i] = 0;
2318:       for (k=kstart; k<kend; k++) {
2319:         if (smap[aj[k]]) {
2320:           lens[i]++;
2321:         }
2322:       }
2323:     }
2324:     /* Create and fill new matrix */
2325:     if (scall == MAT_REUSE_MATRIX) {
2326:       PetscBool equal;

2328:       c = (Mat_SeqAIJ*)((*B)->data);
2329:       if ((*B)->rmap->n  != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
2330:       PetscMemcmp(c->ilen,lens,(*B)->rmap->n*sizeof(PetscInt),&equal);
2331:       if (!equal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
2332:       PetscMemzero(c->ilen,(*B)->rmap->n*sizeof(PetscInt));
2333:       C    = *B;
2334:     } else {
2335:       PetscInt rbs,cbs;
2336:       MatCreate(PetscObjectComm((PetscObject)A),&C);
2337:       MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);
2338:       ISGetBlockSize(isrow,&rbs);
2339:       ISGetBlockSize(iscol,&cbs);
2340:       MatSetBlockSizes(C,rbs,cbs);
2341:       MatSetType(C,((PetscObject)A)->type_name);
2342:       MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);
2343:     }
2344:     c = (Mat_SeqAIJ*)(C->data);
2345:     for (i=0; i<nrows; i++) {
2346:       row      = irow[i];
2347:       kstart   = ai[row];
2348:       kend     = kstart + a->ilen[row];
2349:       mat_i    = c->i[i];
2350:       mat_j    = c->j + mat_i;
2351:       mat_a    = c->a + mat_i;
2352:       mat_ilen = c->ilen + i;
2353:       for (k=kstart; k<kend; k++) {
2354:         if ((tcol=smap[a->j[k]])) {
2355:           *mat_j++ = tcol - 1;
2356:           *mat_a++ = a->a[k];
2357:           (*mat_ilen)++;

2359:         }
2360:       }
2361:     }
2362:     /* Free work space */
2363:     ISRestoreIndices(iscol,&icol);
2364:     PetscFree(smap);
2365:     PetscFree(lens);
2366:     /* sort */
2367:     for (i = 0; i < nrows; i++) {
2368:       PetscInt ilen;

2370:       mat_i = c->i[i];
2371:       mat_j = c->j + mat_i;
2372:       mat_a = c->a + mat_i;
2373:       ilen  = c->ilen[i];
2374:       PetscSortIntWithScalarArray(ilen,mat_j,mat_a);
2375:     }
2376:   }
2377:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
2378:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);

2380:   ISRestoreIndices(isrow,&irow);
2381:   *B   = C;
2382:   return(0);
2383: }

2385: PetscErrorCode  MatGetMultiProcBlock_SeqAIJ(Mat mat,MPI_Comm subComm,MatReuse scall,Mat *subMat)
2386: {
2388:   Mat            B;

2391:   if (scall == MAT_INITIAL_MATRIX) {
2392:     MatCreate(subComm,&B);
2393:     MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->n,mat->cmap->n);
2394:     MatSetBlockSizesFromMats(B,mat,mat);
2395:     MatSetType(B,MATSEQAIJ);
2396:     MatDuplicateNoCreate_SeqAIJ(B,mat,MAT_COPY_VALUES,PETSC_TRUE);
2397:     *subMat = B;
2398:   } else {
2399:     MatCopy_SeqAIJ(mat,*subMat,SAME_NONZERO_PATTERN);
2400:   }
2401:   return(0);
2402: }

2404: PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
2405: {
2406:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
2408:   Mat            outA;
2409:   PetscBool      row_identity,col_identity;

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

2414:   ISIdentity(row,&row_identity);
2415:   ISIdentity(col,&col_identity);

2417:   outA             = inA;
2418:   outA->factortype = MAT_FACTOR_LU;
2419:   PetscFree(inA->solvertype);
2420:   PetscStrallocpy(MATSOLVERPETSC,&inA->solvertype);

2422:   PetscObjectReference((PetscObject)row);
2423:   ISDestroy(&a->row);

2425:   a->row = row;

2427:   PetscObjectReference((PetscObject)col);
2428:   ISDestroy(&a->col);

2430:   a->col = col;

2432:   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
2433:   ISDestroy(&a->icol);
2434:   ISInvertPermutation(col,PETSC_DECIDE,&a->icol);
2435:   PetscLogObjectParent((PetscObject)inA,(PetscObject)a->icol);

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

2442:   MatMarkDiagonal_SeqAIJ(inA);
2443:   if (row_identity && col_identity) {
2444:     MatLUFactorNumeric_SeqAIJ_inplace(outA,inA,info);
2445:   } else {
2446:     MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);
2447:   }
2448:   return(0);
2449: }

2451: PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
2452: {
2453:   Mat_SeqAIJ     *a     = (Mat_SeqAIJ*)inA->data;
2454:   PetscScalar    oalpha = alpha;
2456:   PetscBLASInt   one = 1,bnz;

2459:   PetscBLASIntCast(a->nz,&bnz);
2460:   PetscStackCallBLAS("BLASscal",BLASscal_(&bnz,&oalpha,a->a,&one));
2461:   PetscLogFlops(a->nz);
2462:   MatSeqAIJInvalidateDiagonal(inA);
2463:   return(0);
2464: }

2466: PetscErrorCode MatDestroySubMatrix_Private(Mat_SubSppt *submatj)
2467: {
2469:   PetscInt       i;

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

2475:     for (i=0; i<submatj->nrqr; ++i) {
2476:       PetscFree(submatj->sbuf2[i]);
2477:     }
2478:     PetscFree3(submatj->sbuf2,submatj->req_size,submatj->req_source1);

2480:     if (submatj->rbuf1) {
2481:       PetscFree(submatj->rbuf1[0]);
2482:       PetscFree(submatj->rbuf1);
2483:     }

2485:     for (i=0; i<submatj->nrqs; ++i) {
2486:       PetscFree(submatj->rbuf3[i]);
2487:     }
2488:     PetscFree3(submatj->req_source2,submatj->rbuf2,submatj->rbuf3);
2489:     PetscFree(submatj->pa);
2490:   }

2492: #if defined(PETSC_USE_CTABLE)
2493:   PetscTableDestroy((PetscTable*)&submatj->rmap);
2494:   if (submatj->cmap_loc) {PetscFree(submatj->cmap_loc);}
2495:   PetscFree(submatj->rmap_loc);
2496: #else
2497:   PetscFree(submatj->rmap);
2498: #endif

2500:   if (!submatj->allcolumns) {
2501: #if defined(PETSC_USE_CTABLE)
2502:     PetscTableDestroy((PetscTable*)&submatj->cmap);
2503: #else
2504:     PetscFree(submatj->cmap);
2505: #endif
2506:   }
2507:   PetscFree(submatj->row2proc);

2509:   PetscFree(submatj);
2510:   return(0);
2511: }

2513: PetscErrorCode MatDestroySubMatrix_SeqAIJ(Mat C)
2514: {
2516:   Mat_SeqAIJ     *c = (Mat_SeqAIJ*)C->data;
2517:   Mat_SubSppt    *submatj = c->submatis1;

2520:   submatj->destroy(C);
2521:   MatDestroySubMatrix_Private(submatj);
2522:   return(0);
2523: }

2525: PetscErrorCode MatDestroySubMatrices_SeqAIJ(PetscInt n,Mat *mat[])
2526: {
2528:   PetscInt       i;
2529:   Mat            C;
2530:   Mat_SeqAIJ     *c;
2531:   Mat_SubSppt    *submatj;

2534:   for (i=0; i<n; i++) {
2535:     C       = (*mat)[i];
2536:     c       = (Mat_SeqAIJ*)C->data;
2537:     submatj = c->submatis1;
2538:     if (submatj) {
2539:       if (--((PetscObject)C)->refct <= 0) {
2540:         (submatj->destroy)(C);
2541:         MatDestroySubMatrix_Private(submatj);
2542:         PetscLayoutDestroy(&C->rmap);
2543:         PetscLayoutDestroy(&C->cmap);
2544:         PetscHeaderDestroy(&C);
2545:       }
2546:     } else {
2547:       MatDestroy(&C);
2548:     }
2549:   }

2551:   PetscFree(*mat);
2552:   return(0);
2553: }

2555: PetscErrorCode MatCreateSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
2556: {
2558:   PetscInt       i;

2561:   if (scall == MAT_INITIAL_MATRIX) {
2562:     PetscCalloc1(n+1,B);
2563:   }

2565:   for (i=0; i<n; i++) {
2566:     MatCreateSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);
2567:   }
2568:   return(0);
2569: }

2571: PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
2572: {
2573:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2575:   PetscInt       row,i,j,k,l,m,n,*nidx,isz,val;
2576:   const PetscInt *idx;
2577:   PetscInt       start,end,*ai,*aj;
2578:   PetscBT        table;

2581:   m  = A->rmap->n;
2582:   ai = a->i;
2583:   aj = a->j;

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

2587:   PetscMalloc1(m+1,&nidx);
2588:   PetscBTCreate(m,&table);

2590:   for (i=0; i<is_max; i++) {
2591:     /* Initialize the two local arrays */
2592:     isz  = 0;
2593:     PetscBTMemzero(m,table);

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

2599:     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
2600:     for (j=0; j<n; ++j) {
2601:       if (!PetscBTLookupSet(table,idx[j])) nidx[isz++] = idx[j];
2602:     }
2603:     ISRestoreIndices(is[i],&idx);
2604:     ISDestroy(&is[i]);

2606:     k = 0;
2607:     for (j=0; j<ov; j++) { /* for each overlap */
2608:       n = isz;
2609:       for (; k<n; k++) { /* do only those rows in nidx[k], which are not done yet */
2610:         row   = nidx[k];
2611:         start = ai[row];
2612:         end   = ai[row+1];
2613:         for (l = start; l<end; l++) {
2614:           val = aj[l];
2615:           if (!PetscBTLookupSet(table,val)) nidx[isz++] = val;
2616:         }
2617:       }
2618:     }
2619:     ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,PETSC_COPY_VALUES,(is+i));
2620:   }
2621:   PetscBTDestroy(&table);
2622:   PetscFree(nidx);
2623:   return(0);
2624: }

2626: /* -------------------------------------------------------------- */
2627: PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
2628: {
2629:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2631:   PetscInt       i,nz = 0,m = A->rmap->n,n = A->cmap->n;
2632:   const PetscInt *row,*col;
2633:   PetscInt       *cnew,j,*lens;
2634:   IS             icolp,irowp;
2635:   PetscInt       *cwork = NULL;
2636:   PetscScalar    *vwork = NULL;

2639:   ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);
2640:   ISGetIndices(irowp,&row);
2641:   ISInvertPermutation(colp,PETSC_DECIDE,&icolp);
2642:   ISGetIndices(icolp,&col);

2644:   /* determine lengths of permuted rows */
2645:   PetscMalloc1(m+1,&lens);
2646:   for (i=0; i<m; i++) lens[row[i]] = a->i[i+1] - a->i[i];
2647:   MatCreate(PetscObjectComm((PetscObject)A),B);
2648:   MatSetSizes(*B,m,n,m,n);
2649:   MatSetBlockSizesFromMats(*B,A,A);
2650:   MatSetType(*B,((PetscObject)A)->type_name);
2651:   MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);
2652:   PetscFree(lens);

2654:   PetscMalloc1(n,&cnew);
2655:   for (i=0; i<m; i++) {
2656:     MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);
2657:     for (j=0; j<nz; j++) cnew[j] = col[cwork[j]];
2658:     MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);
2659:     MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);
2660:   }
2661:   PetscFree(cnew);

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

2665:   MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);
2666:   MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);
2667:   ISRestoreIndices(irowp,&row);
2668:   ISRestoreIndices(icolp,&col);
2669:   ISDestroy(&irowp);
2670:   ISDestroy(&icolp);
2671:   return(0);
2672: }

2674: PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
2675: {

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

2684:     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");
2685:     PetscMemcpy(b->a,a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));
2686:     PetscObjectStateIncrease((PetscObject)B);
2687:   } else {
2688:     MatCopy_Basic(A,B,str);
2689:   }
2690:   return(0);
2691: }

2693: PetscErrorCode MatSetUp_SeqAIJ(Mat A)
2694: {

2698:    MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);
2699:   return(0);
2700: }

2702: PetscErrorCode MatSeqAIJGetArray_SeqAIJ(Mat A,PetscScalar *array[])
2703: {
2704:   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;

2707:   *array = a->a;
2708:   return(0);
2709: }

2711: PetscErrorCode MatSeqAIJRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
2712: {
2714:   return(0);
2715: }

2717: /*
2718:    Computes the number of nonzeros per row needed for preallocation when X and Y
2719:    have different nonzero structure.
2720: */
2721: PetscErrorCode MatAXPYGetPreallocation_SeqX_private(PetscInt m,const PetscInt *xi,const PetscInt *xj,const PetscInt *yi,const PetscInt *yj,PetscInt *nnz)
2722: {
2723:   PetscInt       i,j,k,nzx,nzy;

2726:   /* Set the number of nonzeros in the new matrix */
2727:   for (i=0; i<m; i++) {
2728:     const PetscInt *xjj = xj+xi[i],*yjj = yj+yi[i];
2729:     nzx = xi[i+1] - xi[i];
2730:     nzy = yi[i+1] - yi[i];
2731:     nnz[i] = 0;
2732:     for (j=0,k=0; j<nzx; j++) {                   /* Point in X */
2733:       for (; k<nzy && yjj[k]<xjj[j]; k++) nnz[i]++; /* Catch up to X */
2734:       if (k<nzy && yjj[k]==xjj[j]) k++;             /* Skip duplicate */
2735:       nnz[i]++;
2736:     }
2737:     for (; k<nzy; k++) nnz[i]++;
2738:   }
2739:   return(0);
2740: }

2742: PetscErrorCode MatAXPYGetPreallocation_SeqAIJ(Mat Y,Mat X,PetscInt *nnz)
2743: {
2744:   PetscInt       m = Y->rmap->N;
2745:   Mat_SeqAIJ     *x = (Mat_SeqAIJ*)X->data;
2746:   Mat_SeqAIJ     *y = (Mat_SeqAIJ*)Y->data;

2750:   /* Set the number of nonzeros in the new matrix */
2751:   MatAXPYGetPreallocation_SeqX_private(m,x->i,x->j,y->i,y->j,nnz);
2752:   return(0);
2753: }

2755: PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2756: {
2758:   Mat_SeqAIJ     *x = (Mat_SeqAIJ*)X->data,*y = (Mat_SeqAIJ*)Y->data;
2759:   PetscBLASInt   one=1,bnz;

2762:   PetscBLASIntCast(x->nz,&bnz);
2763:   if (str == SAME_NONZERO_PATTERN) {
2764:     PetscScalar alpha = a;
2765:     PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
2766:     MatSeqAIJInvalidateDiagonal(Y);
2767:     PetscObjectStateIncrease((PetscObject)Y);
2768:   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2769:     MatAXPY_Basic(Y,a,X,str);
2770:   } else {
2771:     Mat      B;
2772:     PetscInt *nnz;
2773:     PetscMalloc1(Y->rmap->N,&nnz);
2774:     MatCreate(PetscObjectComm((PetscObject)Y),&B);
2775:     PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);
2776:     MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);
2777:     MatSetBlockSizesFromMats(B,Y,Y);
2778:     MatSetType(B,(MatType) ((PetscObject)Y)->type_name);
2779:     MatAXPYGetPreallocation_SeqAIJ(Y,X,nnz);
2780:     MatSeqAIJSetPreallocation(B,0,nnz);
2781:     MatAXPY_BasicWithPreallocation(B,Y,a,X,str);
2782:     MatHeaderReplace(Y,&B);
2783:     PetscFree(nnz);
2784:   }
2785:   return(0);
2786: }

2788: PetscErrorCode  MatConjugate_SeqAIJ(Mat mat)
2789: {
2790: #if defined(PETSC_USE_COMPLEX)
2791:   Mat_SeqAIJ  *aij = (Mat_SeqAIJ*)mat->data;
2792:   PetscInt    i,nz;
2793:   PetscScalar *a;

2796:   nz = aij->nz;
2797:   a  = aij->a;
2798:   for (i=0; i<nz; i++) a[i] = PetscConj(a[i]);
2799: #else
2801: #endif
2802:   return(0);
2803: }

2805: PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2806: {
2807:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2809:   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2810:   PetscReal      atmp;
2811:   PetscScalar    *x;
2812:   MatScalar      *aa;

2815:   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2816:   aa = a->a;
2817:   ai = a->i;
2818:   aj = a->j;

2820:   VecSet(v,0.0);
2821:   VecGetArray(v,&x);
2822:   VecGetLocalSize(v,&n);
2823:   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2824:   for (i=0; i<m; i++) {
2825:     ncols = ai[1] - ai[0]; ai++;
2826:     x[i]  = 0.0;
2827:     for (j=0; j<ncols; j++) {
2828:       atmp = PetscAbsScalar(*aa);
2829:       if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2830:       aa++; aj++;
2831:     }
2832:   }
2833:   VecRestoreArray(v,&x);
2834:   return(0);
2835: }

2837: PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2838: {
2839:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2841:   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2842:   PetscScalar    *x;
2843:   MatScalar      *aa;

2846:   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2847:   aa = a->a;
2848:   ai = a->i;
2849:   aj = a->j;

2851:   VecSet(v,0.0);
2852:   VecGetArray(v,&x);
2853:   VecGetLocalSize(v,&n);
2854:   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2855:   for (i=0; i<m; i++) {
2856:     ncols = ai[1] - ai[0]; ai++;
2857:     if (ncols == A->cmap->n) { /* row is dense */
2858:       x[i] = *aa; if (idx) idx[i] = 0;
2859:     } else {  /* row is sparse so already KNOW maximum is 0.0 or higher */
2860:       x[i] = 0.0;
2861:       if (idx) {
2862:         idx[i] = 0; /* in case ncols is zero */
2863:         for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */
2864:           if (aj[j] > j) {
2865:             idx[i] = j;
2866:             break;
2867:           }
2868:         }
2869:       }
2870:     }
2871:     for (j=0; j<ncols; j++) {
2872:       if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2873:       aa++; aj++;
2874:     }
2875:   }
2876:   VecRestoreArray(v,&x);
2877:   return(0);
2878: }

2880: PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2881: {
2882:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2884:   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2885:   PetscReal      atmp;
2886:   PetscScalar    *x;
2887:   MatScalar      *aa;

2890:   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2891:   aa = a->a;
2892:   ai = a->i;
2893:   aj = a->j;

2895:   VecSet(v,0.0);
2896:   VecGetArray(v,&x);
2897:   VecGetLocalSize(v,&n);
2898:   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);
2899:   for (i=0; i<m; i++) {
2900:     ncols = ai[1] - ai[0]; ai++;
2901:     if (ncols) {
2902:       /* Get first nonzero */
2903:       for (j = 0; j < ncols; j++) {
2904:         atmp = PetscAbsScalar(aa[j]);
2905:         if (atmp > 1.0e-12) {
2906:           x[i] = atmp;
2907:           if (idx) idx[i] = aj[j];
2908:           break;
2909:         }
2910:       }
2911:       if (j == ncols) {x[i] = PetscAbsScalar(*aa); if (idx) idx[i] = *aj;}
2912:     } else {
2913:       x[i] = 0.0; if (idx) idx[i] = 0;
2914:     }
2915:     for (j = 0; j < ncols; j++) {
2916:       atmp = PetscAbsScalar(*aa);
2917:       if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2918:       aa++; aj++;
2919:     }
2920:   }
2921:   VecRestoreArray(v,&x);
2922:   return(0);
2923: }

2925: PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2926: {
2927:   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
2928:   PetscErrorCode  ierr;
2929:   PetscInt        i,j,m = A->rmap->n,ncols,n;
2930:   const PetscInt  *ai,*aj;
2931:   PetscScalar     *x;
2932:   const MatScalar *aa;

2935:   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2936:   aa = a->a;
2937:   ai = a->i;
2938:   aj = a->j;

2940:   VecSet(v,0.0);
2941:   VecGetArray(v,&x);
2942:   VecGetLocalSize(v,&n);
2943:   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2944:   for (i=0; i<m; i++) {
2945:     ncols = ai[1] - ai[0]; ai++;
2946:     if (ncols == A->cmap->n) { /* row is dense */
2947:       x[i] = *aa; if (idx) idx[i] = 0;
2948:     } else {  /* row is sparse so already KNOW minimum is 0.0 or lower */
2949:       x[i] = 0.0;
2950:       if (idx) {   /* find first implicit 0.0 in the row */
2951:         idx[i] = 0; /* in case ncols is zero */
2952:         for (j=0; j<ncols; j++) {
2953:           if (aj[j] > j) {
2954:             idx[i] = j;
2955:             break;
2956:           }
2957:         }
2958:       }
2959:     }
2960:     for (j=0; j<ncols; j++) {
2961:       if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
2962:       aa++; aj++;
2963:     }
2964:   }
2965:   VecRestoreArray(v,&x);
2966:   return(0);
2967: }

2969:  #include <petscblaslapack.h>
2970:  #include <petsc/private/kernels/blockinvert.h>

2972: PetscErrorCode MatInvertBlockDiagonal_SeqAIJ(Mat A,const PetscScalar **values)
2973: {
2974:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
2976:   PetscInt       i,bs = PetscAbs(A->rmap->bs),mbs = A->rmap->n/bs,ipvt[5],bs2 = bs*bs,*v_pivots,ij[7],*IJ,j;
2977:   MatScalar      *diag,work[25],*v_work;
2978:   PetscReal      shift = 0.0;
2979:   PetscBool      allowzeropivot,zeropivotdetected=PETSC_FALSE;

2982:   allowzeropivot = PetscNot(A->erroriffailure);
2983:   if (a->ibdiagvalid) {
2984:     if (values) *values = a->ibdiag;
2985:     return(0);
2986:   }
2987:   MatMarkDiagonal_SeqAIJ(A);
2988:   if (!a->ibdiag) {
2989:     PetscMalloc1(bs2*mbs,&a->ibdiag);
2990:     PetscLogObjectMemory((PetscObject)A,bs2*mbs*sizeof(PetscScalar));
2991:   }
2992:   diag = a->ibdiag;
2993:   if (values) *values = a->ibdiag;
2994:   /* factor and invert each block */
2995:   switch (bs) {
2996:   case 1:
2997:     for (i=0; i<mbs; i++) {
2998:       MatGetValues(A,1,&i,1,&i,diag+i);
2999:       if (PetscAbsScalar(diag[i] + shift) < PETSC_MACHINE_EPSILON) {
3000:         if (allowzeropivot) {
3001:           A->factorerrortype             = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3002:           A->factorerror_zeropivot_value = PetscAbsScalar(diag[i]);
3003:           A->factorerror_zeropivot_row   = i;
3004:           PetscInfo3(A,"Zero pivot, row %D pivot %g tolerance %g\n",i,(double)PetscAbsScalar(diag[i]),(double)PETSC_MACHINE_EPSILON);
3005:         } 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);
3006:       }
3007:       diag[i] = (PetscScalar)1.0 / (diag[i] + shift);
3008:     }
3009:     break;
3010:   case 2:
3011:     for (i=0; i<mbs; i++) {
3012:       ij[0] = 2*i; ij[1] = 2*i + 1;
3013:       MatGetValues(A,2,ij,2,ij,diag);
3014:       PetscKernel_A_gets_inverse_A_2(diag,shift,allowzeropivot,&zeropivotdetected);
3015:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3016:       PetscKernel_A_gets_transpose_A_2(diag);
3017:       diag += 4;
3018:     }
3019:     break;
3020:   case 3:
3021:     for (i=0; i<mbs; i++) {
3022:       ij[0] = 3*i; ij[1] = 3*i + 1; ij[2] = 3*i + 2;
3023:       MatGetValues(A,3,ij,3,ij,diag);
3024:       PetscKernel_A_gets_inverse_A_3(diag,shift,allowzeropivot,&zeropivotdetected);
3025:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3026:       PetscKernel_A_gets_transpose_A_3(diag);
3027:       diag += 9;
3028:     }
3029:     break;
3030:   case 4:
3031:     for (i=0; i<mbs; i++) {
3032:       ij[0] = 4*i; ij[1] = 4*i + 1; ij[2] = 4*i + 2; ij[3] = 4*i + 3;
3033:       MatGetValues(A,4,ij,4,ij,diag);
3034:       PetscKernel_A_gets_inverse_A_4(diag,shift,allowzeropivot,&zeropivotdetected);
3035:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3036:       PetscKernel_A_gets_transpose_A_4(diag);
3037:       diag += 16;
3038:     }
3039:     break;
3040:   case 5:
3041:     for (i=0; i<mbs; i++) {
3042:       ij[0] = 5*i; ij[1] = 5*i + 1; ij[2] = 5*i + 2; ij[3] = 5*i + 3; ij[4] = 5*i + 4;
3043:       MatGetValues(A,5,ij,5,ij,diag);
3044:       PetscKernel_A_gets_inverse_A_5(diag,ipvt,work,shift,allowzeropivot,&zeropivotdetected);
3045:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3046:       PetscKernel_A_gets_transpose_A_5(diag);
3047:       diag += 25;
3048:     }
3049:     break;
3050:   case 6:
3051:     for (i=0; i<mbs; i++) {
3052:       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;
3053:       MatGetValues(A,6,ij,6,ij,diag);
3054:       PetscKernel_A_gets_inverse_A_6(diag,shift,allowzeropivot,&zeropivotdetected);
3055:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3056:       PetscKernel_A_gets_transpose_A_6(diag);
3057:       diag += 36;
3058:     }
3059:     break;
3060:   case 7:
3061:     for (i=0; i<mbs; i++) {
3062:       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;
3063:       MatGetValues(A,7,ij,7,ij,diag);
3064:       PetscKernel_A_gets_inverse_A_7(diag,shift,allowzeropivot,&zeropivotdetected);
3065:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3066:       PetscKernel_A_gets_transpose_A_7(diag);
3067:       diag += 49;
3068:     }
3069:     break;
3070:   default:
3071:     PetscMalloc3(bs,&v_work,bs,&v_pivots,bs,&IJ);
3072:     for (i=0; i<mbs; i++) {
3073:       for (j=0; j<bs; j++) {
3074:         IJ[j] = bs*i + j;
3075:       }
3076:       MatGetValues(A,bs,IJ,bs,IJ,diag);
3077:       PetscKernel_A_gets_inverse_A(bs,diag,v_pivots,v_work,allowzeropivot,&zeropivotdetected);
3078:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3079:       PetscKernel_A_gets_transpose_A_N(diag,bs);
3080:       diag += bs2;
3081:     }
3082:     PetscFree3(v_work,v_pivots,IJ);
3083:   }
3084:   a->ibdiagvalid = PETSC_TRUE;
3085:   return(0);
3086: }

3088: static PetscErrorCode  MatSetRandom_SeqAIJ(Mat x,PetscRandom rctx)
3089: {
3091:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)x->data;
3092:   PetscScalar    a;
3093:   PetscInt       m,n,i,j,col;

3096:   if (!x->assembled) {
3097:     MatGetSize(x,&m,&n);
3098:     for (i=0; i<m; i++) {
3099:       for (j=0; j<aij->imax[i]; j++) {
3100:         PetscRandomGetValue(rctx,&a);
3101:         col  = (PetscInt)(n*PetscRealPart(a));
3102:         MatSetValues(x,1,&i,1,&col,&a,ADD_VALUES);
3103:       }
3104:     }
3105:   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not yet coded");
3106:   MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);
3107:   MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);
3108:   return(0);
3109: }

3111: PetscErrorCode MatShift_SeqAIJ(Mat Y,PetscScalar a)
3112: {
3114:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)Y->data;

3117:   if (!Y->preallocated || !aij->nz) {
3118:     MatSeqAIJSetPreallocation(Y,1,NULL);
3119:   }
3120:   MatShift_Basic(Y,a);
3121:   return(0);
3122: }

3124: /* -------------------------------------------------------------------*/
3125: static struct _MatOps MatOps_Values = { MatSetValues_SeqAIJ,
3126:                                         MatGetRow_SeqAIJ,
3127:                                         MatRestoreRow_SeqAIJ,
3128:                                         MatMult_SeqAIJ,
3129:                                 /*  4*/ MatMultAdd_SeqAIJ,
3130:                                         MatMultTranspose_SeqAIJ,
3131:                                         MatMultTransposeAdd_SeqAIJ,
3132:                                         0,
3133:                                         0,
3134:                                         0,
3135:                                 /* 10*/ 0,
3136:                                         MatLUFactor_SeqAIJ,
3137:                                         0,
3138:                                         MatSOR_SeqAIJ,
3139:                                         MatTranspose_SeqAIJ,
3140:                                 /*1 5*/ MatGetInfo_SeqAIJ,
3141:                                         MatEqual_SeqAIJ,
3142:                                         MatGetDiagonal_SeqAIJ,
3143:                                         MatDiagonalScale_SeqAIJ,
3144:                                         MatNorm_SeqAIJ,
3145:                                 /* 20*/ 0,
3146:                                         MatAssemblyEnd_SeqAIJ,
3147:                                         MatSetOption_SeqAIJ,
3148:                                         MatZeroEntries_SeqAIJ,
3149:                                 /* 24*/ MatZeroRows_SeqAIJ,
3150:                                         0,
3151:                                         0,
3152:                                         0,
3153:                                         0,
3154:                                 /* 29*/ MatSetUp_SeqAIJ,
3155:                                         0,
3156:                                         0,
3157:                                         0,
3158:                                         0,
3159:                                 /* 34*/ MatDuplicate_SeqAIJ,
3160:                                         0,
3161:                                         0,
3162:                                         MatILUFactor_SeqAIJ,
3163:                                         0,
3164:                                 /* 39*/ MatAXPY_SeqAIJ,
3165:                                         MatCreateSubMatrices_SeqAIJ,
3166:                                         MatIncreaseOverlap_SeqAIJ,
3167:                                         MatGetValues_SeqAIJ,
3168:                                         MatCopy_SeqAIJ,
3169:                                 /* 44*/ MatGetRowMax_SeqAIJ,
3170:                                         MatScale_SeqAIJ,
3171:                                         MatShift_SeqAIJ,
3172:                                         MatDiagonalSet_SeqAIJ,
3173:                                         MatZeroRowsColumns_SeqAIJ,
3174:                                 /* 49*/ MatSetRandom_SeqAIJ,
3175:                                         MatGetRowIJ_SeqAIJ,
3176:                                         MatRestoreRowIJ_SeqAIJ,
3177:                                         MatGetColumnIJ_SeqAIJ,
3178:                                         MatRestoreColumnIJ_SeqAIJ,
3179:                                 /* 54*/ MatFDColoringCreate_SeqXAIJ,
3180:                                         0,
3181:                                         0,
3182:                                         MatPermute_SeqAIJ,
3183:                                         0,
3184:                                 /* 59*/ 0,
3185:                                         MatDestroy_SeqAIJ,
3186:                                         MatView_SeqAIJ,
3187:                                         0,
3188:                                         MatMatMatMult_SeqAIJ_SeqAIJ_SeqAIJ,
3189:                                 /* 64*/ MatMatMatMultSymbolic_SeqAIJ_SeqAIJ_SeqAIJ,
3190:                                         MatMatMatMultNumeric_SeqAIJ_SeqAIJ_SeqAIJ,
3191:                                         0,
3192:                                         0,
3193:                                         0,
3194:                                 /* 69*/ MatGetRowMaxAbs_SeqAIJ,
3195:                                         MatGetRowMinAbs_SeqAIJ,
3196:                                         0,
3197:                                         0,
3198:                                         0,
3199:                                 /* 74*/ 0,
3200:                                         MatFDColoringApply_AIJ,
3201:                                         0,
3202:                                         0,
3203:                                         0,
3204:                                 /* 79*/ MatFindZeroDiagonals_SeqAIJ,
3205:                                         0,
3206:                                         0,
3207:                                         0,
3208:                                         MatLoad_SeqAIJ,
3209:                                 /* 84*/ MatIsSymmetric_SeqAIJ,
3210:                                         MatIsHermitian_SeqAIJ,
3211:                                         0,
3212:                                         0,
3213:                                         0,
3214:                                 /* 89*/ MatMatMult_SeqAIJ_SeqAIJ,
3215:                                         MatMatMultSymbolic_SeqAIJ_SeqAIJ,
3216:                                         MatMatMultNumeric_SeqAIJ_SeqAIJ,
3217:                                         MatPtAP_SeqAIJ_SeqAIJ,
3218:                                         MatPtAPSymbolic_SeqAIJ_SeqAIJ_SparseAxpy,
3219:                                 /* 94*/ MatPtAPNumeric_SeqAIJ_SeqAIJ_SparseAxpy,
3220:                                         MatMatTransposeMult_SeqAIJ_SeqAIJ,
3221:                                         MatMatTransposeMultSymbolic_SeqAIJ_SeqAIJ,
3222:                                         MatMatTransposeMultNumeric_SeqAIJ_SeqAIJ,
3223:                                         0,
3224:                                 /* 99*/ 0,
3225:                                         0,
3226:                                         0,
3227:                                         MatConjugate_SeqAIJ,
3228:                                         0,
3229:                                 /*104*/ MatSetValuesRow_SeqAIJ,
3230:                                         MatRealPart_SeqAIJ,
3231:                                         MatImaginaryPart_SeqAIJ,
3232:                                         0,
3233:                                         0,
3234:                                 /*109*/ MatMatSolve_SeqAIJ,
3235:                                         0,
3236:                                         MatGetRowMin_SeqAIJ,
3237:                                         0,
3238:                                         MatMissingDiagonal_SeqAIJ,
3239:                                 /*114*/ 0,
3240:                                         0,
3241:                                         0,
3242:                                         0,
3243:                                         0,
3244:                                 /*119*/ 0,
3245:                                         0,
3246:                                         0,
3247:                                         0,
3248:                                         MatGetMultiProcBlock_SeqAIJ,
3249:                                 /*124*/ MatFindNonzeroRows_SeqAIJ,
3250:                                         MatGetColumnNorms_SeqAIJ,
3251:                                         MatInvertBlockDiagonal_SeqAIJ,
3252:                                         0,
3253:                                         0,
3254:                                 /*129*/ 0,
3255:                                         MatTransposeMatMult_SeqAIJ_SeqAIJ,
3256:                                         MatTransposeMatMultSymbolic_SeqAIJ_SeqAIJ,
3257:                                         MatTransposeMatMultNumeric_SeqAIJ_SeqAIJ,
3258:                                         MatTransposeColoringCreate_SeqAIJ,
3259:                                 /*134*/ MatTransColoringApplySpToDen_SeqAIJ,
3260:                                         MatTransColoringApplyDenToSp_SeqAIJ,
3261:                                         MatRARt_SeqAIJ_SeqAIJ,
3262:                                         MatRARtSymbolic_SeqAIJ_SeqAIJ,
3263:                                         MatRARtNumeric_SeqAIJ_SeqAIJ,
3264:                                  /*139*/0,
3265:                                         0,
3266:                                         0,
3267:                                         MatFDColoringSetUp_SeqXAIJ,
3268:                                         MatFindOffBlockDiagonalEntries_SeqAIJ,
3269:                                  /*144*/MatCreateMPIMatConcatenateSeqMat_SeqAIJ,
3270:                                         MatDestroySubMatrices_SeqAIJ
3271: };

3273: PetscErrorCode  MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
3274: {
3275:   Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data;
3276:   PetscInt   i,nz,n;

3279:   nz = aij->maxnz;
3280:   n  = mat->rmap->n;
3281:   for (i=0; i<nz; i++) {
3282:     aij->j[i] = indices[i];
3283:   }
3284:   aij->nz = nz;
3285:   for (i=0; i<n; i++) {
3286:     aij->ilen[i] = aij->imax[i];
3287:   }
3288:   return(0);
3289: }

3291: /*@
3292:     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
3293:        in the matrix.

3295:   Input Parameters:
3296: +  mat - the SeqAIJ matrix
3297: -  indices - the column indices

3299:   Level: advanced

3301:   Notes:
3302:     This can be called if you have precomputed the nonzero structure of the
3303:   matrix and want to provide it to the matrix object to improve the performance
3304:   of the MatSetValues() operation.

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

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

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

3313: @*/
3314: PetscErrorCode  MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
3315: {

3321:   PetscUseMethod(mat,"MatSeqAIJSetColumnIndices_C",(Mat,PetscInt*),(mat,indices));
3322:   return(0);
3323: }

3325: /* ----------------------------------------------------------------------------------------*/

3327: PetscErrorCode  MatStoreValues_SeqAIJ(Mat mat)
3328: {
3329:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)mat->data;
3331:   size_t         nz = aij->i[mat->rmap->n];

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

3336:   /* allocate space for values if not already there */
3337:   if (!aij->saved_values) {
3338:     PetscMalloc1(nz+1,&aij->saved_values);
3339:     PetscLogObjectMemory((PetscObject)mat,(nz+1)*sizeof(PetscScalar));
3340:   }

3342:   /* copy values over */
3343:   PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));
3344:   return(0);
3345: }

3347: /*@
3348:     MatStoreValues - Stashes a copy of the matrix values; this allows, for
3349:        example, reuse of the linear part of a Jacobian, while recomputing the
3350:        nonlinear portion.

3352:    Collect on Mat

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

3357:   Level: advanced

3359:   Common Usage, with SNESSolve():
3360: $    Create Jacobian matrix
3361: $    Set linear terms into matrix
3362: $    Apply boundary conditions to matrix, at this time matrix must have
3363: $      final nonzero structure (i.e. setting the nonlinear terms and applying
3364: $      boundary conditions again will not change the nonzero structure
3365: $    MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3366: $    MatStoreValues(mat);
3367: $    Call SNESSetJacobian() with matrix
3368: $    In your Jacobian routine
3369: $      MatRetrieveValues(mat);
3370: $      Set nonlinear terms in matrix

3372:   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
3373: $    // build linear portion of Jacobian
3374: $    MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3375: $    MatStoreValues(mat);
3376: $    loop over nonlinear iterations
3377: $       MatRetrieveValues(mat);
3378: $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
3379: $       // call MatAssemblyBegin/End() on matrix
3380: $       Solve linear system with Jacobian
3381: $    endloop

3383:   Notes:
3384:     Matrix must already be assemblied before calling this routine
3385:     Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
3386:     calling this routine.

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

3391: .seealso: MatRetrieveValues()

3393: @*/
3394: PetscErrorCode  MatStoreValues(Mat mat)
3395: {

3400:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3401:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3402:   PetscUseMethod(mat,"MatStoreValues_C",(Mat),(mat));
3403:   return(0);
3404: }

3406: PetscErrorCode  MatRetrieveValues_SeqAIJ(Mat mat)
3407: {
3408:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)mat->data;
3410:   PetscInt       nz = aij->i[mat->rmap->n];

3413:   if (!aij->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3414:   if (!aij->saved_values) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
3415:   /* copy values over */
3416:   PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));
3417:   return(0);
3418: }

3420: /*@
3421:     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
3422:        example, reuse of the linear part of a Jacobian, while recomputing the
3423:        nonlinear portion.

3425:    Collect on Mat

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

3430:   Level: advanced

3432: .seealso: MatStoreValues()

3434: @*/
3435: PetscErrorCode  MatRetrieveValues(Mat mat)
3436: {

3441:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3442:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3443:   PetscUseMethod(mat,"MatRetrieveValues_C",(Mat),(mat));
3444:   return(0);
3445: }


3448: /* --------------------------------------------------------------------------------*/
3449: /*@C
3450:    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
3451:    (the default parallel PETSc format).  For good matrix assembly performance
3452:    the user should preallocate the matrix storage by setting the parameter nz
3453:    (or the array nnz).  By setting these parameters accurately, performance
3454:    during matrix assembly can be increased by more than a factor of 50.

3456:    Collective on MPI_Comm

3458:    Input Parameters:
3459: +  comm - MPI communicator, set to PETSC_COMM_SELF
3460: .  m - number of rows
3461: .  n - number of columns
3462: .  nz - number of nonzeros per row (same for all rows)
3463: -  nnz - array containing the number of nonzeros in the various rows
3464:          (possibly different for each row) or NULL

3466:    Output Parameter:
3467: .  A - the matrix

3469:    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
3470:    MatXXXXSetPreallocation() paradgm instead of this routine directly.
3471:    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]

3473:    Notes:
3474:    If nnz is given then nz is ignored

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

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

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

3491:    Options Database Keys:
3492: +  -mat_no_inode  - Do not use inodes
3493: -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)

3495:    Level: intermediate

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

3499: @*/
3500: PetscErrorCode  MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
3501: {

3505:   MatCreate(comm,A);
3506:   MatSetSizes(*A,m,n,m,n);
3507:   MatSetType(*A,MATSEQAIJ);
3508:   MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);
3509:   return(0);
3510: }

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

3518:    Collective on MPI_Comm

3520:    Input Parameters:
3521: +  B - The matrix
3522: .  nz - number of nonzeros per row (same for all rows)
3523: -  nnz - array containing the number of nonzeros in the various rows
3524:          (possibly different for each row) or NULL

3526:    Notes:
3527:      If nnz is given then nz is ignored

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

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

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

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

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

3552:    Options Database Keys:
3553: +  -mat_no_inode  - Do not use inodes
3554: -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)

3556:    Level: intermediate

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

3560: @*/
3561: PetscErrorCode  MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
3562: {

3568:   PetscTryMethod(B,"MatSeqAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[]),(B,nz,nnz));
3569:   return(0);
3570: }

3572: PetscErrorCode  MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,const PetscInt *nnz)
3573: {
3574:   Mat_SeqAIJ     *b;
3575:   PetscBool      skipallocation = PETSC_FALSE,realalloc = PETSC_FALSE;
3577:   PetscInt       i;

3580:   if (nz >= 0 || nnz) realalloc = PETSC_TRUE;
3581:   if (nz == MAT_SKIP_ALLOCATION) {
3582:     skipallocation = PETSC_TRUE;
3583:     nz             = 0;
3584:   }
3585:   PetscLayoutSetUp(B->rmap);
3586:   PetscLayoutSetUp(B->cmap);

3588:   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
3589:   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %D",nz);
3590:   if (nnz) {
3591:     for (i=0; i<B->rmap->n; i++) {
3592:       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]);
3593:       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);
3594:     }
3595:   }

3597:   B->preallocated = PETSC_TRUE;

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

3601:   if (!skipallocation) {
3602:     if (!b->imax) {
3603:       PetscMalloc2(B->rmap->n,&b->imax,B->rmap->n,&b->ilen);
3604:       PetscLogObjectMemory((PetscObject)B,2*B->rmap->n*sizeof(PetscInt));
3605:     }
3606:     if (!b->ipre) {
3607:       PetscMalloc1(B->rmap->n,&b->ipre);
3608:       PetscLogObjectMemory((PetscObject)B,B->rmap->n*sizeof(PetscInt));
3609:     }
3610:     if (!nnz) {
3611:       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
3612:       else if (nz < 0) nz = 1;
3613:       for (i=0; i<B->rmap->n; i++) b->imax[i] = nz;
3614:       nz = nz*B->rmap->n;
3615:     } else {
3616:       nz = 0;
3617:       for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
3618:     }
3619:     /* b->ilen will count nonzeros in each row so far. */
3620:     for (i=0; i<B->rmap->n; i++) b->ilen[i] = 0;

3622:     /* allocate the matrix space */
3623:     /* FIXME: should B's old memory be unlogged? */
3624:     MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);
3625:     if (B->structure_only) {
3626:       PetscMalloc1(nz,&b->j);
3627:       PetscMalloc1(B->rmap->n+1,&b->i);
3628:       PetscLogObjectMemory((PetscObject)B,(B->rmap->n+1)*sizeof(PetscInt)+nz*sizeof(PetscInt));
3629:     } else {
3630:       PetscMalloc3(nz,&b->a,nz,&b->j,B->rmap->n+1,&b->i);
3631:       PetscLogObjectMemory((PetscObject)B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));
3632:     }
3633:     b->i[0] = 0;
3634:     for (i=1; i<B->rmap->n+1; i++) {
3635:       b->i[i] = b->i[i-1] + b->imax[i-1];
3636:     }
3637:     if (B->structure_only) {
3638:       b->singlemalloc = PETSC_FALSE;
3639:       b->free_a       = PETSC_FALSE;
3640:     } else {
3641:       b->singlemalloc = PETSC_TRUE;
3642:       b->free_a       = PETSC_TRUE;
3643:     }
3644:     b->free_ij      = PETSC_TRUE;
3645:   } else {
3646:     b->free_a  = PETSC_FALSE;
3647:     b->free_ij = PETSC_FALSE;
3648:   }

3650:   if (b->ipre && nnz != b->ipre  && b->imax) {
3651:     /* reserve user-requested sparsity */
3652:     PetscMemcpy(b->ipre,b->imax,B->rmap->n*sizeof(PetscInt));
3653:   }


3656:   b->nz               = 0;
3657:   b->maxnz            = nz;
3658:   B->info.nz_unneeded = (double)b->maxnz;
3659:   if (realalloc) {
3660:     MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);
3661:   }
3662:   B->was_assembled = PETSC_FALSE;
3663:   B->assembled     = PETSC_FALSE;
3664:   return(0);
3665: }


3668: PetscErrorCode MatResetPreallocation_SeqAIJ(Mat A)
3669: {
3670:   Mat_SeqAIJ     *a;
3671:   PetscInt       i;

3676:   a = (Mat_SeqAIJ*)A->data;
3677:   /* if no saved info, we error out */
3678:   if (!a->ipre) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_ARG_NULL,"No saved preallocation info \n");

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

3682:   PetscMemcpy(a->imax,a->ipre,A->rmap->n*sizeof(PetscInt));
3683:   PetscMemzero(a->ilen,A->rmap->n*sizeof(PetscInt));
3684:   a->i[0] = 0;
3685:   for (i=1; i<A->rmap->n+1; i++) {
3686:     a->i[i] = a->i[i-1] + a->imax[i-1];
3687:   }
3688:   A->preallocated     = PETSC_TRUE;
3689:   a->nz               = 0;
3690:   a->maxnz            = a->i[A->rmap->n];
3691:   A->info.nz_unneeded = (double)a->maxnz;
3692:   A->was_assembled    = PETSC_FALSE;
3693:   A->assembled        = PETSC_FALSE;
3694:   return(0);
3695: }

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

3700:    Input Parameters:
3701: +  B - the matrix
3702: .  i - the indices into j for the start of each row (starts with zero)
3703: .  j - the column indices for each row (starts with zero) these must be sorted for each row
3704: -  v - optional values in the matrix

3706:    Level: developer

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

3710: .keywords: matrix, aij, compressed row, sparse, sequential

3712: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ
3713: @*/
3714: PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
3715: {

3721:   PetscTryMethod(B,"MatSeqAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));
3722:   return(0);
3723: }

3725: PetscErrorCode  MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3726: {
3727:   PetscInt       i;
3728:   PetscInt       m,n;
3729:   PetscInt       nz;
3730:   PetscInt       *nnz, nz_max = 0;
3731:   PetscScalar    *values;

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

3737:   PetscLayoutSetUp(B->rmap);
3738:   PetscLayoutSetUp(B->cmap);

3740:   MatGetSize(B, &m, &n);
3741:   PetscMalloc1(m+1, &nnz);
3742:   for (i = 0; i < m; i++) {
3743:     nz     = Ii[i+1]- Ii[i];
3744:     nz_max = PetscMax(nz_max, nz);
3745:     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
3746:     nnz[i] = nz;
3747:   }
3748:   MatSeqAIJSetPreallocation(B, 0, nnz);
3749:   PetscFree(nnz);

3751:   if (v) {
3752:     values = (PetscScalar*) v;
3753:   } else {
3754:     PetscCalloc1(nz_max, &values);
3755:   }

3757:   for (i = 0; i < m; i++) {
3758:     nz   = Ii[i+1] - Ii[i];
3759:     MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);
3760:   }

3762:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
3763:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);

3765:   if (!v) {
3766:     PetscFree(values);
3767:   }
3768:   MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
3769:   return(0);
3770: }

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

3775: /*
3776:     Computes (B'*A')' since computing B*A directly is untenable

3778:                n                       p                          p
3779:         (              )       (              )         (                  )
3780:       m (      A       )  *  n (       B      )   =   m (         C        )
3781:         (              )       (              )         (                  )

3783: */
3784: PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
3785: {
3786:   PetscErrorCode    ierr;
3787:   Mat_SeqDense      *sub_a = (Mat_SeqDense*)A->data;
3788:   Mat_SeqAIJ        *sub_b = (Mat_SeqAIJ*)B->data;
3789:   Mat_SeqDense      *sub_c = (Mat_SeqDense*)C->data;
3790:   PetscInt          i,n,m,q,p;
3791:   const PetscInt    *ii,*idx;
3792:   const PetscScalar *b,*a,*a_q;
3793:   PetscScalar       *c,*c_q;

3796:   m    = A->rmap->n;
3797:   n    = A->cmap->n;
3798:   p    = B->cmap->n;
3799:   a    = sub_a->v;
3800:   b    = sub_b->a;
3801:   c    = sub_c->v;
3802:   PetscMemzero(c,m*p*sizeof(PetscScalar));

3804:   ii  = sub_b->i;
3805:   idx = sub_b->j;
3806:   for (i=0; i<n; i++) {
3807:     q = ii[i+1] - ii[i];
3808:     while (q-->0) {
3809:       c_q = c + m*(*idx);
3810:       a_q = a + m*i;
3811:       PetscKernelAXPY(c_q,*b,a_q,m);
3812:       idx++;
3813:       b++;
3814:     }
3815:   }
3816:   return(0);
3817: }

3819: PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
3820: {
3822:   PetscInt       m=A->rmap->n,n=B->cmap->n;
3823:   Mat            Cmat;

3826:   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);
3827:   MatCreate(PetscObjectComm((PetscObject)A),&Cmat);
3828:   MatSetSizes(Cmat,m,n,m,n);
3829:   MatSetBlockSizesFromMats(Cmat,A,B);
3830:   MatSetType(Cmat,MATSEQDENSE);
3831:   MatSeqDenseSetPreallocation(Cmat,NULL);

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

3835:   *C = Cmat;
3836:   return(0);
3837: }

3839: /* ----------------------------------------------------------------*/
3840: PETSC_INTERN PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
3841: {

3845:   if (scall == MAT_INITIAL_MATRIX) {
3846:     PetscLogEventBegin(MAT_MatMultSymbolic,A,B,0,0);
3847:     MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);
3848:     PetscLogEventEnd(MAT_MatMultSymbolic,A,B,0,0);
3849:   }
3850:   PetscLogEventBegin(MAT_MatMultNumeric,A,B,0,0);
3851:   MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);
3852:   PetscLogEventEnd(MAT_MatMultNumeric,A,B,0,0);
3853:   return(0);
3854: }


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

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

3864:   Level: beginner

3866: .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
3867: M*/

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

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

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

3881:   Developer Notes:
3882:     Subclasses include MATAIJCUSPARSE, MATAIJPERM, MATAIJMKL, MATAIJCRL, and also automatically switches over to use inodes when
3883:    enough exist.

3885:   Level: beginner

3887: .seealso: MatCreateAIJ(), MatCreateSeqAIJ(), MATSEQAIJ,MATMPIAIJ
3888: M*/

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

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

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

3902:   Level: beginner

3904: .seealso: MatCreateMPIAIJCRL,MATSEQAIJCRL,MATMPIAIJCRL, MATSEQAIJCRL, MATMPIAIJCRL
3905: M*/

3907: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCRL(Mat,MatType,MatReuse,Mat*);
3908: #if defined(PETSC_HAVE_ELEMENTAL)
3909: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_Elemental(Mat,MatType,MatReuse,Mat*);
3910: #endif
3911: #if defined(PETSC_HAVE_HYPRE)
3912: PETSC_INTERN PetscErrorCode MatConvert_AIJ_HYPRE(Mat A,MatType,MatReuse,Mat*);
3913: PETSC_INTERN PetscErrorCode MatMatMatMult_Transpose_AIJ_AIJ(Mat,Mat,Mat,MatReuse,PetscReal,Mat*);
3914: #endif
3915: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqDense(Mat,MatType,MatReuse,Mat*);

3917: #if defined(PETSC_HAVE_MATLAB_ENGINE)
3918: PETSC_EXTERN PetscErrorCode  MatlabEnginePut_SeqAIJ(PetscObject,void*);
3919: PETSC_EXTERN PetscErrorCode  MatlabEngineGet_SeqAIJ(PetscObject,void*);
3920: #endif

3922: PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_SeqSELL(Mat,MatType,MatReuse,Mat*);
3923: PETSC_INTERN PetscErrorCode MatPtAP_IS_XAIJ(Mat,Mat,MatReuse,PetscReal,Mat*);

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

3928:    Not Collective

3930:    Input Parameter:
3931: .  mat - a MATSEQAIJ matrix

3933:    Output Parameter:
3934: .   array - pointer to the data

3936:    Level: intermediate

3938: .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90()
3939: @*/
3940: PetscErrorCode  MatSeqAIJGetArray(Mat A,PetscScalar **array)
3941: {

3945:   PetscUseMethod(A,"MatSeqAIJGetArray_C",(Mat,PetscScalar**),(A,array));
3946:   return(0);
3947: }

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

3952:    Not Collective

3954:    Input Parameter:
3955: .  mat - a MATSEQAIJ matrix

3957:    Output Parameter:
3958: .   nz - the maximum number of nonzeros in any row

3960:    Level: intermediate

3962: .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90()
3963: @*/
3964: PetscErrorCode  MatSeqAIJGetMaxRowNonzeros(Mat A,PetscInt *nz)
3965: {
3966:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)A->data;

3969:   *nz = aij->rmax;
3970:   return(0);
3971: }

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

3976:    Not Collective

3978:    Input Parameters:
3979: .  mat - a MATSEQAIJ matrix
3980: .  array - pointer to the data

3982:    Level: intermediate

3984: .seealso: MatSeqAIJGetArray(), MatSeqAIJRestoreArrayF90()
3985: @*/
3986: PetscErrorCode  MatSeqAIJRestoreArray(Mat A,PetscScalar **array)
3987: {

3991:   PetscUseMethod(A,"MatSeqAIJRestoreArray_C",(Mat,PetscScalar**),(A,array));
3992:   return(0);
3993: }

3995: PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJ(Mat B)
3996: {
3997:   Mat_SeqAIJ     *b;
3999:   PetscMPIInt    size;

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

4005:   PetscNewLog(B,&b);

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

4009:   PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));

4011:   b->row                = 0;
4012:   b->col                = 0;
4013:   b->icol               = 0;
4014:   b->reallocs           = 0;
4015:   b->ignorezeroentries  = PETSC_FALSE;
4016:   b->roworiented        = PETSC_TRUE;
4017:   b->nonew              = 0;
4018:   b->diag               = 0;
4019:   b->solve_work         = 0;
4020:   B->spptr              = 0;
4021:   b->saved_values       = 0;
4022:   b->idiag              = 0;
4023:   b->mdiag              = 0;
4024:   b->ssor_work          = 0;
4025:   b->omega              = 1.0;
4026:   b->fshift             = 0.0;
4027:   b->idiagvalid         = PETSC_FALSE;
4028:   b->ibdiagvalid        = PETSC_FALSE;
4029:   b->keepnonzeropattern = PETSC_FALSE;

4031:   PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);
4032:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJGetArray_C",MatSeqAIJGetArray_SeqAIJ);
4033:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJRestoreArray_C",MatSeqAIJRestoreArray_SeqAIJ);

4035: #if defined(PETSC_HAVE_MATLAB_ENGINE)
4036:   PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEnginePut_C",MatlabEnginePut_SeqAIJ);
4037:   PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEngineGet_C",MatlabEngineGet_SeqAIJ);
4038: #endif

4040:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetColumnIndices_C",MatSeqAIJSetColumnIndices_SeqAIJ);
4041:   PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_SeqAIJ);
4042:   PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_SeqAIJ);
4043:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",MatConvert_SeqAIJ_SeqSBAIJ);
4044:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqbaij_C",MatConvert_SeqAIJ_SeqBAIJ);
4045:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijperm_C",MatConvert_SeqAIJ_SeqAIJPERM);
4046: #if defined(PETSC_HAVE_MKL_SPARSE)
4047:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijmkl_C",MatConvert_SeqAIJ_SeqAIJMKL);
4048: #endif
4049:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijcrl_C",MatConvert_SeqAIJ_SeqAIJCRL);
4050: #if defined(PETSC_HAVE_ELEMENTAL)
4051:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_elemental_C",MatConvert_SeqAIJ_Elemental);
4052: #endif
4053: #if defined(PETSC_HAVE_HYPRE)
4054:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_hypre_C",MatConvert_AIJ_HYPRE);
4055:   PetscObjectComposeFunction((PetscObject)B,"MatMatMatMult_transpose_seqaij_seqaij_C",MatMatMatMult_Transpose_AIJ_AIJ);
4056: #endif
4057:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqdense_C",MatConvert_SeqAIJ_SeqDense);
4058:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqsell_C",MatConvert_SeqAIJ_SeqSELL);
4059:   PetscObjectComposeFunction((PetscObject)B,"MatIsTranspose_C",MatIsTranspose_SeqAIJ);
4060:   PetscObjectComposeFunction((PetscObject)B,"MatIsHermitianTranspose_C",MatIsTranspose_SeqAIJ);
4061:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",MatSeqAIJSetPreallocation_SeqAIJ);
4062:   PetscObjectComposeFunction((PetscObject)B,"MatResetPreallocation_C",MatResetPreallocation_SeqAIJ);
4063:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",MatSeqAIJSetPreallocationCSR_SeqAIJ);
4064:   PetscObjectComposeFunction((PetscObject)B,"MatReorderForNonzeroDiagonal_C",MatReorderForNonzeroDiagonal_SeqAIJ);
4065:   PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqdense_seqaij_C",MatMatMult_SeqDense_SeqAIJ);
4066:   PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C",MatMatMultSymbolic_SeqDense_SeqAIJ);
4067:   PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C",MatMatMultNumeric_SeqDense_SeqAIJ);
4068:   PetscObjectComposeFunction((PetscObject)B,"MatPtAP_is_seqaij_C",MatPtAP_IS_XAIJ);
4069:   MatCreate_SeqAIJ_Inode(B);
4070:   PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);
4071:   MatSeqAIJSetTypeFromOptions(B);  /* this allows changing the matrix subtype to say MATSEQAIJPERM */
4072:   return(0);
4073: }

4075: /*
4076:     Given a matrix generated with MatGetFactor() duplicates all the information in A into B
4077: */
4078: PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscBool mallocmatspace)
4079: {
4080:   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
4082:   PetscInt       i,m = A->rmap->n;

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

4087:   C->factortype = A->factortype;
4088:   c->row        = 0;
4089:   c->col        = 0;
4090:   c->icol       = 0;
4091:   c->reallocs   = 0;

4093:   C->assembled = PETSC_TRUE;

4095:   PetscLayoutReference(A->rmap,&C->rmap);
4096:   PetscLayoutReference(A->cmap,&C->cmap);

4098:   PetscMalloc2(m,&c->imax,m,&c->ilen);
4099:   PetscLogObjectMemory((PetscObject)C, 2*m*sizeof(PetscInt));
4100:   for (i=0; i<m; i++) {
4101:     c->imax[i] = a->imax[i];
4102:     c->ilen[i] = a->ilen[i];
4103:   }

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

4110:     c->singlemalloc = PETSC_TRUE;

4112:     PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));
4113:     if (m > 0) {
4114:       PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));
4115:       if (cpvalues == MAT_COPY_VALUES) {
4116:         PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));
4117:       } else {
4118:         PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));
4119:       }
4120:     }
4121:   }

4123:   c->ignorezeroentries = a->ignorezeroentries;
4124:   c->roworiented       = a->roworiented;
4125:   c->nonew             = a->nonew;
4126:   if (a->diag) {
4127:     PetscMalloc1(m+1,&c->diag);
4128:     PetscLogObjectMemory((PetscObject)C,(m+1)*sizeof(PetscInt));
4129:     for (i=0; i<m; i++) {
4130:       c->diag[i] = a->diag[i];
4131:     }
4132:   } else c->diag = 0;

4134:   c->solve_work         = 0;
4135:   c->saved_values       = 0;
4136:   c->idiag              = 0;
4137:   c->ssor_work          = 0;
4138:   c->keepnonzeropattern = a->keepnonzeropattern;
4139:   c->free_a             = PETSC_TRUE;
4140:   c->free_ij            = PETSC_TRUE;

4142:   c->rmax         = a->rmax;
4143:   c->nz           = a->nz;
4144:   c->maxnz        = a->nz;       /* Since we allocate exactly the right amount */
4145:   C->preallocated = PETSC_TRUE;

4147:   c->compressedrow.use   = a->compressedrow.use;
4148:   c->compressedrow.nrows = a->compressedrow.nrows;
4149:   if (a->compressedrow.use) {
4150:     i    = a->compressedrow.nrows;
4151:     PetscMalloc2(i+1,&c->compressedrow.i,i,&c->compressedrow.rindex);
4152:     PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));
4153:     PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));
4154:   } else {
4155:     c->compressedrow.use    = PETSC_FALSE;
4156:     c->compressedrow.i      = NULL;
4157:     c->compressedrow.rindex = NULL;
4158:   }
4159:   c->nonzerorowcnt = a->nonzerorowcnt;
4160:   C->nonzerostate  = A->nonzerostate;

4162:   MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);
4163:   PetscFunctionListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);
4164:   return(0);
4165: }

4167: PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
4168: {

4172:   MatCreate(PetscObjectComm((PetscObject)A),B);
4173:   MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);
4174:   if (!(A->rmap->n % A->rmap->bs) && !(A->cmap->n % A->cmap->bs)) {
4175:     MatSetBlockSizesFromMats(*B,A,A);
4176:   }
4177:   MatSetType(*B,((PetscObject)A)->type_name);
4178:   MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);
4179:   return(0);
4180: }

4182: PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer)
4183: {
4184:   Mat_SeqAIJ     *a;
4186:   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N,rows,cols;
4187:   int            fd;
4188:   PetscMPIInt    size;
4189:   MPI_Comm       comm;
4190:   PetscInt       bs = newMat->rmap->bs;

4193:   /* force binary viewer to load .info file if it has not yet done so */
4194:   PetscViewerSetUp(viewer);
4195:   PetscObjectGetComm((PetscObject)viewer,&comm);
4196:   MPI_Comm_size(comm,&size);
4197:   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"view must have one processor");

4199:   PetscOptionsBegin(comm,NULL,"Options for loading SEQAIJ matrix","Mat");
4200:   PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,NULL);
4201:   PetscOptionsEnd();
4202:   if (bs < 0) bs = 1;
4203:   MatSetBlockSize(newMat,bs);

4205:   PetscViewerBinaryGetDescriptor(viewer,&fd);
4206:   PetscBinaryRead(fd,header,4,PETSC_INT);
4207:   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
4208:   M = header[1]; N = header[2]; nz = header[3];

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

4212:   /* read in row lengths */
4213:   PetscMalloc1(M,&rowlengths);
4214:   PetscBinaryRead(fd,rowlengths,M,PETSC_INT);

4216:   /* check if sum of rowlengths is same as nz */
4217:   for (i=0,sum=0; i< M; i++) sum +=rowlengths[i];
4218:   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);

4220:   /* set global size if not set already*/
4221:   if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) {
4222:     MatSetSizes(newMat,PETSC_DECIDE,PETSC_DECIDE,M,N);
4223:   } else {
4224:     /* if sizes and type are already set, check if the matrix  global sizes are correct */
4225:     MatGetSize(newMat,&rows,&cols);
4226:     if (rows < 0 && cols < 0) { /* user might provide local size instead of global size */
4227:       MatGetLocalSize(newMat,&rows,&cols);
4228:     }
4229:     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);
4230:   }
4231:   MatSeqAIJSetPreallocation_SeqAIJ(newMat,0,rowlengths);
4232:   a    = (Mat_SeqAIJ*)newMat->data;

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

4236:   /* read in nonzero values */
4237:   PetscBinaryRead(fd,a->a,nz,PETSC_SCALAR);

4239:   /* set matrix "i" values */
4240:   a->i[0] = 0;
4241:   for (i=1; i<= M; i++) {
4242:     a->i[i]      = a->i[i-1] + rowlengths[i-1];
4243:     a->ilen[i-1] = rowlengths[i-1];
4244:   }
4245:   PetscFree(rowlengths);

4247:   MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);
4248:   MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);
4249:   return(0);
4250: }

4252: PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscBool * flg)
4253: {
4254:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*b = (Mat_SeqAIJ*)B->data;
4256: #if defined(PETSC_USE_COMPLEX)
4257:   PetscInt k;
4258: #endif

4261:   /* If the  matrix dimensions are not equal,or no of nonzeros */
4262:   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
4263:     *flg = PETSC_FALSE;
4264:     return(0);
4265:   }

4267:   /* if the a->i are the same */
4268:   PetscMemcmp(a->i,b->i,(A->rmap->n+1)*sizeof(PetscInt),flg);
4269:   if (!*flg) return(0);

4271:   /* if a->j are the same */
4272:   PetscMemcmp(a->j,b->j,(a->nz)*sizeof(PetscInt),flg);
4273:   if (!*flg) return(0);

4275:   /* if a->a are the same */
4276: #if defined(PETSC_USE_COMPLEX)
4277:   for (k=0; k<a->nz; k++) {
4278:     if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])) {
4279:       *flg = PETSC_FALSE;
4280:       return(0);
4281:     }
4282:   }
4283: #else
4284:   PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);
4285: #endif
4286:   return(0);
4287: }

4289: /*@
4290:      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
4291:               provided by the user.

4293:       Collective on MPI_Comm

4295:    Input Parameters:
4296: +   comm - must be an MPI communicator of size 1
4297: .   m - number of rows
4298: .   n - number of columns
4299: .   i - row indices
4300: .   j - column indices
4301: -   a - matrix values

4303:    Output Parameter:
4304: .   mat - the matrix

4306:    Level: intermediate

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

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

4314:        The i and j indices are 0 based

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

4320: $        1 0 0
4321: $        2 0 3
4322: $        4 5 6
4323: $
4324: $        i =  {0,1,3,6}  [size = nrow+1  = 3+1]
4325: $        j =  {0,0,2,0,1,2}  [size = 6]; values must be sorted for each row
4326: $        v =  {1,2,3,4,5,6}  [size = 6]


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

4331: @*/
4332: PetscErrorCode  MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt i[],PetscInt j[],PetscScalar a[],Mat *mat)
4333: {
4335:   PetscInt       ii;
4336:   Mat_SeqAIJ     *aij;
4337: #if defined(PETSC_USE_DEBUG)
4338:   PetscInt jj;
4339: #endif

4342:   if (m > 0 && i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
4343:   MatCreate(comm,mat);
4344:   MatSetSizes(*mat,m,n,m,n);
4345:   /* MatSetBlockSizes(*mat,,); */
4346:   MatSetType(*mat,MATSEQAIJ);
4347:   MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);
4348:   aij  = (Mat_SeqAIJ*)(*mat)->data;
4349:   PetscMalloc2(m,&aij->imax,m,&aij->ilen);

4351:   aij->i            = i;
4352:   aij->j            = j;
4353:   aij->a            = a;
4354:   aij->singlemalloc = PETSC_FALSE;
4355:   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
4356:   aij->free_a       = PETSC_FALSE;
4357:   aij->free_ij      = PETSC_FALSE;

4359:   for (ii=0; ii<m; ii++) {
4360:     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
4361: #if defined(PETSC_USE_DEBUG)
4362:     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]);
4363:     for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
4364:       if (j[jj] < j[jj-1]) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column entry number %D (actual colum %D) in row %D is not sorted",jj-i[ii],j[jj],ii);
4365:       if (j[jj] == j[jj]-1) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column entry number %D (actual colum %D) in row %D is identical to previous entry",jj-i[ii],j[jj],ii);
4366:     }
4367: #endif
4368:   }
4369: #if defined(PETSC_USE_DEBUG)
4370:   for (ii=0; ii<aij->i[m]; ii++) {
4371:     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %D index = %D",ii,j[ii]);
4372:     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]);
4373:   }
4374: #endif

4376:   MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);
4377:   MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);
4378:   return(0);
4379: }
4380: /*@C
4381:      MatCreateSeqAIJFromTriple - Creates an sequential AIJ matrix using matrix elements (in COO format)
4382:               provided by the user.

4384:       Collective on MPI_Comm

4386:    Input Parameters:
4387: +   comm - must be an MPI communicator of size 1
4388: .   m   - number of rows
4389: .   n   - number of columns
4390: .   i   - row indices
4391: .   j   - column indices
4392: .   a   - matrix values
4393: .   nz  - number of nonzeros
4394: -   idx - 0 or 1 based

4396:    Output Parameter:
4397: .   mat - the matrix

4399:    Level: intermediate

4401:    Notes:
4402:        The i and j indices are 0 based

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

4408:         1 0 0
4409:         2 0 3
4410:         4 5 6

4412:         i =  {0,1,1,2,2,2}
4413:         j =  {0,0,2,0,1,2}
4414:         v =  {1,2,3,4,5,6}


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

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


4427:   PetscCalloc1(m,&nnz);
4428:   for (ii = 0; ii < nz; ii++) {
4429:     nnz[i[ii] - !!idx] += 1;
4430:   }
4431:   MatCreate(comm,mat);
4432:   MatSetSizes(*mat,m,n,m,n);
4433:   MatSetType(*mat,MATSEQAIJ);
4434:   MatSeqAIJSetPreallocation_SeqAIJ(*mat,0,nnz);
4435:   for (ii = 0; ii < nz; ii++) {
4436:     if (idx) {
4437:       row = i[ii] - 1;
4438:       col = j[ii] - 1;
4439:     } else {
4440:       row = i[ii];
4441:       col = j[ii];
4442:     }
4443:     MatSetValues(*mat,one,&row,one,&col,&a[ii],ADD_VALUES);
4444:   }
4445:   MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);
4446:   MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);
4447:   PetscFree(nnz);
4448:   return(0);
4449: }

4451: PetscErrorCode MatSeqAIJInvalidateDiagonal(Mat A)
4452: {
4453:   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data;

4457:   a->idiagvalid  = PETSC_FALSE;
4458:   a->ibdiagvalid = PETSC_FALSE;

4460:   MatSeqAIJInvalidateDiagonal_Inode(A);
4461:   return(0);
4462: }

4464: PetscErrorCode MatCreateMPIMatConcatenateSeqMat_SeqAIJ(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat)
4465: {
4467:   PetscMPIInt    size;

4470:   MPI_Comm_size(comm,&size);
4471:   if (size == 1) {
4472:     if (scall == MAT_INITIAL_MATRIX) {
4473:       MatDuplicate(inmat,MAT_COPY_VALUES,outmat);
4474:     } else {
4475:       MatCopy(inmat,*outmat,SAME_NONZERO_PATTERN);
4476:     }
4477:   } else {
4478:     MatCreateMPIMatConcatenateSeqMat_MPIAIJ(comm,inmat,n,scall,outmat);
4479:   }
4480:   return(0);
4481: }

4483: /*
4484:  Permute A into C's *local* index space using rowemb,colemb.
4485:  The embedding are supposed to be injections and the above implies that the range of rowemb is a subset
4486:  of [0,m), colemb is in [0,n).
4487:  If pattern == DIFFERENT_NONZERO_PATTERN, C is preallocated according to A.
4488:  */
4489: PetscErrorCode MatSetSeqMat_SeqAIJ(Mat C,IS rowemb,IS colemb,MatStructure pattern,Mat B)
4490: {
4491:   /* If making this function public, change the error returned in this function away from _PLIB. */
4493:   Mat_SeqAIJ     *Baij;
4494:   PetscBool      seqaij;
4495:   PetscInt       m,n,*nz,i,j,count;
4496:   PetscScalar    v;
4497:   const PetscInt *rowindices,*colindices;

4500:   if (!B) return(0);
4501:   /* Check to make sure the target matrix (and embeddings) are compatible with C and each other. */
4502:   PetscObjectBaseTypeCompare((PetscObject)B,MATSEQAIJ,&seqaij);
4503:   if (!seqaij) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is of wrong type");
4504:   if (rowemb) {
4505:     ISGetLocalSize(rowemb,&m);
4506:     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);
4507:   } else {
4508:     if (C->rmap->n != B->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is row-incompatible with the target matrix");
4509:   }
4510:   if (colemb) {
4511:     ISGetLocalSize(colemb,&n);
4512:     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);
4513:   } else {
4514:     if (C->cmap->n != B->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is col-incompatible with the target matrix");
4515:   }

4517:   Baij = (Mat_SeqAIJ*)(B->data);
4518:   if (pattern == DIFFERENT_NONZERO_PATTERN) {
4519:     PetscMalloc1(B->rmap->n,&nz);
4520:     for (i=0; i<B->rmap->n; i++) {
4521:       nz[i] = Baij->i[i+1] - Baij->i[i];
4522:     }
4523:     MatSeqAIJSetPreallocation(C,0,nz);
4524:     PetscFree(nz);
4525:   }
4526:   if (pattern == SUBSET_NONZERO_PATTERN) {
4527:     MatZeroEntries(C);
4528:   }
4529:   count = 0;
4530:   rowindices = NULL;
4531:   colindices = NULL;
4532:   if (rowemb) {
4533:     ISGetIndices(rowemb,&rowindices);
4534:   }
4535:   if (colemb) {
4536:     ISGetIndices(colemb,&colindices);
4537:   }
4538:   for (i=0; i<B->rmap->n; i++) {
4539:     PetscInt row;
4540:     row = i;
4541:     if (rowindices) row = rowindices[i];
4542:     for (j=Baij->i[i]; j<Baij->i[i+1]; j++) {
4543:       PetscInt col;
4544:       col  = Baij->j[count];
4545:       if (colindices) col = colindices[col];
4546:       v    = Baij->a[count];
4547:       MatSetValues(C,1,&row,1,&col,&v,INSERT_VALUES);
4548:       ++count;
4549:     }
4550:   }
4551:   /* FIXME: set C's nonzerostate correctly. */
4552:   /* Assembly for C is necessary. */
4553:   C->preallocated = PETSC_TRUE;
4554:   C->assembled     = PETSC_TRUE;
4555:   C->was_assembled = PETSC_FALSE;
4556:   return(0);
4557: }

4559: PetscFunctionList MatSeqAIJList = NULL;

4561: /*@C
4562:    MatSeqAIJSetType - Converts a MATSEQAIJ matrix to a subtype

4564:    Collective on Mat

4566:    Input Parameters:
4567: +  mat      - the matrix object
4568: -  matype   - matrix type

4570:    Options Database Key:
4571: .  -mat_seqai_type  <method> - for example seqaijcrl


4574:   Level: intermediate

4576: .keywords: Mat, MatType, set, method

4578: .seealso: PCSetType(), VecSetType(), MatCreate(), MatType, Mat
4579: @*/
4580: PetscErrorCode  MatSeqAIJSetType(Mat mat, MatType matype)
4581: {
4582:   PetscErrorCode ierr,(*r)(Mat,MatType,MatReuse,Mat*);
4583:   PetscBool      sametype;

4587:   PetscObjectTypeCompare((PetscObject)mat,matype,&sametype);
4588:   if (sametype) return(0);

4590:    PetscFunctionListFind(MatSeqAIJList,matype,&r);
4591:   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown Mat type given: %s",matype);
4592:   (*r)(mat,matype,MAT_INPLACE_MATRIX,&mat);
4593:   return(0);
4594: }


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

4600:    Not Collective

4602:    Input Parameters:
4603: +  name - name of a new user-defined matrix type, for example MATSEQAIJCRL
4604: -  function - routine to convert to subtype

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


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

4613:    Level: advanced

4615: .keywords: Mat, register

4617: .seealso: MatSeqAIJRegisterAll()


4620:   Level: advanced
4621: @*/
4622: PetscErrorCode  MatSeqAIJRegister(const char sname[],PetscErrorCode (*function)(Mat,MatType,MatReuse,Mat *))
4623: {

4627:   PetscFunctionListAdd(&MatSeqAIJList,sname,function);
4628:   return(0);
4629: }

4631: PetscBool MatSeqAIJRegisterAllCalled = PETSC_FALSE;

4633: /*@C
4634:   MatSeqAIJRegisterAll - Registers all of the matrix subtypes of SeqAIJ

4636:   Not Collective

4638:   Level: advanced

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

4642: .keywords: KSP, register, all

4644: .seealso:  MatRegisterAll(), MatSeqAIJRegister()
4645: @*/
4646: PetscErrorCode  MatSeqAIJRegisterAll(void)
4647: {

4651:   if (MatSeqAIJRegisterAllCalled) return(0);
4652:   MatSeqAIJRegisterAllCalled = PETSC_TRUE;

4654:   MatSeqAIJRegister(MATSEQAIJCRL,      MatConvert_SeqAIJ_SeqAIJCRL);
4655:   MatSeqAIJRegister(MATSEQAIJPERM,     MatConvert_SeqAIJ_SeqAIJPERM);
4656: #if defined(PETSC_HAVE_MKL_SPARSE)
4657:   MatSeqAIJRegister(MATSEQAIJMKL,      MatConvert_SeqAIJ_SeqAIJMKL);
4658: #endif
4659: #if defined(PETSC_HAVE_VIENNACL) && defined(PETSC_HAVE_VIENNACL_NO_CUDA)
4660:   MatSeqAIJRegister(MATMPIAIJVIENNACL, MatConvert_SeqAIJ_SeqAIJViennaCL);
4661: #endif
4662:   return(0);
4663: }

4665: /*
4666:     Special version for direct calls from Fortran
4667: */
4668:  #include <petsc/private/fortranimpl.h>
4669: #if defined(PETSC_HAVE_FORTRAN_CAPS)
4670: #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
4671: #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
4672: #define matsetvaluesseqaij_ matsetvaluesseqaij
4673: #endif

4675: /* Change these macros so can be used in void function */
4676: #undef CHKERRQ
4677: #define CHKERRQ(ierr) CHKERRABORT(PetscObjectComm((PetscObject)A),ierr)
4678: #undef SETERRQ2
4679: #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
4680: #undef SETERRQ3
4681: #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr)

4683: 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)
4684: {
4685:   Mat            A  = *AA;
4686:   PetscInt       m  = *mm, n = *nn;
4687:   InsertMode     is = *isis;
4688:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
4689:   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
4690:   PetscInt       *imax,*ai,*ailen;
4692:   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
4693:   MatScalar      *ap,value,*aa;
4694:   PetscBool      ignorezeroentries = a->ignorezeroentries;
4695:   PetscBool      roworiented       = a->roworiented;

4698:   MatCheckPreallocated(A,1);
4699:   imax  = a->imax;
4700:   ai    = a->i;
4701:   ailen = a->ilen;
4702:   aj    = a->j;
4703:   aa    = a->a;

4705:   for (k=0; k<m; k++) { /* loop over added rows */
4706:     row = im[k];
4707:     if (row < 0) continue;
4708: #if defined(PETSC_USE_DEBUG)
4709:     if (row >= A->rmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
4710: #endif
4711:     rp   = aj + ai[row]; ap = aa + ai[row];
4712:     rmax = imax[row]; nrow = ailen[row];
4713:     low  = 0;
4714:     high = nrow;
4715:     for (l=0; l<n; l++) { /* loop over added columns */
4716:       if (in[l] < 0) continue;
4717: #if defined(PETSC_USE_DEBUG)
4718:       if (in[l] >= A->cmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
4719: #endif
4720:       col = in[l];
4721:       if (roworiented) value = v[l + k*n];
4722:       else value = v[k + l*m];

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

4726:       if (col <= lastcol) low = 0;
4727:       else high = nrow;
4728:       lastcol = col;
4729:       while (high-low > 5) {
4730:         t = (low+high)/2;
4731:         if (rp[t] > col) high = t;
4732:         else             low  = t;
4733:       }
4734:       for (i=low; i<high; i++) {
4735:         if (rp[i] > col) break;
4736:         if (rp[i] == col) {
4737:           if (is == ADD_VALUES) ap[i] += value;
4738:           else                  ap[i] = value;
4739:           goto noinsert;
4740:         }
4741:       }
4742:       if (value == 0.0 && ignorezeroentries) goto noinsert;
4743:       if (nonew == 1) goto noinsert;
4744:       if (nonew == -1) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
4745:       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
4746:       N = nrow++ - 1; a->nz++; high++;
4747:       /* shift up all the later entries in this row */
4748:       for (ii=N; ii>=i; ii--) {
4749:         rp[ii+1] = rp[ii];
4750:         ap[ii+1] = ap[ii];
4751:       }
4752:       rp[i] = col;
4753:       ap[i] = value;
4754:       A->nonzerostate++;
4755: noinsert:;
4756:       low = i + 1;
4757:     }
4758:     ailen[row] = nrow;
4759:   }
4760:   PetscFunctionReturnVoid();
4761: }