Actual source code: aij.c

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


  8: #include <../src/mat/impls/aij/seq/aij.h>          /*I "petscmat.h" I*/
  9: #include <petscblaslapack.h>
 10: #include <petscbt.h>
 11: #include <petsc-private/kernels/blocktranspose.h>
 12: #if defined(PETSC_THREADCOMM_ACTIVE)
 13: #include <petscthreadcomm.h>
 14: #endif

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

388: */

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

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

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

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

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

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


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

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


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

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

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

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

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

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

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

587: extern PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1082: PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
1083: {

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

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

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

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

1131:   MatDestroy_SeqAIJ_Inode(A);
1132:   PetscFree(A->data);

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

1153: PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscBool flg)
1154: {
1155:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;

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

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

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

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

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

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

1258:   if (zz != yy) {VecCopy(zz,yy);}
1259:   VecGetArray(xx,&x);
1260:   VecGetArray(yy,&y);

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

1292: PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
1293: {

1297:   VecSet(yy,0.0);
1298:   MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);
1299:   return(0);
1300: }

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

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

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

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

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

1403: #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
1404: #pragma disjoint(*x,*y,*aa)
1405: #endif

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

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

1467: #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
1468: #pragma disjoint(*x,*y,*aa)
1469: #endif

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

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

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

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

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

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

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

1640: /*
1641:      Adds diagonal pointers to sparse matrix structure.
1642: */
1645: PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
1646: {
1647:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1649:   PetscInt       i,j,m = A->rmap->n;

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

1668: /*
1669:      Checks for missing diagonals
1670: */
1673: PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscBool  *missing,PetscInt *d)
1674: {
1675:   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
1676:   PetscInt   *diag,*ii = a->i,i;

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

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

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

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

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

1752:   its = its*lits;

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2157:   b          = (Mat_SeqAIJ*)((*B)->data);
2158:   b->free_a  = PETSC_FALSE;
2159:   b->free_ij = PETSC_TRUE;
2160:   b->nonew   = 0;
2161:   return(0);
2162: }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2325: PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
2326: {

2330:   MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);
2331:   return(0);
2332: }

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

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

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

2389:   ISSorted(isrow,&sorted);
2390:   if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"ISrow is not sorted");
2391:   ISSorted(iscol,&sorted);
2392:   if (!sorted) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"IScol is not sorted");

2394:   ISGetIndices(isrow,&irow);
2395:   ISGetLocalSize(isrow,&nrows);
2396:   ISGetLocalSize(iscol,&ncols);

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

2445:     /* loop over rows inserting into submatrix */
2446:     a_new = c->a;
2447:     j_new = c->j;
2448:     i_new = c->i;

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

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

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

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

2530:   ISRestoreIndices(isrow,&irow);
2531:   *B   = C;
2532:   return(0);
2533: }

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

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

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

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

2568:   ISIdentity(row,&row_identity);
2569:   ISIdentity(col,&col_identity);

2571:   outA             = inA;
2572:   outA->factortype = MAT_FACTOR_LU;

2574:   PetscObjectReference((PetscObject)row);
2575:   ISDestroy(&a->row);

2577:   a->row = row;

2579:   PetscObjectReference((PetscObject)col);
2580:   ISDestroy(&a->col);

2582:   a->col = col;

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

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

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

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

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

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

2628:   if (scall == MAT_INITIAL_MATRIX) {
2629:     PetscMalloc1(n+1,B);
2630:   }

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

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

2650:   m  = A->rmap->n;
2651:   ai = a->i;
2652:   aj = a->j;

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

2656:   PetscMalloc1(m+1,&nidx);
2657:   PetscBTCreate(m,&table);

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

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

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

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

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

2710:   ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);
2711:   ISGetIndices(irowp,&row);
2712:   ISInvertPermutation(colp,PETSC_DECIDE,&icolp);
2713:   ISGetIndices(icolp,&col);

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

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

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

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

2747: PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
2748: {

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

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

2767: PetscErrorCode MatSetUp_SeqAIJ(Mat A)
2768: {

2772:    MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);
2773:   return(0);
2774: }

2778: PetscErrorCode MatSeqAIJGetArray_SeqAIJ(Mat A,PetscScalar *array[])
2779: {
2780:   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;

2783:   *array = a->a;
2784:   return(0);
2785: }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3369:   Level: advanced

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

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

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

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

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

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

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

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

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

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

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

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

3426:    Collect on Mat

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

