Actual source code: sf.c
petsc-3.12.0 2019-09-29
1: #include <petsc/private/sfimpl.h>
2: #include <petsc/private/hashseti.h>
3: #include <petscctable.h>
5: #if defined(PETSC_USE_DEBUG)
6: # define PetscSFCheckGraphSet(sf,arg) do { \
7: if (PetscUnlikely(!(sf)->graphset)) \
8: SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call PetscSFSetGraph() or PetscSFSetGraphWithPattern() on argument %D \"%s\" before %s()",(arg),#sf,PETSC_FUNCTION_NAME); \
9: } while (0)
10: #else
11: # define PetscSFCheckGraphSet(sf,arg) do {} while (0)
12: #endif
14: const char *const PetscSFDuplicateOptions[] = {"CONFONLY","RANKS","GRAPH","PetscSFDuplicateOption","PETSCSF_DUPLICATE_",0};
16: /*@
17: PetscSFCreate - create a star forest communication context
19: Collective
21: Input Arguments:
22: . comm - communicator on which the star forest will operate
24: Output Arguments:
25: . sf - new star forest context
27: Options Database Keys:
28: + -sf_type basic -Use MPI persistent Isend/Irecv for communication (Default)
29: . -sf_type window -Use MPI-3 one-sided window for communication
30: - -sf_type neighbor -Use MPI-3 neighborhood collectives for communication
32: Level: intermediate
34: Notes:
35: When one knows the communication graph is one of the predefined graph, such as MPI_Alltoall, MPI_Allgatherv,
36: MPI_Gatherv, one can create a PetscSF and then set its graph with PetscSFSetGraphWithPattern(). These special
37: SFs are optimized and they have better performance than general SFs.
39: .seealso: PetscSFSetGraph(), PetscSFSetGraphWithPattern(), PetscSFDestroy()
40: @*/
41: PetscErrorCode PetscSFCreate(MPI_Comm comm,PetscSF *sf)
42: {
44: PetscSF b;
48: PetscSFInitializePackage();
50: PetscHeaderCreate(b,PETSCSF_CLASSID,"PetscSF","Star Forest","PetscSF",comm,PetscSFDestroy,PetscSFView);
52: b->nroots = -1;
53: b->nleaves = -1;
54: b->minleaf = PETSC_MAX_INT;
55: b->maxleaf = PETSC_MIN_INT;
56: b->nranks = -1;
57: b->rankorder = PETSC_TRUE;
58: b->ingroup = MPI_GROUP_NULL;
59: b->outgroup = MPI_GROUP_NULL;
60: b->graphset = PETSC_FALSE;
62: *sf = b;
63: return(0);
64: }
66: /*@
67: PetscSFReset - Reset a star forest so that different sizes or neighbors can be used
69: Collective
71: Input Arguments:
72: . sf - star forest
74: Level: advanced
76: .seealso: PetscSFCreate(), PetscSFSetGraph(), PetscSFDestroy()
77: @*/
78: PetscErrorCode PetscSFReset(PetscSF sf)
79: {
84: if (sf->ops->Reset) {(*sf->ops->Reset)(sf);}
85: sf->nroots = -1;
86: sf->nleaves = -1;
87: sf->minleaf = PETSC_MAX_INT;
88: sf->maxleaf = PETSC_MIN_INT;
89: sf->mine = NULL;
90: sf->remote = NULL;
91: sf->graphset = PETSC_FALSE;
92: PetscFree(sf->mine_alloc);
93: PetscFree(sf->remote_alloc);
94: sf->nranks = -1;
95: PetscFree4(sf->ranks,sf->roffset,sf->rmine,sf->rremote);
96: #if defined(PETSC_HAVE_CUDA)
97: if (sf->rmine_d) {cudaError_t err = cudaFree(sf->rmine_d);CHKERRCUDA(err);sf->rmine_d=NULL;}
98: #endif
99: sf->degreeknown = PETSC_FALSE;
100: PetscFree(sf->degree);
101: if (sf->ingroup != MPI_GROUP_NULL) {MPI_Group_free(&sf->ingroup);}
102: if (sf->outgroup != MPI_GROUP_NULL) {MPI_Group_free(&sf->outgroup);}
103: PetscSFDestroy(&sf->multi);
104: PetscLayoutDestroy(&sf->map);
105: sf->setupcalled = PETSC_FALSE;
106: return(0);
107: }
109: /*@C
110: PetscSFSetType - Set the PetscSF communication implementation
112: Collective on PetscSF
114: Input Parameters:
115: + sf - the PetscSF context
116: - type - a known method
118: Options Database Key:
119: . -sf_type <type> - Sets the method; use -help for a list
120: of available methods (for instance, window, pt2pt, neighbor)
122: Notes:
123: See "include/petscsf.h" for available methods (for instance)
124: + PETSCSFWINDOW - MPI-2/3 one-sided
125: - PETSCSFBASIC - basic implementation using MPI-1 two-sided
127: Level: intermediate
129: .seealso: PetscSFType, PetscSFCreate()
130: @*/
131: PetscErrorCode PetscSFSetType(PetscSF sf,PetscSFType type)
132: {
133: PetscErrorCode ierr,(*r)(PetscSF);
134: PetscBool match;
140: PetscObjectTypeCompare((PetscObject)sf,type,&match);
141: if (match) return(0);
143: PetscFunctionListFind(PetscSFList,type,&r);
144: if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested PetscSF type %s",type);
145: /* Destroy the previous PetscSF implementation context */
146: if (sf->ops->Destroy) {(*(sf)->ops->Destroy)(sf);}
147: PetscMemzero(sf->ops,sizeof(*sf->ops));
148: PetscObjectChangeTypeName((PetscObject)sf,type);
149: (*r)(sf);
150: return(0);
151: }
153: /*@C
154: PetscSFGetType - Get the PetscSF communication implementation
156: Not Collective
158: Input Parameter:
159: . sf - the PetscSF context
161: Output Parameter:
162: . type - the PetscSF type name
164: Level: intermediate
166: .seealso: PetscSFSetType(), PetscSFCreate()
167: @*/
168: PetscErrorCode PetscSFGetType(PetscSF sf, PetscSFType *type)
169: {
173: *type = ((PetscObject)sf)->type_name;
174: return(0);
175: }
177: /*@
178: PetscSFDestroy - destroy star forest
180: Collective
182: Input Arguments:
183: . sf - address of star forest
185: Level: intermediate
187: .seealso: PetscSFCreate(), PetscSFReset()
188: @*/
189: PetscErrorCode PetscSFDestroy(PetscSF *sf)
190: {
194: if (!*sf) return(0);
196: if (--((PetscObject)(*sf))->refct > 0) {*sf = NULL; return(0);}
197: PetscSFReset(*sf);
198: if ((*sf)->ops->Destroy) {(*(*sf)->ops->Destroy)(*sf);}
199: PetscHeaderDestroy(sf);
200: return(0);
201: }
203: static PetscErrorCode PetscSFCheckGraphValid_Private(PetscSF sf)
204: {
205: #if defined(PETSC_USE_DEBUG)
206: PetscInt i, nleaves;
207: PetscMPIInt size;
208: const PetscInt *ilocal;
209: const PetscSFNode *iremote;
210: PetscErrorCode ierr;
213: if (!sf->graphset) return(0);
214: PetscSFGetGraph(sf,NULL,&nleaves,&ilocal,&iremote);
215: MPI_Comm_size(PetscObjectComm((PetscObject)sf),&size);
216: for (i = 0; i < nleaves; i++) {
217: const PetscInt rank = iremote[i].rank;
218: const PetscInt remote = iremote[i].index;
219: const PetscInt leaf = ilocal ? ilocal[i] : i;
220: if (rank < 0 || rank >= size) SETERRQ3(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Provided rank (%D) for remote %D is invalid, should be in [0, %d)",rank,i,size);
221: if (remote < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Provided index (%D) for remote %D is invalid, should be >= 0",remote,i);
222: if (leaf < 0) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Provided location (%D) for leaf %D is invalid, should be >= 0",leaf,i);
223: }
224: return(0);
225: #else
227: return(0);
228: #endif
229: }
231: /*@
232: PetscSFSetUp - set up communication structures
234: Collective
236: Input Arguments:
237: . sf - star forest communication object
239: Level: beginner
241: .seealso: PetscSFSetFromOptions(), PetscSFSetType()
242: @*/
243: PetscErrorCode PetscSFSetUp(PetscSF sf)
244: {
249: PetscSFCheckGraphSet(sf,1);
250: if (sf->setupcalled) return(0);
251: PetscSFCheckGraphValid_Private(sf);
252: if (!((PetscObject)sf)->type_name) {PetscSFSetType(sf,PETSCSFBASIC);}
253: PetscLogEventBegin(PETSCSF_SetUp,sf,0,0,0);
254: if (sf->ops->SetUp) {(*sf->ops->SetUp)(sf);}
255: PetscLogEventEnd(PETSCSF_SetUp,sf,0,0,0);
256: sf->setupcalled = PETSC_TRUE;
257: return(0);
258: }
260: /*@
261: PetscSFSetFromOptions - set PetscSF options using the options database
263: Logically Collective
265: Input Arguments:
266: . sf - star forest
268: Options Database Keys:
269: + -sf_type - implementation type, see PetscSFSetType()
270: - -sf_rank_order - sort composite points for gathers and scatters in rank order, gathers are non-deterministic otherwise
272: Level: intermediate
274: .seealso: PetscSFWindowSetSyncType()
275: @*/
276: PetscErrorCode PetscSFSetFromOptions(PetscSF sf)
277: {
278: PetscSFType deft;
279: char type[256];
281: PetscBool flg;
285: PetscObjectOptionsBegin((PetscObject)sf);
286: deft = ((PetscObject)sf)->type_name ? ((PetscObject)sf)->type_name : PETSCSFBASIC;
287: PetscOptionsFList("-sf_type","PetscSF implementation type","PetscSFSetType",PetscSFList,deft,type,sizeof(type),&flg);
288: PetscSFSetType(sf,flg ? type : deft);
289: PetscOptionsBool("-sf_rank_order","sort composite points for gathers and scatters in rank order, gathers are non-deterministic otherwise","PetscSFSetRankOrder",sf->rankorder,&sf->rankorder,NULL);
290: if (sf->ops->SetFromOptions) {(*sf->ops->SetFromOptions)(PetscOptionsObject,sf);}
291: PetscOptionsEnd();
292: return(0);
293: }
295: /*@
296: PetscSFSetRankOrder - sort multi-points for gathers and scatters by rank order
298: Logically Collective
300: Input Arguments:
301: + sf - star forest
302: - flg - PETSC_TRUE to sort, PETSC_FALSE to skip sorting (lower setup cost, but non-deterministic)
304: Level: advanced
306: .seealso: PetscSFGatherBegin(), PetscSFScatterBegin()
307: @*/
308: PetscErrorCode PetscSFSetRankOrder(PetscSF sf,PetscBool flg)
309: {
314: if (sf->multi) SETERRQ(PetscObjectComm((PetscObject)sf),PETSC_ERR_ARG_WRONGSTATE,"Rank ordering must be set before first call to PetscSFGatherBegin() or PetscSFScatterBegin()");
315: sf->rankorder = flg;
316: return(0);
317: }
319: /*@
320: PetscSFSetGraph - Set a parallel star forest
322: Collective
324: Input Arguments:
325: + sf - star forest
326: . nroots - number of root vertices on the current process (these are possible targets for other process to attach leaves)
327: . nleaves - number of leaf vertices on the current process, each of these references a root on any process
328: . ilocal - locations of leaves in leafdata buffers, pass NULL for contiguous storage (locations must be >= 0, enforced
329: during setup in debug mode)
330: . localmode - copy mode for ilocal
331: . iremote - remote locations of root vertices for each leaf on the current process (locations must be >= 0, enforced
332: during setup in debug mode)
333: - remotemode - copy mode for iremote
335: Level: intermediate
337: Notes:
338: In Fortran you must use PETSC_COPY_VALUES for localmode and remotemode
340: Developers Note: Local indices which are the identity permutation in the range [0,nleaves) are discarded as they
341: encode contiguous storage. In such case, if localmode is PETSC_OWN_POINTER, the memory is deallocated as it is not
342: needed
344: Developers Note: This object does not necessarily encode a true star forest in the graph theoretic sense, since leaf
345: indices are not required to be unique. Some functions, however, rely on unique leaf indices (checked in debug mode).
347: .seealso: PetscSFCreate(), PetscSFView(), PetscSFGetGraph()
348: @*/
349: PetscErrorCode PetscSFSetGraph(PetscSF sf,PetscInt nroots,PetscInt nleaves,const PetscInt *ilocal,PetscCopyMode localmode,const PetscSFNode *iremote,PetscCopyMode remotemode)
350: {
357: if (nroots < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nroots %D, cannot be negative",nroots);
358: if (nleaves < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"nleaves %D, cannot be negative",nleaves);
360: PetscSFReset(sf);
361: PetscLogEventBegin(PETSCSF_SetGraph,sf,0,0,0);
363: sf->nroots = nroots;
364: sf->nleaves = nleaves;
366: if (nleaves && ilocal) {
367: PetscInt i;
368: PetscInt minleaf = PETSC_MAX_INT;
369: PetscInt maxleaf = PETSC_MIN_INT;
370: int contiguous = 1;
371: for (i=0; i<nleaves; i++) {
372: minleaf = PetscMin(minleaf,ilocal[i]);
373: maxleaf = PetscMax(maxleaf,ilocal[i]);
374: contiguous &= (ilocal[i] == i);
375: }
376: sf->minleaf = minleaf;
377: sf->maxleaf = maxleaf;
378: if (contiguous) {
379: if (localmode == PETSC_OWN_POINTER) {
380: PetscFree(ilocal);
381: }
382: ilocal = NULL;
383: }
384: } else {
385: sf->minleaf = 0;
386: sf->maxleaf = nleaves - 1;
387: }
389: if (ilocal) {
390: switch (localmode) {
391: case PETSC_COPY_VALUES:
392: PetscMalloc1(nleaves,&sf->mine_alloc);
393: PetscArraycpy(sf->mine_alloc,ilocal,nleaves);
394: sf->mine = sf->mine_alloc;
395: break;
396: case PETSC_OWN_POINTER:
397: sf->mine_alloc = (PetscInt*)ilocal;
398: sf->mine = sf->mine_alloc;
399: break;
400: case PETSC_USE_POINTER:
401: sf->mine_alloc = NULL;
402: sf->mine = (PetscInt*)ilocal;
403: break;
404: default: SETERRQ(PetscObjectComm((PetscObject)sf),PETSC_ERR_ARG_OUTOFRANGE,"Unknown localmode");
405: }
406: }
408: switch (remotemode) {
409: case PETSC_COPY_VALUES:
410: PetscMalloc1(nleaves,&sf->remote_alloc);
411: PetscArraycpy(sf->remote_alloc,iremote,nleaves);
412: sf->remote = sf->remote_alloc;
413: break;
414: case PETSC_OWN_POINTER:
415: sf->remote_alloc = (PetscSFNode*)iremote;
416: sf->remote = sf->remote_alloc;
417: break;
418: case PETSC_USE_POINTER:
419: sf->remote_alloc = NULL;
420: sf->remote = (PetscSFNode*)iremote;
421: break;
422: default: SETERRQ(PetscObjectComm((PetscObject)sf),PETSC_ERR_ARG_OUTOFRANGE,"Unknown remotemode");
423: }
425: PetscLogEventEnd(PETSCSF_SetGraph,sf,0,0,0);
426: sf->graphset = PETSC_TRUE;
427: return(0);
428: }
430: /*@
431: PetscSFSetGraphWithPattern - Sets the graph of an SF with a specific pattern
433: Collective
435: Input Parameters:
436: + sf - The PetscSF
437: . map - Layout of roots over all processes (insignificant when pattern is PETSCSF_PATTERN_ALLTOALL)
438: - pattern - One of PETSCSF_PATTERN_ALLGATHER, PETSCSF_PATTERN_GATHER, PETSCSF_PATTERN_ALLTOALL
440: Notes:
441: It is easier to explain PetscSFPattern using vectors. Suppose we have an MPI vector x and its layout is map.
442: n and N are local and global sizes of x respectively.
444: With PETSCSF_PATTERN_ALLGATHER, the routine creates a graph that if one does Bcast on it, it will copy x to
445: sequential vectors y on all ranks.
447: With PETSCSF_PATTERN_GATHER, the routine creates a graph that if one does Bcast on it, it will copy x to a
448: sequential vector y on rank 0.
450: In above cases, entries of x are roots and entries of y are leaves.
452: With PETSCSF_PATTERN_ALLTOALL, map is insignificant. Suppose NP is size of sf's communicator. The routine
453: creates a graph that every rank has NP leaves and NP roots. On rank i, its leaf j is connected to root i
454: of rank j. Here 0 <=i,j<NP. It is a kind of MPI_Alltoall with sendcount/recvcount being 1. Note that it does
455: not mean one can not send multiple items. One just needs to create a new MPI datatype for the mulptiple data
456: items with MPI_Type_contiguous() and use that as the <unit> argument in SF routines.
458: In this case, roots and leaves are symmetric.
460: Level: intermediate
461: @*/
462: PetscErrorCode PetscSFSetGraphWithPattern(PetscSF sf,PetscLayout map,PetscSFPattern pattern)
463: {
464: MPI_Comm comm;
465: PetscInt n,N,res[2];
466: PetscMPIInt rank,size;
467: PetscSFType type;
471: PetscObjectGetComm((PetscObject)sf, &comm);
472: if (pattern < PETSCSF_PATTERN_ALLGATHER || pattern > PETSCSF_PATTERN_ALLTOALL) SETERRQ1(comm,PETSC_ERR_ARG_OUTOFRANGE,"Unsupported PetscSFPattern %D\n",pattern);
473: MPI_Comm_rank(comm,&rank);
474: MPI_Comm_size(comm,&size);
476: if (pattern == PETSCSF_PATTERN_ALLTOALL) {
477: type = PETSCSFALLTOALL;
478: PetscLayoutCreate(comm,&sf->map);
479: PetscLayoutSetLocalSize(sf->map,size);
480: PetscLayoutSetSize(sf->map,((PetscInt)size)*size);
481: PetscLayoutSetUp(sf->map);
482: } else {
483: PetscLayoutGetLocalSize(map,&n);
484: PetscLayoutGetSize(map,&N);
485: res[0] = n;
486: res[1] = -n;
487: /* Check if n are same over all ranks so that we can optimize it */
488: MPIU_Allreduce(MPI_IN_PLACE,res,2,MPIU_INT,MPI_MAX,comm);
489: if (res[0] == -res[1]) { /* same n */
490: type = (pattern == PETSCSF_PATTERN_ALLGATHER) ? PETSCSFALLGATHER : PETSCSFGATHER;
491: } else {
492: type = (pattern == PETSCSF_PATTERN_ALLGATHER) ? PETSCSFALLGATHERV : PETSCSFGATHERV;
493: }
494: PetscLayoutReference(map,&sf->map);
495: }
496: PetscSFSetType(sf,type);
498: sf->pattern = pattern;
499: sf->mine = NULL; /* Contiguous */
501: /* Set nleaves, nroots here in case user calls PetscSFGetGraph, which is legal to call even before PetscSFSetUp is called.
502: Also set other easy stuff.
503: */
504: if (pattern == PETSCSF_PATTERN_ALLGATHER) {
505: sf->nleaves = N;
506: sf->nroots = n;
507: sf->nranks = size;
508: sf->minleaf = 0;
509: sf->maxleaf = N - 1;
510: } else if (pattern == PETSCSF_PATTERN_GATHER) {
511: sf->nleaves = rank ? 0 : N;
512: sf->nroots = n;
513: sf->nranks = rank ? 0 : size;
514: sf->minleaf = 0;
515: sf->maxleaf = rank ? -1 : N - 1;
516: } else if (pattern == PETSCSF_PATTERN_ALLTOALL) {
517: sf->nleaves = size;
518: sf->nroots = size;
519: sf->nranks = size;
520: sf->minleaf = 0;
521: sf->maxleaf = size - 1;
522: }
523: sf->ndranks = 0; /* We do not need to separate out distinguished ranks for patterned graphs to improve communication performance */
524: sf->graphset = PETSC_TRUE;
525: return(0);
526: }
528: /*@
529: PetscSFCreateInverseSF - given a PetscSF in which all vertices have degree 1, creates the inverse map
531: Collective
533: Input Arguments:
535: . sf - star forest to invert
537: Output Arguments:
538: . isf - inverse of sf
539: Level: advanced
541: Notes:
542: All roots must have degree 1.
544: The local space may be a permutation, but cannot be sparse.
546: .seealso: PetscSFSetGraph()
547: @*/
548: PetscErrorCode PetscSFCreateInverseSF(PetscSF sf,PetscSF *isf)
549: {
551: PetscMPIInt rank;
552: PetscInt i,nroots,nleaves,maxlocal,count,*newilocal;
553: const PetscInt *ilocal;
554: PetscSFNode *roots,*leaves;
558: PetscSFCheckGraphSet(sf,1);
561: PetscSFGetGraph(sf,&nroots,&nleaves,&ilocal,NULL);
562: maxlocal = sf->maxleaf+1; /* TODO: We should use PetscSFGetLeafRange() */
564: MPI_Comm_rank(PetscObjectComm((PetscObject)sf),&rank);
565: PetscMalloc2(nroots,&roots,maxlocal,&leaves);
566: for (i=0; i<maxlocal; i++) {
567: leaves[i].rank = rank;
568: leaves[i].index = i;
569: }
570: for (i=0; i <nroots; i++) {
571: roots[i].rank = -1;
572: roots[i].index = -1;
573: }
574: PetscSFReduceBegin(sf,MPIU_2INT,leaves,roots,MPIU_REPLACE);
575: PetscSFReduceEnd(sf,MPIU_2INT,leaves,roots,MPIU_REPLACE);
577: /* Check whether our leaves are sparse */
578: for (i=0,count=0; i<nroots; i++) if (roots[i].rank >= 0) count++;
579: if (count == nroots) newilocal = NULL;
580: else { /* Index for sparse leaves and compact "roots" array (which is to become our leaves). */
581: PetscMalloc1(count,&newilocal);
582: for (i=0,count=0; i<nroots; i++) {
583: if (roots[i].rank >= 0) {
584: newilocal[count] = i;
585: roots[count].rank = roots[i].rank;
586: roots[count].index = roots[i].index;
587: count++;
588: }
589: }
590: }
592: PetscSFDuplicate(sf,PETSCSF_DUPLICATE_CONFONLY,isf);
593: PetscSFSetGraph(*isf,maxlocal,count,newilocal,PETSC_OWN_POINTER,roots,PETSC_COPY_VALUES);
594: PetscFree2(roots,leaves);
595: return(0);
596: }
598: /*@
599: PetscSFDuplicate - duplicate a PetscSF, optionally preserving rank connectivity and graph
601: Collective
603: Input Arguments:
604: + sf - communication object to duplicate
605: - opt - PETSCSF_DUPLICATE_CONFONLY, PETSCSF_DUPLICATE_RANKS, or PETSCSF_DUPLICATE_GRAPH (see PetscSFDuplicateOption)
607: Output Arguments:
608: . newsf - new communication object
610: Level: beginner
612: .seealso: PetscSFCreate(), PetscSFSetType(), PetscSFSetGraph()
613: @*/
614: PetscErrorCode PetscSFDuplicate(PetscSF sf,PetscSFDuplicateOption opt,PetscSF *newsf)
615: {
616: PetscSFType type;
623: PetscSFCreate(PetscObjectComm((PetscObject)sf),newsf);
624: PetscSFGetType(sf,&type);
625: if (type) {PetscSFSetType(*newsf,type);}
626: if (opt == PETSCSF_DUPLICATE_GRAPH) {
627: PetscSFCheckGraphSet(sf,1);
628: if (sf->pattern == PETSCSF_PATTERN_GENERAL) {
629: PetscInt nroots,nleaves;
630: const PetscInt *ilocal;
631: const PetscSFNode *iremote;
632: PetscSFGetGraph(sf,&nroots,&nleaves,&ilocal,&iremote);
633: PetscSFSetGraph(*newsf,nroots,nleaves,ilocal,PETSC_COPY_VALUES,iremote,PETSC_COPY_VALUES);
634: } else {
635: PetscSFSetGraphWithPattern(*newsf,sf->map,sf->pattern);
636: }
637: }
638: if (sf->ops->Duplicate) {(*sf->ops->Duplicate)(sf,opt,*newsf);}
639: return(0);
640: }
642: /*@C
643: PetscSFGetGraph - Get the graph specifying a parallel star forest
645: Not Collective
647: Input Arguments:
648: . sf - star forest
650: Output Arguments:
651: + nroots - number of root vertices on the current process (these are possible targets for other process to attach leaves)
652: . nleaves - number of leaf vertices on the current process, each of these references a root on any process
653: . ilocal - locations of leaves in leafdata buffers
654: - iremote - remote locations of root vertices for each leaf on the current process
656: Notes:
657: We are not currently requiring that the graph is set, thus returning nroots=-1 if it has not been set yet
659: Level: intermediate
661: .seealso: PetscSFCreate(), PetscSFView(), PetscSFSetGraph()
662: @*/
663: PetscErrorCode PetscSFGetGraph(PetscSF sf,PetscInt *nroots,PetscInt *nleaves,const PetscInt **ilocal,const PetscSFNode **iremote)
664: {
669: if (sf->ops->GetGraph) {
670: (sf->ops->GetGraph)(sf,nroots,nleaves,ilocal,iremote);
671: } else {
672: if (nroots) *nroots = sf->nroots;
673: if (nleaves) *nleaves = sf->nleaves;
674: if (ilocal) *ilocal = sf->mine;
675: if (iremote) *iremote = sf->remote;
676: }
677: return(0);
678: }
680: /*@
681: PetscSFGetLeafRange - Get the active leaf ranges
683: Not Collective
685: Input Arguments:
686: . sf - star forest
688: Output Arguments:
689: + minleaf - minimum active leaf on this process. Return 0 if there are no leaves.
690: - maxleaf - maximum active leaf on this process. Return -1 if there are no leaves.
692: Level: developer
694: .seealso: PetscSFCreate(), PetscSFView(), PetscSFSetGraph(), PetscSFGetGraph()
695: @*/
696: PetscErrorCode PetscSFGetLeafRange(PetscSF sf,PetscInt *minleaf,PetscInt *maxleaf)
697: {
701: PetscSFCheckGraphSet(sf,1);
702: if (minleaf) *minleaf = sf->minleaf;
703: if (maxleaf) *maxleaf = sf->maxleaf;
704: return(0);
705: }
707: /*@C
708: PetscSFView - view a star forest
710: Collective
712: Input Arguments:
713: + sf - star forest
714: - viewer - viewer to display graph, for example PETSC_VIEWER_STDOUT_WORLD
716: Level: beginner
718: .seealso: PetscSFCreate(), PetscSFSetGraph()
719: @*/
720: PetscErrorCode PetscSFView(PetscSF sf,PetscViewer viewer)
721: {
722: PetscErrorCode ierr;
723: PetscBool iascii;
724: PetscViewerFormat format;
728: if (!viewer) {PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)sf),&viewer);}
731: if (sf->graphset) {PetscSFSetUp(sf);}
732: PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);
733: if (iascii) {
734: PetscMPIInt rank;
735: PetscInt ii,i,j;
737: PetscObjectPrintClassNamePrefixType((PetscObject)sf,viewer);
738: PetscViewerASCIIPushTab(viewer);
739: if (sf->ops->View) {(*sf->ops->View)(sf,viewer);}
740: if (sf->pattern == PETSCSF_PATTERN_GENERAL) {
741: if (!sf->graphset) {
742: PetscViewerASCIIPrintf(viewer,"PetscSFSetGraph() has not been called yet\n");
743: PetscViewerASCIIPopTab(viewer);
744: return(0);
745: }
746: MPI_Comm_rank(PetscObjectComm((PetscObject)sf),&rank);
747: PetscViewerASCIIPushSynchronized(viewer);
748: PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Number of roots=%D, leaves=%D, remote ranks=%D\n",rank,sf->nroots,sf->nleaves,sf->nranks);
749: for (i=0; i<sf->nleaves; i++) {
750: PetscViewerASCIISynchronizedPrintf(viewer,"[%d] %D <- (%D,%D)\n",rank,sf->mine ? sf->mine[i] : i,sf->remote[i].rank,sf->remote[i].index);
751: }
752: PetscViewerFlush(viewer);
753: PetscViewerGetFormat(viewer,&format);
754: if (format == PETSC_VIEWER_ASCII_INFO_DETAIL) {
755: PetscMPIInt *tmpranks,*perm;
756: PetscMalloc2(sf->nranks,&tmpranks,sf->nranks,&perm);
757: PetscArraycpy(tmpranks,sf->ranks,sf->nranks);
758: for (i=0; i<sf->nranks; i++) perm[i] = i;
759: PetscSortMPIIntWithArray(sf->nranks,tmpranks,perm);
760: PetscViewerASCIISynchronizedPrintf(viewer,"[%d] Roots referenced by my leaves, by rank\n",rank);
761: for (ii=0; ii<sf->nranks; ii++) {
762: i = perm[ii];
763: PetscViewerASCIISynchronizedPrintf(viewer,"[%d] %d: %D edges\n",rank,sf->ranks[i],sf->roffset[i+1]-sf->roffset[i]);
764: for (j=sf->roffset[i]; j<sf->roffset[i+1]; j++) {
765: PetscViewerASCIISynchronizedPrintf(viewer,"[%d] %D <- %D\n",rank,sf->rmine[j],sf->rremote[j]);
766: }
767: }
768: PetscFree2(tmpranks,perm);
769: }
770: PetscViewerFlush(viewer);
771: PetscViewerASCIIPopSynchronized(viewer);
772: }
773: PetscViewerASCIIPopTab(viewer);
774: }
775: return(0);
776: }
778: /*@C
779: PetscSFGetRootRanks - Get root ranks and number of vertices referenced by leaves on this process
781: Not Collective
783: Input Arguments:
784: . sf - star forest
786: Output Arguments:
787: + nranks - number of ranks referenced by local part
788: . ranks - array of ranks
789: . roffset - offset in rmine/rremote for each rank (length nranks+1)
790: . rmine - concatenated array holding local indices referencing each remote rank
791: - rremote - concatenated array holding remote indices referenced for each remote rank
793: Level: developer
795: .seealso: PetscSFGetLeafRanks()
796: @*/
797: PetscErrorCode PetscSFGetRootRanks(PetscSF sf,PetscInt *nranks,const PetscMPIInt **ranks,const PetscInt **roffset,const PetscInt **rmine,const PetscInt **rremote)
798: {
803: if (!sf->setupcalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call PetscSFSetUp() before obtaining ranks");
804: if (sf->ops->GetRootRanks) {
805: (sf->ops->GetRootRanks)(sf,nranks,ranks,roffset,rmine,rremote);
806: } else {
807: /* The generic implementation */
808: if (nranks) *nranks = sf->nranks;
809: if (ranks) *ranks = sf->ranks;
810: if (roffset) *roffset = sf->roffset;
811: if (rmine) *rmine = sf->rmine;
812: if (rremote) *rremote = sf->rremote;
813: }
814: return(0);
815: }
817: /*@C
818: PetscSFGetLeafRanks - Get leaf ranks referencing roots on this process
820: Not Collective
822: Input Arguments:
823: . sf - star forest
825: Output Arguments:
826: + niranks - number of leaf ranks referencing roots on this process
827: . iranks - array of ranks
828: . ioffset - offset in irootloc for each rank (length niranks+1)
829: - irootloc - concatenated array holding local indices of roots referenced by each leaf rank
831: Level: developer
833: .seealso: PetscSFGetRootRanks()
834: @*/
835: PetscErrorCode PetscSFGetLeafRanks(PetscSF sf,PetscInt *niranks,const PetscMPIInt **iranks,const PetscInt **ioffset,const PetscInt **irootloc)
836: {
841: if (!sf->setupcalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call PetscSFSetUp() before obtaining ranks");
842: if (sf->ops->GetLeafRanks) {
843: (sf->ops->GetLeafRanks)(sf,niranks,iranks,ioffset,irootloc);
844: } else {
845: PetscSFType type;
846: PetscSFGetType(sf,&type);
847: SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SUP,"PetscSFGetLeafRanks() is not supported on this StarForest type: %s", type);
848: }
849: return(0);
850: }
852: static PetscBool InList(PetscMPIInt needle,PetscMPIInt n,const PetscMPIInt *list) {
853: PetscInt i;
854: for (i=0; i<n; i++) {
855: if (needle == list[i]) return PETSC_TRUE;
856: }
857: return PETSC_FALSE;
858: }
860: /*@C
861: PetscSFSetUpRanks - Set up data structures associated with ranks; this is for internal use by PetscSF implementations.
863: Collective
865: Input Arguments:
866: + sf - PetscSF to set up; PetscSFSetGraph() must have been called
867: - dgroup - MPI_Group of ranks to be distinguished (e.g., for self or shared memory exchange)
869: Level: developer
871: .seealso: PetscSFGetRootRanks()
872: @*/
873: PetscErrorCode PetscSFSetUpRanks(PetscSF sf,MPI_Group dgroup)
874: {
875: PetscErrorCode ierr;
876: PetscTable table;
877: PetscTablePosition pos;
878: PetscMPIInt size,groupsize,*groupranks;
879: PetscInt *rcount,*ranks;
880: PetscInt i, irank = -1,orank = -1;
884: PetscSFCheckGraphSet(sf,1);
885: MPI_Comm_size(PetscObjectComm((PetscObject)sf),&size);
886: PetscTableCreate(10,size,&table);
887: for (i=0; i<sf->nleaves; i++) {
888: /* Log 1-based rank */
889: PetscTableAdd(table,sf->remote[i].rank+1,1,ADD_VALUES);
890: }
891: PetscTableGetCount(table,&sf->nranks);
892: PetscMalloc4(sf->nranks,&sf->ranks,sf->nranks+1,&sf->roffset,sf->nleaves,&sf->rmine,sf->nleaves,&sf->rremote);
893: PetscMalloc2(sf->nranks,&rcount,sf->nranks,&ranks);
894: PetscTableGetHeadPosition(table,&pos);
895: for (i=0; i<sf->nranks; i++) {
896: PetscTableGetNext(table,&pos,&ranks[i],&rcount[i]);
897: ranks[i]--; /* Convert back to 0-based */
898: }
899: PetscTableDestroy(&table);
901: /* We expect that dgroup is reliably "small" while nranks could be large */
902: {
903: MPI_Group group = MPI_GROUP_NULL;
904: PetscMPIInt *dgroupranks;
905: MPI_Comm_group(PetscObjectComm((PetscObject)sf),&group);
906: MPI_Group_size(dgroup,&groupsize);
907: PetscMalloc1(groupsize,&dgroupranks);
908: PetscMalloc1(groupsize,&groupranks);
909: for (i=0; i<groupsize; i++) dgroupranks[i] = i;
910: if (groupsize) {MPI_Group_translate_ranks(dgroup,groupsize,dgroupranks,group,groupranks);}
911: MPI_Group_free(&group);
912: PetscFree(dgroupranks);
913: }
915: /* Partition ranks[] into distinguished (first sf->ndranks) followed by non-distinguished */
916: for (sf->ndranks=0,i=sf->nranks; sf->ndranks<i; ) {
917: for (i--; sf->ndranks<i; i--) { /* Scan i backward looking for distinguished rank */
918: if (InList(ranks[i],groupsize,groupranks)) break;
919: }
920: for ( ; sf->ndranks<=i; sf->ndranks++) { /* Scan sf->ndranks forward looking for non-distinguished rank */
921: if (!InList(ranks[sf->ndranks],groupsize,groupranks)) break;
922: }
923: if (sf->ndranks < i) { /* Swap ranks[sf->ndranks] with ranks[i] */
924: PetscInt tmprank,tmpcount;
926: tmprank = ranks[i];
927: tmpcount = rcount[i];
928: ranks[i] = ranks[sf->ndranks];
929: rcount[i] = rcount[sf->ndranks];
930: ranks[sf->ndranks] = tmprank;
931: rcount[sf->ndranks] = tmpcount;
932: sf->ndranks++;
933: }
934: }
935: PetscFree(groupranks);
936: PetscSortIntWithArray(sf->ndranks,ranks,rcount);
937: PetscSortIntWithArray(sf->nranks-sf->ndranks,ranks+sf->ndranks,rcount+sf->ndranks);
938: sf->roffset[0] = 0;
939: for (i=0; i<sf->nranks; i++) {
940: PetscMPIIntCast(ranks[i],sf->ranks+i);
941: sf->roffset[i+1] = sf->roffset[i] + rcount[i];
942: rcount[i] = 0;
943: }
944: for (i=0, irank = -1, orank = -1; i<sf->nleaves; i++) {
945: /* short circuit */
946: if (orank != sf->remote[i].rank) {
947: /* Search for index of iremote[i].rank in sf->ranks */
948: PetscFindMPIInt(sf->remote[i].rank,sf->ndranks,sf->ranks,&irank);
949: if (irank < 0) {
950: PetscFindMPIInt(sf->remote[i].rank,sf->nranks-sf->ndranks,sf->ranks+sf->ndranks,&irank);
951: if (irank >= 0) irank += sf->ndranks;
952: }
953: orank = sf->remote[i].rank;
954: }
955: if (irank < 0) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Could not find rank %D in array",sf->remote[i].rank);
956: sf->rmine[sf->roffset[irank] + rcount[irank]] = sf->mine ? sf->mine[i] : i;
957: sf->rremote[sf->roffset[irank] + rcount[irank]] = sf->remote[i].index;
958: rcount[irank]++;
959: }
960: PetscFree2(rcount,ranks);
961: return(0);
962: }
964: /*@C
965: PetscSFGetGroups - gets incoming and outgoing process groups
967: Collective
969: Input Argument:
970: . sf - star forest
972: Output Arguments:
973: + incoming - group of origin processes for incoming edges (leaves that reference my roots)
974: - outgoing - group of destination processes for outgoing edges (roots that I reference)
976: Level: developer
978: .seealso: PetscSFGetWindow(), PetscSFRestoreWindow()
979: @*/
980: PetscErrorCode PetscSFGetGroups(PetscSF sf,MPI_Group *incoming,MPI_Group *outgoing)
981: {
983: MPI_Group group = MPI_GROUP_NULL;
986: if (!sf->setupcalled) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Must call PetscSFSetUp() before obtaining groups");
987: if (sf->ingroup == MPI_GROUP_NULL) {
988: PetscInt i;
989: const PetscInt *indegree;
990: PetscMPIInt rank,*outranks,*inranks;
991: PetscSFNode *remote;
992: PetscSF bgcount;
994: /* Compute the number of incoming ranks */
995: PetscMalloc1(sf->nranks,&remote);
996: for (i=0; i<sf->nranks; i++) {
997: remote[i].rank = sf->ranks[i];
998: remote[i].index = 0;
999: }
1000: PetscSFDuplicate(sf,PETSCSF_DUPLICATE_CONFONLY,&bgcount);
1001: PetscSFSetGraph(bgcount,1,sf->nranks,NULL,PETSC_COPY_VALUES,remote,PETSC_OWN_POINTER);
1002: PetscSFComputeDegreeBegin(bgcount,&indegree);
1003: PetscSFComputeDegreeEnd(bgcount,&indegree);
1005: /* Enumerate the incoming ranks */
1006: PetscMalloc2(indegree[0],&inranks,sf->nranks,&outranks);
1007: MPI_Comm_rank(PetscObjectComm((PetscObject)sf),&rank);
1008: for (i=0; i<sf->nranks; i++) outranks[i] = rank;
1009: PetscSFGatherBegin(bgcount,MPI_INT,outranks,inranks);
1010: PetscSFGatherEnd(bgcount,MPI_INT,outranks,inranks);
1011: MPI_Comm_group(PetscObjectComm((PetscObject)sf),&group);
1012: MPI_Group_incl(group,indegree[0],inranks,&sf->ingroup);
1013: MPI_Group_free(&group);
1014: PetscFree2(inranks,outranks);
1015: PetscSFDestroy(&bgcount);
1016: }
1017: *incoming = sf->ingroup;
1019: if (sf->outgroup == MPI_GROUP_NULL) {
1020: MPI_Comm_group(PetscObjectComm((PetscObject)sf),&group);
1021: MPI_Group_incl(group,sf->nranks,sf->ranks,&sf->outgroup);
1022: MPI_Group_free(&group);
1023: }
1024: *outgoing = sf->outgroup;
1025: return(0);
1026: }
1028: /*@
1029: PetscSFGetMultiSF - gets the inner SF implemeting gathers and scatters
1031: Collective
1033: Input Argument:
1034: . sf - star forest that may contain roots with 0 or with more than 1 vertex
1036: Output Arguments:
1037: . multi - star forest with split roots, such that each root has degree exactly 1
1039: Level: developer
1041: Notes:
1043: In most cases, users should use PetscSFGatherBegin() and PetscSFScatterBegin() instead of manipulating multi
1044: directly. Since multi satisfies the stronger condition that each entry in the global space has exactly one incoming
1045: edge, it is a candidate for future optimization that might involve its removal.
1047: .seealso: PetscSFSetGraph(), PetscSFGatherBegin(), PetscSFScatterBegin(), PetscSFComputeMultiRootOriginalNumbering()
1048: @*/
1049: PetscErrorCode PetscSFGetMultiSF(PetscSF sf,PetscSF *multi)
1050: {
1056: if (sf->nroots < 0) { /* Graph has not been set yet; why do we need this? */
1057: PetscSFDuplicate(sf,PETSCSF_DUPLICATE_RANKS,&sf->multi);
1058: *multi = sf->multi;
1059: return(0);
1060: }
1061: if (!sf->multi) {
1062: const PetscInt *indegree;
1063: PetscInt i,*inoffset,*outones,*outoffset,maxlocal;
1064: PetscSFNode *remote;
1065: maxlocal = sf->maxleaf+1; /* TODO: We should use PetscSFGetLeafRange() */
1066: PetscSFComputeDegreeBegin(sf,&indegree);
1067: PetscSFComputeDegreeEnd(sf,&indegree);
1068: PetscMalloc3(sf->nroots+1,&inoffset,maxlocal,&outones,maxlocal,&outoffset);
1069: inoffset[0] = 0;
1070: for (i=0; i<sf->nroots; i++) inoffset[i+1] = inoffset[i] + indegree[i];
1071: for (i=0; i<maxlocal; i++) outones[i] = 1;
1072: PetscSFFetchAndOpBegin(sf,MPIU_INT,inoffset,outones,outoffset,MPI_SUM);
1073: PetscSFFetchAndOpEnd(sf,MPIU_INT,inoffset,outones,outoffset,MPI_SUM);
1074: for (i=0; i<sf->nroots; i++) inoffset[i] -= indegree[i]; /* Undo the increment */
1075: #if 0
1076: #if defined(PETSC_USE_DEBUG) /* Check that the expected number of increments occurred */
1077: for (i=0; i<sf->nroots; i++) {
1078: if (inoffset[i] + indegree[i] != inoffset[i+1]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"Incorrect result after PetscSFFetchAndOp");
1079: }
1080: #endif
1081: #endif
1082: PetscMalloc1(sf->nleaves,&remote);
1083: for (i=0; i<sf->nleaves; i++) {
1084: remote[i].rank = sf->remote[i].rank;
1085: remote[i].index = outoffset[sf->mine ? sf->mine[i] : i];
1086: }
1087: PetscSFDuplicate(sf,PETSCSF_DUPLICATE_RANKS,&sf->multi);
1088: PetscSFSetGraph(sf->multi,inoffset[sf->nroots],sf->nleaves,sf->mine,PETSC_COPY_VALUES,remote,PETSC_OWN_POINTER);
1089: if (sf->rankorder) { /* Sort the ranks */
1090: PetscMPIInt rank;
1091: PetscInt *inranks,*newoffset,*outranks,*newoutoffset,*tmpoffset,maxdegree;
1092: PetscSFNode *newremote;
1093: MPI_Comm_rank(PetscObjectComm((PetscObject)sf),&rank);
1094: for (i=0,maxdegree=0; i<sf->nroots; i++) maxdegree = PetscMax(maxdegree,indegree[i]);
1095: PetscMalloc5(sf->multi->nroots,&inranks,sf->multi->nroots,&newoffset,maxlocal,&outranks,maxlocal,&newoutoffset,maxdegree,&tmpoffset);
1096: for (i=0; i<maxlocal; i++) outranks[i] = rank;
1097: PetscSFReduceBegin(sf->multi,MPIU_INT,outranks,inranks,MPIU_REPLACE);
1098: PetscSFReduceEnd(sf->multi,MPIU_INT,outranks,inranks,MPIU_REPLACE);
1099: /* Sort the incoming ranks at each vertex, build the inverse map */
1100: for (i=0; i<sf->nroots; i++) {
1101: PetscInt j;
1102: for (j=0; j<indegree[i]; j++) tmpoffset[j] = j;
1103: PetscSortIntWithArray(indegree[i],inranks+inoffset[i],tmpoffset);
1104: for (j=0; j<indegree[i]; j++) newoffset[inoffset[i] + tmpoffset[j]] = inoffset[i] + j;
1105: }
1106: PetscSFBcastBegin(sf->multi,MPIU_INT,newoffset,newoutoffset);
1107: PetscSFBcastEnd(sf->multi,MPIU_INT,newoffset,newoutoffset);
1108: PetscMalloc1(sf->nleaves,&newremote);
1109: for (i=0; i<sf->nleaves; i++) {
1110: newremote[i].rank = sf->remote[i].rank;
1111: newremote[i].index = newoutoffset[sf->mine ? sf->mine[i] : i];
1112: }
1113: PetscSFSetGraph(sf->multi,inoffset[sf->nroots],sf->nleaves,sf->mine,PETSC_COPY_VALUES,newremote,PETSC_OWN_POINTER);
1114: PetscFree5(inranks,newoffset,outranks,newoutoffset,tmpoffset);
1115: }
1116: PetscFree3(inoffset,outones,outoffset);
1117: }
1118: *multi = sf->multi;
1119: return(0);
1120: }
1122: /*@C
1123: PetscSFCreateEmbeddedSF - removes edges from all but the selected roots, does not remap indices
1125: Collective
1127: Input Arguments:
1128: + sf - original star forest
1129: . nselected - number of selected roots on this process
1130: - selected - indices of the selected roots on this process
1132: Output Arguments:
1133: . newsf - new star forest
1135: Level: advanced
1137: Note:
1138: To use the new PetscSF, it may be necessary to know the indices of the leaves that are still participating. This can
1139: be done by calling PetscSFGetGraph().
1141: .seealso: PetscSFSetGraph(), PetscSFGetGraph()
1142: @*/
1143: PetscErrorCode PetscSFCreateEmbeddedSF(PetscSF sf,PetscInt nselected,const PetscInt *selected,PetscSF *newsf)
1144: {
1145: PetscInt i,n,*roots,*rootdata,*leafdata,nroots,nleaves,connected_leaves,*new_ilocal;
1146: const PetscSFNode *iremote;
1147: PetscSFNode *new_iremote;
1148: PetscSF tmpsf;
1149: MPI_Comm comm;
1150: PetscErrorCode ierr;
1154: PetscSFCheckGraphSet(sf,1);
1158: PetscSFSetUp(sf);
1160: /* Uniq selected[] and put results in roots[] */
1161: PetscObjectGetComm((PetscObject)sf,&comm);
1162: PetscMalloc1(nselected,&roots);
1163: PetscArraycpy(roots,selected,nselected);
1164: PetscSortedRemoveDupsInt(&nselected,roots);
1165: if (nselected && (roots[0] < 0 || roots[nselected-1] >= sf->nroots)) SETERRQ3(comm,PETSC_ERR_ARG_OUTOFRANGE,"Min/Max root indices %D/%D are not in [0,%D)",roots[0],roots[nselected-1],sf->nroots);
1167: if (sf->ops->CreateEmbeddedSF) {
1168: (*sf->ops->CreateEmbeddedSF)(sf,nselected,roots,newsf);
1169: } else {
1170: /* A generic version of creating embedded sf. Note that we called PetscSFSetGraph() twice, which is certainly expensive */
1171: /* Find out which leaves (not leaf data items) are still connected to roots in the embedded sf */
1172: PetscSFGetGraph(sf,&nroots,&nleaves,NULL,&iremote);
1173: PetscSFDuplicate(sf,PETSCSF_DUPLICATE_RANKS,&tmpsf);
1174: PetscSFSetGraph(tmpsf,nroots,nleaves,NULL/*contiguous*/,PETSC_USE_POINTER,iremote,PETSC_USE_POINTER);
1175: PetscCalloc2(nroots,&rootdata,nleaves,&leafdata);
1176: for (i=0; i<nselected; ++i) rootdata[roots[i]] = 1;
1177: PetscSFBcastBegin(tmpsf,MPIU_INT,rootdata,leafdata);
1178: PetscSFBcastEnd(tmpsf,MPIU_INT,rootdata,leafdata);
1179: PetscSFDestroy(&tmpsf);
1181: /* Build newsf with leaves that are still connected */
1182: connected_leaves = 0;
1183: for (i=0; i<nleaves; ++i) connected_leaves += leafdata[i];
1184: PetscMalloc1(connected_leaves,&new_ilocal);
1185: PetscMalloc1(connected_leaves,&new_iremote);
1186: for (i=0, n=0; i<nleaves; ++i) {
1187: if (leafdata[i]) {
1188: new_ilocal[n] = sf->mine ? sf->mine[i] : i;
1189: new_iremote[n].rank = sf->remote[i].rank;
1190: new_iremote[n].index = sf->remote[i].index;
1191: ++n;
1192: }
1193: }
1195: if (n != connected_leaves) SETERRQ2(PETSC_COMM_SELF,PETSC_ERR_PLIB,"There is a size mismatch in the SF embedding, %d != %d",n,connected_leaves);
1196: PetscSFCreate(comm,newsf);
1197: PetscSFSetFromOptions(*newsf);
1198: PetscSFSetGraph(*newsf,nroots,connected_leaves,new_ilocal,PETSC_OWN_POINTER,new_iremote,PETSC_OWN_POINTER);
1199: PetscFree2(rootdata,leafdata);
1200: }
1201: PetscFree(roots);
1202: return(0);
1203: }
1205: /*@C
1206: PetscSFCreateEmbeddedLeafSF - removes edges from all but the selected leaves, does not remap indices
1208: Collective
1210: Input Arguments:
1211: + sf - original star forest
1212: . nselected - number of selected leaves on this process
1213: - selected - indices of the selected leaves on this process
1215: Output Arguments:
1216: . newsf - new star forest
1218: Level: advanced
1220: .seealso: PetscSFCreateEmbeddedSF(), PetscSFSetGraph(), PetscSFGetGraph()
1221: @*/
1222: PetscErrorCode PetscSFCreateEmbeddedLeafSF(PetscSF sf,PetscInt nselected,const PetscInt *selected,PetscSF *newsf)
1223: {
1224: const PetscSFNode *iremote;
1225: PetscSFNode *new_iremote;
1226: const PetscInt *ilocal;
1227: PetscInt i,nroots,*leaves,*new_ilocal;
1228: MPI_Comm comm;
1229: PetscErrorCode ierr;
1233: PetscSFCheckGraphSet(sf,1);
1237: /* Uniq selected[] and put results in leaves[] */
1238: PetscObjectGetComm((PetscObject)sf,&comm);
1239: PetscMalloc1(nselected,&leaves);
1240: PetscArraycpy(leaves,selected,nselected);
1241: PetscSortedRemoveDupsInt(&nselected,leaves);
1242: if (nselected && (leaves[0] < 0 || leaves[nselected-1] >= sf->nleaves)) SETERRQ3(comm,PETSC_ERR_ARG_OUTOFRANGE,"Min/Max leaf indices %D/%D are not in [0,%D)",leaves[0],leaves[nselected-1],sf->nleaves);
1244: /* Optimize the routine only when sf is setup and hence we can reuse sf's communication pattern */
1245: if (sf->setupcalled && sf->ops->CreateEmbeddedLeafSF) {
1246: (*sf->ops->CreateEmbeddedLeafSF)(sf,nselected,leaves,newsf);
1247: } else {
1248: PetscSFGetGraph(sf,&nroots,NULL,&ilocal,&iremote);
1249: PetscMalloc1(nselected,&new_ilocal);
1250: PetscMalloc1(nselected,&new_iremote);
1251: for (i=0; i<nselected; ++i) {
1252: const PetscInt l = leaves[i];
1253: new_ilocal[i] = ilocal ? ilocal[l] : l;
1254: new_iremote[i].rank = iremote[l].rank;
1255: new_iremote[i].index = iremote[l].index;
1256: }
1257: PetscSFCreate(comm,newsf);
1258: PetscSFSetFromOptions(*newsf);
1259: PetscSFSetGraph(*newsf,nroots,nselected,new_ilocal,PETSC_OWN_POINTER,new_iremote,PETSC_OWN_POINTER);
1260: }
1261: PetscFree(leaves);
1262: return(0);
1263: }
1265: /*@C
1266: PetscSFBcastAndOpBegin - begin pointwise broadcast with root value being reduced to leaf value, to be concluded with call to PetscSFBcastAndOpEnd()
1268: Collective on PetscSF
1270: Input Arguments:
1271: + sf - star forest on which to communicate
1272: . unit - data type associated with each node
1273: . rootdata - buffer to broadcast
1274: - op - operation to use for reduction
1276: Output Arguments:
1277: . leafdata - buffer to be reduced with values from each leaf's respective root
1279: Level: intermediate
1281: .seealso: PetscSFBcastAndOpEnd(), PetscSFBcastBegin(), PetscSFBcastEnd()
1282: @*/
1283: PetscErrorCode PetscSFBcastAndOpBegin(PetscSF sf,MPI_Datatype unit,const void *rootdata,void *leafdata,MPI_Op op)
1284: {
1286: PetscMemType rootmtype,leafmtype;
1290: PetscSFSetUp(sf);
1291: PetscLogEventBegin(PETSCSF_BcastAndOpBegin,sf,0,0,0);
1292: PetscGetMemType(rootdata,&rootmtype);
1293: PetscGetMemType(leafdata,&leafmtype);
1294: #if defined(PETSC_HAVE_CUDA)
1295: /* Shall we assume rootdata, leafdata are ready to use (instead of being computed by some asynchronous kernels)?
1296: To be similar to MPI, I'd like to have this assumption, since MPI does not have a concept of stream.
1297: But currently this assumption is not enforecd in Petsc. To be safe, I do synchronization here. Otherwise, if
1298: we do not sync now and call the Pack kernel directly on the default NULL stream (assume petsc objects are also
1299: computed on it), we have to sync the NULL stream before calling MPI routines. So, it looks a cudaDeviceSynchronize
1300: is inevitable. We do it now and put pack/unpack kernels to non-NULL streams.
1301: */
1302: if (rootmtype == PETSC_MEMTYPE_DEVICE || leafmtype == PETSC_MEMTYPE_DEVICE) {cudaError_t err = cudaDeviceSynchronize();CHKERRCUDA(err);}
1303: #endif
1304: (*sf->ops->BcastAndOpBegin)(sf,unit,rootmtype,rootdata,leafmtype,leafdata,op);
1305: PetscLogEventEnd(PETSCSF_BcastAndOpBegin,sf,0,0,0);
1306: return(0);
1307: }
1309: /*@C
1310: PetscSFBcastAndOpEnd - end a broadcast & reduce operation started with PetscSFBcastAndOpBegin()
1312: Collective
1314: Input Arguments:
1315: + sf - star forest
1316: . unit - data type
1317: . rootdata - buffer to broadcast
1318: - op - operation to use for reduction
1320: Output Arguments:
1321: . leafdata - buffer to be reduced with values from each leaf's respective root
1323: Level: intermediate
1325: .seealso: PetscSFSetGraph(), PetscSFReduceEnd()
1326: @*/
1327: PetscErrorCode PetscSFBcastAndOpEnd(PetscSF sf,MPI_Datatype unit,const void *rootdata,void *leafdata,MPI_Op op)
1328: {
1330: PetscMemType rootmtype,leafmtype;
1334: PetscSFSetUp(sf);
1335: PetscLogEventBegin(PETSCSF_BcastAndOpEnd,sf,0,0,0);
1336: PetscGetMemType(rootdata,&rootmtype);
1337: PetscGetMemType(leafdata,&leafmtype);
1338: (*sf->ops->BcastAndOpEnd)(sf,unit,rootmtype,rootdata,leafmtype,leafdata,op);
1339: PetscLogEventEnd(PETSCSF_BcastAndOpEnd,sf,0,0,0);
1340: return(0);
1341: }
1343: /*@C
1344: PetscSFReduceBegin - begin reduction of leafdata into rootdata, to be completed with call to PetscSFReduceEnd()
1346: Collective
1348: Input Arguments:
1349: + sf - star forest
1350: . unit - data type
1351: . leafdata - values to reduce
1352: - op - reduction operation
1354: Output Arguments:
1355: . rootdata - result of reduction of values from all leaves of each root
1357: Level: intermediate
1359: .seealso: PetscSFBcastBegin()
1360: @*/
1361: PetscErrorCode PetscSFReduceBegin(PetscSF sf,MPI_Datatype unit,const void *leafdata,void *rootdata,MPI_Op op)
1362: {
1364: PetscMemType rootmtype,leafmtype;
1368: PetscSFSetUp(sf);
1369: PetscLogEventBegin(PETSCSF_ReduceBegin,sf,0,0,0);
1370: PetscGetMemType(rootdata,&rootmtype);
1371: PetscGetMemType(leafdata,&leafmtype);
1372: #if defined(PETSC_HAVE_CUDA)
1373: if (rootmtype == PETSC_MEMTYPE_DEVICE || leafmtype == PETSC_MEMTYPE_DEVICE) {cudaError_t err = cudaDeviceSynchronize();CHKERRCUDA(err);}
1374: #endif
1375: (sf->ops->ReduceBegin)(sf,unit,leafmtype,leafdata,rootmtype,rootdata,op);
1376: PetscLogEventEnd(PETSCSF_ReduceBegin,sf,0,0,0);
1377: return(0);
1378: }
1380: /*@C
1381: PetscSFReduceEnd - end a reduction operation started with PetscSFReduceBegin()
1383: Collective
1385: Input Arguments:
1386: + sf - star forest
1387: . unit - data type
1388: . leafdata - values to reduce
1389: - op - reduction operation
1391: Output Arguments:
1392: . rootdata - result of reduction of values from all leaves of each root
1394: Level: intermediate
1396: .seealso: PetscSFSetGraph(), PetscSFBcastEnd()
1397: @*/
1398: PetscErrorCode PetscSFReduceEnd(PetscSF sf,MPI_Datatype unit,const void *leafdata,void *rootdata,MPI_Op op)
1399: {
1401: PetscMemType rootmtype,leafmtype;
1405: PetscSFSetUp(sf);
1406: PetscLogEventBegin(PETSCSF_ReduceEnd,sf,0,0,0);
1407: PetscGetMemType(rootdata,&rootmtype);
1408: PetscGetMemType(leafdata,&leafmtype);
1409: (*sf->ops->ReduceEnd)(sf,unit,leafmtype,leafdata,rootmtype,rootdata,op);
1410: PetscLogEventEnd(PETSCSF_ReduceEnd,sf,0,0,0);
1411: return(0);
1412: }
1414: /*@C
1415: PetscSFFetchAndOpBegin - begin operation that fetches values from root and updates atomically by applying operation using my leaf value, to be completed with PetscSFFetchAndOpEnd()
1417: Collective
1419: Input Arguments:
1420: + sf - star forest
1421: . unit - data type
1422: . leafdata - leaf values to use in reduction
1423: - op - operation to use for reduction
1425: Output Arguments:
1426: + rootdata - root values to be updated, input state is seen by first process to perform an update
1427: - leafupdate - state at each leaf's respective root immediately prior to my atomic update
1429: Level: advanced
1431: Note:
1432: The update is only atomic at the granularity provided by the hardware. Different roots referenced by the same process
1433: might be updated in a different order. Furthermore, if a composite type is used for the unit datatype, atomicity is
1434: not guaranteed across the whole vertex. Therefore, this function is mostly only used with primitive types such as
1435: integers.
1437: .seealso: PetscSFComputeDegreeBegin(), PetscSFReduceBegin(), PetscSFSetGraph()
1438: @*/
1439: PetscErrorCode PetscSFFetchAndOpBegin(PetscSF sf,MPI_Datatype unit,void *rootdata,const void *leafdata,void *leafupdate,MPI_Op op)
1440: {
1442: PetscMemType rootmtype,leafmtype,leafupdatemtype;
1446: PetscSFSetUp(sf);
1447: PetscLogEventBegin(PETSCSF_FetchAndOpBegin,sf,0,0,0);
1448: PetscGetMemType(rootdata,&rootmtype);
1449: PetscGetMemType(leafdata,&leafmtype);
1450: PetscGetMemType(leafupdate,&leafupdatemtype);
1451: if (leafmtype != leafupdatemtype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for leafdata and leafupdate in different memory types");
1452: #if defined(PETSC_HAVE_CUDA)
1453: if (rootmtype == PETSC_MEMTYPE_DEVICE || leafmtype == PETSC_MEMTYPE_DEVICE) {cudaError_t err = cudaDeviceSynchronize();CHKERRCUDA(err);}
1454: #endif
1455: (*sf->ops->FetchAndOpBegin)(sf,unit,rootmtype,rootdata,leafmtype,leafdata,leafupdate,op);
1456: PetscLogEventEnd(PETSCSF_FetchAndOpBegin,sf,0,0,0);
1457: return(0);
1458: }
1460: /*@C
1461: PetscSFFetchAndOpEnd - end operation started in matching call to PetscSFFetchAndOpBegin() to fetch values from roots and update atomically by applying operation using my leaf value
1463: Collective
1465: Input Arguments:
1466: + sf - star forest
1467: . unit - data type
1468: . leafdata - leaf values to use in reduction
1469: - op - operation to use for reduction
1471: Output Arguments:
1472: + rootdata - root values to be updated, input state is seen by first process to perform an update
1473: - leafupdate - state at each leaf's respective root immediately prior to my atomic update
1475: Level: advanced
1477: .seealso: PetscSFComputeDegreeEnd(), PetscSFReduceEnd(), PetscSFSetGraph()
1478: @*/
1479: PetscErrorCode PetscSFFetchAndOpEnd(PetscSF sf,MPI_Datatype unit,void *rootdata,const void *leafdata,void *leafupdate,MPI_Op op)
1480: {
1482: PetscMemType rootmtype,leafmtype,leafupdatemtype;
1486: PetscSFSetUp(sf);
1487: PetscLogEventBegin(PETSCSF_FetchAndOpEnd,sf,0,0,0);
1488: PetscGetMemType(rootdata,&rootmtype);
1489: PetscGetMemType(leafdata,&leafmtype);
1490: PetscGetMemType(leafupdate,&leafupdatemtype);
1491: if (leafmtype != leafupdatemtype) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"No support for leafdata and leafupdate in different memory types");
1492: (*sf->ops->FetchAndOpEnd)(sf,unit,rootmtype,rootdata,leafmtype,leafdata,leafupdate,op);
1493: PetscLogEventEnd(PETSCSF_FetchAndOpEnd,sf,0,0,0);
1494: return(0);
1495: }
1497: /*@C
1498: PetscSFComputeDegreeBegin - begin computation of degree for each root vertex, to be completed with PetscSFComputeDegreeEnd()
1500: Collective
1502: Input Arguments:
1503: . sf - star forest
1505: Output Arguments:
1506: . degree - degree of each root vertex
1508: Level: advanced
1510: Notes:
1511: The returned array is owned by PetscSF and automatically freed by PetscSFDestroy(). Hence no need to call PetscFree() on it.
1513: .seealso: PetscSFGatherBegin()
1514: @*/
1515: PetscErrorCode PetscSFComputeDegreeBegin(PetscSF sf,const PetscInt **degree)
1516: {
1521: PetscSFCheckGraphSet(sf,1);
1523: if (!sf->degreeknown) {
1524: PetscInt i, nroots = sf->nroots, maxlocal = sf->maxleaf+1; /* TODO: We should use PetscSFGetLeafRange() */
1525: if (sf->degree) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Calls to PetscSFComputeDegreeBegin() cannot be nested.");
1526: PetscMalloc1(nroots,&sf->degree);
1527: PetscMalloc1(PetscMax(maxlocal,1),&sf->degreetmp); /* allocate at least one entry, see check in PetscSFComputeDegreeEnd() */
1528: for (i=0; i<nroots; i++) sf->degree[i] = 0;
1529: for (i=0; i<maxlocal; i++) sf->degreetmp[i] = 1;
1530: PetscSFReduceBegin(sf,MPIU_INT,sf->degreetmp,sf->degree,MPI_SUM);
1531: }
1532: *degree = NULL;
1533: return(0);
1534: }
1536: /*@C
1537: PetscSFComputeDegreeEnd - complete computation of degree for each root vertex, started with PetscSFComputeDegreeBegin()
1539: Collective
1541: Input Arguments:
1542: . sf - star forest
1544: Output Arguments:
1545: . degree - degree of each root vertex
1547: Level: developer
1549: Notes:
1550: The returned array is owned by PetscSF and automatically freed by PetscSFDestroy(). Hence no need to call PetscFree() on it.
1552: .seealso:
1553: @*/
1554: PetscErrorCode PetscSFComputeDegreeEnd(PetscSF sf,const PetscInt **degree)
1555: {
1560: PetscSFCheckGraphSet(sf,1);
1562: if (!sf->degreeknown) {
1563: if (!sf->degreetmp) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "Must call PetscSFComputeDegreeBegin() before PetscSFComputeDegreeEnd()");
1564: PetscSFReduceEnd(sf,MPIU_INT,sf->degreetmp,sf->degree,MPI_SUM);
1565: PetscFree(sf->degreetmp);
1566: sf->degreeknown = PETSC_TRUE;
1567: }
1568: *degree = sf->degree;
1569: return(0);
1570: }
1573: /*@C
1574: PetscSFComputeMultiRootOriginalNumbering - Returns original numbering of multi-roots (roots of multi-SF returned by PetscSFGetMultiSF()).
1575: Each multi-root is assigned index of the corresponding original root.
1577: Collective
1579: Input Arguments:
1580: + sf - star forest
1581: - degree - degree of each root vertex, computed with PetscSFComputeDegreeBegin()/PetscSFComputeDegreeEnd()
1583: Output Arguments:
1584: + nMultiRoots - (optional) number of multi-roots (roots of multi-SF)
1585: - multiRootsOrigNumbering - original indices of multi-roots; length of this array is nMultiRoots
1587: Level: developer
1589: Notes:
1590: The returned array multiRootsOrigNumbering is newly allocated and should be destroyed with PetscFree() when no longer needed.
1592: .seealso: PetscSFComputeDegreeBegin(), PetscSFComputeDegreeEnd(), PetscSFGetMultiSF()
1593: @*/
1594: PetscErrorCode PetscSFComputeMultiRootOriginalNumbering(PetscSF sf, const PetscInt degree[], PetscInt *nMultiRoots, PetscInt *multiRootsOrigNumbering[])
1595: {
1596: PetscSF msf;
1597: PetscInt i, j, k, nroots, nmroots;
1598: PetscErrorCode ierr;
1602: PetscSFGetGraph(sf, &nroots, NULL, NULL, NULL);
1606: PetscSFGetMultiSF(sf,&msf);
1607: PetscSFGetGraph(msf, &nmroots, NULL, NULL, NULL);
1608: PetscMalloc1(nmroots, multiRootsOrigNumbering);
1609: for (i=0,j=0,k=0; i<nroots; i++) {
1610: if (!degree[i]) continue;
1611: for (j=0; j<degree[i]; j++,k++) {
1612: (*multiRootsOrigNumbering)[k] = i;
1613: }
1614: }
1615: #if defined(PETSC_USE_DEBUG)
1616: if (PetscUnlikely(k != nmroots)) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_PLIB,"sanity check fail");
1617: #endif
1618: if (nMultiRoots) *nMultiRoots = nmroots;
1619: return(0);
1620: }
1622: /*@C
1623: PetscSFGatherBegin - begin pointwise gather of all leaves into multi-roots, to be completed with PetscSFGatherEnd()
1625: Collective
1627: Input Arguments:
1628: + sf - star forest
1629: . unit - data type
1630: - leafdata - leaf data to gather to roots
1632: Output Argument:
1633: . multirootdata - root buffer to gather into, amount of space per root is equal to its degree
1635: Level: intermediate
1637: .seealso: PetscSFComputeDegreeBegin(), PetscSFScatterBegin()
1638: @*/
1639: PetscErrorCode PetscSFGatherBegin(PetscSF sf,MPI_Datatype unit,const void *leafdata,void *multirootdata)
1640: {
1642: PetscSF multi;
1646: PetscSFSetUp(sf);
1647: PetscSFGetMultiSF(sf,&multi);
1648: PetscSFReduceBegin(multi,unit,leafdata,multirootdata,MPIU_REPLACE);
1649: return(0);
1650: }
1652: /*@C
1653: PetscSFGatherEnd - ends pointwise gather operation that was started with PetscSFGatherBegin()
1655: Collective
1657: Input Arguments:
1658: + sf - star forest
1659: . unit - data type
1660: - leafdata - leaf data to gather to roots
1662: Output Argument:
1663: . multirootdata - root buffer to gather into, amount of space per root is equal to its degree
1665: Level: intermediate
1667: .seealso: PetscSFComputeDegreeEnd(), PetscSFScatterEnd()
1668: @*/
1669: PetscErrorCode PetscSFGatherEnd(PetscSF sf,MPI_Datatype unit,const void *leafdata,void *multirootdata)
1670: {
1672: PetscSF multi;
1676: PetscSFSetUp(sf);
1677: PetscSFGetMultiSF(sf,&multi);
1678: PetscSFReduceEnd(multi,unit,leafdata,multirootdata,MPIU_REPLACE);
1679: return(0);
1680: }
1682: /*@C
1683: PetscSFScatterBegin - begin pointwise scatter operation from multi-roots to leaves, to be completed with PetscSFScatterEnd()
1685: Collective
1687: Input Arguments:
1688: + sf - star forest
1689: . unit - data type
1690: - multirootdata - root buffer to send to each leaf, one unit of data per leaf
1692: Output Argument:
1693: . leafdata - leaf data to be update with personal data from each respective root
1695: Level: intermediate
1697: .seealso: PetscSFComputeDegreeBegin(), PetscSFScatterBegin()
1698: @*/
1699: PetscErrorCode PetscSFScatterBegin(PetscSF sf,MPI_Datatype unit,const void *multirootdata,void *leafdata)
1700: {
1702: PetscSF multi;
1706: PetscSFSetUp(sf);
1707: PetscSFGetMultiSF(sf,&multi);
1708: PetscSFBcastBegin(multi,unit,multirootdata,leafdata);
1709: return(0);
1710: }
1712: /*@C
1713: PetscSFScatterEnd - ends pointwise scatter operation that was started with PetscSFScatterBegin()
1715: Collective
1717: Input Arguments:
1718: + sf - star forest
1719: . unit - data type
1720: - multirootdata - root buffer to send to each leaf, one unit of data per leaf
1722: Output Argument:
1723: . leafdata - leaf data to be update with personal data from each respective root
1725: Level: intermediate
1727: .seealso: PetscSFComputeDegreeEnd(), PetscSFScatterEnd()
1728: @*/
1729: PetscErrorCode PetscSFScatterEnd(PetscSF sf,MPI_Datatype unit,const void *multirootdata,void *leafdata)
1730: {
1732: PetscSF multi;
1736: PetscSFSetUp(sf);
1737: PetscSFGetMultiSF(sf,&multi);
1738: PetscSFBcastEnd(multi,unit,multirootdata,leafdata);
1739: return(0);
1740: }
1742: static PetscErrorCode PetscSFCheckLeavesUnique_Private(PetscSF sf)
1743: {
1744: #if defined(PETSC_USE_DEBUG)
1745: PetscInt i, n, nleaves;
1746: const PetscInt *ilocal = NULL;
1747: PetscHSetI seen;
1748: PetscErrorCode ierr;
1751: PetscSFGetGraph(sf,NULL,&nleaves,&ilocal,NULL);
1752: PetscHSetICreate(&seen);
1753: for (i = 0; i < nleaves; i++) {
1754: const PetscInt leaf = ilocal ? ilocal[i] : i;
1755: PetscHSetIAdd(seen,leaf);
1756: }
1757: PetscHSetIGetSize(seen,&n);
1758: if (n != nleaves) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Provided leaves have repeated values: all leaves must be unique");
1759: PetscHSetIDestroy(&seen);
1760: return(0);
1761: #else
1763: return(0);
1764: #endif
1765: }
1766: /*@
1767: PetscSFCompose - Compose a new PetscSF by putting the second SF under the first one in a top (roots) down (leaves) view
1769: Input Parameters:
1770: + sfA - The first PetscSF, whose local space may be a permutation, but can not be sparse.
1771: - sfB - The second PetscSF, whose number of roots must be equal to number of leaves of sfA on each processor
1773: Output Parameters:
1774: . sfBA - The composite SF. Doing a Bcast on the new SF is equvalent to doing Bcast on sfA, then Bcast on sfB
1776: Level: developer
1778: Notes:
1779: For the resulting composed SF to be valid, the input SFs must be true star forests: the leaves must be unique. This is
1780: checked in debug mode.
1782: .seealso: PetscSF, PetscSFComposeInverse(), PetscSFGetGraph(), PetscSFSetGraph()
1783: @*/
1784: PetscErrorCode PetscSFCompose(PetscSF sfA,PetscSF sfB,PetscSF *sfBA)
1785: {
1786: PetscErrorCode ierr;
1787: MPI_Comm comm;
1788: const PetscSFNode *remotePointsA,*remotePointsB;
1789: PetscSFNode *remotePointsBA=NULL,*reorderedRemotePointsA = NULL,*leafdataB;
1790: const PetscInt *localPointsA,*localPointsB,*localPointsBA;
1791: PetscInt i,numRootsA,numLeavesA,numRootsB,numLeavesB,minleaf,maxleaf;
1795: PetscSFCheckGraphSet(sfA,1);
1797: PetscSFCheckGraphSet(sfB,2);
1799: PetscObjectGetComm((PetscObject)sfA,&comm);
1800: PetscSFGetGraph(sfA,&numRootsA,&numLeavesA,&localPointsA,&remotePointsA);
1801: PetscSFGetGraph(sfB,&numRootsB,&numLeavesB,&localPointsB,&remotePointsB);
1802: PetscSFGetLeafRange(sfA,&minleaf,&maxleaf);
1803: if (numRootsB != numLeavesA) SETERRQ(comm,PETSC_ERR_ARG_INCOMP,"The second SF's number of roots must be equal to the first SF's number of leaves");
1804: if (maxleaf+1 != numLeavesA || minleaf) SETERRQ(comm,PETSC_ERR_ARG_INCOMP,"The first SF can not have sparse local space");
1805: /* The above check is fast, but not sufficient, since we cannot guarantee that the SF has unique leaves. So in debug
1806: mode, check properly. */
1807: PetscSFCheckLeavesUnique_Private(sfA);
1808: if (localPointsA) {
1809: /* Local space is dense permutation of identity. Need to rewire order of the remote points */
1810: PetscMalloc1(numLeavesA,&reorderedRemotePointsA);
1811: for (i=0; i<numLeavesA; i++) reorderedRemotePointsA[localPointsA[i]-minleaf] = remotePointsA[i];
1812: remotePointsA = reorderedRemotePointsA;
1813: }
1814: PetscSFGetLeafRange(sfB,&minleaf,&maxleaf);
1815: PetscMalloc1(maxleaf-minleaf+1,&leafdataB);
1816: PetscSFBcastBegin(sfB,MPIU_2INT,remotePointsA,leafdataB-minleaf);
1817: PetscSFBcastEnd(sfB,MPIU_2INT,remotePointsA,leafdataB-minleaf);
1818: PetscFree(reorderedRemotePointsA);
1820: /* sfB's leaves must be unique, otherwise BcastAndOp(B o A) != BcastAndOp(B) o BcastAndOp(A) */
1821: PetscSFCheckLeavesUnique_Private(sfB);
1822: if (minleaf == 0 && maxleaf + 1 == numLeavesB) { /* Local space of sfB is an identity or permutation */
1823: localPointsBA = NULL;
1824: remotePointsBA = leafdataB;
1825: } else {
1826: localPointsBA = localPointsB;
1827: PetscMalloc1(numLeavesB,&remotePointsBA);
1828: for (i=0; i<numLeavesB; i++) remotePointsBA[i] = leafdataB[localPointsB[i]-minleaf];
1829: PetscFree(leafdataB);
1830: }
1831: PetscSFCreate(comm, sfBA);
1832: PetscSFSetGraph(*sfBA,numRootsA,numLeavesB,localPointsBA,PETSC_COPY_VALUES,remotePointsBA,PETSC_OWN_POINTER);
1833: return(0);
1834: }
1836: /*@
1837: PetscSFComposeInverse - Compose a new PetscSF by putting inverse of the second SF under the first one
1839: Input Parameters:
1840: + sfA - The first PetscSF, whose local space may be a permutation, but can not be sparse.
1841: - sfB - The second PetscSF, whose number of leaves must be equal to number of leaves of sfA on each processor. All roots must have degree 1.
1843: Output Parameters:
1844: . sfBA - The composite SF. Doing a Bcast on the new SF is equvalent to doing Bcast on sfA, then Bcast on inverse of sfB
1846: Level: developer
1848: .seealso: PetscSF, PetscSFCompose(), PetscSFGetGraph(), PetscSFSetGraph()
1849: @*/
1850: PetscErrorCode PetscSFComposeInverse(PetscSF sfA,PetscSF sfB,PetscSF *sfBA)
1851: {
1852: PetscErrorCode ierr;
1853: MPI_Comm comm;
1854: const PetscSFNode *remotePointsA,*remotePointsB;
1855: PetscSFNode *remotePointsBA;
1856: const PetscInt *localPointsA,*localPointsB;
1857: PetscInt numRootsA,numLeavesA,numRootsB,numLeavesB,minleaf,maxleaf;
1861: PetscSFCheckGraphSet(sfA,1);
1863: PetscSFCheckGraphSet(sfB,2);
1865: PetscObjectGetComm((PetscObject) sfA, &comm);
1866: PetscSFGetGraph(sfA, &numRootsA, &numLeavesA, &localPointsA, &remotePointsA);
1867: PetscSFGetGraph(sfB, &numRootsB, &numLeavesB, &localPointsB, &remotePointsB);
1868: PetscSFGetLeafRange(sfA,&minleaf,&maxleaf);
1869: if (maxleaf+1-minleaf != numLeavesA) SETERRQ(comm,PETSC_ERR_ARG_INCOMP,"The first SF can not have sparse local space");
1870: if (numLeavesA != numLeavesB) SETERRQ(comm,PETSC_ERR_ARG_INCOMP,"The second SF's number of leaves must be equal to the first SF's number of leaves");
1871: PetscMalloc1(numRootsB,&remotePointsBA);
1872: PetscSFReduceBegin(sfB,MPIU_2INT,remotePointsA,remotePointsBA,MPIU_REPLACE);
1873: PetscSFReduceEnd(sfB,MPIU_2INT,remotePointsA,remotePointsBA,MPIU_REPLACE);
1874: PetscSFCreate(comm,sfBA);
1875: PetscSFSetGraph(*sfBA,numRootsA,numRootsB,NULL,PETSC_COPY_VALUES,remotePointsBA,PETSC_OWN_POINTER);
1876: return(0);
1877: }
1879: /*
1880: PetscSFCreateLocalSF_Private - Creates a local PetscSF that only has intra-process edges of the global PetscSF
1882: Input Parameters:
1883: . sf - The global PetscSF
1885: Output Parameters:
1886: . out - The local PetscSF
1887: */
1888: PetscErrorCode PetscSFCreateLocalSF_Private(PetscSF sf,PetscSF *out)
1889: {
1890: MPI_Comm comm;
1891: PetscMPIInt myrank;
1892: const PetscInt *ilocal;
1893: const PetscSFNode *iremote;
1894: PetscInt i,j,nroots,nleaves,lnleaves,*lilocal;
1895: PetscSFNode *liremote;
1896: PetscSF lsf;
1897: PetscErrorCode ierr;
1901: if (sf->ops->CreateLocalSF) {
1902: (*sf->ops->CreateLocalSF)(sf,out);
1903: } else {
1904: /* Could use PetscSFCreateEmbeddedLeafSF, but since we know the comm is PETSC_COMM_SELF, we can make it fast */
1905: PetscObjectGetComm((PetscObject)sf,&comm);
1906: MPI_Comm_rank(comm,&myrank);
1908: /* Find out local edges and build a local SF */
1909: PetscSFGetGraph(sf,&nroots,&nleaves,&ilocal,&iremote);
1910: for (i=lnleaves=0; i<nleaves; i++) {if (iremote[i].rank == (PetscInt)myrank) lnleaves++;}
1911: PetscMalloc1(lnleaves,&lilocal);
1912: PetscMalloc1(lnleaves,&liremote);
1914: for (i=j=0; i<nleaves; i++) {
1915: if (iremote[i].rank == (PetscInt)myrank) {
1916: lilocal[j] = ilocal? ilocal[i] : i; /* ilocal=NULL for contiguous storage */
1917: liremote[j].rank = 0; /* rank in PETSC_COMM_SELF */
1918: liremote[j].index = iremote[i].index;
1919: j++;
1920: }
1921: }
1922: PetscSFCreate(PETSC_COMM_SELF,&lsf);
1923: PetscSFSetGraph(lsf,nroots,lnleaves,lilocal,PETSC_OWN_POINTER,liremote,PETSC_OWN_POINTER);
1924: PetscSFSetUp(lsf);
1925: *out = lsf;
1926: }
1927: return(0);
1928: }
1930: /* Similar to PetscSFBcast, but only Bcast to leaves on rank 0 */
1931: PetscErrorCode PetscSFBcastToZero_Private(PetscSF sf,MPI_Datatype unit,const void *rootdata,void *leafdata)
1932: {
1933: PetscErrorCode ierr;
1934: PetscMemType rootmtype,leafmtype;
1938: PetscSFSetUp(sf);
1939: PetscLogEventBegin(PETSCSF_BcastAndOpBegin,sf,0,0,0);
1940: PetscGetMemType(rootdata,&rootmtype);
1941: PetscGetMemType(leafdata,&leafmtype);
1942: if (sf->ops->BcastToZero) {
1943: (*sf->ops->BcastToZero)(sf,unit,rootmtype,rootdata,leafmtype,leafdata);
1944: } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SUP,"PetscSFBcastToZero_Private is not supported on this SF type");
1945: PetscLogEventEnd(PETSCSF_BcastAndOpBegin,sf,0,0,0);
1946: return(0);
1947: }