Actual source code: blockmat.c
petsc-3.7.7 2017-09-25
2: /*
3: This provides a matrix that consists of Mats
4: */
6: #include <petsc/private/matimpl.h> /*I "petscmat.h" I*/
7: #include <../src/mat/impls/baij/seq/baij.h> /* use the common AIJ data-structure */
9: typedef struct {
10: SEQAIJHEADER(Mat);
11: SEQBAIJHEADER;
12: Mat *diags;
14: Vec left,right,middle,workb; /* dummy vectors to perform local parts of product */
15: } Mat_BlockMat;
17: extern PetscErrorCode MatBlockMatSetPreallocation(Mat,PetscInt,PetscInt,const PetscInt*);
21: static PetscErrorCode MatSOR_BlockMat_Symmetric(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
22: {
23: Mat_BlockMat *a = (Mat_BlockMat*)A->data;
24: PetscScalar *x;
25: const Mat *v;
26: const PetscScalar *b;
27: PetscErrorCode ierr;
28: PetscInt n = A->cmap->n,i,mbs = n/A->rmap->bs,j,bs = A->rmap->bs;
29: const PetscInt *idx;
30: IS row,col;
31: MatFactorInfo info;
32: Vec left = a->left,right = a->right, middle = a->middle;
33: Mat *diag;
36: its = its*lits;
37: if (its <= 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Relaxation requires global its %D and local its %D both positive",its,lits);
38: if (flag & SOR_EISENSTAT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support yet for Eisenstat");
39: if (omega != 1.0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support yet for omega not equal to 1.0");
40: if (fshift) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support yet for fshift");
41: if ((flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) && !(flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP)) {
42: SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Cannot do backward sweep without forward sweep");
43: }
45: if (!a->diags) {
46: PetscMalloc1(mbs,&a->diags);
47: MatFactorInfoInitialize(&info);
48: for (i=0; i<mbs; i++) {
49: MatGetOrdering(a->a[a->diag[i]], MATORDERINGND,&row,&col);
50: MatCholeskyFactorSymbolic(a->diags[i],a->a[a->diag[i]],row,&info);
51: MatCholeskyFactorNumeric(a->diags[i],a->a[a->diag[i]],&info);
52: ISDestroy(&row);
53: ISDestroy(&col);
54: }
55: VecDuplicate(bb,&a->workb);
56: }
57: diag = a->diags;
59: VecSet(xx,0.0);
60: VecGetArray(xx,&x);
61: /* copy right hand side because it must be modified during iteration */
62: VecCopy(bb,a->workb);
63: VecGetArrayRead(a->workb,&b);
65: /* need to add code for when initial guess is zero, see MatSOR_SeqAIJ */
66: while (its--) {
67: if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
69: for (i=0; i<mbs; i++) {
70: n = a->i[i+1] - a->i[i] - 1;
71: idx = a->j + a->i[i] + 1;
72: v = a->a + a->i[i] + 1;
74: VecSet(left,0.0);
75: for (j=0; j<n; j++) {
76: VecPlaceArray(right,x + idx[j]*bs);
77: MatMultAdd(v[j],right,left,left);
78: VecResetArray(right);
79: }
80: VecPlaceArray(right,b + i*bs);
81: VecAYPX(left,-1.0,right);
82: VecResetArray(right);
84: VecPlaceArray(right,x + i*bs);
85: MatSolve(diag[i],left,right);
87: /* now adjust right hand side, see MatSOR_SeqSBAIJ */
88: for (j=0; j<n; j++) {
89: MatMultTranspose(v[j],right,left);
90: VecPlaceArray(middle,b + idx[j]*bs);
91: VecAXPY(middle,-1.0,left);
92: VecResetArray(middle);
93: }
94: VecResetArray(right);
96: }
97: }
98: if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
100: for (i=mbs-1; i>=0; i--) {
101: n = a->i[i+1] - a->i[i] - 1;
102: idx = a->j + a->i[i] + 1;
103: v = a->a + a->i[i] + 1;
105: VecSet(left,0.0);
106: for (j=0; j<n; j++) {
107: VecPlaceArray(right,x + idx[j]*bs);
108: MatMultAdd(v[j],right,left,left);
109: VecResetArray(right);
110: }
111: VecPlaceArray(right,b + i*bs);
112: VecAYPX(left,-1.0,right);
113: VecResetArray(right);
115: VecPlaceArray(right,x + i*bs);
116: MatSolve(diag[i],left,right);
117: VecResetArray(right);
119: }
120: }
121: }
122: VecRestoreArray(xx,&x);
123: VecRestoreArrayRead(a->workb,&b);
124: return(0);
125: }
129: static PetscErrorCode MatSOR_BlockMat(Mat A,Vec bb,PetscReal omega,MatSORType flag,PetscReal fshift,PetscInt its,PetscInt lits,Vec xx)
130: {
131: Mat_BlockMat *a = (Mat_BlockMat*)A->data;
132: PetscScalar *x;
133: const Mat *v;
134: const PetscScalar *b;
135: PetscErrorCode ierr;
136: PetscInt n = A->cmap->n,i,mbs = n/A->rmap->bs,j,bs = A->rmap->bs;
137: const PetscInt *idx;
138: IS row,col;
139: MatFactorInfo info;
140: Vec left = a->left,right = a->right;
141: Mat *diag;
144: its = its*lits;
145: if (its <= 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Relaxation requires global its %D and local its %D both positive",its,lits);
146: if (flag & SOR_EISENSTAT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support yet for Eisenstat");
147: if (omega != 1.0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support yet for omega not equal to 1.0");
148: if (fshift) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support yet for fshift");
150: if (!a->diags) {
151: PetscMalloc1(mbs,&a->diags);
152: MatFactorInfoInitialize(&info);
153: for (i=0; i<mbs; i++) {
154: MatGetOrdering(a->a[a->diag[i]], MATORDERINGND,&row,&col);
155: MatLUFactorSymbolic(a->diags[i],a->a[a->diag[i]],row,col,&info);
156: MatLUFactorNumeric(a->diags[i],a->a[a->diag[i]],&info);
157: ISDestroy(&row);
158: ISDestroy(&col);
159: }
160: }
161: diag = a->diags;
163: VecSet(xx,0.0);
164: VecGetArray(xx,&x);
165: VecGetArrayRead(bb,&b);
167: /* need to add code for when initial guess is zero, see MatSOR_SeqAIJ */
168: while (its--) {
169: if (flag & SOR_FORWARD_SWEEP || flag & SOR_LOCAL_FORWARD_SWEEP) {
171: for (i=0; i<mbs; i++) {
172: n = a->i[i+1] - a->i[i];
173: idx = a->j + a->i[i];
174: v = a->a + a->i[i];
176: VecSet(left,0.0);
177: for (j=0; j<n; j++) {
178: if (idx[j] != i) {
179: VecPlaceArray(right,x + idx[j]*bs);
180: MatMultAdd(v[j],right,left,left);
181: VecResetArray(right);
182: }
183: }
184: VecPlaceArray(right,b + i*bs);
185: VecAYPX(left,-1.0,right);
186: VecResetArray(right);
188: VecPlaceArray(right,x + i*bs);
189: MatSolve(diag[i],left,right);
190: VecResetArray(right);
191: }
192: }
193: if (flag & SOR_BACKWARD_SWEEP || flag & SOR_LOCAL_BACKWARD_SWEEP) {
195: for (i=mbs-1; i>=0; i--) {
196: n = a->i[i+1] - a->i[i];
197: idx = a->j + a->i[i];
198: v = a->a + a->i[i];
200: VecSet(left,0.0);
201: for (j=0; j<n; j++) {
202: if (idx[j] != i) {
203: VecPlaceArray(right,x + idx[j]*bs);
204: MatMultAdd(v[j],right,left,left);
205: VecResetArray(right);
206: }
207: }
208: VecPlaceArray(right,b + i*bs);
209: VecAYPX(left,-1.0,right);
210: VecResetArray(right);
212: VecPlaceArray(right,x + i*bs);
213: MatSolve(diag[i],left,right);
214: VecResetArray(right);
216: }
217: }
218: }
219: VecRestoreArray(xx,&x);
220: VecRestoreArrayRead(bb,&b);
221: return(0);
222: }
226: static PetscErrorCode MatSetValues_BlockMat(Mat A,PetscInt m,const PetscInt im[],PetscInt n,const PetscInt in[],const PetscScalar v[],InsertMode is)
227: {
228: Mat_BlockMat *a = (Mat_BlockMat*)A->data;
229: PetscInt *rp,k,low,high,t,ii,row,nrow,i,col,l,rmax,N,lastcol = -1;
230: PetscInt *imax=a->imax,*ai=a->i,*ailen=a->ilen;
231: PetscInt *aj =a->j,nonew=a->nonew,bs=A->rmap->bs,brow,bcol;
233: PetscInt ridx,cidx;
234: PetscBool roworiented=a->roworiented;
235: MatScalar value;
236: Mat *ap,*aa = a->a;
239: for (k=0; k<m; k++) { /* loop over added rows */
240: row = im[k];
241: brow = row/bs;
242: if (row < 0) continue;
243: #if defined(PETSC_USE_DEBUG)
244: 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);
245: #endif
246: rp = aj + ai[brow];
247: ap = aa + ai[brow];
248: rmax = imax[brow];
249: nrow = ailen[brow];
250: low = 0;
251: high = nrow;
252: for (l=0; l<n; l++) { /* loop over added columns */
253: if (in[l] < 0) continue;
254: #if defined(PETSC_USE_DEBUG)
255: 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);
256: #endif
257: col = in[l]; bcol = col/bs;
258: if (A->symmetric && brow > bcol) continue;
259: ridx = row % bs; cidx = col % bs;
260: if (roworiented) value = v[l + k*n];
261: else value = v[k + l*m];
263: if (col <= lastcol) low = 0;
264: else high = nrow;
265: lastcol = col;
266: while (high-low > 7) {
267: t = (low+high)/2;
268: if (rp[t] > bcol) high = t;
269: else low = t;
270: }
271: for (i=low; i<high; i++) {
272: if (rp[i] > bcol) break;
273: if (rp[i] == bcol) goto noinsert1;
274: }
275: if (nonew == 1) goto noinsert1;
276: if (nonew == -1) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Inserting a new nonzero (%D, %D) in the matrix", row, col);
277: MatSeqXAIJReallocateAIJ(A,a->mbs,1,nrow,brow,bcol,rmax,aa,ai,aj,rp,ap,imax,nonew,Mat);
278: N = nrow++ - 1; high++;
279: /* shift up all the later entries in this row */
280: for (ii=N; ii>=i; ii--) {
281: rp[ii+1] = rp[ii];
282: ap[ii+1] = ap[ii];
283: }
284: if (N>=i) ap[i] = 0;
285: rp[i] = bcol;
286: a->nz++;
287: A->nonzerostate++;
288: noinsert1:;
289: if (!*(ap+i)) {
290: MatCreateSeqAIJ(PETSC_COMM_SELF,bs,bs,0,0,ap+i);
291: }
292: MatSetValues(ap[i],1,&ridx,1,&cidx,&value,is);
293: low = i;
294: }
295: ailen[brow] = nrow;
296: }
297: return(0);
298: }
302: static PetscErrorCode MatLoad_BlockMat(Mat newmat, PetscViewer viewer)
303: {
304: PetscErrorCode ierr;
305: Mat tmpA;
306: PetscInt i,j,m,n,bs = 1,ncols,*lens,currentcol,mbs,**ii,*ilens,nextcol,*llens,cnt = 0;
307: const PetscInt *cols;
308: const PetscScalar *values;
309: PetscBool flg = PETSC_FALSE,notdone;
310: Mat_SeqAIJ *a;
311: Mat_BlockMat *amat;
314: /* force binary viewer to load .info file if it has not yet done so */
315: PetscViewerSetUp(viewer);
316: MatCreate(PETSC_COMM_SELF,&tmpA);
317: MatSetType(tmpA,MATSEQAIJ);
318: MatLoad_SeqAIJ(tmpA,viewer);
320: MatGetLocalSize(tmpA,&m,&n);
321: PetscOptionsBegin(PETSC_COMM_SELF,NULL,"Options for loading BlockMat matrix 1","Mat");
322: PetscOptionsInt("-matload_block_size","Set the blocksize used to store the matrix","MatLoad",bs,&bs,NULL);
323: PetscOptionsBool("-matload_symmetric","Store the matrix as symmetric","MatLoad",flg,&flg,NULL);
324: PetscOptionsEnd();
326: /* Determine number of nonzero blocks for each block row */
327: a = (Mat_SeqAIJ*) tmpA->data;
328: mbs = m/bs;
329: PetscMalloc3(mbs,&lens,bs,&ii,bs,&ilens);
330: PetscMemzero(lens,mbs*sizeof(PetscInt));
332: for (i=0; i<mbs; i++) {
333: for (j=0; j<bs; j++) {
334: ii[j] = a->j + a->i[i*bs + j];
335: ilens[j] = a->i[i*bs + j + 1] - a->i[i*bs + j];
336: }
338: currentcol = -1;
339: while (PETSC_TRUE) {
340: notdone = PETSC_FALSE;
341: nextcol = 1000000000;
342: for (j=0; j<bs; j++) {
343: while ((ilens[j] > 0 && ii[j][0]/bs <= currentcol)) {
344: ii[j]++;
345: ilens[j]--;
346: }
347: if (ilens[j] > 0) {
348: notdone = PETSC_TRUE;
349: nextcol = PetscMin(nextcol,ii[j][0]/bs);
350: }
351: }
352: if (!notdone) break;
353: if (!flg || (nextcol >= i)) lens[i]++;
354: currentcol = nextcol;
355: }
356: }
358: if (newmat->rmap->n < 0 && newmat->rmap->N < 0 && newmat->cmap->n < 0 && newmat->cmap->N < 0) {
359: MatSetSizes(newmat,m,n,PETSC_DETERMINE,PETSC_DETERMINE);
360: }
361: MatBlockMatSetPreallocation(newmat,bs,0,lens);
362: if (flg) {
363: MatSetOption(newmat,MAT_SYMMETRIC,PETSC_TRUE);
364: }
365: amat = (Mat_BlockMat*)(newmat)->data;
367: /* preallocate the submatrices */
368: PetscMalloc1(bs,&llens);
369: for (i=0; i<mbs; i++) { /* loops for block rows */
370: for (j=0; j<bs; j++) {
371: ii[j] = a->j + a->i[i*bs + j];
372: ilens[j] = a->i[i*bs + j + 1] - a->i[i*bs + j];
373: }
375: currentcol = 1000000000;
376: for (j=0; j<bs; j++) { /* loop over rows in block finding first nonzero block */
377: if (ilens[j] > 0) {
378: currentcol = PetscMin(currentcol,ii[j][0]/bs);
379: }
380: }
382: while (PETSC_TRUE) { /* loops over blocks in block row */
383: notdone = PETSC_FALSE;
384: nextcol = 1000000000;
385: PetscMemzero(llens,bs*sizeof(PetscInt));
386: for (j=0; j<bs; j++) { /* loop over rows in block */
387: while ((ilens[j] > 0 && ii[j][0]/bs <= currentcol)) { /* loop over columns in row */
388: ii[j]++;
389: ilens[j]--;
390: llens[j]++;
391: }
392: if (ilens[j] > 0) {
393: notdone = PETSC_TRUE;
394: nextcol = PetscMin(nextcol,ii[j][0]/bs);
395: }
396: }
397: if (cnt >= amat->maxnz) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Number of blocks found greater than expected %D",cnt);
398: if (!flg || currentcol >= i) {
399: amat->j[cnt] = currentcol;
400: MatCreateSeqAIJ(PETSC_COMM_SELF,bs,bs,0,llens,amat->a+cnt++);
401: }
403: if (!notdone) break;
404: currentcol = nextcol;
405: }
406: amat->ilen[i] = lens[i];
407: }
409: PetscFree3(lens,ii,ilens);
410: PetscFree(llens);
412: /* copy over the matrix, one row at a time */
413: for (i=0; i<m; i++) {
414: MatGetRow(tmpA,i,&ncols,&cols,&values);
415: MatSetValues(newmat,1,&i,ncols,cols,values,INSERT_VALUES);
416: MatRestoreRow(tmpA,i,&ncols,&cols,&values);
417: }
418: MatAssemblyBegin(newmat,MAT_FINAL_ASSEMBLY);
419: MatAssemblyEnd(newmat,MAT_FINAL_ASSEMBLY);
420: return(0);
421: }
425: static PetscErrorCode MatView_BlockMat(Mat A,PetscViewer viewer)
426: {
427: Mat_BlockMat *a = (Mat_BlockMat*)A->data;
428: PetscErrorCode ierr;
429: const char *name;
430: PetscViewerFormat format;
433: PetscObjectGetName((PetscObject)A,&name);
434: PetscViewerGetFormat(viewer,&format);
435: if (format == PETSC_VIEWER_ASCII_FACTOR_INFO || format == PETSC_VIEWER_ASCII_INFO) {
436: PetscViewerASCIIPrintf(viewer,"Nonzero block matrices = %D \n",a->nz);
437: if (A->symmetric) {
438: PetscViewerASCIIPrintf(viewer,"Only upper triangular part of symmetric matrix is stored\n");
439: }
440: }
441: return(0);
442: }
446: static PetscErrorCode MatDestroy_BlockMat(Mat mat)
447: {
449: Mat_BlockMat *bmat = (Mat_BlockMat*)mat->data;
450: PetscInt i;
453: VecDestroy(&bmat->right);
454: VecDestroy(&bmat->left);
455: VecDestroy(&bmat->middle);
456: VecDestroy(&bmat->workb);
457: if (bmat->diags) {
458: for (i=0; i<mat->rmap->n/mat->rmap->bs; i++) {
459: MatDestroy(&bmat->diags[i]);
460: }
461: }
462: if (bmat->a) {
463: for (i=0; i<bmat->nz; i++) {
464: MatDestroy(&bmat->a[i]);
465: }
466: }
467: MatSeqXAIJFreeAIJ(mat,(PetscScalar**)&bmat->a,&bmat->j,&bmat->i);
468: PetscFree(mat->data);
469: return(0);
470: }
474: static PetscErrorCode MatMult_BlockMat(Mat A,Vec x,Vec y)
475: {
476: Mat_BlockMat *bmat = (Mat_BlockMat*)A->data;
478: PetscScalar *xx,*yy;
479: PetscInt *aj,i,*ii,jrow,m = A->rmap->n/A->rmap->bs,bs = A->rmap->bs,n,j;
480: Mat *aa;
483: /*
484: Standard CSR multiply except each entry is a Mat
485: */
486: VecGetArray(x,&xx);
488: VecSet(y,0.0);
489: VecGetArray(y,&yy);
490: aj = bmat->j;
491: aa = bmat->a;
492: ii = bmat->i;
493: for (i=0; i<m; i++) {
494: jrow = ii[i];
495: VecPlaceArray(bmat->left,yy + bs*i);
496: n = ii[i+1] - jrow;
497: for (j=0; j<n; j++) {
498: VecPlaceArray(bmat->right,xx + bs*aj[jrow]);
499: MatMultAdd(aa[jrow],bmat->right,bmat->left,bmat->left);
500: VecResetArray(bmat->right);
501: jrow++;
502: }
503: VecResetArray(bmat->left);
504: }
505: VecRestoreArray(x,&xx);
506: VecRestoreArray(y,&yy);
507: return(0);
508: }
512: PetscErrorCode MatMult_BlockMat_Symmetric(Mat A,Vec x,Vec y)
513: {
514: Mat_BlockMat *bmat = (Mat_BlockMat*)A->data;
516: PetscScalar *xx,*yy;
517: PetscInt *aj,i,*ii,jrow,m = A->rmap->n/A->rmap->bs,bs = A->rmap->bs,n,j;
518: Mat *aa;
521: /*
522: Standard CSR multiply except each entry is a Mat
523: */
524: VecGetArray(x,&xx);
526: VecSet(y,0.0);
527: VecGetArray(y,&yy);
528: aj = bmat->j;
529: aa = bmat->a;
530: ii = bmat->i;
531: for (i=0; i<m; i++) {
532: jrow = ii[i];
533: n = ii[i+1] - jrow;
534: VecPlaceArray(bmat->left,yy + bs*i);
535: VecPlaceArray(bmat->middle,xx + bs*i);
536: /* if we ALWAYS required a diagonal entry then could remove this if test */
537: if (aj[jrow] == i) {
538: VecPlaceArray(bmat->right,xx + bs*aj[jrow]);
539: MatMultAdd(aa[jrow],bmat->right,bmat->left,bmat->left);
540: VecResetArray(bmat->right);
541: jrow++;
542: n--;
543: }
544: for (j=0; j<n; j++) {
545: VecPlaceArray(bmat->right,xx + bs*aj[jrow]); /* upper triangular part */
546: MatMultAdd(aa[jrow],bmat->right,bmat->left,bmat->left);
547: VecResetArray(bmat->right);
549: VecPlaceArray(bmat->right,yy + bs*aj[jrow]); /* lower triangular part */
550: MatMultTransposeAdd(aa[jrow],bmat->middle,bmat->right,bmat->right);
551: VecResetArray(bmat->right);
552: jrow++;
553: }
554: VecResetArray(bmat->left);
555: VecResetArray(bmat->middle);
556: }
557: VecRestoreArray(x,&xx);
558: VecRestoreArray(y,&yy);
559: return(0);
560: }
564: static PetscErrorCode MatMultAdd_BlockMat(Mat A,Vec x,Vec y,Vec z)
565: {
567: return(0);
568: }
572: static PetscErrorCode MatMultTranspose_BlockMat(Mat A,Vec x,Vec y)
573: {
575: return(0);
576: }
580: static PetscErrorCode MatMultTransposeAdd_BlockMat(Mat A,Vec x,Vec y,Vec z)
581: {
583: return(0);
584: }
586: /*
587: Adds diagonal pointers to sparse matrix structure.
588: */
591: static PetscErrorCode MatMarkDiagonal_BlockMat(Mat A)
592: {
593: Mat_BlockMat *a = (Mat_BlockMat*)A->data;
595: PetscInt i,j,mbs = A->rmap->n/A->rmap->bs;
598: if (!a->diag) {
599: PetscMalloc1(mbs,&a->diag);
600: }
601: for (i=0; i<mbs; i++) {
602: a->diag[i] = a->i[i+1];
603: for (j=a->i[i]; j<a->i[i+1]; j++) {
604: if (a->j[j] == i) {
605: a->diag[i] = j;
606: break;
607: }
608: }
609: }
610: return(0);
611: }
615: static PetscErrorCode MatGetSubMatrix_BlockMat(Mat A,IS isrow,IS iscol,MatReuse scall,Mat *B)
616: {
617: Mat_BlockMat *a = (Mat_BlockMat*)A->data;
618: Mat_SeqAIJ *c;
620: PetscInt i,k,first,step,lensi,nrows,ncols;
621: PetscInt *j_new,*i_new,*aj = a->j,*ailen = a->ilen;
622: PetscScalar *a_new;
623: Mat C,*aa = a->a;
624: PetscBool stride,equal;
627: ISEqual(isrow,iscol,&equal);
628: if (!equal) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only for idential column and row indices");
629: PetscObjectTypeCompare((PetscObject)iscol,ISSTRIDE,&stride);
630: if (!stride) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Only for stride indices");
631: ISStrideGetInfo(iscol,&first,&step);
632: if (step != A->rmap->bs) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Can only select one entry from each block");
634: ISGetLocalSize(isrow,&nrows);
635: ncols = nrows;
637: /* create submatrix */
638: if (scall == MAT_REUSE_MATRIX) {
639: PetscInt n_cols,n_rows;
640: C = *B;
641: MatGetSize(C,&n_rows,&n_cols);
642: if (n_rows != nrows || n_cols != ncols) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Reused submatrix wrong size");
643: MatZeroEntries(C);
644: } else {
645: MatCreate(PetscObjectComm((PetscObject)A),&C);
646: MatSetSizes(C,nrows,ncols,PETSC_DETERMINE,PETSC_DETERMINE);
647: if (A->symmetric) {
648: MatSetType(C,MATSEQSBAIJ);
649: } else {
650: MatSetType(C,MATSEQAIJ);
651: }
652: MatSeqAIJSetPreallocation(C,0,ailen);
653: MatSeqSBAIJSetPreallocation(C,1,0,ailen);
654: }
655: c = (Mat_SeqAIJ*)C->data;
657: /* loop over rows inserting into submatrix */
658: a_new = c->a;
659: j_new = c->j;
660: i_new = c->i;
662: for (i=0; i<nrows; i++) {
663: lensi = ailen[i];
664: for (k=0; k<lensi; k++) {
665: *j_new++ = *aj++;
666: MatGetValue(*aa++,first,first,a_new++);
667: }
668: i_new[i+1] = i_new[i] + lensi;
669: c->ilen[i] = lensi;
670: }
672: MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
673: MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
674: *B = C;
675: return(0);
676: }
680: static PetscErrorCode MatAssemblyEnd_BlockMat(Mat A,MatAssemblyType mode)
681: {
682: Mat_BlockMat *a = (Mat_BlockMat*)A->data;
684: PetscInt fshift = 0,i,j,*ai = a->i,*aj = a->j,*imax = a->imax;
685: PetscInt m = a->mbs,*ip,N,*ailen = a->ilen,rmax = 0;
686: Mat *aa = a->a,*ap;
689: if (mode == MAT_FLUSH_ASSEMBLY) return(0);
691: if (m) rmax = ailen[0]; /* determine row with most nonzeros */
692: for (i=1; i<m; i++) {
693: /* move each row back by the amount of empty slots (fshift) before it*/
694: fshift += imax[i-1] - ailen[i-1];
695: rmax = PetscMax(rmax,ailen[i]);
696: if (fshift) {
697: ip = aj + ai[i];
698: ap = aa + ai[i];
699: N = ailen[i];
700: for (j=0; j<N; j++) {
701: ip[j-fshift] = ip[j];
702: ap[j-fshift] = ap[j];
703: }
704: }
705: ai[i] = ai[i-1] + ailen[i-1];
706: }
707: if (m) {
708: fshift += imax[m-1] - ailen[m-1];
709: ai[m] = ai[m-1] + ailen[m-1];
710: }
711: /* reset ilen and imax for each row */
712: for (i=0; i<m; i++) {
713: ailen[i] = imax[i] = ai[i+1] - ai[i];
714: }
715: a->nz = ai[m];
716: for (i=0; i<a->nz; i++) {
717: #if defined(PETSC_USE_DEBUG)
718: if (!aa[i]) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Null matrix at location %D column %D nz %D",i,aj[i],a->nz);
719: #endif
720: MatAssemblyBegin(aa[i],MAT_FINAL_ASSEMBLY);
721: MatAssemblyEnd(aa[i],MAT_FINAL_ASSEMBLY);
722: }
723: PetscInfo4(A,"Matrix size: %D X %D; storage space: %D unneeded,%D used\n",m,A->cmap->n/A->cmap->bs,fshift,a->nz);
724: PetscInfo1(A,"Number of mallocs during MatSetValues() is %D\n",a->reallocs);
725: PetscInfo1(A,"Maximum nonzeros in any row is %D\n",rmax);
727: A->info.mallocs += a->reallocs;
728: a->reallocs = 0;
729: A->info.nz_unneeded = (double)fshift;
730: a->rmax = rmax;
731: MatMarkDiagonal_BlockMat(A);
732: return(0);
733: }
737: static PetscErrorCode MatSetOption_BlockMat(Mat A,MatOption opt,PetscBool flg)
738: {
740: if (opt == MAT_SYMMETRIC && flg) {
741: A->ops->sor = MatSOR_BlockMat_Symmetric;
742: A->ops->mult = MatMult_BlockMat_Symmetric;
743: } else {
744: PetscInfo1(A,"Unused matrix option %s\n",MatOptions[opt]);
745: }
746: return(0);
747: }
750: static struct _MatOps MatOps_Values = {MatSetValues_BlockMat,
751: 0,
752: 0,
753: MatMult_BlockMat,
754: /* 4*/ MatMultAdd_BlockMat,
755: MatMultTranspose_BlockMat,
756: MatMultTransposeAdd_BlockMat,
757: 0,
758: 0,
759: 0,
760: /* 10*/ 0,
761: 0,
762: 0,
763: MatSOR_BlockMat,
764: 0,
765: /* 15*/ 0,
766: 0,
767: 0,
768: 0,
769: 0,
770: /* 20*/ 0,
771: MatAssemblyEnd_BlockMat,
772: MatSetOption_BlockMat,
773: 0,
774: /* 24*/ 0,
775: 0,
776: 0,
777: 0,
778: 0,
779: /* 29*/ 0,
780: 0,
781: 0,
782: 0,
783: 0,
784: /* 34*/ 0,
785: 0,
786: 0,
787: 0,
788: 0,
789: /* 39*/ 0,
790: 0,
791: 0,
792: 0,
793: 0,
794: /* 44*/ 0,
795: 0,
796: MatShift_Basic,
797: 0,
798: 0,
799: /* 49*/ 0,
800: 0,
801: 0,
802: 0,
803: 0,
804: /* 54*/ 0,
805: 0,
806: 0,
807: 0,
808: 0,
809: /* 59*/ MatGetSubMatrix_BlockMat,
810: MatDestroy_BlockMat,
811: MatView_BlockMat,
812: 0,
813: 0,
814: /* 64*/ 0,
815: 0,
816: 0,
817: 0,
818: 0,
819: /* 69*/ 0,
820: 0,
821: 0,
822: 0,
823: 0,
824: /* 74*/ 0,
825: 0,
826: 0,
827: 0,
828: 0,
829: /* 79*/ 0,
830: 0,
831: 0,
832: 0,
833: MatLoad_BlockMat,
834: /* 84*/ 0,
835: 0,
836: 0,
837: 0,
838: 0,
839: /* 89*/ 0,
840: 0,
841: 0,
842: 0,
843: 0,
844: /* 94*/ 0,
845: 0,
846: 0,
847: 0,
848: 0,
849: /* 99*/ 0,
850: 0,
851: 0,
852: 0,
853: 0,
854: /*104*/ 0,
855: 0,
856: 0,
857: 0,
858: 0,
859: /*109*/ 0,
860: 0,
861: 0,
862: 0,
863: 0,
864: /*114*/ 0,
865: 0,
866: 0,
867: 0,
868: 0,
869: /*119*/ 0,
870: 0,
871: 0,
872: 0,
873: 0,
874: /*124*/ 0,
875: 0,
876: 0,
877: 0,
878: 0,
879: /*129*/ 0,
880: 0,
881: 0,
882: 0,
883: 0,
884: /*134*/ 0,
885: 0,
886: 0,
887: 0,
888: 0,
889: /*139*/ 0,
890: 0,
891: 0
892: };
896: /*@C
897: MatBlockMatSetPreallocation - For good matrix assembly performance
898: the user should preallocate the matrix storage by setting the parameter nz
899: (or the array nnz). By setting these parameters accurately, performance
900: during matrix assembly can be increased by more than a factor of 50.
902: Collective on MPI_Comm
904: Input Parameters:
905: + B - The matrix
906: . bs - size of each block in matrix
907: . nz - number of nonzeros per block row (same for all rows)
908: - nnz - array containing the number of nonzeros in the various block rows
909: (possibly different for each row) or NULL
911: Notes:
912: If nnz is given then nz is ignored
914: Specify the preallocated storage with either nz or nnz (not both).
915: Set nz=PETSC_DEFAULT and nnz=NULL for PETSc to control dynamic memory
916: allocation. For large problems you MUST preallocate memory or you
917: will get TERRIBLE performance, see the users' manual chapter on matrices.
919: Level: intermediate
921: .seealso: MatCreate(), MatCreateBlockMat(), MatSetValues()
923: @*/
924: PetscErrorCode MatBlockMatSetPreallocation(Mat B,PetscInt bs,PetscInt nz,const PetscInt nnz[])
925: {
929: PetscTryMethod(B,"MatBlockMatSetPreallocation_C",(Mat,PetscInt,PetscInt,const PetscInt[]),(B,bs,nz,nnz));
930: return(0);
931: }
935: static PetscErrorCode MatBlockMatSetPreallocation_BlockMat(Mat A,PetscInt bs,PetscInt nz,PetscInt *nnz)
936: {
937: Mat_BlockMat *bmat = (Mat_BlockMat*)A->data;
939: PetscInt i;
942: PetscLayoutSetBlockSize(A->rmap,bs);
943: PetscLayoutSetBlockSize(A->cmap,bs);
944: PetscLayoutSetUp(A->rmap);
945: PetscLayoutSetUp(A->cmap);
946: PetscLayoutGetBlockSize(A->rmap,&bs);
948: if (nz == PETSC_DEFAULT || nz == PETSC_DECIDE) nz = 5;
949: if (nz < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nz cannot be less than 0: value %d",nz);
950: if (nnz) {
951: for (i=0; i<A->rmap->n/bs; i++) {
952: 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]);
953: if (nnz[i] > A->cmap->n/bs) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nnz cannot be greater than row length: local row %d value %d rowlength %d",i,nnz[i],A->cmap->n/bs);
954: }
955: }
956: bmat->mbs = A->rmap->n/bs;
958: VecCreateSeqWithArray(PETSC_COMM_SELF,1,bs,NULL,&bmat->right);
959: VecCreateSeqWithArray(PETSC_COMM_SELF,1,bs,NULL,&bmat->middle);
960: VecCreateSeq(PETSC_COMM_SELF,bs,&bmat->left);
962: if (!bmat->imax) {
963: PetscMalloc2(A->rmap->n,&bmat->imax,A->rmap->n,&bmat->ilen);
964: PetscLogObjectMemory((PetscObject)A,2*A->rmap->n*sizeof(PetscInt));
965: }
966: if (nnz) {
967: nz = 0;
968: for (i=0; i<A->rmap->n/A->rmap->bs; i++) {
969: bmat->imax[i] = nnz[i];
970: nz += nnz[i];
971: }
972: } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"Currently requires block row by row preallocation");
974: /* bmat->ilen will count nonzeros in each row so far. */
975: for (i=0; i<bmat->mbs; i++) bmat->ilen[i] = 0;
977: /* allocate the matrix space */
978: MatSeqXAIJFreeAIJ(A,(PetscScalar**)&bmat->a,&bmat->j,&bmat->i);
979: PetscMalloc3(nz,&bmat->a,nz,&bmat->j,A->rmap->n+1,&bmat->i);
980: PetscLogObjectMemory((PetscObject)A,(A->rmap->n+1)*sizeof(PetscInt)+nz*(sizeof(PetscScalar)+sizeof(PetscInt)));
981: bmat->i[0] = 0;
982: for (i=1; i<bmat->mbs+1; i++) {
983: bmat->i[i] = bmat->i[i-1] + bmat->imax[i-1];
984: }
985: bmat->singlemalloc = PETSC_TRUE;
986: bmat->free_a = PETSC_TRUE;
987: bmat->free_ij = PETSC_TRUE;
989: bmat->nz = 0;
990: bmat->maxnz = nz;
991: A->info.nz_unneeded = (double)bmat->maxnz;
992: MatSetOption(A,MAT_NEW_NONZERO_ALLOCATION_ERR,PETSC_TRUE);
993: return(0);
994: }
996: /*MC
997: MATBLOCKMAT - A matrix that is defined by a set of Mat's that represents a sparse block matrix
998: consisting of (usually) sparse blocks.
1000: Level: advanced
1002: .seealso: MatCreateBlockMat()
1004: M*/
1008: PETSC_EXTERN PetscErrorCode MatCreate_BlockMat(Mat A)
1009: {
1010: Mat_BlockMat *b;
1014: PetscNewLog(A,&b);
1015: A->data = (void*)b;
1016: PetscMemcpy(A->ops,&MatOps_Values,sizeof(struct _MatOps));
1018: A->assembled = PETSC_TRUE;
1019: A->preallocated = PETSC_FALSE;
1020: PetscObjectChangeTypeName((PetscObject)A,MATBLOCKMAT);
1022: PetscObjectComposeFunction((PetscObject)A,"MatBlockMatSetPreallocation_C",MatBlockMatSetPreallocation_BlockMat);
1023: return(0);
1024: }
1028: /*@C
1029: MatCreateBlockMat - Creates a new matrix in which each block contains a uniform-size sequential Mat object
1031: Collective on MPI_Comm
1033: Input Parameters:
1034: + comm - MPI communicator
1035: . m - number of rows
1036: . n - number of columns
1037: . bs - size of each submatrix
1038: . nz - expected maximum number of nonzero blocks in row (use PETSC_DEFAULT if not known)
1039: - nnz - expected number of nonzers per block row if known (use NULL otherwise)
1042: Output Parameter:
1043: . A - the matrix
1045: Level: intermediate
1047: Notes: Matrices of this type are nominally-sparse matrices in which each "entry" is a Mat object. Each Mat must
1048: have the same size and be sequential. The local and global sizes must be compatible with this decomposition.
1050: For matrices containing parallel submatrices and variable block sizes, see MATNEST.
1052: .keywords: matrix, bmat, create
1054: .seealso: MATBLOCKMAT, MatCreateNest()
1055: @*/
1056: PetscErrorCode MatCreateBlockMat(MPI_Comm comm,PetscInt m,PetscInt n,PetscInt bs,PetscInt nz,PetscInt *nnz, Mat *A)
1057: {
1061: MatCreate(comm,A);
1062: MatSetSizes(*A,m,n,PETSC_DETERMINE,PETSC_DETERMINE);
1063: MatSetType(*A,MATBLOCKMAT);
1064: MatBlockMatSetPreallocation(*A,bs,nz,nnz);
1065: return(0);
1066: }