3431:   Level: advanced

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

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

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

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

3465: .seealso: MatRetrieveValues()

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

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

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

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

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

3503:    Collect on Mat

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

3508:   Level: advanced

3510: .seealso: MatStoreValues()

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

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


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

3536:    Collective on MPI_Comm

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

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

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

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

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

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

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

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

3575:    Level: intermediate

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

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

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

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

3600:    Collective on MPI_Comm

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

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

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

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

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

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

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

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

3641:    Level: intermediate

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

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

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

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

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

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

3676:   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
3677:   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %D",nz);
3678:   if (nnz) {
3679:     for (i=0; i<B->rmap->n; i++) {
3680:       if (nnz[i] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be less than 0: local row %D value %D",i,nnz[i]);
3681:       if (nnz[i] > B->cmap->n) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be greater than row length: local row %D value %d rowlength %D",i,nnz[i],B->cmap->n);
3682:     }
3683:   }

3685:   B->preallocated = PETSC_TRUE;

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

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

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

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

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

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

3745:    Level: developer

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

3871:   if (A->cmap->n != B->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"A->cmap->n %D != B->rmap->n %D\n",A->cmap->n,B->rmap->n);
3872:   MatCreate(PetscObjectComm((PetscObject)A),&Cmat);
3873:   MatSetSizes(Cmat,m,n,m,n);
3874:   MatSetBlockSizesFromMats(Cmat,A,B);
3875:   MatSetType(Cmat,MATSEQDENSE);
3876:   MatSeqDenseSetPreallocation(Cmat,NULL);

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

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

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

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


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

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

3911:   Level: beginner

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

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

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

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

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

3931:   Level: beginner

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

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

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

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

3948:   Level: beginner

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

3953: PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCRL(Mat,MatType,MatReuse,Mat*);
3954: #if defined(PETSC_HAVE_ELEMENTAL)
3955: PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_Elemental(Mat,MatType,MatReuse,Mat*);
3956: #endif

3958: #if defined(PETSC_HAVE_MATLAB_ENGINE)
3959: PETSC_EXTERN PetscErrorCode  MatlabEnginePut_SeqAIJ(PetscObject,void*);
3960: PETSC_EXTERN PetscErrorCode  MatlabEngineGet_SeqAIJ(PetscObject,void*);
3961: #endif


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

3969:    Not Collective

3971:    Input Parameter:
3972: .  mat - a MATSEQAIJ matrix

3974:    Output Parameter:
3975: .   array - pointer to the data

3977:    Level: intermediate

3979: .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90()
3980: @*/
3981: PetscErrorCode  MatSeqAIJGetArray(Mat A,PetscScalar **array)
3982: {

3986:   PetscUseMethod(A,"MatSeqAIJGetArray_C",(Mat,PetscScalar**),(A,array));
3987:   return(0);
3988: }

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

3995:    Not Collective

3997:    Input Parameter:
3998: .  mat - a MATSEQAIJ matrix

4000:    Output Parameter:
4001: .   nz - the maximum number of nonzeros in any row

4003:    Level: intermediate

4005: .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90()
4006: @*/
4007: PetscErrorCode  MatSeqAIJGetMaxRowNonzeros(Mat A,PetscInt *nz)
4008: {
4009:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)A->data;

4012:   *nz = aij->rmax;
4013:   return(0);
4014: }

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

4021:    Not Collective

4023:    Input Parameters:
4024: .  mat - a MATSEQAIJ matrix
4025: .  array - pointer to the data

4027:    Level: intermediate

4029: .seealso: MatSeqAIJGetArray(), MatSeqAIJRestoreArrayF90()
4030: @*/
4031: PetscErrorCode  MatSeqAIJRestoreArray(Mat A,PetscScalar **array)
4032: {

4036:   PetscUseMethod(A,"MatSeqAIJRestoreArray_C",(Mat,PetscScalar**),(A,array));
4037:   return(0);
4038: }

