Actual source code: matnest.c

petsc-3.13.0 2020-03-29
Report Typos and Errors
  1:  #include <../src/mat/impls/nest/matnestimpl.h>
  2:  #include <../src/mat/impls/aij/seq/aij.h>
  3:  #include <petscsf.h>

  5: static PetscErrorCode MatSetUp_NestIS_Private(Mat,PetscInt,const IS[],PetscInt,const IS[]);
  6: static PetscErrorCode MatCreateVecs_Nest(Mat,Vec*,Vec*);
  7: static PetscErrorCode MatReset_Nest(Mat);

  9: PETSC_INTERN PetscErrorCode MatConvert_Nest_IS(Mat,MatType,MatReuse,Mat*);

 11: /* private functions */
 12: static PetscErrorCode MatNestGetSizes_Private(Mat A,PetscInt *m,PetscInt *n,PetscInt *M,PetscInt *N)
 13: {
 14:   Mat_Nest       *bA = (Mat_Nest*)A->data;
 15:   PetscInt       i,j;

 19:   *m = *n = *M = *N = 0;
 20:   for (i=0; i<bA->nr; i++) {  /* rows */
 21:     PetscInt sm,sM;
 22:     ISGetLocalSize(bA->isglobal.row[i],&sm);
 23:     ISGetSize(bA->isglobal.row[i],&sM);
 24:     *m  += sm;
 25:     *M  += sM;
 26:   }
 27:   for (j=0; j<bA->nc; j++) {  /* cols */
 28:     PetscInt sn,sN;
 29:     ISGetLocalSize(bA->isglobal.col[j],&sn);
 30:     ISGetSize(bA->isglobal.col[j],&sN);
 31:     *n  += sn;
 32:     *N  += sN;
 33:   }
 34:   return(0);
 35: }

 37: /* operations */
 38: static PetscErrorCode MatMult_Nest(Mat A,Vec x,Vec y)
 39: {
 40:   Mat_Nest       *bA = (Mat_Nest*)A->data;
 41:   Vec            *bx = bA->right,*by = bA->left;
 42:   PetscInt       i,j,nr = bA->nr,nc = bA->nc;

 46:   for (i=0; i<nr; i++) {VecGetSubVector(y,bA->isglobal.row[i],&by[i]);}
 47:   for (i=0; i<nc; i++) {VecGetSubVector(x,bA->isglobal.col[i],&bx[i]);}
 48:   for (i=0; i<nr; i++) {
 49:     VecZeroEntries(by[i]);
 50:     for (j=0; j<nc; j++) {
 51:       if (!bA->m[i][j]) continue;
 52:       /* y[i] <- y[i] + A[i][j] * x[j] */
 53:       MatMultAdd(bA->m[i][j],bx[j],by[i],by[i]);
 54:     }
 55:   }
 56:   for (i=0; i<nr; i++) {VecRestoreSubVector(y,bA->isglobal.row[i],&by[i]);}
 57:   for (i=0; i<nc; i++) {VecRestoreSubVector(x,bA->isglobal.col[i],&bx[i]);}
 58:   return(0);
 59: }

 61: static PetscErrorCode MatMultAdd_Nest(Mat A,Vec x,Vec y,Vec z)
 62: {
 63:   Mat_Nest       *bA = (Mat_Nest*)A->data;
 64:   Vec            *bx = bA->right,*bz = bA->left;
 65:   PetscInt       i,j,nr = bA->nr,nc = bA->nc;

 69:   for (i=0; i<nr; i++) {VecGetSubVector(z,bA->isglobal.row[i],&bz[i]);}
 70:   for (i=0; i<nc; i++) {VecGetSubVector(x,bA->isglobal.col[i],&bx[i]);}
 71:   for (i=0; i<nr; i++) {
 72:     if (y != z) {
 73:       Vec by;
 74:       VecGetSubVector(y,bA->isglobal.row[i],&by);
 75:       VecCopy(by,bz[i]);
 76:       VecRestoreSubVector(y,bA->isglobal.row[i],&by);
 77:     }
 78:     for (j=0; j<nc; j++) {
 79:       if (!bA->m[i][j]) continue;
 80:       /* y[i] <- y[i] + A[i][j] * x[j] */
 81:       MatMultAdd(bA->m[i][j],bx[j],bz[i],bz[i]);
 82:     }
 83:   }
 84:   for (i=0; i<nr; i++) {VecRestoreSubVector(z,bA->isglobal.row[i],&bz[i]);}
 85:   for (i=0; i<nc; i++) {VecRestoreSubVector(x,bA->isglobal.col[i],&bx[i]);}
 86:   return(0);
 87: }

 89: typedef struct {
 90:   Mat          *workC;    /* array of Mat with specific containers depending on the underlying MatMatMult implementation */
 91:   PetscScalar  *tarray;   /* buffer for storing all temporary products A[i][j] B[j] */
 92:   PetscInt     *dm,*dn,k; /* displacements and number of submatrices */
 93: } Nest_Dense;

 95: PETSC_INTERN PetscErrorCode MatMatMultNumeric_Nest_Dense(Mat A,Mat B,Mat C)
 96: {
 97:   Mat_Nest          *bA = (Mat_Nest*)A->data;
 98:   PetscContainer    container;
 99:   Nest_Dense        *contents;
100:   Mat               viewB,viewC,seq,productB,workC;
101:   const PetscScalar *barray;
102:   PetscScalar       *carray;
103:   PetscInt          i,j,M,N,nr = bA->nr,nc = bA->nc,ldb,ldc;
104:   PetscErrorCode    ierr;

107:   PetscObjectQuery((PetscObject)C,"workC",(PetscObject*)&container);
108:   if (!container) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Container does not exist");
109:   PetscContainerGetPointer(container,(void**)&contents);
110:   MatDenseGetLDA(B,&ldb);
111:   MatDenseGetLDA(C,&ldc);
112:   MatGetSize(B,NULL,&N);
113:   MatZeroEntries(C);
114:   MatDenseGetArrayRead(B,&barray);
115:   MatDenseGetArray(C,&carray);
116:   for (i=0; i<nr; i++) {
117:     ISGetSize(bA->isglobal.row[i],&M);
118:     MatCreateDense(PetscObjectComm((PetscObject)A),contents->dm[i+1]-contents->dm[i],PETSC_DECIDE,M,N,carray+contents->dm[i],&viewC);
119:     MatDenseGetLocalMatrix(viewC,&seq);
120:     MatSeqDenseSetLDA(seq,ldc);
121:     for (j=0; j<nc; j++) {
122:       if (!bA->m[i][j]) continue;
123:       ISGetSize(bA->isglobal.col[j],&M);
124:       MatCreateDense(PetscObjectComm((PetscObject)A),contents->dn[j+1]-contents->dn[j],PETSC_DECIDE,M,N,(PetscScalar*)(barray+contents->dn[j]),&viewB);
125:       MatDenseGetLocalMatrix(viewB,&seq);
126:       MatSeqDenseSetLDA(seq,ldb);

128:       /* MatMatMultNumeric(bA->m[i][j],viewB,contents->workC[i*nc + j]); */
129:       workC             = contents->workC[i*nc + j];
130:       productB          = workC->product->B;
131:       workC->product->B = viewB; /* use newly created dense matrix viewB */
132:       (workC->ops->productnumeric)(workC);
133:       MatDestroy(&viewB);
134:       workC->product->B = productB; /* resume original B */

136:       /* C[i] <- workC + C[i] */
137:       MatAXPY(viewC,1.0,contents->workC[i*nc + j],SAME_NONZERO_PATTERN);
138:     }
139:     MatDestroy(&viewC);
140:   }
141:   MatDenseRestoreArray(C,&carray);
142:   MatDenseRestoreArrayRead(B,&barray);

144:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
145:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
146:   return(0);
147: }

149: PetscErrorCode MatNest_DenseDestroy(void *ctx)
150: {
151:   Nest_Dense     *contents = (Nest_Dense*)ctx;
152:   PetscInt       i;

156:   PetscFree(contents->tarray);
157:   for (i=0; i<contents->k; i++) {
158:     MatDestroy(contents->workC + i);
159:   }
160:   PetscFree3(contents->dm,contents->dn,contents->workC);
161:   PetscFree(contents);
162:   return(0);
163: }

165: PETSC_INTERN PetscErrorCode MatMatMultSymbolic_Nest_Dense(Mat A,Mat B,PetscReal fill,Mat C)
166: {
167:   Mat_Nest          *bA = (Mat_Nest*)A->data;
168:   Mat               viewB,viewSeq,workC;
169:   const PetscScalar *barray;
170:   PetscInt          i,j,M,N,m,nr = bA->nr,nc = bA->nc,maxm = 0,ldb;
171:   PetscContainer    container;
172:   Nest_Dense        *contents=NULL;
173:   PetscErrorCode    ierr;

176:   if (!C->assembled) {
177:     MatGetSize(B,NULL,&N);
178:     MatGetLocalSize(A,&m,NULL);
179:     MatGetSize(A,&M,NULL);

181:     MatSetSizes(C,m,PETSC_DECIDE,M,N);
182:     MatSetType(C,MATDENSE);
183:     MatSeqDenseSetPreallocation(C,NULL);
184:     MatMPIDenseSetPreallocation(C,NULL);
185:   }

187:   PetscNew(&contents);
188:   PetscContainerCreate(PetscObjectComm((PetscObject)A),&container);
189:   PetscContainerSetPointer(container,contents);
190:   PetscContainerSetUserDestroy(container,MatNest_DenseDestroy);
191:   PetscObjectCompose((PetscObject)C,"workC",(PetscObject)container);
192:   PetscContainerDestroy(&container);
193:   PetscCalloc3(nr+1,&contents->dm,nc+1,&contents->dn,nr*nc,&contents->workC);
194:   contents->k = nr*nc;
195:   for (i=0; i<nr; i++) {
196:     ISGetLocalSize(bA->isglobal.row[i],contents->dm + i+1);
197:     maxm = PetscMax(maxm,contents->dm[i+1]);
198:     contents->dm[i+1] += contents->dm[i];
199:   }
200:   for (i=0; i<nc; i++) {
201:     ISGetLocalSize(bA->isglobal.col[i],contents->dn + i+1);
202:     contents->dn[i+1] += contents->dn[i];
203:   }
204:   PetscMalloc1(maxm*N,&contents->tarray);
205:   MatDenseGetLDA(B,&ldb);
206:   MatGetSize(B,NULL,&N);
207:   MatDenseGetArrayRead(B,&barray);
208:   /* loops are permuted compared to MatMatMultNumeric so that viewB is created only once per column of A */
209:   for (j=0; j<nc; j++) {
210:     ISGetSize(bA->isglobal.col[j],&M);
211:     MatCreateDense(PetscObjectComm((PetscObject)A),contents->dn[j+1]-contents->dn[j],PETSC_DECIDE,M,N,(PetscScalar*)(barray+contents->dn[j]),&viewB);
212:     MatDenseGetLocalMatrix(viewB,&viewSeq);
213:     MatSeqDenseSetLDA(viewSeq,ldb);
214:     for (i=0; i<nr; i++) {
215:       if (!bA->m[i][j]) continue;
216:       /* MatMatMultSymbolic may attach a specific container (depending on MatType of bA->m[i][j]) to workC[i][j] */

218:       MatProductCreate(bA->m[i][j],viewB,NULL,&contents->workC[i*nc + j]);
219:       workC = contents->workC[i*nc + j];
220:       MatProductSetType(workC,MATPRODUCT_AB);
221:       MatProductSetAlgorithm(workC,"default");
222:       MatProductSetFill(workC,fill);
223:       MatProductSetFromOptions(workC);
224:       MatProductSymbolic(workC);

226:       MatDenseGetLocalMatrix(workC,&viewSeq);
227:       /* free the memory allocated in MatMatMultSymbolic, since tarray will be shared by all Mat */
228:       MatSeqDenseSetPreallocation(viewSeq,contents->tarray);
229:     }
230:     MatDestroy(&viewB);
231:   }
232:   MatDenseRestoreArrayRead(B,&barray);

234:   C->ops->matmultnumeric = MatMatMultNumeric_Nest_Dense;
235:   return(0);
236: }

238: /* --------------------------------------------------------- */
239: static PetscErrorCode MatProductSetFromOptions_Nest_Dense_AB(Mat C)
240: {
242:   C->ops->matmultsymbolic = MatMatMultSymbolic_Nest_Dense;
243:   C->ops->productsymbolic = MatProductSymbolic_AB;
244:   return(0);
245: }

