Actual source code: aij.c

petsc-master 2014-10-23
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>
 12: #if defined(PETSC_THREADCOMM_ACTIVE)
 13: #include <petscthreadcomm.h>
 14: #endif

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

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

 41:   if (type == NORM_2) {
 42:     for (i=0; i<n; i++) norms[i] = PetscSqrtReal(norms[i]);
 43:   }
 44:   return(0);
 45: }

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

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

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

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

108: PetscErrorCode MatFindZeroDiagonals_SeqAIJ(Mat A,IS *zrows)
109: {
110:   PetscInt       nrows,*rows;

114:   *zrows = NULL;
115:   MatFindZeroDiagonals_SeqAIJ_Private(A,&nrows,&rows);
116:   ISCreateGeneral(PetscObjectComm((PetscObject)A),nrows,rows,PETSC_OWN_POINTER,zrows);
117:   return(0);
118: }

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

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

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

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

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

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

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

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

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

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

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

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

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

301:   PetscFree(*ia);
302:   PetscFree(*ja);
303:   return(0);
304: }

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

322:   *nn = n;
323:   if (!ia) return(0);

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

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

360:   MatRestoreColumnIJ_SeqAIJ(A,oshift,symmetric,inodecompressed,n,ia,ja,done);
361:   PetscFree(*spidx);
362:   return(0);
363: }

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

374:   PetscMemcpy(a->a+ai[row],v,(ai[row+1]-ai[row])*sizeof(PetscScalar));
375:   return(0);
376: }

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

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

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

388: */

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

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

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

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

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

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


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

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


550: PetscErrorCode MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer)
551: {
552:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
554:   PetscInt       i,*col_lens;
555:   int            fd;
556:   FILE           *file;

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

562:   col_lens[0] = MAT_FILE_CLASSID;
563:   col_lens[1] = A->rmap->n;
564:   col_lens[2] = A->cmap->n;
565:   col_lens[3] = a->nz;

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

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

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

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

587: extern PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);

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

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

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

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

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

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

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

865:   PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);
866:   PetscViewerGetFormat(viewer,&format);

868:   PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);
869:   /* loop over matrix elements drawing boxes */

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

907:     for (i=0; i<nz; i++) {
908:       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
909:     }
910:     scale = (245.0 - PETSC_DRAW_BASIC_COLORS)/maxv;
911:     PetscDrawGetPopup(draw,&popup);
912:     if (popup) {
913:       PetscDrawScalePopup(popup,0.0,maxv);
914:     }
915:     count = 0;
916:     for (i=0; i<m; i++) {
917:       y_l = m - i - 1.0; y_r = y_l + 1.0;
918:       for (j=a->i[i]; j<a->i[i+1]; j++) {
919:         x_l   = a->j[j]; x_r = x_l + 1.0;
920:         color = PETSC_DRAW_BASIC_COLORS + (PetscInt)(scale*PetscAbsScalar(a->a[count]));
921:         PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);
922:         count++;
923:       }
924:     }
925:   }
926:   return(0);
927: }

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

940:   PetscViewerDrawGetDraw(viewer,0,&draw);
941:   PetscDrawIsNull(draw,&isnull);
942:   if (isnull) return(0);

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

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

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

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

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

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

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

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

1024:   A->info.mallocs    += a->reallocs;
1025:   a->reallocs         = 0;
1026:   A->info.nz_unneeded = (PetscReal)fshift;
1027:   a->rmax             = rmax;

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

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

1045:   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
1046:   MatSeqAIJInvalidateDiagonal(A);
1047:   return(0);
1048: }

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

1060:   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
1061:   MatSeqAIJInvalidateDiagonal(A);
1062:   return(0);
1063: }

1065: #if defined(PETSC_THREADCOMM_ACTIVE)
1066: PetscErrorCode MatZeroEntries_SeqAIJ_Kernel(PetscInt thread_id,Mat A)
1067: {
1069:   PetscInt       *trstarts=A->rmap->trstarts;
1070:   PetscInt       n,start,end;
1071:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;

1073:   start = trstarts[thread_id];
1074:   end   = trstarts[thread_id+1];
1075:   n     = a->i[end] - a->i[start];
1076:   PetscMemzero(a->a+a->i[start],n*sizeof(PetscScalar));
1077:   return 0;
1078: }

1082: PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
1083: {

1087:   PetscThreadCommRunKernel(PetscObjectComm((PetscObject)A),(PetscThreadKernel)MatZeroEntries_SeqAIJ_Kernel,1,A);
1088:   MatSeqAIJInvalidateDiagonal(A);
1089:   return(0);
1090: }
1091: #else
1094: PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
1095: {
1096:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;

1100:   PetscMemzero(a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));
1101:   MatSeqAIJInvalidateDiagonal(A);
1102:   return(0);
1103: }
1104: #endif

1106: extern PetscErrorCode MatDestroy_Redundant(Mat_Redundant **);

1110: PetscErrorCode MatDestroy_SeqAIJ(Mat A)
1111: {
1112:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;

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

1134:   MatDestroy_SeqAIJ_Inode(A);
1135:   PetscFree(A->data);

1137:   PetscObjectChangeTypeName((PetscObject)A,0);
1138:   PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetColumnIndices_C",NULL);
1139:   PetscObjectComposeFunction((PetscObject)A,"MatStoreValues_C",NULL);
1140:   PetscObjectComposeFunction((PetscObject)A,"MatRetrieveValues_C",NULL);
1141:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqsbaij_C",NULL);
1142:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqbaij_C",NULL);
1143:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqaijperm_C",NULL);
1144: #if defined(PETSC_HAVE_ELEMENTAL)
1145:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_elemental_C",NULL);
1146: #endif
1147:   PetscObjectComposeFunction((PetscObject)A,"MatIsTranspose_C",NULL);
1148:   PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocation_C",NULL);
1149:   PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C",NULL);
1150:   PetscObjectComposeFunction((PetscObject)A,"MatReorderForNonzeroDiagonal_C",NULL);
1151:   return(0);
1152: }

1156: PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscBool flg)
1157: {
1158:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;

1162:   switch (op) {
1163:   case MAT_ROW_ORIENTED:
1164:     a->roworiented = flg;
1165:     break;
1166:   case MAT_KEEP_NONZERO_PATTERN:
1167:     a->keepnonzeropattern = flg;
1168:     break;
1169:   case MAT_NEW_NONZERO_LOCATIONS:
1170:     a->nonew = (flg ? 0 : 1);
1171:     break;
1172:   case MAT_NEW_NONZERO_LOCATION_ERR:
1173:     a->nonew = (flg ? -1 : 0);
1174:     break;
1175:   case MAT_NEW_NONZERO_ALLOCATION_ERR:
1176:     a->nonew = (flg ? -2 : 0);
1177:     break;
1178:   case MAT_UNUSED_NONZERO_LOCATION_ERR:
1179:     a->nounused = (flg ? -1 : 0);
1180:     break;
1181:   case MAT_IGNORE_ZERO_ENTRIES:
1182:     a->ignorezeroentries = flg;
1183:     break;
1184:   case MAT_SPD:
1185:   case MAT_SYMMETRIC:
1186:   case MAT_STRUCTURALLY_SYMMETRIC:
1187:   case MAT_HERMITIAN:
1188:   case MAT_SYMMETRY_ETERNAL:
1189:     /* These options are handled directly by MatSetOption() */
1190:     break;
1191:   case MAT_NEW_DIAGONALS:
1192:   case MAT_IGNORE_OFF_PROC_ENTRIES:
1193:   case MAT_USE_HASH_TABLE:
1194:     PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);
1195:     break;
1196:   case MAT_USE_INODES:
1197:     /* Not an error because MatSetOption_SeqAIJ_Inode handles this one */
1198:     break;
1199:   default:
1200:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
1201:   }
1202:   MatSetOption_SeqAIJ_Inode(A,op,flg);
1203:   return(0);
1204: }

1208: PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
1209: {
1210:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1212:   PetscInt       i,j,n,*ai=a->i,*aj=a->j,nz;
1213:   PetscScalar    *aa=a->a,*x,zero=0.0;

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

1219:   if (A->factortype == MAT_FACTOR_ILU || A->factortype == MAT_FACTOR_LU) {
1220:     PetscInt *diag=a->diag;
1221:     VecGetArray(v,&x);
1222:     for (i=0; i<n; i++) x[i] = 1.0/aa[diag[i]];
1223:     VecRestoreArray(v,&x);
1224:     return(0);
1225:   }

1227:   VecSet(v,zero);
1228:   VecGetArray(v,&x);
1229:   for (i=0; i<n; i++) {
1230:     nz = ai[i+1] - ai[i];
1231:     if (!nz) x[i] = 0.0;
1232:     for (j=ai[i]; j<ai[i+1]; j++) {
1233:       if (aj[j] == i) {
1234:         x[i] = aa[j];
1235:         break;
1236:       }
1237:     }
1238:   }
1239:   VecRestoreArray(v,&x);
1240:   return(0);
1241: }

1243: #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
1246: PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
1247: {
1248:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1249:   PetscScalar    *x,*y;
1251:   PetscInt       m = A->rmap->n;
1252: #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1253:   MatScalar         *v;
1254:   PetscScalar       alpha;
1255:   PetscInt          n,i,j,*idx,*ii,*ridx=NULL;
1256:   Mat_CompressedRow cprow    = a->compressedrow;
1257:   PetscBool         usecprow = cprow.use;
1258: #endif

1261:   if (zz != yy) {VecCopy(zz,yy);}
1262:   VecGetArray(xx,&x);
1263:   VecGetArray(yy,&y);

1265: #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1266:   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
1267: #else
1268:   if (usecprow) {
1269:     m    = cprow.nrows;
1270:     ii   = cprow.i;
1271:     ridx = cprow.rindex;
1272:   } else {
1273:     ii = a->i;
1274:   }
1275:   for (i=0; i<m; i++) {
1276:     idx = a->j + ii[i];
1277:     v   = a->a + ii[i];
1278:     n   = ii[i+1] - ii[i];
1279:     if (usecprow) {
1280:       alpha = x[ridx[i]];
1281:     } else {
1282:       alpha = x[i];
1283:     }
1284:     for (j=0; j<n; j++) y[idx[j]] += alpha*v[j];
1285:   }
1286: #endif
1287:   PetscLogFlops(2.0*a->nz);
1288:   VecRestoreArray(xx,&x);
1289:   VecRestoreArray(yy,&y);
1290:   return(0);
1291: }

1295: PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
1296: {

1300:   VecSet(yy,0.0);
1301:   MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);
1302:   return(0);
1303: }

1305: #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
1306: #if defined(PETSC_THREADCOMM_ACTIVE)
1307: PetscErrorCode MatMult_SeqAIJ_Kernel(PetscInt thread_id,Mat A,Vec xx,Vec yy)
1308: {
1309:   PetscErrorCode    ierr;
1310:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1311:   PetscScalar       *y;
1312:   const PetscScalar *x;
1313:   const MatScalar   *aa;
1314:   PetscInt          *trstarts=A->rmap->trstarts;
1315:   PetscInt          n,start,end,i;
1316:   const PetscInt    *aj,*ai;
1317:   PetscScalar       sum;

1319:   VecGetArrayRead(xx,&x);
1320:   VecGetArray(yy,&y);
1321:   start = trstarts[thread_id];
1322:   end   = trstarts[thread_id+1];
1323:   aj    = a->j;
1324:   aa    = a->a;
1325:   ai    = a->i;
1326:   for (i=start; i<end; i++) {
1327:     n   = ai[i+1] - ai[i];
1328:     aj  = a->j + ai[i];
1329:     aa  = a->a + ai[i];
1330:     sum = 0.0;
1331:     PetscSparseDensePlusDot(sum,x,aa,aj,n);
1332:     y[i] = sum;
1333:   }
1334:   VecRestoreArrayRead(xx,&x);
1335:   VecRestoreArray(yy,&y);
1336:   return 0;
1337: }

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

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