4042: PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJ(Mat B)
4043: {
4044:   Mat_SeqAIJ     *b;
4046:   PetscMPIInt    size;

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

4052:   PetscNewLog(B,&b);

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

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

4058:   b->row                = 0;
4059:   b->col                = 0;
4060:   b->icol               = 0;
4061:   b->reallocs           = 0;
4062:   b->ignorezeroentries  = PETSC_FALSE;
4063:   b->roworiented        = PETSC_TRUE;
4064:   b->nonew              = 0;
4065:   b->diag               = 0;
4066:   b->solve_work         = 0;
4067:   B->spptr              = 0;
4068:   b->saved_values       = 0;
4069:   b->idiag              = 0;
4070:   b->mdiag              = 0;
4071:   b->ssor_work          = 0;
4072:   b->omega              = 1.0;
4073:   b->fshift             = 0.0;
4074:   b->idiagvalid         = PETSC_FALSE;
4075:   b->ibdiagvalid        = PETSC_FALSE;
4076:   b->keepnonzeropattern = PETSC_FALSE;

4078:   PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);
4079:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJGetArray_C",MatSeqAIJGetArray_SeqAIJ);
4080:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJRestoreArray_C",MatSeqAIJRestoreArray_SeqAIJ);

4082: #if defined(PETSC_HAVE_MATLAB_ENGINE)
4083:   PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEnginePut_C",MatlabEnginePut_SeqAIJ);
4084:   PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEngineGet_C",MatlabEngineGet_SeqAIJ);
4085: #endif

4087:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetColumnIndices_C",MatSeqAIJSetColumnIndices_SeqAIJ);
4088:   PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_SeqAIJ);
4089:   PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_SeqAIJ);
4090:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",MatConvert_SeqAIJ_SeqSBAIJ);
4091:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqbaij_C",MatConvert_SeqAIJ_SeqBAIJ);
4092:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijperm_C",MatConvert_SeqAIJ_SeqAIJPERM);
4093:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijcrl_C",MatConvert_SeqAIJ_SeqAIJCRL);
4094: #if defined(PETSC_HAVE_ELEMENTAL)
4095:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_elemental_C",MatConvert_SeqAIJ_Elemental);
4096: #endif
4097:   PetscObjectComposeFunction((PetscObject)B,"MatIsTranspose_C",MatIsTranspose_SeqAIJ);
4098:   PetscObjectComposeFunction((PetscObject)B,"MatIsHermitianTranspose_C",MatIsTranspose_SeqAIJ);
4099:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",MatSeqAIJSetPreallocation_SeqAIJ);
4100:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",MatSeqAIJSetPreallocationCSR_SeqAIJ);
4101:   PetscObjectComposeFunction((PetscObject)B,"MatReorderForNonzeroDiagonal_C",MatReorderForNonzeroDiagonal_SeqAIJ);
4102:   PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqdense_seqaij_C",MatMatMult_SeqDense_SeqAIJ);
4103:   PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C",MatMatMultSymbolic_SeqDense_SeqAIJ);
4104:   PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C",MatMatMultNumeric_SeqDense_SeqAIJ);
4105:   MatCreate_SeqAIJ_Inode(B);
4106:   PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);
4107:   return(0);
4108: }

4112: /*
4113:     Given a matrix generated with MatGetFactor() duplicates all the information in A into B
4114: */
4115: PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscBool mallocmatspace)
4116: {
4117:   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
4119:   PetscInt       i,m = A->rmap->n;

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

4124:   C->factortype = A->factortype;
4125:   c->row        = 0;
4126:   c->col        = 0;
4127:   c->icol       = 0;
4128:   c->reallocs   = 0;

4130:   C->assembled = PETSC_TRUE;

4132:   PetscLayoutReference(A->rmap,&C->rmap);
4133:   PetscLayoutReference(A->cmap,&C->cmap);

4135:   PetscMalloc2(m,&c->imax,m,&c->ilen);
4136:   PetscLogObjectMemory((PetscObject)C, 2*m*sizeof(PetscInt));
4137:   for (i=0; i<m; i++) {
4138:     c->imax[i] = a->imax[i];
4139:     c->ilen[i] = a->ilen[i];
4140:   }

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

4147:     c->singlemalloc = PETSC_TRUE;

4149:     PetscMemcpy(c->i,a->i,(m+1)*sizeof(PetscInt));
4150:     if (m > 0) {
4151:       PetscMemcpy(c->j,a->j,(a->i[m])*sizeof(PetscInt));
4152:       if (cpvalues == MAT_COPY_VALUES) {
4153:         PetscMemcpy(c->a,a->a,(a->i[m])*sizeof(PetscScalar));
4154:       } else {
4155:         PetscMemzero(c->a,(a->i[m])*sizeof(PetscScalar));
4156:       }
4157:     }
4158:   }

