Actual source code: aij.c

petsc-3.6.0 2015-06-09
Report Typos and Errors
  2: /*
  3:     Defines the basic matrix operations for the AIJ (compressed row)
  4:   matrix storage format.
  5: */


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

385: */

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

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

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

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

442:   for (k=0; k<m; k++) { /* loop over added rows */
443:     row = im[k];
444:     if (row < 0) continue;
445: #if defined(PETSC_USE_DEBUG)
446:     if (row >= A->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,A->rmap->n-1);
447: #endif
448:     rp   = aj + ai[row]; ap = aa + ai[row];
449:     rmax = imax[row]; nrow = ailen[row];
450:     low  = 0;
451:     high = nrow;
452:     for (l=0; l<n; l++) { /* loop over added columns */
453:       if (in[l] < 0) continue;
454: #if defined(PETSC_USE_DEBUG)
455:       if (in[l] >= A->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->cmap->n-1);
456: #endif
457:       col = in[l];
458:       if (roworiented) {
459:         value = v[l + k*n];
460:       } else {
461:         value = v[k + l*m];
462:       }
463:       if ((value == 0.0 && ignorezeroentries) && (is == ADD_VALUES)) continue;

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


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

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


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

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

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

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

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

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

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

584: extern PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);

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

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

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

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

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

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

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

862:   PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);
863:   PetscViewerGetFormat(viewer,&format);

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

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

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

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

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

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

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

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

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

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

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

1007:   /* reset ilen and imax for each row */
1008:   a->nonzerorowcnt = 0;
1009:   for (i=0; i<m; i++) {
1010:     ailen[i] = imax[i] = ai[i+1] - ai[i];
1011:     a->nonzerorowcnt += ((ai[i+1] - ai[i]) > 0);
1012:   }
1013:   a->nz = ai[m];
1014:   if (fshift && a->nounused == -1) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_PLIB, "Unused space detected in matrix: %D X %D, %D unneeded", m, A->cmap->n, fshift);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1406:   VecGetArrayRead(xx,&x);
1407:   VecGetArrayPair(yy,zz,&y,&z);

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

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

1459:   VecGetArrayRead(xx,&x);
1460:   VecGetArrayPair(yy,zz,&y,&z);

1462:   aj = a->j;
1463:   aa = a->a;
1464:   ii = a->i;
1465:   if (usecprow) { /* use compressed row format */
1466:     if (zz != yy) {
1467:       PetscMemcpy(z,y,m*sizeof(PetscScalar));
1468:     }
1469:     m    = a->compressedrow.nrows;
1470:     ii   = a->compressedrow.i;
1471:     ridx = a->compressedrow.rindex;
1472:     for (i=0; i<m; i++) {
1473:       n   = ii[i+1] - ii[i];
1474:       aj  = a->j + ii[i];
1475:       aa  = a->a + ii[i];
1476:       sum = y[*ridx];
1477:       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1478:       z[*ridx++] = sum;
1479:     }
1480:   } else { /* do not use compressed row format */
1481: #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
1482:     fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
1483: #else
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 = y[i];
1489:       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1490:       z[i] = sum;
1491:     }
1492: #endif
1493:   }
1494:   PetscLogFlops(2.0*a->nz);
1495:   VecRestoreArrayRead(xx,&x);
1496:   VecRestoreArrayPair(yy,zz,&y,&z);
1497: #if defined(PETSC_HAVE_CUSP)
1498:   /*
1499:   VecView(xx,0);
1500:   VecView(zz,0);
1501:   MatView(A,0);
1502:   */
1503: #endif
1504:   return(0);
1505: }

1507: /*
1508:      Adds diagonal pointers to sparse matrix structure.
1509: */
1512: PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
1513: {
1514:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1516:   PetscInt       i,j,m = A->rmap->n;

1519:   if (!a->diag) {
1520:     PetscMalloc1(m,&a->diag);
1521:     PetscLogObjectMemory((PetscObject)A, m*sizeof(PetscInt));
1522:   }
1523:   for (i=0; i<A->rmap->n; i++) {
1524:     a->diag[i] = a->i[i+1];
1525:     for (j=a->i[i]; j<a->i[i+1]; j++) {
1526:       if (a->j[j] == i) {
1527:         a->diag[i] = j;
1528:         break;
1529:       }
1530:     }
1531:   }
1532:   return(0);
1533: }

1535: /*
1536:      Checks for missing diagonals
1537: */
1540: PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscBool  *missing,PetscInt *d)
1541: {
1542:   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1543:   PetscInt   *diag,*ii = a->i,i;

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

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

1579:   if (a->idiagvalid) return(0);
1580:   MatMarkDiagonal_SeqAIJ(A);
1581:   diag = a->diag;
1582:   if (!a->idiag) {
1583:     PetscMalloc3(m,&a->idiag,m,&a->mdiag,m,&a->ssor_work);
1584:     PetscLogObjectMemory((PetscObject)A, 3*m*sizeof(PetscScalar));
1585:     v    = a->a;
1586:   }
1587:   mdiag = a->mdiag;
1588:   idiag = a->idiag;

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

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

1622:   its = its*lits;

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1999:   /* Allocate space for symbolic transpose info and work array */
2000:   PetscCalloc1(an+1,&ati);
2001:   PetscMalloc1(ai[am],&atj);
2002:   PetscMalloc1(an,&atfill);

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

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

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

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

2027:   b          = (Mat_SeqAIJ*)((*B)->data);
2028:   b->free_a  = PETSC_FALSE;
2029:   b->free_ij = PETSC_TRUE;
2030:   b->nonew   = 0;
2031:   return(0);
2032: }

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

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

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

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

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

2070:   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
2071:     *B = C;
2072:   } else {
2073:     MatHeaderMerge(A,C);
2074:   }
2075:   return(0);
2076: }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