1359:   aj = a->j;
1360:   aa = a->a;
1361:   ii = a->i;
1362:   if (usecprow) { /* use compressed row format */
1363:     VecGetArrayRead(xx,&x);
1364:     VecGetArray(yy,&y);
1365:     PetscMemzero(y,m*sizeof(PetscScalar));
1366:     m    = a->compressedrow.nrows;
1367:     ii   = a->compressedrow.i;
1368:     ridx = a->compressedrow.rindex;
1369:     for (i=0; i<m; i++) {
1370:       n           = ii[i+1] - ii[i];
1371:       aj          = a->j + ii[i];
1372:       aa          = a->a + ii[i];
1373:       sum         = 0.0;
1374:       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1375:       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
1376:       y[*ridx++] = sum;
1377:     }
1378:     VecRestoreArrayRead(xx,&x);
1379:     VecRestoreArray(yy,&y);
1380:   } else { /* do not use compressed row format */
1381: #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
1382:     fortranmultaij_(&m,x,ii,aj,aa,y);
1383: #else
1384:     PetscThreadCommRunKernel(PetscObjectComm((PetscObject)A),(PetscThreadKernel)MatMult_SeqAIJ_Kernel,3,A,xx,yy);
1385: #endif
1386:   }
1387:   PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);
1388:   return(0);
1389: }
1390: #else
1393: PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
1394: {
1395:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1396:   PetscScalar       *y;
1397:   const PetscScalar *x;
1398:   const MatScalar   *aa;
1399:   PetscErrorCode    ierr;
1400:   PetscInt          m=A->rmap->n;
1401:   const PetscInt    *aj,*ii,*ridx=NULL;
1402:   PetscInt          n,i;
1403:   PetscScalar       sum;
1404:   PetscBool         usecprow=a->compressedrow.use;

1406: #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
1407: #pragma disjoint(*x,*y,*aa)
1408: #endif

1411:   VecGetArrayRead(xx,&x);
1412:   VecGetArray(yy,&y);
1413:   aj   = a->j;
1414:   aa   = a->a;
1415:   ii   = a->i;
1416:   if (usecprow) { /* use compressed row format */
1417:     PetscMemzero(y,m*sizeof(PetscScalar));
1418:     m    = a->compressedrow.nrows;
1419:     ii   = a->compressedrow.i;
1420:     ridx = a->compressedrow.rindex;
1421:     for (i=0; i<m; i++) {
1422:       n           = ii[i+1] - ii[i];
1423:       aj          = a->j + ii[i];
1424:       aa          = a->a + ii[i];
1425:       sum         = 0.0;
1426:       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1427:       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
1428:       y[*ridx++] = sum;
1429:     }
1430:   } else { /* do not use compressed row format */
1431: #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
1432:     fortranmultaij_(&m,x,ii,aj,aa,y);
1433: #else
1434: #if defined(PETSC_THREADCOMM_ACTIVE)
1435:     PetscThreadCommRunKernel(PetscObjectComm((PetscObject)A),(PetscThreadKernel)MatMult_SeqAIJ_Kernel,3,A,xx,yy);
1436: #else
1437:     for (i=0; i<m; i++) {
1438:       n           = ii[i+1] - ii[i];
1439:       aj          = a->j + ii[i];
1440:       aa          = a->a + ii[i];
1441:       sum         = 0.0;
1442:       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1443:       y[i] = sum;
1444:     }
1445: #endif
1446: #endif
1447:   }
1448:   PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);
1449:   VecRestoreArrayRead(xx,&x);
1450:   VecRestoreArray(yy,&y);
1451:   return(0);
1452: }
1453: #endif

1457: PetscErrorCode MatMultMax_SeqAIJ(Mat A,Vec xx,Vec yy)
1458: {
1459:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1460:   PetscScalar       *y;
1461:   const PetscScalar *x;
1462:   const MatScalar   *aa;
1463:   PetscErrorCode    ierr;
1464:   PetscInt          m=A->rmap->n;
1465:   const PetscInt    *aj,*ii,*ridx=NULL;
1466:   PetscInt          n,i,nonzerorow=0;
1467:   PetscScalar       sum;
1468:   PetscBool         usecprow=a->compressedrow.use;

1470: #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
1471: #pragma disjoint(*x,*y,*aa)
1472: #endif

1475:   VecGetArrayRead(xx,&x);
1476:   VecGetArray(yy,&y);
1477:   aj   = a->j;
1478:   aa   = a->a;
1479:   ii   = a->i;
1480:   if (usecprow) { /* use compressed row format */
1481:     m    = a->compressedrow.nrows;
1482:     ii   = a->compressedrow.i;
1483:     ridx = a->compressedrow.rindex;
1484:     for (i=0; i<m; i++) {
1485:       n           = ii[i+1] - ii[i];
1486:       aj          = a->j + ii[i];
1487:       aa          = a->a + ii[i];
1488:       sum         = 0.0;
1489:       nonzerorow += (n>0);
1490:       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1491:       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
1492:       y[*ridx++] = sum;
1493:     }
1494:   } else { /* do not use compressed row format */
1495:     for (i=0; i<m; i++) {
1496:       n           = ii[i+1] - ii[i];
1497:       aj          = a->j + ii[i];
1498:       aa          = a->a + ii[i];
1499:       sum         = 0.0;
1500:       nonzerorow += (n>0);
1501:       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1502:       y[i] = sum;
1503:     }
1504:   }
1505:   PetscLogFlops(2.0*a->nz - nonzerorow);
1506:   VecRestoreArrayRead(xx,&x);
1507:   VecRestoreArray(yy,&y);
1508:   return(0);
1509: }

1513: PetscErrorCode MatMultAddMax_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1514: {
1515:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1516:   PetscScalar       *y,*z;
1517:   const PetscScalar *x;
1518:   const MatScalar   *aa;
1519:   PetscErrorCode    ierr;
1520:   PetscInt          m = A->rmap->n,*aj,*ii;
1521:   PetscInt          n,i,*ridx=NULL;
1522:   PetscScalar       sum;
1523:   PetscBool         usecprow=a->compressedrow.use;

1526:   VecGetArrayRead(xx,&x);
1527:   VecGetArray(yy,&y);
1528:   if (zz != yy) {
1529:     VecGetArray(zz,&z);
1530:   } else {
1531:     z = y;
1532:   }

1534:   aj = a->j;
1535:   aa = a->a;
1536:   ii = a->i;
1537:   if (usecprow) { /* use compressed row format */
1538:     if (zz != yy) {
1539:       PetscMemcpy(z,y,m*sizeof(PetscScalar));
1540:     }
1541:     m    = a->compressedrow.nrows;
1542:     ii   = a->compressedrow.i;
1543:     ridx = a->compressedrow.rindex;
1544:     for (i=0; i<m; i++) {
1545:       n   = ii[i+1] - ii[i];
1546:       aj  = a->j + ii[i];
1547:       aa  = a->a + ii[i];
1548:       sum = y[*ridx];
1549:       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1550:       z[*ridx++] = sum;
1551:     }
1552:   } else { /* do not use compressed row format */
1553:     for (i=0; i<m; i++) {
1554:       n   = ii[i+1] - ii[i];
1555:       aj  = a->j + ii[i];
1556:       aa  = a->a + ii[i];
1557:       sum = y[i];
1558:       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1559:       z[i] = sum;
1560:     }
1561:   }
1562:   PetscLogFlops(2.0*a->nz);
1563:   VecRestoreArrayRead(xx,&x);
1564:   VecRestoreArray(yy,&y);
1565:   if (zz != yy) {
1566:     VecRestoreArray(zz,&z);
1567:   }
1568:   return(0);
1569: }

1571: #include <../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h>
1574: PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1575: {
1576:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1577:   PetscScalar       *y,*z;
1578:   const PetscScalar *x;
1579:   const MatScalar   *aa;
1580:   PetscErrorCode    ierr;
1581:   PetscInt          m = A->rmap->n,*aj,*ii;
1582:   PetscInt          n,i,*ridx=NULL;
1583:   PetscScalar       sum;
1584:   PetscBool         usecprow=a->compressedrow.use;

1587:   VecGetArrayRead(xx,&x);
1588:   VecGetArray(yy,&y);
1589:   if (zz != yy) {
1590:     VecGetArray(zz,&z);
1591:   } else {
1592:     z = y;
1593:   }

1595:   aj = a->j;
1596:   aa = a->a;
1597:   ii = a->i;
1598:   if (usecprow) { /* use compressed row format */
1599:     if (zz != yy) {
1600:       PetscMemcpy(z,y,m*sizeof(PetscScalar));
1601:     }
1602:     m    = a->compressedrow.nrows;
1603:     ii   = a->compressedrow.i;
1604:     ridx = a->compressedrow.rindex;
1605:     for (i=0; i<m; i++) {
1606:       n   = ii[i+1] - ii[i];
1607:       aj  = a->j + ii[i];
1608:       aa  = a->a + ii[i];
1609:       sum = y[*ridx];
1610:       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1611:       z[*ridx++] = sum;
1612:     }
1613:   } else { /* do not use compressed row format */
1614: #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
1615:     fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
1616: #else
1617:     for (i=0; i<m; i++) {
1618:       n   = ii[i+1] - ii[i];
1619:       aj  = a->j + ii[i];
1620:       aa  = a->a + ii[i];
1621:       sum = y[i];
1622:       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1623:       z[i] = sum;
1624:     }
1625: #endif
1626:   }
1627:   PetscLogFlops(2.0*a->nz);
1628:   VecRestoreArrayRead(xx,&x);
1629:   VecRestoreArray(yy,&y);
1630:   if (zz != yy) {
1631:     VecRestoreArray(zz,&z);
1632:   }
1633: #if defined(PETSC_HAVE_CUSP)
1634:   /*
1635:   VecView(xx,0);
1636:   VecView(zz,0);
1637:   MatView(A,0);
1638:   */
1639: #endif
1640:   return(0);
1641: }

1643: /*
1644:      Adds diagonal pointers to sparse matrix structure.
1645: */
1648: PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
1649: {
1650:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1652:   PetscInt       i,j,m = A->rmap->n;

1655:   if (!a->diag) {
1656:     PetscMalloc1(m,&a->diag);
1657:     PetscLogObjectMemory((PetscObject)A, m*sizeof(PetscInt));
1658:   }
1659:   for (i=0; i<A->rmap->n; i++) {
1660:     a->diag[i] = a->i[i+1];
1661:     for (j=a->i[i]; j<a->i[i+1]; j++) {
1662:       if (a->j[j] == i) {
1663:         a->diag[i] = j;
1664:         break;
1665:       }
1666:     }
1667:   }
1668:   return(0);
1669: }

1671: /*
1672:      Checks for missing diagonals
1673: */
1676: PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscBool  *missing,PetscInt *d)
1677: {
1678:   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1679:   PetscInt   *diag,*ii = a->i,i;

1682:   *missing = PETSC_FALSE;
1683:   if (A->rmap->n > 0 && !ii) {
1684:     *missing = PETSC_TRUE;
1685:     if (d) *d = 0;
1686:     PetscInfo(A,"Matrix has no entries therefore is missing diagonal");
1687:   } else {
1688:     diag = a->diag;
1689:     for (i=0; i<A->rmap->n; i++) {
1690:       if (diag[i] >= ii[i+1]) {
1691:         *missing = PETSC_TRUE;
1692:         if (d) *d = i;
1693:         PetscInfo1(A,"Matrix is missing diagonal number %D",i);
1694:         break;
1695:       }
1696:     }
1697:   }
1698:   return(0);
1699: }

1703: PetscErrorCode  MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
1704: {
1705:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
1707:   PetscInt       i,*diag,m = A->rmap->n;
1708:   MatScalar      *v = a->a;
1709:   PetscScalar    *idiag,*mdiag;

1712:   if (a->idiagvalid) return(0);
1713:   MatMarkDiagonal_SeqAIJ(A);
1714:   diag = a->diag;
1715:   if (!a->idiag) {
1716:     PetscMalloc3(m,&a->idiag,m,&a->mdiag,m,&a->ssor_work);
1717:     PetscLogObjectMemory((PetscObject)A, 3*m*sizeof(PetscScalar));
1718:     v    = a->a;
1719:   }
1720:   mdiag = a->mdiag;
1721:   idiag = a->idiag;

1723:   if (omega == 1.0 && !PetscAbsScalar(fshift)) {
1724:     for (i=0; i<m; i++) {
1725:       mdiag[i] = v[diag[i]];
1726:       if (!PetscAbsScalar(mdiag[i])) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
1727:       idiag[i] = 1.0/v[diag[i]];
1728:     }
1729:     PetscLogFlops(m);
1730:   } else {
1731:     for (i=0; i<m; i++) {
1732:       mdiag[i] = v[diag[i]];
1733:       idiag[i] = omega/(fshift + v[diag[i]]);
1734:     }
1735:     PetscLogFlops(2.0*m);
1736:   }
1737:   a->idiagvalid = PETSC_TRUE;
1738:   return(0);
1739: }

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

1755:   its = its*lits;

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

1762:   diag  = a->diag;
1763:   t     = a->ssor_work;
1764:   idiag = a->idiag;
1765:   mdiag = a->mdiag;

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

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

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

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

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

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

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

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


