Actual source code: sbaij.c

petsc-master 2015-01-31
Report Typos and Errors
  2: /*
  3:     Defines the basic matrix operations for the SBAIJ (compressed row)
  4:   matrix storage format.
  5: */
  6: #include <../src/mat/impls/baij/seq/baij.h>         /*I "petscmat.h" I*/
  7: #include <../src/mat/impls/sbaij/seq/sbaij.h>
  8: #include <petscblaslapack.h>

 10: #include <../src/mat/impls/sbaij/seq/relax.h>
 11: #define USESHORT
 12: #include <../src/mat/impls/sbaij/seq/relax.h>

 14: extern PetscErrorCode MatSeqSBAIJSetNumericFactorization_inplace(Mat,PetscBool);
 15: #if defined(PETSC_HAVE_ELEMENTAL)
 16: PETSC_EXTERN PetscErrorCode MatConvert_SeqSBAIJ_Elemental(Mat,MatType,MatReuse,Mat*);
 17: #endif

 19: /*
 20:      Checks for missing diagonals
 21: */
 24: PetscErrorCode MatMissingDiagonal_SeqSBAIJ(Mat A,PetscBool  *missing,PetscInt *dd)
 25: {
 26:   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
 28:   PetscInt       *diag,*ii = a->i,i;

 31:   MatMarkDiagonal_SeqSBAIJ(A);
 32:   *missing = PETSC_FALSE;
 33:   if (A->rmap->n > 0 && !ii) {
 34:     *missing = PETSC_TRUE;
 35:     if (dd) *dd = 0;
 36:     PetscInfo(A,"Matrix has no entries therefore is missing diagonal\n");
 37:   } else {
 38:     diag = a->diag;
 39:     for (i=0; i<a->mbs; i++) {
 40:       if (diag[i] >= ii[i+1]) {
 41:         *missing = PETSC_TRUE;
 42:         if (dd) *dd = i;
 43:         break;
 44:       }
 45:     }
 46:   }
 47:   return(0);
 48: }

 52: PetscErrorCode MatMarkDiagonal_SeqSBAIJ(Mat A)
 53: {
 54:   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
 56:   PetscInt       i,j;

 59:   if (!a->diag) {
 60:     PetscMalloc1(a->mbs,&a->diag);
 61:     PetscLogObjectMemory((PetscObject)A,a->mbs*sizeof(PetscInt));
 62:     a->free_diag = PETSC_TRUE;
 63:   }
 64:   for (i=0; i<a->mbs; i++) {
 65:     a->diag[i] = a->i[i+1];
 66:     for (j=a->i[i]; j<a->i[i+1]; j++) {
 67:       if (a->j[j] == i) {
 68:         a->diag[i] = j;
 69:         break;
 70:       }
 71:     }
 72:   }
 73:   return(0);
 74: }

 78: static PetscErrorCode MatGetRowIJ_SeqSBAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool blockcompressed,PetscInt *nn,const PetscInt *inia[],const PetscInt *inja[],PetscBool  *done)
 79: {
 80:   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
 81:   PetscInt       i,j,n = a->mbs,nz = a->i[n],bs = A->rmap->bs;
 82:   PetscInt       **ia = (PetscInt**)inia,**ja = (PetscInt**)inja;

 86:   *nn = n;
 87:   if (!ia) return(0);
 88:   if (!blockcompressed) {
 89:     /* malloc & create the natural set of indices */
 90:     PetscMalloc2((n+1)*bs,ia,nz*bs,ja);
 91:     for (i=0; i<n+1; i++) {
 92:       for (j=0; j<bs; j++) {
 93:         *ia[i*bs+j] = a->i[i]*bs+j+oshift;
 94:       }
 95:     }
 96:     for (i=0; i<nz; i++) {
 97:       for (j=0; j<bs; j++) {
 98:         *ja[i*bs+j] = a->j[i]*bs+j+oshift;
 99:       }
100:     }
101:   } else { /* blockcompressed */
102:     if (oshift == 1) {
103:       /* temporarily add 1 to i and j indices */
104:       for (i=0; i<nz; i++) a->j[i]++;
105:       for (i=0; i<n+1; i++) a->i[i]++;
106:     }
107:     *ia = a->i; *ja = a->j;
108:   }
109:   return(0);
110: }

114: static PetscErrorCode MatRestoreRowIJ_SeqSBAIJ(Mat A,PetscInt oshift,PetscBool symmetric,PetscBool blockcompressed,PetscInt *nn,const PetscInt *ia[],const PetscInt *ja[],PetscBool  *done)
115: {
116:   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
117:   PetscInt       i,n = a->mbs,nz = a->i[n];

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

123:   if (!blockcompressed) {
124:     PetscFree2(*ia,*ja);
125:   } else if (oshift == 1) { /* blockcompressed */
126:     for (i=0; i<nz; i++) a->j[i]--;
127:     for (i=0; i<n+1; i++) a->i[i]--;
128:   }
129:   return(0);
130: }

134: PetscErrorCode MatDestroy_SeqSBAIJ(Mat A)
135: {
136:   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;

140: #if defined(PETSC_USE_LOG)
141:   PetscLogObjectState((PetscObject)A,"Rows=%D, NZ=%D",A->rmap->N,a->nz);
142: #endif
143:   MatSeqXAIJFreeAIJ(A,&a->a,&a->j,&a->i);
144:   if (a->free_diag) {PetscFree(a->diag);}
145:   ISDestroy(&a->row);
146:   ISDestroy(&a->col);
147:   ISDestroy(&a->icol);
148:   PetscFree(a->idiag);
149:   PetscFree(a->inode.size);
150:   if (a->free_imax_ilen) {PetscFree2(a->imax,a->ilen);}
151:   PetscFree(a->solve_work);
152:   PetscFree(a->sor_work);
153:   PetscFree(a->solves_work);
154:   PetscFree(a->mult_work);
155:   PetscFree(a->saved_values);
156:   if (a->free_jshort) {PetscFree(a->jshort);}
157:   PetscFree(a->inew);
158:   MatDestroy(&a->parent);
159:   PetscFree(A->data);

161:   PetscObjectChangeTypeName((PetscObject)A,0);
162:   PetscObjectComposeFunction((PetscObject)A,"MatStoreValues_C",NULL);
163:   PetscObjectComposeFunction((PetscObject)A,"MatRetrieveValues_C",NULL);
164:   PetscObjectComposeFunction((PetscObject)A,"MatSeqSBAIJSetColumnIndices_C",NULL);
165:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqsbaij_seqaij_C",NULL);
166:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqsbaij_seqbaij_C",NULL);
167:   PetscObjectComposeFunction((PetscObject)A,"MatSeqSBAIJSetPreallocation_C",NULL);
168:   PetscObjectComposeFunction((PetscObject)A,"MatSeqSBAIJSetPreallocationCSR_C",NULL);
169:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqsbaij_seqsbstrm_C",NULL);
170: #if defined(PETSC_HAVE_ELEMENTAL)
171:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_seqsbaij_elemental_C",NULL);
172: #endif
173:   return(0);
174: }

178: PetscErrorCode MatSetOption_SeqSBAIJ(Mat A,MatOption op,PetscBool flg)
179: {
180:   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;

184:   switch (op) {
185:   case MAT_ROW_ORIENTED:
186:     a->roworiented = flg;
187:     break;
188:   case MAT_KEEP_NONZERO_PATTERN:
189:     a->keepnonzeropattern = flg;
190:     break;
191:   case MAT_NEW_NONZERO_LOCATIONS:
192:     a->nonew = (flg ? 0 : 1);
193:     break;
194:   case MAT_NEW_NONZERO_LOCATION_ERR:
195:     a->nonew = (flg ? -1 : 0);
196:     break;
197:   case MAT_NEW_NONZERO_ALLOCATION_ERR:
198:     a->nonew = (flg ? -2 : 0);
199:     break;
200:   case MAT_UNUSED_NONZERO_LOCATION_ERR:
201:     a->nounused = (flg ? -1 : 0);
202:     break;
203:   case MAT_NEW_DIAGONALS:
204:   case MAT_IGNORE_OFF_PROC_ENTRIES:
205:   case MAT_USE_HASH_TABLE:
206:     PetscInfo1(A,"Option %s ignored\n",MatOptions[op]);
207:     break;
208:   case MAT_HERMITIAN:
209:     if (!A->assembled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call MatAssemblyEnd() first");
210:     if (A->cmap->n < 65536 && A->cmap->bs == 1) {
211:       A->ops->mult = MatMult_SeqSBAIJ_1_Hermitian_ushort;
212:     } else if (A->cmap->bs == 1) {
213:       A->ops->mult = MatMult_SeqSBAIJ_1_Hermitian;
214:     } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for Hermitian with block size greater than 1");
215:     break;
216:   case MAT_SPD:
217:     /* These options are handled directly by MatSetOption() */
218:     break;
219:   case MAT_SYMMETRIC:
220:   case MAT_STRUCTURALLY_SYMMETRIC:
221:   case MAT_SYMMETRY_ETERNAL:
222:     /* These options are handled directly by MatSetOption() */
223:     break;
224:   case MAT_IGNORE_LOWER_TRIANGULAR:
225:     a->ignore_ltriangular = flg;
226:     break;
227:   case MAT_ERROR_LOWER_TRIANGULAR:
228:     a->ignore_ltriangular = flg;
229:     break;
230:   case MAT_GETROW_UPPERTRIANGULAR:
231:     a->getrow_utriangular = flg;
232:     break;
233:   default:
234:     SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"unknown option %d",op);
235:   }
236:   return(0);
237: }

241: PetscErrorCode MatGetRow_SeqSBAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
242: {
243:   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;

247:   if (A && !a->getrow_utriangular) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"MatGetRow is not supported for SBAIJ matrix format. Getting the upper triangular part of row, run with -mat_getrow_uppertriangular, call MatSetOption(mat,MAT_GETROW_UPPERTRIANGULAR,PETSC_TRUE) or MatGetRowUpperTriangular()");

249:   /* Get the upper triangular part of the row */
250:   MatGetRow_SeqBAIJ_private(A,row,nz,idx,v,a->i,a->j,a->a);
251:   return(0);
252: }

256: PetscErrorCode MatRestoreRow_SeqSBAIJ(Mat A,PetscInt row,PetscInt *nz,PetscInt **idx,PetscScalar **v)
257: {

261:   if (idx) {PetscFree(*idx);}
262:   if (v)   {PetscFree(*v);}
263:   return(0);
264: }

268: PetscErrorCode MatGetRowUpperTriangular_SeqSBAIJ(Mat A)
269: {
270:   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;

273:   a->getrow_utriangular = PETSC_TRUE;
274:   return(0);
275: }
278: PetscErrorCode MatRestoreRowUpperTriangular_SeqSBAIJ(Mat A)
279: {
280:   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;

283:   a->getrow_utriangular = PETSC_FALSE;
284:   return(0);
285: }

289: PetscErrorCode MatTranspose_SeqSBAIJ(Mat A,MatReuse reuse,Mat *B)
290: {

294:   if (reuse == MAT_INITIAL_MATRIX || *B != A) {
295:     MatDuplicate(A,MAT_COPY_VALUES,B);
296:   }
297:   return(0);
298: }