2447:   outA             = inA;
2448:   outA->factortype = MAT_FACTOR_LU;

2450:   PetscObjectReference((PetscObject)row);
2451:   ISDestroy(&a->row);

2453:   a->row = row;

2455:   PetscObjectReference((PetscObject)col);
2456:   ISDestroy(&a->col);

2458:   a->col = col;

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

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

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

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

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

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

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

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

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

2526:   m  = A->rmap->n;
2527:   ai = a->i;
2528:   aj = a->j;

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

2532:   PetscMalloc1(m+1,&nidx);
2533:   PetscBTCreate(m,&table);

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

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

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

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

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

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

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

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

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

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

2623: PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
2624: {

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

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

2643: PetscErrorCode MatSetUp_SeqAIJ(Mat A)
2644: {

2648:    MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);
2649:   return(0);
2650: }

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

2659:   *array = a->a;
2660:   return(0);
2661: }

2665: PetscErrorCode MatSeqAIJRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
2666: {
2668:   return(0);
2669: }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2939: #include <petscblaslapack.h>
2940: #include <petsc/private/kernels/blockinvert.h>

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

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

3045: static PetscErrorCode  MatSetRandom_SeqAIJ(Mat x,PetscRandom rctx)
3046: {
3048:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)x->data;
3049:   PetscScalar    a;
3050:   PetscInt       m,n,i,j,col;

3053:   if (!x->assembled) {
3054:     MatGetSize(x,&m,&n);
3055:     for (i=0; i<m; i++) {
3056:       for (j=0; j<aij->imax[i]; j++) {
3057:         PetscRandomGetValue(rctx,&a);
3058:         col  = (PetscInt)(n*PetscRealPart(a));
3059:         MatSetValues(x,1,&i,1,&col,&a,ADD_VALUES);
3060:       }
3061:     }
3062:   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Not yet coded");
3063:   MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);
3064:   MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);
3065:   return(0);
3066: }

3070: PetscErrorCode MatShift_SeqAIJ(Mat Y,PetscScalar a)
3071: {
3073:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)Y->data;

3076:   if (!aij->nz) {
3077:     MatSeqAIJSetPreallocation(Y,1,NULL);
3078:   }
3079:   MatShift_Basic(Y,a);
3080:   return(0);
3081: }

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

3233: PetscErrorCode  MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
3234: {
3235:   Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data;
3236:   PetscInt   i,nz,n;

3239:   nz = aij->maxnz;
3240:   n  = mat->rmap->n;
3241:   for (i=0; i<nz; i++) {
3242:     aij->j[i] = indices[i];
3243:   }
3244:   aij->nz = nz;
3245:   for (i=0; i<n; i++) {
3246:     aij->ilen[i] = aij->imax[i];
3247:   }
3248:   return(0);
3249: }

3253: /*@
3254:     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
3255:        in the matrix.

3257:   Input Parameters:
3258: +  mat - the SeqAIJ matrix
3259: -  indices - the column indices

3261:   Level: advanced

3263:   Notes:
3264:     This can be called if you have precomputed the nonzero structure of the
3265:   matrix and want to provide it to the matrix object to improve the performance
3266:   of the MatSetValues() operation.

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

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

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

3275: @*/
3276: PetscErrorCode  MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
3277: {

3283:   PetscUseMethod(mat,"MatSeqAIJSetColumnIndices_C",(Mat,PetscInt*),(mat,indices));
3284:   return(0);
3285: }

3287: /* ----------------------------------------------------------------------------------------*/

3291: PetscErrorCode  MatStoreValues_SeqAIJ(Mat mat)
3292: {
3293:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)mat->data;
3295:   size_t         nz = aij->i[mat->rmap->n];

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

3300:   /* allocate space for values if not already there */
3301:   if (!aij->saved_values) {
3302:     PetscMalloc1(nz+1,&aij->saved_values);
3303:     PetscLogObjectMemory((PetscObject)mat,(nz+1)*sizeof(PetscScalar));
3304:   }

3306:   /* copy values over */
3307:   PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));
3308:   return(0);
3309: }

3313: /*@
3314:     MatStoreValues - Stashes a copy of the matrix values; this allows, for
3315:        example, reuse of the linear part of a Jacobian, while recomputing the
3316:        nonlinear portion.

3318:    Collect on Mat

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

3323:   Level: advanced

3325:   Common Usage, with SNESSolve():
3326: $    Create Jacobian matrix
3327: $    Set linear terms into matrix
3328: $    Apply boundary conditions to matrix, at this time matrix must have
3329: $      final nonzero structure (i.e. setting the nonlinear terms and applying
3330: $      boundary conditions again will not change the nonzero structure
3331: $    MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3332: $    MatStoreValues(mat);
3333: $    Call SNESSetJacobian() with matrix
3334: $    In your Jacobian routine
3335: $      MatRetrieveValues(mat);
3336: $      Set nonlinear terms in matrix

3338:   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
3339: $    // build linear portion of Jacobian
3340: $    MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3341: $    MatStoreValues(mat);
3342: $    loop over nonlinear iterations
3343: $       MatRetrieveValues(mat);
3344: $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
3345: $       // call MatAssemblyBegin/End() on matrix
3346: $       Solve linear system with Jacobian
3347: $    endloop

3349:   Notes:
3350:     Matrix must already be assemblied before calling this routine
3351:     Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
3352:     calling this routine.

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

3357: .seealso: MatRetrieveValues()

3359: @*/
3360: PetscErrorCode  MatStoreValues(Mat mat)
3361: {

3366:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3367:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3368:   PetscUseMethod(mat,"MatStoreValues_C",(Mat),(mat));
3369:   return(0);
3370: }

