xref: /petsc/include/petscsf.h (revision 843d480f9cddad1d931d742d77ea89a67ca9f317)
1 /*
2    A star forest (SF) describes a communication pattern
3 */
4 #if !defined(PETSCSF_H)
5 #define PETSCSF_H
6 #include <petscsys.h>
7 #include <petscsftypes.h>
8 #include <petscvec.h> /* for Vec, VecScatter etc */
9 
10 PETSC_EXTERN PetscClassId PETSCSF_CLASSID;
11 
12 #define PETSCSFBASIC      "basic"
13 #define PETSCSFNEIGHBOR   "neighbor"
14 #define PETSCSFALLGATHERV "allgatherv"
15 #define PETSCSFALLGATHER  "allgather"
16 #define PETSCSFGATHERV    "gatherv"
17 #define PETSCSFGATHER     "gather"
18 #define PETSCSFALLTOALL   "alltoall"
19 #define PETSCSFWINDOW     "window"
20 
21 /*E
22    PetscSFPattern - Pattern of the PetscSF graph
23 
24 $  PETSCSF_PATTERN_GENERAL   - A general graph. One sets the graph with PetscSFSetGraph() and usually does not use this enum directly.
25 $  PETSCSF_PATTERN_ALLGATHER - A graph that every rank gathers all roots from all ranks (like MPI_Allgather/v). One sets the graph with PetscSFSetGraphWithPattern().
26 $  PETSCSF_PATTERN_GATHER    - A graph that rank 0 gathers all roots from all ranks (like MPI_Gather/v with root=0). One sets the graph with PetscSFSetGraphWithPattern().
27 $  PETSCSF_PATTERN_ALLTOALL  - A graph that every rank gathers different roots from all ranks (like MPI_Alltoall). One sets the graph with PetscSFSetGraphWithPattern().
28                                In an ALLTOALL graph, we assume each process has <size> leaves and <size> roots, with each leaf connecting to a remote root. Here <size> is
29                                the size of the communicator. This does not mean one can not communicate multiple data items between a pair of processes. One just needs to
30                                create a new MPI datatype for the multiple data items, e.g., by MPI_Type_contiguous.
31    Level: beginner
32 
33 .seealso: PetscSFSetGraph(), PetscSFSetGraphWithPattern()
34 E*/
35 typedef enum {PETSCSF_PATTERN_GENERAL=0,PETSCSF_PATTERN_ALLGATHER,PETSCSF_PATTERN_GATHER,PETSCSF_PATTERN_ALLTOALL} PetscSFPattern;
36 
37 /*E
38     PetscSFWindowSyncType - Type of synchronization for PETSCSFWINDOW
39 
40 $  PETSCSF_WINDOW_SYNC_FENCE - simplest model, synchronizing across communicator
41 $  PETSCSF_WINDOW_SYNC_LOCK - passive model, less synchronous, requires less setup than PETSCSF_WINDOW_SYNC_ACTIVE, but may require more handshakes
42 $  PETSCSF_WINDOW_SYNC_ACTIVE - active model, provides most information to MPI implementation, needs to construct 2-way process groups (more setup than PETSCSF_WINDOW_SYNC_LOCK)
43 
44    Level: advanced
45 
46 .seealso: PetscSFWindowSetSyncType(), PetscSFWindowGetSyncType()
47 E*/
48 typedef enum {PETSCSF_WINDOW_SYNC_FENCE,PETSCSF_WINDOW_SYNC_LOCK,PETSCSF_WINDOW_SYNC_ACTIVE} PetscSFWindowSyncType;
49 PETSC_EXTERN const char *const PetscSFWindowSyncTypes[];
50 
51 /*E
52     PetscSFWindowFlavorType - Flavor for the creation of MPI windows for PETSCSFWINDOW
53 
54 $  PETSCSF_WINDOW_FLAVOR_CREATE - Use MPI_Win_create, no reusage
55 $  PETSCSF_WINDOW_FLAVOR_DYNAMIC - Use MPI_Win_create_dynamic and dynamically attach pointers
56 $  PETSCSF_WINDOW_FLAVOR_ALLOCATE - Use MPI_Win_allocate
57 $  PETSCSF_WINDOW_FLAVOR_SHARED - Use MPI_Win_allocate_shared
58 
59    Level: advanced
60 
61 .seealso: PetscSFWindowSetFlavorType(), PetscSFWindowGetFlavorType()
62 E*/
63 typedef enum {PETSCSF_WINDOW_FLAVOR_CREATE,PETSCSF_WINDOW_FLAVOR_DYNAMIC,PETSCSF_WINDOW_FLAVOR_ALLOCATE,PETSCSF_WINDOW_FLAVOR_SHARED} PetscSFWindowFlavorType;
64 PETSC_EXTERN const char *const PetscSFWindowFlavorTypes[];
65 
66 /*E
67     PetscSFDuplicateOption - Aspects to preserve when duplicating a PetscSF
68 
69 $  PETSCSF_DUPLICATE_CONFONLY - configuration only, user must call PetscSFSetGraph()
70 $  PETSCSF_DUPLICATE_RANKS - communication ranks preserved, but different graph (allows simpler setup after calling PetscSFSetGraph())
71 $  PETSCSF_DUPLICATE_GRAPH - entire graph duplicated
72 
73    Level: beginner
74 
75 .seealso: PetscSFDuplicate()
76 E*/
77 typedef enum {PETSCSF_DUPLICATE_CONFONLY,PETSCSF_DUPLICATE_RANKS,PETSCSF_DUPLICATE_GRAPH} PetscSFDuplicateOption;
78 PETSC_EXTERN const char *const PetscSFDuplicateOptions[];
79 
80 PETSC_EXTERN PetscFunctionList PetscSFList;
81 PETSC_EXTERN PetscErrorCode PetscSFRegister(const char[],PetscErrorCode (*)(PetscSF));
82 
83 PETSC_EXTERN PetscErrorCode PetscSFInitializePackage(void);
84 PETSC_EXTERN PetscErrorCode PetscSFFinalizePackage(void);
85 PETSC_EXTERN PetscErrorCode PetscSFCreate(MPI_Comm,PetscSF*);
86 PETSC_EXTERN PetscErrorCode PetscSFDestroy(PetscSF*);
87 PETSC_EXTERN PetscErrorCode PetscSFSetType(PetscSF,PetscSFType);
88 PETSC_EXTERN PetscErrorCode PetscSFGetType(PetscSF,PetscSFType*);
89 PETSC_EXTERN PetscErrorCode PetscSFView(PetscSF,PetscViewer);
90 PETSC_EXTERN PetscErrorCode PetscSFViewFromOptions(PetscSF,PetscObject,const char[]);
91 PETSC_EXTERN PetscErrorCode PetscSFSetUp(PetscSF);
92 PETSC_EXTERN PetscErrorCode PetscSFSetFromOptions(PetscSF);
93 PETSC_EXTERN PetscErrorCode PetscSFDuplicate(PetscSF,PetscSFDuplicateOption,PetscSF*);
94 PETSC_EXTERN PetscErrorCode PetscSFWindowSetSyncType(PetscSF,PetscSFWindowSyncType);
95 PETSC_EXTERN PetscErrorCode PetscSFWindowGetSyncType(PetscSF,PetscSFWindowSyncType*);
96 PETSC_EXTERN PetscErrorCode PetscSFWindowSetFlavorType(PetscSF,PetscSFWindowFlavorType);
97 PETSC_EXTERN PetscErrorCode PetscSFWindowGetFlavorType(PetscSF,PetscSFWindowFlavorType*);
98 PETSC_EXTERN PetscErrorCode PetscSFWindowSetInfo(PetscSF,MPI_Info);
99 PETSC_EXTERN PetscErrorCode PetscSFWindowGetInfo(PetscSF,MPI_Info*);
100 PETSC_EXTERN PetscErrorCode PetscSFSetRankOrder(PetscSF,PetscBool);
101 PETSC_EXTERN PetscErrorCode PetscSFSetGraph(PetscSF,PetscInt,PetscInt,const PetscInt*,PetscCopyMode,const PetscSFNode*,PetscCopyMode);
102 PETSC_EXTERN PetscErrorCode PetscSFSetGraphWithPattern(PetscSF,PetscLayout,PetscSFPattern);
103 PETSC_EXTERN PetscErrorCode PetscSFGetGraph(PetscSF,PetscInt*,PetscInt*,const PetscInt**,const PetscSFNode**);
104 PETSC_EXTERN PetscErrorCode PetscSFGetLeafRange(PetscSF,PetscInt*,PetscInt*);
105 PETSC_EXTERN PetscErrorCode PetscSFCreateEmbeddedSF(PetscSF,PetscInt,const PetscInt*,PetscSF*);
106 PETSC_EXTERN PetscErrorCode PetscSFCreateEmbeddedLeafSF(PetscSF,PetscInt,const PetscInt *, PetscSF *);
107 PETSC_EXTERN PetscErrorCode PetscSFReset(PetscSF);
108 PETSC_EXTERN PetscErrorCode PetscSFSetUpRanks(PetscSF,MPI_Group);
109 PETSC_EXTERN PetscErrorCode PetscSFGetRootRanks(PetscSF,PetscInt*,const PetscMPIInt**,const PetscInt**,const PetscInt**,const PetscInt**);
110 PETSC_EXTERN PetscErrorCode PetscSFGetLeafRanks(PetscSF,PetscInt*,const PetscMPIInt**,const PetscInt**,const PetscInt**);
111 PETSC_EXTERN PetscErrorCode PetscSFGetGroups(PetscSF,MPI_Group*,MPI_Group*);
112 PETSC_EXTERN PetscErrorCode PetscSFGetMultiSF(PetscSF,PetscSF*);
113 PETSC_EXTERN PetscErrorCode PetscSFCreateInverseSF(PetscSF,PetscSF*);
114 
115 /* Build PetscSF from PetscLayout */
116 PETSC_EXTERN PetscErrorCode PetscSFSetGraphLayout(PetscSF,PetscLayout,PetscInt,const PetscInt*,PetscCopyMode,const PetscInt*);
117 PETSC_EXTERN PetscErrorCode PetscSFCreateFromLayouts(PetscLayout,PetscLayout,PetscSF*);
118 PETSC_DEPRECATED_FUNCTION("Use PetscSFCreateFromLayouts (since v3.15)")
119 PETSC_STATIC_INLINE PetscErrorCode PetscLayoutsCreateSF(PetscLayout rmap, PetscLayout lmap, PetscSF* sf) {
120   return PetscSFCreateFromLayouts(rmap, lmap, sf);
121 }
122 
123 /* PetscSection interoperability */
124 PETSC_EXTERN PetscErrorCode PetscSFSetGraphSection(PetscSF,PetscSection,PetscSection);
125 PETSC_EXTERN PetscErrorCode PetscSFCreateRemoteOffsets(PetscSF, PetscSection, PetscSection, PetscInt **);
126 PETSC_EXTERN PetscErrorCode PetscSFDistributeSection(PetscSF, PetscSection, PetscInt **, PetscSection);
127 PETSC_EXTERN PetscErrorCode PetscSFCreateSectionSF(PetscSF, PetscSection, PetscInt [], PetscSection, PetscSF *);
128 
129 /* Reduce rootdata to leafdata using provided operation */
130 PETSC_EXTERN PetscErrorCode PetscSFBcastAndOpBegin(PetscSF,MPI_Datatype,const void*,void*,MPI_Op)
131   PetscAttrMPIPointerWithType(3,2) PetscAttrMPIPointerWithType(4,2);
132 PETSC_EXTERN PetscErrorCode PetscSFBcastAndOpEnd(PetscSF,MPI_Datatype,const void*,void*,MPI_Op)
133   PetscAttrMPIPointerWithType(3,2) PetscAttrMPIPointerWithType(4,2);
134 PETSC_EXTERN PetscErrorCode PetscSFBcastAndOpWithMemTypeBegin(PetscSF,MPI_Datatype,PetscMemType,const void*,PetscMemType,void*,MPI_Op)
135   PetscAttrMPIPointerWithType(4,2) PetscAttrMPIPointerWithType(6,2);
136 
137 /* Reduce leafdata into rootdata using provided operation */
138 PETSC_EXTERN PetscErrorCode PetscSFReduceBegin(PetscSF,MPI_Datatype,const void*,void *,MPI_Op)
139   PetscAttrMPIPointerWithType(3,2) PetscAttrMPIPointerWithType(4,2);
140 PETSC_EXTERN PetscErrorCode PetscSFReduceEnd(PetscSF,MPI_Datatype,const void*,void*,MPI_Op)
141   PetscAttrMPIPointerWithType(3,2) PetscAttrMPIPointerWithType(4,2);
142 PETSC_EXTERN PetscErrorCode PetscSFReduceWithMemTypeBegin(PetscSF,MPI_Datatype,PetscMemType,const void*,PetscMemType,void *,MPI_Op)
143   PetscAttrMPIPointerWithType(4,2) PetscAttrMPIPointerWithType(6,2);
144 /* Atomically modifies (using provided operation) rootdata using leafdata from each leaf, value at root at time of modification is returned in leafupdate. */
145 PETSC_EXTERN PetscErrorCode PetscSFFetchAndOpBegin(PetscSF,MPI_Datatype,void*,const void*,void*,MPI_Op)
146   PetscAttrMPIPointerWithType(3,2) PetscAttrMPIPointerWithType(4,2) PetscAttrMPIPointerWithType(5,2);
147 PETSC_EXTERN PetscErrorCode PetscSFFetchAndOpEnd(PetscSF,MPI_Datatype,void*,const void*,void*,MPI_Op)
148   PetscAttrMPIPointerWithType(3,2) PetscAttrMPIPointerWithType(4,2) PetscAttrMPIPointerWithType(5,2);
149 /* Compute the degree of every root vertex (number of leaves in its star) */
150 PETSC_EXTERN PetscErrorCode PetscSFComputeDegreeBegin(PetscSF,const PetscInt**);
151 PETSC_EXTERN PetscErrorCode PetscSFComputeDegreeEnd(PetscSF,const PetscInt**);
152 PETSC_EXTERN PetscErrorCode PetscSFComputeMultiRootOriginalNumbering(PetscSF,const PetscInt[],PetscInt*,PetscInt*[]);
153 /* Concatenate data from all leaves into roots */
154 PETSC_EXTERN PetscErrorCode PetscSFGatherBegin(PetscSF,MPI_Datatype,const void*,void*)
155   PetscAttrMPIPointerWithType(3,2) PetscAttrMPIPointerWithType(4,2);
156 PETSC_EXTERN PetscErrorCode PetscSFGatherEnd(PetscSF,MPI_Datatype,const void*,void*)
157   PetscAttrMPIPointerWithType(3,2) PetscAttrMPIPointerWithType(4,2);
158 /* Distribute distinct values to each leaf from roots */
159 PETSC_EXTERN PetscErrorCode PetscSFScatterBegin(PetscSF,MPI_Datatype,const void*,void*)
160   PetscAttrMPIPointerWithType(3,2) PetscAttrMPIPointerWithType(4,2);
161 PETSC_EXTERN PetscErrorCode PetscSFScatterEnd(PetscSF,MPI_Datatype,const void*,void*)
162   PetscAttrMPIPointerWithType(3,2) PetscAttrMPIPointerWithType(4,2);
163 
164 PETSC_EXTERN PetscErrorCode PetscSFCompose(PetscSF,PetscSF,PetscSF*);
165 PETSC_EXTERN PetscErrorCode PetscSFComposeInverse(PetscSF,PetscSF,PetscSF*);
166 
167 #if defined(MPI_REPLACE)
168 #  define MPIU_REPLACE MPI_REPLACE
169 #else
170 /* When using an old MPI such that MPI_REPLACE is not defined, we do not pass MPI_REPLACE to MPI at all.  Instead, we
171  * use it as a flag for our own reducer in the PETSCSFBASIC implementation.  This could be any unique value unlikely to
172  * collide with another MPI_Op so we'll just use the value that has been used by every version of MPICH since
173  * MPICH2-1.0.6. */
174 #  define MPIU_REPLACE (MPI_Op)(0x5800000d)
175 #endif
176 
177 PETSC_DEPRECATED_FUNCTION("Use PetscSFGetRootRanks (since v3.12)")
178 PETSC_STATIC_INLINE PetscErrorCode PetscSFGetRanks(PetscSF sf,PetscInt *nranks,const PetscMPIInt **ranks,const PetscInt **roffset,const PetscInt **rmine,const PetscInt **rremote) {
179   return PetscSFGetRootRanks(sf,nranks,ranks,roffset,rmine,rremote);
180 }
181 
182 /*@C
183    PetscSFBcastBegin - begin pointwise broadcast to be concluded with call to PetscSFBcastEnd()
184 
185    Collective on PetscSF
186 
187    Input Arguments:
188 +  sf - star forest on which to communicate
189 .  unit - data type associated with each node
190 -  rootdata - buffer to broadcast
191 
192    Output Arguments:
193 .  leafdata - buffer to update with values from each leaf's respective root
194 
195    Level: intermediate
196 
197 .seealso: PetscSFCreate(), PetscSFSetGraph(), PetscSFView(), PetscSFBcastEnd(), PetscSFReduceBegin(), PetscSFBcastAndOpBegin()
198 @*/
199 PETSC_STATIC_INLINE PetscErrorCode PetscSFBcastBegin(PetscSF sf,MPI_Datatype unit,const void* rootdata,void* leafdata) {
200   return PetscSFBcastAndOpBegin(sf,unit,rootdata,leafdata,MPIU_REPLACE);
201 }
202 
203 PETSC_STATIC_INLINE PetscErrorCode PetscSFBcastWithMemTypeBegin(PetscSF sf,MPI_Datatype unit,PetscMemType rootmtype,const void* rootdata,PetscMemType leafmtype,void* leafdata) {
204   return PetscSFBcastAndOpWithMemTypeBegin(sf,unit,rootmtype,rootdata,leafmtype,leafdata,MPIU_REPLACE);
205 }
206 
207 /*@C
208    PetscSFBcastEnd - end a broadcast operation started with PetscSFBcastBegin()
209 
210    Collective
211 
212    Input Arguments:
213 +  sf - star forest
214 .  unit - data type
215 -  rootdata - buffer to broadcast
216 
217    Output Arguments:
218 .  leafdata - buffer to update with values from each leaf's respective root
219 
220    Level: intermediate
221 
222 .seealso: PetscSFSetGraph(), PetscSFReduceEnd()
223 @*/
224 PETSC_STATIC_INLINE PetscErrorCode PetscSFBcastEnd(PetscSF sf,MPI_Datatype unit,const void* rootdata,void* leafdata) {
225   return PetscSFBcastAndOpEnd(sf,unit,rootdata,leafdata,MPIU_REPLACE);
226 }
227 
228 #endif
229