4160:   c->ignorezeroentries = a->ignorezeroentries;
4161:   c->roworiented       = a->roworiented;
4162:   c->nonew             = a->nonew;
4163:   if (a->diag) {
4164:     PetscMalloc1(m+1,&c->diag);
4165:     PetscLogObjectMemory((PetscObject)C,(m+1)*sizeof(PetscInt));
4166:     for (i=0; i<m; i++) {
4167:       c->diag[i] = a->diag[i];
4168:     }
4169:   } else c->diag = 0;

4171:   c->solve_work         = 0;
4172:   c->saved_values       = 0;
4173:   c->idiag              = 0;
4174:   c->ssor_work          = 0;
4175:   c->keepnonzeropattern = a->keepnonzeropattern;
4176:   c->free_a             = PETSC_TRUE;
4177:   c->free_ij            = PETSC_TRUE;

4179:   c->rmax         = a->rmax;
4180:   c->nz           = a->nz;
4181:   c->maxnz        = a->nz;       /* Since we allocate exactly the right amount */
4182:   C->preallocated = PETSC_TRUE;

4184:   c->compressedrow.use   = a->compressedrow.use;
4185:   c->compressedrow.nrows = a->compressedrow.nrows;
4186:   if (a->compressedrow.use) {
4187:     i    = a->compressedrow.nrows;
4188:     PetscMalloc2(i+1,&c->compressedrow.i,i,&c->compressedrow.rindex);
4189:     PetscMemcpy(c->compressedrow.i,a->compressedrow.i,(i+1)*sizeof(PetscInt));
4190:     PetscMemcpy(c->compressedrow.rindex,a->compressedrow.rindex,i*sizeof(PetscInt));
4191:   } else {
4192:     c->compressedrow.use    = PETSC_FALSE;
4193:     c->compressedrow.i      = NULL;
4194:     c->compressedrow.rindex = NULL;
4195:   }
4196:   C->nonzerostate = A->nonzerostate;

4198:   MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);
4199:   PetscFunctionListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);
4200:   return(0);
4201: }

4205: PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
4206: {

4210:   MatCreate(PetscObjectComm((PetscObject)A),B);
4211:   MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);
4212:   if (!(A->rmap->n % A->rmap->bs) && !(A->cmap->n % A->cmap->bs)) {
4213:     MatSetBlockSizesFromMats(*B,A,A);
4214:   }
4215:   MatSetType(*B,((PetscObject)A)->type_name);
4216:   MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);
4217:   return(0);
4218: }

4222: PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer)
4223: {
4224:   Mat_SeqAIJ     *a;
4226:   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N,rows,cols;
4227:   int            fd;
4228:   PetscMPIInt    size;
4229:   MPI_Comm       comm;
4230:   PetscInt       bs = newMat->rmap->bs;

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

4237:   PetscOptionsBegin(comm,NULL,"Options for loading SEQAIJ matrix","Mat");
4238:   PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,NULL);
4239:   PetscOptionsEnd();
4240:   if (bs < 0) bs = 1;
4241:   MatSetBlockSize(newMat,bs);

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

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

4250:   /* read in row lengths */
4251:   PetscMalloc1(M,&rowlengths);
4252:   PetscBinaryRead(fd,rowlengths,M,PETSC_INT);

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

4258:   /* set global size if not set already*/
4259:   if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) {
4260:     MatSetSizes(newMat,PETSC_DECIDE,PETSC_DECIDE,M,N);
4261:   } else {
4262:     /* if sizes and type are already set, check if the vector global sizes are correct */
4263:     MatGetSize(newMat,&rows,&cols);
4264:     if (rows < 0 && cols < 0) { /* user might provide local size instead of global size */
4265:       MatGetLocalSize(newMat,&rows,&cols);
4266:     }
4267:     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);
4268:   }
4269:   MatSeqAIJSetPreallocation_SeqAIJ(newMat,0,rowlengths);
4270:   a    = (Mat_SeqAIJ*)newMat->data;

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

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

4277:   /* set matrix "i" values */
4278:   a->i[0] = 0;
4279:   for (i=1; i<= M; i++) {
4280:     a->i[i]      = a->i[i-1] + rowlengths[i-1];
4281:     a->ilen[i-1] = rowlengths[i-1];
4282:   }
4283:   PetscFree(rowlengths);

4285:   MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);
4286:   MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);
4287:   return(0);
4288: }

