xref: /petsc/include/petscsf.h (revision 6ffe77eaecce1557e50d00ca5347a7f48e598865)
1 /*
2    A star forest (SF) describes a communication pattern
3 */
4 #if !defined(PETSCSF_H)
5 #define PETSCSF_H
6 
7 #include <petscsys.h>
8 #include <petscsftypes.h>
9 #include <petscvec.h> /* for Vec, VecScatter etc */
10 
11 /* SUBMANSEC = PetscSF */
12 
13 PETSC_EXTERN PetscClassId PETSCSF_CLASSID;
14 
15 #define PETSCSFBASIC      "basic"
16 #define PETSCSFNEIGHBOR   "neighbor"
17 #define PETSCSFALLGATHERV "allgatherv"
18 #define PETSCSFALLGATHER  "allgather"
19 #define PETSCSFGATHERV    "gatherv"
20 #define PETSCSFGATHER     "gather"
21 #define PETSCSFALLTOALL   "alltoall"
22 #define PETSCSFWINDOW     "window"
23 
24 /*E
25    PetscSFPattern - Pattern of the PetscSF graph
26 
27 $  PETSCSF_PATTERN_GENERAL   - A general graph. One sets the graph with PetscSFSetGraph() and usually does not use this enum directly.
28 $  PETSCSF_PATTERN_ALLGATHER - A graph that every rank gathers all roots from all ranks (like MPI_Allgather/v). One sets the graph with PetscSFSetGraphWithPattern().
29 $  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().
30 $  PETSCSF_PATTERN_ALLTOALL  - A graph that every rank gathers different roots from all ranks (like MPI_Alltoall). One sets the graph with PetscSFSetGraphWithPattern().
31                                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
32                                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
33                                create a new MPI datatype for the multiple data items, e.g., by MPI_Type_contiguous.
34    Level: beginner
35 
36 .seealso: `PetscSFSetGraph()`, `PetscSFSetGraphWithPattern()`
37 E*/
38 typedef enum {PETSCSF_PATTERN_GENERAL=0,PETSCSF_PATTERN_ALLGATHER,PETSCSF_PATTERN_GATHER,PETSCSF_PATTERN_ALLTOALL} PetscSFPattern;
39 
40 /*E
41     PetscSFWindowSyncType - Type of synchronization for PETSCSFWINDOW
42 
43 $  PETSCSF_WINDOW_SYNC_FENCE - simplest model, synchronizing across communicator
44 $  PETSCSF_WINDOW_SYNC_LOCK - passive model, less synchronous, requires less setup than PETSCSF_WINDOW_SYNC_ACTIVE, but may require more handshakes
45 $  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)
46 
47    Level: advanced
48 
49 .seealso: `PetscSFWindowSetSyncType()`, `PetscSFWindowGetSyncType()`
50 E*/
51 typedef enum {PETSCSF_WINDOW_SYNC_FENCE,PETSCSF_WINDOW_SYNC_LOCK,PETSCSF_WINDOW_SYNC_ACTIVE} PetscSFWindowSyncType;
52 PETSC_EXTERN const char *const PetscSFWindowSyncTypes[];
53 
54 /*E
55     PetscSFWindowFlavorType - Flavor for the creation of MPI windows for PETSCSFWINDOW
56 
57 $  PETSCSF_WINDOW_FLAVOR_CREATE - Use MPI_Win_create, no reusage
58 $  PETSCSF_WINDOW_FLAVOR_DYNAMIC - Use MPI_Win_create_dynamic and dynamically attach pointers
59 $  PETSCSF_WINDOW_FLAVOR_ALLOCATE - Use MPI_Win_allocate
60 $  PETSCSF_WINDOW_FLAVOR_SHARED - Use MPI_Win_allocate_shared
61 
62    Level: advanced
63 
64 .seealso: `PetscSFWindowSetFlavorType()`, `PetscSFWindowGetFlavorType()`
65 E*/
66 typedef enum {PETSCSF_WINDOW_FLAVOR_CREATE,PETSCSF_WINDOW_FLAVOR_DYNAMIC,PETSCSF_WINDOW_FLAVOR_ALLOCATE,PETSCSF_WINDOW_FLAVOR_SHARED} PetscSFWindowFlavorType;
67 PETSC_EXTERN const char *const PetscSFWindowFlavorTypes[];
68 
69 /*E
70     PetscSFDuplicateOption - Aspects to preserve when duplicating a PetscSF
71 
72 $  PETSCSF_DUPLICATE_CONFONLY - configuration only, user must call PetscSFSetGraph()
73 $  PETSCSF_DUPLICATE_RANKS - communication ranks preserved, but different graph (allows simpler setup after calling PetscSFSetGraph())
74 $  PETSCSF_DUPLICATE_GRAPH - entire graph duplicated
75 
76    Level: beginner
77 
78 .seealso: `PetscSFDuplicate()`
79 E*/
80 typedef enum {PETSCSF_DUPLICATE_CONFONLY,PETSCSF_DUPLICATE_RANKS,PETSCSF_DUPLICATE_GRAPH} PetscSFDuplicateOption;
81 PETSC_EXTERN const char *const PetscSFDuplicateOptions[];
82 
83 PETSC_EXTERN PetscFunctionList PetscSFList;
84 PETSC_EXTERN PetscErrorCode PetscSFRegister(const char[],PetscErrorCode (*)(PetscSF));
85 
86 PETSC_EXTERN PetscErrorCode PetscSFInitializePackage(void);
87 PETSC_EXTERN PetscErrorCode PetscSFFinalizePackage(void);
88 PETSC_EXTERN PetscErrorCode PetscSFCreate(MPI_Comm,PetscSF*);
89 PETSC_EXTERN PetscErrorCode PetscSFDestroy(PetscSF*);
90 PETSC_EXTERN PetscErrorCode PetscSFSetType(PetscSF,PetscSFType);
91 PETSC_EXTERN PetscErrorCode PetscSFGetType(PetscSF,PetscSFType*);
92 PETSC_EXTERN PetscErrorCode PetscSFView(PetscSF,PetscViewer);
93 PETSC_EXTERN PetscErrorCode PetscSFViewFromOptions(PetscSF,PetscObject,const char[]);
94 PETSC_EXTERN PetscErrorCode PetscSFSetUp(PetscSF);
95 PETSC_EXTERN PetscErrorCode PetscSFSetFromOptions(PetscSF);
96 PETSC_EXTERN PetscErrorCode PetscSFDuplicate(PetscSF,PetscSFDuplicateOption,PetscSF*);
97 PETSC_EXTERN PetscErrorCode PetscSFWindowSetSyncType(PetscSF,PetscSFWindowSyncType);
98 PETSC_EXTERN PetscErrorCode PetscSFWindowGetSyncType(PetscSF,PetscSFWindowSyncType*);
99 PETSC_EXTERN PetscErrorCode PetscSFWindowSetFlavorType(PetscSF,PetscSFWindowFlavorType);
100 PETSC_EXTERN PetscErrorCode PetscSFWindowGetFlavorType(PetscSF,PetscSFWindowFlavorType*);
101 PETSC_EXTERN PetscErrorCode PetscSFWindowSetInfo(PetscSF,MPI_Info);
102 PETSC_EXTERN PetscErrorCode PetscSFWindowGetInfo(PetscSF,MPI_Info*);
103 PETSC_EXTERN PetscErrorCode PetscSFSetRankOrder(PetscSF,PetscBool);
104 PETSC_EXTERN PetscErrorCode PetscSFSetGraph(PetscSF,PetscInt,PetscInt,PetscInt*,PetscCopyMode,PetscSFNode*,PetscCopyMode);
105 PETSC_EXTERN PetscErrorCode PetscSFSetGraphWithPattern(PetscSF,PetscLayout,PetscSFPattern);
106 PETSC_EXTERN PetscErrorCode PetscSFGetGraph(PetscSF,PetscInt*,PetscInt*,const PetscInt**,const PetscSFNode**);
107 PETSC_EXTERN PetscErrorCode PetscSFGetLeafRange(PetscSF,PetscInt*,PetscInt*);
108 PETSC_EXTERN PetscErrorCode PetscSFCreateEmbeddedRootSF(PetscSF,PetscInt,const PetscInt*,PetscSF*);
109 PETSC_EXTERN PetscErrorCode PetscSFCreateEmbeddedLeafSF(PetscSF,PetscInt,const PetscInt*,PetscSF*);
110 PETSC_EXTERN PetscErrorCode PetscSFReset(PetscSF);
111 PETSC_EXTERN PetscErrorCode PetscSFSetUpRanks(PetscSF,MPI_Group);
112 PETSC_EXTERN PetscErrorCode PetscSFGetRootRanks(PetscSF,PetscInt*,const PetscMPIInt**,const PetscInt**,const PetscInt**,const PetscInt**);
113 PETSC_EXTERN PetscErrorCode PetscSFGetLeafRanks(PetscSF,PetscInt*,const PetscMPIInt**,const PetscInt**,const PetscInt**);
114 PETSC_EXTERN PetscErrorCode PetscSFGetGroups(PetscSF,MPI_Group*,MPI_Group*);
115 PETSC_EXTERN PetscErrorCode PetscSFGetMultiSF(PetscSF,PetscSF*);
116 PETSC_EXTERN PetscErrorCode PetscSFCreateInverseSF(PetscSF,PetscSF*);
117 PETSC_EXTERN PetscErrorCode PetscSFConcatenate(MPI_Comm,PetscInt,PetscSF[],PetscBool,PetscInt[],PetscSF*);
118 
119 /* Build PetscSF from PetscLayout */
120 PETSC_EXTERN PetscErrorCode PetscSFSetGraphLayout(PetscSF,PetscLayout,PetscInt,PetscInt*,PetscCopyMode,const PetscInt*);
121 PETSC_EXTERN PetscErrorCode PetscSFCreateFromLayouts(PetscLayout,PetscLayout,PetscSF*);
122 PETSC_DEPRECATED_FUNCTION("Use PetscSFCreateFromLayouts (since v3.15)")
123 static inline PetscErrorCode PetscLayoutsCreateSF(PetscLayout rmap, PetscLayout lmap, PetscSF* sf) { return PetscSFCreateFromLayouts(rmap, lmap, sf); }
124 PETSC_EXTERN PetscErrorCode PetscSFCreateByMatchingIndices(PetscLayout,PetscInt,const PetscInt*,const PetscInt*,PetscInt,PetscInt,const PetscInt*,const PetscInt*,PetscInt,PetscSF*,PetscSF*);
125 
126 /* PetscSection interoperability */
127 PETSC_EXTERN PetscErrorCode PetscSFSetGraphSection(PetscSF,PetscSection,PetscSection);
128 PETSC_EXTERN PetscErrorCode PetscSFCreateRemoteOffsets(PetscSF, PetscSection, PetscSection, PetscInt **);
129 PETSC_EXTERN PetscErrorCode PetscSFDistributeSection(PetscSF, PetscSection, PetscInt **, PetscSection);
130 PETSC_EXTERN PetscErrorCode PetscSFCreateSectionSF(PetscSF, PetscSection, PetscInt [], PetscSection, PetscSF *);
131 
132 /* Reduce rootdata to leafdata using provided operation */
133 PETSC_EXTERN PetscErrorCode PetscSFBcastBegin(PetscSF,MPI_Datatype,const void*,void*,MPI_Op)
134   PetscAttrMPIPointerWithType(3,2) PetscAttrMPIPointerWithType(4,2);
135 PETSC_EXTERN PetscErrorCode PetscSFBcastEnd(PetscSF,MPI_Datatype,const void*,void*,MPI_Op)
136   PetscAttrMPIPointerWithType(3,2) PetscAttrMPIPointerWithType(4,2);
137 PETSC_EXTERN PetscErrorCode PetscSFBcastWithMemTypeBegin(PetscSF,MPI_Datatype,PetscMemType,const void*,PetscMemType,void*,MPI_Op)
138   PetscAttrMPIPointerWithType(4,2) PetscAttrMPIPointerWithType(6,2);
139 
140 /* Reduce leafdata into rootdata using provided operation */
141 PETSC_EXTERN PetscErrorCode PetscSFReduceBegin(PetscSF,MPI_Datatype,const void*,void *,MPI_Op)
142   PetscAttrMPIPointerWithType(3,2) PetscAttrMPIPointerWithType(4,2);
143 PETSC_EXTERN PetscErrorCode PetscSFReduceEnd(PetscSF,MPI_Datatype,const void*,void*,MPI_Op)
144   PetscAttrMPIPointerWithType(3,2) PetscAttrMPIPointerWithType(4,2);
145 PETSC_EXTERN PetscErrorCode PetscSFReduceWithMemTypeBegin(PetscSF,MPI_Datatype,PetscMemType,const void*,PetscMemType,void *,MPI_Op)
146   PetscAttrMPIPointerWithType(4,2) PetscAttrMPIPointerWithType(6,2);
147 
148 /* Atomically modifies (using provided operation) rootdata using leafdata from each leaf, value at root at time of modification is returned in leafupdate. */
149 PETSC_EXTERN PetscErrorCode PetscSFFetchAndOpBegin(PetscSF,MPI_Datatype,void*,const void*,void*,MPI_Op)
150   PetscAttrMPIPointerWithType(3,2) PetscAttrMPIPointerWithType(4,2) PetscAttrMPIPointerWithType(5,2);
151 PETSC_EXTERN PetscErrorCode PetscSFFetchAndOpEnd(PetscSF,MPI_Datatype,void*,const void*,void*,MPI_Op)
152   PetscAttrMPIPointerWithType(3,2) PetscAttrMPIPointerWithType(4,2) PetscAttrMPIPointerWithType(5,2);
153 
154 PETSC_EXTERN PetscErrorCode PetscSFFetchAndOpWithMemTypeBegin(PetscSF,MPI_Datatype,PetscMemType,void*,PetscMemType,const void*,PetscMemType,void*,MPI_Op)
155   PetscAttrMPIPointerWithType(4,2) PetscAttrMPIPointerWithType(6,2) PetscAttrMPIPointerWithType(8,2);
156 
157 /* Compute the degree of every root vertex (number of leaves in its star) */
158 PETSC_EXTERN PetscErrorCode PetscSFComputeDegreeBegin(PetscSF,const PetscInt**);
159 PETSC_EXTERN PetscErrorCode PetscSFComputeDegreeEnd(PetscSF,const PetscInt**);
160 PETSC_EXTERN PetscErrorCode PetscSFComputeMultiRootOriginalNumbering(PetscSF,const PetscInt[],PetscInt*,PetscInt*[]);
161 /* Concatenate data from all leaves into roots */
162 PETSC_EXTERN PetscErrorCode PetscSFGatherBegin(PetscSF,MPI_Datatype,const void*,void*)
163   PetscAttrMPIPointerWithType(3,2) PetscAttrMPIPointerWithType(4,2);
164 PETSC_EXTERN PetscErrorCode PetscSFGatherEnd(PetscSF,MPI_Datatype,const void*,void*)
165   PetscAttrMPIPointerWithType(3,2) PetscAttrMPIPointerWithType(4,2);
166 /* Distribute distinct values to each leaf from roots */
167 PETSC_EXTERN PetscErrorCode PetscSFScatterBegin(PetscSF,MPI_Datatype,const void*,void*)
168   PetscAttrMPIPointerWithType(3,2) PetscAttrMPIPointerWithType(4,2);
169 PETSC_EXTERN PetscErrorCode PetscSFScatterEnd(PetscSF,MPI_Datatype,const void*,void*)
170   PetscAttrMPIPointerWithType(3,2) PetscAttrMPIPointerWithType(4,2);
171 
172 PETSC_EXTERN PetscErrorCode PetscSFCompose(PetscSF,PetscSF,PetscSF*);
173 PETSC_EXTERN PetscErrorCode PetscSFComposeInverse(PetscSF,PetscSF,PetscSF*);
174 
175 #define MPIU_REPLACE MPI_REPLACE PETSC_DEPRECATED_MACRO("GCC warning \"MPIU_REPLACE macro is deprecated use MPI_REPLACE (since version 3.15)\"")
176 
177 PETSC_DEPRECATED_FUNCTION("Use PetscSFGetRootRanks (since v3.12)")
178 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 PETSC_DEPRECATED_FUNCTION("Use PetscSFCreateEmbeddedRootSF (since v3.15)")
182 static inline PetscErrorCode PetscSFCreateEmbeddedSF(PetscSF sf,PetscInt nselected,const PetscInt *selected,PetscSF *esf)
183 { return PetscSFCreateEmbeddedRootSF(sf,nselected,selected,esf); }
184 
185 PETSC_DEPRECATED_FUNCTION("Use PetscSFBcastBegin (since v3.15)")
186 static inline PetscErrorCode PetscSFBcastAndOpBegin(PetscSF sf,MPI_Datatype unit,const void* rootdata,void* leafdata,MPI_Op op)
187 { return PetscSFBcastBegin(sf,unit,rootdata,leafdata,op); }
188 
189 PETSC_DEPRECATED_FUNCTION("Use PetscSFBcastEnd (since v3.15)")
190 static inline PetscErrorCode PetscSFBcastAndOpEnd(PetscSF sf,MPI_Datatype unit,const void* rootdata,void* leafdata,MPI_Op op)
191 { return PetscSFBcastEnd(sf,unit,rootdata,leafdata,op); }
192 
193 PETSC_DEPRECATED_FUNCTION("Use PetscSFBcastWithMemTypeBegin (since v3.15)")
194 static inline PetscErrorCode PetscSFBcastAndOpWithMemtypeBegin(PetscSF sf,MPI_Datatype unit,PetscMemType rootmtype,const void *rootdata,PetscMemType leafmtype,void *leafdata,MPI_Op op)
195 { return PetscSFBcastWithMemTypeBegin(sf,unit,rootmtype,rootdata,leafmtype,leafdata,op); }
196 
197 #endif
198