1916: PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
1917: {
1918:   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;

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

1942: PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
1943: {
1944:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1945:   PetscInt          i,m = A->rmap->n - 1,d = 0;
1946:   PetscErrorCode    ierr;
1947:   const PetscScalar *xx;
1948:   PetscScalar       *bb;
1949:   PetscBool         missing;

1952:   if (x && b) {
1953:     VecGetArrayRead(x,&xx);
1954:     VecGetArray(b,&bb);
1955:     for (i=0; i<N; i++) {
1956:       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1957:       bb[rows[i]] = diag*xx[rows[i]];
1958:     }
1959:     VecRestoreArrayRead(x,&xx);
1960:     VecRestoreArray(b,&bb);
1961:   }

1963:   if (a->keepnonzeropattern) {
1964:     for (i=0; i<N; i++) {
1965:       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1966:       PetscMemzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]*sizeof(PetscScalar));
1967:     }
1968:     if (diag != 0.0) {
1969:       MatMissingDiagonal_SeqAIJ(A,&missing,&d);
1970:       if (missing) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D",d);
1971:       for (i=0; i<N; i++) {
1972:         a->a[a->diag[rows[i]]] = diag;
1973:       }
1974:     }
1975:   } else {
1976:     if (diag != 0.0) {
1977:       for (i=0; i<N; i++) {
1978:         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1979:         if (a->ilen[rows[i]] > 0) {
1980:           a->ilen[rows[i]]    = 1;
1981:           a->a[a->i[rows[i]]] = diag;
1982:           a->j[a->i[rows[i]]] = rows[i];
1983:         } else { /* in case row was completely empty */
1984:           MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);
1985:         }
1986:       }
1987:     } else {
1988:       for (i=0; i<N; i++) {
1989:         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
1990:         a->ilen[rows[i]] = 0;
1991:       }
1992:     }
1993:     A->nonzerostate++;
1994:   }
1995:   MatAssemblyEnd_SeqAIJ(A,MAT_FINAL_ASSEMBLY);
1996:   return(0);
1997: }

2001: PetscErrorCode MatZeroRowsColumns_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
2002: {
2003:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
2004:   PetscInt          i,j,m = A->rmap->n - 1,d = 0;
2005:   PetscErrorCode    ierr;
2006:   PetscBool         missing,*zeroed,vecs = PETSC_FALSE;
2007:   const PetscScalar *xx;
2008:   PetscScalar       *bb;

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

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

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

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

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

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

2080: PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
2081: {
2082:   Mat_SeqAIJ     *a  = (Mat_SeqAIJ*)A->data;
2083:   MatScalar      *v  = a->a;
2084:   PetscReal      sum = 0.0;
2086:   PetscInt       i,j;

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

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

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

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

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

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

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

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

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

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

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

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

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

2203:   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
2204:     *B = C;
2205:   } else {
2206:     MatHeaderMerge(A,C);
2207:   }
2208:   return(0);
2209: }

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

2222:   bij = (Mat_SeqAIJ*) B->data;

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

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

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

2274:   bij = (Mat_SeqAIJ*) B->data;

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

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

2317: PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
2318: {

2322:   MatIsTranspose_SeqAIJ(A,A,tol,f);
2323:   return(0);
2324: }

2328: PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
2329: {

2333:   MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);
2334:   return(0);
2335: }

2339: PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
2340: {
2341:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2342:   PetscScalar    *l,*r,x;
2343:   MatScalar      *v;
2345:   PetscInt       i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz,*jj;

2348:   if (ll) {
2349:     /* The local size is used so that VecMPI can be passed to this routine
2350:        by MatDiagonalScale_MPIAIJ */
2351:     VecGetLocalSize(ll,&m);
2352:     if (m != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
2353:     VecGetArray(ll,&l);
2354:     v    = a->a;
2355:     for (i=0; i<m; i++) {
2356:       x = l[i];
2357:       M = a->i[i+1] - a->i[i];
2358:       for (j=0; j<M; j++) (*v++) *= x;
2359:     }
2360:     VecRestoreArray(ll,&l);
2361:     PetscLogFlops(nz);
2362:   }
2363:   if (rr) {
2364:     VecGetLocalSize(rr,&n);
2365:     if (n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
2366:     VecGetArray(rr,&r);
2367:     v    = a->a; jj = a->j;
2368:     for (i=0; i<nz; i++) (*v++) *= r[*jj++];
2369:     VecRestoreArray(rr,&r);
2370:     PetscLogFlops(nz);
2371:   }
2372:   MatSeqAIJInvalidateDiagonal(A);
2373:   return(0);
2374: }

2378: PetscErrorCode MatGetSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
2379: {
2380:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
2382:   PetscInt       *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens;
2383:   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
2384:   const PetscInt *irow,*icol;
2385:   PetscInt       nrows,ncols;
2386:   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
2387:   MatScalar      *a_new,*mat_a;
2388:   Mat            C;
2389:   PetscBool      stride,sorted;

2392:   ISSorted(isrow,&sorted);
2393:   if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
2394:   ISSorted(iscol,&sorted);
2395:   if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");

2397:   ISGetIndices(isrow,&irow);
2398:   ISGetLocalSize(isrow,&nrows);
2399:   ISGetLocalSize(iscol,&ncols);

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

2448:     /* loop over rows inserting into submatrix */
2449:     a_new = c->a;
2450:     j_new = c->j;
2451:     i_new = c->i;

2453:     for (i=0; i<nrows; i++) {
2454:       ii    = starts[i];
2455:       lensi = lens[i];
2456:       for (k=0; k<lensi; k++) {
2457:         *j_new++ = aj[ii+k] - first;
2458:       }
2459:       PetscMemcpy(a_new,a->a + starts[i],lensi*sizeof(PetscScalar));
2460:       a_new     += lensi;
2461:       i_new[i+1] = i_new[i] + lensi;
2462:       c->ilen[i] = lensi;
2463:     }
2464:     PetscFree2(lens,starts);
2465:   } else {
2466:     ISGetIndices(iscol,&icol);
2467:     PetscCalloc1(oldcols,&smap);
2468:     PetscMalloc1((1+nrows),&lens);
2469:     for (i=0; i<ncols; i++) {
2470: #if defined(PETSC_USE_DEBUG)
2471:       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);
2472: #endif
2473:       smap[icol[i]] = i+1;
2474:     }

2476:     /* determine lens of each row */
2477:     for (i=0; i<nrows; i++) {
2478:       kstart  = ai[irow[i]];
2479:       kend    = kstart + a->ilen[irow[i]];
2480:       lens[i] = 0;
2481:       for (k=kstart; k<kend; k++) {
2482:         if (smap[aj[k]]) {
2483:           lens[i]++;
2484:         }
2485:       }
2486:     }
2487:     /* Create and fill new matrix */
2488:     if (scall == MAT_REUSE_MATRIX) {
2489:       PetscBool equal;

2491:       c = (Mat_SeqAIJ*)((*B)->data);
2492:       if ((*B)->rmap->n  != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
2493:       PetscMemcmp(c->ilen,lens,(*B)->rmap->n*sizeof(PetscInt),&equal);
2494:       if (!equal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
2495:       PetscMemzero(c->ilen,(*B)->rmap->n*sizeof(PetscInt));
2496:       C    = *B;
2497:     } else {
2498:       PetscInt rbs,cbs;
2499:       MatCreate(PetscObjectComm((PetscObject)A),&C);
2500:       MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);
2501:       ISGetBlockSize(isrow,&rbs);
2502:       ISGetBlockSize(iscol,&cbs);
2503:       MatSetBlockSizes(C,rbs,cbs);
2504:       MatSetType(C,((PetscObject)A)->type_name);
2505:       MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);
2506:     }
2507:     c = (Mat_SeqAIJ*)(C->data);
2508:     for (i=0; i<nrows; i++) {
2509:       row      = irow[i];
2510:       kstart   = ai[row];
2511:       kend     = kstart + a->ilen[row];
2512:       mat_i    = c->i[i];
2513:       mat_j    = c->j + mat_i;
2514:       mat_a    = c->a + mat_i;
2515:       mat_ilen = c->ilen + i;
2516:       for (k=kstart; k<kend; k++) {
2517:         if ((tcol=smap[a->j[k]])) {
2518:           *mat_j++ = tcol - 1;
2519:           *mat_a++ = a->a[k];
2520:           (*mat_ilen)++;

2522:         }
2523:       }
2524:     }
2525:     /* Free work space */
2526:     ISRestoreIndices(iscol,&icol);
2527:     PetscFree(smap);
2528:     PetscFree(lens);
2529:   }
2530:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
2531:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);

2533:   ISRestoreIndices(isrow,&irow);
2534:   *B   = C;
2535:   return(0);
2536: }

2540: PetscErrorCode  MatGetMultiProcBlock_SeqAIJ(Mat mat,MPI_Comm subComm,MatReuse scall,Mat *subMat)
2541: {
2543:   Mat            B;

2546:   if (scall == MAT_INITIAL_MATRIX) {
2547:     MatCreate(subComm,&B);
2548:     MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->n,mat->cmap->n);
2549:     MatSetBlockSizesFromMats(B,mat,mat);
2550:     MatSetType(B,MATSEQAIJ);
2551:     MatDuplicateNoCreate_SeqAIJ(B,mat,MAT_COPY_VALUES,PETSC_TRUE);
2552:     *subMat = B;
2553:   } else {
2554:     MatCopy_SeqAIJ(mat,*subMat,SAME_NONZERO_PATTERN);
2555:   }
2556:   return(0);
2557: }

2561: PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
2562: {
2563:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
2565:   Mat            outA;
2566:   PetscBool      row_identity,col_identity;

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

2571:   ISIdentity(row,&row_identity);
2572:   ISIdentity(col,&col_identity);

2574:   outA             = inA;
2575:   outA->factortype = MAT_FACTOR_LU;

2577:   PetscObjectReference((PetscObject)row);
2578:   ISDestroy(&a->row);

2580:   a->row = row;

2582:   PetscObjectReference((PetscObject)col);
2583:   ISDestroy(&a->col);

2585:   a->col = col;

2587:   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
2588:   ISDestroy(&a->icol);
2589:   ISInvertPermutation(col,PETSC_DECIDE,&a->icol);
2590:   PetscLogObjectParent((PetscObject)inA,(PetscObject)a->icol);

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

2597:   MatMarkDiagonal_SeqAIJ(inA);
2598:   if (row_identity && col_identity) {
2599:     MatLUFactorNumeric_SeqAIJ_inplace(outA,inA,info);
2600:   } else {
2601:     MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);
2602:   }
2603:   return(0);
2604: }

2608: PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
2609: {
2610:   Mat_SeqAIJ     *a     = (Mat_SeqAIJ*)inA->data;
2611:   PetscScalar    oalpha = alpha;
2613:   PetscBLASInt   one = 1,bnz;

2616:   PetscBLASIntCast(a->nz,&bnz);
2617:   PetscStackCallBLAS("BLASscal",BLASscal_(&bnz,&oalpha,a->a,&one));
2618:   PetscLogFlops(a->nz);
2619:   MatSeqAIJInvalidateDiagonal(inA);
2620:   return(0);
2621: }

2625: PetscErrorCode MatGetSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
2626: {
2628:   PetscInt       i;

2631:   if (scall == MAT_INITIAL_MATRIX) {
2632:     PetscMalloc1((n+1),B);
2633:   }

2635:   for (i=0; i<n; i++) {
2636:     MatGetSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);
2637:   }
2638:   return(0);
2639: }

2643: PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
2644: {
2645:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2647:   PetscInt       row,i,j,k,l,m,n,*nidx,isz,val;
2648:   const PetscInt *idx;
2649:   PetscInt       start,end,*ai,*aj;
2650:   PetscBT        table;

2653:   m  = A->rmap->n;
2654:   ai = a->i;
2655:   aj = a->j;

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

2659:   PetscMalloc1((m+1),&nidx);
2660:   PetscBTCreate(m,&table);

2662:   for (i=0; i<is_max; i++) {
2663:     /* Initialize the two local arrays */
2664:     isz  = 0;
2665:     PetscBTMemzero(m,table);

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

2671:     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
2672:     for (j=0; j<n; ++j) {
2673:       if (!PetscBTLookupSet(table,idx[j])) nidx[isz++] = idx[j];
2674:     }
2675:     ISRestoreIndices(is[i],&idx);
2676:     ISDestroy(&is[i]);

2678:     k = 0;
2679:     for (j=0; j<ov; j++) { /* for each overlap */
2680:       n = isz;
2681:       for (; k<n; k++) { /* do only those rows in nidx[k], which are not done yet */
2682:         row   = nidx[k];
2683:         start = ai[row];
2684:         end   = ai[row+1];
2685:         for (l = start; l<end; l++) {
2686:           val = aj[l];
2687:           if (!PetscBTLookupSet(table,val)) nidx[isz++] = val;
2688:         }
2689:       }
2690:     }
2691:     ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,PETSC_COPY_VALUES,(is+i));
2692:   }
2693:   PetscBTDestroy(&table);
2694:   PetscFree(nidx);
2695:   return(0);
2696: }

