xref: /petsc/include/petscsf.h (revision dd5b3ca66ca4de5e00e985c205d0f80060c552e8)
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 <petscis.h>
8 #include <petscsftypes.h>
9 
10 PETSC_EXTERN PetscClassId PETSCSF_CLASSID;
11 
12 /*J
13     PetscSFType - String with the name of a PetscSF type
14 
15    Level: beginner
16 
17 .seealso: PetscSFSetType(), PetscSF
18 J*/
19 typedef const char *PetscSFType;
20 #define PETSCSFBASIC      "basic"
21 #define PETSCSFALLGATHERV "allgatherv"
22 #define PETSCSFALLGATHER  "allgather"
23 #define PETSCSFGATHERV    "gatherv"
24 #define PETSCSFGATHER     "gather"
25 #define PETSCSFALLTOALL   "alltoall"
26 #define PETSCSFWINDOW     "window"
27 
28 /*E
29    PetscSFPattern - Pattern of the PetscSF graph
30 
31 $  PETSCSF_PATTERN_GENERAL   - A general graph. One sets the graph with PetscSFSetGraph() and usually does not use this enum directly.
32 $  PETSCSF_PATTERN_ALLGATHER - A graph that every rank gathers all roots from all ranks (like MPI_Allgather/v). One sets the graph with PetscSFSetGraphWithPattern().
33 $  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().
34 $  PETSCSF_PATTERN_ALLTOALL  - A graph that every rank gathers different roots from all ranks (like MPI_Alltoall). One sets the graph with PetscSFSetGraphWithPattern().
35                                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
36                                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
37                                create a new MPI datatype for the multiple data items, e.g., by MPI_Type_contiguous.
38    Level: beginner
39 
40 .seealso: PetscSFSetGraph(), PetscSFSetGraphWithPattern()
41 E*/
42 typedef enum {PETSCSF_PATTERN_GENERAL=0,PETSCSF_PATTERN_ALLGATHER,PETSCSF_PATTERN_GATHER,PETSCSF_PATTERN_ALLTOALL} PetscSFPattern;
43 
44 /*E
45     PetscSFWindowSyncType - Type of synchronization for PETSCSFWINDOW
46 
47 $  PETSCSF_WINDOW_SYNC_FENCE - simplest model, synchronizing across communicator
48 $  PETSCSF_WINDOW_SYNC_LOCK - passive model, less synchronous, requires less setup than PETSCSF_WINDOW_SYNC_ACTIVE, but may require more handshakes
49 $  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)
50 
51    Level: advanced
52 
53 .seealso: PetscSFWindowSetSyncType(), PetscSFWindowGetSyncType()
54 E*/
55 typedef enum {PETSCSF_WINDOW_SYNC_FENCE,PETSCSF_WINDOW_SYNC_LOCK,PETSCSF_WINDOW_SYNC_ACTIVE} PetscSFWindowSyncType;
56 PETSC_EXTERN const char *const PetscSFWindowSyncTypes[];
57 
58 /*E
59     PetscSFDuplicateOption - Aspects to preserve when duplicating a PetscSF
60 
61 $  PETSCSF_DUPLICATE_CONFONLY - configuration only, user must call PetscSFSetGraph()
62 $  PETSCSF_DUPLICATE_RANKS - communication ranks preserved, but different graph (allows simpler setup after calling PetscSFSetGraph())
63 $  PETSCSF_DUPLICATE_GRAPH - entire graph duplicated
64 
65    Level: beginner
66 
67 .seealso: PetscSFDuplicate()
68 E*/
69 typedef enum {PETSCSF_DUPLICATE_CONFONLY,PETSCSF_DUPLICATE_RANKS,PETSCSF_DUPLICATE_GRAPH} PetscSFDuplicateOption;
70 PETSC_EXTERN const char *const PetscSFDuplicateOptions[];
71 
72 PETSC_EXTERN PetscFunctionList PetscSFList;
73 PETSC_EXTERN PetscErrorCode PetscSFRegister(const char[],PetscErrorCode (*)(PetscSF));
74 
75 PETSC_EXTERN PetscErrorCode PetscSFInitializePackage(void);
76 PETSC_EXTERN PetscErrorCode PetscSFFinalizePackage(void);
77 PETSC_EXTERN PetscErrorCode PetscSFCreate(MPI_Comm,PetscSF*);
78 PETSC_EXTERN PetscErrorCode PetscSFDestroy(PetscSF*);
79 PETSC_EXTERN PetscErrorCode PetscSFSetType(PetscSF,PetscSFType);
80 PETSC_EXTERN PetscErrorCode PetscSFGetType(PetscSF,PetscSFType*);
81 PETSC_EXTERN PetscErrorCode PetscSFView(PetscSF,PetscViewer);
82 PETSC_STATIC_INLINE PetscErrorCode PetscSFViewFromOptions(PetscSF A,PetscObject obj,const char name[]) {return PetscObjectViewFromOptions((PetscObject)A,obj,name);}
83 PETSC_EXTERN PetscErrorCode PetscSFSetUp(PetscSF);
84 PETSC_EXTERN PetscErrorCode PetscSFSetFromOptions(PetscSF);
85 PETSC_EXTERN PetscErrorCode PetscSFDuplicate(PetscSF,PetscSFDuplicateOption,PetscSF*);
86 PETSC_EXTERN PetscErrorCode PetscSFWindowSetSyncType(PetscSF,PetscSFWindowSyncType);
87 PETSC_EXTERN PetscErrorCode PetscSFWindowGetSyncType(PetscSF,PetscSFWindowSyncType*);
88 PETSC_EXTERN PetscErrorCode PetscSFSetRankOrder(PetscSF,PetscBool);
89 PETSC_EXTERN PetscErrorCode PetscSFSetGraph(PetscSF,PetscInt,PetscInt,const PetscInt*,PetscCopyMode,const PetscSFNode*,PetscCopyMode);
90 PETSC_EXTERN PetscErrorCode PetscSFSetGraphWithPattern(PetscSF,PetscLayout,PetscSFPattern);
91 PETSC_EXTERN PetscErrorCode PetscSFGetGraph(PetscSF,PetscInt*,PetscInt*,const PetscInt**,const PetscSFNode**);
92 PETSC_EXTERN PetscErrorCode PetscSFGetLeafRange(PetscSF,PetscInt*,PetscInt*);
93 PETSC_EXTERN PetscErrorCode PetscSFCreateEmbeddedSF(PetscSF,PetscInt,const PetscInt*,PetscSF*);
94 PETSC_EXTERN PetscErrorCode PetscSFCreateEmbeddedLeafSF(PetscSF,PetscInt,const PetscInt *, PetscSF *);
95 PETSC_EXTERN PetscErrorCode PetscSFReset(PetscSF);
96 PETSC_EXTERN PetscErrorCode PetscSFSetUpRanks(PetscSF,MPI_Group);
97 PETSC_EXTERN PetscErrorCode PetscSFGetRootRanks(PetscSF,PetscInt*,const PetscMPIInt**,const PetscInt**,const PetscInt**,const PetscInt**);
98 PETSC_EXTERN PetscErrorCode PetscSFGetLeafRanks(PetscSF,PetscInt*,const PetscMPIInt**,const PetscInt**,const PetscInt**);
99 PETSC_EXTERN PetscErrorCode PetscSFGetGroups(PetscSF,MPI_Group*,MPI_Group*);
100 PETSC_EXTERN PetscErrorCode PetscSFGetMultiSF(PetscSF,PetscSF*);
101 PETSC_EXTERN PetscErrorCode PetscSFCreateInverseSF(PetscSF,PetscSF*);
102 
103 /* Reduce rootdata to leafdata using provided operation */
104 PETSC_EXTERN PetscErrorCode PetscSFBcastAndOpBegin(PetscSF,MPI_Datatype,const void*,void*,MPI_Op)
105   PetscAttrMPIPointerWithType(3,2) PetscAttrMPIPointerWithType(4,2);
106 PETSC_EXTERN PetscErrorCode PetscSFBcastAndOpEnd(PetscSF,MPI_Datatype,const void*,void*,MPI_Op)
107   PetscAttrMPIPointerWithType(3,2) PetscAttrMPIPointerWithType(4,2);
108 /* Reduce leafdata into rootdata using provided operation */
109 PETSC_EXTERN PetscErrorCode PetscSFReduceBegin(PetscSF,MPI_Datatype,const void*,void *,MPI_Op)
110   PetscAttrMPIPointerWithType(3,2) PetscAttrMPIPointerWithType(4,2);
111 PETSC_EXTERN PetscErrorCode PetscSFReduceEnd(PetscSF,MPI_Datatype,const void*,void*,MPI_Op)
112   PetscAttrMPIPointerWithType(3,2) PetscAttrMPIPointerWithType(4,2);
113 /* Atomically modifies (using provided operation) rootdata using leafdata from each leaf, value at root at time of modification is returned in leafupdate. */
114 PETSC_EXTERN PetscErrorCode PetscSFFetchAndOpBegin(PetscSF,MPI_Datatype,void*,const void*,void*,MPI_Op)
115   PetscAttrMPIPointerWithType(3,2) PetscAttrMPIPointerWithType(4,2) PetscAttrMPIPointerWithType(5,2);
116 PETSC_EXTERN PetscErrorCode PetscSFFetchAndOpEnd(PetscSF,MPI_Datatype,void*,const void*,void*,MPI_Op)
117   PetscAttrMPIPointerWithType(3,2) PetscAttrMPIPointerWithType(4,2) PetscAttrMPIPointerWithType(5,2);
118 /* Compute the degree of every root vertex (number of leaves in its star) */
119 PETSC_EXTERN PetscErrorCode PetscSFComputeDegreeBegin(PetscSF,const PetscInt**);
120 PETSC_EXTERN PetscErrorCode PetscSFComputeDegreeEnd(PetscSF,const PetscInt**);
121 PETSC_EXTERN PetscErrorCode PetscSFComputeMultiRootOriginalNumbering(PetscSF,const PetscInt[],PetscInt*,PetscInt*[]);
122 /* Concatenate data from all leaves into roots */
123 PETSC_EXTERN PetscErrorCode PetscSFGatherBegin(PetscSF,MPI_Datatype,const void*,void*)
124   PetscAttrMPIPointerWithType(3,2) PetscAttrMPIPointerWithType(4,2);
125 PETSC_EXTERN PetscErrorCode PetscSFGatherEnd(PetscSF,MPI_Datatype,const void*,void*)
126   PetscAttrMPIPointerWithType(3,2) PetscAttrMPIPointerWithType(4,2);
127 /* Distribute distinct values to each leaf from roots */
128 PETSC_EXTERN PetscErrorCode PetscSFScatterBegin(PetscSF,MPI_Datatype,const void*,void*)
129   PetscAttrMPIPointerWithType(3,2) PetscAttrMPIPointerWithType(4,2);
130 PETSC_EXTERN PetscErrorCode PetscSFScatterEnd(PetscSF,MPI_Datatype,const void*,void*)
131   PetscAttrMPIPointerWithType(3,2) PetscAttrMPIPointerWithType(4,2);
132 
133 PETSC_EXTERN PetscErrorCode PetscSFCompose(PetscSF,PetscSF,PetscSF*);
134 
135 #if defined(MPI_REPLACE)
136 #  define MPIU_REPLACE MPI_REPLACE
137 #else
138 /* When using an old MPI such that MPI_REPLACE is not defined, we do not pass MPI_REPLACE to MPI at all.  Instead, we
139  * use it as a flag for our own reducer in the PETSCSFBASIC implementation.  This could be any unique value unlikely to
140  * collide with another MPI_Op so we'll just use the value that has been used by every version of MPICH since
141  * MPICH2-1.0.6. */
142 #  define MPIU_REPLACE (MPI_Op)(0x5800000d)
143 #endif
144 
145 PETSC_DEPRECATED_FUNCTION("Use PetscSFGetRootRanks (since v3.12)")
146 PETSC_STATIC_INLINE PetscErrorCode PetscSFGetRanks(PetscSF sf,PetscInt *nranks,const PetscMPIInt **ranks,const PetscInt **roffset,const PetscInt **rmine,const PetscInt **rremote) {
147   return PetscSFGetRootRanks(sf,nranks,ranks,roffset,rmine,rremote);
148 }
149 
150 PETSC_STATIC_INLINE PetscErrorCode PetscSFBcastBegin(PetscSF sf,MPI_Datatype unit,const void* rootdata,void* leafdata) {
151   return PetscSFBcastAndOpBegin(sf,unit,rootdata,leafdata,MPIU_REPLACE);
152 }
153 PETSC_STATIC_INLINE PetscErrorCode PetscSFBcastEnd(PetscSF sf,MPI_Datatype unit,const void* rootdata,void* leafdata) {
154   return PetscSFBcastAndOpEnd(sf,unit,rootdata,leafdata,MPIU_REPLACE);
155 }
156 
157 #endif
158