3374: PetscErrorCode  MatRetrieveValues_SeqAIJ(Mat mat)
3375: {
3376:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)mat->data;
3378:   PetscInt       nz = aij->i[mat->rmap->n];

3381:   if (!aij->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3382:   if (!aij->saved_values) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
3383:   /* copy values over */
3384:   PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));
3385:   return(0);
3386: }

3390: /*@
3391:     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
3392:        example, reuse of the linear part of a Jacobian, while recomputing the
3393:        nonlinear portion.

3395:    Collect on Mat

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

3400:   Level: advanced

3402: .seealso: MatStoreValues()

3404: @*/
3405: PetscErrorCode  MatRetrieveValues(Mat mat)
3406: {

3411:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3412:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3413:   PetscUseMethod(mat,"MatRetrieveValues_C",(Mat),(mat));
3414:   return(0);
3415: }


3418: /* --------------------------------------------------------------------------------*/
3421: /*@C
3422:    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
3423:    (the default parallel PETSc format).  For good matrix assembly performance
3424:    the user should preallocate the matrix storage by setting the parameter nz
3425:    (or the array nnz).  By setting these parameters accurately, performance
3426:    during matrix assembly can be increased by more than a factor of 50.

3428:    Collective on MPI_Comm

3430:    Input Parameters:
3431: +  comm - MPI communicator, set to PETSC_COMM_SELF
3432: .  m - number of rows
3433: .  n - number of columns
3434: .  nz - number of nonzeros per row (same for all rows)
3435: -  nnz - array containing the number of nonzeros in the various rows
3436:          (possibly different for each row) or NULL

3438:    Output Parameter:
3439: .  A - the matrix

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

3445:    Notes:
3446:    If nnz is given then nz is ignored

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

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

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

3463:    Options Database Keys:
3464: +  -mat_no_inode  - Do not use inodes
3465: -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)

3467:    Level: intermediate

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

3471: @*/
3472: PetscErrorCode  MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
3473: {

3477:   MatCreate(comm,A);
3478:   MatSetSizes(*A,m,n,m,n);
3479:   MatSetType(*A,MATSEQAIJ);
3480:   MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);
3481:   return(0);
3482: }

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

3492:    Collective on MPI_Comm

3494:    Input Parameters:
3495: +  B - The matrix
3496: .  nz - number of nonzeros per row (same for all rows)
3497: -  nnz - array containing the number of nonzeros in the various rows
3498:          (possibly different for each row) or NULL

3500:    Notes:
3501:      If nnz is given then nz is ignored

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

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

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

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

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

3526:    Options Database Keys:
3527: +  -mat_no_inode  - Do not use inodes
3528: .  -mat_inode_limit <limit> - Sets inode limit (max limit=5)
3529: -  -mat_aij_oneindex - Internally use indexing starting at 1
3530:         rather than 0.  Note that when calling MatSetValues(),
3531:         the user still MUST index entries starting at 0!

3533:    Level: intermediate

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

3537: @*/
3538: PetscErrorCode  MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
3539: {

3545:   PetscTryMethod(B,"MatSeqAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[]),(B,nz,nnz));
3546:   return(0);
3547: }

3551: PetscErrorCode  MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,const PetscInt *nnz)
3552: {
3553:   Mat_SeqAIJ     *b;
3554:   PetscBool      skipallocation = PETSC_FALSE,realalloc = PETSC_FALSE;
3556:   PetscInt       i;

3559:   if (nz >= 0 || nnz) realalloc = PETSC_TRUE;
3560:   if (nz == MAT_SKIP_ALLOCATION) {
3561:     skipallocation = PETSC_TRUE;
3562:     nz             = 0;
3563:   }

3565:   PetscLayoutSetUp(B->rmap);
3566:   PetscLayoutSetUp(B->cmap);

3568:   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
3569:   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %D",nz);
3570:   if (nnz) {
3571:     for (i=0; i<B->rmap->n; i++) {
3572:       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]);
3573:       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);
3574:     }
3575:   }

3577:   B->preallocated = PETSC_TRUE;

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

3581:   if (!skipallocation) {
3582:     if (!b->imax) {
3583:       PetscMalloc2(B->rmap->n,&b->imax,B->rmap->n,&b->ilen);
3584:       PetscLogObjectMemory((PetscObject)B,2*B->rmap->n*sizeof(PetscInt));
3585:     }
3586:     if (!nnz) {
3587:       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
3588:       else if (nz < 0) nz = 1;
3589:       for (i=0; i<B->rmap->n; i++) b->imax[i] = nz;
3590:       nz = nz*B->rmap->n;
3591:     } else {
3592:       nz = 0;
3593:       for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
3594:     }
3595:     /* b->ilen will count nonzeros in each row so far. */
3596:     for (i=0; i<B->rmap->n; i++) b->ilen[i] = 0;

3598:     /* allocate the matrix space */
3599:     /* FIXME: should B's old memory be unlogged? */
3600:     MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);
3601:     PetscMalloc3(nz,&b->a,nz,&b->j,B->rmap->n+1,&b->i);
3602:     PetscLogObjectMemory((PetscObject)B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));
3603:     b->i[0] = 0;
3604:     for (i=1; i<B->rmap->n+1; i++) {
3605:       b->i[i] = b->i[i-1] + b->imax[i-1];
3606:     }
3607:     b->singlemalloc = PETSC_TRUE;
3608:     b->free_a       = PETSC_TRUE;
3609:     b->free_ij      = PETSC_TRUE;
3610:   } else {
3611:     b->free_a  = PETSC_FALSE;
3612:     b->free_ij = PETSC_FALSE;
3613:   }

3615:   b->nz               = 0;
3616:   b->maxnz            = nz;
3617:   B->info.nz_unneeded = (double)b->maxnz;
3618:   if (realalloc) {
3619:     MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);
3620:   }
3621:   return(0);
3622: }