247: PETSC_INTERN PetscErrorCode MatProductSetFromOptions_Nest_Dense(Mat C)
248: {
250:   Mat_Product    *product = C->product;

253:   if (product->type == MATPRODUCT_AB) {
254:     MatProductSetFromOptions_Nest_Dense_AB(C);
255:   } else SETERRQ(PetscObjectComm((PetscObject)C),PETSC_ERR_SUP,"MatProduct type is not supported");
256:   return(0);
257: }
258: /* --------------------------------------------------------- */

260: static PetscErrorCode MatMultTranspose_Nest(Mat A,Vec x,Vec y)
261: {
262:   Mat_Nest       *bA = (Mat_Nest*)A->data;
263:   Vec            *bx = bA->left,*by = bA->right;
264:   PetscInt       i,j,nr = bA->nr,nc = bA->nc;

268:   for (i=0; i<nr; i++) {VecGetSubVector(x,bA->isglobal.row[i],&bx[i]);}
269:   for (i=0; i<nc; i++) {VecGetSubVector(y,bA->isglobal.col[i],&by[i]);}
270:   for (j=0; j<nc; j++) {
271:     VecZeroEntries(by[j]);
272:     for (i=0; i<nr; i++) {
273:       if (!bA->m[i][j]) continue;
274:       /* y[j] <- y[j] + (A[i][j])^T * x[i] */
275:       MatMultTransposeAdd(bA->m[i][j],bx[i],by[j],by[j]);
276:     }
277:   }
278:   for (i=0; i<nr; i++) {VecRestoreSubVector(x,bA->isglobal.row[i],&bx[i]);}
279:   for (i=0; i<nc; i++) {VecRestoreSubVector(y,bA->isglobal.col[i],&by[i]);}
280:   return(0);
281: }

283: static PetscErrorCode MatMultTransposeAdd_Nest(Mat A,Vec x,Vec y,Vec z)
284: {
285:   Mat_Nest       *bA = (Mat_Nest*)A->data;
286:   Vec            *bx = bA->left,*bz = bA->right;
287:   PetscInt       i,j,nr = bA->nr,nc = bA->nc;

291:   for (i=0; i<nr; i++) {VecGetSubVector(x,bA->isglobal.row[i],&bx[i]);}
292:   for (i=0; i<nc; i++) {VecGetSubVector(z,bA->isglobal.col[i],&bz[i]);}
293:   for (j=0; j<nc; j++) {
294:     if (y != z) {
295:       Vec by;
296:       VecGetSubVector(y,bA->isglobal.col[j],&by);
297:       VecCopy(by,bz[j]);
298:       VecRestoreSubVector(y,bA->isglobal.col[j],&by);
299:     }
300:     for (i=0; i<nr; i++) {
301:       if (!bA->m[i][j]) continue;
302:       /* z[j] <- y[j] + (A[i][j])^T * x[i] */
303:       MatMultTransposeAdd(bA->m[i][j],bx[i],bz[j],bz[j]);
304:     }
305:   }
306:   for (i=0; i<nr; i++) {VecRestoreSubVector(x,bA->isglobal.row[i],&bx[i]);}
307:   for (i=0; i<nc; i++) {VecRestoreSubVector(z,bA->isglobal.col[i],&bz[i]);}
308:   return(0);
309: }

311: static PetscErrorCode MatTranspose_Nest(Mat A,MatReuse reuse,Mat *B)
312: {
313:   Mat_Nest       *bA = (Mat_Nest*)A->data, *bC;
314:   Mat            C;
315:   PetscInt       i,j,nr = bA->nr,nc = bA->nc;

319:   if (reuse == MAT_INPLACE_MATRIX && nr != nc) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_SIZ,"Square nested matrix only for in-place");

321:   if (reuse == MAT_INITIAL_MATRIX || reuse == MAT_INPLACE_MATRIX) {
322:     Mat *subs;
323:     IS  *is_row,*is_col;

325:     PetscCalloc1(nr * nc,&subs);
326:     PetscMalloc2(nr,&is_row,nc,&is_col);
327:     MatNestGetISs(A,is_row,is_col);
328:     if (reuse == MAT_INPLACE_MATRIX) {
329:       for (i=0; i<nr; i++) {
330:         for (j=0; j<nc; j++) {
331:           subs[i + nr * j] = bA->m[i][j];
332:         }
333:       }
334:     }

336:     MatCreateNest(PetscObjectComm((PetscObject)A),nc,is_col,nr,is_row,subs,&C);
337:     PetscFree(subs);
338:     PetscFree2(is_row,is_col);
339:   } else {
340:     C = *B;
341:   }

343:   bC = (Mat_Nest*)C->data;
344:   for (i=0; i<nr; i++) {
345:     for (j=0; j<nc; j++) {
346:       if (bA->m[i][j]) {
347:         MatTranspose(bA->m[i][j], reuse, &(bC->m[j][i]));
348:       } else {
349:         bC->m[j][i] = NULL;
350:       }
351:     }
352:   }

354:   if (reuse == MAT_INITIAL_MATRIX || reuse == MAT_REUSE_MATRIX) {
355:     *B = C;
356:   } else {
357:     MatHeaderMerge(A, &C);
358:   }
359:   return(0);
360: }

362: static PetscErrorCode MatNestDestroyISList(PetscInt n,IS **list)
363: {
365:   IS             *lst = *list;
366:   PetscInt       i;

369:   if (!lst) return(0);
370:   for (i=0; i<n; i++) if (lst[i]) {ISDestroy(&lst[i]);}
371:   PetscFree(lst);
372:   *list = NULL;
373:   return(0);
374: }

376: static PetscErrorCode MatReset_Nest(Mat A)
377: {
378:   Mat_Nest       *vs = (Mat_Nest*)A->data;
379:   PetscInt       i,j;

383:   /* release the matrices and the place holders */
384:   MatNestDestroyISList(vs->nr,&vs->isglobal.row);
385:   MatNestDestroyISList(vs->nc,&vs->isglobal.col);
386:   MatNestDestroyISList(vs->nr,&vs->islocal.row);
387:   MatNestDestroyISList(vs->nc,&vs->islocal.col);

389:   PetscFree(vs->row_len);
390:   PetscFree(vs->col_len);
391:   PetscFree(vs->nnzstate);

393:   PetscFree2(vs->left,vs->right);

395:   /* release the matrices and the place holders */
396:   if (vs->m) {
397:     for (i=0; i<vs->nr; i++) {
398:       for (j=0; j<vs->nc; j++) {
399:         MatDestroy(&vs->m[i][j]);
400:       }
401:       PetscFree(vs->m[i]);
402:     }
403:     PetscFree(vs->m);
404:   }

406:   /* restore defaults */
407:   vs->nr = 0;
408:   vs->nc = 0;
409:   vs->splitassembly = PETSC_FALSE;
410:   return(0);
411: }

413: static PetscErrorCode MatDestroy_Nest(Mat A)
414: {

417:   MatReset_Nest(A);
418:   PetscFree(A->data);

420:   PetscObjectComposeFunction((PetscObject)A,"MatNestGetSubMat_C",0);
421:   PetscObjectComposeFunction((PetscObject)A,"MatNestSetSubMat_C",0);
422:   PetscObjectComposeFunction((PetscObject)A,"MatNestGetSubMats_C",0);
423:   PetscObjectComposeFunction((PetscObject)A,"MatNestGetSize_C",0);
424:   PetscObjectComposeFunction((PetscObject)A,"MatNestGetISs_C",0);
425:   PetscObjectComposeFunction((PetscObject)A,"MatNestGetLocalISs_C",0);
426:   PetscObjectComposeFunction((PetscObject)A,"MatNestSetVecType_C",0);
427:   PetscObjectComposeFunction((PetscObject)A,"MatNestSetSubMats_C",0);
428:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_nest_mpiaij_C",0);
429:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_nest_seqaij_C",0);
430:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_nest_aij_C",0);
431:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_nest_is_C",0);
432:   PetscObjectComposeFunction((PetscObject)A,"MatProductSetFromOptions_nest_seqdense_C",NULL);
433:   PetscObjectComposeFunction((PetscObject)A,"MatProductSetFromOptions_nest_mpidense_C",NULL);
434:   PetscObjectComposeFunction((PetscObject)A,"MatProductSetFromOptions_nest_dense_C",NULL);
435:   return(0);
436: }

438: static PetscErrorCode MatMissingDiagonal_Nest(Mat mat,PetscBool *missing,PetscInt *dd)
439: {
440:   Mat_Nest       *vs = (Mat_Nest*)mat->data;
441:   PetscInt       i;

445:   if (dd) *dd = 0;
446:   if (!vs->nr) {
447:     *missing = PETSC_TRUE;
448:     return(0);
449:   }
450:   *missing = PETSC_FALSE;
451:   for (i = 0; i < vs->nr && !(*missing); i++) {
452:     *missing = PETSC_TRUE;
453:     if (vs->m[i][i]) {
454:       MatMissingDiagonal(vs->m[i][i],missing,NULL);
455:       if (*missing && dd) SETERRQ(PetscObjectComm((PetscObject)mat),PETSC_ERR_SUP,"First missing entry not yet implemented");
456:     }
457:   }
458:   return(0);
459: }

461: static PetscErrorCode MatAssemblyBegin_Nest(Mat A,MatAssemblyType type)
462: {
463:   Mat_Nest       *vs = (Mat_Nest*)A->data;
464:   PetscInt       i,j;
466:   PetscBool      nnzstate = PETSC_FALSE;

469:   for (i=0; i<vs->nr; i++) {
470:     for (j=0; j<vs->nc; j++) {
471:       PetscObjectState subnnzstate = 0;
472:       if (vs->m[i][j]) {
473:         MatAssemblyBegin(vs->m[i][j],type);
474:         if (!vs->splitassembly) {
475:           /* Note: split assembly will fail if the same block appears more than once (even indirectly through a nested
476:            * sub-block). This could be fixed by adding a flag to Mat so that there was a way to check if a Mat was
477:            * already performing an assembly, but the result would by more complicated and appears to offer less
478:            * potential for diagnostics and correctness checking. Split assembly should be fixed once there is an
479:            * interface for libraries to make asynchronous progress in "user-defined non-blocking collectives".
480:            */
481:           MatAssemblyEnd(vs->m[i][j],type);
482:           MatGetNonzeroState(vs->m[i][j],&subnnzstate);
483:         }
484:       }
485:       nnzstate = (PetscBool)(nnzstate || vs->nnzstate[i*vs->nc+j] != subnnzstate);
486:       vs->nnzstate[i*vs->nc+j] = subnnzstate;
487:     }
488:   }
489:   if (nnzstate) A->nonzerostate++;
490:   return(0);
491: }

493: static PetscErrorCode MatAssemblyEnd_Nest(Mat A, MatAssemblyType type)
494: {
495:   Mat_Nest       *vs = (Mat_Nest*)A->data;
496:   PetscInt       i,j;

500:   for (i=0; i<vs->nr; i++) {
501:     for (j=0; j<vs->nc; j++) {
502:       if (vs->m[i][j]) {
503:         if (vs->splitassembly) {
504:           MatAssemblyEnd(vs->m[i][j],type);
505:         }
506:       }
507:     }
508:   }
509:   return(0);
510: }

512: static PetscErrorCode MatNestFindNonzeroSubMatRow(Mat A,PetscInt row,Mat *B)
513: {
515:   Mat_Nest       *vs = (Mat_Nest*)A->data;
516:   PetscInt       j;
517:   Mat            sub;

520:   sub = (row < vs->nc) ? vs->m[row][row] : (Mat)NULL; /* Prefer to find on the diagonal */
521:   for (j=0; !sub && j<vs->nc; j++) sub = vs->m[row][j];
522:   if (sub) {MatSetUp(sub);}       /* Ensure that the sizes are available */
523:   *B = sub;
524:   return(0);
525: }

527: static PetscErrorCode MatNestFindNonzeroSubMatCol(Mat A,PetscInt col,Mat *B)
528: {
530:   Mat_Nest       *vs = (Mat_Nest*)A->data;
531:   PetscInt       i;
532:   Mat            sub;

535:   sub = (col < vs->nr) ? vs->m[col][col] : (Mat)NULL; /* Prefer to find on the diagonal */
536:   for (i=0; !sub && i<vs->nr; i++) sub = vs->m[i][col];
537:   if (sub) {MatSetUp(sub);}       /* Ensure that the sizes are available */
538:   *B = sub;
539:   return(0);
540: }

542: static PetscErrorCode MatNestFindIS(Mat A,PetscInt n,const IS list[],IS is,PetscInt *found)
543: {
545:   PetscInt       i;
546:   PetscBool      flg;

552:   *found = -1;
553:   for (i=0; i<n; i++) {
554:     if (!list[i]) continue;
555:     ISEqualUnsorted(list[i],is,&flg);
556:     if (flg) {
557:       *found = i;
558:       return(0);
559:     }
560:   }
561:   SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_INCOMP,"Could not find index set");
562:   return(0);
563: }

