Actual source code: bjacobi.c

petsc-master 2017-06-22
Report Typos and Errors

  2: /*
  3:    Defines a block Jacobi preconditioner.
  4: */

  6:  #include <../src/ksp/pc/impls/bjacobi/bjacobi.h>

  8: static PetscErrorCode PCSetUp_BJacobi_Singleblock(PC,Mat,Mat);
  9: static PetscErrorCode PCSetUp_BJacobi_Multiblock(PC,Mat,Mat);
 10: static PetscErrorCode PCSetUp_BJacobi_Multiproc(PC);

 12: static PetscErrorCode PCSetUp_BJacobi(PC pc)
 13: {
 14:   PC_BJacobi     *jac = (PC_BJacobi*)pc->data;
 15:   Mat            mat  = pc->mat,pmat = pc->pmat;
 17:   void           (*f)(void);
 18:   PetscInt       N,M,start,i,sum,end;
 19:   PetscInt       bs,i_start=-1,i_end=-1;
 20:   PetscMPIInt    rank,size;
 21:   const char     *pprefix,*mprefix;

 24:   MPI_Comm_rank(PetscObjectComm((PetscObject)pc),&rank);
 25:   MPI_Comm_size(PetscObjectComm((PetscObject)pc),&size);
 26:   MatGetLocalSize(pc->pmat,&M,&N);
 27:   MatGetBlockSize(pc->pmat,&bs);

 29:   if (jac->n > 0 && jac->n < size) {
 30:     PCSetUp_BJacobi_Multiproc(pc);
 31:     return(0);
 32:   }

 34:   /* --------------------------------------------------------------------------
 35:       Determines the number of blocks assigned to each processor
 36:   -----------------------------------------------------------------------------*/

 38:   /*   local block count  given */
 39:   if (jac->n_local > 0 && jac->n < 0) {
 40:     MPIU_Allreduce(&jac->n_local,&jac->n,1,MPIU_INT,MPI_SUM,PetscObjectComm((PetscObject)pc));
 41:     if (jac->l_lens) { /* check that user set these correctly */
 42:       sum = 0;
 43:       for (i=0; i<jac->n_local; i++) {
 44:         if (jac->l_lens[i]/bs*bs !=jac->l_lens[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat blocksize doesn't match block Jacobi layout");
 45:         sum += jac->l_lens[i];
 46:       }
 47:       if (sum != M) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Local lens set incorrectly");
 48:     } else {
 49:       PetscMalloc1(jac->n_local,&jac->l_lens);
 50:       for (i=0; i<jac->n_local; i++) jac->l_lens[i] = bs*((M/bs)/jac->n_local + (((M/bs) % jac->n_local) > i));
 51:     }
 52:   } else if (jac->n > 0 && jac->n_local < 0) { /* global block count given */
 53:     /* global blocks given: determine which ones are local */
 54:     if (jac->g_lens) {
 55:       /* check if the g_lens is has valid entries */
 56:       for (i=0; i<jac->n; i++) {
 57:         if (!jac->g_lens[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Zero block not allowed");
 58:         if (jac->g_lens[i]/bs*bs != jac->g_lens[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Mat blocksize doesn't match block Jacobi layout");
 59:       }
 60:       if (size == 1) {
 61:         jac->n_local = jac->n;
 62:         PetscMalloc1(jac->n_local,&jac->l_lens);
 63:         PetscMemcpy(jac->l_lens,jac->g_lens,jac->n_local*sizeof(PetscInt));
 64:         /* check that user set these correctly */
 65:         sum = 0;
 66:         for (i=0; i<jac->n_local; i++) sum += jac->l_lens[i];
 67:         if (sum != M) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Global lens set incorrectly");
 68:       } else {
 69:         MatGetOwnershipRange(pc->pmat,&start,&end);
 70:         /* loop over blocks determing first one owned by me */
 71:         sum = 0;
 72:         for (i=0; i<jac->n+1; i++) {
 73:           if (sum == start) { i_start = i; goto start_1;}
 74:           if (i < jac->n) sum += jac->g_lens[i];
 75:         }
 76:         SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Block sizes used in PCBJacobiSetTotalBlocks()\nare not compatible with parallel matrix layout");
 77: start_1:
 78:         for (i=i_start; i<jac->n+1; i++) {
 79:           if (sum == end) { i_end = i; goto end_1; }
 80:           if (i < jac->n) sum += jac->g_lens[i];
 81:         }
 82:         SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Block sizes used in PCBJacobiSetTotalBlocks()\nare not compatible with parallel matrix layout");
 83: end_1:
 84:         jac->n_local = i_end - i_start;
 85:         PetscMalloc1(jac->n_local,&jac->l_lens);
 86:         PetscMemcpy(jac->l_lens,jac->g_lens+i_start,jac->n_local*sizeof(PetscInt));
 87:       }
 88:     } else { /* no global blocks given, determine then using default layout */
 89:       jac->n_local = jac->n/size + ((jac->n % size) > rank);
 90:       PetscMalloc1(jac->n_local,&jac->l_lens);
 91:       for (i=0; i<jac->n_local; i++) {
 92:         jac->l_lens[i] = ((M/bs)/jac->n_local + (((M/bs) % jac->n_local) > i))*bs;
 93:         if (!jac->l_lens[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Too many blocks given");
 94:       }
 95:     }
 96:   } else if (jac->n < 0 && jac->n_local < 0) { /* no blocks given */
 97:     jac->n         = size;
 98:     jac->n_local   = 1;
 99:     PetscMalloc1(1,&jac->l_lens);
100:     jac->l_lens[0] = M;
101:   } else { /* jac->n > 0 && jac->n_local > 0 */
102:     if (!jac->l_lens) {
103:       PetscMalloc1(jac->n_local,&jac->l_lens);
104:       for (i=0; i<jac->n_local; i++) jac->l_lens[i] = bs*((M/bs)/jac->n_local + (((M/bs) % jac->n_local) > i));
105:     }
106:   }
107:   if (jac->n_local < 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Number of blocks is less than number of processors");

109:   /* -------------------------
110:       Determines mat and pmat
111:   ---------------------------*/
112:   MatShellGetOperation(pc->mat,MATOP_GET_DIAGONAL_BLOCK,&f);
113:   if (!f && size == 1) {
114:     mat  = pc->mat;
115:     pmat = pc->pmat;
116:   } else {
117:     if (pc->useAmat) {
118:       /* use block from Amat matrix, not Pmat for local MatMult() */
119:       MatGetDiagonalBlock(pc->mat,&mat);
120:       /* make submatrix have same prefix as entire matrix */
121:       PetscObjectGetOptionsPrefix((PetscObject)pc->mat,&mprefix);
122:       PetscObjectSetOptionsPrefix((PetscObject)mat,mprefix);
123:     }
124:     if (pc->pmat != pc->mat || !pc->useAmat) {
125:       MatGetDiagonalBlock(pc->pmat,&pmat);
126:       /* make submatrix have same prefix as entire matrix */
127:       PetscObjectGetOptionsPrefix((PetscObject)pc->pmat,&pprefix);
128:       PetscObjectSetOptionsPrefix((PetscObject)pmat,pprefix);
129:     } else pmat = mat;
130:   }

132:   /* ------
133:      Setup code depends on the number of blocks
134:   */
135:   if (jac->n_local == 1) {
136:     PCSetUp_BJacobi_Singleblock(pc,mat,pmat);
137:   } else {
138:     PCSetUp_BJacobi_Multiblock(pc,mat,pmat);
139:   }
140:   return(0);
141: }

143: /* Default destroy, if it has never been setup */
144: static PetscErrorCode PCDestroy_BJacobi(PC pc)
145: {
146:   PC_BJacobi     *jac = (PC_BJacobi*)pc->data;

150:   PetscFree(jac->g_lens);
151:   PetscFree(jac->l_lens);
152:   PetscFree(pc->data);
153:   return(0);
154: }


157: static PetscErrorCode PCSetFromOptions_BJacobi(PetscOptionItems *PetscOptionsObject,PC pc)
158: {
159:   PC_BJacobi     *jac = (PC_BJacobi*)pc->data;
161:   PetscInt       blocks,i;
162:   PetscBool      flg;

165:   PetscOptionsHead(PetscOptionsObject,"Block Jacobi options");
166:   PetscOptionsInt("-pc_bjacobi_blocks","Total number of blocks","PCBJacobiSetTotalBlocks",jac->n,&blocks,&flg);
167:   if (flg) {
168:     PCBJacobiSetTotalBlocks(pc,blocks,NULL);
169:   }
170:   if (jac->ksp) {
171:     /* The sub-KSP has already been set up (e.g., PCSetUp_BJacobi_Singleblock), but KSPSetFromOptions was not called
172:      * unless we had already been called. */
173:     for (i=0; i<jac->n_local; i++) {
174:       KSPSetFromOptions(jac->ksp[i]);
175:     }
176:   }
177:   PetscOptionsTail();
178:   return(0);
179: }

181:  #include <petscdraw.h>
182: static PetscErrorCode PCView_BJacobi(PC pc,PetscViewer viewer)
183: {
184:   PC_BJacobi           *jac   = (PC_BJacobi*)pc->data;
185:   PC_BJacobi_Multiproc *mpjac = (PC_BJacobi_Multiproc*)jac->data;
186:   PetscErrorCode       ierr;
187:   PetscMPIInt          rank;
188:   PetscInt             i;
189:   PetscBool            iascii,isstring,isdraw;
190:   PetscViewer          sviewer;

193:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
194:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);
195:   PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
196:   if (iascii) {
197:     if (pc->useAmat) {
198:       PetscViewerASCIIPrintf(viewer,"  block Jacobi: using Amat local matrix, number of blocks = %D\n",jac->n);
199:     }
200:     PetscViewerASCIIPrintf(viewer,"  block Jacobi: number of blocks = %D\n",jac->n);
201:     MPI_Comm_rank(PetscObjectComm((PetscObject)pc),&rank);
202:     if (jac->same_local_solves) {
203:       PetscViewerASCIIPrintf(viewer,"  Local solve is same for all blocks, in the following KSP and PC objects:\n");
204:       if (jac->ksp && !jac->psubcomm) {
205:         PetscViewerGetSubViewer(viewer,PETSC_COMM_SELF,&sviewer);
206:         if (!rank) {
207:           PetscViewerASCIIPushTab(viewer);
208:           KSPView(jac->ksp[0],sviewer);
209:           PetscViewerASCIIPopTab(viewer);
210:         }
211:         PetscViewerRestoreSubViewer(viewer,PETSC_COMM_SELF,&sviewer);
212:       } else if (mpjac && jac->ksp && mpjac->psubcomm) {
213:         PetscViewerGetSubViewer(viewer,mpjac->psubcomm->child,&sviewer);
214:         if (!mpjac->psubcomm->color) {
215:           PetscViewerASCIIPushTab(viewer);
216:           KSPView(*(jac->ksp),sviewer);
217:           PetscViewerASCIIPopTab(viewer);
218:         }
219:         PetscViewerRestoreSubViewer(viewer,mpjac->psubcomm->child,&sviewer);
220:       }
221:     } else {
222:       PetscInt n_global;
223:       MPIU_Allreduce(&jac->n_local,&n_global,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)pc));
224:       PetscViewerASCIIPushSynchronized(viewer);
225:       PetscViewerASCIIPrintf(viewer,"  Local solve info for each block is in the following KSP and PC objects:\n");
226:       PetscViewerASCIISynchronizedPrintf(viewer,"[%d] number of local blocks = %D, first local block number = %D\n",
227:                                                 rank,jac->n_local,jac->first_local);
228:       PetscViewerASCIIPushTab(viewer);
229:       PetscViewerGetSubViewer(viewer,PETSC_COMM_SELF,&sviewer);
230:       for (i=0; i<jac->n_local; i++) {
231:         PetscViewerASCIISynchronizedPrintf(viewer,"[%d] local block number %D\n",rank,i);
232:         KSPView(jac->ksp[i],sviewer);
233:         PetscViewerASCIISynchronizedPrintf(viewer,"- - - - - - - - - - - - - - - - - -\n");
234:       }
235:       PetscViewerRestoreSubViewer(viewer,PETSC_COMM_SELF,&sviewer);
236:       PetscViewerASCIIPopTab(viewer);
237:       PetscViewerFlush(viewer);
238:       PetscViewerASCIIPopSynchronized(viewer);
239:     }
240:   } else if (isstring) {
241:     PetscViewerStringSPrintf(viewer," blks=%D",jac->n);
242:     PetscViewerGetSubViewer(viewer,PETSC_COMM_SELF,&sviewer);
243:     if (jac->ksp) {KSPView(jac->ksp[0],sviewer);}
244:     PetscViewerRestoreSubViewer(viewer,PETSC_COMM_SELF,&sviewer);
245:   } else if (isdraw) {
246:     PetscDraw draw;
247:     char      str[25];
248:     PetscReal x,y,bottom,h;

250:     PetscViewerDrawGetDraw(viewer,0,&draw);
251:     PetscDrawGetCurrentPoint(draw,&x,&y);
252:     PetscSNPrintf(str,25,"Number blocks %D",jac->n);
253:     PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_RED,PETSC_DRAW_BLACK,str,NULL,&h);
254:     bottom = y - h;
255:     PetscDrawPushCurrentPoint(draw,x,bottom);
256:     /* warning the communicator on viewer is different then on ksp in parallel */
257:     if (jac->ksp) {KSPView(jac->ksp[0],viewer);}
258:     PetscDrawPopCurrentPoint(draw);
259:   }
260:   return(0);
261: }

263: /* -------------------------------------------------------------------------------------*/

265: static PetscErrorCode  PCBJacobiGetSubKSP_BJacobi(PC pc,PetscInt *n_local,PetscInt *first_local,KSP **ksp)
266: {
267:   PC_BJacobi *jac = (PC_BJacobi*)pc->data;;

270:   if (!pc->setupcalled) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Must call KSPSetUp() or PCSetUp() first");

272:   if (n_local) *n_local = jac->n_local;
273:   if (first_local) *first_local = jac->first_local;
274:   *ksp                   = jac->ksp;
275:   jac->same_local_solves = PETSC_FALSE;        /* Assume that local solves are now different;
276:                                                   not necessarily true though!  This flag is
277:                                                   used only for PCView_BJacobi() */
278:   return(0);
279: }

281: static PetscErrorCode  PCBJacobiSetTotalBlocks_BJacobi(PC pc,PetscInt blocks,PetscInt *lens)
282: {
283:   PC_BJacobi     *jac = (PC_BJacobi*)pc->data;

287:   if (pc->setupcalled > 0 && jac->n!=blocks) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ORDER,"Cannot alter number of blocks after PCSetUp()/KSPSetUp() has been called");
288:   jac->n = blocks;
289:   if (!lens) jac->g_lens = 0;
290:   else {
291:     PetscMalloc1(blocks,&jac->g_lens);
292:     PetscLogObjectMemory((PetscObject)pc,blocks*sizeof(PetscInt));
293:     PetscMemcpy(jac->g_lens,lens,blocks*sizeof(PetscInt));
294:   }
295:   return(0);
296: }

298: static PetscErrorCode  PCBJacobiGetTotalBlocks_BJacobi(PC pc, PetscInt *blocks, const PetscInt *lens[])
299: {
300:   PC_BJacobi *jac = (PC_BJacobi*) pc->data;

303:   *blocks = jac->n;
304:   if (lens) *lens = jac->g_lens;
305:   return(0);
306: }

308: static PetscErrorCode  PCBJacobiSetLocalBlocks_BJacobi(PC pc,PetscInt blocks,const PetscInt lens[])
309: {
310:   PC_BJacobi     *jac;

314:   jac = (PC_BJacobi*)pc->data;

316:   jac->n_local = blocks;
317:   if (!lens) jac->l_lens = 0;
318:   else {
319:     PetscMalloc1(blocks,&jac->l_lens);
320:     PetscLogObjectMemory((PetscObject)pc,blocks*sizeof(PetscInt));
321:     PetscMemcpy(jac->l_lens,lens,blocks*sizeof(PetscInt));
322:   }
323:   return(0);
324: }

326: static PetscErrorCode  PCBJacobiGetLocalBlocks_BJacobi(PC pc, PetscInt *blocks, const PetscInt *lens[])
327: {
328:   PC_BJacobi *jac = (PC_BJacobi*) pc->data;

331:   *blocks = jac->n_local;
332:   if (lens) *lens = jac->l_lens;
333:   return(0);
334: }

336: /* -------------------------------------------------------------------------------------*/

338: /*@C
339:    PCBJacobiGetSubKSP - Gets the local KSP contexts for all blocks on
340:    this processor.

342:    Note Collective

344:    Input Parameter:
345: .  pc - the preconditioner context

347:    Output Parameters:
348: +  n_local - the number of blocks on this processor, or NULL
349: .  first_local - the global number of the first block on this processor, or NULL
350: -  ksp - the array of KSP contexts

352:    Notes:
353:    After PCBJacobiGetSubKSP() the array of KSP contexts is not to be freed.

355:    Currently for some matrix implementations only 1 block per processor
356:    is supported.

358:    You must call KSPSetUp() or PCSetUp() before calling PCBJacobiGetSubKSP().

360:    Fortran Usage: You must pass in a KSP array that is large enough to contain all the local KSPs.
361:       You can call PCBJacobiGetSubKSP(pc,nlocal,firstlocal,PETSC_NULL_KSP,ierr) to determine how large the
362:       KSP array must be.

364:    Level: advanced

366: .keywords:  block, Jacobi, get, sub, KSP, context

368: .seealso: PCBJacobiGetSubKSP()
369: @*/
370: PetscErrorCode  PCBJacobiGetSubKSP(PC pc,PetscInt *n_local,PetscInt *first_local,KSP *ksp[])
371: {

376:   PetscUseMethod(pc,"PCBJacobiGetSubKSP_C",(PC,PetscInt*,PetscInt*,KSP **),(pc,n_local,first_local,ksp));
377:   return(0);
378: }

380: /*@
381:    PCBJacobiSetTotalBlocks - Sets the global number of blocks for the block
382:    Jacobi preconditioner.

384:    Collective on PC

386:    Input Parameters:
387: +  pc - the preconditioner context
388: .  blocks - the number of blocks
389: -  lens - [optional] integer array containing the size of each block

391:    Options Database Key:
392: .  -pc_bjacobi_blocks <blocks> - Sets the number of global blocks

394:    Notes:
395:    Currently only a limited number of blocking configurations are supported.
396:    All processors sharing the PC must call this routine with the same data.

398:    Level: intermediate

400: .keywords:  set, number, Jacobi, global, total, blocks

402: .seealso: PCSetUseAmat(), PCBJacobiSetLocalBlocks()
403: @*/
404: PetscErrorCode  PCBJacobiSetTotalBlocks(PC pc,PetscInt blocks,const PetscInt lens[])
405: {

410:   if (blocks <= 0) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_OUTOFRANGE,"Must have positive blocks");
411:   PetscTryMethod(pc,"PCBJacobiSetTotalBlocks_C",(PC,PetscInt,const PetscInt[]),(pc,blocks,lens));
412:   return(0);
413: }

415: /*@C
416:    PCBJacobiGetTotalBlocks - Gets the global number of blocks for the block
417:    Jacobi preconditioner.

419:    Not Collective

421:    Input Parameter:
422: .  pc - the preconditioner context

424:    Output parameters:
425: +  blocks - the number of blocks
426: -  lens - integer array containing the size of each block

428:    Level: intermediate

430: .keywords:  get, number, Jacobi, global, total, blocks

432: .seealso: PCSetUseAmat(), PCBJacobiGetLocalBlocks()
433: @*/
434: PetscErrorCode  PCBJacobiGetTotalBlocks(PC pc, PetscInt *blocks, const PetscInt *lens[])
435: {

441:   PetscUseMethod(pc,"PCBJacobiGetTotalBlocks_C",(PC,PetscInt*, const PetscInt *[]),(pc,blocks,lens));
442:   return(0);
443: }

445: /*@
446:    PCBJacobiSetLocalBlocks - Sets the local number of blocks for the block
447:    Jacobi preconditioner.

449:    Not Collective

451:    Input Parameters:
452: +  pc - the preconditioner context
453: .  blocks - the number of blocks
454: -  lens - [optional] integer array containing size of each block

456:    Note:
457:    Currently only a limited number of blocking configurations are supported.

459:    Level: intermediate

461: .keywords: PC, set, number, Jacobi, local, blocks

463: .seealso: PCSetUseAmat(), PCBJacobiSetTotalBlocks()
464: @*/
465: PetscErrorCode  PCBJacobiSetLocalBlocks(PC pc,PetscInt blocks,const PetscInt lens[])
466: {

471:   if (blocks < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Must have nonegative blocks");
472:   PetscTryMethod(pc,"PCBJacobiSetLocalBlocks_C",(PC,PetscInt,const PetscInt []),(pc,blocks,lens));
473:   return(0);
474: }

476: /*@C
477:    PCBJacobiGetLocalBlocks - Gets the local number of blocks for the block
478:    Jacobi preconditioner.

480:    Not Collective

482:    Input Parameters:
483: +  pc - the preconditioner context
484: .  blocks - the number of blocks
485: -  lens - [optional] integer array containing size of each block

487:    Note:
488:    Currently only a limited number of blocking configurations are supported.

490:    Level: intermediate

492: .keywords: PC, get, number, Jacobi, local, blocks

494: .seealso: PCSetUseAmat(), PCBJacobiGetTotalBlocks()
495: @*/
496: PetscErrorCode  PCBJacobiGetLocalBlocks(PC pc, PetscInt *blocks, const PetscInt *lens[])
497: {

503:   PetscUseMethod(pc,"PCBJacobiGetLocalBlocks_C",(PC,PetscInt*, const PetscInt *[]),(pc,blocks,lens));
504:   return(0);
505: }

507: /* -----------------------------------------------------------------------------------*/

509: /*MC
510:    PCBJACOBI - Use block Jacobi preconditioning, each block is (approximately) solved with
511:            its own KSP object.

513:    Options Database Keys:
514: .  -pc_use_amat - use Amat to apply block of operator in inner Krylov method

516:    Notes: Each processor can have one or more blocks, but a block cannot be shared by more
517:      than one processor. Defaults to one block per processor.

519:      To set options on the solvers for each block append -sub_ to all the KSP, KSP, and PC
520:         options database keys. For example, -sub_pc_type ilu -sub_pc_factor_levels 1 -sub_ksp_type preonly

522:      To set the options on the solvers separate for each block call PCBJacobiGetSubKSP()
523:          and set the options directly on the resulting KSP object (you can access its PC
524:          KSPGetPC())

526:      For GPU-based vectors (CUDA, CUSP, ViennaCL) it is recommended to use exactly one block per MPI process for best
527:          performance.  Different block partitioning may lead to additional data transfers
528:          between host and GPU that lead to degraded performance.

530:    Level: beginner

532:    Concepts: block Jacobi


535: .seealso:  PCCreate(), PCSetType(), PCType (for list of available types), PC,
536:            PCASM, PCSetUseAmat(), PCGetUseAmat(), PCBJacobiGetSubKSP(), PCBJacobiSetTotalBlocks(),
537:            PCBJacobiSetLocalBlocks(), PCSetModifySubmatrices()
538: M*/

540: PETSC_EXTERN PetscErrorCode PCCreate_BJacobi(PC pc)
541: {
543:   PetscMPIInt    rank;
544:   PC_BJacobi     *jac;

547:   PetscNewLog(pc,&jac);
548:   MPI_Comm_rank(PetscObjectComm((PetscObject)pc),&rank);

550:   pc->ops->apply           = 0;
551:   pc->ops->applytranspose  = 0;
552:   pc->ops->setup           = PCSetUp_BJacobi;
553:   pc->ops->destroy         = PCDestroy_BJacobi;
554:   pc->ops->setfromoptions  = PCSetFromOptions_BJacobi;
555:   pc->ops->view            = PCView_BJacobi;
556:   pc->ops->applyrichardson = 0;

558:   pc->data               = (void*)jac;
559:   jac->n                 = -1;
560:   jac->n_local           = -1;
561:   jac->first_local       = rank;
562:   jac->ksp               = 0;
563:   jac->same_local_solves = PETSC_TRUE;
564:   jac->g_lens            = 0;
565:   jac->l_lens            = 0;
566:   jac->psubcomm          = 0;

568:   PetscObjectComposeFunction((PetscObject)pc,"PCBJacobiGetSubKSP_C",PCBJacobiGetSubKSP_BJacobi);
569:   PetscObjectComposeFunction((PetscObject)pc,"PCBJacobiSetTotalBlocks_C",PCBJacobiSetTotalBlocks_BJacobi);
570:   PetscObjectComposeFunction((PetscObject)pc,"PCBJacobiGetTotalBlocks_C",PCBJacobiGetTotalBlocks_BJacobi);
571:   PetscObjectComposeFunction((PetscObject)pc,"PCBJacobiSetLocalBlocks_C",PCBJacobiSetLocalBlocks_BJacobi);
572:   PetscObjectComposeFunction((PetscObject)pc,"PCBJacobiGetLocalBlocks_C",PCBJacobiGetLocalBlocks_BJacobi);
573:   return(0);
574: }

576: /* --------------------------------------------------------------------------------------------*/
577: /*
578:         These are for a single block per processor; works for AIJ, BAIJ; Seq and MPI
579: */
580: static PetscErrorCode PCReset_BJacobi_Singleblock(PC pc)
581: {
582:   PC_BJacobi             *jac  = (PC_BJacobi*)pc->data;
583:   PC_BJacobi_Singleblock *bjac = (PC_BJacobi_Singleblock*)jac->data;
584:   PetscErrorCode         ierr;

587:   KSPReset(jac->ksp[0]);
588:   VecDestroy(&bjac->x);
589:   VecDestroy(&bjac->y);
590:   return(0);
591: }

593: static PetscErrorCode PCDestroy_BJacobi_Singleblock(PC pc)
594: {
595:   PC_BJacobi             *jac  = (PC_BJacobi*)pc->data;
596:   PC_BJacobi_Singleblock *bjac = (PC_BJacobi_Singleblock*)jac->data;
597:   PetscErrorCode         ierr;

600:   PCReset_BJacobi_Singleblock(pc);
601:   KSPDestroy(&jac->ksp[0]);
602:   PetscFree(jac->ksp);
603:   PetscFree(jac->l_lens);
604:   PetscFree(jac->g_lens);
605:   PetscFree(bjac);
606:   PetscFree(pc->data);
607:   return(0);
608: }

610: static PetscErrorCode PCSetUpOnBlocks_BJacobi_Singleblock(PC pc)
611: {
612:   PetscErrorCode     ierr;
613:   PC_BJacobi         *jac = (PC_BJacobi*)pc->data;
614:   KSP                subksp = jac->ksp[0];
615:   KSPConvergedReason reason;

618:   KSPSetUp(subksp);
619:   KSPGetConvergedReason(subksp,&reason);
620:   if (reason == KSP_DIVERGED_PCSETUP_FAILED) {
621:     pc->failedreason = PC_SUBPC_ERROR;
622:   }
623:   return(0);
624: }

626: static PetscErrorCode PCApply_BJacobi_Singleblock(PC pc,Vec x,Vec y)
627: {
628:   PetscErrorCode         ierr;
629:   PC_BJacobi             *jac  = (PC_BJacobi*)pc->data;
630:   PC_BJacobi_Singleblock *bjac = (PC_BJacobi_Singleblock*)jac->data;

633:   VecGetLocalVectorRead(x, bjac->x);
634:   VecGetLocalVector(y, bjac->y);
635:  /* Since the inner KSP matrix may point directly to the diagonal block of an MPI matrix the inner
636:      matrix may change even if the outter KSP/PC has not updated the preconditioner, this will trigger a rebuild
637:      of the inner preconditioner automatically unless we pass down the outter preconditioners reuse flag.*/
638:   KSPSetReusePreconditioner(jac->ksp[0],pc->reusepreconditioner);
639:   KSPSolve(jac->ksp[0],bjac->x,bjac->y);
640:   VecRestoreLocalVectorRead(x, bjac->x);
641:   VecRestoreLocalVector(y, bjac->y);
642:   return(0);
643: }

645: static PetscErrorCode PCApplySymmetricLeft_BJacobi_Singleblock(PC pc,Vec x,Vec y)
646: {
647:   PetscErrorCode         ierr;
648:   PC_BJacobi             *jac  = (PC_BJacobi*)pc->data;
649:   PC_BJacobi_Singleblock *bjac = (PC_BJacobi_Singleblock*)jac->data;
650:   PetscScalar            *y_array;
651:   const PetscScalar      *x_array;
652:   PC                     subpc;

655:   /*
656:       The VecPlaceArray() is to avoid having to copy the
657:     y vector into the bjac->x vector. The reason for
658:     the bjac->x vector is that we need a sequential vector
659:     for the sequential solve.
660:   */
661:   VecGetArrayRead(x,&x_array);
662:   VecGetArray(y,&y_array);
663:   VecPlaceArray(bjac->x,x_array);
664:   VecPlaceArray(bjac->y,y_array);
665:   /* apply the symmetric left portion of the inner PC operator */
666:   /* note this by-passes the inner KSP and its options completely */
667:   KSPGetPC(jac->ksp[0],&subpc);
668:   PCApplySymmetricLeft(subpc,bjac->x,bjac->y);
669:   VecResetArray(bjac->x);
670:   VecResetArray(bjac->y);
671:   VecRestoreArrayRead(x,&x_array);
672:   VecRestoreArray(y,&y_array);
673:   return(0);
674: }

676: static PetscErrorCode PCApplySymmetricRight_BJacobi_Singleblock(PC pc,Vec x,Vec y)
677: {
678:   PetscErrorCode         ierr;
679:   PC_BJacobi             *jac  = (PC_BJacobi*)pc->data;
680:   PC_BJacobi_Singleblock *bjac = (PC_BJacobi_Singleblock*)jac->data;
681:   PetscScalar            *y_array;
682:   const PetscScalar      *x_array;
683:   PC                     subpc;

686:   /*
687:       The VecPlaceArray() is to avoid having to copy the
688:     y vector into the bjac->x vector. The reason for
689:     the bjac->x vector is that we need a sequential vector
690:     for the sequential solve.
691:   */
692:   VecGetArrayRead(x,&x_array);
693:   VecGetArray(y,&y_array);
694:   VecPlaceArray(bjac->x,x_array);
695:   VecPlaceArray(bjac->y,y_array);

697:   /* apply the symmetric right portion of the inner PC operator */
698:   /* note this by-passes the inner KSP and its options completely */

700:   KSPGetPC(jac->ksp[0],&subpc);
701:   PCApplySymmetricRight(subpc,bjac->x,bjac->y);

703:   VecRestoreArrayRead(x,&x_array);
704:   VecRestoreArray(y,&y_array);
705:   return(0);
706: }

708: static PetscErrorCode PCApplyTranspose_BJacobi_Singleblock(PC pc,Vec x,Vec y)
709: {
710:   PetscErrorCode         ierr;
711:   PC_BJacobi             *jac  = (PC_BJacobi*)pc->data;
712:   PC_BJacobi_Singleblock *bjac = (PC_BJacobi_Singleblock*)jac->data;
713:   PetscScalar            *y_array;
714:   const PetscScalar      *x_array;

717:   /*
718:       The VecPlaceArray() is to avoid having to copy the
719:     y vector into the bjac->x vector. The reason for
720:     the bjac->x vector is that we need a sequential vector
721:     for the sequential solve.
722:   */
723:   VecGetArrayRead(x,&x_array);
724:   VecGetArray(y,&y_array);
725:   VecPlaceArray(bjac->x,x_array);
726:   VecPlaceArray(bjac->y,y_array);
727:   KSPSolveTranspose(jac->ksp[0],bjac->x,bjac->y);
728:   VecResetArray(bjac->x);
729:   VecResetArray(bjac->y);
730:   VecRestoreArrayRead(x,&x_array);
731:   VecRestoreArray(y,&y_array);
732:   return(0);
733: }

735: static PetscErrorCode PCSetUp_BJacobi_Singleblock(PC pc,Mat mat,Mat pmat)
736: {
737:   PC_BJacobi             *jac = (PC_BJacobi*)pc->data;
738:   PetscErrorCode         ierr;
739:   PetscInt               m;
740:   KSP                    ksp;
741:   PC_BJacobi_Singleblock *bjac;
742:   PetscBool              wasSetup = PETSC_TRUE;

745:   if (!pc->setupcalled) {
746:     const char *prefix;

748:     if (!jac->ksp) {
749:       wasSetup = PETSC_FALSE;

751:       KSPCreate(PETSC_COMM_SELF,&ksp);
752:       KSPSetErrorIfNotConverged(ksp,pc->erroriffailure);
753:       PetscObjectIncrementTabLevel((PetscObject)ksp,(PetscObject)pc,1);
754:       PetscLogObjectParent((PetscObject)pc,(PetscObject)ksp);
755:       KSPSetType(ksp,KSPPREONLY);
756:       PCGetOptionsPrefix(pc,&prefix);
757:       KSPSetOptionsPrefix(ksp,prefix);
758:       KSPAppendOptionsPrefix(ksp,"sub_");

760:       pc->ops->reset               = PCReset_BJacobi_Singleblock;
761:       pc->ops->destroy             = PCDestroy_BJacobi_Singleblock;
762:       pc->ops->apply               = PCApply_BJacobi_Singleblock;
763:       pc->ops->applysymmetricleft  = PCApplySymmetricLeft_BJacobi_Singleblock;
764:       pc->ops->applysymmetricright = PCApplySymmetricRight_BJacobi_Singleblock;
765:       pc->ops->applytranspose      = PCApplyTranspose_BJacobi_Singleblock;
766:       pc->ops->setuponblocks       = PCSetUpOnBlocks_BJacobi_Singleblock;

768:       PetscMalloc1(1,&jac->ksp);
769:       jac->ksp[0] = ksp;

771:       PetscNewLog(pc,&bjac);
772:       jac->data = (void*)bjac;
773:     } else {
774:       ksp  = jac->ksp[0];
775:       bjac = (PC_BJacobi_Singleblock*)jac->data;
776:     }

778:     /*
779:       The reason we need to generate these vectors is to serve
780:       as the right-hand side and solution vector for the solve on the
781:       block. We do not need to allocate space for the vectors since
782:       that is provided via VecPlaceArray() just before the call to
783:       KSPSolve() on the block.
784:     */
785:     MatGetSize(pmat,&m,&m);
786:     VecCreateSeqWithArray(PETSC_COMM_SELF,1,m,NULL,&bjac->x);
787:     VecCreateSeqWithArray(PETSC_COMM_SELF,1,m,NULL,&bjac->y);
788: #if defined(PETSC_HAVE_CUSP)
789:     VecSetType(bjac->x,VECCUSP);
790:     VecSetType(bjac->y,VECCUSP);
791: #elif defined(PETSC_HAVE_VECCUDA)
792:     VecSetType(bjac->x,VECCUDA);
793:     VecSetType(bjac->y,VECCUDA);
794: #elif defined(PETSC_HAVE_VIENNACL)
795:     VecSetType(bjac->x,VECVIENNACL);
796:     VecSetType(bjac->y,VECVIENNACL);
797: #endif
798:     PetscLogObjectParent((PetscObject)pc,(PetscObject)bjac->x);
799:     PetscLogObjectParent((PetscObject)pc,(PetscObject)bjac->y);
800:   } else {
801:     ksp  = jac->ksp[0];
802:     bjac = (PC_BJacobi_Singleblock*)jac->data;
803:   }
804:   if (pc->useAmat) {
805:     KSPSetOperators(ksp,mat,pmat);
806:   } else {
807:     KSPSetOperators(ksp,pmat,pmat);
808:   }
809:   if (!wasSetup && pc->setfromoptionscalled) {
810:     /* If PCSetFromOptions_BJacobi is called later, KSPSetFromOptions will be called at that time. */
811:     KSPSetFromOptions(ksp);
812:   }
813:   return(0);
814: }

816: /* ---------------------------------------------------------------------------------------------*/
817: static PetscErrorCode PCReset_BJacobi_Multiblock(PC pc)
818: {
819:   PC_BJacobi            *jac  = (PC_BJacobi*)pc->data;
820:   PC_BJacobi_Multiblock *bjac = (PC_BJacobi_Multiblock*)jac->data;
821:   PetscErrorCode        ierr;
822:   PetscInt              i;

825:   if (bjac && bjac->pmat) {
826:     MatDestroyMatrices(jac->n_local,&bjac->pmat);
827:     if (pc->useAmat) {
828:       MatDestroyMatrices(jac->n_local,&bjac->mat);
829:     }
830:   }

832:   for (i=0; i<jac->n_local; i++) {
833:     KSPReset(jac->ksp[i]);
834:     if (bjac && bjac->x) {
835:       VecDestroy(&bjac->x[i]);
836:       VecDestroy(&bjac->y[i]);
837:       ISDestroy(&bjac->is[i]);
838:     }
839:   }
840:   PetscFree(jac->l_lens);
841:   PetscFree(jac->g_lens);
842:   return(0);
843: }

845: static PetscErrorCode PCDestroy_BJacobi_Multiblock(PC pc)
846: {
847:   PC_BJacobi            *jac  = (PC_BJacobi*)pc->data;
848:   PC_BJacobi_Multiblock *bjac = (PC_BJacobi_Multiblock*)jac->data;
849:   PetscErrorCode        ierr;
850:   PetscInt              i;

853:   PCReset_BJacobi_Multiblock(pc);
854:   if (bjac) {
855:     PetscFree2(bjac->x,bjac->y);
856:     PetscFree(bjac->starts);
857:     PetscFree(bjac->is);
858:   }
859:   PetscFree(jac->data);
860:   for (i=0; i<jac->n_local; i++) {
861:     KSPDestroy(&jac->ksp[i]);
862:   }
863:   PetscFree(jac->ksp);
864:   PetscFree(pc->data);
865:   return(0);
866: }

868: static PetscErrorCode PCSetUpOnBlocks_BJacobi_Multiblock(PC pc)
869: {
870:   PC_BJacobi         *jac = (PC_BJacobi*)pc->data;
871:   PetscErrorCode     ierr;
872:   PetscInt           i,n_local = jac->n_local;
873:   KSPConvergedReason reason;

876:   for (i=0; i<n_local; i++) {
877:     KSPSetUp(jac->ksp[i]);
878:     KSPGetConvergedReason(jac->ksp[i],&reason);
879:     if (reason == KSP_DIVERGED_PCSETUP_FAILED) {
880:       pc->failedreason = PC_SUBPC_ERROR;
881:     }
882:   }
883:   return(0);
884: }

886: /*
887:       Preconditioner for block Jacobi
888: */
889: static PetscErrorCode PCApply_BJacobi_Multiblock(PC pc,Vec x,Vec y)
890: {
891:   PC_BJacobi            *jac = (PC_BJacobi*)pc->data;
892:   PetscErrorCode        ierr;
893:   PetscInt              i,n_local = jac->n_local;
894:   PC_BJacobi_Multiblock *bjac = (PC_BJacobi_Multiblock*)jac->data;
895:   PetscScalar           *yin;
896:   const PetscScalar     *xin;

899:   VecGetArrayRead(x,&xin);
900:   VecGetArray(y,&yin);
901:   for (i=0; i<n_local; i++) {
902:     /*
903:        To avoid copying the subvector from x into a workspace we instead
904:        make the workspace vector array point to the subpart of the array of
905:        the global vector.
906:     */
907:     VecPlaceArray(bjac->x[i],xin+bjac->starts[i]);
908:     VecPlaceArray(bjac->y[i],yin+bjac->starts[i]);

910:     PetscLogEventBegin(PC_ApplyOnBlocks,jac->ksp[i],bjac->x[i],bjac->y[i],0);
911:     KSPSolve(jac->ksp[i],bjac->x[i],bjac->y[i]);
912:     PetscLogEventEnd(PC_ApplyOnBlocks,jac->ksp[i],bjac->x[i],bjac->y[i],0);

914:     VecResetArray(bjac->x[i]);
915:     VecResetArray(bjac->y[i]);
916:   }
917:   VecRestoreArrayRead(x,&xin);
918:   VecRestoreArray(y,&yin);
919:   return(0);
920: }

922: /*
923:       Preconditioner for block Jacobi
924: */
925: static PetscErrorCode PCApplyTranspose_BJacobi_Multiblock(PC pc,Vec x,Vec y)
926: {
927:   PC_BJacobi            *jac = (PC_BJacobi*)pc->data;
928:   PetscErrorCode        ierr;
929:   PetscInt              i,n_local = jac->n_local;
930:   PC_BJacobi_Multiblock *bjac = (PC_BJacobi_Multiblock*)jac->data;
931:   PetscScalar           *yin;
932:   const PetscScalar     *xin;

935:   VecGetArrayRead(x,&xin);
936:   VecGetArray(y,&yin);
937:   for (i=0; i<n_local; i++) {
938:     /*
939:        To avoid copying the subvector from x into a workspace we instead
940:        make the workspace vector array point to the subpart of the array of
941:        the global vector.
942:     */
943:     VecPlaceArray(bjac->x[i],xin+bjac->starts[i]);
944:     VecPlaceArray(bjac->y[i],yin+bjac->starts[i]);

946:     PetscLogEventBegin(PC_ApplyTransposeOnBlocks,jac->ksp[i],bjac->x[i],bjac->y[i],0);
947:     KSPSolveTranspose(jac->ksp[i],bjac->x[i],bjac->y[i]);
948:     PetscLogEventEnd(PC_ApplyTransposeOnBlocks,jac->ksp[i],bjac->x[i],bjac->y[i],0);

950:     VecResetArray(bjac->x[i]);
951:     VecResetArray(bjac->y[i]);
952:   }
953:   VecRestoreArrayRead(x,&xin);
954:   VecRestoreArray(y,&yin);
955:   return(0);
956: }

958: static PetscErrorCode PCSetUp_BJacobi_Multiblock(PC pc,Mat mat,Mat pmat)
959: {
960:   PC_BJacobi            *jac = (PC_BJacobi*)pc->data;
961:   PetscErrorCode        ierr;
962:   PetscInt              m,n_local,N,M,start,i;
963:   const char            *prefix,*pprefix,*mprefix;
964:   KSP                   ksp;
965:   Vec                   x,y;
966:   PC_BJacobi_Multiblock *bjac = (PC_BJacobi_Multiblock*)jac->data;
967:   PC                    subpc;
968:   IS                    is;
969:   MatReuse              scall;

972:   MatGetLocalSize(pc->pmat,&M,&N);

974:   n_local = jac->n_local;

976:   if (pc->useAmat) {
977:     PetscBool same;
978:     PetscObjectTypeCompare((PetscObject)mat,((PetscObject)pmat)->type_name,&same);
979:     if (!same) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_INCOMP,"Matrices not of same type");
980:   }

982:   if (!pc->setupcalled) {
983:     scall = MAT_INITIAL_MATRIX;

985:     if (!jac->ksp) {
986:       pc->ops->reset         = PCReset_BJacobi_Multiblock;
987:       pc->ops->destroy       = PCDestroy_BJacobi_Multiblock;
988:       pc->ops->apply         = PCApply_BJacobi_Multiblock;
989:       pc->ops->applytranspose= PCApplyTranspose_BJacobi_Multiblock;
990:       pc->ops->setuponblocks = PCSetUpOnBlocks_BJacobi_Multiblock;

992:       PetscNewLog(pc,&bjac);
993:       PetscMalloc1(n_local,&jac->ksp);
994:       PetscLogObjectMemory((PetscObject)pc,sizeof(n_local*sizeof(KSP)));
995:       PetscMalloc2(n_local,&bjac->x,n_local,&bjac->y);
996:       PetscMalloc1(n_local,&bjac->starts);
997:       PetscLogObjectMemory((PetscObject)pc,sizeof(n_local*sizeof(PetscScalar)));

999:       jac->data = (void*)bjac;
1000:       PetscMalloc1(n_local,&bjac->is);
1001:       PetscLogObjectMemory((PetscObject)pc,sizeof(n_local*sizeof(IS)));

1003:       for (i=0; i<n_local; i++) {
1004:         KSPCreate(PETSC_COMM_SELF,&ksp);
1005:         KSPSetErrorIfNotConverged(ksp,pc->erroriffailure);
1006:         PetscObjectIncrementTabLevel((PetscObject)ksp,(PetscObject)pc,1);
1007:         PetscLogObjectParent((PetscObject)pc,(PetscObject)ksp);
1008:         KSPSetType(ksp,KSPPREONLY);
1009:         KSPGetPC(ksp,&subpc);
1010:         PCGetOptionsPrefix(pc,&prefix);
1011:         KSPSetOptionsPrefix(ksp,prefix);
1012:         KSPAppendOptionsPrefix(ksp,"sub_");

1014:         jac->ksp[i] = ksp;
1015:       }
1016:     } else {
1017:       bjac = (PC_BJacobi_Multiblock*)jac->data;
1018:     }

1020:     start = 0;
1021:     for (i=0; i<n_local; i++) {
1022:       m = jac->l_lens[i];
1023:       /*
1024:       The reason we need to generate these vectors is to serve
1025:       as the right-hand side and solution vector for the solve on the
1026:       block. We do not need to allocate space for the vectors since
1027:       that is provided via VecPlaceArray() just before the call to
1028:       KSPSolve() on the block.

1030:       */
1031:       VecCreateSeq(PETSC_COMM_SELF,m,&x);
1032:       VecCreateSeqWithArray(PETSC_COMM_SELF,1,m,NULL,&y);
1033: #if defined(PETSC_HAVE_CUSP)
1034:       VecSetType(x,VECCUSP);
1035:       VecSetType(y,VECCUSP);
1036: #elif defined(PETSC_HAVE_VECCUDA)
1037:       VecSetType(x,VECCUDA);
1038:       VecSetType(y,VECCUDA);
1039: #elif defined(PETSC_HAVE_VIENNACL)
1040:       VecSetType(x,VECVIENNACL);
1041:       VecSetType(y,VECVIENNACL);
1042: #endif
1043:       PetscLogObjectParent((PetscObject)pc,(PetscObject)x);
1044:       PetscLogObjectParent((PetscObject)pc,(PetscObject)y);

1046:       bjac->x[i]      = x;
1047:       bjac->y[i]      = y;
1048:       bjac->starts[i] = start;

1050:       ISCreateStride(PETSC_COMM_SELF,m,start,1,&is);
1051:       bjac->is[i] = is;
1052:       PetscLogObjectParent((PetscObject)pc,(PetscObject)is);

1054:       start += m;
1055:     }
1056:   } else {
1057:     bjac = (PC_BJacobi_Multiblock*)jac->data;
1058:     /*
1059:        Destroy the blocks from the previous iteration
1060:     */
1061:     if (pc->flag == DIFFERENT_NONZERO_PATTERN) {
1062:       MatDestroyMatrices(n_local,&bjac->pmat);
1063:       if (pc->useAmat) {
1064:         MatDestroyMatrices(n_local,&bjac->mat);
1065:       }
1066:       scall = MAT_INITIAL_MATRIX;
1067:     } else scall = MAT_REUSE_MATRIX;
1068:   }

1070:   MatCreateSubMatrices(pmat,n_local,bjac->is,bjac->is,scall,&bjac->pmat);
1071:   if (pc->useAmat) {
1072:     PetscObjectGetOptionsPrefix((PetscObject)mat,&mprefix);
1073:     MatCreateSubMatrices(mat,n_local,bjac->is,bjac->is,scall,&bjac->mat);
1074:   }
1075:   /* Return control to the user so that the submatrices can be modified (e.g., to apply
1076:      different boundary conditions for the submatrices than for the global problem) */
1077:   PCModifySubMatrices(pc,n_local,bjac->is,bjac->is,bjac->pmat,pc->modifysubmatricesP);

1079:   PetscObjectGetOptionsPrefix((PetscObject)pmat,&pprefix);
1080:   for (i=0; i<n_local; i++) {
1081:     PetscLogObjectParent((PetscObject)pc,(PetscObject)bjac->pmat[i]);
1082:     PetscObjectSetOptionsPrefix((PetscObject)bjac->pmat[i],pprefix);
1083:     if (pc->useAmat) {
1084:       PetscLogObjectParent((PetscObject)pc,(PetscObject)bjac->mat[i]);
1085:       PetscObjectSetOptionsPrefix((PetscObject)bjac->mat[i],mprefix);
1086:       KSPSetOperators(jac->ksp[i],bjac->mat[i],bjac->pmat[i]);
1087:     } else {
1088:       KSPSetOperators(jac->ksp[i],bjac->pmat[i],bjac->pmat[i]);
1089:     }
1090:     if (pc->setfromoptionscalled) {
1091:       KSPSetFromOptions(jac->ksp[i]);
1092:     }
1093:   }
1094:   return(0);
1095: }

1097: /* ---------------------------------------------------------------------------------------------*/
1098: /*
1099:       These are for a single block with multiple processes;
1100: */
1101: static PetscErrorCode PCReset_BJacobi_Multiproc(PC pc)
1102: {
1103:   PC_BJacobi           *jac   = (PC_BJacobi*)pc->data;
1104:   PC_BJacobi_Multiproc *mpjac = (PC_BJacobi_Multiproc*)jac->data;
1105:   PetscErrorCode       ierr;

1108:   VecDestroy(&mpjac->ysub);
1109:   VecDestroy(&mpjac->xsub);
1110:   MatDestroy(&mpjac->submats);
1111:   if (jac->ksp) {KSPReset(jac->ksp[0]);}
1112:   return(0);
1113: }

1115: static PetscErrorCode PCDestroy_BJacobi_Multiproc(PC pc)
1116: {
1117:   PC_BJacobi           *jac   = (PC_BJacobi*)pc->data;
1118:   PC_BJacobi_Multiproc *mpjac = (PC_BJacobi_Multiproc*)jac->data;
1119:   PetscErrorCode       ierr;

1122:   PCReset_BJacobi_Multiproc(pc);
1123:   KSPDestroy(&jac->ksp[0]);
1124:   PetscFree(jac->ksp);
1125:   PetscSubcommDestroy(&mpjac->psubcomm);

1127:   PetscFree(mpjac);
1128:   PetscFree(pc->data);
1129:   return(0);
1130: }

1132: static PetscErrorCode PCApply_BJacobi_Multiproc(PC pc,Vec x,Vec y)
1133: {
1134:   PC_BJacobi           *jac   = (PC_BJacobi*)pc->data;
1135:   PC_BJacobi_Multiproc *mpjac = (PC_BJacobi_Multiproc*)jac->data;
1136:   PetscErrorCode       ierr;
1137:   PetscScalar          *yarray;
1138:   const PetscScalar    *xarray;
1139:   KSPConvergedReason   reason;

1142:   /* place x's and y's local arrays into xsub and ysub */
1143:   VecGetArrayRead(x,&xarray);
1144:   VecGetArray(y,&yarray);
1145:   VecPlaceArray(mpjac->xsub,xarray);
1146:   VecPlaceArray(mpjac->ysub,yarray);

1148:   /* apply preconditioner on each matrix block */
1149:   PetscLogEventBegin(PC_ApplyOnMproc,jac->ksp[0],mpjac->xsub,mpjac->ysub,0);
1150:   KSPSolve(jac->ksp[0],mpjac->xsub,mpjac->ysub);
1151:   PetscLogEventEnd(PC_ApplyOnMproc,jac->ksp[0],mpjac->xsub,mpjac->ysub,0);
1152:   KSPGetConvergedReason(jac->ksp[0],&reason);
1153:   if (reason == KSP_DIVERGED_PCSETUP_FAILED) {
1154:     pc->failedreason = PC_SUBPC_ERROR;
1155:   }

1157:   VecResetArray(mpjac->xsub);
1158:   VecResetArray(mpjac->ysub);
1159:   VecRestoreArrayRead(x,&xarray);
1160:   VecRestoreArray(y,&yarray);
1161:   return(0);
1162: }

1164: static PetscErrorCode PCSetUp_BJacobi_Multiproc(PC pc)
1165: {
1166:   PC_BJacobi           *jac   = (PC_BJacobi*)pc->data;
1167:   PC_BJacobi_Multiproc *mpjac = (PC_BJacobi_Multiproc*)jac->data;
1168:   PetscErrorCode       ierr;
1169:   PetscInt             m,n;
1170:   MPI_Comm             comm,subcomm=0;
1171:   const char           *prefix;
1172:   PetscBool            wasSetup = PETSC_TRUE;

1175:   PetscObjectGetComm((PetscObject)pc,&comm);
1176:   if (jac->n_local > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only a single block in a subcommunicator is supported");
1177:   jac->n_local = 1; /* currently only a single block is supported for a subcommunicator */
1178:   if (!pc->setupcalled) {
1179:     wasSetup  = PETSC_FALSE;
1180:     PetscNewLog(pc,&mpjac);
1181:     jac->data = (void*)mpjac;

1183:     /* initialize datastructure mpjac */
1184:     if (!jac->psubcomm) {
1185:       /* Create default contiguous subcommunicatiors if user does not provide them */
1186:       PetscSubcommCreate(comm,&jac->psubcomm);
1187:       PetscSubcommSetNumber(jac->psubcomm,jac->n);
1188:       PetscSubcommSetType(jac->psubcomm,PETSC_SUBCOMM_CONTIGUOUS);
1189:       PetscLogObjectMemory((PetscObject)pc,sizeof(PetscSubcomm));
1190:     }
1191:     mpjac->psubcomm = jac->psubcomm;
1192:     subcomm         = PetscSubcommChild(mpjac->psubcomm);

1194:     /* Get matrix blocks of pmat */
1195:     MatGetMultiProcBlock(pc->pmat,subcomm,MAT_INITIAL_MATRIX,&mpjac->submats);

1197:     /* create a new PC that processors in each subcomm have copy of */
1198:     PetscMalloc1(1,&jac->ksp);
1199:     KSPCreate(subcomm,&jac->ksp[0]);
1200:     KSPSetErrorIfNotConverged(jac->ksp[0],pc->erroriffailure);
1201:     PetscObjectIncrementTabLevel((PetscObject)jac->ksp[0],(PetscObject)pc,1);
1202:     PetscLogObjectParent((PetscObject)pc,(PetscObject)jac->ksp[0]);
1203:     KSPSetOperators(jac->ksp[0],mpjac->submats,mpjac->submats);
1204:     KSPGetPC(jac->ksp[0],&mpjac->pc);

1206:     PCGetOptionsPrefix(pc,&prefix);
1207:     KSPSetOptionsPrefix(jac->ksp[0],prefix);
1208:     KSPAppendOptionsPrefix(jac->ksp[0],"sub_");
1209:     /*
1210:       PetscMPIInt rank,subsize,subrank;
1211:       MPI_Comm_rank(comm,&rank);
1212:       MPI_Comm_size(subcomm,&subsize);
1213:       MPI_Comm_rank(subcomm,&subrank);

1215:       MatGetLocalSize(mpjac->submats,&m,NULL);
1216:       MatGetSize(mpjac->submats,&n,NULL);
1217:       PetscSynchronizedPrintf(comm,"[%d], sub-size %d,sub-rank %d\n",rank,subsize,subrank);
1218:       PetscSynchronizedFlush(comm,PETSC_STDOUT);
1219:     */

1221:     /* create dummy vectors xsub and ysub */
1222:     MatGetLocalSize(mpjac->submats,&m,&n);
1223:     VecCreateMPIWithArray(subcomm,1,n,PETSC_DECIDE,NULL,&mpjac->xsub);
1224:     VecCreateMPIWithArray(subcomm,1,m,PETSC_DECIDE,NULL,&mpjac->ysub);
1225: #if defined(PETSC_HAVE_CUSP)
1226:     VecSetType(mpjac->xsub,VECMPICUSP);
1227:     VecSetType(mpjac->ysub,VECMPICUSP);
1228: #elif defined(PETSC_HAVE_VECCUDA)
1229:     VecSetType(mpjac->xsub,VECMPICUDA);
1230:     VecSetType(mpjac->ysub,VECMPICUDA);
1231: #elif defined(PETSC_HAVE_VECVIENNACL)
1232:     VecSetType(mpjac->xsub,VECMPIVIENNACL);
1233:     VecSetType(mpjac->ysub,VECMPIVIENNACL);
1234: #endif
1235:     PetscLogObjectParent((PetscObject)pc,(PetscObject)mpjac->xsub);
1236:     PetscLogObjectParent((PetscObject)pc,(PetscObject)mpjac->ysub);

1238:     pc->ops->reset   = PCReset_BJacobi_Multiproc;
1239:     pc->ops->destroy = PCDestroy_BJacobi_Multiproc;
1240:     pc->ops->apply   = PCApply_BJacobi_Multiproc;
1241:   } else { /* pc->setupcalled */
1242:     subcomm = PetscSubcommChild(mpjac->psubcomm);
1243:     if (pc->flag == DIFFERENT_NONZERO_PATTERN) {
1244:       /* destroy old matrix blocks, then get new matrix blocks */
1245:       if (mpjac->submats) {MatDestroy(&mpjac->submats);}
1246:       MatGetMultiProcBlock(pc->pmat,subcomm,MAT_INITIAL_MATRIX,&mpjac->submats);
1247:     } else {
1248:       MatGetMultiProcBlock(pc->pmat,subcomm,MAT_REUSE_MATRIX,&mpjac->submats);
1249:     }
1250:     KSPSetOperators(jac->ksp[0],mpjac->submats,mpjac->submats);
1251:   }

1253:   if (!wasSetup && pc->setfromoptionscalled) {
1254:     KSPSetFromOptions(jac->ksp[0]);
1255:   }
1256:   return(0);
1257: }