Actual source code: bjacobi.c

  1: #define PETSCKSP_DLL

  3: /*
  4:    Defines a block Jacobi preconditioner.
  5: */
 6:  #include private/matimpl.h
 7:  #include private/pcimpl.h
 8:  #include ../src/ksp/pc/impls/bjacobi/bjacobi.h

 10: static PetscErrorCode PCSetUp_BJacobi_Singleblock(PC,Mat,Mat);
 11: static PetscErrorCode PCSetUp_BJacobi_Multiblock(PC,Mat,Mat);

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

 26:   MPI_Comm_rank(((PetscObject)pc)->comm,&rank);
 27:   MPI_Comm_size(((PetscObject)pc)->comm,&size);
 28:   MatGetLocalSize(pc->pmat,&M,&N);
 29:   MatGetBlockSize(pc->pmat,&bs);

 31:   /* ----------
 32:       Determines the number of blocks assigned to each processor 
 33:   */

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

110:   MPI_Comm_size(((PetscObject)pc)->comm,&size);
111:   PetscObjectQueryFunction((PetscObject)pc->mat,"MatGetDiagonalBlock_C",(void (**)(void))&f);
112:   if (size == 1 && !f) {
113:     mat  = pc->mat;
114:     pmat = pc->pmat;
115:   } else {
116:     PetscTruth iscopy;
117:     MatReuse   scall;

119:     if (jac->use_true_local) {
120:       scall = MAT_INITIAL_MATRIX;
121:       if (pc->setupcalled) {
122:         if (pc->flag == SAME_NONZERO_PATTERN) {
123:           if (jac->tp_mat) {
124:             scall = MAT_REUSE_MATRIX;
125:             mat   = jac->tp_mat;
126:           }
127:         } else {
128:           if (jac->tp_mat)  {
129:             MatDestroy(jac->tp_mat);
130:           }
131:         }
132:       }
133:       if (!f) {
134:         SETERRQ(PETSC_ERR_SUP,"This matrix does not support getting diagonal block");
135:       }
136:       (*f)(pc->mat,&iscopy,scall,&mat);
137:       /* make submatrix have same prefix as entire matrix */
138:       PetscObjectGetOptionsPrefix((PetscObject)pc->mat,&mprefix);
139:       PetscObjectSetOptionsPrefix((PetscObject)mat,mprefix);
140:       if (iscopy) {
141:         jac->tp_mat = mat;
142:       }
143:     }
144:     if (pc->pmat != pc->mat || !jac->use_true_local) {
145:       scall = MAT_INITIAL_MATRIX;
146:       if (pc->setupcalled) {
147:         if (pc->flag == SAME_NONZERO_PATTERN) {
148:           if (jac->tp_pmat) {
149:             scall = MAT_REUSE_MATRIX;
150:             pmat   = jac->tp_pmat;
151:           }
152:         } else {
153:           if (jac->tp_pmat)  {
154:             MatDestroy(jac->tp_pmat);
155:           }
156:         }
157:       }
158:       PetscObjectQueryFunction((PetscObject)pc->pmat,"MatGetDiagonalBlock_C",(void (**)(void))&f);
159:       if (!f) {
160:         const char *type;
161:         PetscObjectGetType((PetscObject) pc->pmat,&type);
162:         SETERRQ1(PETSC_ERR_SUP,"This matrix type, %s, does not support getting diagonal block", type);
163:       }
164:       (*f)(pc->pmat,&iscopy,scall,&pmat);
165:       /* make submatrix have same prefix as entire matrix */
166:       PetscObjectGetOptionsPrefix((PetscObject)pc->pmat,&pprefix);
167:       PetscObjectSetOptionsPrefix((PetscObject)pmat,pprefix);
168:       if (iscopy) {
169:         jac->tp_pmat = pmat;
170:       }
171:     } else {
172:       pmat = mat;
173:     }
174:   }

176:   /* ------
177:      Setup code depends on the number of blocks 
178:   */
179:   if (jac->n_local == 1) {
180:     PCSetUp_BJacobi_Singleblock(pc,mat,pmat);
181:   } else {
182:     PCSetUp_BJacobi_Multiblock(pc,mat,pmat);
183:   }
184:   return(0);
185: }

187: /* Default destroy, if it has never been setup */
190: static PetscErrorCode PCDestroy_BJacobi(PC pc)
191: {
192:   PC_BJacobi     *jac = (PC_BJacobi*)pc->data;

196:   PetscFree(jac->g_lens);
197:   PetscFree(jac->l_lens);
198:   PetscFree(jac);
199:   return(0);
200: }


205: static PetscErrorCode PCSetFromOptions_BJacobi(PC pc)
206: {
207:   PC_BJacobi     *jac = (PC_BJacobi*)pc->data;
209:   PetscInt       blocks;
210:   PetscTruth     flg;

213:   PetscOptionsHead("Block Jacobi options");
214:     PetscOptionsInt("-pc_bjacobi_blocks","Total number of blocks","PCBJacobiSetTotalBlocks",jac->n,&blocks,&flg);
215:     if (flg) {
216:       PCBJacobiSetTotalBlocks(pc,blocks,PETSC_NULL);
217:     }
218:     PetscOptionsName("-pc_bjacobi_truelocal","Use the true matrix, not preconditioner matrix to define matrix vector product in sub-problems","PCBJacobiSetUseTrueLocal",&flg);
219:     if (flg) {
220:       PCBJacobiSetUseTrueLocal(pc);
221:     }
222:   PetscOptionsTail();
223:   return(0);
224: }

