Actual source code: aij.c

petsc-master 2019-09-15
Report Typos and Errors

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


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

 13: PetscErrorCode MatSeqAIJSetTypeFromOptions(Mat A)
 14: {
 15:   PetscErrorCode       ierr;
 16:   PetscBool            flg;
 17:   char                 type[256];

 20:   PetscObjectOptionsBegin((PetscObject)A);
 21:   PetscOptionsFList("-mat_seqaij_type","Matrix SeqAIJ type","MatSeqAIJSetType",MatSeqAIJList,"seqaij",type,256,&flg);
 22:   if (flg) {
 23:     MatSeqAIJSetType(A,type);
 24:   }
 25:   PetscOptionsEnd();
 26:   return(0);
 27: }

 29: PetscErrorCode MatGetColumnNorms_SeqAIJ(Mat A,NormType type,PetscReal *norms)
 30: {
 32:   PetscInt       i,m,n;
 33:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)A->data;

 36:   MatGetSize(A,&m,&n);
 37:   PetscArrayzero(norms,n);
 38:   if (type == NORM_2) {
 39:     for (i=0; i<aij->i[m]; i++) {
 40:       norms[aij->j[i]] += PetscAbsScalar(aij->a[i]*aij->a[i]);
 41:     }
 42:   } else if (type == NORM_1) {
 43:     for (i=0; i<aij->i[m]; i++) {
 44:       norms[aij->j[i]] += PetscAbsScalar(aij->a[i]);
 45:     }
 46:   } else if (type == NORM_INFINITY) {
 47:     for (i=0; i<aij->i[m]; i++) {
 48:       norms[aij->j[i]] = PetscMax(PetscAbsScalar(aij->a[i]),norms[aij->j[i]]);
 49:     }
 50:   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Unknown NormType");

 52:   if (type == NORM_2) {
 53:     for (i=0; i<n; i++) norms[i] = PetscSqrtReal(norms[i]);
 54:   }
 55:   return(0);
 56: }

 58: PetscErrorCode MatFindOffBlockDiagonalEntries_SeqAIJ(Mat A,IS *is)
 59: {
 60:   Mat_SeqAIJ      *a  = (Mat_SeqAIJ*)A->data;
 61:   PetscInt        i,m=A->rmap->n,cnt = 0, bs = A->rmap->bs;
 62:   const PetscInt  *jj = a->j,*ii = a->i;
 63:   PetscInt        *rows;
 64:   PetscErrorCode  ierr;

 67:   for (i=0; i<m; i++) {
 68:     if ((ii[i] != ii[i+1]) && ((jj[ii[i]] < bs*(i/bs)) || (jj[ii[i+1]-1] > bs*((i+bs)/bs)-1))) {
 69:       cnt++;
 70:     }
 71:   }
 72:   PetscMalloc1(cnt,&rows);
 73:   cnt  = 0;
 74:   for (i=0; i<m; i++) {
 75:     if ((ii[i] != ii[i+1]) && ((jj[ii[i]] < bs*(i/bs)) || (jj[ii[i+1]-1] > bs*((i+bs)/bs)-1))) {
 76:       rows[cnt] = i;
 77:       cnt++;
 78:     }
 79:   }
 80:   ISCreateGeneral(PETSC_COMM_SELF,cnt,rows,PETSC_OWN_POINTER,is);
 81:   return(0);
 82: }

 84: PetscErrorCode MatFindZeroDiagonals_SeqAIJ_Private(Mat A,PetscInt *nrows,PetscInt **zrows)
 85: {
 86:   Mat_SeqAIJ      *a  = (Mat_SeqAIJ*)A->data;
 87:   const MatScalar *aa = a->a;
 88:   PetscInt        i,m=A->rmap->n,cnt = 0;
 89:   const PetscInt  *ii = a->i,*jj = a->j,*diag;
 90:   PetscInt        *rows;
 91:   PetscErrorCode  ierr;

 94:   MatMarkDiagonal_SeqAIJ(A);
 95:   diag = a->diag;
 96:   for (i=0; i<m; i++) {
 97:     if ((diag[i] >= ii[i+1]) || (jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) {
 98:       cnt++;
 99:     }
100:   }
101:   PetscMalloc1(cnt,&rows);
102:   cnt  = 0;
103:   for (i=0; i<m; i++) {
104:     if ((diag[i] >= ii[i+1]) || (jj[diag[i]] != i) || (aa[diag[i]] == 0.0)) {
105:       rows[cnt++] = i;
106:     }
107:   }
108:   *nrows = cnt;
109:   *zrows = rows;
110:   return(0);
111: }

113: PetscErrorCode MatFindZeroDiagonals_SeqAIJ(Mat A,IS *zrows)
114: {
115:   PetscInt       nrows,*rows;

119:   *zrows = NULL;
120:   MatFindZeroDiagonals_SeqAIJ_Private(A,&nrows,&rows);
121:   ISCreateGeneral(PetscObjectComm((PetscObject)A),nrows,rows,PETSC_OWN_POINTER,zrows);
122:   return(0);
123: }

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

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

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

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

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

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

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

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

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

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

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

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

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

294:   PetscFree(*ia);
295:   PetscFree(*ja);
296:   return(0);
297: }

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

314:   *nn = n;
315:   if (!ia) return(0);

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

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

352:   MatRestoreColumnIJ_SeqAIJ(A,oshift,symmetric,inodecompressed,n,ia,ja,done);
353:   PetscFree(*spidx);
354:   return(0);
355: }

357: PetscErrorCode MatSetValuesRow_SeqAIJ(Mat A,PetscInt row,const PetscScalar v[])
358: {
359:   Mat_SeqAIJ     *a  = (Mat_SeqAIJ*)A->data;
360:   PetscInt       *ai = a->i;

364:   PetscArraycpy(a->a+ai[row],v,ai[row+1]-ai[row]);
365: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
366:   if (A->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED && ai[row+1]-ai[row]) A->valid_GPU_matrix = PETSC_OFFLOAD_CPU;
367: #endif
368:   return(0);
369: }

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

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

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

381: */

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

393:   row  = ridx[im[0]];
394:   rp   = aj + ai[row];
395:   ap   = aa + ai[row];
396:   nrow = ailen[row];
397:   low  = 0;
398:   high = nrow;
399:   for (l=0; l<n; l++) { /* loop over added columns */
400:     col = cidx[in[l]];
401:     value = v[l];

403:     if (col <= lastcol) low = 0;
404:     else high = nrow;
405:     lastcol = col;
406:     while (high-low > 5) {
407:       t = (low+high)/2;
408:       if (rp[t] > col) high = t;
409:       else low = t;
410:     }
411:     for (i=low; i<high; i++) {
412:       if (rp[i] == col) {
413:         ap[i] += value;
414:         low = i + 1;
415:         break;
416:       }
417:     }
418:   }
419: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
420:   if (A->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED && m*n) A->valid_GPU_matrix = PETSC_OFFLOAD_CPU;
421: #endif
422:   return 0;
423: }

425: PetscErrorCode MatSetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
426: {
427:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
428:   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
429:   PetscInt       *imax = a->imax,*ai = a->i,*ailen = a->ilen;
431:   PetscInt       *aj = a->j,nonew = a->nonew,lastcol = -1;
432:   MatScalar      *ap=NULL,value=0.0,*aa = a->a;
433:   PetscBool      ignorezeroentries = a->ignorezeroentries;
434:   PetscBool      roworiented       = a->roworiented;
435: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
436:   PetscBool      inserted          = PETSC_FALSE;
437: #endif

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

460:       if (col <= lastcol) low = 0;
461:       else high = nrow;
462:       lastcol = col;
463:       while (high-low > 5) {
464:         t = (low+high)/2;
465:         if (rp[t] > col) high = t;
466:         else low = t;
467:       }
468:       for (i=low; i<high; i++) {
469:         if (rp[i] > col) break;
470:         if (rp[i] == col) {
471:           if (!A->structure_only) {
472:             if (is == ADD_VALUES) {
473:               ap[i] += value;
474:               (void)PetscLogFlops(1.0);
475:             }
476:             else ap[i] = value;
477: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
478:             inserted = PETSC_TRUE;
479: #endif
480:           }
481:           low = i + 1;
482:           goto noinsert;
483:         }
484:       }
485:       if (value == 0.0 && ignorezeroentries && row != col) 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:       if (A->structure_only) {
489:         MatSeqXAIJReallocateAIJ_structure_only(A,A->rmap->n,1,nrow,row,col,rmax,ai,aj,rp,imax,nonew,MatScalar);
490:       } else {
491:         MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
492:       }
493:       N = nrow++ - 1; a->nz++; high++;
494:       /* shift up all the later entries in this row */
495:       PetscArraymove(rp+i+1,rp+i,N-i+1);
496:       rp[i] = col;
497:       if (!A->structure_only){
498:         PetscArraymove(ap+i+1,ap+i,N-i+1);
499:         ap[i] = value;
500:       }
501:       low = i + 1;
502:       A->nonzerostate++;
503: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
504:       inserted = PETSC_TRUE;
505: #endif
506: noinsert:;
507:     }
508:     ailen[row] = nrow;
509:   }
510: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
511:   if (A->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED && inserted) A->valid_GPU_matrix = PETSC_OFFLOAD_CPU;
512: #endif
513:   return(0);
514: }

516: PetscErrorCode MatSetValues_SeqAIJ_SortedFull(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
517: {
518:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
519:   PetscInt       *rp,k,row;
520:   PetscInt       *ai = a->i,*ailen = a->ilen;
522:   PetscInt       *aj = a->j;
523:   MatScalar      *aa = a->a,*ap;

526:   for (k=0; k<m; k++) { /* loop over added rows */
527:     row  = im[k];
528:     rp   = aj + ai[row];
529:     ap   = aa + ai[row];
530:     if (!A->was_assembled) {
531:       PetscMemcpy(rp,in,n*sizeof(PetscInt));
532:     }
533:     if (!A->structure_only) {
534:       if (v) {
535:         PetscMemcpy(ap,v,n*sizeof(PetscScalar));
536:         v   += n;
537:       } else {
538:         PetscMemzero(ap,n*sizeof(PetscScalar));
539:       }
540:     }
541:     ailen[row] = n;
542:     a->nz      += n;
543:   }
544: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
545:   if (A->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED && m*n) A->valid_GPU_matrix = PETSC_OFFLOAD_CPU;
546: #endif
547:   return(0);
548: }


551: PetscErrorCode MatGetValues_SeqAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
552: {
553:   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
554:   PetscInt   *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
555:   PetscInt   *ai = a->i,*ailen = a->ilen;
556:   MatScalar  *ap,*aa = a->a;

559:   for (k=0; k<m; k++) { /* loop over rows */
560:     row = im[k];
561:     if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */
562:     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);
563:     rp   = aj + ai[row]; ap = aa + ai[row];
564:     nrow = ailen[row];
565:     for (l=0; l<n; l++) { /* loop over columns */
566:       if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */
567:       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);
568:       col  = in[l];
569:       high = nrow; low = 0; /* assume unsorted */
570:       while (high-low > 5) {
571:         t = (low+high)/2;
572:         if (rp[t] > col) high = t;
573:         else low = t;
574:       }
575:       for (i=low; i<high; i++) {
576:         if (rp[i] > col) break;
577:         if (rp[i] == col) {
578:           *v++ = ap[i];
579:           goto finished;
580:         }
581:       }
582:       *v++ = 0.0;
583: finished:;
584:     }
585:   }
586:   return(0);
587: }


590: PetscErrorCode MatView_SeqAIJ_Binary(Mat A,PetscViewer viewer)
591: {
592:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
594:   PetscInt       i,*col_lens;
595:   int            fd;
596:   FILE           *file;

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

602:   col_lens[0] = MAT_FILE_CLASSID;
603:   col_lens[1] = A->rmap->n;
604:   col_lens[2] = A->cmap->n;
605:   col_lens[3] = a->nz;

607:   /* store lengths of each row and write (including header) to file */
608:   for (i=0; i<A->rmap->n; i++) {
609:     col_lens[4+i] = a->i[i+1] - a->i[i];
610:   }
611:   PetscBinaryWrite(fd,col_lens,4+A->rmap->n,PETSC_INT,PETSC_TRUE);
612:   PetscFree(col_lens);

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

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

620:   PetscViewerBinaryGetInfoPointer(viewer,&file);
621:   if (file) {
622:     fprintf(file,"-matload_block_size %d\n",(int)PetscAbs(A->rmap->bs));
623:   }
624:   return(0);
625: }

627: static PetscErrorCode MatView_SeqAIJ_ASCII_structonly(Mat A,PetscViewer viewer)
628: {
630:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
631:   PetscInt       i,k,m=A->rmap->N;

634:   PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
635:   for (i=0; i<m; i++) {
636:     PetscViewerASCIIPrintf(viewer,"row %D:",i);
637:     for (k=a->i[i]; k<a->i[i+1]; k++) {
638:       PetscViewerASCIIPrintf(viewer," (%D) ",a->j[k]);
639:     }
640:     PetscViewerASCIIPrintf(viewer,"\n");
641:   }
642:   PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
643:   return(0);
644: }

646: extern PetscErrorCode MatSeqAIJFactorInfo_Matlab(Mat,PetscViewer);

648: PetscErrorCode MatView_SeqAIJ_ASCII(Mat A,PetscViewer viewer)
649: {
650:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
651:   PetscErrorCode    ierr;
652:   PetscInt          i,j,m = A->rmap->n;
653:   const char        *name;
654:   PetscViewerFormat format;

657:   if (A->structure_only) {
658:     MatView_SeqAIJ_ASCII_structonly(A,viewer);
659:     return(0);
660:   }

662:   PetscViewerGetFormat(viewer,&format);
663:   if (format == PETSC_VIEWER_ASCII_MATLAB) {
664:     PetscInt nofinalvalue = 0;
665:     if (m && ((a->i[m] == a->i[m-1]) || (a->j[a->nz-1] != A->cmap->n-1))) {
666:       /* Need a dummy value to ensure the dimension of the matrix. */
667:       nofinalvalue = 1;
668:     }
669:     PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
670:     PetscViewerASCIIPrintf(viewer,"%% Size = %D %D \n",m,A->cmap->n);
671:     PetscViewerASCIIPrintf(viewer,"%% Nonzeros = %D \n",a->nz);
672: #if defined(PETSC_USE_COMPLEX)
673:     PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,4);\n",a->nz+nofinalvalue);
674: #else
675:     PetscViewerASCIIPrintf(viewer,"zzz = zeros(%D,3);\n",a->nz+nofinalvalue);
676: #endif
677:     PetscViewerASCIIPrintf(viewer,"zzz = [\n");

679:     for (i=0; i<m; i++) {
680:       for (j=a->i[i]; j<a->i[i+1]; j++) {
681: #if defined(PETSC_USE_COMPLEX)
682:         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]));
683: #else
684:         PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",i+1,a->j[j]+1,(double)a->a[j]);
685: #endif
686:       }
687:     }
688:     if (nofinalvalue) {
689: #if defined(PETSC_USE_COMPLEX)
690:       PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e %18.16e\n",m,A->cmap->n,0.,0.);
691: #else
692:       PetscViewerASCIIPrintf(viewer,"%D %D  %18.16e\n",m,A->cmap->n,0.0);
693: #endif
694:     }
695:     PetscObjectGetName((PetscObject)A,&name);
696:     PetscViewerASCIIPrintf(viewer,"];\n %s = spconvert(zzz);\n",name);
697:     PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
698:   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
699:     return(0);
700:   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
701:     PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
702:     for (i=0; i<m; i++) {
703:       PetscViewerASCIIPrintf(viewer,"row %D:",i);
704:       for (j=a->i[i]; j<a->i[i+1]; j++) {
705: #if defined(PETSC_USE_COMPLEX)
706:         if (PetscImaginaryPart(a->a[j]) > 0.0 && PetscRealPart(a->a[j]) != 0.0) {
707:           PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));
708:         } else if (PetscImaginaryPart(a->a[j]) < 0.0 && PetscRealPart(a->a[j]) != 0.0) {
709:           PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)-PetscImaginaryPart(a->a[j]));
710:         } else if (PetscRealPart(a->a[j]) != 0.0) {
711:           PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));
712:         }
713: #else
714:         if (a->a[j] != 0.0) {PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);}
715: #endif
716:       }
717:       PetscViewerASCIIPrintf(viewer,"\n");
718:     }
719:     PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
720:   } else if (format == PETSC_VIEWER_ASCII_SYMMODU) {
721:     PetscInt nzd=0,fshift=1,*sptr;
722:     PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
723:     PetscMalloc1(m+1,&sptr);
724:     for (i=0; i<m; i++) {
725:       sptr[i] = nzd+1;
726:       for (j=a->i[i]; j<a->i[i+1]; j++) {
727:         if (a->j[j] >= i) {
728: #if defined(PETSC_USE_COMPLEX)
729:           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) nzd++;
730: #else
731:           if (a->a[j] != 0.0) nzd++;
732: #endif
733:         }
734:       }
735:     }
736:     sptr[m] = nzd+1;
737:     PetscViewerASCIIPrintf(viewer," %D %D\n\n",m,nzd);
738:     for (i=0; i<m+1; i+=6) {
739:       if (i+4<m) {
740:         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]);
741:       } else if (i+3<m) {
742:         PetscViewerASCIIPrintf(viewer," %D %D %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3],sptr[i+4]);
743:       } else if (i+2<m) {
744:         PetscViewerASCIIPrintf(viewer," %D %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2],sptr[i+3]);
745:       } else if (i+1<m) {
746:         PetscViewerASCIIPrintf(viewer," %D %D %D\n",sptr[i],sptr[i+1],sptr[i+2]);
747:       } else if (i<m) {
748:         PetscViewerASCIIPrintf(viewer," %D %D\n",sptr[i],sptr[i+1]);
749:       } else {
750:         PetscViewerASCIIPrintf(viewer," %D\n",sptr[i]);
751:       }
752:     }
753:     PetscViewerASCIIPrintf(viewer,"\n");
754:     PetscFree(sptr);
755:     for (i=0; i<m; i++) {
756:       for (j=a->i[i]; j<a->i[i+1]; j++) {
757:         if (a->j[j] >= i) {PetscViewerASCIIPrintf(viewer," %D ",a->j[j]+fshift);}
758:       }
759:       PetscViewerASCIIPrintf(viewer,"\n");
760:     }
761:     PetscViewerASCIIPrintf(viewer,"\n");
762:     for (i=0; i<m; i++) {
763:       for (j=a->i[i]; j<a->i[i+1]; j++) {
764:         if (a->j[j] >= i) {
765: #if defined(PETSC_USE_COMPLEX)
766:           if (PetscImaginaryPart(a->a[j]) != 0.0 || PetscRealPart(a->a[j]) != 0.0) {
767:             PetscViewerASCIIPrintf(viewer," %18.16e %18.16e ",(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));
768:           }
769: #else
770:           if (a->a[j] != 0.0) {PetscViewerASCIIPrintf(viewer," %18.16e ",(double)a->a[j]);}
771: #endif
772:         }
773:       }
774:       PetscViewerASCIIPrintf(viewer,"\n");
775:     }
776:     PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
777:   } else if (format == PETSC_VIEWER_ASCII_DENSE) {
778:     PetscInt    cnt = 0,jcnt;
779:     PetscScalar value;
780: #if defined(PETSC_USE_COMPLEX)
781:     PetscBool   realonly = PETSC_TRUE;

783:     for (i=0; i<a->i[m]; i++) {
784:       if (PetscImaginaryPart(a->a[i]) != 0.0) {
785:         realonly = PETSC_FALSE;
786:         break;
787:       }
788:     }
789: #endif

791:     PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
792:     for (i=0; i<m; i++) {
793:       jcnt = 0;
794:       for (j=0; j<A->cmap->n; j++) {
795:         if (jcnt < a->i[i+1]-a->i[i] && j == a->j[cnt]) {
796:           value = a->a[cnt++];
797:           jcnt++;
798:         } else {
799:           value = 0.0;
800:         }
801: #if defined(PETSC_USE_COMPLEX)
802:         if (realonly) {
803:           PetscViewerASCIIPrintf(viewer," %7.5e ",(double)PetscRealPart(value));
804:         } else {
805:           PetscViewerASCIIPrintf(viewer," %7.5e+%7.5e i ",(double)PetscRealPart(value),(double)PetscImaginaryPart(value));
806:         }
807: #else
808:         PetscViewerASCIIPrintf(viewer," %7.5e ",(double)value);
809: #endif
810:       }
811:       PetscViewerASCIIPrintf(viewer,"\n");
812:     }
813:     PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
814:   } else if (format == PETSC_VIEWER_ASCII_MATRIXMARKET) {
815:     PetscInt fshift=1;
816:     PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
817: #if defined(PETSC_USE_COMPLEX)
818:     PetscViewerASCIIPrintf(viewer,"%%%%MatrixMarket matrix coordinate complex general\n");
819: #else
820:     PetscViewerASCIIPrintf(viewer,"%%%%MatrixMarket matrix coordinate real general\n");
821: #endif
822:     PetscViewerASCIIPrintf(viewer,"%D %D %D\n", m, A->cmap->n, a->nz);
823:     for (i=0; i<m; i++) {
824:       for (j=a->i[i]; j<a->i[i+1]; j++) {
825: #if defined(PETSC_USE_COMPLEX)
826:         PetscViewerASCIIPrintf(viewer,"%D %D %g %g\n", i+fshift,a->j[j]+fshift,(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));
827: #else
828:         PetscViewerASCIIPrintf(viewer,"%D %D %g\n", i+fshift, a->j[j]+fshift, (double)a->a[j]);
829: #endif
830:       }
831:     }
832:     PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
833:   } else {
834:     PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
835:     if (A->factortype) {
836:       for (i=0; i<m; i++) {
837:         PetscViewerASCIIPrintf(viewer,"row %D:",i);
838:         /* L part */
839:         for (j=a->i[i]; j<a->i[i+1]; j++) {
840: #if defined(PETSC_USE_COMPLEX)
841:           if (PetscImaginaryPart(a->a[j]) > 0.0) {
842:             PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));
843:           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
844:             PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)(-PetscImaginaryPart(a->a[j])));
845:           } else {
846:             PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));
847:           }
848: #else
849:           PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);
850: #endif
851:         }
852:         /* diagonal */
853:         j = a->diag[i];
854: #if defined(PETSC_USE_COMPLEX)
855:         if (PetscImaginaryPart(a->a[j]) > 0.0) {
856:           PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(1.0/a->a[j]),(double)PetscImaginaryPart(1.0/a->a[j]));
857:         } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
858:           PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(1.0/a->a[j]),(double)(-PetscImaginaryPart(1.0/a->a[j])));
859:         } else {
860:           PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(1.0/a->a[j]));
861:         }
862: #else
863:         PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)(1.0/a->a[j]));
864: #endif