3624: #undef  __FUNCT__
3626: /*@
3627:    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.

3629:    Input Parameters:
3630: +  B - the matrix
3631: .  i - the indices into j for the start of each row (starts with zero)
3632: .  j - the column indices for each row (starts with zero) these must be sorted for each row
3633: -  v - optional values in the matrix

3635:    Level: developer

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

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

3641: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), SeqAIJ
3642: @*/
3643: PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
3644: {

3650:   PetscTryMethod(B,"MatSeqAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));
3651:   return(0);
3652: }

3654: #undef  __FUNCT__
3656: PetscErrorCode  MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
3657: {
3658:   PetscInt       i;
3659:   PetscInt       m,n;
3660:   PetscInt       nz;
3661:   PetscInt       *nnz, nz_max = 0;
3662:   PetscScalar    *values;

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

3668:   PetscLayoutSetUp(B->rmap);
3669:   PetscLayoutSetUp(B->cmap);

3671:   MatGetSize(B, &m, &n);
3672:   PetscMalloc1(m+1, &nnz);
3673:   for (i = 0; i < m; i++) {
3674:     nz     = Ii[i+1]- Ii[i];
3675:     nz_max = PetscMax(nz_max, nz);
3676:     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
3677:     nnz[i] = nz;
3678:   }
3679:   MatSeqAIJSetPreallocation(B, 0, nnz);
3680:   PetscFree(nnz);

3682:   if (v) {
3683:     values = (PetscScalar*) v;
3684:   } else {
3685:     PetscCalloc1(nz_max, &values);
3686:   }

3688:   for (i = 0; i < m; i++) {
3689:     nz   = Ii[i+1] - Ii[i];
3690:     MatSetValues_SeqAIJ(B, 1, &i, nz, J+Ii[i], values + (v ? Ii[i] : 0), INSERT_VALUES);
3691:   }

3693:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
3694:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);

3696:   if (!v) {
3697:     PetscFree(values);
3698:   }
3699:   MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
3700:   return(0);
3701: }

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

3708: /*
3709:     Computes (B'*A')' since computing B*A directly is untenable

3711:                n                       p                          p
3712:         (              )       (              )         (                  )
3713:       m (      A       )  *  n (       B      )   =   m (         C        )
3714:         (              )       (              )         (                  )

3716: */
3717: PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
3718: {
3719:   PetscErrorCode    ierr;
3720:   Mat_SeqDense      *sub_a = (Mat_SeqDense*)A->data;
3721:   Mat_SeqAIJ        *sub_b = (Mat_SeqAIJ*)B->data;
3722:   Mat_SeqDense      *sub_c = (Mat_SeqDense*)C->data;
3723:   PetscInt          i,n,m,q,p;
3724:   const PetscInt    *ii,*idx;
3725:   const PetscScalar *b,*a,*a_q;
3726:   PetscScalar       *c,*c_q;

3729:   m    = A->rmap->n;
3730:   n    = A->cmap->n;
3731:   p    = B->cmap->n;
3732:   a    = sub_a->v;
3733:   b    = sub_b->a;
3734:   c    = sub_c->v;
3735:   PetscMemzero(c,m*p*sizeof(PetscScalar));

3737:   ii  = sub_b->i;
3738:   idx = sub_b->j;
3739:   for (i=0; i<n; i++) {
3740:     q = ii[i+1] - ii[i];
3741:     while (q-->0) {
3742:       c_q = c + m*(*idx);
3743:       a_q = a + m*i;
3744:       PetscKernelAXPY(c_q,*b,a_q,m);
3745:       idx++;
3746:       b++;
3747:     }
3748:   }
3749:   return(0);
3750: }

3754: PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
3755: {
3757:   PetscInt       m=A->rmap->n,n=B->cmap->n;
3758:   Mat            Cmat;

3761:   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);
3762:   MatCreate(PetscObjectComm((PetscObject)A),&Cmat);
3763:   MatSetSizes(Cmat,m,n,m,n);
3764:   MatSetBlockSizesFromMats(Cmat,A,B);
3765:   MatSetType(Cmat,MATSEQDENSE);
3766:   MatSeqDenseSetPreallocation(Cmat,NULL);

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

3770:   *C = Cmat;
3771:   return(0);
3772: }

3774: /* ----------------------------------------------------------------*/
3777: PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
3778: {

3782:   if (scall == MAT_INITIAL_MATRIX) {
3783:     PetscLogEventBegin(MAT_MatMultSymbolic,A,B,0,0);
3784:     MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);
3785:     PetscLogEventEnd(MAT_MatMultSymbolic,A,B,0,0);
3786:   }
3787:   PetscLogEventBegin(MAT_MatMultNumeric,A,B,0,0);
3788:   MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);
3789:   PetscLogEventEnd(MAT_MatMultNumeric,A,B,0,0);
3790:   return(0);
3791: }


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

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

3801:   Level: beginner

3803: .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
3804: M*/

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

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

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

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

3821:   Level: beginner

3823: .seealso: MatCreateAIJ(), MatCreateSeqAIJ(), MATSEQAIJ,MATMPIAIJ
3824: M*/

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

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

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

3838:   Level: beginner

3840: .seealso: MatCreateMPIAIJCRL,MATSEQAIJCRL,MATMPIAIJCRL, MATSEQAIJCRL, MATMPIAIJCRL
3841: M*/

3843: PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCRL(Mat,MatType,MatReuse,Mat*);
3844: #if defined(PETSC_HAVE_ELEMENTAL)
3845: PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_Elemental(Mat,MatType,MatReuse,Mat*);
3846: #endif
3847: PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_SeqDense(Mat,MatType,MatReuse,Mat*);

