Actual source code: aij.c
petsc-3.7.1 2016-05-15
Report Typos and Errors

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

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

     15: PetscErrorCode MatGetColumnNorms_SeqAIJ(Mat A,NormType type,PetscReal *norms)
     16: {
     18:   PetscInt       i,m,n;
     19:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)A->data;

     22:   MatGetSize(A,&m,&n);
     23:   PetscMemzero(norms,n*sizeof(PetscReal));
     24:   if (type == NORM_2) {
     25:     for (i=0; i<aij->i[m]; i++) {
     26:       norms[aij->j[i]] += PetscAbsScalar(aij->a[i]*aij->a[i]);
     27:     }
     28:   } else if (type == NORM_1) {
     29:     for (i=0; i<aij->i[m]; i++) {
     30:       norms[aij->j[i]] += PetscAbsScalar(aij->a[i]);
     31:     }
     32:   } else if (type == NORM_INFINITY) {
     33:     for (i=0; i<aij->i[m]; i++) {
     34:       norms[aij->j[i]] = PetscMax(PetscAbsScalar(aij->a[i]),norms[aij->j[i]]);
     35:     }
     36:   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Unknown NormType");

     38:   if (type == NORM_2) {
     39:     for (i=0; i<n; i++) norms[i] = PetscSqrtReal(norms[i]);
     40:   }
     41:   return(0);
     42: }

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

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

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

     84:   MatMarkDiagonal_SeqAIJ(A);
     85:   diag = a->diag;
     86:   for (i=0; i<m; i++) {
     87:     if ((jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) {
     88:       cnt++;
     89:     }
     90:   }
     91:   PetscMalloc1(cnt,&rows);
     92:   cnt  = 0;
     93:   for (i=0; i<m; i++) {
     94:     if ((jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) {
     95:       rows[cnt++] = i;
     96:     }
     97:   }
     98:   *nrows = cnt;
     99:   *zrows = rows;
    100:   return(0);
    101: }

    105: PetscErrorCode MatFindZeroDiagonals_SeqAIJ(Mat A,IS *zrows)
    106: {
    107:   PetscInt       nrows,*rows;

    111:   *zrows = NULL;
    112:   MatFindZeroDiagonals_SeqAIJ_Private(A,&nrows,&rows);
    113:   ISCreateGeneral(PetscObjectComm((PetscObject)A),nrows,rows,PETSC_OWN_POINTER,zrows);
    114:   return(0);
    115: }

    119: PetscErrorCode MatFindNonzeroRows_SeqAIJ(Mat A,IS *keptrows)
    120: {
    121:   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
    122:   const MatScalar *aa;
    123:   PetscInt        m=A->rmap->n,cnt = 0;
    124:   const PetscInt  *ii;
    125:   PetscInt        n,i,j,*rows;
    126:   PetscErrorCode  ierr;

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

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

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

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

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

    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: }

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

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

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

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

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

    298:   PetscFree(*ia);
    299:   PetscFree(*ja);
    300:   return(0);
    301: }

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

    319:   *nn = n;
    320:   if (!ia) return(0);

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

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

    357:   MatRestoreColumnIJ_SeqAIJ(A,oshift,symmetric,inodecompressed,n,ia,ja,done);
    358:   PetscFree(*spidx);
    359:   return(0);
    360: }

    364: PetscErrorCode MatSetValuesRow_SeqAIJ(Mat A,PetscInt row,const PetscScalar v[])
    365: {
    366:   Mat_SeqAIJ     *a  = (Mat_SeqAIJ*)A->data;
    367:   PetscInt       *ai = a->i;

    371:   PetscMemcpy(a->a+ai[row],v,(ai[row+1]-ai[row])*sizeof(PetscScalar));
    372:   return(0);
    373: }

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

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

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

    385: */

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

    399:   row = ridx[im[0]];
    400:   rp   = aj + ai[row];
    401:   ap = aa + ai[row];
    402:   nrow = ailen[row];
    403:   low  = 0;
    404:   high = nrow;
    405:   for (l=0; l<n; l++) { /* loop over added columns */
    406:     col = cidx[in[l]];
    407:     value = v[l];

    409:     if (col <= lastcol) low = 0;
    410:     else high = nrow;
    411:     lastcol = col;
    412:     while (high-low > 5) {
    413:       t = (low+high)/2;
    414:       if (rp[t] > col) high = t;
    415:       else low = t;
    416:     }
    417:     for (i=low; i<high; i++) {
    418:       if (rp[i] == col) {
    419:         ap[i] += value;
    420:         low = i + 1;
    421:         break;
    422:       }
    423:     }
    424:   }
    425:   return 0;
    426: }

    430: PetscErrorCode MatSetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
    431: {
    432:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
    433:   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
    434:   PetscInt       *imax = a->imax,*ai = a->i,*ailen = a->ilen;
    436:   PetscInt       *aj = a->j,nonew = a->nonew,lastcol = -1;
    437:   MatScalar      *ap,value,*aa = a->a;
    438:   PetscBool      ignorezeroentries = a->ignorezeroentries;
    439:   PetscBool      roworiented       = a->roworiented;

    442:   for (k=0; k<m; k++) { /* loop over added rows */
    443:     row = im[k];
    444:     if (row < 0) continue;
    445: #if defined(PETSC_USE_DEBUG)
    446:     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);
    447: #endif
    448:     rp   = aj + ai[row]; ap = aa + ai[row];
    449:     rmax = imax[row]; nrow = ailen[row];
    450:     low  = 0;
    451:     high = nrow;
    452:     for (l=0; l<n; l++) { /* loop over added columns */
    453:       if (in[l] < 0) continue;
    454: #if defined(PETSC_USE_DEBUG)
    455:       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);
    456: #endif
    457:       col = in[l];
    458:       if (roworiented) {
    459:         value = v[l + k*n];
    460:       } else {
    461:         value = v[k + l*m];
    462:       }
    463:       if ((value == 0.0 && ignorezeroentries) && (is == ADD_VALUES)) continue;

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

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

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

    547: PetscErrorCode MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer)
    548: {
    549:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
    551:   PetscInt       i,*col_lens;
    552:   int            fd;
    553:   FILE           *file;

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

    559:   col_lens[0] = MAT_FILE_CLASSID;
    560:   col_lens[1] = A->rmap->n;
    561:   col_lens[2] = A->cmap->n;
    562:   col_lens[3] = a->nz;

    564:   /* store lengths of each row and write (including header) to file */
    565:   for (i=0; i<A->rmap->n; i++) {
    566:     col_lens[4+i] = a->i[i+1] - a->i[i];
    567:   }
    568:   PetscBinaryWrite(fd,col_lens,4+A->rmap->n,PETSC_INT,PETSC_TRUE);
    569:   PetscFree(col_lens);

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

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

    577:   PetscViewerBinaryGetInfoPointer(viewer,&file);
    578:   if (file) {
    579:     fprintf(file,"-matload_block_size %d\n",(int)PetscAbs(A->rmap->bs));
    580:   }
    581:   return(0);
    582: }

    584: extern PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);

    588: PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer)
    589: {
    590:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
    591:   PetscErrorCode    ierr;
    592:   PetscInt          i,j,m = A->rmap->n;
    593:   const char        *name;
    594:   PetscViewerFormat format;

    597:   PetscViewerGetFormat(viewer,&format);
    598:   if (format == PETSC_VIEWER_ASCII_MATLAB) {
    599:     PetscInt nofinalvalue = 0;
    600:     if (m && ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->cmap->n-1))) {
    601:       /* Need a dummy value to ensure the dimension of the matrix. */
    602:       nofinalvalue = 1;
    603:     }
    604:     PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
    605:     PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->cmap->n);
    606:     PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);
    607: #if defined(PETSC_USE_COMPLEX)
    608:     PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,4);\n",a->nz+nofinalvalue);
    609: #else
    610:     PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);
    611: #endif
    612:     PetscViewerASCIIPrintf(viewer,"zzz = [\n");

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

    718:     for (i=0; i<a->i[m]; i++) {
    719:       if (PetscImaginaryPart(a->a[i]) != 0.0) {
    720:         realonly = PETSC_FALSE;
    721:         break;
    722:       }
    723:     }
    724: #endif

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

    801:         /* U part */
    802:         for (j=a->diag[i+1]+1; j<a->diag[i]; j++) {
    803: #if defined(PETSC_USE_COMPLEX)
    804:           if (PetscImaginaryPart(a->a[j]) > 0.0) {
    805:             PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));
    806:           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
    807:             PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)(-PetscImaginaryPart(a->a[j])));
    808:           } else {
    809:             PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));
    810:           }
    811: #else
    812:           PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);
    813: #endif
    814:         }
    815:         PetscViewerASCIIPrintf(viewer,"\n");
    816:       }
    817:     } else {
    818:       for (i=0; i<m; i++) {
    819:         PetscViewerASCIIPrintf(viewer,"row %D:",i);
    820:         for (j=a->i[i]; j<a->i[i+1]; j++) {
    821: #if defined(PETSC_USE_COMPLEX)
    822:           if (PetscImaginaryPart(a->a[j]) > 0.0) {
    823:             PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));
    824:           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
    825:             PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)-PetscImaginaryPart(a->a[j]));
    826:           } else {
    827:             PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));
    828:           }
    829: #else
    830:           PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);
    831: #endif
    832:         }
    833:         PetscViewerASCIIPrintf(viewer,"\n");
    834:       }
    835:     }
    836:     PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
    837:   }
    838:   PetscViewerFlush(viewer);
    839:   return(0);
    840: }

    842: #include <petscdraw.h>
    845: PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
    846: {
    847:   Mat               A  = (Mat) Aa;
    848:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
    849:   PetscErrorCode    ierr;
    850:   PetscInt          i,j,m = A->rmap->n;
    851:   int               color;
    852:   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r;
    853:   PetscViewer       viewer;
    854:   PetscViewerFormat format;

    857:   PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);
    858:   PetscViewerGetFormat(viewer,&format);
    859:   PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);

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

    863:   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
    864:     PetscDrawCollectiveBegin(draw);
    865:     /* Blue for negative, Cyan for zero and  Red for positive */
    866:     color = PETSC_DRAW_BLUE;
    867:     for (i=0; i<m; i++) {
    868:       y_l = m - i - 1.0; y_r = y_l + 1.0;
    869:       for (j=a->i[i]; j<a->i[i+1]; j++) {
    870:         x_l = a->j[j]; x_r = x_l + 1.0;
    871:         if (PetscRealPart(a->a[j]) >=  0.) continue;
    872:         PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);
    873:       }
    874:     }
    875:     color = PETSC_DRAW_CYAN;
    876:     for (i=0; i<m; i++) {
    877:       y_l = m - i - 1.0; y_r = y_l + 1.0;
    878:       for (j=a->i[i]; j<a->i[i+1]; j++) {
    879:         x_l = a->j[j]; x_r = x_l + 1.0;
    880:         if (a->a[j] !=  0.) continue;
    881:         PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);
    882:       }
    883:     }
    884:     color = PETSC_DRAW_RED;
    885:     for (i=0; i<m; i++) {
    886:       y_l = m - i - 1.0; y_r = y_l + 1.0;
    887:       for (j=a->i[i]; j<a->i[i+1]; j++) {
    888:         x_l = a->j[j]; x_r = x_l + 1.0;
    889:         if (PetscRealPart(a->a[j]) <=  0.) continue;
    890:         PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);
    891:       }
    892:     }
    893:     PetscDrawCollectiveEnd(draw);
    894:   } else {
    895:     /* use contour shading to indicate magnitude of values */
    896:     /* first determine max of all nonzero values */
    897:     PetscReal minv = 0.0, maxv = 0.0;
    898:     PetscInt  nz = a->nz, count = 0;
    899:     PetscDraw popup;

    901:     for (i=0; i<nz; i++) {
    902:       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
    903:     }
    904:     if (minv >= maxv) maxv = minv + PETSC_SMALL;
    905:     PetscDrawGetPopup(draw,&popup);
    906:     PetscDrawScalePopup(popup,minv,maxv);

    908:     PetscDrawCollectiveBegin(draw);
    909:     for (i=0; i<m; i++) {
    910:       y_l = m - i - 1.0;
    911:       y_r = y_l + 1.0;
    912:       for (j=a->i[i]; j<a->i[i+1]; j++) {
    913:         x_l = a->j[j];
    914:         x_r = x_l + 1.0;
    915:         color = PetscDrawRealToColor(PetscAbsScalar(a->a[count]),minv,maxv);
    916:         PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);
    917:         count++;
    918:       }
    919:     }
    920:     PetscDrawCollectiveEnd(draw);
    921:   }
    922:   return(0);
    923: }

    925: #include <petscdraw.h>
    928: PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
    929: {
    931:   PetscDraw      draw;
    932:   PetscReal      xr,yr,xl,yl,h,w;
    933:   PetscBool      isnull;

    936:   PetscViewerDrawGetDraw(viewer,0,&draw);
    937:   PetscDrawIsNull(draw,&isnull);
    938:   if (isnull) return(0);

    940:   xr   = A->cmap->n; yr  = A->rmap->n; h = yr/10.0; w = xr/10.0;
    941:   xr  += w;          yr += h;         xl = -w;     yl = -h;
    942:   PetscDrawSetCoordinates(draw,xl,yl,xr,yr);
    943:   PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);
    944:   PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);
    945:   PetscObjectCompose((PetscObject)A,"Zoomviewer",NULL);
    946:   PetscDrawSave(draw);
    947:   return(0);
    948: }

    952: PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer)
    953: {
    955:   PetscBool      iascii,isbinary,isdraw;

    958:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
    959:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
    960:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
    961:   if (iascii) {
    962:     MatView_SeqAIJ_ASCII(A,viewer);
    963:   } else if (isbinary) {
    964:     MatView_SeqAIJ_Binary(A,viewer);
    965:   } else if (isdraw) {
    966:     MatView_SeqAIJ_Draw(A,viewer);
    967:   }
    968:   MatView_SeqAIJ_Inode(A,viewer);
    969:   return(0);
    970: }

    974: PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
    975: {
    976:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
    978:   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
    979:   PetscInt       m      = A->rmap->n,*ip,N,*ailen = a->ilen,rmax = 0;
    980:   MatScalar      *aa    = a->a,*ap;
    981:   PetscReal      ratio  = 0.6;

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

    986:   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
    987:   for (i=1; i<m; i++) {
    988:     /* move each row back by the amount of empty slots (fshift) before it*/
    989:     fshift += imax[i-1] - ailen[i-1];
    990:     rmax    = PetscMax(rmax,ailen[i]);
    991:     if (fshift) {
    992:       ip = aj + ai[i];
    993:       ap = aa + ai[i];
    994:       N  = ailen[i];
    995:       for (j=0; j<N; j++) {
    996:         ip[j-fshift] = ip[j];
    997:         ap[j-fshift] = ap[j];
    998:       }
    999:     }
    1000:     ai[i] = ai[i-1] + ailen[i-1];
    1001:   }
    1002:   if (m) {
    1003:     fshift += imax[m-1] - ailen[m-1];
    1004:     ai[m]   = ai[m-1] + ailen[m-1];
    1005:   }

    1007:   /* reset ilen and imax for each row */
    1008:   a->nonzerorowcnt = 0;
    1009:   for (i=0; i<m; i++) {
    1010:     ailen[i] = imax[i] = ai[i+1] - ai[i];
    1011:     a->nonzerorowcnt += ((ai[i+1] - ai[i]) > 0);
    1012:   }
    1013:   a->nz = ai[m];
    1014:   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);

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

    1021:   A->info.mallocs    += a->reallocs;
    1022:   a->reallocs         = 0;
    1023:   A->info.nz_unneeded = (PetscReal)fshift;
    1024:   a->rmax             = rmax;

    1026:   MatCheckCompressedRow(A,a->nonzerorowcnt,&a->compressedrow,a->i,m,ratio);
    1027:   MatAssemblyEnd_SeqAIJ_Inode(A,mode);
    1028:   MatSeqAIJInvalidateDiagonal(A);
    1029:   return(0);
    1030: }

    1034: PetscErrorCode MatRealPart_SeqAIJ(Mat A)
    1035: {
    1036:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
    1037:   PetscInt       i,nz = a->nz;
    1038:   MatScalar      *aa = a->a;

    1042:   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
    1043:   MatSeqAIJInvalidateDiagonal(A);
    1044:   return(0);
    1045: }

    1049: PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
    1050: {
    1051:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
    1052:   PetscInt       i,nz = a->nz;
    1053:   MatScalar      *aa = a->a;

    1057:   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
    1058:   MatSeqAIJInvalidateDiagonal(A);
    1059:   return(0);
    1060: }

    1064: PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
    1065: {
    1066:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;

    1070:   PetscMemzero(a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));
    1071:   MatSeqAIJInvalidateDiagonal(A);
    1072:   return(0);
    1073: }

    1077: PetscErrorCode MatDestroy_SeqAIJ(Mat A)
    1078: {
    1079:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;

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

    1100:   MatDestroy_SeqAIJ_Inode(A);
    1101:   PetscFree(A->data);

    1103:   PetscObjectChangeTypeName((PetscObject)A,0);
    1104:   PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetColumnIndices_C",NULL);
    1105:   PetscObjectComposeFunction((PetscObject)A,"MatStoreValues_C",NULL);
    1106:   PetscObjectComposeFunction((PetscObject)A,"MatRetrieveValues_C",NULL);
    1107:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqsbaij_C",NULL);
    1108:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqbaij_C",NULL);
    1109:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqaijperm_C",NULL);
    1110: #if defined(PETSC_HAVE_ELEMENTAL)
    1111:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_elemental_C",NULL);
    1112: #endif
    1113:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqdense_C",NULL);
    1114:   PetscObjectComposeFunction((PetscObject)A,"MatIsTranspose_C",NULL);
    1115:   PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocation_C",NULL);
    1116:   PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C",NULL);
    1117:   PetscObjectComposeFunction((PetscObject)A,"MatReorderForNonzeroDiagonal_C",NULL);
    1118:   return(0);
    1119: }

    1123: PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscBool flg)
    1124: {
    1125:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;

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

    1175: PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
    1176: {
    1177:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
    1179:   PetscInt       i,j,n,*ai=a->i,*aj=a->j,nz;
    1180:   PetscScalar    *aa=a->a,*x,zero=0.0;

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

    1186:   if (A->factortype == MAT_FACTOR_ILU || A->factortype == MAT_FACTOR_LU) {
    1187:     PetscInt *diag=a->diag;
    1188:     VecGetArray(v,&x);
    1189:     for (i=0; i<n; i++) x[i] = 1.0/aa[diag[i]];
    1190:     VecRestoreArray(v,&x);
    1191:     return(0);
    1192:   }

    1194:   VecSet(v,zero);
    1195:   VecGetArray(v,&x);
    1196:   for (i=0; i<n; i++) {
    1197:     nz = ai[i+1] - ai[i];
    1198:     if (!nz) x[i] = 0.0;
    1199:     for (j=ai[i]; j<ai[i+1]; j++) {
    1200:       if (aj[j] == i) {
    1201:         x[i] = aa[j];
    1202:         break;
    1203:       }
    1204:     }
    1205:   }
    1206:   VecRestoreArray(v,&x);
    1207:   return(0);
    1208: }

    1210: #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
    1213: PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
    1214: {
    1215:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
    1216:   PetscScalar       *y;
    1217:   const PetscScalar *x;
    1218:   PetscErrorCode    ierr;
    1219:   PetscInt          m = A->rmap->n;
    1220: #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
    1221:   const MatScalar   *v;
    1222:   PetscScalar       alpha;
    1223:   PetscInt          n,i,j;
    1224:   const PetscInt    *idx,*ii,*ridx=NULL;
    1225:   Mat_CompressedRow cprow    = a->compressedrow;
    1226:   PetscBool         usecprow = cprow.use;
    1227: #endif

    1230:   if (zz != yy) {VecCopy(zz,yy);}
    1231:   VecGetArrayRead(xx,&x);
    1232:   VecGetArray(yy,&y);

    1234: #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
    1235:   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
    1236: #else
    1237:   if (usecprow) {
    1238:     m    = cprow.nrows;
    1239:     ii   = cprow.i;
    1240:     ridx = cprow.rindex;
    1241:   } else {
    1242:     ii = a->i;
    1243:   }
    1244:   for (i=0; i<m; i++) {
    1245:     idx = a->j + ii[i];
    1246:     v   = a->a + ii[i];
    1247:     n   = ii[i+1] - ii[i];
    1248:     if (usecprow) {
    1249:       alpha = x[ridx[i]];
    1250:     } else {
    1251:       alpha = x[i];
    1252:     }
    1253:     for (j=0; j<n; j++) y[idx[j]] += alpha*v[j];
    1254:   }
    1255: #endif
    1256:   PetscLogFlops(2.0*a->nz);
    1257:   VecRestoreArrayRead(xx,&x);
    1258:   VecRestoreArray(yy,&y);
    1259:   return(0);
    1260: }

    1264: PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
    1265: {

    1269:   VecSet(yy,0.0);
    1270:   MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);
    1271:   return(0);
    1272: }

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

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

    1291: #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
    1292: #pragma disjoint(*x,*y,*aa)
    1293: #endif

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

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

    1350: #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
    1351: #pragma disjoint(*x,*y,*aa)
    1352: #endif

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

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

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

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

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

    1501: /*
    1502:      Adds diagonal pointers to sparse matrix structure.
    1503: */
    1506: PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
    1507: {
    1508:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
    1510:   PetscInt       i,j,m = A->rmap->n;

    1513:   if (!a->diag) {
    1514:     PetscMalloc1(m,&a->diag);
    1515:     PetscLogObjectMemory((PetscObject)A, m*sizeof(PetscInt));
    1516:   }
    1517:   for (i=0; i<A->rmap->n; i++) {
    1518:     a->diag[i] = a->i[i+1];
    1519:     for (j=a->i[i]; j<a->i[i+1]; j++) {
    1520:       if (a->j[j] == i) {
    1521:         a->diag[i] = j;
    1522:         break;
    1523:       }
    1524:     }
    1525:   }
    1526:   return(0);
    1527: }

    1529: /*
    1530:      Checks for missing diagonals
    1531: */
    1534: PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscBool  *missing,PetscInt *d)
    1535: {
    1536:   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
    1537:   PetscInt   *diag,*ii = a->i,i;

    1540:   *missing = PETSC_FALSE;
    1541:   if (A->rmap->n > 0 && !ii) {
    1542:     *missing = PETSC_TRUE;
    1543:     if (d) *d = 0;
    1544:     PetscInfo(A,"Matrix has no entries therefore is missing diagonal\n");
    1545:   } else {
    1546:     diag = a->diag;
    1547:     for (i=0; i<A->rmap->n; i++) {
    1548:       if (diag[i] >= ii[i+1]) {
    1549:         *missing = PETSC_TRUE;
    1550:         if (d) *d = i;
    1551:         PetscInfo1(A,"Matrix is missing diagonal number %D\n",i);
    1552:         break;
    1553:       }
    1554:     }
    1555:   }
    1556:   return(0);
    1557: }

    1561: /*
    1562:    Negative shift indicates do not generate an error if there is a zero diagonal, just invert it anyways
    1563: */
    1564: PetscErrorCode  MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
    1565: {
    1566:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
    1568:   PetscInt       i,*diag,m = A->rmap->n;
    1569:   MatScalar      *v = a->a;
    1570:   PetscScalar    *idiag,*mdiag;

    1573:   if (a->idiagvalid) return(0);
    1574:   MatMarkDiagonal_SeqAIJ(A);
    1575:   diag = a->diag;
    1576:   if (!a->idiag) {
    1577:     PetscMalloc3(m,&a->idiag,m,&a->mdiag,m,&a->ssor_work);
    1578:     PetscLogObjectMemory((PetscObject)A, 3*m*sizeof(PetscScalar));
    1579:     v    = a->a;
    1580:   }
    1581:   mdiag = a->mdiag;
    1582:   idiag = a->idiag;

    1584:   if (omega == 1.0 && PetscRealPart(fshift) <= 0.0) {
    1585:     for (i=0; i<m; i++) {
    1586:       mdiag[i] = v[diag[i]];
    1587:       if (!PetscAbsScalar(mdiag[i])) { /* zero diagonal */
    1588:         if (PetscRealPart(fshift)) {
    1589:           PetscInfo1(A,"Zero diagonal on row %D\n",i);
    1590:           A->errortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
    1591:         } else {
    1592:           SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
    1593:         }
    1594:       }
    1595:       idiag[i] = 1.0/v[diag[i]];
    1596:     }
    1597:     PetscLogFlops(m);
    1598:   } else {
    1599:     for (i=0; i<m; i++) {
    1600:       mdiag[i] = v[diag[i]];
    1601:       idiag[i] = omega/(fshift + v[diag[i]]);
    1602:     }
    1603:     PetscLogFlops(2.0*m);
    1604:   }
    1605:   a->idiagvalid = PETSC_TRUE;
    1606:   return(0);
    1607: }

    1609: #include <../src/mat/impls/aij/seq/ftn-kernels/frelax.h>
    1612: PetscErrorCode MatSOR_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
    1613: {
    1614:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
    1615:   PetscScalar       *x,d,sum,*t,scale;
    1616:   const MatScalar   *v,*idiag=0,*mdiag;
    1617:   const PetscScalar *b, *bs,*xb, *ts;
    1618:   PetscErrorCode    ierr;
    1619:   PetscInt          n,m = A->rmap->n,i;
    1620:   const PetscInt    *idx,*diag;

    1623:   its = its*lits;

    1625:   if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
    1626:   if (!a->idiagvalid) {MatInvertDiagonal_SeqAIJ(A,omega,fshift);}
    1627:   a->fshift = fshift;
    1628:   a->omega  = omega;

    1630:   diag  = a->diag;
    1631:   t     = a->ssor_work;
    1632:   idiag = a->idiag;
    1633:   mdiag = a->mdiag;

    1635:   VecGetArray(xx,&x);
    1636:   VecGetArrayRead(bb,&b);
    1637:   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
    1638:   if (flag == SOR_APPLY_UPPER) {
    1639:     /* apply (U + D/omega) to the vector */
    1640:     bs = b;
    1641:     for (i=0; i<m; i++) {
    1642:       d   = fshift + mdiag[i];
    1643:       n   = a->i[i+1] - diag[i] - 1;
    1644:       idx = a->j + diag[i] + 1;
    1645:       v   = a->a + diag[i] + 1;
    1646:       sum = b[i]*d/omega;
    1647:       PetscSparseDensePlusDot(sum,bs,v,idx,n);
    1648:       x[i] = sum;
    1649:     }
    1650:     VecRestoreArray(xx,&x);
    1651:     VecRestoreArrayRead(bb,&b);
    1652:     PetscLogFlops(a->nz);
    1653:     return(0);
    1654:   }

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

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

    1663:     to a vector efficiently using Eisenstat's trick.
    1664:     */
    1665:     scale = (2.0/omega) - 1.0;

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

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

    1681:     /*  t = (E + L)^{-1}t */
    1682:     ts   = t;
    1683:     diag = a->diag;
    1684:     for (i=0; i<m; i++) {
    1685:       n   = diag[i] - a->i[i];
    1686:       idx = a->j + a->i[i];
    1687:       v   = a->a + a->i[i];
    1688:       sum = t[i];
    1689:       PetscSparseDenseMinusDot(sum,ts,v,idx,n);
    1690:       t[i] = sum*idiag[i];
    1691:       /*  x = x + t */
    1692:       x[i] += t[i];
    1693:     }

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

    1784: PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
    1785: {
    1786:   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;

    1789:   info->block_size   = 1.0;
    1790:   info->nz_allocated = (double)a->maxnz;
    1791:   info->nz_used      = (double)a->nz;
    1792:   info->nz_unneeded  = (double)(a->maxnz - a->nz);
    1793:   info->assemblies   = (double)A->num_ass;
    1794:   info->mallocs      = (double)A->info.mallocs;
    1795:   info->memory       = ((PetscObject)A)->mem;
    1796:   if (A->factortype) {
    1797:     info->fill_ratio_given  = A->info.fill_ratio_given;
    1798:     info->fill_ratio_needed = A->info.fill_ratio_needed;
    1799:     info->factor_mallocs    = A->info.factor_mallocs;
    1800:   } else {
    1801:     info->fill_ratio_given  = 0;
    1802:     info->fill_ratio_needed = 0;
    1803:     info->factor_mallocs    = 0;
    1804:   }
    1805:   return(0);
    1806: }

    1810: PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
    1811: {
    1812:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
    1813:   PetscInt          i,m = A->rmap->n - 1,d = 0;
    1814:   PetscErrorCode    ierr;
    1815:   const PetscScalar *xx;
    1816:   PetscScalar       *bb;
    1817:   PetscBool         missing;

    1820:   if (x && b) {
    1821:     VecGetArrayRead(x,&xx);
    1822:     VecGetArray(b,&bb);
    1823:     for (i=0; i<N; i++) {
    1824:       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
    1825:       bb[rows[i]] = diag*xx[rows[i]];
    1826:     }
    1827:     VecRestoreArrayRead(x,&xx);
    1828:     VecRestoreArray(b,&bb);
    1829:   }

    1831:   if (a->keepnonzeropattern) {
    1832:     for (i=0; i<N; i++) {
    1833:       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
    1834:       PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));
    1835:     }
    1836:     if (diag != 0.0) {
    1837:       MatMissingDiagonal_SeqAIJ(A,&missing,&d);
    1838:       if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
    1839:       for (i=0; i<N; i++) {
    1840:         a->a[a->diag[rows[i]]] = diag;
    1841:       }
    1842:     }
    1843:   } else {
    1844:     if (diag != 0.0) {
    1845:       for (i=0; i<N; i++) {
    1846:         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
    1847:         if (a->ilen[rows[i]] > 0) {
    1848:           a->ilen[rows[i]]    = 1;
    1849:           a->a[a->i[rows[i]]] = diag;
    1850:           a->j[a->i[rows[i]]] = rows[i];
    1851:         } else { /* in case row was completely empty */
    1852:           MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);
    1853:         }
    1854:       }
    1855:     } else {
    1856:       for (i=0; i<N; i++) {
    1857:         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
    1858:         a->ilen[rows[i]] = 0;
    1859:       }
    1860:     }
    1861:     A->nonzerostate++;
    1862:   }
    1863:   MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);
    1864:   return(0);
    1865: }

    1869: PetscErrorCode MatZeroRowsColumns_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
    1870: {
    1871:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
    1872:   PetscInt          i,j,m = A->rmap->n - 1,d = 0;
    1873:   PetscErrorCode    ierr;
    1874:   PetscBool         missing,*zeroed,vecs = PETSC_FALSE;
    1875:   const PetscScalar *xx;
    1876:   PetscScalar       *bb;

    1879:   if (x && b) {
    1880:     VecGetArrayRead(x,&xx);
    1881:     VecGetArray(b,&bb);
    1882:     vecs = PETSC_TRUE;
    1883:   }
    1884:   PetscCalloc1(A->rmap->n,&zeroed);
    1885:   for (i=0; i<N; i++) {
    1886:     if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
    1887:     PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));

    1889:     zeroed[rows[i]] = PETSC_TRUE;
    1890:   }
    1891:   for (i=0; i<A->rmap->n; i++) {
    1892:     if (!zeroed[i]) {
    1893:       for (j=a->i[i]; j<a->i[i+1]; j++) {
    1894:         if (zeroed[a->j[j]]) {
    1895:           if (vecs) bb[i] -= a->a[j]*xx[a->j[j]];
    1896:           a->a[j] = 0.0;
    1897:         }
    1898:       }
    1899:     } else if (vecs) bb[i] = diag*xx[i];
    1900:   }
    1901:   if (x && b) {
    1902:     VecRestoreArrayRead(x,&xx);
    1903:     VecRestoreArray(b,&bb);
    1904:   }
    1905:   PetscFree(zeroed);
    1906:   if (diag != 0.0) {
    1907:     MatMissingDiagonal_SeqAIJ(A,&missing,&d);
    1908:     if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
    1909:     for (i=0; i<N; i++) {
    1910:       a->a[a->diag[rows[i]]] = diag;
    1911:     }
    1912:   }
    1913:   MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);
    1914:   return(0);
    1915: }

    1919: PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
    1920: {
    1921:   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
    1922:   PetscInt   *itmp;

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

    1927:   *nz = a->i[row+1] - a->i[row];
    1928:   if (v) *v = a->a + a->i[row];
    1929:   if (idx) {
    1930:     itmp = a->j + a->i[row];
    1931:     if (*nz) *idx = itmp;
    1932:     else *idx = 0;
    1933:   }
    1934:   return(0);
    1935: }

    1937: /* remove this function? */
    1940: PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
    1941: {
    1943:   return(0);
    1944: }

    1948: PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
    1949: {
    1950:   Mat_SeqAIJ     *a  = (Mat_SeqAIJ*)A->data;
    1951:   MatScalar      *v  = a->a;
    1952:   PetscReal      sum = 0.0;
    1954:   PetscInt       i,j;

    1957:   if (type == NORM_FROBENIUS) {
    1958:     for (i=0; i<a->nz; i++) {
    1959:       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
    1960:     }
    1961:     *nrm = PetscSqrtReal(sum);
    1962:     PetscLogFlops(2*a->nz);
    1963:   } else if (type == NORM_1) {
    1964:     PetscReal *tmp;
    1965:     PetscInt  *jj = a->j;
    1966:     PetscCalloc1(A->cmap->n+1,&tmp);
    1967:     *nrm = 0.0;
    1968:     for (j=0; j<a->nz; j++) {
    1969:       tmp[*jj++] += PetscAbsScalar(*v);  v++;
    1970:     }
    1971:     for (j=0; j<A->cmap->n; j++) {
    1972:       if (tmp[j] > *nrm) *nrm = tmp[j];
    1973:     }
    1974:     PetscFree(tmp);
    1975:     PetscLogFlops(PetscMax(a->nz-1,0));
    1976:   } else if (type == NORM_INFINITY) {
    1977:     *nrm = 0.0;
    1978:     for (j=0; j<A->rmap->n; j++) {
    1979:       v   = a->a + a->i[j];
    1980:       sum = 0.0;
    1981:       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
    1982:         sum += PetscAbsScalar(*v); v++;
    1983:       }
    1984:       if (sum > *nrm) *nrm = sum;
    1985:     }
    1986:     PetscLogFlops(PetscMax(a->nz-1,0));
    1987:   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for two norm");
    1988:   return(0);
    1989: }

    1991: /* Merged from MatGetSymbolicTranspose_SeqAIJ() - replace MatGetSymbolicTranspose_SeqAIJ()? */
    1994: PetscErrorCode MatTransposeSymbolic_SeqAIJ(Mat A,Mat *B)
    1995: {
    1997:   PetscInt       i,j,anzj;
    1998:   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data,*b;
    1999:   PetscInt       an=A->cmap->N,am=A->rmap->N;
    2000:   PetscInt       *ati,*atj,*atfill,*ai=a->i,*aj=a->j;

    2003:   /* Allocate space for symbolic transpose info and work array */
    2004:   PetscCalloc1(an+1,&ati);
    2005:   PetscMalloc1(ai[am],&atj);
    2006:   PetscMalloc1(an,&atfill);

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

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

    2017:   /* Walk through A row-wise and mark nonzero entries of A^T. */
    2018:   for (i=0;i<am;i++) {
    2019:     anzj = ai[i+1] - ai[i];
    2020:     for (j=0;j<anzj;j++) {
    2021:       atj[atfill[*aj]] = i;
    2022:       atfill[*aj++]   += 1;
    2023:     }
    2024:   }

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

    2031:   b          = (Mat_SeqAIJ*)((*B)->data);
    2032:   b->free_a  = PETSC_FALSE;
    2033:   b->free_ij = PETSC_TRUE;
    2034:   b->nonew   = 0;
    2035:   return(0);
    2036: }

    2040: PetscErrorCode MatTranspose_SeqAIJ(Mat A,MatReuse reuse,Mat *B)
    2041: {
    2042:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
    2043:   Mat            C;
    2045:   PetscInt       i,*aj = a->j,*ai = a->i,m = A->rmap->n,len,*col;
    2046:   MatScalar      *array = a->a;

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

    2051:   if (reuse == MAT_INITIAL_MATRIX || *B == A) {
    2052:     PetscCalloc1(1+A->cmap->n,&col);

    2054:     for (i=0; i<ai[m]; i++) col[aj[i]] += 1;
    2055:     MatCreate(PetscObjectComm((PetscObject)A),&C);
    2056:     MatSetSizes(C,A->cmap->n,m,A->cmap->n,m);
    2057:     MatSetBlockSizes(C,PetscAbs(A->cmap->bs),PetscAbs(A->rmap->bs));
    2058:     MatSetType(C,((PetscObject)A)->type_name);
    2059:     MatSeqAIJSetPreallocation_SeqAIJ(C,0,col);
    2060:     PetscFree(col);
    2061:   } else {
    2062:     C = *B;
    2063:   }

    2065:   for (i=0; i<m; i++) {
    2066:     len    = ai[i+1]-ai[i];
    2067:     MatSetValues_SeqAIJ(C,len,aj,1,&i,array,INSERT_VALUES);
    2068:     array += len;
    2069:     aj    += len;
    2070:   }
    2071:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
    2072:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);

    2074:   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
    2075:     *B = C;
    2076:   } else {
    2077:     MatHeaderMerge(A,&C);
    2078:   }
    2079:   return(0);
    2080: }

    2084: PetscErrorCode  MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
    2085: {
    2086:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) B->data;
    2087:   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
    2088:   MatScalar      *va,*vb;
    2090:   PetscInt       ma,na,mb,nb, i;

    2093:   MatGetSize(A,&ma,&na);
    2094:   MatGetSize(B,&mb,&nb);
    2095:   if (ma!=nb || na!=mb) {
    2096:     *f = PETSC_FALSE;
    2097:     return(0);
    2098:   }
    2099:   aii  = aij->i; bii = bij->i;
    2100:   adx  = aij->j; bdx = bij->j;
    2101:   va   = aij->a; vb = bij->a;
    2102:   PetscMalloc1(ma,&aptr);
    2103:   PetscMalloc1(mb,&bptr);
    2104:   for (i=0; i<ma; i++) aptr[i] = aii[i];
    2105:   for (i=0; i<mb; i++) bptr[i] = bii[i];

    2107:   *f = PETSC_TRUE;
    2108:   for (i=0; i<ma; i++) {
    2109:     while (aptr[i]<aii[i+1]) {
    2110:       PetscInt    idc,idr;
    2111:       PetscScalar vc,vr;
    2112:       /* column/row index/value */
    2113:       idc = adx[aptr[i]];
    2114:       idr = bdx[bptr[idc]];
    2115:       vc  = va[aptr[i]];
    2116:       vr  = vb[bptr[idc]];
    2117:       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
    2118:         *f = PETSC_FALSE;
    2119:         goto done;
    2120:       } else {
    2121:         aptr[i]++;
    2122:         if (B || i!=idc) bptr[idc]++;
    2123:       }
    2124:     }
    2125:   }
    2126: done:
    2127:   PetscFree(aptr);
    2128:   PetscFree(bptr);
    2129:   return(0);
    2130: }

    2134: PetscErrorCode  MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
    2135: {
    2136:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) B->data;
    2137:   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
    2138:   MatScalar      *va,*vb;
    2140:   PetscInt       ma,na,mb,nb, i;

    2143:   MatGetSize(A,&ma,&na);
    2144:   MatGetSize(B,&mb,&nb);
    2145:   if (ma!=nb || na!=mb) {
    2146:     *f = PETSC_FALSE;
    2147:     return(0);
    2148:   }
    2149:   aii  = aij->i; bii = bij->i;
    2150:   adx  = aij->j; bdx = bij->j;
    2151:   va   = aij->a; vb = bij->a;
    2152:   PetscMalloc1(ma,&aptr);
    2153:   PetscMalloc1(mb,&bptr);
    2154:   for (i=0; i<ma; i++) aptr[i] = aii[i];
    2155:   for (i=0; i<mb; i++) bptr[i] = bii[i];

    2157:   *f = PETSC_TRUE;
    2158:   for (i=0; i<ma; i++) {
    2159:     while (aptr[i]<aii[i+1]) {
    2160:       PetscInt    idc,idr;
    2161:       PetscScalar vc,vr;
    2162:       /* column/row index/value */
    2163:       idc = adx[aptr[i]];
    2164:       idr = bdx[bptr[idc]];
    2165:       vc  = va[aptr[i]];
    2166:       vr  = vb[bptr[idc]];
    2167:       if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) {
    2168:         *f = PETSC_FALSE;
    2169:         goto done;
    2170:       } else {
    2171:         aptr[i]++;
    2172:         if (B || i!=idc) bptr[idc]++;
    2173:       }
    2174:     }
    2175:   }
    2176: done:
    2177:   PetscFree(aptr);
    2178:   PetscFree(bptr);
    2179:   return(0);
    2180: }

    2184: PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
    2185: {

    2189:   MatIsTranspose_SeqAIJ(A,A,tol,f);
    2190:   return(0);
    2191: }

    2195: PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
    2196: {

    2200:   MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);
    2201:   return(0);
    2202: }

    2206: PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
    2207: {
    2208:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
    2209:   PetscScalar    *l,*r,x;
    2210:   MatScalar      *v;
    2212:   PetscInt       i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz,*jj;

    2215:   if (ll) {
    2216:     /* The local size is used so that VecMPI can be passed to this routine
    2217:        by MatDiagonalScale_MPIAIJ */
    2218:     VecGetLocalSize(ll,&m);
    2219:     if (m != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
    2220:     VecGetArray(ll,&l);
    2221:     v    = a->a;
    2222:     for (i=0; i<m; i++) {
    2223:       x = l[i];
    2224:       M = a->i[i+1] - a->i[i];
    2225:       for (j=0; j<M; j++) (*v++) *= x;
    2226:     }
    2227:     VecRestoreArray(ll,&l);
    2228:     PetscLogFlops(nz);
    2229:   }
    2230:   if (rr) {
    2231:     VecGetLocalSize(rr,&n);
    2232:     if (n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
    2233:     VecGetArray(rr,&r);
    2234:     v    = a->a; jj = a->j;
    2235:     for (i=0; i<nz; i++) (*v++) *= r[*jj++];
    2236:     VecRestoreArray(rr,&r);
    2237:     PetscLogFlops(nz);
    2238:   }
    2239:   MatSeqAIJInvalidateDiagonal(A);
    2240:   return(0);
    2241: }

    2245: PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
    2246: {
    2247:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
    2249:   PetscInt       *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens;
    2250:   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
    2251:   const PetscInt *irow,*icol;
    2252:   PetscInt       nrows,ncols;
    2253:   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
    2254:   MatScalar      *a_new,*mat_a;
    2255:   Mat            C;
    2256:   PetscBool      stride;

    2260:   ISGetIndices(isrow,&irow);
    2261:   ISGetLocalSize(isrow,&nrows);
    2262:   ISGetLocalSize(iscol,&ncols);

    2264:   PetscObjectTypeCompare((PetscObject)iscol,ISSTRIDE,&stride);
    2265:   if (stride) {
    2266:     ISStrideGetInfo(iscol,&first,&step);
    2267:   } else {
    2268:     first = 0;
    2269:     step  = 0;
    2270:   }
    2271:   if (stride && step == 1) {
    2272:     /* special case of contiguous rows */
    2273:     PetscMalloc2(nrows,&lens,nrows,&starts);
    2274:     /* loop over new rows determining lens and starting points */
    2275:     for (i=0; i<nrows; i++) {
    2276:       kstart = ai[irow[i]];
    2277:       kend   = kstart + ailen[irow[i]];
    2278:       starts[i] = kstart;
    2279:       for (k=kstart; k<kend; k++) {
    2280:         if (aj[k] >= first) {
    2281:           starts[i] = k;
    2282:           break;
    2283:         }
    2284:       }
    2285:       sum = 0;
    2286:       while (k < kend) {
    2287:         if (aj[k++] >= first+ncols) break;
    2288:         sum++;
    2289:       }
    2290:       lens[i] = sum;
    2291:     }
    2292:     /* create submatrix */
    2293:     if (scall == MAT_REUSE_MATRIX) {
    2294:       PetscInt n_cols,n_rows;
    2295:       MatGetSize(*B,&n_rows,&n_cols);
    2296:       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
    2297:       MatZeroEntries(*B);
    2298:       C    = *B;
    2299:     } else {
    2300:       PetscInt rbs,cbs;
    2301:       MatCreate(PetscObjectComm((PetscObject)A),&C);
    2302:       MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);
    2303:       ISGetBlockSize(isrow,&rbs);
    2304:       ISGetBlockSize(iscol,&cbs);
    2305:       MatSetBlockSizes(C,rbs,cbs);
    2306:       MatSetType(C,((PetscObject)A)->type_name);
    2307:       MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);
    2308:     }
    2309:     c = (Mat_SeqAIJ*)C->data;

    2311:     /* loop over rows inserting into submatrix */
    2312:     a_new = c->a;
    2313:     j_new = c->j;
    2314:     i_new = c->i;

    2316:     for (i=0; i<nrows; i++) {
    2317:       ii    = starts[i];
    2318:       lensi = lens[i];
    2319:       for (k=0; k<lensi; k++) {
    2320:         *j_new++ = aj[ii+k] - first;
    2321:       }
    2322:       PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));
    2323:       a_new     += lensi;
    2324:       i_new[i+1] = i_new[i] + lensi;
    2325:       c->ilen[i] = lensi;
    2326:     }
    2327:     PetscFree2(lens,starts);
    2328:   } else {
    2329:     ISGetIndices(iscol,&icol);
    2330:     PetscCalloc1(oldcols,&smap);
    2331:     PetscMalloc1(1+nrows,&lens);
    2332:     for (i=0; i<ncols; i++) {
    2333: #if defined(PETSC_USE_DEBUG)
    2334:       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);
    2335: #endif
    2336:       smap[icol[i]] = i+1;
    2337:     }

    2339:     /* determine lens of each row */
    2340:     for (i=0; i<nrows; i++) {
    2341:       kstart  = ai[irow[i]];
    2342:       kend    = kstart + a->ilen[irow[i]];
    2343:       lens[i] = 0;
    2344:       for (k=kstart; k<kend; k++) {
    2345:         if (smap[aj[k]]) {
    2346:           lens[i]++;
    2347:         }
    2348:       }
    2349:     }
    2350:     /* Create and fill new matrix */
    2351:     if (scall == MAT_REUSE_MATRIX) {
    2352:       PetscBool equal;

    2354:       c = (Mat_SeqAIJ*)((*B)->data);
    2355:       if ((*B)->rmap->n  != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
    2356:       PetscMemcmp(c->ilen,lens,(*B)->rmap->n*sizeof(PetscInt),&equal);
    2357:       if (!equal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
    2358:       PetscMemzero(c->ilen,(*B)->rmap->n*sizeof(PetscInt));
    2359:       C    = *B;
    2360:     } else {
    2361:       PetscInt rbs,cbs;
    2362:       MatCreate(PetscObjectComm((PetscObject)A),&C);
    2363:       MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);
    2364:       ISGetBlockSize(isrow,&rbs);
    2365:       ISGetBlockSize(iscol,&cbs);
    2366:       MatSetBlockSizes(C,rbs,cbs);
    2367:       MatSetType(C,((PetscObject)A)->type_name);
    2368:       MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);
    2369:     }
    2370:     c = (Mat_SeqAIJ*)(C->data);
    2371:     for (i=0; i<nrows; i++) {
    2372:       row      = irow[i];
    2373:       kstart   = ai[row];
    2374:       kend     = kstart + a->ilen[row];
    2375:       mat_i    = c->i[i];
    2376:       mat_j    = c->j + mat_i;
    2377:       mat_a    = c->a + mat_i;
    2378:       mat_ilen = c->ilen + i;
    2379:       for (k=kstart; k<kend; k++) {
    2380:         if ((tcol=smap[a->j[k]])) {
    2381:           *mat_j++ = tcol - 1;
    2382:           *mat_a++ = a->a[k];
    2383:           (*mat_ilen)++;

    2385:         }
    2386:       }
    2387:     }
    2388:     /* Free work space */
    2389:     ISRestoreIndices(iscol,&icol);
    2390:     PetscFree(smap);
    2391:     PetscFree(lens);
    2392:     /* sort */
    2393:     for (i = 0; i < nrows; i++) {
    2394:       PetscInt ilen;

    2396:       mat_i = c->i[i];
    2397:       mat_j = c->j + mat_i;
    2398:       mat_a = c->a + mat_i;
    2399:       ilen  = c->ilen[i];
    2400:       PetscSortIntWithMatScalarArray(ilen,mat_j,mat_a);
    2401:     }
    2402:   }
    2403:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
    2404:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);

    2406:   ISRestoreIndices(isrow,&irow);
    2407:   *B   = C;
    2408:   return(0);
    2409: }

    2413: PetscErrorCode  MatGetMultiProcBlock_SeqAIJ(Mat mat,MPI_Comm subComm,MatReuse scall,Mat *subMat)
    2414: {
    2416:   Mat            B;

    2419:   if (scall == MAT_INITIAL_MATRIX) {
    2420:     MatCreate(subComm,&B);
    2421:     MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->n,mat->cmap->n);
    2422:     MatSetBlockSizesFromMats(B,mat,mat);
    2423:     MatSetType(B,MATSEQAIJ);
    2424:     MatDuplicateNoCreate_SeqAIJ(B,mat,MAT_COPY_VALUES,PETSC_TRUE);
    2425:     *subMat = B;
    2426:   } else {
    2427:     MatCopy_SeqAIJ(mat,*subMat,SAME_NONZERO_PATTERN);
    2428:   }
    2429:   return(0);
    2430: }

    2434: PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
    2435: {
    2436:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
    2438:   Mat            outA;
    2439:   PetscBool      row_identity,col_identity;

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

    2444:   ISIdentity(row,&row_identity);
    2445:   ISIdentity(col,&col_identity);

    2447:   outA             = inA;
    2448:   outA->factortype = MAT_FACTOR_LU;
    2449:   PetscFree(inA->solvertype);
    2450:   PetscStrallocpy(MATSOLVERPETSC,&inA->solvertype);

    2452:   PetscObjectReference((PetscObject)row);
    2453:   ISDestroy(&a->row);

    2455:   a->row = row;

    2457:   PetscObjectReference((PetscObject)col);
    2458:   ISDestroy(&a->col);

    2460:   a->col = col;

    2462:   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
    2463:   ISDestroy(&a->icol);
    2464:   ISInvertPermutation(col,PETSC_DECIDE,&a->icol);
    2465:   PetscLogObjectParent((PetscObject)inA,(PetscObject)a->icol);

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

    2472:   MatMarkDiagonal_SeqAIJ(inA);
    2473:   if (row_identity && col_identity) {
    2474:     MatLUFactorNumeric_SeqAIJ_inplace(outA,inA,info);
    2475:   } else {
    2476:     MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);
    2477:   }
    2478:   return(0);
    2479: }

    2483: PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
    2484: {
    2485:   Mat_SeqAIJ     *a     = (Mat_SeqAIJ*)inA->data;
    2486:   PetscScalar    oalpha = alpha;
    2488:   PetscBLASInt   one = 1,bnz;

    2491:   PetscBLASIntCast(a->nz,&bnz);
    2492:   PetscStackCallBLAS("BLASscal",BLASscal_(&bnz,&oalpha,a->a,&one));
    2493:   PetscLogFlops(a->nz);
    2494:   MatSeqAIJInvalidateDiagonal(inA);
    2495:   return(0);
    2496: }

    2500: PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
    2501: {
    2503:   PetscInt       i;

    2506:   if (scall == MAT_INITIAL_MATRIX) {
    2507:     PetscMalloc1(n+1,B);
    2508:   }

    2510:   for (i=0; i<n; i++) {
    2511:     MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);
    2512:   }
    2513:   return(0);
    2514: }

    2518: PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
    2519: {
    2520:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
    2522:   PetscInt       row,i,j,k,l,m,n,*nidx,isz,val;
    2523:   const PetscInt *idx;
    2524:   PetscInt       start,end,*ai,*aj;
    2525:   PetscBT        table;

    2528:   m  = A->rmap->n;
    2529:   ai = a->i;
    2530:   aj = a->j;

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

    2534:   PetscMalloc1(m+1,&nidx);
    2535:   PetscBTCreate(m,&table);

    2537:   for (i=0; i<is_max; i++) {
    2538:     /* Initialize the two local arrays */
    2539:     isz  = 0;
    2540:     PetscBTMemzero(m,table);

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

    2546:     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
    2547:     for (j=0; j<n; ++j) {
    2548:       if (!PetscBTLookupSet(table,idx[j])) nidx[isz++] = idx[j];
    2549:     }
    2550:     ISRestoreIndices(is[i],&idx);
    2551:     ISDestroy(&is[i]);

    2553:     k = 0;
    2554:     for (j=0; j<ov; j++) { /* for each overlap */
    2555:       n = isz;
    2556:       for (; k<n; k++) { /* do only those rows in nidx[k], which are not done yet */
    2557:         row   = nidx[k];
    2558:         start = ai[row];
    2559:         end   = ai[row+1];
    2560:         for (l = start; l<end; l++) {
    2561:           val = aj[l];
    2562:           if (!PetscBTLookupSet(table,val)) nidx[isz++] = val;
    2563:         }
    2564:       }
    2565:     }
    2566:     ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,PETSC_COPY_VALUES,(is+i));
    2567:   }
    2568:   PetscBTDestroy(&table);
    2569:   PetscFree(nidx);
    2570:   return(0);
    2571: }

    2573: /* -------------------------------------------------------------- */
    2576: PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
    2577: {
    2578:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
    2580:   PetscInt       i,nz = 0,m = A->rmap->n,n = A->cmap->n;
    2581:   const PetscInt *row,*col;
    2582:   PetscInt       *cnew,j,*lens;
    2583:   IS             icolp,irowp;
    2584:   PetscInt       *cwork = NULL;
    2585:   PetscScalar    *vwork = NULL;

    2588:   ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);
    2589:   ISGetIndices(irowp,&row);
    2590:   ISInvertPermutation(colp,PETSC_DECIDE,&icolp);
    2591:   ISGetIndices(icolp,&col);

    2593:   /* determine lengths of permuted rows */
    2594:   PetscMalloc1(m+1,&lens);
    2595:   for (i=0; i<m; i++) lens[row[i]] = a->i[i+1] - a->i[i];
    2596:   MatCreate(PetscObjectComm((PetscObject)A),B);
    2597:   MatSetSizes(*B,m,n,m,n);
    2598:   MatSetBlockSizesFromMats(*B,A,A);
    2599:   MatSetType(*B,((PetscObject)A)->type_name);
    2600:   MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);
    2601:   PetscFree(lens);

    2603:   PetscMalloc1(n,&cnew);
    2604:   for (i=0; i<m; i++) {
    2605:     MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);
    2606:     for (j=0; j<nz; j++) cnew[j] = col[cwork[j]];
    2607:     MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);
    2608:     MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);
    2609:   }
    2610:   PetscFree(cnew);

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

    2614:   MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);
    2615:   MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);
    2616:   ISRestoreIndices(irowp,&row);
    2617:   ISRestoreIndices(icolp,&col);
    2618:   ISDestroy(&irowp);
    2619:   ISDestroy(&icolp);
    2620:   return(0);
    2621: }

    2625: PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
    2626: {

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

    2635:     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");
    2636:     PetscMemcpy(b->a,a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));
    2637:   } else {
    2638:     MatCopy_Basic(A,B,str);
    2639:   }
    2640:   return(0);
    2641: }

    2645: PetscErrorCode MatSetUp_SeqAIJ(Mat A)
    2646: {

    2650:    MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);
    2651:   return(0);
    2652: }

    2656: PetscErrorCode MatSeqAIJGetArray_SeqAIJ(Mat A,PetscScalar *array[])
    2657: {
    2658:   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;

    2661:   *array = a->a;
    2662:   return(0);
    2663: }

    2667: PetscErrorCode MatSeqAIJRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
    2668: {
    2670:   return(0);
    2671: }

    2673: /*
    2674:    Computes the number of nonzeros per row needed for preallocation when X and Y
    2675:    have different nonzero structure.
    2676: */
    2679: PetscErrorCode MatAXPYGetPreallocation_SeqX_private(PetscInt m,const PetscInt *xi,const PetscInt *xj,const PetscInt *yi,const PetscInt *yj,PetscInt *nnz)
    2680: {
    2681:   PetscInt       i,j,k,nzx,nzy;

    2684:   /* Set the number of nonzeros in the new matrix */
    2685:   for (i=0; i<m; i++) {
    2686:     const PetscInt *xjj = xj+xi[i],*yjj = yj+yi[i];
    2687:     nzx = xi[i+1] - xi[i];
    2688:     nzy = yi[i+1] - yi[i];
    2689:     nnz[i] = 0;
    2690:     for (j=0,k=0; j<nzx; j++) {                   /* Point in X */
    2691:       for (; k<nzy && yjj[k]<xjj[j]; k++) nnz[i]++; /* Catch up to X */
    2692:       if (k<nzy && yjj[k]==xjj[j]) k++;             /* Skip duplicate */
    2693:       nnz[i]++;
    2694:     }
    2695:     for (; k<nzy; k++) nnz[i]++;
    2696:   }
    2697:   return(0);
    2698: }

    2702: PetscErrorCode MatAXPYGetPreallocation_SeqAIJ(Mat Y,Mat X,PetscInt *nnz)
    2703: {
    2704:   PetscInt       m = Y->rmap->N;
    2705:   Mat_SeqAIJ     *x = (Mat_SeqAIJ*)X->data;
    2706:   Mat_SeqAIJ     *y = (Mat_SeqAIJ*)Y->data;

    2710:   /* Set the number of nonzeros in the new matrix */
    2711:   MatAXPYGetPreallocation_SeqX_private(m,x->i,x->j,y->i,y->j,nnz);
    2712:   return(0);
    2713: }

    2717: PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
    2718: {
    2720:   Mat_SeqAIJ     *x = (Mat_SeqAIJ*)X->data,*y = (Mat_SeqAIJ*)Y->data;
    2721:   PetscBLASInt   one=1,bnz;

    2724:   PetscBLASIntCast(x->nz,&bnz);
    2725:   if (str == SAME_NONZERO_PATTERN) {
    2726:     PetscScalar alpha = a;
    2727:     PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
    2728:     MatSeqAIJInvalidateDiagonal(Y);
    2729:     PetscObjectStateIncrease((PetscObject)Y);
    2730:   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
    2731:     MatAXPY_Basic(Y,a,X,str);
    2732:   } else {
    2733:     Mat      B;
    2734:     PetscInt *nnz;
    2735:     PetscMalloc1(Y->rmap->N,&nnz);
    2736:     MatCreate(PetscObjectComm((PetscObject)Y),&B);
    2737:     PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);
    2738:     MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);
    2739:     MatSetBlockSizesFromMats(B,Y,Y);
    2740:     MatSetType(B,(MatType) ((PetscObject)Y)->type_name);
    2741:     MatAXPYGetPreallocation_SeqAIJ(Y,X,nnz);
    2742:     MatSeqAIJSetPreallocation(B,0,nnz);
    2743:     MatAXPY_BasicWithPreallocation(B,Y,a,X,str);
    2744:     MatHeaderReplace(Y,&B);
    2745:     PetscFree(nnz);
    2746:   }
    2747:   return(0);
    2748: }

    2752: PetscErrorCode  MatConjugate_SeqAIJ(Mat mat)
    2753: {
    2754: #if defined(PETSC_USE_COMPLEX)
    2755:   Mat_SeqAIJ  *aij = (Mat_SeqAIJ*)mat->data;
    2756:   PetscInt    i,nz;
    2757:   PetscScalar *a;

    2760:   nz = aij->nz;
    2761:   a  = aij->a;
    2762:   for (i=0; i<nz; i++) a[i] = PetscConj(a[i]);
    2763: #else
    2765: #endif
    2766:   return(0);
    2767: }

    2771: PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
    2772: {
    2773:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
    2775:   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
    2776:   PetscReal      atmp;
    2777:   PetscScalar    *x;
    2778:   MatScalar      *aa;

    2781:   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
    2782:   aa = a->a;
    2783:   ai = a->i;
    2784:   aj = a->j;

    2786:   VecSet(v,0.0);
    2787:   VecGetArray(v,&x);
    2788:   VecGetLocalSize(v,&n);
    2789:   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
    2790:   for (i=0; i<m; i++) {
    2791:     ncols = ai[1] - ai[0]; ai++;
    2792:     x[i]  = 0.0;
    2793:     for (j=0; j<ncols; j++) {
    2794:       atmp = PetscAbsScalar(*aa);
    2795:       if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
    2796:       aa++; aj++;
    2797:     }
    2798:   }
    2799:   VecRestoreArray(v,&x);
    2800:   return(0);
    2801: }

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

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

    2819:   VecSet(v,0.0);
    2820:   VecGetArray(v,&x);
    2821:   VecGetLocalSize(v,&n);
    2822:   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
    2823:   for (i=0; i<m; i++) {
    2824:     ncols = ai[1] - ai[0]; ai++;
    2825:     if (ncols == A->cmap->n) { /* row is dense */
    2826:       x[i] = *aa; if (idx) idx[i] = 0;
    2827:     } else {  /* row is sparse so already KNOW maximum is 0.0 or higher */
    2828:       x[i] = 0.0;
    2829:       if (idx) {
    2830:         idx[i] = 0; /* in case ncols is zero */
    2831:         for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */
    2832:           if (aj[j] > j) {
    2833:             idx[i] = j;
    2834:             break;
    2835:           }
    2836:         }
    2837:       }
    2838:     }
    2839:     for (j=0; j<ncols; j++) {
    2840:       if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
    2841:       aa++; aj++;
    2842:     }
    2843:   }
    2844:   VecRestoreArray(v,&x);
    2845:   return(0);
    2846: }

    2850: PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
    2851: {
    2852:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
    2854:   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
    2855:   PetscReal      atmp;
    2856:   PetscScalar    *x;
    2857:   MatScalar      *aa;

    2860:   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
    2861:   aa = a->a;
    2862:   ai = a->i;
    2863:   aj = a->j;

    2865:   VecSet(v,0.0);
    2866:   VecGetArray(v,&x);
    2867:   VecGetLocalSize(v,&n);
    2868:   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);
    2869:   for (i=0; i<m; i++) {
    2870:     ncols = ai[1] - ai[0]; ai++;
    2871:     if (ncols) {
    2872:       /* Get first nonzero */
    2873:       for (j = 0; j < ncols; j++) {
    2874:         atmp = PetscAbsScalar(aa[j]);
    2875:         if (atmp > 1.0e-12) {
    2876:           x[i] = atmp;
    2877:           if (idx) idx[i] = aj[j];
    2878:           break;
    2879:         }
    2880:       }
    2881:       if (j == ncols) {x[i] = PetscAbsScalar(*aa); if (idx) idx[i] = *aj;}
    2882:     } else {
    2883:       x[i] = 0.0; if (idx) idx[i] = 0;
    2884:     }
    2885:     for (j = 0; j < ncols; j++) {
    2886:       atmp = PetscAbsScalar(*aa);
    2887:       if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
    2888:       aa++; aj++;
    2889:     }
    2890:   }
    2891:   VecRestoreArray(v,&x);
    2892:   return(0);
    2893: }

    2897: PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[])
    2898: {
    2899:   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
    2900:   PetscErrorCode  ierr;
    2901:   PetscInt        i,j,m = A->rmap->n,ncols,n;
    2902:   const PetscInt  *ai,*aj;
    2903:   PetscScalar     *x;
    2904:   const MatScalar *aa;

    2907:   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
    2908:   aa = a->a;
    2909:   ai = a->i;
    2910:   aj = a->j;

    2912:   VecSet(v,0.0);
    2913:   VecGetArray(v,&x);
    2914:   VecGetLocalSize(v,&n);
    2915:   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
    2916:   for (i=0; i<m; i++) {
    2917:     ncols = ai[1] - ai[0]; ai++;
    2918:     if (ncols == A->cmap->n) { /* row is dense */
    2919:       x[i] = *aa; if (idx) idx[i] = 0;
    2920:     } else {  /* row is sparse so already KNOW minimum is 0.0 or lower */
    2921:       x[i] = 0.0;
    2922:       if (idx) {   /* find first implicit 0.0 in the row */
    2923:         idx[i] = 0; /* in case ncols is zero */
    2924:         for (j=0; j<ncols; j++) {
    2925:           if (aj[j] > j) {
    2926:             idx[i] = j;
    2927:             break;
    2928:           }
    2929:         }
    2930:       }
    2931:     }
    2932:     for (j=0; j<ncols; j++) {
    2933:       if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
    2934:       aa++; aj++;
    2935:     }
    2936:   }
    2937:   VecRestoreArray(v,&x);
    2938:   return(0);
    2939: }

    2941: #include <petscblaslapack.h>
    2942: #include <petsc/private/kernels/blockinvert.h>

    2946: PetscErrorCode MatInvertBlockDiagonal_SeqAIJ(Mat A,const PetscScalar **values)
    2947: {
    2948:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
    2950:   PetscInt       i,bs = PetscAbs(A->rmap->bs),mbs = A->rmap->n/bs,ipvt[5],bs2 = bs*bs,*v_pivots,ij[7],*IJ,j;
    2951:   MatScalar      *diag,work[25],*v_work;
    2952:   PetscReal      shift = 0.0;
    2953:   PetscBool      allowzeropivot,zeropivotdetected=PETSC_FALSE;

    2956:   allowzeropivot = PetscNot(A->erroriffailure);
    2957:   if (a->ibdiagvalid) {
    2958:     if (values) *values = a->ibdiag;
    2959:     return(0);
    2960:   }
    2961:   MatMarkDiagonal_SeqAIJ(A);
    2962:   if (!a->ibdiag) {
    2963:     PetscMalloc1(bs2*mbs,&a->ibdiag);
    2964:     PetscLogObjectMemory((PetscObject)A,bs2*mbs*sizeof(PetscScalar));
    2965:   }
    2966:   diag = a->ibdiag;
    2967:   if (values) *values = a->ibdiag;
    2968:   /* factor and invert each block */
    2969:   switch (bs) {
    2970:   case 1:
    2971:     for (i=0; i<mbs; i++) {
    2972:       MatGetValues(A,1,&i,1,&i,diag+i);
    2973:       if (PetscAbsScalar(diag[i] + shift) < PETSC_MACHINE_EPSILON) {
    2974:         if (allowzeropivot) {
    2975:           A->errortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
    2976:           PetscInfo1(A,"Zero pivot, row %D\n",i);
    2977:         } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_MAT_LU_ZRPVT,"Zero pivot, row %D",i);
    2978:       }
    2979:       diag[i] = (PetscScalar)1.0 / (diag[i] + shift);
    2980:     }
    2981:     break;
    2982:   case 2:
    2983:     for (i=0; i<mbs; i++) {
    2984:       ij[0] = 2*i; ij[1] = 2*i + 1;
    2985:       MatGetValues(A,2,ij,2,ij,diag);
    2986:       PetscKernel_A_gets_inverse_A_2(diag,shift,allowzeropivot,&zeropivotdetected);
    2987:       if (zeropivotdetected) A->errortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
    2988:       PetscKernel_A_gets_transpose_A_2(diag);
    2989:       diag += 4;
    2990:     }
    2991:     break;
    2992:   case 3:
    2993:     for (i=0; i<mbs; i++) {
    2994:       ij[0] = 3*i; ij[1] = 3*i + 1; ij[2] = 3*i + 2;
    2995:       MatGetValues(A,3,ij,3,ij,diag);
    2996:       PetscKernel_A_gets_inverse_A_3(diag,shift,allowzeropivot,&zeropivotdetected);
    2997:       if (zeropivotdetected) A->errortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
    2998:       PetscKernel_A_gets_transpose_A_3(diag);
    2999:       diag += 9;
    3000:     }
    3001:     break;
    3002:   case 4:
    3003:     for (i=0; i<mbs; i++) {
    3004:       ij[0] = 4*i; ij[1] = 4*i + 1; ij[2] = 4*i + 2; ij[3] = 4*i + 3;
    3005:       MatGetValues(A,4,ij,4,ij,diag);
    3006:       PetscKernel_A_gets_inverse_A_4(diag,shift,allowzeropivot,&zeropivotdetected);
    3007:       if (zeropivotdetected) A->errortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
    3008:       PetscKernel_A_gets_transpose_A_4(diag);
    3009:       diag += 16;
    3010:     }
    3011:     break;
    3012:   case 5:
    3013:     for (i=0; i<mbs; i++) {
    3014:       ij[0] = 5*i; ij[1] = 5*i + 1; ij[2] = 5*i + 2; ij[3] = 5*i + 3; ij[4] = 5*i + 4;
    3015:       MatGetValues(A,5,ij,5,ij,diag);
    3016:       PetscKernel_A_gets_inverse_A_5(diag,ipvt,work,shift,allowzeropivot,&zeropivotdetected);
    3017:       if (zeropivotdetected) A->errortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
    3018:       PetscKernel_A_gets_transpose_A_5(diag);
    3019:       diag += 25;
    3020:     }
    3021:     break;
    3022:   case 6:
    3023:     for (i=0; i<mbs; i++) {
    3024:       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;
    3025:       MatGetValues(A,6,ij,6,ij,diag);
    3026:       PetscKernel_A_gets_inverse_A_6(diag,shift,allowzeropivot,&zeropivotdetected);
    3027:       if (zeropivotdetected) A->errortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
    3028:       PetscKernel_A_gets_transpose_A_6(diag);
    3029:       diag += 36;
    3030:     }
    3031:     break;
    3032:   case 7:
    3033:     for (i=0; i<mbs; i++) {
    3034:       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;
    3035:       MatGetValues(A,7,ij,7,ij,diag);
    3036:       PetscKernel_A_gets_inverse_A_7(diag,shift,allowzeropivot,&zeropivotdetected);
    3037:       if (zeropivotdetected) A->errortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
    3038:       PetscKernel_A_gets_transpose_A_7(diag);
    3039:       diag += 49;
    3040:     }
    3041:     break;
    3042:   default:
    3043:     PetscMalloc3(bs,&v_work,bs,&v_pivots,bs,&IJ);
    3044:     for (i=0; i<mbs; i++) {
    3045:       for (j=0; j<bs; j++) {
    3046:         IJ[j] = bs*i + j;
    3047:       }
    3048:       MatGetValues(A,bs,IJ,bs,IJ,diag);
    3049:       PetscKernel_A_gets_inverse_A(bs,diag,v_pivots,v_work,allowzeropivot,&zeropivotdetected);
    3050:       if (zeropivotdetected) A->errortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
    3051:       PetscKernel_A_gets_transpose_A_N(diag,bs);
    3052:       diag += bs2;
    3053:     }
    3054:     PetscFree3(v_work,v_pivots,IJ);
    3055:   }
    3056:   a->ibdiagvalid = PETSC_TRUE;
    3057:   return(0);
    3058: }

    3062: static PetscErrorCode  MatSetRandom_SeqAIJ(Mat x,PetscRandom rctx)
    3063: {
    3065:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)x->data;
    3066:   PetscScalar    a;
    3067:   PetscInt       m,n,i,j,col;

    3070:   if (!x->assembled) {
    3071:     MatGetSize(x,&m,&n);
    3072:     for (i=0; i<m; i++) {
    3073:       for (j=0; j<aij->imax[i]; j++) {
    3074:         PetscRandomGetValue(rctx,&a);
    3075:         col  = (PetscInt)(n*PetscRealPart(a));
    3076:         MatSetValues(x,1,&i,1,&col,&a,ADD_VALUES);
    3077:       }
    3078:     }
    3079:   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not yet coded");
    3080:   MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);
    3081:   MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);
    3082:   return(0);
    3083: }

    3087: PetscErrorCode MatShift_SeqAIJ(Mat Y,PetscScalar a)
    3088: {
    3090:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)Y->data;

    3093:   if (!Y->preallocated || !aij->nz) {
    3094:     MatSeqAIJSetPreallocation(Y,1,NULL);
    3095:   }
    3096:   MatShift_Basic(Y,a);
    3097:   return(0);
    3098: }

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

    3250: PetscErrorCode  MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
    3251: {
    3252:   Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data;
    3253:   PetscInt   i,nz,n;

    3256:   nz = aij->maxnz;
    3257:   n  = mat->rmap->n;
    3258:   for (i=0; i<nz; i++) {
    3259:     aij->j[i] = indices[i];
    3260:   }
    3261:   aij->nz = nz;
    3262:   for (i=0; i<n; i++) {
    3263:     aij->ilen[i] = aij->imax[i];
    3264:   }
    3265:   return(0);
    3266: }

    3270: /*@
    3271:     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
    3272:        in the matrix.

    3274:   Input Parameters:
    3275: +  mat - the SeqAIJ matrix
    3276: -  indices - the column indices

    3278:   Level: advanced

    3280:   Notes:
    3281:     This can be called if you have precomputed the nonzero structure of the
    3282:   matrix and want to provide it to the matrix object to improve the performance
    3283:   of the MatSetValues() operation.

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

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

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

    3292: @*/
    3293: PetscErrorCode  MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
    3294: {

    3300:   PetscUseMethod(mat,"MatSeqAIJSetColumnIndices_C",(Mat,PetscInt*),(mat,indices));
    3301:   return(0);
    3302: }

    3304: /* ----------------------------------------------------------------------------------------*/

    3308: PetscErrorCode  MatStoreValues_SeqAIJ(Mat mat)
    3309: {
    3310:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)mat->data;
    3312:   size_t         nz = aij->i[mat->rmap->n];

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

    3317:   /* allocate space for values if not already there */
    3318:   if (!aij->saved_values) {
    3319:     PetscMalloc1(nz+1,&aij->saved_values);
    3320:     PetscLogObjectMemory((PetscObject)mat,(nz+1)*sizeof(PetscScalar));
    3321:   }

    3323:   /* copy values over */
    3324:   PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));
    3325:   return(0);
    3326: }

    3330: /*@
    3331:     MatStoreValues - Stashes a copy of the matrix values; this allows, for
    3332:        example, reuse of the linear part of a Jacobian, while recomputing the
    3333:        nonlinear portion.

    3335:    Collect on Mat

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

    3340:   Level: advanced

    3342:   Common Usage, with SNESSolve():
    3343: $    Create Jacobian matrix
    3344: $    Set linear terms into matrix
    3345: $    Apply boundary conditions to matrix, at this time matrix must have
    3346: $      final nonzero structure (i.e. setting the nonlinear terms and applying
    3347: $      boundary conditions again will not change the nonzero structure
    3348: $    MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
    3349: $    MatStoreValues(mat);
    3350: $    Call SNESSetJacobian() with matrix
    3351: $    In your Jacobian routine
    3352: $      MatRetrieveValues(mat);
    3353: $      Set nonlinear terms in matrix

    3355:   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
    3356: $    // build linear portion of Jacobian
    3357: $    MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
    3358: $    MatStoreValues(mat);
    3359: $    loop over nonlinear iterations
    3360: $       MatRetrieveValues(mat);
    3361: $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
    3362: $       // call MatAssemblyBegin/End() on matrix
    3363: $       Solve linear system with Jacobian
    3364: $    endloop

    3366:   Notes:
    3367:     Matrix must already be assemblied before calling this routine
    3368:     Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
    3369:     calling this routine.

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

    3374: .seealso: MatRetrieveValues()

    3376: @*/
    3377: PetscErrorCode  MatStoreValues(Mat mat)
    3378: {

    3383:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
    3384:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
    3385:   PetscUseMethod(mat,"MatStoreValues_C",(Mat),(mat));
    3386:   return(0);
    3387: }

    3391: PetscErrorCode  MatRetrieveValues_SeqAIJ(Mat mat)
    3392: {
    3393:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)mat->data;
    3395:   PetscInt       nz = aij->i[mat->rmap->n];

    3398:   if (!aij->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
    3399:   if (!aij->saved_values) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
    3400:   /* copy values over */
    3401:   PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));
    3402:   return(0);
    3403: }

    3407: /*@
    3408:     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
    3409:        example, reuse of the linear part of a Jacobian, while recomputing the
    3410:        nonlinear portion.

    3412:    Collect on Mat

    3414:   Input Parameters:
    3415: .  mat - the matrix (currently on AIJ matrices support this option)

    3417:   Level: advanced

    3419: .seealso: MatStoreValues()

    3421: @*/
    3422: PetscErrorCode  MatRetrieveValues(Mat mat)
    3423: {

    3428:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
    3429:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
    3430:   PetscUseMethod(mat,"MatRetrieveValues_C",(Mat),(mat));
    3431:   return(0);
    3432: }

    3435: /* --------------------------------------------------------------------------------*/
    3438: /*@C
    3439:    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
    3440:    (the default parallel PETSc format).  For good matrix assembly performance
    3441:    the user should preallocate the matrix storage by setting the parameter nz
    3442:    (or the array nnz).  By setting these parameters accurately, performance
    3443:    during matrix assembly can be increased by more than a factor of 50.

    3445:    Collective on MPI_Comm

    3447:    Input Parameters:
    3448: +  comm - MPI communicator, set to PETSC_COMM_SELF
    3449: .  m - number of rows
    3450: .  n - number of columns
    3451: .  nz - number of nonzeros per row (same for all rows)
    3452: -  nnz - array containing the number of nonzeros in the various rows
    3453:          (possibly different for each row) or NULL

    3455:    Output Parameter:
    3456: .  A - the matrix

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

    3462:    Notes:
    3463:    If nnz is given then nz is ignored

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

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

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

    3480:    Options Database Keys:
    3481: +  -mat_no_inode  - Do not use inodes
    3482: -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)

    3484:    Level: intermediate

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

    3488: @*/
    3489: PetscErrorCode  MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
    3490: {

    3494:   MatCreate(comm,A);
    3495:   MatSetSizes(*A,m,n,m,n);
    3496:   MatSetType(*A,MATSEQAIJ);
    3497:   MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);
    3498:   return(0);
    3499: }

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

    3509:    Collective on MPI_Comm

    3511:    Input Parameters:
    3512: +  B - The matrix
    3513: .  nz - number of nonzeros per row (same for all rows)
    3514: -  nnz - array containing the number of nonzeros in the various rows
    3515:          (possibly different for each row) or NULL

    3517:    Notes:
    3518:      If nnz is given then nz is ignored

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

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

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

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

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

    3543:    Options Database Keys:
    3544: +  -mat_no_inode  - Do not use inodes
    3545: .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
    3546: -  -mat_aij_oneindex - Internally use indexing starting at 1
    3547:         rather than 0.  Note that when calling MatSetValues(),
    3548:         the user still MUST index entries starting at 0!

    3550:    Level: intermediate

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

    3554: @*/
    3555: PetscErrorCode  MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
    3556: {

    3562:   PetscTryMethod(B,"MatSeqAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[]),(B,nz,nnz));
    3563:   return(0);
    3564: }

    3568: PetscErrorCode  MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,const PetscInt *nnz)
    3569: {
    3570:   Mat_SeqAIJ     *b;
    3571:   PetscBool      skipallocation = PETSC_FALSE,realalloc = PETSC_FALSE;
    3573:   PetscInt       i;

    3576:   if (nz >= 0 || nnz) realalloc = PETSC_TRUE;
    3577:   if (nz == MAT_SKIP_ALLOCATION) {
    3578:     skipallocation = PETSC_TRUE;
    3579:     nz             = 0;
    3580:   }

    3582:   PetscLayoutSetUp(B->rmap);
    3583:   PetscLayoutSetUp(B->cmap);

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

    3594:   B->preallocated = PETSC_TRUE;

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

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

    3615:     /* allocate the matrix space */
    3616:     /* FIXME: should B's old memory be unlogged? */
    3617:     MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);
    3618:     PetscMalloc3(nz,&b->a,nz,&b->j,B->rmap->n+1,&b->i);
    3619:     PetscLogObjectMemory((PetscObject)B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));
    3620:     b->i[0] = 0;
    3621:     for (i=1; i<B->rmap->n+1; i++) {
    3622:       b->i[i] = b->i[i-1] + b->imax[i-1];
    3623:     }
    3624:     b->singlemalloc = PETSC_TRUE;
    3625:     b->free_a       = PETSC_TRUE;
    3626:     b->free_ij      = PETSC_TRUE;
    3627:   } else {
    3628:     b->free_a  = PETSC_FALSE;
    3629:     b->free_ij = PETSC_FALSE;
    3630:   }

    3632:   b->nz               = 0;
    3633:   b->maxnz            = nz;
    3634:   B->info.nz_unneeded = (double)b->maxnz;
    3635:   if (realalloc) {
    3636:     MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);
    3637:   }
    3638:   return(0);
    3639: }

    3641: #undef  __FUNCT__
    3643: /*@
    3644:    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.

    3646:    Input Parameters:
    3647: +  B - the matrix
    3648: .  i - the indices into j for the start of each row (starts with zero)
    3649: .  j - the column indices for each row (starts with zero) these must be sorted for each row
    3650: -  v - optional values in the matrix

    3652:    Level: developer

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

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

    3658: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ
    3659: @*/
    3660: PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
    3661: {

    3667:   PetscTryMethod(B,"MatSeqAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));
    3668:   return(0);
    3669: }

    3671: #undef  __FUNCT__
    3673: PetscErrorCode  MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
    3674: {
    3675:   PetscInt       i;
    3676:   PetscInt       m,n;
    3677:   PetscInt       nz;
    3678:   PetscInt       *nnz, nz_max = 0;
    3679:   PetscScalar    *values;

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

    3685:   PetscLayoutSetUp(B->rmap);
    3686:   PetscLayoutSetUp(B->cmap);

    3688:   MatGetSize(B, &m, &n);
    3689:   PetscMalloc1(m+1, &nnz);
    3690:   for (i = 0; i < m; i++) {
    3691:     nz     = Ii[i+1]- Ii[i];
    3692:     nz_max = PetscMax(nz_max, nz);
    3693:     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
    3694:     nnz[i] = nz;
    3695:   }
    3696:   MatSeqAIJSetPreallocation(B, 0, nnz);
    3697:   PetscFree(nnz);

    3699:   if (v) {
    3700:     values = (PetscScalar*) v;
    3701:   } else {
    3702:     PetscCalloc1(nz_max, &values);
    3703:   }

    3705:   for (i = 0; i < m; i++) {
    3706:     nz   = Ii[i+1] - Ii[i];
    3707:     MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);
    3708:   }

    3710:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
    3711:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);

    3713:   if (!v) {
    3714:     PetscFree(values);
    3715:   }
    3716:   MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
    3717:   return(0);
    3718: }

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

    3725: /*
    3726:     Computes (B'*A')' since computing B*A directly is untenable

    3728:                n                       p                          p
    3729:         (              )       (              )         (                  )
    3730:       m (      A       )  *  n (       B      )   =   m (         C        )
    3731:         (              )       (              )         (                  )

    3733: */
    3734: PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
    3735: {
    3736:   PetscErrorCode    ierr;
    3737:   Mat_SeqDense      *sub_a = (Mat_SeqDense*)A->data;
    3738:   Mat_SeqAIJ        *sub_b = (Mat_SeqAIJ*)B->data;
    3739:   Mat_SeqDense      *sub_c = (Mat_SeqDense*)C->data;
    3740:   PetscInt          i,n,m,q,p;
    3741:   const PetscInt    *ii,*idx;
    3742:   const PetscScalar *b,*a,*a_q;
    3743:   PetscScalar       *c,*c_q;

    3746:   m    = A->rmap->n;
    3747:   n    = A->cmap->n;
    3748:   p    = B->cmap->n;
    3749:   a    = sub_a->v;
    3750:   b    = sub_b->a;
    3751:   c    = sub_c->v;
    3752:   PetscMemzero(c,m*p*sizeof(PetscScalar));

    3754:   ii  = sub_b->i;
    3755:   idx = sub_b->j;
    3756:   for (i=0; i<n; i++) {
    3757:     q = ii[i+1] - ii[i];
    3758:     while (q-->0) {
    3759:       c_q = c + m*(*idx);
    3760:       a_q = a + m*i;
    3761:       PetscKernelAXPY(c_q,*b,a_q,m);
    3762:       idx++;
    3763:       b++;
    3764:     }
    3765:   }
    3766:   return(0);
    3767: }

    3771: PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
    3772: {
    3774:   PetscInt       m=A->rmap->n,n=B->cmap->n;
    3775:   Mat            Cmat;

    3778:   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);
    3779:   MatCreate(PetscObjectComm((PetscObject)A),&Cmat);
    3780:   MatSetSizes(Cmat,m,n,m,n);
    3781:   MatSetBlockSizesFromMats(Cmat,A,B);
    3782:   MatSetType(Cmat,MATSEQDENSE);
    3783:   MatSeqDenseSetPreallocation(Cmat,NULL);

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

    3787:   *C = Cmat;
    3788:   return(0);
    3789: }

    3791: /* ----------------------------------------------------------------*/
    3794: PETSC_INTERN PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
    3795: {

    3799:   if (scall == MAT_INITIAL_MATRIX) {
    3800:     PetscLogEventBegin(MAT_MatMultSymbolic,A,B,0,0);
    3801:     MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);
    3802:     PetscLogEventEnd(MAT_MatMultSymbolic,A,B,0,0);
    3803:   }
    3804:   PetscLogEventBegin(MAT_MatMultNumeric,A,B,0,0);
    3805:   MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);
    3806:   PetscLogEventEnd(MAT_MatMultNumeric,A,B,0,0);
    3807:   return(0);
    3808: }

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

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

    3818:   Level: beginner

    3820: .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
    3821: M*/

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

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

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

    3835:   Developer Notes: Subclasses include MATAIJCUSP, MATAIJPERM, MATAIJCRL, and also automatically switches over to use inodes when
    3836:    enough exist.

    3838:   Level: beginner

    3840: .seealso: MatCreateAIJ(), MatCreateSeqAIJ(), MATSEQAIJ,MATMPIAIJ
    3841: M*/

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

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

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

    3855:   Level: beginner

    3857: .seealso: MatCreateMPIAIJCRL,MATSEQAIJCRL,MATMPIAIJCRL, MATSEQAIJCRL, MATMPIAIJCRL
    3858: M*/

    3860: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCRL(Mat,MatType,MatReuse,Mat*);
    3861: #if defined(PETSC_HAVE_ELEMENTAL)
    3862: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_Elemental(Mat,MatType,MatReuse,Mat*);
    3863: #endif
    3864: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqDense(Mat,MatType,MatReuse,Mat*);

    3866: #if defined(PETSC_HAVE_MATLAB_ENGINE)
    3867: PETSC_EXTERN PetscErrorCode  MatlabEnginePut_SeqAIJ(PetscObject,void*);
    3868: PETSC_EXTERN PetscErrorCode  MatlabEngineGet_SeqAIJ(PetscObject,void*);
    3869: #endif

    3874: /*@C
    3875:    MatSeqAIJGetArray - gives access to the array where the data for a SeqSeqAIJ matrix is stored

    3877:    Not Collective

    3879:    Input Parameter:
    3880: .  mat - a MATSEQAIJ matrix

    3882:    Output Parameter:
    3883: .   array - pointer to the data

    3885:    Level: intermediate

    3887: .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90()
    3888: @*/
    3889: PetscErrorCode  MatSeqAIJGetArray(Mat A,PetscScalar **array)
    3890: {

    3894:   PetscUseMethod(A,"MatSeqAIJGetArray_C",(Mat,PetscScalar**),(A,array));
    3895:   return(0);
    3896: }

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

    3903:    Not Collective

    3905:    Input Parameter:
    3906: .  mat - a MATSEQAIJ matrix

    3908:    Output Parameter:
    3909: .   nz - the maximum number of nonzeros in any row

    3911:    Level: intermediate

    3913: .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90()
    3914: @*/
    3915: PetscErrorCode  MatSeqAIJGetMaxRowNonzeros(Mat A,PetscInt *nz)
    3916: {
    3917:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)A->data;

    3920:   *nz = aij->rmax;
    3921:   return(0);
    3922: }

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

    3929:    Not Collective

    3931:    Input Parameters:
    3932: .  mat - a MATSEQAIJ matrix
    3933: .  array - pointer to the data

    3935:    Level: intermediate

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

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

    3950: PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJ(Mat B)
    3951: {
    3952:   Mat_SeqAIJ     *b;
    3954:   PetscMPIInt    size;

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

    3960:   PetscNewLog(B,&b);

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

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

    3966:   b->row                = 0;
    3967:   b->col                = 0;
    3968:   b->icol               = 0;
    3969:   b->reallocs           = 0;
    3970:   b->ignorezeroentries  = PETSC_FALSE;
    3971:   b->roworiented        = PETSC_TRUE;
    3972:   b->nonew              = 0;
    3973:   b->diag               = 0;
    3974:   b->solve_work         = 0;
    3975:   B->spptr              = 0;
    3976:   b->saved_values       = 0;
    3977:   b->idiag              = 0;
    3978:   b->mdiag              = 0;
    3979:   b->ssor_work          = 0;
    3980:   b->omega              = 1.0;
    3981:   b->fshift             = 0.0;
    3982:   b->idiagvalid         = PETSC_FALSE;
    3983:   b->ibdiagvalid        = PETSC_FALSE;
    3984:   b->keepnonzeropattern = PETSC_FALSE;

    3986:   PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);
    3987:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJGetArray_C",MatSeqAIJGetArray_SeqAIJ);
    3988:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJRestoreArray_C",MatSeqAIJRestoreArray_SeqAIJ);

    3990: #if defined(PETSC_HAVE_MATLAB_ENGINE)
    3991:   PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEnginePut_C",MatlabEnginePut_SeqAIJ);
    3992:   PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEngineGet_C",MatlabEngineGet_SeqAIJ);
    3993: #endif

    3995:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetColumnIndices_C",MatSeqAIJSetColumnIndices_SeqAIJ);
    3996:   PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_SeqAIJ);
    3997:   PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_SeqAIJ);
    3998:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",MatConvert_SeqAIJ_SeqSBAIJ);
    3999:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqbaij_C",MatConvert_SeqAIJ_SeqBAIJ);
    4000:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijperm_C",MatConvert_SeqAIJ_SeqAIJPERM);
    4001:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijcrl_C",MatConvert_SeqAIJ_SeqAIJCRL);
    4002: #if defined(PETSC_HAVE_ELEMENTAL)
    4003:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_elemental_C",MatConvert_SeqAIJ_Elemental);
    4004: #endif
    4005:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqdense_C",MatConvert_SeqAIJ_SeqDense);
    4006:   PetscObjectComposeFunction((PetscObject)B,"MatIsTranspose_C",MatIsTranspose_SeqAIJ);
    4007:   PetscObjectComposeFunction((PetscObject)B,"MatIsHermitianTranspose_C",MatIsTranspose_SeqAIJ);
    4008:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",MatSeqAIJSetPreallocation_SeqAIJ);
    4009:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",MatSeqAIJSetPreallocationCSR_SeqAIJ);
    4010:   PetscObjectComposeFunction((PetscObject)B,"MatReorderForNonzeroDiagonal_C",MatReorderForNonzeroDiagonal_SeqAIJ);
    4011:   PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqdense_seqaij_C",MatMatMult_SeqDense_SeqAIJ);
    4012:   PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C",MatMatMultSymbolic_SeqDense_SeqAIJ);
    4013:   PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C",MatMatMultNumeric_SeqDense_SeqAIJ);
    4014:   MatCreate_SeqAIJ_Inode(B);
    4015:   PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);
    4016:   return(0);
    4017: }

    4021: /*
    4022:     Given a matrix generated with MatGetFactor() duplicates all the information in A into B
    4023: */
    4024: PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscBool mallocmatspace)
    4025: {
    4026:   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
    4028:   PetscInt       i,m = A->rmap->n;

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

    4033:   C->factortype = A->factortype;
    4034:   c->row        = 0;
    4035:   c->col        = 0;
    4036:   c->icol       = 0;
    4037:   c->reallocs   = 0;

    4039:   C->assembled = PETSC_TRUE;

    4041:   PetscLayoutReference(A->rmap,&C->rmap);
    4042:   PetscLayoutReference(A->cmap,&C->cmap);

    4044:   PetscMalloc2(m,&c->imax,m,&c->ilen);
    4045:   PetscLogObjectMemory((PetscObject)C, 2*m*sizeof(PetscInt));
    4046:   for (i=0; i<m; i++) {
    4047:     c->imax[i] = a->imax[i];
    4048:     c->ilen[i] = a->ilen[i];
    4049:   }

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

    4056:     c->singlemalloc = PETSC_TRUE;

    4058:     PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));
    4059:     if (m > 0) {
    4060:       PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));
    4061:       if (cpvalues == MAT_COPY_VALUES) {
    4062:         PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));
    4063:       } else {
    4064:         PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));
    4065:       }
    4066:     }
    4067:   }

    4069:   c->ignorezeroentries = a->ignorezeroentries;
    4070:   c->roworiented       = a->roworiented;
    4071:   c->nonew             = a->nonew;
    4072:   if (a->diag) {
    4073:     PetscMalloc1(m+1,&c->diag);
    4074:     PetscLogObjectMemory((PetscObject)C,(m+1)*sizeof(PetscInt));
    4075:     for (i=0; i<m; i++) {
    4076:       c->diag[i] = a->diag[i];
    4077:     }
    4078:   } else c->diag = 0;

    4080:   c->solve_work         = 0;
    4081:   c->saved_values       = 0;
    4082:   c->idiag              = 0;
    4083:   c->ssor_work          = 0;
    4084:   c->keepnonzeropattern = a->keepnonzeropattern;
    4085:   c->free_a             = PETSC_TRUE;
    4086:   c->free_ij            = PETSC_TRUE;

    4088:   c->rmax         = a->rmax;
    4089:   c->nz           = a->nz;
    4090:   c->maxnz        = a->nz;       /* Since we allocate exactly the right amount */
    4091:   C->preallocated = PETSC_TRUE;

    4093:   c->compressedrow.use   = a->compressedrow.use;
    4094:   c->compressedrow.nrows = a->compressedrow.nrows;
    4095:   if (a->compressedrow.use) {
    4096:     i    = a->compressedrow.nrows;
    4097:     PetscMalloc2(i+1,&c->compressedrow.i,i,&c->compressedrow.rindex);
    4098:     PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));
    4099:     PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));
    4100:   } else {
    4101:     c->compressedrow.use    = PETSC_FALSE;
    4102:     c->compressedrow.i      = NULL;
    4103:     c->compressedrow.rindex = NULL;
    4104:   }
    4105:   c->nonzerorowcnt = a->nonzerorowcnt;
    4106:   C->nonzerostate  = A->nonzerostate;

    4108:   MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);
    4109:   PetscFunctionListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);
    4110:   return(0);
    4111: }

    4115: PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
    4116: {

    4120:   MatCreate(PetscObjectComm((PetscObject)A),B);
    4121:   MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);
    4122:   if (!(A->rmap->n % A->rmap->bs) && !(A->cmap->n % A->cmap->bs)) {
    4123:     MatSetBlockSizesFromMats(*B,A,A);
    4124:   }
    4125:   MatSetType(*B,((PetscObject)A)->type_name);
    4126:   MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);
    4127:   return(0);
    4128: }

    4132: PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer)
    4133: {
    4134:   Mat_SeqAIJ     *a;
    4136:   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N,rows,cols;
    4137:   int            fd;
    4138:   PetscMPIInt    size;
    4139:   MPI_Comm       comm;
    4140:   PetscInt       bs = newMat->rmap->bs;

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

    4149:   PetscOptionsBegin(comm,NULL,"Options for loading SEQAIJ matrix","Mat");
    4150:   PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,NULL);
    4151:   PetscOptionsEnd();
    4152:   if (bs < 0) bs = 1;
    4153:   MatSetBlockSize(newMat,bs);

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

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

    4162:   /* read in row lengths */
    4163:   PetscMalloc1(M,&rowlengths);
    4164:   PetscBinaryRead(fd,rowlengths,M,PETSC_INT);

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

    4170:   /* set global size if not set already*/
    4171:   if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) {
    4172:     MatSetSizes(newMat,PETSC_DECIDE,PETSC_DECIDE,M,N);
    4173:   } else {
    4174:     /* if sizes and type are already set, check if the matrix  global sizes are correct */
    4175:     MatGetSize(newMat,&rows,&cols);
    4176:     if (rows < 0 && cols < 0) { /* user might provide local size instead of global size */
    4177:       MatGetLocalSize(newMat,&rows,&cols);
    4178:     }
    4179:     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);
    4180:   }
    4181:   MatSeqAIJSetPreallocation_SeqAIJ(newMat,0,rowlengths);
    4182:   a    = (Mat_SeqAIJ*)newMat->data;

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

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

    4189:   /* set matrix "i" values */
    4190:   a->i[0] = 0;
    4191:   for (i=1; i<= M; i++) {
    4192:     a->i[i]      = a->i[i-1] + rowlengths[i-1];
    4193:     a->ilen[i-1] = rowlengths[i-1];
    4194:   }
    4195:   PetscFree(rowlengths);

    4197:   MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);
    4198:   MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);
    4199:   return(0);
    4200: }

    4204: PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscBool * flg)
    4205: {
    4206:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*b = (Mat_SeqAIJ*)B->data;
    4208: #if defined(PETSC_USE_COMPLEX)
    4209:   PetscInt k;
    4210: #endif

    4213:   /* If the  matrix dimensions are not equal,or no of nonzeros */
    4214:   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
    4215:     *flg = PETSC_FALSE;
    4216:     return(0);
    4217:   }

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

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

    4227:   /* if a->a are the same */
    4228: #if defined(PETSC_USE_COMPLEX)
    4229:   for (k=0; k<a->nz; k++) {
    4230:     if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])) {
    4231:       *flg = PETSC_FALSE;
    4232:       return(0);
    4233:     }
    4234:   }
    4235: #else
    4236:   PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);
    4237: #endif
    4238:   return(0);
    4239: }

    4243: /*@
    4244:      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
    4245:               provided by the user.

    4247:       Collective on MPI_Comm

    4249:    Input Parameters:
    4250: +   comm - must be an MPI communicator of size 1
    4251: .   m - number of rows
    4252: .   n - number of columns
    4253: .   i - row indices
    4254: .   j - column indices
    4255: -   a - matrix values

    4257:    Output Parameter:
    4258: .   mat - the matrix

    4260:    Level: intermediate

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

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

    4268:        The i and j indices are 0 based

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

    4274: $        1 0 0
    4275: $        2 0 3
    4276: $        4 5 6
    4277: $
    4278: $        i =  {0,1,3,6}  [size = nrow+1  = 3+1]
    4279: $        j =  {0,0,2,0,1,2}  [size = 6]; values must be sorted for each row
    4280: $        v =  {1,2,3,4,5,6}  [size = 6]

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

    4285: @*/
    4286: PetscErrorCode  MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt *i,PetscInt *j,PetscScalar *a,Mat *mat)
    4287: {
    4289:   PetscInt       ii;
    4290:   Mat_SeqAIJ     *aij;
    4291: #if defined(PETSC_USE_DEBUG)
    4292:   PetscInt jj;
    4293: #endif

    4296:   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
    4297:   MatCreate(comm,mat);
    4298:   MatSetSizes(*mat,m,n,m,n);
    4299:   /* MatSetBlockSizes(*mat,,); */
    4300:   MatSetType(*mat,MATSEQAIJ);
    4301:   MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);
    4302:   aij  = (Mat_SeqAIJ*)(*mat)->data;
    4303:   PetscMalloc2(m,&aij->imax,m,&aij->ilen);

    4305:   aij->i            = i;
    4306:   aij->j            = j;
    4307:   aij->a            = a;
    4308:   aij->singlemalloc = PETSC_FALSE;
    4309:   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
    4310:   aij->free_a       = PETSC_FALSE;
    4311:   aij->free_ij      = PETSC_FALSE;

    4313:   for (ii=0; ii<m; ii++) {
    4314:     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
    4315: #if defined(PETSC_USE_DEBUG)
    4316:     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]);
    4317:     for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
    4318:       if (j[jj] < j[jj-1]) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column entry number %D (actual colum %D) in row %D is not sorted",jj-i[ii],j[jj],ii);
    4319:       if (j[jj] == j[jj]-1) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column entry number %D (actual colum %D) in row %D is identical to previous entry",jj-i[ii],j[jj],ii);
    4320:     }
    4321: #endif
    4322:   }
    4323: #if defined(PETSC_USE_DEBUG)
    4324:   for (ii=0; ii<aij->i[m]; ii++) {
    4325:     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %D index = %D",ii,j[ii]);
    4326:     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]);
    4327:   }
    4328: #endif

    4330:   MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);
    4331:   MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);
    4332:   return(0);
    4333: }
    4336: /*@C
    4337:      MatCreateSeqAIJFromTriple - Creates an sequential AIJ matrix using matrix elements (in COO format)
    4338:               provided by the user.

    4340:       Collective on MPI_Comm

    4342:    Input Parameters:
    4343: +   comm - must be an MPI communicator of size 1
    4344: .   m   - number of rows
    4345: .   n   - number of columns
    4346: .   i   - row indices
    4347: .   j   - column indices
    4348: .   a   - matrix values
    4349: .   nz  - number of nonzeros
    4350: -   idx - 0 or 1 based

    4352:    Output Parameter:
    4353: .   mat - the matrix

    4355:    Level: intermediate

    4357:    Notes:
    4358:        The i and j indices are 0 based

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

    4364:         1 0 0
    4365:         2 0 3
    4366:         4 5 6

    4368:         i =  {0,1,1,2,2,2}
    4369:         j =  {0,0,2,0,1,2}
    4370:         v =  {1,2,3,4,5,6}

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

    4375: @*/
    4376: PetscErrorCode  MatCreateSeqAIJFromTriple(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt *i,PetscInt *j,PetscScalar *a,Mat *mat,PetscInt nz,PetscBool idx)
    4377: {
    4379:   PetscInt       ii, *nnz, one = 1,row,col;

    4383:   PetscCalloc1(m,&nnz);
    4384:   for (ii = 0; ii < nz; ii++) {
    4385:     nnz[i[ii] - !!idx] += 1;
    4386:   }
    4387:   MatCreate(comm,mat);
    4388:   MatSetSizes(*mat,m,n,m,n);
    4389:   MatSetType(*mat,MATSEQAIJ);
    4390:   MatSeqAIJSetPreallocation_SeqAIJ(*mat,0,nnz);
    4391:   for (ii = 0; ii < nz; ii++) {
    4392:     if (idx) {
    4393:       row = i[ii] - 1;
    4394:       col = j[ii] - 1;
    4395:     } else {
    4396:       row = i[ii];
    4397:       col = j[ii];
    4398:     }
    4399:     MatSetValues(*mat,one,&row,one,&col,&a[ii],ADD_VALUES);
    4400:   }
    4401:   MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);
    4402:   MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);
    4403:   PetscFree(nnz);
    4404:   return(0);
    4405: }

    4409: PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
    4410: {
    4412:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;

    4415:   if (coloring->ctype == IS_COLORING_GLOBAL) {
    4416:     ISColoringReference(coloring);
    4417:     a->coloring = coloring;
    4418:   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
    4419:     PetscInt        i,*larray;
    4420:     ISColoring      ocoloring;
    4421:     ISColoringValue *colors;

    4423:     /* set coloring for diagonal portion */
    4424:     PetscMalloc1(A->cmap->n,&larray);
    4425:     for (i=0; i<A->cmap->n; i++) larray[i] = i;
    4426:     ISGlobalToLocalMappingApply(A->cmap->mapping,IS_GTOLM_MASK,A->cmap->n,larray,NULL,larray);
    4427:     PetscMalloc1(A->cmap->n,&colors);
    4428:     for (i=0; i<A->cmap->n; i++) colors[i] = coloring->colors[larray[i]];
    4429:     PetscFree(larray);
    4430:     ISColoringCreate(PETSC_COMM_SELF,coloring->n,A->cmap->n,colors,PETSC_OWN_POINTER,&ocoloring);
    4431:     a->coloring = ocoloring;
    4432:   }
    4433:   return(0);
    4434: }

    4438: PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues)
    4439: {
    4440:   Mat_SeqAIJ      *a      = (Mat_SeqAIJ*)A->data;
    4441:   PetscInt        m       = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j;
    4442:   MatScalar       *v      = a->a;
    4443:   PetscScalar     *values = (PetscScalar*)advalues;
    4444:   ISColoringValue *color;

    4447:   if (!a->coloring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
    4448:   color = a->coloring->colors;
    4449:   /* loop over rows */
    4450:   for (i=0; i<m; i++) {
    4451:     nz = ii[i+1] - ii[i];
    4452:     /* loop over columns putting computed value into matrix */
    4453:     for (j=0; j<nz; j++) *v++ = values[color[*jj++]];
    4454:     values += nl; /* jump to next row of derivatives */
    4455:   }
    4456:   return(0);
    4457: }

    4461: PetscErrorCode MatSeqAIJInvalidateDiagonal(Mat A)
    4462: {
    4463:   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data;

    4467:   a->idiagvalid  = PETSC_FALSE;
    4468:   a->ibdiagvalid = PETSC_FALSE;

    4470:   MatSeqAIJInvalidateDiagonal_Inode(A);
    4471:   return(0);
    4472: }

    4476: PetscErrorCode MatCreateMPIMatConcatenateSeqMat_SeqAIJ(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat)
    4477: {

    4481:   MatCreateMPIMatConcatenateSeqMat_MPIAIJ(comm,inmat,n,scall,outmat);
    4482:   return(0);
    4483: }

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

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

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

    4564: /*
    4565:     Special version for direct calls from Fortran
    4566: */
    4567: #include <petsc/private/fortranimpl.h>
    4568: #if defined(PETSC_HAVE_FORTRAN_CAPS)
    4569: #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
    4570: #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
    4571: #define matsetvaluesseqaij_ matsetvaluesseqaij
    4572: #endif

    4574: /* Change these macros so can be used in void function */
    4575: #undef CHKERRQ
    4576: #define CHKERRQ(ierr) CHKERRABORT(PetscObjectComm((PetscObject)A),ierr)
    4577: #undef SETERRQ2
    4578: #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
    4579: #undef SETERRQ3
    4580: #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr)

    4584: 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)
    4585: {
    4586:   Mat            A  = *AA;
    4587:   PetscInt       m  = *mm, n = *nn;
    4588:   InsertMode     is = *isis;
    4589:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
    4590:   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
    4591:   PetscInt       *imax,*ai,*ailen;
    4593:   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
    4594:   MatScalar      *ap,value,*aa;
    4595:   PetscBool      ignorezeroentries = a->ignorezeroentries;
    4596:   PetscBool      roworiented       = a->roworiented;

    4599:   MatCheckPreallocated(A,1);
    4600:   imax  = a->imax;
    4601:   ai    = a->i;
    4602:   ailen = a->ilen;
    4603:   aj    = a->j;
    4604:   aa    = a->a;

    4606:   for (k=0; k<m; k++) { /* loop over added rows */
    4607:     row = im[k];
    4608:     if (row < 0) continue;
    4609: #if defined(PETSC_USE_DEBUG)
    4610:     if (row >= A->rmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
    4611: #endif
    4612:     rp   = aj + ai[row]; ap = aa + ai[row];
    4613:     rmax = imax[row]; nrow = ailen[row];
    4614:     low  = 0;
    4615:     high = nrow;
    4616:     for (l=0; l<n; l++) { /* loop over added columns */
    4617:       if (in[l] < 0) continue;
    4618: #if defined(PETSC_USE_DEBUG)
    4619:       if (in[l] >= A->cmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
    4620: #endif
    4621:       col = in[l];
    4622:       if (roworiented) value = v[l + k*n];
    4623:       else value = v[k + l*m];

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

    4627:       if (col <= lastcol) low = 0;
    4628:       else high = nrow;
    4629:       lastcol = col;
    4630:       while (high-low > 5) {
    4631:         t = (low+high)/2;
    4632:         if (rp[t] > col) high = t;
    4633:         else             low  = t;
    4634:       }
    4635:       for (i=low; i<high; i++) {
    4636:         if (rp[i] > col) break;
    4637:         if (rp[i] == col) {
    4638:           if (is == ADD_VALUES) ap[i] += value;
    4639:           else                  ap[i] = value;
    4640:           goto noinsert;
    4641:         }
    4642:       }
    4643:       if (value == 0.0 && ignorezeroentries) goto noinsert;
    4644:       if (nonew == 1) goto noinsert;
    4645:       if (nonew == -1) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
    4646:       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
    4647:       N = nrow++ - 1; a->nz++; high++;
    4648:       /* shift up all the later entries in this row */
    4649:       for (ii=N; ii>=i; ii--) {
    4650:         rp[ii+1] = rp[ii];
    4651:         ap[ii+1] = ap[ii];
    4652:       }
    4653:       rp[i] = col;
    4654:       ap[i] = value;
    4655:       A->nonzerostate++;
    4656: noinsert:;
    4657:       low = i + 1;
    4658:     }
    4659:     ailen[row] = nrow;
    4660:   }
    4661:   PetscFunctionReturnVoid();
    4662: }