Actual source code: aij.c

petsc-master 2019-03-20
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,"MatConvert_seqaij_seqsell_C",NULL);
1125:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_is_C",NULL);
1126:   PetscObjectComposeFunction((PetscObject)A,"MatIsTranspose_C",NULL);
1127:   PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocation_C",NULL);
1128:   PetscObjectComposeFunction((PetscObject)A,"MatResetPreallocation_C",NULL);
1129:   PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C",NULL);
1130:   PetscObjectComposeFunction((PetscObject)A,"MatReorderForNonzeroDiagonal_C",NULL);
1131:   PetscObjectComposeFunction((PetscObject)A,"MatPtAP_is_seqaij_C",NULL);
1132:   return(0);
1133: }

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

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

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

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

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

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

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

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

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

1274: PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
1275: {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1522: PetscErrorCode MatShift_SeqAIJ(Mat A,PetscScalar v)
1523: {
1524:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1525:   const PetscInt    *diag = (const PetscInt*)a->diag;
1526:   const PetscInt    *ii = (const PetscInt*) a->i;
1527:   PetscInt          i,*mdiag = NULL;
1528:   PetscErrorCode    ierr;
1529:   PetscInt          cnt = 0; /* how many diagonals are missing */

1532:   if (!A->preallocated || !a->nz) {
1533:     MatSeqAIJSetPreallocation(A,1,NULL);
1534:     MatShift_Basic(A,v);
1535:     return(0);
1536:   }

1538:   if (a->diagonaldense) {
1539:     cnt = 0;
1540:   } else {
1541:     PetscCalloc1(A->rmap->n,&mdiag);
1542:     for (i=0; i<A->rmap->n; i++) {
1543:       if (diag[i] >= ii[i+1]) {
1544:         cnt++;
1545:         mdiag[i] = 1;
1546:       }
1547:     }
1548:   }
1549:   if (!cnt) {
1550:     MatShift_Basic(A,v);
1551:   } else {
1552:     PetscScalar *olda = a->a;  /* preserve pointers to current matrix nonzeros structure and values */
1553:     PetscInt    *oldj = a->j, *oldi = a->i;
1554:     PetscBool   singlemalloc = a->singlemalloc,free_a = a->free_a,free_ij = a->free_ij;

1556:     a->a = NULL;
1557:     a->j = NULL;
1558:     a->i = NULL;
1559:     /* increase the values in imax for each row where a diagonal is being inserted then reallocate the matrix data structures */
1560:     for (i=0; i<A->rmap->n; i++) {
1561:       a->imax[i] += mdiag[i];
1562:     }
1563:     MatSeqAIJSetPreallocation_SeqAIJ(A,0,a->imax);

1565:     /* copy old values into new matrix data structure */
1566:     for (i=0; i<A->rmap->n; i++) {
1567:       MatSetValues(A,1,&i,a->imax[i] - mdiag[i],&oldj[oldi[i]],&olda[oldi[i]],ADD_VALUES);
1568:       MatSetValue(A,i,i,v,ADD_VALUES);
1569:     }
1570:     MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
1571:     MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
1572:     if (singlemalloc) {
1573:       PetscFree3(olda,oldj,oldi);
1574:     } else {
1575:       if (free_a)  {PetscFree(olda);}
1576:       if (free_ij) {PetscFree(oldj);}
1577:       if (free_ij) {PetscFree(oldi);}
1578:     }
1579:   }
1580:   PetscFree(mdiag);
1581:   a->diagonaldense = PETSC_TRUE;
1582:   return(0);
1583: }

1585: /*
1586:      Checks for missing diagonals
1587: */
1588: PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscBool  *missing,PetscInt *d)
1589: {
1590:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1591:   PetscInt       *diag,*ii = a->i,i;

1595:   *missing = PETSC_FALSE;
1596:   if (A->rmap->n > 0 && !ii) {
1597:     *missing = PETSC_TRUE;
1598:     if (d) *d = 0;
1599:     PetscInfo(A,"Matrix has no entries therefore is missing diagonal\n");
1600:   } else {
1601:     diag = a->diag;
1602:     for (i=0; i<A->rmap->n; i++) {
1603:       if (diag[i] >= ii[i+1]) {
1604:         *missing = PETSC_TRUE;
1605:         if (d) *d = i;
1606:         PetscInfo1(A,"Matrix is missing diagonal number %D\n",i);
1607:         break;
1608:       }
1609:     }
1610:   }
1611:   return(0);
1612: }

1614:  #include <petscblaslapack.h>
1615:  #include <petsc/private/kernels/blockinvert.h>

1617: /*
1618:     Note that values is allocated externally by the PC and then passed into this routine
1619: */
1620: PetscErrorCode MatInvertVariableBlockDiagonal_SeqAIJ(Mat A,PetscInt nblocks,const PetscInt *bsizes,PetscScalar *diag)
1621: {
1622:   PetscErrorCode  ierr;
1623:   PetscInt        n = A->rmap->n, i, ncnt = 0, *indx,j,bsizemax = 0,*v_pivots;
1624:   PetscBool       allowzeropivot,zeropivotdetected=PETSC_FALSE;
1625:   const PetscReal shift = 0.0;
1626:   PetscInt        ipvt[5];
1627:   PetscScalar     work[25],*v_work;

1630:   allowzeropivot = PetscNot(A->erroriffailure);
1631:   for (i=0; i<nblocks; i++) ncnt += bsizes[i];
1632:   if (ncnt != n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Total blocksizes %D doesn't match number matrix rows %D",ncnt,n);
1633:   for (i=0; i<nblocks; i++) {
1634:     bsizemax = PetscMax(bsizemax,bsizes[i]);
1635:   }
1636:   PetscMalloc1(bsizemax,&indx);
1637:   if (bsizemax > 7) {
1638:     PetscMalloc2(bsizemax,&v_work,bsizemax,&v_pivots);
1639:   }
1640:   ncnt = 0;
1641:   for (i=0; i<nblocks; i++) {
1642:     for (j=0; j<bsizes[i]; j++) indx[j] = ncnt+j;
1643:     MatGetValues(A,bsizes[i],indx,bsizes[i],indx,diag);
1644:     switch (bsizes[i]) {
1645:     case 1:
1646:       *diag = 1.0/(*diag);
1647:       break;
1648:     case 2:
1649:       PetscKernel_A_gets_inverse_A_2(diag,shift,allowzeropivot,&zeropivotdetected);
1650:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1651:       PetscKernel_A_gets_transpose_A_2(diag);
1652:       break;
1653:     case 3:
1654:       PetscKernel_A_gets_inverse_A_3(diag,shift,allowzeropivot,&zeropivotdetected);
1655:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1656:       PetscKernel_A_gets_transpose_A_3(diag);
1657:       break;
1658:     case 4:
1659:       PetscKernel_A_gets_inverse_A_4(diag,shift,allowzeropivot,&zeropivotdetected);
1660:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1661:       PetscKernel_A_gets_transpose_A_4(diag);
1662:       break;
1663:     case 5:
1664:       PetscKernel_A_gets_inverse_A_5(diag,ipvt,work,shift,allowzeropivot,&zeropivotdetected);
1665:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1666:       PetscKernel_A_gets_transpose_A_5(diag);
1667:       break;
1668:     case 6:
1669:       PetscKernel_A_gets_inverse_A_6(diag,shift,allowzeropivot,&zeropivotdetected);
1670:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1671:       PetscKernel_A_gets_transpose_A_6(diag);
1672:       break;
1673:     case 7:
1674:       PetscKernel_A_gets_inverse_A_7(diag,shift,allowzeropivot,&zeropivotdetected);
1675:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1676:       PetscKernel_A_gets_transpose_A_7(diag);
1677:       break;
1678:     default:
1679:       PetscKernel_A_gets_inverse_A(bsizes[i],diag,v_pivots,v_work,allowzeropivot,&zeropivotdetected);
1680:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1681:       PetscKernel_A_gets_transpose_A_N(diag,bsizes[i]);
1682:     }
1683:     ncnt   += bsizes[i];
1684:     diag += bsizes[i]*bsizes[i];
1685:   }
1686:   if (bsizemax > 7) {
1687:     PetscFree2(v_work,v_pivots);
1688:   }
1689:   PetscFree(indx);
1690:   return(0);
1691: }

1693: /*
1694:    Negative shift indicates do not generate an error if there is a zero diagonal, just invert it anyways
1695: */
1696: PetscErrorCode  MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
1697: {
1698:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
1700:   PetscInt       i,*diag,m = A->rmap->n;
1701:   MatScalar      *v = a->a;
1702:   PetscScalar    *idiag,*mdiag;

1705:   if (a->idiagvalid) return(0);
1706:   MatMarkDiagonal_SeqAIJ(A);
1707:   diag = a->diag;
1708:   if (!a->idiag) {
1709:     PetscMalloc3(m,&a->idiag,m,&a->mdiag,m,&a->ssor_work);
1710:     PetscLogObjectMemory((PetscObject)A, 3*m*sizeof(PetscScalar));
1711:     v    = a->a;
1712:   }
1713:   mdiag = a->mdiag;
1714:   idiag = a->idiag;

1716:   if (omega == 1.0 && PetscRealPart(fshift) <= 0.0) {
1717:     for (i=0; i<m; i++) {
1718:       mdiag[i] = v[diag[i]];
1719:       if (!PetscAbsScalar(mdiag[i])) { /* zero diagonal */
1720:         if (PetscRealPart(fshift)) {
1721:           PetscInfo1(A,"Zero diagonal on row %D\n",i);
1722:           A->factorerrortype             = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1723:           A->factorerror_zeropivot_value = 0.0;
1724:           A->factorerror_zeropivot_row   = i;
1725:         } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
1726:       }
1727:       idiag[i] = 1.0/v[diag[i]];
1728:     }
1729:     PetscLogFlops(m);
1730:   } else {
1731:     for (i=0; i<m; i++) {
1732:       mdiag[i] = v[diag[i]];
1733:       idiag[i] = omega/(fshift + v[diag[i]]);
1734:     }
1735:     PetscLogFlops(2.0*m);
1736:   }
1737:   a->idiagvalid = PETSC_TRUE;
1738:   return(0);
1739: }

1741: #include <../src/mat/impls/aij/seq/ftn-kernels/frelax.h>
1742: PetscErrorCode MatSOR_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
1743: {
1744:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1745:   PetscScalar       *x,d,sum,*t,scale;
1746:   const MatScalar   *v,*idiag=0,*mdiag;
1747:   const PetscScalar *b, *bs,*xb, *ts;
1748:   PetscErrorCode    ierr;
1749:   PetscInt          n,m = A->rmap->n,i;
1750:   const PetscInt    *idx,*diag;

1753:   its = its*lits;

1755:   if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
1756:   if (!a->idiagvalid) {MatInvertDiagonal_SeqAIJ(A,omega,fshift);}
1757:   a->fshift = fshift;
1758:   a->omega  = omega;

1760:   diag  = a->diag;
1761:   t     = a->ssor_work;
1762:   idiag = a->idiag;
1763:   mdiag = a->mdiag;

1765:   VecGetArray(xx,&x);
1766:   VecGetArrayRead(bb,&b);
1767:   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
1768:   if (flag == SOR_APPLY_UPPER) {
1769:     /* apply (U + D/omega) to the vector */
1770:     bs = b;
1771:     for (i=0; i<m; i++) {
1772:       d   = fshift + mdiag[i];
1773:       n   = a->i[i+1] - diag[i] - 1;
1774:       idx = a->j + diag[i] + 1;
1775:       v   = a->a + diag[i] + 1;
1776:       sum = b[i]*d/omega;
1777:       PetscSparseDensePlusDot(sum,bs,v,idx,n);
1778:       x[i] = sum;
1779:     }
1780:     VecRestoreArray(xx,&x);
1781:     VecRestoreArrayRead(bb,&b);
1782:     PetscLogFlops(a->nz);
1783:     return(0);
1784:   }

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

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

1793:     to a vector efficiently using Eisenstat's trick.
1794:     */
1795:     scale = (2.0/omega) - 1.0;

1797:     /*  x = (E + U)^{-1} b */
1798:     for (i=m-1; i>=0; i--) {
1799:       n   = a->i[i+1] - diag[i] - 1;
1800:       idx = a->j + diag[i] + 1;
1801:       v   = a->a + diag[i] + 1;
1802:       sum = b[i];
1803:       PetscSparseDenseMinusDot(sum,x,v,idx,n);
1804:       x[i] = sum*idiag[i];
1805:     }

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

1811:     /*  t = (E + L)^{-1}t */
1812:     ts   = t;
1813:     diag = a->diag;
1814:     for (i=0; i<m; i++) {
1815:       n   = diag[i] - a->i[i];
1816:       idx = a->j + a->i[i];
1817:       v   = a->a + a->i[i];
1818:       sum = t[i];
1819:       PetscSparseDenseMinusDot(sum,ts,v,idx,n);
1820:       t[i] = sum*idiag[i];
1821:       /*  x = x + t */
1822:       x[i] += t[i];
1823:     }

1825:     PetscLogFlops(6.0*m-1 + 2.0*a->nz);
1826:     VecRestoreArray(xx,&x);
1827:     VecRestoreArrayRead(bb,&b);
1828:     return(0);
1829:   }
1830:   if (flag & SOR_ZERO_INITIAL_GUESS) {
1831:     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
1832:       for (i=0; i<m; i++) {
1833:         n   = diag[i] - a->i[i];
1834:         idx = a->j + a->i[i];
1835:         v   = a->a + a->i[i];
1836:         sum = b[i];
1837:         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1838:         t[i] = sum;
1839:         x[i] = sum*idiag[i];
1840:       }
1841:       xb   = t;
1842:       PetscLogFlops(a->nz);
1843:     } else xb = b;
1844:     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
1845:       for (i=m-1; i>=0; i--) {
1846:         n   = a->i[i+1] - diag[i] - 1;
1847:         idx = a->j + diag[i] + 1;
1848:         v   = a->a + diag[i] + 1;
1849:         sum = xb[i];
1850:         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1851:         if (xb == b) {
1852:           x[i] = sum*idiag[i];
1853:         } else {
1854:           x[i] = (1-omega)*x[i] + sum*idiag[i];  /* omega in idiag */
1855:         }
1856:       }
1857:       PetscLogFlops(a->nz); /* assumes 1/2 in upper */
1858:     }
1859:     its--;
1860:   }
1861:   while (its--) {
1862:     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
1863:       for (i=0; i<m; i++) {
1864:         /* lower */
1865:         n   = diag[i] - a->i[i];
1866:         idx = a->j + a->i[i];
1867:         v   = a->a + a->i[i];
1868:         sum = b[i];
1869:         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1870:         t[i] = sum;             /* save application of the lower-triangular part */
1871:         /* upper */
1872:         n   = a->i[i+1] - diag[i] - 1;
1873:         idx = a->j + diag[i] + 1;
1874:         v   = a->a + diag[i] + 1;
1875:         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1876:         x[i] = (1. - omega)*x[i] + sum*idiag[i]; /* omega in idiag */
1877:       }
1878:       xb   = t;
1879:       PetscLogFlops(2.0*a->nz);
1880:     } else xb = b;
1881:     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
1882:       for (i=m-1; i>=0; i--) {
1883:         sum = xb[i];
1884:         if (xb == b) {
1885:           /* whole matrix (no checkpointing available) */
1886:           n   = a->i[i+1] - a->i[i];
1887:           idx = a->j + a->i[i];
1888:           v   = a->a + a->i[i];
1889:           PetscSparseDenseMinusDot(sum,x,v,idx,n);
1890:           x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
1891:         } else { /* lower-triangular part has been saved, so only apply upper-triangular */
1892:           n   = a->i[i+1] - diag[i] - 1;
1893:           idx = a->j + diag[i] + 1;
1894:           v   = a->a + diag[i] + 1;
1895:           PetscSparseDenseMinusDot(sum,x,v,idx,n);
1896:           x[i] = (1. - omega)*x[i] + sum*idiag[i];  /* omega in idiag */
1897:         }
1898:       }
1899:       if (xb == b) {
1900:         PetscLogFlops(2.0*a->nz);
1901:       } else {
1902:         PetscLogFlops(a->nz); /* assumes 1/2 in upper */
1903:       }
1904:     }
1905:   }
1906:   VecRestoreArray(xx,&x);
1907:   VecRestoreArrayRead(bb,&b);
1908:   return(0);
1909: }