4292: PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscBool * flg)
4293: {
4294:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*b = (Mat_SeqAIJ*)B->data;
4296: #if defined(PETSC_USE_COMPLEX)
4297:   PetscInt k;
4298: #endif

4301:   /* If the  matrix dimensions are not equal,or no of nonzeros */
4302:   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
4303:     *flg = PETSC_FALSE;
4304:     return(0);
4305:   }

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

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

4315:   /* if a->a are the same */
4316: #if defined(PETSC_USE_COMPLEX)
4317:   for (k=0; k<a->nz; k++) {
4318:     if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])) {
4319:       *flg = PETSC_FALSE;
4320:       return(0);
4321:     }
4322:   }
4323: #else
4324:   PetscMemcmp(a->a,b->a,(a->nz)*sizeof(PetscScalar),flg);
4325: #endif
4326:   return(0);
4327: }

4331: /*@
4332:      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
4333:               provided by the user.

4335:       Collective on MPI_Comm

4337:    Input Parameters:
4338: +   comm - must be an MPI communicator of size 1
4339: .   m - number of rows
4340: .   n - number of columns
4341: .   i - row indices
4342: .   j - column indices
4343: -   a - matrix values

4345:    Output Parameter:
4346: .   mat - the matrix

4348:    Level: intermediate

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

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

4356:        The i and j indices are 0 based

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

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

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


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

4373: @*/
4374: PetscErrorCode  MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt *i,PetscInt *j,PetscScalar *a,Mat *mat)
4375: {
4377:   PetscInt       ii;
4378:   Mat_SeqAIJ     *aij;
4379: #if defined(PETSC_USE_DEBUG)
4380:   PetscInt jj;
4381: #endif

4384:   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
4385:   MatCreate(comm,mat);
4386:   MatSetSizes(*mat,m,n,m,n);
4387:   /* MatSetBlockSizes(*mat,,); */
4388:   MatSetType(*mat,MATSEQAIJ);
4389:   MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);
4390:   aij  = (Mat_SeqAIJ*)(*mat)->data;
4391:   PetscMalloc2(m,&aij->imax,m,&aij->ilen);

4393:   aij->i            = i;
4394:   aij->j            = j;
4395:   aij->a            = a;
4396:   aij->singlemalloc = PETSC_FALSE;
4397:   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
4398:   aij->free_a       = PETSC_FALSE;
4399:   aij->free_ij      = PETSC_FALSE;

4401:   for (ii=0; ii<m; ii++) {
4402:     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
4403: #if defined(PETSC_USE_DEBUG)
4404:     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]);
4405:     for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
4406:       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);
4407:       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);
4408:     }
4409: #endif
4410:   }
4411: #if defined(PETSC_USE_DEBUG)
4412:   for (ii=0; ii<aij->i[m]; ii++) {
4413:     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %D index = %D",ii,j[ii]);
4414:     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]);
4415:   }
4416: #endif

4418:   MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);
4419:   MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);
4420:   return(0);
4421: }
4424: /*@C
4425:      MatCreateSeqAIJFromTriple - Creates an sequential AIJ matrix using matrix elements (in COO format)
4426:               provided by the user.

4428:       Collective on MPI_Comm

4430:    Input Parameters:
4431: +   comm - must be an MPI communicator of size 1
4432: .   m   - number of rows
4433: .   n   - number of columns
4434: .   i   - row indices
4435: .   j   - column indices
4436: .   a   - matrix values
4437: .   nz  - number of nonzeros
4438: -   idx - 0 or 1 based

4440:    Output Parameter:
4441: .   mat - the matrix

4443:    Level: intermediate

4445:    Notes:
4446:        The i and j indices are 0 based

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

4452:         1 0 0
4453:         2 0 3
4454:         4 5 6

4456:         i =  {0,1,1,2,2,2}
4457:         j =  {0,0,2,0,1,2}
4458:         v =  {1,2,3,4,5,6}


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

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


4471:   PetscCalloc1(m,&nnz);
4472:   for (ii = 0; ii < nz; ii++) {
4473:     nnz[i[ii] - !!idx] += 1;
4474:   }
4475:   MatCreate(comm,mat);
4476:   MatSetSizes(*mat,m,n,m,n);
4477:   MatSetType(*mat,MATSEQAIJ);
4478:   MatSeqAIJSetPreallocation_SeqAIJ(*mat,0,nnz);
4479:   for (ii = 0; ii < nz; ii++) {
4480:     if (idx) {
4481:       row = i[ii] - 1;
4482:       col = j[ii] - 1;
4483:     } else {
4484:       row = i[ii];
4485:       col = j[ii];
4486:     }
4487:     MatSetValues(*mat,one,&row,one,&col,&a[ii],ADD_VALUES);
4488:   }
4489:   MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);
4490:   MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);
4491:   PetscFree(nnz);
4492:   return(0);
4493: }

4497: PetscErrorCode MatSetColoring_SeqAIJ(Mat A,ISColoring coloring)
4498: {
4500:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;

4503:   if (coloring->ctype == IS_COLORING_GLOBAL) {
4504:     ISColoringReference(coloring);
4505:     a->coloring = coloring;
4506:   } else if (coloring->ctype == IS_COLORING_GHOSTED) {
4507:     PetscInt        i,*larray;
4508:     ISColoring      ocoloring;
4509:     ISColoringValue *colors;

4511:     /* set coloring for diagonal portion */
4512:     PetscMalloc1(A->cmap->n,&larray);
4513:     for (i=0; i<A->cmap->n; i++) larray[i] = i;
4514:     ISGlobalToLocalMappingApply(A->cmap->mapping,IS_GTOLM_MASK,A->cmap->n,larray,NULL,larray);
4515:     PetscMalloc1(A->cmap->n,&colors);
4516:     for (i=0; i<A->cmap->n; i++) colors[i] = coloring->colors[larray[i]];
4517:     PetscFree(larray);
4518:     ISColoringCreate(PETSC_COMM_SELF,coloring->n,A->cmap->n,colors,&ocoloring);
4519:     a->coloring = ocoloring;
4520:   }
4521:   return(0);
4522: }