3849: #if defined(PETSC_HAVE_MATLAB_ENGINE)
3850: PETSC_EXTERN PetscErrorCode  MatlabEnginePut_SeqAIJ(PetscObject,void*);
3851: PETSC_EXTERN PetscErrorCode  MatlabEngineGet_SeqAIJ(PetscObject,void*);
3852: #endif


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

3860:    Not Collective

3862:    Input Parameter:
3863: .  mat - a MATSEQAIJ matrix

3865:    Output Parameter:
3866: .   array - pointer to the data

3868:    Level: intermediate

3870: .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90()
3871: @*/
3872: PetscErrorCode  MatSeqAIJGetArray(Mat A,PetscScalar **array)
3873: {

3877:   PetscUseMethod(A,"MatSeqAIJGetArray_C",(Mat,PetscScalar**),(A,array));
3878:   return(0);
3879: }

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

3886:    Not Collective

3888:    Input Parameter:
3889: .  mat - a MATSEQAIJ matrix

3891:    Output Parameter:
3892: .   nz - the maximum number of nonzeros in any row

3894:    Level: intermediate

3896: .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90()
3897: @*/
3898: PetscErrorCode  MatSeqAIJGetMaxRowNonzeros(Mat A,PetscInt *nz)
3899: {
3900:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)A->data;

3903:   *nz = aij->rmax;
3904:   return(0);
3905: }

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

3912:    Not Collective

3914:    Input Parameters:
3915: .  mat - a MATSEQAIJ matrix
3916: .  array - pointer to the data

3918:    Level: intermediate

3920: .seealso: MatSeqAIJGetArray(), MatSeqAIJRestoreArrayF90()
3921: @*/
3922: PetscErrorCode  MatSeqAIJRestoreArray(Mat A,PetscScalar **array)
3923: {

3927:   PetscUseMethod(A,"MatSeqAIJRestoreArray_C",(Mat,PetscScalar**),(A,array));
3928:   return(0);
3929: }

3933: PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJ(Mat B)
3934: {
3935:   Mat_SeqAIJ     *b;
3937:   PetscMPIInt    size;

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

3943:   PetscNewLog(B,&b);

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

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

3949:   b->row                = 0;
3950:   b->col                = 0;
3951:   b->icol               = 0;
3952:   b->reallocs           = 0;
3953:   b->ignorezeroentries  = PETSC_FALSE;
3954:   b->roworiented        = PETSC_TRUE;
3955:   b->nonew              = 0;
3956:   b->diag               = 0;
3957:   b->solve_work         = 0;
3958:   B->spptr              = 0;
3959:   b->saved_values       = 0;
3960:   b->idiag              = 0;
3961:   b->mdiag              = 0;
3962:   b->ssor_work          = 0;
3963:   b->omega              = 1.0;
3964:   b->fshift             = 0.0;
3965:   b->idiagvalid         = PETSC_FALSE;
3966:   b->ibdiagvalid        = PETSC_FALSE;
3967:   b->keepnonzeropattern = PETSC_FALSE;

3969:   PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);
3970:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJGetArray_C",MatSeqAIJGetArray_SeqAIJ);
3971:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJRestoreArray_C",MatSeqAIJRestoreArray_SeqAIJ);

3973: #if defined(PETSC_HAVE_MATLAB_ENGINE)
3974:   PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEnginePut_C",MatlabEnginePut_SeqAIJ);
3975:   PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEngineGet_C",MatlabEngineGet_SeqAIJ);
3976: #endif

3978:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetColumnIndices_C",MatSeqAIJSetColumnIndices_SeqAIJ);
3979:   PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_SeqAIJ);
3980:   PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_SeqAIJ);
3981:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",MatConvert_SeqAIJ_SeqSBAIJ);
3982:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqbaij_C",MatConvert_SeqAIJ_SeqBAIJ);
3983:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijperm_C",MatConvert_SeqAIJ_SeqAIJPERM);
3984:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijcrl_C",MatConvert_SeqAIJ_SeqAIJCRL);
3985: #if defined(PETSC_HAVE_ELEMENTAL)
3986:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_elemental_C",MatConvert_SeqAIJ_Elemental);
3987: #endif
3988:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqdense_C",MatConvert_SeqAIJ_SeqDense);
3989:   PetscObjectComposeFunction((PetscObject)B,"MatIsTranspose_C",MatIsTranspose_SeqAIJ);
3990:   PetscObjectComposeFunction((PetscObject)B,"MatIsHermitianTranspose_C",MatIsTranspose_SeqAIJ);
3991:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",MatSeqAIJSetPreallocation_SeqAIJ);
3992:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",MatSeqAIJSetPreallocationCSR_SeqAIJ);
3993:   PetscObjectComposeFunction((PetscObject)B,"MatReorderForNonzeroDiagonal_C",MatReorderForNonzeroDiagonal_SeqAIJ);
3994:   PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqdense_seqaij_C",MatMatMult_SeqDense_SeqAIJ);
3995:   PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C",MatMatMultSymbolic_SeqDense_SeqAIJ);
3996:   PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C",MatMatMultNumeric_SeqDense_SeqAIJ);
3997:   MatCreate_SeqAIJ_Inode(B);
3998:   PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);
3999:   return(0);
4000: }

4004: /*
4005:     Given a matrix generated with MatGetFactor() duplicates all the information in A into B
4006: */
4007: PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscBool mallocmatspace)
4008: {
4009:   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
4011:   PetscInt       i,m = A->rmap->n;

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

4016:   C->factortype = A->factortype;
4017:   c->row        = 0;
4018:   c->col        = 0;
4019:   c->icol       = 0;
4020:   c->reallocs   = 0;

4022:   C->assembled = PETSC_TRUE;

4024:   PetscLayoutReference(A->rmap,&C->rmap);
4025:   PetscLayoutReference(A->cmap,&C->cmap);

4027:   PetscMalloc2(m,&c->imax,m,&c->ilen);
4028:   PetscLogObjectMemory((PetscObject)C, 2*m*sizeof(PetscInt));
4029:   for (i=0; i<m; i++) {
4030:     c->imax[i] = a->imax[i];
4031:     c->ilen[i] = a->ilen[i];
4032:   }

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

4039:     c->singlemalloc = PETSC_TRUE;

4041:     PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));
4042:     if (m > 0) {
4043:       PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));
4044:       if (cpvalues == MAT_COPY_VALUES) {
4045:         PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));
4046:       } else {
4047:         PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));
4048:       }
4049:     }
4050:   }

