Actual source code: aij.c

petsc-master 2016-02-12
Report Typos and Errors
  2: /*
  3:     Defines the basic matrix operations for the AIJ (compressed row)
  4:   matrix storage format.
  5: */


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

385: */

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

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

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

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

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

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


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

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


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

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

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

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

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

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

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

584: extern PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);

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

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

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

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

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

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

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

856:   PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);
857:   PetscViewerGetFormat(viewer,&format);

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

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

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

920: #include <petscdraw.h>
923: PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
924: {
926:   PetscDraw      draw;
927:   PetscReal      xr,yr,xl,yl,h,w;
928:   PetscBool      isnull;

931:   PetscViewerDrawGetDraw(viewer,0,&draw);
932:   PetscDrawIsNull(draw,&isnull);
933:   if (isnull) return(0);

935:   PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);
936:   xr   = A->cmap->n; yr = A->rmap->n; h = yr/10.0; w = xr/10.0;
937:   xr  += w;    yr += h;  xl = -w;     yl = -h;
938:   PetscDrawSetCoordinates(draw,xl,yl,xr,yr);
939:   PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);
940:   PetscObjectCompose((PetscObject)A,"Zoomviewer",NULL);
941:   return(0);
942: }

946: PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer)
947: {
949:   PetscBool      iascii,isbinary,isdraw;

952:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
953:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
954:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
955:   if (iascii) {
956:     MatView_SeqAIJ_ASCII(A,viewer);
957:   } else if (isbinary) {
958:     MatView_SeqAIJ_Binary(A,viewer);
959:   } else if (isdraw) {
960:     MatView_SeqAIJ_Draw(A,viewer);
961:   }
962:   MatView_SeqAIJ_Inode(A,viewer);
963:   return(0);
964: }

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

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

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

1001:   /* reset ilen and imax for each row */
1002:   a->nonzerorowcnt = 0;
1003:   for (i=0; i<m; i++) {
1004:     ailen[i] = imax[i] = ai[i+1] - ai[i];
1005:     a->nonzerorowcnt += ((ai[i+1] - ai[i]) > 0);
1006:   }
1007:   a->nz = ai[m];
1008:   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);

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

1015:   A->info.mallocs    += a->reallocs;
1016:   a->reallocs         = 0;
1017:   A->info.nz_unneeded = (PetscReal)fshift;
1018:   a->rmax             = rmax;

1020:   MatCheckCompressedRow(A,a->nonzerorowcnt,&a->compressedrow,a->i,m,ratio);
1021:   MatAssemblyEnd_SeqAIJ_Inode(A,mode);
1022:   MatSeqAIJInvalidateDiagonal(A);
1023:   return(0);
1024: }

1028: PetscErrorCode MatRealPart_SeqAIJ(Mat A)
1029: {
1030:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1031:   PetscInt       i,nz = a->nz;
1032:   MatScalar      *aa = a->a;

1036:   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
1037:   MatSeqAIJInvalidateDiagonal(A);
1038:   return(0);
1039: }

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

1051:   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
1052:   MatSeqAIJInvalidateDiagonal(A);
1053:   return(0);
1054: }

1058: PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
1059: {
1060:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;

1064:   PetscMemzero(a->a,(a->i[A->rmap->n])*sizeof(PetscScalar));
1065:   MatSeqAIJInvalidateDiagonal(A);
1066:   return(0);
1067: }

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

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

1094:   MatDestroy_SeqAIJ_Inode(A);
1095:   PetscFree(A->data);

1097:   PetscObjectChangeTypeName((PetscObject)A,0);
1098:   PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetColumnIndices_C",NULL);
1099:   PetscObjectComposeFunction((PetscObject)A,"MatStoreValues_C",NULL);
1100:   PetscObjectComposeFunction((PetscObject)A,"MatRetrieveValues_C",NULL);
1101:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqsbaij_C",NULL);
1102:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqbaij_C",NULL);
1103:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqaijperm_C",NULL);
1104: #if defined(PETSC_HAVE_ELEMENTAL)
1105:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_elemental_C",NULL);
1106: #endif
1107:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqdense_C",NULL);
1108:   PetscObjectComposeFunction((PetscObject)A,"MatIsTranspose_C",NULL);
1109:   PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocation_C",NULL);
1110:   PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C",NULL);
1111:   PetscObjectComposeFunction((PetscObject)A,"MatReorderForNonzeroDiagonal_C",NULL);
1112:   return(0);
1113: }