565: /* Get a block row as a new MatNest */
566: static PetscErrorCode MatNestGetRow(Mat A,PetscInt row,Mat *B)
567: {
568:   Mat_Nest       *vs = (Mat_Nest*)A->data;
569:   char           keyname[256];

573:   *B   = NULL;
574:   PetscSNPrintf(keyname,sizeof(keyname),"NestRow_%D",row);
575:   PetscObjectQuery((PetscObject)A,keyname,(PetscObject*)B);
576:   if (*B) return(0);

578:   MatCreateNest(PetscObjectComm((PetscObject)A),1,NULL,vs->nc,vs->isglobal.col,vs->m[row],B);

580:   (*B)->assembled = A->assembled;

582:   PetscObjectCompose((PetscObject)A,keyname,(PetscObject)*B);
583:   PetscObjectDereference((PetscObject)*B); /* Leave the only remaining reference in the composition */
584:   return(0);
585: }

587: static PetscErrorCode MatNestFindSubMat(Mat A,struct MatNestISPair *is,IS isrow,IS iscol,Mat *B)
588: {
589:   Mat_Nest       *vs = (Mat_Nest*)A->data;
591:   PetscInt       row,col;
592:   PetscBool      same,isFullCol,isFullColGlobal;

595:   /* Check if full column space. This is a hack */
596:   isFullCol = PETSC_FALSE;
597:   PetscObjectTypeCompare((PetscObject)iscol,ISSTRIDE,&same);
598:   if (same) {
599:     PetscInt n,first,step,i,an,am,afirst,astep;
600:     ISStrideGetInfo(iscol,&first,&step);
601:     ISGetLocalSize(iscol,&n);
602:     isFullCol = PETSC_TRUE;
603:     for (i=0,an=A->cmap->rstart; i<vs->nc; i++) {
604:       PetscObjectTypeCompare((PetscObject)is->col[i],ISSTRIDE,&same);
605:       ISGetLocalSize(is->col[i],&am);
606:       if (same) {
607:         ISStrideGetInfo(is->col[i],&afirst,&astep);
608:         if (afirst != an || astep != step) isFullCol = PETSC_FALSE;
609:       } else isFullCol = PETSC_FALSE;
610:       an += am;
611:     }
612:     if (an != A->cmap->rstart+n) isFullCol = PETSC_FALSE;
613:   }
614:   MPIU_Allreduce(&isFullCol,&isFullColGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)iscol));

616:   if (isFullColGlobal && vs->nc > 1) {
617:     PetscInt row;
618:     MatNestFindIS(A,vs->nr,is->row,isrow,&row);
619:     MatNestGetRow(A,row,B);
620:   } else {
621:     MatNestFindIS(A,vs->nr,is->row,isrow,&row);
622:     MatNestFindIS(A,vs->nc,is->col,iscol,&col);
623:     if (!vs->m[row][col]) {
624:       PetscInt lr,lc;

626:       MatCreate(PetscObjectComm((PetscObject)A),&vs->m[row][col]);
627:       ISGetLocalSize(vs->isglobal.row[row],&lr);
628:       ISGetLocalSize(vs->isglobal.col[col],&lc);
629:       MatSetSizes(vs->m[row][col],lr,lc,PETSC_DECIDE,PETSC_DECIDE);
630:       MatSetType(vs->m[row][col],MATAIJ);
631:       MatSeqAIJSetPreallocation(vs->m[row][col],0,NULL);
632:       MatMPIAIJSetPreallocation(vs->m[row][col],0,NULL,0,NULL);
633:       MatSetUp(vs->m[row][col]);
634:       MatAssemblyBegin(vs->m[row][col],MAT_FINAL_ASSEMBLY);
635:       MatAssemblyEnd(vs->m[row][col],MAT_FINAL_ASSEMBLY);
636:     }
637:     *B = vs->m[row][col];
638:   }
639:   return(0);
640: }

642: /*
643:    TODO: This does not actually returns a submatrix we can modify
644: */
645: static PetscErrorCode MatCreateSubMatrix_Nest(Mat A,IS isrow,IS iscol,MatReuse reuse,Mat *B)
646: {
648:   Mat_Nest       *vs = (Mat_Nest*)A->data;
649:   Mat            sub;

652:   MatNestFindSubMat(A,&vs->isglobal,isrow,iscol,&sub);
653:   switch (reuse) {
654:   case MAT_INITIAL_MATRIX:
655:     if (sub) { PetscObjectReference((PetscObject)sub); }
656:     *B = sub;
657:     break;
658:   case MAT_REUSE_MATRIX:
659:     if (sub != *B) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Submatrix was not used before in this call");
660:     break;
661:   case MAT_IGNORE_MATRIX:       /* Nothing to do */
662:     break;
663:   case MAT_INPLACE_MATRIX:       /* Nothing to do */
664:     SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"MAT_INPLACE_MATRIX is not supported yet");
665:     break;
666:   }
667:   return(0);
668: }

670: PetscErrorCode MatGetLocalSubMatrix_Nest(Mat A,IS isrow,IS iscol,Mat *B)
671: {
673:   Mat_Nest       *vs = (Mat_Nest*)A->data;
674:   Mat            sub;

677:   MatNestFindSubMat(A,&vs->islocal,isrow,iscol,&sub);
678:   /* We allow the submatrix to be NULL, perhaps it would be better for the user to return an empty matrix instead */
679:   if (sub) {PetscObjectReference((PetscObject)sub);}
680:   *B = sub;
681:   return(0);
682: }

684: static PetscErrorCode MatRestoreLocalSubMatrix_Nest(Mat A,IS isrow,IS iscol,Mat *B)
685: {
687:   Mat_Nest       *vs = (Mat_Nest*)A->data;
688:   Mat            sub;

691:   MatNestFindSubMat(A,&vs->islocal,isrow,iscol,&sub);
692:   if (*B != sub) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Local submatrix has not been gotten");
693:   if (sub) {
694:     if (((PetscObject)sub)->refct <= 1) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Local submatrix has had reference count decremented too many times");
695:     MatDestroy(B);
696:   }
697:   return(0);
698: }

700: static PetscErrorCode MatGetDiagonal_Nest(Mat A,Vec v)
701: {
702:   Mat_Nest       *bA = (Mat_Nest*)A->data;
703:   PetscInt       i;

707:   for (i=0; i<bA->nr; i++) {
708:     Vec bv;
709:     VecGetSubVector(v,bA->isglobal.row[i],&bv);
710:     if (bA->m[i][i]) {
711:       MatGetDiagonal(bA->m[i][i],bv);
712:     } else {
713:       VecSet(bv,0.0);
714:     }
715:     VecRestoreSubVector(v,bA->isglobal.row[i],&bv);
716:   }
717:   return(0);
718: }

720: static PetscErrorCode MatDiagonalScale_Nest(Mat A,Vec l,Vec r)
721: {
722:   Mat_Nest       *bA = (Mat_Nest*)A->data;
723:   Vec            bl,*br;
724:   PetscInt       i,j;

728:   PetscCalloc1(bA->nc,&br);
729:   if (r) {
730:     for (j=0; j<bA->nc; j++) {VecGetSubVector(r,bA->isglobal.col[j],&br[j]);}
731:   }
732:   bl = NULL;
733:   for (i=0; i<bA->nr; i++) {
734:     if (l) {
735:       VecGetSubVector(l,bA->isglobal.row[i],&bl);
736:     }
737:     for (j=0; j<bA->nc; j++) {
738:       if (bA->m[i][j]) {
739:         MatDiagonalScale(bA->m[i][j],bl,br[j]);
740:       }
741:     }
742:     if (l) {
743:       VecRestoreSubVector(l,bA->isglobal.row[i],&bl);
744:     }
745:   }
746:   if (r) {
747:     for (j=0; j<bA->nc; j++) {VecRestoreSubVector(r,bA->isglobal.col[j],&br[j]);}
748:   }
749:   PetscFree(br);
750:   return(0);
751: }

753: static PetscErrorCode MatScale_Nest(Mat A,PetscScalar a)
754: {
755:   Mat_Nest       *bA = (Mat_Nest*)A->data;
756:   PetscInt       i,j;

760:   for (i=0; i<bA->nr; i++) {
761:     for (j=0; j<bA->nc; j++) {
762:       if (bA->m[i][j]) {
763:         MatScale(bA->m[i][j],a);
764:       }
765:     }
766:   }
767:   return(0);
768: }

770: static PetscErrorCode MatShift_Nest(Mat A,PetscScalar a)
771: {
772:   Mat_Nest       *bA = (Mat_Nest*)A->data;
773:   PetscInt       i;
775:   PetscBool      nnzstate = PETSC_FALSE;

778:   for (i=0; i<bA->nr; i++) {
779:     PetscObjectState subnnzstate = 0;
780:     if (!bA->m[i][i]) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"No support for shifting an empty diagonal block, insert a matrix in block (%D,%D)",i,i);
781:     MatShift(bA->m[i][i],a);
782:     MatGetNonzeroState(bA->m[i][i],&subnnzstate);
783:     nnzstate = (PetscBool)(nnzstate || bA->nnzstate[i*bA->nc+i] != subnnzstate);
784:     bA->nnzstate[i*bA->nc+i] = subnnzstate;
785:   }
786:   if (nnzstate) A->nonzerostate++;
787:   return(0);
788: }

790: static PetscErrorCode MatDiagonalSet_Nest(Mat A,Vec D,InsertMode is)
791: {
792:   Mat_Nest       *bA = (Mat_Nest*)A->data;
793:   PetscInt       i;
795:   PetscBool      nnzstate = PETSC_FALSE;

798:   for (i=0; i<bA->nr; i++) {
799:     PetscObjectState subnnzstate = 0;
800:     Vec              bv;
801:     VecGetSubVector(D,bA->isglobal.row[i],&bv);
802:     if (bA->m[i][i]) {
803:       MatDiagonalSet(bA->m[i][i],bv,is);
804:       MatGetNonzeroState(bA->m[i][i],&subnnzstate);
805:     }
806:     VecRestoreSubVector(D,bA->isglobal.row[i],&bv);
807:     nnzstate = (PetscBool)(nnzstate || bA->nnzstate[i*bA->nc+i] != subnnzstate);
808:     bA->nnzstate[i*bA->nc+i] = subnnzstate;
809:   }
810:   if (nnzstate) A->nonzerostate++;
811:   return(0);
812: }

814: static PetscErrorCode MatSetRandom_Nest(Mat A,PetscRandom rctx)
815: {
816:   Mat_Nest       *bA = (Mat_Nest*)A->data;
817:   PetscInt       i,j;

821:   for (i=0; i<bA->nr; i++) {
822:     for (j=0; j<bA->nc; j++) {
823:       if (bA->m[i][j]) {
824:         MatSetRandom(bA->m[i][j],rctx);
825:       }
826:     }
827:   }
828:   return(0);
829: }