4052:   c->ignorezeroentries = a->ignorezeroentries;
4053:   c->roworiented       = a->roworiented;
4054:   c->nonew             = a->nonew;
4055:   if (a->diag) {
4056:     PetscMalloc1(m+1,&c->diag);
4057:     PetscLogObjectMemory((PetscObject)C,(m+1)*sizeof(PetscInt));
4058:     for (i=0; i<m; i++) {
4059:       c->diag[i] = a->diag[i];
4060:     }
4061:   } else c->diag = 0;

4063:   c->solve_work         = 0;
4064:   c->saved_values       = 0;
4065:   c->idiag              = 0;
4066:   c->ssor_work          = 0;
4067:   c->keepnonzeropattern = a->keepnonzeropattern;
4068:   c->free_a             = PETSC_TRUE;
4069:   c->free_ij            = PETSC_TRUE;

4071:   c->rmax         = a->rmax;
4072:   c->nz           = a->nz;
4073:   c->maxnz        = a->nz;       /* Since we allocate exactly the right amount */
4074:   C->preallocated = PETSC_TRUE;

4076:   c->compressedrow.use   = a->compressedrow.use;
4077:   c->compressedrow.nrows = a->compressedrow.nrows;
4078:   if (a->compressedrow.use) {
4079:     i    = a->compressedrow.nrows;
4080:     PetscMalloc2(i+1,&c->compressedrow.i,i,&c->compressedrow.rindex);
4081:     PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));
4082:     PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));
4083:   } else {
4084:     c->compressedrow.use    = PETSC_FALSE;
4085:     c->compressedrow.i      = NULL;
4086:     c->compressedrow.rindex = NULL;
4087:   }
4088:   c->nonzerorowcnt = a->nonzerorowcnt;
4089:   C->nonzerostate  = A->nonzerostate;

4091:   MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);
4092:   PetscFunctionListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);
4093:   return(0);
4094: }

4098: PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
4099: {

4103:   MatCreate(PetscObjectComm((PetscObject)A),B);
4104:   MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);
4105:   if (!(A->rmap->n % A->rmap->bs) && !(A->cmap->n % A->cmap->bs)) {
4106:     MatSetBlockSizesFromMats(*B,A,A);
4107:   }
4108:   MatSetType(*B,((PetscObject)A)->type_name);
4109:   MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);
4110:   return(0);
4111: }

4115: PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer)
4116: {
4117:   Mat_SeqAIJ     *a;
4119:   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N,rows,cols;
4120:   int            fd;
4121:   PetscMPIInt    size;
4122:   MPI_Comm       comm;
4123:   PetscInt       bs = newMat->rmap->bs;

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

4132:   PetscOptionsBegin(comm,NULL,"Options for loading SEQAIJ matrix","Mat");
4133:   PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,NULL);
4134:   PetscOptionsEnd();
4135:   if (bs < 0) bs = 1;
4136:   MatSetBlockSize(newMat,bs);

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

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

4145:   /* read in row lengths */
4146:   PetscMalloc1(M,&rowlengths);
4147:   PetscBinaryRead(fd,rowlengths,M,PETSC_INT);

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

4153:   /* set global size if not set already*/
4154:   if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) {
4155:     MatSetSizes(newMat,PETSC_DECIDE,PETSC_DECIDE,M,N);
4156:   } else {
4157:     /* if sizes and type are already set, check if the matrix  global sizes are correct */
4158:     MatGetSize(newMat,&rows,&cols);
4159:     if (rows < 0 && cols < 0) { /* user might provide local size instead of global size */
4160:       MatGetLocalSize(newMat,&rows,&cols);
4161:     }
4162:     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);
4163:   }
4164:   MatSeqAIJSetPreallocation_SeqAIJ(newMat,0,rowlengths);
4165:   a    = (Mat_SeqAIJ*)newMat->data;

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

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

4172:   /* set matrix "i" values */
4173:   a->i[0] = 0;
4174:   for (i=1; i<= M; i++) {
4175:     a->i[i]      = a->i[i-1] + rowlengths[i-1];
4176:     a->ilen[i-1] = rowlengths[i-1];
4177:   }
4178:   PetscFree(rowlengths);

4180:   MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);
4181:   MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);
4182:   return(0);
4183: }

4187: PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscBool * flg)
4188: {
4189:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*b = (Mat_SeqAIJ*)B->data;
4191: #if defined(PETSC_USE_COMPLEX)
4192:   PetscInt k;
4193: #endif

4196:   /* If the  matrix dimensions are not equal,or no of nonzeros */
4197:   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
4198:     *flg = PETSC_FALSE;
4199:     return(0);
4200:   }

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

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

4210:   /* if a->a are the same */
4211: #if defined(PETSC_USE_COMPLEX)
4212:   for (k=0; k<a->nz; k++) {
4213:     if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])) {
4214:       *flg = PETSC_FALSE;
4215:       return(0);
4216:     }
4217:   }
4218: #else
4219:   PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);
4220: #endif
4221:   return(0);
4222: }