228: static PetscErrorCode PCView_BJacobi(PC pc,PetscViewer viewer)
229: {
230:   PC_BJacobi     *jac = (PC_BJacobi*)pc->data;
232:   PetscMPIInt    rank;
233:   PetscInt       i;
234:   PetscTruth     iascii,isstring;
235:   PetscViewer    sviewer;

238:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);
239:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_STRING,&isstring);
240:   if (iascii) {
241:     if (jac->use_true_local) {
242:       PetscViewerASCIIPrintf(viewer,"  block Jacobi: using true local matrix, number of blocks = %D\n",jac->n);
243:     }
244:     PetscViewerASCIIPrintf(viewer,"  block Jacobi: number of blocks = %D\n",jac->n);
245:     MPI_Comm_rank(((PetscObject)pc)->comm,&rank);
246:     if (jac->same_local_solves) {
247:       PetscViewerASCIIPrintf(viewer,"  Local solve is same for all blocks, in the following KSP and PC objects:\n");
248:       PetscViewerGetSingleton(viewer,&sviewer);
249:       if (!rank && jac->ksp) {
250:         PetscViewerASCIIPushTab(viewer);
251:         KSPView(jac->ksp[0],sviewer);
252:         PetscViewerASCIIPopTab(viewer);
253:       }
254:       PetscViewerRestoreSingleton(viewer,&sviewer);
255:     } else {
256:       PetscInt n_global;
257:       MPI_Allreduce(&jac->n_local,&n_global,1,MPIU_INT,MPI_MAX,((PetscObject)pc)->comm);
258:       PetscViewerASCIIPrintf(viewer,"  Local solve info for each block is in the following KSP and PC objects:\n");
259:       PetscViewerASCIISynchronizedPrintf(viewer,"[%d] number of local blocks = %D, first local block number = %D\n",
260:                    rank,jac->n_local,jac->first_local);
261:       PetscViewerASCIIPushTab(viewer);
262:       for (i=0; i<n_global; i++) {
263:         PetscViewerGetSingleton(viewer,&sviewer);
264:         if (i < jac->n_local) {
265:           PetscViewerASCIISynchronizedPrintf(viewer,"[%d] local block number %D\n",rank,i);
266:           KSPView(jac->ksp[i],sviewer);
267:           PetscViewerASCIISynchronizedPrintf(viewer,"- - - - - - - - - - - - - - - - - -\n");
268:         }
269:         PetscViewerRestoreSingleton(viewer,&sviewer);
270:       }
271:       PetscViewerASCIIPopTab(viewer);
272:       PetscViewerFlush(viewer);
273:     }
274:   } else if (isstring) {
275:     PetscViewerStringSPrintf(viewer," blks=%D",jac->n);
276:     PetscViewerGetSingleton(viewer,&sviewer);
277:     if (jac->ksp) {KSPView(jac->ksp[0],sviewer);}
278:     PetscViewerRestoreSingleton(viewer,&sviewer);
279:   } else {
280:     SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported for block Jacobi",((PetscObject)viewer)->type_name);
281:   }
282:   return(0);
283: }

285: /* -------------------------------------------------------------------------------------*/

290: PetscErrorCode  PCBJacobiSetUseTrueLocal_BJacobi(PC pc)
291: {
292:   PC_BJacobi   *jac;

295:   jac                 = (PC_BJacobi*)pc->data;
296:   jac->use_true_local = PETSC_TRUE;
297:   return(0);
298: }

304: PetscErrorCode  PCBJacobiGetSubKSP_BJacobi(PC pc,PetscInt *n_local,PetscInt *first_local,KSP **ksp)
305: {
306:   PC_BJacobi   *jac = (PC_BJacobi*)pc->data;;

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

311:   if (n_local)     *n_local     = jac->n_local;
312:   if (first_local) *first_local = jac->first_local;
313:   *ksp                          = jac->ksp;
314:   jac->same_local_solves        = PETSC_FALSE; /* Assume that local solves are now different;
315:                                                   not necessarily true though!  This flag is 
316:                                                   used only for PCView_BJacobi() */
317:   return(0);
318: }

324: PetscErrorCode  PCBJacobiSetTotalBlocks_BJacobi(PC pc,PetscInt blocks,PetscInt *lens)
325: {
326:   PC_BJacobi     *jac = (PC_BJacobi*)pc->data;


331:   if (pc->setupcalled > 0 && jac->n!=blocks) SETERRQ(PETSC_ERR_ORDER,"Cannot alter number of blocks after PCSetUp()/KSPSetUp() has been called");
332:   jac->n = blocks;
333:   if (!lens) {
334:     jac->g_lens = 0;
335:   } else {
336:     PetscMalloc(blocks*sizeof(PetscInt),&jac->g_lens);
337:     PetscLogObjectMemory(pc,blocks*sizeof(PetscInt));
338:     PetscMemcpy(jac->g_lens,lens,blocks*sizeof(PetscInt));
339:   }
340:   return(0);
341: }

347: PetscErrorCode  PCBJacobiGetTotalBlocks_BJacobi(PC pc, PetscInt *blocks, const PetscInt *lens[])
348: {
349:   PC_BJacobi *jac = (PC_BJacobi*) pc->data;

352:   *blocks = jac->n;
353:   if (lens) *lens = jac->g_lens;
354:   return(0);
355: }