831: static PetscErrorCode MatCreateVecs_Nest(Mat A,Vec *right,Vec *left)
832: {
833:   Mat_Nest       *bA = (Mat_Nest*)A->data;
834:   Vec            *L,*R;
835:   MPI_Comm       comm;
836:   PetscInt       i,j;

840:   PetscObjectGetComm((PetscObject)A,&comm);
841:   if (right) {
842:     /* allocate R */
843:     PetscMalloc1(bA->nc, &R);
844:     /* Create the right vectors */
845:     for (j=0; j<bA->nc; j++) {
846:       for (i=0; i<bA->nr; i++) {
847:         if (bA->m[i][j]) {
848:           MatCreateVecs(bA->m[i][j],&R[j],NULL);
849:           break;
850:         }
851:       }
852:       if (i==bA->nr) SETERRQ(PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Mat(Nest) contains a null column.");
853:     }
854:     VecCreateNest(comm,bA->nc,bA->isglobal.col,R,right);
855:     /* hand back control to the nest vector */
856:     for (j=0; j<bA->nc; j++) {
857:       VecDestroy(&R[j]);
858:     }
859:     PetscFree(R);
860:   }

862:   if (left) {
863:     /* allocate L */
864:     PetscMalloc1(bA->nr, &L);
865:     /* Create the left vectors */
866:     for (i=0; i<bA->nr; i++) {
867:       for (j=0; j<bA->nc; j++) {
868:         if (bA->m[i][j]) {
869:           MatCreateVecs(bA->m[i][j],NULL,&L[i]);
870:           break;
871:         }
872:       }
873:       if (j==bA->nc) SETERRQ(PetscObjectComm((PetscObject)A), PETSC_ERR_ARG_WRONG, "Mat(Nest) contains a null row.");
874:     }

876:     VecCreateNest(comm,bA->nr,bA->isglobal.row,L,left);
877:     for (i=0; i<bA->nr; i++) {
878:       VecDestroy(&L[i]);
879:     }

881:     PetscFree(L);
882:   }
883:   return(0);
884: }

886: static PetscErrorCode MatView_Nest(Mat A,PetscViewer viewer)
887: {
888:   Mat_Nest       *bA = (Mat_Nest*)A->data;
889:   PetscBool      isascii,viewSub = PETSC_FALSE;
890:   PetscInt       i,j;

894:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&isascii);
895:   if (isascii) {

897:     PetscOptionsGetBool(((PetscObject)A)->options,((PetscObject)A)->prefix,"-mat_view_nest_sub",&viewSub,NULL);
898:     PetscViewerASCIIPrintf(viewer,"Matrix object: \n");
899:     PetscViewerASCIIPushTab(viewer);
900:     PetscViewerASCIIPrintf(viewer, "type=nest, rows=%D, cols=%D \n",bA->nr,bA->nc);

902:     PetscViewerASCIIPrintf(viewer,"MatNest structure: \n");
903:     for (i=0; i<bA->nr; i++) {
904:       for (j=0; j<bA->nc; j++) {
905:         MatType   type;
906:         char      name[256] = "",prefix[256] = "";
907:         PetscInt  NR,NC;
908:         PetscBool isNest = PETSC_FALSE;

910:         if (!bA->m[i][j]) {
911:           PetscViewerASCIIPrintf(viewer, "(%D,%D) : NULL \n",i,j);
912:           continue;
913:         }
914:         MatGetSize(bA->m[i][j],&NR,&NC);
915:         MatGetType(bA->m[i][j], &type);
916:         if (((PetscObject)bA->m[i][j])->name) {PetscSNPrintf(name,sizeof(name),"name=\"%s\", ",((PetscObject)bA->m[i][j])->name);}
917:         if (((PetscObject)bA->m[i][j])->prefix) {PetscSNPrintf(prefix,sizeof(prefix),"prefix=\"%s\", ",((PetscObject)bA->m[i][j])->prefix);}
918:         PetscObjectTypeCompare((PetscObject)bA->m[i][j],MATNEST,&isNest);

920:         PetscViewerASCIIPrintf(viewer,"(%D,%D) : %s%stype=%s, rows=%D, cols=%D \n",i,j,name,prefix,type,NR,NC);

922:         if (isNest || viewSub) {
923:           PetscViewerASCIIPushTab(viewer);  /* push1 */
924:           MatView(bA->m[i][j],viewer);
925:           PetscViewerASCIIPopTab(viewer);    /* pop1 */
926:         }
927:       }
928:     }
929:     PetscViewerASCIIPopTab(viewer);    /* pop0 */
930:   }
931:   return(0);
932: }

934: static PetscErrorCode MatZeroEntries_Nest(Mat A)
935: {
936:   Mat_Nest       *bA = (Mat_Nest*)A->data;
937:   PetscInt       i,j;

941:   for (i=0; i<bA->nr; i++) {
942:     for (j=0; j<bA->nc; j++) {
943:       if (!bA->m[i][j]) continue;
944:       MatZeroEntries(bA->m[i][j]);
945:     }
946:   }
947:   return(0);
948: }

950: static PetscErrorCode MatCopy_Nest(Mat A,Mat B,MatStructure str)
951: {
952:   Mat_Nest       *bA = (Mat_Nest*)A->data,*bB = (Mat_Nest*)B->data;
953:   PetscInt       i,j,nr = bA->nr,nc = bA->nc;
955:   PetscBool      nnzstate = PETSC_FALSE;

958:   if (nr != bB->nr || nc != bB->nc) SETERRQ4(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_INCOMP,"Cannot copy a Mat_Nest of block size (%D,%D) to a Mat_Nest of block size (%D,%D)",bB->nr,bB->nc,nr,nc);
959:   for (i=0; i<nr; i++) {
960:     for (j=0; j<nc; j++) {
961:       PetscObjectState subnnzstate = 0;
962:       if (bA->m[i][j] && bB->m[i][j]) {
963:         MatCopy(bA->m[i][j],bB->m[i][j],str);
964:       } else if (bA->m[i][j] || bB->m[i][j]) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_INCOMP,"Matrix block does not exist at %D,%D",i,j);
965:       MatGetNonzeroState(bB->m[i][j],&subnnzstate);
966:       nnzstate = (PetscBool)(nnzstate || bB->nnzstate[i*nc+j] != subnnzstate);
967:       bB->nnzstate[i*nc+j] = subnnzstate;
968:     }
969:   }
970:   if (nnzstate) B->nonzerostate++;
971:   return(0);
972: }

974: static PetscErrorCode MatAXPY_Nest(Mat Y,PetscScalar a,Mat X,MatStructure str)
975: {
976:   Mat_Nest       *bY = (Mat_Nest*)Y->data,*bX = (Mat_Nest*)X->data;
977:   PetscInt       i,j,nr = bY->nr,nc = bY->nc;
979:   PetscBool      nnzstate = PETSC_FALSE;

982:   if (nr != bX->nr || nc != bX->nc) SETERRQ4(PetscObjectComm((PetscObject)Y),PETSC_ERR_ARG_INCOMP,"Cannot AXPY a MatNest of block size (%D,%D) with a MatNest of block size (%D,%D)",bX->nr,bX->nc,nr,nc);
983:   for (i=0; i<nr; i++) {
984:     for (j=0; j<nc; j++) {
985:       PetscObjectState subnnzstate = 0;
986:       if (bY->m[i][j] && bX->m[i][j]) {
987:         MatAXPY(bY->m[i][j],a,bX->m[i][j],str);
988:       } else if (bX->m[i][j]) {
989:         Mat M;

991:         if (str != DIFFERENT_NONZERO_PATTERN) SETERRQ2(PetscObjectComm((PetscObject)Y),PETSC_ERR_ARG_INCOMP,"Matrix block does not exist at %D,%D. Use DIFFERENT_NONZERO_PATTERN",i,j);
992:         MatDuplicate(bX->m[i][j],MAT_COPY_VALUES,&M);
993:         MatNestSetSubMat(Y,i,j,M);
994:         MatDestroy(&M);
995:       }
996:       if (bY->m[i][j]) { MatGetNonzeroState(bY->m[i][j],&subnnzstate); }
997:       nnzstate = (PetscBool)(nnzstate || bY->nnzstate[i*nc+j] != subnnzstate);
998:       bY->nnzstate[i*nc+j] = subnnzstate;
999:     }
1000:   }
1001:   if (nnzstate) Y->nonzerostate++;
1002:   return(0);
1003: }

1005: static PetscErrorCode MatDuplicate_Nest(Mat A,MatDuplicateOption op,Mat *B)
1006: {
1007:   Mat_Nest       *bA = (Mat_Nest*)A->data;
1008:   Mat            *b;
1009:   PetscInt       i,j,nr = bA->nr,nc = bA->nc;

1013:   PetscMalloc1(nr*nc,&b);
1014:   for (i=0; i<nr; i++) {
1015:     for (j=0; j<nc; j++) {
1016:       if (bA->m[i][j]) {
1017:         MatDuplicate(bA->m[i][j],op,&b[i*nc+j]);
1018:       } else {
1019:         b[i*nc+j] = NULL;
1020:       }
1021:     }
1022:   }
1023:   MatCreateNest(PetscObjectComm((PetscObject)A),nr,bA->isglobal.row,nc,bA->isglobal.col,b,B);
1024:   /* Give the new MatNest exclusive ownership */
1025:   for (i=0; i<nr*nc; i++) {
1026:     MatDestroy(&b[i]);
1027:   }
1028:   PetscFree(b);

1030:   MatAssemblyBegin(*B,MAT_FINAL_ASSEMBLY);
1031:   MatAssemblyEnd(*B,MAT_FINAL_ASSEMBLY);
1032:   return(0);
1033: }

1035: /* nest api */
1036: PetscErrorCode MatNestGetSubMat_Nest(Mat A,PetscInt idxm,PetscInt jdxm,Mat *mat)
1037: {
1038:   Mat_Nest *bA = (Mat_Nest*)A->data;

1041:   if (idxm >= bA->nr) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",idxm,bA->nr-1);
1042:   if (jdxm >= bA->nc) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Col too large: row %D max %D",jdxm,bA->nc-1);
1043:   *mat = bA->m[idxm][jdxm];
1044:   return(0);
1045: }

1047: /*@
1048:  MatNestGetSubMat - Returns a single, sub-matrix from a nest matrix.

1050:  Not collective

1052:  Input Parameters:
1053: +   A  - nest matrix
1054: .   idxm - index of the matrix within the nest matrix
1055: -   jdxm - index of the matrix within the nest matrix

1057:  Output Parameter:
1058: .   sub - matrix at index idxm,jdxm within the nest matrix

1060:  Level: developer

1062: .seealso: MatNestGetSize(), MatNestGetSubMats(), MatNestCreate(), MATNEST, MatNestSetSubMat(),
1063:           MatNestGetLocalISs(), MatNestGetISs()
1064: @*/
1065: PetscErrorCode  MatNestGetSubMat(Mat A,PetscInt idxm,PetscInt jdxm,Mat *sub)
1066: {

1070:   PetscUseMethod(A,"MatNestGetSubMat_C",(Mat,PetscInt,PetscInt,Mat*),(A,idxm,jdxm,sub));
1071:   return(0);
1072: }