866:         /* U part */
867:         for (j=a->diag[i+1]+1; j<a->diag[i]; j++) {
868: #if defined(PETSC_USE_COMPLEX)
869:           if (PetscImaginaryPart(a->a[j]) > 0.0) {
870:             PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));
871:           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
872:             PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)(-PetscImaginaryPart(a->a[j])));
873:           } else {
874:             PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));
875:           }
876: #else
877:           PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);
878: #endif
879:         }
880:         PetscViewerASCIIPrintf(viewer,"\n");
881:       }
882:     } else {
883:       for (i=0; i<m; i++) {
884:         PetscViewerASCIIPrintf(viewer,"row %D:",i);
885:         for (j=a->i[i]; j<a->i[i+1]; j++) {
886: #if defined(PETSC_USE_COMPLEX)
887:           if (PetscImaginaryPart(a->a[j]) > 0.0) {
888:             PetscViewerASCIIPrintf(viewer," (%D, %g + %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)PetscImaginaryPart(a->a[j]));
889:           } else if (PetscImaginaryPart(a->a[j]) < 0.0) {
890:             PetscViewerASCIIPrintf(viewer," (%D, %g - %g i)",a->j[j],(double)PetscRealPart(a->a[j]),(double)-PetscImaginaryPart(a->a[j]));
891:           } else {
892:             PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)PetscRealPart(a->a[j]));
893:           }
894: #else
895:           PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[j],(double)a->a[j]);
896: #endif
897:         }
898:         PetscViewerASCIIPrintf(viewer,"\n");
899:       }
900:     }
901:     PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
902:   }
903:   PetscViewerFlush(viewer);
904:   return(0);
905: }

907:  #include <petscdraw.h>
908: PetscErrorCode MatView_SeqAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
909: {
910:   Mat               A  = (Mat) Aa;
911:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
912:   PetscErrorCode    ierr;
913:   PetscInt          i,j,m = A->rmap->n;
914:   int               color;
915:   PetscReal         xl,yl,xr,yr,x_l,x_r,y_l,y_r;
916:   PetscViewer       viewer;
917:   PetscViewerFormat format;

920:   PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);
921:   PetscViewerGetFormat(viewer,&format);
922:   PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);

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

926:   if (format != PETSC_VIEWER_DRAW_CONTOUR) {
927:     PetscDrawCollectiveBegin(draw);
928:     /* Blue for negative, Cyan for zero and  Red for positive */
929:     color = PETSC_DRAW_BLUE;
930:     for (i=0; i<m; i++) {
931:       y_l = m - i - 1.0; y_r = y_l + 1.0;
932:       for (j=a->i[i]; j<a->i[i+1]; j++) {
933:         x_l = a->j[j]; x_r = x_l + 1.0;
934:         if (PetscRealPart(a->a[j]) >=  0.) continue;
935:         PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);
936:       }
937:     }
938:     color = PETSC_DRAW_CYAN;
939:     for (i=0; i<m; i++) {
940:       y_l = m - i - 1.0; y_r = y_l + 1.0;
941:       for (j=a->i[i]; j<a->i[i+1]; j++) {
942:         x_l = a->j[j]; x_r = x_l + 1.0;
943:         if (a->a[j] !=  0.) continue;
944:         PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);
945:       }
946:     }
947:     color = PETSC_DRAW_RED;
948:     for (i=0; i<m; i++) {
949:       y_l = m - i - 1.0; y_r = y_l + 1.0;
950:       for (j=a->i[i]; j<a->i[i+1]; j++) {
951:         x_l = a->j[j]; x_r = x_l + 1.0;
952:         if (PetscRealPart(a->a[j]) <=  0.) continue;
953:         PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);
954:       }
955:     }
956:     PetscDrawCollectiveEnd(draw);
957:   } else {
958:     /* use contour shading to indicate magnitude of values */
959:     /* first determine max of all nonzero values */
960:     PetscReal minv = 0.0, maxv = 0.0;
961:     PetscInt  nz = a->nz, count = 0;
962:     PetscDraw popup;

964:     for (i=0; i<nz; i++) {
965:       if (PetscAbsScalar(a->a[i]) > maxv) maxv = PetscAbsScalar(a->a[i]);
966:     }
967:     if (minv >= maxv) maxv = minv + PETSC_SMALL;
968:     PetscDrawGetPopup(draw,&popup);
969:     PetscDrawScalePopup(popup,minv,maxv);

971:     PetscDrawCollectiveBegin(draw);
972:     for (i=0; i<m; i++) {
973:       y_l = m - i - 1.0;
974:       y_r = y_l + 1.0;
975:       for (j=a->i[i]; j<a->i[i+1]; j++) {
976:         x_l = a->j[j];
977:         x_r = x_l + 1.0;
978:         color = PetscDrawRealToColor(PetscAbsScalar(a->a[count]),minv,maxv);
979:         PetscDrawRectangle(draw,x_l,y_l,x_r,y_r,color,color,color,color);
980:         count++;
981:       }
982:     }
983:     PetscDrawCollectiveEnd(draw);
984:   }
985:   return(0);
986: }

988:  #include <petscdraw.h>
989: PetscErrorCode MatView_SeqAIJ_Draw(Mat A,PetscViewer viewer)
990: {
992:   PetscDraw      draw;
993:   PetscReal      xr,yr,xl,yl,h,w;
994:   PetscBool      isnull;

997:   PetscViewerDrawGetDraw(viewer,0,&draw);
998:   PetscDrawIsNull(draw,&isnull);
999:   if (isnull) return(0);

1001:   xr   = A->cmap->n; yr  = A->rmap->n; h = yr/10.0; w = xr/10.0;
1002:   xr  += w;          yr += h;         xl = -w;     yl = -h;
1003:   PetscDrawSetCoordinates(draw,xl,yl,xr,yr);
1004:   PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);
1005:   PetscDrawZoom(draw,MatView_SeqAIJ_Draw_Zoom,A);
1006:   PetscObjectCompose((PetscObject)A,"Zoomviewer",NULL);
1007:   PetscDrawSave(draw);
1008:   return(0);
1009: }

1011: PetscErrorCode MatView_SeqAIJ(Mat A,PetscViewer viewer)
1012: {
1014:   PetscBool      iascii,isbinary,isdraw;

1017:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
1018:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
1019:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
1020:   if (iascii) {
1021:     MatView_SeqAIJ_ASCII(A,viewer);
1022:   } else if (isbinary) {
1023:     MatView_SeqAIJ_Binary(A,viewer);
1024:   } else if (isdraw) {
1025:     MatView_SeqAIJ_Draw(A,viewer);
1026:   }
1027:   MatView_SeqAIJ_Inode(A,viewer);
1028:   return(0);
1029: }

1031: PetscErrorCode MatAssemblyEnd_SeqAIJ(Mat A,MatAssemblyType mode)
1032: {
1033:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1035:   PetscInt       fshift = 0,i,*ai = a->i,*aj = a->j,*imax = a->imax;
1036:   PetscInt       m      = A->rmap->n,*ip,N,*ailen = a->ilen,rmax = 0;
1037:   MatScalar      *aa    = a->a,*ap;
1038:   PetscReal      ratio  = 0.6;

1041:   if (mode == MAT_FLUSH_ASSEMBLY) return(0);
1042:   MatSeqAIJInvalidateDiagonal(A);
1043:   if (A->was_assembled && A->ass_nonzerostate == A->nonzerostate) return(0);

1045:   if (m) rmax = ailen[0]; /* determine row with most nonzeros */
1046:   for (i=1; i<m; i++) {
1047:     /* move each row back by the amount of empty slots (fshift) before it*/
1048:     fshift += imax[i-1] - ailen[i-1];
1049:     rmax    = PetscMax(rmax,ailen[i]);
1050:     if (fshift) {
1051:       ip = aj + ai[i];
1052:       ap = aa + ai[i];
1053:       N  = ailen[i];
1054:       PetscArraymove(ip-fshift,ip,N);
1055:       if (!A->structure_only) {
1056:         PetscArraymove(ap-fshift,ap,N);
1057:       }
1058:     }
1059:     ai[i] = ai[i-1] + ailen[i-1];
1060:   }
1061:   if (m) {
1062:     fshift += imax[m-1] - ailen[m-1];
1063:     ai[m]   = ai[m-1] + ailen[m-1];
1064:   }

1066:   /* reset ilen and imax for each row */
1067:   a->nonzerorowcnt = 0;
1068:   if (A->structure_only) {
1069:     PetscFree(a->imax);
1070:     PetscFree(a->ilen);
1071:   } else { /* !A->structure_only */
1072:     for (i=0; i<m; i++) {
1073:       ailen[i] = imax[i] = ai[i+1] - ai[i];
1074:       a->nonzerorowcnt += ((ai[i+1] - ai[i]) > 0);
1075:     }
1076:   }
1077:   a->nz = ai[m];
1078:   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);

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

1085:   A->info.mallocs    += a->reallocs;
1086:   a->reallocs         = 0;
1087:   A->info.nz_unneeded = (PetscReal)fshift;
1088:   a->rmax             = rmax;

1090:   if (!A->structure_only) {
1091:     MatCheckCompressedRow(A,a->nonzerorowcnt,&a->compressedrow,a->i,m,ratio);
1092:   }
1093:   MatAssemblyEnd_SeqAIJ_Inode(A,mode);
1094:   return(0);
1095: }

1097: PetscErrorCode MatRealPart_SeqAIJ(Mat A)
1098: {
1099:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1100:   PetscInt       i,nz = a->nz;
1101:   MatScalar      *aa = a->a;

1105:   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
1106:   MatSeqAIJInvalidateDiagonal(A);
1107: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
1108:   if (A->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) A->valid_GPU_matrix = PETSC_OFFLOAD_CPU;
1109: #endif
1110:   return(0);
1111: }

1113: PetscErrorCode MatImaginaryPart_SeqAIJ(Mat A)
1114: {
1115:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1116:   PetscInt       i,nz = a->nz;
1117:   MatScalar      *aa = a->a;

1121:   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
1122:   MatSeqAIJInvalidateDiagonal(A);
1123: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
1124:   if (A->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) A->valid_GPU_matrix = PETSC_OFFLOAD_CPU;
1125: #endif
1126:   return(0);
1127: }

1129: PetscErrorCode MatZeroEntries_SeqAIJ(Mat A)
1130: {
1131:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;

1135:   PetscArrayzero(a->a,a->i[A->rmap->n]);
1136:   MatSeqAIJInvalidateDiagonal(A);
1137: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
1138:   if (A->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) A->valid_GPU_matrix = PETSC_OFFLOAD_CPU;
1139: #endif
1140:   return(0);
1141: }

1143: PetscErrorCode MatDestroy_SeqAIJ(Mat A)
1144: {
1145:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;

1149: #if defined(PETSC_USE_LOG)
1150:   PetscLogObjectState((PetscObject)A,"Rows=%D, Cols=%D, NZ=%D",A->rmap->n,A->cmap->n,a->nz);
1151: #endif
1152:   MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);
1153:   ISDestroy(&a->row);
1154:   ISDestroy(&a->col);
1155:   PetscFree(a->diag);
1156:   PetscFree(a->ibdiag);
1157:   PetscFree(a->imax);
1158:   PetscFree(a->ilen);
1159:   PetscFree(a->ipre);
1160:   PetscFree3(a->idiag,a->mdiag,a->ssor_work);
1161:   PetscFree(a->solve_work);
1162:   ISDestroy(&a->icol);
1163:   PetscFree(a->saved_values);
1164:   ISColoringDestroy(&a->coloring);
1165:   PetscFree2(a->compressedrow.i,a->compressedrow.rindex);
1166:   PetscFree(a->matmult_abdense);

1168:   MatDestroy_SeqAIJ_Inode(A);
1169:   PetscFree(A->data);

1171:   PetscObjectChangeTypeName((PetscObject)A,0);
1172:   PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetColumnIndices_C",NULL);
1173:   PetscObjectComposeFunction((PetscObject)A,"MatStoreValues_C",NULL);
1174:   PetscObjectComposeFunction((PetscObject)A,"MatRetrieveValues_C",NULL);
1175:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqsbaij_C",NULL);
1176:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqbaij_C",NULL);
1177:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqaijperm_C",NULL);
1178: #if defined(PETSC_HAVE_ELEMENTAL)
1179:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_elemental_C",NULL);
1180: #endif
1181: #if defined(PETSC_HAVE_HYPRE)
1182:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_hypre_C",NULL);
1183:   PetscObjectComposeFunction((PetscObject)A,"MatMatMatMult_transpose_seqaij_seqaij_C",NULL);
1184: #endif
1185:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqdense_C",NULL);
1186:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_seqsell_C",NULL);
1187:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqaij_is_C",NULL);
1188:   PetscObjectComposeFunction((PetscObject)A,"MatIsTranspose_C",NULL);
1189:   PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocation_C",NULL);
1190:   PetscObjectComposeFunction((PetscObject)A,"MatResetPreallocation_C",NULL);
1191:   PetscObjectComposeFunction((PetscObject)A,"MatSeqAIJSetPreallocationCSR_C",NULL);
1192:   PetscObjectComposeFunction((PetscObject)A,"MatReorderForNonzeroDiagonal_C",NULL);
1193:   PetscObjectComposeFunction((PetscObject)A,"MatPtAP_is_seqaij_C",NULL);
1194:   return(0);
1195: }

1197: PetscErrorCode MatSetOption_SeqAIJ(Mat A,MatOption op,PetscBool flg)
1198: {
1199:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;

1203:   switch (op) {
1204:   case MAT_ROW_ORIENTED:
1205:     a->roworiented = flg;
1206:     break;
1207:   case MAT_KEEP_NONZERO_PATTERN:
1208:     a->keepnonzeropattern = flg;
1209:     break;
1210:   case MAT_NEW_NONZERO_LOCATIONS:
1211:     a->nonew = (flg ? 0 : 1);
1212:     break;
1213:   case MAT_NEW_NONZERO_LOCATION_ERR:
1214:     a->nonew = (flg ? -1 : 0);
1215:     break;
1216:   case MAT_NEW_NONZERO_ALLOCATION_ERR:
1217:     a->nonew = (flg ? -2 : 0);
1218:     break;
1219:   case MAT_UNUSED_NONZERO_LOCATION_ERR:
1220:     a->nounused = (flg ? -1 : 0);
1221:     break;
1222:   case MAT_IGNORE_ZERO_ENTRIES:
1223:     a->ignorezeroentries = flg;
1224:     break;
1225:   case MAT_SPD:
1226:   case MAT_SYMMETRIC:
1227:   case MAT_STRUCTURALLY_SYMMETRIC:
1228:   case MAT_HERMITIAN:
1229:   case MAT_SYMMETRY_ETERNAL:
1230:   case MAT_STRUCTURE_ONLY:
1231:     /* These options are handled directly by MatSetOption() */
1232:     break;
1233:   case MAT_NEW_DIAGONALS:
1234:   case MAT_IGNORE_OFF_PROC_ENTRIES:
1235:   case MAT_USE_HASH_TABLE:
1236:     PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);
1237:     break;
1238:   case MAT_USE_INODES:
1239:     /* Not an error because MatSetOption_SeqAIJ_Inode handles this one */
1240:     break;
1241:   case MAT_SUBMAT_SINGLEIS:
1242:     A->submat_singleis = flg;
1243:     break;
1244:   case MAT_SORTED_FULL:
1245:     if (flg) A->ops->setvalues = MatSetValues_SeqAIJ_SortedFull;
1246:     else     A->ops->setvalues = MatSetValues_SeqAIJ;
1247:     break;
1248:   default:
1249:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
1250:   }
1251:   MatSetOption_SeqAIJ_Inode(A,op,flg);
1252:   return(0);
1253: }

1255: PetscErrorCode MatGetDiagonal_SeqAIJ(Mat A,Vec v)
1256: {
1257:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1259:   PetscInt       i,j,n,*ai=a->i,*aj=a->j;
1260:   PetscScalar    *aa=a->a,*x;

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

1266:   if (A->factortype == MAT_FACTOR_ILU || A->factortype == MAT_FACTOR_LU) {
1267:     PetscInt *diag=a->diag;
1268:     VecGetArrayWrite(v,&x);
1269:     for (i=0; i<n; i++) x[i] = 1.0/aa[diag[i]];
1270:     VecRestoreArrayWrite(v,&x);
1271:     return(0);
1272:   }

1274:   VecGetArrayWrite(v,&x);
1275:   for (i=0; i<n; i++) {
1276:     x[i] = 0.0;
1277:     for (j=ai[i]; j<ai[i+1]; j++) {
1278:       if (aj[j] == i) {
1279:         x[i] = aa[j];
1280:         break;
1281:       }
1282:     }
1283:   }
1284:   VecRestoreArrayWrite(v,&x);
1285:   return(0);
1286: }

1288: #include <../src/mat/impls/aij/seq/ftn-kernels/fmult.h>
1289: PetscErrorCode MatMultTransposeAdd_SeqAIJ(Mat A,Vec xx,Vec zz,Vec yy)
1290: {
1291:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1292:   PetscScalar       *y;
1293:   const PetscScalar *x;
1294:   PetscErrorCode    ierr;
1295:   PetscInt          m = A->rmap->n;
1296: #if !defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1297:   const MatScalar   *v;
1298:   PetscScalar       alpha;
1299:   PetscInt          n,i,j;
1300:   const PetscInt    *idx,*ii,*ridx=NULL;
1301:   Mat_CompressedRow cprow    = a->compressedrow;
1302:   PetscBool         usecprow = cprow.use;
1303: #endif

1306:   if (zz != yy) {VecCopy(zz,yy);}
1307:   VecGetArrayRead(xx,&x);
1308:   VecGetArray(yy,&y);

1310: #if defined(PETSC_USE_FORTRAN_KERNEL_MULTTRANSPOSEAIJ)
1311:   fortranmulttransposeaddaij_(&m,x,a->i,a->j,a->a,y);
1312: #else
1313:   if (usecprow) {
1314:     m    = cprow.nrows;
1315:     ii   = cprow.i;
1316:     ridx = cprow.rindex;
1317:   } else {
1318:     ii = a->i;
1319:   }
1320:   for (i=0; i<m; i++) {
1321:     idx = a->j + ii[i];
1322:     v   = a->a + ii[i];
1323:     n   = ii[i+1] - ii[i];
1324:     if (usecprow) {
1325:       alpha = x[ridx[i]];
1326:     } else {
1327:       alpha = x[i];
1328:     }
1329:     for (j=0; j<n; j++) y[idx[j]] += alpha*v[j];
1330:   }
1331: #endif
1332:   PetscLogFlops(2.0*a->nz);
1333:   VecRestoreArrayRead(xx,&x);
1334:   VecRestoreArray(yy,&y);
1335:   return(0);
1336: }

1338: PetscErrorCode MatMultTranspose_SeqAIJ(Mat A,Vec xx,Vec yy)
1339: {

1343:   VecSet(yy,0.0);
1344:   MatMultTransposeAdd_SeqAIJ(A,xx,yy,yy);
1345:   return(0);
1346: }

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

1350: PetscErrorCode MatMult_SeqAIJ(Mat A,Vec xx,Vec yy)
1351: {
1352:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1353:   PetscScalar       *y;
1354:   const PetscScalar *x;
1355:   const MatScalar   *aa;
1356:   PetscErrorCode    ierr;
1357:   PetscInt          m=A->rmap->n;
1358:   const PetscInt    *aj,*ii,*ridx=NULL;
1359:   PetscInt          n,i;
1360:   PetscScalar       sum;
1361:   PetscBool         usecprow=a->compressedrow.use;

1363: #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
1364: #pragma disjoint(*x,*y,*aa)
1365: #endif

1368:   VecGetArrayRead(xx,&x);
1369:   VecGetArray(yy,&y);
1370:   ii   = a->i;
1371:   if (usecprow) { /* use compressed row format */
1372:     PetscArrayzero(y,m);
1373:     m    = a->compressedrow.nrows;
1374:     ii   = a->compressedrow.i;
1375:     ridx = a->compressedrow.rindex;
1376:     for (i=0; i<m; i++) {
1377:       n           = ii[i+1] - ii[i];
1378:       aj          = a->j + ii[i];
1379:       aa          = a->a + ii[i];
1380:       sum         = 0.0;
1381:       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1382:       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
1383:       y[*ridx++] = sum;
1384:     }
1385:   } else { /* do not use compressed row format */
1386: #if defined(PETSC_USE_FORTRAN_KERNEL_MULTAIJ)
1387:     aj   = a->j;
1388:     aa   = a->a;
1389:     fortranmultaij_(&m,x,ii,aj,aa,y);
1390: #else
1391:     for (i=0; i<m; i++) {
1392:       n           = ii[i+1] - ii[i];
1393:       aj          = a->j + ii[i];
1394:       aa          = a->a + ii[i];
1395:       sum         = 0.0;
1396:       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1397:       y[i] = sum;
1398:     }
1399: #endif
1400:   }
1401:   PetscLogFlops(2.0*a->nz - a->nonzerorowcnt);
1402:   VecRestoreArrayRead(xx,&x);
1403:   VecRestoreArray(yy,&y);
1404:   return(0);
1405: }

1407: PetscErrorCode MatMultMax_SeqAIJ(Mat A,Vec xx,Vec yy)
1408: {
1409:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1410:   PetscScalar       *y;
1411:   const PetscScalar *x;
1412:   const MatScalar   *aa;
1413:   PetscErrorCode    ierr;
1414:   PetscInt          m=A->rmap->n;
1415:   const PetscInt    *aj,*ii,*ridx=NULL;
1416:   PetscInt          n,i,nonzerorow=0;
1417:   PetscScalar       sum;
1418:   PetscBool         usecprow=a->compressedrow.use;

1420: #if defined(PETSC_HAVE_PRAGMA_DISJOINT)
1421: #pragma disjoint(*x,*y,*aa)
1422: #endif

1425:   VecGetArrayRead(xx,&x);
1426:   VecGetArray(yy,&y);
1427:   if (usecprow) { /* use compressed row format */
1428:     m    = a->compressedrow.nrows;
1429:     ii   = a->compressedrow.i;
1430:     ridx = a->compressedrow.rindex;
1431:     for (i=0; i<m; i++) {
1432:       n           = ii[i+1] - ii[i];
1433:       aj          = a->j + ii[i];
1434:       aa          = a->a + ii[i];
1435:       sum         = 0.0;
1436:       nonzerorow += (n>0);
1437:       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1438:       /* for (j=0; j<n; j++) sum += (*aa++)*x[*aj++]; */
1439:       y[*ridx++] = sum;
1440:     }
1441:   } else { /* do not use compressed row format */
1442:     ii = a->i;
1443:     for (i=0; i<m; i++) {
1444:       n           = ii[i+1] - ii[i];
1445:       aj          = a->j + ii[i];
1446:       aa          = a->a + ii[i];
1447:       sum         = 0.0;
1448:       nonzerorow += (n>0);
1449:       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1450:       y[i] = sum;
1451:     }
1452:   }
1453:   PetscLogFlops(2.0*a->nz - nonzerorow);
1454:   VecRestoreArrayRead(xx,&x);
1455:   VecRestoreArray(yy,&y);
1456:   return(0);
1457: }

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

1472:   VecGetArrayRead(xx,&x);
1473:   VecGetArrayPair(yy,zz,&y,&z);
1474:   if (usecprow) { /* use compressed row format */
1475:     if (zz != yy) {
1476:       PetscArraycpy(z,y,m);
1477:     }
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 = y[*ridx];
1486:       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1487:       z[*ridx++] = sum;
1488:     }
1489:   } else { /* do not use compressed row format */
1490:     ii = a->i;
1491:     for (i=0; i<m; i++) {
1492:       n   = ii[i+1] - ii[i];
1493:       aj  = a->j + ii[i];
1494:       aa  = a->a + ii[i];
1495:       sum = y[i];
1496:       PetscSparseDenseMaxDot(sum,x,aa,aj,n);
1497:       z[i] = sum;
1498:     }
1499:   }
1500:   PetscLogFlops(2.0*a->nz);
1501:   VecRestoreArrayRead(xx,&x);
1502:   VecRestoreArrayPair(yy,zz,&y,&z);
1503:   return(0);
1504: }