1912: PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
1913: {
1914:   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;

1917:   info->block_size   = 1.0;
1918:   info->nz_allocated = (double)a->maxnz;
1919:   info->nz_used      = (double)a->nz;
1920:   info->nz_unneeded  = (double)(a->maxnz - a->nz);
1921:   info->assemblies   = (double)A->num_ass;
1922:   info->mallocs      = (double)A->info.mallocs;
1923:   info->memory       = ((PetscObject)A)->mem;
1924:   if (A->factortype) {
1925:     info->fill_ratio_given  = A->info.fill_ratio_given;
1926:     info->fill_ratio_needed = A->info.fill_ratio_needed;
1927:     info->factor_mallocs    = A->info.factor_mallocs;
1928:   } else {
1929:     info->fill_ratio_given  = 0;
1930:     info->fill_ratio_needed = 0;
1931:     info->factor_mallocs    = 0;
1932:   }
1933:   return(0);
1934: }

1936: PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
1937: {
1938:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1939:   PetscInt          i,m = A->rmap->n - 1;
1940:   PetscErrorCode    ierr;
1941:   const PetscScalar *xx;
1942:   PetscScalar       *bb;
1943:   PetscInt          d = 0;

1946:   if (x && b) {
1947:     VecGetArrayRead(x,&xx);
1948:     VecGetArray(b,&bb);
1949:     for (i=0; i<N; i++) {
1950:       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1951:       bb[rows[i]] = diag*xx[rows[i]];
1952:     }
1953:     VecRestoreArrayRead(x,&xx);
1954:     VecRestoreArray(b,&bb);
1955:   }

1957:   if (a->keepnonzeropattern) {
1958:     for (i=0; i<N; i++) {
1959:       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1960:       PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));
1961:     }
1962:     if (diag != 0.0) {
1963:       for (i=0; i<N; i++) {
1964:         d = rows[i];
1965:         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);
1966:       }
1967:       for (i=0; i<N; i++) {
1968:         a->a[a->diag[rows[i]]] = diag;
1969:       }
1970:     }
1971:   } else {
1972:     if (diag != 0.0) {
1973:       for (i=0; i<N; i++) {
1974:         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1975:         if (a->ilen[rows[i]] > 0) {
1976:           a->ilen[rows[i]]    = 1;
1977:           a->a[a->i[rows[i]]] = diag;
1978:           a->j[a->i[rows[i]]] = rows[i];
1979:         } else { /* in case row was completely empty */
1980:           MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);
1981:         }
1982:       }
1983:     } else {
1984:       for (i=0; i<N; i++) {
1985:         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1986:         a->ilen[rows[i]] = 0;
1987:       }
1988:     }
1989:     A->nonzerostate++;
1990:   }
1991:   (*A->ops->assemblyend)(A,MAT_FINAL_ASSEMBLY);
1992:   return(0);
1993: }

1995: PetscErrorCode MatZeroRowsColumns_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
1996: {
1997:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1998:   PetscInt          i,j,m = A->rmap->n - 1,d = 0;
1999:   PetscErrorCode    ierr;
2000:   PetscBool         missing,*zeroed,vecs = PETSC_FALSE;
2001:   const PetscScalar *xx;
2002:   PetscScalar       *bb;

2005:   if (x && b) {
2006:     VecGetArrayRead(x,&xx);
2007:     VecGetArray(b,&bb);
2008:     vecs = PETSC_TRUE;
2009:   }
2010:   PetscCalloc1(A->rmap->n,&zeroed);
2011:   for (i=0; i<N; i++) {
2012:     if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
2013:     PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));

2015:     zeroed[rows[i]] = PETSC_TRUE;
2016:   }
2017:   for (i=0; i<A->rmap->n; i++) {
2018:     if (!zeroed[i]) {
2019:       for (j=a->i[i]; j<a->i[i+1]; j++) {
2020:         if (zeroed[a->j[j]]) {
2021:           if (vecs) bb[i] -= a->a[j]*xx[a->j[j]];
2022:           a->a[j] = 0.0;
2023:         }
2024:       }
2025:     } else if (vecs) bb[i] = diag*xx[i];
2026:   }
2027:   if (x && b) {
2028:     VecRestoreArrayRead(x,&xx);
2029:     VecRestoreArray(b,&bb);
2030:   }
2031:   PetscFree(zeroed);
2032:   if (diag != 0.0) {
2033:     MatMissingDiagonal_SeqAIJ(A,&missing,&d);
2034:     if (missing) {
2035:       if (a->nonew) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
2036:       else {
2037:         for (i=0; i<N; i++) {
2038:           MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);
2039:         }
2040:       }
2041:     } else {
2042:       for (i=0; i<N; i++) {
2043:         a->a[a->diag[rows[i]]] = diag;
2044:       }
2045:     }
2046:   }
2047:   (*A->ops->assemblyend)(A,MAT_FINAL_ASSEMBLY);
2048:   return(0);
2049: }

2051: PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
2052: {
2053:   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2054:   PetscInt   *itmp;

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

2059:   *nz = a->i[row+1] - a->i[row];
2060:   if (v) *v = a->a + a->i[row];
2061:   if (idx) {
2062:     itmp = a->j + a->i[row];
2063:     if (*nz) *idx = itmp;
2064:     else *idx = 0;
2065:   }
2066:   return(0);
2067: }

2069: /* remove this function? */
2070: PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
2071: {
2073:   return(0);
2074: }

2076: PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
2077: {
2078:   Mat_SeqAIJ     *a  = (Mat_SeqAIJ*)A->data;
2079:   MatScalar      *v  = a->a;
2080:   PetscReal      sum = 0.0;
2082:   PetscInt       i,j;

2085:   if (type == NORM_FROBENIUS) {
2086: #if defined(PETSC_USE_REAL___FP16)
2087:     PetscBLASInt one = 1,nz = a->nz;
2088:     *nrm = BLASnrm2_(&nz,v,&one);
2089: #else
2090:     for (i=0; i<a->nz; i++) {
2091:       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
2092:     }
2093:     *nrm = PetscSqrtReal(sum);
2094: #endif
2095:     PetscLogFlops(2*a->nz);
2096:   } else if (type == NORM_1) {
2097:     PetscReal *tmp;
2098:     PetscInt  *jj = a->j;
2099:     PetscCalloc1(A->cmap->n+1,&tmp);
2100:     *nrm = 0.0;
2101:     for (j=0; j<a->nz; j++) {
2102:       tmp[*jj++] += PetscAbsScalar(*v);  v++;
2103:     }
2104:     for (j=0; j<A->cmap->n; j++) {
2105:       if (tmp[j] > *nrm) *nrm = tmp[j];
2106:     }
2107:     PetscFree(tmp);
2108:     PetscLogFlops(PetscMax(a->nz-1,0));
2109:   } else if (type == NORM_INFINITY) {
2110:     *nrm = 0.0;
2111:     for (j=0; j<A->rmap->n; j++) {
2112:       v   = a->a + a->i[j];
2113:       sum = 0.0;
2114:       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
2115:         sum += PetscAbsScalar(*v); v++;
2116:       }
2117:       if (sum > *nrm) *nrm = sum;
2118:     }
2119:     PetscLogFlops(PetscMax(a->nz-1,0));
2120:   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for two norm");
2121:   return(0);
2122: }