302: PetscErrorCode MatView_SeqSBAIJ_ASCII(Mat A,PetscViewer viewer)
303: {
304:   Mat_SeqSBAIJ      *a = (Mat_SeqSBAIJ*)A->data;
305:   PetscErrorCode    ierr;
306:   PetscInt          i,j,bs = A->rmap->bs,k,l,bs2=a->bs2;
307:   PetscViewerFormat format;
308:   PetscInt          *diag;

311:   PetscViewerGetFormat(viewer,&format);
312:   if (format == PETSC_VIEWER_ASCII_INFO || format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
313:     PetscViewerASCIIPrintf(viewer,"  block size is %D\n",bs);
314:   } else if (format == PETSC_VIEWER_ASCII_MATLAB) {
315:     Mat        aij;
316:     const char *matname;

318:     if (A->factortype && bs>1) {
319:       PetscPrintf(PETSC_COMM_SELF,"Warning: matrix is factored with bs>1. MatView() with PETSC_VIEWER_ASCII_MATLAB is not supported and ignored!\n");
320:       return(0);
321:     }
322:     MatConvert(A,MATSEQAIJ,MAT_INITIAL_MATRIX,&aij);
323:     PetscObjectGetName((PetscObject)A,&matname);
324:     PetscObjectSetName((PetscObject)aij,matname);
325:     MatView(aij,viewer);
326:     MatDestroy(&aij);
327:   } else if (format == PETSC_VIEWER_ASCII_COMMON) {
328:     PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
329:     for (i=0; i<a->mbs; i++) {
330:       for (j=0; j<bs; j++) {
331:         PetscViewerASCIIPrintf(viewer,"row %D:",i*bs+j);
332:         for (k=a->i[i]; k<a->i[i+1]; k++) {
333:           for (l=0; l<bs; l++) {
334: #if defined(PETSC_USE_COMPLEX)
335:             if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) > 0.0 && PetscRealPart(a->a[bs2*k + l*bs + j]) != 0.0) {
336:               PetscViewerASCIIPrintf(viewer," (%D, %g + %g i) ",bs*a->j[k]+l,
337:                                             (double)PetscRealPart(a->a[bs2*k + l*bs + j]),(double)PetscImaginaryPart(a->a[bs2*k + l*bs + j]));
338:             } else if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) < 0.0 && PetscRealPart(a->a[bs2*k + l*bs + j]) != 0.0) {
339:               PetscViewerASCIIPrintf(viewer," (%D, %g - %g i) ",bs*a->j[k]+l,
340:                                             (double)PetscRealPart(a->a[bs2*k + l*bs + j]),-(double)PetscImaginaryPart(a->a[bs2*k + l*bs + j]));
341:             } else if (PetscRealPart(a->a[bs2*k + l*bs + j]) != 0.0) {
342:               PetscViewerASCIIPrintf(viewer," (%D, %g) ",bs*a->j[k]+l,(double)PetscRealPart(a->a[bs2*k + l*bs + j]));
343:             }
344: #else
345:             if (a->a[bs2*k + l*bs + j] != 0.0) {
346:               PetscViewerASCIIPrintf(viewer," (%D, %g) ",bs*a->j[k]+l,(double)a->a[bs2*k + l*bs + j]);
347:             }
348: #endif
349:           }
350:         }
351:         PetscViewerASCIIPrintf(viewer,"\n");
352:       }
353:     }
354:     PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
355:   } else if (format == PETSC_VIEWER_ASCII_FACTOR_INFO) {
356:     return(0);
357:   } else {
358:     PetscViewerASCIIUseTabs(viewer,PETSC_FALSE);
359:     if (A->factortype) { /* for factored matrix */
360:       if (bs>1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"matrix is factored with bs>1. Not implemented yet");

362:       diag=a->diag;
363:       for (i=0; i<a->mbs; i++) { /* for row block i */
364:         PetscViewerASCIIPrintf(viewer,"row %D:",i);
365:         /* diagonal entry */
366: #if defined(PETSC_USE_COMPLEX)
367:         if (PetscImaginaryPart(a->a[diag[i]]) > 0.0) {
368:           PetscViewerASCIIPrintf(viewer," (%D, %g + %g i) ",a->j[diag[i]],(double)PetscRealPart(1.0/a->a[diag[i]]),(double)PetscImaginaryPart(1.0/a->a[diag[i]]));
369:         } else if (PetscImaginaryPart(a->a[diag[i]]) < 0.0) {
370:           PetscViewerASCIIPrintf(viewer," (%D, %g - %g i) ",a->j[diag[i]],(double)PetscRealPart(1.0/a->a[diag[i]]),-(double)PetscImaginaryPart(1.0/a->a[diag[i]]));
371:         } else {
372:           PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[diag[i]],(double)PetscRealPart(1.0/a->a[diag[i]]));
373:         }
374: #else
375:         PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[diag[i]],(double)(1.0/a->a[diag[i]]));
376: #endif
377:         /* off-diagonal entries */
378:         for (k=a->i[i]; k<a->i[i+1]-1; k++) {
379: #if defined(PETSC_USE_COMPLEX)
380:           if (PetscImaginaryPart(a->a[k]) > 0.0) {
381:             PetscViewerASCIIPrintf(viewer," (%D, %g + %g i) ",bs*a->j[k],(double)PetscRealPart(a->a[k]),(double)PetscImaginaryPart(a->a[k]));
382:           } else if (PetscImaginaryPart(a->a[k]) < 0.0) {
383:             PetscViewerASCIIPrintf(viewer," (%D, %g - %g i) ",bs*a->j[k],(double)PetscRealPart(a->a[k]),-(double)PetscImaginaryPart(a->a[k]));
384:           } else {
385:             PetscViewerASCIIPrintf(viewer," (%D, %g) ",bs*a->j[k],(double)PetscRealPart(a->a[k]));
386:           }
387: #else
388:           PetscViewerASCIIPrintf(viewer," (%D, %g) ",a->j[k],(double)a->a[k]);
389: #endif
390:         }
391:         PetscViewerASCIIPrintf(viewer,"\n");
392:       }

394:     } else { /* for non-factored matrix */
395:       for (i=0; i<a->mbs; i++) { /* for row block i */
396:         for (j=0; j<bs; j++) {   /* for row bs*i + j */
397:           PetscViewerASCIIPrintf(viewer,"row %D:",i*bs+j);
398:           for (k=a->i[i]; k<a->i[i+1]; k++) { /* for column block */
399:             for (l=0; l<bs; l++) {            /* for column */
400: #if defined(PETSC_USE_COMPLEX)
401:               if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) > 0.0) {
402:                 PetscViewerASCIIPrintf(viewer," (%D, %g + %g i) ",bs*a->j[k]+l,
403:                                               (double)PetscRealPart(a->a[bs2*k + l*bs + j]),(double)PetscImaginaryPart(a->a[bs2*k + l*bs + j]));
404:               } else if (PetscImaginaryPart(a->a[bs2*k + l*bs + j]) < 0.0) {
405:                 PetscViewerASCIIPrintf(viewer," (%D, %g - %g i) ",bs*a->j[k]+l,
406:                                               (double)PetscRealPart(a->a[bs2*k + l*bs + j]),-(double)PetscImaginaryPart(a->a[bs2*k + l*bs + j]));
407:               } else {
408:                 PetscViewerASCIIPrintf(viewer," (%D, %g) ",bs*a->j[k]+l,(double)PetscRealPart(a->a[bs2*k + l*bs + j]));
409:               }
410: #else
411:               PetscViewerASCIIPrintf(viewer," (%D, %g) ",bs*a->j[k]+l,(double)a->a[bs2*k + l*bs + j]);
412: #endif
413:             }
414:           }
415:           PetscViewerASCIIPrintf(viewer,"\n");
416:         }
417:       }
418:     }
419:     PetscViewerASCIIUseTabs(viewer,PETSC_TRUE);
420:   }
421:   PetscViewerFlush(viewer);
422:   return(0);
423: }

425: #include <petscdraw.h>
428: static PetscErrorCode MatView_SeqSBAIJ_Draw_Zoom(PetscDraw draw,void *Aa)
429: {
430:   Mat            A = (Mat) Aa;
431:   Mat_SeqSBAIJ   *a=(Mat_SeqSBAIJ*)A->data;
433:   PetscInt       row,i,j,k,l,mbs=a->mbs,color,bs=A->rmap->bs,bs2=a->bs2;
434:   PetscMPIInt    rank;
435:   PetscReal      xl,yl,xr,yr,x_l,x_r,y_l,y_r;
436:   MatScalar      *aa;
437:   MPI_Comm       comm;
438:   PetscViewer    viewer;

441:   /*
442:     This is nasty. If this is called from an originally parallel matrix
443:     then all processes call this,but only the first has the matrix so the
444:     rest should return immediately.
445:   */
446:   PetscObjectGetComm((PetscObject)draw,&comm);
447:   MPI_Comm_rank(comm,&rank);
448:   if (rank) return(0);

450:   PetscObjectQuery((PetscObject)A,"Zoomviewer",(PetscObject*)&viewer);

452:   PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);
453:   PetscDrawString(draw, .3*(xl+xr), .3*(yl+yr), PETSC_DRAW_BLACK, "symmetric");

455:   /* loop over matrix elements drawing boxes */
456:   color = PETSC_DRAW_BLUE;
457:   for (i=0,row=0; i<mbs; i++,row+=bs) {
458:     for (j=a->i[i]; j<a->i[i+1]; j++) {
459:       y_l = A->rmap->N - row - 1.0; y_r = y_l + 1.0;
460:       x_l = a->j[j]*bs; x_r = x_l + 1.0;
461:       aa  = a->a + j*bs2;
462:       for (k=0; k<bs; k++) {
463:         for (l=0; l<bs; l++) {
464:           if (PetscRealPart(*aa++) >=  0.) continue;
465:           PetscDrawRectangle(draw,x_l+k,y_l-l,x_r+k,y_r-l,color,color,color,color);
466:         }
467:       }
468:     }
469:   }
470:   color = PETSC_DRAW_CYAN;
471:   for (i=0,row=0; i<mbs; i++,row+=bs) {
472:     for (j=a->i[i]; j<a->i[i+1]; j++) {
473:       y_l = A->rmap->N - row - 1.0; y_r = y_l + 1.0;
474:       x_l = a->j[j]*bs; x_r = x_l + 1.0;
475:       aa = a->a + j*bs2;
476:       for (k=0; k<bs; k++) {
477:         for (l=0; l<bs; l++) {
478:           if (PetscRealPart(*aa++) != 0.) continue;
479:           PetscDrawRectangle(draw,x_l+k,y_l-l,x_r+k,y_r-l,color,color,color,color);
480:         }
481:       }
482:     }
483:   }

485:   color = PETSC_DRAW_RED;
486:   for (i=0,row=0; i<mbs; i++,row+=bs) {
487:     for (j=a->i[i]; j<a->i[i+1]; j++) {
488:       y_l = A->rmap->N - row - 1.0; y_r = y_l + 1.0;
489:       x_l = a->j[j]*bs; x_r = x_l + 1.0;
490:       aa = a->a + j*bs2;
491:       for (k=0; k<bs; k++) {
492:         for (l=0; l<bs; l++) {
493:           if (PetscRealPart(*aa++) <= 0.) continue;
494:           PetscDrawRectangle(draw,x_l+k,y_l-l,x_r+k,y_r-l,color,color,color,color);
495:         }
496:       }
497:     }
498:   }
499:   return(0);
500: }

504: static PetscErrorCode MatView_SeqSBAIJ_Draw(Mat A,PetscViewer viewer)
505: {
507:   PetscReal      xl,yl,xr,yr,w,h;
508:   PetscDraw      draw;
509:   PetscBool      isnull;

512:   PetscViewerDrawGetDraw(viewer,0,&draw);
513:   PetscDrawIsNull(draw,&isnull); if (isnull) return(0);

515:   PetscObjectCompose((PetscObject)A,"Zoomviewer",(PetscObject)viewer);
516:   xr   = A->rmap->N; yr = A->rmap->N; h = yr/10.0; w = xr/10.0;
517:   xr  += w;    yr += h;  xl = -w;     yl = -h;
518:   PetscDrawSetCoordinates(draw,xl,yl,xr,yr);
519:   PetscDrawZoom(draw,MatView_SeqSBAIJ_Draw_Zoom,A);
520:   PetscObjectCompose((PetscObject)A,"Zoomviewer",NULL);
521:   return(0);
522: }

526: PetscErrorCode MatView_SeqSBAIJ(Mat A,PetscViewer viewer)
527: {
529:   PetscBool      iascii,isdraw;
530:   FILE           *file = 0;

533:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
534:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
535:   if (iascii) {
536:     MatView_SeqSBAIJ_ASCII(A,viewer);
537:   } else if (isdraw) {
538:     MatView_SeqSBAIJ_Draw(A,viewer);
539:   } else {
540:     Mat        B;
541:     const char *matname;
542:     MatConvert(A,MATSEQAIJ,MAT_INITIAL_MATRIX,&B);
543:     PetscObjectGetName((PetscObject)A,&matname);
544:     PetscObjectSetName((PetscObject)B,matname);
545:     MatView(B,viewer);
546:     MatDestroy(&B);
547:     PetscViewerBinaryGetInfoPointer(viewer,&file);
548:     if (file) {
549:       fprintf(file,"-matload_block_size %d\n",(int)A->rmap->bs);
550:     }
551:   }
552:   return(0);
553: }


558: PetscErrorCode MatGetValues_SeqSBAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],PetscScalar v[])
559: {
560:   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
561:   PetscInt     *rp,k,low,high,t,row,nrow,i,col,l,*aj = a->j;
562:   PetscInt     *ai = a->i,*ailen = a->ilen;
563:   PetscInt     brow,bcol,ridx,cidx,bs=A->rmap->bs,bs2=a->bs2;
564:   MatScalar    *ap,*aa = a->a;

567:   for (k=0; k<m; k++) { /* loop over rows */
568:     row = im[k]; brow = row/bs;
569:     if (row < 0) {v += n; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative row: %D",row); */
570:     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);
571:     rp   = aj + ai[brow]; ap = aa + bs2*ai[brow];
572:     nrow = ailen[brow];
573:     for (l=0; l<n; l++) { /* loop over columns */
574:       if (in[l] < 0) {v++; continue;} /* SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column: %D",in[l]); */
575:       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);
576:       col  = in[l];
577:       bcol = col/bs;
578:       cidx = col%bs;
579:       ridx = row%bs;
580:       high = nrow;
581:       low  = 0; /* assume unsorted */
582:       while (high-low > 5) {
583:         t = (low+high)/2;
584:         if (rp[t] > bcol) high = t;
585:         else              low  = t;
586:       }
587:       for (i=low; i<high; i++) {
588:         if (rp[i] > bcol) break;
589:         if (rp[i] == bcol) {
590:           *v++ = ap[bs2*i+bs*cidx+ridx];
591:           goto finished;
592:         }
593:       }
594:       *v++ = 0.0;
595: finished:;
596:     }
597:   }
598:   return(0);
599: }