1506: #include <../src/mat/impls/aij/seq/ftn-kernels/fmultadd.h>
1507: PetscErrorCode MatMultAdd_SeqAIJ(Mat A,Vec xx,Vec yy,Vec zz)
1508: {
1509:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1510:   PetscScalar       *y,*z;
1511:   const PetscScalar *x;
1512:   const MatScalar   *aa;
1513:   PetscErrorCode    ierr;
1514:   const PetscInt    *aj,*ii,*ridx=NULL;
1515:   PetscInt          m = A->rmap->n,n,i;
1516:   PetscScalar       sum;
1517:   PetscBool         usecprow=a->compressedrow.use;

1520:   VecGetArrayRead(xx,&x);
1521:   VecGetArrayPair(yy,zz,&y,&z);
1522:   if (usecprow) { /* use compressed row format */
1523:     if (zz != yy) {
1524:       PetscArraycpy(z,y,m);
1525:     }
1526:     m    = a->compressedrow.nrows;
1527:     ii   = a->compressedrow.i;
1528:     ridx = a->compressedrow.rindex;
1529:     for (i=0; i<m; i++) {
1530:       n   = ii[i+1] - ii[i];
1531:       aj  = a->j + ii[i];
1532:       aa  = a->a + ii[i];
1533:       sum = y[*ridx];
1534:       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1535:       z[*ridx++] = sum;
1536:     }
1537:   } else { /* do not use compressed row format */
1538:     ii = a->i;
1539: #if defined(PETSC_USE_FORTRAN_KERNEL_MULTADDAIJ)
1540:     aj = a->j;
1541:     aa = a->a;
1542:     fortranmultaddaij_(&m,x,ii,aj,aa,y,z);
1543: #else
1544:     for (i=0; i<m; i++) {
1545:       n   = ii[i+1] - ii[i];
1546:       aj  = a->j + ii[i];
1547:       aa  = a->a + ii[i];
1548:       sum = y[i];
1549:       PetscSparseDensePlusDot(sum,x,aa,aj,n);
1550:       z[i] = sum;
1551:     }
1552: #endif
1553:   }
1554:   PetscLogFlops(2.0*a->nz);
1555:   VecRestoreArrayRead(xx,&x);
1556:   VecRestoreArrayPair(yy,zz,&y,&z);
1557:   return(0);
1558: }

1560: /*
1561:      Adds diagonal pointers to sparse matrix structure.
1562: */
1563: PetscErrorCode MatMarkDiagonal_SeqAIJ(Mat A)
1564: {
1565:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1567:   PetscInt       i,j,m = A->rmap->n;

1570:   if (!a->diag) {
1571:     PetscMalloc1(m,&a->diag);
1572:     PetscLogObjectMemory((PetscObject)A, m*sizeof(PetscInt));
1573:   }
1574:   for (i=0; i<A->rmap->n; i++) {
1575:     a->diag[i] = a->i[i+1];
1576:     for (j=a->i[i]; j<a->i[i+1]; j++) {
1577:       if (a->j[j] == i) {
1578:         a->diag[i] = j;
1579:         break;
1580:       }
1581:     }
1582:   }
1583:   return(0);
1584: }

1586: PetscErrorCode MatShift_SeqAIJ(Mat A,PetscScalar v)
1587: {
1588:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1589:   const PetscInt    *diag = (const PetscInt*)a->diag;
1590:   const PetscInt    *ii = (const PetscInt*) a->i;
1591:   PetscInt          i,*mdiag = NULL;
1592:   PetscErrorCode    ierr;
1593:   PetscInt          cnt = 0; /* how many diagonals are missing */

1596:   if (!A->preallocated || !a->nz) {
1597:     MatSeqAIJSetPreallocation(A,1,NULL);
1598:     MatShift_Basic(A,v);
1599:     return(0);
1600:   }

1602:   if (a->diagonaldense) {
1603:     cnt = 0;
1604:   } else {
1605:     PetscCalloc1(A->rmap->n,&mdiag);
1606:     for (i=0; i<A->rmap->n; i++) {
1607:       if (diag[i] >= ii[i+1]) {
1608:         cnt++;
1609:         mdiag[i] = 1;
1610:       }
1611:     }
1612:   }
1613:   if (!cnt) {
1614:     MatShift_Basic(A,v);
1615:   } else {
1616:     PetscScalar *olda = a->a;  /* preserve pointers to current matrix nonzeros structure and values */
1617:     PetscInt    *oldj = a->j, *oldi = a->i;
1618:     PetscBool   singlemalloc = a->singlemalloc,free_a = a->free_a,free_ij = a->free_ij;

1620:     a->a = NULL;
1621:     a->j = NULL;
1622:     a->i = NULL;
1623:     /* increase the values in imax for each row where a diagonal is being inserted then reallocate the matrix data structures */
1624:     for (i=0; i<A->rmap->n; i++) {
1625:       a->imax[i] += mdiag[i];
1626:       a->imax[i] = PetscMin(a->imax[i],A->cmap->n);
1627:     }
1628:     MatSeqAIJSetPreallocation_SeqAIJ(A,0,a->imax);

1630:     /* copy old values into new matrix data structure */
1631:     for (i=0; i<A->rmap->n; i++) {
1632:       MatSetValues(A,1,&i,a->imax[i] - mdiag[i],&oldj[oldi[i]],&olda[oldi[i]],ADD_VALUES);
1633:       if (i < A->cmap->n) {
1634:         MatSetValue(A,i,i,v,ADD_VALUES);
1635:       }
1636:     }
1637:     MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);
1638:     MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);
1639:     if (singlemalloc) {
1640:       PetscFree3(olda,oldj,oldi);
1641:     } else {
1642:       if (free_a)  {PetscFree(olda);}
1643:       if (free_ij) {PetscFree(oldj);}
1644:       if (free_ij) {PetscFree(oldi);}
1645:     }
1646:   }
1647:   PetscFree(mdiag);
1648:   a->diagonaldense = PETSC_TRUE;
1649:   return(0);
1650: }

1652: /*
1653:      Checks for missing diagonals
1654: */
1655: PetscErrorCode MatMissingDiagonal_SeqAIJ(Mat A,PetscBool  *missing,PetscInt *d)
1656: {
1657:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
1658:   PetscInt       *diag,*ii = a->i,i;

1662:   *missing = PETSC_FALSE;
1663:   if (A->rmap->n > 0 && !ii) {
1664:     *missing = PETSC_TRUE;
1665:     if (d) *d = 0;
1666:     PetscInfo(A,"Matrix has no entries therefore is missing diagonal\n");
1667:   } else {
1668:     PetscInt n;
1669:     n = PetscMin(A->rmap->n, A->cmap->n);
1670:     diag = a->diag;
1671:     for (i=0; i<n; i++) {
1672:       if (diag[i] >= ii[i+1]) {
1673:         *missing = PETSC_TRUE;
1674:         if (d) *d = i;
1675:         PetscInfo1(A,"Matrix is missing diagonal number %D\n",i);
1676:         break;
1677:       }
1678:     }
1679:   }
1680:   return(0);
1681: }

1683:  #include <petscblaslapack.h>
1684:  #include <petsc/private/kernels/blockinvert.h>

1686: /*
1687:     Note that values is allocated externally by the PC and then passed into this routine
1688: */
1689: PetscErrorCode MatInvertVariableBlockDiagonal_SeqAIJ(Mat A,PetscInt nblocks,const PetscInt *bsizes,PetscScalar *diag)
1690: {
1691:   PetscErrorCode  ierr;
1692:   PetscInt        n = A->rmap->n, i, ncnt = 0, *indx,j,bsizemax = 0,*v_pivots;
1693:   PetscBool       allowzeropivot,zeropivotdetected=PETSC_FALSE;
1694:   const PetscReal shift = 0.0;
1695:   PetscInt        ipvt[5];
1696:   PetscScalar     work[25],*v_work;

1699:   allowzeropivot = PetscNot(A->erroriffailure);
1700:   for (i=0; i<nblocks; i++) ncnt += bsizes[i];
1701:   if (ncnt != n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Total blocksizes %D doesn't match number matrix rows %D",ncnt,n);
1702:   for (i=0; i<nblocks; i++) {
1703:     bsizemax = PetscMax(bsizemax,bsizes[i]);
1704:   }
1705:   PetscMalloc1(bsizemax,&indx);
1706:   if (bsizemax > 7) {
1707:     PetscMalloc2(bsizemax,&v_work,bsizemax,&v_pivots);
1708:   }
1709:   ncnt = 0;
1710:   for (i=0; i<nblocks; i++) {
1711:     for (j=0; j<bsizes[i]; j++) indx[j] = ncnt+j;
1712:     MatGetValues(A,bsizes[i],indx,bsizes[i],indx,diag);
1713:     switch (bsizes[i]) {
1714:     case 1:
1715:       *diag = 1.0/(*diag);
1716:       break;
1717:     case 2:
1718:       PetscKernel_A_gets_inverse_A_2(diag,shift,allowzeropivot,&zeropivotdetected);
1719:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1720:       PetscKernel_A_gets_transpose_A_2(diag);
1721:       break;
1722:     case 3:
1723:       PetscKernel_A_gets_inverse_A_3(diag,shift,allowzeropivot,&zeropivotdetected);
1724:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1725:       PetscKernel_A_gets_transpose_A_3(diag);
1726:       break;
1727:     case 4:
1728:       PetscKernel_A_gets_inverse_A_4(diag,shift,allowzeropivot,&zeropivotdetected);
1729:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1730:       PetscKernel_A_gets_transpose_A_4(diag);
1731:       break;
1732:     case 5:
1733:       PetscKernel_A_gets_inverse_A_5(diag,ipvt,work,shift,allowzeropivot,&zeropivotdetected);
1734:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1735:       PetscKernel_A_gets_transpose_A_5(diag);
1736:       break;
1737:     case 6:
1738:       PetscKernel_A_gets_inverse_A_6(diag,shift,allowzeropivot,&zeropivotdetected);
1739:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1740:       PetscKernel_A_gets_transpose_A_6(diag);
1741:       break;
1742:     case 7:
1743:       PetscKernel_A_gets_inverse_A_7(diag,shift,allowzeropivot,&zeropivotdetected);
1744:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1745:       PetscKernel_A_gets_transpose_A_7(diag);
1746:       break;
1747:     default:
1748:       PetscKernel_A_gets_inverse_A(bsizes[i],diag,v_pivots,v_work,allowzeropivot,&zeropivotdetected);
1749:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1750:       PetscKernel_A_gets_transpose_A_N(diag,bsizes[i]);
1751:     }
1752:     ncnt   += bsizes[i];
1753:     diag += bsizes[i]*bsizes[i];
1754:   }
1755:   if (bsizemax > 7) {
1756:     PetscFree2(v_work,v_pivots);
1757:   }
1758:   PetscFree(indx);
1759:   return(0);
1760: }

1762: /*
1763:    Negative shift indicates do not generate an error if there is a zero diagonal, just invert it anyways
1764: */
1765: PetscErrorCode  MatInvertDiagonal_SeqAIJ(Mat A,PetscScalar omega,PetscScalar fshift)
1766: {
1767:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*) A->data;
1769:   PetscInt       i,*diag,m = A->rmap->n;
1770:   MatScalar      *v = a->a;
1771:   PetscScalar    *idiag,*mdiag;

1774:   if (a->idiagvalid) return(0);
1775:   MatMarkDiagonal_SeqAIJ(A);
1776:   diag = a->diag;
1777:   if (!a->idiag) {
1778:     PetscMalloc3(m,&a->idiag,m,&a->mdiag,m,&a->ssor_work);
1779:     PetscLogObjectMemory((PetscObject)A, 3*m*sizeof(PetscScalar));
1780:     v    = a->a;
1781:   }
1782:   mdiag = a->mdiag;
1783:   idiag = a->idiag;

1785:   if (omega == 1.0 && PetscRealPart(fshift) <= 0.0) {
1786:     for (i=0; i<m; i++) {
1787:       mdiag[i] = v[diag[i]];
1788:       if (!PetscAbsScalar(mdiag[i])) { /* zero diagonal */
1789:         if (PetscRealPart(fshift)) {
1790:           PetscInfo1(A,"Zero diagonal on row %D\n",i);
1791:           A->factorerrortype             = MAT_FACTOR_NUMERIC_ZEROPIVOT;
1792:           A->factorerror_zeropivot_value = 0.0;
1793:           A->factorerror_zeropivot_row   = i;
1794:         } else SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"Zero diagonal on row %D",i);
1795:       }
1796:       idiag[i] = 1.0/v[diag[i]];
1797:     }
1798:     PetscLogFlops(m);
1799:   } else {
1800:     for (i=0; i<m; i++) {
1801:       mdiag[i] = v[diag[i]];
1802:       idiag[i] = omega/(fshift + v[diag[i]]);
1803:     }
1804:     PetscLogFlops(2.0*m);
1805:   }
1806:   a->idiagvalid = PETSC_TRUE;
1807:   return(0);
1808: }

1810: #include <../src/mat/impls/aij/seq/ftn-kernels/frelax.h>
1811: PetscErrorCode MatSOR_SeqAIJ(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
1812: {
1813:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
1814:   PetscScalar       *x,d,sum,*t,scale;
1815:   const MatScalar   *v,*idiag=0,*mdiag;
1816:   const PetscScalar *b, *bs,*xb, *ts;
1817:   PetscErrorCode    ierr;
1818:   PetscInt          n,m = A->rmap->n,i;
1819:   const PetscInt    *idx,*diag;

1822:   its = its*lits;

1824:   if (fshift != a->fshift || omega != a->omega) a->idiagvalid = PETSC_FALSE; /* must recompute idiag[] */
1825:   if (!a->idiagvalid) {MatInvertDiagonal_SeqAIJ(A,omega,fshift);}
1826:   a->fshift = fshift;
1827:   a->omega  = omega;

1829:   diag  = a->diag;
1830:   t     = a->ssor_work;
1831:   idiag = a->idiag;
1832:   mdiag = a->mdiag;

1834:   VecGetArray(xx,&x);
1835:   VecGetArrayRead(bb,&b);
1836:   /* We count flops by assuming the upper triangular and lower triangular parts have the same number of nonzeros */
1837:   if (flag == SOR_APPLY_UPPER) {
1838:     /* apply (U + D/omega) to the vector */
1839:     bs = b;
1840:     for (i=0; i<m; i++) {
1841:       d   = fshift + mdiag[i];
1842:       n   = a->i[i+1] - diag[i] - 1;
1843:       idx = a->j + diag[i] + 1;
1844:       v   = a->a + diag[i] + 1;
1845:       sum = b[i]*d/omega;
1846:       PetscSparseDensePlusDot(sum,bs,v,idx,n);
1847:       x[i] = sum;
1848:     }
1849:     VecRestoreArray(xx,&x);
1850:     VecRestoreArrayRead(bb,&b);
1851:     PetscLogFlops(a->nz);
1852:     return(0);
1853:   }

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

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

1862:     to a vector efficiently using Eisenstat's trick.
1863:     */
1864:     scale = (2.0/omega) - 1.0;

1866:     /*  x = (E + U)^{-1} b */
1867:     for (i=m-1; i>=0; i--) {
1868:       n   = a->i[i+1] - diag[i] - 1;
1869:       idx = a->j + diag[i] + 1;
1870:       v   = a->a + diag[i] + 1;
1871:       sum = b[i];
1872:       PetscSparseDenseMinusDot(sum,x,v,idx,n);
1873:       x[i] = sum*idiag[i];
1874:     }

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

1880:     /*  t = (E + L)^{-1}t */
1881:     ts   = t;
1882:     diag = a->diag;
1883:     for (i=0; i<m; i++) {
1884:       n   = diag[i] - a->i[i];
1885:       idx = a->j + a->i[i];
1886:       v   = a->a + a->i[i];
1887:       sum = t[i];
1888:       PetscSparseDenseMinusDot(sum,ts,v,idx,n);
1889:       t[i] = sum*idiag[i];
1890:       /*  x = x + t */
1891:       x[i] += t[i];
1892:     }

1894:     PetscLogFlops(6.0*m-1 + 2.0*a->nz);
1895:     VecRestoreArray(xx,&x);
1896:     VecRestoreArrayRead(bb,&b);
1897:     return(0);
1898:   }
1899:   if (flag & SOR_ZERO_INITIAL_GUESS) {
1900:     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
1901:       for (i=0; i<m; i++) {
1902:         n   = diag[i] - a->i[i];
1903:         idx = a->j + a->i[i];
1904:         v   = a->a + a->i[i];
1905:         sum = b[i];
1906:         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1907:         t[i] = sum;
1908:         x[i] = sum*idiag[i];
1909:       }
1910:       xb   = t;
1911:       PetscLogFlops(a->nz);
1912:     } else xb = b;
1913:     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
1914:       for (i=m-1; i>=0; i--) {
1915:         n   = a->i[i+1] - diag[i] - 1;
1916:         idx = a->j + diag[i] + 1;
1917:         v   = a->a + diag[i] + 1;
1918:         sum = xb[i];
1919:         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1920:         if (xb == b) {
1921:           x[i] = sum*idiag[i];
1922:         } else {
1923:           x[i] = (1-omega)*x[i] + sum*idiag[i];  /* omega in idiag */
1924:         }
1925:       }
1926:       PetscLogFlops(a->nz); /* assumes 1/2 in upper */
1927:     }
1928:     its--;
1929:   }
1930:   while (its--) {
1931:     if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
1932:       for (i=0; i<m; i++) {
1933:         /* lower */
1934:         n   = diag[i] - a->i[i];
1935:         idx = a->j + a->i[i];
1936:         v   = a->a + a->i[i];
1937:         sum = b[i];
1938:         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1939:         t[i] = sum;             /* save application of the lower-triangular part */
1940:         /* upper */
1941:         n   = a->i[i+1] - diag[i] - 1;
1942:         idx = a->j + diag[i] + 1;
1943:         v   = a->a + diag[i] + 1;
1944:         PetscSparseDenseMinusDot(sum,x,v,idx,n);
1945:         x[i] = (1. - omega)*x[i] + sum*idiag[i]; /* omega in idiag */
1946:       }
1947:       xb   = t;
1948:       PetscLogFlops(2.0*a->nz);
1949:     } else xb = b;
1950:     if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
1951:       for (i=m-1; i>=0; i--) {
1952:         sum = xb[i];
1953:         if (xb == b) {
1954:           /* whole matrix (no checkpointing available) */
1955:           n   = a->i[i+1] - a->i[i];
1956:           idx = a->j + a->i[i];
1957:           v   = a->a + a->i[i];
1958:           PetscSparseDenseMinusDot(sum,x,v,idx,n);
1959:           x[i] = (1. - omega)*x[i] + (sum + mdiag[i]*x[i])*idiag[i];
1960:         } else { /* lower-triangular part has been saved, so only apply upper-triangular */
1961:           n   = a->i[i+1] - diag[i] - 1;
1962:           idx = a->j + diag[i] + 1;
1963:           v   = a->a + diag[i] + 1;
1964:           PetscSparseDenseMinusDot(sum,x,v,idx,n);
1965:           x[i] = (1. - omega)*x[i] + sum*idiag[i];  /* omega in idiag */
1966:         }
1967:       }
1968:       if (xb == b) {
1969:         PetscLogFlops(2.0*a->nz);
1970:       } else {
1971:         PetscLogFlops(a->nz); /* assumes 1/2 in upper */
1972:       }
1973:     }
1974:   }
1975:   VecRestoreArray(xx,&x);
1976:   VecRestoreArrayRead(bb,&b);
1977:   return(0);
1978: }


1981: PetscErrorCode MatGetInfo_SeqAIJ(Mat A,MatInfoType flag,MatInfo *info)
1982: {
1983:   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;

1986:   info->block_size   = 1.0;
1987:   info->nz_allocated = (double)a->maxnz;
1988:   info->nz_used      = (double)a->nz;
1989:   info->nz_unneeded  = (double)(a->maxnz - a->nz);
1990:   info->assemblies   = (double)A->num_ass;
1991:   info->mallocs      = (double)A->info.mallocs;
1992:   info->memory       = ((PetscObject)A)->mem;
1993:   if (A->factortype) {
1994:     info->fill_ratio_given  = A->info.fill_ratio_given;
1995:     info->fill_ratio_needed = A->info.fill_ratio_needed;
1996:     info->factor_mallocs    = A->info.factor_mallocs;
1997:   } else {
1998:     info->fill_ratio_given  = 0;
1999:     info->fill_ratio_needed = 0;
2000:     info->factor_mallocs    = 0;
2001:   }
2002:   return(0);
2003: }

2005: PetscErrorCode MatZeroRows_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
2006: {
2007:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
2008:   PetscInt          i,m = A->rmap->n - 1;
2009:   PetscErrorCode    ierr;
2010:   const PetscScalar *xx;
2011:   PetscScalar       *bb;
2012:   PetscInt          d = 0;

2015:   if (x && b) {
2016:     VecGetArrayRead(x,&xx);
2017:     VecGetArray(b,&bb);
2018:     for (i=0; i<N; i++) {
2019:       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
2020:       if (rows[i] >= A->cmap->n) continue;
2021:       bb[rows[i]] = diag*xx[rows[i]];
2022:     }
2023:     VecRestoreArrayRead(x,&xx);
2024:     VecRestoreArray(b,&bb);
2025:   }

2027:   if (a->keepnonzeropattern) {
2028:     for (i=0; i<N; i++) {
2029:       if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
2030:       PetscArrayzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]);
2031:     }
2032:     if (diag != 0.0) {
2033:       for (i=0; i<N; i++) {
2034:         d = rows[i];
2035:         if (rows[i] >= A->cmap->n) continue;
2036:         if (a->diag[d] >= a->i[d+1]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in the zeroed row %D",d);
2037:       }
2038:       for (i=0; i<N; i++) {
2039:         if (rows[i] >= A->cmap->n) continue;
2040:         a->a[a->diag[rows[i]]] = diag;
2041:       }
2042:     }
2043:   } else {
2044:     if (diag != 0.0) {
2045:       for (i=0; i<N; i++) {
2046:         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
2047:         if (a->ilen[rows[i]] > 0) {
2048:           if (rows[i] >= A->cmap->n) {
2049:             a->ilen[rows[i]] = 0;
2050:           } else {
2051:             a->ilen[rows[i]]    = 1;
2052:             a->a[a->i[rows[i]]] = diag;
2053:             a->j[a->i[rows[i]]] = rows[i];
2054:           }
2055:         } else if (rows[i] < A->cmap->n) { /* in case row was completely empty */
2056:           MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);
2057:         }
2058:       }
2059:     } else {
2060:       for (i=0; i<N; i++) {
2061:         if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
2062:         a->ilen[rows[i]] = 0;
2063:       }
2064:     }
2065:     A->nonzerostate++;
2066:   }
2067: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
2068:   if (A->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) A->valid_GPU_matrix = PETSC_OFFLOAD_CPU;
2069: #endif
2070:   (*A->ops->assemblyend)(A,MAT_FINAL_ASSEMBLY);
2071:   return(0);
2072: }