2124: /* Merged from MatGetSymbolicTranspose_SeqAIJ() - replace MatGetSymbolicTranspose_SeqAIJ()? */
2125: PetscErrorCode MatTransposeSymbolic_SeqAIJ(Mat A,Mat *B)
2126: {
2128:   PetscInt       i,j,anzj;
2129:   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data,*b;
2130:   PetscInt       an=A->cmap->N,am=A->rmap->N;
2131:   PetscInt       *ati,*atj,*atfill,*ai=a->i,*aj=a->j;

2134:   /* Allocate space for symbolic transpose info and work array */
2135:   PetscCalloc1(an+1,&ati);
2136:   PetscMalloc1(ai[am],&atj);
2137:   PetscMalloc1(an,&atfill);

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

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

2148:   /* Walk through A row-wise and mark nonzero entries of A^T. */
2149:   for (i=0;i<am;i++) {
2150:     anzj = ai[i+1] - ai[i];
2151:     for (j=0;j<anzj;j++) {
2152:       atj[atfill[*aj]] = i;
2153:       atfill[*aj++]   += 1;
2154:     }
2155:   }

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

2162:   b          = (Mat_SeqAIJ*)((*B)->data);
2163:   b->free_a  = PETSC_FALSE;
2164:   b->free_ij = PETSC_TRUE;
2165:   b->nonew   = 0;
2166:   return(0);
2167: }

2169: PetscErrorCode MatTranspose_SeqAIJ(Mat A,MatReuse reuse,Mat *B)
2170: {
2171:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2172:   Mat            C;
2174:   PetscInt       i,*aj = a->j,*ai = a->i,m = A->rmap->n,len,*col;
2175:   MatScalar      *array = a->a;

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

2181:     for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
2182:     MatCreate(PetscObjectComm((PetscObject)A),&C);
2183:     MatSetSizes(C,A->cmap->n,m,A->cmap->n,m);
2184:     MatSetBlockSizes(C,PetscAbs(A->cmap->bs),PetscAbs(A->rmap->bs));
2185:     MatSetType(C,((PetscObject)A)->type_name);
2186:     MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);
2187:     PetscFree(col);
2188:   } else {
2189:     C = *B;
2190:   }

2192:   for (i=0; i<m; i++) {
2193:     len    = ai[i+1]-ai[i];
2194:     MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);
2195:     array += len;
2196:     aj    += len;
2197:   }
2198:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
2199:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);

2201:   if (reuse == MAT_INITIAL_MATRIX || reuse == MAT_REUSE_MATRIX) {
2202:     *B = C;
2203:   } else {
2204:     MatHeaderMerge(A,&C);
2205:   }
2206:   return(0);
2207: }

2209: PetscErrorCode  MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
2210: {
2211:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) B->data;
2212:   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
2213:   MatScalar      *va,*vb;
2215:   PetscInt       ma,na,mb,nb, i;

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

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

2257: PetscErrorCode  MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
2258: {
2259:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) B->data;
2260:   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
2261:   MatScalar      *va,*vb;
2263:   PetscInt       ma,na,mb,nb, i;

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

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

2305: PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
2306: {

2310:   MatIsTranspose_SeqAIJ(A,A,tol,f);
2311:   return(0);
2312: }

2314: PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
2315: {

2319:   MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);
2320:   return(0);
2321: }

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

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

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


2377:   ISGetIndices(isrow,&irow);
2378:   ISGetLocalSize(isrow,&nrows);
2379:   ISGetLocalSize(iscol,&ncols);

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

2428:     /* loop over rows inserting into submatrix */
2429:     a_new = c->a;
2430:     j_new = c->j;
2431:     i_new = c->i;

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

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

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

2502:         }
2503:       }
2504:     }
2505:     /* Free work space */
2506:     ISRestoreIndices(iscol,&icol);
2507:     PetscFree(smap);
2508:     PetscFree(lens);
2509:     /* sort */
2510:     for (i = 0; i < nrows; i++) {
2511:       PetscInt ilen;

2513:       mat_i = c->i[i];
2514:       mat_j = c->j + mat_i;
2515:       mat_a = c->a + mat_i;
2516:       ilen  = c->ilen[i];
2517:       PetscSortIntWithScalarArray(ilen,mat_j,mat_a);
2518:     }
2519:   }
2520:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
2521:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);

2523:   ISRestoreIndices(isrow,&irow);
2524:   *B   = C;
2525:   return(0);
2526: }

2528: PetscErrorCode  MatGetMultiProcBlock_SeqAIJ(Mat mat,MPI_Comm subComm,MatReuse scall,Mat *subMat)
2529: {
2531:   Mat            B;

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

2547: PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
2548: {
2549:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
2551:   Mat            outA;
2552:   PetscBool      row_identity,col_identity;

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

2557:   ISIdentity(row,&row_identity);
2558:   ISIdentity(col,&col_identity);

2560:   outA             = inA;
2561:   outA->factortype = MAT_FACTOR_LU;
2562:   PetscFree(inA->solvertype);
2563:   PetscStrallocpy(MATSOLVERPETSC,&inA->solvertype);

2565:   PetscObjectReference((PetscObject)row);
2566:   ISDestroy(&a->row);

2568:   a->row = row;

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

2573:   a->col = col;

2575:   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
2576:   ISDestroy(&a->icol);
2577:   ISInvertPermutation(col,PETSC_DECIDE,&a->icol);
2578:   PetscLogObjectParent((PetscObject)inA,(PetscObject)a->icol);

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

2585:   MatMarkDiagonal_SeqAIJ(inA);
2586:   if (row_identity && col_identity) {
2587:     MatLUFactorNumeric_SeqAIJ_inplace(outA,inA,info);
2588:   } else {
2589:     MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);
2590:   }
2591:   return(0);
2592: }

2594: PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
2595: {
2596:   Mat_SeqAIJ     *a     = (Mat_SeqAIJ*)inA->data;
2597:   PetscScalar    oalpha = alpha;
2599:   PetscBLASInt   one = 1,bnz;

2602:   PetscBLASIntCast(a->nz,&bnz);
2603:   PetscStackCallBLAS("BLASscal",BLASscal_(&bnz,&oalpha,a->a,&one));
2604:   PetscLogFlops(a->nz);
2605:   MatSeqAIJInvalidateDiagonal(inA);
2606:   return(0);
2607: }

2609: PetscErrorCode MatDestroySubMatrix_Private(Mat_SubSppt *submatj)
2610: {
2612:   PetscInt       i;

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

2618:     for (i=0; i<submatj->nrqr; ++i) {
2619:       PetscFree(submatj->sbuf2[i]);
2620:     }
2621:     PetscFree3(submatj->sbuf2,submatj->req_size,submatj->req_source1);

2623:     if (submatj->rbuf1) {
2624:       PetscFree(submatj->rbuf1[0]);
2625:       PetscFree(submatj->rbuf1);
2626:     }

2628:     for (i=0; i<submatj->nrqs; ++i) {
2629:       PetscFree(submatj->rbuf3[i]);
2630:     }
2631:     PetscFree3(submatj->req_source2,submatj->rbuf2,submatj->rbuf3);
2632:     PetscFree(submatj->pa);
2633:   }

2635: #if defined(PETSC_USE_CTABLE)
2636:   PetscTableDestroy((PetscTable*)&submatj->rmap);
2637:   if (submatj->cmap_loc) {PetscFree(submatj->cmap_loc);}
2638:   PetscFree(submatj->rmap_loc);
2639: #else
2640:   PetscFree(submatj->rmap);
2641: #endif

2643:   if (!submatj->allcolumns) {
2644: #if defined(PETSC_USE_CTABLE)
2645:     PetscTableDestroy((PetscTable*)&submatj->cmap);
2646: #else
2647:     PetscFree(submatj->cmap);
2648: #endif
2649:   }
2650:   PetscFree(submatj->row2proc);

2652:   PetscFree(submatj);
2653:   return(0);
2654: }

2656: PetscErrorCode MatDestroySubMatrix_SeqAIJ(Mat C)
2657: {
2659:   Mat_SeqAIJ     *c = (Mat_SeqAIJ*)C->data;
2660:   Mat_SubSppt    *submatj = c->submatis1;

2663:   (*submatj->destroy)(C);
2664:   MatDestroySubMatrix_Private(submatj);
2665:   return(0);
2666: }

2668: PetscErrorCode MatDestroySubMatrices_SeqAIJ(PetscInt n,Mat *mat[])
2669: {
2671:   PetscInt       i;
2672:   Mat            C;
2673:   Mat_SeqAIJ     *c;
2674:   Mat_SubSppt    *submatj;

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

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

2698:   PetscFree(*mat);
2699:   return(0);
2700: }

2702: PetscErrorCode MatCreateSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
2703: {
2705:   PetscInt       i;

2708:   if (scall == MAT_INITIAL_MATRIX) {
2709:     PetscCalloc1(n+1,B);
2710:   }

2712:   for (i=0; i<n; i++) {
2713:     MatCreateSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);
2714:   }
2715:   return(0);
2716: }

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

2728:   m  = A->rmap->n;
2729:   ai = a->i;
2730:   aj = a->j;

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

2734:   PetscMalloc1(m+1,&nidx);
2735:   PetscBTCreate(m,&table);

2737:   for (i=0; i<is_max; i++) {
2738:     /* Initialize the two local arrays */
2739:     isz  = 0;
2740:     PetscBTMemzero(m,table);

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

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

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

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

2786:   ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);
2787:   ISGetIndices(irowp,&row);
2788:   ISInvertPermutation(colp,PETSC_DECIDE,&icolp);
2789:   ISGetIndices(icolp,&col);

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

2801:   PetscMalloc1(n,&cnew);
2802:   for (i=0; i<m; i++) {
2803:     MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);
2804:     for (j=0; j<nz; j++) cnew[j] = col[cwork[j]];
2805:     MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);
2806:     MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);
2807:   }
2808:   PetscFree(cnew);

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

2812:   MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);
2813:   MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);
2814:   ISRestoreIndices(irowp,&row);
2815:   ISRestoreIndices(icolp,&col);
2816:   ISDestroy(&irowp);
2817:   ISDestroy(&icolp);
2818:   return(0);
2819: }

2821: PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
2822: {

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

2831:     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");
2832:     PetscMemcpy(b->a,a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));
2833:     PetscObjectStateIncrease((PetscObject)B);
2834:   } else {
2835:     MatCopy_Basic(A,B,str);
2836:   }
2837:   return(0);
2838: }

2840: PetscErrorCode MatSetUp_SeqAIJ(Mat A)
2841: {

2845:    MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);
2846:   return(0);
2847: }

2849: PetscErrorCode MatSeqAIJGetArray_SeqAIJ(Mat A,PetscScalar *array[])
2850: {
2851:   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;

2854:   *array = a->a;
2855:   return(0);
2856: }

2858: PetscErrorCode MatSeqAIJRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
2859: {
2861:   return(0);
2862: }

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

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