604: PetscErrorCode MatSetValuesBlocked_SeqSBAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
605: {
606:   Mat_SeqSBAIJ      *a = (Mat_SeqSBAIJ*)A->data;
607:   PetscErrorCode    ierr;
608:   PetscInt          *rp,k,low,high,t,ii,jj,row,nrow,i,col,l,rmax,N,lastcol = -1;
609:   PetscInt          *imax      =a->imax,*ai=a->i,*ailen=a->ilen;
610:   PetscInt          *aj        =a->j,nonew=a->nonew,bs2=a->bs2,bs=A->rmap->bs,stepval;
611:   PetscBool         roworiented=a->roworiented;
612:   const PetscScalar *value     = v;
613:   MatScalar         *ap,*aa = a->a,*bap;

616:   if (roworiented) stepval = (n-1)*bs;
617:   else stepval = (m-1)*bs;

619:   for (k=0; k<m; k++) { /* loop over added rows */
620:     row = im[k];
621:     if (row < 0) continue;
622: #if defined(PETSC_USE_DEBUG)
623:     if (row >= a->mbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",row,a->mbs-1);
624: #endif
625:     rp   = aj + ai[row];
626:     ap   = aa + bs2*ai[row];
627:     rmax = imax[row];
628:     nrow = ailen[row];
629:     low  = 0;
630:     high = nrow;
631:     for (l=0; l<n; l++) { /* loop over added columns */
632:       if (in[l] < 0) continue;
633:       col = in[l];
634: #if defined(PETSC_USE_DEBUG)
635:       if (col >= a->nbs) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",col,a->nbs-1);
636: #endif
637:       if (col < row) {
638:         if (a->ignore_ltriangular) continue; /* ignore lower triangular block */
639:         else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Lower triangular value cannot be set for sbaij format. Ignoring these values, run with -mat_ignore_lower_triangular or call MatSetOption(mat,MAT_IGNORE_LOWER_TRIANGULAR,PETSC_TRUE)");
640:       }
641:       if (roworiented) value = v + k*(stepval+bs)*bs + l*bs;
642:       else value = v + l*(stepval+bs)*bs + k*bs;

644:       if (col <= lastcol) low = 0;
645:       else high = nrow;

647:       lastcol = col;
648:       while (high-low > 7) {
649:         t = (low+high)/2;
650:         if (rp[t] > col) high = t;
651:         else             low  = t;
652:       }
653:       for (i=low; i<high; i++) {
654:         if (rp[i] > col) break;
655:         if (rp[i] == col) {
656:           bap = ap +  bs2*i;
657:           if (roworiented) {
658:             if (is == ADD_VALUES) {
659:               for (ii=0; ii<bs; ii++,value+=stepval) {
660:                 for (jj=ii; jj<bs2; jj+=bs) {
661:                   bap[jj] += *value++;
662:                 }
663:               }
664:             } else {
665:               for (ii=0; ii<bs; ii++,value+=stepval) {
666:                 for (jj=ii; jj<bs2; jj+=bs) {
667:                   bap[jj] = *value++;
668:                 }
669:                }
670:             }
671:           } else {
672:             if (is == ADD_VALUES) {
673:               for (ii=0; ii<bs; ii++,value+=stepval) {
674:                 for (jj=0; jj<bs; jj++) {
675:                   *bap++ += *value++;
676:                 }
677:               }
678:             } else {
679:               for (ii=0; ii<bs; ii++,value+=stepval) {
680:                 for (jj=0; jj<bs; jj++) {
681:                   *bap++  = *value++;
682:                 }
683:               }
684:             }
685:           }
686:           goto noinsert2;
687:         }
688:       }
689:       if (nonew == 1) goto noinsert2;
690:       if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) in the matrix", row, col);
691:       MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,row,col,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);
692:       N = nrow++ - 1; high++;
693:       /* shift up all the later entries in this row */
694:       for (ii=N; ii>=i; ii--) {
695:         rp[ii+1] = rp[ii];
696:         PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));
697:       }
698:       if (N >= i) {
699:         PetscMemzero(ap+bs2*i,bs2*sizeof(MatScalar));
700:       }
701:       rp[i] = col;
702:       bap   = ap +  bs2*i;
703:       if (roworiented) {
704:         for (ii=0; ii<bs; ii++,value+=stepval) {
705:           for (jj=ii; jj<bs2; jj+=bs) {
706:             bap[jj] = *value++;
707:           }
708:         }
709:       } else {
710:         for (ii=0; ii<bs; ii++,value+=stepval) {
711:           for (jj=0; jj<bs; jj++) {
712:             *bap++ = *value++;
713:           }
714:         }
715:        }
716:     noinsert2:;
717:       low = i;
718:     }
719:     ailen[row] = nrow;
720:   }
721:   return(0);
722: }

724: /*
725:     This is not yet used
726: */
729: PetscErrorCode MatAssemblyEnd_SeqSBAIJ_SeqAIJ_Inode(Mat A)
730: {
731:   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
733:   const PetscInt *ai = a->i, *aj = a->j,*cols;
734:   PetscInt       i   = 0,j,blk_size,m = A->rmap->n,node_count = 0,nzx,nzy,*ns,row,nz,cnt,cnt2,*counts;
735:   PetscBool      flag;

738:   PetscMalloc1(m,&ns);
739:   while (i < m) {
740:     nzx = ai[i+1] - ai[i];       /* Number of nonzeros */
741:     /* Limits the number of elements in a node to 'a->inode.limit' */
742:     for (j=i+1,blk_size=1; j<m && blk_size <a->inode.limit; ++j,++blk_size) {
743:       nzy = ai[j+1] - ai[j];
744:       if (nzy != (nzx - j + i)) break;
745:       PetscMemcmp(aj + ai[i] + j - i,aj + ai[j],nzy*sizeof(PetscInt),&flag);
746:       if (!flag) break;
747:     }
748:     ns[node_count++] = blk_size;

750:     i = j;
751:   }
752:   if (!a->inode.size && m && node_count > .9*m) {
753:     PetscFree(ns);
754:     PetscInfo2(A,"Found %D nodes out of %D rows. Not using Inode routines\n",node_count,m);
755:   } else {
756:     a->inode.node_count = node_count;

758:     PetscMalloc1(node_count,&a->inode.size);
759:     PetscLogObjectMemory((PetscObject)A,node_count*sizeof(PetscInt));
760:     PetscMemcpy(a->inode.size,ns,node_count*sizeof(PetscInt));
761:     PetscFree(ns);
762:     PetscInfo3(A,"Found %D nodes of %D. Limit used: %D. Using Inode routines\n",node_count,m,a->inode.limit);

764:     /* count collections of adjacent columns in each inode */
765:     row = 0;
766:     cnt = 0;
767:     for (i=0; i<node_count; i++) {
768:       cols = aj + ai[row] + a->inode.size[i];
769:       nz   = ai[row+1] - ai[row] - a->inode.size[i];
770:       for (j=1; j<nz; j++) {
771:         if (cols[j] != cols[j-1]+1) cnt++;
772:       }
773:       cnt++;
774:       row += a->inode.size[i];
775:     }
776:     PetscMalloc1(2*cnt,&counts);
777:     cnt  = 0;
778:     row  = 0;
779:     for (i=0; i<node_count; i++) {
780:       cols = aj + ai[row] + a->inode.size[i];
781:       counts[2*cnt] = cols[0];
782:       nz   = ai[row+1] - ai[row] - a->inode.size[i];
783:       cnt2 = 1;
784:       for (j=1; j<nz; j++) {
785:         if (cols[j] != cols[j-1]+1) {
786:           counts[2*(cnt++)+1] = cnt2;
787:           counts[2*cnt]       = cols[j];
788:           cnt2 = 1;
789:         } else cnt2++;
790:       }
791:       counts[2*(cnt++)+1] = cnt2;
792:       row += a->inode.size[i];
793:     }
794:     PetscIntView(2*cnt,counts,0);
795:   }
796:   return(0);
797: }

801: PetscErrorCode MatAssemblyEnd_SeqSBAIJ(Mat A,MatAssemblyType mode)
802: {
803:   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
805:   PetscInt       fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
806:   PetscInt       m      = A->rmap->N,*ip,N,*ailen = a->ilen;
807:   PetscInt       mbs    = a->mbs,bs2 = a->bs2,rmax = 0;
808:   MatScalar      *aa    = a->a,*ap;

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

813:   if (m) rmax = ailen[0];
814:   for (i=1; i<mbs; i++) {
815:     /* move each row back by the amount of empty slots (fshift) before it*/
816:     fshift += imax[i-1] - ailen[i-1];
817:     rmax    = PetscMax(rmax,ailen[i]);
818:     if (fshift) {
819:       ip = aj + ai[i]; ap = aa + bs2*ai[i];
820:       N  = ailen[i];
821:       for (j=0; j<N; j++) {
822:         ip[j-fshift] = ip[j];
823:         PetscMemcpy(ap+(j-fshift)*bs2,ap+j*bs2,bs2*sizeof(MatScalar));
824:       }
825:     }
826:     ai[i] = ai[i-1] + ailen[i-1];
827:   }
828:   if (mbs) {
829:     fshift += imax[mbs-1] - ailen[mbs-1];
830:     ai[mbs] = ai[mbs-1] + ailen[mbs-1];
831:   }
832:   /* reset ilen and imax for each row */
833:   for (i=0; i<mbs; i++) {
834:     ailen[i] = imax[i] = ai[i+1] - ai[i];
835:   }
836:   a->nz = ai[mbs];

838:   /* diagonals may have moved, reset it */
839:   if (a->diag) {
840:     PetscMemcpy(a->diag,ai,mbs*sizeof(PetscInt));
841:   }
842:   if (fshift && a->nounused == -1) SETERRQ4(PETSC_COMM_SELF,PETSC_ERR_PLIB, "Unused space detected in matrix: %D X %D block size %D, %D unneeded", m, A->cmap->n, A->rmap->bs, fshift*bs2);

844:   PetscInfo5(A,"Matrix size: %D X %D, block size %D; storage space: %D unneeded, %D used\n",m,A->rmap->N,A->rmap->bs,fshift*bs2,a->nz*bs2);
845:   PetscInfo1(A,"Number of mallocs during MatSetValues is %D\n",a->reallocs);
846:   PetscInfo1(A,"Most nonzeros blocks in any row is %D\n",rmax);

848:   A->info.mallocs    += a->reallocs;
849:   a->reallocs         = 0;
850:   A->info.nz_unneeded = (PetscReal)fshift*bs2;
851:   a->idiagvalid       = PETSC_FALSE;
852:   a->rmax             = rmax;

854:   if (A->cmap->n < 65536 && A->cmap->bs == 1) {
855:     if (a->jshort && a->free_jshort) {
856:       /* when matrix data structure is changed, previous jshort must be replaced */
857:       PetscFree(a->jshort);
858:     }
859:     PetscMalloc1(a->i[A->rmap->n],&a->jshort);
860:     PetscLogObjectMemory((PetscObject)A,a->i[A->rmap->n]*sizeof(unsigned short));
861:     for (i=0; i<a->i[A->rmap->n]; i++) a->jshort[i] = a->j[i];
862:     A->ops->mult   = MatMult_SeqSBAIJ_1_ushort;
863:     A->ops->sor    = MatSOR_SeqSBAIJ_ushort;
864:     a->free_jshort = PETSC_TRUE;
865:   }
866:   return(0);
867: }

869: /*
870:    This function returns an array of flags which indicate the locations of contiguous
871:    blocks that should be zeroed. for eg: if bs = 3  and is = [0,1,2,3,5,6,7,8,9]
872:    then the resulting sizes = [3,1,1,3,1] correspondig to sets [(0,1,2),(3),(5),(6,7,8),(9)]
873:    Assume: sizes should be long enough to hold all the values.
874: */
877: PetscErrorCode MatZeroRows_SeqSBAIJ_Check_Blocks(PetscInt idx[],PetscInt n,PetscInt bs,PetscInt sizes[], PetscInt *bs_max)
878: {
879:   PetscInt  i,j,k,row;
880:   PetscBool flg;

883:   for (i=0,j=0; i<n; j++) {
884:     row = idx[i];
885:     if (row%bs!=0) { /* Not the begining of a block */
886:       sizes[j] = 1;
887:       i++;
888:     } else if (i+bs > n) { /* Beginning of a block, but complete block doesn't exist (at idx end) */
889:       sizes[j] = 1;         /* Also makes sure atleast 'bs' values exist for next else */
890:       i++;
891:     } else { /* Begining of the block, so check if the complete block exists */
892:       flg = PETSC_TRUE;
893:       for (k=1; k<bs; k++) {
894:         if (row+k != idx[i+k]) { /* break in the block */
895:           flg = PETSC_FALSE;
896:           break;
897:         }
898:       }
899:       if (flg) { /* No break in the bs */
900:         sizes[j] = bs;
901:         i       += bs;
902:       } else {
903:         sizes[j] = 1;
904:         i++;
905:       }
906:     }
907:   }
908:   *bs_max = j;
909:   return(0);
910: }


913: /* Only add/insert a(i,j) with i<=j (blocks).
914:    Any a(i,j) with i>j input by user is ingored.
915: */