361: PetscErrorCode  PCBJacobiSetLocalBlocks_BJacobi(PC pc,PetscInt blocks,const PetscInt lens[])
362: {
363:   PC_BJacobi     *jac;

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

369:   jac->n_local = blocks;
370:   if (!lens) {
371:     jac->l_lens = 0;
372:   } else {
373:     PetscMalloc(blocks*sizeof(PetscInt),&jac->l_lens);
374:     PetscLogObjectMemory(pc,blocks*sizeof(PetscInt));
375:     PetscMemcpy(jac->l_lens,lens,blocks*sizeof(PetscInt));
376:   }
377:   return(0);
378: }

384: PetscErrorCode  PCBJacobiGetLocalBlocks_BJacobi(PC pc, PetscInt *blocks, const PetscInt *lens[])
385: {
386:   PC_BJacobi *jac = (PC_BJacobi*) pc->data;

389:   *blocks = jac->n_local;
390:   if (lens) *lens = jac->l_lens;
391:   return(0);
392: }

395: /* -------------------------------------------------------------------------------------*/

399: /*@
400:    PCBJacobiSetUseTrueLocal - Sets a flag to indicate that the block 
401:    problem is associated with the linear system matrix instead of the
402:    default (where it is associated with the preconditioning matrix).
403:    That is, if the local system is solved iteratively then it iterates
404:    on the block from the matrix using the block from the preconditioner
405:    as the preconditioner for the local block.

407:    Collective on PC

409:    Input Parameters:
410: .  pc - the preconditioner context

412:    Options Database Key:
413: .  -pc_bjacobi_truelocal - Activates PCBJacobiSetUseTrueLocal()

415:    Notes:
416:    For the common case in which the preconditioning and linear 
417:    system matrices are identical, this routine is unnecessary.

419:    Level: intermediate

421: .keywords:  block, Jacobi, set, true, local, flag

423: .seealso: PCSetOperators(), PCBJacobiSetLocalBlocks()
424: @*/
425: PetscErrorCode  PCBJacobiSetUseTrueLocal(PC pc)
426: {
427:   PetscErrorCode ierr,(*f)(PC);

431:   PetscObjectQueryFunction((PetscObject)pc,"PCBJacobiSetUseTrueLocal_C",(void (**)(void))&f);
432:   if (f) {
433:     (*f)(pc);
434:   }

436:   return(0);
437: }

441: /*@C
442:    PCBJacobiGetSubKSP - Gets the local KSP contexts for all blocks on
443:    this processor.
444:    
445:    Note Collective

447:    Input Parameter:
448: .  pc - the preconditioner context

450:    Output Parameters:
451: +  n_local - the number of blocks on this processor, or PETSC_NULL
452: .  first_local - the global number of the first block on this processor, or PETSC_NULL
453: -  ksp - the array of KSP contexts

455:    Notes:  
456:    After PCBJacobiGetSubKSP() the array of KSP contexts is not to be freed.
457:    
458:    Currently for some matrix implementations only 1 block per processor 
459:    is supported.
460:    
461:    You must call KSPSetUp() or PCSetUp() before calling PCBJacobiGetSubKSP().

463:    Level: advanced

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

467: .seealso: PCBJacobiGetSubKSP()
468: @*/
469: PetscErrorCode  PCBJacobiGetSubKSP(PC pc,PetscInt *n_local,PetscInt *first_local,KSP *ksp[])
470: {
471:   PetscErrorCode ierr,(*f)(PC,PetscInt *,PetscInt *,KSP **);

475:   PetscObjectQueryFunction((PetscObject)pc,"PCBJacobiGetSubKSP_C",(void (**)(void))&f);
476:   if (f) {
477:     (*f)(pc,n_local,first_local,ksp);
478:   } else {
479:     SETERRQ(PETSC_ERR_ARG_WRONG,"Cannot get subsolvers for this preconditioner");
480:   }
481:   return(0);
482: }