2698: /* -------------------------------------------------------------- */
2701: PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
2702: {
2703:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2705:   PetscInt       i,nz = 0,m = A->rmap->n,n = A->cmap->n;
2706:   const PetscInt *row,*col;
2707:   PetscInt       *cnew,j,*lens;
2708:   IS             icolp,irowp;
2709:   PetscInt       *cwork = NULL;
2710:   PetscScalar    *vwork = NULL;

2713:   ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);
2714:   ISGetIndices(irowp,&row);
2715:   ISInvertPermutation(colp,PETSC_DECIDE,&icolp);
2716:   ISGetIndices(icolp,&col);

2718:   /* determine lengths of permuted rows */
2719:   PetscMalloc1((m+1),&lens);
2720:   for (i=0; i<m; i++) lens[row[i]] = a->i[i+1] - a->i[i];
2721:   MatCreate(PetscObjectComm((PetscObject)A),B);
2722:   MatSetSizes(*B,m,n,m,n);
2723:   MatSetBlockSizesFromMats(*B,A,A);
2724:   MatSetType(*B,((PetscObject)A)->type_name);
2725:   MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);
2726:   PetscFree(lens);

2728:   PetscMalloc1(n,&cnew);
2729:   for (i=0; i<m; i++) {
2730:     MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);
2731:     for (j=0; j<nz; j++) cnew[j] = col[cwork[j]];
2732:     MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);
2733:     MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);
2734:   }
2735:   PetscFree(cnew);

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

2739:   MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);
2740:   MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);
2741:   ISRestoreIndices(irowp,&row);
2742:   ISRestoreIndices(icolp,&col);
2743:   ISDestroy(&irowp);
2744:   ISDestroy(&icolp);
2745:   return(0);
2746: }

2750: PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
2751: {

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

2760:     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");
2761:     PetscMemcpy(b->a,a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));
2762:   } else {
2763:     MatCopy_Basic(A,B,str);
2764:   }
2765:   return(0);
2766: }

2770: PetscErrorCode MatSetUp_SeqAIJ(Mat A)
2771: {

2775:    MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);
2776:   return(0);
2777: }

2781: PetscErrorCode MatSeqAIJGetArray_SeqAIJ(Mat A,PetscScalar *array[])
2782: {
2783:   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;

2786:   *array = a->a;
2787:   return(0);
2788: }

2792: PetscErrorCode MatSeqAIJRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
2793: {
2795:   return(0);
2796: }

2798: /*
2799:    Computes the number of nonzeros per row needed for preallocation when X and Y
2800:    have different nonzero structure.
2801: */
2804: PetscErrorCode MatAXPYGetPreallocation_SeqX_private(PetscInt m,const PetscInt *xi,const PetscInt *xj,const PetscInt *yi,const PetscInt *yj,PetscInt *nnz)
2805: {
2806:   PetscInt       i,j,k,nzx,nzy;

2809:   /* Set the number of nonzeros in the new matrix */
2810:   for (i=0; i<m; i++) {
2811:     const PetscInt *xjj = xj+xi[i],*yjj = yj+yi[i];
2812:     nzx = xi[i+1] - xi[i];
2813:     nzy = yi[i+1] - yi[i];
2814:     nnz[i] = 0;
2815:     for (j=0,k=0; j<nzx; j++) {                   /* Point in X */
2816:       for (; k<nzy && yjj[k]<xjj[j]; k++) nnz[i]++; /* Catch up to X */
2817:       if (k<nzy && yjj[k]==xjj[j]) k++;             /* Skip duplicate */
2818:       nnz[i]++;
2819:     }
2820:     for (; k<nzy; k++) nnz[i]++;
2821:   }
2822:   return(0);
2823: }

2827: PetscErrorCode MatAXPYGetPreallocation_SeqAIJ(Mat Y,Mat X,PetscInt *nnz)
2828: {
2829:   PetscInt       m = Y->rmap->N;
2830:   Mat_SeqAIJ     *x = (Mat_SeqAIJ*)X->data;
2831:   Mat_SeqAIJ     *y = (Mat_SeqAIJ*)Y->data;

2835:   /* Set the number of nonzeros in the new matrix */
2836:   MatAXPYGetPreallocation_SeqX_private(m,x->i,x->j,y->i,y->j,nnz);
2837:   return(0);
2838: }

2842: PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2843: {
2845:   Mat_SeqAIJ     *x = (Mat_SeqAIJ*)X->data,*y = (Mat_SeqAIJ*)Y->data;
2846:   PetscBLASInt   one=1,bnz;

2849:   PetscBLASIntCast(x->nz,&bnz);
2850:   if (str == SAME_NONZERO_PATTERN) {
2851:     PetscScalar alpha = a;
2852:     PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
2853:     MatSeqAIJInvalidateDiagonal(Y);
2854:     PetscObjectStateIncrease((PetscObject)Y);
2855:   } else {
2856:     Mat      B;
2857:     PetscInt *nnz;
2858:     PetscMalloc1(Y->rmap->N,&nnz);
2859:     MatCreate(PetscObjectComm((PetscObject)Y),&B);
2860:     PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);
2861:     MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);
2862:     MatSetBlockSizesFromMats(B,Y,Y);
2863:     MatSetType(B,(MatType) ((PetscObject)Y)->type_name);
2864:     MatAXPYGetPreallocation_SeqAIJ(Y,X,nnz);
2865:     MatSeqAIJSetPreallocation(B,0,nnz);
2866:     MatAXPY_BasicWithPreallocation(B,Y,a,X,str);
2867:     MatHeaderReplace(Y,B);
2868:     PetscFree(nnz);
2869:   }
2870:   return(0);
2871: }

2875: PetscErrorCode  MatConjugate_SeqAIJ(Mat mat)
2876: {
2877: #if defined(PETSC_USE_COMPLEX)
2878:   Mat_SeqAIJ  *aij = (Mat_SeqAIJ*)mat->data;
2879:   PetscInt    i,nz;
2880:   PetscScalar *a;

2883:   nz = aij->nz;
2884:   a  = aij->a;
2885:   for (i=0; i<nz; i++) a[i] = PetscConj(a[i]);
2886: #else
2888: #endif
2889:   return(0);
2890: }

2894: PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2895: {
2896:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2898:   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2899:   PetscReal      atmp;
2900:   PetscScalar    *x;
2901:   MatScalar      *aa;

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

2909:   VecSet(v,0.0);
2910:   VecGetArray(v,&x);
2911:   VecGetLocalSize(v,&n);
2912:   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
2913:   for (i=0; i<m; i++) {
2914:     ncols = ai[1] - ai[0]; ai++;
2915:     x[i]  = 0.0;
2916:     for (j=0; j<ncols; j++) {
2917:       atmp = PetscAbsScalar(*aa);
2918:       if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
2919:       aa++; aj++;
2920:     }
2921:   }
2922:   VecRestoreArray(v,&x);
2923:   return(0);
2924: }

2928: PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2929: {
2930:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2932:   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2933:   PetscScalar    *x;
2934:   MatScalar      *aa;

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

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

2973: PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
2974: {
2975:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2977:   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
2978:   PetscReal      atmp;
2979:   PetscScalar    *x;
2980:   MatScalar      *aa;

2983:   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
2984:   aa = a->a;
2985:   ai = a->i;
2986:   aj = a->j;

2988:   VecSet(v,0.0);
2989:   VecGetArray(v,&x);
2990:   VecGetLocalSize(v,&n);
2991:   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);
2992:   for (i=0; i<m; i++) {
2993:     ncols = ai[1] - ai[0]; ai++;
2994:     if (ncols) {
2995:       /* Get first nonzero */
2996:       for (j = 0; j < ncols; j++) {
2997:         atmp = PetscAbsScalar(aa[j]);
2998:         if (atmp > 1.0e-12) {
2999:           x[i] = atmp;
3000:           if (idx) idx[i] = aj[j];
3001:           break;
3002:         }
3003:       }
3004:       if (j == ncols) {x[i] = PetscAbsScalar(*aa); if (idx) idx[i] = *aj;}
3005:     } else {
3006:       x[i] = 0.0; if (idx) idx[i] = 0;
3007:     }
3008:     for (j = 0; j < ncols; j++) {
3009:       atmp = PetscAbsScalar(*aa);
3010:       if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
3011:       aa++; aj++;
3012:     }
3013:   }
3014:   VecRestoreArray(v,&x);
3015:   return(0);
3016: }

3020: PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[])
3021: {
3022:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
3024:   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
3025:   PetscScalar    *x;
3026:   MatScalar      *aa;

3029:   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3030:   aa = a->a;
3031:   ai = a->i;
3032:   aj = a->j;

3034:   VecSet(v,0.0);
3035:   VecGetArray(v,&x);
3036:   VecGetLocalSize(v,&n);
3037:   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
3038:   for (i=0; i<m; i++) {
3039:     ncols = ai[1] - ai[0]; ai++;
3040:     if (ncols == A->cmap->n) { /* row is dense */
3041:       x[i] = *aa; if (idx) idx[i] = 0;
3042:     } else {  /* row is sparse so already KNOW minimum is 0.0 or lower */
3043:       x[i] = 0.0;
3044:       if (idx) {   /* find first implicit 0.0 in the row */
3045:         idx[i] = 0; /* in case ncols is zero */
3046:         for (j=0; j<ncols; j++) {
3047:           if (aj[j] > j) {
3048:             idx[i] = j;
3049:             break;
3050:           }
3051:         }
3052:       }
3053:     }
3054:     for (j=0; j<ncols; j++) {
3055:       if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
3056:       aa++; aj++;
3057:     }
3058:   }
3059:   VecRestoreArray(v,&x);
3060:   return(0);
3061: }

3063: #include <petscblaslapack.h>
3064: #include <petsc-private/kernels/blockinvert.h>