2889: PetscErrorCode MatAXPYGetPreallocation_SeqAIJ(Mat Y,Mat X,PetscInt *nnz)
2890: {
2891:   PetscInt       m = Y->rmap->N;
2892:   Mat_SeqAIJ     *x = (Mat_SeqAIJ*)X->data;
2893:   Mat_SeqAIJ     *y = (Mat_SeqAIJ*)Y->data;

2897:   /* Set the number of nonzeros in the new matrix */
2898:   MatAXPYGetPreallocation_SeqX_private(m,x->i,x->j,y->i,y->j,nnz);
2899:   return(0);
2900: }

2902: PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2903: {
2905:   Mat_SeqAIJ     *x = (Mat_SeqAIJ*)X->data,*y = (Mat_SeqAIJ*)Y->data;
2906:   PetscBLASInt   one=1,bnz;

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

2935: PetscErrorCode  MatConjugate_SeqAIJ(Mat mat)
2936: {
2937: #if defined(PETSC_USE_COMPLEX)
2938:   Mat_SeqAIJ  *aij = (Mat_SeqAIJ*)mat->data;
2939:   PetscInt    i,nz;
2940:   PetscScalar *a;

2943:   nz = aij->nz;
2944:   a  = aij->a;
2945:   for (i=0; i<nz; i++) a[i] = PetscConj(a[i]);
2946: #else
2948: #endif
2949:   return(0);
2950: }

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

2962:   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2963:   aa = a->a;
2964:   ai = a->i;
2965:   aj = a->j;

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

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

2993:   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2994:   aa = a->a;
2995:   ai = a->i;
2996:   aj = a->j;

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

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

3037:   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3038:   aa = a->a;
3039:   ai = a->i;
3040:   aj = a->j;

3042:   VecSet(v,0.0);
3043:   VecGetArray(v,&x);
3044:   VecGetLocalSize(v,&n);
3045:   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);
3046:   for (i=0; i<m; i++) {
3047:     ncols = ai[1] - ai[0]; ai++;
3048:     if (ncols) {
3049:       /* Get first nonzero */
3050:       for (j = 0; j < ncols; j++) {
3051:         atmp = PetscAbsScalar(aa[j]);
3052:         if (atmp > 1.0e-12) {
3053:           x[i] = atmp;
3054:           if (idx) idx[i] = aj[j];
3055:           break;
3056:         }
3057:       }
3058:       if (j == ncols) {x[i] = PetscAbsScalar(*aa); if (idx) idx[i] = *aj;}
3059:     } else {
3060:       x[i] = 0.0; if (idx) idx[i] = 0;
3061:     }
3062:     for (j = 0; j < ncols; j++) {
3063:       atmp = PetscAbsScalar(*aa);
3064:       if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
3065:       aa++; aj++;
3066:     }
3067:   }
3068:   VecRestoreArray(v,&x);
3069:   return(0);
3070: }

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

3082:   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3083:   aa = a->a;
3084:   ai = a->i;
3085:   aj = a->j;

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

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

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

3232: static PetscErrorCode  MatSetRandom_SeqAIJ(Mat x,PetscRandom rctx)
3233: {
3235:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)x->data;
3236:   PetscScalar    a;
3237:   PetscInt       m,n,i,j,col;

3240:   if (!x->assembled) {
3241:     MatGetSize(x,&m,&n);
3242:     for (i=0; i<m; i++) {
3243:       for (j=0; j<aij->imax[i]; j++) {
3244:         PetscRandomGetValue(rctx,&a);
3245:         col  = (PetscInt)(n*PetscRealPart(a));
3246:         MatSetValues(x,1,&i,1,&col,&a,ADD_VALUES);
3247:       }
3248:     }
3249:   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not yet coded");
3250:   MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);
3251:   MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);
3252:   return(0);
3253: }

3255: /* -------------------------------------------------------------------*/
3256: static struct _MatOps MatOps_Values = { MatSetValues_SeqAIJ,
3257:                                         MatGetRow_SeqAIJ,
3258:                                         MatRestoreRow_SeqAIJ,
3259:                                         MatMult_SeqAIJ,
3260:                                 /*  4*/ MatMultAdd_SeqAIJ,
3261:                                         MatMultTranspose_SeqAIJ,
3262:                                         MatMultTransposeAdd_SeqAIJ,
3263:                                         0,
3264:                                         0,
3265:                                         0,
3266:                                 /* 10*/ 0,
3267:                                         MatLUFactor_SeqAIJ,
3268:                                         0,
3269:                                         MatSOR_SeqAIJ,
3270:                                         MatTranspose_SeqAIJ_FAST,
3271:                                 /*1 5*/ MatGetInfo_SeqAIJ,
3272:                                         MatEqual_SeqAIJ,
3273:                                         MatGetDiagonal_SeqAIJ,
3274:                                         MatDiagonalScale_SeqAIJ,
3275:                                         MatNorm_SeqAIJ,
3276:                                 /* 20*/ 0,
3277:                                         MatAssemblyEnd_SeqAIJ,
3278:                                         MatSetOption_SeqAIJ,
3279:                                         MatZeroEntries_SeqAIJ,
3280:                                 /* 24*/ MatZeroRows_SeqAIJ,
3281:                                         0,
3282:                                         0,
3283:                                         0,
3284:                                         0,
3285:                                 /* 29*/ MatSetUp_SeqAIJ,
3286:                                         0,
3287:                                         0,
3288:                                         0,
3289:                                         0,
3290:                                 /* 34*/ MatDuplicate_SeqAIJ,
3291:                                         0,
3292:                                         0,
3293:                                         MatILUFactor_SeqAIJ,
3294:                                         0,
3295:                                 /* 39*/ MatAXPY_SeqAIJ,
3296:                                         MatCreateSubMatrices_SeqAIJ,
3297:                                         MatIncreaseOverlap_SeqAIJ,
3298:                                         MatGetValues_SeqAIJ,
3299:                                         MatCopy_SeqAIJ,
3300:                                 /* 44*/ MatGetRowMax_SeqAIJ,
3301:                                         MatScale_SeqAIJ,
3302:                                         MatShift_SeqAIJ,
3303:                                         MatDiagonalSet_SeqAIJ,
3304:                                         MatZeroRowsColumns_SeqAIJ,
3305:                                 /* 49*/ MatSetRandom_SeqAIJ,
3306:                                         MatGetRowIJ_SeqAIJ,
3307:                                         MatRestoreRowIJ_SeqAIJ,
3308:                                         MatGetColumnIJ_SeqAIJ,
3309:                                         MatRestoreColumnIJ_SeqAIJ,
3310:                                 /* 54*/ MatFDColoringCreate_SeqXAIJ,
3311:                                         0,
3312:                                         0,
3313:                                         MatPermute_SeqAIJ,
3314:                                         0,
3315:                                 /* 59*/ 0,
3316:                                         MatDestroy_SeqAIJ,
3317:                                         MatView_SeqAIJ,
3318:                                         0,
3319:                                         MatMatMatMult_SeqAIJ_SeqAIJ_SeqAIJ,
3320:                                 /* 64*/ MatMatMatMultSymbolic_SeqAIJ_SeqAIJ_SeqAIJ,
3321:                                         MatMatMatMultNumeric_SeqAIJ_SeqAIJ_SeqAIJ,
3322:                                         0,
3323:                                         0,
3324:                                         0,
3325:                                 /* 69*/ MatGetRowMaxAbs_SeqAIJ,
3326:                                         MatGetRowMinAbs_SeqAIJ,
3327:                                         0,
3328:                                         0,
3329:                                         0,
3330:                                 /* 74*/ 0,
3331:                                         MatFDColoringApply_AIJ,
3332:                                         0,
3333:                                         0,
3334:                                         0,
3335:                                 /* 79*/ MatFindZeroDiagonals_SeqAIJ,
3336:                                         0,
3337:                                         0,
3338:                                         0,
3339:                                         MatLoad_SeqAIJ,
3340:                                 /* 84*/ MatIsSymmetric_SeqAIJ,
3341:                                         MatIsHermitian_SeqAIJ,
3342:                                         0,
3343:                                         0,
3344:                                         0,
3345:                                 /* 89*/ MatMatMult_SeqAIJ_SeqAIJ,
3346:                                         MatMatMultSymbolic_SeqAIJ_SeqAIJ,
3347:                                         MatMatMultNumeric_SeqAIJ_SeqAIJ,
3348:                                         MatPtAP_SeqAIJ_SeqAIJ,
3349:                                         MatPtAPSymbolic_SeqAIJ_SeqAIJ_SparseAxpy,
3350:                                 /* 94*/ MatPtAPNumeric_SeqAIJ_SeqAIJ_SparseAxpy,
3351:                                         MatMatTransposeMult_SeqAIJ_SeqAIJ,
3352:                                         MatMatTransposeMultSymbolic_SeqAIJ_SeqAIJ,
3353:                                         MatMatTransposeMultNumeric_SeqAIJ_SeqAIJ,
3354:                                         0,
3355:                                 /* 99*/ 0,
3356:                                         0,
3357:                                         0,
3358:                                         MatConjugate_SeqAIJ,
3359:                                         0,
3360:                                 /*104*/ MatSetValuesRow_SeqAIJ,
3361:                                         MatRealPart_SeqAIJ,
3362:                                         MatImaginaryPart_SeqAIJ,
3363:                                         0,
3364:                                         0,
3365:                                 /*109*/ MatMatSolve_SeqAIJ,
3366:                                         0,
3367:                                         MatGetRowMin_SeqAIJ,
3368:                                         0,
3369:                                         MatMissingDiagonal_SeqAIJ,
3370:                                 /*114*/ 0,
3371:                                         0,
3372:                                         0,
3373:                                         0,
3374:                                         0,
3375:                                 /*119*/ 0,
3376:                                         0,
3377:                                         0,
3378:                                         0,
3379:                                         MatGetMultiProcBlock_SeqAIJ,
3380:                                 /*124*/ MatFindNonzeroRows_SeqAIJ,
3381:                                         MatGetColumnNorms_SeqAIJ,
3382:                                         MatInvertBlockDiagonal_SeqAIJ,
3383:                                         MatInvertVariableBlockDiagonal_SeqAIJ,
3384:                                         0,
3385:                                 /*129*/ 0,
3386:                                         MatTransposeMatMult_SeqAIJ_SeqAIJ,
3387:                                         MatTransposeMatMultSymbolic_SeqAIJ_SeqAIJ,
3388:                                         MatTransposeMatMultNumeric_SeqAIJ_SeqAIJ,
3389:                                         MatTransposeColoringCreate_SeqAIJ,
3390:                                 /*134*/ MatTransColoringApplySpToDen_SeqAIJ,
3391:                                         MatTransColoringApplyDenToSp_SeqAIJ,
3392:                                         MatRARt_SeqAIJ_SeqAIJ,
3393:                                         MatRARtSymbolic_SeqAIJ_SeqAIJ,
3394:                                         MatRARtNumeric_SeqAIJ_SeqAIJ,
3395:                                  /*139*/0,
3396:                                         0,
3397:                                         0,
3398:                                         MatFDColoringSetUp_SeqXAIJ,
3399:                                         MatFindOffBlockDiagonalEntries_SeqAIJ,
3400:                                  /*144*/MatCreateMPIMatConcatenateSeqMat_SeqAIJ,
3401:                                         MatDestroySubMatrices_SeqAIJ
3402: };

3404: PetscErrorCode  MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
3405: {
3406:   Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data;
3407:   PetscInt   i,nz,n;

3410:   nz = aij->maxnz;
3411:   n  = mat->rmap->n;
3412:   for (i=0; i<nz; i++) {
3413:     aij->j[i] = indices[i];
3414:   }
3415:   aij->nz = nz;
3416:   for (i=0; i<n; i++) {
3417:     aij->ilen[i] = aij->imax[i];
3418:   }
3419:   return(0);
3420: }