4226: /*@
4227:      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
4228:               provided by the user.

4230:       Collective on MPI_Comm

4232:    Input Parameters:
4233: +   comm - must be an MPI communicator of size 1
4234: .   m - number of rows
4235: .   n - number of columns
4236: .   i - row indices
4237: .   j - column indices
4238: -   a - matrix values

4240:    Output Parameter:
4241: .   mat - the matrix

4243:    Level: intermediate

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

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

4251:        The i and j indices are 0 based

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

4257:         1 0 0
4258:         2 0 3
4259:         4 5 6

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


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

4268: @*/
4269: PetscErrorCode  MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt *i,PetscInt *j,PetscScalar *a,Mat *mat)
4270: {
4272:   PetscInt       ii;
4273:   Mat_SeqAIJ     *aij;
4274: #if defined(PETSC_USE_DEBUG)
4275:   PetscInt jj;
4276: #endif

4279:   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
4280:   MatCreate(comm,mat);
4281:   MatSetSizes(*mat,m,n,m,n);
4282:   /* MatSetBlockSizes(*mat,,); */
4283:   MatSetType(*mat,MATSEQAIJ);
4284:   MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);
4285:   aij  = (Mat_SeqAIJ*)(*mat)->data;
4286:   PetscMalloc2(m,&aij->imax,m,&aij->ilen);

4288:   aij->i            = i;
4289:   aij->j            = j;
4290:   aij->a            = a;
4291:   aij->singlemalloc = PETSC_FALSE;
4292:   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
4293:   aij->free_a       = PETSC_FALSE;
4294:   aij->free_ij      = PETSC_FALSE;

4296:   for (ii=0; ii<m; ii++) {
4297:     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
4298: #if defined(PETSC_USE_DEBUG)
4299:     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]);
4300:     for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
4301:       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);
4302:       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);
4303:     }
4304: #endif
4305:   }
4306: #if defined(PETSC_USE_DEBUG)
4307:   for (ii=0; ii<aij->i[m]; ii++) {
4308:     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %D index = %D",ii,j[ii]);
4309:     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]);
4310:   }
4311: #endif

4313:   MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);
4314:   MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);
4315:   return(0);
4316: }
4319: /*@C
4320:      MatCreateSeqAIJFromTriple - Creates an sequential AIJ matrix using matrix elements (in COO format)
4321:               provided by the user.

4323:       Collective on MPI_Comm

4325:    Input Parameters:
4326: +   comm - must be an MPI communicator of size 1
4327: .   m   - number of rows
4328: .   n   - number of columns
4329: .   i   - row indices
4330: .   j   - column indices
4331: .   a   - matrix values
4332: .   nz  - number of nonzeros
4333: -   idx - 0 or 1 based

4335:    Output Parameter:
4336: .   mat - the matrix

4338:    Level: intermediate

4340:    Notes:
4341:        The i and j indices are 0 based

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

4347:         1 0 0
4348:         2 0 3
4349:         4 5 6

4351:         i =  {0,1,1,2,2,2}
4352:         j =  {0,0,2,0,1,2}
4353:         v =  {1,2,3,4,5,6}


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

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


4366:   PetscCalloc1(m,&nnz);
4367:   for (ii = 0; ii < nz; ii++) {
4368:     nnz[i[ii] - !!idx] += 1;
4369:   }
4370:   MatCreate(comm,mat);
4371:   MatSetSizes(*mat,m,n,m,n);
4372:   MatSetType(*mat,MATSEQAIJ);
4373:   MatSeqAIJSetPreallocation_SeqAIJ(*mat,0,nnz);
4374:   for (ii = 0; ii < nz; ii++) {
4375:     if (idx) {
4376:       row = i[ii] - 1;
4377:       col = j[ii] - 1;
4378:     } else {
4379:       row = i[ii];
4380:       col = j[ii];
4381:     }
4382:     MatSetValues(*mat,one,&row,one,&col,&a[ii],ADD_VALUES);
4383:   }
4384:   MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);
4385:   MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);
4386:   PetscFree(nnz);
4387:   return(0);
4388: }

4392: PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
4393: {
4395:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;

4398:   if (coloring->ctype == IS_COLORING_GLOBAL) {
4399:     ISColoringReference(coloring);
4400:     a->coloring = coloring;
4401:   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
4402:     PetscInt        i,*larray;
4403:     ISColoring      ocoloring;
4404:     ISColoringValue *colors;

4406:     /* set coloring for diagonal portion */
4407:     PetscMalloc1(A->cmap->n,&larray);
4408:     for (i=0; i<A->cmap->n; i++) larray[i] = i;
4409:     ISGlobalToLocalMappingApply(A->cmap->mapping,IS_GTOLM_MASK,A->cmap->n,larray,NULL,larray);
4410:     PetscMalloc1(A->cmap->n,&colors);
4411:     for (i=0; i<A->cmap->n; i++) colors[i] = coloring->colors[larray[i]];
4412:     PetscFree(larray);
4413:     ISColoringCreate(PETSC_COMM_SELF,coloring->n,A->cmap->n,colors,PETSC_OWN_POINTER,&ocoloring);
4414:     a->coloring = ocoloring;
4415:   }
4416:   return(0);
4417: }

4421: PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues)
4422: {
4423:   Mat_SeqAIJ      *a      = (Mat_SeqAIJ*)A->data;
4424:   PetscInt        m       = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j;
4425:   MatScalar       *v      = a->a;
4426:   PetscScalar     *values = (PetscScalar*)advalues;
4427:   ISColoringValue *color;

4430:   if (!a->coloring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
4431:   color = a->coloring->colors;
4432:   /* loop over rows */
4433:   for (i=0; i<m; i++) {
4434:     nz = ii[i+1] - ii[i];
4435:     /* loop over columns putting computed value into matrix */
4436:     for (j=0; j<nz; j++) *v++ = values[color[*jj++]];
4437:     values += nl; /* jump to next row of derivatives */
4438:   }
4439:   return(0);
4440: }

4444: PetscErrorCode MatSeqAIJInvalidateDiagonal(Mat A)
4445: {
4446:   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data;

4450:   a->idiagvalid  = PETSC_FALSE;
4451:   a->ibdiagvalid = PETSC_FALSE;

4453:   MatSeqAIJInvalidateDiagonal_Inode(A);
4454:   return(0);
4455: }

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

4464:   MatCreateMPIMatConcatenateSeqMat_MPIAIJ(comm,inmat,n,scall,outmat);
4465:   return(0);
4466: }