3068: PetscErrorCode  MatInvertBlockDiagonal_SeqAIJ(Mat A,const PetscScalar **values)
3069: {
3070:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
3072:   PetscInt       i,bs = PetscAbs(A->rmap->bs),mbs = A->rmap->n/bs,ipvt[5],bs2 = bs*bs,*v_pivots,ij[7],*IJ,j;
3073:   MatScalar      *diag,work[25],*v_work;
3074:   PetscReal      shift = 0.0;

3077:   if (a->ibdiagvalid) {
3078:     if (values) *values = a->ibdiag;
3079:     return(0);
3080:   }
3081:   MatMarkDiagonal_SeqAIJ(A);
3082:   if (!a->ibdiag) {
3083:     PetscMalloc1(bs2*mbs,&a->ibdiag);
3084:     PetscLogObjectMemory((PetscObject)A,bs2*mbs*sizeof(PetscScalar));
3085:   }
3086:   diag = a->ibdiag;
3087:   if (values) *values = a->ibdiag;
3088:   /* factor and invert each block */
3089:   switch (bs) {
3090:   case 1:
3091:     for (i=0; i<mbs; i++) {
3092:       MatGetValues(A,1,&i,1,&i,diag+i);
3093:       diag[i] = (PetscScalar)1.0 / (diag[i] + shift);
3094:     }
3095:     break;
3096:   case 2:
3097:     for (i=0; i<mbs; i++) {
3098:       ij[0] = 2*i; ij[1] = 2*i + 1;
3099:       MatGetValues(A,2,ij,2,ij,diag);
3100:       PetscKernel_A_gets_inverse_A_2(diag,shift);
3101:       PetscKernel_A_gets_transpose_A_2(diag);
3102:       diag += 4;
3103:     }
3104:     break;
3105:   case 3:
3106:     for (i=0; i<mbs; i++) {
3107:       ij[0] = 3*i; ij[1] = 3*i + 1; ij[2] = 3*i + 2;
3108:       MatGetValues(A,3,ij,3,ij,diag);
3109:       PetscKernel_A_gets_inverse_A_3(diag,shift);
3110:       PetscKernel_A_gets_transpose_A_3(diag);
3111:       diag += 9;
3112:     }
3113:     break;
3114:   case 4:
3115:     for (i=0; i<mbs; i++) {
3116:       ij[0] = 4*i; ij[1] = 4*i + 1; ij[2] = 4*i + 2; ij[3] = 4*i + 3;
3117:       MatGetValues(A,4,ij,4,ij,diag);
3118:       PetscKernel_A_gets_inverse_A_4(diag,shift);
3119:       PetscKernel_A_gets_transpose_A_4(diag);
3120:       diag += 16;
3121:     }
3122:     break;
3123:   case 5:
3124:     for (i=0; i<mbs; i++) {
3125:       ij[0] = 5*i; ij[1] = 5*i + 1; ij[2] = 5*i + 2; ij[3] = 5*i + 3; ij[4] = 5*i + 4;
3126:       MatGetValues(A,5,ij,5,ij,diag);
3127:       PetscKernel_A_gets_inverse_A_5(diag,ipvt,work,shift);
3128:       PetscKernel_A_gets_transpose_A_5(diag);
3129:       diag += 25;
3130:     }
3131:     break;
3132:   case 6:
3133:     for (i=0; i<mbs; i++) {
3134:       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;
3135:       MatGetValues(A,6,ij,6,ij,diag);
3136:       PetscKernel_A_gets_inverse_A_6(diag,shift);
3137:       PetscKernel_A_gets_transpose_A_6(diag);
3138:       diag += 36;
3139:     }
3140:     break;
3141:   case 7:
3142:     for (i=0; i<mbs; i++) {
3143:       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;
3144:       MatGetValues(A,7,ij,7,ij,diag);
3145:       PetscKernel_A_gets_inverse_A_7(diag,shift);
3146:       PetscKernel_A_gets_transpose_A_7(diag);
3147:       diag += 49;
3148:     }
3149:     break;
3150:   default:
3151:     PetscMalloc3(bs,&v_work,bs,&v_pivots,bs,&IJ);
3152:     for (i=0; i<mbs; i++) {
3153:       for (j=0; j<bs; j++) {
3154:         IJ[j] = bs*i + j;
3155:       }
3156:       MatGetValues(A,bs,IJ,bs,IJ,diag);
3157:       PetscKernel_A_gets_inverse_A(bs,diag,v_pivots,v_work);
3158:       PetscKernel_A_gets_transpose_A_N(diag,bs);
3159:       diag += bs2;
3160:     }
3161:     PetscFree3(v_work,v_pivots,IJ);
3162:   }
3163:   a->ibdiagvalid = PETSC_TRUE;
3164:   return(0);
3165: }

3169: static PetscErrorCode  MatSetRandom_SeqAIJ(Mat x,PetscRandom rctx)
3170: {
3172:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)x->data;
3173:   PetscScalar    a;
3174:   PetscInt       m,n,i,j,col;

3177:   if (!x->assembled) {
3178:     MatGetSize(x,&m,&n);
3179:     for (i=0; i<m; i++) {
3180:       for (j=0; j<aij->imax[i]; j++) {
3181:         PetscRandomGetValue(rctx,&a);
3182:         col  = (PetscInt)(n*PetscRealPart(a));
3183:         MatSetValues(x,1,&i,1,&col,&a,ADD_VALUES);
3184:       }
3185:     }
3186:   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not yet coded");
3187:   MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);
3188:   MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);
3189:   return(0);
3190: }

3192: /* -------------------------------------------------------------------*/
3193: static struct _MatOps MatOps_Values = { MatSetValues_SeqAIJ,
3194:                                         MatGetRow_SeqAIJ,
3195:                                         MatRestoreRow_SeqAIJ,
3196:                                         MatMult_SeqAIJ,
3197:                                 /*  4*/ MatMultAdd_SeqAIJ,
3198:                                         MatMultTranspose_SeqAIJ,
3199:                                         MatMultTransposeAdd_SeqAIJ,
3200:                                         0,
3201:                                         0,
3202:                                         0,
3203:                                 /* 10*/ 0,
3204:                                         MatLUFactor_SeqAIJ,
3205:                                         0,
3206:                                         MatSOR_SeqAIJ,
3207:                                         MatTranspose_SeqAIJ,
3208:                                 /*1 5*/ MatGetInfo_SeqAIJ,
3209:                                         MatEqual_SeqAIJ,
3210:                                         MatGetDiagonal_SeqAIJ,
3211:                                         MatDiagonalScale_SeqAIJ,
3212:                                         MatNorm_SeqAIJ,
3213:                                 /* 20*/ 0,
3214:                                         MatAssemblyEnd_SeqAIJ,
3215:                                         MatSetOption_SeqAIJ,
3216:                                         MatZeroEntries_SeqAIJ,
3217:                                 /* 24*/ MatZeroRows_SeqAIJ,
3218:                                         0,
3219:                                         0,
3220:                                         0,
3221:                                         0,
3222:                                 /* 29*/ MatSetUp_SeqAIJ,
3223:                                         0,
3224:                                         0,
3225:                                         0,
3226:                                         0,
3227:                                 /* 34*/ MatDuplicate_SeqAIJ,
3228:                                         0,
3229:                                         0,
3230:                                         MatILUFactor_SeqAIJ,
3231:                                         0,
3232:                                 /* 39*/ MatAXPY_SeqAIJ,
3233:                                         MatGetSubMatrices_SeqAIJ,
3234:                                         MatIncreaseOverlap_SeqAIJ,
3235:                                         MatGetValues_SeqAIJ,
3236:                                         MatCopy_SeqAIJ,
3237:                                 /* 44*/ MatGetRowMax_SeqAIJ,
3238:                                         MatScale_SeqAIJ,
3239:                                         0,
3240:                                         MatDiagonalSet_SeqAIJ,
3241:                                         MatZeroRowsColumns_SeqAIJ,
3242:                                 /* 49*/ MatSetRandom_SeqAIJ,
3243:                                         MatGetRowIJ_SeqAIJ,
3244:                                         MatRestoreRowIJ_SeqAIJ,
3245:                                         MatGetColumnIJ_SeqAIJ,
3246:                                         MatRestoreColumnIJ_SeqAIJ,
3247:                                 /* 54*/ MatFDColoringCreate_SeqXAIJ,
3248:                                         0,
3249:                                         0,
3250:                                         MatPermute_SeqAIJ,
3251:                                         0,
3252:                                 /* 59*/ 0,
3253:                                         MatDestroy_SeqAIJ,
3254:                                         MatView_SeqAIJ,
3255:                                         0,
3256:                                         MatMatMatMult_SeqAIJ_SeqAIJ_SeqAIJ,
3257:                                 /* 64*/ MatMatMatMultSymbolic_SeqAIJ_SeqAIJ_SeqAIJ,
3258:                                         MatMatMatMultNumeric_SeqAIJ_SeqAIJ_SeqAIJ,
3259:                                         0,
3260:                                         0,
3261:                                         0,
3262:                                 /* 69*/ MatGetRowMaxAbs_SeqAIJ,
3263:                                         MatGetRowMinAbs_SeqAIJ,
3264:                                         0,
3265:                                         MatSetColoring_SeqAIJ,
3266:                                         0,
3267:                                 /* 74*/ MatSetValuesAdifor_SeqAIJ,
3268:                                         MatFDColoringApply_AIJ,
3269:                                         0,
3270:                                         0,
3271:                                         0,
3272:                                 /* 79*/ MatFindZeroDiagonals_SeqAIJ,
3273:                                         0,
3274:                                         0,
3275:                                         0,
3276:                                         MatLoad_SeqAIJ,
3277:                                 /* 84*/ MatIsSymmetric_SeqAIJ,
3278:                                         MatIsHermitian_SeqAIJ,
3279:                                         0,
3280:                                         0,
3281:                                         0,
3282:                                 /* 89*/ MatMatMult_SeqAIJ_SeqAIJ,
3283:                                         MatMatMultSymbolic_SeqAIJ_SeqAIJ,
3284:                                         MatMatMultNumeric_SeqAIJ_SeqAIJ,
3285:                                         MatPtAP_SeqAIJ_SeqAIJ,
3286:                                         MatPtAPSymbolic_SeqAIJ_SeqAIJ_DenseAxpy,
3287:                                 /* 94*/ MatPtAPNumeric_SeqAIJ_SeqAIJ,
3288:                                         MatMatTransposeMult_SeqAIJ_SeqAIJ,
3289:                                         MatMatTransposeMultSymbolic_SeqAIJ_SeqAIJ,
3290:                                         MatMatTransposeMultNumeric_SeqAIJ_SeqAIJ,
3291:                                         0,
3292:                                 /* 99*/ 0,
3293:                                         0,
3294:                                         0,
3295:                                         MatConjugate_SeqAIJ,
3296:                                         0,
3297:                                 /*104*/ MatSetValuesRow_SeqAIJ,
3298:                                         MatRealPart_SeqAIJ,
3299:                                         MatImaginaryPart_SeqAIJ,
3300:                                         0,
3301:                                         0,
3302:                                 /*109*/ MatMatSolve_SeqAIJ,
3303:                                         0,
3304:                                         MatGetRowMin_SeqAIJ,
3305:                                         0,
3306:                                         MatMissingDiagonal_SeqAIJ,
3307:                                 /*114*/ 0,
3308:                                         0,
3309:                                         0,
3310:                                         0,
3311:                                         0,
3312:                                 /*119*/ 0,
3313:                                         0,
3314:                                         0,
3315:                                         0,
3316:                                         MatGetMultiProcBlock_SeqAIJ,
3317:                                 /*124*/ MatFindNonzeroRows_SeqAIJ,
3318:                                         MatGetColumnNorms_SeqAIJ,
3319:                                         MatInvertBlockDiagonal_SeqAIJ,
3320:                                         0,
3321:                                         0,
3322:                                 /*129*/ 0,
3323:                                         MatTransposeMatMult_SeqAIJ_SeqAIJ,
3324:                                         MatTransposeMatMultSymbolic_SeqAIJ_SeqAIJ,
3325:                                         MatTransposeMatMultNumeric_SeqAIJ_SeqAIJ,
3326:                                         MatTransposeColoringCreate_SeqAIJ,
3327:                                 /*134*/ MatTransColoringApplySpToDen_SeqAIJ,
3328:                                         MatTransColoringApplyDenToSp_SeqAIJ,
3329:                                         MatRARt_SeqAIJ_SeqAIJ,
3330:                                         MatRARtSymbolic_SeqAIJ_SeqAIJ,
3331:                                         MatRARtNumeric_SeqAIJ_SeqAIJ,
3332:                                  /*139*/0,
3333:                                         0,
3334:                                         0,
3335:                                         MatFDColoringSetUp_SeqXAIJ,
3336:                                         MatFindOffBlockDiagonalEntries_SeqAIJ
3337: };

3341: PetscErrorCode  MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
3342: {
3343:   Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data;
3344:   PetscInt   i,nz,n;

3347:   nz = aij->maxnz;
3348:   n  = mat->rmap->n;
3349:   for (i=0; i<nz; i++) {
3350:     aij->j[i] = indices[i];
3351:   }
3352:   aij->nz = nz;
3353:   for (i=0; i<n; i++) {
3354:     aij->ilen[i] = aij->imax[i];
3355:   }
3356:   return(0);
3357: }

3361: /*@
3362:     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
3363:        in the matrix.

3365:   Input Parameters:
3366: +  mat - the SeqAIJ matrix
3367: -  indices - the column indices

3369:   Level: advanced

3371:   Notes:
3372:     This can be called if you have precomputed the nonzero structure of the
3373:   matrix and want to provide it to the matrix object to improve the performance
3374:   of the MatSetValues() operation.

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

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

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

3383: @*/
3384: PetscErrorCode  MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
3385: {

3391:   PetscUseMethod(mat,"MatSeqAIJSetColumnIndices_C",(Mat,PetscInt*),(mat,indices));
3392:   return(0);
3393: }

3395: /* ----------------------------------------------------------------------------------------*/

3399: PetscErrorCode  MatStoreValues_SeqAIJ(Mat mat)
3400: {
3401:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)mat->data;
3403:   size_t         nz = aij->i[mat->rmap->n];

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

3408:   /* allocate space for values if not already there */
3409:   if (!aij->saved_values) {
3410:     PetscMalloc1((nz+1),&aij->saved_values);
3411:     PetscLogObjectMemory((PetscObject)mat,(nz+1)*sizeof(PetscScalar));
3412:   }

3414:   /* copy values over */
3415:   PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));
3416:   return(0);
3417: }

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

3426:    Collect on Mat

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

3431:   Level: advanced