3422: /*@
3423:     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
3424:        in the matrix.

3426:   Input Parameters:
3427: +  mat - the SeqAIJ matrix
3428: -  indices - the column indices

3430:   Level: advanced

3432:   Notes:
3433:     This can be called if you have precomputed the nonzero structure of the
3434:   matrix and want to provide it to the matrix object to improve the performance
3435:   of the MatSetValues() operation.

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

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

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

3444: @*/
3445: PetscErrorCode  MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
3446: {

3452:   PetscUseMethod(mat,"MatSeqAIJSetColumnIndices_C",(Mat,PetscInt*),(mat,indices));
3453:   return(0);
3454: }

3456: /* ----------------------------------------------------------------------------------------*/

3458: PetscErrorCode  MatStoreValues_SeqAIJ(Mat mat)
3459: {
3460:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)mat->data;
3462:   size_t         nz = aij->i[mat->rmap->n];

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

3467:   /* allocate space for values if not already there */
3468:   if (!aij->saved_values) {
3469:     PetscMalloc1(nz+1,&aij->saved_values);
3470:     PetscLogObjectMemory((PetscObject)mat,(nz+1)*sizeof(PetscScalar));
3471:   }

3473:   /* copy values over */
3474:   PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));
3475:   return(0);
3476: }

3478: /*@
3479:     MatStoreValues - Stashes a copy of the matrix values; this allows, for
3480:        example, reuse of the linear part of a Jacobian, while recomputing the
3481:        nonlinear portion.

3483:    Collect on Mat

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

3488:   Level: advanced

3490:   Common Usage, with SNESSolve():
3491: $    Create Jacobian matrix
3492: $    Set linear terms into matrix
3493: $    Apply boundary conditions to matrix, at this time matrix must have
3494: $      final nonzero structure (i.e. setting the nonlinear terms and applying
3495: $      boundary conditions again will not change the nonzero structure
3496: $    MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3497: $    MatStoreValues(mat);
3498: $    Call SNESSetJacobian() with matrix
3499: $    In your Jacobian routine
3500: $      MatRetrieveValues(mat);
3501: $      Set nonlinear terms in matrix

3503:   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
3504: $    // build linear portion of Jacobian
3505: $    MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3506: $    MatStoreValues(mat);
3507: $    loop over nonlinear iterations
3508: $       MatRetrieveValues(mat);
3509: $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
3510: $       // call MatAssemblyBegin/End() on matrix
3511: $       Solve linear system with Jacobian
3512: $    endloop

3514:   Notes:
3515:     Matrix must already be assemblied before calling this routine
3516:     Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
3517:     calling this routine.

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

3522: .seealso: MatRetrieveValues()

3524: @*/
3525: PetscErrorCode  MatStoreValues(Mat mat)
3526: {

3531:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3532:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3533:   PetscUseMethod(mat,"MatStoreValues_C",(Mat),(mat));
3534:   return(0);
3535: }

3537: PetscErrorCode  MatRetrieveValues_SeqAIJ(Mat mat)
3538: {
3539:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)mat->data;
3541:   PetscInt       nz = aij->i[mat->rmap->n];

3544:   if (!aij->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3545:   if (!aij->saved_values) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
3546:   /* copy values over */
3547:   PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));
3548:   return(0);
3549: }

3551: /*@
3552:     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
3553:        example, reuse of the linear part of a Jacobian, while recomputing the
3554:        nonlinear portion.

3556:    Collect on Mat

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

3561:   Level: advanced

3563: .seealso: MatStoreValues()

3565: @*/
3566: PetscErrorCode  MatRetrieveValues(Mat mat)
3567: {

3572:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3573:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3574:   PetscUseMethod(mat,"MatRetrieveValues_C",(Mat),(mat));
3575:   return(0);
3576: }


3579: /* --------------------------------------------------------------------------------*/
3580: /*@C
3581:    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
3582:    (the default parallel PETSc format).  For good matrix assembly performance
3583:    the user should preallocate the matrix storage by setting the parameter nz
3584:    (or the array nnz).  By setting these parameters accurately, performance
3585:    during matrix assembly can be increased by more than a factor of 50.

3587:    Collective on MPI_Comm

3589:    Input Parameters:
3590: +  comm - MPI communicator, set to PETSC_COMM_SELF
3591: .  m - number of rows
3592: .  n - number of columns
3593: .  nz - number of nonzeros per row (same for all rows)
3594: -  nnz - array containing the number of nonzeros in the various rows
3595:          (possibly different for each row) or NULL

3597:    Output Parameter:
3598: .  A - the matrix

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

3604:    Notes:
3605:    If nnz is given then nz is ignored

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

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

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

3622:    Options Database Keys:
3623: +  -mat_no_inode  - Do not use inodes
3624: -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)

3626:    Level: intermediate

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

3630: @*/
3631: PetscErrorCode  MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
3632: {

3636:   MatCreate(comm,A);
3637:   MatSetSizes(*A,m,n,m,n);
3638:   MatSetType(*A,MATSEQAIJ);
3639:   MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);
3640:   return(0);
3641: }

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

3649:    Collective on MPI_Comm

3651:    Input Parameters:
3652: +  B - The matrix
3653: .  nz - number of nonzeros per row (same for all rows)
3654: -  nnz - array containing the number of nonzeros in the various rows
3655:          (possibly different for each row) or NULL

3657:    Notes:
3658:      If nnz is given then nz is ignored

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

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

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

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

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

3683:    Options Database Keys:
3684: +  -mat_no_inode  - Do not use inodes
3685: -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)

3687:    Level: intermediate

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

3691: @*/
3692: PetscErrorCode  MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
3693: {

3699:   PetscTryMethod(B,"MatSeqAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[]),(B,nz,nnz));
3700:   return(0);
3701: }

3703: PetscErrorCode  MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,const PetscInt *nnz)
3704: {
3705:   Mat_SeqAIJ     *b;
3706:   PetscBool      skipallocation = PETSC_FALSE,realalloc = PETSC_FALSE;
3708:   PetscInt       i;

3711:   if (nz >= 0 || nnz) realalloc = PETSC_TRUE;
3712:   if (nz == MAT_SKIP_ALLOCATION) {
3713:     skipallocation = PETSC_TRUE;
3714:     nz             = 0;
3715:   }
3716:   PetscLayoutSetUp(B->rmap);
3717:   PetscLayoutSetUp(B->cmap);

3719:   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
3720:   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %D",nz);
3721:   if (nnz) {
3722:     for (i=0; i<B->rmap->n; i++) {
3723:       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]);
3724:       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);
3725:     }
3726:   }

3728:   B->preallocated = PETSC_TRUE;

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

3732:   if (!skipallocation) {
3733:     if (!b->imax) {
3734:       PetscMalloc2(B->rmap->n,&b->imax,B->rmap->n,&b->ilen);
3735:       PetscLogObjectMemory((PetscObject)B,2*B->rmap->n*sizeof(PetscInt));
3736:     }
3737:     if (!b->ipre) {
3738:       PetscMalloc1(B->rmap->n,&b->ipre);
3739:       PetscLogObjectMemory((PetscObject)B,B->rmap->n*sizeof(PetscInt));
3740:     }
3741:     if (!nnz) {
3742:       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
3743:       else if (nz < 0) nz = 1;
3744:       for (i=0; i<B->rmap->n; i++) b->imax[i] = nz;
3745:       nz = nz*B->rmap->n;
3746:     } else {
3747:       nz = 0;
3748:       for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
3749:     }
3750:     /* b->ilen will count nonzeros in each row so far. */
3751:     for (i=0; i<B->rmap->n; i++) b->ilen[i] = 0;

3753:     /* allocate the matrix space */
3754:     /* FIXME: should B's old memory be unlogged? */
3755:     MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);
3756:     if (B->structure_only) {
3757:       PetscMalloc1(nz,&b->j);
3758:       PetscMalloc1(B->rmap->n+1,&b->i);
3759:       PetscLogObjectMemory((PetscObject)B,(B->rmap->n+1)*sizeof(PetscInt)+nz*sizeof(PetscInt));
3760:     } else {
3761:       PetscMalloc3(nz,&b->a,nz,&b->j,B->rmap->n+1,&b->i);
3762:       PetscLogObjectMemory((PetscObject)B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));
3763:     }
3764:     b->i[0] = 0;
3765:     for (i=1; i<B->rmap->n+1; i++) {
3766:       b->i[i] = b->i[i-1] + b->imax[i-1];
3767:     }
3768:     if (B->structure_only) {
3769:       b->singlemalloc = PETSC_FALSE;
3770:       b->free_a       = PETSC_FALSE;
3771:     } else {
3772:       b->singlemalloc = PETSC_TRUE;
3773:       b->free_a       = PETSC_TRUE;
3774:     }
3775:     b->free_ij      = PETSC_TRUE;
3776:   } else {
3777:     b->free_a  = PETSC_FALSE;
3778:     b->free_ij = PETSC_FALSE;
3779:   }

3781:   if (b->ipre && nnz != b->ipre  && b->imax) {
3782:     /* reserve user-requested sparsity */
3783:     PetscMemcpy(b->ipre,b->imax,B->rmap->n*sizeof(PetscInt));
3784:   }


3787:   b->nz               = 0;
3788:   b->maxnz            = nz;
3789:   B->info.nz_unneeded = (double)b->maxnz;
3790:   if (realalloc) {
3791:     MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);
3792:   }
3793:   B->was_assembled = PETSC_FALSE;
3794:   B->assembled     = PETSC_FALSE;
3795:   return(0);
3796: }


3799: PetscErrorCode MatResetPreallocation_SeqAIJ(Mat A)
3800: {
3801:   Mat_SeqAIJ     *a;
3802:   PetscInt       i;

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

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

3813:   PetscMemcpy(a->imax,a->ipre,A->rmap->n*sizeof(PetscInt));
3814:   PetscMemzero(a->ilen,A->rmap->n*sizeof(PetscInt));
3815:   a->i[0] = 0;
3816:   for (i=1; i<A->rmap->n+1; i++) {
3817:     a->i[i] = a->i[i-1] + a->imax[i-1];
3818:   }
3819:   A->preallocated     = PETSC_TRUE;
3820:   a->nz               = 0;
3821:   a->maxnz            = a->i[A->rmap->n];
3822:   A->info.nz_unneeded = (double)a->maxnz;
3823:   A->was_assembled    = PETSC_FALSE;
3824:   A->assembled        = PETSC_FALSE;
3825:   return(0);
3826: }

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

3831:    Input Parameters:
3832: +  B - the matrix
3833: .  i - the indices into j for the start of each row (starts with zero)
3834: .  j - the column indices for each row (starts with zero) these must be sorted for each row
3835: -  v - optional values in the matrix

3837:    Level: developer

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

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