2074: PetscErrorCode MatZeroRowsColumns_SeqAIJ(Mat A,PetscInt N,const PetscInt rows[],PetscScalar diag,Vec x,Vec b)
2075: {
2076:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
2077:   PetscInt          i,j,m = A->rmap->n - 1,d = 0;
2078:   PetscErrorCode    ierr;
2079:   PetscBool         missing,*zeroed,vecs = PETSC_FALSE;
2080:   const PetscScalar *xx;
2081:   PetscScalar       *bb;

2084:   if (x && b) {
2085:     VecGetArrayRead(x,&xx);
2086:     VecGetArray(b,&bb);
2087:     vecs = PETSC_TRUE;
2088:   }
2089:   PetscCalloc1(A->rmap->n,&zeroed);
2090:   for (i=0; i<N; i++) {
2091:     if (rows[i] < 0 || rows[i] > m) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range", rows[i]);
2092:     PetscArrayzero(&a->a[a->i[rows[i]]],a->ilen[rows[i]]);

2094:     zeroed[rows[i]] = PETSC_TRUE;
2095:   }
2096:   for (i=0; i<A->rmap->n; i++) {
2097:     if (!zeroed[i]) {
2098:       for (j=a->i[i]; j<a->i[i+1]; j++) {
2099:         if (a->j[j] < A->rmap->n && zeroed[a->j[j]]) {
2100:           if (vecs) bb[i] -= a->a[j]*xx[a->j[j]];
2101:           a->a[j] = 0.0;
2102:         }
2103:       }
2104:     } else if (vecs && i < A->cmap->N) bb[i] = diag*xx[i];
2105:   }
2106:   if (x && b) {
2107:     VecRestoreArrayRead(x,&xx);
2108:     VecRestoreArray(b,&bb);
2109:   }
2110:   PetscFree(zeroed);
2111:   if (diag != 0.0) {
2112:     MatMissingDiagonal_SeqAIJ(A,&missing,&d);
2113:     if (missing) {
2114:       for (i=0; i<N; i++) {
2115:         if (rows[i] >= A->cmap->N) continue;
2116:         if (a->nonew && rows[i] >= d) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Matrix is missing diagonal entry in row %D (%D)",d,rows[i]);
2117:         MatSetValues_SeqAIJ(A,1,&rows[i],1,&rows[i],&diag,INSERT_VALUES);
2118:       }
2119:     } else {
2120:       for (i=0; i<N; i++) {
2121:         a->a[a->diag[rows[i]]] = diag;
2122:       }
2123:     }
2124:   }
2125: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
2126:   if (A->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) A->valid_GPU_matrix = PETSC_OFFLOAD_CPU;
2127: #endif
2128:   (*A->ops->assemblyend)(A,MAT_FINAL_ASSEMBLY);
2129:   return(0);
2130: }

2132: PetscErrorCode MatGetRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
2133: {
2134:   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;
2135:   PetscInt   *itmp;

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

2140:   *nz = a->i[row+1] - a->i[row];
2141:   if (v) *v = a->a + a->i[row];
2142:   if (idx) {
2143:     itmp = a->j + a->i[row];
2144:     if (*nz) *idx = itmp;
2145:     else *idx = 0;
2146:   }
2147:   return(0);
2148: }

2150: /* remove this function? */
2151: PetscErrorCode MatRestoreRow_SeqAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
2152: {
2154:   return(0);
2155: }

2157: PetscErrorCode MatNorm_SeqAIJ(Mat A,NormType type,PetscReal *nrm)
2158: {
2159:   Mat_SeqAIJ     *a  = (Mat_SeqAIJ*)A->data;
2160:   MatScalar      *v  = a->a;
2161:   PetscReal      sum = 0.0;
2163:   PetscInt       i,j;

2166:   if (type == NORM_FROBENIUS) {
2167: #if defined(PETSC_USE_REAL___FP16)
2168:     PetscBLASInt one = 1,nz = a->nz;
2169:     *nrm = BLASnrm2_(&nz,v,&one);
2170: #else
2171:     for (i=0; i<a->nz; i++) {
2172:       sum += PetscRealPart(PetscConj(*v)*(*v)); v++;
2173:     }
2174:     *nrm = PetscSqrtReal(sum);
2175: #endif
2176:     PetscLogFlops(2*a->nz);
2177:   } else if (type == NORM_1) {
2178:     PetscReal *tmp;
2179:     PetscInt  *jj = a->j;
2180:     PetscCalloc1(A->cmap->n+1,&tmp);
2181:     *nrm = 0.0;
2182:     for (j=0; j<a->nz; j++) {
2183:       tmp[*jj++] += PetscAbsScalar(*v);  v++;
2184:     }
2185:     for (j=0; j<A->cmap->n; j++) {
2186:       if (tmp[j] > *nrm) *nrm = tmp[j];
2187:     }
2188:     PetscFree(tmp);
2189:     PetscLogFlops(PetscMax(a->nz-1,0));
2190:   } else if (type == NORM_INFINITY) {
2191:     *nrm = 0.0;
2192:     for (j=0; j<A->rmap->n; j++) {
2193:       v   = a->a + a->i[j];
2194:       sum = 0.0;
2195:       for (i=0; i<a->i[j+1]-a->i[j]; i++) {
2196:         sum += PetscAbsScalar(*v); v++;
2197:       }
2198:       if (sum > *nrm) *nrm = sum;
2199:     }
2200:     PetscLogFlops(PetscMax(a->nz-1,0));
2201:   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for two norm");
2202:   return(0);
2203: }

2205: /* Merged from MatGetSymbolicTranspose_SeqAIJ() - replace MatGetSymbolicTranspose_SeqAIJ()? */
2206: PetscErrorCode MatTransposeSymbolic_SeqAIJ(Mat A,Mat *B)
2207: {
2209:   PetscInt       i,j,anzj;
2210:   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data,*b;
2211:   PetscInt       an=A->cmap->N,am=A->rmap->N;
2212:   PetscInt       *ati,*atj,*atfill,*ai=a->i,*aj=a->j;

2215:   /* Allocate space for symbolic transpose info and work array */
2216:   PetscCalloc1(an+1,&ati);
2217:   PetscMalloc1(ai[am],&atj);
2218:   PetscMalloc1(an,&atfill);

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

2226:   /* Copy ati into atfill so we have locations of the next free space in atj */
2227:   PetscArraycpy(atfill,ati,an);

2229:   /* Walk through A row-wise and mark nonzero entries of A^T. */
2230:   for (i=0;i<am;i++) {
2231:     anzj = ai[i+1] - ai[i];
2232:     for (j=0;j<anzj;j++) {
2233:       atj[atfill[*aj]] = i;
2234:       atfill[*aj++]   += 1;
2235:     }
2236:   }

2238:   /* Clean up temporary space and complete requests. */
2239:   PetscFree(atfill);
2240:   MatCreateSeqAIJWithArrays(PetscObjectComm((PetscObject)A),an,am,ati,atj,NULL,B);
2241:   MatSetBlockSizes(*B,PetscAbs(A->cmap->bs),PetscAbs(A->rmap->bs));
2242:   MatSetType(*B,((PetscObject)A)->type_name);

2244:   b          = (Mat_SeqAIJ*)((*B)->data);
2245:   b->free_a  = PETSC_FALSE;
2246:   b->free_ij = PETSC_TRUE;
2247:   b->nonew   = 0;
2248:   return(0);
2249: }

2251: PetscErrorCode  MatIsTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
2252: {
2253:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) B->data;
2254:   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
2255:   MatScalar      *va,*vb;
2257:   PetscInt       ma,na,mb,nb, i;

2260:   MatGetSize(A,&ma,&na);
2261:   MatGetSize(B,&mb,&nb);
2262:   if (ma!=nb || na!=mb) {
2263:     *f = PETSC_FALSE;
2264:     return(0);
2265:   }
2266:   aii  = aij->i; bii = bij->i;
2267:   adx  = aij->j; bdx = bij->j;
2268:   va   = aij->a; vb = bij->a;
2269:   PetscMalloc1(ma,&aptr);
2270:   PetscMalloc1(mb,&bptr);
2271:   for (i=0; i<ma; i++) aptr[i] = aii[i];
2272:   for (i=0; i<mb; i++) bptr[i] = bii[i];

2274:   *f = PETSC_TRUE;
2275:   for (i=0; i<ma; i++) {
2276:     while (aptr[i]<aii[i+1]) {
2277:       PetscInt    idc,idr;
2278:       PetscScalar vc,vr;
2279:       /* column/row index/value */
2280:       idc = adx[aptr[i]];
2281:       idr = bdx[bptr[idc]];
2282:       vc  = va[aptr[i]];
2283:       vr  = vb[bptr[idc]];
2284:       if (i!=idr || PetscAbsScalar(vc-vr) > tol) {
2285:         *f = PETSC_FALSE;
2286:         goto done;
2287:       } else {
2288:         aptr[i]++;
2289:         if (B || i!=idc) bptr[idc]++;
2290:       }
2291:     }
2292:   }
2293: done:
2294:   PetscFree(aptr);
2295:   PetscFree(bptr);
2296:   return(0);
2297: }

2299: PetscErrorCode  MatIsHermitianTranspose_SeqAIJ(Mat A,Mat B,PetscReal tol,PetscBool  *f)
2300: {
2301:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*) A->data,*bij = (Mat_SeqAIJ*) B->data;
2302:   PetscInt       *adx,*bdx,*aii,*bii,*aptr,*bptr;
2303:   MatScalar      *va,*vb;
2305:   PetscInt       ma,na,mb,nb, i;

2308:   MatGetSize(A,&ma,&na);
2309:   MatGetSize(B,&mb,&nb);
2310:   if (ma!=nb || na!=mb) {
2311:     *f = PETSC_FALSE;
2312:     return(0);
2313:   }
2314:   aii  = aij->i; bii = bij->i;
2315:   adx  = aij->j; bdx = bij->j;
2316:   va   = aij->a; vb = bij->a;
2317:   PetscMalloc1(ma,&aptr);
2318:   PetscMalloc1(mb,&bptr);
2319:   for (i=0; i<ma; i++) aptr[i] = aii[i];
2320:   for (i=0; i<mb; i++) bptr[i] = bii[i];

2322:   *f = PETSC_TRUE;
2323:   for (i=0; i<ma; i++) {
2324:     while (aptr[i]<aii[i+1]) {
2325:       PetscInt    idc,idr;
2326:       PetscScalar vc,vr;
2327:       /* column/row index/value */
2328:       idc = adx[aptr[i]];
2329:       idr = bdx[bptr[idc]];
2330:       vc  = va[aptr[i]];
2331:       vr  = vb[bptr[idc]];
2332:       if (i!=idr || PetscAbsScalar(vc-PetscConj(vr)) > tol) {
2333:         *f = PETSC_FALSE;
2334:         goto done;
2335:       } else {
2336:         aptr[i]++;
2337:         if (B || i!=idc) bptr[idc]++;
2338:       }
2339:     }
2340:   }
2341: done:
2342:   PetscFree(aptr);
2343:   PetscFree(bptr);
2344:   return(0);
2345: }

2347: PetscErrorCode MatIsSymmetric_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
2348: {

2352:   MatIsTranspose_SeqAIJ(A,A,tol,f);
2353:   return(0);
2354: }

2356: PetscErrorCode MatIsHermitian_SeqAIJ(Mat A,PetscReal tol,PetscBool  *f)
2357: {

2361:   MatIsHermitianTranspose_SeqAIJ(A,A,tol,f);
2362:   return(0);
2363: }

2365: PetscErrorCode MatDiagonalScale_SeqAIJ(Mat A,Vec ll,Vec rr)
2366: {
2367:   Mat_SeqAIJ        *a = (Mat_SeqAIJ*)A->data;
2368:   const PetscScalar *l,*r;
2369:   PetscScalar       x;
2370:   MatScalar         *v;
2371:   PetscErrorCode    ierr;
2372:   PetscInt          i,j,m = A->rmap->n,n = A->cmap->n,M,nz = a->nz;
2373:   const PetscInt    *jj;

2376:   if (ll) {
2377:     /* The local size is used so that VecMPI can be passed to this routine
2378:        by MatDiagonalScale_MPIAIJ */
2379:     VecGetLocalSize(ll,&m);
2380:     if (m != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Left scaling vector wrong length");
2381:     VecGetArrayRead(ll,&l);
2382:     v    = a->a;
2383:     for (i=0; i<m; i++) {
2384:       x = l[i];
2385:       M = a->i[i+1] - a->i[i];
2386:       for (j=0; j<M; j++) (*v++) *= x;
2387:     }
2388:     VecRestoreArrayRead(ll,&l);
2389:     PetscLogFlops(nz);
2390:   }
2391:   if (rr) {
2392:     VecGetLocalSize(rr,&n);
2393:     if (n != A->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Right scaling vector wrong length");
2394:     VecGetArrayRead(rr,&r);
2395:     v    = a->a; jj = a->j;
2396:     for (i=0; i<nz; i++) (*v++) *= r[*jj++];
2397:     VecRestoreArrayRead(rr,&r);
2398:     PetscLogFlops(nz);
2399:   }
2400:   MatSeqAIJInvalidateDiagonal(A);
2401: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
2402:   if (A->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) A->valid_GPU_matrix = PETSC_OFFLOAD_CPU;
2403: #endif
2404:   return(0);
2405: }

2407: PetscErrorCode MatCreateSubMatrix_SeqAIJ(Mat A,IS isrow,IS iscol,PetscInt csize,MatReuse scall,Mat *B)
2408: {
2409:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*c;
2411:   PetscInt       *smap,i,k,kstart,kend,oldcols = A->cmap->n,*lens;
2412:   PetscInt       row,mat_i,*mat_j,tcol,first,step,*mat_ilen,sum,lensi;
2413:   const PetscInt *irow,*icol;
2414:   PetscInt       nrows,ncols;
2415:   PetscInt       *starts,*j_new,*i_new,*aj = a->j,*ai = a->i,ii,*ailen = a->ilen;
2416:   MatScalar      *a_new,*mat_a;
2417:   Mat            C;
2418:   PetscBool      stride;


2422:   ISGetIndices(isrow,&irow);
2423:   ISGetLocalSize(isrow,&nrows);
2424:   ISGetLocalSize(iscol,&ncols);

2426:   PetscObjectTypeCompare((PetscObject)iscol,ISSTRIDE,&stride);
2427:   if (stride) {
2428:     ISStrideGetInfo(iscol,&first,&step);
2429:   } else {
2430:     first = 0;
2431:     step  = 0;
2432:   }
2433:   if (stride && step == 1) {
2434:     /* special case of contiguous rows */
2435:     PetscMalloc2(nrows,&lens,nrows,&starts);
2436:     /* loop over new rows determining lens and starting points */
2437:     for (i=0; i<nrows; i++) {
2438:       kstart = ai[irow[i]];
2439:       kend   = kstart + ailen[irow[i]];
2440:       starts[i] = kstart;
2441:       for (k=kstart; k<kend; k++) {
2442:         if (aj[k] >= first) {
2443:           starts[i] = k;
2444:           break;
2445:         }
2446:       }
2447:       sum = 0;
2448:       while (k < kend) {
2449:         if (aj[k++] >= first+ncols) break;
2450:         sum++;
2451:       }
2452:       lens[i] = sum;
2453:     }
2454:     /* create submatrix */
2455:     if (scall == MAT_REUSE_MATRIX) {
2456:       PetscInt n_cols,n_rows;
2457:       MatGetSize(*B,&n_rows,&n_cols);
2458:       if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
2459:       MatZeroEntries(*B);
2460:       C    = *B;
2461:     } else {
2462:       PetscInt rbs,cbs;
2463:       MatCreate(PetscObjectComm((PetscObject)A),&C);
2464:       MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);
2465:       ISGetBlockSize(isrow,&rbs);
2466:       ISGetBlockSize(iscol,&cbs);
2467:       MatSetBlockSizes(C,rbs,cbs);
2468:       MatSetType(C,((PetscObject)A)->type_name);
2469:       MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);
2470:     }
2471:     c = (Mat_SeqAIJ*)C->data;

2473:     /* loop over rows inserting into submatrix */
2474:     a_new = c->a;
2475:     j_new = c->j;
2476:     i_new = c->i;

2478:     for (i=0; i<nrows; i++) {
2479:       ii    = starts[i];
2480:       lensi = lens[i];
2481:       for (k=0; k<lensi; k++) {
2482:         *j_new++ = aj[ii+k] - first;
2483:       }
2484:       PetscArraycpy(a_new,a->a + starts[i],lensi);
2485:       a_new     += lensi;
2486:       i_new[i+1] = i_new[i] + lensi;
2487:       c->ilen[i] = lensi;
2488:     }
2489:     PetscFree2(lens,starts);
2490:   } else {
2491:     ISGetIndices(iscol,&icol);
2492:     PetscCalloc1(oldcols,&smap);
2493:     PetscMalloc1(1+nrows,&lens);
2494:     for (i=0; i<ncols; i++) {
2495: #if defined(PETSC_USE_DEBUG)
2496:       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);
2497: #endif
2498:       smap[icol[i]] = i+1;
2499:     }

2501:     /* determine lens of each row */
2502:     for (i=0; i<nrows; i++) {
2503:       kstart  = ai[irow[i]];
2504:       kend    = kstart + a->ilen[irow[i]];
2505:       lens[i] = 0;
2506:       for (k=kstart; k<kend; k++) {
2507:         if (smap[aj[k]]) {
2508:           lens[i]++;
2509:         }
2510:       }
2511:     }
2512:     /* Create and fill new matrix */
2513:     if (scall == MAT_REUSE_MATRIX) {
2514:       PetscBool equal;

2516:       c = (Mat_SeqAIJ*)((*B)->data);
2517:       if ((*B)->rmap->n  != nrows || (*B)->cmap->n != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong size");
2518:       PetscArraycmp(c->ilen,lens,(*B)->rmap->n,&equal);
2519:       if (!equal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Cannot reuse matrix. wrong no of nonzeros");
2520:       PetscArrayzero(c->ilen,(*B)->rmap->n);
2521:       C    = *B;
2522:     } else {
2523:       PetscInt rbs,cbs;
2524:       MatCreate(PetscObjectComm((PetscObject)A),&C);
2525:       MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);
2526:       ISGetBlockSize(isrow,&rbs);
2527:       ISGetBlockSize(iscol,&cbs);
2528:       MatSetBlockSizes(C,rbs,cbs);
2529:       MatSetType(C,((PetscObject)A)->type_name);
2530:       MatSeqAIJSetPreallocation_SeqAIJ(C,0,lens);
2531:     }
2532:     c = (Mat_SeqAIJ*)(C->data);
2533:     for (i=0; i<nrows; i++) {
2534:       row      = irow[i];
2535:       kstart   = ai[row];
2536:       kend     = kstart + a->ilen[row];
2537:       mat_i    = c->i[i];
2538:       mat_j    = c->j + mat_i;
2539:       mat_a    = c->a + mat_i;
2540:       mat_ilen = c->ilen + i;
2541:       for (k=kstart; k<kend; k++) {
2542:         if ((tcol=smap[a->j[k]])) {
2543:           *mat_j++ = tcol - 1;
2544:           *mat_a++ = a->a[k];
2545:           (*mat_ilen)++;

2547:         }
2548:       }
2549:     }
2550:     /* Free work space */
2551:     ISRestoreIndices(iscol,&icol);
2552:     PetscFree(smap);
2553:     PetscFree(lens);
2554:     /* sort */
2555:     for (i = 0; i < nrows; i++) {
2556:       PetscInt ilen;

2558:       mat_i = c->i[i];
2559:       mat_j = c->j + mat_i;
2560:       mat_a = c->a + mat_i;
2561:       ilen  = c->ilen[i];
2562:       PetscSortIntWithScalarArray(ilen,mat_j,mat_a);
2563:     }
2564:   }
2565:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
2566:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);

2568:   ISRestoreIndices(isrow,&irow);
2569:   *B   = C;
2570:   return(0);
2571: }

2573: PetscErrorCode  MatGetMultiProcBlock_SeqAIJ(Mat mat,MPI_Comm subComm,MatReuse scall,Mat *subMat)
2574: {
2576:   Mat            B;

2579:   if (scall == MAT_INITIAL_MATRIX) {
2580:     MatCreate(subComm,&B);
2581:     MatSetSizes(B,mat->rmap->n,mat->cmap->n,mat->rmap->n,mat->cmap->n);
2582:     MatSetBlockSizesFromMats(B,mat,mat);
2583:     MatSetType(B,MATSEQAIJ);
2584:     MatDuplicateNoCreate_SeqAIJ(B,mat,MAT_COPY_VALUES,PETSC_TRUE);
2585:     *subMat = B;
2586:   } else {
2587:     MatCopy_SeqAIJ(mat,*subMat,SAME_NONZERO_PATTERN);
2588:   }
2589:   return(0);
2590: }

2592: PetscErrorCode MatILUFactor_SeqAIJ(Mat inA,IS row,IS col,const MatFactorInfo *info)
2593: {
2594:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)inA->data;
2596:   Mat            outA;
2597:   PetscBool      row_identity,col_identity;

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

2602:   ISIdentity(row,&row_identity);
2603:   ISIdentity(col,&col_identity);

2605:   outA             = inA;
2606:   outA->factortype = MAT_FACTOR_LU;
2607:   PetscFree(inA->solvertype);
2608:   PetscStrallocpy(MATSOLVERPETSC,&inA->solvertype);

2610:   PetscObjectReference((PetscObject)row);
2611:   ISDestroy(&a->row);

2613:   a->row = row;

2615:   PetscObjectReference((PetscObject)col);
2616:   ISDestroy(&a->col);

2618:   a->col = col;

2620:   /* Create the inverse permutation so that it can be used in MatLUFactorNumeric() */
2621:   ISDestroy(&a->icol);
2622:   ISInvertPermutation(col,PETSC_DECIDE,&a->icol);
2623:   PetscLogObjectParent((PetscObject)inA,(PetscObject)a->icol);

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

2630:   MatMarkDiagonal_SeqAIJ(inA);
2631:   if (row_identity && col_identity) {
2632:     MatLUFactorNumeric_SeqAIJ_inplace(outA,inA,info);
2633:   } else {
2634:     MatLUFactorNumeric_SeqAIJ_InplaceWithPerm(outA,inA,info);
2635:   }
2636:   return(0);
2637: }

2639: PetscErrorCode MatScale_SeqAIJ(Mat inA,PetscScalar alpha)
2640: {
2641:   Mat_SeqAIJ     *a     = (Mat_SeqAIJ*)inA->data;
2642:   PetscScalar    oalpha = alpha;
2644:   PetscBLASInt   one = 1,bnz;

2647:   PetscBLASIntCast(a->nz,&bnz);
2648:   PetscStackCallBLAS("BLASscal",BLASscal_(&bnz,&oalpha,a->a,&one));
2649:   PetscLogFlops(a->nz);
2650:   MatSeqAIJInvalidateDiagonal(inA);
2651: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
2652:   if (inA->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) inA->valid_GPU_matrix = PETSC_OFFLOAD_CPU;
2653: #endif
2654:   return(0);
2655: }