3433:   Common Usage, with SNESSolve():
3434: $    Create Jacobian matrix
3435: $    Set linear terms into matrix
3436: $    Apply boundary conditions to matrix, at this time matrix must have
3437: $      final nonzero structure (i.e. setting the nonlinear terms and applying
3438: $      boundary conditions again will not change the nonzero structure
3439: $    MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3440: $    MatStoreValues(mat);
3441: $    Call SNESSetJacobian() with matrix
3442: $    In your Jacobian routine
3443: $      MatRetrieveValues(mat);
3444: $      Set nonlinear terms in matrix

3446:   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
3447: $    // build linear portion of Jacobian
3448: $    MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3449: $    MatStoreValues(mat);
3450: $    loop over nonlinear iterations
3451: $       MatRetrieveValues(mat);
3452: $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
3453: $       // call MatAssemblyBegin/End() on matrix
3454: $       Solve linear system with Jacobian
3455: $    endloop

3457:   Notes:
3458:     Matrix must already be assemblied before calling this routine
3459:     Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
3460:     calling this routine.

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

3465: .seealso: MatRetrieveValues()

3467: @*/
3468: PetscErrorCode  MatStoreValues(Mat mat)
3469: {

3474:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3475:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3476:   PetscUseMethod(mat,"MatStoreValues_C",(Mat),(mat));
3477:   return(0);
3478: }

3482: PetscErrorCode  MatRetrieveValues_SeqAIJ(Mat mat)
3483: {
3484:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)mat->data;
3486:   PetscInt       nz = aij->i[mat->rmap->n];

3489:   if (!aij->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3490:   if (!aij->saved_values) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
3491:   /* copy values over */
3492:   PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));
3493:   return(0);
3494: }

3498: /*@
3499:     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
3500:        example, reuse of the linear part of a Jacobian, while recomputing the
3501:        nonlinear portion.

3503:    Collect on Mat

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

3508:   Level: advanced

3510: .seealso: MatStoreValues()

3512: @*/
3513: PetscErrorCode  MatRetrieveValues(Mat mat)
3514: {

3519:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3520:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3521:   PetscUseMethod(mat,"MatRetrieveValues_C",(Mat),(mat));
3522:   return(0);
3523: }


3526: /* --------------------------------------------------------------------------------*/
3529: /*@C
3530:    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
3531:    (the default parallel PETSc format).  For good matrix assembly performance
3532:    the user should preallocate the matrix storage by setting the parameter nz
3533:    (or the array nnz).  By setting these parameters accurately, performance
3534:    during matrix assembly can be increased by more than a factor of 50.

3536:    Collective on MPI_Comm

3538:    Input Parameters:
3539: +  comm - MPI communicator, set to PETSC_COMM_SELF
3540: .  m - number of rows
3541: .  n - number of columns
3542: .  nz - number of nonzeros per row (same for all rows)
3543: -  nnz - array containing the number of nonzeros in the various rows
3544:          (possibly different for each row) or NULL

3546:    Output Parameter:
3547: .  A - the matrix

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

3553:    Notes:
3554:    If nnz is given then nz is ignored

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

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

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

3571:    Options Database Keys:
3572: +  -mat_no_inode  - Do not use inodes
3573: -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)

3575:    Level: intermediate

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

3579: @*/
3580: PetscErrorCode  MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
3581: {

3585:   MatCreate(comm,A);
3586:   MatSetSizes(*A,m,n,m,n);
3587:   MatSetType(*A,MATSEQAIJ);
3588:   MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);
3589:   return(0);
3590: }

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

3600:    Collective on MPI_Comm

3602:    Input Parameters:
3603: +  B - The matrix
3604: .  nz - number of nonzeros per row (same for all rows)
3605: -  nnz - array containing the number of nonzeros in the various rows
3606:          (possibly different for each row) or NULL

3608:    Notes:
3609:      If nnz is given then nz is ignored

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

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

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

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

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

3634:    Options Database Keys:
3635: +  -mat_no_inode  - Do not use inodes
3636: .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
3637: -  -mat_aij_oneindex - Internally use indexing starting at 1
3638:         rather than 0.  Note that when calling MatSetValues(),
3639:         the user still MUST index entries starting at 0!

3641:    Level: intermediate

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

3645: @*/
3646: PetscErrorCode  MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
3647: {

3653:   PetscTryMethod(B,"MatSeqAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[]),(B,nz,nnz));
3654:   return(0);
3655: }

3659: PetscErrorCode  MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,const PetscInt *nnz)
3660: {
3661:   Mat_SeqAIJ     *b;
3662:   PetscBool      skipallocation = PETSC_FALSE,realalloc = PETSC_FALSE;
3664:   PetscInt       i;

3667:   if (nz >= 0 || nnz) realalloc = PETSC_TRUE;
3668:   if (nz == MAT_SKIP_ALLOCATION) {
3669:     skipallocation = PETSC_TRUE;
3670:     nz             = 0;
3671:   }

3673:   PetscLayoutSetUp(B->rmap);
3674:   PetscLayoutSetUp(B->cmap);

3676:   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
3677:   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %D",nz);
3678:   if (nnz) {
3679:     for (i=0; i<B->rmap->n; i++) {
3680:       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]);
3681:       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);
3682:     }
3683:   }

3685:   B->preallocated = PETSC_TRUE;

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

3689:   if (!skipallocation) {
3690:     if (!b->imax) {
3691:       PetscMalloc2(B->rmap->n,&b->imax,B->rmap->n,&b->ilen);
3692:       PetscLogObjectMemory((PetscObject)B,2*B->rmap->n*sizeof(PetscInt));
3693:     }
3694:     if (!nnz) {
3695:       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
3696:       else if (nz < 0) nz = 1;
3697:       for (i=0; i<B->rmap->n; i++) b->imax[i] = nz;
3698:       nz = nz*B->rmap->n;
3699:     } else {
3700:       nz = 0;
3701:       for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
3702:     }
3703:     /* b->ilen will count nonzeros in each row so far. */
3704:     for (i=0; i<B->rmap->n; i++) b->ilen[i] = 0;

3706:     /* allocate the matrix space */
3707:     MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);
3708:     PetscMalloc3(nz,&b->a,nz,&b->j,B->rmap->n+1,&b->i);
3709:     PetscLogObjectMemory((PetscObject)B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));
3710:     b->i[0] = 0;
3711:     for (i=1; i<B->rmap->n+1; i++) {
3712:       b->i[i] = b->i[i-1] + b->imax[i-1];
3713:     }
3714:     b->singlemalloc = PETSC_TRUE;
3715:     b->free_a       = PETSC_TRUE;
3716:     b->free_ij      = PETSC_TRUE;
3717: #if defined(PETSC_THREADCOMM_ACTIVE)
3718:     MatZeroEntries_SeqAIJ(B);
3719: #endif
3720:   } else {
3721:     b->free_a  = PETSC_FALSE;
3722:     b->free_ij = PETSC_FALSE;
3723:   }

3725:   b->nz               = 0;
3726:   b->maxnz            = nz;
3727:   B->info.nz_unneeded = (double)b->maxnz;
3728:   if (realalloc) {
3729:     MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);
3730:   }
3731:   return(0);
3732: }

3734: #undef  __FUNCT__
3736: /*@
3737:    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.

3739:    Input Parameters:
3740: +  B - the matrix
3741: .  i - the indices into j for the start of each row (starts with zero)
3742: .  j - the column indices for each row (starts with zero) these must be sorted for each row
3743: -  v - optional values in the matrix

3745:    Level: developer

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

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

3751: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ
3752: @*/
3753: PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
3754: {

3760:   PetscTryMethod(B,"MatSeqAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));
3761:   return(0);
3762: }

3764: #undef  __FUNCT__
3766: PetscErrorCode  MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3767: {
3768:   PetscInt       i;
3769:   PetscInt       m,n;
3770:   PetscInt       nz;
3771:   PetscInt       *nnz, nz_max = 0;
3772:   PetscScalar    *values;

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

3778:   PetscLayoutSetUp(B->rmap);
3779:   PetscLayoutSetUp(B->cmap);

3781:   MatGetSize(B, &m, &n);
3782:   PetscMalloc1((m+1), &nnz);
3783:   for (i = 0; i < m; i++) {
3784:     nz     = Ii[i+1]- Ii[i];
3785:     nz_max = PetscMax(nz_max, nz);
3786:     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
3787:     nnz[i] = nz;
3788:   }
3789:   MatSeqAIJSetPreallocation(B, 0, nnz);
3790:   PetscFree(nnz);

3792:   if (v) {
3793:     values = (PetscScalar*) v;
3794:   } else {
3795:     PetscCalloc1(nz_max, &values);
3796:   }

3798:   for (i = 0; i < m; i++) {
3799:     nz   = Ii[i+1] - Ii[i];
3800:     MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);
3801:   }

3803:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
3804:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);

3806:   if (!v) {
3807:     PetscFree(values);
3808:   }
3809:   MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
3810:   return(0);
3811: }

3813: #include <../src/mat/impls/dense/seq/dense.h>
3814: #include <petsc-private/kernels/petscaxpy.h>

3818: /*
3819:     Computes (B'*A')' since computing B*A directly is untenable

3821:                n                       p                          p
3822:         (              )       (              )         (                  )
3823:       m (      A       )  *  n (       B      )   =   m (         C        )
3824:         (              )       (              )         (                  )

3826: */
3827: PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
3828: {
3829:   PetscErrorCode    ierr;
3830:   Mat_SeqDense      *sub_a = (Mat_SeqDense*)A->data;
3831:   Mat_SeqAIJ        *sub_b = (Mat_SeqAIJ*)B->data;
3832:   Mat_SeqDense      *sub_c = (Mat_SeqDense*)C->data;
3833:   PetscInt          i,n,m,q,p;
3834:   const PetscInt    *ii,*idx;
3835:   const PetscScalar *b,*a,*a_q;
3836:   PetscScalar       *c,*c_q;

3839:   m    = A->rmap->n;
3840:   n    = A->cmap->n;
3841:   p    = B->cmap->n;
3842:   a    = sub_a->v;
3843:   b    = sub_b->a;
3844:   c    = sub_c->v;
3845:   PetscMemzero(c,m*p*sizeof(PetscScalar));

3847:   ii  = sub_b->i;
3848:   idx = sub_b->j;
3849:   for (i=0; i<n; i++) {
3850:     q = ii[i+1] - ii[i];
3851:     while (q-->0) {
3852:       c_q = c + m*(*idx);
3853:       a_q = a + m*i;
3854:       PetscKernelAXPY(c_q,*b,a_q,m);
3855:       idx++;
3856:       b++;
3857:     }
3858:   }
3859:   return(0);
3860: }

3864: PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
3865: {
3867:   PetscInt       m=A->rmap->n,n=B->cmap->n;
3868:   Mat            Cmat;

3871:   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);
3872:   MatCreate(PetscObjectComm((PetscObject)A),&Cmat);
3873:   MatSetSizes(Cmat,m,n,m,n);
3874:   MatSetBlockSizesFromMats(Cmat,A,B);
3875:   MatSetType(Cmat,MATSEQDENSE);
3876:   MatSeqDenseSetPreallocation(Cmat,NULL);

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

3880:   *C = Cmat;
3881:   return(0);
3882: }

3884: /* ----------------------------------------------------------------*/
3887: PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
3888: {

3892:   if (scall == MAT_INITIAL_MATRIX) {
3893:     PetscLogEventBegin(MAT_MatMultSymbolic,A,B,0,0);
3894:     MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);
3895:     PetscLogEventEnd(MAT_MatMultSymbolic,A,B,0,0);
3896:   }
3897:   PetscLogEventBegin(MAT_MatMultNumeric,A,B,0,0);
3898:   MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);
3899:   PetscLogEventEnd(MAT_MatMultNumeric,A,B,0,0);
3900:   return(0);
3901: }


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

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

3911:   Level: beginner

3913: .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
3914: M*/

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

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

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

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

3931:   Level: beginner

3933: .seealso: MatCreateAIJ(), MatCreateSeqAIJ(), MATSEQAIJ,MATMPIAIJ
3934: M*/

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

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

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

3948:   Level: beginner

3950: .seealso: MatCreateMPIAIJCRL,MATSEQAIJCRL,MATMPIAIJCRL, MATSEQAIJCRL, MATMPIAIJCRL
3951: M*/