3843: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), MATSEQAIJ
3844: @*/
3845: PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
3846: {

3852:   PetscTryMethod(B,"MatSeqAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));
3853:   return(0);
3854: }

3856: PetscErrorCode  MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3857: {
3858:   PetscInt       i;
3859:   PetscInt       m,n;
3860:   PetscInt       nz;
3861:   PetscInt       *nnz, nz_max = 0;
3862:   PetscScalar    *values;

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

3868:   PetscLayoutSetUp(B->rmap);
3869:   PetscLayoutSetUp(B->cmap);

3871:   MatGetSize(B, &m, &n);
3872:   PetscMalloc1(m+1, &nnz);
3873:   for (i = 0; i < m; i++) {
3874:     nz     = Ii[i+1]- Ii[i];
3875:     nz_max = PetscMax(nz_max, nz);
3876:     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
3877:     nnz[i] = nz;
3878:   }
3879:   MatSeqAIJSetPreallocation(B, 0, nnz);
3880:   PetscFree(nnz);

3882:   if (v) {
3883:     values = (PetscScalar*) v;
3884:   } else {
3885:     PetscCalloc1(nz_max, &values);
3886:   }

3888:   for (i = 0; i < m; i++) {
3889:     nz   = Ii[i+1] - Ii[i];
3890:     MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);
3891:   }

3893:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
3894:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);

3896:   if (!v) {
3897:     PetscFree(values);
3898:   }
3899:   MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
3900:   return(0);
3901: }

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

3906: /*
3907:     Computes (B'*A')' since computing B*A directly is untenable

3909:                n                       p                          p
3910:         (              )       (              )         (                  )
3911:       m (      A       )  *  n (       B      )   =   m (         C        )
3912:         (              )       (              )         (                  )

3914: */
3915: PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
3916: {
3917:   PetscErrorCode    ierr;
3918:   Mat_SeqDense      *sub_a = (Mat_SeqDense*)A->data;
3919:   Mat_SeqAIJ        *sub_b = (Mat_SeqAIJ*)B->data;
3920:   Mat_SeqDense      *sub_c = (Mat_SeqDense*)C->data;
3921:   PetscInt          i,n,m,q,p;
3922:   const PetscInt    *ii,*idx;
3923:   const PetscScalar *b,*a,*a_q;
3924:   PetscScalar       *c,*c_q;

3927:   m    = A->rmap->n;
3928:   n    = A->cmap->n;
3929:   p    = B->cmap->n;
3930:   a    = sub_a->v;
3931:   b    = sub_b->a;
3932:   c    = sub_c->v;
3933:   PetscMemzero(c,m*p*sizeof(PetscScalar));

3935:   ii  = sub_b->i;
3936:   idx = sub_b->j;
3937:   for (i=0; i<n; i++) {
3938:     q = ii[i+1] - ii[i];
3939:     while (q-->0) {
3940:       c_q = c + m*(*idx);
3941:       a_q = a + m*i;
3942:       PetscKernelAXPY(c_q,*b,a_q,m);
3943:       idx++;
3944:       b++;
3945:     }
3946:   }
3947:   return(0);
3948: }

3950: PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
3951: {
3953:   PetscInt       m=A->rmap->n,n=B->cmap->n;
3954:   Mat            Cmat;

3957:   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);
3958:   MatCreate(PetscObjectComm((PetscObject)A),&Cmat);
3959:   MatSetSizes(Cmat,m,n,m,n);
3960:   MatSetBlockSizesFromMats(Cmat,A,B);
3961:   MatSetType(Cmat,MATSEQDENSE);
3962:   MatSeqDenseSetPreallocation(Cmat,NULL);

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

3966:   *C = Cmat;
3967:   return(0);
3968: }

3970: /* ----------------------------------------------------------------*/
3971: PETSC_INTERN PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
3972: {

3976:   if (scall == MAT_INITIAL_MATRIX) {
3977:     PetscLogEventBegin(MAT_MatMultSymbolic,A,B,0,0);
3978:     MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);
3979:     PetscLogEventEnd(MAT_MatMultSymbolic,A,B,0,0);
3980:   }
3981:   PetscLogEventBegin(MAT_MatMultNumeric,A,B,0,0);
3982:   MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);
3983:   PetscLogEventEnd(MAT_MatMultNumeric,A,B,0,0);
3984:   return(0);
3985: }


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

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

3995:   Level: beginner

3997: .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
3998: M*/

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

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

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

4012:   Developer Notes:
4013:     Subclasses include MATAIJCUSPARSE, MATAIJPERM, MATAIJSELL, MATAIJMKL, MATAIJCRL, and also automatically switches over to use inodes when
4014:    enough exist.

4016:   Level: beginner

4018: .seealso: MatCreateAIJ(), MatCreateSeqAIJ(), MATSEQAIJ,MATMPIAIJ
4019: M*/

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

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

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

4033:   Level: beginner

4035: .seealso: MatCreateMPIAIJCRL,MATSEQAIJCRL,MATMPIAIJCRL, MATSEQAIJCRL, MATMPIAIJCRL
4036: M*/

4038: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCRL(Mat,MatType,MatReuse,Mat*);
4039: #if defined(PETSC_HAVE_ELEMENTAL)
4040: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_Elemental(Mat,MatType,MatReuse,Mat*);
4041: #endif
4042: #if defined(PETSC_HAVE_HYPRE)
4043: PETSC_INTERN PetscErrorCode MatConvert_AIJ_HYPRE(Mat A,MatType,MatReuse,Mat*);
4044: PETSC_INTERN PetscErrorCode MatMatMatMult_Transpose_AIJ_AIJ(Mat,Mat,Mat,MatReuse,PetscReal,Mat*);
4045: #endif
4046: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqDense(Mat,MatType,MatReuse,Mat*);

4048: PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_SeqSELL(Mat,MatType,MatReuse,Mat*);
4049: PETSC_INTERN PetscErrorCode MatConvert_XAIJ_IS(Mat,MatType,MatReuse,Mat*);
4050: PETSC_INTERN PetscErrorCode MatPtAP_IS_XAIJ(Mat,Mat,MatReuse,PetscReal,Mat*);

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

4055:    Not Collective

4057:    Input Parameter:
4058: .  mat - a MATSEQAIJ matrix

4060:    Output Parameter:
4061: .   array - pointer to the data

4063:    Level: intermediate

4065: .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90()
4066: @*/
4067: PetscErrorCode  MatSeqAIJGetArray(Mat A,PetscScalar **array)
4068: {

4072:   PetscUseMethod(A,"MatSeqAIJGetArray_C",(Mat,PetscScalar**),(A,array));
4073:   return(0);
4074: }

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

4079:    Not Collective

4081:    Input Parameter:
4082: .  mat - a MATSEQAIJ matrix

4084:    Output Parameter:
4085: .   nz - the maximum number of nonzeros in any row

4087:    Level: intermediate

4089: .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90()
4090: @*/
4091: PetscErrorCode  MatSeqAIJGetMaxRowNonzeros(Mat A,PetscInt *nz)
4092: {
4093:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)A->data;

4096:   *nz = aij->rmax;
4097:   return(0);
4098: }

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

4103:    Not Collective

4105:    Input Parameters:
4106: .  mat - a MATSEQAIJ matrix
4107: .  array - pointer to the data

4109:    Level: intermediate

4111: .seealso: MatSeqAIJGetArray(), MatSeqAIJRestoreArrayF90()
4112: @*/
4113: PetscErrorCode  MatSeqAIJRestoreArray(Mat A,PetscScalar **array)
4114: {

4118:   PetscUseMethod(A,"MatSeqAIJRestoreArray_C",(Mat,PetscScalar**),(A,array));
4119:   return(0);
4120: }

4122: #if defined(PETSC_HAVE_CUDA)
4123: PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCUSPARSE(Mat);
4124: #endif

4126: PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJ(Mat B)
4127: {
4128:   Mat_SeqAIJ     *b;
4130:   PetscMPIInt    size;

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

4136:   PetscNewLog(B,&b);

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

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

4142:   b->row                = 0;
4143:   b->col                = 0;
4144:   b->icol               = 0;
4145:   b->reallocs           = 0;
4146:   b->ignorezeroentries  = PETSC_FALSE;
4147:   b->roworiented        = PETSC_TRUE;
4148:   b->nonew              = 0;
4149:   b->diag               = 0;
4150:   b->solve_work         = 0;
4151:   B->spptr              = 0;
4152:   b->saved_values       = 0;
4153:   b->idiag              = 0;
4154:   b->mdiag              = 0;
4155:   b->ssor_work          = 0;
4156:   b->omega              = 1.0;
4157:   b->fshift             = 0.0;
4158:   b->idiagvalid         = PETSC_FALSE;
4159:   b->ibdiagvalid        = PETSC_FALSE;
4160:   b->keepnonzeropattern = PETSC_FALSE;

4162:   PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);
4163:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJGetArray_C",MatSeqAIJGetArray_SeqAIJ);
4164:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJRestoreArray_C",MatSeqAIJRestoreArray_SeqAIJ);

4166: #if defined(PETSC_HAVE_MATLAB_ENGINE)
4167:   PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEnginePut_C",MatlabEnginePut_SeqAIJ);
4168:   PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEngineGet_C",MatlabEngineGet_SeqAIJ);
4169: #endif

4171:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetColumnIndices_C",MatSeqAIJSetColumnIndices_SeqAIJ);
4172:   PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_SeqAIJ);
4173:   PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_SeqAIJ);
4174:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",MatConvert_SeqAIJ_SeqSBAIJ);
4175:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqbaij_C",MatConvert_SeqAIJ_SeqBAIJ);
4176:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijperm_C",MatConvert_SeqAIJ_SeqAIJPERM);
4177:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijsell_C",MatConvert_SeqAIJ_SeqAIJSELL);
4178: #if defined(PETSC_HAVE_MKL_SPARSE)
4179:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijmkl_C",MatConvert_SeqAIJ_SeqAIJMKL);
4180: #endif
4181: #if defined(PETSC_HAVE_CUDA)
4182:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijcusparse_C",MatConvert_SeqAIJ_SeqAIJCUSPARSE);
4183: #endif
4184:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijcrl_C",MatConvert_SeqAIJ_SeqAIJCRL);
4185: #if defined(PETSC_HAVE_ELEMENTAL)
4186:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_elemental_C",MatConvert_SeqAIJ_Elemental);
4187: #endif
4188: #if defined(PETSC_HAVE_HYPRE)
4189:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_hypre_C",MatConvert_AIJ_HYPRE);
4190:   PetscObjectComposeFunction((PetscObject)B,"MatMatMatMult_transpose_seqaij_seqaij_C",MatMatMatMult_Transpose_AIJ_AIJ);
4191: #endif
4192:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqdense_C",MatConvert_SeqAIJ_SeqDense);
4193:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqsell_C",MatConvert_SeqAIJ_SeqSELL);
4194:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_is_C",MatConvert_XAIJ_IS);
4195:   PetscObjectComposeFunction((PetscObject)B,"MatIsTranspose_C",MatIsTranspose_SeqAIJ);
4196:   PetscObjectComposeFunction((PetscObject)B,"MatIsHermitianTranspose_C",MatIsTranspose_SeqAIJ);
4197:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",MatSeqAIJSetPreallocation_SeqAIJ);
4198:   PetscObjectComposeFunction((PetscObject)B,"MatResetPreallocation_C",MatResetPreallocation_SeqAIJ);
4199:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",MatSeqAIJSetPreallocationCSR_SeqAIJ);
4200:   PetscObjectComposeFunction((PetscObject)B,"MatReorderForNonzeroDiagonal_C",MatReorderForNonzeroDiagonal_SeqAIJ);
4201:   PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqdense_seqaij_C",MatMatMult_SeqDense_SeqAIJ);
4202:   PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C",MatMatMultSymbolic_SeqDense_SeqAIJ);
4203:   PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C",MatMatMultNumeric_SeqDense_SeqAIJ);
4204:   PetscObjectComposeFunction((PetscObject)B,"MatPtAP_is_seqaij_C",MatPtAP_IS_XAIJ);
4205:   MatCreate_SeqAIJ_Inode(B);
4206:   PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);
4207:   MatSeqAIJSetTypeFromOptions(B);  /* this allows changing the matrix subtype to say MATSEQAIJPERM */
4208:   return(0);
4209: }