2657: PetscErrorCode MatDestroySubMatrix_Private(Mat_SubSppt *submatj)
2658: {
2660:   PetscInt       i;

2663:   if (!submatj->id) { /* delete data that are linked only to submats[id=0] */
2664:     PetscFree4(submatj->sbuf1,submatj->ptr,submatj->tmp,submatj->ctr);

2666:     for (i=0; i<submatj->nrqr; ++i) {
2667:       PetscFree(submatj->sbuf2[i]);
2668:     }
2669:     PetscFree3(submatj->sbuf2,submatj->req_size,submatj->req_source1);

2671:     if (submatj->rbuf1) {
2672:       PetscFree(submatj->rbuf1[0]);
2673:       PetscFree(submatj->rbuf1);
2674:     }

2676:     for (i=0; i<submatj->nrqs; ++i) {
2677:       PetscFree(submatj->rbuf3[i]);
2678:     }
2679:     PetscFree3(submatj->req_source2,submatj->rbuf2,submatj->rbuf3);
2680:     PetscFree(submatj->pa);
2681:   }

2683: #if defined(PETSC_USE_CTABLE)
2684:   PetscTableDestroy((PetscTable*)&submatj->rmap);
2685:   if (submatj->cmap_loc) {PetscFree(submatj->cmap_loc);}
2686:   PetscFree(submatj->rmap_loc);
2687: #else
2688:   PetscFree(submatj->rmap);
2689: #endif

2691:   if (!submatj->allcolumns) {
2692: #if defined(PETSC_USE_CTABLE)
2693:     PetscTableDestroy((PetscTable*)&submatj->cmap);
2694: #else
2695:     PetscFree(submatj->cmap);
2696: #endif
2697:   }
2698:   PetscFree(submatj->row2proc);

2700:   PetscFree(submatj);
2701:   return(0);
2702: }

2704: PetscErrorCode MatDestroySubMatrix_SeqAIJ(Mat C)
2705: {
2707:   Mat_SeqAIJ     *c = (Mat_SeqAIJ*)C->data;
2708:   Mat_SubSppt    *submatj = c->submatis1;

2711:   (*submatj->destroy)(C);
2712:   MatDestroySubMatrix_Private(submatj);
2713:   return(0);
2714: }

2716: PetscErrorCode MatDestroySubMatrices_SeqAIJ(PetscInt n,Mat *mat[])
2717: {
2719:   PetscInt       i;
2720:   Mat            C;
2721:   Mat_SeqAIJ     *c;
2722:   Mat_SubSppt    *submatj;

2725:   for (i=0; i<n; i++) {
2726:     C       = (*mat)[i];
2727:     c       = (Mat_SeqAIJ*)C->data;
2728:     submatj = c->submatis1;
2729:     if (submatj) {
2730:       if (--((PetscObject)C)->refct <= 0) {
2731:         (*submatj->destroy)(C);
2732:         MatDestroySubMatrix_Private(submatj);
2733:         PetscFree(C->defaultvectype);
2734:         PetscLayoutDestroy(&C->rmap);
2735:         PetscLayoutDestroy(&C->cmap);
2736:         PetscHeaderDestroy(&C);
2737:       }
2738:     } else {
2739:       MatDestroy(&C);
2740:     }
2741:   }

2743:   /* Destroy Dummy submatrices created for reuse */
2744:   MatDestroySubMatrices_Dummy(n,mat);

2746:   PetscFree(*mat);
2747:   return(0);
2748: }

2750: PetscErrorCode MatCreateSubMatrices_SeqAIJ(Mat A,PetscInt n,const IS irow[],const IS icol[],MatReuse scall,Mat *B[])
2751: {
2753:   PetscInt       i;

2756:   if (scall == MAT_INITIAL_MATRIX) {
2757:     PetscCalloc1(n+1,B);
2758:   }

2760:   for (i=0; i<n; i++) {
2761:     MatCreateSubMatrix_SeqAIJ(A,irow[i],icol[i],PETSC_DECIDE,scall,&(*B)[i]);
2762:   }
2763:   return(0);
2764: }

2766: PetscErrorCode MatIncreaseOverlap_SeqAIJ(Mat A,PetscInt is_max,IS is[],PetscInt ov)
2767: {
2768:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2770:   PetscInt       row,i,j,k,l,m,n,*nidx,isz,val;
2771:   const PetscInt *idx;
2772:   PetscInt       start,end,*ai,*aj;
2773:   PetscBT        table;

2776:   m  = A->rmap->n;
2777:   ai = a->i;
2778:   aj = a->j;

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

2782:   PetscMalloc1(m+1,&nidx);
2783:   PetscBTCreate(m,&table);

2785:   for (i=0; i<is_max; i++) {
2786:     /* Initialize the two local arrays */
2787:     isz  = 0;
2788:     PetscBTMemzero(m,table);

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

2794:     /* Enter these into the temp arrays. I.e., mark table[row], enter row into new index */
2795:     for (j=0; j<n; ++j) {
2796:       if (!PetscBTLookupSet(table,idx[j])) nidx[isz++] = idx[j];
2797:     }
2798:     ISRestoreIndices(is[i],&idx);
2799:     ISDestroy(&is[i]);

2801:     k = 0;
2802:     for (j=0; j<ov; j++) { /* for each overlap */
2803:       n = isz;
2804:       for (; k<n; k++) { /* do only those rows in nidx[k], which are not done yet */
2805:         row   = nidx[k];
2806:         start = ai[row];
2807:         end   = ai[row+1];
2808:         for (l = start; l<end; l++) {
2809:           val = aj[l];
2810:           if (!PetscBTLookupSet(table,val)) nidx[isz++] = val;
2811:         }
2812:       }
2813:     }
2814:     ISCreateGeneral(PETSC_COMM_SELF,isz,nidx,PETSC_COPY_VALUES,(is+i));
2815:   }
2816:   PetscBTDestroy(&table);
2817:   PetscFree(nidx);
2818:   return(0);
2819: }

2821: /* -------------------------------------------------------------- */
2822: PetscErrorCode MatPermute_SeqAIJ(Mat A,IS rowp,IS colp,Mat *B)
2823: {
2824:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
2826:   PetscInt       i,nz = 0,m = A->rmap->n,n = A->cmap->n;
2827:   const PetscInt *row,*col;
2828:   PetscInt       *cnew,j,*lens;
2829:   IS             icolp,irowp;
2830:   PetscInt       *cwork = NULL;
2831:   PetscScalar    *vwork = NULL;

2834:   ISInvertPermutation(rowp,PETSC_DECIDE,&irowp);
2835:   ISGetIndices(irowp,&row);
2836:   ISInvertPermutation(colp,PETSC_DECIDE,&icolp);
2837:   ISGetIndices(icolp,&col);

2839:   /* determine lengths of permuted rows */
2840:   PetscMalloc1(m+1,&lens);
2841:   for (i=0; i<m; i++) lens[row[i]] = a->i[i+1] - a->i[i];
2842:   MatCreate(PetscObjectComm((PetscObject)A),B);
2843:   MatSetSizes(*B,m,n,m,n);
2844:   MatSetBlockSizesFromMats(*B,A,A);
2845:   MatSetType(*B,((PetscObject)A)->type_name);
2846:   MatSeqAIJSetPreallocation_SeqAIJ(*B,0,lens);
2847:   PetscFree(lens);

2849:   PetscMalloc1(n,&cnew);
2850:   for (i=0; i<m; i++) {
2851:     MatGetRow_SeqAIJ(A,i,&nz,&cwork,&vwork);
2852:     for (j=0; j<nz; j++) cnew[j] = col[cwork[j]];
2853:     MatSetValues_SeqAIJ(*B,1,&row[i],nz,cnew,vwork,INSERT_VALUES);
2854:     MatRestoreRow_SeqAIJ(A,i,&nz,&cwork,&vwork);
2855:   }
2856:   PetscFree(cnew);

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

2860:   MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);
2861:   MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);
2862:   ISRestoreIndices(irowp,&row);
2863:   ISRestoreIndices(icolp,&col);
2864:   ISDestroy(&irowp);
2865:   ISDestroy(&icolp);
2866:   return(0);
2867: }

2869: PetscErrorCode MatCopy_SeqAIJ(Mat A,Mat B,MatStructure str)
2870: {

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

2879:     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");
2880:     PetscArraycpy(b->a,a->a,a->i[A->rmap->n]);
2881:     PetscObjectStateIncrease((PetscObject)B);
2882:   } else {
2883:     MatCopy_Basic(A,B,str);
2884:   }
2885:   return(0);
2886: }

2888: PetscErrorCode MatSetUp_SeqAIJ(Mat A)
2889: {

2893:    MatSeqAIJSetPreallocation_SeqAIJ(A,PETSC_DEFAULT,0);
2894:   return(0);
2895: }

2897: PetscErrorCode MatSeqAIJGetArray_SeqAIJ(Mat A,PetscScalar *array[])
2898: {
2899:   Mat_SeqAIJ *a = (Mat_SeqAIJ*)A->data;

2902:   *array = a->a;
2903:   return(0);
2904: }

2906: PetscErrorCode MatSeqAIJRestoreArray_SeqAIJ(Mat A,PetscScalar *array[])
2907: {
2909:   return(0);
2910: }

2912: /*
2913:    Computes the number of nonzeros per row needed for preallocation when X and Y
2914:    have different nonzero structure.
2915: */
2916: PetscErrorCode MatAXPYGetPreallocation_SeqX_private(PetscInt m,const PetscInt *xi,const PetscInt *xj,const PetscInt *yi,const PetscInt *yj,PetscInt *nnz)
2917: {
2918:   PetscInt       i,j,k,nzx,nzy;

2921:   /* Set the number of nonzeros in the new matrix */
2922:   for (i=0; i<m; i++) {
2923:     const PetscInt *xjj = xj+xi[i],*yjj = yj+yi[i];
2924:     nzx = xi[i+1] - xi[i];
2925:     nzy = yi[i+1] - yi[i];
2926:     nnz[i] = 0;
2927:     for (j=0,k=0; j<nzx; j++) {                   /* Point in X */
2928:       for (; k<nzy && yjj[k]<xjj[j]; k++) nnz[i]++; /* Catch up to X */
2929:       if (k<nzy && yjj[k]==xjj[j]) k++;             /* Skip duplicate */
2930:       nnz[i]++;
2931:     }
2932:     for (; k<nzy; k++) nnz[i]++;
2933:   }
2934:   return(0);
2935: }

2937: PetscErrorCode MatAXPYGetPreallocation_SeqAIJ(Mat Y,Mat X,PetscInt *nnz)
2938: {
2939:   PetscInt       m = Y->rmap->N;
2940:   Mat_SeqAIJ     *x = (Mat_SeqAIJ*)X->data;
2941:   Mat_SeqAIJ     *y = (Mat_SeqAIJ*)Y->data;

2945:   /* Set the number of nonzeros in the new matrix */
2946:   MatAXPYGetPreallocation_SeqX_private(m,x->i,x->j,y->i,y->j,nnz);
2947:   return(0);
2948: }

2950: PetscErrorCode MatAXPY_SeqAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
2951: {
2953:   Mat_SeqAIJ     *x = (Mat_SeqAIJ*)X->data,*y = (Mat_SeqAIJ*)Y->data;
2954:   PetscBLASInt   one=1,bnz;

2957:   PetscBLASIntCast(x->nz,&bnz);
2958:   if (str == SAME_NONZERO_PATTERN) {
2959:     PetscScalar alpha = a;
2960:     PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
2961:     MatSeqAIJInvalidateDiagonal(Y);
2962:     PetscObjectStateIncrease((PetscObject)Y);
2963:     /* the MatAXPY_Basic* subroutines calls MatAssembly, so the matrix on the GPU
2964:        will be updated */
2965: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
2966:     if (Y->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) {
2967:       Y->valid_GPU_matrix = PETSC_OFFLOAD_CPU;
2968:     }
2969: #endif
2970:   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
2971:     MatAXPY_Basic(Y,a,X,str);
2972:   } else {
2973:     Mat      B;
2974:     PetscInt *nnz;
2975:     PetscMalloc1(Y->rmap->N,&nnz);
2976:     MatCreate(PetscObjectComm((PetscObject)Y),&B);
2977:     PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);
2978:     MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);
2979:     MatSetBlockSizesFromMats(B,Y,Y);
2980:     MatSetType(B,(MatType) ((PetscObject)Y)->type_name);
2981:     MatAXPYGetPreallocation_SeqAIJ(Y,X,nnz);
2982:     MatSeqAIJSetPreallocation(B,0,nnz);
2983:     MatAXPY_BasicWithPreallocation(B,Y,a,X,str);
2984:     MatHeaderReplace(Y,&B);
2985:     PetscFree(nnz);
2986:   }
2987:   return(0);
2988: }

2990: PetscErrorCode  MatConjugate_SeqAIJ(Mat mat)
2991: {
2992: #if defined(PETSC_USE_COMPLEX)
2993:   Mat_SeqAIJ  *aij = (Mat_SeqAIJ*)mat->data;
2994:   PetscInt    i,nz;
2995:   PetscScalar *a;

2998:   nz = aij->nz;
2999:   a  = aij->a;
3000:   for (i=0; i<nz; i++) a[i] = PetscConj(a[i]);
3001: #if defined(PETSC_HAVE_VIENNACL) || defined(PETSC_HAVE_CUDA)
3002:   if (mat->valid_GPU_matrix != PETSC_OFFLOAD_UNALLOCATED) mat->valid_GPU_matrix = PETSC_OFFLOAD_CPU;
3003: #endif
3004: #else
3006: #endif
3007:   return(0);
3008: }

3010: PetscErrorCode MatGetRowMaxAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
3011: {
3012:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
3014:   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
3015:   PetscReal      atmp;
3016:   PetscScalar    *x;
3017:   MatScalar      *aa;

3020:   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3021:   aa = a->a;
3022:   ai = a->i;
3023:   aj = a->j;

3025:   VecSet(v,0.0);
3026:   VecGetArray(v,&x);
3027:   VecGetLocalSize(v,&n);
3028:   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
3029:   for (i=0; i<m; i++) {
3030:     ncols = ai[1] - ai[0]; ai++;
3031:     x[i]  = 0.0;
3032:     for (j=0; j<ncols; j++) {
3033:       atmp = PetscAbsScalar(*aa);
3034:       if (PetscAbsScalar(x[i]) < atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
3035:       aa++; aj++;
3036:     }
3037:   }
3038:   VecRestoreArray(v,&x);
3039:   return(0);
3040: }

3042: PetscErrorCode MatGetRowMax_SeqAIJ(Mat A,Vec v,PetscInt idx[])
3043: {
3044:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
3046:   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
3047:   PetscScalar    *x;
3048:   MatScalar      *aa;

3051:   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3052:   aa = a->a;
3053:   ai = a->i;
3054:   aj = a->j;

3056:   VecSet(v,0.0);
3057:   VecGetArray(v,&x);
3058:   VecGetLocalSize(v,&n);
3059:   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
3060:   for (i=0; i<m; i++) {
3061:     ncols = ai[1] - ai[0]; ai++;
3062:     if (ncols == A->cmap->n) { /* row is dense */
3063:       x[i] = *aa; if (idx) idx[i] = 0;
3064:     } else {  /* row is sparse so already KNOW maximum is 0.0 or higher */
3065:       x[i] = 0.0;
3066:       if (idx) {
3067:         idx[i] = 0; /* in case ncols is zero */
3068:         for (j=0;j<ncols;j++) { /* find first implicit 0.0 in the row */
3069:           if (aj[j] > j) {
3070:             idx[i] = j;
3071:             break;
3072:           }
3073:         }
3074:       }
3075:     }
3076:     for (j=0; j<ncols; j++) {
3077:       if (PetscRealPart(x[i]) < PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
3078:       aa++; aj++;
3079:     }
3080:   }
3081:   VecRestoreArray(v,&x);
3082:   return(0);
3083: }

3085: PetscErrorCode MatGetRowMinAbs_SeqAIJ(Mat A,Vec v,PetscInt idx[])
3086: {
3087:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
3089:   PetscInt       i,j,m = A->rmap->n,*ai,*aj,ncols,n;
3090:   PetscReal      atmp;
3091:   PetscScalar    *x;
3092:   MatScalar      *aa;

3095:   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3096:   aa = a->a;
3097:   ai = a->i;
3098:   aj = a->j;

3100:   VecSet(v,0.0);
3101:   VecGetArray(v,&x);
3102:   VecGetLocalSize(v,&n);
3103:   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);
3104:   for (i=0; i<m; i++) {
3105:     ncols = ai[1] - ai[0]; ai++;
3106:     if (ncols) {
3107:       /* Get first nonzero */
3108:       for (j = 0; j < ncols; j++) {
3109:         atmp = PetscAbsScalar(aa[j]);
3110:         if (atmp > 1.0e-12) {
3111:           x[i] = atmp;
3112:           if (idx) idx[i] = aj[j];
3113:           break;
3114:         }
3115:       }
3116:       if (j == ncols) {x[i] = PetscAbsScalar(*aa); if (idx) idx[i] = *aj;}
3117:     } else {
3118:       x[i] = 0.0; if (idx) idx[i] = 0;
3119:     }
3120:     for (j = 0; j < ncols; j++) {
3121:       atmp = PetscAbsScalar(*aa);
3122:       if (atmp > 1.0e-12 && PetscAbsScalar(x[i]) > atmp) {x[i] = atmp; if (idx) idx[i] = *aj;}
3123:       aa++; aj++;
3124:     }
3125:   }
3126:   VecRestoreArray(v,&x);
3127:   return(0);
3128: }

3130: PetscErrorCode MatGetRowMin_SeqAIJ(Mat A,Vec v,PetscInt idx[])
3131: {
3132:   Mat_SeqAIJ      *a = (Mat_SeqAIJ*)A->data;
3133:   PetscErrorCode  ierr;
3134:   PetscInt        i,j,m = A->rmap->n,ncols,n;
3135:   const PetscInt  *ai,*aj;
3136:   PetscScalar     *x;
3137:   const MatScalar *aa;

3140:   if (A->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3141:   aa = a->a;
3142:   ai = a->i;
3143:   aj = a->j;

3145:   VecSet(v,0.0);
3146:   VecGetArray(v,&x);
3147:   VecGetLocalSize(v,&n);
3148:   if (n != A->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Nonconforming matrix and vector");
3149:   for (i=0; i<m; i++) {
3150:     ncols = ai[1] - ai[0]; ai++;
3151:     if (ncols == A->cmap->n) { /* row is dense */
3152:       x[i] = *aa; if (idx) idx[i] = 0;
3153:     } else {  /* row is sparse so already KNOW minimum is 0.0 or lower */
3154:       x[i] = 0.0;
3155:       if (idx) {   /* find first implicit 0.0 in the row */
3156:         idx[i] = 0; /* in case ncols is zero */
3157:         for (j=0; j<ncols; j++) {
3158:           if (aj[j] > j) {
3159:             idx[i] = j;
3160:             break;
3161:           }
3162:         }
3163:       }
3164:     }
3165:     for (j=0; j<ncols; j++) {
3166:       if (PetscRealPart(x[i]) > PetscRealPart(*aa)) {x[i] = *aa; if (idx) idx[i] = *aj;}
3167:       aa++; aj++;
3168:     }
3169:   }
3170:   VecRestoreArray(v,&x);
3171:   return(0);
3172: }

3174: PetscErrorCode MatInvertBlockDiagonal_SeqAIJ(Mat A,const PetscScalar **values)
3175: {
3176:   Mat_SeqAIJ      *a = (Mat_SeqAIJ*) A->data;
3177:   PetscErrorCode  ierr;
3178:   PetscInt        i,bs = PetscAbs(A->rmap->bs),mbs = A->rmap->n/bs,ipvt[5],bs2 = bs*bs,*v_pivots,ij[7],*IJ,j;
3179:   MatScalar       *diag,work[25],*v_work;
3180:   const PetscReal shift = 0.0;
3181:   PetscBool       allowzeropivot,zeropivotdetected=PETSC_FALSE;

3184:   allowzeropivot = PetscNot(A->erroriffailure);
3185:   if (a->ibdiagvalid) {
3186:     if (values) *values = a->ibdiag;
3187:     return(0);
3188:   }
3189:   MatMarkDiagonal_SeqAIJ(A);
3190:   if (!a->ibdiag) {
3191:     PetscMalloc1(bs2*mbs,&a->ibdiag);
3192:     PetscLogObjectMemory((PetscObject)A,bs2*mbs*sizeof(PetscScalar));
3193:   }
3194:   diag = a->ibdiag;
3195:   if (values) *values = a->ibdiag;
3196:   /* factor and invert each block */
3197:   switch (bs) {
3198:   case 1:
3199:     for (i=0; i<mbs; i++) {
3200:       MatGetValues(A,1,&i,1,&i,diag+i);
3201:       if (PetscAbsScalar(diag[i] + shift) < PETSC_MACHINE_EPSILON) {
3202:         if (allowzeropivot) {
3203:           A->factorerrortype             = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3204:           A->factorerror_zeropivot_value = PetscAbsScalar(diag[i]);
3205:           A->factorerror_zeropivot_row   = i;
3206:           PetscInfo3(A,"Zero pivot, row %D pivot %g tolerance %g\n",i,(double)PetscAbsScalar(diag[i]),(double)PETSC_MACHINE_EPSILON);
3207:         } else SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_MAT_LU_ZRPVT,"Zero pivot, row %D pivot %g tolerance %g",i,(double)PetscAbsScalar(diag[i]),(double)PETSC_MACHINE_EPSILON);
3208:       }
3209:       diag[i] = (PetscScalar)1.0 / (diag[i] + shift);
3210:     }
3211:     break;
3212:   case 2:
3213:     for (i=0; i<mbs; i++) {
3214:       ij[0] = 2*i; ij[1] = 2*i + 1;
3215:       MatGetValues(A,2,ij,2,ij,diag);
3216:       PetscKernel_A_gets_inverse_A_2(diag,shift,allowzeropivot,&zeropivotdetected);
3217:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3218:       PetscKernel_A_gets_transpose_A_2(diag);
3219:       diag += 4;
3220:     }
3221:     break;
3222:   case 3:
3223:     for (i=0; i<mbs; i++) {
3224:       ij[0] = 3*i; ij[1] = 3*i + 1; ij[2] = 3*i + 2;
3225:       MatGetValues(A,3,ij,3,ij,diag);
3226:       PetscKernel_A_gets_inverse_A_3(diag,shift,allowzeropivot,&zeropivotdetected);
3227:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3228:       PetscKernel_A_gets_transpose_A_3(diag);
3229:       diag += 9;
3230:     }
3231:     break;
3232:   case 4:
3233:     for (i=0; i<mbs; i++) {
3234:       ij[0] = 4*i; ij[1] = 4*i + 1; ij[2] = 4*i + 2; ij[3] = 4*i + 3;
3235:       MatGetValues(A,4,ij,4,ij,diag);
3236:       PetscKernel_A_gets_inverse_A_4(diag,shift,allowzeropivot,&zeropivotdetected);
3237:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3238:       PetscKernel_A_gets_transpose_A_4(diag);
3239:       diag += 16;
3240:     }
3241:     break;
3242:   case 5:
3243:     for (i=0; i<mbs; i++) {
3244:       ij[0] = 5*i; ij[1] = 5*i + 1; ij[2] = 5*i + 2; ij[3] = 5*i + 3; ij[4] = 5*i + 4;
3245:       MatGetValues(A,5,ij,5,ij,diag);
3246:       PetscKernel_A_gets_inverse_A_5(diag,ipvt,work,shift,allowzeropivot,&zeropivotdetected);
3247:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3248:       PetscKernel_A_gets_transpose_A_5(diag);
3249:       diag += 25;
3250:     }
3251:     break;
3252:   case 6:
3253:     for (i=0; i<mbs; i++) {
3254:       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;
3255:       MatGetValues(A,6,ij,6,ij,diag);
3256:       PetscKernel_A_gets_inverse_A_6(diag,shift,allowzeropivot,&zeropivotdetected);
3257:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3258:       PetscKernel_A_gets_transpose_A_6(diag);
3259:       diag += 36;
3260:     }
3261:     break;
3262:   case 7:
3263:     for (i=0; i<mbs; i++) {
3264:       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;
3265:       MatGetValues(A,7,ij,7,ij,diag);
3266:       PetscKernel_A_gets_inverse_A_7(diag,shift,allowzeropivot,&zeropivotdetected);
3267:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3268:       PetscKernel_A_gets_transpose_A_7(diag);
3269:       diag += 49;
3270:     }
3271:     break;
3272:   default:
3273:     PetscMalloc3(bs,&v_work,bs,&v_pivots,bs,&IJ);
3274:     for (i=0; i<mbs; i++) {
3275:       for (j=0; j<bs; j++) {
3276:         IJ[j] = bs*i + j;
3277:       }
3278:       MatGetValues(A,bs,IJ,bs,IJ,diag);
3279:       PetscKernel_A_gets_inverse_A(bs,diag,v_pivots,v_work,allowzeropivot,&zeropivotdetected);
3280:       if (zeropivotdetected) A->factorerrortype = MAT_FACTOR_NUMERIC_ZEROPIVOT;
3281:       PetscKernel_A_gets_transpose_A_N(diag,bs);
3282:       diag += bs2;
3283:     }
3284:     PetscFree3(v_work,v_pivots,IJ);
3285:   }
3286:   a->ibdiagvalid = PETSC_TRUE;
3287:   return(0);
3288: }

3290: static PetscErrorCode  MatSetRandom_SeqAIJ(Mat x,PetscRandom rctx)
3291: {
3293:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)x->data;
3294:   PetscScalar    a;
3295:   PetscInt       m,n,i,j,col;

3298:   if (!x->assembled) {
3299:     MatGetSize(x,&m,&n);
3300:     for (i=0; i<m; i++) {
3301:       for (j=0; j<aij->imax[i]; j++) {
3302:         PetscRandomGetValue(rctx,&a);
3303:         col  = (PetscInt)(n*PetscRealPart(a));
3304:         MatSetValues(x,1,&i,1,&col,&a,ADD_VALUES);
3305:       }
3306:     }
3307:   } else {
3308:     for (i=0; i<aij->nz; i++) {PetscRandomGetValue(rctx,aij->a+i);}
3309:   }
3310:   MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);
3311:   MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);
3312:   return(0);
3313: }