1074: PetscErrorCode MatNestSetSubMat_Nest(Mat A,PetscInt idxm,PetscInt jdxm,Mat mat)
1075: {
1076:   Mat_Nest       *bA = (Mat_Nest*)A->data;
1077:   PetscInt       m,n,M,N,mi,ni,Mi,Ni;

1081:   if (idxm >= bA->nr) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Row too large: row %D max %D",idxm,bA->nr-1);
1082:   if (jdxm >= bA->nc) SETERRQ2(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Col too large: row %D max %D",jdxm,bA->nc-1);
1083:   MatGetLocalSize(mat,&m,&n);
1084:   MatGetSize(mat,&M,&N);
1085:   ISGetLocalSize(bA->isglobal.row[idxm],&mi);
1086:   ISGetSize(bA->isglobal.row[idxm],&Mi);
1087:   ISGetLocalSize(bA->isglobal.col[jdxm],&ni);
1088:   ISGetSize(bA->isglobal.col[jdxm],&Ni);
1089:   if (M != Mi || N != Ni) SETERRQ4(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_INCOMP,"Submatrix dimension (%D,%D) incompatible with nest block (%D,%D)",M,N,Mi,Ni);
1090:   if (m != mi || n != ni) SETERRQ4(PetscObjectComm((PetscObject)mat),PETSC_ERR_ARG_INCOMP,"Submatrix local dimension (%D,%D) incompatible with nest block (%D,%D)",m,n,mi,ni);

1092:   /* do not increase object state */
1093:   if (mat == bA->m[idxm][jdxm]) return(0);

1095:   PetscObjectReference((PetscObject)mat);
1096:   MatDestroy(&bA->m[idxm][jdxm]);
1097:   bA->m[idxm][jdxm] = mat;
1098:   PetscObjectStateIncrease((PetscObject)A);
1099:   MatGetNonzeroState(mat,&bA->nnzstate[idxm*bA->nc+jdxm]);
1100:   A->nonzerostate++;
1101:   return(0);
1102: }

1104: /*@
1105:  MatNestSetSubMat - Set a single submatrix in the nest matrix.

1107:  Logically collective on the submatrix communicator

1109:  Input Parameters:
1110: +   A  - nest matrix
1111: .   idxm - index of the matrix within the nest matrix
1112: .   jdxm - index of the matrix within the nest matrix
1113: -   sub - matrix at index idxm,jdxm within the nest matrix

1115:  Notes:
1116:  The new submatrix must have the same size and communicator as that block of the nest.

1118:  This increments the reference count of the submatrix.

1120:  Level: developer

1122: .seealso: MatNestSetSubMats(), MatNestGetSubMats(), MatNestGetLocalISs(), MATNEST, MatNestCreate(),
1123:           MatNestGetSubMat(), MatNestGetISs(), MatNestGetSize()
1124: @*/
1125: PetscErrorCode  MatNestSetSubMat(Mat A,PetscInt idxm,PetscInt jdxm,Mat sub)
1126: {

1130:   PetscUseMethod(A,"MatNestSetSubMat_C",(Mat,PetscInt,PetscInt,Mat),(A,idxm,jdxm,sub));
1131:   return(0);
1132: }

1134: PetscErrorCode MatNestGetSubMats_Nest(Mat A,PetscInt *M,PetscInt *N,Mat ***mat)
1135: {
1136:   Mat_Nest *bA = (Mat_Nest*)A->data;

1139:   if (M)   *M   = bA->nr;
1140:   if (N)   *N   = bA->nc;
1141:   if (mat) *mat = bA->m;
1142:   return(0);
1143: }

1145: /*@C
1146:  MatNestGetSubMats - Returns the entire two dimensional array of matrices defining a nest matrix.

1148:  Not collective

1150:  Input Parameters:
1151: .   A  - nest matrix

1153:  Output Parameter:
1154: +   M - number of rows in the nest matrix
1155: .   N - number of cols in the nest matrix
1156: -   mat - 2d array of matrices

1158:  Notes:

1160:  The user should not free the array mat.

1162:  In Fortran, this routine has a calling sequence
1163: $   call MatNestGetSubMats(A, M, N, mat, ierr)
1164:  where the space allocated for the optional argument mat is assumed large enough (if provided).

1166:  Level: developer

1168: .seealso: MatNestGetSize(), MatNestGetSubMat(), MatNestGetLocalISs(), MATNEST, MatNestCreate(),
1169:           MatNestSetSubMats(), MatNestGetISs(), MatNestSetSubMat()
1170: @*/
1171: PetscErrorCode  MatNestGetSubMats(Mat A,PetscInt *M,PetscInt *N,Mat ***mat)
1172: {

1176:   PetscUseMethod(A,"MatNestGetSubMats_C",(Mat,PetscInt*,PetscInt*,Mat***),(A,M,N,mat));
1177:   return(0);
1178: }

1180: PetscErrorCode  MatNestGetSize_Nest(Mat A,PetscInt *M,PetscInt *N)
1181: {
1182:   Mat_Nest *bA = (Mat_Nest*)A->data;

1185:   if (M) *M = bA->nr;
1186:   if (N) *N = bA->nc;
1187:   return(0);
1188: }

1190: /*@
1191:  MatNestGetSize - Returns the size of the nest matrix.

1193:  Not collective

1195:  Input Parameters:
1196: .   A  - nest matrix

1198:  Output Parameter:
1199: +   M - number of rows in the nested mat
1200: -   N - number of cols in the nested mat

1202:  Notes:

1204:  Level: developer

1206: .seealso: MatNestGetSubMat(), MatNestGetSubMats(), MATNEST, MatNestCreate(), MatNestGetLocalISs(),
1207:           MatNestGetISs()
1208: @*/
1209: PetscErrorCode  MatNestGetSize(Mat A,PetscInt *M,PetscInt *N)
1210: {

1214:   PetscUseMethod(A,"MatNestGetSize_C",(Mat,PetscInt*,PetscInt*),(A,M,N));
1215:   return(0);
1216: }

1218: static PetscErrorCode MatNestGetISs_Nest(Mat A,IS rows[],IS cols[])
1219: {
1220:   Mat_Nest *vs = (Mat_Nest*)A->data;
1221:   PetscInt i;

1224:   if (rows) for (i=0; i<vs->nr; i++) rows[i] = vs->isglobal.row[i];
1225:   if (cols) for (i=0; i<vs->nc; i++) cols[i] = vs->isglobal.col[i];
1226:   return(0);
1227: }

1229: /*@C
1230:  MatNestGetISs - Returns the index sets partitioning the row and column spaces

1232:  Not collective

1234:  Input Parameters:
1235: .   A  - nest matrix

1237:  Output Parameter:
1238: +   rows - array of row index sets
1239: -   cols - array of column index sets

1241:  Level: advanced

1243:  Notes:
1244:  The user must have allocated arrays of the correct size. The reference count is not increased on the returned ISs.

1246: .seealso: MatNestGetSubMat(), MatNestGetSubMats(), MatNestGetSize(), MatNestGetLocalISs(), MATNEST,
1247:           MatNestCreate(), MatNestGetSubMats(), MatNestSetSubMats()
1248: @*/
1249: PetscErrorCode  MatNestGetISs(Mat A,IS rows[],IS cols[])
1250: {

1255:   PetscUseMethod(A,"MatNestGetISs_C",(Mat,IS[],IS[]),(A,rows,cols));
1256:   return(0);
1257: }

1259: static PetscErrorCode MatNestGetLocalISs_Nest(Mat A,IS rows[],IS cols[])
1260: {
1261:   Mat_Nest *vs = (Mat_Nest*)A->data;
1262:   PetscInt i;

1265:   if (rows) for (i=0; i<vs->nr; i++) rows[i] = vs->islocal.row[i];
1266:   if (cols) for (i=0; i<vs->nc; i++) cols[i] = vs->islocal.col[i];
1267:   return(0);
1268: }

1270: /*@C
1271:  MatNestGetLocalISs - Returns the index sets partitioning the row and column spaces

1273:  Not collective

1275:  Input Parameters:
1276: .   A  - nest matrix

1278:  Output Parameter:
1279: +   rows - array of row index sets (or NULL to ignore)
1280: -   cols - array of column index sets (or NULL to ignore)

1282:  Level: advanced

1284:  Notes:
1285:  The user must have allocated arrays of the correct size. The reference count is not increased on the returned ISs.

1287: .seealso: MatNestGetSubMat(), MatNestGetSubMats(), MatNestGetSize(), MatNestGetISs(), MatNestCreate(),
1288:           MATNEST, MatNestSetSubMats(), MatNestSetSubMat()
1289: @*/
1290: PetscErrorCode  MatNestGetLocalISs(Mat A,IS rows[],IS cols[])
1291: {

1296:   PetscUseMethod(A,"MatNestGetLocalISs_C",(Mat,IS[],IS[]),(A,rows,cols));
1297:   return(0);
1298: }

1300: PetscErrorCode  MatNestSetVecType_Nest(Mat A,VecType vtype)
1301: {
1303:   PetscBool      flg;

1306:   PetscStrcmp(vtype,VECNEST,&flg);
1307:   /* In reality, this only distinguishes VECNEST and "other" */
1308:   if (flg) A->ops->getvecs = MatCreateVecs_Nest;
1309:   else A->ops->getvecs = (PetscErrorCode (*)(Mat,Vec*,Vec*)) 0;
1310:   return(0);
1311: }

1313: /*@C
1314:  MatNestSetVecType - Sets the type of Vec returned by MatCreateVecs()

1316:  Not collective

1318:  Input Parameters:
1319: +  A  - nest matrix
1320: -  vtype - type to use for creating vectors

1322:  Notes:

1324:  Level: developer

1326: .seealso: MatCreateVecs(), MATNEST, MatNestCreate()
1327: @*/
1328: PetscErrorCode  MatNestSetVecType(Mat A,VecType vtype)
1329: {

1333:   PetscTryMethod(A,"MatNestSetVecType_C",(Mat,VecType),(A,vtype));
1334:   return(0);
1335: }

1337: PetscErrorCode MatNestSetSubMats_Nest(Mat A,PetscInt nr,const IS is_row[],PetscInt nc,const IS is_col[],const Mat a[])
1338: {
1339:   Mat_Nest       *s = (Mat_Nest*)A->data;
1340:   PetscInt       i,j,m,n,M,N;
1342:   PetscBool      cong;

1345:   MatReset_Nest(A);

1347:   s->nr = nr;
1348:   s->nc = nc;

1350:   /* Create space for submatrices */
1351:   PetscMalloc1(nr,&s->m);
1352:   for (i=0; i<nr; i++) {
1353:     PetscMalloc1(nc,&s->m[i]);
1354:   }
1355:   for (i=0; i<nr; i++) {
1356:     for (j=0; j<nc; j++) {
1357:       s->m[i][j] = a[i*nc+j];
1358:       if (a[i*nc+j]) {
1359:         PetscObjectReference((PetscObject)a[i*nc+j]);
1360:       }
1361:     }
1362:   }

1364:   MatSetUp_NestIS_Private(A,nr,is_row,nc,is_col);

1366:   PetscMalloc1(nr,&s->row_len);
1367:   PetscMalloc1(nc,&s->col_len);
1368:   for (i=0; i<nr; i++) s->row_len[i]=-1;
1369:   for (j=0; j<nc; j++) s->col_len[j]=-1;

1371:   PetscCalloc1(nr*nc,&s->nnzstate);
1372:   for (i=0; i<nr; i++) {
1373:     for (j=0; j<nc; j++) {
1374:       if (s->m[i][j]) {
1375:         MatGetNonzeroState(s->m[i][j],&s->nnzstate[i*nc+j]);
1376:       }
1377:     }
1378:   }

1380:   MatNestGetSizes_Private(A,&m,&n,&M,&N);

1382:   PetscLayoutSetSize(A->rmap,M);
1383:   PetscLayoutSetLocalSize(A->rmap,m);
1384:   PetscLayoutSetSize(A->cmap,N);
1385:   PetscLayoutSetLocalSize(A->cmap,n);

1387:   PetscLayoutSetUp(A->rmap);
1388:   PetscLayoutSetUp(A->cmap);

1390:   /* disable operations that are not supported for non-square matrices,
1391:      or matrices for which is_row != is_col  */
1392:   MatHasCongruentLayouts(A,&cong);
1393:   if (cong && nr != nc) cong = PETSC_FALSE;
1394:   if (cong) {
1395:     for (i = 0; cong && i < nr; i++) {
1396:       ISEqualUnsorted(s->isglobal.row[i],s->isglobal.col[i],&cong);
1397:     }
1398:   }
1399:   if (!cong) {
1400:     A->ops->missingdiagonal = NULL;
1401:     A->ops->getdiagonal     = NULL;
1402:     A->ops->shift           = NULL;
1403:     A->ops->diagonalset     = NULL;
1404:   }

1406:   PetscCalloc2(nr,&s->left,nc,&s->right);
1407:   PetscObjectStateIncrease((PetscObject)A);
1408:   A->nonzerostate++;
1409:   return(0);
1410: }

1412: /*@
1413:    MatNestSetSubMats - Sets the nested submatrices

1415:    Collective on Mat

1417:    Input Parameter:
1418: +  A - nested matrix
1419: .  nr - number of nested row blocks
1420: .  is_row - index sets for each nested row block, or NULL to make contiguous
1421: .  nc - number of nested column blocks
1422: .  is_col - index sets for each nested column block, or NULL to make contiguous
1423: -  a - row-aligned array of nr*nc submatrices, empty submatrices can be passed using NULL

1425:    Notes: this always resets any submatrix information previously set

1427:    Level: advanced

1429: .seealso: MatCreateNest(), MATNEST, MatNestSetSubMat(), MatNestGetSubMat(), MatNestGetSubMats()
1430: @*/
1431: PetscErrorCode MatNestSetSubMats(Mat A,PetscInt nr,const IS is_row[],PetscInt nc,const IS is_col[],const Mat a[])
1432: {
1434:   PetscInt       i;

1438:   if (nr < 0) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Number of rows cannot be negative");
1439:   if (nr && is_row) {
1442:   }
1443:   if (nc < 0) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_OUTOFRANGE,"Number of columns cannot be negative");
1444:   if (nc && is_col) {
1447:   }
1449:   PetscUseMethod(A,"MatNestSetSubMats_C",(Mat,PetscInt,const IS[],PetscInt,const IS[],const Mat[]),(A,nr,is_row,nc,is_col,a));
1450:   return(0);
1451: }