4211: /*
4212:     Given a matrix generated with MatGetFactor() duplicates all the information in A into B
4213: */
4214: PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscBool mallocmatspace)
4215: {
4216:   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
4218:   PetscInt       i,m = A->rmap->n;

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

4223:   C->factortype = A->factortype;
4224:   c->row        = 0;
4225:   c->col        = 0;
4226:   c->icol       = 0;
4227:   c->reallocs   = 0;

4229:   C->assembled = PETSC_TRUE;

4231:   PetscLayoutReference(A->rmap,&C->rmap);
4232:   PetscLayoutReference(A->cmap,&C->cmap);

4234:   PetscMalloc2(m,&c->imax,m,&c->ilen);
4235:   PetscLogObjectMemory((PetscObject)C, 2*m*sizeof(PetscInt));
4236:   for (i=0; i<m; i++) {
4237:     c->imax[i] = a->imax[i];
4238:     c->ilen[i] = a->ilen[i];
4239:   }

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

4246:     c->singlemalloc = PETSC_TRUE;

4248:     PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));
4249:     if (m > 0) {
4250:       PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));
4251:       if (cpvalues == MAT_COPY_VALUES) {
4252:         PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));
4253:       } else {
4254:         PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));
4255:       }
4256:     }
4257:   }

4259:   c->ignorezeroentries = a->ignorezeroentries;
4260:   c->roworiented       = a->roworiented;
4261:   c->nonew             = a->nonew;
4262:   if (a->diag) {
4263:     PetscMalloc1(m+1,&c->diag);
4264:     PetscLogObjectMemory((PetscObject)C,(m+1)*sizeof(PetscInt));
4265:     for (i=0; i<m; i++) {
4266:       c->diag[i] = a->diag[i];
4267:     }
4268:   } else c->diag = 0;

4270:   c->solve_work         = 0;
4271:   c->saved_values       = 0;
4272:   c->idiag              = 0;
4273:   c->ssor_work          = 0;
4274:   c->keepnonzeropattern = a->keepnonzeropattern;
4275:   c->free_a             = PETSC_TRUE;
4276:   c->free_ij            = PETSC_TRUE;

4278:   c->rmax         = a->rmax;
4279:   c->nz           = a->nz;
4280:   c->maxnz        = a->nz;       /* Since we allocate exactly the right amount */
4281:   C->preallocated = PETSC_TRUE;

4283:   c->compressedrow.use   = a->compressedrow.use;
4284:   c->compressedrow.nrows = a->compressedrow.nrows;
4285:   if (a->compressedrow.use) {
4286:     i    = a->compressedrow.nrows;
4287:     PetscMalloc2(i+1,&c->compressedrow.i,i,&c->compressedrow.rindex);
4288:     PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));
4289:     PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));
4290:   } else {
4291:     c->compressedrow.use    = PETSC_FALSE;
4292:     c->compressedrow.i      = NULL;
4293:     c->compressedrow.rindex = NULL;
4294:   }
4295:   c->nonzerorowcnt = a->nonzerorowcnt;
4296:   C->nonzerostate  = A->nonzerostate;

4298:   MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);
4299:   PetscFunctionListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);
4300:   return(0);
4301: }

4303: PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
4304: {

4308:   MatCreate(PetscObjectComm((PetscObject)A),B);
4309:   MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);
4310:   if (!(A->rmap->n % A->rmap->bs) && !(A->cmap->n % A->cmap->bs)) {
4311:     MatSetBlockSizesFromMats(*B,A,A);
4312:   }
4313:   MatSetType(*B,((PetscObject)A)->type_name);
4314:   MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);
4315:   return(0);
4316: }

4318: PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer)
4319: {
4320:   PetscBool      isbinary, ishdf5;

4326:   /* force binary viewer to load .info file if it has not yet done so */
4327:   PetscViewerSetUp(viewer);
4328:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
4329:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,  &ishdf5);
4330:   if (isbinary) {
4331:     MatLoad_SeqAIJ_Binary(newMat,viewer);
4332:   } else if (ishdf5) {
4333: #if defined(PETSC_HAVE_HDF5)
4334:     MatLoad_AIJ_HDF5(newMat,viewer);
4335: #else
4336:     SETERRQ(PetscObjectComm((PetscObject)newMat),PETSC_ERR_SUP,"HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
4337: #endif
4338:   } else {
4339:     SETERRQ2(PetscObjectComm((PetscObject)newMat),PETSC_ERR_SUP,"Viewer type %s not yet supported for reading %s matrices",((PetscObject)viewer)->type_name,((PetscObject)newMat)->type_name);
4340:   }
4341:   return(0);
4342: }

4344: PetscErrorCode MatLoad_SeqAIJ_Binary(Mat newMat, PetscViewer viewer)
4345: {
4346:   Mat_SeqAIJ     *a;
4348:   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N,rows,cols;
4349:   int            fd;
4350:   PetscMPIInt    size;
4351:   MPI_Comm       comm;
4352:   PetscInt       bs = newMat->rmap->bs;

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

4359:   PetscOptionsBegin(comm,NULL,"Options for loading SEQAIJ matrix","Mat");
4360:   PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,NULL);
4361:   PetscOptionsEnd();
4362:   if (bs < 0) bs = 1;
4363:   MatSetBlockSize(newMat,bs);

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

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

4372:   /* read in row lengths */
4373:   PetscMalloc1(M,&rowlengths);
4374:   PetscBinaryRead(fd,rowlengths,M,PETSC_INT);

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

4380:   /* set global size if not set already*/
4381:   if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) {
4382:     MatSetSizes(newMat,PETSC_DECIDE,PETSC_DECIDE,M,N);
4383:   } else {
4384:     /* if sizes and type are already set, check if the matrix  global sizes are correct */
4385:     MatGetSize(newMat,&rows,&cols);
4386:     if (rows < 0 && cols < 0) { /* user might provide local size instead of global size */
4387:       MatGetLocalSize(newMat,&rows,&cols);
4388:     }
4389:     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);
4390:   }
4391:   MatSeqAIJSetPreallocation_SeqAIJ(newMat,0,rowlengths);
4392:   a    = (Mat_SeqAIJ*)newMat->data;

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

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

4399:   /* set matrix "i" values */
4400:   a->i[0] = 0;
4401:   for (i=1; i<= M; i++) {
4402:     a->i[i]      = a->i[i-1] + rowlengths[i-1];
4403:     a->ilen[i-1] = rowlengths[i-1];
4404:   }
4405:   PetscFree(rowlengths);

4407:   MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);
4408:   MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);
4409:   return(0);
4410: }

4412: PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscBool * flg)
4413: {
4414:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*b = (Mat_SeqAIJ*)B->data;
4416: #if defined(PETSC_USE_COMPLEX)
4417:   PetscInt k;
4418: #endif

4421:   /* If the  matrix dimensions are not equal,or no of nonzeros */
4422:   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
4423:     *flg = PETSC_FALSE;
4424:     return(0);
4425:   }

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

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

4435:   /* if a->a are the same */
4436: #if defined(PETSC_USE_COMPLEX)
4437:   for (k=0; k<a->nz; k++) {
4438:     if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])) {
4439:       *flg = PETSC_FALSE;
4440:       return(0);
4441:     }
4442:   }
4443: #else
4444:   PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);
4445: #endif
4446:   return(0);
4447: }

4449: /*@
4450:      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
4451:               provided by the user.

4453:       Collective on MPI_Comm

4455:    Input Parameters:
4456: +   comm - must be an MPI communicator of size 1
4457: .   m - number of rows
4458: .   n - number of columns
4459: .   i - row indices; that is i[0] = 0, i[row] = i[row-1] + number of elements in that row of the matrix
4460: .   j - column indices
4461: -   a - matrix values

4463:    Output Parameter:
4464: .   mat - the matrix

4466:    Level: intermediate

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

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

4474:        The i and j indices are 0 based

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

4480: $        1 0 0
4481: $        2 0 3
4482: $        4 5 6
4483: $
4484: $        i =  {0,1,3,6}  [size = nrow+1  = 3+1]
4485: $        j =  {0,0,2,0,1,2}  [size = 6]; values must be sorted for each row
4486: $        v =  {1,2,3,4,5,6}  [size = 6]


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

4491: @*/
4492: PetscErrorCode  MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt i[],PetscInt j[],PetscScalar a[],Mat *mat)
4493: {
4495:   PetscInt       ii;
4496:   Mat_SeqAIJ     *aij;
4497: #if defined(PETSC_USE_DEBUG)
4498:   PetscInt jj;
4499: #endif

4502:   if (m > 0 && i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
4503:   MatCreate(comm,mat);
4504:   MatSetSizes(*mat,m,n,m,n);
4505:   /* MatSetBlockSizes(*mat,,); */
4506:   MatSetType(*mat,MATSEQAIJ);
4507:   MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);
4508:   aij  = (Mat_SeqAIJ*)(*mat)->data;
4509:   PetscMalloc2(m,&aij->imax,m,&aij->ilen);

4511:   aij->i            = i;
4512:   aij->j            = j;
4513:   aij->a            = a;
4514:   aij->singlemalloc = PETSC_FALSE;
4515:   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
4516:   aij->free_a       = PETSC_FALSE;
4517:   aij->free_ij      = PETSC_FALSE;

4519:   for (ii=0; ii<m; ii++) {
4520:     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
4521: #if defined(PETSC_USE_DEBUG)
4522:     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]);
4523:     for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
4524:       if (j[jj] < j[jj-1]) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column entry number %D (actual column %D) in row %D is not sorted",jj-i[ii],j[jj],ii);
4525:       if (j[jj] == j[jj-1]) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column entry number %D (actual column %D) in row %D is identical to previous entry",jj-i[ii],j[jj],ii);
4526:     }
4527: #endif
4528:   }
4529: #if defined(PETSC_USE_DEBUG)
4530:   for (ii=0; ii<aij->i[m]; ii++) {
4531:     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %D index = %D",ii,j[ii]);
4532:     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]);
4533:   }
4534: #endif