4526: PetscErrorCode MatSetValuesAdifor_SeqAIJ(Mat A,PetscInt nl,void *advalues)
4527: {
4528:   Mat_SeqAIJ      *a      = (Mat_SeqAIJ*)A->data;
4529:   PetscInt        m       = A->rmap->n,*ii = a->i,*jj = a->j,nz,i,j;
4530:   MatScalar       *v      = a->a;
4531:   PetscScalar     *values = (PetscScalar*)advalues;
4532:   ISColoringValue *color;

4535:   if (!a->coloring) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Coloring not set for matrix");
4536:   color = a->coloring->colors;
4537:   /* loop over rows */
4538:   for (i=0; i<m; i++) {
4539:     nz = ii[i+1] - ii[i];
4540:     /* loop over columns putting computed value into matrix */
4541:     for (j=0; j<nz; j++) *v++ = values[color[*jj++]];
4542:     values += nl; /* jump to next row of derivatives */
4543:   }
4544:   return(0);
4545: }

4549: PetscErrorCode MatSeqAIJInvalidateDiagonal(Mat A)
4550: {
4551:   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data;

4555:   a->idiagvalid  = PETSC_FALSE;
4556:   a->ibdiagvalid = PETSC_FALSE;

4558:   MatSeqAIJInvalidateDiagonal_Inode(A);
4559:   return(0);
4560: }

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

4569:   MatCreateMPIMatConcatenateSeqMat_MPIAIJ(comm,inmat,n,scall,outmat);
4570:   return(0);
4571: }

4573: /*
4574:     Special version for direct calls from Fortran
4575: */
4576: #include <petsc-private/fortranimpl.h>
4577: #if defined(PETSC_HAVE_FORTRAN_CAPS)
4578: #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
4579: #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
4580: #define matsetvaluesseqaij_ matsetvaluesseqaij
4581: #endif

4583: /* Change these macros so can be used in void function */
4584: #undef CHKERRQ
4585: #define CHKERRQ(ierr) CHKERRABORT(PetscObjectComm((PetscObject)A),ierr)
4586: #undef SETERRQ2
4587: #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
4588: #undef SETERRQ3
4589: #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr)

4593: 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)
4594: {
4595:   Mat            A  = *AA;
4596:   PetscInt       m  = *mm, n = *nn;
4597:   InsertMode     is = *isis;
4598:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
4599:   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
4600:   PetscInt       *imax,*ai,*ailen;
4602:   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
4603:   MatScalar      *ap,value,*aa;
4604:   PetscBool      ignorezeroentries = a->ignorezeroentries;
4605:   PetscBool      roworiented       = a->roworiented;

4608:   MatCheckPreallocated(A,1);
4609:   imax  = a->imax;
4610:   ai    = a->i;
4611:   ailen = a->ilen;
4612:   aj    = a->j;
4613:   aa    = a->a;

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

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

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