Actual source code: telescope.c
petsc-3.11.4 2019-09-28
2: #include <petsc/private/petscimpl.h>
3: #include <petsc/private/matimpl.h>
4: #include <petsc/private/pcimpl.h>
5: #include <petscksp.h>
6: #include <petscdm.h>
7: #include "../src/ksp/pc/impls/telescope/telescope.h"
9: static PetscBool cited = PETSC_FALSE;
10: static const char citation[] =
11: "@inproceedings{MaySananRuppKnepleySmith2016,\n"
12: " title = {Extreme-Scale Multigrid Components within PETSc},\n"
13: " author = {Dave A. May and Patrick Sanan and Karl Rupp and Matthew G. Knepley and Barry F. Smith},\n"
14: " booktitle = {Proceedings of the Platform for Advanced Scientific Computing Conference},\n"
15: " series = {PASC '16},\n"
16: " isbn = {978-1-4503-4126-4},\n"
17: " location = {Lausanne, Switzerland},\n"
18: " pages = {5:1--5:12},\n"
19: " articleno = {5},\n"
20: " numpages = {12},\n"
21: " url = {http://doi.acm.org/10.1145/2929908.2929913},\n"
22: " doi = {10.1145/2929908.2929913},\n"
23: " acmid = {2929913},\n"
24: " publisher = {ACM},\n"
25: " address = {New York, NY, USA},\n"
26: " keywords = {GPU, HPC, agglomeration, coarse-level solver, multigrid, parallel computing, preconditioning},\n"
27: " year = {2016}\n"
28: "}\n";
30: /*
31: default setup mode
33: [1a] scatter to (FORWARD)
34: x(comm) -> xtmp(comm)
35: [1b] local copy (to) ranks with color = 0
36: xred(subcomm) <- xtmp
37:
38: [2] solve on sub KSP to obtain yred(subcomm)
40: [3a] local copy (from) ranks with color = 0
41: yred(subcomm) --> xtmp
42: [2b] scatter from (REVERSE)
43: xtmp(comm) -> y(comm)
44: */
46: /*
47: Collective on MPI_Comm[comm_f]
48: Notes
49: * Using comm_f = MPI_COMM_NULL will result in an error
50: * Using comm_c = MPI_COMM_NULL is valid. If all instances of comm_c are NULL the subcomm is not valid.
51: * If any non NULL comm_c communicator cannot map any of its ranks to comm_f, the subcomm is not valid.
52: */
53: PetscErrorCode PCTelescopeTestValidSubcomm(MPI_Comm comm_f,MPI_Comm comm_c,PetscBool *isvalid)
54: {
55: PetscInt valid = 1;
56: MPI_Group group_f,group_c;
58: PetscMPIInt count,k,size_f = 0,size_c = 0,size_c_sum = 0;
59: int *ranks_f = NULL,*ranks_c = NULL;
62: if (comm_f == MPI_COMM_NULL) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"comm_f cannot be MPI_COMM_NULL");
64: MPI_Comm_group(comm_f,&group_f);
65: if (comm_c != MPI_COMM_NULL) {
66: MPI_Comm_group(comm_c,&group_c);
67: }
69: MPI_Comm_size(comm_f,&size_f);
70: if (comm_c != MPI_COMM_NULL) {
71: MPI_Comm_size(comm_c,&size_c);
72: }
74: /* check not all comm_c's are NULL */
75: size_c_sum = size_c;
76: MPI_Allreduce(MPI_IN_PLACE,&size_c_sum,1,MPI_INT,MPI_SUM,comm_f);
77: if (size_c_sum == 0) {
78: valid = 0;
79: }
81: /* check we can map at least 1 rank in comm_c to comm_f */
82: PetscMalloc1(size_f,&ranks_f);
83: PetscMalloc1(size_c,&ranks_c);
84: for (k=0; k<size_f; k++) {
85: ranks_f[k] = MPI_UNDEFINED;
86: }
87: for (k=0; k<size_c; k++) {
88: ranks_c[k] = (int)k;
89: }
91: /*
92: MPI_Group_translate_ranks() returns a non-zero exit code if any rank cannot be translated.
93: I do not want the code to terminate immediately if this occurs, rather I want to throw
94: the error later (during PCSetUp_Telescope()) via SETERRQ() with a message indicating
95: that comm_c is not a valid sub-communicator.
96: Hence I purposefully do not call CHKERRQ() after MPI_Group_translate_ranks().
97: */
98: count = 0;
99: if (comm_c != MPI_COMM_NULL) {
100: (void)MPI_Group_translate_ranks(group_c,size_c,ranks_c,group_f,ranks_f);
101: for (k=0; k<size_f; k++) {
102: if (ranks_f[k] == MPI_UNDEFINED) {
103: count++;
104: }
105: }
106: }
107: if (count == size_f) {
108: valid = 0;
109: }
111: MPI_Allreduce(MPI_IN_PLACE,&valid,1,MPIU_INT,MPI_MIN,comm_f);
112: if (valid == 1) { *isvalid = PETSC_TRUE; }
113: else { *isvalid = PETSC_FALSE; }
115: PetscFree(ranks_f);
116: PetscFree(ranks_c);
117: MPI_Group_free(&group_f);
118: if (comm_c != MPI_COMM_NULL) {
119: MPI_Group_free(&group_c);
120: }
121: return(0);
122: }
124: DM private_PCTelescopeGetSubDM(PC_Telescope sred)
125: {
126: DM subdm = NULL;
128: if (!PCTelescope_isActiveRank(sred)) { subdm = NULL; }
129: else {
130: switch (sred->sr_type) {
131: case TELESCOPE_DEFAULT: subdm = NULL;
132: break;
133: case TELESCOPE_DMDA: subdm = ((PC_Telescope_DMDACtx*)sred->dm_ctx)->dmrepart;
134: break;
135: case TELESCOPE_DMPLEX: subdm = NULL;
136: break;
137: case TELESCOPE_COARSEDM: if (sred->ksp) { KSPGetDM(sred->ksp,&subdm); }
138: break;
139: }
140: }
141: return(subdm);
142: }
144: PetscErrorCode PCTelescopeSetUp_default(PC pc,PC_Telescope sred)
145: {
147: PetscInt m,M,bs,st,ed;
148: Vec x,xred,yred,xtmp;
149: Mat B;
150: MPI_Comm comm,subcomm;
151: VecScatter scatter;
152: IS isin;
155: PetscInfo(pc,"PCTelescope: setup (default)\n");
156: comm = PetscSubcommParent(sred->psubcomm);
157: subcomm = PetscSubcommChild(sred->psubcomm);
159: PCGetOperators(pc,NULL,&B);
160: MatGetSize(B,&M,NULL);
161: MatGetBlockSize(B,&bs);
162: MatCreateVecs(B,&x,NULL);
164: xred = NULL;
165: m = 0;
166: if (PCTelescope_isActiveRank(sred)) {
167: VecCreate(subcomm,&xred);
168: VecSetSizes(xred,PETSC_DECIDE,M);
169: VecSetBlockSize(xred,bs);
170: VecSetFromOptions(xred);
171: VecGetLocalSize(xred,&m);
172: }
174: yred = NULL;
175: if (PCTelescope_isActiveRank(sred)) {
176: VecDuplicate(xred,&yred);
177: }
179: VecCreate(comm,&xtmp);
180: VecSetSizes(xtmp,m,PETSC_DECIDE);
181: VecSetBlockSize(xtmp,bs);
182: VecSetType(xtmp,((PetscObject)x)->type_name);
184: if (PCTelescope_isActiveRank(sred)) {
185: VecGetOwnershipRange(xred,&st,&ed);
186: ISCreateStride(comm,(ed-st),st,1,&isin);
187: } else {
188: VecGetOwnershipRange(x,&st,&ed);
189: ISCreateStride(comm,0,st,1,&isin);
190: }
191: ISSetBlockSize(isin,bs);
193: VecScatterCreate(x,isin,xtmp,NULL,&scatter);
195: sred->isin = isin;
196: sred->scatter = scatter;
197: sred->xred = xred;
198: sred->yred = yred;
199: sred->xtmp = xtmp;
200: VecDestroy(&x);
201: return(0);
202: }
204: PetscErrorCode PCTelescopeMatCreate_default(PC pc,PC_Telescope sred,MatReuse reuse,Mat *A)
205: {
207: MPI_Comm comm,subcomm;
208: Mat Bred,B;
209: PetscInt nr,nc;
210: IS isrow,iscol;
211: Mat Blocal,*_Blocal;
214: PetscInfo(pc,"PCTelescope: updating the redundant preconditioned operator (default)\n");
215: PetscObjectGetComm((PetscObject)pc,&comm);
216: subcomm = PetscSubcommChild(sred->psubcomm);
217: PCGetOperators(pc,NULL,&B);
218: MatGetSize(B,&nr,&nc);
219: isrow = sred->isin;
220: ISCreateStride(comm,nc,0,1,&iscol);
221: MatCreateSubMatrices(B,1,&isrow,&iscol,MAT_INITIAL_MATRIX,&_Blocal);
222: Blocal = *_Blocal;
223: PetscFree(_Blocal);
224: Bred = NULL;
225: if (PCTelescope_isActiveRank(sred)) {
226: PetscInt mm;
228: if (reuse != MAT_INITIAL_MATRIX) { Bred = *A; }
230: MatGetSize(Blocal,&mm,NULL);
231: MatCreateMPIMatConcatenateSeqMat(subcomm,Blocal,mm,reuse,&Bred);
232: }
233: *A = Bred;
234: ISDestroy(&iscol);
235: MatDestroy(&Blocal);
236: return(0);
237: }
239: static PetscErrorCode PCTelescopeSubNullSpaceCreate_Telescope(PC pc,PC_Telescope sred,MatNullSpace nullspace,MatNullSpace *sub_nullspace)
240: {
242: PetscBool has_const;
243: const Vec *vecs;
244: Vec *sub_vecs = NULL;
245: PetscInt i,k,n = 0;
246: MPI_Comm subcomm;
249: subcomm = PetscSubcommChild(sred->psubcomm);
250: MatNullSpaceGetVecs(nullspace,&has_const,&n,&vecs);
252: if (PCTelescope_isActiveRank(sred)) {
253: if (n) {
254: VecDuplicateVecs(sred->xred,n,&sub_vecs);
255: }
256: }
258: /* copy entries */
259: for (k=0; k<n; k++) {
260: const PetscScalar *x_array;
261: PetscScalar *LA_sub_vec;
262: PetscInt st,ed;
264: /* pull in vector x->xtmp */
265: VecScatterBegin(sred->scatter,vecs[k],sred->xtmp,INSERT_VALUES,SCATTER_FORWARD);
266: VecScatterEnd(sred->scatter,vecs[k],sred->xtmp,INSERT_VALUES,SCATTER_FORWARD);
267: if (sub_vecs) {
268: /* copy vector entries into xred */
269: VecGetArrayRead(sred->xtmp,&x_array);
270: if (sub_vecs[k]) {
271: VecGetOwnershipRange(sub_vecs[k],&st,&ed);
272: VecGetArray(sub_vecs[k],&LA_sub_vec);
273: for (i=0; i<ed-st; i++) {
274: LA_sub_vec[i] = x_array[i];
275: }
276: VecRestoreArray(sub_vecs[k],&LA_sub_vec);
277: }
278: VecRestoreArrayRead(sred->xtmp,&x_array);
279: }
280: }
282: if (PCTelescope_isActiveRank(sred)) {
283: /* create new (near) nullspace for redundant object */
284: MatNullSpaceCreate(subcomm,has_const,n,sub_vecs,sub_nullspace);
285: VecDestroyVecs(n,&sub_vecs);
286: if (nullspace->remove) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Propagation of custom remove callbacks not supported when propagating (near) nullspaces with PCTelescope");
287: if (nullspace->rmctx) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"Propagation of custom remove callback context not supported when propagating (near) nullspaces with PCTelescope");
288: }
289: return(0);
290: }
292: static PetscErrorCode PCTelescopeMatNullSpaceCreate_default(PC pc,PC_Telescope sred,Mat sub_mat)
293: {
295: Mat B;
298: PCGetOperators(pc,NULL,&B);
299: /* Propagate the nullspace if it exists */
300: {
301: MatNullSpace nullspace,sub_nullspace;
302: MatGetNullSpace(B,&nullspace);
303: if (nullspace) {
304: PetscInfo(pc,"PCTelescope: generating nullspace (default)\n");
305: PCTelescopeSubNullSpaceCreate_Telescope(pc,sred,nullspace,&sub_nullspace);
306: if (PCTelescope_isActiveRank(sred)) {
307: MatSetNullSpace(sub_mat,sub_nullspace);
308: MatNullSpaceDestroy(&sub_nullspace);
309: }
310: }
311: }
312: /* Propagate the near nullspace if it exists */
313: {
314: MatNullSpace nearnullspace,sub_nearnullspace;
315: MatGetNearNullSpace(B,&nearnullspace);
316: if (nearnullspace) {
317: PetscInfo(pc,"PCTelescope: generating near nullspace (default)\n");
318: PCTelescopeSubNullSpaceCreate_Telescope(pc,sred,nearnullspace,&sub_nearnullspace);
319: if (PCTelescope_isActiveRank(sred)) {
320: MatSetNearNullSpace(sub_mat,sub_nearnullspace);
321: MatNullSpaceDestroy(&sub_nearnullspace);
322: }
323: }
324: }
325: return(0);
326: }
328: static PetscErrorCode PCView_Telescope(PC pc,PetscViewer viewer)
329: {
330: PC_Telescope sred = (PC_Telescope)pc->data;
332: PetscBool iascii,isstring;
333: PetscViewer subviewer;
336: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
337: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERSTRING,&isstring);
338: if (iascii) {
339: {
340: MPI_Comm comm,subcomm;
341: PetscMPIInt comm_size,subcomm_size;
342: DM dm = NULL,subdm = NULL;
344: PCGetDM(pc,&dm);
345: subdm = private_PCTelescopeGetSubDM(sred);
347: if (sred->psubcomm) {
348: comm = PetscSubcommParent(sred->psubcomm);
349: subcomm = PetscSubcommChild(sred->psubcomm);
350: MPI_Comm_size(comm,&comm_size);
351: MPI_Comm_size(subcomm,&subcomm_size);
353: PetscViewerASCIIPushTab(viewer);
354: PetscViewerASCIIPrintf(viewer,"petsc subcomm: parent comm size reduction factor = %D\n",sred->redfactor);
355: PetscViewerASCIIPrintf(viewer,"petsc subcomm: parent_size = %d , subcomm_size = %d\n",(int)comm_size,(int)subcomm_size);
356: switch (sred->subcommtype) {
357: case PETSC_SUBCOMM_INTERLACED :
358: PetscViewerASCIIPrintf(viewer,"petsc subcomm: type = interlaced\n",sred->subcommtype);
359: break;
360: case PETSC_SUBCOMM_CONTIGUOUS :
361: PetscViewerASCIIPrintf(viewer,"petsc subcomm type = contiguous\n",sred->subcommtype);
362: break;
363: default :
364: SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"General subcomm type not supported by PCTelescope");
365: }
366: PetscViewerASCIIPopTab(viewer);
367: } else {
368: PetscObjectGetComm((PetscObject)pc,&comm);
369: subcomm = sred->subcomm;
370: if (!PCTelescope_isActiveRank(sred)) {
371: subcomm = PETSC_COMM_SELF;
372: }
374: PetscViewerASCIIPushTab(viewer);
375: PetscViewerASCIIPrintf(viewer,"subcomm: using user provided sub-communicator\n");
376: PetscViewerASCIIPopTab(viewer);
377: }
379: PetscViewerGetSubViewer(viewer,subcomm,&subviewer);
380: if (PCTelescope_isActiveRank(sred)) {
381: PetscViewerASCIIPushTab(subviewer);
383: if (dm && sred->ignore_dm) {
384: PetscViewerASCIIPrintf(subviewer,"ignoring DM\n");
385: }
386: if (sred->ignore_kspcomputeoperators) {
387: PetscViewerASCIIPrintf(subviewer,"ignoring KSPComputeOperators\n");
388: }
389: switch (sred->sr_type) {
390: case TELESCOPE_DEFAULT:
391: PetscViewerASCIIPrintf(subviewer,"setup type: default\n");
392: break;
393: case TELESCOPE_DMDA:
394: PetscViewerASCIIPrintf(subviewer,"setup type: DMDA auto-repartitioning\n");
395: DMView_DA_Short(subdm,subviewer);
396: break;
397: case TELESCOPE_DMPLEX:
398: PetscViewerASCIIPrintf(subviewer,"setup type: DMPLEX auto-repartitioning\n");
399: break;
400: case TELESCOPE_COARSEDM:
401: PetscViewerASCIIPrintf(subviewer,"setup type: coarse DM\n");
402: break;
403: }
405: if (dm) {
406: PetscObject obj = (PetscObject)dm;
407: PetscViewerASCIIPrintf(subviewer,"Parent DM object:");
408: PetscViewerASCIIUseTabs(subviewer,PETSC_FALSE);
409: if (obj->type_name) { PetscViewerASCIIPrintf(subviewer," type = %s;",obj->type_name); }
410: if (obj->name) { PetscViewerASCIIPrintf(subviewer," name = %s;",obj->name); }
411: if (obj->prefix) { PetscViewerASCIIPrintf(subviewer," prefix = %s",obj->prefix); }
412: PetscViewerASCIIPrintf(subviewer,"\n");
413: PetscViewerASCIIUseTabs(subviewer,PETSC_TRUE);
414: } else {
415: PetscViewerASCIIPrintf(subviewer,"Parent DM object: NULL\n");
416: }
417: if (subdm) {
418: PetscObject obj = (PetscObject)subdm;
419: PetscViewerASCIIPrintf(subviewer,"Sub DM object:");
420: PetscViewerASCIIUseTabs(subviewer,PETSC_FALSE);
421: if (obj->type_name) { PetscViewerASCIIPrintf(subviewer," type = %s;",obj->type_name); }
422: if (obj->name) { PetscViewerASCIIPrintf(subviewer," name = %s;",obj->name); }
423: if (obj->prefix) { PetscViewerASCIIPrintf(subviewer," prefix = %s",obj->prefix); }
424: PetscViewerASCIIPrintf(subviewer,"\n");
425: PetscViewerASCIIUseTabs(subviewer,PETSC_TRUE);
426: } else {
427: PetscViewerASCIIPrintf(subviewer,"Sub DM object: NULL\n");
428: }
429:
430: KSPView(sred->ksp,subviewer);
431: PetscViewerASCIIPopTab(subviewer);
432: }
433: PetscViewerRestoreSubViewer(viewer,subcomm,&subviewer);
434: }
435: }
436: return(0);
437: }
439: static PetscErrorCode PCSetUp_Telescope(PC pc)
440: {
441: PC_Telescope sred = (PC_Telescope)pc->data;
442: PetscErrorCode ierr;
443: MPI_Comm comm,subcomm=0;
444: PCTelescopeType sr_type;
447: PetscObjectGetComm((PetscObject)pc,&comm);
449: /* Determine type of setup/update */
450: if (!pc->setupcalled) {
451: PetscBool has_dm,same;
452: DM dm;
454: sr_type = TELESCOPE_DEFAULT;
455: has_dm = PETSC_FALSE;
456: PCGetDM(pc,&dm);
457: if (dm) { has_dm = PETSC_TRUE; }
458: if (has_dm) {
459: /* check for dmda */
460: PetscObjectTypeCompare((PetscObject)dm,DMDA,&same);
461: if (same) {
462: PetscInfo(pc,"PCTelescope: found DMDA\n");
463: sr_type = TELESCOPE_DMDA;
464: }
465: /* check for dmplex */
466: PetscObjectTypeCompare((PetscObject)dm,DMPLEX,&same);
467: if (same) {
468: PetscInfo(pc,"PCTelescope: found DMPLEX\n");
469: sr_type = TELESCOPE_DMPLEX;
470: }
472: if (sred->use_coarse_dm) {
473: PetscInfo(pc,"PCTelescope: using coarse DM\n");
474: sr_type = TELESCOPE_COARSEDM;
475: }
477: if (sred->ignore_dm) {
478: PetscInfo(pc,"PCTelescope: ignoring DM\n");
479: sr_type = TELESCOPE_DEFAULT;
480: }
481: }
482: sred->sr_type = sr_type;
483: } else {
484: sr_type = sred->sr_type;
485: }
487: /* set function pointers for repartition setup, matrix creation/update, matrix (near) nullspace, and reset functionality */
488: switch (sr_type) {
489: case TELESCOPE_DEFAULT:
490: sred->pctelescope_setup_type = PCTelescopeSetUp_default;
491: sred->pctelescope_matcreate_type = PCTelescopeMatCreate_default;
492: sred->pctelescope_matnullspacecreate_type = PCTelescopeMatNullSpaceCreate_default;
493: sred->pctelescope_reset_type = NULL;
494: break;
495: case TELESCOPE_DMDA:
496: pc->ops->apply = PCApply_Telescope_dmda;
497: pc->ops->applyrichardson = PCApplyRichardson_Telescope_dmda;
498: sred->pctelescope_setup_type = PCTelescopeSetUp_dmda;
499: sred->pctelescope_matcreate_type = PCTelescopeMatCreate_dmda;
500: sred->pctelescope_matnullspacecreate_type = PCTelescopeMatNullSpaceCreate_dmda;
501: sred->pctelescope_reset_type = PCReset_Telescope_dmda;
502: break;
503: case TELESCOPE_DMPLEX: SETERRQ(comm,PETSC_ERR_SUP,"Support for DMPLEX is currently not available");
504: break;
505: case TELESCOPE_COARSEDM:
506: pc->ops->apply = PCApply_Telescope_CoarseDM;
507: pc->ops->applyrichardson = PCApplyRichardson_Telescope_CoarseDM;
508: sred->pctelescope_setup_type = PCTelescopeSetUp_CoarseDM;
509: sred->pctelescope_matcreate_type = NULL;
510: sred->pctelescope_matnullspacecreate_type = NULL; /* PCTelescopeMatNullSpaceCreate_CoarseDM; */
511: sred->pctelescope_reset_type = PCReset_Telescope_CoarseDM;
512: break;
513: default: SETERRQ(comm,PETSC_ERR_SUP,"Support only provided for: repartitioning an operator; repartitioning a DMDA; or using a coarse DM");
514: break;
515: }
517: /* subcomm definition */
518: if (!pc->setupcalled) {
519: if ((sr_type == TELESCOPE_DEFAULT) || (sr_type == TELESCOPE_DMDA)) {
520: if (!sred->psubcomm) {
521: PetscSubcommCreate(comm,&sred->psubcomm);
522: PetscSubcommSetNumber(sred->psubcomm,sred->redfactor);
523: PetscSubcommSetType(sred->psubcomm,sred->subcommtype);
524: PetscLogObjectMemory((PetscObject)pc,sizeof(PetscSubcomm));
525: sred->subcomm = PetscSubcommChild(sred->psubcomm);
526: }
527: } else { /* query PC for DM, check communicators */
528: DM dm,dm_coarse_partition = NULL;
529: MPI_Comm comm_fine,comm_coarse_partition = MPI_COMM_NULL;
530: PetscMPIInt csize_fine=0,csize_coarse_partition=0,cs[2],csg[2],cnt=0;
531: PetscBool isvalidsubcomm;
533: PCGetDM(pc,&dm);
534: comm_fine = PetscObjectComm((PetscObject)dm);
535: DMGetCoarseDM(dm,&dm_coarse_partition);
536: if (dm_coarse_partition) { cnt = 1; }
537: MPI_Allreduce(MPI_IN_PLACE,&cnt,1,MPI_INT,MPI_SUM,comm_fine);
538: if (cnt == 0) SETERRQ(comm_fine,PETSC_ERR_SUP,"Zero instances of a coarse DM were found");
540: MPI_Comm_size(comm_fine,&csize_fine);
541: if (dm_coarse_partition) {
542: comm_coarse_partition = PetscObjectComm((PetscObject)dm_coarse_partition);
543: MPI_Comm_size(comm_coarse_partition,&csize_coarse_partition);
544: }
546: cs[0] = csize_fine;
547: cs[1] = csize_coarse_partition;
548: MPI_Allreduce(cs,csg,2,MPI_INT,MPI_MAX,comm_fine);
549: 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");
551: PCTelescopeTestValidSubcomm(comm_fine,comm_coarse_partition,&isvalidsubcomm);
552: if (!isvalidsubcomm) SETERRQ(comm_fine,PETSC_ERR_SUP,"Coarse DM communicator is not a sub-communicator of parentDM->comm");
553: sred->subcomm = comm_coarse_partition;
554: }
555: }
556: subcomm = sred->subcomm;
558: /* internal KSP */
559: if (!pc->setupcalled) {
560: const char *prefix;
562: if (PCTelescope_isActiveRank(sred)) {
563: KSPCreate(subcomm,&sred->ksp);
564: KSPSetErrorIfNotConverged(sred->ksp,pc->erroriffailure);
565: PetscObjectIncrementTabLevel((PetscObject)sred->ksp,(PetscObject)pc,1);
566: PetscLogObjectParent((PetscObject)pc,(PetscObject)sred->ksp);
567: KSPSetType(sred->ksp,KSPPREONLY);
568: PCGetOptionsPrefix(pc,&prefix);
569: KSPSetOptionsPrefix(sred->ksp,prefix);
570: KSPAppendOptionsPrefix(sred->ksp,"telescope_");
571: }
572: }
574: /* setup */
575: if (!pc->setupcalled && sred->pctelescope_setup_type) {
576: sred->pctelescope_setup_type(pc,sred);
577: }
578: /* update */
579: if (!pc->setupcalled) {
580: if (sred->pctelescope_matcreate_type) {
581: sred->pctelescope_matcreate_type(pc,sred,MAT_INITIAL_MATRIX,&sred->Bred);
582: }
583: if (sred->pctelescope_matnullspacecreate_type) {
584: sred->pctelescope_matnullspacecreate_type(pc,sred,sred->Bred);
585: }
586: } else {
587: if (sred->pctelescope_matcreate_type) {
588: sred->pctelescope_matcreate_type(pc,sred,MAT_REUSE_MATRIX,&sred->Bred);
589: }
590: }
592: /* common - no construction */
593: if (PCTelescope_isActiveRank(sred)) {
594: KSPSetOperators(sred->ksp,sred->Bred,sred->Bred);
595: if (pc->setfromoptionscalled && !pc->setupcalled){
596: KSPSetFromOptions(sred->ksp);
597: }
598: }
599: return(0);
600: }
602: static PetscErrorCode PCApply_Telescope(PC pc,Vec x,Vec y)
603: {
604: PC_Telescope sred = (PC_Telescope)pc->data;
605: PetscErrorCode ierr;
606: Vec xtmp,xred,yred;
607: PetscInt i,st,ed;
608: VecScatter scatter;
609: PetscScalar *array;
610: const PetscScalar *x_array;
613: PetscCitationsRegister(citation,&cited);
615: xtmp = sred->xtmp;
616: scatter = sred->scatter;
617: xred = sred->xred;
618: yred = sred->yred;
620: /* pull in vector x->xtmp */
621: VecScatterBegin(scatter,x,xtmp,INSERT_VALUES,SCATTER_FORWARD);
622: VecScatterEnd(scatter,x,xtmp,INSERT_VALUES,SCATTER_FORWARD);
624: /* copy vector entries into xred */
625: VecGetArrayRead(xtmp,&x_array);
626: if (xred) {
627: PetscScalar *LA_xred;
628: VecGetOwnershipRange(xred,&st,&ed);
629: VecGetArray(xred,&LA_xred);
630: for (i=0; i<ed-st; i++) {
631: LA_xred[i] = x_array[i];
632: }
633: VecRestoreArray(xred,&LA_xred);
634: }
635: VecRestoreArrayRead(xtmp,&x_array);
636: /* solve */
637: if (PCTelescope_isActiveRank(sred)) {
638: KSPSolve(sred->ksp,xred,yred);
639: KSPCheckSolve(sred->ksp,pc,yred);
640: }
641: /* return vector */
642: VecGetArray(xtmp,&array);
643: if (yred) {
644: const PetscScalar *LA_yred;
645: VecGetOwnershipRange(yred,&st,&ed);
646: VecGetArrayRead(yred,&LA_yred);
647: for (i=0; i<ed-st; i++) {
648: array[i] = LA_yred[i];
649: }
650: VecRestoreArrayRead(yred,&LA_yred);
651: }
652: VecRestoreArray(xtmp,&array);
653: VecScatterBegin(scatter,xtmp,y,INSERT_VALUES,SCATTER_REVERSE);
654: VecScatterEnd(scatter,xtmp,y,INSERT_VALUES,SCATTER_REVERSE);
655: return(0);
656: }
658: 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)
659: {
660: PC_Telescope sred = (PC_Telescope)pc->data;
661: PetscErrorCode ierr;
662: Vec xtmp,yred;
663: PetscInt i,st,ed;
664: VecScatter scatter;
665: const PetscScalar *x_array;
666: PetscBool default_init_guess_value;
669: xtmp = sred->xtmp;
670: scatter = sred->scatter;
671: yred = sred->yred;
673: if (its > 1) SETERRQ(PetscObjectComm((PetscObject)pc),PETSC_ERR_SUP,"PCApplyRichardson_Telescope only supports max_it = 1");
674: *reason = (PCRichardsonConvergedReason)0;
676: if (!zeroguess) {
677: PetscInfo(pc,"PCTelescope: Scattering y for non-zero initial guess\n");
678: /* pull in vector y->xtmp */
679: VecScatterBegin(scatter,y,xtmp,INSERT_VALUES,SCATTER_FORWARD);
680: VecScatterEnd(scatter,y,xtmp,INSERT_VALUES,SCATTER_FORWARD);
682: /* copy vector entries into xred */
683: VecGetArrayRead(xtmp,&x_array);
684: if (yred) {
685: PetscScalar *LA_yred;
686: VecGetOwnershipRange(yred,&st,&ed);
687: VecGetArray(yred,&LA_yred);
688: for (i=0; i<ed-st; i++) {
689: LA_yred[i] = x_array[i];
690: }
691: VecRestoreArray(yred,&LA_yred);
692: }
693: VecRestoreArrayRead(xtmp,&x_array);
694: }
696: if (PCTelescope_isActiveRank(sred)) {
697: KSPGetInitialGuessNonzero(sred->ksp,&default_init_guess_value);
698: if (!zeroguess) KSPSetInitialGuessNonzero(sred->ksp,PETSC_TRUE);
699: }
701: PCApply_Telescope(pc,x,y);
703: if (PCTelescope_isActiveRank(sred)) {
704: KSPSetInitialGuessNonzero(sred->ksp,default_init_guess_value);
705: }
707: if (!*reason) *reason = PCRICHARDSON_CONVERGED_ITS;
708: *outits = 1;
709: return(0);
710: }
712: static PetscErrorCode PCReset_Telescope(PC pc)
713: {
714: PC_Telescope sred = (PC_Telescope)pc->data;
717: ISDestroy(&sred->isin);
718: VecScatterDestroy(&sred->scatter);
719: VecDestroy(&sred->xred);
720: VecDestroy(&sred->yred);
721: VecDestroy(&sred->xtmp);
722: MatDestroy(&sred->Bred);
723: KSPReset(sred->ksp);
724: if (sred->pctelescope_reset_type) {
725: sred->pctelescope_reset_type(pc);
726: }
727: return(0);
728: }
730: static PetscErrorCode PCDestroy_Telescope(PC pc)
731: {
732: PC_Telescope sred = (PC_Telescope)pc->data;
736: PCReset_Telescope(pc);
737: KSPDestroy(&sred->ksp);
738: PetscSubcommDestroy(&sred->psubcomm);
739: PetscFree(sred->dm_ctx);
740: PetscFree(pc->data);
741: return(0);
742: }
744: static PetscErrorCode PCSetFromOptions_Telescope(PetscOptionItems *PetscOptionsObject,PC pc)
745: {
746: PC_Telescope sred = (PC_Telescope)pc->data;
747: PetscErrorCode ierr;
748: MPI_Comm comm;
749: PetscMPIInt size;
750: PetscBool flg;
751: PetscSubcommType subcommtype;
754: PetscObjectGetComm((PetscObject)pc,&comm);
755: MPI_Comm_size(comm,&size);
756: PetscOptionsHead(PetscOptionsObject,"Telescope options");
757: PetscOptionsEnum("-pc_telescope_subcomm_type","Subcomm type (interlaced or contiguous)","PCTelescopeSetSubcommType",PetscSubcommTypes,(PetscEnum)sred->subcommtype,(PetscEnum*)&subcommtype,&flg);
758: if (flg) {
759: PCTelescopeSetSubcommType(pc,subcommtype);
760: }
761: PetscOptionsInt("-pc_telescope_reduction_factor","Factor to reduce comm size by","PCTelescopeSetReductionFactor",sred->redfactor,&sred->redfactor,0);
762: if (sred->redfactor > size) SETERRQ(comm,PETSC_ERR_ARG_WRONG,"-pc_telescope_reduction_factor <= comm size");
763: PetscOptionsBool("-pc_telescope_ignore_dm","Ignore any DM attached to the PC","PCTelescopeSetIgnoreDM",sred->ignore_dm,&sred->ignore_dm,0);
764: PetscOptionsBool("-pc_telescope_ignore_kspcomputeoperators","Ignore method used to compute A","PCTelescopeSetIgnoreKSPComputeOperators",sred->ignore_kspcomputeoperators,&sred->ignore_kspcomputeoperators,0);
765: PetscOptionsBool("-pc_telescope_use_coarse_dm","Define sub-communicator from the coarse DM","PCTelescopeSetUseCoarseDM",sred->use_coarse_dm,&sred->use_coarse_dm,0);
766: PetscOptionsTail();
767: return(0);
768: }
770: /* PC simplementation specific API's */
772: static PetscErrorCode PCTelescopeGetKSP_Telescope(PC pc,KSP *ksp)
773: {
774: PC_Telescope red = (PC_Telescope)pc->data;
776: if (ksp) *ksp = red->ksp;
777: return(0);
778: }
780: static PetscErrorCode PCTelescopeGetSubcommType_Telescope(PC pc,PetscSubcommType *subcommtype)
781: {
782: PC_Telescope red = (PC_Telescope)pc->data;
784: if (subcommtype) *subcommtype = red->subcommtype;
785: return(0);
786: }
788: static PetscErrorCode PCTelescopeSetSubcommType_Telescope(PC pc,PetscSubcommType subcommtype)
789: {
790: PC_Telescope red = (PC_Telescope)pc->data;
793: 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.");
794: red->subcommtype = subcommtype;
795: return(0);
796: }
798: static PetscErrorCode PCTelescopeGetReductionFactor_Telescope(PC pc,PetscInt *fact)
799: {
800: PC_Telescope red = (PC_Telescope)pc->data;
802: if (fact) *fact = red->redfactor;
803: return(0);
804: }
806: static PetscErrorCode PCTelescopeSetReductionFactor_Telescope(PC pc,PetscInt fact)
807: {
808: PC_Telescope red = (PC_Telescope)pc->data;
809: PetscMPIInt size;
810: PetscErrorCode ierr;
813: MPI_Comm_size(PetscObjectComm((PetscObject)pc),&size);
814: if (fact <= 0) SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONG,"Reduction factor of telescoping PC %D must be positive",fact);
815: if (fact > size) SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_WRONG,"Reduction factor of telescoping PC %D must be <= comm.size",fact);
816: red->redfactor = fact;
817: return(0);
818: }
820: static PetscErrorCode PCTelescopeGetIgnoreDM_Telescope(PC pc,PetscBool *v)
821: {
822: PC_Telescope red = (PC_Telescope)pc->data;
824: if (v) *v = red->ignore_dm;
825: return(0);
826: }
828: static PetscErrorCode PCTelescopeSetIgnoreDM_Telescope(PC pc,PetscBool v)
829: {
830: PC_Telescope red = (PC_Telescope)pc->data;
832: red->ignore_dm = v;
833: return(0);
834: }
836: static PetscErrorCode PCTelescopeGetUseCoarseDM_Telescope(PC pc,PetscBool *v)
837: {
838: PC_Telescope red = (PC_Telescope)pc->data;
840: if (v) *v = red->use_coarse_dm;
841: return(0);
842: }
844: static PetscErrorCode PCTelescopeSetUseCoarseDM_Telescope(PC pc,PetscBool v)
845: {
846: PC_Telescope red = (PC_Telescope)pc->data;
848: red->use_coarse_dm = v;
849: return(0);
850: }
852: static PetscErrorCode PCTelescopeGetIgnoreKSPComputeOperators_Telescope(PC pc,PetscBool *v)
853: {
854: PC_Telescope red = (PC_Telescope)pc->data;
856: if (v) *v = red->ignore_kspcomputeoperators;
857: return(0);
858: }
860: static PetscErrorCode PCTelescopeSetIgnoreKSPComputeOperators_Telescope(PC pc,PetscBool v)
861: {
862: PC_Telescope red = (PC_Telescope)pc->data;
864: red->ignore_kspcomputeoperators = v;
865: return(0);
866: }
868: static PetscErrorCode PCTelescopeGetDM_Telescope(PC pc,DM *dm)
869: {
870: PC_Telescope red = (PC_Telescope)pc->data;
872: *dm = private_PCTelescopeGetSubDM(red);
873: return(0);
874: }
876: /*@
877: PCTelescopeGetKSP - Gets the KSP created by the telescoping PC.
879: Not Collective
881: Input Parameter:
882: . pc - the preconditioner context
884: Output Parameter:
885: . subksp - the KSP defined the smaller set of processes
887: Level: advanced
889: .keywords: PC, telescopting solve
890: @*/
891: PetscErrorCode PCTelescopeGetKSP(PC pc,KSP *subksp)
892: {
895: PetscUseMethod(pc,"PCTelescopeGetKSP_C",(PC,KSP*),(pc,subksp));
896: return(0);
897: }
899: /*@
900: PCTelescopeGetReductionFactor - Gets the factor by which the original number of processes has been reduced by.
902: Not Collective
904: Input Parameter:
905: . pc - the preconditioner context
907: Output Parameter:
908: . fact - the reduction factor
910: Level: advanced
912: .keywords: PC, telescoping solve
913: @*/
914: PetscErrorCode PCTelescopeGetReductionFactor(PC pc,PetscInt *fact)
915: {
918: PetscUseMethod(pc,"PCTelescopeGetReductionFactor_C",(PC,PetscInt*),(pc,fact));
919: return(0);
920: }
922: /*@
923: PCTelescopeSetReductionFactor - Sets the factor by which the original number of processes has been reduced by.
925: Not Collective
927: Input Parameter:
928: . pc - the preconditioner context
930: Output Parameter:
931: . fact - the reduction factor
933: Level: advanced
935: .keywords: PC, telescoping solve
936: @*/
937: PetscErrorCode PCTelescopeSetReductionFactor(PC pc,PetscInt fact)
938: {
941: PetscTryMethod(pc,"PCTelescopeSetReductionFactor_C",(PC,PetscInt),(pc,fact));
942: return(0);
943: }
945: /*@
946: PCTelescopeGetIgnoreDM - Get the flag indicating if any DM attached to the PC will be used.
948: Not Collective
950: Input Parameter:
951: . pc - the preconditioner context
953: Output Parameter:
954: . v - the flag
956: Level: advanced
958: .keywords: PC, telescoping solve
959: @*/
960: PetscErrorCode PCTelescopeGetIgnoreDM(PC pc,PetscBool *v)
961: {
964: PetscUseMethod(pc,"PCTelescopeGetIgnoreDM_C",(PC,PetscBool*),(pc,v));
965: return(0);
966: }
968: /*@
969: PCTelescopeSetIgnoreDM - Set a flag to ignore any DM attached to the PC.
971: Not Collective
973: Input Parameter:
974: . pc - the preconditioner context
976: Output Parameter:
977: . v - Use PETSC_TRUE to ignore any DM
979: Level: advanced
981: .keywords: PC, telescoping solve
982: @*/
983: PetscErrorCode PCTelescopeSetIgnoreDM(PC pc,PetscBool v)
984: {
987: PetscTryMethod(pc,"PCTelescopeSetIgnoreDM_C",(PC,PetscBool),(pc,v));
988: return(0);
989: }
991: /*@
992: PCTelescopeGetUseCoarseDM - Get the flag indicating if the coarse DM attached to DM associated with the PC will be used.
994: Not Collective
996: Input Parameter:
997: . pc - the preconditioner context
999: Output Parameter:
1000: . v - the flag
1002: Level: advanced
1004: .keywords: PC, telescoping solve
1005: @*/
1006: PetscErrorCode PCTelescopeGetUseCoarseDM(PC pc,PetscBool *v)
1007: {
1010: PetscUseMethod(pc,"PCTelescopeGetUseCoarseDM_C",(PC,PetscBool*),(pc,v));
1011: return(0);
1012: }
1014: /*@
1015: PCTelescopeSetUseCoarseDM - Set a flag to query the DM attached to the PC if it also has a coarse DM
1017: Not Collective
1019: Input Parameter:
1020: . pc - the preconditioner context
1022: Output Parameter:
1023: . v - Use PETSC_FALSE to ignore any coarse DM
1025: Notes:
1026: When you have specified to use a coarse DM, the communicator used to create the sub-KSP within PCTelescope
1027: will be that of the coarse DM. Hence the flags -pc_telescope_reduction_factor and
1028: -pc_telescope_subcomm_type will no longer have any meaning.
1029: It is required that the communicator associated with the parent (fine) and the coarse DM are of different sizes.
1030: An error will occur of the size of the communicator associated with the coarse DM
1031: is the same as that of the parent DM.
1032: Furthermore, it is required that the communicator on the coarse DM is a sub-communicator of the parent.
1033: This will be checked at the time the preconditioner is setup and an error will occur if
1034: the coarse DM does not define a sub-communicator of that used by the parent DM.
1036: The particular Telescope setup invoked when using a coarse DM is agnostic with respect to the type of
1037: the DM used (e.g. it supports DMSHELL, DMPLEX, etc).
1039: Support is currently only provided for the case when you are using KSPSetComputeOperators()
1041: 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.
1042: In the user code, this is achieved via
1043: .vb
1044: {
1045: DM dm_fine;
1046: PetscObjectCompose((PetscObject)dm_fine,"PCTelescopeFieldScatter",your_field_scatter_method);
1047: }
1048: .ve
1049: The signature of the user provided field scatter method is
1050: .vb
1051: PetscErrorCode your_field_scatter_method(DM dm_fine,Vec x_fine,ScatterMode mode,DM dm_coarse,Vec x_coarse);
1052: .ve
1053: The user must provide support for both mode = SCATTER_FORWARD and mode = SCATTER_REVERSE.
1054: SCATTER_FORWARD implies the direction of transfer is from the parent (fine) DM to the coarse DM.
1056: Optionally, the user may also compose a function with the parent DM to facilitate the transfer
1057: of state variables between the fine and coarse DMs.
1058: In the context of a finite element discretization, an example state variable might be
1059: values associated with quadrature points within each element.
1060: A user provided state scatter method is composed via
1061: .vb
1062: {
1063: DM dm_fine;
1064: PetscObjectCompose((PetscObject)dm_fine,"PCTelescopeStateScatter",your_state_scatter_method);
1065: }
1066: .ve
1067: The signature of the user provided state scatter method is
1068: .vb
1069: PetscErrorCode your_state_scatter_method(DM dm_fine,ScatterMode mode,DM dm_coarse);
1070: .ve
1071: SCATTER_FORWARD implies the direction of transfer is from the fine DM to the coarse DM.
1072: The user is only required to support mode = SCATTER_FORWARD.
1073: No assumption is made about the data type of the state variables.
1074: These must be managed by the user and must be accessible from the DM.
1076: Care must be taken in defining the user context passed to KSPSetComputeOperators() which is to be
1077: associated with the sub-KSP residing within PCTelescope.
1078: In general, PCTelescope assumes that the context on the fine and coarse DM used with
1079: KSPSetComputeOperators() should be "similar" in type or origin.
1080: Specifically the following rules are used to infer what context on the sub-KSP should be.
1082: First the contexts from the KSP and the fine and coarse DMs are retrieved.
1083: Note that the special case of a DMSHELL context is queried.
1085: .vb
1086: DMKSPGetComputeOperators(dm_fine,&dmfine_kspfunc,&dmfine_kspctx);
1087: DMGetApplicationContext(dm_fine,&dmfine_appctx);
1088: DMShellGetContext(dm_fine,&dmfine_shellctx);
1090: DMGetApplicationContext(dm_coarse,&dmcoarse_appctx);
1091: DMShellGetContext(dm_coarse,&dmcoarse_shellctx);
1092: .ve
1094: The following rules are then enforced:
1096: 1. If dmfine_kspctx = NULL, then we provide a NULL pointer as the context for the sub-KSP:
1097: KSPSetComputeOperators(sub_ksp,dmfine_kspfunc,NULL);
1099: 2. If dmfine_kspctx != NULL and dmfine_kspctx == dmfine_appctx,
1100: check that dmcoarse_appctx is also non-NULL. If this is true, then:
1101: KSPSetComputeOperators(sub_ksp,dmfine_kspfunc,dmcoarse_appctx);
1103: 3. If dmfine_kspctx != NULL and dmfine_kspctx == dmfine_shellctx,
1104: check that dmcoarse_shellctx is also non-NULL. If this is true, then:
1105: KSPSetComputeOperators(sub_ksp,dmfine_kspfunc,dmcoarse_shellctx);
1107: If neither of the above three tests passed, then PCTelescope cannot safely determine what
1108: context should be provided to KSPSetComputeOperators() for use with the sub-KSP.
1109: In this case, an additional mechanism is provided via a composed function which will return
1110: the actual context to be used. To use this feature you must compose the "getter" function
1111: with the coarse DM, e.g.
1112: .vb
1113: {
1114: DM dm_coarse;
1115: PetscObjectCompose((PetscObject)dm_coarse,"PCTelescopeGetCoarseDMKSPContext",your_coarse_context_getter);
1116: }
1117: .ve
1118: The signature of the user provided method is
1119: .vb
1120: PetscErrorCode your_coarse_context_getter(DM dm_coarse,void **your_kspcontext);
1121: .ve
1123: Level: advanced
1125: .keywords: PC, telescoping solve
1126: @*/
1127: PetscErrorCode PCTelescopeSetUseCoarseDM(PC pc,PetscBool v)
1128: {
1131: PetscTryMethod(pc,"PCTelescopeSetUseCoarseDM_C",(PC,PetscBool),(pc,v));
1132: return(0);
1133: }
1135: /*@
1136: PCTelescopeGetIgnoreKSPComputeOperators - Get the flag indicating if KSPComputeOperators will be used.
1138: Not Collective
1140: Input Parameter:
1141: . pc - the preconditioner context
1143: Output Parameter:
1144: . v - the flag
1146: Level: advanced
1148: .keywords: PC, telescoping solve
1149: @*/
1150: PetscErrorCode PCTelescopeGetIgnoreKSPComputeOperators(PC pc,PetscBool *v)
1151: {
1154: PetscUseMethod(pc,"PCTelescopeGetIgnoreKSPComputeOperators_C",(PC,PetscBool*),(pc,v));
1155: return(0);
1156: }
1158: /*@
1159: PCTelescopeSetIgnoreKSPComputeOperators - Set a flag to ignore KSPComputeOperators.
1161: Not Collective
1163: Input Parameter:
1164: . pc - the preconditioner context
1166: Output Parameter:
1167: . v - Use PETSC_TRUE to ignore the method (if defined) set via KSPSetComputeOperators on pc
1169: Level: advanced
1171: .keywords: PC, telescoping solve
1172: @*/
1173: PetscErrorCode PCTelescopeSetIgnoreKSPComputeOperators(PC pc,PetscBool v)
1174: {
1177: PetscTryMethod(pc,"PCTelescopeSetIgnoreKSPComputeOperators_C",(PC,PetscBool),(pc,v));
1178: return(0);
1179: }
1181: /*@
1182: PCTelescopeGetDM - Get the re-partitioned DM attached to the sub KSP.
1184: Not Collective
1186: Input Parameter:
1187: . pc - the preconditioner context
1189: Output Parameter:
1190: . subdm - The re-partitioned DM
1192: Level: advanced
1194: .keywords: PC, telescoping solve
1195: @*/
1196: PetscErrorCode PCTelescopeGetDM(PC pc,DM *subdm)
1197: {
1200: PetscUseMethod(pc,"PCTelescopeGetDM_C",(PC,DM*),(pc,subdm));
1201: return(0);
1202: }
1204: /*@
1205: PCTelescopeSetSubcommType - set subcommunicator type (interlaced or contiguous)
1207: Logically Collective
1209: Input Parameter:
1210: + pc - the preconditioner context
1211: - subcommtype - the subcommunicator type (see PetscSubcommType)
1213: Level: advanced
1215: .keywords: PC, telescoping solve
1217: .seealso: PetscSubcommType, PetscSubcomm, PCTELESCOPE
1218: @*/
1219: PetscErrorCode PCTelescopeSetSubcommType(PC pc, PetscSubcommType subcommtype)
1220: {
1223: PetscTryMethod(pc,"PCTelescopeSetSubcommType_C",(PC,PetscSubcommType),(pc,subcommtype));
1224: return(0);
1225: }
1227: /*@
1228: PCTelescopeGetSubcommType - Get the subcommunicator type (interlaced or contiguous)
1230: Not Collective
1232: Input Parameter:
1233: . pc - the preconditioner context
1235: Output Parameter:
1236: . subcommtype - the subcommunicator type (see PetscSubcommType)
1238: Level: advanced
1240: .keywords: PC, telescoping solve
1242: .seealso: PetscSubcomm, PetscSubcommType, PCTELESCOPE
1243: @*/
1244: PetscErrorCode PCTelescopeGetSubcommType(PC pc, PetscSubcommType *subcommtype)
1245: {
1248: PetscUseMethod(pc,"PCTelescopeGetSubcommType_C",(PC,PetscSubcommType*),(pc,subcommtype));
1249: return(0);
1250: }
1252: /* -------------------------------------------------------------------------------------*/
1253: /*MC
1254: PCTELESCOPE - Runs a KSP solver on a sub-communicator. MPI ranks not in the sub-communicator are idle during the solve.
1256: Options Database:
1257: + -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.
1258: . -pc_telescope_ignore_dm - flag to indicate whether an attached DM should be ignored.
1259: . -pc_telescope_subcomm_type <interlaced,contiguous> - defines the selection of MPI ranks on the sub-communicator. see PetscSubcomm for more information.
1260: . -pc_telescope_ignore_kspcomputeoperators - flag to indicate whether KSPSetComputeOperators should be used on the sub-KSP.
1261: - -pc_telescope_use_coarse_dm - flag to indicate whether the coarse DM should be used to define the sub-communicator.
1263: Level: advanced
1265: Notes:
1266: Assuming that the parent preconditioner (PC) is defined on a communicator c, this implementation
1267: creates a child sub-communicator (c') containing fewer MPI ranks than the original parent preconditioner (PC).
1268: The preconditioner is deemed telescopic as it only calls KSPSolve() on a single
1269: sub-communicator, in contrast with PCREDUNDANT which calls KSPSolve() on N sub-communicators.
1270: This means there will be MPI ranks which will be idle during the application of this preconditioner.
1271: Additionally, in comparison with PCREDUNDANT, PCTELESCOPE can utilize an attached DM.
1273: The default type of the sub KSP (the KSP defined on c') is PREONLY.
1275: There are three setup mechanisms for PCTelescope. Features support by each type are described below.
1276: In the following, we will refer to the operators B and B', these are the Bmat provided to the KSP on the
1277: communicators c and c' respectively.
1279: [1] Default setup
1280: The sub-communicator c' is created via PetscSubcommCreate().
1281: Explicitly defined nullspace and near nullspace vectors will be propogated from B to B'.
1282: Currently there is no support define nullspaces via a user supplied method (e.g. as passed to MatNullSpaceSetFunction()).
1283: No support is provided for KSPSetComputeOperators().
1284: Currently there is no support for the flag -pc_use_amat.
1286: [2] DM aware setup
1287: If a DM is attached to the PC, it is re-partitioned on the sub-communicator c'.
1288: c' is created via PetscSubcommCreate().
1289: Both the Bmat operator and the right hand side vector are permuted into the new DOF ordering defined by the re-partitioned DM.
1290: Currently only support for re-partitioning a DMDA is provided.
1291: 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').
1292: Currently there is no support define nullspaces via a user supplied method (e.g. as passed to MatNullSpaceSetFunction()).
1293: Support is provided for KSPSetComputeOperators(). The user provided function and context is propagated to the sub KSP.
1294: This is fragile since the user must ensure that their user context is valid for use on c'.
1295: Currently there is no support for the flag -pc_use_amat.
1297: [3] Coarse DM setup
1298: If a DM (dmfine) is attached to the PC, dmfine is queried for a "coarse" DM (call this dmcoarse) via DMGetCoarseDM().
1299: PCTELESCOPE will interpret the coarse DM as being defined on a sub-communicator of c.
1300: The communicator associated with dmcoarse will define the c' to be used within PCTELESCOPE.
1301: PCTELESCOPE will check that c' is in fact a sub-communicator of c. If it is not, an error will be reported.
1302: The intention of this setup type is that PCTELESCOPE will use an existing (e.g. user defined) communicator hierarchy, say as would be
1303: available with using multi-grid on unstructured meshes.
1304: This setup will not use the command line options -pc_telescope_reduction_factor or -pc_telescope_subcomm_type.
1305: 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'.
1306: Currently there is no support define nullspaces via a user supplied method (e.g. as passed to MatNullSpaceSetFunction()).
1307: There is no general method to permute field orderings, hence only KSPSetComputeOperators() is supported.
1308: The user must use PetscObjectComposeFunction() with dmfine to define the method to scatter fields from dmfine to dmcoarse.
1309: Propogation 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().
1310: Currently there is no support for the flag -pc_use_amat.
1311: This setup can be invoked by the option -pc_telescope_use_coarse_dm or by calling PCTelescopeSetUseCoarseDM(pc,PETSC_TRUE);
1312: Further information about the user-provided methods required by this setup type are described here PCTelescopeSetUseCoarseDM().
1314: Developer Notes:
1315: During PCSetup, the B operator is scattered onto c'.
1316: Within PCApply, the RHS vector (x) is scattered into a redundant vector, xred (defined on c').
1317: Then, KSPSolve() is executed on the c' communicator.
1319: The communicator used within the telescoping preconditioner is defined by a PetscSubcomm using the INTERLACED
1320: 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.
1322: The telescoping preconditioner is aware of nullspaces and near nullspaces which are attached to the B operator.
1323: In the case where B has a (near) nullspace attached, the (near) nullspace vectors are extracted from B and mapped into
1324: a new (near) nullspace, defined on the sub-communicator, which is attached to B' (the B operator which was scattered to c')
1326: The telescoping preconditioner can re-partition an attached DM if it is a DMDA (2D or 3D -
1327: support for 1D DMDAs is not provided). If a DMDA is found, a topolgically equivalent DMDA is created on c'
1328: and this new DM is attached the sub KSP. The design of telescope is such that it should be possible to extend support
1329: for re-partitioning other to DM's (e.g. DMPLEX). The user can supply a flag to ignore attached DMs.
1330: Alternatively, user-provided re-partitioned DMs can be used via -pc_telescope_use_coarse_dm.
1332: With the default setup mode, B' is defined by fusing rows (in order) associated with MPI ranks common to c and c'.
1334: When a DMDA is attached to the parent preconditioner, B' is defined by: (i) performing a symmetric permutation of B
1335: into the ordering defined by the DMDA on c', (ii) extracting the local chunks via MatCreateSubMatrices(), (iii) fusing the
1336: locally (sequential) matrices defined on the ranks common to c and c' into B' using MatCreateMPIMatConcatenateSeqMat()
1338: Limitations/improvements include the following.
1339: VecPlaceArray() could be used within PCApply() to improve efficiency and reduce memory usage.
1340: A unified mechanism to query for user contexts as required by KSPSetComputeOperators() and MatNullSpaceSetFunction().
1342: The symmetric permutation used when a DMDA is encountered is performed via explicitly assmbleming a permutation matrix P,
1343: 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
1344: VecPermute() does not supported for the use case required here. By computing P, one can permute both the operator and RHS in a
1345: consistent manner.
1347: Mapping of vectors (default setup mode) is performed in the following way.
1348: 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.
1349: Using the interlaced creation routine, the ranks in c with color = 0 will be rank 0 and 2.
1350: We perform the scatter to the sub-communicator in the following way.
1351: [1] Given a vector x defined on communicator c
1353: .vb
1354: rank(c) local values of x
1355: ------- ----------------------------------------
1356: 0 [ 0.0, 1.0, 2.0, 3.0, 4.0, 5.0 ]
1357: 1 [ 6.0, 7.0, 8.0, 9.0, 10.0, 11.0 ]
1358: 2 [ 12.0, 13.0, 14.0, 15.0, 16.0, 17.0 ]
1359: 3 [ 18.0, 19.0, 20.0, 21.0, 22.0, 23.0 ]
1360: .ve
1362: scatter into xtmp defined also on comm c, so that we have the following values
1364: .vb
1365: rank(c) local values of xtmp
1366: ------- ----------------------------------------------------------------------------
1367: 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 ]
1368: 1 [ ]
1369: 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 ]
1370: 3 [ ]
1371: .ve
1373: The entries on rank 1 and 3 (ranks which do not have a color = 0 in c') have no values
1376: [2] Copy the values from ranks 0, 2 (indices with respect to comm c) into the vector xred which is defined on communicator c'.
1377: Ranks 0 and 2 are the only ranks in the subcomm which have a color = 0.
1379: .vb
1380: rank(c') local values of xred
1381: -------- ----------------------------------------------------------------------------
1382: 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 ]
1383: 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 ]
1384: .ve
1386: Contributed by Dave May
1388: Reference:
1389: 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
1391: .seealso: PCTelescopeGetKSP(), PCTelescopeGetDM(), PCTelescopeGetReductionFactor(), PCTelescopeSetReductionFactor(), PCTelescopeGetIgnoreDM(), PCTelescopeSetIgnoreDM(), PCREDUNDANT
1392: M*/
1393: PETSC_EXTERN PetscErrorCode PCCreate_Telescope(PC pc)
1394: {
1395: PetscErrorCode ierr;
1396: struct _PC_Telescope *sred;
1399: PetscNewLog(pc,&sred);
1400: sred->psubcomm = NULL;
1401: sred->subcommtype = PETSC_SUBCOMM_INTERLACED;
1402: sred->subcomm = MPI_COMM_NULL;
1403: sred->redfactor = 1;
1404: sred->ignore_dm = PETSC_FALSE;
1405: sred->ignore_kspcomputeoperators = PETSC_FALSE;
1406: sred->use_coarse_dm = PETSC_FALSE;
1407: pc->data = (void*)sred;
1409: pc->ops->apply = PCApply_Telescope;
1410: pc->ops->applytranspose = NULL;
1411: pc->ops->applyrichardson = PCApplyRichardson_Telescope;
1412: pc->ops->setup = PCSetUp_Telescope;
1413: pc->ops->destroy = PCDestroy_Telescope;
1414: pc->ops->reset = PCReset_Telescope;
1415: pc->ops->setfromoptions = PCSetFromOptions_Telescope;
1416: pc->ops->view = PCView_Telescope;
1418: sred->pctelescope_setup_type = PCTelescopeSetUp_default;
1419: sred->pctelescope_matcreate_type = PCTelescopeMatCreate_default;
1420: sred->pctelescope_matnullspacecreate_type = PCTelescopeMatNullSpaceCreate_default;
1421: sred->pctelescope_reset_type = NULL;
1423: PetscObjectComposeFunction((PetscObject)pc,"PCTelescopeGetKSP_C",PCTelescopeGetKSP_Telescope);
1424: PetscObjectComposeFunction((PetscObject)pc,"PCTelescopeGetSubcommType_C",PCTelescopeGetSubcommType_Telescope);
1425: PetscObjectComposeFunction((PetscObject)pc,"PCTelescopeSetSubcommType_C",PCTelescopeSetSubcommType_Telescope);
1426: PetscObjectComposeFunction((PetscObject)pc,"PCTelescopeGetReductionFactor_C",PCTelescopeGetReductionFactor_Telescope);
1427: PetscObjectComposeFunction((PetscObject)pc,"PCTelescopeSetReductionFactor_C",PCTelescopeSetReductionFactor_Telescope);
1428: PetscObjectComposeFunction((PetscObject)pc,"PCTelescopeGetIgnoreDM_C",PCTelescopeGetIgnoreDM_Telescope);
1429: PetscObjectComposeFunction((PetscObject)pc,"PCTelescopeSetIgnoreDM_C",PCTelescopeSetIgnoreDM_Telescope);
1430: PetscObjectComposeFunction((PetscObject)pc,"PCTelescopeGetIgnoreKSPComputeOperators_C",PCTelescopeGetIgnoreKSPComputeOperators_Telescope);
1431: PetscObjectComposeFunction((PetscObject)pc,"PCTelescopeSetIgnoreKSPComputeOperators_C",PCTelescopeSetIgnoreKSPComputeOperators_Telescope);
1432: PetscObjectComposeFunction((PetscObject)pc,"PCTelescopeGetDM_C",PCTelescopeGetDM_Telescope);
1433: PetscObjectComposeFunction((PetscObject)pc,"PCTelescopeGetUseCoarseDM_C",PCTelescopeGetUseCoarseDM_Telescope);
1434: PetscObjectComposeFunction((PetscObject)pc,"PCTelescopeSetUseCoarseDM_C",PCTelescopeSetUseCoarseDM_Telescope);
1435: return(0);
1436: }