Actual source code: bjacobi.c
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: PetscBool hasop;
18: PetscInt N,M,start,i,sum,end;
19: PetscInt bs,i_start=-1,i_end=-1;
20: PetscMPIInt rank,size;
23: MPI_Comm_rank(PetscObjectComm((PetscObject)pc),&rank);
24: MPI_Comm_size(PetscObjectComm((PetscObject)pc),&size);
25: MatGetLocalSize(pc->pmat,&M,&N);
26: MatGetBlockSize(pc->pmat,&bs);
28: if (jac->n > 0 && jac->n < size) {
29: PCSetUp_BJacobi_Multiproc(pc);
30: return(0);
31: }
33: /* --------------------------------------------------------------------------
34: Determines the number of blocks assigned to each processor
35: -----------------------------------------------------------------------------*/
37: /* local block count given */
38: if (jac->n_local > 0 && jac->n < 0) {
39: MPIU_Allreduce(&jac->n_local,&jac->n,1,MPIU_INT,MPI_SUM,PetscObjectComm((PetscObject)pc));
40: if (jac->l_lens) { /* check that user set these correctly */
41: sum = 0;
42: for (i=0; i<jac->n_local; i++) {
43: 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");
44: sum += jac->l_lens[i];
45: }
46: if (sum != M) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Local lens set incorrectly");
47: } else {
48: PetscMalloc1(jac->n_local,&jac->l_lens);
49: for (i=0; i<jac->n_local; i++) jac->l_lens[i] = bs*((M/bs)/jac->n_local + (((M/bs) % jac->n_local) > i));
50: }
51: } else if (jac->n > 0 && jac->n_local < 0) { /* global block count given */
52: /* global blocks given: determine which ones are local */
53: if (jac->g_lens) {
54: /* check if the g_lens is has valid entries */
55: for (i=0; i<jac->n; i++) {
56: if (!jac->g_lens[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Zero block not allowed");
57: 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");
58: }
59: if (size == 1) {
60: jac->n_local = jac->n;
61: PetscMalloc1(jac->n_local,&jac->l_lens);
62: PetscArraycpy(jac->l_lens,jac->g_lens,jac->n_local);
63: /* check that user set these correctly */
64: sum = 0;
65: for (i=0; i<jac->n_local; i++) sum += jac->l_lens[i];
66: if (sum != M) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Global lens set incorrectly");
67: } else {
68: MatGetOwnershipRange(pc->pmat,&start,&end);
69: /* loop over blocks determing first one owned by me */
70: sum = 0;
71: for (i=0; i<jac->n+1; i++) {
72: if (sum == start) { i_start = i; goto start_1;}
73: if (i < jac->n) sum += jac->g_lens[i];
74: }
75: SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Block sizes used in PCBJacobiSetTotalBlocks()\nare not compatible with parallel matrix layout");
76: start_1:
77: for (i=i_start; i<jac->n+1; i++) {
78: if (sum == end) { i_end = i; goto end_1; }
79: if (i < jac->n) sum += jac->g_lens[i];
80: }
81: SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Block sizes used in PCBJacobiSetTotalBlocks()\nare not compatible with parallel matrix layout");
82: end_1:
83: jac->n_local = i_end - i_start;
84: PetscMalloc1(jac->n_local,&jac->l_lens);
85: PetscArraycpy(jac->l_lens,jac->g_lens+i_start,jac->n_local);
86: }
87: } else { /* no global blocks given, determine then using default layout */
88: jac->n_local = jac->n/size + ((jac->n % size) > rank);
89: PetscMalloc1(jac->n_local,&jac->l_lens);
90: for (i=0; i<jac->n_local; i++) {
91: jac->l_lens[i] = ((M/bs)/jac->n_local + (((M/bs) % jac->n_local) > i))*bs;
92: if (!jac->l_lens[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_SIZ,"Too many blocks given");
93: }
94: }
95: } else if (jac->n < 0 && jac->n_local < 0) { /* no blocks given */
96: jac->n = size;
97: jac->n_local = 1;
98: PetscMalloc1(1,&jac->l_lens);
99: jac->l_lens[0] = M;
100: } else { /* jac->n > 0 && jac->n_local > 0 */
101: if (!jac->l_lens) {
102: PetscMalloc1(jac->n_local,&jac->l_lens);
103: for (i=0; i<jac->n_local; i++) jac->l_lens[i] = bs*((M/bs)/jac->n_local + (((M/bs) % jac->n_local) > i));
104: }
105: }
106: if (jac->n_local < 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Number of blocks is less than number of processors");
108: /* -------------------------
109: Determines mat and pmat
110: ---------------------------*/
111: MatHasOperation(pc->mat,MATOP_GET_DIAGONAL_BLOCK,&hasop);
112: if (!hasop && size == 1) {
113: mat = pc->mat;
114: pmat = pc->pmat;
115: } else {
116: if (pc->useAmat) {
117: /* use block from Amat matrix, not Pmat for local MatMult() */
118: MatGetDiagonalBlock(pc->mat,&mat);
119: }
120: if (pc->pmat != pc->mat || !pc->useAmat) {
121: MatGetDiagonalBlock(pc->pmat,&pmat);
122: } else pmat = mat;
123: }
125: /* ------
126: Setup code depends on the number of blocks
127: */
128: if (jac->n_local == 1) {
129: PCSetUp_BJacobi_Singleblock(pc,mat,pmat);
130: } else {
131: PCSetUp_BJacobi_Multiblock(pc,mat,pmat);
132: }
133: return(0);
134: }
136: /* Default destroy, if it has never been setup */
137: static PetscErrorCode PCDestroy_BJacobi(PC pc)
138: {
139: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
143: PetscFree(jac->g_lens);
144: PetscFree(jac->l_lens);
145: PetscFree(pc->data);
146: return(0);
147: }
149: static PetscErrorCode PCSetFromOptions_BJacobi(PetscOptionItems *PetscOptionsObject,PC pc)
150: {
151: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
153: PetscInt blocks,i;
154: PetscBool flg;
157: PetscOptionsHead(PetscOptionsObject,"Block Jacobi options");
158: PetscOptionsInt("-pc_bjacobi_blocks","Total number of blocks","PCBJacobiSetTotalBlocks",jac->n,&blocks,&flg);
159: if (flg) {PCBJacobiSetTotalBlocks(pc,blocks,NULL);}
160: PetscOptionsInt("-pc_bjacobi_local_blocks","Local number of blocks","PCBJacobiSetLocalBlocks",jac->n_local,&blocks,&flg);
161: if (flg) {PCBJacobiSetLocalBlocks(pc,blocks,NULL);}
162: if (jac->ksp) {
163: /* The sub-KSP has already been set up (e.g., PCSetUp_BJacobi_Singleblock), but KSPSetFromOptions was not called
164: * unless we had already been called. */
165: for (i=0; i<jac->n_local; i++) {
166: KSPSetFromOptions(jac->ksp[i]);
167: }
168: }
169: PetscOptionsTail();
170: return(0);
171: }
173: #include <petscdraw.h>
174: static PetscErrorCode PCView_BJacobi(PC pc,PetscViewer viewer)
175: {
176: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
177: PC_BJacobi_Multiproc *mpjac = (PC_BJacobi_Multiproc*)jac->data;
178: PetscErrorCode ierr;
179: PetscMPIInt rank;
180: PetscInt i;
181: PetscBool iascii,isstring,isdraw;
182: PetscViewer sviewer;
183: PetscViewerFormat format;
184: const char *prefix;
187: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
188: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);
189: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERDRAW,&isdraw);
190: if (iascii) {
191: if (pc->useAmat) {
192: PetscViewerASCIIPrintf(viewer," using Amat local matrix, number of blocks = %D\n",jac->n);
193: }
194: PetscViewerASCIIPrintf(viewer," number of blocks = %D\n",jac->n);
195: MPI_Comm_rank(PetscObjectComm((PetscObject)pc),&rank);
196: PetscViewerGetFormat(viewer,&format);
197: if (format != PETSC_VIEWER_ASCII_INFO_DETAIL) {
198: PetscViewerASCIIPrintf(viewer," Local solver information for first block is in the following KSP and PC objects on rank 0:\n");
199: PCGetOptionsPrefix(pc,&prefix);
200: PetscViewerASCIIPrintf(viewer," Use -%sksp_view ::ascii_info_detail to display information for all blocks\n",prefix?prefix:"");
201: if (jac->ksp && !jac->psubcomm) {
202: PetscViewerGetSubViewer(viewer,PETSC_COMM_SELF,&sviewer);
203: if (rank == 0) {
204: PetscViewerASCIIPushTab(viewer);
205: KSPView(jac->ksp[0],sviewer);
206: PetscViewerASCIIPopTab(viewer);
207: }
208: PetscViewerFlush(sviewer);
209: PetscViewerRestoreSubViewer(viewer,PETSC_COMM_SELF,&sviewer);
210: PetscViewerFlush(viewer);
211: /* extra call needed because of the two calls to PetscViewerASCIIPushSynchronized() in PetscViewerGetSubViewer() */
212: PetscViewerASCIIPopSynchronized(viewer);
213: } else if (mpjac && jac->ksp && mpjac->psubcomm) {
214: PetscViewerGetSubViewer(viewer,mpjac->psubcomm->child,&sviewer);
215: if (!mpjac->psubcomm->color) {
216: PetscViewerASCIIPushTab(viewer);
217: KSPView(*(jac->ksp),sviewer);
218: PetscViewerASCIIPopTab(viewer);
219: }
220: PetscViewerFlush(sviewer);
221: PetscViewerRestoreSubViewer(viewer,mpjac->psubcomm->child,&sviewer);
222: PetscViewerFlush(viewer);
223: /* extra call needed because of the two calls to PetscViewerASCIIPushSynchronized() in PetscViewerGetSubViewer() */
224: PetscViewerASCIIPopSynchronized(viewer);
225: } else {
226: PetscViewerFlush(viewer);
227: }
228: } else {
229: PetscInt n_global;
230: MPIU_Allreduce(&jac->n_local,&n_global,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)pc));
231: PetscViewerASCIIPushSynchronized(viewer);
232: PetscViewerASCIIPrintf(viewer," Local solver information for each block is in the following KSP and PC objects:\n");
233: PetscViewerASCIISynchronizedPrintf(viewer,"[%d] number of local blocks = %D, first local block number = %D\n",
234: rank,jac->n_local,jac->first_local);
235: PetscViewerASCIIPushTab(viewer);
236: PetscViewerGetSubViewer(viewer,PETSC_COMM_SELF,&sviewer);
237: for (i=0; i<jac->n_local; i++) {
238: PetscViewerASCIISynchronizedPrintf(viewer,"[%d] local block number %D\n",rank,i);
239: KSPView(jac->ksp[i],sviewer);
240: PetscViewerASCIISynchronizedPrintf(viewer,"- - - - - - - - - - - - - - - - - -\n");
241: }
242: PetscViewerRestoreSubViewer(viewer,PETSC_COMM_SELF,&sviewer);
243: PetscViewerASCIIPopTab(viewer);
244: PetscViewerFlush(viewer);
245: PetscViewerASCIIPopSynchronized(viewer);
246: }
247: } else if (isstring) {
248: PetscViewerStringSPrintf(viewer," blks=%D",jac->n);
249: PetscViewerGetSubViewer(viewer,PETSC_COMM_SELF,&sviewer);
250: if (jac->ksp) {KSPView(jac->ksp[0],sviewer);}
251: PetscViewerRestoreSubViewer(viewer,PETSC_COMM_SELF,&sviewer);
252: } else if (isdraw) {
253: PetscDraw draw;
254: char str[25];
255: PetscReal x,y,bottom,h;
257: PetscViewerDrawGetDraw(viewer,0,&draw);
258: PetscDrawGetCurrentPoint(draw,&x,&y);
259: PetscSNPrintf(str,25,"Number blocks %D",jac->n);
260: PetscDrawStringBoxed(draw,x,y,PETSC_DRAW_RED,PETSC_DRAW_BLACK,str,NULL,&h);
261: bottom = y - h;
262: PetscDrawPushCurrentPoint(draw,x,bottom);
263: /* warning the communicator on viewer is different then on ksp in parallel */
264: if (jac->ksp) {KSPView(jac->ksp[0],viewer);}
265: PetscDrawPopCurrentPoint(draw);
266: }
267: return(0);
268: }
270: /* -------------------------------------------------------------------------------------*/
272: static PetscErrorCode PCBJacobiGetSubKSP_BJacobi(PC pc,PetscInt *n_local,PetscInt *first_local,KSP **ksp)
273: {
274: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
277: if (!pc->setupcalled) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"Must call KSPSetUp() or PCSetUp() first");
279: if (n_local) *n_local = jac->n_local;
280: if (first_local) *first_local = jac->first_local;
281: if (ksp) *ksp = jac->ksp;
282: return(0);
283: }
285: static PetscErrorCode PCBJacobiSetTotalBlocks_BJacobi(PC pc,PetscInt blocks,PetscInt *lens)
286: {
287: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
291: 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");
292: jac->n = blocks;
293: if (!lens) jac->g_lens = NULL;
294: else {
295: PetscMalloc1(blocks,&jac->g_lens);
296: PetscLogObjectMemory((PetscObject)pc,blocks*sizeof(PetscInt));
297: PetscArraycpy(jac->g_lens,lens,blocks);
298: }
299: return(0);
300: }
302: static PetscErrorCode PCBJacobiGetTotalBlocks_BJacobi(PC pc, PetscInt *blocks, const PetscInt *lens[])
303: {
304: PC_BJacobi *jac = (PC_BJacobi*) pc->data;
307: *blocks = jac->n;
308: if (lens) *lens = jac->g_lens;
309: return(0);
310: }
312: static PetscErrorCode PCBJacobiSetLocalBlocks_BJacobi(PC pc,PetscInt blocks,const PetscInt lens[])
313: {
314: PC_BJacobi *jac;
318: jac = (PC_BJacobi*)pc->data;
320: jac->n_local = blocks;
321: if (!lens) jac->l_lens = NULL;
322: else {
323: PetscMalloc1(blocks,&jac->l_lens);
324: PetscLogObjectMemory((PetscObject)pc,blocks*sizeof(PetscInt));
325: PetscArraycpy(jac->l_lens,lens,blocks);
326: }
327: return(0);
328: }
330: static PetscErrorCode PCBJacobiGetLocalBlocks_BJacobi(PC pc, PetscInt *blocks, const PetscInt *lens[])
331: {
332: PC_BJacobi *jac = (PC_BJacobi*) pc->data;
335: *blocks = jac->n_local;
336: if (lens) *lens = jac->l_lens;
337: return(0);
338: }
340: /* -------------------------------------------------------------------------------------*/
342: /*@C
343: PCBJacobiGetSubKSP - Gets the local KSP contexts for all blocks on
344: this processor.
346: Not Collective
348: Input Parameter:
349: . pc - the preconditioner context
351: Output Parameters:
352: + n_local - the number of blocks on this processor, or NULL
353: . first_local - the global number of the first block on this processor, or NULL
354: - ksp - the array of KSP contexts
356: Notes:
357: After PCBJacobiGetSubKSP() the array of KSP contexts is not to be freed.
359: Currently for some matrix implementations only 1 block per processor
360: is supported.
362: You must call KSPSetUp() or PCSetUp() before calling PCBJacobiGetSubKSP().
364: Fortran Usage: You must pass in a KSP array that is large enough to contain all the local KSPs.
365: You can call PCBJacobiGetSubKSP(pc,nlocal,firstlocal,PETSC_NULL_KSP,ierr) to determine how large the
366: KSP array must be.
368: Level: advanced
370: .seealso: PCASMGetSubKSP()
371: @*/
372: PetscErrorCode PCBJacobiGetSubKSP(PC pc,PetscInt *n_local,PetscInt *first_local,KSP *ksp[])
373: {
378: PetscUseMethod(pc,"PCBJacobiGetSubKSP_C",(PC,PetscInt*,PetscInt*,KSP **),(pc,n_local,first_local,ksp));
379: return(0);
380: }
382: /*@
383: PCBJacobiSetTotalBlocks - Sets the global number of blocks for the block
384: Jacobi preconditioner.
386: Collective on PC
388: Input Parameters:
389: + pc - the preconditioner context
390: . blocks - the number of blocks
391: - lens - [optional] integer array containing the size of each block
393: Options Database Key:
394: . -pc_bjacobi_blocks <blocks> - Sets the number of global blocks
396: Notes:
397: Currently only a limited number of blocking configurations are supported.
398: All processors sharing the PC must call this routine with the same data.
400: Level: intermediate
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: .seealso: PCSetUseAmat(), PCBJacobiGetLocalBlocks()
431: @*/
432: PetscErrorCode PCBJacobiGetTotalBlocks(PC pc, PetscInt *blocks, const PetscInt *lens[])
433: {
439: PetscUseMethod(pc,"PCBJacobiGetTotalBlocks_C",(PC,PetscInt*, const PetscInt *[]),(pc,blocks,lens));
440: return(0);
441: }
443: /*@
444: PCBJacobiSetLocalBlocks - Sets the local number of blocks for the block
445: Jacobi preconditioner.
447: Not Collective
449: Input Parameters:
450: + pc - the preconditioner context
451: . blocks - the number of blocks
452: - lens - [optional] integer array containing size of each block
454: Options Database Key:
455: . -pc_bjacobi_local_blocks <blocks> - Sets the number of local blocks
457: Note:
458: Currently only a limited number of blocking configurations are supported.
460: Level: intermediate
462: .seealso: PCSetUseAmat(), PCBJacobiSetTotalBlocks()
463: @*/
464: PetscErrorCode PCBJacobiSetLocalBlocks(PC pc,PetscInt blocks,const PetscInt lens[])
465: {
470: if (blocks < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Must have nonegative blocks");
471: PetscTryMethod(pc,"PCBJacobiSetLocalBlocks_C",(PC,PetscInt,const PetscInt []),(pc,blocks,lens));
472: return(0);
473: }
475: /*@C
476: PCBJacobiGetLocalBlocks - Gets the local number of blocks for the block
477: Jacobi preconditioner.
479: Not Collective
481: Input Parameters:
482: + pc - the preconditioner context
483: . blocks - the number of blocks
484: - lens - [optional] integer array containing size of each block
486: Note:
487: Currently only a limited number of blocking configurations are supported.
489: Level: intermediate
491: .seealso: PCSetUseAmat(), PCBJacobiGetTotalBlocks()
492: @*/
493: PetscErrorCode PCBJacobiGetLocalBlocks(PC pc, PetscInt *blocks, const PetscInt *lens[])
494: {
500: PetscUseMethod(pc,"PCBJacobiGetLocalBlocks_C",(PC,PetscInt*, const PetscInt *[]),(pc,blocks,lens));
501: return(0);
502: }
504: /* -----------------------------------------------------------------------------------*/
506: /*MC
507: PCBJACOBI - Use block Jacobi preconditioning, each block is (approximately) solved with
508: its own KSP object.
510: Options Database Keys:
511: + -pc_use_amat - use Amat to apply block of operator in inner Krylov method
512: - -pc_bjacobi_blocks <n> - use n total blocks
514: Notes:
515: Each processor can have one or more blocks, or a single block can be shared by several processes. Defaults to one block per processor.
517: To set options on the solvers for each block append -sub_ to all the KSP, KSP, and PC
518: options database keys. For example, -sub_pc_type ilu -sub_pc_factor_levels 1 -sub_ksp_type preonly
520: To set the options on the solvers separate for each block call PCBJacobiGetSubKSP()
521: and set the options directly on the resulting KSP object (you can access its PC
522: KSPGetPC())
524: For GPU-based vectors (CUDA, ViennaCL) it is recommended to use exactly one block per MPI process for best
525: performance. Different block partitioning may lead to additional data transfers
526: between host and GPU that lead to degraded performance.
528: The options prefix for each block is sub_, for example -sub_pc_type lu.
530: When multiple processes share a single block, each block encompasses exactly all the unknowns owned its set of processes.
532: See PCJACOBI for point Jacobi preconditioning, PCVPBJACOBI for variable size point block Jacobi and PCPBJACOBI for large blocks
534: Level: beginner
536: .seealso: PCCreate(), PCSetType(), PCType (for list of available types), PC,
537: PCASM, PCSetUseAmat(), PCGetUseAmat(), PCBJacobiGetSubKSP(), PCBJacobiSetTotalBlocks(),
538: PCBJacobiSetLocalBlocks(), PCSetModifySubMatrices(), PCJACOBI, PCVPBJACOBI, PCPBJACOBI
539: M*/
541: PETSC_EXTERN PetscErrorCode PCCreate_BJacobi(PC pc)
542: {
544: PetscMPIInt rank;
545: PC_BJacobi *jac;
548: PetscNewLog(pc,&jac);
549: MPI_Comm_rank(PetscObjectComm((PetscObject)pc),&rank);
551: pc->ops->apply = NULL;
552: pc->ops->matapply = NULL;
553: pc->ops->applytranspose = NULL;
554: pc->ops->setup = PCSetUp_BJacobi;
555: pc->ops->destroy = PCDestroy_BJacobi;
556: pc->ops->setfromoptions = PCSetFromOptions_BJacobi;
557: pc->ops->view = PCView_BJacobi;
558: pc->ops->applyrichardson = NULL;
560: pc->data = (void*)jac;
561: jac->n = -1;
562: jac->n_local = -1;
563: jac->first_local = rank;
564: jac->ksp = NULL;
565: jac->g_lens = NULL;
566: jac->l_lens = NULL;
567: jac->psubcomm = NULL;
569: PetscObjectComposeFunction((PetscObject)pc,"PCBJacobiGetSubKSP_C",PCBJacobiGetSubKSP_BJacobi);
570: PetscObjectComposeFunction((PetscObject)pc,"PCBJacobiSetTotalBlocks_C",PCBJacobiSetTotalBlocks_BJacobi);
571: PetscObjectComposeFunction((PetscObject)pc,"PCBJacobiGetTotalBlocks_C",PCBJacobiGetTotalBlocks_BJacobi);
572: PetscObjectComposeFunction((PetscObject)pc,"PCBJacobiSetLocalBlocks_C",PCBJacobiSetLocalBlocks_BJacobi);
573: PetscObjectComposeFunction((PetscObject)pc,"PCBJacobiGetLocalBlocks_C",PCBJacobiGetLocalBlocks_BJacobi);
574: return(0);
575: }
577: /* --------------------------------------------------------------------------------------------*/
578: /*
579: These are for a single block per processor; works for AIJ, BAIJ; Seq and MPI
580: */
581: static PetscErrorCode PCReset_BJacobi_Singleblock(PC pc)
582: {
583: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
584: PC_BJacobi_Singleblock *bjac = (PC_BJacobi_Singleblock*)jac->data;
585: PetscErrorCode ierr;
588: KSPReset(jac->ksp[0]);
589: VecDestroy(&bjac->x);
590: VecDestroy(&bjac->y);
591: return(0);
592: }
594: static PetscErrorCode PCDestroy_BJacobi_Singleblock(PC pc)
595: {
596: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
597: PC_BJacobi_Singleblock *bjac = (PC_BJacobi_Singleblock*)jac->data;
598: PetscErrorCode ierr;
601: PCReset_BJacobi_Singleblock(pc);
602: KSPDestroy(&jac->ksp[0]);
603: PetscFree(jac->ksp);
604: PetscFree(jac->l_lens);
605: PetscFree(jac->g_lens);
606: PetscFree(bjac);
607: PetscFree(pc->data);
608: return(0);
609: }
611: static PetscErrorCode PCSetUpOnBlocks_BJacobi_Singleblock(PC pc)
612: {
613: PetscErrorCode ierr;
614: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
615: KSP subksp = jac->ksp[0];
616: KSPConvergedReason reason;
619: KSPSetUp(subksp);
620: KSPGetConvergedReason(subksp,&reason);
621: if (reason == KSP_DIVERGED_PC_FAILED) {
622: pc->failedreason = PC_SUBPC_ERROR;
623: }
624: return(0);
625: }
627: static PetscErrorCode PCApply_BJacobi_Singleblock(PC pc,Vec x,Vec y)
628: {
629: PetscErrorCode ierr;
630: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
631: PC_BJacobi_Singleblock *bjac = (PC_BJacobi_Singleblock*)jac->data;
634: VecGetLocalVectorRead(x, bjac->x);
635: VecGetLocalVector(y, bjac->y);
636: /* Since the inner KSP matrix may point directly to the diagonal block of an MPI matrix the inner
637: matrix may change even if the outer KSP/PC has not updated the preconditioner, this will trigger a rebuild
638: of the inner preconditioner automatically unless we pass down the outer preconditioners reuse flag.*/
639: KSPSetReusePreconditioner(jac->ksp[0],pc->reusepreconditioner);
640: KSPSolve(jac->ksp[0],bjac->x,bjac->y);
641: KSPCheckSolve(jac->ksp[0],pc,bjac->y);
642: VecRestoreLocalVectorRead(x, bjac->x);
643: VecRestoreLocalVector(y, bjac->y);
644: return(0);
645: }
647: static PetscErrorCode PCMatApply_BJacobi_Singleblock(PC pc,Mat X,Mat Y)
648: {
649: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
650: Mat sX,sY;
654: /* Since the inner KSP matrix may point directly to the diagonal block of an MPI matrix the inner
655: matrix may change even if the outer KSP/PC has not updated the preconditioner, this will trigger a rebuild
656: of the inner preconditioner automatically unless we pass down the outer preconditioners reuse flag.*/
657: KSPSetReusePreconditioner(jac->ksp[0],pc->reusepreconditioner);
658: MatDenseGetLocalMatrix(X,&sX);
659: MatDenseGetLocalMatrix(Y,&sY);
660: KSPMatSolve(jac->ksp[0],sX,sY);
661: return(0);
662: }
664: static PetscErrorCode PCApplySymmetricLeft_BJacobi_Singleblock(PC pc,Vec x,Vec y)
665: {
666: PetscErrorCode ierr;
667: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
668: PC_BJacobi_Singleblock *bjac = (PC_BJacobi_Singleblock*)jac->data;
669: PetscScalar *y_array;
670: const PetscScalar *x_array;
671: PC subpc;
674: /*
675: The VecPlaceArray() is to avoid having to copy the
676: y vector into the bjac->x vector. The reason for
677: the bjac->x vector is that we need a sequential vector
678: for the sequential solve.
679: */
680: VecGetArrayRead(x,&x_array);
681: VecGetArray(y,&y_array);
682: VecPlaceArray(bjac->x,x_array);
683: VecPlaceArray(bjac->y,y_array);
684: /* apply the symmetric left portion of the inner PC operator */
685: /* note this by-passes the inner KSP and its options completely */
686: KSPGetPC(jac->ksp[0],&subpc);
687: PCApplySymmetricLeft(subpc,bjac->x,bjac->y);
688: VecResetArray(bjac->x);
689: VecResetArray(bjac->y);
690: VecRestoreArrayRead(x,&x_array);
691: VecRestoreArray(y,&y_array);
692: return(0);
693: }
695: static PetscErrorCode PCApplySymmetricRight_BJacobi_Singleblock(PC pc,Vec x,Vec y)
696: {
697: PetscErrorCode ierr;
698: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
699: PC_BJacobi_Singleblock *bjac = (PC_BJacobi_Singleblock*)jac->data;
700: PetscScalar *y_array;
701: const PetscScalar *x_array;
702: PC subpc;
705: /*
706: The VecPlaceArray() is to avoid having to copy the
707: y vector into the bjac->x vector. The reason for
708: the bjac->x vector is that we need a sequential vector
709: for the sequential solve.
710: */
711: VecGetArrayRead(x,&x_array);
712: VecGetArray(y,&y_array);
713: VecPlaceArray(bjac->x,x_array);
714: VecPlaceArray(bjac->y,y_array);
716: /* apply the symmetric right portion of the inner PC operator */
717: /* note this by-passes the inner KSP and its options completely */
719: KSPGetPC(jac->ksp[0],&subpc);
720: PCApplySymmetricRight(subpc,bjac->x,bjac->y);
722: VecRestoreArrayRead(x,&x_array);
723: VecRestoreArray(y,&y_array);
724: return(0);
725: }
727: static PetscErrorCode PCApplyTranspose_BJacobi_Singleblock(PC pc,Vec x,Vec y)
728: {
729: PetscErrorCode ierr;
730: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
731: PC_BJacobi_Singleblock *bjac = (PC_BJacobi_Singleblock*)jac->data;
732: PetscScalar *y_array;
733: const PetscScalar *x_array;
736: /*
737: The VecPlaceArray() is to avoid having to copy the
738: y vector into the bjac->x vector. The reason for
739: the bjac->x vector is that we need a sequential vector
740: for the sequential solve.
741: */
742: VecGetArrayRead(x,&x_array);
743: VecGetArray(y,&y_array);
744: VecPlaceArray(bjac->x,x_array);
745: VecPlaceArray(bjac->y,y_array);
746: KSPSolveTranspose(jac->ksp[0],bjac->x,bjac->y);
747: KSPCheckSolve(jac->ksp[0],pc,bjac->y);
748: VecResetArray(bjac->x);
749: VecResetArray(bjac->y);
750: VecRestoreArrayRead(x,&x_array);
751: VecRestoreArray(y,&y_array);
752: return(0);
753: }
755: static PetscErrorCode PCSetUp_BJacobi_Singleblock(PC pc,Mat mat,Mat pmat)
756: {
757: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
758: PetscErrorCode ierr;
759: PetscInt m;
760: KSP ksp;
761: PC_BJacobi_Singleblock *bjac;
762: PetscBool wasSetup = PETSC_TRUE;
763: VecType vectype;
764: const char *prefix;
767: if (!pc->setupcalled) {
768: if (!jac->ksp) {
769: wasSetup = PETSC_FALSE;
771: KSPCreate(PETSC_COMM_SELF,&ksp);
772: KSPSetErrorIfNotConverged(ksp,pc->erroriffailure);
773: PetscObjectIncrementTabLevel((PetscObject)ksp,(PetscObject)pc,1);
774: PetscLogObjectParent((PetscObject)pc,(PetscObject)ksp);
775: KSPSetType(ksp,KSPPREONLY);
776: PCGetOptionsPrefix(pc,&prefix);
777: KSPSetOptionsPrefix(ksp,prefix);
778: KSPAppendOptionsPrefix(ksp,"sub_");
780: pc->ops->reset = PCReset_BJacobi_Singleblock;
781: pc->ops->destroy = PCDestroy_BJacobi_Singleblock;
782: pc->ops->apply = PCApply_BJacobi_Singleblock;
783: pc->ops->matapply = PCMatApply_BJacobi_Singleblock;
784: pc->ops->applysymmetricleft = PCApplySymmetricLeft_BJacobi_Singleblock;
785: pc->ops->applysymmetricright = PCApplySymmetricRight_BJacobi_Singleblock;
786: pc->ops->applytranspose = PCApplyTranspose_BJacobi_Singleblock;
787: pc->ops->setuponblocks = PCSetUpOnBlocks_BJacobi_Singleblock;
789: PetscMalloc1(1,&jac->ksp);
790: jac->ksp[0] = ksp;
792: PetscNewLog(pc,&bjac);
793: jac->data = (void*)bjac;
794: } else {
795: ksp = jac->ksp[0];
796: bjac = (PC_BJacobi_Singleblock*)jac->data;
797: }
799: /*
800: The reason we need to generate these vectors is to serve
801: as the right-hand side and solution vector for the solve on the
802: block. We do not need to allocate space for the vectors since
803: that is provided via VecPlaceArray() just before the call to
804: KSPSolve() on the block.
805: */
806: MatGetSize(pmat,&m,&m);
807: VecCreateSeqWithArray(PETSC_COMM_SELF,1,m,NULL,&bjac->x);
808: VecCreateSeqWithArray(PETSC_COMM_SELF,1,m,NULL,&bjac->y);
809: MatGetVecType(pmat,&vectype);
810: VecSetType(bjac->x,vectype);
811: VecSetType(bjac->y,vectype);
812: PetscLogObjectParent((PetscObject)pc,(PetscObject)bjac->x);
813: PetscLogObjectParent((PetscObject)pc,(PetscObject)bjac->y);
814: } else {
815: ksp = jac->ksp[0];
816: bjac = (PC_BJacobi_Singleblock*)jac->data;
817: }
818: KSPGetOptionsPrefix(ksp,&prefix);
819: if (pc->useAmat) {
820: KSPSetOperators(ksp,mat,pmat);
821: MatSetOptionsPrefix(mat,prefix);
822: } else {
823: KSPSetOperators(ksp,pmat,pmat);
824: }
825: MatSetOptionsPrefix(pmat,prefix);
826: if (!wasSetup && pc->setfromoptionscalled) {
827: /* If PCSetFromOptions_BJacobi is called later, KSPSetFromOptions will be called at that time. */
828: KSPSetFromOptions(ksp);
829: }
830: return(0);
831: }
833: /* ---------------------------------------------------------------------------------------------*/
834: static PetscErrorCode PCReset_BJacobi_Multiblock(PC pc)
835: {
836: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
837: PC_BJacobi_Multiblock *bjac = (PC_BJacobi_Multiblock*)jac->data;
838: PetscErrorCode ierr;
839: PetscInt i;
842: if (bjac && bjac->pmat) {
843: MatDestroyMatrices(jac->n_local,&bjac->pmat);
844: if (pc->useAmat) {
845: MatDestroyMatrices(jac->n_local,&bjac->mat);
846: }
847: }
849: for (i=0; i<jac->n_local; i++) {
850: KSPReset(jac->ksp[i]);
851: if (bjac && bjac->x) {
852: VecDestroy(&bjac->x[i]);
853: VecDestroy(&bjac->y[i]);
854: ISDestroy(&bjac->is[i]);
855: }
856: }
857: PetscFree(jac->l_lens);
858: PetscFree(jac->g_lens);
859: return(0);
860: }
862: static PetscErrorCode PCDestroy_BJacobi_Multiblock(PC pc)
863: {
864: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
865: PC_BJacobi_Multiblock *bjac = (PC_BJacobi_Multiblock*)jac->data;
866: PetscErrorCode ierr;
867: PetscInt i;
870: PCReset_BJacobi_Multiblock(pc);
871: if (bjac) {
872: PetscFree2(bjac->x,bjac->y);
873: PetscFree(bjac->starts);
874: PetscFree(bjac->is);
875: }
876: PetscFree(jac->data);
877: for (i=0; i<jac->n_local; i++) {
878: KSPDestroy(&jac->ksp[i]);
879: }
880: PetscFree(jac->ksp);
881: PetscFree(pc->data);
882: return(0);
883: }
885: static PetscErrorCode PCSetUpOnBlocks_BJacobi_Multiblock(PC pc)
886: {
887: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
888: PetscErrorCode ierr;
889: PetscInt i,n_local = jac->n_local;
890: KSPConvergedReason reason;
893: for (i=0; i<n_local; i++) {
894: KSPSetUp(jac->ksp[i]);
895: KSPGetConvergedReason(jac->ksp[i],&reason);
896: if (reason == KSP_DIVERGED_PC_FAILED) {
897: pc->failedreason = PC_SUBPC_ERROR;
898: }
899: }
900: return(0);
901: }
903: /*
904: Preconditioner for block Jacobi
905: */
906: static PetscErrorCode PCApply_BJacobi_Multiblock(PC pc,Vec x,Vec y)
907: {
908: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
909: PetscErrorCode ierr;
910: PetscInt i,n_local = jac->n_local;
911: PC_BJacobi_Multiblock *bjac = (PC_BJacobi_Multiblock*)jac->data;
912: PetscScalar *yin;
913: const PetscScalar *xin;
916: VecGetArrayRead(x,&xin);
917: VecGetArray(y,&yin);
918: for (i=0; i<n_local; i++) {
919: /*
920: To avoid copying the subvector from x into a workspace we instead
921: make the workspace vector array point to the subpart of the array of
922: the global vector.
923: */
924: VecPlaceArray(bjac->x[i],xin+bjac->starts[i]);
925: VecPlaceArray(bjac->y[i],yin+bjac->starts[i]);
927: PetscLogEventBegin(PC_ApplyOnBlocks,jac->ksp[i],bjac->x[i],bjac->y[i],0);
928: KSPSolve(jac->ksp[i],bjac->x[i],bjac->y[i]);
929: KSPCheckSolve(jac->ksp[i],pc,bjac->y[i]);
930: PetscLogEventEnd(PC_ApplyOnBlocks,jac->ksp[i],bjac->x[i],bjac->y[i],0);
932: VecResetArray(bjac->x[i]);
933: VecResetArray(bjac->y[i]);
934: }
935: VecRestoreArrayRead(x,&xin);
936: VecRestoreArray(y,&yin);
937: return(0);
938: }
940: /*
941: Preconditioner for block Jacobi
942: */
943: static PetscErrorCode PCApplyTranspose_BJacobi_Multiblock(PC pc,Vec x,Vec y)
944: {
945: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
946: PetscErrorCode ierr;
947: PetscInt i,n_local = jac->n_local;
948: PC_BJacobi_Multiblock *bjac = (PC_BJacobi_Multiblock*)jac->data;
949: PetscScalar *yin;
950: const PetscScalar *xin;
953: VecGetArrayRead(x,&xin);
954: VecGetArray(y,&yin);
955: for (i=0; i<n_local; i++) {
956: /*
957: To avoid copying the subvector from x into a workspace we instead
958: make the workspace vector array point to the subpart of the array of
959: the global vector.
960: */
961: VecPlaceArray(bjac->x[i],xin+bjac->starts[i]);
962: VecPlaceArray(bjac->y[i],yin+bjac->starts[i]);
964: PetscLogEventBegin(PC_ApplyTransposeOnBlocks,jac->ksp[i],bjac->x[i],bjac->y[i],0);
965: KSPSolveTranspose(jac->ksp[i],bjac->x[i],bjac->y[i]);
966: KSPCheckSolve(jac->ksp[i],pc,bjac->y[i]);
967: PetscLogEventEnd(PC_ApplyTransposeOnBlocks,jac->ksp[i],bjac->x[i],bjac->y[i],0);
969: VecResetArray(bjac->x[i]);
970: VecResetArray(bjac->y[i]);
971: }
972: VecRestoreArrayRead(x,&xin);
973: VecRestoreArray(y,&yin);
974: return(0);
975: }
977: static PetscErrorCode PCSetUp_BJacobi_Multiblock(PC pc,Mat mat,Mat pmat)
978: {
979: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
980: PetscErrorCode ierr;
981: PetscInt m,n_local,N,M,start,i;
982: const char *prefix;
983: KSP ksp;
984: Vec x,y;
985: PC_BJacobi_Multiblock *bjac = (PC_BJacobi_Multiblock*)jac->data;
986: PC subpc;
987: IS is;
988: MatReuse scall;
989: VecType vectype;
992: MatGetLocalSize(pc->pmat,&M,&N);
994: n_local = jac->n_local;
996: if (pc->useAmat) {
997: PetscBool same;
998: PetscObjectTypeCompare((PetscObject)mat,((PetscObject)pmat)->type_name,&same);
999: if (!same) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_INCOMP,"Matrices not of same type");
1000: }
1002: if (!pc->setupcalled) {
1003: scall = MAT_INITIAL_MATRIX;
1005: if (!jac->ksp) {
1006: pc->ops->reset = PCReset_BJacobi_Multiblock;
1007: pc->ops->destroy = PCDestroy_BJacobi_Multiblock;
1008: pc->ops->apply = PCApply_BJacobi_Multiblock;
1009: pc->ops->matapply = NULL;
1010: pc->ops->applytranspose= PCApplyTranspose_BJacobi_Multiblock;
1011: pc->ops->setuponblocks = PCSetUpOnBlocks_BJacobi_Multiblock;
1013: PetscNewLog(pc,&bjac);
1014: PetscMalloc1(n_local,&jac->ksp);
1015: PetscLogObjectMemory((PetscObject)pc,sizeof(n_local*sizeof(KSP)));
1016: PetscMalloc2(n_local,&bjac->x,n_local,&bjac->y);
1017: PetscMalloc1(n_local,&bjac->starts);
1018: PetscLogObjectMemory((PetscObject)pc,sizeof(n_local*sizeof(PetscScalar)));
1020: jac->data = (void*)bjac;
1021: PetscMalloc1(n_local,&bjac->is);
1022: PetscLogObjectMemory((PetscObject)pc,sizeof(n_local*sizeof(IS)));
1024: for (i=0; i<n_local; i++) {
1025: KSPCreate(PETSC_COMM_SELF,&ksp);
1026: KSPSetErrorIfNotConverged(ksp,pc->erroriffailure);
1027: PetscObjectIncrementTabLevel((PetscObject)ksp,(PetscObject)pc,1);
1028: PetscLogObjectParent((PetscObject)pc,(PetscObject)ksp);
1029: KSPSetType(ksp,KSPPREONLY);
1030: KSPGetPC(ksp,&subpc);
1031: PCGetOptionsPrefix(pc,&prefix);
1032: KSPSetOptionsPrefix(ksp,prefix);
1033: KSPAppendOptionsPrefix(ksp,"sub_");
1035: jac->ksp[i] = ksp;
1036: }
1037: } else {
1038: bjac = (PC_BJacobi_Multiblock*)jac->data;
1039: }
1041: start = 0;
1042: MatGetVecType(pmat,&vectype);
1043: for (i=0; i<n_local; i++) {
1044: m = jac->l_lens[i];
1045: /*
1046: The reason we need to generate these vectors is to serve
1047: as the right-hand side and solution vector for the solve on the
1048: block. We do not need to allocate space for the vectors since
1049: that is provided via VecPlaceArray() just before the call to
1050: KSPSolve() on the block.
1052: */
1053: VecCreateSeq(PETSC_COMM_SELF,m,&x);
1054: VecCreateSeqWithArray(PETSC_COMM_SELF,1,m,NULL,&y);
1055: VecSetType(x,vectype);
1056: VecSetType(y,vectype);
1057: PetscLogObjectParent((PetscObject)pc,(PetscObject)x);
1058: PetscLogObjectParent((PetscObject)pc,(PetscObject)y);
1060: bjac->x[i] = x;
1061: bjac->y[i] = y;
1062: bjac->starts[i] = start;
1064: ISCreateStride(PETSC_COMM_SELF,m,start,1,&is);
1065: bjac->is[i] = is;
1066: PetscLogObjectParent((PetscObject)pc,(PetscObject)is);
1068: start += m;
1069: }
1070: } else {
1071: bjac = (PC_BJacobi_Multiblock*)jac->data;
1072: /*
1073: Destroy the blocks from the previous iteration
1074: */
1075: if (pc->flag == DIFFERENT_NONZERO_PATTERN) {
1076: MatDestroyMatrices(n_local,&bjac->pmat);
1077: if (pc->useAmat) {
1078: MatDestroyMatrices(n_local,&bjac->mat);
1079: }
1080: scall = MAT_INITIAL_MATRIX;
1081: } else scall = MAT_REUSE_MATRIX;
1082: }
1084: MatCreateSubMatrices(pmat,n_local,bjac->is,bjac->is,scall,&bjac->pmat);
1085: if (pc->useAmat) {
1086: MatCreateSubMatrices(mat,n_local,bjac->is,bjac->is,scall,&bjac->mat);
1087: }
1088: /* Return control to the user so that the submatrices can be modified (e.g., to apply
1089: different boundary conditions for the submatrices than for the global problem) */
1090: PCModifySubMatrices(pc,n_local,bjac->is,bjac->is,bjac->pmat,pc->modifysubmatricesP);
1092: for (i=0; i<n_local; i++) {
1093: PetscLogObjectParent((PetscObject)pc,(PetscObject)bjac->pmat[i]);
1094: KSPGetOptionsPrefix(jac->ksp[i],&prefix);
1095: if (pc->useAmat) {
1096: PetscLogObjectParent((PetscObject)pc,(PetscObject)bjac->mat[i]);
1097: KSPSetOperators(jac->ksp[i],bjac->mat[i],bjac->pmat[i]);
1098: MatSetOptionsPrefix(bjac->mat[i],prefix);
1099: } else {
1100: KSPSetOperators(jac->ksp[i],bjac->pmat[i],bjac->pmat[i]);
1101: }
1102: MatSetOptionsPrefix(bjac->pmat[i],prefix);
1103: if (pc->setfromoptionscalled) {
1104: KSPSetFromOptions(jac->ksp[i]);
1105: }
1106: }
1107: return(0);
1108: }
1110: /* ---------------------------------------------------------------------------------------------*/
1111: /*
1112: These are for a single block with multiple processes
1113: */
1114: static PetscErrorCode PCSetUpOnBlocks_BJacobi_Multiproc(PC pc)
1115: {
1116: PetscErrorCode ierr;
1117: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
1118: KSP subksp = jac->ksp[0];
1119: KSPConvergedReason reason;
1122: KSPSetUp(subksp);
1123: KSPGetConvergedReason(subksp,&reason);
1124: if (reason == KSP_DIVERGED_PC_FAILED) {
1125: pc->failedreason = PC_SUBPC_ERROR;
1126: }
1127: return(0);
1128: }
1130: static PetscErrorCode PCReset_BJacobi_Multiproc(PC pc)
1131: {
1132: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
1133: PC_BJacobi_Multiproc *mpjac = (PC_BJacobi_Multiproc*)jac->data;
1134: PetscErrorCode ierr;
1137: VecDestroy(&mpjac->ysub);
1138: VecDestroy(&mpjac->xsub);
1139: MatDestroy(&mpjac->submats);
1140: if (jac->ksp) {KSPReset(jac->ksp[0]);}
1141: return(0);
1142: }
1144: static PetscErrorCode PCDestroy_BJacobi_Multiproc(PC pc)
1145: {
1146: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
1147: PC_BJacobi_Multiproc *mpjac = (PC_BJacobi_Multiproc*)jac->data;
1148: PetscErrorCode ierr;
1151: PCReset_BJacobi_Multiproc(pc);
1152: KSPDestroy(&jac->ksp[0]);
1153: PetscFree(jac->ksp);
1154: PetscSubcommDestroy(&mpjac->psubcomm);
1156: PetscFree(mpjac);
1157: PetscFree(pc->data);
1158: return(0);
1159: }
1161: static PetscErrorCode PCApply_BJacobi_Multiproc(PC pc,Vec x,Vec y)
1162: {
1163: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
1164: PC_BJacobi_Multiproc *mpjac = (PC_BJacobi_Multiproc*)jac->data;
1165: PetscErrorCode ierr;
1166: PetscScalar *yarray;
1167: const PetscScalar *xarray;
1168: KSPConvergedReason reason;
1171: /* place x's and y's local arrays into xsub and ysub */
1172: VecGetArrayRead(x,&xarray);
1173: VecGetArray(y,&yarray);
1174: VecPlaceArray(mpjac->xsub,xarray);
1175: VecPlaceArray(mpjac->ysub,yarray);
1177: /* apply preconditioner on each matrix block */
1178: PetscLogEventBegin(PC_ApplyOnBlocks,jac->ksp[0],mpjac->xsub,mpjac->ysub,0);
1179: KSPSolve(jac->ksp[0],mpjac->xsub,mpjac->ysub);
1180: KSPCheckSolve(jac->ksp[0],pc,mpjac->ysub);
1181: PetscLogEventEnd(PC_ApplyOnBlocks,jac->ksp[0],mpjac->xsub,mpjac->ysub,0);
1182: KSPGetConvergedReason(jac->ksp[0],&reason);
1183: if (reason == KSP_DIVERGED_PC_FAILED) {
1184: pc->failedreason = PC_SUBPC_ERROR;
1185: }
1187: VecResetArray(mpjac->xsub);
1188: VecResetArray(mpjac->ysub);
1189: VecRestoreArrayRead(x,&xarray);
1190: VecRestoreArray(y,&yarray);
1191: return(0);
1192: }
1194: static PetscErrorCode PCMatApply_BJacobi_Multiproc(PC pc,Mat X,Mat Y)
1195: {
1196: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
1197: KSPConvergedReason reason;
1198: Mat sX,sY;
1199: const PetscScalar *x;
1200: PetscScalar *y;
1201: PetscInt m,N,lda,ldb;
1202: PetscErrorCode ierr;
1205: /* apply preconditioner on each matrix block */
1206: MatGetLocalSize(X,&m,NULL);
1207: MatGetSize(X,NULL,&N);
1208: MatDenseGetLDA(X,&lda);
1209: MatDenseGetLDA(Y,&ldb);
1210: MatDenseGetArrayRead(X,&x);
1211: MatDenseGetArrayWrite(Y,&y);
1212: MatCreateDense(PetscObjectComm((PetscObject)jac->ksp[0]),m,PETSC_DECIDE,PETSC_DECIDE,N,(PetscScalar*)x,&sX);
1213: MatCreateDense(PetscObjectComm((PetscObject)jac->ksp[0]),m,PETSC_DECIDE,PETSC_DECIDE,N,y,&sY);
1214: MatDenseSetLDA(sX,lda);
1215: MatDenseSetLDA(sY,ldb);
1216: PetscLogEventBegin(PC_ApplyOnBlocks,jac->ksp[0],X,Y,0);
1217: KSPMatSolve(jac->ksp[0],sX,sY);
1218: KSPCheckSolve(jac->ksp[0],pc,NULL);
1219: PetscLogEventEnd(PC_ApplyOnBlocks,jac->ksp[0],X,Y,0);
1220: MatDestroy(&sY);
1221: MatDestroy(&sX);
1222: MatDenseRestoreArrayWrite(Y,&y);
1223: MatDenseRestoreArrayRead(X,&x);
1224: KSPGetConvergedReason(jac->ksp[0],&reason);
1225: if (reason == KSP_DIVERGED_PC_FAILED) {
1226: pc->failedreason = PC_SUBPC_ERROR;
1227: }
1228: return(0);
1229: }
1231: static PetscErrorCode PCSetUp_BJacobi_Multiproc(PC pc)
1232: {
1233: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
1234: PC_BJacobi_Multiproc *mpjac = (PC_BJacobi_Multiproc*)jac->data;
1235: PetscErrorCode ierr;
1236: PetscInt m,n;
1237: MPI_Comm comm,subcomm=0;
1238: const char *prefix;
1239: PetscBool wasSetup = PETSC_TRUE;
1240: VecType vectype;
1243: PetscObjectGetComm((PetscObject)pc,&comm);
1244: if (jac->n_local > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only a single block in a subcommunicator is supported");
1245: jac->n_local = 1; /* currently only a single block is supported for a subcommunicator */
1246: if (!pc->setupcalled) {
1247: wasSetup = PETSC_FALSE;
1248: PetscNewLog(pc,&mpjac);
1249: jac->data = (void*)mpjac;
1251: /* initialize datastructure mpjac */
1252: if (!jac->psubcomm) {
1253: /* Create default contiguous subcommunicatiors if user does not provide them */
1254: PetscSubcommCreate(comm,&jac->psubcomm);
1255: PetscSubcommSetNumber(jac->psubcomm,jac->n);
1256: PetscSubcommSetType(jac->psubcomm,PETSC_SUBCOMM_CONTIGUOUS);
1257: PetscLogObjectMemory((PetscObject)pc,sizeof(PetscSubcomm));
1258: }
1259: mpjac->psubcomm = jac->psubcomm;
1260: subcomm = PetscSubcommChild(mpjac->psubcomm);
1262: /* Get matrix blocks of pmat */
1263: MatGetMultiProcBlock(pc->pmat,subcomm,MAT_INITIAL_MATRIX,&mpjac->submats);
1265: /* create a new PC that processors in each subcomm have copy of */
1266: PetscMalloc1(1,&jac->ksp);
1267: KSPCreate(subcomm,&jac->ksp[0]);
1268: KSPSetErrorIfNotConverged(jac->ksp[0],pc->erroriffailure);
1269: PetscObjectIncrementTabLevel((PetscObject)jac->ksp[0],(PetscObject)pc,1);
1270: PetscLogObjectParent((PetscObject)pc,(PetscObject)jac->ksp[0]);
1271: KSPSetOperators(jac->ksp[0],mpjac->submats,mpjac->submats);
1272: KSPGetPC(jac->ksp[0],&mpjac->pc);
1274: PCGetOptionsPrefix(pc,&prefix);
1275: KSPSetOptionsPrefix(jac->ksp[0],prefix);
1276: KSPAppendOptionsPrefix(jac->ksp[0],"sub_");
1277: KSPGetOptionsPrefix(jac->ksp[0],&prefix);
1278: MatSetOptionsPrefix(mpjac->submats,prefix);
1280: /* create dummy vectors xsub and ysub */
1281: MatGetLocalSize(mpjac->submats,&m,&n);
1282: VecCreateMPIWithArray(subcomm,1,n,PETSC_DECIDE,NULL,&mpjac->xsub);
1283: VecCreateMPIWithArray(subcomm,1,m,PETSC_DECIDE,NULL,&mpjac->ysub);
1284: MatGetVecType(mpjac->submats,&vectype);
1285: VecSetType(mpjac->xsub,vectype);
1286: VecSetType(mpjac->ysub,vectype);
1287: PetscLogObjectParent((PetscObject)pc,(PetscObject)mpjac->xsub);
1288: PetscLogObjectParent((PetscObject)pc,(PetscObject)mpjac->ysub);
1290: pc->ops->setuponblocks = PCSetUpOnBlocks_BJacobi_Multiproc;
1291: pc->ops->reset = PCReset_BJacobi_Multiproc;
1292: pc->ops->destroy = PCDestroy_BJacobi_Multiproc;
1293: pc->ops->apply = PCApply_BJacobi_Multiproc;
1294: pc->ops->matapply = PCMatApply_BJacobi_Multiproc;
1295: } else { /* pc->setupcalled */
1296: subcomm = PetscSubcommChild(mpjac->psubcomm);
1297: if (pc->flag == DIFFERENT_NONZERO_PATTERN) {
1298: /* destroy old matrix blocks, then get new matrix blocks */
1299: if (mpjac->submats) {MatDestroy(&mpjac->submats);}
1300: MatGetMultiProcBlock(pc->pmat,subcomm,MAT_INITIAL_MATRIX,&mpjac->submats);
1301: } else {
1302: MatGetMultiProcBlock(pc->pmat,subcomm,MAT_REUSE_MATRIX,&mpjac->submats);
1303: }
1304: KSPSetOperators(jac->ksp[0],mpjac->submats,mpjac->submats);
1305: }
1307: if (!wasSetup && pc->setfromoptionscalled) {
1308: KSPSetFromOptions(jac->ksp[0]);
1309: }
1310: return(0);
1311: }