3953: #if defined(PETSC_HAVE_PASTIX)
3954: PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_pastix(Mat,MatFactorType,Mat*);
3955: #endif
3956: #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128)
3957: PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_essl(Mat,MatFactorType,Mat*);
3958: #endif
3959: PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCRL(Mat,MatType,MatReuse,Mat*);
3960: #if defined(PETSC_HAVE_ELEMENTAL)
3961: PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_Elemental(Mat,MatType,MatReuse,Mat*);
3962: #endif
3963: PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_petsc(Mat,MatFactorType,Mat*);
3964: PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_bas(Mat,MatFactorType,Mat*);
3965: extern PetscErrorCode  MatGetFactorAvailable_seqaij_petsc(Mat,MatFactorType,PetscBool*);
3966: #if defined(PETSC_HAVE_MUMPS)
3967: PETSC_EXTERN PetscErrorCode MatGetFactor_aij_mumps(Mat,MatFactorType,Mat*);
3968: #endif
3969: #if defined(PETSC_HAVE_SUPERLU)
3970: PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_superlu(Mat,MatFactorType,Mat*);
3971: #endif
3972: #if defined(PETSC_HAVE_MKL_PARDISO)
3973: PETSC_EXTERN PetscErrorCode MatGetFactor_aij_mkl_pardiso(Mat,MatFactorType,Mat*);
3974: #endif
3975: #if defined(PETSC_HAVE_SUPERLU_DIST)
3976: PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_superlu_dist(Mat,MatFactorType,Mat*);
3977: #endif
3978: #if defined(PETSC_HAVE_SUITESPARSE)
3979: PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_umfpack(Mat,MatFactorType,Mat*);
3980: #endif
3981: #if defined(PETSC_HAVE_SUITESPARSE)
3982: PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_cholmod(Mat,MatFactorType,Mat*);
3983: #endif
3984: #if defined(PETSC_HAVE_SUITESPARSE)
3985: PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_klu(Mat,MatFactorType,Mat*);
3986: #endif
3987: #if defined(PETSC_HAVE_LUSOL)
3988: PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_lusol(Mat,MatFactorType,Mat*);
3989: #endif
3990: #if defined(PETSC_HAVE_MATLAB_ENGINE)
3991: PETSC_EXTERN PetscErrorCode MatGetFactor_seqaij_matlab(Mat,MatFactorType,Mat*);
3992: extern PetscErrorCode  MatlabEnginePut_SeqAIJ(PetscObject,void*);
3993: extern PetscErrorCode  MatlabEngineGet_SeqAIJ(PetscObject,void*);
3994: #endif
3995: #if defined(PETSC_HAVE_CLIQUE)
3996: PETSC_EXTERN PetscErrorCode MatGetFactor_aij_clique(Mat,MatFactorType,Mat*);
3997: #endif


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

4005:    Not Collective

4007:    Input Parameter:
4008: .  mat - a MATSEQAIJ matrix

4010:    Output Parameter:
4011: .   array - pointer to the data

4013:    Level: intermediate

4015: .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90()
4016: @*/
4017: PetscErrorCode  MatSeqAIJGetArray(Mat A,PetscScalar **array)
4018: {

4022:   PetscUseMethod(A,"MatSeqAIJGetArray_C",(Mat,PetscScalar**),(A,array));
4023:   return(0);
4024: }

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

4031:    Not Collective

4033:    Input Parameter:
4034: .  mat - a MATSEQAIJ matrix

4036:    Output Parameter:
4037: .   nz - the maximum number of nonzeros in any row

4039:    Level: intermediate

4041: .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90()
4042: @*/
4043: PetscErrorCode  MatSeqAIJGetMaxRowNonzeros(Mat A,PetscInt *nz)
4044: {
4045:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)A->data;

4048:   *nz = aij->rmax;
4049:   return(0);
4050: }

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

4057:    Not Collective

4059:    Input Parameters:
4060: .  mat - a MATSEQAIJ matrix
4061: .  array - pointer to the data

4063:    Level: intermediate

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

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

4078: PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJ(Mat B)
4079: {
4080:   Mat_SeqAIJ     *b;
4082:   PetscMPIInt    size;

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

4088:   PetscNewLog(B,&b);

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

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

4094:   b->row                = 0;
4095:   b->col                = 0;
4096:   b->icol               = 0;
4097:   b->reallocs           = 0;
4098:   b->ignorezeroentries  = PETSC_FALSE;
4099:   b->roworiented        = PETSC_TRUE;
4100:   b->nonew              = 0;
4101:   b->diag               = 0;
4102:   b->solve_work         = 0;
4103:   B->spptr              = 0;
4104:   b->saved_values       = 0;
4105:   b->idiag              = 0;
4106:   b->mdiag              = 0;
4107:   b->ssor_work          = 0;
4108:   b->omega              = 1.0;
4109:   b->fshift             = 0.0;
4110:   b->idiagvalid         = PETSC_FALSE;
4111:   b->ibdiagvalid        = PETSC_FALSE;
4112:   b->keepnonzeropattern = PETSC_FALSE;

4114:   PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);
4115:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJGetArray_C",MatSeqAIJGetArray_SeqAIJ);
4116:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJRestoreArray_C",MatSeqAIJRestoreArray_SeqAIJ);

4118: #if defined(PETSC_HAVE_MATLAB_ENGINE)
4119:   PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_matlab_C",MatGetFactor_seqaij_matlab);
4120:   PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEnginePut_C",MatlabEnginePut_SeqAIJ);
4121:   PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEngineGet_C",MatlabEngineGet_SeqAIJ);
4122: #endif
4123: #if defined(PETSC_HAVE_PASTIX)
4124:   PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_pastix_C",MatGetFactor_seqaij_pastix);
4125: #endif
4126: #if defined(PETSC_HAVE_ESSL) && !defined(PETSC_USE_COMPLEX) && !defined(PETSC_USE_REAL_SINGLE) && !defined(PETSC_USE_REAL___FLOAT128)
4127:   PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_essl_C",MatGetFactor_seqaij_essl);
4128: #endif
4129: #if defined(PETSC_HAVE_SUPERLU)
4130:   PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_superlu_C",MatGetFactor_seqaij_superlu);
4131: #endif
4132: #if defined(PETSC_HAVE_MKL_PARDISO)
4133:   PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_mkl_pardiso_C",MatGetFactor_aij_mkl_pardiso);
4134: #endif
4135: #if defined(PETSC_HAVE_SUPERLU_DIST)
4136:   PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_superlu_dist_C",MatGetFactor_seqaij_superlu_dist);
4137: #endif
4138: #if defined(PETSC_HAVE_MUMPS)
4139:   PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_mumps_C",MatGetFactor_aij_mumps);
4140: #endif
4141: #if defined(PETSC_HAVE_SUITESPARSE)
4142:   PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_umfpack_C",MatGetFactor_seqaij_umfpack);
4143: #endif
4144: #if defined(PETSC_HAVE_SUITESPARSE)
4145:   PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_cholmod_C",MatGetFactor_seqaij_cholmod);
4146: #endif
4147: #if defined(PETSC_HAVE_SUITESPARSE)
4148:   PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_klu_C",MatGetFactor_seqaij_klu);
4149: #endif
4150: #if defined(PETSC_HAVE_LUSOL)
4151:   PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_lusol_C",MatGetFactor_seqaij_lusol);
4152: #endif
4153: #if defined(PETSC_HAVE_CLIQUE)
4154:   PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_clique_C",MatGetFactor_aij_clique);
4155: #endif

4157:   PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_petsc_C",MatGetFactor_seqaij_petsc);
4158:   PetscObjectComposeFunction((PetscObject)B,"MatGetFactorAvailable_petsc_C",MatGetFactorAvailable_seqaij_petsc);
4159:   PetscObjectComposeFunction((PetscObject)B,"MatGetFactor_bas_C",MatGetFactor_seqaij_bas);
4160:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetColumnIndices_C",MatSeqAIJSetColumnIndices_SeqAIJ);
4161:   PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_SeqAIJ);
4162:   PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_SeqAIJ);
4163:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",MatConvert_SeqAIJ_SeqSBAIJ);
4164:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqbaij_C",MatConvert_SeqAIJ_SeqBAIJ);
4165:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijperm_C",MatConvert_SeqAIJ_SeqAIJPERM);
4166:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijcrl_C",MatConvert_SeqAIJ_SeqAIJCRL);
4167: #if defined(PETSC_HAVE_ELEMENTAL)
4168:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_elemental_C",MatConvert_SeqAIJ_Elemental);
4169: #endif
4170:   PetscObjectComposeFunction((PetscObject)B,"MatIsTranspose_C",MatIsTranspose_SeqAIJ);
4171:   PetscObjectComposeFunction((PetscObject)B,"MatIsHermitianTranspose_C",MatIsTranspose_SeqAIJ);
4172:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",MatSeqAIJSetPreallocation_SeqAIJ);
4173:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",MatSeqAIJSetPreallocationCSR_SeqAIJ);
4174:   PetscObjectComposeFunction((PetscObject)B,"MatReorderForNonzeroDiagonal_C",MatReorderForNonzeroDiagonal_SeqAIJ);
4175:   PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqdense_seqaij_C",MatMatMult_SeqDense_SeqAIJ);
4176:   PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C",MatMatMultSymbolic_SeqDense_SeqAIJ);
4177:   PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C",MatMatMultNumeric_SeqDense_SeqAIJ);
4178:   MatCreate_SeqAIJ_Inode(B);
4179:   PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);
4180:   return(0);
4181: }

4185: /*
4186:     Given a matrix generated with MatGetFactor() duplicates all the information in A into B
4187: */
4188: PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscBool mallocmatspace)
4189: {
4190:   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
4192:   PetscInt       i,m = A->rmap->n;

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

4197:   C->factortype = A->factortype;
4198:   c->row        = 0;
4199:   c->col        = 0;
4200:   c->icol       = 0;
4201:   c->reallocs   = 0;

4203:   C->assembled = PETSC_TRUE;

4205:   PetscLayoutReference(A->rmap,&C->rmap);
4206:   PetscLayoutReference(A->cmap,&C->cmap);

4208:   PetscMalloc2(m,&c->imax,m,&c->ilen);
4209:   PetscLogObjectMemory((PetscObject)C, 2*m*sizeof(PetscInt));
4210:   for (i=0; i<m; i++) {
4211:     c->imax[i] = a->imax[i];
4212:     c->ilen[i] = a->ilen[i];
4213:   }

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

4220:     c->singlemalloc = PETSC_TRUE;

4222:     PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));
4223:     if (m > 0) {
4224:       PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));
4225:       if (cpvalues == MAT_COPY_VALUES) {
4226:         PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));
4227:       } else {
4228:         PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));
4229:       }
4230:     }
4231:   }

4233:   c->ignorezeroentries = a->ignorezeroentries;
4234:   c->roworiented       = a->roworiented;
4235:   c->nonew             = a->nonew;
4236:   if (a->diag) {
4237:     PetscMalloc1((m+1),&c->diag);
4238:     PetscLogObjectMemory((PetscObject)C,(m+1)*sizeof(PetscInt));
4239:     for (i=0; i<m; i++) {
4240:       c->diag[i] = a->diag[i];
4241:     }
4242:   } else c->diag = 0;

4244:   c->solve_work         = 0;
4245:   c->saved_values       = 0;
4246:   c->idiag              = 0;
4247:   c->ssor_work          = 0;
4248:   c->keepnonzeropattern = a->keepnonzeropattern;
4249:   c->free_a             = PETSC_TRUE;
4250:   c->free_ij            = PETSC_TRUE;

4252:   c->rmax         = a->rmax;
4253:   c->nz           = a->nz;
4254:   c->maxnz        = a->nz;       /* Since we allocate exactly the right amount */
4255:   C->preallocated = PETSC_TRUE;

4257:   c->compressedrow.use   = a->compressedrow.use;
4258:   c->compressedrow.nrows = a->compressedrow.nrows;
4259:   if (a->compressedrow.use) {
4260:     i    = a->compressedrow.nrows;
4261:     PetscMalloc2(i+1,&c->compressedrow.i,i,&c->compressedrow.rindex);
4262:     PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));
4263:     PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));
4264:   } else {
4265:     c->compressedrow.use    = PETSC_FALSE;
4266:     c->compressedrow.i      = NULL;
4267:     c->compressedrow.rindex = NULL;
4268:   }
4269:   C->nonzerostate = A->nonzerostate;

4271:   MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);
4272:   PetscFunctionListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);
4273:   return(0);
4274: }

4278: PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
4279: {

4283:   MatCreate(PetscObjectComm((PetscObject)A),B);
4284:   MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);
4285:   if (!(A->rmap->n % A->rmap->bs) && !(A->cmap->n % A->cmap->bs)) {
4286:     MatSetBlockSizesFromMats(*B,A,A);
4287:   }
4288:   MatSetType(*B,((PetscObject)A)->type_name);
4289:   MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);
4290:   return(0);
4291: }

