Actual source code: bjacobi.c
petsc-3.10.5 2019-03-28
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;
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: MatHasOperation(pc->mat,MATOP_GET_DIAGONAL_BLOCK,&hasop);
113: if (!hasop && 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) {PCBJacobiSetTotalBlocks(pc,blocks,NULL);}
168: PetscOptionsInt("-pc_bjacobi_local_blocks","Local number of blocks","PCBJacobiSetLocalBlocks",jac->n_local,&blocks,&flg);
169: if (flg) {PCBJacobiSetLocalBlocks(pc,blocks,NULL);}
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," using Amat local matrix, number of blocks = %D\n",jac->n);
199: }
200: PetscViewerASCIIPrintf(viewer," 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: Not 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: Options Database Key:
457: . -pc_bjacobi_local_blocks <blocks> - Sets the number of local blocks
459: Note:
460: Currently only a limited number of blocking configurations are supported.
462: Level: intermediate
464: .keywords: PC, set, number, Jacobi, local, blocks
466: .seealso: PCSetUseAmat(), PCBJacobiSetTotalBlocks()
467: @*/
468: PetscErrorCode PCBJacobiSetLocalBlocks(PC pc,PetscInt blocks,const PetscInt lens[])
469: {
474: if (blocks < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Must have nonegative blocks");
475: PetscTryMethod(pc,"PCBJacobiSetLocalBlocks_C",(PC,PetscInt,const PetscInt []),(pc,blocks,lens));
476: return(0);
477: }
479: /*@C
480: PCBJacobiGetLocalBlocks - Gets the local number of blocks for the block
481: Jacobi preconditioner.
483: Not Collective
485: Input Parameters:
486: + pc - the preconditioner context
487: . blocks - the number of blocks
488: - lens - [optional] integer array containing size of each block
490: Note:
491: Currently only a limited number of blocking configurations are supported.
493: Level: intermediate
495: .keywords: PC, get, number, Jacobi, local, blocks
497: .seealso: PCSetUseAmat(), PCBJacobiGetTotalBlocks()
498: @*/
499: PetscErrorCode PCBJacobiGetLocalBlocks(PC pc, PetscInt *blocks, const PetscInt *lens[])
500: {
506: PetscUseMethod(pc,"PCBJacobiGetLocalBlocks_C",(PC,PetscInt*, const PetscInt *[]),(pc,blocks,lens));
507: return(0);
508: }
510: /* -----------------------------------------------------------------------------------*/
512: /*MC
513: PCBJACOBI - Use block Jacobi preconditioning, each block is (approximately) solved with
514: its own KSP object.
516: Options Database Keys:
517: + -pc_use_amat - use Amat to apply block of operator in inner Krylov method
518: - -pc_bjacobi_blocks <n> - use n total blocks
520: Notes:
521: Each processor can have one or more blocks, or a single block can be shared by several processes. Defaults to one block per processor.
523: To set options on the solvers for each block append -sub_ to all the KSP, KSP, and PC
524: options database keys. For example, -sub_pc_type ilu -sub_pc_factor_levels 1 -sub_ksp_type preonly
526: To set the options on the solvers separate for each block call PCBJacobiGetSubKSP()
527: and set the options directly on the resulting KSP object (you can access its PC
528: KSPGetPC())
530: For GPU-based vectors (CUDA, ViennaCL) it is recommended to use exactly one block per MPI process for best
531: performance. Different block partitioning may lead to additional data transfers
532: between host and GPU that lead to degraded performance.
534: The options prefix for each block is sub_, for example -sub_pc_type lu.
536: When multiple processes share a single block, each block encompasses exactly all the unknowns owned its set of processes.
538: Level: beginner
540: Concepts: block Jacobi
542: .seealso: PCCreate(), PCSetType(), PCType (for list of available types), PC,
543: PCASM, PCSetUseAmat(), PCGetUseAmat(), PCBJacobiGetSubKSP(), PCBJacobiSetTotalBlocks(),
544: PCBJacobiSetLocalBlocks(), PCSetModifySubmatrices()
545: M*/
547: PETSC_EXTERN PetscErrorCode PCCreate_BJacobi(PC pc)
548: {
550: PetscMPIInt rank;
551: PC_BJacobi *jac;
554: PetscNewLog(pc,&jac);
555: MPI_Comm_rank(PetscObjectComm((PetscObject)pc),&rank);
557: pc->ops->apply = 0;
558: pc->ops->applytranspose = 0;
559: pc->ops->setup = PCSetUp_BJacobi;
560: pc->ops->destroy = PCDestroy_BJacobi;
561: pc->ops->setfromoptions = PCSetFromOptions_BJacobi;
562: pc->ops->view = PCView_BJacobi;
563: pc->ops->applyrichardson = 0;
565: pc->data = (void*)jac;
566: jac->n = -1;
567: jac->n_local = -1;
568: jac->first_local = rank;
569: jac->ksp = 0;
570: jac->same_local_solves = PETSC_TRUE;
571: jac->g_lens = 0;
572: jac->l_lens = 0;
573: jac->psubcomm = 0;
575: PetscObjectComposeFunction((PetscObject)pc,"PCBJacobiGetSubKSP_C",PCBJacobiGetSubKSP_BJacobi);
576: PetscObjectComposeFunction((PetscObject)pc,"PCBJacobiSetTotalBlocks_C",PCBJacobiSetTotalBlocks_BJacobi);
577: PetscObjectComposeFunction((PetscObject)pc,"PCBJacobiGetTotalBlocks_C",PCBJacobiGetTotalBlocks_BJacobi);
578: PetscObjectComposeFunction((PetscObject)pc,"PCBJacobiSetLocalBlocks_C",PCBJacobiSetLocalBlocks_BJacobi);
579: PetscObjectComposeFunction((PetscObject)pc,"PCBJacobiGetLocalBlocks_C",PCBJacobiGetLocalBlocks_BJacobi);
580: return(0);
581: }
583: /* --------------------------------------------------------------------------------------------*/
584: /*
585: These are for a single block per processor; works for AIJ, BAIJ; Seq and MPI
586: */
587: static PetscErrorCode PCReset_BJacobi_Singleblock(PC pc)
588: {
589: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
590: PC_BJacobi_Singleblock *bjac = (PC_BJacobi_Singleblock*)jac->data;
591: PetscErrorCode ierr;
594: KSPReset(jac->ksp[0]);
595: VecDestroy(&bjac->x);
596: VecDestroy(&bjac->y);
597: return(0);
598: }
600: static PetscErrorCode PCDestroy_BJacobi_Singleblock(PC pc)
601: {
602: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
603: PC_BJacobi_Singleblock *bjac = (PC_BJacobi_Singleblock*)jac->data;
604: PetscErrorCode ierr;
607: PCReset_BJacobi_Singleblock(pc);
608: KSPDestroy(&jac->ksp[0]);
609: PetscFree(jac->ksp);
610: PetscFree(jac->l_lens);
611: PetscFree(jac->g_lens);
612: PetscFree(bjac);
613: PetscFree(pc->data);
614: return(0);
615: }
617: static PetscErrorCode PCSetUpOnBlocks_BJacobi_Singleblock(PC pc)
618: {
619: PetscErrorCode ierr;
620: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
621: KSP subksp = jac->ksp[0];
622: KSPConvergedReason reason;
625: KSPSetUp(subksp);
626: KSPGetConvergedReason(subksp,&reason);
627: if (reason == KSP_DIVERGED_PCSETUP_FAILED) {
628: pc->failedreason = PC_SUBPC_ERROR;
629: }
630: return(0);
631: }
633: static PetscErrorCode PCApply_BJacobi_Singleblock(PC pc,Vec x,Vec y)
634: {
635: PetscErrorCode ierr;
636: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
637: PC_BJacobi_Singleblock *bjac = (PC_BJacobi_Singleblock*)jac->data;
640: VecGetLocalVectorRead(x, bjac->x);
641: VecGetLocalVector(y, bjac->y);
642: /* Since the inner KSP matrix may point directly to the diagonal block of an MPI matrix the inner
643: matrix may change even if the outter KSP/PC has not updated the preconditioner, this will trigger a rebuild
644: of the inner preconditioner automatically unless we pass down the outter preconditioners reuse flag.*/
645: KSPSetReusePreconditioner(jac->ksp[0],pc->reusepreconditioner);
646: KSPSolve(jac->ksp[0],bjac->x,bjac->y);
647: VecRestoreLocalVectorRead(x, bjac->x);
648: VecRestoreLocalVector(y, bjac->y);
649: return(0);
650: }
652: static PetscErrorCode PCApplySymmetricLeft_BJacobi_Singleblock(PC pc,Vec x,Vec y)
653: {
654: PetscErrorCode ierr;
655: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
656: PC_BJacobi_Singleblock *bjac = (PC_BJacobi_Singleblock*)jac->data;
657: PetscScalar *y_array;
658: const PetscScalar *x_array;
659: PC subpc;
662: /*
663: The VecPlaceArray() is to avoid having to copy the
664: y vector into the bjac->x vector. The reason for
665: the bjac->x vector is that we need a sequential vector
666: for the sequential solve.
667: */
668: VecGetArrayRead(x,&x_array);
669: VecGetArray(y,&y_array);
670: VecPlaceArray(bjac->x,x_array);
671: VecPlaceArray(bjac->y,y_array);
672: /* apply the symmetric left portion of the inner PC operator */
673: /* note this by-passes the inner KSP and its options completely */
674: KSPGetPC(jac->ksp[0],&subpc);
675: PCApplySymmetricLeft(subpc,bjac->x,bjac->y);
676: VecResetArray(bjac->x);
677: VecResetArray(bjac->y);
678: VecRestoreArrayRead(x,&x_array);
679: VecRestoreArray(y,&y_array);
680: return(0);
681: }
683: static PetscErrorCode PCApplySymmetricRight_BJacobi_Singleblock(PC pc,Vec x,Vec y)
684: {
685: PetscErrorCode ierr;
686: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
687: PC_BJacobi_Singleblock *bjac = (PC_BJacobi_Singleblock*)jac->data;
688: PetscScalar *y_array;
689: const PetscScalar *x_array;
690: PC subpc;
693: /*
694: The VecPlaceArray() is to avoid having to copy the
695: y vector into the bjac->x vector. The reason for
696: the bjac->x vector is that we need a sequential vector
697: for the sequential solve.
698: */
699: VecGetArrayRead(x,&x_array);
700: VecGetArray(y,&y_array);
701: VecPlaceArray(bjac->x,x_array);
702: VecPlaceArray(bjac->y,y_array);
704: /* apply the symmetric right portion of the inner PC operator */
705: /* note this by-passes the inner KSP and its options completely */
707: KSPGetPC(jac->ksp[0],&subpc);
708: PCApplySymmetricRight(subpc,bjac->x,bjac->y);
710: VecRestoreArrayRead(x,&x_array);
711: VecRestoreArray(y,&y_array);
712: return(0);
713: }
715: static PetscErrorCode PCApplyTranspose_BJacobi_Singleblock(PC pc,Vec x,Vec y)
716: {
717: PetscErrorCode ierr;
718: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
719: PC_BJacobi_Singleblock *bjac = (PC_BJacobi_Singleblock*)jac->data;
720: PetscScalar *y_array;
721: const PetscScalar *x_array;
724: /*
725: The VecPlaceArray() is to avoid having to copy the
726: y vector into the bjac->x vector. The reason for
727: the bjac->x vector is that we need a sequential vector
728: for the sequential solve.
729: */
730: VecGetArrayRead(x,&x_array);
731: VecGetArray(y,&y_array);
732: VecPlaceArray(bjac->x,x_array);
733: VecPlaceArray(bjac->y,y_array);
734: KSPSolveTranspose(jac->ksp[0],bjac->x,bjac->y);
735: VecResetArray(bjac->x);
736: VecResetArray(bjac->y);
737: VecRestoreArrayRead(x,&x_array);
738: VecRestoreArray(y,&y_array);
739: return(0);
740: }
742: static PetscErrorCode PCSetUp_BJacobi_Singleblock(PC pc,Mat mat,Mat pmat)
743: {
744: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
745: PetscErrorCode ierr;
746: PetscInt m;
747: KSP ksp;
748: PC_BJacobi_Singleblock *bjac;
749: PetscBool wasSetup = PETSC_TRUE;
750: #if defined(PETSC_HAVE_VECCUDA) || defined(PETSC_HAVE_VIENNACL)
751: PetscBool is_gpumatrix = PETSC_FALSE;
752: #endif
755: if (!pc->setupcalled) {
756: const char *prefix;
758: if (!jac->ksp) {
759: wasSetup = PETSC_FALSE;
761: KSPCreate(PETSC_COMM_SELF,&ksp);
762: KSPSetErrorIfNotConverged(ksp,pc->erroriffailure);
763: PetscObjectIncrementTabLevel((PetscObject)ksp,(PetscObject)pc,1);
764: PetscLogObjectParent((PetscObject)pc,(PetscObject)ksp);
765: KSPSetType(ksp,KSPPREONLY);
766: PCGetOptionsPrefix(pc,&prefix);
767: KSPSetOptionsPrefix(ksp,prefix);
768: KSPAppendOptionsPrefix(ksp,"sub_");
770: pc->ops->reset = PCReset_BJacobi_Singleblock;
771: pc->ops->destroy = PCDestroy_BJacobi_Singleblock;
772: pc->ops->apply = PCApply_BJacobi_Singleblock;
773: pc->ops->applysymmetricleft = PCApplySymmetricLeft_BJacobi_Singleblock;
774: pc->ops->applysymmetricright = PCApplySymmetricRight_BJacobi_Singleblock;
775: pc->ops->applytranspose = PCApplyTranspose_BJacobi_Singleblock;
776: pc->ops->setuponblocks = PCSetUpOnBlocks_BJacobi_Singleblock;
778: PetscMalloc1(1,&jac->ksp);
779: jac->ksp[0] = ksp;
781: PetscNewLog(pc,&bjac);
782: jac->data = (void*)bjac;
783: } else {
784: ksp = jac->ksp[0];
785: bjac = (PC_BJacobi_Singleblock*)jac->data;
786: }
788: /*
789: The reason we need to generate these vectors is to serve
790: as the right-hand side and solution vector for the solve on the
791: block. We do not need to allocate space for the vectors since
792: that is provided via VecPlaceArray() just before the call to
793: KSPSolve() on the block.
794: */
795: MatGetSize(pmat,&m,&m);
796: VecCreateSeqWithArray(PETSC_COMM_SELF,1,m,NULL,&bjac->x);
797: VecCreateSeqWithArray(PETSC_COMM_SELF,1,m,NULL,&bjac->y);
798: #if defined(PETSC_HAVE_VECCUDA)
799: PetscObjectTypeCompareAny((PetscObject)pmat,&is_gpumatrix,MATAIJCUSPARSE,MATSEQAIJCUSPARSE,MATMPIAIJCUSPARSE,"");
800: if (is_gpumatrix) {
801: VecSetType(bjac->x,VECCUDA);
802: VecSetType(bjac->y,VECCUDA);
803: }
804: #endif
805: #if defined(PETSC_HAVE_VIENNACL)
806: PetscObjectTypeCompareAny((PetscObject)pmat,&is_gpumatrix,MATAIJVIENNACL,MATSEQAIJVIENNACL,MATMPIAIJVIENNACL,"");
807: if (is_gpumatrix) {
808: VecSetType(bjac->x,VECVIENNACL);
809: VecSetType(bjac->y,VECVIENNACL);
810: }
811: #endif
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: if (pc->useAmat) {
819: KSPSetOperators(ksp,mat,pmat);
820: } else {
821: KSPSetOperators(ksp,pmat,pmat);
822: }
823: if (!wasSetup && pc->setfromoptionscalled) {
824: /* If PCSetFromOptions_BJacobi is called later, KSPSetFromOptions will be called at that time. */
825: KSPSetFromOptions(ksp);
826: }
827: return(0);
828: }
830: /* ---------------------------------------------------------------------------------------------*/
831: static PetscErrorCode PCReset_BJacobi_Multiblock(PC pc)
832: {
833: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
834: PC_BJacobi_Multiblock *bjac = (PC_BJacobi_Multiblock*)jac->data;
835: PetscErrorCode ierr;
836: PetscInt i;
839: if (bjac && bjac->pmat) {
840: MatDestroyMatrices(jac->n_local,&bjac->pmat);
841: if (pc->useAmat) {
842: MatDestroyMatrices(jac->n_local,&bjac->mat);
843: }
844: }
846: for (i=0; i<jac->n_local; i++) {
847: KSPReset(jac->ksp[i]);
848: if (bjac && bjac->x) {
849: VecDestroy(&bjac->x[i]);
850: VecDestroy(&bjac->y[i]);
851: ISDestroy(&bjac->is[i]);
852: }
853: }
854: PetscFree(jac->l_lens);
855: PetscFree(jac->g_lens);
856: return(0);
857: }
859: static PetscErrorCode PCDestroy_BJacobi_Multiblock(PC pc)
860: {
861: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
862: PC_BJacobi_Multiblock *bjac = (PC_BJacobi_Multiblock*)jac->data;
863: PetscErrorCode ierr;
864: PetscInt i;
867: PCReset_BJacobi_Multiblock(pc);
868: if (bjac) {
869: PetscFree2(bjac->x,bjac->y);
870: PetscFree(bjac->starts);
871: PetscFree(bjac->is);
872: }
873: PetscFree(jac->data);
874: for (i=0; i<jac->n_local; i++) {
875: KSPDestroy(&jac->ksp[i]);
876: }
877: PetscFree(jac->ksp);
878: PetscFree(pc->data);
879: return(0);
880: }
882: static PetscErrorCode PCSetUpOnBlocks_BJacobi_Multiblock(PC pc)
883: {
884: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
885: PetscErrorCode ierr;
886: PetscInt i,n_local = jac->n_local;
887: KSPConvergedReason reason;
890: for (i=0; i<n_local; i++) {
891: KSPSetUp(jac->ksp[i]);
892: KSPGetConvergedReason(jac->ksp[i],&reason);
893: if (reason == KSP_DIVERGED_PCSETUP_FAILED) {
894: pc->failedreason = PC_SUBPC_ERROR;
895: }
896: }
897: return(0);
898: }
900: /*
901: Preconditioner for block Jacobi
902: */
903: static PetscErrorCode PCApply_BJacobi_Multiblock(PC pc,Vec x,Vec y)
904: {
905: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
906: PetscErrorCode ierr;
907: PetscInt i,n_local = jac->n_local;
908: PC_BJacobi_Multiblock *bjac = (PC_BJacobi_Multiblock*)jac->data;
909: PetscScalar *yin;
910: const PetscScalar *xin;
913: VecGetArrayRead(x,&xin);
914: VecGetArray(y,&yin);
915: for (i=0; i<n_local; i++) {
916: /*
917: To avoid copying the subvector from x into a workspace we instead
918: make the workspace vector array point to the subpart of the array of
919: the global vector.
920: */
921: VecPlaceArray(bjac->x[i],xin+bjac->starts[i]);
922: VecPlaceArray(bjac->y[i],yin+bjac->starts[i]);
924: PetscLogEventBegin(PC_ApplyOnBlocks,jac->ksp[i],bjac->x[i],bjac->y[i],0);
925: KSPSolve(jac->ksp[i],bjac->x[i],bjac->y[i]);
926: PetscLogEventEnd(PC_ApplyOnBlocks,jac->ksp[i],bjac->x[i],bjac->y[i],0);
928: VecResetArray(bjac->x[i]);
929: VecResetArray(bjac->y[i]);
930: }
931: VecRestoreArrayRead(x,&xin);
932: VecRestoreArray(y,&yin);
933: return(0);
934: }
936: /*
937: Preconditioner for block Jacobi
938: */
939: static PetscErrorCode PCApplyTranspose_BJacobi_Multiblock(PC pc,Vec x,Vec y)
940: {
941: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
942: PetscErrorCode ierr;
943: PetscInt i,n_local = jac->n_local;
944: PC_BJacobi_Multiblock *bjac = (PC_BJacobi_Multiblock*)jac->data;
945: PetscScalar *yin;
946: const PetscScalar *xin;
949: VecGetArrayRead(x,&xin);
950: VecGetArray(y,&yin);
951: for (i=0; i<n_local; i++) {
952: /*
953: To avoid copying the subvector from x into a workspace we instead
954: make the workspace vector array point to the subpart of the array of
955: the global vector.
956: */
957: VecPlaceArray(bjac->x[i],xin+bjac->starts[i]);
958: VecPlaceArray(bjac->y[i],yin+bjac->starts[i]);
960: PetscLogEventBegin(PC_ApplyTransposeOnBlocks,jac->ksp[i],bjac->x[i],bjac->y[i],0);
961: KSPSolveTranspose(jac->ksp[i],bjac->x[i],bjac->y[i]);
962: PetscLogEventEnd(PC_ApplyTransposeOnBlocks,jac->ksp[i],bjac->x[i],bjac->y[i],0);
964: VecResetArray(bjac->x[i]);
965: VecResetArray(bjac->y[i]);
966: }
967: VecRestoreArrayRead(x,&xin);
968: VecRestoreArray(y,&yin);
969: return(0);
970: }
972: static PetscErrorCode PCSetUp_BJacobi_Multiblock(PC pc,Mat mat,Mat pmat)
973: {
974: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
975: PetscErrorCode ierr;
976: PetscInt m,n_local,N,M,start,i;
977: const char *prefix,*pprefix,*mprefix;
978: KSP ksp;
979: Vec x,y;
980: PC_BJacobi_Multiblock *bjac = (PC_BJacobi_Multiblock*)jac->data;
981: PC subpc;
982: IS is;
983: MatReuse scall;
984: #if defined(PETSC_HAVE_VECCUDA) || defined(PETSC_HAVE_VIENNACL)
985: PetscBool is_gpumatrix = PETSC_FALSE;
986: #endif
989: MatGetLocalSize(pc->pmat,&M,&N);
991: n_local = jac->n_local;
993: if (pc->useAmat) {
994: PetscBool same;
995: PetscObjectTypeCompare((PetscObject)mat,((PetscObject)pmat)->type_name,&same);
996: if (!same) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_INCOMP,"Matrices not of same type");
997: }
999: if (!pc->setupcalled) {
1000: scall = MAT_INITIAL_MATRIX;
1002: if (!jac->ksp) {
1003: pc->ops->reset = PCReset_BJacobi_Multiblock;
1004: pc->ops->destroy = PCDestroy_BJacobi_Multiblock;
1005: pc->ops->apply = PCApply_BJacobi_Multiblock;
1006: pc->ops->applytranspose= PCApplyTranspose_BJacobi_Multiblock;
1007: pc->ops->setuponblocks = PCSetUpOnBlocks_BJacobi_Multiblock;
1009: PetscNewLog(pc,&bjac);
1010: PetscMalloc1(n_local,&jac->ksp);
1011: PetscLogObjectMemory((PetscObject)pc,sizeof(n_local*sizeof(KSP)));
1012: PetscMalloc2(n_local,&bjac->x,n_local,&bjac->y);
1013: PetscMalloc1(n_local,&bjac->starts);
1014: PetscLogObjectMemory((PetscObject)pc,sizeof(n_local*sizeof(PetscScalar)));
1016: jac->data = (void*)bjac;
1017: PetscMalloc1(n_local,&bjac->is);
1018: PetscLogObjectMemory((PetscObject)pc,sizeof(n_local*sizeof(IS)));
1020: for (i=0; i<n_local; i++) {
1021: KSPCreate(PETSC_COMM_SELF,&ksp);
1022: KSPSetErrorIfNotConverged(ksp,pc->erroriffailure);
1023: PetscObjectIncrementTabLevel((PetscObject)ksp,(PetscObject)pc,1);
1024: PetscLogObjectParent((PetscObject)pc,(PetscObject)ksp);
1025: KSPSetType(ksp,KSPPREONLY);
1026: KSPGetPC(ksp,&subpc);
1027: PCGetOptionsPrefix(pc,&prefix);
1028: KSPSetOptionsPrefix(ksp,prefix);
1029: KSPAppendOptionsPrefix(ksp,"sub_");
1031: jac->ksp[i] = ksp;
1032: }
1033: } else {
1034: bjac = (PC_BJacobi_Multiblock*)jac->data;
1035: }
1037: start = 0;
1038: for (i=0; i<n_local; i++) {
1039: m = jac->l_lens[i];
1040: /*
1041: The reason we need to generate these vectors is to serve
1042: as the right-hand side and solution vector for the solve on the
1043: block. We do not need to allocate space for the vectors since
1044: that is provided via VecPlaceArray() just before the call to
1045: KSPSolve() on the block.
1047: */
1048: VecCreateSeq(PETSC_COMM_SELF,m,&x);
1049: VecCreateSeqWithArray(PETSC_COMM_SELF,1,m,NULL,&y);
1050: #if defined(PETSC_HAVE_VECCUDA)
1051: PetscObjectTypeCompareAny((PetscObject)pmat,&is_gpumatrix,MATAIJCUSPARSE,MATSEQAIJCUSPARSE,MATMPIAIJCUSPARSE,"");
1052: if (is_gpumatrix) {
1053: VecSetType(x,VECCUDA);
1054: VecSetType(y,VECCUDA);
1055: }
1056: #endif
1057: #if defined(PETSC_HAVE_VIENNACL)
1058: PetscObjectTypeCompareAny((PetscObject)pmat,&is_gpumatrix,MATAIJVIENNACL,MATSEQAIJVIENNACL,MATMPIAIJVIENNACL,"");
1059: if (is_gpumatrix) {
1060: VecSetType(x,VECVIENNACL);
1061: VecSetType(y,VECVIENNACL);
1062: }
1063: #endif
1064: PetscLogObjectParent((PetscObject)pc,(PetscObject)x);
1065: PetscLogObjectParent((PetscObject)pc,(PetscObject)y);
1067: bjac->x[i] = x;
1068: bjac->y[i] = y;
1069: bjac->starts[i] = start;
1071: ISCreateStride(PETSC_COMM_SELF,m,start,1,&is);
1072: bjac->is[i] = is;
1073: PetscLogObjectParent((PetscObject)pc,(PetscObject)is);
1075: start += m;
1076: }
1077: } else {
1078: bjac = (PC_BJacobi_Multiblock*)jac->data;
1079: /*
1080: Destroy the blocks from the previous iteration
1081: */
1082: if (pc->flag == DIFFERENT_NONZERO_PATTERN) {
1083: MatDestroyMatrices(n_local,&bjac->pmat);
1084: if (pc->useAmat) {
1085: MatDestroyMatrices(n_local,&bjac->mat);
1086: }
1087: scall = MAT_INITIAL_MATRIX;
1088: } else scall = MAT_REUSE_MATRIX;
1089: }
1091: MatCreateSubMatrices(pmat,n_local,bjac->is,bjac->is,scall,&bjac->pmat);
1092: if (pc->useAmat) {
1093: MatCreateSubMatrices(mat,n_local,bjac->is,bjac->is,scall,&bjac->mat);
1094: }
1095: /* Return control to the user so that the submatrices can be modified (e.g., to apply
1096: different boundary conditions for the submatrices than for the global problem) */
1097: PCModifySubMatrices(pc,n_local,bjac->is,bjac->is,bjac->pmat,pc->modifysubmatricesP);
1099: PetscObjectGetOptionsPrefix((PetscObject)pmat,&pprefix);
1100: for (i=0; i<n_local; i++) {
1101: PetscLogObjectParent((PetscObject)pc,(PetscObject)bjac->pmat[i]);
1102: PetscObjectSetOptionsPrefix((PetscObject)bjac->pmat[i],pprefix);
1103: if (pc->useAmat) {
1104: PetscLogObjectParent((PetscObject)pc,(PetscObject)bjac->mat[i]);
1105: PetscObjectGetOptionsPrefix((PetscObject)mat,&mprefix);
1106: PetscObjectSetOptionsPrefix((PetscObject)bjac->mat[i],mprefix);
1107: KSPSetOperators(jac->ksp[i],bjac->mat[i],bjac->pmat[i]);
1108: } else {
1109: KSPSetOperators(jac->ksp[i],bjac->pmat[i],bjac->pmat[i]);
1110: }
1111: if (pc->setfromoptionscalled) {
1112: KSPSetFromOptions(jac->ksp[i]);
1113: }
1114: }
1115: return(0);
1116: }
1118: /* ---------------------------------------------------------------------------------------------*/
1119: /*
1120: These are for a single block with multiple processes;
1121: */
1122: static PetscErrorCode PCReset_BJacobi_Multiproc(PC pc)
1123: {
1124: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
1125: PC_BJacobi_Multiproc *mpjac = (PC_BJacobi_Multiproc*)jac->data;
1126: PetscErrorCode ierr;
1129: VecDestroy(&mpjac->ysub);
1130: VecDestroy(&mpjac->xsub);
1131: MatDestroy(&mpjac->submats);
1132: if (jac->ksp) {KSPReset(jac->ksp[0]);}
1133: return(0);
1134: }
1136: static PetscErrorCode PCDestroy_BJacobi_Multiproc(PC pc)
1137: {
1138: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
1139: PC_BJacobi_Multiproc *mpjac = (PC_BJacobi_Multiproc*)jac->data;
1140: PetscErrorCode ierr;
1143: PCReset_BJacobi_Multiproc(pc);
1144: KSPDestroy(&jac->ksp[0]);
1145: PetscFree(jac->ksp);
1146: PetscSubcommDestroy(&mpjac->psubcomm);
1148: PetscFree(mpjac);
1149: PetscFree(pc->data);
1150: return(0);
1151: }
1153: static PetscErrorCode PCApply_BJacobi_Multiproc(PC pc,Vec x,Vec y)
1154: {
1155: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
1156: PC_BJacobi_Multiproc *mpjac = (PC_BJacobi_Multiproc*)jac->data;
1157: PetscErrorCode ierr;
1158: PetscScalar *yarray;
1159: const PetscScalar *xarray;
1160: KSPConvergedReason reason;
1163: /* place x's and y's local arrays into xsub and ysub */
1164: VecGetArrayRead(x,&xarray);
1165: VecGetArray(y,&yarray);
1166: VecPlaceArray(mpjac->xsub,xarray);
1167: VecPlaceArray(mpjac->ysub,yarray);
1169: /* apply preconditioner on each matrix block */
1170: PetscLogEventBegin(PC_ApplyOnBlocks,jac->ksp[0],mpjac->xsub,mpjac->ysub,0);
1171: KSPSolve(jac->ksp[0],mpjac->xsub,mpjac->ysub);
1172: PetscLogEventEnd(PC_ApplyOnBlocks,jac->ksp[0],mpjac->xsub,mpjac->ysub,0);
1173: KSPGetConvergedReason(jac->ksp[0],&reason);
1174: if (reason == KSP_DIVERGED_PCSETUP_FAILED) {
1175: pc->failedreason = PC_SUBPC_ERROR;
1176: }
1178: VecResetArray(mpjac->xsub);
1179: VecResetArray(mpjac->ysub);
1180: VecRestoreArrayRead(x,&xarray);
1181: VecRestoreArray(y,&yarray);
1182: return(0);
1183: }
1185: static PetscErrorCode PCSetUp_BJacobi_Multiproc(PC pc)
1186: {
1187: PC_BJacobi *jac = (PC_BJacobi*)pc->data;
1188: PC_BJacobi_Multiproc *mpjac = (PC_BJacobi_Multiproc*)jac->data;
1189: PetscErrorCode ierr;
1190: PetscInt m,n;
1191: MPI_Comm comm,subcomm=0;
1192: const char *prefix;
1193: PetscBool wasSetup = PETSC_TRUE;
1194: #if defined(PETSC_HAVE_VECCUDA) || defined(PETSC_HAVE_VIENNACL)
1195: PetscBool is_gpumatrix = PETSC_FALSE;
1196: #endif
1200: PetscObjectGetComm((PetscObject)pc,&comm);
1201: if (jac->n_local > 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Only a single block in a subcommunicator is supported");
1202: jac->n_local = 1; /* currently only a single block is supported for a subcommunicator */
1203: if (!pc->setupcalled) {
1204: wasSetup = PETSC_FALSE;
1205: PetscNewLog(pc,&mpjac);
1206: jac->data = (void*)mpjac;
1208: /* initialize datastructure mpjac */
1209: if (!jac->psubcomm) {
1210: /* Create default contiguous subcommunicatiors if user does not provide them */
1211: PetscSubcommCreate(comm,&jac->psubcomm);
1212: PetscSubcommSetNumber(jac->psubcomm,jac->n);
1213: PetscSubcommSetType(jac->psubcomm,PETSC_SUBCOMM_CONTIGUOUS);
1214: PetscLogObjectMemory((PetscObject)pc,sizeof(PetscSubcomm));
1215: }
1216: mpjac->psubcomm = jac->psubcomm;
1217: subcomm = PetscSubcommChild(mpjac->psubcomm);
1219: /* Get matrix blocks of pmat */
1220: MatGetMultiProcBlock(pc->pmat,subcomm,MAT_INITIAL_MATRIX,&mpjac->submats);
1222: /* create a new PC that processors in each subcomm have copy of */
1223: PetscMalloc1(1,&jac->ksp);
1224: KSPCreate(subcomm,&jac->ksp[0]);
1225: KSPSetErrorIfNotConverged(jac->ksp[0],pc->erroriffailure);
1226: PetscObjectIncrementTabLevel((PetscObject)jac->ksp[0],(PetscObject)pc,1);
1227: PetscLogObjectParent((PetscObject)pc,(PetscObject)jac->ksp[0]);
1228: KSPSetOperators(jac->ksp[0],mpjac->submats,mpjac->submats);
1229: KSPGetPC(jac->ksp[0],&mpjac->pc);
1231: PCGetOptionsPrefix(pc,&prefix);
1232: KSPSetOptionsPrefix(jac->ksp[0],prefix);
1233: KSPAppendOptionsPrefix(jac->ksp[0],"sub_");
1234: /*
1235: PetscMPIInt rank,subsize,subrank;
1236: MPI_Comm_rank(comm,&rank);
1237: MPI_Comm_size(subcomm,&subsize);
1238: MPI_Comm_rank(subcomm,&subrank);
1240: MatGetLocalSize(mpjac->submats,&m,NULL);
1241: MatGetSize(mpjac->submats,&n,NULL);
1242: PetscSynchronizedPrintf(comm,"[%d], sub-size %d,sub-rank %d\n",rank,subsize,subrank);
1243: PetscSynchronizedFlush(comm,PETSC_STDOUT);
1244: */
1246: /* create dummy vectors xsub and ysub */
1247: MatGetLocalSize(mpjac->submats,&m,&n);
1248: VecCreateMPIWithArray(subcomm,1,n,PETSC_DECIDE,NULL,&mpjac->xsub);
1249: VecCreateMPIWithArray(subcomm,1,m,PETSC_DECIDE,NULL,&mpjac->ysub);
1250: #if defined(PETSC_HAVE_VECCUDA)
1251: PetscObjectTypeCompareAny((PetscObject)mpjac->submats,&is_gpumatrix,MATAIJCUSPARSE,MATMPIAIJCUSPARSE,"");
1252: if (is_gpumatrix) {
1253: VecSetType(mpjac->xsub,VECMPICUDA);
1254: VecSetType(mpjac->ysub,VECMPICUDA);
1255: }
1256: #endif
1257: #if defined(PETSC_HAVE_VIENNACL)
1258: PetscObjectTypeCompareAny((PetscObject)mpjac->submats,&is_gpumatrix,MATAIJVIENNACL,MATMPIAIJVIENNACL,"");
1259: if (is_gpumatrix) {
1260: VecSetType(mpjac->xsub,VECMPIVIENNACL);
1261: VecSetType(mpjac->ysub,VECMPIVIENNACL);
1262: }
1263: #endif
1264: PetscLogObjectParent((PetscObject)pc,(PetscObject)mpjac->xsub);
1265: PetscLogObjectParent((PetscObject)pc,(PetscObject)mpjac->ysub);
1267: pc->ops->reset = PCReset_BJacobi_Multiproc;
1268: pc->ops->destroy = PCDestroy_BJacobi_Multiproc;
1269: pc->ops->apply = PCApply_BJacobi_Multiproc;
1270: } else { /* pc->setupcalled */
1271: subcomm = PetscSubcommChild(mpjac->psubcomm);
1272: if (pc->flag == DIFFERENT_NONZERO_PATTERN) {
1273: /* destroy old matrix blocks, then get new matrix blocks */
1274: if (mpjac->submats) {MatDestroy(&mpjac->submats);}
1275: MatGetMultiProcBlock(pc->pmat,subcomm,MAT_INITIAL_MATRIX,&mpjac->submats);
1276: } else {
1277: MatGetMultiProcBlock(pc->pmat,subcomm,MAT_REUSE_MATRIX,&mpjac->submats);
1278: }
1279: KSPSetOperators(jac->ksp[0],mpjac->submats,mpjac->submats);
1280: }
1282: if (!wasSetup && pc->setfromoptionscalled) {
1283: KSPSetFromOptions(jac->ksp[0]);
1284: }
1285: return(0);
1286: }