3315: /* Like MatSetRandom_SeqAIJ, but do not set values on columns in range of [low, high) */
3316: PetscErrorCode  MatSetRandomSkipColumnRange_SeqAIJ_Private(Mat x,PetscInt low,PetscInt high,PetscRandom rctx)
3317: {
3319:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)x->data;
3320:   PetscScalar    a;
3321:   PetscInt       m,n,i,j,col,nskip;

3324:   nskip = high - low;
3325:   MatGetSize(x,&m,&n);
3326:   n    -= nskip; /* shrink number of columns where nonzeros can be set */
3327:   for (i=0; i<m; i++) {
3328:     for (j=0; j<aij->imax[i]; j++) {
3329:       PetscRandomGetValue(rctx,&a);
3330:       col  = (PetscInt)(n*PetscRealPart(a));
3331:       if (col >= low) col += nskip; /* shift col rightward to skip the hole */
3332:       MatSetValues(x,1,&i,1,&col,&a,ADD_VALUES);
3333:     }
3334:   }
3335:   MatAssemblyBegin(x,MAT_FINAL_ASSEMBLY);
3336:   MatAssemblyEnd(x,MAT_FINAL_ASSEMBLY);
3337:   return(0);
3338: }


3341: /* -------------------------------------------------------------------*/
3342: static struct _MatOps MatOps_Values = { MatSetValues_SeqAIJ,
3343:                                         MatGetRow_SeqAIJ,
3344:                                         MatRestoreRow_SeqAIJ,
3345:                                         MatMult_SeqAIJ,
3346:                                 /*  4*/ MatMultAdd_SeqAIJ,
3347:                                         MatMultTranspose_SeqAIJ,
3348:                                         MatMultTransposeAdd_SeqAIJ,
3349:                                         0,
3350:                                         0,
3351:                                         0,
3352:                                 /* 10*/ 0,
3353:                                         MatLUFactor_SeqAIJ,
3354:                                         0,
3355:                                         MatSOR_SeqAIJ,
3356:                                         MatTranspose_SeqAIJ,
3357:                                 /*1 5*/ MatGetInfo_SeqAIJ,
3358:                                         MatEqual_SeqAIJ,
3359:                                         MatGetDiagonal_SeqAIJ,
3360:                                         MatDiagonalScale_SeqAIJ,
3361:                                         MatNorm_SeqAIJ,
3362:                                 /* 20*/ 0,
3363:                                         MatAssemblyEnd_SeqAIJ,
3364:                                         MatSetOption_SeqAIJ,
3365:                                         MatZeroEntries_SeqAIJ,
3366:                                 /* 24*/ MatZeroRows_SeqAIJ,
3367:                                         0,
3368:                                         0,
3369:                                         0,
3370:                                         0,
3371:                                 /* 29*/ MatSetUp_SeqAIJ,
3372:                                         0,
3373:                                         0,
3374:                                         0,
3375:                                         0,
3376:                                 /* 34*/ MatDuplicate_SeqAIJ,
3377:                                         0,
3378:                                         0,
3379:                                         MatILUFactor_SeqAIJ,
3380:                                         0,
3381:                                 /* 39*/ MatAXPY_SeqAIJ,
3382:                                         MatCreateSubMatrices_SeqAIJ,
3383:                                         MatIncreaseOverlap_SeqAIJ,
3384:                                         MatGetValues_SeqAIJ,
3385:                                         MatCopy_SeqAIJ,
3386:                                 /* 44*/ MatGetRowMax_SeqAIJ,
3387:                                         MatScale_SeqAIJ,
3388:                                         MatShift_SeqAIJ,
3389:                                         MatDiagonalSet_SeqAIJ,
3390:                                         MatZeroRowsColumns_SeqAIJ,
3391:                                 /* 49*/ MatSetRandom_SeqAIJ,
3392:                                         MatGetRowIJ_SeqAIJ,
3393:                                         MatRestoreRowIJ_SeqAIJ,
3394:                                         MatGetColumnIJ_SeqAIJ,
3395:                                         MatRestoreColumnIJ_SeqAIJ,
3396:                                 /* 54*/ MatFDColoringCreate_SeqXAIJ,
3397:                                         0,
3398:                                         0,
3399:                                         MatPermute_SeqAIJ,
3400:                                         0,
3401:                                 /* 59*/ 0,
3402:                                         MatDestroy_SeqAIJ,
3403:                                         MatView_SeqAIJ,
3404:                                         0,
3405:                                         MatMatMatMult_SeqAIJ_SeqAIJ_SeqAIJ,
3406:                                 /* 64*/ MatMatMatMultSymbolic_SeqAIJ_SeqAIJ_SeqAIJ,
3407:                                         MatMatMatMultNumeric_SeqAIJ_SeqAIJ_SeqAIJ,
3408:                                         0,
3409:                                         0,
3410:                                         0,
3411:                                 /* 69*/ MatGetRowMaxAbs_SeqAIJ,
3412:                                         MatGetRowMinAbs_SeqAIJ,
3413:                                         0,
3414:                                         0,
3415:                                         0,
3416:                                 /* 74*/ 0,
3417:                                         MatFDColoringApply_AIJ,
3418:                                         0,
3419:                                         0,
3420:                                         0,
3421:                                 /* 79*/ MatFindZeroDiagonals_SeqAIJ,
3422:                                         0,
3423:                                         0,
3424:                                         0,
3425:                                         MatLoad_SeqAIJ,
3426:                                 /* 84*/ MatIsSymmetric_SeqAIJ,
3427:                                         MatIsHermitian_SeqAIJ,
3428:                                         0,
3429:                                         0,
3430:                                         0,
3431:                                 /* 89*/ MatMatMult_SeqAIJ_SeqAIJ,
3432:                                         MatMatMultSymbolic_SeqAIJ_SeqAIJ,
3433:                                         MatMatMultNumeric_SeqAIJ_SeqAIJ,
3434:                                         MatPtAP_SeqAIJ_SeqAIJ,
3435:                                         MatPtAPSymbolic_SeqAIJ_SeqAIJ_SparseAxpy,
3436:                                 /* 94*/ MatPtAPNumeric_SeqAIJ_SeqAIJ_SparseAxpy,
3437:                                         MatMatTransposeMult_SeqAIJ_SeqAIJ,
3438:                                         MatMatTransposeMultSymbolic_SeqAIJ_SeqAIJ,
3439:                                         MatMatTransposeMultNumeric_SeqAIJ_SeqAIJ,
3440:                                         0,
3441:                                 /* 99*/ 0,
3442:                                         0,
3443:                                         0,
3444:                                         MatConjugate_SeqAIJ,
3445:                                         0,
3446:                                 /*104*/ MatSetValuesRow_SeqAIJ,
3447:                                         MatRealPart_SeqAIJ,
3448:                                         MatImaginaryPart_SeqAIJ,
3449:                                         0,
3450:                                         0,
3451:                                 /*109*/ MatMatSolve_SeqAIJ,
3452:                                         0,
3453:                                         MatGetRowMin_SeqAIJ,
3454:                                         0,
3455:                                         MatMissingDiagonal_SeqAIJ,
3456:                                 /*114*/ 0,
3457:                                         0,
3458:                                         0,
3459:                                         0,
3460:                                         0,
3461:                                 /*119*/ 0,
3462:                                         0,
3463:                                         0,
3464:                                         0,
3465:                                         MatGetMultiProcBlock_SeqAIJ,
3466:                                 /*124*/ MatFindNonzeroRows_SeqAIJ,
3467:                                         MatGetColumnNorms_SeqAIJ,
3468:                                         MatInvertBlockDiagonal_SeqAIJ,
3469:                                         MatInvertVariableBlockDiagonal_SeqAIJ,
3470:                                         0,
3471:                                 /*129*/ 0,
3472:                                         MatTransposeMatMult_SeqAIJ_SeqAIJ,
3473:                                         MatTransposeMatMultSymbolic_SeqAIJ_SeqAIJ,
3474:                                         MatTransposeMatMultNumeric_SeqAIJ_SeqAIJ,
3475:                                         MatTransposeColoringCreate_SeqAIJ,
3476:                                 /*134*/ MatTransColoringApplySpToDen_SeqAIJ,
3477:                                         MatTransColoringApplyDenToSp_SeqAIJ,
3478:                                         MatRARt_SeqAIJ_SeqAIJ,
3479:                                         MatRARtSymbolic_SeqAIJ_SeqAIJ,
3480:                                         MatRARtNumeric_SeqAIJ_SeqAIJ,
3481:                                  /*139*/0,
3482:                                         0,
3483:                                         0,
3484:                                         MatFDColoringSetUp_SeqXAIJ,
3485:                                         MatFindOffBlockDiagonalEntries_SeqAIJ,
3486:                                  /*144*/MatCreateMPIMatConcatenateSeqMat_SeqAIJ,
3487:                                         MatDestroySubMatrices_SeqAIJ
3488: };

3490: PetscErrorCode  MatSeqAIJSetColumnIndices_SeqAIJ(Mat mat,PetscInt *indices)
3491: {
3492:   Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data;
3493:   PetscInt   i,nz,n;

3496:   nz = aij->maxnz;
3497:   n  = mat->rmap->n;
3498:   for (i=0; i<nz; i++) {
3499:     aij->j[i] = indices[i];
3500:   }
3501:   aij->nz = nz;
3502:   for (i=0; i<n; i++) {
3503:     aij->ilen[i] = aij->imax[i];
3504:   }
3505:   return(0);
3506: }

3508: /*
3509:  * When a sparse matrix has many zero columns, we should compact them out to save the space
3510:  * This happens in MatPtAPSymbolic_MPIAIJ_MPIAIJ_scalable()
3511:  * */
3512: PetscErrorCode  MatSeqAIJCompactOutExtraColumns_SeqAIJ(Mat mat, ISLocalToGlobalMapping *mapping)
3513: {
3514:   Mat_SeqAIJ *aij = (Mat_SeqAIJ*)mat->data;
3515:   PetscTable         gid1_lid1;
3516:   PetscTablePosition tpos;
3517:   PetscInt           gid,lid,i,j,ncols,ec;
3518:   PetscInt           *garray;
3519:   PetscErrorCode  ierr;

3524:   /* use a table */
3525:   PetscTableCreate(mat->rmap->n,mat->cmap->N+1,&gid1_lid1);
3526:   ec = 0;
3527:   for (i=0; i<mat->rmap->n; i++) {
3528:     ncols = aij->i[i+1] - aij->i[i];
3529:     for (j=0; j<ncols; j++) {
3530:       PetscInt data,gid1 = aij->j[aij->i[i] + j] + 1;
3531:       PetscTableFind(gid1_lid1,gid1,&data);
3532:       if (!data) {
3533:         /* one based table */
3534:         PetscTableAdd(gid1_lid1,gid1,++ec,INSERT_VALUES);
3535:       }
3536:     }
3537:   }
3538:   /* form array of columns we need */
3539:   PetscMalloc1(ec+1,&garray);
3540:   PetscTableGetHeadPosition(gid1_lid1,&tpos);
3541:   while (tpos) {
3542:     PetscTableGetNext(gid1_lid1,&tpos,&gid,&lid);
3543:     gid--;
3544:     lid--;
3545:     garray[lid] = gid;
3546:   }
3547:   PetscSortInt(ec,garray); /* sort, and rebuild */
3548:   PetscTableRemoveAll(gid1_lid1);
3549:   for (i=0; i<ec; i++) {
3550:     PetscTableAdd(gid1_lid1,garray[i]+1,i+1,INSERT_VALUES);
3551:   }
3552:   /* compact out the extra columns in B */
3553:   for (i=0; i<mat->rmap->n; i++) {
3554:         ncols = aij->i[i+1] - aij->i[i];
3555:     for (j=0; j<ncols; j++) {
3556:       PetscInt gid1 = aij->j[aij->i[i] + j] + 1;
3557:       PetscTableFind(gid1_lid1,gid1,&lid);
3558:       lid--;
3559:       aij->j[aij->i[i] + j] = lid;
3560:     }
3561:   }
3562:   PetscLayoutDestroy(&mat->cmap);
3563:   PetscLayoutCreateFromSizes(PetscObjectComm((PetscObject)mat),ec,ec,1,&mat->cmap);
3564:   PetscTableDestroy(&gid1_lid1);
3565:   ISLocalToGlobalMappingCreate(PETSC_COMM_SELF,mat->cmap->bs,mat->cmap->n,garray,PETSC_OWN_POINTER,mapping);
3566:   ISLocalToGlobalMappingSetType(*mapping,ISLOCALTOGLOBALMAPPINGHASH);
3567:   return(0);
3568: }

3570: /*@
3571:     MatSeqAIJSetColumnIndices - Set the column indices for all the rows
3572:        in the matrix.

3574:   Input Parameters:
3575: +  mat - the SeqAIJ matrix
3576: -  indices - the column indices

3578:   Level: advanced

3580:   Notes:
3581:     This can be called if you have precomputed the nonzero structure of the
3582:   matrix and want to provide it to the matrix object to improve the performance
3583:   of the MatSetValues() operation.

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

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

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

3592: @*/
3593: PetscErrorCode  MatSeqAIJSetColumnIndices(Mat mat,PetscInt *indices)
3594: {

3600:   PetscUseMethod(mat,"MatSeqAIJSetColumnIndices_C",(Mat,PetscInt*),(mat,indices));
3601:   return(0);
3602: }

3604: /* ----------------------------------------------------------------------------------------*/

3606: PetscErrorCode  MatStoreValues_SeqAIJ(Mat mat)
3607: {
3608:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)mat->data;
3610:   size_t         nz = aij->i[mat->rmap->n];

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

3615:   /* allocate space for values if not already there */
3616:   if (!aij->saved_values) {
3617:     PetscMalloc1(nz+1,&aij->saved_values);
3618:     PetscLogObjectMemory((PetscObject)mat,(nz+1)*sizeof(PetscScalar));
3619:   }

3621:   /* copy values over */
3622:   PetscArraycpy(aij->saved_values,aij->a,nz);
3623:   return(0);
3624: }

3626: /*@
3627:     MatStoreValues - Stashes a copy of the matrix values; this allows, for
3628:        example, reuse of the linear part of a Jacobian, while recomputing the
3629:        nonlinear portion.

3631:    Collect on Mat

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

3636:   Level: advanced

3638:   Common Usage, with SNESSolve():
3639: $    Create Jacobian matrix
3640: $    Set linear terms into matrix
3641: $    Apply boundary conditions to matrix, at this time matrix must have
3642: $      final nonzero structure (i.e. setting the nonlinear terms and applying
3643: $      boundary conditions again will not change the nonzero structure
3644: $    MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3645: $    MatStoreValues(mat);
3646: $    Call SNESSetJacobian() with matrix
3647: $    In your Jacobian routine
3648: $      MatRetrieveValues(mat);
3649: $      Set nonlinear terms in matrix

3651:   Common Usage without SNESSolve(), i.e. when you handle nonlinear solve yourself:
3652: $    // build linear portion of Jacobian
3653: $    MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);
3654: $    MatStoreValues(mat);
3655: $    loop over nonlinear iterations
3656: $       MatRetrieveValues(mat);
3657: $       // call MatSetValues(mat,...) to set nonliner portion of Jacobian
3658: $       // call MatAssemblyBegin/End() on matrix
3659: $       Solve linear system with Jacobian
3660: $    endloop

3662:   Notes:
3663:     Matrix must already be assemblied before calling this routine
3664:     Must set the matrix option MatSetOption(mat,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE); before
3665:     calling this routine.

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

3670: .seealso: MatRetrieveValues()

3672: @*/
3673: PetscErrorCode  MatStoreValues(Mat mat)
3674: {

3679:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3680:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3681:   PetscUseMethod(mat,"MatStoreValues_C",(Mat),(mat));
3682:   return(0);
3683: }

3685: PetscErrorCode  MatRetrieveValues_SeqAIJ(Mat mat)
3686: {
3687:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)mat->data;
3689:   PetscInt       nz = aij->i[mat->rmap->n];

3692:   if (!aij->nonew) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
3693:   if (!aij->saved_values) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");
3694:   /* copy values over */
3695:   PetscArraycpy(aij->a,aij->saved_values,nz);
3696:   return(0);
3697: }

3699: /*@
3700:     MatRetrieveValues - Retrieves the copy of the matrix values; this allows, for
3701:        example, reuse of the linear part of a Jacobian, while recomputing the
3702:        nonlinear portion.

3704:    Collect on Mat

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

3709:   Level: advanced

3711: .seealso: MatStoreValues()

3713: @*/
3714: PetscErrorCode  MatRetrieveValues(Mat mat)
3715: {

3720:   if (!mat->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for unassembled matrix");
3721:   if (mat->factortype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Not for factored matrix");
3722:   PetscUseMethod(mat,"MatRetrieveValues_C",(Mat),(mat));
3723:   return(0);
3724: }


3727: /* --------------------------------------------------------------------------------*/
3728: /*@C
3729:    MatCreateSeqAIJ - Creates a sparse matrix in AIJ (compressed row) format
3730:    (the default parallel PETSc format).  For good matrix assembly performance
3731:    the user should preallocate the matrix storage by setting the parameter nz
3732:    (or the array nnz).  By setting these parameters accurately, performance
3733:    during matrix assembly can be increased by more than a factor of 50.

3735:    Collective

3737:    Input Parameters:
3738: +  comm - MPI communicator, set to PETSC_COMM_SELF
3739: .  m - number of rows
3740: .  n - number of columns
3741: .  nz - number of nonzeros per row (same for all rows)
3742: -  nnz - array containing the number of nonzeros in the various rows
3743:          (possibly different for each row) or NULL

3745:    Output Parameter:
3746: .  A - the matrix

3748:    It is recommended that one use the MatCreate(), MatSetType() and/or MatSetFromOptions(),
3749:    MatXXXXSetPreallocation() paradigm instead of this routine directly.
3750:    [MatXXXXSetPreallocation() is, for example, MatSeqAIJSetPreallocation]

3752:    Notes:
3753:    If nnz is given then nz is ignored

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

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

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

3770:    Options Database Keys:
3771: +  -mat_no_inode  - Do not use inodes
3772: -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)

3774:    Level: intermediate

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

3778: @*/
3779: PetscErrorCode  MatCreateSeqAIJ(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
3780: {

3784:   MatCreate(comm,A);
3785:   MatSetSizes(*A,m,n,m,n);
3786:   MatSetType(*A,MATSEQAIJ);
3787:   MatSeqAIJSetPreallocation_SeqAIJ(*A,nz,nnz);
3788:   return(0);
3789: }

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

3797:    Collective

3799:    Input Parameters:
3800: +  B - The matrix
3801: .  nz - number of nonzeros per row (same for all rows)
3802: -  nnz - array containing the number of nonzeros in the various rows
3803:          (possibly different for each row) or NULL

3805:    Notes:
3806:      If nnz is given then nz is ignored

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

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

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

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

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

3831:    Options Database Keys:
3832: +  -mat_no_inode  - Do not use inodes
3833: -  -mat_inode_limit <limit> - Sets inode limit (max limit=5)

3835:    Level: intermediate

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

3839: @*/
3840: PetscErrorCode  MatSeqAIJSetPreallocation(Mat B,PetscInt nz,const PetscInt nnz[])
3841: {

3847:   PetscTryMethod(B,"MatSeqAIJSetPreallocation_C",(Mat,PetscInt,const PetscInt[]),(B,nz,nnz));
3848:   return(0);
3849: }

3851: PetscErrorCode  MatSeqAIJSetPreallocation_SeqAIJ(Mat B,PetscInt nz,const PetscInt *nnz)
3852: {
3853:   Mat_SeqAIJ     *b;
3854:   PetscBool      skipallocation = PETSC_FALSE,realalloc = PETSC_FALSE;
3856:   PetscInt       i;

3859:   if (nz >= 0 || nnz) realalloc = PETSC_TRUE;
3860:   if (nz == MAT_SKIP_ALLOCATION) {
3861:     skipallocation = PETSC_TRUE;
3862:     nz             = 0;
3863:   }
3864:   PetscLayoutSetUp(B->rmap);
3865:   PetscLayoutSetUp(B->cmap);

3867:   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
3868:   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %D",nz);
3869: #if defined(PETSC_USE_DEBUG)
3870:   if (nnz) {
3871:     for (i=0; i<B->rmap->n; i++) {
3872:       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]);
3873:       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);
3874:     }
3875:   }
3876: #endif

3878:   B->preallocated = PETSC_TRUE;

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

3882:   if (!skipallocation) {
3883:     if (!b->imax) {
3884:       PetscMalloc1(B->rmap->n,&b->imax);
3885:       PetscLogObjectMemory((PetscObject)B,B->rmap->n*sizeof(PetscInt));
3886:     }
3887:     if (!b->ilen) {
3888:       /* b->ilen will count nonzeros in each row so far. */
3889:       PetscCalloc1(B->rmap->n,&b->ilen);
3890:       PetscLogObjectMemory((PetscObject)B,B->rmap->n*sizeof(PetscInt));
3891:     } else {
3892:       PetscMemzero(b->ilen,B->rmap->n*sizeof(PetscInt));
3893:     }
3894:     if (!b->ipre) {
3895:       PetscMalloc1(B->rmap->n,&b->ipre);
3896:       PetscLogObjectMemory((PetscObject)B,B->rmap->n*sizeof(PetscInt));
3897:     }
3898:     if (!nnz) {
3899:       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 10;
3900:       else if (nz < 0) nz = 1;
3901:       nz = PetscMin(nz,B->cmap->n);
3902:       for (i=0; i<B->rmap->n; i++) b->imax[i] = nz;
3903:       nz = nz*B->rmap->n;
3904:     } else {
3905:       PetscInt64 nz64 = 0;
3906:       for (i=0; i<B->rmap->n; i++) {b->imax[i] = nnz[i]; nz64 += nnz[i];}
3907:       PetscIntCast(nz64,&nz);
3908:     }

3910:     /* allocate the matrix space */
3911:     /* FIXME: should B's old memory be unlogged? */
3912:     MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);
3913:     if (B->structure_only) {
3914:       PetscMalloc1(nz,&b->j);
3915:       PetscMalloc1(B->rmap->n+1,&b->i);
3916:       PetscLogObjectMemory((PetscObject)B,(B->rmap->n+1)*sizeof(PetscInt)+nz*sizeof(PetscInt));
3917:     } else {
3918:       PetscMalloc3(nz,&b->a,nz,&b->j,B->rmap->n+1,&b->i);
3919:       PetscLogObjectMemory((PetscObject)B,(B->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));
3920:     }
3921:     b->i[0] = 0;
3922:     for (i=1; i<B->rmap->n+1; i++) {
3923:       b->i[i] = b->i[i-1] + b->imax[i-1];
3924:     }
3925:     if (B->structure_only) {
3926:       b->singlemalloc = PETSC_FALSE;
3927:       b->free_a       = PETSC_FALSE;
3928:     } else {
3929:       b->singlemalloc = PETSC_TRUE;
3930:       b->free_a       = PETSC_TRUE;
3931:     }
3932:     b->free_ij      = PETSC_TRUE;
3933:   } else {
3934:     b->free_a  = PETSC_FALSE;
3935:     b->free_ij = PETSC_FALSE;
3936:   }