4468: /*
4469:  Permute A into C's *local* index space using rowemb,colemb.
4470:  The embedding are supposed to be injections and the above implies that the range of rowemb is a subset
4471:  of [0,m), colemb is in [0,n).
4472:  If pattern == DIFFERENT_NONZERO_PATTERN, C is preallocated according to A.
4473:  */
4476: PetscErrorCode MatSetSeqMat_SeqAIJ(Mat C,IS rowemb,IS colemb,MatStructure pattern,Mat B)
4477: {
4478:   /* If making this function public, change the error returned in this function away from _PLIB. */
4480:   Mat_SeqAIJ     *Baij;
4481:   PetscBool      seqaij;
4482:   PetscInt       m,n,*nz,i,j,count;
4483:   PetscScalar    v;
4484:   const PetscInt *rowindices,*colindices;

4487:   if (!B) return(0);
4488:   /* Check to make sure the target matrix (and embeddings) are compatible with C and each other. */
4489:   PetscObjectTypeCompare((PetscObject)B,MATSEQAIJ,&seqaij);
4490:   if (!seqaij) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is of wrong type");
4491:   if (rowemb) {
4492:     ISGetLocalSize(rowemb,&m);
4493:     if (m != B->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Row IS of size %D is incompatible with matrix row size %D",m,B->rmap->n);
4494:   } else {
4495:     if (C->rmap->n != B->rmap->n) {
4496:       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is row-incompatible with the target matrix");
4497:     }
4498:   }
4499:   if (colemb) {
4500:     ISGetLocalSize(colemb,&n);
4501:     if (n != B->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Diag col IS of size %D is incompatible with input matrix col size %D",n,B->cmap->n);
4502:   } else {
4503:     if (C->cmap->n != B->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is col-incompatible with the target matrix");
4504:   }

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


4549: /*
4550:     Special version for direct calls from Fortran
4551: */
4552: #include <petsc/private/fortranimpl.h>
4553: #if defined(PETSC_HAVE_FORTRAN_CAPS)
4554: #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
4555: #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
4556: #define matsetvaluesseqaij_ matsetvaluesseqaij
4557: #endif

4559: /* Change these macros so can be used in void function */
4560: #undef CHKERRQ
4561: #define CHKERRQ(ierr) CHKERRABORT(PetscObjectComm((PetscObject)A),ierr)
4562: #undef SETERRQ2
4563: #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
4564: #undef SETERRQ3
4565: #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr)

4569: 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)
4570: {
4571:   Mat            A  = *AA;
4572:   PetscInt       m  = *mm, n = *nn;
4573:   InsertMode     is = *isis;
4574:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
4575:   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
4576:   PetscInt       *imax,*ai,*ailen;
4578:   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
4579:   MatScalar      *ap,value,*aa;
4580:   PetscBool      ignorezeroentries = a->ignorezeroentries;
4581:   PetscBool      roworiented       = a->roworiented;

4584:   MatCheckPreallocated(A,1);
4585:   imax  = a->imax;
4586:   ai    = a->i;
4587:   ailen = a->ilen;
4588:   aj    = a->j;
4589:   aa    = a->a;

4591:   for (k=0; k<m; k++) { /* loop over added rows */
4592:     row = im[k];
4593:     if (row < 0) continue;
4594: #if defined(PETSC_USE_DEBUG)
4595:     if (row >= A->rmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
4596: #endif
4597:     rp   = aj + ai[row]; ap = aa + ai[row];
4598:     rmax = imax[row]; nrow = ailen[row];
4599:     low  = 0;
4600:     high = nrow;
4601:     for (l=0; l<n; l++) { /* loop over added columns */
4602:       if (in[l] < 0) continue;
4603: #if defined(PETSC_USE_DEBUG)
4604:       if (in[l] >= A->cmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
4605: #endif
4606:       col = in[l];
4607:       if (roworiented) value = v[l + k*n];
4608:       else value = v[k + l*m];

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

4612:       if (col <= lastcol) low = 0;
4613:       else high = nrow;
4614:       lastcol = col;
4615:       while (high-low > 5) {
4616:         t = (low+high)/2;
4617:         if (rp[t] > col) high = t;
4618:         else             low  = t;
4619:       }
4620:       for (i=low; i<high; i++) {
4621:         if (rp[i] > col) break;
4622:         if (rp[i] == col) {
4623:           if (is == ADD_VALUES) ap[i] += value;
4624:           else                  ap[i] = value;
4625:           goto noinsert;
4626:         }
4627:       }
4628:       if (value == 0.0 && ignorezeroentries) goto noinsert;
4629:       if (nonew == 1) goto noinsert;
4630:       if (nonew == -1) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
4631:       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
4632:       N = nrow++ - 1; a->nz++; high++;
4633:       /* shift up all the later entries in this row */
4634:       for (ii=N; ii>=i; ii--) {
4635:         rp[ii+1] = rp[ii];
4636:         ap[ii+1] = ap[ii];
4637:       }
4638:       rp[i] = col;
4639:       ap[i] = value;
4640:       A->nonzerostate++;
4641: noinsert:;
4642:       low = i + 1;
4643:     }
4644:     ailen[row] = nrow;
4645:   }
4646:   PetscFunctionReturnVoid();
4647: }