919: PetscErrorCode MatSetValues_SeqSBAIJ(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
920: {
921:   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)A->data;
923:   PetscInt       *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N,lastcol = -1;
924:   PetscInt       *imax=a->imax,*ai=a->i,*ailen=a->ilen,roworiented=a->roworiented;
925:   PetscInt       *aj  =a->j,nonew=a->nonew,bs=A->rmap->bs,brow,bcol;
926:   PetscInt       ridx,cidx,bs2=a->bs2;
927:   MatScalar      *ap,value,*aa=a->a,*bap;

930:   for (k=0; k<m; k++) { /* loop over added rows */
931:     row  = im[k];       /* row number */
932:     brow = row/bs;      /* block row number */
933:     if (row < 0) continue;
934: #if defined(PETSC_USE_DEBUG)
935:     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);
936: #endif
937:     rp   = aj + ai[brow]; /*ptr to beginning of column value of the row block*/
938:     ap   = aa + bs2*ai[brow]; /*ptr to beginning of element value of the row block*/
939:     rmax = imax[brow];  /* maximum space allocated for this row */
940:     nrow = ailen[brow]; /* actual length of this row */
941:     low  = 0;

943:     for (l=0; l<n; l++) { /* loop over added columns */
944:       if (in[l] < 0) continue;
945: #if defined(PETSC_USE_DEBUG)
946:       if (in[l] >= A->rmap->N) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Column too large: col %D max %D",in[l],A->rmap->N-1);
947: #endif
948:       col  = in[l];
949:       bcol = col/bs;              /* block col number */

951:       if (brow > bcol) {
952:         if (a->ignore_ltriangular) continue; /* ignore lower triangular values */
953:         else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"Lower triangular value cannot be set for sbaij format. Ignoring these values, run with -mat_ignore_lower_triangular or call MatSetOption(mat,MAT_IGNORE_LOWER_TRIANGULAR,PETSC_TRUE)");
954:       }

956:       ridx = row % bs; cidx = col % bs; /*row and col index inside the block */
957:       if ((brow==bcol && ridx<=cidx) || (brow<bcol)) {
958:         /* element value a(k,l) */
959:         if (roworiented) value = v[l + k*n];
960:         else value = v[k + l*m];

962:         /* move pointer bap to a(k,l) quickly and add/insert value */
963:         if (col <= lastcol) low = 0;
964:         high = nrow;
965:         lastcol = col;
966:         while (high-low > 7) {
967:           t = (low+high)/2;
968:           if (rp[t] > bcol) high = t;
969:           else              low  = t;
970:         }
971:         for (i=low; i<high; i++) {
972:           if (rp[i] > bcol) break;
973:           if (rp[i] == bcol) {
974:             bap = ap +  bs2*i + bs*cidx + ridx;
975:             if (is == ADD_VALUES) *bap += value;
976:             else                  *bap  = value;
977:             /* for diag block, add/insert its symmetric element a(cidx,ridx) */
978:             if (brow == bcol && ridx < cidx) {
979:               bap = ap +  bs2*i + bs*ridx + cidx;
980:               if (is == ADD_VALUES) *bap += value;
981:               else                  *bap  = value;
982:             }
983:             goto noinsert1;
984:           }
985:         }

987:         if (nonew == 1) goto noinsert1;
988:         if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) in the matrix", row, col);
989:         MatSeqXAIJReallocateAIJ(A,a->mbs,bs2,nrow,brow,bcol,rmax,aa,ai,aj,rp,ap,imax,nonew,MatScalar);

991:         N = nrow++ - 1; high++;
992:         /* shift up all the later entries in this row */
993:         for (ii=N; ii>=i; ii--) {
994:           rp[ii+1] = rp[ii];
995:           PetscMemcpy(ap+bs2*(ii+1),ap+bs2*(ii),bs2*sizeof(MatScalar));
996:         }
997:         if (N>=i) {
998:           PetscMemzero(ap+bs2*i,bs2*sizeof(MatScalar));
999:         }
1000:         rp[i]                      = bcol;
1001:         ap[bs2*i + bs*cidx + ridx] = value;
1002:         A->nonzerostate++;
1003: noinsert1:;
1004:         low = i;
1005:       }
1006:     }   /* end of loop over added columns */
1007:     ailen[brow] = nrow;
1008:   }   /* end of loop over added rows */
1009:   return(0);
1010: }