1453: static PetscErrorCode MatNestCreateAggregateL2G_Private(Mat A,PetscInt n,const IS islocal[],const IS isglobal[],PetscBool colflg,ISLocalToGlobalMapping *ltog)
1454: {
1456:   PetscBool      flg;
1457:   PetscInt       i,j,m,mi,*ix;

1460:   *ltog = NULL;
1461:   for (i=0,m=0,flg=PETSC_FALSE; i<n; i++) {
1462:     if (islocal[i]) {
1463:       ISGetLocalSize(islocal[i],&mi);
1464:       flg  = PETSC_TRUE;      /* We found a non-trivial entry */
1465:     } else {
1466:       ISGetLocalSize(isglobal[i],&mi);
1467:     }
1468:     m += mi;
1469:   }
1470:   if (!flg) return(0);

1472:   PetscMalloc1(m,&ix);
1473:   for (i=0,m=0; i<n; i++) {
1474:     ISLocalToGlobalMapping smap = NULL;
1475:     Mat                    sub = NULL;
1476:     PetscSF                sf;
1477:     PetscLayout            map;
1478:     const PetscInt         *ix2;

1480:     if (!colflg) {
1481:       MatNestFindNonzeroSubMatRow(A,i,&sub);
1482:     } else {
1483:       MatNestFindNonzeroSubMatCol(A,i,&sub);
1484:     }
1485:     if (sub) {
1486:       if (!colflg) {
1487:         MatGetLocalToGlobalMapping(sub,&smap,NULL);
1488:       } else {
1489:         MatGetLocalToGlobalMapping(sub,NULL,&smap);
1490:       }
1491:     }
1492:     /*
1493:        Now we need to extract the monolithic global indices that correspond to the given split global indices.
1494:        In many/most cases, we only want MatGetLocalSubMatrix() to work, in which case we only need to know the size of the local spaces.
1495:     */
1496:     ISGetIndices(isglobal[i],&ix2);
1497:     if (islocal[i]) {
1498:       PetscInt *ilocal,*iremote;
1499:       PetscInt mil,nleaves;

1501:       ISGetLocalSize(islocal[i],&mi);
1502:       if (!smap) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_PLIB,"Missing local to global map");
1503:       for (j=0; j<mi; j++) ix[m+j] = j;
1504:       ISLocalToGlobalMappingApply(smap,mi,ix+m,ix+m);

1506:       /* PetscSFSetGraphLayout does not like negative indices */
1507:       PetscMalloc2(mi,&ilocal,mi,&iremote);
1508:       for (j=0, nleaves = 0; j<mi; j++) {
1509:         if (ix[m+j] < 0) continue;
1510:         ilocal[nleaves]  = j;
1511:         iremote[nleaves] = ix[m+j];
1512:         nleaves++;
1513:       }
1514:       ISGetLocalSize(isglobal[i],&mil);
1515:       PetscSFCreate(PetscObjectComm((PetscObject)A),&sf);
1516:       PetscLayoutCreate(PetscObjectComm((PetscObject)A),&map);
1517:       PetscLayoutSetLocalSize(map,mil);
1518:       PetscLayoutSetUp(map);
1519:       PetscSFSetGraphLayout(sf,map,nleaves,ilocal,PETSC_USE_POINTER,iremote);
1520:       PetscLayoutDestroy(&map);
1521:       PetscSFBcastBegin(sf,MPIU_INT,ix2,ix + m);
1522:       PetscSFBcastEnd(sf,MPIU_INT,ix2,ix + m);
1523:       PetscSFDestroy(&sf);
1524:       PetscFree2(ilocal,iremote);
1525:     } else {
1526:       ISGetLocalSize(isglobal[i],&mi);
1527:       for (j=0; j<mi; j++) ix[m+j] = ix2[i];
1528:     }
1529:     ISRestoreIndices(isglobal[i],&ix2);
1530:     m   += mi;
1531:   }
1532:   ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)A),1,m,ix,PETSC_OWN_POINTER,ltog);
1533:   return(0);
1534: }


1537: /* If an IS was provided, there is nothing Nest needs to do, otherwise Nest will build a strided IS */
1538: /*
1539:   nprocessors = NP
1540:   Nest x^T = ((g_0,g_1,...g_nprocs-1), (h_0,h_1,...h_NP-1))
1541:        proc 0: => (g_0,h_0,)
1542:        proc 1: => (g_1,h_1,)
1543:        ...
1544:        proc nprocs-1: => (g_NP-1,h_NP-1,)

1546:             proc 0:                      proc 1:                    proc nprocs-1:
1547:     is[0] = (0,1,2,...,nlocal(g_0)-1)  (0,1,...,nlocal(g_1)-1)  (0,1,...,nlocal(g_NP-1))

1549:             proc 0:
1550:     is[1] = (nlocal(g_0),nlocal(g_0)+1,...,nlocal(g_0)+nlocal(h_0)-1)
1551:             proc 1:
1552:     is[1] = (nlocal(g_1),nlocal(g_1)+1,...,nlocal(g_1)+nlocal(h_1)-1)

1554:             proc NP-1:
1555:     is[1] = (nlocal(g_NP-1),nlocal(g_NP-1)+1,...,nlocal(g_NP-1)+nlocal(h_NP-1)-1)
1556: */
1557: static PetscErrorCode MatSetUp_NestIS_Private(Mat A,PetscInt nr,const IS is_row[],PetscInt nc,const IS is_col[])
1558: {
1559:   Mat_Nest       *vs = (Mat_Nest*)A->data;
1560:   PetscInt       i,j,offset,n,nsum,bs;
1562:   Mat            sub = NULL;

1565:   PetscMalloc1(nr,&vs->isglobal.row);
1566:   PetscMalloc1(nc,&vs->isglobal.col);
1567:   if (is_row) { /* valid IS is passed in */
1568:     /* refs on is[] are incremeneted */
1569:     for (i=0; i<vs->nr; i++) {
1570:       PetscObjectReference((PetscObject)is_row[i]);

1572:       vs->isglobal.row[i] = is_row[i];
1573:     }
1574:   } else {                      /* Create the ISs by inspecting sizes of a submatrix in each row */
1575:     nsum = 0;
1576:     for (i=0; i<vs->nr; i++) {  /* Add up the local sizes to compute the aggregate offset */
1577:       MatNestFindNonzeroSubMatRow(A,i,&sub);
1578:       if (!sub) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONG,"No nonzero submatrix in row %D",i);
1579:       MatGetLocalSize(sub,&n,NULL);
1580:       if (n < 0) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Sizes have not yet been set for submatrix");
1581:       nsum += n;
1582:     }
1583:     MPI_Scan(&nsum,&offset,1,MPIU_INT,MPI_SUM,PetscObjectComm((PetscObject)A));
1584:     offset -= nsum;
1585:     for (i=0; i<vs->nr; i++) {
1586:       MatNestFindNonzeroSubMatRow(A,i,&sub);
1587:       MatGetLocalSize(sub,&n,NULL);
1588:       MatGetBlockSize(sub,&bs);
1589:       ISCreateStride(PetscObjectComm((PetscObject)sub),n,offset,1,&vs->isglobal.row[i]);
1590:       ISSetBlockSize(vs->isglobal.row[i],bs);
1591:       offset += n;
1592:     }
1593:   }

1595:   if (is_col) { /* valid IS is passed in */
1596:     /* refs on is[] are incremeneted */
1597:     for (j=0; j<vs->nc; j++) {
1598:       PetscObjectReference((PetscObject)is_col[j]);

1600:       vs->isglobal.col[j] = is_col[j];
1601:     }
1602:   } else {                      /* Create the ISs by inspecting sizes of a submatrix in each column */
1603:     offset = A->cmap->rstart;
1604:     nsum   = 0;
1605:     for (j=0; j<vs->nc; j++) {
1606:       MatNestFindNonzeroSubMatCol(A,j,&sub);
1607:       if (!sub) SETERRQ1(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONG,"No nonzero submatrix in column %D",i);
1608:       MatGetLocalSize(sub,NULL,&n);
1609:       if (n < 0) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_ARG_WRONGSTATE,"Sizes have not yet been set for submatrix");
1610:       nsum += n;
1611:     }
1612:     MPI_Scan(&nsum,&offset,1,MPIU_INT,MPI_SUM,PetscObjectComm((PetscObject)A));
1613:     offset -= nsum;
1614:     for (j=0; j<vs->nc; j++) {
1615:       MatNestFindNonzeroSubMatCol(A,j,&sub);
1616:       MatGetLocalSize(sub,NULL,&n);
1617:       MatGetBlockSize(sub,&bs);
1618:       ISCreateStride(PetscObjectComm((PetscObject)sub),n,offset,1,&vs->isglobal.col[j]);
1619:       ISSetBlockSize(vs->isglobal.col[j],bs);
1620:       offset += n;
1621:     }
1622:   }

1624:   /* Set up the local ISs */
1625:   PetscMalloc1(vs->nr,&vs->islocal.row);
1626:   PetscMalloc1(vs->nc,&vs->islocal.col);
1627:   for (i=0,offset=0; i<vs->nr; i++) {
1628:     IS                     isloc;
1629:     ISLocalToGlobalMapping rmap = NULL;
1630:     PetscInt               nlocal,bs;
1631:     MatNestFindNonzeroSubMatRow(A,i,&sub);
1632:     if (sub) {MatGetLocalToGlobalMapping(sub,&rmap,NULL);}
1633:     if (rmap) {
1634:       MatGetBlockSize(sub,&bs);
1635:       ISLocalToGlobalMappingGetSize(rmap,&nlocal);
1636:       ISCreateStride(PETSC_COMM_SELF,nlocal,offset,1,&isloc);
1637:       ISSetBlockSize(isloc,bs);
1638:     } else {
1639:       nlocal = 0;
1640:       isloc  = NULL;
1641:     }
1642:     vs->islocal.row[i] = isloc;
1643:     offset            += nlocal;
1644:   }
1645:   for (i=0,offset=0; i<vs->nc; i++) {
1646:     IS                     isloc;
1647:     ISLocalToGlobalMapping cmap = NULL;
1648:     PetscInt               nlocal,bs;
1649:     MatNestFindNonzeroSubMatCol(A,i,&sub);
1650:     if (sub) {MatGetLocalToGlobalMapping(sub,NULL,&cmap);}
1651:     if (cmap) {
1652:       MatGetBlockSize(sub,&bs);
1653:       ISLocalToGlobalMappingGetSize(cmap,&nlocal);
1654:       ISCreateStride(PETSC_COMM_SELF,nlocal,offset,1,&isloc);
1655:       ISSetBlockSize(isloc,bs);
1656:     } else {
1657:       nlocal = 0;
1658:       isloc  = NULL;
1659:     }
1660:     vs->islocal.col[i] = isloc;
1661:     offset            += nlocal;
1662:   }

1664:   /* Set up the aggregate ISLocalToGlobalMapping */
1665:   {
1666:     ISLocalToGlobalMapping rmap,cmap;
1667:     MatNestCreateAggregateL2G_Private(A,vs->nr,vs->islocal.row,vs->isglobal.row,PETSC_FALSE,&rmap);
1668:     MatNestCreateAggregateL2G_Private(A,vs->nc,vs->islocal.col,vs->isglobal.col,PETSC_TRUE,&cmap);
1669:     if (rmap && cmap) {MatSetLocalToGlobalMapping(A,rmap,cmap);}
1670:     ISLocalToGlobalMappingDestroy(&rmap);
1671:     ISLocalToGlobalMappingDestroy(&cmap);
1672:   }

1674: #if defined(PETSC_USE_DEBUG)
1675:   for (i=0; i<vs->nr; i++) {
1676:     for (j=0; j<vs->nc; j++) {
1677:       PetscInt m,n,M,N,mi,ni,Mi,Ni;
1678:       Mat      B = vs->m[i][j];
1679:       if (!B) continue;
1680:       MatGetSize(B,&M,&N);
1681:       MatGetLocalSize(B,&m,&n);
1682:       ISGetSize(vs->isglobal.row[i],&Mi);
1683:       ISGetSize(vs->isglobal.col[j],&Ni);
1684:       ISGetLocalSize(vs->isglobal.row[i],&mi);
1685:       ISGetLocalSize(vs->isglobal.col[j],&ni);
1686:       if (M != Mi || N != Ni) SETERRQ6(PetscObjectComm((PetscObject)sub),PETSC_ERR_ARG_INCOMP,"Global sizes (%D,%D) of nested submatrix (%D,%D) do not agree with space defined by index sets (%D,%D)",M,N,i,j,Mi,Ni);
1687:       if (m != mi || n != ni) SETERRQ6(PetscObjectComm((PetscObject)sub),PETSC_ERR_ARG_INCOMP,"Local sizes (%D,%D) of nested submatrix (%D,%D) do not agree with space defined by index sets (%D,%D)",m,n,i,j,mi,ni);
1688:     }
1689:   }
1690: #endif

1692:   /* Set A->assembled if all non-null blocks are currently assembled */
1693:   for (i=0; i<vs->nr; i++) {
1694:     for (j=0; j<vs->nc; j++) {
1695:       if (vs->m[i][j] && !vs->m[i][j]->assembled) return(0);
1696:     }
1697:   }
1698:   A->assembled = PETSC_TRUE;
1699:   return(0);
1700: }

