Actual source code: telescope.c
1: #include <petsc/private/petscimpl.h>
2: #include <petsc/private/matimpl.h>
3: #include <petsc/private/pcimpl.h>
4: #include <petscksp.h>
5: #include <petscdm.h>
6: #include "../src/ksp/pc/impls/telescope/telescope.h"
8: static PetscBool cited = PETSC_FALSE;
9: static const char citation[] =
10: "@inproceedings{MaySananRuppKnepleySmith2016,\n"
11: " title = {Extreme-Scale Multigrid Components within PETSc},\n"
12: " author = {Dave A. May and Patrick Sanan and Karl Rupp and Matthew G. Knepley and Barry F. Smith},\n"
13: " booktitle = {Proceedings of the Platform for Advanced Scientific Computing Conference},\n"
14: " series = {PASC '16},\n"
15: " isbn = {978-1-4503-4126-4},\n"
16: " location = {Lausanne, Switzerland},\n"
17: " pages = {5:1--5:12},\n"
18: " articleno = {5},\n"
19: " numpages = {12},\n"
20: " url = {https://doi.acm.org/10.1145/2929908.2929913},\n"
21: " doi = {10.1145/2929908.2929913},\n"
22: " acmid = {2929913},\n"
23: " publisher = {ACM},\n"
24: " address = {New York, NY, USA},\n"
25: " keywords = {GPU, HPC, agglomeration, coarse-level solver, multigrid, parallel computing, preconditioning},\n"
26: " year = {2016}\n"
27: "}\n";
29: /*
30: default setup mode
32: [1a] scatter to (FORWARD)
33: x(comm) -> xtmp(comm)
34: [1b] local copy (to) ranks with color = 0
35: xred(subcomm) <- xtmp
37: [2] solve on sub KSP to obtain yred(subcomm)
39: [3a] local copy (from) ranks with color = 0
40: yred(subcomm) --> xtmp
41: [2b] scatter from (REVERSE)
42: xtmp(comm) -> y(comm)
43: */
45: /*
46: Collective[comm_f]
47: Notes
48: * Using comm_f = MPI_COMM_NULL will result in an error
49: * Using comm_c = MPI_COMM_NULL is valid. If all instances of comm_c are NULL the subcomm is not valid.
50: * If any non NULL comm_c communicator cannot map any of its ranks to comm_f, the subcomm is not valid.
51: */
52: PetscErrorCode PCTelescopeTestValidSubcomm(MPI_Comm comm_f,MPI_Comm comm_c,PetscBool *isvalid)
53: {
54: PetscInt valid = 1;
55: MPI_Group group_f,group_c;
57: PetscMPIInt count,k,size_f = 0,size_c = 0,size_c_sum = 0;
58: PetscMPIInt *ranks_f,*ranks_c;
61: if (comm_f == MPI_COMM_NULL) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"comm_f cannot be MPI_COMM_NULL");
63: MPI_Comm_group(comm_f,&group_f);
64: if (comm_c != MPI_COMM_NULL) {
65: MPI_Comm_group(comm_c,&group_c);
66: }
68: MPI_Comm_size(comm_f,&size_f);
69: if (comm_c != MPI_COMM_NULL) {
70: MPI_Comm_size(comm_c,&size_c);
71: }
73: /* check not all comm_c's are NULL */
74: size_c_sum = size_c;
75: MPI_Allreduce(MPI_IN_PLACE,&size_c_sum,1,MPI_INT,MPI_SUM,comm_f);
76: if (size_c_sum == 0) valid = 0;
78: /* check we can map at least 1 rank in comm_c to comm_f */
79: PetscMalloc1(size_f,&ranks_f);
80: PetscMalloc1(size_c,&ranks_c);
81: for (k=0; k<size_f; k++) ranks_f[k] = MPI_UNDEFINED;
82: for (k=0; k<size_c; k++) ranks_c[k] = k;
84: /*
85: MPI_Group_translate_ranks() returns a non-zero exit code if any rank cannot be translated.
86: I do not want the code to terminate immediately if this occurs, rather I want to throw
87: the error later (during PCSetUp_Telescope()) via SETERRQ() with a message indicating
88: that comm_c is not a valid sub-communicator.
89: Hence I purposefully do not call CHKERRQ() after MPI_Group_translate_ranks().
90: */
91: count = 0;
92: if (comm_c != MPI_COMM_NULL) {
93: (void)MPI_Group_translate_ranks(group_c,size_c,ranks_c,group_f,ranks_f);
94: for (k=0; k<size_f; k++) {
95: if (ranks_f[k] == MPI_UNDEFINED) {
96: count++;
97: }
98: }
99: }
100: if (count == size_f) valid = 0;
102: MPI_Allreduce(MPI_IN_PLACE,&valid,1,MPIU_INT,MPI_MIN,comm_f);
103: if (valid == 1) *isvalid = PETSC_TRUE;
104: else *isvalid = PETSC_FALSE;
106: PetscFree(ranks_f);
107: PetscFree(ranks_c);
108: MPI_Group_free(&group_f);
109: if (comm_c != MPI_COMM_NULL) {
110: MPI_Group_free(&group_c);
111: }
112: return(0);
113: }
115: DM private_PCTelescopeGetSubDM(PC_Telescope sred)
116: {
117: DM subdm = NULL;
119: if (!PCTelescope_isActiveRank(sred)) { subdm = NULL; }
120: else {
121: switch (sred->sr_type) {
122: case TELESCOPE_DEFAULT: subdm = NULL;
123: break;
124: case TELESCOPE_DMDA: subdm = ((PC_Telescope_DMDACtx*)sred->dm_ctx)->dmrepart;
125: break;
126: case TELESCOPE_DMPLEX: subdm = NULL;
127: break;
128: case TELESCOPE_COARSEDM: if (sred->ksp) { KSPGetDM(sred->ksp,&subdm); }
129: break;
130: }
131: }
132: return(subdm);
133: }
135: PetscErrorCode PCTelescopeSetUp_default(PC pc,PC_Telescope sred)
136: {
138: PetscInt m,M,bs,st,ed;
139: Vec x,xred,yred,xtmp;
140: Mat B;
141: MPI_Comm comm,subcomm;
142: VecScatter scatter;
143: IS isin;
144: VecType vectype;
147: PetscInfo(pc,"PCTelescope: setup (default)\n");
148: comm = PetscSubcommParent(sred->psubcomm);
149: subcomm = PetscSubcommChild(sred->psubcomm);
151: PCGetOperators(pc,NULL,&B);
152: MatGetSize(B,&M,NULL);
153: MatGetBlockSize(B,&bs);
154: MatCreateVecs(B,&x,NULL);
155: MatGetVecType(B,&vectype);
157: xred = NULL;
158: m = 0;
159: if (PCTelescope_isActiveRank(sred)) {
160: VecCreate(subcomm,&xred);
161: VecSetSizes(xred,PETSC_DECIDE,M);
162: VecSetBlockSize(xred,bs);
163: VecSetType(xred,vectype); /* Use the preconditioner matrix's vectype by default */
164: VecSetFromOptions(xred);
165: VecGetLocalSize(xred,&m);
166: }
168: yred = NULL;
169: if (PCTelescope_isActiveRank(sred)) {
170: VecDuplicate(xred,&yred);
171: }
173: VecCreate(comm,&xtmp);
174: VecSetSizes(xtmp,m,PETSC_DECIDE);
175: VecSetBlockSize(xtmp,bs);
176: VecSetType(xtmp,vectype);
178: if (PCTelescope_isActiveRank(sred)) {
179: VecGetOwnershipRange(xred,&st,&ed);
180: ISCreateStride(comm,(ed-st),st,1,&isin);
181: } else {
182: VecGetOwnershipRange(x,&st,&ed);
183: ISCreateStride(comm,0,st,1,&isin);
184: }
185: ISSetBlockSize(isin,bs);
187: VecScatterCreate(x,isin,xtmp,NULL,&scatter);
189: sred->isin = isin;
190: sred->scatter = scatter;
191: sred->xred = xred;
192: sred->yred = yred;
193: sred->xtmp = xtmp;
194: VecDestroy(&x);
195: return(0);
196: }
198: PetscErrorCode PCTelescopeMatCreate_default(PC pc,PC_Telescope sred,MatReuse reuse,Mat *A)
199: {
201: MPI_Comm comm,subcomm;
202: Mat Bred,B;
203: PetscInt nr,nc,bs;
204: IS isrow,iscol;
205: Mat Blocal,*_Blocal;
208: PetscInfo(pc,"PCTelescope: updating the redundant preconditioned operator (default)\n");
209: PetscObjectGetComm((PetscObject)pc,&comm);
210: subcomm = PetscSubcommChild(sred->psubcomm);
211: PCGetOperators(pc,NULL,&B);
212: MatGetSize(B,&nr,&nc);
213: isrow = sred->isin;
214: ISCreateStride(PETSC_COMM_SELF,nc,0,1,&iscol);
215: ISSetIdentity(iscol);
216: MatGetBlockSizes(B,NULL,&bs);
217: ISSetBlockSize(iscol,bs);
218: MatSetOption(B,MAT_SUBMAT_SINGLEIS,PETSC_TRUE);
219: MatCreateSubMatrices(B,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&_Blocal);
220: Blocal = *_Blocal;
221: PetscFree(_Blocal);
222: Bred = NULL;
223: if (PCTelescope_isActiveRank(sred)) {
224: PetscInt mm;
226: if (reuse != MAT_INITIAL_MATRIX) { Bred = *A; }
228: MatGetSize(Blocal,&mm,NULL);
229: MatCreateMPIMatConcatenateSeqMat(subcomm,Blocal,mm,reuse,&Bred);
230: }
231: *A = Bred;
232: ISDestroy(&iscol);
233: MatDestroy(&Blocal);
234: return(0);
235: }
237: static PetscErrorCode PCTelescopeSubNullSpaceCreate_Telescope(PC pc,PC_Telescope sred,MatNullSpace nullspace,MatNullSpace *sub_nullspace)
238: {
240: PetscBool has_const;
241: const Vec *vecs;
242: Vec *sub_vecs = NULL;
243: PetscInt i,k,n = 0;
244: MPI_Comm subcomm;
247: subcomm = PetscSubcommChild(sred->psubcomm);
248: MatNullSpaceGetVecs(nullspace,&has_const,&n,&vecs);
250: if (PCTelescope_isActiveRank(sred)) {
251: if (n) {
252: VecDuplicateVecs(sred->xred,n,&sub_vecs);
253: }
254: }
256: /* copy entries */
257: for (k=0; k<n; k++) {
258: const PetscScalar *x_array;
259: PetscScalar *LA_sub_vec;
260: PetscInt st,ed;
262: /* pull in vector x->xtmp */
263: VecScatterBegin(sred->scatter,vecs[k],sred->xtmp,INSERT_VALUES,SCATTER_FORWARD);
264: VecScatterEnd(sred->scatter,vecs[k],sred->xtmp,INSERT_VALUES,SCATTER_FORWARD);
265: if (sub_vecs) {
266: /* copy vector entries into xred */
267: VecGetArrayRead(sred->xtmp,&x_array);
268: if (sub_vecs[k]) {
269: VecGetOwnershipRange(sub_vecs[k],&st,&ed);
270: VecGetArray(sub_vecs[k],&LA_sub_vec);
271: for (i=0; i<ed-st; i++) {
272: LA_sub_vec[i] = x_array[i];
273: }
274: VecRestoreArray(sub_vecs[k],&LA_sub_vec);
275: }
276: VecRestoreArrayRead(sred->xtmp,&x_array);
277: }
278: }
280: if (PCTelescope_isActiveRank(sred)) {
281: /* create new (near) nullspace for redundant object */
282: MatNullSpaceCreate(subcomm,has_const,n,sub_vecs,sub_nullspace);
283: VecDestroyVecs(n,&sub_vecs);
284: if (nullspace->remove) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Propagation of custom remove callbacks not supported when propagating (near) nullspaces with PCTelescope");
285: if (nullspace->rmctx) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Propagation of custom remove callback context not supported when propagating (near) nullspaces with PCTelescope");
286: }
287: return(0);
288: }
290: static PetscErrorCode PCTelescopeMatNullSpaceCreate_default(PC pc,PC_Telescope sred,Mat sub_mat)
291: {
293: Mat B;
296: PCGetOperators(pc,NULL,&B);
297: /* Propagate the nullspace if it exists */
298: {
299: MatNullSpace nullspace,sub_nullspace;
300: MatGetNullSpace(B,&nullspace);
301: if (nullspace) {
302: PetscInfo(pc,"PCTelescope: generating nullspace (default)\n");
303: PCTelescopeSubNullSpaceCreate_Telescope(pc,sred,nullspace,&sub_nullspace);
304: if (PCTelescope_isActiveRank(sred)) {
305: MatSetNullSpace(sub_mat,sub_nullspace);
306: MatNullSpaceDestroy(&sub_nullspace);
307: }
308: }
309: }
310: /* Propagate the near nullspace if it exists */
311: {
312: MatNullSpace nearnullspace,sub_nearnullspace;
313: MatGetNearNullSpace(B,&nearnullspace);
314: if (nearnullspace) {
315: PetscInfo(pc,"PCTelescope: generating near nullspace (default)\n");
316: PCTelescopeSubNullSpaceCreate_Telescope(pc,sred,nearnullspace,&sub_nearnullspace);
317: if (PCTelescope_isActiveRank(sred)) {
318: MatSetNearNullSpace(sub_mat,sub_nearnullspace);
319: MatNullSpaceDestroy(&sub_nearnullspace);
320: }
321: }
322: }
323: return(0);
324: }
326: static PetscErrorCode PCView_Telescope(PC pc,PetscViewer viewer)
327: {
328: PC_Telescope sred = (PC_Telescope)pc->data;
330: PetscBool iascii,isstring;
331: PetscViewer subviewer;
334: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
335: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);
336: if (iascii) {
337: {
338: MPI_Comm comm,subcomm;
339: PetscMPIInt comm_size,subcomm_size;
340: DM dm = NULL,subdm = NULL;
342: PCGetDM(pc,&dm);
343: subdm = private_PCTelescopeGetSubDM(sred);
345: if (sred->psubcomm) {
346: comm = PetscSubcommParent(sred->psubcomm);
347: subcomm = PetscSubcommChild(sred->psubcomm);
348: MPI_Comm_size(comm,&comm_size);
349: MPI_Comm_size(subcomm,&subcomm_size);
351: PetscViewerASCIIPushTab(viewer);
352: PetscViewerASCIIPrintf(viewer,"petsc subcomm: parent comm size reduction factor = %D\n",sred->redfactor);
353: PetscViewerASCIIPrintf(viewer,"petsc subcomm: parent_size = %d , subcomm_size = %d\n",(int)comm_size,(int)subcomm_size);
354: switch (sred->subcommtype) {
355: case PETSC_SUBCOMM_INTERLACED :
356: PetscViewerASCIIPrintf(viewer,"petsc subcomm: type = interlaced\n",sred->subcommtype);
357: break;
358: case PETSC_SUBCOMM_CONTIGUOUS :
359: PetscViewerASCIIPrintf(viewer,"petsc subcomm type = contiguous\n",sred->subcommtype);
360: break;
361: default :
362: SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"General subcomm type not supported by PCTelescope");
363: }
364: PetscViewerASCIIPopTab(viewer);
365: } else {
366: PetscObjectGetComm((PetscObject)pc,&comm);
367: subcomm = sred->subcomm;
368: if (!PCTelescope_isActiveRank(sred)) {
369: subcomm = PETSC_COMM_SELF;
370: }
372: PetscViewerASCIIPushTab(viewer);
373: PetscViewerASCIIPrintf(viewer,"subcomm: using user provided sub-communicator\n");
374: PetscViewerASCIIPopTab(viewer);
375: }
377: PetscViewerGetSubViewer(viewer,subcomm,&subviewer);
378: if (PCTelescope_isActiveRank(sred)) {
379: PetscViewerASCIIPushTab(subviewer);
381: if (dm && sred->ignore_dm) {
382: PetscViewerASCIIPrintf(subviewer,"ignoring DM\n");
383: }
384: if (sred->ignore_kspcomputeoperators) {
385: PetscViewerASCIIPrintf(subviewer,"ignoring KSPComputeOperators\n");
386: }
387: switch (sred->sr_type) {
388: case TELESCOPE_DEFAULT:
389: PetscViewerASCIIPrintf(subviewer,"setup type: default\n");
390: break;
391: case TELESCOPE_DMDA:
392: PetscViewerASCIIPrintf(subviewer,"setup type: DMDA auto-repartitioning\n");
393: DMView_DA_Short(subdm,subviewer);
394: break;
395: case TELESCOPE_DMPLEX:
396: PetscViewerASCIIPrintf(subviewer,"setup type: DMPLEX auto-repartitioning\n");
397: break;
398: case TELESCOPE_COARSEDM:
399: PetscViewerASCIIPrintf(subviewer,"setup type: coarse DM\n");
400: break;
401: }
403: if (dm) {
404: PetscObject obj = (PetscObject)dm;
405: PetscViewerASCIIPrintf(subviewer,"Parent DM object:");
406: PetscViewerASCIIUseTabs(subviewer,PETSC_FALSE);
407: if (obj->type_name) { PetscViewerASCIIPrintf(subviewer," type = %s;",obj->type_name); }
408: if (obj->name) { PetscViewerASCIIPrintf(subviewer," name = %s;",obj->name); }
409: if (obj->prefix) { PetscViewerASCIIPrintf(subviewer," prefix = %s",obj->prefix); }
410: PetscViewerASCIIPrintf(subviewer,"\n");
411: PetscViewerASCIIUseTabs(subviewer,PETSC_TRUE);
412: } else {
413: PetscViewerASCIIPrintf(subviewer,"Parent DM object: NULL\n");
414: }
415: if (subdm) {
416: PetscObject obj = (PetscObject)subdm;
417: PetscViewerASCIIPrintf(subviewer,"Sub DM object:");
418: PetscViewerASCIIUseTabs(subviewer,PETSC_FALSE);
419: if (obj->type_name) { PetscViewerASCIIPrintf(subviewer," type = %s;",obj->type_name); }
420: if (obj->name) { PetscViewerASCIIPrintf(subviewer," name = %s;",obj->name); }
421: if (obj->prefix) { PetscViewerASCIIPrintf(subviewer," prefix = %s",obj->prefix); }
422: PetscViewerASCIIPrintf(subviewer,"\n");
423: PetscViewerASCIIUseTabs(subviewer,PETSC_TRUE);
424: } else {
425: PetscViewerASCIIPrintf(subviewer,"Sub DM object: NULL\n");
426: }
428: KSPView(sred->ksp,subviewer);
429: PetscViewerASCIIPopTab(subviewer);
430: }
431: PetscViewerRestoreSubViewer(viewer,subcomm,&subviewer);
432: }
433: }
434: return(0);
435: }
437: static PetscErrorCode PCSetUp_Telescope(PC pc)
438: {
439: PC_Telescope sred = (PC_Telescope)pc->data;
440: PetscErrorCode ierr;
441: MPI_Comm comm,subcomm=0;
442: PCTelescopeType sr_type;
445: PetscObjectGetComm((PetscObject)pc,&comm);
447: /* Determine type of setup/update */
448: if (!pc->setupcalled) {
449: PetscBool has_dm,same;
450: DM dm;
452: sr_type = TELESCOPE_DEFAULT;
453: has_dm = PETSC_FALSE;
454: PCGetDM(pc,&dm);
455: if (dm) { has_dm = PETSC_TRUE; }
456: if (has_dm) {
457: /* check for dmda */
458: PetscObjectTypeCompare((PetscObject)dm,DMDA,&same);
459: if (same) {
460: PetscInfo(pc,"PCTelescope: found DMDA\n");
461: sr_type = TELESCOPE_DMDA;
462: }
463: /* check for dmplex */
464: PetscObjectTypeCompare((PetscObject)dm,DMPLEX,&same);
465: if (same) {
466: PetscInfo(pc,"PCTelescope: found DMPLEX\n");
467: sr_type = TELESCOPE_DMPLEX;
468: }
470: if (sred->use_coarse_dm) {
471: PetscInfo(pc,"PCTelescope: using coarse DM\n");
472: sr_type = TELESCOPE_COARSEDM;
473: }
475: if (sred->ignore_dm) {
476: PetscInfo(pc,"PCTelescope: ignoring DM\n");
477: sr_type = TELESCOPE_DEFAULT;
478: }
479: }
480: sred->sr_type = sr_type;
481: } else {
482: sr_type = sred->sr_type;
483: }
485: /* set function pointers for repartition setup, matrix creation/update, matrix (near) nullspace, and reset functionality */
486: switch (sr_type) {
487: case TELESCOPE_DEFAULT:
488: sred->pctelescope_setup_type = PCTelescopeSetUp_default;
489: sred->pctelescope_matcreate_type = PCTelescopeMatCreate_default;
490: sred->pctelescope_matnullspacecreate_type = PCTelescopeMatNullSpaceCreate_default;
491: sred->pctelescope_reset_type = NULL;
492: break;
493: case TELESCOPE_DMDA:
494: pc->ops->apply = PCApply_Telescope_dmda;
495: pc->ops->applyrichardson = PCApplyRichardson_Telescope_dmda;
496: sred->pctelescope_setup_type = PCTelescopeSetUp_dmda;
497: sred->pctelescope_matcreate_type = PCTelescopeMatCreate_dmda;
498: sred->pctelescope_matnullspacecreate_type = PCTelescopeMatNullSpaceCreate_dmda;
499: sred->pctelescope_reset_type = PCReset_Telescope_dmda;
500: break;
501: case TELESCOPE_DMPLEX:
502: SETERRQ(comm,PETSC_ERR_SUP,"Support for DMPLEX is currently not available");
503: case TELESCOPE_COARSEDM:
504: pc->ops->apply = PCApply_Telescope_CoarseDM;
505: pc->ops->applyrichardson = PCApplyRichardson_Telescope_CoarseDM;
506: sred->pctelescope_setup_type = PCTelescopeSetUp_CoarseDM;
507: sred->pctelescope_matcreate_type = NULL;
508: sred->pctelescope_matnullspacecreate_type = NULL; /* PCTelescopeMatNullSpaceCreate_CoarseDM; */
509: sred->pctelescope_reset_type = PCReset_Telescope_CoarseDM;
510: break;
511: default:
512: SETERRQ(comm,PETSC_ERR_SUP,"Support only provided for: repartitioning an operator; repartitioning a DMDA; or using a coarse DM");
513: }
515: /* subcomm definition */
516: if (!pc->setupcalled) {
517: if ((sr_type == TELESCOPE_DEFAULT) || (sr_type == TELESCOPE_DMDA)) {
518: if (!sred->psubcomm) {
519: PetscSubcommCreate(comm,&sred->psubcomm);
520: PetscSubcommSetNumber(sred->psubcomm,sred->redfactor);
521: PetscSubcommSetType(sred->psubcomm,sred->subcommtype);
522: PetscLogObjectMemory((PetscObject)pc,sizeof(PetscSubcomm));
523: sred->subcomm = PetscSubcommChild(sred->psubcomm);
524: }
525: } else { /* query PC for DM, check communicators */
526: DM dm,dm_coarse_partition = NULL;
527: MPI_Comm comm_fine,comm_coarse_partition = MPI_COMM_NULL;
528: PetscMPIInt csize_fine=0,csize_coarse_partition=0,cs[2],csg[2],cnt=0;
529: PetscBool isvalidsubcomm;
531: PCGetDM(pc,&dm);
532: comm_fine = PetscObjectComm((PetscObject)dm);
533: DMGetCoarseDM(dm,&dm_coarse_partition);
534: if (dm_coarse_partition) { cnt = 1; }
535: MPI_Allreduce(MPI_IN_PLACE,&cnt,1,MPI_INT,MPI_SUM,comm_fine);
536: if (cnt == 0) SETERRQ(comm_fine,PETSC_ERR_SUP,"Zero instances of a coarse DM were found");
538: MPI_Comm_size(comm_fine,&csize_fine);
539: if (dm_coarse_partition) {
540: comm_coarse_partition = PetscObjectComm((PetscObject)dm_coarse_partition);
541: MPI_Comm_size(comm_coarse_partition,&csize_coarse_partition);
542: }
544: cs[0] = csize_fine;
545: cs[1] = csize_coarse_partition;
546: MPI_Allreduce(cs,csg,2,MPI_INT,MPI_MAX,comm_fine);
547: if (csg[0] == csg[1]) SETERRQ(comm_fine,PETSC_ERR_SUP,"Coarse DM uses the same size communicator as the parent DM attached to the PC");
549: PCTelescopeTestValidSubcomm(comm_fine,comm_coarse_partition,&isvalidsubcomm);
550: if (!isvalidsubcomm) SETERRQ(comm_fine,PETSC_ERR_SUP,"Coarse DM communicator is not a sub-communicator of parentDM->comm");
551: sred->subcomm = comm_coarse_partition;
552: }
553: }
554: subcomm = sred->subcomm;
556: /* internal KSP */
557: if (!pc->setupcalled) {
558: const char *prefix;
560: if (PCTelescope_isActiveRank(sred)) {
561: KSPCreate(subcomm,&sred->ksp);
562: KSPSetErrorIfNotConverged(sred->ksp,pc->erroriffailure);
563: PetscObjectIncrementTabLevel((PetscObject)sred->ksp,(PetscObject)pc,1);
564: PetscLogObjectParent((PetscObject)pc,(PetscObject)sred->ksp);
565: KSPSetType(sred->ksp,KSPPREONLY);
566: PCGetOptionsPrefix(pc,&prefix);
567: KSPSetOptionsPrefix(sred->ksp,prefix);
568: KSPAppendOptionsPrefix(sred->ksp,"telescope_");
569: }
570: }
572: /* setup */
573: if (!pc->setupcalled && sred->pctelescope_setup_type) {
574: sred->pctelescope_setup_type(pc,sred);
575: }
576: /* update */
577: if (!pc->setupcalled) {
578: if (sred->pctelescope_matcreate_type) {
579: sred->pctelescope_matcreate_type(pc,sred,MAT_INITIAL_MATRIX,&sred->Bred);
580: }
581: if (sred->pctelescope_matnullspacecreate_type) {
582: sred->pctelescope_matnullspacecreate_type(pc,sred,sred->Bred);
583: }
584: } else {
585: if (sred->pctelescope_matcreate_type) {
586: sred->pctelescope_matcreate_type(pc,sred,MAT_REUSE_MATRIX,&sred->Bred);
587: }
588: }
590: /* common - no construction */
591: if (PCTelescope_isActiveRank(sred)) {
592: KSPSetOperators(sred->ksp,sred->Bred,sred->Bred);
593: if (pc->setfromoptionscalled && !pc->setupcalled) {
594: KSPSetFromOptions(sred->ksp);
595: }
596: }
597: return(0);
598: }
600: static PetscErrorCode PCApply_Telescope(PC pc,Vec x,Vec y)
601: {
602: PC_Telescope sred = (PC_Telescope)pc->data;
603: PetscErrorCode ierr;
604: Vec xtmp,xred,yred;
605: PetscInt i,st,ed;
606: VecScatter scatter;
607: PetscScalar *array;
608: const PetscScalar *x_array;
611: PetscCitationsRegister(citation,&cited);
613: xtmp = sred->xtmp;
614: scatter = sred->scatter;
615: xred = sred->xred;
616: yred = sred->yred;
618: /* pull in vector x->xtmp */
619: VecScatterBegin(scatter,x,xtmp,INSERT_VALUES,SCATTER_FORWARD);
620: VecScatterEnd(scatter,x,xtmp,INSERT_VALUES,SCATTER_FORWARD);
622: /* copy vector entries into xred */
623: VecGetArrayRead(xtmp,&x_array);
624: if (xred) {
625: PetscScalar *LA_xred;
626: VecGetOwnershipRange(xred,&st,&ed);
627: VecGetArray(xred,&LA_xred);
628: for (i=0; i<ed-st; i++) {
629: LA_xred[i] = x_array[i];
630: }
631: VecRestoreArray(xred,&LA_xred);
632: }
633: VecRestoreArrayRead(xtmp,&x_array);
634: /* solve */
635: if (PCTelescope_isActiveRank(sred)) {
636: KSPSolve(sred->ksp,xred,yred);
637: KSPCheckSolve(sred->ksp,pc,yred);
638: }
639: /* return vector */
640: VecGetArray(xtmp,&array);
641: if (yred) {
642: const PetscScalar *LA_yred;
643: VecGetOwnershipRange(yred,&st,&ed);
644: VecGetArrayRead(yred,&LA_yred);
645: for (i=0; i<ed-st; i++) {
646: array[i] = LA_yred[i];
647: }
648: VecRestoreArrayRead(yred,&LA_yred);
649: }
650: VecRestoreArray(xtmp,&array);
651: VecScatterBegin(scatter,xtmp,y,INSERT_VALUES,SCATTER_REVERSE);
652: VecScatterEnd(scatter,xtmp,y,INSERT_VALUES,SCATTER_REVERSE);
653: return(0);
654: }
656: static PetscErrorCode PCApplyRichardson_Telescope(PC pc,Vec x,Vec y,Vec w,PetscReal rtol,PetscReal abstol, PetscReal dtol,PetscInt its,PetscBool zeroguess,PetscInt *outits,PCRichardsonConvergedReason *reason)
657: {
658: PC_Telescope sred = (PC_Telescope)pc->data;
659: PetscErrorCode ierr;
660: Vec xtmp,yred;
661: PetscInt i,st,ed;
662: VecScatter scatter;
663: const PetscScalar *x_array;
664: PetscBool default_init_guess_value;
667: xtmp = sred->xtmp;
668: scatter = sred->scatter;
669: yred = sred->yred;
671: if (its > 1) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"PCApplyRichardson_Telescope only supports max_it = 1");
672: *reason = (PCRichardsonConvergedReason)0;
674: if (!zeroguess) {
675: PetscInfo(pc,"PCTelescope: Scattering y for non-zero initial guess\n");
676: /* pull in vector y->xtmp */
677: VecScatterBegin(scatter,y,xtmp,INSERT_VALUES,SCATTER_FORWARD);
678: VecScatterEnd(scatter,y,xtmp,INSERT_VALUES,SCATTER_FORWARD);
680: /* copy vector entries into xred */
681: VecGetArrayRead(xtmp,&x_array);
682: if (yred) {
683: PetscScalar *LA_yred;
684: VecGetOwnershipRange(yred,&st,&ed);
685: VecGetArray(yred,&LA_yred);
686: for (i=0; i<ed-st; i++) {
687: LA_yred[i] = x_array[i];
688: }
689: VecRestoreArray(yred,&LA_yred);
690: }
691: VecRestoreArrayRead(xtmp,&x_array);
692: }
694: if (PCTelescope_isActiveRank(sred)) {
695: KSPGetInitialGuessNonzero(sred->ksp,&default_init_guess_value);
696: if (!zeroguess) {KSPSetInitialGuessNonzero(sred->ksp,PETSC_TRUE);}
697: }
699: PCApply_Telescope(pc,x,y);
701: if (PCTelescope_isActiveRank(sred)) {
702: KSPSetInitialGuessNonzero(sred->ksp,default_init_guess_value);
703: }
705: if (!*reason) *reason = PCRICHARDSON_CONVERGED_ITS;
706: *outits = 1;
707: return(0);
708: }
710: static PetscErrorCode PCReset_Telescope(PC pc)
711: {
712: PC_Telescope sred = (PC_Telescope)pc->data;
716: ISDestroy(&sred->isin);
717: VecScatterDestroy(&sred->scatter);
718: VecDestroy(&sred->xred);
719: VecDestroy(&sred->yred);
720: VecDestroy(&sred->xtmp);
721: MatDestroy(&sred->Bred);
722: KSPReset(sred->ksp);
723: if (sred->pctelescope_reset_type) {
724: sred->pctelescope_reset_type(pc);
725: }
726: return(0);
727: }
729: static PetscErrorCode PCDestroy_Telescope(PC pc)
730: {
731: PC_Telescope sred = (PC_Telescope)pc->data;
735: PCReset_Telescope(pc);
736: KSPDestroy(&sred->ksp);
737: PetscSubcommDestroy(&sred->psubcomm);
738: PetscFree(sred->dm_ctx);
739: PetscFree(pc->data);
740: return(0);
741: }
743: static PetscErrorCode PCSetFromOptions_Telescope(PetscOptionItems *PetscOptionsObject,PC pc)
744: {
745: PC_Telescope sred = (PC_Telescope)pc->data;
746: PetscErrorCode ierr;
747: MPI_Comm comm;
748: PetscMPIInt size;
749: PetscBool flg;
750: PetscSubcommType subcommtype;
753: PetscObjectGetComm((PetscObject)pc,&comm);
754: MPI_Comm_size(comm,&size);
755: PetscOptionsHead(PetscOptionsObject,"Telescope options");
756: PetscOptionsEnum("-pc_telescope_subcomm_type","Subcomm type (interlaced or contiguous)","PCTelescopeSetSubcommType",PetscSubcommTypes,(PetscEnum)sred->subcommtype,(PetscEnum*)&subcommtype,&flg);
757: if (flg) {
758: PCTelescopeSetSubcommType(pc,subcommtype);
759: }
760: PetscOptionsInt("-pc_telescope_reduction_factor","Factor to reduce comm size by","PCTelescopeSetReductionFactor",sred->redfactor,&sred->redfactor,NULL);
761: if (sred->redfactor > size) SETERRQ(comm,PETSC_ERR_ARG_WRONG,"-pc_telescope_reduction_factor <= comm size");
762: PetscOptionsBool("-pc_telescope_ignore_dm","Ignore any DM attached to the PC","PCTelescopeSetIgnoreDM",sred->ignore_dm,&sred->ignore_dm,NULL);
763: PetscOptionsBool("-pc_telescope_ignore_kspcomputeoperators","Ignore method used to compute A","PCTelescopeSetIgnoreKSPComputeOperators",sred->ignore_kspcomputeoperators,&sred->ignore_kspcomputeoperators,NULL);
764: PetscOptionsBool("-pc_telescope_use_coarse_dm","Define sub-communicator from the coarse DM","PCTelescopeSetUseCoarseDM",sred->use_coarse_dm,&sred->use_coarse_dm,NULL);
765: PetscOptionsTail();
766: return(0);
767: }
769: /* PC simplementation specific API's */
771: static PetscErrorCode PCTelescopeGetKSP_Telescope(PC pc,KSP *ksp)
772: {
773: PC_Telescope red = (PC_Telescope)pc->data;
775: if (ksp) *ksp = red->ksp;
776: return(0);
777: }
779: static PetscErrorCode PCTelescopeGetSubcommType_Telescope(PC pc,PetscSubcommType *subcommtype)
780: {
781: PC_Telescope red = (PC_Telescope)pc->data;
783: if (subcommtype) *subcommtype = red->subcommtype;
784: return(0);
785: }
787: static PetscErrorCode PCTelescopeSetSubcommType_Telescope(PC pc,PetscSubcommType subcommtype)
788: {
789: PC_Telescope red = (PC_Telescope)pc->data;
792: if (pc->setupcalled) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONGSTATE,"You cannot change the subcommunicator type for PCTelescope after it has been set up.");
793: red->subcommtype = subcommtype;
794: return(0);
795: }
797: static PetscErrorCode PCTelescopeGetReductionFactor_Telescope(PC pc,PetscInt *fact)
798: {
799: PC_Telescope red = (PC_Telescope)pc->data;
801: if (fact) *fact = red->redfactor;
802: return(0);
803: }
805: static PetscErrorCode PCTelescopeSetReductionFactor_Telescope(PC pc,PetscInt fact)
806: {
807: PC_Telescope red = (PC_Telescope)pc->data;
808: PetscMPIInt size;
809: PetscErrorCode ierr;
812: MPI_Comm_size(PetscObjectComm((PetscObject)pc),&size);
813: if (fact <= 0) SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONG,"Reduction factor of telescoping PC %D must be positive",fact);
814: if (fact > size) SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONG,"Reduction factor of telescoping PC %D must be <= comm.size",fact);
815: red->redfactor = fact;
816: return(0);
817: }
819: static PetscErrorCode PCTelescopeGetIgnoreDM_Telescope(PC pc,PetscBool *v)
820: {
821: PC_Telescope red = (PC_Telescope)pc->data;
823: if (v) *v = red->ignore_dm;
824: return(0);
825: }
827: static PetscErrorCode PCTelescopeSetIgnoreDM_Telescope(PC pc,PetscBool v)
828: {
829: PC_Telescope red = (PC_Telescope)pc->data;
831: red->ignore_dm = v;
832: return(0);
833: }
835: static PetscErrorCode PCTelescopeGetUseCoarseDM_Telescope(PC pc,PetscBool *v)
836: {
837: PC_Telescope red = (PC_Telescope)pc->data;
839: if (v) *v = red->use_coarse_dm;
840: return(0);
841: }
843: static PetscErrorCode PCTelescopeSetUseCoarseDM_Telescope(PC pc,PetscBool v)
844: {
845: PC_Telescope red = (PC_Telescope)pc->data;
847: red->use_coarse_dm = v;
848: return(0);
849: }
851: static PetscErrorCode PCTelescopeGetIgnoreKSPComputeOperators_Telescope(PC pc,PetscBool *v)
852: {
853: PC_Telescope red = (PC_Telescope)pc->data;
855: if (v) *v = red->ignore_kspcomputeoperators;
856: return(0);
857: }
859: static PetscErrorCode PCTelescopeSetIgnoreKSPComputeOperators_Telescope(PC pc,PetscBool v)
860: {
861: PC_Telescope red = (PC_Telescope)pc->data;
863: red->ignore_kspcomputeoperators = v;
864: return(0);
865: }
867: static PetscErrorCode PCTelescopeGetDM_Telescope(PC pc,DM *dm)
868: {
869: PC_Telescope red = (PC_Telescope)pc->data;
871: *dm = private_PCTelescopeGetSubDM(red);
872: return(0);
873: }
875: /*@
876: PCTelescopeGetKSP - Gets the KSP created by the telescoping PC.
878: Not Collective
880: Input Parameter:
881: . pc - the preconditioner context
883: Output Parameter:
884: . subksp - the KSP defined the smaller set of processes
886: Level: advanced
888: @*/
889: PetscErrorCode PCTelescopeGetKSP(PC pc,KSP *subksp)
890: {
893: PetscUseMethod(pc,"PCTelescopeGetKSP_C",(PC,KSP*),(pc,subksp));
894: return(0);
895: }
897: /*@
898: PCTelescopeGetReductionFactor - Gets the factor by which the original number of processes has been reduced by.
900: Not Collective
902: Input Parameter:
903: . pc - the preconditioner context
905: Output Parameter:
906: . fact - the reduction factor
908: Level: advanced
910: @*/
911: PetscErrorCode PCTelescopeGetReductionFactor(PC pc,PetscInt *fact)
912: {
915: PetscUseMethod(pc,"PCTelescopeGetReductionFactor_C",(PC,PetscInt*),(pc,fact));
916: return(0);
917: }
919: /*@
920: PCTelescopeSetReductionFactor - Sets the factor by which the original number of processes has been reduced by.
922: Not Collective
924: Input Parameter:
925: . pc - the preconditioner context
927: Output Parameter:
928: . fact - the reduction factor
930: Level: advanced
932: @*/
933: PetscErrorCode PCTelescopeSetReductionFactor(PC pc,PetscInt fact)
934: {
937: PetscTryMethod(pc,"PCTelescopeSetReductionFactor_C",(PC,PetscInt),(pc,fact));
938: return(0);
939: }
941: /*@
942: PCTelescopeGetIgnoreDM - Get the flag indicating if any DM attached to the PC will be used.
944: Not Collective
946: Input Parameter:
947: . pc - the preconditioner context
949: Output Parameter:
950: . v - the flag
952: Level: advanced
954: @*/
955: PetscErrorCode PCTelescopeGetIgnoreDM(PC pc,PetscBool *v)
956: {
959: PetscUseMethod(pc,"PCTelescopeGetIgnoreDM_C",(PC,PetscBool*),(pc,v));
960: return(0);
961: }
963: /*@
964: PCTelescopeSetIgnoreDM - Set a flag to ignore any DM attached to the PC.
966: Not Collective
968: Input Parameter:
969: . pc - the preconditioner context
971: Output Parameter:
972: . v - Use PETSC_TRUE to ignore any DM
974: Level: advanced
976: @*/
977: PetscErrorCode PCTelescopeSetIgnoreDM(PC pc,PetscBool v)
978: {
981: PetscTryMethod(pc,"PCTelescopeSetIgnoreDM_C",(PC,PetscBool),(pc,v));
982: return(0);
983: }
985: /*@
986: PCTelescopeGetUseCoarseDM - Get the flag indicating if the coarse DM attached to DM associated with the PC will be used.
988: Not Collective
990: Input Parameter:
991: . pc - the preconditioner context
993: Output Parameter:
994: . v - the flag
996: Level: advanced
998: @*/
999: PetscErrorCode PCTelescopeGetUseCoarseDM(PC pc,PetscBool *v)
1000: {
1003: PetscUseMethod(pc,"PCTelescopeGetUseCoarseDM_C",(PC,PetscBool*),(pc,v));
1004: return(0);
1005: }
1007: /*@
1008: PCTelescopeSetUseCoarseDM - Set a flag to query the DM attached to the PC if it also has a coarse DM
1010: Not Collective
1012: Input Parameter:
1013: . pc - the preconditioner context
1015: Output Parameter:
1016: . v - Use PETSC_FALSE to ignore any coarse DM
1018: Notes:
1019: When you have specified to use a coarse DM, the communicator used to create the sub-KSP within PCTelescope
1020: will be that of the coarse DM. Hence the flags -pc_telescope_reduction_factor and
1021: -pc_telescope_subcomm_type will no longer have any meaning.
1022: It is required that the communicator associated with the parent (fine) and the coarse DM are of different sizes.
1023: An error will occur of the size of the communicator associated with the coarse DM
1024: is the same as that of the parent DM.
1025: Furthermore, it is required that the communicator on the coarse DM is a sub-communicator of the parent.
1026: This will be checked at the time the preconditioner is setup and an error will occur if
1027: the coarse DM does not define a sub-communicator of that used by the parent DM.
1029: The particular Telescope setup invoked when using a coarse DM is agnostic with respect to the type of
1030: the DM used (e.g. it supports DMSHELL, DMPLEX, etc).
1032: Support is currently only provided for the case when you are using KSPSetComputeOperators()
1034: The user is required to compose a function with the parent DM to facilitate the transfer of fields (Vec) between the different decompositions defined by the fine and coarse DMs.
1035: In the user code, this is achieved via
1036: .vb
1037: {
1038: DM dm_fine;
1039: PetscObjectCompose((PetscObject)dm_fine,"PCTelescopeFieldScatter",your_field_scatter_method);
1040: }
1041: .ve
1042: The signature of the user provided field scatter method is
1043: .vb
1044: PetscErrorCode your_field_scatter_method(DM dm_fine,Vec x_fine,ScatterMode mode,DM dm_coarse,Vec x_coarse);
1045: .ve
1046: The user must provide support for both mode = SCATTER_FORWARD and mode = SCATTER_REVERSE.
1047: SCATTER_FORWARD implies the direction of transfer is from the parent (fine) DM to the coarse DM.
1049: Optionally, the user may also compose a function with the parent DM to facilitate the transfer
1050: of state variables between the fine and coarse DMs.
1051: In the context of a finite element discretization, an example state variable might be
1052: values associated with quadrature points within each element.
1053: A user provided state scatter method is composed via
1054: .vb
1055: {
1056: DM dm_fine;
1057: PetscObjectCompose((PetscObject)dm_fine,"PCTelescopeStateScatter",your_state_scatter_method);
1058: }
1059: .ve
1060: The signature of the user provided state scatter method is
1061: .vb
1062: PetscErrorCode your_state_scatter_method(DM dm_fine,ScatterMode mode,DM dm_coarse);
1063: .ve
1064: SCATTER_FORWARD implies the direction of transfer is from the fine DM to the coarse DM.
1065: The user is only required to support mode = SCATTER_FORWARD.
1066: No assumption is made about the data type of the state variables.
1067: These must be managed by the user and must be accessible from the DM.
1069: Care must be taken in defining the user context passed to KSPSetComputeOperators() which is to be
1070: associated with the sub-KSP residing within PCTelescope.
1071: In general, PCTelescope assumes that the context on the fine and coarse DM used with
1072: KSPSetComputeOperators() should be "similar" in type or origin.
1073: Specifically the following rules are used to infer what context on the sub-KSP should be.
1075: First the contexts from the KSP and the fine and coarse DMs are retrieved.
1076: Note that the special case of a DMSHELL context is queried.
1078: .vb
1079: DMKSPGetComputeOperators(dm_fine,&dmfine_kspfunc,&dmfine_kspctx);
1080: DMGetApplicationContext(dm_fine,&dmfine_appctx);
1081: DMShellGetContext(dm_fine,&dmfine_shellctx);
1083: DMGetApplicationContext(dm_coarse,&dmcoarse_appctx);
1084: DMShellGetContext(dm_coarse,&dmcoarse_shellctx);
1085: .ve
1087: The following rules are then enforced:
1089: 1. If dmfine_kspctx = NULL, then we provide a NULL pointer as the context for the sub-KSP:
1090: KSPSetComputeOperators(sub_ksp,dmfine_kspfunc,NULL);
1092: 2. If dmfine_kspctx != NULL and dmfine_kspctx == dmfine_appctx,
1093: check that dmcoarse_appctx is also non-NULL. If this is true, then:
1094: KSPSetComputeOperators(sub_ksp,dmfine_kspfunc,dmcoarse_appctx);
1096: 3. If dmfine_kspctx != NULL and dmfine_kspctx == dmfine_shellctx,
1097: check that dmcoarse_shellctx is also non-NULL. If this is true, then:
1098: KSPSetComputeOperators(sub_ksp,dmfine_kspfunc,dmcoarse_shellctx);
1100: If neither of the above three tests passed, then PCTelescope cannot safely determine what
1101: context should be provided to KSPSetComputeOperators() for use with the sub-KSP.
1102: In this case, an additional mechanism is provided via a composed function which will return
1103: the actual context to be used. To use this feature you must compose the "getter" function
1104: with the coarse DM, e.g.
1105: .vb
1106: {
1107: DM dm_coarse;
1108: PetscObjectCompose((PetscObject)dm_coarse,"PCTelescopeGetCoarseDMKSPContext",your_coarse_context_getter);
1109: }
1110: .ve
1111: The signature of the user provided method is
1112: .vb
1113: PetscErrorCode your_coarse_context_getter(DM dm_coarse,void **your_kspcontext);
1114: .ve
1116: Level: advanced
1118: @*/
1119: PetscErrorCode PCTelescopeSetUseCoarseDM(PC pc,PetscBool v)
1120: {
1123: PetscTryMethod(pc,"PCTelescopeSetUseCoarseDM_C",(PC,PetscBool),(pc,v));
1124: return(0);
1125: }
1127: /*@
1128: PCTelescopeGetIgnoreKSPComputeOperators - Get the flag indicating if KSPComputeOperators will be used.
1130: Not Collective
1132: Input Parameter:
1133: . pc - the preconditioner context
1135: Output Parameter:
1136: . v - the flag
1138: Level: advanced
1140: @*/
1141: PetscErrorCode PCTelescopeGetIgnoreKSPComputeOperators(PC pc,PetscBool *v)
1142: {
1145: PetscUseMethod(pc,"PCTelescopeGetIgnoreKSPComputeOperators_C",(PC,PetscBool*),(pc,v));
1146: return(0);
1147: }
1149: /*@
1150: PCTelescopeSetIgnoreKSPComputeOperators - Set a flag to ignore KSPComputeOperators.
1152: Not Collective
1154: Input Parameter:
1155: . pc - the preconditioner context
1157: Output Parameter:
1158: . v - Use PETSC_TRUE to ignore the method (if defined) set via KSPSetComputeOperators on pc
1160: Level: advanced
1162: @*/
1163: PetscErrorCode PCTelescopeSetIgnoreKSPComputeOperators(PC pc,PetscBool v)
1164: {
1167: PetscTryMethod(pc,"PCTelescopeSetIgnoreKSPComputeOperators_C",(PC,PetscBool),(pc,v));
1168: return(0);
1169: }
1171: /*@
1172: PCTelescopeGetDM - Get the re-partitioned DM attached to the sub KSP.
1174: Not Collective
1176: Input Parameter:
1177: . pc - the preconditioner context
1179: Output Parameter:
1180: . subdm - The re-partitioned DM
1182: Level: advanced
1184: @*/
1185: PetscErrorCode PCTelescopeGetDM(PC pc,DM *subdm)
1186: {
1189: PetscUseMethod(pc,"PCTelescopeGetDM_C",(PC,DM*),(pc,subdm));
1190: return(0);
1191: }
1193: /*@
1194: PCTelescopeSetSubcommType - set subcommunicator type (interlaced or contiguous)
1196: Logically Collective
1198: Input Parameters:
1199: + pc - the preconditioner context
1200: - subcommtype - the subcommunicator type (see PetscSubcommType)
1202: Level: advanced
1204: .seealso: PetscSubcommType, PetscSubcomm, PCTELESCOPE
1205: @*/
1206: PetscErrorCode PCTelescopeSetSubcommType(PC pc, PetscSubcommType subcommtype)
1207: {
1210: PetscTryMethod(pc,"PCTelescopeSetSubcommType_C",(PC,PetscSubcommType),(pc,subcommtype));
1211: return(0);
1212: }
1214: /*@
1215: PCTelescopeGetSubcommType - Get the subcommunicator type (interlaced or contiguous)
1217: Not Collective
1219: Input Parameter:
1220: . pc - the preconditioner context
1222: Output Parameter:
1223: . subcommtype - the subcommunicator type (see PetscSubcommType)
1225: Level: advanced
1227: .seealso: PetscSubcomm, PetscSubcommType, PCTELESCOPE
1228: @*/
1229: PetscErrorCode PCTelescopeGetSubcommType(PC pc, PetscSubcommType *subcommtype)
1230: {
1233: PetscUseMethod(pc,"PCTelescopeGetSubcommType_C",(PC,PetscSubcommType*),(pc,subcommtype));
1234: return(0);
1235: }
1237: /* -------------------------------------------------------------------------------------*/
1238: /*MC
1239: PCTELESCOPE - Runs a KSP solver on a sub-communicator. MPI ranks not in the sub-communicator are idle during the solve.
1241: Options Database:
1242: + -pc_telescope_reduction_factor <r> - factor to reduce the communicator size by. e.g. with 64 MPI ranks and r=4, the new sub-communicator will have 64/4 = 16 ranks.
1243: . -pc_telescope_ignore_dm - flag to indicate whether an attached DM should be ignored.
1244: . -pc_telescope_subcomm_type <interlaced,contiguous> - defines the selection of MPI ranks on the sub-communicator. see PetscSubcomm for more information.
1245: . -pc_telescope_ignore_kspcomputeoperators - flag to indicate whether KSPSetComputeOperators should be used on the sub-KSP.
1246: - -pc_telescope_use_coarse_dm - flag to indicate whether the coarse DM should be used to define the sub-communicator.
1248: Level: advanced
1250: Notes:
1251: Assuming that the parent preconditioner (PC) is defined on a communicator c, this implementation
1252: creates a child sub-communicator (c') containing fewer MPI ranks than the original parent preconditioner (PC).
1253: The preconditioner is deemed telescopic as it only calls KSPSolve() on a single
1254: sub-communicator, in contrast with PCREDUNDANT which calls KSPSolve() on N sub-communicators.
1255: This means there will be MPI ranks which will be idle during the application of this preconditioner.
1256: Additionally, in comparison with PCREDUNDANT, PCTELESCOPE can utilize an attached DM.
1258: The default type of the sub KSP (the KSP defined on c') is PREONLY.
1260: There are three setup mechanisms for PCTelescope. Features support by each type are described below.
1261: In the following, we will refer to the operators B and B', these are the Bmat provided to the KSP on the
1262: communicators c and c' respectively.
1264: [1] Default setup
1265: The sub-communicator c' is created via PetscSubcommCreate().
1266: Explicitly defined nullspace and near nullspace vectors will be propagated from B to B'.
1267: Currently there is no support define nullspaces via a user supplied method (e.g. as passed to MatNullSpaceSetFunction()).
1268: No support is provided for KSPSetComputeOperators().
1269: Currently there is no support for the flag -pc_use_amat.
1271: [2] DM aware setup
1272: If a DM is attached to the PC, it is re-partitioned on the sub-communicator c'.
1273: c' is created via PetscSubcommCreate().
1274: Both the Bmat operator and the right hand side vector are permuted into the new DOF ordering defined by the re-partitioned DM.
1275: Currently only support for re-partitioning a DMDA is provided.
1276: Any explicitly defined nullspace or near nullspace vectors attached to the original Bmat operator (B) are extracted, re-partitioned and set on the re-partitioned Bmat operator (B').
1277: Currently there is no support define nullspaces via a user supplied method (e.g. as passed to MatNullSpaceSetFunction()).
1278: Support is provided for KSPSetComputeOperators(). The user provided function and context is propagated to the sub KSP.
1279: This is fragile since the user must ensure that their user context is valid for use on c'.
1280: Currently there is no support for the flag -pc_use_amat.
1282: [3] Coarse DM setup
1283: If a DM (dmfine) is attached to the PC, dmfine is queried for a "coarse" DM (call this dmcoarse) via DMGetCoarseDM().
1284: PCTELESCOPE will interpret the coarse DM as being defined on a sub-communicator of c.
1285: The communicator associated with dmcoarse will define the c' to be used within PCTELESCOPE.
1286: PCTELESCOPE will check that c' is in fact a sub-communicator of c. If it is not, an error will be reported.
1287: The intention of this setup type is that PCTELESCOPE will use an existing (e.g. user defined) communicator hierarchy, say as would be
1288: available with using multi-grid on unstructured meshes.
1289: This setup will not use the command line options -pc_telescope_reduction_factor or -pc_telescope_subcomm_type.
1290: Any explicitly defined nullspace or near nullspace vectors attached to the original Bmat operator (B) are extracted, scattered into the correct ordering consistent with dmcoarse and set on B'.
1291: Currently there is no support define nullspaces via a user supplied method (e.g. as passed to MatNullSpaceSetFunction()).
1292: There is no general method to permute field orderings, hence only KSPSetComputeOperators() is supported.
1293: The user must use PetscObjectComposeFunction() with dmfine to define the method to scatter fields from dmfine to dmcoarse.
1294: Propagation of the user context for KSPSetComputeOperators() on the sub KSP is attempted by querying the DM contexts associated with dmfine and dmcoarse. Alternatively, the user may use PetscObjectComposeFunction() with dmcoarse to define a method which will return the appropriate user context for KSPSetComputeOperators().
1295: Currently there is no support for the flag -pc_use_amat.
1296: This setup can be invoked by the option -pc_telescope_use_coarse_dm or by calling PCTelescopeSetUseCoarseDM(pc,PETSC_TRUE);
1297: Further information about the user-provided methods required by this setup type are described here PCTelescopeSetUseCoarseDM().
1299: Developer Notes:
1300: During PCSetup, the B operator is scattered onto c'.
1301: Within PCApply, the RHS vector (x) is scattered into a redundant vector, xred (defined on c').
1302: Then, KSPSolve() is executed on the c' communicator.
1304: The communicator used within the telescoping preconditioner is defined by a PetscSubcomm using the INTERLACED
1305: creation routine by default (this can be changed with -pc_telescope_subcomm_type). We run the sub KSP on only the ranks within the communicator which have a color equal to zero.
1307: The telescoping preconditioner is aware of nullspaces and near nullspaces which are attached to the B operator.
1308: In the case where B has a (near) nullspace attached, the (near) nullspace vectors are extracted from B and mapped into
1309: a new (near) nullspace, defined on the sub-communicator, which is attached to B' (the B operator which was scattered to c')
1311: The telescoping preconditioner can re-partition an attached DM if it is a DMDA (2D or 3D -
1312: support for 1D DMDAs is not provided). If a DMDA is found, a topologically equivalent DMDA is created on c'
1313: and this new DM is attached the sub KSP. The design of telescope is such that it should be possible to extend support
1314: for re-partitioning other to DM's (e.g. DMPLEX). The user can supply a flag to ignore attached DMs.
1315: Alternatively, user-provided re-partitioned DMs can be used via -pc_telescope_use_coarse_dm.
1317: With the default setup mode, B' is defined by fusing rows (in order) associated with MPI ranks common to c and c'.
1319: When a DMDA is attached to the parent preconditioner, B' is defined by: (i) performing a symmetric permutation of B
1320: into the ordering defined by the DMDA on c', (ii) extracting the local chunks via MatCreateSubMatrices(), (iii) fusing the
1321: locally (sequential) matrices defined on the ranks common to c and c' into B' using MatCreateMPIMatConcatenateSeqMat()
1323: Limitations/improvements include the following.
1324: VecPlaceArray() could be used within PCApply() to improve efficiency and reduce memory usage.
1325: A unified mechanism to query for user contexts as required by KSPSetComputeOperators() and MatNullSpaceSetFunction().
1327: The symmetric permutation used when a DMDA is encountered is performed via explicitly assembling a permutation matrix P,
1328: and performing P^T.A.P. Possibly it might be more efficient to use MatPermute(). We opted to use P^T.A.P as it appears
1329: VecPermute() does not support the use case required here. By computing P, one can permute both the operator and RHS in a
1330: consistent manner.
1332: Mapping of vectors (default setup mode) is performed in the following way.
1333: Suppose the parent communicator size was 4, and we set a reduction factor of 2; this would give a comm size on c' of 2.
1334: Using the interlaced creation routine, the ranks in c with color = 0 will be rank 0 and 2.
1335: We perform the scatter to the sub-communicator in the following way.
1336: [1] Given a vector x defined on communicator c
1338: .vb
1339: rank(c) local values of x
1340: ------- ----------------------------------------
1341: 0 [ 0.0, 1.0, 2.0, 3.0, 4.0, 5.0 ]
1342: 1 [ 6.0, 7.0, 8.0, 9.0, 10.0, 11.0 ]
1343: 2 [ 12.0, 13.0, 14.0, 15.0, 16.0, 17.0 ]
1344: 3 [ 18.0, 19.0, 20.0, 21.0, 22.0, 23.0 ]
1345: .ve
1347: scatter into xtmp defined also on comm c, so that we have the following values
1349: .vb
1350: rank(c) local values of xtmp
1351: ------- ----------------------------------------------------------------------------
1352: 0 [ 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0 ]
1353: 1 [ ]
1354: 2 [ 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0 ]
1355: 3 [ ]
1356: .ve
1358: The entries on rank 1 and 3 (ranks which do not have a color = 0 in c') have no values
1360: [2] Copy the values from ranks 0, 2 (indices with respect to comm c) into the vector xred which is defined on communicator c'.
1361: Ranks 0 and 2 are the only ranks in the subcomm which have a color = 0.
1363: .vb
1364: rank(c') local values of xred
1365: -------- ----------------------------------------------------------------------------
1366: 0 [ 0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0 ]
1367: 1 [ 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0, 21.0, 22.0, 23.0 ]
1368: .ve
1370: Contributed by Dave May
1372: Reference:
1373: Dave A. May, Patrick Sanan, Karl Rupp, Matthew G. Knepley, and Barry F. Smith, "Extreme-Scale Multigrid Components within PETSc". 2016. In Proceedings of the Platform for Advanced Scientific Computing Conference (PASC '16). DOI: 10.1145/2929908.2929913
1375: .seealso: PCTelescopeGetKSP(), PCTelescopeGetDM(), PCTelescopeGetReductionFactor(), PCTelescopeSetReductionFactor(), PCTelescopeGetIgnoreDM(), PCTelescopeSetIgnoreDM(), PCREDUNDANT
1376: M*/
1377: PETSC_EXTERN PetscErrorCode PCCreate_Telescope(PC pc)
1378: {
1379: PetscErrorCode ierr;
1380: struct _PC_Telescope *sred;
1383: PetscNewLog(pc,&sred);
1384: sred->psubcomm = NULL;
1385: sred->subcommtype = PETSC_SUBCOMM_INTERLACED;
1386: sred->subcomm = MPI_COMM_NULL;
1387: sred->redfactor = 1;
1388: sred->ignore_dm = PETSC_FALSE;
1389: sred->ignore_kspcomputeoperators = PETSC_FALSE;
1390: sred->use_coarse_dm = PETSC_FALSE;
1391: pc->data = (void*)sred;
1393: pc->ops->apply = PCApply_Telescope;
1394: pc->ops->applytranspose = NULL;
1395: pc->ops->applyrichardson = PCApplyRichardson_Telescope;
1396: pc->ops->setup = PCSetUp_Telescope;
1397: pc->ops->destroy = PCDestroy_Telescope;
1398: pc->ops->reset = PCReset_Telescope;
1399: pc->ops->setfromoptions = PCSetFromOptions_Telescope;
1400: pc->ops->view = PCView_Telescope;
1402: sred->pctelescope_setup_type = PCTelescopeSetUp_default;
1403: sred->pctelescope_matcreate_type = PCTelescopeMatCreate_default;
1404: sred->pctelescope_matnullspacecreate_type = PCTelescopeMatNullSpaceCreate_default;
1405: sred->pctelescope_reset_type = NULL;
1407: PetscObjectComposeFunction((PetscObject)pc,"PCTelescopeGetKSP_C",PCTelescopeGetKSP_Telescope);
1408: PetscObjectComposeFunction((PetscObject)pc,"PCTelescopeGetSubcommType_C",PCTelescopeGetSubcommType_Telescope);
1409: PetscObjectComposeFunction((PetscObject)pc,"PCTelescopeSetSubcommType_C",PCTelescopeSetSubcommType_Telescope);
1410: PetscObjectComposeFunction((PetscObject)pc,"PCTelescopeGetReductionFactor_C",PCTelescopeGetReductionFactor_Telescope);
1411: PetscObjectComposeFunction((PetscObject)pc,"PCTelescopeSetReductionFactor_C",PCTelescopeSetReductionFactor_Telescope);
1412: PetscObjectComposeFunction((PetscObject)pc,"PCTelescopeGetIgnoreDM_C",PCTelescopeGetIgnoreDM_Telescope);
1413: PetscObjectComposeFunction((PetscObject)pc,"PCTelescopeSetIgnoreDM_C",PCTelescopeSetIgnoreDM_Telescope);
1414: PetscObjectComposeFunction((PetscObject)pc,"PCTelescopeGetIgnoreKSPComputeOperators_C",PCTelescopeGetIgnoreKSPComputeOperators_Telescope);
1415: PetscObjectComposeFunction((PetscObject)pc,"PCTelescopeSetIgnoreKSPComputeOperators_C",PCTelescopeSetIgnoreKSPComputeOperators_Telescope);
1416: PetscObjectComposeFunction((PetscObject)pc,"PCTelescopeGetDM_C",PCTelescopeGetDM_Telescope);
1417: PetscObjectComposeFunction((PetscObject)pc,"PCTelescopeGetUseCoarseDM_C",PCTelescopeGetUseCoarseDM_Telescope);
1418: PetscObjectComposeFunction((PetscObject)pc,"PCTelescopeSetUseCoarseDM_C",PCTelescopeSetUseCoarseDM_Telescope);
1419: return(0);
1420: }