1014: PetscErrorCode MatICCFactor_SeqSBAIJ(Mat inA,IS row,const MatFactorInfo *info)
1015: {
1016:   Mat_SeqSBAIJ   *a = (Mat_SeqSBAIJ*)inA->data;
1017:   Mat            outA;
1019:   PetscBool      row_identity;

1022:   if (info->levels != 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only levels=0 is supported for in-place icc");
1023:   ISIdentity(row,&row_identity);
1024:   if (!row_identity) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix reordering is not supported");
1025:   if (inA->rmap->bs != 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"Matrix block size %D is not supported",inA->rmap->bs); /* Need to replace MatCholeskyFactorSymbolic_SeqSBAIJ_MSR()! */

1027:   outA            = inA;
1028:   inA->factortype = MAT_FACTOR_ICC;

1030:   MatMarkDiagonal_SeqSBAIJ(inA);
1031:   MatSeqSBAIJSetNumericFactorization_inplace(inA,row_identity);

1033:   PetscObjectReference((PetscObject)row);
1034:   ISDestroy(&a->row);
1035:   a->row = row;
1036:   PetscObjectReference((PetscObject)row);
1037:   ISDestroy(&a->col);
1038:   a->col = row;

1040:   /* Create the invert permutation so that it can be used in MatCholeskyFactorNumeric() */
1041:   if (a->icol) {ISInvertPermutation(row,PETSC_DECIDE, &a->icol);}
1042:   PetscLogObjectParent((PetscObject)inA,(PetscObject)a->icol);

1044:   if (!a->solve_work) {
1045:     PetscMalloc1(inA->rmap->N+inA->rmap->bs,&a->solve_work);
1046:     PetscLogObjectMemory((PetscObject)inA,(inA->rmap->N+inA->rmap->bs)*sizeof(PetscScalar));
1047:   }

1049:   MatCholeskyFactorNumeric(outA,inA,info);
1050:   return(0);
1051: }

1055: PetscErrorCode  MatSeqSBAIJSetColumnIndices_SeqSBAIJ(Mat mat,PetscInt *indices)
1056: {
1057:   Mat_SeqSBAIJ   *baij = (Mat_SeqSBAIJ*)mat->data;
1058:   PetscInt       i,nz,n;

1062:   nz = baij->maxnz;
1063:   n  = mat->cmap->n;
1064:   for (i=0; i<nz; i++) baij->j[i] = indices[i];

1066:   baij->nz = nz;
1067:   for (i=0; i<n; i++) baij->ilen[i] = baij->imax[i];

1069:   MatSetOption(mat,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
1070:   return(0);
1071: }

1075: /*@
1076:   MatSeqSBAIJSetColumnIndices - Set the column indices for all the rows
1077:   in the matrix.

1079:   Input Parameters:
1080:   +  mat     - the SeqSBAIJ matrix
1081:   -  indices - the column indices

1083:   Level: advanced

1085:   Notes:
1086:   This can be called if you have precomputed the nonzero structure of the
1087:   matrix and want to provide it to the matrix object to improve the performance
1088:   of the MatSetValues() operation.

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

1093:   MUST be called before any calls to MatSetValues()

1095:   .seealso: MatCreateSeqSBAIJ
1096: @*/
1097: PetscErrorCode  MatSeqSBAIJSetColumnIndices(Mat mat,PetscInt *indices)
1098: {

1104:   PetscUseMethod(mat,"MatSeqSBAIJSetColumnIndices_C",(Mat,PetscInt*),(mat,indices));
1105:   return(0);
1106: }

1110: PetscErrorCode MatCopy_SeqSBAIJ(Mat A,Mat B,MatStructure str)
1111: {

1115:   /* If the two matrices have the same copy implementation, use fast copy. */
1116:   if (str == SAME_NONZERO_PATTERN && (A->ops->copy == B->ops->copy)) {
1117:     Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
1118:     Mat_SeqSBAIJ *b = (Mat_SeqSBAIJ*)B->data;

1120:     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");
1121:     PetscMemcpy(b->a,a->a,(a->i[A->rmap->N])*sizeof(PetscScalar));
1122:   } else {
1123:     MatGetRowUpperTriangular(A);
1124:     MatCopy_Basic(A,B,str);
1125:     MatRestoreRowUpperTriangular(A);
1126:   }
1127:   return(0);
1128: }

1132: PetscErrorCode MatSetUp_SeqSBAIJ(Mat A)
1133: {

1137:   MatSeqSBAIJSetPreallocation_SeqSBAIJ(A,A->rmap->bs,PETSC_DEFAULT,0);
1138:   return(0);
1139: }

1143: PetscErrorCode MatSeqSBAIJGetArray_SeqSBAIJ(Mat A,PetscScalar *array[])
1144: {
1145:   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;

1148:   *array = a->a;
1149:   return(0);
1150: }

1154: PetscErrorCode MatSeqSBAIJRestoreArray_SeqSBAIJ(Mat A,PetscScalar *array[])
1155: {
1157:   return(0);
1158: }

1162: PetscErrorCode MatAXPYGetPreallocation_SeqSBAIJ(Mat Y,Mat X,PetscInt *nnz)
1163: {
1164:   PetscInt       bs = Y->rmap->bs,mbs = Y->rmap->N/bs;
1165:   Mat_SeqSBAIJ   *x = (Mat_SeqSBAIJ*)X->data;
1166:   Mat_SeqSBAIJ   *y = (Mat_SeqSBAIJ*)Y->data;

1170:   /* Set the number of nonzeros in the new matrix */
1171:   MatAXPYGetPreallocation_SeqX_private(mbs,x->i,x->j,y->i,y->j,nnz);
1172:   return(0);
1173: }

1177: PetscErrorCode MatAXPY_SeqSBAIJ(Mat Y,PetscScalar a,Mat X,MatStructure str)
1178: {
1179:   Mat_SeqSBAIJ   *x=(Mat_SeqSBAIJ*)X->data, *y=(Mat_SeqSBAIJ*)Y->data;
1181:   PetscInt       bs=Y->rmap->bs,bs2=bs*bs;
1182:   PetscBLASInt   one = 1;

1185:   if (str == SAME_NONZERO_PATTERN) {
1186:     PetscScalar  alpha = a;
1187:     PetscBLASInt bnz;
1188:     PetscBLASIntCast(x->nz*bs2,&bnz);
1189:     PetscStackCallBLAS("BLASaxpy",BLASaxpy_(&bnz,&alpha,x->a,&one,y->a,&one));
1190:     PetscObjectStateIncrease((PetscObject)Y);
1191:   } else if (str == SUBSET_NONZERO_PATTERN) { /* nonzeros of X is a subset of Y's */
1192:     MatSetOption(X,MAT_GETROW_UPPERTRIANGULAR,PETSC_TRUE);
1193:     MatAXPY_Basic(Y,a,X,str);
1194:     MatSetOption(X,MAT_GETROW_UPPERTRIANGULAR,PETSC_FALSE);
1195:   } else {
1196:     Mat      B;
1197:     PetscInt *nnz;
1198:     if (bs != X->rmap->bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Matrices must have same block size");
1199:     MatGetRowUpperTriangular(X);
1200:     MatGetRowUpperTriangular(Y);
1201:     PetscMalloc1(Y->rmap->N,&nnz);
1202:     MatCreate(PetscObjectComm((PetscObject)Y),&B);
1203:     PetscObjectSetName((PetscObject)B,((PetscObject)Y)->name);
1204:     MatSetSizes(B,Y->rmap->n,Y->cmap->n,Y->rmap->N,Y->cmap->N);
1205:     MatSetBlockSizesFromMats(B,Y,Y);
1206:     MatSetType(B,(MatType) ((PetscObject)Y)->type_name);
1207:     MatAXPYGetPreallocation_SeqSBAIJ(Y,X,nnz);
1208:     MatSeqSBAIJSetPreallocation(B,bs,0,nnz);
1209: 
1210:     MatAXPY_BasicWithPreallocation(B,Y,a,X,str);
1211: 
1212:     MatHeaderReplace(Y,B);
1213:     PetscFree(nnz);
1214:     MatRestoreRowUpperTriangular(X);
1215:     MatRestoreRowUpperTriangular(Y);
1216:   }
1217:   return(0);
1218: }

1222: PetscErrorCode MatIsSymmetric_SeqSBAIJ(Mat A,PetscReal tol,PetscBool  *flg)
1223: {
1225:   *flg = PETSC_TRUE;
1226:   return(0);
1227: }

1231: PetscErrorCode MatIsStructurallySymmetric_SeqSBAIJ(Mat A,PetscBool  *flg)
1232: {
1234:   *flg = PETSC_TRUE;
1235:   return(0);
1236: }

1240: PetscErrorCode MatIsHermitian_SeqSBAIJ(Mat A,PetscReal tol,PetscBool  *flg)
1241: {
1243:   *flg = PETSC_FALSE;
1244:   return(0);
1245: }

1249: PetscErrorCode MatRealPart_SeqSBAIJ(Mat A)
1250: {
1251:   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
1252:   PetscInt     i,nz = a->bs2*a->i[a->mbs];
1253:   MatScalar    *aa = a->a;

1256:   for (i=0; i<nz; i++) aa[i] = PetscRealPart(aa[i]);
1257:   return(0);
1258: }

1262: PetscErrorCode MatImaginaryPart_SeqSBAIJ(Mat A)
1263: {
1264:   Mat_SeqSBAIJ *a = (Mat_SeqSBAIJ*)A->data;
1265:   PetscInt     i,nz = a->bs2*a->i[a->mbs];
1266:   MatScalar    *aa = a->a;

1269:   for (i=0; i<nz; i++) aa[i] = PetscImaginaryPart(aa[i]);
1270:   return(0);
1271: }

1275: PetscErrorCode MatZeroRowsColumns_SeqSBAIJ(Mat A,PetscInt is_n,const PetscInt is_idx[],PetscScalar diag,Vec x, Vec b)
1276: {
1277:   Mat_SeqSBAIJ      *baij=(Mat_SeqSBAIJ*)A->data;
1278:   PetscErrorCode    ierr;
1279:   PetscInt          i,j,k,count;
1280:   PetscInt          bs   =A->rmap->bs,bs2=baij->bs2,row,col;
1281:   PetscScalar       zero = 0.0;
1282:   MatScalar         *aa;
1283:   const PetscScalar *xx;
1284:   PetscScalar       *bb;
1285:   PetscBool         *zeroed,vecs = PETSC_FALSE;

1288:   /* fix right hand side if needed */
1289:   if (x && b) {
1290:     VecGetArrayRead(x,&xx);
1291:     VecGetArray(b,&bb);
1292:     vecs = PETSC_TRUE;
1293:   }

1295:   /* zero the columns */
1296:   PetscCalloc1(A->rmap->n,&zeroed);
1297:   for (i=0; i<is_n; i++) {
1298:     if (is_idx[i] < 0 || is_idx[i] >= A->rmap->N) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"row %D out of range",is_idx[i]);
1299:     zeroed[is_idx[i]] = PETSC_TRUE;
1300:   }
1301:   if (vecs) {
1302:     for (i=0; i<A->rmap->N; i++) {
1303:       row = i/bs;
1304:       for (j=baij->i[row]; j<baij->i[row+1]; j++) {
1305:         for (k=0; k<bs; k++) {
1306:           col = bs*baij->j[j] + k;
1307:           if (col <= i) continue;
1308:           aa = ((MatScalar*)(baij->a)) + j*bs2 + (i%bs) + bs*k;
1309:           if (!zeroed[i] && zeroed[col]) bb[i]   -= aa[0]*xx[col];
1310:           if (zeroed[i] && !zeroed[col]) bb[col] -= aa[0]*xx[i];
1311:         }
1312:       }
1313:     }
1314:     for (i=0; i<is_n; i++) bb[is_idx[i]] = diag*xx[is_idx[i]];
1315:   }

1317:   for (i=0; i<A->rmap->N; i++) {
1318:     if (!zeroed[i]) {
1319:       row = i/bs;
1320:       for (j=baij->i[row]; j<baij->i[row+1]; j++) {
1321:         for (k=0; k<bs; k++) {
1322:           col = bs*baij->j[j] + k;
1323:           if (zeroed[col]) {
1324:             aa = ((MatScalar*)(baij->a)) + j*bs2 + (i%bs) + bs*k;
1325:             aa[0] = 0.0;
1326:           }
1327:         }
1328:       }
1329:     }
1330:   }
1331:   PetscFree(zeroed);
1332:   if (vecs) {
1333:     VecRestoreArrayRead(x,&xx);
1334:     VecRestoreArray(b,&bb);
1335:   }

1337:   /* zero the rows */
1338:   for (i=0; i<is_n; i++) {
1339:     row   = is_idx[i];
1340:     count = (baij->i[row/bs +1] - baij->i[row/bs])*bs;
1341:     aa    = ((MatScalar*)(baij->a)) + baij->i[row/bs]*bs2 + (row%bs);
1342:     for (k=0; k<count; k++) {
1343:       aa[0] =  zero;
1344:       aa   += bs;
1345:     }
1346:     if (diag != 0.0) {
1347:       (*A->ops->setvalues)(A,1,&row,1,&row,&diag,INSERT_VALUES);
1348:     }
1349:   }
1350:   MatAssemblyEnd_SeqSBAIJ(A,MAT_FINAL_ASSEMBLY);
1351:   return(0);
1352: }

1354: /* -------------------------------------------------------------------*/
1355: static struct _MatOps MatOps_Values = {MatSetValues_SeqSBAIJ,
1356:                                        MatGetRow_SeqSBAIJ,
1357:                                        MatRestoreRow_SeqSBAIJ,
1358:                                        MatMult_SeqSBAIJ_N,
1359:                                /*  4*/ MatMultAdd_SeqSBAIJ_N,
1360:                                        MatMult_SeqSBAIJ_N,       /* transpose versions are same as non-transpose versions */
1361:                                        MatMultAdd_SeqSBAIJ_N,
1362:                                        0,
1363:                                        0,
1364:                                        0,
1365:                                /* 10*/ 0,
1366:                                        0,
1367:                                        MatCholeskyFactor_SeqSBAIJ,
1368:                                        MatSOR_SeqSBAIJ,
1369:                                        MatTranspose_SeqSBAIJ,
1370:                                /* 15*/ MatGetInfo_SeqSBAIJ,
1371:                                        MatEqual_SeqSBAIJ,
1372:                                        MatGetDiagonal_SeqSBAIJ,
1373:                                        MatDiagonalScale_SeqSBAIJ,
1374:                                        MatNorm_SeqSBAIJ,
1375:                                /* 20*/ 0,
1376:                                        MatAssemblyEnd_SeqSBAIJ,
1377:                                        MatSetOption_SeqSBAIJ,
1378:                                        MatZeroEntries_SeqSBAIJ,
1379:                                /* 24*/ 0,
1380:                                        0,
1381:                                        0,
1382:                                        0,
1383:                                        0,
1384:                                /* 29*/ MatSetUp_SeqSBAIJ,
1385:                                        0,
1386:                                        0,
1387:                                        0,
1388:                                        0,
1389:                                /* 34*/ MatDuplicate_SeqSBAIJ,
1390:                                        0,
1391:                                        0,
1392:                                        0,
1393:                                        MatICCFactor_SeqSBAIJ,
1394:                                /* 39*/ MatAXPY_SeqSBAIJ,
1395:                                        MatGetSubMatrices_SeqSBAIJ,
1396:                                        MatIncreaseOverlap_SeqSBAIJ,
1397:                                        MatGetValues_SeqSBAIJ,
1398:                                        MatCopy_SeqSBAIJ,
1399:                                /* 44*/ 0,
1400:                                        MatScale_SeqSBAIJ,
1401:                                        0,
1402:                                        0,
1403:                                        MatZeroRowsColumns_SeqSBAIJ,
1404:                                /* 49*/ 0,
1405:                                        MatGetRowIJ_SeqSBAIJ,
1406:                                        MatRestoreRowIJ_SeqSBAIJ,
1407:                                        0,
1408:                                        0,
1409:                                /* 54*/ 0,
1410:                                        0,
1411:                                        0,
1412:                                        0,
1413:                                        MatSetValuesBlocked_SeqSBAIJ,
1414:                                /* 59*/ MatGetSubMatrix_SeqSBAIJ,
1415:                                        0,
1416:                                        0,
1417:                                        0,
1418:                                        0,
1419:                                /* 64*/ 0,
1420:                                        0,
1421:                                        0,
1422:                                        0,
1423:                                        0,
1424:                                /* 69*/ MatGetRowMaxAbs_SeqSBAIJ,
1425:                                        0,
1426:                                        0,
1427:                                        0,
1428:                                        0,
1429:                                /* 74*/ 0,
1430:                                        0,
1431:                                        0,
1432:                                        0,
1433:                                        0,
1434:                                /* 79*/ 0,
1435:                                        0,
1436:                                        0,
1437:                                        MatGetInertia_SeqSBAIJ,
1438:                                        MatLoad_SeqSBAIJ,
1439:                                /* 84*/ MatIsSymmetric_SeqSBAIJ,
1440:                                        MatIsHermitian_SeqSBAIJ,
1441:                                        MatIsStructurallySymmetric_SeqSBAIJ,
1442:                                        0,
1443:                                        0,
1444:                                /* 89*/ 0,
1445:                                        0,
1446:                                        0,
1447:                                        0,
1448:                                        0,
1449:                                /* 94*/ 0,
1450:                                        0,
1451:                                        0,
1452:                                        0,
1453:                                        0,
1454:                                /* 99*/ 0,
1455:                                        0,
1456:                                        0,
1457:                                        0,
1458:                                        0,
1459:                                /*104*/ 0,
1460:                                        MatRealPart_SeqSBAIJ,
1461:                                        MatImaginaryPart_SeqSBAIJ,
1462:                                        MatGetRowUpperTriangular_SeqSBAIJ,
1463:                                        MatRestoreRowUpperTriangular_SeqSBAIJ,
1464:                                /*109*/ 0,
1465:                                        0,
1466:                                        0,
1467:                                        0,
1468:                                        MatMissingDiagonal_SeqSBAIJ,
1469:                                /*114*/ 0,
1470:                                        0,
1471:                                        0,
1472:                                        0,
1473:                                        0,
1474:                                /*119*/ 0,
1475:                                        0,
1476:                                        0,
1477:                                        0,
1478:                                        0,
1479:                                /*124*/ 0,
1480:                                        0,
1481:                                        0,
1482:                                        0,
1483:                                        0,
1484:                                /*129*/ 0,
1485:                                        0,
1486:                                        0,
1487:                                        0,
1488:                                        0,
1489:                                /*134*/ 0,
1490:                                        0,
1491:                                        0,
1492:                                        0,
1493:                                        0,
1494:                                /*139*/ 0,
1495:                                        0,
1496:                                        0,
1497:                                        0,
1498:                                        0,
1499:                                 /*144*/MatCreateMPIMatConcatenateSeqMat_SeqSBAIJ
1500: };

1504: PetscErrorCode  MatStoreValues_SeqSBAIJ(Mat mat)
1505: {
1506:   Mat_SeqSBAIJ   *aij = (Mat_SeqSBAIJ*)mat->data;
1507:   PetscInt       nz   = aij->i[mat->rmap->N]*mat->rmap->bs*aij->bs2;

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

1513:   /* allocate space for values if not already there */
1514:   if (!aij->saved_values) {
1515:     PetscMalloc1(nz+1,&aij->saved_values);
1516:   }

1518:   /* copy values over */
1519:   PetscMemcpy(aij->saved_values,aij->a,nz*sizeof(PetscScalar));
1520:   return(0);
1521: }

1525: PetscErrorCode  MatRetrieveValues_SeqSBAIJ(Mat mat)
1526: {
1527:   Mat_SeqSBAIJ   *aij = (Mat_SeqSBAIJ*)mat->data;
1529:   PetscInt       nz = aij->i[mat->rmap->N]*mat->rmap->bs*aij->bs2;

1532:   if (aij->nonew != 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE);first");
1533:   if (!aij->saved_values) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ORDER,"Must call MatStoreValues(A);first");

1535:   /* copy values over */
1536:   PetscMemcpy(aij->a,aij->saved_values,nz*sizeof(PetscScalar));
1537:   return(0);
1538: }

1542: PetscErrorCode  MatSeqSBAIJSetPreallocation_SeqSBAIJ(Mat B,PetscInt bs,PetscInt nz,PetscInt *nnz)
1543: {
1544:   Mat_SeqSBAIJ   *b = (Mat_SeqSBAIJ*)B->data;
1546:   PetscInt       i,mbs,nbs,bs2;
1547:   PetscBool      skipallocation = PETSC_FALSE,flg = PETSC_FALSE,realalloc = PETSC_FALSE;

1550:   if (nz >= 0 || nnz) realalloc = PETSC_TRUE;
1551:   B->preallocated = PETSC_TRUE;

1553:   MatSetBlockSize(B,PetscAbs(bs));
1554:   PetscLayoutSetUp(B->rmap);
1555:   PetscLayoutSetUp(B->cmap);
1556:   PetscLayoutGetBlockSize(B->rmap,&bs);

1558:   mbs = B->rmap->N/bs;
1559:   nbs = B->cmap->n/bs;
1560:   bs2 = bs*bs;

1562:   if (mbs*bs != B->rmap->N || nbs*bs!=B->cmap->n) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Number rows, cols must be divisible by blocksize");

1564:   if (nz == MAT_SKIP_ALLOCATION) {
1565:     skipallocation = PETSC_TRUE;
1566:     nz             = 0;
1567:   }

1569:   if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 3;
1570:   if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %D",nz);
1571:   if (nnz) {
1572:     for (i=0; i<mbs; i++) {
1573:       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]);
1574:       if (nnz[i] > nbs) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be greater than block row length: local row %D value %D block rowlength %D",i,nnz[i],nbs);
1575:     }
1576:   }

1578:   B->ops->mult             = MatMult_SeqSBAIJ_N;
1579:   B->ops->multadd          = MatMultAdd_SeqSBAIJ_N;
1580:   B->ops->multtranspose    = MatMult_SeqSBAIJ_N;
1581:   B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_N;