3938:   if (b->ipre && nnz != b->ipre  && b->imax) {
3939:     /* reserve user-requested sparsity */
3940:     PetscArraycpy(b->ipre,b->imax,B->rmap->n);
3941:   }


3944:   b->nz               = 0;
3945:   b->maxnz            = nz;
3946:   B->info.nz_unneeded = (double)b->maxnz;
3947:   if (realalloc) {
3948:     MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);
3949:   }
3950:   B->was_assembled = PETSC_FALSE;
3951:   B->assembled     = PETSC_FALSE;
3952:   return(0);
3953: }


3956: PetscErrorCode MatResetPreallocation_SeqAIJ(Mat A)
3957: {
3958:   Mat_SeqAIJ     *a;
3959:   PetscInt       i;


3965:   /* Check local size. If zero, then return */
3966:   if (!A->rmap->n) return(0);

3968:   a = (Mat_SeqAIJ*)A->data;
3969:   /* if no saved info, we error out */
3970:   if (!a->ipre) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"No saved preallocation info \n");

3972:   if (!a->i || !a->j || !a->a || !a->imax || !a->ilen) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NULL,"Memory info is incomplete, and can not reset preallocation \n");

3974:   PetscArraycpy(a->imax,a->ipre,A->rmap->n);
3975:   PetscArrayzero(a->ilen,A->rmap->n);
3976:   a->i[0] = 0;
3977:   for (i=1; i<A->rmap->n+1; i++) {
3978:     a->i[i] = a->i[i-1] + a->imax[i-1];
3979:   }
3980:   A->preallocated     = PETSC_TRUE;
3981:   a->nz               = 0;
3982:   a->maxnz            = a->i[A->rmap->n];
3983:   A->info.nz_unneeded = (double)a->maxnz;
3984:   A->was_assembled    = PETSC_FALSE;
3985:   A->assembled        = PETSC_FALSE;
3986:   return(0);
3987: }

3989: /*@
3990:    MatSeqAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in AIJ format.

3992:    Input Parameters:
3993: +  B - the matrix
3994: .  i - the indices into j for the start of each row (starts with zero)
3995: .  j - the column indices for each row (starts with zero) these must be sorted for each row
3996: -  v - optional values in the matrix

3998:    Level: developer

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

4002: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatSeqAIJSetPreallocation(), MatCreateSeqAIJ(), MATSEQAIJ
4003: @*/
4004: PetscErrorCode MatSeqAIJSetPreallocationCSR(Mat B,const PetscInt i[],const PetscInt j[],const PetscScalar v[])
4005: {

4011:   PetscTryMethod(B,"MatSeqAIJSetPreallocationCSR_C",(Mat,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,i,j,v));
4012:   return(0);
4013: }

4015: PetscErrorCode  MatSeqAIJSetPreallocationCSR_SeqAIJ(Mat B,const PetscInt Ii[],const PetscInt J[],const PetscScalar v[])
4016: {
4017:   PetscInt       i;
4018:   PetscInt       m,n;
4019:   PetscInt       nz;
4020:   PetscInt       *nnz, nz_max = 0;

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

4026:   PetscLayoutSetUp(B->rmap);
4027:   PetscLayoutSetUp(B->cmap);

4029:   MatGetSize(B, &m, &n);
4030:   PetscMalloc1(m+1, &nnz);
4031:   for (i = 0; i < m; i++) {
4032:     nz     = Ii[i+1]- Ii[i];
4033:     nz_max = PetscMax(nz_max, nz);
4034:     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE, "Local row %D has a negative number of columns %D", i, nnz);
4035:     nnz[i] = nz;
4036:   }
4037:   MatSeqAIJSetPreallocation(B, 0, nnz);
4038:   PetscFree(nnz);

4040:   for (i = 0; i < m; i++) {
4041:     MatSetValues_SeqAIJ(B, 1, &i, Ii[i+1] - Ii[i], J+Ii[i], v ? v + Ii[i] : NULL, INSERT_VALUES);
4042:   }

4044:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
4045:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);

4047:   MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
4048:   return(0);
4049: }

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

4054: /*
4055:     Computes (B'*A')' since computing B*A directly is untenable

4057:                n                       p                          p
4058:         (              )       (              )         (                  )
4059:       m (      A       )  *  n (       B      )   =   m (         C        )
4060:         (              )       (              )         (                  )

4062: */
4063: PetscErrorCode MatMatMultNumeric_SeqDense_SeqAIJ(Mat A,Mat B,Mat C)
4064: {
4065:   PetscErrorCode    ierr;
4066:   Mat_SeqDense      *sub_a = (Mat_SeqDense*)A->data;
4067:   Mat_SeqAIJ        *sub_b = (Mat_SeqAIJ*)B->data;
4068:   Mat_SeqDense      *sub_c = (Mat_SeqDense*)C->data;
4069:   PetscInt          i,n,m,q,p;
4070:   const PetscInt    *ii,*idx;
4071:   const PetscScalar *b,*a,*a_q;
4072:   PetscScalar       *c,*c_q;

4075:   m    = A->rmap->n;
4076:   n    = A->cmap->n;
4077:   p    = B->cmap->n;
4078:   a    = sub_a->v;
4079:   b    = sub_b->a;
4080:   c    = sub_c->v;
4081:   PetscArrayzero(c,m*p);

4083:   ii  = sub_b->i;
4084:   idx = sub_b->j;
4085:   for (i=0; i<n; i++) {
4086:     q = ii[i+1] - ii[i];
4087:     while (q-->0) {
4088:       c_q = c + m*(*idx);
4089:       a_q = a + m*i;
4090:       PetscKernelAXPY(c_q,*b,a_q,m);
4091:       idx++;
4092:       b++;
4093:     }
4094:   }
4095:   return(0);
4096: }

4098: PetscErrorCode MatMatMultSymbolic_SeqDense_SeqAIJ(Mat A,Mat B,PetscReal fill,Mat *C)
4099: {
4101:   PetscInt       m=A->rmap->n,n=B->cmap->n;
4102:   Mat            Cmat;

4105:   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);
4106:   MatCreate(PetscObjectComm((PetscObject)A),&Cmat);
4107:   MatSetSizes(Cmat,m,n,m,n);
4108:   MatSetBlockSizesFromMats(Cmat,A,B);
4109:   MatSetType(Cmat,MATSEQDENSE);
4110:   MatSeqDenseSetPreallocation(Cmat,NULL);

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

4114:   *C = Cmat;
4115:   return(0);
4116: }

4118: /* ----------------------------------------------------------------*/
4119: PETSC_INTERN PetscErrorCode MatMatMult_SeqDense_SeqAIJ(Mat A,Mat B,MatReuse scall,PetscReal fill,Mat *C)
4120: {

4124:   if (scall == MAT_INITIAL_MATRIX) {
4125:     PetscLogEventBegin(MAT_MatMultSymbolic,A,B,0,0);
4126:     MatMatMultSymbolic_SeqDense_SeqAIJ(A,B,fill,C);
4127:     PetscLogEventEnd(MAT_MatMultSymbolic,A,B,0,0);
4128:   }
4129:   PetscLogEventBegin(MAT_MatMultNumeric,A,B,0,0);
4130:   MatMatMultNumeric_SeqDense_SeqAIJ(A,B,*C);
4131:   PetscLogEventEnd(MAT_MatMultNumeric,A,B,0,0);
4132:   return(0);
4133: }


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

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

4143:    Level: beginner

4145:    Notes:
4146:     MatSetValues() may be called for this matrix type with a NULL argument for the numerical values,
4147:     in this case the values associated with the rows and columns one passes in are set to zero
4148:     in the matrix

4150:     MatSetOptions(,MAT_STRUCTURE_ONLY,PETSC_TRUE) may be called for this matrix type. In this no
4151:     space is allocated for the nonzero entries and any entries passed with MatSetValues() are ignored

4153:   Developer Notes:
4154:     It would be nice if all matrix formats supported passing NULL in for the numerical values

4156: .seealso: MatCreateSeqAIJ(), MatSetFromOptions(), MatSetType(), MatCreate(), MatType
4157: M*/

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

4162:    This matrix type is identical to MATSEQAIJ when constructed with a single process communicator,
4163:    and MATMPIAIJ otherwise.  As a result, for single process communicators,
4164:   MatSeqAIJSetPreallocation is supported, and similarly MatMPIAIJSetPreallocation() is supported
4165:   for communicators controlling multiple processes.  It is recommended that you call both of
4166:   the above preallocation routines for simplicity.

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

4171:   Developer Notes:
4172:     Subclasses include MATAIJCUSPARSE, MATAIJPERM, MATAIJSELL, MATAIJMKL, MATAIJCRL, and also automatically switches over to use inodes when
4173:    enough exist.

4175:   Level: beginner

4177: .seealso: MatCreateAIJ(), MatCreateSeqAIJ(), MATSEQAIJ,MATMPIAIJ
4178: M*/

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

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

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

4192:   Level: beginner

4194: .seealso: MatCreateMPIAIJCRL,MATSEQAIJCRL,MATMPIAIJCRL, MATSEQAIJCRL, MATMPIAIJCRL
4195: M*/

4197: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCRL(Mat,MatType,MatReuse,Mat*);
4198: #if defined(PETSC_HAVE_ELEMENTAL)
4199: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_Elemental(Mat,MatType,MatReuse,Mat*);
4200: #endif
4201: #if defined(PETSC_HAVE_HYPRE)
4202: PETSC_INTERN PetscErrorCode MatConvert_AIJ_HYPRE(Mat A,MatType,MatReuse,Mat*);
4203: PETSC_INTERN PetscErrorCode MatMatMatMult_Transpose_AIJ_AIJ(Mat,Mat,Mat,MatReuse,PetscReal,Mat*);
4204: #endif
4205: PETSC_INTERN PetscErrorCode MatConvert_SeqAIJ_SeqDense(Mat,MatType,MatReuse,Mat*);

4207: PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_SeqSELL(Mat,MatType,MatReuse,Mat*);
4208: PETSC_INTERN PetscErrorCode MatConvert_XAIJ_IS(Mat,MatType,MatReuse,Mat*);
4209: PETSC_INTERN PetscErrorCode MatPtAP_IS_XAIJ(Mat,Mat,MatReuse,PetscReal,Mat*);

4211: /*@C
4212:    MatSeqAIJGetArray - gives access to the array where the data for a MATSEQAIJ matrix is stored

4214:    Not Collective

4216:    Input Parameter:
4217: .  mat - a MATSEQAIJ matrix

4219:    Output Parameter:
4220: .   array - pointer to the data

4222:    Level: intermediate

4224: .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90()
4225: @*/
4226: PetscErrorCode  MatSeqAIJGetArray(Mat A,PetscScalar **array)
4227: {

4231:   PetscUseMethod(A,"MatSeqAIJGetArray_C",(Mat,PetscScalar**),(A,array));
4232:   return(0);
4233: }

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

4238:    Not Collective

4240:    Input Parameter:
4241: .  mat - a MATSEQAIJ matrix

4243:    Output Parameter:
4244: .   nz - the maximum number of nonzeros in any row

4246:    Level: intermediate

4248: .seealso: MatSeqAIJRestoreArray(), MatSeqAIJGetArrayF90()
4249: @*/
4250: PetscErrorCode  MatSeqAIJGetMaxRowNonzeros(Mat A,PetscInt *nz)
4251: {
4252:   Mat_SeqAIJ     *aij = (Mat_SeqAIJ*)A->data;

4255:   *nz = aij->rmax;
4256:   return(0);
4257: }

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

4262:    Not Collective

4264:    Input Parameters:
4265: +  mat - a MATSEQAIJ matrix
4266: -  array - pointer to the data

4268:    Level: intermediate

4270: .seealso: MatSeqAIJGetArray(), MatSeqAIJRestoreArrayF90()
4271: @*/
4272: PetscErrorCode  MatSeqAIJRestoreArray(Mat A,PetscScalar **array)
4273: {

4277:   PetscUseMethod(A,"MatSeqAIJRestoreArray_C",(Mat,PetscScalar**),(A,array));
4278:   return(0);
4279: }

4281: #if defined(PETSC_HAVE_CUDA)
4282: PETSC_EXTERN PetscErrorCode MatConvert_SeqAIJ_SeqAIJCUSPARSE(Mat);
4283: #endif

4285: PETSC_EXTERN PetscErrorCode MatCreate_SeqAIJ(Mat B)
4286: {
4287:   Mat_SeqAIJ     *b;
4289:   PetscMPIInt    size;

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

4295:   PetscNewLog(B,&b);

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

4299:   PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));
4300:   if (B->sortedfull) B->ops->setvalues = MatSetValues_SeqAIJ_SortedFull;

4302:   b->row                = 0;
4303:   b->col                = 0;
4304:   b->icol               = 0;
4305:   b->reallocs           = 0;
4306:   b->ignorezeroentries  = PETSC_FALSE;
4307:   b->roworiented        = PETSC_TRUE;
4308:   b->nonew              = 0;
4309:   b->diag               = 0;
4310:   b->solve_work         = 0;
4311:   B->spptr              = 0;
4312:   b->saved_values       = 0;
4313:   b->idiag              = 0;
4314:   b->mdiag              = 0;
4315:   b->ssor_work          = 0;
4316:   b->omega              = 1.0;
4317:   b->fshift             = 0.0;
4318:   b->idiagvalid         = PETSC_FALSE;
4319:   b->ibdiagvalid        = PETSC_FALSE;
4320:   b->keepnonzeropattern = PETSC_FALSE;

4322:   PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);
4323:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJGetArray_C",MatSeqAIJGetArray_SeqAIJ);
4324:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJRestoreArray_C",MatSeqAIJRestoreArray_SeqAIJ);

4326: #if defined(PETSC_HAVE_MATLAB_ENGINE)
4327:   PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEnginePut_C",MatlabEnginePut_SeqAIJ);
4328:   PetscObjectComposeFunction((PetscObject)B,"PetscMatlabEngineGet_C",MatlabEngineGet_SeqAIJ);
4329: #endif

4331:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetColumnIndices_C",MatSeqAIJSetColumnIndices_SeqAIJ);
4332:   PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_SeqAIJ);
4333:   PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_SeqAIJ);
4334:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqsbaij_C",MatConvert_SeqAIJ_SeqSBAIJ);
4335:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqbaij_C",MatConvert_SeqAIJ_SeqBAIJ);
4336:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijperm_C",MatConvert_SeqAIJ_SeqAIJPERM);
4337:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijsell_C",MatConvert_SeqAIJ_SeqAIJSELL);
4338: #if defined(PETSC_HAVE_MKL_SPARSE)
4339:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijmkl_C",MatConvert_SeqAIJ_SeqAIJMKL);
4340: #endif
4341: #if defined(PETSC_HAVE_CUDA)
4342:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijcusparse_C",MatConvert_SeqAIJ_SeqAIJCUSPARSE);
4343: #endif
4344:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqaijcrl_C",MatConvert_SeqAIJ_SeqAIJCRL);
4345: #if defined(PETSC_HAVE_ELEMENTAL)
4346:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_elemental_C",MatConvert_SeqAIJ_Elemental);
4347: #endif
4348: #if defined(PETSC_HAVE_HYPRE)
4349:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_hypre_C",MatConvert_AIJ_HYPRE);
4350:   PetscObjectComposeFunction((PetscObject)B,"MatMatMatMult_transpose_seqaij_seqaij_C",MatMatMatMult_Transpose_AIJ_AIJ);
4351: #endif
4352:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqdense_C",MatConvert_SeqAIJ_SeqDense);
4353:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_seqsell_C",MatConvert_SeqAIJ_SeqSELL);
4354:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqaij_is_C",MatConvert_XAIJ_IS);
4355:   PetscObjectComposeFunction((PetscObject)B,"MatIsTranspose_C",MatIsTranspose_SeqAIJ);
4356:   PetscObjectComposeFunction((PetscObject)B,"MatIsHermitianTranspose_C",MatIsTranspose_SeqAIJ);
4357:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocation_C",MatSeqAIJSetPreallocation_SeqAIJ);
4358:   PetscObjectComposeFunction((PetscObject)B,"MatResetPreallocation_C",MatResetPreallocation_SeqAIJ);
4359:   PetscObjectComposeFunction((PetscObject)B,"MatSeqAIJSetPreallocationCSR_C",MatSeqAIJSetPreallocationCSR_SeqAIJ);
4360:   PetscObjectComposeFunction((PetscObject)B,"MatReorderForNonzeroDiagonal_C",MatReorderForNonzeroDiagonal_SeqAIJ);
4361:   PetscObjectComposeFunction((PetscObject)B,"MatMatMult_seqdense_seqaij_C",MatMatMult_SeqDense_SeqAIJ);
4362:   PetscObjectComposeFunction((PetscObject)B,"MatMatMultSymbolic_seqdense_seqaij_C",MatMatMultSymbolic_SeqDense_SeqAIJ);
4363:   PetscObjectComposeFunction((PetscObject)B,"MatMatMultNumeric_seqdense_seqaij_C",MatMatMultNumeric_SeqDense_SeqAIJ);
4364:   PetscObjectComposeFunction((PetscObject)B,"MatPtAP_is_seqaij_C",MatPtAP_IS_XAIJ);
4365:   MatCreate_SeqAIJ_Inode(B);
4366:   PetscObjectChangeTypeName((PetscObject)B,MATSEQAIJ);
4367:   MatSeqAIJSetTypeFromOptions(B);  /* this allows changing the matrix subtype to say MATSEQAIJPERM */
4368:   return(0);
4369: }

4371: /*
4372:     Given a matrix generated with MatGetFactor() duplicates all the information in A into B
4373: */
4374: PetscErrorCode MatDuplicateNoCreate_SeqAIJ(Mat C,Mat A,MatDuplicateOption cpvalues,PetscBool mallocmatspace)
4375: {
4376:   Mat_SeqAIJ     *c,*a = (Mat_SeqAIJ*)A->data;
4378:   PetscInt       m = A->rmap->n,i;

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

4383:   C->factortype = A->factortype;
4384:   c->row        = 0;
4385:   c->col        = 0;
4386:   c->icol       = 0;
4387:   c->reallocs   = 0;

4389:   C->assembled = PETSC_TRUE;

4391:   PetscLayoutReference(A->rmap,&C->rmap);
4392:   PetscLayoutReference(A->cmap,&C->cmap);

4394:   PetscMalloc1(m,&c->imax);
4395:   PetscMemcpy(c->imax,a->imax,m*sizeof(PetscInt));
4396:   PetscMalloc1(m,&c->ilen);
4397:   PetscMemcpy(c->ilen,a->ilen,m*sizeof(PetscInt));
4398:   PetscLogObjectMemory((PetscObject)C, 2*m*sizeof(PetscInt));

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

4405:     c->singlemalloc = PETSC_TRUE;

4407:     PetscArraycpy(c->i,a->i,m+1);
4408:     if (m > 0) {
4409:       PetscArraycpy(c->j,a->j,a->i[m]);
4410:       if (cpvalues == MAT_COPY_VALUES) {
4411:         PetscArraycpy(c->a,a->a,a->i[m]);
4412:       } else {
4413:         PetscArrayzero(c->a,a->i[m]);
4414:       }
4415:     }
4416:   }

4418:   c->ignorezeroentries = a->ignorezeroentries;
4419:   c->roworiented       = a->roworiented;
4420:   c->nonew             = a->nonew;
4421:   if (a->diag) {
4422:     PetscMalloc1(m+1,&c->diag);
4423:     PetscMemcpy(c->diag,a->diag,m*sizeof(PetscInt));
4424:     PetscLogObjectMemory((PetscObject)C,(m+1)*sizeof(PetscInt));
4425:   } else c->diag = NULL;

4427:   c->solve_work         = 0;
4428:   c->saved_values       = 0;
4429:   c->idiag              = 0;
4430:   c->ssor_work          = 0;
4431:   c->keepnonzeropattern = a->keepnonzeropattern;
4432:   c->free_a             = PETSC_TRUE;
4433:   c->free_ij            = PETSC_TRUE;

4435:   c->rmax         = a->rmax;
4436:   c->nz           = a->nz;
4437:   c->maxnz        = a->nz;       /* Since we allocate exactly the right amount */
4438:   C->preallocated = PETSC_TRUE;

4440:   c->compressedrow.use   = a->compressedrow.use;
4441:   c->compressedrow.nrows = a->compressedrow.nrows;
4442:   if (a->compressedrow.use) {
4443:     i    = a->compressedrow.nrows;
4444:     PetscMalloc2(i+1,&c->compressedrow.i,i,&c->compressedrow.rindex);
4445:     PetscArraycpy(c->compressedrow.i,a->compressedrow.i,i+1);
4446:     PetscArraycpy(c->compressedrow.rindex,a->compressedrow.rindex,i);
4447:   } else {
4448:     c->compressedrow.use    = PETSC_FALSE;
4449:     c->compressedrow.i      = NULL;
4450:     c->compressedrow.rindex = NULL;
4451:   }
4452:   c->nonzerorowcnt = a->nonzerorowcnt;
4453:   C->nonzerostate  = A->nonzerostate;

4455:   MatDuplicate_SeqAIJ_Inode(A,cpvalues,&C);
4456:   PetscFunctionListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);
4457:   return(0);
4458: }