486: /*@
487:    PCBJacobiSetTotalBlocks - Sets the global number of blocks for the block
488:    Jacobi preconditioner.

490:    Collective on PC

492:    Input Parameters:
493: +  pc - the preconditioner context
494: .  blocks - the number of blocks
495: -  lens - [optional] integer array containing the size of each block

497:    Options Database Key:
498: .  -pc_bjacobi_blocks <blocks> - Sets the number of global blocks

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

504:    Level: intermediate

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

508: .seealso: PCBJacobiSetUseTrueLocal(), PCBJacobiSetLocalBlocks()
509: @*/
510: PetscErrorCode  PCBJacobiSetTotalBlocks(PC pc,PetscInt blocks,const PetscInt lens[])
511: {
512:   PetscErrorCode ierr,(*f)(PC,PetscInt,const PetscInt[]);

516:   if (blocks <= 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Must have positive blocks");
517:   PetscObjectQueryFunction((PetscObject)pc,"PCBJacobiSetTotalBlocks_C",(void (**)(void))&f);
518:   if (f) {
519:     (*f)(pc,blocks,lens);
520:   }
521:   return(0);
522: }

526: /*@C
527:    PCBJacobiGetTotalBlocks - Gets the global number of blocks for the block
528:    Jacobi preconditioner.

530:    Collective on PC

532:    Input Parameter:
533: .  pc - the preconditioner context

535:    Output parameters:
536: +  blocks - the number of blocks
537: -  lens - integer array containing the size of each block

539:    Level: intermediate

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

543: .seealso: PCBJacobiSetUseTrueLocal(), PCBJacobiGetLocalBlocks()
544: @*/
545: PetscErrorCode  PCBJacobiGetTotalBlocks(PC pc, PetscInt *blocks, const PetscInt *lens[])
546: {
547:   PetscErrorCode ierr,(*f)(PC,PetscInt*, const PetscInt *[]);

552:   PetscObjectQueryFunction((PetscObject)pc,"PCBJacobiGetTotalBlocks_C",(void (**)(void))&f);
553:   if (f) {
554:     (*f)(pc,blocks,lens);
555:   }
556:   return(0);
557: }
558: 
561: /*@
562:    PCBJacobiSetLocalBlocks - Sets the local number of blocks for the block
563:    Jacobi preconditioner.

565:    Not Collective

567:    Input Parameters:
568: +  pc - the preconditioner context
569: .  blocks - the number of blocks
570: -  lens - [optional] integer array containing size of each block

572:    Note:  
573:    Currently only a limited number of blocking configurations are supported.

575:    Level: intermediate

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

579: .seealso: PCBJacobiSetUseTrueLocal(), PCBJacobiSetTotalBlocks()
580: @*/
581: PetscErrorCode  PCBJacobiSetLocalBlocks(PC pc,PetscInt blocks,const PetscInt lens[])
582: {
583:   PetscErrorCode ierr,(*f)(PC,PetscInt,const PetscInt []);

587:   if (blocks < 0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Must have nonegative blocks");
588:   PetscObjectQueryFunction((PetscObject)pc,"PCBJacobiSetLocalBlocks_C",(void (**)(void))&f);
589:   if (f) {
590:     (*f)(pc,blocks,lens);
591:   }
592:   return(0);
593: }
594: 
597: /*@C
598:    PCBJacobiGetLocalBlocks - Gets the local number of blocks for the block
599:    Jacobi preconditioner.

601:    Not Collective

603:    Input Parameters:
604: +  pc - the preconditioner context
605: .  blocks - the number of blocks
606: -  lens - [optional] integer array containing size of each block

608:    Note:  
609:    Currently only a limited number of blocking configurations are supported.

611:    Level: intermediate

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

615: .seealso: PCBJacobiSetUseTrueLocal(), PCBJacobiGetTotalBlocks()
616: @*/
617: PetscErrorCode  PCBJacobiGetLocalBlocks(PC pc, PetscInt *blocks, const PetscInt *lens[])
618: {
619:   PetscErrorCode ierr,(*f)(PC,PetscInt*, const PetscInt *[]);

624:   PetscObjectQueryFunction((PetscObject)pc,"PCBJacobiGetLocalBlocks_C",(void (**)(void))&f);
625:   if (f) {
626:     (*f)(pc,blocks,lens);
627:   }
628:   return(0);
629: }

631: /* -----------------------------------------------------------------------------------*/

633: /*MC
634:    PCBJACOBI - Use block Jacobi preconditioning, each block is (approximately) solved with 
635:            its own KSP object.

637:    Options Database Keys:
638: .  -pc_bjacobi_truelocal - Activates PCBJacobiSetUseTrueLocal()

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

643:      To set options on the solvers for each block append -sub_ to all the KSP, KSP, and PC
644:         options database keys. For example, -sub_pc_type ilu -sub_pc_factor_levels 1 -sub_ksp_type preonly
645:         
646:      To set the options on the solvers separate for each block call PCBJacobiGetSubKSP()
647:          and set the options directly on the resulting KSP object (you can access its PC
648:          KSPGetPC())

650:    Level: beginner

652:    Concepts: block Jacobi

654: .seealso:  PCCreate(), PCSetType(), PCType (for list of available types), PC,
655:            PCASM, PCBJacobiSetUseTrueLocal(), PCBJacobiGetSubKSP(), PCBJacobiSetTotalBlocks(),
656:            PCBJacobiSetLocalBlocks(), PCSetModifySubmatrices()
657: M*/

662: PetscErrorCode  PCCreate_BJacobi(PC pc)
663: {
665:   PetscMPIInt    rank;
666:   PC_BJacobi     *jac;

669:   PetscNewLog(pc,PC_BJacobi,&jac);
670:   MPI_Comm_rank(((PetscObject)pc)->comm,&rank);
671:   pc->ops->apply              = 0;
672:   pc->ops->applytranspose     = 0;
673:   pc->ops->setup              = PCSetUp_BJacobi;
674:   pc->ops->destroy            = PCDestroy_BJacobi;
675:   pc->ops->setfromoptions     = PCSetFromOptions_BJacobi;
676:   pc->ops->view               = PCView_BJacobi;
677:   pc->ops->applyrichardson    = 0;

679:   pc->data               = (void*)jac;
680:   jac->n                 = -1;
681:   jac->n_local           = -1;
682:   jac->first_local       = rank;
683:   jac->ksp              = 0;
684:   jac->use_true_local    = PETSC_FALSE;
685:   jac->same_local_solves = PETSC_TRUE;
686:   jac->g_lens            = 0;
687:   jac->l_lens            = 0;
688:   jac->tp_mat            = 0;
689:   jac->tp_pmat           = 0;

691:   PetscObjectComposeFunctionDynamic((PetscObject)pc,"PCBJacobiSetUseTrueLocal_C",
692:                     "PCBJacobiSetUseTrueLocal_BJacobi",
693:                     PCBJacobiSetUseTrueLocal_BJacobi);
694:   PetscObjectComposeFunctionDynamic((PetscObject)pc,"PCBJacobiGetSubKSP_C","PCBJacobiGetSubKSP_BJacobi",
695:                     PCBJacobiGetSubKSP_BJacobi);
696:   PetscObjectComposeFunctionDynamic((PetscObject)pc,"PCBJacobiSetTotalBlocks_C","PCBJacobiSetTotalBlocks_BJacobi",
697:                     PCBJacobiSetTotalBlocks_BJacobi);
698:   PetscObjectComposeFunctionDynamic((PetscObject)pc,"PCBJacobiGetTotalBlocks_C","PCBJacobiGetTotalBlocks_BJacobi",
699:                     PCBJacobiGetTotalBlocks_BJacobi);
700:   PetscObjectComposeFunctionDynamic((PetscObject)pc,"PCBJacobiSetLocalBlocks_C","PCBJacobiSetLocalBlocks_BJacobi",
701:                     PCBJacobiSetLocalBlocks_BJacobi);
702:   PetscObjectComposeFunctionDynamic((PetscObject)pc,"PCBJacobiGetLocalBlocks_C","PCBJacobiGetLocalBlocks_BJacobi",
703:                     PCBJacobiGetLocalBlocks_BJacobi);

705:   return(0);
706: }

709: /* --------------------------------------------------------------------------------------------*/
710: /*
711:         These are for a single block per processor; works for AIJ, BAIJ; Seq and MPI
712: */
715: PetscErrorCode PCDestroy_BJacobi_Singleblock(PC pc)
716: {
717:   PC_BJacobi             *jac = (PC_BJacobi*)pc->data;
718:   PC_BJacobi_Singleblock *bjac = (PC_BJacobi_Singleblock*)jac->data;
719:   PetscErrorCode         ierr;

722:   /*
723:         If the on processor block had to be generated via a MatGetDiagonalBlock()
724:      that creates a copy, this frees the space
725:   */
726:   if (jac->tp_mat) {
727:     MatDestroy(jac->tp_mat);
728:   }
729:   if (jac->tp_pmat) {
730:     MatDestroy(jac->tp_pmat);
731:   }

733:   KSPDestroy(jac->ksp[0]);
734:   PetscFree(jac->ksp);
735:   VecDestroy(bjac->x);
736:   VecDestroy(bjac->y);
737:   PetscFree(jac->l_lens);
738:   PetscFree(jac->g_lens);
739:   PetscFree(bjac);
740:   PetscFree(jac);
741:   return(0);
742: }

746: PetscErrorCode PCSetUpOnBlocks_BJacobi_Singleblock(PC pc)
747: {
749:   PC_BJacobi     *jac = (PC_BJacobi*)pc->data;

752:   KSPSetUp(jac->ksp[0]);
753:   return(0);
754: }

758: PetscErrorCode PCApply_BJacobi_Singleblock(PC pc,Vec x,Vec y)
759: {
760:   PetscErrorCode         ierr;
761:   PC_BJacobi             *jac = (PC_BJacobi*)pc->data;
762:   PC_BJacobi_Singleblock *bjac = (PC_BJacobi_Singleblock*)jac->data;
763:   PetscScalar            *x_array,*y_array;

766:   /* 
767:       The VecPlaceArray() is to avoid having to copy the 
768:     y vector into the bjac->x vector. The reason for 
769:     the bjac->x vector is that we need a sequential vector
770:     for the sequential solve.
771:   */
772:   VecGetArray(x,&x_array);
773:   VecGetArray(y,&y_array);
774:   VecPlaceArray(bjac->x,x_array);
775:   VecPlaceArray(bjac->y,y_array);
776:   KSPSolve(jac->ksp[0],bjac->x,bjac->y);
777:   VecResetArray(bjac->x);
778:   VecResetArray(bjac->y);
779:   VecRestoreArray(x,&x_array);
780:   VecRestoreArray(y,&y_array);
781:   return(0);
782: }

786: PetscErrorCode PCApplySymmetricLeft_BJacobi_Singleblock(PC pc,Vec x,Vec y)
787: {
788:   PetscErrorCode         ierr;
789:   PC_BJacobi             *jac = (PC_BJacobi*)pc->data;
790:   PC_BJacobi_Singleblock *bjac = (PC_BJacobi_Singleblock*)jac->data;
791:   PetscScalar            *x_array,*y_array;
792:   PC                     subpc;

795:   /* 
796:       The VecPlaceArray() is to avoid having to copy the 
797:     y vector into the bjac->x vector. The reason for 
798:     the bjac->x vector is that we need a sequential vector
799:     for the sequential solve.
800:   */
801:   VecGetArray(x,&x_array);
802:   VecGetArray(y,&y_array);
803:   VecPlaceArray(bjac->x,x_array);
804:   VecPlaceArray(bjac->y,y_array);

806:   /* apply the symmetric left portion of the inner PC operator */
807:   /* note this by-passes the inner KSP and its options completely */

809:   KSPGetPC(jac->ksp[0],&subpc);
810:   PCApplySymmetricLeft(subpc,bjac->x,bjac->y);
811:   VecResetArray(bjac->x);
812:   VecResetArray(bjac->y);

814:   VecRestoreArray(x,&x_array);
815:   VecRestoreArray(y,&y_array);
816:   return(0);
817: }

821: PetscErrorCode PCApplySymmetricRight_BJacobi_Singleblock(PC pc,Vec x,Vec y)
822: {
823:   PetscErrorCode         ierr;
824:   PC_BJacobi             *jac = (PC_BJacobi*)pc->data;
825:   PC_BJacobi_Singleblock *bjac = (PC_BJacobi_Singleblock*)jac->data;
826:   PetscScalar            *x_array,*y_array;
827:   PC                     subpc;

830:   /* 
831:       The VecPlaceArray() is to avoid having to copy the 
832:     y vector into the bjac->x vector. The reason for 
833:     the bjac->x vector is that we need a sequential vector
834:     for the sequential solve.
835:   */
836:   VecGetArray(x,&x_array);
837:   VecGetArray(y,&y_array);
838:   VecPlaceArray(bjac->x,x_array);
839:   VecPlaceArray(bjac->y,y_array);

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

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

847:   VecRestoreArray(x,&x_array);
848:   VecRestoreArray(y,&y_array);
849:   return(0);
850: }

854: PetscErrorCode PCApplyTranspose_BJacobi_Singleblock(PC pc,Vec x,Vec y)
855: {
856:   PetscErrorCode         ierr;
857:   PC_BJacobi             *jac = (PC_BJacobi*)pc->data;
858:   PC_BJacobi_Singleblock *bjac = (PC_BJacobi_Singleblock*)jac->data;
859:   PetscScalar            *x_array,*y_array;

862:   /* 
863:       The VecPlaceArray() is to avoid having to copy the 
864:     y vector into the bjac->x vector. The reason for 
865:     the bjac->x vector is that we need a sequential vector
866:     for the sequential solve.
867:   */
868:   VecGetArray(x,&x_array);
869:   VecGetArray(y,&y_array);
870:   VecPlaceArray(bjac->x,x_array);
871:   VecPlaceArray(bjac->y,y_array);
872:   KSPSolveTranspose(jac->ksp[0],bjac->x,bjac->y);
873:   VecResetArray(bjac->x);
874:   VecResetArray(bjac->y);
875:   VecRestoreArray(x,&x_array);
876:   VecRestoreArray(y,&y_array);
877:   return(0);
878: }

882: static PetscErrorCode PCSetUp_BJacobi_Singleblock(PC pc,Mat mat,Mat pmat)
883: {
884:   PC_BJacobi             *jac = (PC_BJacobi*)pc->data;
885:   PetscErrorCode         ierr;
886:   PetscInt               m;
887:   KSP                    ksp;
888:   Vec                    x,y;
889:   PC_BJacobi_Singleblock *bjac;
890:   PetscTruth             wasSetup;


894:   /* set default direct solver with no Krylov method */
895:   if (!pc->setupcalled) {
896:     const char *prefix;
897:     wasSetup = PETSC_FALSE;
898:     KSPCreate(PETSC_COMM_SELF,&ksp);
899:     PetscObjectIncrementTabLevel((PetscObject)ksp,(PetscObject)pc,1);
900:     PetscLogObjectParent(pc,ksp);
901:     KSPSetType(ksp,KSPPREONLY);
902:     PCGetOptionsPrefix(pc,&prefix);
903:     KSPSetOptionsPrefix(ksp,prefix);
904:     KSPAppendOptionsPrefix(ksp,"sub_");
905:     /*
906:       The reason we need to generate these vectors is to serve 
907:       as the right-hand side and solution vector for the solve on the 
908:       block. We do not need to allocate space for the vectors since
909:       that is provided via VecPlaceArray() just before the call to 
910:       KSPSolve() on the block.
911:     */
912:     MatGetSize(pmat,&m,&m);
913:     VecCreateSeqWithArray(PETSC_COMM_SELF,m,PETSC_NULL,&x);
914:     VecCreateSeqWithArray(PETSC_COMM_SELF,m,PETSC_NULL,&y);
915:     PetscLogObjectParent(pc,x);
916:     PetscLogObjectParent(pc,y);

918:     pc->ops->destroy             = PCDestroy_BJacobi_Singleblock;
919:     pc->ops->apply               = PCApply_BJacobi_Singleblock;
920:     pc->ops->applysymmetricleft  = PCApplySymmetricLeft_BJacobi_Singleblock;
921:     pc->ops->applysymmetricright = PCApplySymmetricRight_BJacobi_Singleblock;
922:     pc->ops->applytranspose      = PCApplyTranspose_BJacobi_Singleblock;
923:     pc->ops->setuponblocks       = PCSetUpOnBlocks_BJacobi_Singleblock;

925:     PetscNewLog(pc,PC_BJacobi_Singleblock,&bjac);
926:     bjac->x      = x;
927:     bjac->y      = y;

929:     PetscMalloc(sizeof(KSP),&jac->ksp);
930:     jac->ksp[0] = ksp;
931:     jac->data    = (void*)bjac;
932:   } else {
933:     wasSetup = PETSC_TRUE;
934:     ksp = jac->ksp[0];
935:     bjac = (PC_BJacobi_Singleblock *)jac->data;
936:   }
937:   if (jac->use_true_local) {
938:     KSPSetOperators(ksp,mat,pmat,pc->flag);
939:   }  else {
940:     KSPSetOperators(ksp,pmat,pmat,pc->flag);
941:   }
942:   if (!wasSetup && pc->setfromoptionscalled) {
943:     KSPSetFromOptions(ksp);
944:   }
945:   return(0);
946: }

948: /* ---------------------------------------------------------------------------------------------*/

952: PetscErrorCode PCDestroy_BJacobi_Multiblock(PC pc)
953: {
954:   PC_BJacobi            *jac = (PC_BJacobi*)pc->data;
955:   PC_BJacobi_Multiblock *bjac = (PC_BJacobi_Multiblock*)jac->data;
956:   PetscErrorCode        ierr;
957:   PetscInt              i;

960:   MatDestroyMatrices(jac->n_local,&bjac->pmat);
961:   if (jac->use_true_local) {
962:     MatDestroyMatrices(jac->n_local,&bjac->mat);
963:   }

965:   /*
966:         If the on processor block had to be generated via a MatGetDiagonalBlock()
967:      that creates a copy, this frees the space
968:   */
969:   if (jac->tp_mat) {
970:     MatDestroy(jac->tp_mat);
971:   }
972:   if (jac->tp_pmat) {
973:     MatDestroy(jac->tp_pmat);
974:   }

976:   for (i=0; i<jac->n_local; i++) {
977:     KSPDestroy(jac->ksp[i]);
978:     VecDestroy(bjac->x[i]);
979:     VecDestroy(bjac->y[i]);
980:     ISDestroy(bjac->is[i]);
981:   }
982:   PetscFree(jac->ksp);
983:   PetscFree2(bjac->x,bjac->y);
984:   PetscFree(bjac->starts);
985:   PetscFree(bjac->is);
986:   PetscFree(bjac);
987:   PetscFree(jac->l_lens);
988:   PetscFree(jac->g_lens);
989:   PetscFree(jac);
990:   return(0);
991: }

995: PetscErrorCode PCSetUpOnBlocks_BJacobi_Multiblock(PC pc)
996: {
997:   PC_BJacobi     *jac = (PC_BJacobi*)pc->data;
999:   PetscInt       i,n_local = jac->n_local;

1002:   for (i=0; i<n_local; i++) {
1003:     KSPSetUp(jac->ksp[i]);
1004:   }
1005:   return(0);
1006: }

1008: /*
1009:       Preconditioner for block Jacobi 
1010: */
1013: PetscErrorCode PCApply_BJacobi_Multiblock(PC pc,Vec x,Vec y)
1014: {
1015:   PC_BJacobi            *jac = (PC_BJacobi*)pc->data;
1016:   PetscErrorCode        ierr;
1017:   PetscInt              i,n_local = jac->n_local;
1018:   PC_BJacobi_Multiblock *bjac = (PC_BJacobi_Multiblock*)jac->data;
1019:   PetscScalar           *xin,*yin;

1022:   VecGetArray(x,&xin);
1023:   VecGetArray(y,&yin);
1024:   for (i=0; i<n_local; i++) {
1025:     /* 
1026:        To avoid copying the subvector from x into a workspace we instead 
1027:        make the workspace vector array point to the subpart of the array of
1028:        the global vector.
1029:     */
1030:     VecPlaceArray(bjac->x[i],xin+bjac->starts[i]);
1031:     VecPlaceArray(bjac->y[i],yin+bjac->starts[i]);

1033:     PetscLogEventBegin(PC_SetUpOnBlocks,jac->ksp[i],bjac->x[i],bjac->y[i],0);
1034:     KSPSolve(jac->ksp[i],bjac->x[i],bjac->y[i]);
1035:     PetscLogEventEnd(PC_SetUpOnBlocks,jac->ksp[i],bjac->x[i],bjac->y[i],0);

1037:     VecResetArray(bjac->x[i]);
1038:     VecResetArray(bjac->y[i]);
1039:   }
1040:   VecRestoreArray(x,&xin);
1041:   VecRestoreArray(y,&yin);
1042:   return(0);
1043: }

1045: /*
1046:       Preconditioner for block Jacobi 
1047: */
1050: PetscErrorCode PCApplyTranspose_BJacobi_Multiblock(PC pc,Vec x,Vec y)
1051: {
1052:   PC_BJacobi            *jac = (PC_BJacobi*)pc->data;
1053:   PetscErrorCode        ierr;
1054:   PetscInt              i,n_local = jac->n_local;
1055:   PC_BJacobi_Multiblock *bjac = (PC_BJacobi_Multiblock*)jac->data;
1056:   PetscScalar           *xin,*yin;

1059:   VecGetArray(x,&xin);
1060:   VecGetArray(y,&yin);
1061:   for (i=0; i<n_local; i++) {
1062:     /* 
1063:        To avoid copying the subvector from x into a workspace we instead 
1064:        make the workspace vector array point to the subpart of the array of
1065:        the global vector.
1066:     */
1067:     VecPlaceArray(bjac->x[i],xin+bjac->starts[i]);
1068:     VecPlaceArray(bjac->y[i],yin+bjac->starts[i]);

1070:     PetscLogEventBegin(PC_ApplyTransposeOnBlocks,jac->ksp[i],bjac->x[i],bjac->y[i],0);
1071:     KSPSolveTranspose(jac->ksp[i],bjac->x[i],bjac->y[i]);
1072:     PetscLogEventEnd(PC_ApplyTransposeOnBlocks,jac->ksp[i],bjac->x[i],bjac->y[i],0);
1073:   }
1074:   VecRestoreArray(x,&xin);
1075:   VecRestoreArray(y,&yin);
1076:   return(0);
1077: }

1081: static PetscErrorCode PCSetUp_BJacobi_Multiblock(PC pc,Mat mat,Mat pmat)
1082: {
1083:   PC_BJacobi             *jac = (PC_BJacobi*)pc->data;
1084:   PetscErrorCode         ierr;
1085:   PetscInt               m,n_local,N,M,start,i;
1086:   const char             *prefix,*pprefix,*mprefix;
1087:   KSP                    ksp;
1088:   Vec                    x,y;
1089:   PC_BJacobi_Multiblock  *bjac = (PC_BJacobi_Multiblock*)jac->data;
1090:   PC                     subpc;
1091:   IS                     is;
1092:   MatReuse               scall = MAT_REUSE_MATRIX;

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

1097:   n_local = jac->n_local;

1099:   if (jac->use_true_local) {
1100:     PetscTruth same;
1101:     PetscTypeCompare((PetscObject)mat,((PetscObject)pmat)->type_name,&same);
1102:     if (!same) SETERRQ(PETSC_ERR_ARG_INCOMP,"Matrices not of same type");
1103:   }

1105:   if (!pc->setupcalled) {
1106:     scall                  = MAT_INITIAL_MATRIX;
1107:     pc->ops->destroy       = PCDestroy_BJacobi_Multiblock;
1108:     pc->ops->apply         = PCApply_BJacobi_Multiblock;
1109:     pc->ops->applytranspose= PCApplyTranspose_BJacobi_Multiblock;
1110:     pc->ops->setuponblocks = PCSetUpOnBlocks_BJacobi_Multiblock;

1112:     PetscNewLog(pc,PC_BJacobi_Multiblock,&bjac);
1113:     PetscMalloc(n_local*sizeof(KSP),&jac->ksp);
1114:     PetscLogObjectMemory(pc,sizeof(n_local*sizeof(KSP)));
1115:     PetscMalloc2(n_local,Vec,&bjac->x,n_local,Vec,&bjac->y);
1116:     PetscMalloc(n_local*sizeof(PetscScalar),&bjac->starts);
1117:     PetscLogObjectMemory(pc,sizeof(n_local*sizeof(PetscScalar)));
1118: 
1119:     jac->data    = (void*)bjac;
1120:     PetscMalloc(n_local*sizeof(IS),&bjac->is);
1121:     PetscLogObjectMemory(pc,sizeof(n_local*sizeof(IS)));

1123:     start = 0;
1124:     for (i=0; i<n_local; i++) {
1125:       KSPCreate(PETSC_COMM_SELF,&ksp);
1126:       PetscObjectIncrementTabLevel((PetscObject)ksp,(PetscObject)pc,1);
1127:       PetscLogObjectParent(pc,ksp);
1128:       KSPSetType(ksp,KSPPREONLY);
1129:       KSPGetPC(ksp,&subpc);
1130:       PCGetOptionsPrefix(pc,&prefix);
1131:       KSPSetOptionsPrefix(ksp,prefix);
1132:       KSPAppendOptionsPrefix(ksp,"sub_");

1134:       m = jac->l_lens[i];

1136:       /*
1137:       The reason we need to generate these vectors is to serve 
1138:       as the right-hand side and solution vector for the solve on the 
1139:       block. We do not need to allocate space for the vectors since
1140:       that is provided via VecPlaceArray() just before the call to 
1141:       KSPSolve() on the block.

1143:       */
1144:       VecCreateSeq(PETSC_COMM_SELF,m,&x);
1145:       VecCreateSeqWithArray(PETSC_COMM_SELF,m,PETSC_NULL,&y);
1146:       PetscLogObjectParent(pc,x);
1147:       PetscLogObjectParent(pc,y);
1148:       bjac->x[i]      = x;
1149:       bjac->y[i]      = y;
1150:       bjac->starts[i] = start;
1151:       jac->ksp[i]    = ksp;

1153:       ISCreateStride(PETSC_COMM_SELF,m,start,1,&is);
1154:       bjac->is[i] = is;
1155:       PetscLogObjectParent(pc,is);

1157:       start += m;
1158:     }
1159:   } else {
1160:     bjac = (PC_BJacobi_Multiblock*)jac->data;
1161:     /* 
1162:        Destroy the blocks from the previous iteration
1163:     */
1164:     if (pc->flag == DIFFERENT_NONZERO_PATTERN) {
1165:       MatDestroyMatrices(n_local,&bjac->pmat);
1166:       if (jac->use_true_local) {
1167:         MatDestroyMatrices(n_local,&bjac->mat);
1168:       }
1169:       scall = MAT_INITIAL_MATRIX;
1170:     }
1171:   }

1173:   MatGetSubMatrices(pmat,n_local,bjac->is,bjac->is,scall,&bjac->pmat);
1174:   if (jac->use_true_local) {
1175:     PetscObjectGetOptionsPrefix((PetscObject)mat,&mprefix);
1176:     MatGetSubMatrices(mat,n_local,bjac->is,bjac->is,scall,&bjac->mat);
1177:   }
1178:   /* Return control to the user so that the submatrices can be modified (e.g., to apply
1179:      different boundary conditions for the submatrices than for the global problem) */
1180:   PCModifySubMatrices(pc,n_local,bjac->is,bjac->is,bjac->pmat,pc->modifysubmatricesP);

1182:   PetscObjectGetOptionsPrefix((PetscObject)pmat,&pprefix);
1183:   for (i=0; i<n_local; i++) {
1184:     PetscLogObjectParent(pc,bjac->pmat[i]);
1185:     PetscObjectSetOptionsPrefix((PetscObject)bjac->pmat[i],pprefix);
1186:     if (jac->use_true_local) {
1187:       PetscLogObjectParent(pc,bjac->mat[i]);
1188:       PetscObjectSetOptionsPrefix((PetscObject)bjac->mat[i],mprefix);
1189:       KSPSetOperators(jac->ksp[i],bjac->mat[i],bjac->pmat[i],pc->flag);
1190:     } else {
1191:       KSPSetOperators(jac->ksp[i],bjac->pmat[i],bjac->pmat[i],pc->flag);
1192:     }
1193:     if(pc->setfromoptionscalled){
1194:       KSPSetFromOptions(jac->ksp[i]);
1195:     }
1196:   }
1197:   return(0);
1198: }