1583:   PetscOptionsGetBool(((PetscObject)B)->prefix,"-mat_no_unroll",&flg,NULL);
1584:   if (!flg) {
1585:     switch (bs) {
1586:     case 1:
1587:       B->ops->mult             = MatMult_SeqSBAIJ_1;
1588:       B->ops->multadd          = MatMultAdd_SeqSBAIJ_1;
1589:       B->ops->multtranspose    = MatMult_SeqSBAIJ_1;
1590:       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_1;
1591:       break;
1592:     case 2:
1593:       B->ops->mult             = MatMult_SeqSBAIJ_2;
1594:       B->ops->multadd          = MatMultAdd_SeqSBAIJ_2;
1595:       B->ops->multtranspose    = MatMult_SeqSBAIJ_2;
1596:       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_2;
1597:       break;
1598:     case 3:
1599:       B->ops->mult             = MatMult_SeqSBAIJ_3;
1600:       B->ops->multadd          = MatMultAdd_SeqSBAIJ_3;
1601:       B->ops->multtranspose    = MatMult_SeqSBAIJ_3;
1602:       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_3;
1603:       break;
1604:     case 4:
1605:       B->ops->mult             = MatMult_SeqSBAIJ_4;
1606:       B->ops->multadd          = MatMultAdd_SeqSBAIJ_4;
1607:       B->ops->multtranspose    = MatMult_SeqSBAIJ_4;
1608:       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_4;
1609:       break;
1610:     case 5:
1611:       B->ops->mult             = MatMult_SeqSBAIJ_5;
1612:       B->ops->multadd          = MatMultAdd_SeqSBAIJ_5;
1613:       B->ops->multtranspose    = MatMult_SeqSBAIJ_5;
1614:       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_5;
1615:       break;
1616:     case 6:
1617:       B->ops->mult             = MatMult_SeqSBAIJ_6;
1618:       B->ops->multadd          = MatMultAdd_SeqSBAIJ_6;
1619:       B->ops->multtranspose    = MatMult_SeqSBAIJ_6;
1620:       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_6;
1621:       break;
1622:     case 7:
1623:       B->ops->mult             = MatMult_SeqSBAIJ_7;
1624:       B->ops->multadd          = MatMultAdd_SeqSBAIJ_7;
1625:       B->ops->multtranspose    = MatMult_SeqSBAIJ_7;
1626:       B->ops->multtransposeadd = MatMultAdd_SeqSBAIJ_7;
1627:       break;
1628:     }
1629:   }

1631:   b->mbs = mbs;
1632:   b->nbs = nbs;
1633:   if (!skipallocation) {
1634:     if (!b->imax) {
1635:       PetscMalloc2(mbs,&b->imax,mbs,&b->ilen);

1637:       b->free_imax_ilen = PETSC_TRUE;

1639:       PetscLogObjectMemory((PetscObject)B,2*mbs*sizeof(PetscInt));
1640:     }
1641:     if (!nnz) {
1642:       if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
1643:       else if (nz <= 0) nz = 1;
1644:       for (i=0; i<mbs; i++) b->imax[i] = nz;
1645:       nz = nz*mbs; /* total nz */
1646:     } else {
1647:       nz = 0;
1648:       for (i=0; i<mbs; i++) {b->imax[i] = nnz[i]; nz += nnz[i];}
1649:     }
1650:     /* b->ilen will count nonzeros in each block row so far. */
1651:     for (i=0; i<mbs; i++) b->ilen[i] = 0;
1652:     /* nz=(nz+mbs)/2; */ /* total diagonal and superdiagonal nonzero blocks */

1654:     /* allocate the matrix space */
1655:     MatSeqXAIJFreeAIJ(B,&b->a,&b->j,&b->i);
1656:     PetscMalloc3(bs2*nz,&b->a,nz,&b->j,B->rmap->N+1,&b->i);
1657:     PetscLogObjectMemory((PetscObject)B,(B->rmap->N+1)*sizeof(PetscInt)+nz*(bs2*sizeof(PetscScalar)+sizeof(PetscInt)));
1658:     PetscMemzero(b->a,nz*bs2*sizeof(MatScalar));
1659:     PetscMemzero(b->j,nz*sizeof(PetscInt));

1661:     b->singlemalloc = PETSC_TRUE;

1663:     /* pointer to beginning of each row */
1664:     b->i[0] = 0;
1665:     for (i=1; i<mbs+1; i++) b->i[i] = b->i[i-1] + b->imax[i-1];

1667:     b->free_a  = PETSC_TRUE;
1668:     b->free_ij = PETSC_TRUE;
1669:   } else {
1670:     b->free_a  = PETSC_FALSE;
1671:     b->free_ij = PETSC_FALSE;
1672:   }

1674:   B->rmap->bs = bs;
1675:   b->bs2      = bs2;
1676:   b->nz       = 0;
1677:   b->maxnz    = nz;

1679:   b->inew    = 0;
1680:   b->jnew    = 0;
1681:   b->anew    = 0;
1682:   b->a2anew  = 0;
1683:   b->permute = PETSC_FALSE;
1684:   if (realalloc) {MatSetOption(B,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);}
1685:   return(0);
1686: }

1690: PetscErrorCode MatSeqSBAIJSetPreallocationCSR_SeqSBAIJ(Mat B,PetscInt bs,const PetscInt ii[],const PetscInt jj[], const PetscScalar V[])
1691: {
1692:   PetscInt       i,j,m,nz,nz_max=0,*nnz;
1693:   PetscScalar    *values=0;
1694:   PetscBool      roworiented = ((Mat_SeqSBAIJ*)B->data)->roworiented;
1697:   if (bs < 1) SETERRQ1(PetscObjectComm((PetscObject)B),PETSC_ERR_ARG_OUTOFRANGE,"Invalid block size specified, must be positive but it is %D",bs);
1698:   PetscLayoutSetBlockSize(B->rmap,bs);
1699:   PetscLayoutSetBlockSize(B->cmap,bs);
1700:   PetscLayoutSetUp(B->rmap);
1701:   PetscLayoutSetUp(B->cmap);
1702:   PetscLayoutGetBlockSize(B->rmap,&bs);
1703:   m      = B->rmap->n/bs;

1705:   if (ii[0]) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"ii[0] must be 0 but it is %D",ii[0]);
1706:   PetscMalloc1(m+1,&nnz);
1707:   for (i=0; i<m; i++) {
1708:     nz = ii[i+1] - ii[i];
1709:     if (nz < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Row %D has a negative number of columns %D",i,nz);
1710:     nz_max = PetscMax(nz_max,nz);
1711:     nnz[i] = nz;
1712:   }
1713:   MatSeqSBAIJSetPreallocation(B,bs,0,nnz);
1714:   PetscFree(nnz);

1716:   values = (PetscScalar*)V;
1717:   if (!values) {
1718:     PetscCalloc1(bs*bs*nz_max,&values);
1719:   }
1720:   for (i=0; i<m; i++) {
1721:     PetscInt          ncols  = ii[i+1] - ii[i];
1722:     const PetscInt    *icols = jj + ii[i];
1723:     if (!roworiented || bs == 1) {
1724:       const PetscScalar *svals = values + (V ? (bs*bs*ii[i]) : 0);
1725:       MatSetValuesBlocked_SeqSBAIJ(B,1,&i,ncols,icols,svals,INSERT_VALUES);
1726:     } else {
1727:       for (j=0; j<ncols; j++) {
1728:         const PetscScalar *svals = values + (V ? (bs*bs*(ii[i]+j)) : 0);
1729:         MatSetValuesBlocked_SeqSBAIJ(B,1,&i,1,&icols[j],svals,INSERT_VALUES);
1730:       }
1731:     }
1732:   }
1733:   if (!V) { PetscFree(values); }
1734:   MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);
1735:   MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);
1736:   MatSetOption(B,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
1737:   return(0);
1738: }

1740: /*
1741:    This is used to set the numeric factorization for both Cholesky and ICC symbolic factorization
1742: */
1745: PetscErrorCode MatSeqSBAIJSetNumericFactorization_inplace(Mat B,PetscBool natural)
1746: {
1748:   PetscBool      flg = PETSC_FALSE;
1749:   PetscInt       bs  = B->rmap->bs;

1752:   PetscOptionsGetBool(((PetscObject)B)->prefix,"-mat_no_unroll",&flg,NULL);
1753:   if (flg) bs = 8;

1755:   if (!natural) {
1756:     switch (bs) {
1757:     case 1:
1758:       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_1_inplace;
1759:       break;
1760:     case 2:
1761:       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_2;
1762:       break;
1763:     case 3:
1764:       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_3;
1765:       break;
1766:     case 4:
1767:       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_4;
1768:       break;
1769:     case 5:
1770:       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_5;
1771:       break;
1772:     case 6:
1773:       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_6;
1774:       break;
1775:     case 7:
1776:       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_7;
1777:       break;
1778:     default:
1779:       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_N;
1780:       break;
1781:     }
1782:   } else {
1783:     switch (bs) {
1784:     case 1:
1785:       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_1_NaturalOrdering_inplace;
1786:       break;
1787:     case 2:
1788:       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_2_NaturalOrdering;
1789:       break;
1790:     case 3:
1791:       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_3_NaturalOrdering;
1792:       break;
1793:     case 4:
1794:       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_4_NaturalOrdering;
1795:       break;
1796:     case 5:
1797:       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_5_NaturalOrdering;
1798:       break;
1799:     case 6:
1800:       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_6_NaturalOrdering;
1801:       break;
1802:     case 7:
1803:       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_7_NaturalOrdering;
1804:       break;
1805:     default:
1806:       B->ops->choleskyfactornumeric = MatCholeskyFactorNumeric_SeqSBAIJ_N_NaturalOrdering;
1807:       break;
1808:     }
1809:   }
1810:   return(0);
1811: }

1813: PETSC_EXTERN PetscErrorCode MatConvert_SeqSBAIJ_SeqAIJ(Mat, MatType,MatReuse,Mat*);
1814: PETSC_EXTERN PetscErrorCode MatConvert_SeqSBAIJ_SeqBAIJ(Mat, MatType,MatReuse,Mat*);