4460: PetscErrorCode MatDuplicate_SeqAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
4461: {

4465:   MatCreate(PetscObjectComm((PetscObject)A),B);
4466:   MatSetSizes(*B,A->rmap->n,A->cmap->n,A->rmap->n,A->cmap->n);
4467:   if (!(A->rmap->n % A->rmap->bs) && !(A->cmap->n % A->cmap->bs)) {
4468:     MatSetBlockSizesFromMats(*B,A,A);
4469:   }
4470:   MatSetType(*B,((PetscObject)A)->type_name);
4471:   MatDuplicateNoCreate_SeqAIJ(*B,A,cpvalues,PETSC_TRUE);
4472:   return(0);
4473: }

4475: PetscErrorCode MatLoad_SeqAIJ(Mat newMat, PetscViewer viewer)
4476: {
4477:   PetscBool      isbinary, ishdf5;

4483:   /* force binary viewer to load .info file if it has not yet done so */
4484:   PetscViewerSetUp(viewer);
4485:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);
4486:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,  &ishdf5);
4487:   if (isbinary) {
4488:     MatLoad_SeqAIJ_Binary(newMat,viewer);
4489:   } else if (ishdf5) {
4490: #if defined(PETSC_HAVE_HDF5)
4491:     MatLoad_AIJ_HDF5(newMat,viewer);
4492: #else
4493:     SETERRQ(PetscObjectComm((PetscObject)newMat),PETSC_ERR_SUP,"HDF5 not supported in this build.\nPlease reconfigure using --download-hdf5");
4494: #endif
4495:   } else {
4496:     SETERRQ2(PetscObjectComm((PetscObject)newMat),PETSC_ERR_SUP,"Viewer type %s not yet supported for reading %s matrices",((PetscObject)viewer)->type_name,((PetscObject)newMat)->type_name);
4497:   }
4498:   return(0);
4499: }

4501: PetscErrorCode MatLoad_SeqAIJ_Binary(Mat newMat, PetscViewer viewer)
4502: {
4503:   Mat_SeqAIJ     *a;
4505:   PetscInt       i,sum,nz,header[4],*rowlengths = 0,M,N,rows,cols;
4506:   int            fd;
4507:   PetscMPIInt    size;
4508:   MPI_Comm       comm;
4509:   PetscInt       bs = newMat->rmap->bs;

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

4516:   PetscOptionsBegin(comm,NULL,"Options for loading SEQAIJ matrix","Mat");
4517:   PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,NULL);
4518:   PetscOptionsEnd();
4519:   if (bs < 0) bs = 1;
4520:   MatSetBlockSize(newMat,bs);

4522:   PetscViewerBinaryGetDescriptor(viewer,&fd);
4523:   PetscBinaryRead(fd,header,4,NULL,PETSC_INT);
4524:   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not matrix object in file");
4525:   M = header[1]; N = header[2]; nz = header[3];

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

4529:   /* read in row lengths */
4530:   PetscMalloc1(M,&rowlengths);
4531:   PetscBinaryRead(fd,rowlengths,M,NULL,PETSC_INT);

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

4537:   /* set global size if not set already*/
4538:   if (newMat->rmap->n < 0 && newMat->rmap->N < 0 && newMat->cmap->n < 0 && newMat->cmap->N < 0) {
4539:     MatSetSizes(newMat,PETSC_DECIDE,PETSC_DECIDE,M,N);
4540:   } else {
4541:     /* if sizes and type are already set, check if the matrix  global sizes are correct */
4542:     MatGetSize(newMat,&rows,&cols);
4543:     if (rows < 0 && cols < 0) { /* user might provide local size instead of global size */
4544:       MatGetLocalSize(newMat,&rows,&cols);
4545:     }
4546:     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);
4547:   }
4548:   MatSeqAIJSetPreallocation_SeqAIJ(newMat,0,rowlengths);
4549:   a    = (Mat_SeqAIJ*)newMat->data;

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

4553:   /* read in nonzero values */
4554:   PetscBinaryRead(fd,a->a,nz,NULL,PETSC_SCALAR);

4556:   /* set matrix "i" values */
4557:   a->i[0] = 0;
4558:   for (i=1; i<= M; i++) {
4559:     a->i[i]      = a->i[i-1] + rowlengths[i-1];
4560:     a->ilen[i-1] = rowlengths[i-1];
4561:   }
4562:   PetscFree(rowlengths);

4564:   MatAssemblyBegin(newMat,MAT_FINAL_ASSEMBLY);
4565:   MatAssemblyEnd(newMat,MAT_FINAL_ASSEMBLY);
4566:   return(0);
4567: }

4569: PetscErrorCode MatEqual_SeqAIJ(Mat A,Mat B,PetscBool * flg)
4570: {
4571:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data,*b = (Mat_SeqAIJ*)B->data;
4573: #if defined(PETSC_USE_COMPLEX)
4574:   PetscInt k;
4575: #endif

4578:   /* If the  matrix dimensions are not equal,or no of nonzeros */
4579:   if ((A->rmap->n != B->rmap->n) || (A->cmap->n != B->cmap->n) ||(a->nz != b->nz)) {
4580:     *flg = PETSC_FALSE;
4581:     return(0);
4582:   }

4584:   /* if the a->i are the same */
4585:   PetscArraycmp(a->i,b->i,A->rmap->n+1,flg);
4586:   if (!*flg) return(0);

4588:   /* if a->j are the same */
4589:   PetscArraycmp(a->j,b->j,a->nz,flg);
4590:   if (!*flg) return(0);

4592:   /* if a->a are the same */
4593: #if defined(PETSC_USE_COMPLEX)
4594:   for (k=0; k<a->nz; k++) {
4595:     if (PetscRealPart(a->a[k]) != PetscRealPart(b->a[k]) || PetscImaginaryPart(a->a[k]) != PetscImaginaryPart(b->a[k])) {
4596:       *flg = PETSC_FALSE;
4597:       return(0);
4598:     }
4599:   }
4600: #else
4601:   PetscArraycmp(a->a,b->a,a->nz,flg);
4602: #endif
4603:   return(0);
4604: }

4606: /*@
4607:      MatCreateSeqAIJWithArrays - Creates an sequential AIJ matrix using matrix elements (in CSR format)
4608:               provided by the user.

4610:       Collective

4612:    Input Parameters:
4613: +   comm - must be an MPI communicator of size 1
4614: .   m - number of rows
4615: .   n - number of columns
4616: .   i - row indices; that is i[0] = 0, i[row] = i[row-1] + number of elements in that row of the matrix
4617: .   j - column indices
4618: -   a - matrix values

4620:    Output Parameter:
4621: .   mat - the matrix

4623:    Level: intermediate

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

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

4631:        The i and j indices are 0 based

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

4637: $        1 0 0
4638: $        2 0 3
4639: $        4 5 6
4640: $
4641: $        i =  {0,1,3,6}  [size = nrow+1  = 3+1]
4642: $        j =  {0,0,2,0,1,2}  [size = 6]; values must be sorted for each row
4643: $        v =  {1,2,3,4,5,6}  [size = 6]


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

4648: @*/
4649: PetscErrorCode  MatCreateSeqAIJWithArrays(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt i[],PetscInt j[],PetscScalar a[],Mat *mat)
4650: {
4652:   PetscInt       ii;
4653:   Mat_SeqAIJ     *aij;
4654: #if defined(PETSC_USE_DEBUG)
4655:   PetscInt jj;
4656: #endif

4659:   if (m > 0 && i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");
4660:   MatCreate(comm,mat);
4661:   MatSetSizes(*mat,m,n,m,n);
4662:   /* MatSetBlockSizes(*mat,,); */
4663:   MatSetType(*mat,MATSEQAIJ);
4664:   MatSeqAIJSetPreallocation_SeqAIJ(*mat,MAT_SKIP_ALLOCATION,0);
4665:   aij  = (Mat_SeqAIJ*)(*mat)->data;
4666:   PetscMalloc1(m,&aij->imax);
4667:   PetscMalloc1(m,&aij->ilen);

4669:   aij->i            = i;
4670:   aij->j            = j;
4671:   aij->a            = a;
4672:   aij->singlemalloc = PETSC_FALSE;
4673:   aij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
4674:   aij->free_a       = PETSC_FALSE;
4675:   aij->free_ij      = PETSC_FALSE;

4677:   for (ii=0; ii<m; ii++) {
4678:     aij->ilen[ii] = aij->imax[ii] = i[ii+1] - i[ii];
4679: #if defined(PETSC_USE_DEBUG)
4680:     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]);
4681:     for (jj=i[ii]+1; jj<i[ii+1]; jj++) {
4682:       if (j[jj] < j[jj-1]) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column entry number %D (actual column %D) in row %D is not sorted",jj-i[ii],j[jj],ii);
4683:       if (j[jj] == j[jj-1]) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column entry number %D (actual column %D) in row %D is identical to previous entry",jj-i[ii],j[jj],ii);
4684:     }
4685: #endif
4686:   }
4687: #if defined(PETSC_USE_DEBUG)
4688:   for (ii=0; ii<aij->i[m]; ii++) {
4689:     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %D index = %D",ii,j[ii]);
4690:     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]);
4691:   }
4692: #endif

4694:   MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);
4695:   MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);
4696:   return(0);
4697: }
4698: /*@C
4699:      MatCreateSeqAIJFromTriple - Creates an sequential AIJ matrix using matrix elements (in COO format)
4700:               provided by the user.

4702:       Collective

4704:    Input Parameters:
4705: +   comm - must be an MPI communicator of size 1
4706: .   m   - number of rows
4707: .   n   - number of columns
4708: .   i   - row indices
4709: .   j   - column indices
4710: .   a   - matrix values
4711: .   nz  - number of nonzeros
4712: -   idx - 0 or 1 based

4714:    Output Parameter:
4715: .   mat - the matrix

4717:    Level: intermediate

4719:    Notes:
4720:        The i and j indices are 0 based

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

4726:         1 0 0
4727:         2 0 3
4728:         4 5 6

4730:         i =  {0,1,1,2,2,2}
4731:         j =  {0,0,2,0,1,2}
4732:         v =  {1,2,3,4,5,6}


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

4737: @*/
4738: PetscErrorCode  MatCreateSeqAIJFromTriple(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt i[],PetscInt j[],PetscScalar a[],Mat *mat,PetscInt nz,PetscBool idx)
4739: {
4741:   PetscInt       ii, *nnz, one = 1,row,col;


4745:   PetscCalloc1(m,&nnz);
4746:   for (ii = 0; ii < nz; ii++) {
4747:     nnz[i[ii] - !!idx] += 1;
4748:   }
4749:   MatCreate(comm,mat);
4750:   MatSetSizes(*mat,m,n,m,n);
4751:   MatSetType(*mat,MATSEQAIJ);
4752:   MatSeqAIJSetPreallocation_SeqAIJ(*mat,0,nnz);
4753:   for (ii = 0; ii < nz; ii++) {
4754:     if (idx) {
4755:       row = i[ii] - 1;
4756:       col = j[ii] - 1;
4757:     } else {
4758:       row = i[ii];
4759:       col = j[ii];
4760:     }
4761:     MatSetValues(*mat,one,&row,one,&col,&a[ii],ADD_VALUES);
4762:   }
4763:   MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);
4764:   MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);
4765:   PetscFree(nnz);
4766:   return(0);
4767: }

4769: PetscErrorCode MatSeqAIJInvalidateDiagonal(Mat A)
4770: {
4771:   Mat_SeqAIJ     *a=(Mat_SeqAIJ*)A->data;

4775:   a->idiagvalid  = PETSC_FALSE;
4776:   a->ibdiagvalid = PETSC_FALSE;

4778:   MatSeqAIJInvalidateDiagonal_Inode(A);
4779:   return(0);
4780: }

4782: PetscErrorCode MatCreateMPIMatConcatenateSeqMat_SeqAIJ(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat)
4783: {
4785:   PetscMPIInt    size;

4788:   MPI_Comm_size(comm,&size);
4789:   if (size == 1) {
4790:     if (scall == MAT_INITIAL_MATRIX) {
4791:       MatDuplicate(inmat,MAT_COPY_VALUES,outmat);
4792:     } else {
4793:       MatCopy(inmat,*outmat,SAME_NONZERO_PATTERN);
4794:     }
4795:   } else {
4796:     MatCreateMPIMatConcatenateSeqMat_MPIAIJ(comm,inmat,n,scall,outmat);
4797:   }
4798:   return(0);
4799: }

4801: /*
4802:  Permute A into C's *local* index space using rowemb,colemb.
4803:  The embedding are supposed to be injections and the above implies that the range of rowemb is a subset
4804:  of [0,m), colemb is in [0,n).
4805:  If pattern == DIFFERENT_NONZERO_PATTERN, C is preallocated according to A.
4806:  */
4807: PetscErrorCode MatSetSeqMat_SeqAIJ(Mat C,IS rowemb,IS colemb,MatStructure pattern,Mat B)
4808: {
4809:   /* If making this function public, change the error returned in this function away from _PLIB. */
4811:   Mat_SeqAIJ     *Baij;
4812:   PetscBool      seqaij;
4813:   PetscInt       m,n,*nz,i,j,count;
4814:   PetscScalar    v;
4815:   const PetscInt *rowindices,*colindices;

4818:   if (!B) return(0);
4819:   /* Check to make sure the target matrix (and embeddings) are compatible with C and each other. */
4820:   PetscObjectBaseTypeCompare((PetscObject)B,MATSEQAIJ,&seqaij);
4821:   if (!seqaij) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is of wrong type");
4822:   if (rowemb) {
4823:     ISGetLocalSize(rowemb,&m);
4824:     if (m != B->rmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Row IS of size %D is incompatible with matrix row size %D",m,B->rmap->n);
4825:   } else {
4826:     if (C->rmap->n != B->rmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is row-incompatible with the target matrix");
4827:   }
4828:   if (colemb) {
4829:     ISGetLocalSize(colemb,&n);
4830:     if (n != B->cmap->n) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Diag col IS of size %D is incompatible with input matrix col size %D",n,B->cmap->n);
4831:   } else {
4832:     if (C->cmap->n != B->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Input matrix is col-incompatible with the target matrix");
4833:   }

4835:   Baij = (Mat_SeqAIJ*)(B->data);
4836:   if (pattern == DIFFERENT_NONZERO_PATTERN) {
4837:     PetscMalloc1(B->rmap->n,&nz);
4838:     for (i=0; i<B->rmap->n; i++) {
4839:       nz[i] = Baij->i[i+1] - Baij->i[i];
4840:     }
4841:     MatSeqAIJSetPreallocation(C,0,nz);
4842:     PetscFree(nz);
4843:   }
4844:   if (pattern == SUBSET_NONZERO_PATTERN) {
4845:     MatZeroEntries(C);
4846:   }
4847:   count = 0;
4848:   rowindices = NULL;
4849:   colindices = NULL;
4850:   if (rowemb) {
4851:     ISGetIndices(rowemb,&rowindices);
4852:   }
4853:   if (colemb) {
4854:     ISGetIndices(colemb,&colindices);
4855:   }
4856:   for (i=0; i<B->rmap->n; i++) {
4857:     PetscInt row;
4858:     row = i;
4859:     if (rowindices) row = rowindices[i];
4860:     for (j=Baij->i[i]; j<Baij->i[i+1]; j++) {
4861:       PetscInt col;
4862:       col  = Baij->j[count];
4863:       if (colindices) col = colindices[col];
4864:       v    = Baij->a[count];
4865:       MatSetValues(C,1,&row,1,&col,&v,INSERT_VALUES);
4866:       ++count;
4867:     }
4868:   }
4869:   /* FIXME: set C's nonzerostate correctly. */
4870:   /* Assembly for C is necessary. */
4871:   C->preallocated = PETSC_TRUE;
4872:   C->assembled     = PETSC_TRUE;
4873:   C->was_assembled = PETSC_FALSE;
4874:   return(0);
4875: }

4877: PetscFunctionList MatSeqAIJList = NULL;

4879: /*@C
4880:    MatSeqAIJSetType - Converts a MATSEQAIJ matrix to a subtype

4882:    Collective on Mat

4884:    Input Parameters:
4885: +  mat      - the matrix object
4886: -  matype   - matrix type

4888:    Options Database Key:
4889: .  -mat_seqai_type  <method> - for example seqaijcrl


4892:   Level: intermediate

4894: .seealso: PCSetType(), VecSetType(), MatCreate(), MatType, Mat
4895: @*/
4896: PetscErrorCode  MatSeqAIJSetType(Mat mat, MatType matype)
4897: {
4898:   PetscErrorCode ierr,(*r)(Mat,MatType,MatReuse,Mat*);
4899:   PetscBool      sametype;

4903:   PetscObjectTypeCompare((PetscObject)mat,matype,&sametype);
4904:   if (sametype) return(0);

4906:    PetscFunctionListFind(MatSeqAIJList,matype,&r);
4907:   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unknown Mat type given: %s",matype);
4908:   (*r)(mat,matype,MAT_INPLACE_MATRIX,&mat);
4909:   return(0);
4910: }


4913: /*@C
4914:   MatSeqAIJRegister -  - Adds a new sub-matrix type for sequential AIJ matrices

4916:    Not Collective

4918:    Input Parameters:
4919: +  name - name of a new user-defined matrix type, for example MATSEQAIJCRL
4920: -  function - routine to convert to subtype

4922:    Notes:
4923:    MatSeqAIJRegister() may be called multiple times to add several user-defined solvers.


4926:    Then, your matrix can be chosen with the procedural interface at runtime via the option
4927: $     -mat_seqaij_type my_mat

4929:    Level: advanced

4931: .seealso: MatSeqAIJRegisterAll()


4934:   Level: advanced
4935: @*/
4936: PetscErrorCode  MatSeqAIJRegister(const char sname[],PetscErrorCode (*function)(Mat,MatType,MatReuse,Mat *))
4937: {

4941:   MatInitializePackage();
4942:   PetscFunctionListAdd(&MatSeqAIJList,sname,function);
4943:   return(0);
4944: }

4946: PetscBool MatSeqAIJRegisterAllCalled = PETSC_FALSE;

4948: /*@C
4949:   MatSeqAIJRegisterAll - Registers all of the matrix subtypes of SeqAIJ

4951:   Not Collective

4953:   Level: advanced

4955:   Developers Note: CUSP and CUSPARSE do not yet support the  MatConvert_SeqAIJ..() paradigm and thus cannot be registered here

4957: .seealso:  MatRegisterAll(), MatSeqAIJRegister()
4958: @*/
4959: PetscErrorCode  MatSeqAIJRegisterAll(void)
4960: {

4964:   if (MatSeqAIJRegisterAllCalled) return(0);
4965:   MatSeqAIJRegisterAllCalled = PETSC_TRUE;

4967:   MatSeqAIJRegister(MATSEQAIJCRL,      MatConvert_SeqAIJ_SeqAIJCRL);
4968:   MatSeqAIJRegister(MATSEQAIJPERM,     MatConvert_SeqAIJ_SeqAIJPERM);
4969:   MatSeqAIJRegister(MATSEQAIJSELL,     MatConvert_SeqAIJ_SeqAIJSELL);
4970: #if defined(PETSC_HAVE_MKL_SPARSE)
4971:   MatSeqAIJRegister(MATSEQAIJMKL,      MatConvert_SeqAIJ_SeqAIJMKL);
4972: #endif
4973: #if defined(PETSC_HAVE_VIENNACL) && defined(PETSC_HAVE_VIENNACL_NO_CUDA)
4974:   MatSeqAIJRegister(MATMPIAIJVIENNACL, MatConvert_SeqAIJ_SeqAIJViennaCL);
4975: #endif
4976:   return(0);
4977: }

4979: /*
4980:     Special version for direct calls from Fortran
4981: */
4982:  #include <petsc/private/fortranimpl.h>
4983: #if defined(PETSC_HAVE_FORTRAN_CAPS)
4984: #define matsetvaluesseqaij_ MATSETVALUESSEQAIJ
4985: #elif !defined(PETSC_HAVE_FORTRAN_UNDERSCORE)
4986: #define matsetvaluesseqaij_ matsetvaluesseqaij
4987: #endif

4989: /* Change these macros so can be used in void function */
4990: #undef CHKERRQ
4991: #define CHKERRQ(ierr) CHKERRABORT(PetscObjectComm((PetscObject)A),ierr)
4992: #undef SETERRQ2
4993: #define SETERRQ2(comm,ierr,b,c,d) CHKERRABORT(comm,ierr)
4994: #undef SETERRQ3
4995: #define SETERRQ3(comm,ierr,b,c,d,e) CHKERRABORT(comm,ierr)

4997: 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)
4998: {
4999:   Mat            A  = *AA;
5000:   PetscInt       m  = *mm, n = *nn;
5001:   InsertMode     is = *isis;
5002:   Mat_SeqAIJ     *a = (Mat_SeqAIJ*)A->data;
5003:   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N;
5004:   PetscInt       *imax,*ai,*ailen;
5006:   PetscInt       *aj,nonew = a->nonew,lastcol = -1;
5007:   MatScalar      *ap,value,*aa;
5008:   PetscBool      ignorezeroentries = a->ignorezeroentries;
5009:   PetscBool      roworiented       = a->roworiented;

5012:   MatCheckPreallocated(A,1);
5013:   imax  = a->imax;
5014:   ai    = a->i;
5015:   ailen = a->ilen;
5016:   aj    = a->j;
5017:   aa    = a->a;

5019:   for (k=0; k<m; k++) { /* loop over added rows */
5020:     row = im[k];
5021:     if (row < 0) continue;
5022: #if defined(PETSC_USE_DEBUG)
5023:     if (row >= A->rmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Row too large");
5024: #endif
5025:     rp   = aj + ai[row]; ap = aa + ai[row];
5026:     rmax = imax[row]; nrow = ailen[row];
5027:     low  = 0;
5028:     high = nrow;
5029:     for (l=0; l<n; l++) { /* loop over added columns */
5030:       if (in[l] < 0) continue;
5031: #if defined(PETSC_USE_DEBUG)
5032:       if (in[l] >= A->cmap->n) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Column too large");
5033: #endif
5034:       col = in[l];
5035:       if (roworiented) value = v[l + k*n];
5036:       else value = v[k + l*m];

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

5040:       if (col <= lastcol) low = 0;
5041:       else high = nrow;
5042:       lastcol = col;
5043:       while (high-low > 5) {
5044:         t = (low+high)/2;
5045:         if (rp[t] > col) high = t;
5046:         else             low  = t;
5047:       }
5048:       for (i=low; i<high; i++) {
5049:         if (rp[i] > col) break;
5050:         if (rp[i] == col) {
5051:           if (is == ADD_VALUES) ap[i] += value;
5052:           else                  ap[i] = value;
5053:           goto noinsert;
5054:         }
5055:       }
5056:       if (value == 0.0 && ignorezeroentries) goto noinsert;
5057:       if (nonew == 1) goto noinsert;
5058:       if (nonew == -1) SETERRABORT(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero in the matrix");
5059:       MatSeqXAIJReallocateAIJ(A,A->rmap->n,1,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
5060:       N = nrow++ - 1; a->nz++; high++;
5061:       /* shift up all the later entries in this row */
5062:       for (ii=N; ii>=i; ii--) {
5063:         rp[ii+1] = rp[ii];
5064:         ap[ii+1] = ap[ii];
5065:       }
5066:       rp[i] = col;
5067:       ap[i] = value;
5068:       A->nonzerostate++;
5069: noinsert:;
5070:       low = i + 1;
5071:     }
5072:     ailen[row] = nrow;
5073:   }
5074:   PetscFunctionReturnVoid();
5075: }