1117: PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscBool flg)
1118: {
1119:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;

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

1169: PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
1170: {
1171:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1173:   PetscInt       i,j,n,*ai=a->i,*aj=a->j,nz;
1174:   PetscScalar    *aa=a->a,*x,zero=0.0;

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

1180:   if (A->factortype == MAT_FACTOR_ILU || A->factortype == MAT_FACTOR_LU) {
1181:     PetscInt *diag=a->diag;
1182:     VecGetArray(v,&x);
1183:     for (i=0; i<n; i++) x[i] = 1.0/aa[diag[i]];
1184:     VecRestoreArray(v,&x);
1185:     return(0);
1186:   }

1188:   VecSet(v,zero);
1189:   VecGetArray(v,&x);
1190:   for (i=0; i<n; i++) {
1191:     nz = ai[i+1] - ai[i];
1192:     if (!nz) x[i] = 0.0;
1193:     for (j=ai[i]; j<ai[i+1]; j++) {
1194:       if (aj[j] == i) {
1195:         x[i] = aa[j];
1196:         break;
1197:       }
1198:     }
1199:   }
1200:   VecRestoreArray(v,&x);
1201:   return(0);
1202: }

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

1224:   if (zz != yy) {VecCopy(zz,yy);}
1225:   VecGetArrayRead(xx,&x);
1226:   VecGetArray(yy,&y);

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

1258: PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
1259: {

1263:   VecSet(yy,0.0);
1264:   MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);
1265:   return(0);
1266: }

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

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

1285: #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
1286: #pragma disjoint(*x,*y,*aa)
1287: #endif

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

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

1344: #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
1345: #pragma disjoint(*x,*y,*aa)
1346: #endif

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

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

1400:   VecGetArrayRead(xx,&x);
1401:   VecGetArrayPair(yy,zz,&y,&z);

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

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

1453:   VecGetArrayRead(xx,&x);
1454:   VecGetArrayPair(yy,zz,&y,&z);

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

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

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

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

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

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

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

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

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

1623:   its = its*lits;

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2454:   a->row = row;

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

2459:   a->col = col;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2644: PetscErrorCode MatSetUp_SeqAIJ(Mat A)
2645: {

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3277:   Level: advanced

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

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

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

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

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

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

3303: /* ----------------------------------------------------------------------------------------*/

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

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

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

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

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

3334:    Collect on Mat

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

3339:   Level: advanced

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

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

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

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

3373: .seealso: MatRetrieveValues()

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

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

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

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

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

3411:    Collect on Mat

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

3416:   Level: advanced

3418: .seealso: MatStoreValues()

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

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


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

3444:    Collective on MPI_Comm

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

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

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

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

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

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

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

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

3483:    Level: intermediate

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

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

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

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

3508:    Collective on MPI_Comm

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

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

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

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

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

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

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

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

3549:    Level: intermediate

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

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

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

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

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

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

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

3593:   B->preallocated = PETSC_TRUE;

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

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

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

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

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

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

3651:    Level: developer

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3790: /* ----------------------------------------------------------------*/
3793: PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
3794: {

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


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

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

3817:   Level: beginner

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

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

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

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

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

3837:   Level: beginner

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

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

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

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

3854:   Level: beginner

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

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

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


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

3876:    Not Collective

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

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

3884:    Level: intermediate

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

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

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

3902:    Not Collective

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

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

3910:    Level: intermediate

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

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

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

3928:    Not Collective

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

3934:    Level: intermediate

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

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

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

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

3959:   PetscNewLog(B,&b);

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

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

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

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

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

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

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

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

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

4038:   C->assembled = PETSC_TRUE;

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

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

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

4055:     c->singlemalloc = PETSC_TRUE;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

4246:       Collective on MPI_Comm

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

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

4259:    Level: intermediate

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

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

4267:        The i and j indices are 0 based

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

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


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

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

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

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

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

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

4339:       Collective on MPI_Comm

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

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

4354:    Level: intermediate

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

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

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

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


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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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