Actual source code: sfimpl.h
1: #pragma once
3: #include <petscvec.h>
4: #include <petscsf.h>
5: #include <petsc/private/deviceimpl.h>
6: #include <petsc/private/mpiutils.h>
7: #include <petsc/private/petscimpl.h>
9: PETSC_EXTERN PetscLogEvent PETSCSF_SetGraph;
10: PETSC_EXTERN PetscLogEvent PETSCSF_SetUp;
11: PETSC_EXTERN PetscLogEvent PETSCSF_BcastBegin;
12: PETSC_EXTERN PetscLogEvent PETSCSF_BcastEnd;
13: PETSC_EXTERN PetscLogEvent PETSCSF_BcastBegin;
14: PETSC_EXTERN PetscLogEvent PETSCSF_BcastEnd;
15: PETSC_EXTERN PetscLogEvent PETSCSF_ReduceBegin;
16: PETSC_EXTERN PetscLogEvent PETSCSF_ReduceEnd;
17: PETSC_EXTERN PetscLogEvent PETSCSF_FetchAndOpBegin;
18: PETSC_EXTERN PetscLogEvent PETSCSF_FetchAndOpEnd;
19: PETSC_EXTERN PetscLogEvent PETSCSF_EmbedSF;
20: PETSC_EXTERN PetscLogEvent PETSCSF_DistSect;
21: PETSC_EXTERN PetscLogEvent PETSCSF_SectSF;
22: PETSC_EXTERN PetscLogEvent PETSCSF_RemoteOff;
23: PETSC_EXTERN PetscLogEvent PETSCSF_Pack;
24: PETSC_EXTERN PetscLogEvent PETSCSF_Unpack;
26: typedef enum {
27: PETSCSF_../../..2LEAF = 0,
28: PETSCSF_LEAF2../../..
29: } PetscSFDirection;
30: typedef enum {
31: PETSCSF_BCAST = 0,
32: PETSCSF_REDUCE,
33: PETSCSF_FETCH
34: } PetscSFOperation;
35: /* When doing device-aware MPI, a backend refers to the SF/device interface */
36: typedef enum {
37: PETSCSF_BACKEND_INVALID = 0,
38: PETSCSF_BACKEND_CUDA,
39: PETSCSF_BACKEND_HIP,
40: PETSCSF_BACKEND_KOKKOS
41: } PetscSFBackend;
43: struct _PetscSFOps {
44: PetscErrorCode (*Reset)(PetscSF);
45: PetscErrorCode (*Destroy)(PetscSF);
46: PetscErrorCode (*SetUp)(PetscSF);
47: PetscErrorCode (*SetFromOptions)(PetscSF, PetscOptionItems *);
48: PetscErrorCode (*View)(PetscSF, PetscViewer);
49: PetscErrorCode (*Duplicate)(PetscSF, PetscSFDuplicateOption, PetscSF);
50: PetscErrorCode (*BcastBegin)(PetscSF, MPI_Datatype, PetscMemType, const void *, PetscMemType, void *, MPI_Op);
51: PetscErrorCode (*BcastEnd)(PetscSF, MPI_Datatype, const void *, void *, MPI_Op);
52: PetscErrorCode (*ReduceBegin)(PetscSF, MPI_Datatype, PetscMemType, const void *, PetscMemType, void *, MPI_Op);
53: PetscErrorCode (*ReduceEnd)(PetscSF, MPI_Datatype, const void *, void *, MPI_Op);
54: PetscErrorCode (*FetchAndOpBegin)(PetscSF, MPI_Datatype, PetscMemType, void *, PetscMemType, const void *, void *, MPI_Op);
55: PetscErrorCode (*FetchAndOpEnd)(PetscSF, MPI_Datatype, void *, const void *, void *, MPI_Op);
56: PetscErrorCode (*BcastToZero)(PetscSF, MPI_Datatype, PetscMemType, const void *, PetscMemType, void *); /* For internal use only */
57: PetscErrorCode (*GetRootRanks)(PetscSF, PetscInt *, const PetscMPIInt **, const PetscInt **, const PetscInt **, const PetscInt **);
58: PetscErrorCode (*GetLeafRanks)(PetscSF, PetscInt *, const PetscMPIInt **, const PetscInt **, const PetscInt **);
59: PetscErrorCode (*CreateLocalSF)(PetscSF, PetscSF *);
60: PetscErrorCode (*GetGraph)(PetscSF, PetscInt *, PetscInt *, const PetscInt **, const PetscSFNode **);
61: PetscErrorCode (*CreateEmbeddedRootSF)(PetscSF, PetscInt, const PetscInt *, PetscSF *);
62: PetscErrorCode (*CreateEmbeddedLeafSF)(PetscSF, PetscInt, const PetscInt *, PetscSF *);
64: PetscErrorCode (*Malloc)(PetscMemType, size_t, void **);
65: PetscErrorCode (*Free)(PetscMemType, void *);
66: };
68: typedef struct _n_PetscSFPackOpt *PetscSFPackOpt;
70: struct _p_PetscSF {
71: PETSCHEADER(struct _PetscSFOps);
72: struct { /* Fields needed to implement VecScatter behavior */
73: PetscInt from_n, to_n; /* Recorded local sizes of the input from/to vectors in VecScatterCreate(). Used subsequently for error checking. */
74: PetscBool beginandendtogether; /* Indicates that the scatter begin and end function are called together, VecScatterEnd() is then treated as a nop */
75: const PetscScalar *xdata; /* Vector data to read from */
76: PetscScalar *ydata; /* Vector data to write to. The two pointers are recorded in VecScatterBegin. Memory is not managed by SF. */
77: PetscSF lsf; /* The local part of the scatter, used in SCATTER_LOCAL. Built on demand. */
78: PetscInt bs; /* Block size, determined by IS passed to VecScatterCreate */
79: MPI_Datatype unit; /* one unit = bs PetscScalars */
80: PetscBool logging; /* Indicate if vscat log events are happening. If yes, avoid duplicated SF logging to have clear -log_view */
81: } vscat;
83: /* Fields for generic PetscSF functionality */
84: PetscInt nroots; /* Number of root vertices on current process (candidates for incoming edges) */
85: PetscInt nleaves; /* Number of leaf vertices on current process (this process specifies a root for each leaf) */
86: PetscInt *mine; /* Location of leaves in leafdata arrays provided to the communication routines */
87: PetscInt *mine_alloc;
88: PetscInt minleaf, maxleaf;
89: PetscSFNode *remote; /* Remote references to roots for each local leaf */
90: PetscSFNode *remote_alloc;
91: PetscInt nranks; /* Number of ranks owning roots connected to my leaves */
92: PetscInt ndranks; /* Number of ranks in distinguished group holding roots connected to my leaves */
93: PetscMPIInt *ranks; /* List of ranks referenced by "remote" */
94: PetscInt *roffset; /* Array of length nranks+1, offset in rmine/rremote for each rank */
95: PetscInt *rmine; /* Concatenated array holding local indices referencing each remote rank */
96: PetscInt *rmine_d[2]; /* A copy of rmine[local/remote] in device memory if needed */
98: /* Some results useful in packing by analyzing rmine[] */
99: PetscInt leafbuflen[2]; /* Length (in unit) of leaf buffers, in layout of [PETSCSF_LOCAL/REMOTE] */
100: PetscBool leafcontig[2]; /* True means indices in rmine[self part] or rmine[remote part] are contiguous, and they start from ... */
101: PetscInt leafstart[2]; /* ... leafstart[0] and leafstart[1] respectively */
102: PetscSFPackOpt leafpackopt[2]; /* Optimization plans to (un)pack leaves connected to remote roots, based on index patterns in rmine[]. NULL for no optimization */
103: PetscSFPackOpt leafpackopt_d[2]; /* Copy of leafpackopt_d[] on device if needed */
104: PetscBool leafdups[2]; /* Indices in rmine[] for self(0)/remote(1) communication have dups respectively? TRUE implies theads working on them in parallel may have data race. */
106: PetscInt nleafreqs; /* Number of MPI requests for leaves */
107: PetscInt *rremote; /* Concatenated array holding remote indices referenced for each remote rank */
108: PetscBool degreeknown; /* The degree is currently known, do not have to recompute */
109: PetscInt *degree; /* Degree of each of my root vertices */
110: PetscInt *degreetmp; /* Temporary local array for computing degree */
111: PetscBool rankorder; /* Sort ranks for gather and scatter operations */
112: MPI_Group ingroup; /* Group of processes connected to my roots */
113: MPI_Group outgroup; /* Group of processes connected to my leaves */
114: PetscSF multi; /* Internal graph used to implement gather and scatter operations */
115: PetscBool graphset; /* Flag indicating that the graph has been set, required before calling communication routines */
116: PetscBool setupcalled; /* Type and communication structures have been set up */
117: PetscSFPattern pattern; /* Pattern of the graph */
118: PetscBool persistent; /* Does this SF use MPI persistent requests for communication */
119: PetscLayout map; /* Layout of leaves over all processes when building a patterned graph */
120: PetscBool unknown_input_stream; /* If true, SF does not know which streams root/leafdata is on. Default is false, since we only use petsc default stream */
121: PetscBool use_gpu_aware_mpi; /* If true, SF assumes it can pass GPU pointers to MPI */
122: PetscBool use_stream_aware_mpi; /* If true, SF assumes the underlying MPI is cuda-stream aware and we won't sync streams for send/recv buffers passed to MPI */
123: PetscInt maxResidentThreadsPerGPU;
124: PetscBool allow_multi_leaves;
125: PetscSFBackend backend; /* The device backend (if any) SF will use */
126: void *data; /* Pointer to implementation */
128: #if defined(PETSC_HAVE_NVSHMEM)
129: PetscBool use_nvshmem; /* TRY to use nvshmem on cuda devices with this SF when possible */
130: PetscBool use_nvshmem_get; /* If true, use nvshmem_get based protocol, otherwise, use nvshmem_put based protocol */
131: PetscBool checked_nvshmem_eligibility; /* Have we checked eligibility of using NVSHMEM on this sf? */
132: PetscBool setup_nvshmem; /* Have we already set up NVSHMEM related fields below? These fields are built on-demand */
133: PetscInt leafbuflen_rmax; /* max leafbuflen[REMOTE] over comm */
134: PetscInt nRemoteRootRanks; /* nranks - ndranks */
135: PetscInt nRemoteRootRanksMax; /* max nranks-ndranks over comm */
137: /* The following two fields look confusing but actually make sense: They are offsets of buffers at the remote side. We're doing one-sided communication! */
138: PetscInt *rootsigdisp; /* [nRemoteRootRanks]. For my i-th remote root rank, I will access its rootsigdisp[i]-th root signal */
139: PetscInt *rootbufdisp; /* [nRemoteRootRanks]. For my i-th remote root rank, I will access its root buf at offset rootbufdisp[i], in <unit> to be set */
141: PetscInt *rootbufdisp_d;
142: PetscInt *rootsigdisp_d; /* Copy of rootsigdisp[] on device */
143: PetscMPIInt *ranks_d; /* Copy of the remote part of (root) ranks[] on device */
144: PetscInt *roffset_d; /* Copy of the remote part of roffset[] on device */
145: #endif
146: #if defined(PETSC_HAVE_MPIX_STREAM)
147: MPIX_Stream mpi_stream;
148: MPI_Comm stream_comm; /* gpu stream aware MPI communicator */
149: #endif
150: };
152: PETSC_EXTERN PetscBool PetscSFRegisterAllCalled;
153: PETSC_EXTERN PetscErrorCode PetscSFRegisterAll(void);
155: PETSC_INTERN PetscErrorCode PetscSFCreateLocalSF_Private(PetscSF, PetscSF *);
156: PETSC_INTERN PetscErrorCode PetscSFBcastToZero_Private(PetscSF, MPI_Datatype, const void *, void *) PETSC_ATTRIBUTE_MPI_POINTER_WITH_TYPE(3, 2) PETSC_ATTRIBUTE_MPI_POINTER_WITH_TYPE(4, 2);
158: PETSC_EXTERN PetscErrorCode MPIPetsc_Type_unwrap(MPI_Datatype, MPI_Datatype *, PetscBool *);
159: PETSC_EXTERN PetscErrorCode MPIPetsc_Type_compare(MPI_Datatype, MPI_Datatype, PetscBool *);
160: PETSC_EXTERN PetscErrorCode MPIPetsc_Type_compare_contig(MPI_Datatype, MPI_Datatype, PetscInt *);
162: #if defined(PETSC_HAVE_MPI_NONBLOCKING_COLLECTIVES)
163: #define MPIU_Ibcast(a, b, c, d, e, req) MPI_Ibcast(a, b, c, d, e, req)
164: #define MPIU_Ireduce(a, b, c, d, e, f, g, req) MPI_Ireduce(a, b, c, d, e, f, g, req)
165: #define MPIU_Iscatter(a, b, c, d, e, f, g, h, req) MPI_Iscatter(a, b, c, d, e, f, g, h, req)
166: #define MPIU_Iscatterv(a, b, c, d, e, f, g, h, i, req) MPI_Iscatterv(a, b, c, d, e, f, g, h, i, req)
167: #define MPIU_Igather(a, b, c, d, e, f, g, h, req) MPI_Igather(a, b, c, d, e, f, g, h, req)
168: #define MPIU_Igatherv(a, b, c, d, e, f, g, h, i, req) MPI_Igatherv(a, b, c, d, e, f, g, h, i, req)
169: #define MPIU_Iallgather(a, b, c, d, e, f, g, req) MPI_Iallgather(a, b, c, d, e, f, g, req)
170: #define MPIU_Iallgatherv(a, b, c, d, e, f, g, h, req) MPI_Iallgatherv(a, b, c, d, e, f, g, h, req)
171: #define MPIU_Ialltoall(a, b, c, d, e, f, g, req) MPI_Ialltoall(a, b, c, d, e, f, g, req)
172: #else
173: /* Ignore req, the MPI_Request argument, and use MPI blocking collectives. One should initialize req
174: to MPI_REQUEST_NULL so that one can do MPI_Wait(req,status) no matter the call is blocking or not.
175: */
176: #define MPIU_Ibcast(a, b, c, d, e, req) MPI_Bcast(a, b, c, d, e)
177: #define MPIU_Ireduce(a, b, c, d, e, f, g, req) MPI_Reduce(a, b, c, d, e, f, g)
178: #define MPIU_Iscatter(a, b, c, d, e, f, g, h, req) MPI_Scatter(a, b, c, d, e, f, g, h)
179: #define MPIU_Iscatterv(a, b, c, d, e, f, g, h, i, req) MPI_Scatterv(a, b, c, d, e, f, g, h, i)
180: #define MPIU_Igather(a, b, c, d, e, f, g, h, req) MPI_Gather(a, b, c, d, e, f, g, h)
181: #define MPIU_Igatherv(a, b, c, d, e, f, g, h, i, req) MPI_Gatherv(a, b, c, d, e, f, g, h, i)
182: #define MPIU_Iallgather(a, b, c, d, e, f, g, req) MPI_Allgather(a, b, c, d, e, f, g)
183: #define MPIU_Iallgatherv(a, b, c, d, e, f, g, h, req) MPI_Allgatherv(a, b, c, d, e, f, g, h)
184: #define MPIU_Ialltoall(a, b, c, d, e, f, g, req) MPI_Alltoall(a, b, c, d, e, f, g)
185: #endif
187: PETSC_EXTERN PetscErrorCode VecScatterGetRemoteCount_Private(VecScatter, PetscBool, PetscInt *, PetscInt *);
188: PETSC_EXTERN PetscErrorCode VecScatterGetRemote_Private(VecScatter, PetscBool, PetscInt *, const PetscInt **, const PetscInt **, const PetscMPIInt **, PetscInt *);
189: PETSC_EXTERN PetscErrorCode VecScatterGetRemoteOrdered_Private(VecScatter, PetscBool, PetscInt *, const PetscInt **, const PetscInt **, const PetscMPIInt **, PetscInt *);
190: PETSC_EXTERN PetscErrorCode VecScatterRestoreRemote_Private(VecScatter, PetscBool, PetscInt *, const PetscInt **, const PetscInt **, const PetscMPIInt **, PetscInt *);
191: PETSC_EXTERN PetscErrorCode VecScatterRestoreRemoteOrdered_Private(VecScatter, PetscBool, PetscInt *, const PetscInt **, const PetscInt **, const PetscMPIInt **, PetscInt *);
193: #if defined(PETSC_HAVE_CUDA)
194: PETSC_EXTERN PetscErrorCode PetscSFMalloc_CUDA(PetscMemType, size_t, void **);
195: PETSC_EXTERN PetscErrorCode PetscSFFree_CUDA(PetscMemType, void *);
196: #endif
197: #if defined(PETSC_HAVE_HIP)
198: PETSC_EXTERN PetscErrorCode PetscSFMalloc_HIP(PetscMemType, size_t, void **);
199: PETSC_EXTERN PetscErrorCode PetscSFFree_HIP(PetscMemType, void *);
200: #endif
201: #if defined(PETSC_HAVE_KOKKOS)
202: PETSC_EXTERN PetscErrorCode PetscSFMalloc_Kokkos(PetscMemType, size_t, void **);
203: PETSC_EXTERN PetscErrorCode PetscSFFree_Kokkos(PetscMemType, void *);
204: #endif
206: /* SF only supports CUDA and Kokkos devices. Even VIENNACL is a device, its device pointers are invisible to SF.
207: Through VecGetArray(), we copy data of VECVIENNACL from device to host and pass host pointers to SF.
208: */
209: #if defined(PETSC_HAVE_CUDA) || defined(PETSC_HAVE_KOKKOS) || defined(PETSC_HAVE_HIP)
210: #define PetscSFMalloc(sf, mtype, sz, ptr) ((*(sf)->ops->Malloc)(mtype, sz, ptr))
211: /* Free memory and set ptr to NULL when succeeded */
212: #define PetscSFFree(sf, mtype, ptr) ((PetscErrorCode)((ptr) && ((*(sf)->ops->Free)(mtype, ptr) || ((ptr) = NULL, PETSC_SUCCESS))))
213: #else
214: /* If pure host code, do with less indirection */
215: #define PetscSFMalloc(sf, mtype, sz, ptr) PetscMalloc(sz, ptr)
216: #define PetscSFFree(sf, mtype, ptr) PetscFree(ptr)
217: #endif