4295: PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer)
4296: {
4297:   Mat_SeqAIJ     *a;
4299:   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N,rows,cols;
4300:   int            fd;
4301:   PetscMPIInt    size;
4302:   MPI_Comm       comm;
4303:   PetscInt       bs = 1;

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

4310:   PetscOptionsBegin(comm,NULL,"Options for loading SEQAIJ matrix","Mat");
4311:   PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,NULL);
4312:   PetscOptionsEnd();
4313:   if (bs > 1) {MatSetBlockSize(newMat,bs);}

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

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

4322:   /* read in row lengths */
4323:   PetscMalloc1(M,&rowlengths);
4324:   PetscBinaryRead(fd,rowlengths,M,PETSC_INT);

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

4330:   /* set global size if not set already*/
4331:   if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) {
4332:     MatSetSizes(newMat,PETSC_DECIDE,PETSC_DECIDE,M,N);
4333:   } else {
4334:     /* if sizes and type are already set, check if the vector global sizes are correct */
4335:     MatGetSize(newMat,&rows,&cols);
4336:     if (rows < 0 && cols < 0) { /* user might provide local size instead of global size */
4337:       MatGetLocalSize(newMat,&rows,&cols);
4338:     }
4339:     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);
4340:   }
4341:   MatSeqAIJSetPreallocation_SeqAIJ(newMat,0,rowlengths);
4342:   a    = (Mat_SeqAIJ*)newMat->data;

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

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

4349:   /* set matrix "i" values */
4350:   a->i[0] = 0;
4351:   for (i=1; i<= M; i++) {
4352:     a->i[i]      = a->i[i-1] + rowlengths[i-1];
4353:     a->ilen[i-1] = rowlengths[i-1];
4354:   }
4355:   PetscFree(rowlengths);

4357:   MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);
4358:   MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);
4359:   return(0);
4360: }

4364: PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscBool * flg)
4365: {
4366:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*b = (Mat_SeqAIJ*)B->data;
4368: #if defined(PETSC_USE_COMPLEX)
4369:   PetscInt k;
4370: #endif

4373:   /* If the  matrix dimensions are not equal,or no of nonzeros */
4374:   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
4375:     *flg = PETSC_FALSE;
4376:     return(0);
4377:   }

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

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

4387:   /* if a->a are the same */
4388: #if defined(PETSC_USE_COMPLEX)
4389:   for (k=0; k<a->nz; k++) {
4390:     if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])) {
4391:       *flg = PETSC_FALSE;
4392:       return(0);
4393:     }
4394:   }
4395: #else
4396:   PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);
4397: #endif
4398:   return(0);
4399: }

4403: /*@
4404:      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
4405:               provided by the user.

4407:       Collective on MPI_Comm

4409:    Input Parameters:
4410: +   comm - must be an MPI communicator of size 1
4411: .   m - number of rows
4412: .   n - number of columns
4413: .   i - row indices
4414: .   j - column indices
4415: -   a - matrix values

4417:    Output Parameter:
4418: .   mat - the matrix

4420:    Level: intermediate

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

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

4428:        The i and j indices are 0 based

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

4434:         1 0 0
4435:         2 0 3
4436:         4 5 6

4438:         i =  {0,1,3,6}  [size = nrow+1  = 3+1]
4439:         j =  {0,0,2,0,1,2}  [size = nz = 6]; values must be sorted for each row
4440:         v =  {1,2,3,4,5,6}  [size = nz = 6]


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

4445: @*/
4446: PetscErrorCode  MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt *i,PetscInt *j,PetscScalar *a,Mat *mat)
4447: {
4449:   PetscInt       ii;
4450:   Mat_SeqAIJ     *aij;
4451: #if defined(PETSC_USE_DEBUG)
4452:   PetscInt jj;
4453: #endif

4456:   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
4457:   MatCreate(comm,mat);
4458:   MatSetSizes(*mat,m,n,m,n);
4459:   /* MatSetBlockSizes(*mat,,); */
4460:   MatSetType(*mat,MATSEQAIJ);
4461:   MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);
4462:   aij  = (Mat_SeqAIJ*)(*mat)->data;
4463:   PetscMalloc2(m,&aij->imax,m,&aij->ilen);

4465:   aij->i            = i;
4466:   aij->j            = j;
4467:   aij->a            = a;
4468:   aij->singlemalloc = PETSC_FALSE;
4469:   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
4470:   aij->free_a       = PETSC_FALSE;
4471:   aij->free_ij      = PETSC_FALSE;

4473:   for (ii=0; ii<m; ii++) {
4474:     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
4475: #if defined(PETSC_USE_DEBUG)
4476:     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]);
4477:     for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
4478:       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);
4479:       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);
4480:     }
4481: #endif
4482:   }
4483: #if defined(PETSC_USE_DEBUG)
4484:   for (ii=0; ii<aij->i[m]; ii++) {
4485:     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %D index = %D",ii,j[ii]);
4486:     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]);
4487:   }
4488: #endif

4490:   MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);
4491:   MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);
4492:   return(0);
4493: }
4496: /*@C
4497:      MatCreateSeqAIJFromTriple - Creates an sequential AIJ matrix using matrix elements (in COO format)
4498:               provided by the user.

4500:       Collective on MPI_Comm

4502:    Input Parameters:
4503: +   comm - must be an MPI communicator of size 1
4504: .   m   - number of rows
4505: .   n   - number of columns
4506: .   i   - row indices
4507: .   j   - column indices
4508: .   a   - matrix values
4509: .   nz  - number of nonzeros
4510: -   idx - 0 or 1 based

4512:    Output Parameter:
4513: .   mat - the matrix

4515:    Level: intermediate

4517:    Notes:
4518:        The i and j indices are 0 based

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

4524:         1 0 0
4525:         2 0 3
4526:         4 5 6

4528:         i =  {0,1,1,2,2,2}
4529:         j =  {0,0,2,0,1,2}
4530:         v =  {1,2,3,4,5,6}


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

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


4543:   PetscCalloc1(m,&nnz);
4544:   for (ii = 0; ii < nz; ii++) {
4545:     nnz[i[ii] - !!idx] += 1;
4546:   }
4547:   MatCreate(comm,mat);
4548:   MatSetSizes(*mat,m,n,m,n);
4549:   MatSetType(*mat,MATSEQAIJ);
4550:   MatSeqAIJSetPreallocation_SeqAIJ(*mat,0,nnz);
4551:   for (ii = 0; ii < nz; ii++) {
4552:     if (idx) {
4553:       row = i[ii] - 1;
4554:       col = j[ii] - 1;
4555:     } else {
4556:       row = i[ii];
4557:       col = j[ii];
4558:     }
4559:     MatSetValues(*mat,one,&row,one,&col,&a[ii],ADD_VALUES);
4560:   }
4561:   MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);
4562:   MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);
4563:   PetscFree(nnz);
4564:   return(0);
4565: }

4569: PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
4570: {
4572:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;

4575:   if (coloring->ctype == IS_COLORING_GLOBAL) {
4576:     ISColoringReference(coloring);
4577:     a->coloring = coloring;
4578:   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
4579:     PetscInt        i,*larray;
4580:     ISColoring      ocoloring;
4581:     ISColoringValue *colors;

4583:     /* set coloring for diagonal portion */
4584:     PetscMalloc1(A->cmap->n,&larray);
4585:     for (i=0; i<A->cmap->n; i++) larray[i] = i;
4586:     ISGlobalToLocalMappingApply(A->cmap->mapping,IS_GTOLM_MASK,A->cmap->n,larray,NULL,larray);
4587:     PetscMalloc1(A->cmap->n,&colors);
4588:     for (i=0; i<A->cmap->n; i++) colors[i] = coloring->colors[larray[i]];
4589:     PetscFree(larray);
4590:     ISColoringCreate(PETSC_COMM_SELF,coloring->n,A->cmap->n,colors,&ocoloring);
4591:     a->coloring = ocoloring;
4592:   }
4593:   return(0);
4594: }

4598: PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues)
4599: {
4600:   Mat_SeqAIJ      *a      = (Mat_SeqAIJ*)A->data;
4601:   PetscInt        m       = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j;
4602:   MatScalar       *v      = a->a;
4603:   PetscScalar     *values = (PetscScalar*)advalues;
4604:   ISColoringValue *color;

4607:   if (!a->coloring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
4608:   color = a->coloring->colors;
4609:   /* loop over rows */
4610:   for (i=0; i<m; i++) {
4611:     nz = ii[i+1] - ii[i];
4612:     /* loop over columns putting computed value into matrix */
4613:     for (j=0; j<nz; j++) *v++ = values[color[*jj++]];
4614:     values += nl; /* jump to next row of derivatives */
4615:   }
4616:   return(0);
4617: }

4621: PetscErrorCode MatSeqAIJInvalidateDiagonal(Mat A)
4622: {
4623:   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data;

4627:   a->idiagvalid  = PETSC_FALSE;
4628:   a->ibdiagvalid = PETSC_FALSE;

4630:   MatSeqAIJInvalidateDiagonal_Inode(A);
4631:   return(0);
4632: }

4634: /*
4635:     Special version for direct calls from Fortran
4636: */
4637: #include <petsc-private/fortranimpl.h>
4638: #if defined(PETSC_HAVE_FORTRAN_CAPS)
4639: #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
4640: #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
4641: #define matsetvaluesseqaij_ matsetvaluesseqaij
4642: #endif

4644: /* Change these macros so can be used in void function */
4645: #undef CHKERRQ
4646: #define CHKERRQ(ierr) CHKERRABORT(PetscObjectComm((PetscObject)A),ierr)
4647: #undef SETERRQ2
4648: #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
4649: #undef SETERRQ3
4650: #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr)

4654: 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)
4655: {
4656:   Mat            A  = *AA;
4657:   PetscInt       m  = *mm, n = *nn;
4658:   InsertMode     is = *isis;
4659:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
4660:   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
4661:   PetscInt       *imax,*ai,*ailen;
4663:   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
4664:   MatScalar      *ap,value,*aa;
4665:   PetscBool      ignorezeroentries = a->ignorezeroentries;
4666:   PetscBool      roworiented       = a->roworiented;

4669:   MatCheckPreallocated(A,1);
4670:   imax  = a->imax;
4671:   ai    = a->i;
4672:   ailen = a->ilen;
4673:   aj    = a->j;
4674:   aa    = a->a;

4676:   for (k=0; k<m; k++) { /* loop over added rows */
4677:     row = im[k];
4678:     if (row < 0) continue;
4679: #if defined(PETSC_USE_DEBUG)
4680:     if (row >= A->rmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
4681: #endif
4682:     rp   = aj + ai[row]; ap = aa + ai[row];
4683:     rmax = imax[row]; nrow = ailen[row];
4684:     low  = 0;
4685:     high = nrow;
4686:     for (l=0; l<n; l++) { /* loop over added columns */
4687:       if (in[l] < 0) continue;
4688: #if defined(PETSC_USE_DEBUG)
4689:       if (in[l] >= A->cmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
4690: #endif
4691:       col = in[l];
4692:       if (roworiented) value = v[l + k*n];
4693:       else value = v[k + l*m];

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

4697:       if (col <= lastcol) low = 0;
4698:       else high = nrow;
4699:       lastcol = col;
4700:       while (high-low > 5) {
4701:         t = (low+high)/2;
4702:         if (rp[t] > col) high = t;
4703:         else             low  = t;
4704:       }
4705:       for (i=low; i<high; i++) {
4706:         if (rp[i] > col) break;
4707:         if (rp[i] == col) {
4708:           if (is == ADD_VALUES) ap[i] += value;
4709:           else                  ap[i] = value;
4710:           goto noinsert;
4711:         }
4712:       }
4713:       if (value == 0.0 && ignorezeroentries) goto noinsert;
4714:       if (nonew == 1) goto noinsert;
4715:       if (nonew == -1) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
4716:       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
4717:       N = nrow++ - 1; a->nz++; high++;
4718:       /* shift up all the later entries in this row */
4719:       for (ii=N; ii>=i; ii--) {
4720:         rp[ii+1] = rp[ii];
4721:         ap[ii+1] = ap[ii];
4722:       }
4723:       rp[i] = col;
4724:       ap[i] = value;
4725:       A->nonzerostate++;
4726: noinsert:;
4727:       low = i + 1;
4728:     }
4729:     ailen[row] = nrow;
4730:   }
4731:   PetscFunctionReturnVoid();
4732: }