1702: /*@C
1703:    MatCreateNest - Creates a new matrix containing several nested submatrices, each stored separately

1705:    Collective on Mat

1707:    Input Parameter:
1708: +  comm - Communicator for the new Mat
1709: .  nr - number of nested row blocks
1710: .  is_row - index sets for each nested row block, or NULL to make contiguous
1711: .  nc - number of nested column blocks
1712: .  is_col - index sets for each nested column block, or NULL to make contiguous
1713: -  a - row-aligned array of nr*nc submatrices, empty submatrices can be passed using NULL

1715:    Output Parameter:
1716: .  B - new matrix

1718:    Level: advanced

1720: .seealso: MatCreate(), VecCreateNest(), DMCreateMatrix(), MATNEST, MatNestSetSubMat(),
1721:           MatNestGetSubMat(), MatNestGetLocalISs(), MatNestGetSize(),
1722:           MatNestGetISs(), MatNestSetSubMats(), MatNestGetSubMats()
1723: @*/
1724: PetscErrorCode MatCreateNest(MPI_Comm comm,PetscInt nr,const IS is_row[],PetscInt nc,const IS is_col[],const Mat a[],Mat *B)
1725: {
1726:   Mat            A;

1730:   *B   = 0;
1731:   MatCreate(comm,&A);
1732:   MatSetType(A,MATNEST);
1733:   A->preallocated = PETSC_TRUE;
1734:   MatNestSetSubMats(A,nr,is_row,nc,is_col,a);
1735:   *B   = A;
1736:   return(0);
1737: }

1739: static PetscErrorCode MatConvert_Nest_SeqAIJ_fast(Mat A,MatType newtype,MatReuse reuse,Mat *newmat)
1740: {
1741:   Mat_Nest       *nest = (Mat_Nest*)A->data;
1742:   Mat            *trans;
1743:   PetscScalar    **avv;
1744:   PetscScalar    *vv;
1745:   PetscInt       **aii,**ajj;
1746:   PetscInt       *ii,*jj,*ci;
1747:   PetscInt       nr,nc,nnz,i,j;
1748:   PetscBool      done;

1752:   MatGetSize(A,&nr,&nc);
1753:   if (reuse == MAT_REUSE_MATRIX) {
1754:     PetscInt rnr;

1756:     MatGetRowIJ(*newmat,0,PETSC_FALSE,PETSC_FALSE,&rnr,(const PetscInt**)&ii,(const PetscInt**)&jj,&done);
1757:     if (!done) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_PLIB,"MatGetRowIJ");
1758:     if (rnr != nr) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_USER,"Cannot reuse matrix, wrong number of rows");
1759:     MatSeqAIJGetArray(*newmat,&vv);
1760:   }
1761:   /* extract CSR for nested SeqAIJ matrices */
1762:   nnz  = 0;
1763:   PetscCalloc4(nest->nr*nest->nc,&aii,nest->nr*nest->nc,&ajj,nest->nr*nest->nc,&avv,nest->nr*nest->nc,&trans);
1764:   for (i=0; i<nest->nr; ++i) {
1765:     for (j=0; j<nest->nc; ++j) {
1766:       Mat B = nest->m[i][j];
1767:       if (B) {
1768:         PetscScalar *naa;
1769:         PetscInt    *nii,*njj,nnr;
1770:         PetscBool   istrans;

1772:         PetscObjectTypeCompare((PetscObject)B,MATTRANSPOSEMAT,&istrans);
1773:         if (istrans) {
1774:           Mat Bt;

1776:           MatTransposeGetMat(B,&Bt);
1777:           MatTranspose(Bt,MAT_INITIAL_MATRIX,&trans[i*nest->nc+j]);
1778:           B    = trans[i*nest->nc+j];
1779:         }
1780:         MatGetRowIJ(B,0,PETSC_FALSE,PETSC_FALSE,&nnr,(const PetscInt**)&nii,(const PetscInt**)&njj,&done);
1781:         if (!done) SETERRQ(PetscObjectComm((PetscObject)B),PETSC_ERR_PLIB,"MatGetRowIJ");
1782:         MatSeqAIJGetArray(B,&naa);
1783:         nnz += nii[nnr];

1785:         aii[i*nest->nc+j] = nii;
1786:         ajj[i*nest->nc+j] = njj;
1787:         avv[i*nest->nc+j] = naa;
1788:       }
1789:     }
1790:   }
1791:   if (reuse != MAT_REUSE_MATRIX) {
1792:     PetscMalloc1(nr+1,&ii);
1793:     PetscMalloc1(nnz,&jj);
1794:     PetscMalloc1(nnz,&vv);
1795:   } else {
1796:     if (nnz != ii[nr]) SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_USER,"Cannot reuse matrix, wrong number of nonzeros");
1797:   }

1799:   /* new row pointer */
1800:   PetscArrayzero(ii,nr+1);
1801:   for (i=0; i<nest->nr; ++i) {
1802:     PetscInt       ncr,rst;

1804:     ISStrideGetInfo(nest->isglobal.row[i],&rst,NULL);
1805:     ISGetLocalSize(nest->isglobal.row[i],&ncr);
1806:     for (j=0; j<nest->nc; ++j) {
1807:       if (aii[i*nest->nc+j]) {
1808:         PetscInt    *nii = aii[i*nest->nc+j];
1809:         PetscInt    ir;

1811:         for (ir=rst; ir<ncr+rst; ++ir) {
1812:           ii[ir+1] += nii[1]-nii[0];
1813:           nii++;
1814:         }
1815:       }
1816:     }
1817:   }
1818:   for (i=0; i<nr; i++) ii[i+1] += ii[i];

1820:   /* construct CSR for the new matrix */
1821:   PetscCalloc1(nr,&ci);
1822:   for (i=0; i<nest->nr; ++i) {
1823:     PetscInt       ncr,rst;

1825:     ISStrideGetInfo(nest->isglobal.row[i],&rst,NULL);
1826:     ISGetLocalSize(nest->isglobal.row[i],&ncr);
1827:     for (j=0; j<nest->nc; ++j) {
1828:       if (aii[i*nest->nc+j]) {
1829:         PetscScalar *nvv = avv[i*nest->nc+j];
1830:         PetscInt    *nii = aii[i*nest->nc+j];
1831:         PetscInt    *njj = ajj[i*nest->nc+j];
1832:         PetscInt    ir,cst;

1834:         ISStrideGetInfo(nest->isglobal.col[j],&cst,NULL);
1835:         for (ir=rst; ir<ncr+rst; ++ir) {
1836:           PetscInt ij,rsize = nii[1]-nii[0],ist = ii[ir]+ci[ir];

1838:           for (ij=0;ij<rsize;ij++) {
1839:             jj[ist+ij] = *njj+cst;
1840:             vv[ist+ij] = *nvv;
1841:             njj++;
1842:             nvv++;
1843:           }
1844:           ci[ir] += rsize;
1845:           nii++;
1846:         }
1847:       }
1848:     }
1849:   }
1850:   PetscFree(ci);

1852:   /* restore info */
1853:   for (i=0; i<nest->nr; ++i) {
1854:     for (j=0; j<nest->nc; ++j) {
1855:       Mat B = nest->m[i][j];
1856:       if (B) {
1857:         PetscInt nnr = 0, k = i*nest->nc+j;

1859:         B    = (trans[k] ? trans[k] : B);
1860:         MatRestoreRowIJ(B,0,PETSC_FALSE,PETSC_FALSE,&nnr,(const PetscInt**)&aii[k],(const PetscInt**)&ajj[k],&done);
1861:         if (!done) SETERRQ(PetscObjectComm((PetscObject)B),PETSC_ERR_PLIB,"MatRestoreRowIJ");
1862:         MatSeqAIJRestoreArray(B,&avv[k]);
1863:         MatDestroy(&trans[k]);
1864:       }
1865:     }
1866:   }
1867:   PetscFree4(aii,ajj,avv,trans);

1869:   /* finalize newmat */
1870:   if (reuse == MAT_INITIAL_MATRIX) {
1871:     MatCreateSeqAIJWithArrays(PetscObjectComm((PetscObject)A),nr,nc,ii,jj,vv,newmat);
1872:   } else if (reuse == MAT_INPLACE_MATRIX) {
1873:     Mat B;

1875:     MatCreateSeqAIJWithArrays(PetscObjectComm((PetscObject)A),nr,nc,ii,jj,vv,&B);
1876:     MatHeaderReplace(A,&B);
1877:   }
1878:   MatAssemblyBegin(*newmat,MAT_FINAL_ASSEMBLY);
1879:   MatAssemblyEnd(*newmat,MAT_FINAL_ASSEMBLY);
1880:   {
1881:     Mat_SeqAIJ *a = (Mat_SeqAIJ*)((*newmat)->data);
1882:     a->free_a     = PETSC_TRUE;
1883:     a->free_ij    = PETSC_TRUE;
1884:   }
1885:   return(0);
1886: }