4536:   MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);
4537:   MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);
4538:   return(0);
4539: }
4540: /*@C
4541:      MatCreateSeqAIJFromTriple - Creates an sequential AIJ matrix using matrix elements (in COO format)
4542:               provided by the user.

4544:       Collective on MPI_Comm

4546:    Input Parameters:
4547: +   comm - must be an MPI communicator of size 1
4548: .   m   - number of rows
4549: .   n   - number of columns
4550: .   i   - row indices
4551: .   j   - column indices
4552: .   a   - matrix values
4553: .   nz  - number of nonzeros
4554: -   idx - 0 or 1 based

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

4559:    Level: intermediate

4561:    Notes:
4562:        The i and j indices are 0 based

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

4568:         1 0 0
4569:         2 0 3
4570:         4 5 6

4572:         i =  {0,1,1,2,2,2}
4573:         j =  {0,0,2,0,1,2}
4574:         v =  {1,2,3,4,5,6}


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

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


4587:   PetscCalloc1(m,&nnz);
4588:   for (ii = 0; ii < nz; ii++) {
4589:     nnz[i[ii] - !!idx] += 1;
4590:   }
4591:   MatCreate(comm,mat);
4592:   MatSetSizes(*mat,m,n,m,n);
4593:   MatSetType(*mat,MATSEQAIJ);
4594:   MatSeqAIJSetPreallocation_SeqAIJ(*mat,0,nnz);
4595:   for (ii = 0; ii < nz; ii++) {
4596:     if (idx) {
4597:       row = i[ii] - 1;
4598:       col = j[ii] - 1;
4599:     } else {
4600:       row = i[ii];
4601:       col = j[ii];
4602:     }
4603:     MatSetValues(*mat,one,&row,one,&col,&a[ii],ADD_VALUES);
4604:   }
4605:   MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);
4606:   MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);
4607:   PetscFree(nnz);
4608:   return(0);
4609: }

4611: PetscErrorCode MatSeqAIJInvalidateDiagonal(Mat A)
4612: {
4613:   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data;

4617:   a->idiagvalid  = PETSC_FALSE;
4618:   a->ibdiagvalid = PETSC_FALSE;

4620:   MatSeqAIJInvalidateDiagonal_Inode(A);
4621:   return(0);
4622: }

4624: PetscErrorCode MatCreateMPIMatConcatenateSeqMat_SeqAIJ(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat)
4625: {
4627:   PetscMPIInt    size;

4630:   MPI_Comm_size(comm,&size);
4631:   if (size == 1) {
4632:     if (scall == MAT_INITIAL_MATRIX) {
4633:       MatDuplicate(inmat,MAT_COPY_VALUES,outmat);
4634:     } else {
4635:       MatCopy(inmat,*outmat,SAME_NONZERO_PATTERN);
4636:     }
4637:   } else {
4638:     MatCreateMPIMatConcatenateSeqMat_MPIAIJ(comm,inmat,n,scall,outmat);
4639:   }
4640:   return(0);
4641: }

4643: /*
4644:  Permute A into C's *local* index space using rowemb,colemb.
4645:  The embedding are supposed to be injections and the above implies that the range of rowemb is a subset
4646:  of [0,m), colemb is in [0,n).
4647:  If pattern == DIFFERENT_NONZERO_PATTERN, C is preallocated according to A.
4648:  */
4649: PetscErrorCode MatSetSeqMat_SeqAIJ(Mat C,IS rowemb,IS colemb,MatStructure pattern,Mat B)
4650: {
4651:   /* If making this function public, change the error returned in this function away from _PLIB. */
4653:   Mat_SeqAIJ     *Baij;
4654:   PetscBool      seqaij;
4655:   PetscInt       m,n,*nz,i,j,count;
4656:   PetscScalar    v;
4657:   const PetscInt *rowindices,*colindices;

4660:   if (!B) return(0);
4661:   /* Check to make sure the target matrix (and embeddings) are compatible with C and each other. */
4662:   PetscObjectBaseTypeCompare((PetscObject)B,MATSEQAIJ,&seqaij);
4663:   if (!seqaij) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is of wrong type");
4664:   if (rowemb) {
4665:     ISGetLocalSize(rowemb,&m);
4666:     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);
4667:   } else {
4668:     if (C->rmap->n != B->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is row-incompatible with the target matrix");
4669:   }
4670:   if (colemb) {
4671:     ISGetLocalSize(colemb,&n);
4672:     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);
4673:   } else {
4674:     if (C->cmap->n != B->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is col-incompatible with the target matrix");
4675:   }

4677:   Baij = (Mat_SeqAIJ*)(B->data);
4678:   if (pattern == DIFFERENT_NONZERO_PATTERN) {
4679:     PetscMalloc1(B->rmap->n,&nz);
4680:     for (i=0; i<B->rmap->n; i++) {
4681:       nz[i] = Baij->i[i+1] - Baij->i[i];
4682:     }
4683:     MatSeqAIJSetPreallocation(C,0,nz);
4684:     PetscFree(nz);
4685:   }
4686:   if (pattern == SUBSET_NONZERO_PATTERN) {
4687:     MatZeroEntries(C);
4688:   }
4689:   count = 0;
4690:   rowindices = NULL;
4691:   colindices = NULL;
4692:   if (rowemb) {
4693:     ISGetIndices(rowemb,&rowindices);
4694:   }
4695:   if (colemb) {
4696:     ISGetIndices(colemb,&colindices);
4697:   }
4698:   for (i=0; i<B->rmap->n; i++) {
4699:     PetscInt row;
4700:     row = i;
4701:     if (rowindices) row = rowindices[i];
4702:     for (j=Baij->i[i]; j<Baij->i[i+1]; j++) {
4703:       PetscInt col;
4704:       col  = Baij->j[count];
4705:       if (colindices) col = colindices[col];
4706:       v    = Baij->a[count];
4707:       MatSetValues(C,1,&row,1,&col,&v,INSERT_VALUES);
4708:       ++count;
4709:     }
4710:   }
4711:   /* FIXME: set C's nonzerostate correctly. */
4712:   /* Assembly for C is necessary. */
4713:   C->preallocated = PETSC_TRUE;
4714:   C->assembled     = PETSC_TRUE;
4715:   C->was_assembled = PETSC_FALSE;
4716:   return(0);
4717: }

4719: PetscFunctionList MatSeqAIJList = NULL;

4721: /*@C
4722:    MatSeqAIJSetType - Converts a MATSEQAIJ matrix to a subtype

4724:    Collective on Mat

4726:    Input Parameters:
4727: +  mat      - the matrix object
4728: -  matype   - matrix type

4730:    Options Database Key:
4731: .  -mat_seqai_type  <method> - for example seqaijcrl


4734:   Level: intermediate

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

4738: .seealso: PCSetType(), VecSetType(), MatCreate(), MatType, Mat
4739: @*/
4740: PetscErrorCode  MatSeqAIJSetType(Mat mat, MatType matype)
4741: {
4742:   PetscErrorCode ierr,(*r)(Mat,MatType,MatReuse,Mat*);
4743:   PetscBool      sametype;

4747:   PetscObjectTypeCompare((PetscObject)mat,matype,&sametype);
4748:   if (sametype) return(0);

4750:    PetscFunctionListFind(MatSeqAIJList,matype,&r);
4751:   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown Mat type given: %s",matype);
4752:   (*r)(mat,matype,MAT_INPLACE_MATRIX,&mat);
4753:   return(0);
4754: }


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

4760:    Not Collective

4762:    Input Parameters:
4763: +  name - name of a new user-defined matrix type, for example MATSEQAIJCRL
4764: -  function - routine to convert to subtype

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


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

4773:    Level: advanced

4775: .keywords: Mat, register

4777: .seealso: MatSeqAIJRegisterAll()


4780:   Level: advanced
4781: @*/
4782: PetscErrorCode  MatSeqAIJRegister(const char sname[],PetscErrorCode (*function)(Mat,MatType,MatReuse,Mat *))
4783: {

4787:   MatInitializePackage();
4788:   PetscFunctionListAdd(&MatSeqAIJList,sname,function);
4789:   return(0);
4790: }

4792: PetscBool MatSeqAIJRegisterAllCalled = PETSC_FALSE;

4794: /*@C
4795:   MatSeqAIJRegisterAll - Registers all of the matrix subtypes of SeqAIJ

4797:   Not Collective

4799:   Level: advanced

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

4803: .keywords: KSP, register, all

4805: .seealso:  MatRegisterAll(), MatSeqAIJRegister()
4806: @*/
4807: PetscErrorCode  MatSeqAIJRegisterAll(void)
4808: {

4812:   if (MatSeqAIJRegisterAllCalled) return(0);
4813:   MatSeqAIJRegisterAllCalled = PETSC_TRUE;

4815:   MatSeqAIJRegister(MATSEQAIJCRL,      MatConvert_SeqAIJ_SeqAIJCRL);
4816:   MatSeqAIJRegister(MATSEQAIJPERM,     MatConvert_SeqAIJ_SeqAIJPERM);
4817:   MatSeqAIJRegister(MATSEQAIJSELL,     MatConvert_SeqAIJ_SeqAIJSELL);
4818: #if defined(PETSC_HAVE_MKL_SPARSE)
4819:   MatSeqAIJRegister(MATSEQAIJMKL,      MatConvert_SeqAIJ_SeqAIJMKL);
4820: #endif
4821: #if defined(PETSC_HAVE_VIENNACL) && defined(PETSC_HAVE_VIENNACL_NO_CUDA)
4822:   MatSeqAIJRegister(MATMPIAIJVIENNACL, MatConvert_SeqAIJ_SeqAIJViennaCL);
4823: #endif
4824:   return(0);
4825: }

4827: /*
4828:     Special version for direct calls from Fortran
4829: */
4830:  #include <petsc/private/fortranimpl.h>
4831: #if defined(PETSC_HAVE_FORTRAN_CAPS)
4832: #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
4833: #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
4834: #define matsetvaluesseqaij_ matsetvaluesseqaij
4835: #endif

4837: /* Change these macros so can be used in void function */
4838: #undef CHKERRQ
4839: #define CHKERRQ(ierr) CHKERRABORT(PetscObjectComm((PetscObject)A),ierr)
4840: #undef SETERRQ2
4841: #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
4842: #undef SETERRQ3
4843: #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr)

4845: 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)
4846: {
4847:   Mat            A  = *AA;
4848:   PetscInt       m  = *mm, n = *nn;
4849:   InsertMode     is = *isis;
4850:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
4851:   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
4852:   PetscInt       *imax,*ai,*ailen;
4854:   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
4855:   MatScalar      *ap,value,*aa;
4856:   PetscBool      ignorezeroentries = a->ignorezeroentries;
4857:   PetscBool      roworiented       = a->roworiented;

4860:   MatCheckPreallocated(A,1);
4861:   imax  = a->imax;
4862:   ai    = a->i;
4863:   ailen = a->ilen;
4864:   aj    = a->j;
4865:   aa    = a->a;

4867:   for (k=0; k<m; k++) { /* loop over added rows */
4868:     row = im[k];
4869:     if (row < 0) continue;
4870: #if defined(PETSC_USE_DEBUG)
4871:     if (row >= A->rmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
4872: #endif
4873:     rp   = aj + ai[row]; ap = aa + ai[row];
4874:     rmax = imax[row]; nrow = ailen[row];
4875:     low  = 0;
4876:     high = nrow;
4877:     for (l=0; l<n; l++) { /* loop over added columns */
4878:       if (in[l] < 0) continue;
4879: #if defined(PETSC_USE_DEBUG)
4880:       if (in[l] >= A->cmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
4881: #endif
4882:       col = in[l];
4883:       if (roworiented) value = v[l + k*n];
4884:       else value = v[k + l*m];

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

4888:       if (col <= lastcol) low = 0;
4889:       else high = nrow;
4890:       lastcol = col;
4891:       while (high-low > 5) {
4892:         t = (low+high)/2;
4893:         if (rp[t] > col) high = t;
4894:         else             low  = t;
4895:       }
4896:       for (i=low; i<high; i++) {
4897:         if (rp[i] > col) break;
4898:         if (rp[i] == col) {
4899:           if (is == ADD_VALUES) ap[i] += value;
4900:           else                  ap[i] = value;
4901:           goto noinsert;
4902:         }
4903:       }
4904:       if (value == 0.0 && ignorezeroentries) goto noinsert;
4905:       if (nonew == 1) goto noinsert;
4906:       if (nonew == -1) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
4907:       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
4908:       N = nrow++ - 1; a->nz++; high++;
4909:       /* shift up all the later entries in this row */
4910:       for (ii=N; ii>=i; ii--) {
4911:         rp[ii+1] = rp[ii];
4912:         ap[ii+1] = ap[ii];
4913:       }
4914:       rp[i] = col;
4915:       ap[i] = value;
4916:       A->nonzerostate++;
4917: noinsert:;
4918:       low = i + 1;
4919:     }
4920:     ailen[row] = nrow;
4921:   }
4922:   PetscFunctionReturnVoid();
4923: }