1818: PETSC_EXTERN PetscErrorCode MatGetFactor_seqsbaij_petsc(Mat A,MatFactorType ftype,Mat *B)
1819: {
1820:   PetscInt       n = A->rmap->n;

1824: #if defined(PETSC_USE_COMPLEX)
1825:   if (A->hermitian) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Hermitian Factor is not supported");
1826: #endif
1827:   MatCreate(PetscObjectComm((PetscObject)A),B);
1828:   MatSetSizes(*B,n,n,n,n);
1829:   if (ftype == MAT_FACTOR_CHOLESKY || ftype == MAT_FACTOR_ICC) {
1830:     MatSetType(*B,MATSEQSBAIJ);
1831:     MatSeqSBAIJSetPreallocation(*B,A->rmap->bs,MAT_SKIP_ALLOCATION,NULL);

1833:     (*B)->ops->choleskyfactorsymbolic = MatCholeskyFactorSymbolic_SeqSBAIJ;
1834:     (*B)->ops->iccfactorsymbolic      = MatICCFactorSymbolic_SeqSBAIJ;
1835:   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Factor type not supported");
1836:   (*B)->factortype = ftype;
1837:   return(0);
1838: }

1840: /*MC
1841:   MATSEQSBAIJ - MATSEQSBAIJ = "seqsbaij" - A matrix type to be used for sequential symmetric block sparse matrices,
1842:   based on block compressed sparse row format.  Only the upper triangular portion of the matrix is stored.

1844:   For complex numbers by default this matrix is symmetric, NOT Hermitian symmetric. To make it Hermitian symmetric you
1845:   can call MatSetOption(Mat, MAT_HERMITIAN); after MatAssemblyEnd()

1847:   Options Database Keys:
1848:   . -mat_type seqsbaij - sets the matrix type to "seqsbaij" during a call to MatSetFromOptions()

1850:   Notes: By default if you insert values into the lower triangular part of the matrix they are simply ignored (since they are not
1851:      stored and it is assumed they symmetric to the upper triangular). If you call MatSetOption(Mat,MAT_IGNORE_LOWER_TRIANGULAR,PETSC_FALSE) or use
1852:      the options database -mat_ignore_lower_triangular false it will generate an error if you try to set a value in the lower triangular portion.


1855:   Level: beginner

1857:   .seealso: MatCreateSeqSBAIJ
1858: M*/

1860: PETSC_EXTERN PetscErrorCode MatConvert_SeqSBAIJ_SeqSBSTRM(Mat, MatType,MatReuse,Mat*);

1864: PETSC_EXTERN PetscErrorCode MatCreate_SeqSBAIJ(Mat B)
1865: {
1866:   Mat_SeqSBAIJ   *b;
1868:   PetscMPIInt    size;
1869:   PetscBool      no_unroll = PETSC_FALSE,no_inode = PETSC_FALSE;

1872:   MPI_Comm_size(PetscObjectComm((PetscObject)B),&size);
1873:   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Comm must be of size 1");

1875:   PetscNewLog(B,&b);
1876:   B->data = (void*)b;
1877:   PetscMemcpy(B->ops,&MatOps_Values,sizeof(struct _MatOps));

1879:   B->ops->destroy    = MatDestroy_SeqSBAIJ;
1880:   B->ops->view       = MatView_SeqSBAIJ;
1881:   b->row             = 0;
1882:   b->icol            = 0;
1883:   b->reallocs        = 0;
1884:   b->saved_values    = 0;
1885:   b->inode.limit     = 5;
1886:   b->inode.max_limit = 5;

1888:   b->roworiented        = PETSC_TRUE;
1889:   b->nonew              = 0;
1890:   b->diag               = 0;
1891:   b->solve_work         = 0;
1892:   b->mult_work          = 0;
1893:   B->spptr              = 0;
1894:   B->info.nz_unneeded   = (PetscReal)b->maxnz*b->bs2;
1895:   b->keepnonzeropattern = PETSC_FALSE;

1897:   b->inew    = 0;
1898:   b->jnew    = 0;
1899:   b->anew    = 0;
1900:   b->a2anew  = 0;
1901:   b->permute = PETSC_FALSE;

1903:   b->ignore_ltriangular = PETSC_TRUE;

1905:   PetscOptionsGetBool(((PetscObject)B)->prefix,"-mat_ignore_lower_triangular",&b->ignore_ltriangular,NULL);

1907:   b->getrow_utriangular = PETSC_FALSE;

1909:   PetscOptionsGetBool(((PetscObject)B)->prefix,"-mat_getrow_uppertriangular",&b->getrow_utriangular,NULL);

1911:   PetscObjectComposeFunction((PetscObject)B,"MatStoreValues_C",MatStoreValues_SeqSBAIJ);
1912:   PetscObjectComposeFunction((PetscObject)B,"MatRetrieveValues_C",MatRetrieveValues_SeqSBAIJ);
1913:   PetscObjectComposeFunction((PetscObject)B,"MatSeqSBAIJSetColumnIndices_C",MatSeqSBAIJSetColumnIndices_SeqSBAIJ);
1914:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqsbaij_seqaij_C",MatConvert_SeqSBAIJ_SeqAIJ);
1915:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqsbaij_seqbaij_C",MatConvert_SeqSBAIJ_SeqBAIJ);
1916:   PetscObjectComposeFunction((PetscObject)B,"MatSeqSBAIJSetPreallocation_C",MatSeqSBAIJSetPreallocation_SeqSBAIJ);
1917:   PetscObjectComposeFunction((PetscObject)B,"MatSeqSBAIJSetPreallocationCSR_C",MatSeqSBAIJSetPreallocationCSR_SeqSBAIJ);
1918:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqsbaij_seqsbstrm_C",MatConvert_SeqSBAIJ_SeqSBSTRM);
1919: #if defined(PETSC_HAVE_ELEMENTAL)
1920:   PetscObjectComposeFunction((PetscObject)B,"MatConvert_seqsbaij_elemental_C",MatConvert_SeqSBAIJ_Elemental);
1921: #endif

1923:   B->symmetric                  = PETSC_TRUE;
1924:   B->structurally_symmetric     = PETSC_TRUE;
1925:   B->symmetric_set              = PETSC_TRUE;
1926:   B->structurally_symmetric_set = PETSC_TRUE;

1928:   PetscObjectChangeTypeName((PetscObject)B,MATSEQSBAIJ);

1930:   PetscOptionsBegin(PetscObjectComm((PetscObject)B),((PetscObject)B)->prefix,"Options for SEQSBAIJ matrix","Mat");
1931:   PetscOptionsBool("-mat_no_unroll","Do not optimize for inodes (slower)",NULL,no_unroll,&no_unroll,NULL);
1932:   if (no_unroll) {
1933:     PetscInfo(B,"Not using Inode routines due to -mat_no_unroll\n");
1934:   }
1935:   PetscOptionsBool("-mat_no_inode","Do not optimize for inodes (slower)",NULL,no_inode,&no_inode,NULL);
1936:   if (no_inode) {
1937:     PetscInfo(B,"Not using Inode routines due to -mat_no_inode\n");
1938:   }
1939:   PetscOptionsInt("-mat_inode_limit","Do not use inodes larger then this value",NULL,b->inode.limit,&b->inode.limit,NULL);
1940:   PetscOptionsEnd();
1941:   b->inode.use = (PetscBool)(!(no_unroll || no_inode));
1942:   if (b->inode.limit > b->inode.max_limit) b->inode.limit = b->inode.max_limit;
1943:   return(0);
1944: }

1948: /*@C
1949:    MatSeqSBAIJSetPreallocation - Creates a sparse symmetric matrix in block AIJ (block
1950:    compressed row) format.  For good matrix assembly performance the
1951:    user should preallocate the matrix storage by setting the parameter nz
1952:    (or the array nnz).  By setting these parameters accurately, performance
1953:    during matrix assembly can be increased by more than a factor of 50.

1955:    Collective on Mat

1957:    Input Parameters:
1958: +  B - the symmetric matrix
1959: .  bs - size of block, the blocks are ALWAYS square. One can use MatSetBlockSizes() to set a different row and column blocksize but the row
1960:           blocksize always defines the size of the blocks. The column blocksize sets the blocksize of the vectors obtained with MatCreateVecs()
1961: .  nz - number of block nonzeros per block row (same for all rows)
1962: -  nnz - array containing the number of block nonzeros in the upper triangular plus
1963:          diagonal portion of each block (possibly different for each block row) or NULL

1965:    Options Database Keys:
1966: .   -mat_no_unroll - uses code that does not unroll the loops in the
1967:                      block calculations (much slower)
1968: .    -mat_block_size - size of the blocks to use (only works if a negative bs is passed in

1970:    Level: intermediate

1972:    Notes:
1973:    Specify the preallocated storage with either nz or nnz (not both).
1974:    Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
1975:    allocation.  See Users-Manual: ch_mat for details.

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

1982:    If the nnz parameter is given then the nz parameter is ignored


1985: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateSBAIJ()
1986: @*/
1987: PetscErrorCode  MatSeqSBAIJSetPreallocation(Mat B,PetscInt bs,PetscInt nz,const PetscInt nnz[])
1988: {

1995:   PetscTryMethod(B,"MatSeqSBAIJSetPreallocation_C",(Mat,PetscInt,PetscInt,const PetscInt[]),(B,bs,nz,nnz));
1996:   return(0);
1997: }

1999: #undef  __FUNCT__
2001: /*@C
2002:    MatSeqSBAIJSetPreallocationCSR - Allocates memory for a sparse sequential matrix in symmetric block AIJ format.

2004:    Input Parameters:
2005: +  B - the matrix
2006: .  i - the indices into j for the start of each local row (starts with zero)
2007: .  j - the column indices for each local row (starts with zero) these must be sorted for each row
2008: -  v - optional values in the matrix

2010:    Level: developer

2012:    Notes:
2013:    The order of the entries in values is specified by the MatOption MAT_ROW_ORIENTED.  For example, C programs
2014:    may want to use the default MAT_ROW_ORIENTED=PETSC_TRUE and use an array v[nnz][bs][bs] where the second index is
2015:    over rows within a block and the last index is over columns within a block row.  Fortran programs will likely set
2016:    MAT_ROW_ORIENTED=PETSC_FALSE and use a Fortran array v(bs,bs,nnz) in which the first index is over rows within a
2017:    block column and the second index is over columns within a block.

2019: .keywords: matrix, block, aij, compressed row, sparse

2021: .seealso: MatCreate(), MatCreateSeqSBAIJ(), MatSetValuesBlocked(), MatSeqSBAIJSetPreallocation(), MATSEQSBAIJ
2022: @*/
2023: PetscErrorCode MatSeqSBAIJSetPreallocationCSR(Mat B,PetscInt bs,const PetscInt i[],const PetscInt j[], const PetscScalar v[])
2024: {

2031:   PetscTryMethod(B,"MatSeqSBAIJSetPreallocationCSR_C",(Mat,PetscInt,const PetscInt[],const PetscInt[],const PetscScalar[]),(B,bs,i,j,v));
2032:   return(0);
2033: }

2037: /*@C
2038:    MatCreateSeqSBAIJ - Creates a sparse symmetric matrix in block AIJ (block
2039:    compressed row) format.  For good matrix assembly performance the
2040:    user should preallocate the matrix storage by setting the parameter nz
2041:    (or the array nnz).  By setting these parameters accurately, performance
2042:    during matrix assembly can be increased by more than a factor of 50.

2044:    Collective on MPI_Comm

2046:    Input Parameters:
2047: +  comm - MPI communicator, set to PETSC_COMM_SELF
2048: .  bs - size of block, the blocks are ALWAYS square. One can use MatSetBlockSizes() to set a different row and column blocksize but the row
2049:           blocksize always defines the size of the blocks. The column blocksize sets the blocksize of the vectors obtained with MatCreateVecs()
2050: .  m - number of rows, or number of columns
2051: .  nz - number of block nonzeros per block row (same for all rows)
2052: -  nnz - array containing the number of block nonzeros in the upper triangular plus
2053:          diagonal portion of each block (possibly different for each block row) or NULL

2055:    Output Parameter:
2056: .  A - the symmetric matrix

2058:    Options Database Keys:
2059: .   -mat_no_unroll - uses code that does not unroll the loops in the
2060:                      block calculations (much slower)
2061: .    -mat_block_size - size of the blocks to use

2063:    Level: intermediate

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

2069:    Notes:
2070:    The number of rows and columns must be divisible by blocksize.
2071:    This matrix type does not support complex Hermitian operation.

2073:    Specify the preallocated storage with either nz or nnz (not both).
2074:    Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
2075:    allocation.  See Users-Manual: ch_mat for details.

2077:    If the nnz parameter is given then the nz parameter is ignored

2079: .seealso: MatCreate(), MatCreateSeqAIJ(), MatSetValues(), MatCreateSBAIJ()
2080: @*/
2081: PetscErrorCode  MatCreateSeqSBAIJ(MPI_Comm comm,PetscInt bs,PetscInt m,PetscInt n,PetscInt nz,const PetscInt nnz[],Mat *A)
2082: {

2086:   MatCreate(comm,A);
2087:   MatSetSizes(*A,m,n,m,n);
2088:   MatSetType(*A,MATSEQSBAIJ);
2089:   MatSeqSBAIJSetPreallocation_SeqSBAIJ(*A,bs,nz,(PetscInt*)nnz);
2090:   return(0);
2091: }

2095: PetscErrorCode MatDuplicate_SeqSBAIJ(Mat A,MatDuplicateOption cpvalues,Mat *B)
2096: {
2097:   Mat            C;
2098:   Mat_SeqSBAIJ   *c,*a = (Mat_SeqSBAIJ*)A->data;
2100:   PetscInt       i,mbs = a->mbs,nz = a->nz,bs2 =a->bs2;

2103:   if (a->i[mbs] != nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Corrupt matrix");

2105:   *B   = 0;
2106:   MatCreate(PetscObjectComm((PetscObject)A),&C);
2107:   MatSetSizes(C,A->rmap->N,A->cmap->n,A->rmap->N,A->cmap->n);
2108:   MatSetType(C,MATSEQSBAIJ);
2109:   PetscMemcpy(C->ops,A->ops,sizeof(struct _MatOps));
2110:   c    = (Mat_SeqSBAIJ*)C->data;

2112:   C->preallocated       = PETSC_TRUE;
2113:   C->factortype         = A->factortype;
2114:   c->row                = 0;
2115:   c->icol               = 0;
2116:   c->saved_values       = 0;
2117:   c->keepnonzeropattern = a->keepnonzeropattern;
2118:   C->assembled          = PETSC_TRUE;

2120:   PetscLayoutReference(A->rmap,&C->rmap);
2121:   PetscLayoutReference(A->cmap,&C->cmap);
2122:   c->bs2 = a->bs2;
2123:   c->mbs = a->mbs;
2124:   c->nbs = a->nbs;

2126:   if  (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
2127:     c->imax           = a->imax;
2128:     c->ilen           = a->ilen;
2129:     c->free_imax_ilen = PETSC_FALSE;
2130:   } else {
2131:     PetscMalloc2((mbs+1),&c->imax,(mbs+1),&c->ilen);
2132:     PetscLogObjectMemory((PetscObject)C,2*(mbs+1)*sizeof(PetscInt));
2133:     for (i=0; i<mbs; i++) {
2134:       c->imax[i] = a->imax[i];
2135:       c->ilen[i] = a->ilen[i];
2136:     }
2137:     c->free_imax_ilen = PETSC_TRUE;
2138:   }

2140:   /* allocate the matrix space */
2141:   if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
2142:     PetscMalloc1(bs2*nz,&c->a);
2143:     PetscLogObjectMemory((PetscObject)C,nz*bs2*sizeof(MatScalar));
2144:     c->i            = a->i;
2145:     c->j            = a->j;
2146:     c->singlemalloc = PETSC_FALSE;
2147:     c->free_a       = PETSC_TRUE;
2148:     c->free_ij      = PETSC_FALSE;
2149:     c->parent       = A;
2150:     PetscObjectReference((PetscObject)A);
2151:     MatSetOption(A,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
2152:     MatSetOption(C,MAT_NEW_NONZERO_LOCATION_ERR,PETSC_TRUE);
2153:   } else {
2154:     PetscMalloc3(bs2*nz,&c->a,nz,&c->j,mbs+1,&c->i);
2155:     PetscMemcpy(c->i,a->i,(mbs+1)*sizeof(PetscInt));
2156:     PetscLogObjectMemory((PetscObject)C,(mbs+1)*sizeof(PetscInt) + nz*(bs2*sizeof(MatScalar) + sizeof(PetscInt)));
2157:     c->singlemalloc = PETSC_TRUE;
2158:     c->free_a       = PETSC_TRUE;
2159:     c->free_ij      = PETSC_TRUE;
2160:   }
2161:   if (mbs > 0) {
2162:     if (cpvalues != MAT_SHARE_NONZERO_PATTERN) {
2163:       PetscMemcpy(c->j,a->j,nz*sizeof(PetscInt));
2164:     }
2165:     if (cpvalues == MAT_COPY_VALUES) {
2166:       PetscMemcpy(c->a,a->a,bs2*nz*sizeof(MatScalar));
2167:     } else {
2168:       PetscMemzero(c->a,bs2*nz*sizeof(MatScalar));
2169:     }
2170:     if (a->jshort) {
2171:       /* cannot share jshort, it is reallocated in MatAssemblyEnd_SeqSBAIJ() */
2172:       /* if the parent matrix is reassembled, this child matrix will never notice */
2173:       PetscMalloc1(nz,&c->jshort);
2174:       PetscLogObjectMemory((PetscObject)C,nz*sizeof(unsigned short));
2175:       PetscMemcpy(c->jshort,a->jshort,nz*sizeof(unsigned short));

2177:       c->free_jshort = PETSC_TRUE;
2178:     }
2179:   }

2181:   c->roworiented = a->roworiented;
2182:   c->nonew       = a->nonew;

2184:   if (a->diag) {
2185:     if (cpvalues == MAT_SHARE_NONZERO_PATTERN) {
2186:       c->diag      = a->diag;
2187:       c->free_diag = PETSC_FALSE;
2188:     } else {
2189:       PetscMalloc1(mbs,&c->diag);
2190:       PetscLogObjectMemory((PetscObject)C,mbs*sizeof(PetscInt));
2191:       for (i=0; i<mbs; i++) c->diag[i] = a->diag[i];
2192:       c->free_diag = PETSC_TRUE;
2193:     }
2194:   }
2195:   c->nz         = a->nz;
2196:   c->maxnz      = a->nz; /* Since we allocate exactly the right amount */
2197:   c->solve_work = 0;
2198:   c->mult_work  = 0;

2200:   *B   = C;
2201:   PetscFunctionListDuplicate(((PetscObject)A)->qlist,&((PetscObject)C)->qlist);
2202:   return(0);
2203: }

2207: PetscErrorCode MatLoad_SeqSBAIJ(Mat newmat,PetscViewer viewer)
2208: {
2209:   Mat_SeqSBAIJ   *a;
2211:   int            fd;
2212:   PetscMPIInt    size;
2213:   PetscInt       i,nz,header[4],*rowlengths=0,M,N,bs = newmat->rmap->bs;
2214:   PetscInt       *mask,mbs,*jj,j,rowcount,nzcount,k,*s_browlengths,maskcount;
2215:   PetscInt       kmax,jcount,block,idx,point,nzcountb,extra_rows,rows,cols;
2216:   PetscInt       *masked,nmask,tmp,bs2,ishift;
2217:   PetscScalar    *aa;
2218:   MPI_Comm       comm;

2221:   PetscObjectGetComm((PetscObject)viewer,&comm);
2222:   PetscOptionsGetInt(((PetscObject)newmat)->prefix,"-matload_block_size",&bs,NULL);
2223:   if (bs < 0) bs = 1;
2224:   bs2  = bs*bs;

2226:   MPI_Comm_size(comm,&size);
2227:   if (size > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"view must have one processor");
2228:   PetscViewerBinaryGetDescriptor(viewer,&fd);
2229:   PetscBinaryRead(fd,header,4,PETSC_INT);
2230:   if (header[0] != MAT_FILE_CLASSID) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"not Mat object");
2231:   M = header[1]; N = header[2]; nz = header[3];

2233:   if (header[3] < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Matrix stored in special format, cannot load as SeqSBAIJ");

2235:   if (M != N) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Can only do square matrices");

2237:   /*
2238:      This code adds extra rows to make sure the number of rows is
2239:     divisible by the blocksize
2240:   */
2241:   mbs        = M/bs;
2242:   extra_rows = bs - M + bs*(mbs);
2243:   if (extra_rows == bs) extra_rows = 0;
2244:   else                  mbs++;
2245:   if (extra_rows) {
2246:     PetscInfo(viewer,"Padding loaded matrix to match blocksize\n");
2247:   }

2249:   /* Set global sizes if not already set */
2250:   if (newmat->rmap->n < 0 && newmat->rmap->N < 0 && newmat->cmap->n < 0 && newmat->cmap->N < 0) {
2251:     MatSetSizes(newmat,PETSC_DECIDE,PETSC_DECIDE,M+extra_rows,N+extra_rows);
2252:   } else { /* Check if the matrix global sizes are correct */
2253:     MatGetSize(newmat,&rows,&cols);
2254:     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);
2255:   }

2257:   /* read in row lengths */
2258:   PetscMalloc1(M+extra_rows,&rowlengths);
2259:   PetscBinaryRead(fd,rowlengths,M,PETSC_INT);
2260:   for (i=0; i<extra_rows; i++) rowlengths[M+i] = 1;

2262:   /* read in column indices */
2263:   PetscMalloc1(nz+extra_rows,&jj);
2264:   PetscBinaryRead(fd,jj,nz,PETSC_INT);
2265:   for (i=0; i<extra_rows; i++) jj[nz+i] = M+i;

2267:   /* loop over row lengths determining block row lengths */
2268:   PetscCalloc1(mbs,&s_browlengths);
2269:   PetscMalloc2(mbs,&mask,mbs,&masked);
2270:   PetscMemzero(mask,mbs*sizeof(PetscInt));
2271:   rowcount = 0;
2272:   nzcount  = 0;
2273:   for (i=0; i<mbs; i++) {
2274:     nmask = 0;
2275:     for (j=0; j<bs; j++) {
2276:       kmax = rowlengths[rowcount];
2277:       for (k=0; k<kmax; k++) {
2278:         tmp = jj[nzcount++]/bs;   /* block col. index */
2279:         if (!mask[tmp] && tmp >= i) {masked[nmask++] = tmp; mask[tmp] = 1;}
2280:       }
2281:       rowcount++;
2282:     }
2283:     s_browlengths[i] += nmask;

2285:     /* zero out the mask elements we set */
2286:     for (j=0; j<nmask; j++) mask[masked[j]] = 0;
2287:   }

2289:   /* Do preallocation */
2290:   MatSeqSBAIJSetPreallocation_SeqSBAIJ(newmat,bs,0,s_browlengths);
2291:   a    = (Mat_SeqSBAIJ*)newmat->data;

2293:   /* set matrix "i" values */
2294:   a->i[0] = 0;
2295:   for (i=1; i<= mbs; i++) {
2296:     a->i[i]      = a->i[i-1] + s_browlengths[i-1];
2297:     a->ilen[i-1] = s_browlengths[i-1];
2298:   }
2299:   a->nz = a->i[mbs];

2301:   /* read in nonzero values */
2302:   PetscMalloc1(nz+extra_rows,&aa);
2303:   PetscBinaryRead(fd,aa,nz,PETSC_SCALAR);
2304:   for (i=0; i<extra_rows; i++) aa[nz+i] = 1.0;

2306:   /* set "a" and "j" values into matrix */
2307:   nzcount = 0; jcount = 0;
2308:   for (i=0; i<mbs; i++) {
2309:     nzcountb = nzcount;
2310:     nmask    = 0;
2311:     for (j=0; j<bs; j++) {
2312:       kmax = rowlengths[i*bs+j];
2313:       for (k=0; k<kmax; k++) {
2314:         tmp = jj[nzcount++]/bs; /* block col. index */
2315:         if (!mask[tmp] && tmp >= i) { masked[nmask++] = tmp; mask[tmp] = 1;}
2316:       }
2317:     }
2318:     /* sort the masked values */
2319:     PetscSortInt(nmask,masked);

2321:     /* set "j" values into matrix */
2322:     maskcount = 1;
2323:     for (j=0; j<nmask; j++) {
2324:       a->j[jcount++]  = masked[j];
2325:       mask[masked[j]] = maskcount++;
2326:     }

2328:     /* set "a" values into matrix */
2329:     ishift = bs2*a->i[i];
2330:     for (j=0; j<bs; j++) {
2331:       kmax = rowlengths[i*bs+j];
2332:       for (k=0; k<kmax; k++) {
2333:         tmp = jj[nzcountb]/bs;        /* block col. index */
2334:         if (tmp >= i) {
2335:           block     = mask[tmp] - 1;
2336:           point     = jj[nzcountb] - bs*tmp;
2337:           idx       = ishift + bs2*block + j + bs*point;
2338:           a->a[idx] = aa[nzcountb];
2339:         }
2340:         nzcountb++;
2341:       }
2342:     }
2343:     /* zero out the mask elements we set */
2344:     for (j=0; j<nmask; j++) mask[masked[j]] = 0;
2345:   }
2346:   if (jcount != a->nz) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_FILE_UNEXPECTED,"Bad binary matrix");

2348:   PetscFree(rowlengths);
2349:   PetscFree(s_browlengths);
2350:   PetscFree(aa);
2351:   PetscFree(jj);
2352:   PetscFree2(mask,masked);

2354:   MatAssemblyBegin(newmat,MAT_FINAL_ASSEMBLY);
2355:   MatAssemblyEnd(newmat,MAT_FINAL_ASSEMBLY);
2356:   return(0);
2357: }