1888: PETSC_INTERN PetscErrorCode MatConvert_Nest_AIJ(Mat A,MatType newtype,MatReuse reuse,Mat *newmat)
1889: {
1891:   Mat_Nest       *nest = (Mat_Nest*)A->data;
1892:   PetscInt       m,n,M,N,i,j,k,*dnnz,*onnz,rstart;
1893:   PetscInt       cstart,cend;
1894:   PetscMPIInt    size;
1895:   Mat            C;

1898:   MPI_Comm_size(PetscObjectComm((PetscObject)A),&size);
1899:   if (size == 1) { /* look for a special case with SeqAIJ matrices and strided-1, contiguous, blocks */
1900:     PetscInt  nf;
1901:     PetscBool fast;

1903:     PetscStrcmp(newtype,MATAIJ,&fast);
1904:     if (!fast) {
1905:       PetscStrcmp(newtype,MATSEQAIJ,&fast);
1906:     }
1907:     for (i=0; i<nest->nr && fast; ++i) {
1908:       for (j=0; j<nest->nc && fast; ++j) {
1909:         Mat B = nest->m[i][j];
1910:         if (B) {
1911:           PetscObjectTypeCompare((PetscObject)B,MATSEQAIJ,&fast);
1912:           if (!fast) {
1913:             PetscBool istrans;

1915:             PetscObjectTypeCompare((PetscObject)B,MATTRANSPOSEMAT,&istrans);
1916:             if (istrans) {
1917:               Mat Bt;

1919:               MatTransposeGetMat(B,&Bt);
1920:               PetscObjectTypeCompare((PetscObject)Bt,MATSEQAIJ,&fast);
1921:             }
1922:           }
1923:         }
1924:       }
1925:     }
1926:     for (i=0, nf=0; i<nest->nr && fast; ++i) {
1927:       PetscObjectTypeCompare((PetscObject)nest->isglobal.row[i],ISSTRIDE,&fast);
1928:       if (fast) {
1929:         PetscInt f,s;

1931:         ISStrideGetInfo(nest->isglobal.row[i],&f,&s);
1932:         if (f != nf || s != 1) { fast = PETSC_FALSE; }
1933:         else {
1934:           ISGetSize(nest->isglobal.row[i],&f);
1935:           nf  += f;
1936:         }
1937:       }
1938:     }
1939:     for (i=0, nf=0; i<nest->nc && fast; ++i) {
1940:       PetscObjectTypeCompare((PetscObject)nest->isglobal.col[i],ISSTRIDE,&fast);
1941:       if (fast) {
1942:         PetscInt f,s;

1944:         ISStrideGetInfo(nest->isglobal.col[i],&f,&s);
1945:         if (f != nf || s != 1) { fast = PETSC_FALSE; }
1946:         else {
1947:           ISGetSize(nest->isglobal.col[i],&f);
1948:           nf  += f;
1949:         }
1950:       }
1951:     }
1952:     if (fast) {
1953:       MatConvert_Nest_SeqAIJ_fast(A,newtype,reuse,newmat);
1954:       return(0);
1955:     }
1956:   }
1957:   MatGetSize(A,&M,&N);
1958:   MatGetLocalSize(A,&m,&n);
1959:   MatGetOwnershipRangeColumn(A,&cstart,&cend);
1960:   switch (reuse) {
1961:   case MAT_INITIAL_MATRIX:
1962:     MatCreate(PetscObjectComm((PetscObject)A),&C);
1963:     MatSetType(C,newtype);
1964:     MatSetSizes(C,m,n,M,N);
1965:     *newmat = C;
1966:     break;
1967:   case MAT_REUSE_MATRIX:
1968:     C = *newmat;
1969:     break;
1970:   default: SETERRQ(PetscObjectComm((PetscObject)A),PETSC_ERR_SUP,"MatReuse");
1971:   }
1972:   PetscMalloc1(2*m,&dnnz);
1973:   onnz = dnnz + m;
1974:   for (k=0; k<m; k++) {
1975:     dnnz[k] = 0;
1976:     onnz[k] = 0;
1977:   }
1978:   for (j=0; j<nest->nc; ++j) {
1979:     IS             bNis;
1980:     PetscInt       bN;
1981:     const PetscInt *bNindices;
1982:     /* Using global column indices and ISAllGather() is not scalable. */
1983:     ISAllGather(nest->isglobal.col[j], &bNis);
1984:     ISGetSize(bNis, &bN);
1985:     ISGetIndices(bNis,&bNindices);
1986:     for (i=0; i<nest->nr; ++i) {
1987:       PetscSF        bmsf;
1988:       PetscSFNode    *iremote;
1989:       Mat            B;
1990:       PetscInt       bm, *sub_dnnz,*sub_onnz, br;
1991:       const PetscInt *bmindices;
1992:       B = nest->m[i][j];
1993:       if (!B) continue;
1994:       ISGetLocalSize(nest->isglobal.row[i],&bm);
1995:       ISGetIndices(nest->isglobal.row[i],&bmindices);
1996:       PetscSFCreate(PetscObjectComm((PetscObject)A), &bmsf);
1997:       PetscMalloc1(bm,&iremote);
1998:       PetscMalloc1(bm,&sub_dnnz);
1999:       PetscMalloc1(bm,&sub_onnz);
2000:       for (k = 0; k < bm; ++k){
2001:             sub_dnnz[k] = 0;
2002:             sub_onnz[k] = 0;
2003:       }
2004:       /*
2005:        Locate the owners for all of the locally-owned global row indices for this row block.
2006:        These determine the roots of PetscSF used to communicate preallocation data to row owners.
2007:        The roots correspond to the dnnz and onnz entries; thus, there are two roots per row.
2008:        */
2009:       MatGetOwnershipRange(B,&rstart,NULL);
2010:       for (br = 0; br < bm; ++br) {
2011:         PetscInt       row = bmindices[br], brncols, col;
2012:         const PetscInt *brcols;
2013:         PetscInt       rowrel = 0; /* row's relative index on its owner rank */
2014:         PetscMPIInt    rowowner = 0;
2015:         PetscLayoutFindOwnerIndex(A->rmap,row,&rowowner,&rowrel);
2016:         /* how many roots  */
2017:         iremote[br].rank = rowowner; iremote[br].index = rowrel;           /* edge from bmdnnz to dnnz */
2018:         /* get nonzero pattern */
2019:         MatGetRow(B,br+rstart,&brncols,&brcols,NULL);
2020:         for (k=0; k<brncols; k++) {
2021:           col  = bNindices[brcols[k]];
2022:           if (col>=A->cmap->range[rowowner] && col<A->cmap->range[rowowner+1]) {
2023:             sub_dnnz[br]++;
2024:           } else {
2025:             sub_onnz[br]++;
2026:           }
2027:         }
2028:         MatRestoreRow(B,br+rstart,&brncols,&brcols,NULL);
2029:       }
2030:       ISRestoreIndices(nest->isglobal.row[i],&bmindices);
2031:       /* bsf will have to take care of disposing of bedges. */
2032:       PetscSFSetGraph(bmsf,m,bm,NULL,PETSC_OWN_POINTER,iremote,PETSC_OWN_POINTER);
2033:       PetscSFReduceBegin(bmsf,MPIU_INT,sub_dnnz,dnnz,MPI_SUM);
2034:       PetscSFReduceEnd(bmsf,MPIU_INT,sub_dnnz,dnnz,MPI_SUM);
2035:       PetscSFReduceBegin(bmsf,MPIU_INT,sub_onnz,onnz,MPI_SUM);
2036:       PetscSFReduceEnd(bmsf,MPIU_INT,sub_onnz,onnz,MPI_SUM);
2037:       PetscFree(sub_dnnz);
2038:       PetscFree(sub_onnz);
2039:       PetscSFDestroy(&bmsf);
2040:     }
2041:     ISRestoreIndices(bNis,&bNindices);
2042:     ISDestroy(&bNis);
2043:   }
2044:   /* Resize preallocation if overestimated */
2045:   for (i=0;i<m;i++) {
2046:     dnnz[i] = PetscMin(dnnz[i],A->cmap->n);
2047:     onnz[i] = PetscMin(onnz[i],A->cmap->N - A->cmap->n);
2048:   }
2049:   MatSeqAIJSetPreallocation(C,0,dnnz);
2050:   MatMPIAIJSetPreallocation(C,0,dnnz,0,onnz);
2051:   PetscFree(dnnz);

2053:   /* Fill by row */
2054:   for (j=0; j<nest->nc; ++j) {
2055:     /* Using global column indices and ISAllGather() is not scalable. */
2056:     IS             bNis;
2057:     PetscInt       bN;
2058:     const PetscInt *bNindices;
2059:     ISAllGather(nest->isglobal.col[j], &bNis);
2060:     ISGetSize(bNis,&bN);
2061:     ISGetIndices(bNis,&bNindices);
2062:     for (i=0; i<nest->nr; ++i) {
2063:       Mat            B;
2064:       PetscInt       bm, br;
2065:       const PetscInt *bmindices;
2066:       B = nest->m[i][j];
2067:       if (!B) continue;
2068:       ISGetLocalSize(nest->isglobal.row[i],&bm);
2069:       ISGetIndices(nest->isglobal.row[i],&bmindices);
2070:       MatGetOwnershipRange(B,&rstart,NULL);
2071:       for (br = 0; br < bm; ++br) {
2072:         PetscInt          row = bmindices[br], brncols,  *cols;
2073:         const PetscInt    *brcols;
2074:         const PetscScalar *brcoldata;
2075:         MatGetRow(B,br+rstart,&brncols,&brcols,&brcoldata);
2076:         PetscMalloc1(brncols,&cols);
2077:         for (k=0; k<brncols; k++) cols[k] = bNindices[brcols[k]];
2078:         /*
2079:           Nest blocks are required to be nonoverlapping -- otherwise nest and monolithic index layouts wouldn't match.
2080:           Thus, we could use INSERT_VALUES, but I prefer ADD_VALUES.
2081:          */
2082:         MatSetValues(C,1,&row,brncols,cols,brcoldata,ADD_VALUES);
2083:         MatRestoreRow(B,br+rstart,&brncols,&brcols,&brcoldata);
2084:         PetscFree(cols);
2085:       }
2086:       ISRestoreIndices(nest->isglobal.row[i],&bmindices);
2087:     }
2088:     ISRestoreIndices(bNis,&bNindices);
2089:     ISDestroy(&bNis);
2090:   }
2091:   MatAssemblyBegin(C,MAT_FINAL_ASSEMBLY);
2092:   MatAssemblyEnd(C,MAT_FINAL_ASSEMBLY);
2093:   return(0);
2094: }

2096: PetscErrorCode MatHasOperation_Nest(Mat mat,MatOperation op,PetscBool *has)
2097: {
2098:   Mat_Nest       *bA = (Mat_Nest*)mat->data;
2099:   PetscInt       i,j,nr = bA->nr,nc = bA->nc;
2100:   PetscBool      flg;

2104:   *has = PETSC_FALSE;
2105:   if (op == MATOP_MULT_TRANSPOSE || op == MATOP_MAT_MULT) {
2106:     for (j=0; j<nc; j++) {
2107:       for (i=0; i<nr; i++) {
2108:         if (!bA->m[i][j]) continue;
2109:         MatHasOperation(bA->m[i][j],op,&flg);
2110:         if (!flg) return(0);
2111:       }
2112:     }
2113:   }
2114:   if (((void**)mat->ops)[op] || (op == MATOP_MAT_MULT && flg)) *has = PETSC_TRUE;
2115:   return(0);
2116: }

2118: /*MC
2119:   MATNEST - MATNEST = "nest" - Matrix type consisting of nested submatrices, each stored separately.

2121:   Level: intermediate

2123:   Notes:
2124:   This matrix type permits scalable use of PCFieldSplit and avoids the large memory costs of extracting submatrices.
2125:   It allows the use of symmetric and block formats for parts of multi-physics simulations.
2126:   It is usually used with DMComposite and DMCreateMatrix()

2128:   Each of the submatrices lives on the same MPI communicator as the original nest matrix (though they can have zero
2129:   rows/columns on some processes.) Thus this is not meant for cases where the submatrices live on far fewer processes
2130:   than the nest matrix.

2132: .seealso: MatCreate(), MatType, MatCreateNest(), MatNestSetSubMat(), MatNestGetSubMat(),
2133:           VecCreateNest(), DMCreateMatrix(), DMCOMPOSITE, MatNestSetVecType(), MatNestGetLocalISs(),
2134:           MatNestGetISs(), MatNestSetSubMats(), MatNestGetSubMats()
2135: M*/
2136: PETSC_EXTERN PetscErrorCode MatCreate_Nest(Mat A)
2137: {
2138:   Mat_Nest       *s;

2142:   PetscNewLog(A,&s);
2143:   A->data = (void*)s;

2145:   s->nr            = -1;
2146:   s->nc            = -1;
2147:   s->m             = NULL;
2148:   s->splitassembly = PETSC_FALSE;

2150:   PetscMemzero(A->ops,sizeof(*A->ops));

2152:   A->ops->mult                  = MatMult_Nest;
2153:   A->ops->multadd               = MatMultAdd_Nest;
2154:   A->ops->multtranspose         = MatMultTranspose_Nest;
2155:   A->ops->multtransposeadd      = MatMultTransposeAdd_Nest;
2156:   A->ops->transpose             = MatTranspose_Nest;
2157:   A->ops->assemblybegin         = MatAssemblyBegin_Nest;
2158:   A->ops->assemblyend           = MatAssemblyEnd_Nest;
2159:   A->ops->zeroentries           = MatZeroEntries_Nest;
2160:   A->ops->copy                  = MatCopy_Nest;
2161:   A->ops->axpy                  = MatAXPY_Nest;
2162:   A->ops->duplicate             = MatDuplicate_Nest;
2163:   A->ops->createsubmatrix       = MatCreateSubMatrix_Nest;
2164:   A->ops->destroy               = MatDestroy_Nest;
2165:   A->ops->view                  = MatView_Nest;
2166:   A->ops->getvecs               = 0; /* Use VECNEST by calling MatNestSetVecType(A,VECNEST) */
2167:   A->ops->getlocalsubmatrix     = MatGetLocalSubMatrix_Nest;
2168:   A->ops->restorelocalsubmatrix = MatRestoreLocalSubMatrix_Nest;
2169:   A->ops->getdiagonal           = MatGetDiagonal_Nest;
2170:   A->ops->diagonalscale         = MatDiagonalScale_Nest;
2171:   A->ops->scale                 = MatScale_Nest;
2172:   A->ops->shift                 = MatShift_Nest;
2173:   A->ops->diagonalset           = MatDiagonalSet_Nest;
2174:   A->ops->setrandom             = MatSetRandom_Nest;
2175:   A->ops->hasoperation          = MatHasOperation_Nest;
2176:   A->ops->missingdiagonal       = MatMissingDiagonal_Nest;

2178:   A->spptr        = 0;
2179:   A->assembled    = PETSC_FALSE;

2181:   /* expose Nest api's */
2182:   PetscObjectComposeFunction((PetscObject)A,"MatNestGetSubMat_C",        MatNestGetSubMat_Nest);
2183:   PetscObjectComposeFunction((PetscObject)A,"MatNestSetSubMat_C",        MatNestSetSubMat_Nest);
2184:   PetscObjectComposeFunction((PetscObject)A,"MatNestGetSubMats_C",       MatNestGetSubMats_Nest);
2185:   PetscObjectComposeFunction((PetscObject)A,"MatNestGetSize_C",          MatNestGetSize_Nest);
2186:   PetscObjectComposeFunction((PetscObject)A,"MatNestGetISs_C",           MatNestGetISs_Nest);
2187:   PetscObjectComposeFunction((PetscObject)A,"MatNestGetLocalISs_C",      MatNestGetLocalISs_Nest);
2188:   PetscObjectComposeFunction((PetscObject)A,"MatNestSetVecType_C",       MatNestSetVecType_Nest);
2189:   PetscObjectComposeFunction((PetscObject)A,"MatNestSetSubMats_C",       MatNestSetSubMats_Nest);
2190:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_nest_mpiaij_C",  MatConvert_Nest_AIJ);
2191:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_nest_seqaij_C",  MatConvert_Nest_AIJ);
2192:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_nest_aij_C",     MatConvert_Nest_AIJ);
2193:   PetscObjectComposeFunction((PetscObject)A,"MatConvert_nest_is_C",      MatConvert_Nest_IS);
2194:   PetscObjectComposeFunction((PetscObject)A,"MatProductSetFromOptions_nest_seqdense_C",MatProductSetFromOptions_Nest_Dense);
2195:   PetscObjectComposeFunction((PetscObject)A,"MatProductSetFromOptions_nest_mpidense_C",MatProductSetFromOptions_Nest_Dense);
2196:   PetscObjectComposeFunction((PetscObject)A,"MatProductSetFromOptions_nest_dense_C",MatProductSetFromOptions_Nest_Dense);

2198:   PetscObjectChangeTypeName((PetscObject)A,MATNEST);
2199:   return(0);
2200: }