2361: /*@
2362:      MatCreateSeqSBAIJWithArrays - Creates an sequential SBAIJ matrix using matrix elements
2363:               (upper triangular entries in CSR format) provided by the user.

2365:      Collective on MPI_Comm

2367:    Input Parameters:
2368: +  comm - must be an MPI communicator of size 1
2369: .  bs - size of block
2370: .  m - number of rows
2371: .  n - number of columns
2372: .  i - row indices
2373: .  j - column indices
2374: -  a - matrix values

2376:    Output Parameter:
2377: .  mat - the matrix

2379:    Level: advanced

2381:    Notes:
2382:        The i, j, and a arrays are not copied by this routine, the user must free these arrays
2383:     once the matrix is destroyed

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

2387:        The i and j indices are 0 based

2389:        When block size is greater than 1 the matrix values must be stored using the SBAIJ storage format (see the SBAIJ code to determine this). For block size of 1
2390:        it is the regular CSR format excluding the lower triangular elements.

2392: .seealso: MatCreate(), MatCreateSBAIJ(), MatCreateSeqSBAIJ()

2394: @*/
2395: PetscErrorCode  MatCreateSeqSBAIJWithArrays(MPI_Comm comm,PetscInt bs,PetscInt m,PetscInt n,PetscInt *i,PetscInt *j,PetscScalar *a,Mat *mat)
2396: {
2398:   PetscInt       ii;
2399:   Mat_SeqSBAIJ   *sbaij;

2402:   if (bs != 1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"block size %D > 1 is not supported yet",bs);
2403:   if (i[0]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"i (row indices) must start with 0");

2405:   MatCreate(comm,mat);
2406:   MatSetSizes(*mat,m,n,m,n);
2407:   MatSetType(*mat,MATSEQSBAIJ);
2408:   MatSeqSBAIJSetPreallocation_SeqSBAIJ(*mat,bs,MAT_SKIP_ALLOCATION,0);
2409:   sbaij = (Mat_SeqSBAIJ*)(*mat)->data;
2410:   PetscMalloc2(m,&sbaij->imax,m,&sbaij->ilen);
2411:   PetscLogObjectMemory((PetscObject)*mat,2*m*sizeof(PetscInt));

2413:   sbaij->i = i;
2414:   sbaij->j = j;
2415:   sbaij->a = a;

2417:   sbaij->singlemalloc = PETSC_FALSE;
2418:   sbaij->nonew        = -1;             /*this indicates that inserting a new value in the matrix that generates a new nonzero is an error*/
2419:   sbaij->free_a       = PETSC_FALSE;
2420:   sbaij->free_ij      = PETSC_FALSE;

2422:   for (ii=0; ii<m; ii++) {
2423:     sbaij->ilen[ii] = sbaij->imax[ii] = i[ii+1] - i[ii];
2424: #if defined(PETSC_USE_DEBUG)
2425:     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]);
2426: #endif
2427:   }
2428: #if defined(PETSC_USE_DEBUG)
2429:   for (ii=0; ii<sbaij->i[m]; ii++) {
2430:     if (j[ii] < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Negative column index at location = %d index = %d",ii,j[ii]);
2431:     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]);
2432:   }
2433: #endif

2435:   MatAssemblyBegin(*mat,MAT_FINAL_ASSEMBLY);
2436:   MatAssemblyEnd(*mat,MAT_FINAL_ASSEMBLY);
2437:   return(0);
2438: }

2442: PetscErrorCode MatCreateMPIMatConcatenateSeqMat_SeqSBAIJ(MPI_Comm comm,Mat inmat,PetscInt n,MatReuse scall,Mat *outmat)
2443: {

2447:   MatCreateMPIMatConcatenateSeqMat_MPISBAIJ(comm,inmat,n,scall,outmat);
2448:   return(0);
2449: }