Actual source code: sfnvshmem.cu
1: #include <petsc/private/deviceimpl.h>
2: #include <petsc/private/veccupmimpl.h>
3: #include <petscdevice_cuda.h>
4: #include <../src/vec/is/sf/impls/basic/sfpack.h>
5: #include <mpi.h>
6: #include <nvshmem.h>
7: #include <nvshmemx.h>
9: PetscErrorCode PetscNvshmemInitializeCheck(void)
10: {
11: PetscFunctionBegin;
12: if (!PetscNvshmemInitialized) { /* Note NVSHMEM does not provide a routine to check whether it is initialized */
13: nvshmemx_init_attr_t attr;
14: attr.mpi_comm = &PETSC_COMM_WORLD;
15: PetscCall(PetscDeviceInitialize(PETSC_DEVICE_CUDA));
16: PetscCall(nvshmemx_init_attr(NVSHMEMX_INIT_WITH_MPI_COMM, &attr));
17: PetscNvshmemInitialized = PETSC_TRUE;
18: PetscBeganNvshmem = PETSC_TRUE;
19: }
20: PetscFunctionReturn(PETSC_SUCCESS);
21: }
23: PetscErrorCode PetscNvshmemMalloc(size_t size, void **ptr)
24: {
25: PetscFunctionBegin;
26: PetscCall(PetscNvshmemInitializeCheck());
27: *ptr = nvshmem_malloc(size);
28: PetscCheck(*ptr, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "nvshmem_malloc() failed to allocate %zu bytes", size);
29: PetscFunctionReturn(PETSC_SUCCESS);
30: }
32: PetscErrorCode PetscNvshmemCalloc(size_t size, void **ptr)
33: {
34: PetscFunctionBegin;
35: PetscCall(PetscNvshmemInitializeCheck());
36: *ptr = nvshmem_calloc(size, 1);
37: PetscCheck(*ptr, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "nvshmem_calloc() failed to allocate %zu bytes", size);
38: PetscFunctionReturn(PETSC_SUCCESS);
39: }
41: PetscErrorCode PetscNvshmemFree_Private(void *ptr)
42: {
43: PetscFunctionBegin;
44: nvshmem_free(ptr);
45: PetscFunctionReturn(PETSC_SUCCESS);
46: }
48: /*@
49: PetscNvshmemFinalize - Tear down NVSHMEM after PETSc is done using it
51: Collective
53: Level: developer
55: Note:
56: Called internally during `PetscFinalize()` if PETSc previously initialized NVSHMEM. Users normally
57: do not need to call this directly.
59: .seealso: `PetscFinalize()`, `PetscSF`
60: @*/
61: PetscErrorCode PetscNvshmemFinalize(void)
62: {
63: PetscFunctionBegin;
64: nvshmem_finalize();
65: PetscFunctionReturn(PETSC_SUCCESS);
66: }
68: /* Free nvshmem related fields in the SF */
69: PetscErrorCode PetscSFReset_Basic_NVSHMEM(PetscSF sf)
70: {
71: PetscSF_Basic *bas = (PetscSF_Basic *)sf->data;
73: PetscFunctionBegin;
74: PetscCall(PetscFree2(bas->leafsigdisp, bas->leafbufdisp));
75: PetscCall(PetscSFFree(sf, PETSC_MEMTYPE_CUDA, bas->leafbufdisp_d));
76: PetscCall(PetscSFFree(sf, PETSC_MEMTYPE_CUDA, bas->leafsigdisp_d));
77: PetscCall(PetscSFFree(sf, PETSC_MEMTYPE_CUDA, bas->iranks_d));
78: PetscCall(PetscSFFree(sf, PETSC_MEMTYPE_CUDA, bas->ioffset_d));
80: PetscCall(PetscFree2(sf->rootsigdisp, sf->rootbufdisp));
81: PetscCall(PetscSFFree(sf, PETSC_MEMTYPE_CUDA, sf->rootbufdisp_d));
82: PetscCall(PetscSFFree(sf, PETSC_MEMTYPE_CUDA, sf->rootsigdisp_d));
83: PetscCall(PetscSFFree(sf, PETSC_MEMTYPE_CUDA, sf->ranks_d));
84: PetscCall(PetscSFFree(sf, PETSC_MEMTYPE_CUDA, sf->roffset_d));
85: PetscFunctionReturn(PETSC_SUCCESS);
86: }
88: /* Set up NVSHMEM related fields for an SF of type SFBASIC (only after PetscSFSetup_Basic() already set up dependent fields) */
89: static PetscErrorCode PetscSFSetUp_Basic_NVSHMEM(PetscSF sf)
90: {
91: PetscDeviceContext dctx;
92: cudaStream_t *stream;
93: PetscSF_Basic *bas = (PetscSF_Basic *)sf->data;
94: PetscInt i, nRemoteRootRanks, nRemoteLeafRanks;
95: PetscInt tmp, rtmp[4]; /* tmps for send/recv buffers */
96: PetscMPIInt tag;
97: MPI_Comm comm;
98: MPI_Request *rootreqs, *leafreqs;
100: PetscFunctionBegin;
101: PetscCall(PetscDeviceContextGetCurrentContextAssertType_Internal(&dctx, PETSC_DEVICE_CUDA));
102: PetscCall(PetscDeviceContextGetStreamHandle(dctx, (void **)&stream));
103: PetscCall(PetscObjectGetComm((PetscObject)sf, &comm));
104: PetscCall(PetscObjectGetNewTag((PetscObject)sf, &tag));
106: nRemoteRootRanks = sf->nranks - sf->ndranks;
107: nRemoteLeafRanks = bas->niranks - bas->ndiranks;
108: sf->nRemoteRootRanks = nRemoteRootRanks;
109: bas->nRemoteLeafRanks = nRemoteLeafRanks;
111: PetscCall(PetscMalloc2(nRemoteLeafRanks, &rootreqs, nRemoteRootRanks, &leafreqs));
113: rtmp[0] = nRemoteRootRanks;
114: rtmp[1] = sf->leafbuflen[PETSCSF_REMOTE];
115: rtmp[2] = nRemoteLeafRanks;
116: rtmp[3] = bas->rootbuflen[PETSCSF_REMOTE];
118: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, rtmp, 4, MPIU_INT, MPI_MAX, comm));
120: sf->nRemoteRootRanksMax = rtmp[0];
121: sf->leafbuflen_rmax = rtmp[1];
122: bas->nRemoteLeafRanksMax = rtmp[2];
123: bas->rootbuflen_rmax = rtmp[3];
125: /* Total four rounds of MPI communications to set up the nvshmem fields */
127: /* Root ranks to leaf ranks: send info about rootsigdisp[] and rootbufdisp[] */
128: PetscCall(PetscMalloc2(nRemoteRootRanks, &sf->rootsigdisp, nRemoteRootRanks, &sf->rootbufdisp));
129: for (i = 0; i < nRemoteRootRanks; i++) PetscCallMPI(MPIU_Irecv(&sf->rootsigdisp[i], 1, MPIU_INT, sf->ranks[i + sf->ndranks], tag, comm, &leafreqs[i])); /* Leaves recv */
130: for (i = 0; i < nRemoteLeafRanks; i++) PetscCallMPI(MPI_Send(&i, 1, MPIU_INT, bas->iranks[i + bas->ndiranks], tag, comm)); /* Roots send. Note i changes, so we use MPI_Send. */
131: PetscCallMPI(MPI_Waitall(nRemoteRootRanks, leafreqs, MPI_STATUSES_IGNORE));
133: for (i = 0; i < nRemoteRootRanks; i++) PetscCallMPI(MPIU_Irecv(&sf->rootbufdisp[i], 1, MPIU_INT, sf->ranks[i + sf->ndranks], tag, comm, &leafreqs[i])); /* Leaves recv */
134: for (i = 0; i < nRemoteLeafRanks; i++) {
135: tmp = bas->ioffset[i + bas->ndiranks] - bas->ioffset[bas->ndiranks];
136: PetscCallMPI(MPI_Send(&tmp, 1, MPIU_INT, bas->iranks[i + bas->ndiranks], tag, comm)); /* Roots send. Note tmp changes, so we use MPI_Send. */
137: }
138: PetscCallMPI(MPI_Waitall(nRemoteRootRanks, leafreqs, MPI_STATUSES_IGNORE));
140: PetscCallCUDA(cudaMalloc((void **)&sf->rootbufdisp_d, nRemoteRootRanks * sizeof(PetscInt)));
141: PetscCallCUDA(cudaMalloc((void **)&sf->rootsigdisp_d, nRemoteRootRanks * sizeof(PetscInt)));
142: PetscCallCUDA(cudaMalloc((void **)&sf->ranks_d, nRemoteRootRanks * sizeof(PetscMPIInt)));
143: PetscCallCUDA(cudaMalloc((void **)&sf->roffset_d, (nRemoteRootRanks + 1) * sizeof(PetscInt)));
145: PetscCallCUDA(cudaMemcpyAsync(sf->rootbufdisp_d, sf->rootbufdisp, nRemoteRootRanks * sizeof(PetscInt), cudaMemcpyHostToDevice, *stream));
146: PetscCallCUDA(cudaMemcpyAsync(sf->rootsigdisp_d, sf->rootsigdisp, nRemoteRootRanks * sizeof(PetscInt), cudaMemcpyHostToDevice, *stream));
147: PetscCallCUDA(cudaMemcpyAsync(sf->ranks_d, sf->ranks + sf->ndranks, nRemoteRootRanks * sizeof(PetscMPIInt), cudaMemcpyHostToDevice, *stream));
148: PetscCallCUDA(cudaMemcpyAsync(sf->roffset_d, sf->roffset + sf->ndranks, (nRemoteRootRanks + 1) * sizeof(PetscInt), cudaMemcpyHostToDevice, *stream));
150: /* Leaf ranks to root ranks: send info about leafsigdisp[] and leafbufdisp[] */
151: PetscCall(PetscMalloc2(nRemoteLeafRanks, &bas->leafsigdisp, nRemoteLeafRanks, &bas->leafbufdisp));
152: for (i = 0; i < nRemoteLeafRanks; i++) PetscCallMPI(MPIU_Irecv(&bas->leafsigdisp[i], 1, MPIU_INT, bas->iranks[i + bas->ndiranks], tag, comm, &rootreqs[i]));
153: for (i = 0; i < nRemoteRootRanks; i++) PetscCallMPI(MPI_Send(&i, 1, MPIU_INT, sf->ranks[i + sf->ndranks], tag, comm));
154: PetscCallMPI(MPI_Waitall(nRemoteLeafRanks, rootreqs, MPI_STATUSES_IGNORE));
156: for (i = 0; i < nRemoteLeafRanks; i++) PetscCallMPI(MPIU_Irecv(&bas->leafbufdisp[i], 1, MPIU_INT, bas->iranks[i + bas->ndiranks], tag, comm, &rootreqs[i]));
157: for (i = 0; i < nRemoteRootRanks; i++) {
158: tmp = sf->roffset[i + sf->ndranks] - sf->roffset[sf->ndranks];
159: PetscCallMPI(MPI_Send(&tmp, 1, MPIU_INT, sf->ranks[i + sf->ndranks], tag, comm));
160: }
161: PetscCallMPI(MPI_Waitall(nRemoteLeafRanks, rootreqs, MPI_STATUSES_IGNORE));
163: PetscCallCUDA(cudaMalloc((void **)&bas->leafbufdisp_d, nRemoteLeafRanks * sizeof(PetscInt)));
164: PetscCallCUDA(cudaMalloc((void **)&bas->leafsigdisp_d, nRemoteLeafRanks * sizeof(PetscInt)));
165: PetscCallCUDA(cudaMalloc((void **)&bas->iranks_d, nRemoteLeafRanks * sizeof(PetscMPIInt)));
166: PetscCallCUDA(cudaMalloc((void **)&bas->ioffset_d, (nRemoteLeafRanks + 1) * sizeof(PetscInt)));
168: PetscCallCUDA(cudaMemcpyAsync(bas->leafbufdisp_d, bas->leafbufdisp, nRemoteLeafRanks * sizeof(PetscInt), cudaMemcpyHostToDevice, *stream));
169: PetscCallCUDA(cudaMemcpyAsync(bas->leafsigdisp_d, bas->leafsigdisp, nRemoteLeafRanks * sizeof(PetscInt), cudaMemcpyHostToDevice, *stream));
170: PetscCallCUDA(cudaMemcpyAsync(bas->iranks_d, bas->iranks + bas->ndiranks, nRemoteLeafRanks * sizeof(PetscMPIInt), cudaMemcpyHostToDevice, *stream));
171: PetscCallCUDA(cudaMemcpyAsync(bas->ioffset_d, bas->ioffset + bas->ndiranks, (nRemoteLeafRanks + 1) * sizeof(PetscInt), cudaMemcpyHostToDevice, *stream));
173: // Complete metadata uploads before another context or the communication stream reads them.
174: PetscCall(PetscDeviceContextSynchronize(dctx));
175: PetscCall(PetscFree2(rootreqs, leafreqs));
176: PetscFunctionReturn(PETSC_SUCCESS);
177: }
179: PetscErrorCode PetscSFLinkNvshmemCheck(PetscSF sf, PetscMemType rootmtype, const void *rootdata, PetscMemType leafmtype, const void *leafdata, PetscBool *use_nvshmem)
180: {
181: MPI_Comm comm;
182: PetscBool isBasic;
183: PetscMPIInt result = MPI_UNEQUAL;
185: PetscFunctionBegin;
186: PetscCall(PetscObjectGetComm((PetscObject)sf, &comm));
187: /* Check if the sf is eligible for NVSHMEM, if we have not checked yet.
188: Note the check result <use_nvshmem> must be the same over comm, since an SFLink must be collectively either NVSHMEM or MPI.
189: */
190: sf->checked_nvshmem_eligibility = PETSC_TRUE;
191: if (sf->use_nvshmem && !sf->checked_nvshmem_eligibility) {
192: /* Only use NVSHMEM for SFBASIC on PETSC_COMM_WORLD */
193: PetscCall(PetscObjectTypeCompare((PetscObject)sf, PETSCSFBASIC, &isBasic));
194: if (isBasic) PetscCallMPI(MPI_Comm_compare(PETSC_COMM_WORLD, comm, &result));
195: if (!isBasic || (result != MPI_IDENT && result != MPI_CONGRUENT)) sf->use_nvshmem = PETSC_FALSE; /* If not eligible, clear the flag so that we don't try again */
197: /* Do further check: If on a rank, both rootdata and leafdata are NULL, we might think they are PETSC_MEMTYPE_CUDA (or HOST)
198: and then use NVSHMEM. But if root/leafmtypes on other ranks are PETSC_MEMTYPE_HOST (or DEVICE), this would lead to
199: inconsistency on the return value <use_nvshmem>. To be safe, we simply disable nvshmem on these rare SFs.
200: */
201: if (sf->use_nvshmem) {
202: PetscInt hasNullRank = (!rootdata && !leafdata) ? 1 : 0;
203: PetscCallMPI(MPIU_Allreduce(MPI_IN_PLACE, &hasNullRank, 1, MPIU_INT, MPI_LOR, comm));
204: if (hasNullRank) sf->use_nvshmem = PETSC_FALSE;
205: }
206: sf->checked_nvshmem_eligibility = PETSC_TRUE; /* If eligible, don't do above check again */
207: }
209: /* Check if rootmtype and leafmtype collectively are PETSC_MEMTYPE_CUDA */
210: if (sf->use_nvshmem) {
211: PetscInt oneCuda = (!rootdata || PetscMemTypeCUDA(rootmtype)) && (!leafdata || PetscMemTypeCUDA(leafmtype)) ? 1 : 0; /* Do I use cuda for both root&leafmtype? */
212: PetscInt allCuda = oneCuda; /* Assume the same for all ranks. But if not, in opt mode, return value <use_nvshmem> won't be collective! */
213: if (PetscDefined(USE_DEBUG)) { /* Check in debug mode. Note MPI_Allreduce is expensive, so only in debug mode */
214: PetscCallMPI(MPIU_Allreduce(&oneCuda, &allCuda, 1, MPIU_INT, MPI_LAND, comm));
215: PetscCheck(allCuda == oneCuda, comm, PETSC_ERR_SUP, "root/leaf mtypes are inconsistent among ranks, which may lead to SF nvshmem failure in opt mode. Add -use_nvshmem 0 to disable it.");
216: }
217: if (allCuda) {
218: PetscCall(PetscNvshmemInitializeCheck());
219: if (!sf->setup_nvshmem) { /* Set up nvshmem related fields on this SF on-demand */
220: PetscCall(PetscSFSetUp_Basic_NVSHMEM(sf));
221: sf->setup_nvshmem = PETSC_TRUE;
222: }
223: *use_nvshmem = PETSC_TRUE;
224: } else {
225: *use_nvshmem = PETSC_FALSE;
226: }
227: } else {
228: *use_nvshmem = PETSC_FALSE;
229: }
230: PetscFunctionReturn(PETSC_SUCCESS);
231: }
233: /* Build dependence between <stream> and <remoteCommStream> at the entry of NVSHMEM communication */
234: static PetscErrorCode PetscSFLinkBuildDependenceBegin(PetscSF sf, PetscSFLink link, PetscSFDirection direction)
235: {
236: cudaError_t cerr;
237: PetscSF_Basic *bas = (PetscSF_Basic *)sf->data;
238: PetscInt buflen = (direction == PETSCSF_ROOT2LEAF) ? bas->rootbuflen[PETSCSF_REMOTE] : sf->leafbuflen[PETSCSF_REMOTE];
240: PetscFunctionBegin;
241: if (buflen) {
242: PetscCallCUDA(cudaEventRecord(link->dataReady, link->stream));
243: PetscCallCUDA(cudaStreamWaitEvent(link->remoteCommStream, link->dataReady, 0));
244: }
245: PetscFunctionReturn(PETSC_SUCCESS);
246: }
248: /* Build dependence between <stream> and <remoteCommStream> at the exit of NVSHMEM communication */
249: static PetscErrorCode PetscSFLinkBuildDependenceEnd(PetscSF sf, PetscSFLink link, PetscSFDirection direction)
250: {
251: cudaError_t cerr;
252: PetscSF_Basic *bas = (PetscSF_Basic *)sf->data;
253: PetscInt buflen = (direction == PETSCSF_ROOT2LEAF) ? sf->leafbuflen[PETSCSF_REMOTE] : bas->rootbuflen[PETSCSF_REMOTE];
255: PetscFunctionBegin;
256: /* If unpack to non-null device buffer, build the endRemoteComm dependence */
257: if (buflen) {
258: PetscCallCUDA(cudaEventRecord(link->endRemoteComm, link->remoteCommStream));
259: PetscCallCUDA(cudaStreamWaitEvent(link->stream, link->endRemoteComm, 0));
260: }
261: PetscFunctionReturn(PETSC_SUCCESS);
262: }
264: /* Send/Put signals to remote ranks
266: Input parameters:
267: + n - Number of remote ranks
268: . sig - Signal address in symmetric heap
269: . sigdisp - To i-th rank, use its signal at offset sigdisp[i]
270: . ranks - remote ranks
271: - newval - Set signals to this value
272: */
273: __global__ static void NvshmemSendSignals(PetscInt n, uint64_t *sig, PetscInt *sigdisp, PetscMPIInt *ranks, uint64_t newval)
274: {
275: int i = blockIdx.x * blockDim.x + threadIdx.x;
277: /* Each thread puts one remote signal */
278: if (i < n) nvshmemx_uint64_signal(sig + sigdisp[i], newval, ranks[i]);
279: }
281: /* Wait until local signals equal to the expected value and then set them to a new value
283: Input parameters:
284: + n - Number of signals
285: . sig - Local signal address
286: . expval - expected value
287: - newval - Set signals to this new value
288: */
289: __global__ static void NvshmemWaitSignals(PetscInt n, uint64_t *sig, uint64_t expval, uint64_t newval)
290: {
291: #if 0
292: /* Akhil Langer@NVIDIA said using 1 thread and nvshmem_uint64_wait_until_all is better */
293: int i = blockIdx.x*blockDim.x + threadIdx.x;
294: if (i < n) {
295: nvshmem_signal_wait_until(sig+i,NVSHMEM_CMP_EQ,expval);
296: sig[i] = newval;
297: }
298: #else
299: nvshmem_uint64_wait_until_all(sig, n, NULL /*no mask*/, NVSHMEM_CMP_EQ, expval);
300: for (int i = 0; i < n; i++) sig[i] = newval;
301: #endif
302: }
304: /* ===========================================================================================================
306: A set of routines to support receiver initiated communication using the get method
308: The getting protocol is:
310: Sender has a send buf (sbuf) and a signal variable (ssig); Receiver has a recv buf (rbuf) and a signal variable (rsig);
311: All signal variables have an initial value 0.
313: Sender: | Receiver:
314: 1. Wait ssig be 0, then set it to 1
315: 2. Pack data into stand alone sbuf |
316: 3. Put 1 to receiver's rsig | 1. Wait rsig to be 1, then set it 0
317: | 2. Get data from remote sbuf to local rbuf
318: | 3. Put 1 to sender's ssig
319: | 4. Unpack data from local rbuf
320: ===========================================================================================================*/
321: /* PrePack operation -- since sender will overwrite the send buffer which the receiver might be getting data from.
322: Sender waits for signals (from receivers) indicating receivers have finished getting data
323: */
324: static PetscErrorCode PetscSFLinkWaitSignalsOfCompletionOfGettingData_NVSHMEM(PetscSF sf, PetscSFLink link, PetscSFDirection direction)
325: {
326: PetscSF_Basic *bas = (PetscSF_Basic *)sf->data;
327: uint64_t *sig;
328: PetscInt n;
330: PetscFunctionBegin;
331: if (direction == PETSCSF_ROOT2LEAF) { /* leaf ranks are getting data */
332: sig = link->rootSendSig; /* leaf ranks set my rootSendsig */
333: n = bas->nRemoteLeafRanks;
334: } else { /* LEAF2ROOT */
335: sig = link->leafSendSig;
336: n = sf->nRemoteRootRanks;
337: }
339: if (n) {
340: NvshmemWaitSignals<<<1, 1, 0, link->remoteCommStream>>>(n, sig, 0, 1); /* wait the signals to be 0, then set them to 1 */
341: PetscCallCUDA(cudaGetLastError());
342: }
343: PetscFunctionReturn(PETSC_SUCCESS);
344: }
346: /* n thread blocks. Each takes in charge one remote rank */
347: __global__ static void GetDataFromRemotelyAccessible(PetscInt nsrcranks, PetscMPIInt *srcranks, const char *src, PetscInt *srcdisp, char *dst, PetscInt *dstdisp, PetscInt unitbytes)
348: {
349: int bid = blockIdx.x;
350: PetscMPIInt pe = srcranks[bid];
352: if (!nvshmem_ptr(src, pe)) {
353: PetscInt nelems = (dstdisp[bid + 1] - dstdisp[bid]) * unitbytes;
354: nvshmem_getmem_nbi(dst + (dstdisp[bid] - dstdisp[0]) * unitbytes, src + srcdisp[bid] * unitbytes, nelems, pe);
355: }
356: }
358: /* Start communication -- Get data in the given direction */
359: static PetscErrorCode PetscSFLinkGetDataBegin_NVSHMEM(PetscSF sf, PetscSFLink link, PetscSFDirection direction)
360: {
361: cudaError_t cerr;
362: PetscSF_Basic *bas = (PetscSF_Basic *)sf->data;
364: PetscInt nsrcranks, ndstranks, nLocallyAccessible = 0;
366: char *src, *dst;
367: PetscInt *srcdisp_h, *dstdisp_h;
368: PetscInt *srcdisp_d, *dstdisp_d;
369: PetscMPIInt *srcranks_h;
370: PetscMPIInt *srcranks_d, *dstranks_d;
371: uint64_t *dstsig;
372: PetscInt *dstsigdisp_d;
374: PetscFunctionBegin;
375: PetscCall(PetscSFLinkBuildDependenceBegin(sf, link, direction));
376: if (direction == PETSCSF_ROOT2LEAF) { /* src is root, dst is leaf; we will move data from src to dst */
377: nsrcranks = sf->nRemoteRootRanks;
378: src = link->rootbuf[PETSCSF_REMOTE][PETSC_MEMTYPE_DEVICE]; /* root buf is the send buf; it is in symmetric heap */
380: srcdisp_h = sf->rootbufdisp; /* for my i-th remote root rank, I will access its buf at offset rootbufdisp[i] */
381: srcdisp_d = sf->rootbufdisp_d;
382: srcranks_h = sf->ranks + sf->ndranks; /* my (remote) root ranks */
383: srcranks_d = sf->ranks_d;
385: ndstranks = bas->nRemoteLeafRanks;
386: dst = link->leafbuf[PETSCSF_REMOTE][PETSC_MEMTYPE_DEVICE]; /* recv buf is the local leaf buf, also in symmetric heap */
388: dstdisp_h = sf->roffset + sf->ndranks; /* offsets of the local leaf buf. Note dstdisp[0] is not necessarily 0 */
389: dstdisp_d = sf->roffset_d;
390: dstranks_d = bas->iranks_d; /* my (remote) leaf ranks */
392: dstsig = link->leafRecvSig;
393: dstsigdisp_d = bas->leafsigdisp_d;
394: } else { /* src is leaf, dst is root; we will move data from src to dst */
395: nsrcranks = bas->nRemoteLeafRanks;
396: src = link->leafbuf[PETSCSF_REMOTE][PETSC_MEMTYPE_DEVICE]; /* leaf buf is the send buf */
398: srcdisp_h = bas->leafbufdisp; /* for my i-th remote root rank, I will access its buf at offset rootbufdisp[i] */
399: srcdisp_d = bas->leafbufdisp_d;
400: srcranks_h = bas->iranks + bas->ndiranks; /* my (remote) root ranks */
401: srcranks_d = bas->iranks_d;
403: ndstranks = sf->nRemoteRootRanks;
404: dst = link->rootbuf[PETSCSF_REMOTE][PETSC_MEMTYPE_DEVICE]; /* the local root buf is the recv buf */
406: dstdisp_h = bas->ioffset + bas->ndiranks; /* offsets of the local root buf. Note dstdisp[0] is not necessarily 0 */
407: dstdisp_d = bas->ioffset_d;
408: dstranks_d = sf->ranks_d; /* my (remote) root ranks */
410: dstsig = link->rootRecvSig;
411: dstsigdisp_d = sf->rootsigdisp_d;
412: }
414: /* After Pack operation -- src tells dst ranks that they are allowed to get data */
415: if (ndstranks) {
416: NvshmemSendSignals<<<(ndstranks + 255) / 256, 256, 0, link->remoteCommStream>>>(ndstranks, dstsig, dstsigdisp_d, dstranks_d, 1); /* set signals to 1 */
417: PetscCallCUDA(cudaGetLastError());
418: }
420: /* dst waits for signals (permissions) from src ranks to start getting data */
421: if (nsrcranks) {
422: NvshmemWaitSignals<<<1, 1, 0, link->remoteCommStream>>>(nsrcranks, dstsig, 1, 0); /* wait the signals to be 1, then set them to 0 */
423: PetscCallCUDA(cudaGetLastError());
424: }
426: /* dst gets data from src ranks using non-blocking nvshmem_gets, which are finished in PetscSFLinkGetDataEnd_NVSHMEM() */
428: /* Count number of locally accessible src ranks, which should be a small number */
429: for (int i = 0; i < nsrcranks; i++) {
430: if (nvshmem_ptr(src, srcranks_h[i])) nLocallyAccessible++;
431: }
433: /* Get data from remotely accessible PEs */
434: if (nLocallyAccessible < nsrcranks) {
435: GetDataFromRemotelyAccessible<<<nsrcranks, 1, 0, link->remoteCommStream>>>(nsrcranks, srcranks_d, src, srcdisp_d, dst, dstdisp_d, link->unitbytes);
436: PetscCallCUDA(cudaGetLastError());
437: }
439: /* Get data from locally accessible PEs */
440: if (nLocallyAccessible) {
441: for (int i = 0; i < nsrcranks; i++) {
442: int pe = srcranks_h[i];
443: if (nvshmem_ptr(src, pe)) {
444: size_t nelems = (dstdisp_h[i + 1] - dstdisp_h[i]) * link->unitbytes;
445: nvshmemx_getmem_nbi_on_stream(dst + (dstdisp_h[i] - dstdisp_h[0]) * link->unitbytes, src + srcdisp_h[i] * link->unitbytes, nelems, pe, link->remoteCommStream);
446: }
447: }
448: }
449: PetscFunctionReturn(PETSC_SUCCESS);
450: }
452: /* Finish the communication (can be done before Unpack)
453: Receiver tells its senders that they are allowed to reuse their send buffer (since receiver has got data from their send buffer)
454: */
455: static PetscErrorCode PetscSFLinkGetDataEnd_NVSHMEM(PetscSF sf, PetscSFLink link, PetscSFDirection direction)
456: {
457: cudaError_t cerr;
458: PetscSF_Basic *bas = (PetscSF_Basic *)sf->data;
459: uint64_t *srcsig;
460: PetscInt nsrcranks, *srcsigdisp;
461: PetscMPIInt *srcranks;
463: PetscFunctionBegin;
464: if (direction == PETSCSF_ROOT2LEAF) { /* leaf ranks are getting data */
465: nsrcranks = sf->nRemoteRootRanks;
466: srcsig = link->rootSendSig; /* I want to set their root signal */
467: srcsigdisp = sf->rootsigdisp_d; /* offset of each root signal */
468: srcranks = sf->ranks_d; /* ranks of the n root ranks */
469: } else { /* LEAF2ROOT, root ranks are getting data */
470: nsrcranks = bas->nRemoteLeafRanks;
471: srcsig = link->leafSendSig;
472: srcsigdisp = bas->leafsigdisp_d;
473: srcranks = bas->iranks_d;
474: }
476: if (nsrcranks) {
477: nvshmemx_quiet_on_stream(link->remoteCommStream); /* Finish the nonblocking get, so that we can unpack afterwards */
478: PetscCallCUDA(cudaGetLastError());
479: NvshmemSendSignals<<<(nsrcranks + 511) / 512, 512, 0, link->remoteCommStream>>>(nsrcranks, srcsig, srcsigdisp, srcranks, 0); /* set signals to 0 */
480: PetscCallCUDA(cudaGetLastError());
481: }
482: PetscCall(PetscSFLinkBuildDependenceEnd(sf, link, direction));
483: PetscFunctionReturn(PETSC_SUCCESS);
484: }
486: /* ===========================================================================================================
488: A set of routines to support sender initiated communication using the put-based method (the default)
490: The putting protocol is:
492: Sender has a send buf (sbuf) and a send signal var (ssig); Receiver has a stand-alone recv buf (rbuf)
493: and a recv signal var (rsig); All signal variables have an initial value 0. rbuf is allocated by SF and
494: is in nvshmem space.
496: Sender: | Receiver:
497: |
498: 1. Pack data into sbuf |
499: 2. Wait ssig be 0, then set it to 1 |
500: 3. Put data to remote stand-alone rbuf |
501: 4. Fence // make sure 5 happens after 3 |
502: 5. Put 1 to receiver's rsig | 1. Wait rsig to be 1, then set it 0
503: | 2. Unpack data from local rbuf
504: | 3. Put 0 to sender's ssig
505: ===========================================================================================================*/
507: /* n thread blocks. Each takes in charge one remote rank */
508: __global__ static void WaitAndPutDataToRemotelyAccessible(PetscInt ndstranks, PetscMPIInt *dstranks, char *dst, PetscInt *dstdisp, const char *src, PetscInt *srcdisp, uint64_t *srcsig, PetscInt unitbytes)
509: {
510: int bid = blockIdx.x;
511: PetscMPIInt pe = dstranks[bid];
513: if (!nvshmem_ptr(dst, pe)) {
514: PetscInt nelems = (srcdisp[bid + 1] - srcdisp[bid]) * unitbytes;
515: nvshmem_uint64_wait_until(srcsig + bid, NVSHMEM_CMP_EQ, 0); /* Wait until the sig = 0 */
516: srcsig[bid] = 1;
517: nvshmem_putmem_nbi(dst + dstdisp[bid] * unitbytes, src + (srcdisp[bid] - srcdisp[0]) * unitbytes, nelems, pe);
518: }
519: }
521: /* one-thread kernel, which takes in charge all locally accessible */
522: __global__ static void WaitSignalsFromLocallyAccessible(PetscInt ndstranks, PetscMPIInt *dstranks, uint64_t *srcsig, const char *dst)
523: {
524: for (int i = 0; i < ndstranks; i++) {
525: int pe = dstranks[i];
526: if (nvshmem_ptr(dst, pe)) {
527: nvshmem_uint64_wait_until(srcsig + i, NVSHMEM_CMP_EQ, 0); /* Wait until the sig = 0 */
528: srcsig[i] = 1;
529: }
530: }
531: }
533: /* Put data in the given direction */
534: static PetscErrorCode PetscSFLinkPutDataBegin_NVSHMEM(PetscSF sf, PetscSFLink link, PetscSFDirection direction)
535: {
536: cudaError_t cerr;
537: PetscSF_Basic *bas = (PetscSF_Basic *)sf->data;
538: PetscInt ndstranks, nLocallyAccessible = 0;
539: char *src, *dst;
540: PetscInt *srcdisp_h, *dstdisp_h;
541: PetscInt *srcdisp_d, *dstdisp_d;
542: PetscMPIInt *dstranks_h;
543: PetscMPIInt *dstranks_d;
544: uint64_t *srcsig;
546: PetscFunctionBegin;
547: PetscCall(PetscSFLinkBuildDependenceBegin(sf, link, direction));
548: if (direction == PETSCSF_ROOT2LEAF) { /* put data in rootbuf to leafbuf */
549: ndstranks = bas->nRemoteLeafRanks; /* number of (remote) leaf ranks */
550: src = link->rootbuf[PETSCSF_REMOTE][PETSC_MEMTYPE_DEVICE]; /* Both src & dst must be symmetric */
551: dst = link->leafbuf[PETSCSF_REMOTE][PETSC_MEMTYPE_DEVICE];
553: srcdisp_h = bas->ioffset + bas->ndiranks; /* offsets of rootbuf. srcdisp[0] is not necessarily zero */
554: srcdisp_d = bas->ioffset_d;
555: srcsig = link->rootSendSig;
557: dstdisp_h = bas->leafbufdisp; /* for my i-th remote leaf rank, I will access its leaf buf at offset leafbufdisp[i] */
558: dstdisp_d = bas->leafbufdisp_d;
559: dstranks_h = bas->iranks + bas->ndiranks; /* remote leaf ranks */
560: dstranks_d = bas->iranks_d;
561: } else { /* put data in leafbuf to rootbuf */
562: ndstranks = sf->nRemoteRootRanks;
563: src = link->leafbuf[PETSCSF_REMOTE][PETSC_MEMTYPE_DEVICE];
564: dst = link->rootbuf[PETSCSF_REMOTE][PETSC_MEMTYPE_DEVICE];
566: srcdisp_h = sf->roffset + sf->ndranks; /* offsets of leafbuf */
567: srcdisp_d = sf->roffset_d;
568: srcsig = link->leafSendSig;
570: dstdisp_h = sf->rootbufdisp; /* for my i-th remote root rank, I will access its root buf at offset rootbufdisp[i] */
571: dstdisp_d = sf->rootbufdisp_d;
572: dstranks_h = sf->ranks + sf->ndranks; /* remote root ranks */
573: dstranks_d = sf->ranks_d;
574: }
576: /* Wait for signals and then put data to dst ranks using non-blocking nvshmem_put, which are finished in PetscSFLinkPutDataEnd_NVSHMEM */
578: /* Count number of locally accessible neighbors, which should be a small number */
579: for (int i = 0; i < ndstranks; i++) {
580: if (nvshmem_ptr(dst, dstranks_h[i])) nLocallyAccessible++;
581: }
583: /* For remotely accessible PEs, send data to them in one kernel call */
584: if (nLocallyAccessible < ndstranks) {
585: WaitAndPutDataToRemotelyAccessible<<<ndstranks, 1, 0, link->remoteCommStream>>>(ndstranks, dstranks_d, dst, dstdisp_d, src, srcdisp_d, srcsig, link->unitbytes);
586: PetscCallCUDA(cudaGetLastError());
587: }
589: /* For locally accessible PEs, use host API, which uses CUDA copy-engines and is much faster than device API */
590: if (nLocallyAccessible) {
591: WaitSignalsFromLocallyAccessible<<<1, 1, 0, link->remoteCommStream>>>(ndstranks, dstranks_d, srcsig, dst);
592: for (int i = 0; i < ndstranks; i++) {
593: int pe = dstranks_h[i];
594: if (nvshmem_ptr(dst, pe)) { /* If return a non-null pointer, then <pe> is locally accessible */
595: size_t nelems = (srcdisp_h[i + 1] - srcdisp_h[i]) * link->unitbytes;
596: /* Initiate the nonblocking communication */
597: nvshmemx_putmem_nbi_on_stream(dst + dstdisp_h[i] * link->unitbytes, src + (srcdisp_h[i] - srcdisp_h[0]) * link->unitbytes, nelems, pe, link->remoteCommStream);
598: }
599: }
600: }
602: if (nLocallyAccessible) nvshmemx_quiet_on_stream(link->remoteCommStream); /* Calling nvshmem_fence/quiet() does not fence the above nvshmemx_putmem_nbi_on_stream! */
603: PetscFunctionReturn(PETSC_SUCCESS);
604: }
606: /* A one-thread kernel. The thread takes in charge all remote PEs */
607: __global__ static void PutDataEnd(PetscInt nsrcranks, PetscInt ndstranks, PetscMPIInt *dstranks, uint64_t *dstsig, PetscInt *dstsigdisp)
608: {
609: /* TODO: Shall we finished the non-blocking remote puts? */
611: /* 1. Send a signal to each dst rank */
613: /* According to Akhil@NVIDIA, IB is orderred, so no fence is needed for remote PEs.
614: For local PEs, we already called nvshmemx_quiet_on_stream(). Therefore, we are good to send signals to all dst ranks now.
615: */
616: for (int i = 0; i < ndstranks; i++) nvshmemx_uint64_signal(dstsig + dstsigdisp[i], 1, dstranks[i]); /* set sig to 1 */
618: /* 2. Wait for signals from src ranks (if any) */
619: if (nsrcranks) {
620: nvshmem_uint64_wait_until_all(dstsig, nsrcranks, NULL /*no mask*/, NVSHMEM_CMP_EQ, 1); /* wait sigs to be 1, then set them to 0 */
621: for (int i = 0; i < nsrcranks; i++) dstsig[i] = 0;
622: }
623: }
625: /* Finish the communication -- A receiver waits until it can access its receive buffer */
626: static PetscErrorCode PetscSFLinkPutDataEnd_NVSHMEM(PetscSF sf, PetscSFLink link, PetscSFDirection direction)
627: {
628: cudaError_t cerr;
629: PetscSF_Basic *bas = (PetscSF_Basic *)sf->data;
630: PetscMPIInt *dstranks;
631: uint64_t *dstsig;
632: PetscInt nsrcranks, ndstranks, *dstsigdisp;
634: PetscFunctionBegin;
635: if (direction == PETSCSF_ROOT2LEAF) { /* put root data to leaf */
636: nsrcranks = sf->nRemoteRootRanks;
638: ndstranks = bas->nRemoteLeafRanks;
639: dstranks = bas->iranks_d; /* leaf ranks */
640: dstsig = link->leafRecvSig; /* I will set my leaf ranks's RecvSig */
641: dstsigdisp = bas->leafsigdisp_d; /* for my i-th remote leaf rank, I will access its signal at offset leafsigdisp[i] */
642: } else { /* LEAF2ROOT */
643: nsrcranks = bas->nRemoteLeafRanks;
645: ndstranks = sf->nRemoteRootRanks;
646: dstranks = sf->ranks_d;
647: dstsig = link->rootRecvSig;
648: dstsigdisp = sf->rootsigdisp_d;
649: }
651: if (nsrcranks || ndstranks) {
652: PutDataEnd<<<1, 1, 0, link->remoteCommStream>>>(nsrcranks, ndstranks, dstranks, dstsig, dstsigdisp);
653: PetscCallCUDA(cudaGetLastError());
654: }
655: PetscCall(PetscSFLinkBuildDependenceEnd(sf, link, direction));
656: PetscFunctionReturn(PETSC_SUCCESS);
657: }
659: /* PostUnpack operation -- A receiver tells its senders that they are allowed to put data to here (it implies recv buf is free to take new data) */
660: static PetscErrorCode PetscSFLinkSendSignalsToAllowPuttingData_NVSHMEM(PetscSF sf, PetscSFLink link, PetscSFDirection direction)
661: {
662: PetscSF_Basic *bas = (PetscSF_Basic *)sf->data;
663: uint64_t *srcsig;
664: PetscInt nsrcranks, *srcsigdisp_d;
665: PetscMPIInt *srcranks_d;
667: PetscFunctionBegin;
668: if (direction == PETSCSF_ROOT2LEAF) { /* I allow my root ranks to put data to me */
669: nsrcranks = sf->nRemoteRootRanks;
670: srcsig = link->rootSendSig; /* I want to set their send signals */
671: srcsigdisp_d = sf->rootsigdisp_d; /* offset of each root signal */
672: srcranks_d = sf->ranks_d; /* ranks of the n root ranks */
673: } else { /* LEAF2ROOT */
674: nsrcranks = bas->nRemoteLeafRanks;
675: srcsig = link->leafSendSig;
676: srcsigdisp_d = bas->leafsigdisp_d;
677: srcranks_d = bas->iranks_d;
678: }
680: if (nsrcranks) {
681: NvshmemSendSignals<<<(nsrcranks + 255) / 256, 256, 0, link->remoteCommStream>>>(nsrcranks, srcsig, srcsigdisp_d, srcranks_d, 0); /* Set remote signals to 0 */
682: PetscCallCUDA(cudaGetLastError());
683: }
684: PetscFunctionReturn(PETSC_SUCCESS);
685: }
687: /* Destructor when the link uses nvshmem for communication */
688: static PetscErrorCode PetscSFLinkDestroy_NVSHMEM(PetscSF sf, PetscSFLink link)
689: {
690: cudaError_t cerr;
692: PetscFunctionBegin;
693: PetscCallCUDA(cudaEventDestroy(link->dataReady));
694: PetscCallCUDA(cudaEventDestroy(link->endRemoteComm));
695: PetscCallCUDA(cudaStreamDestroy(link->remoteCommStream));
697: /* nvshmem does not need buffers on host, which should be NULL */
698: PetscCall(PetscNvshmemFree(link->leafbuf_alloc[PETSCSF_REMOTE][PETSC_MEMTYPE_DEVICE]));
699: PetscCall(PetscNvshmemFree(link->leafSendSig));
700: PetscCall(PetscNvshmemFree(link->leafRecvSig));
701: PetscCall(PetscNvshmemFree(link->rootbuf_alloc[PETSCSF_REMOTE][PETSC_MEMTYPE_DEVICE]));
702: PetscCall(PetscNvshmemFree(link->rootSendSig));
703: PetscCall(PetscNvshmemFree(link->rootRecvSig));
704: PetscFunctionReturn(PETSC_SUCCESS);
705: }
707: PetscErrorCode PetscSFLinkCreate_NVSHMEM(PetscSF sf, MPI_Datatype unit, PetscMemType rootmtype, const void *rootdata, PetscMemType leafmtype, const void *leafdata, MPI_Op op, PetscSFOperation sfop, PetscSFLink *mylink)
708: {
709: cudaError_t cerr;
710: PetscSF_Basic *bas = (PetscSF_Basic *)sf->data;
711: PetscSFLink *p, link;
712: PetscBool match, rootdirect[2], leafdirect[2];
713: int greatestPriority;
715: PetscFunctionBegin;
716: /* Check to see if we can directly send/recv root/leafdata with the given sf, sfop and op.
717: We only care root/leafdirect[PETSCSF_REMOTE], since we never need intermediate buffers in local communication with NVSHMEM.
718: */
719: if (sfop == PETSCSF_BCAST) { /* Move data from rootbuf to leafbuf */
720: if (sf->use_nvshmem_get) {
721: rootdirect[PETSCSF_REMOTE] = PETSC_FALSE; /* send buffer has to be stand-alone (can't be rootdata) */
722: leafdirect[PETSCSF_REMOTE] = (PetscMemTypeNVSHMEM(leafmtype) && sf->leafcontig[PETSCSF_REMOTE] && op == MPI_REPLACE) ? PETSC_TRUE : PETSC_FALSE;
723: } else {
724: rootdirect[PETSCSF_REMOTE] = (PetscMemTypeNVSHMEM(rootmtype) && bas->rootcontig[PETSCSF_REMOTE]) ? PETSC_TRUE : PETSC_FALSE;
725: leafdirect[PETSCSF_REMOTE] = PETSC_FALSE; /* Our put-protocol always needs a nvshmem alloc'ed recv buffer */
726: }
727: } else if (sfop == PETSCSF_REDUCE) { /* Move data from leafbuf to rootbuf */
728: if (sf->use_nvshmem_get) {
729: rootdirect[PETSCSF_REMOTE] = (PetscMemTypeNVSHMEM(rootmtype) && bas->rootcontig[PETSCSF_REMOTE] && op == MPI_REPLACE) ? PETSC_TRUE : PETSC_FALSE;
730: leafdirect[PETSCSF_REMOTE] = PETSC_FALSE;
731: } else {
732: rootdirect[PETSCSF_REMOTE] = PETSC_FALSE;
733: leafdirect[PETSCSF_REMOTE] = (PetscMemTypeNVSHMEM(leafmtype) && sf->leafcontig[PETSCSF_REMOTE]) ? PETSC_TRUE : PETSC_FALSE;
734: }
735: } else { /* PETSCSF_FETCH */
736: rootdirect[PETSCSF_REMOTE] = PETSC_FALSE; /* FETCH always need a separate rootbuf */
737: leafdirect[PETSCSF_REMOTE] = PETSC_FALSE; /* We also force allocating a separate leafbuf so that leafdata and leafupdate can share mpi requests */
738: }
740: /* Look for free nvshmem links in cache */
741: for (p = &bas->avail; (link = *p); p = &link->next) {
742: if (link->use_nvshmem) {
743: PetscCall(MPIPetsc_Type_compare(unit, link->unit, &match));
744: if (match) {
745: if (sf->backend == PETSCSF_BACKEND_CUDA) PetscCall(PetscSFLinkSetUp_CUDA(sf, link, unit));
746: *p = link->next; /* Remove from available list */
747: goto found;
748: }
749: }
750: }
751: PetscCall(PetscNew(&link));
752: PetscCall(PetscSFLinkSetUp_Host(sf, link, unit)); /* Compute link->unitbytes, dup link->unit etc. */
753: if (sf->backend == PETSCSF_BACKEND_CUDA) PetscCall(PetscSFLinkSetUp_CUDA(sf, link, unit)); /* Setup pack routines, streams etc */
754: #if PetscDefined(HAVE_KOKKOS)
755: else if (sf->backend == PETSCSF_BACKEND_KOKKOS) PetscCall(PetscSFLinkSetUp_Kokkos(sf, link, unit));
756: #endif
758: link->rootdirect[PETSCSF_LOCAL] = PETSC_TRUE; /* For the local part we directly use root/leafdata */
759: link->leafdirect[PETSCSF_LOCAL] = PETSC_TRUE;
761: /* Init signals to zero */
762: if (!link->rootSendSig) PetscCall(PetscNvshmemCalloc(bas->nRemoteLeafRanksMax * sizeof(uint64_t), (void **)&link->rootSendSig));
763: if (!link->rootRecvSig) PetscCall(PetscNvshmemCalloc(bas->nRemoteLeafRanksMax * sizeof(uint64_t), (void **)&link->rootRecvSig));
764: if (!link->leafSendSig) PetscCall(PetscNvshmemCalloc(sf->nRemoteRootRanksMax * sizeof(uint64_t), (void **)&link->leafSendSig));
765: if (!link->leafRecvSig) PetscCall(PetscNvshmemCalloc(sf->nRemoteRootRanksMax * sizeof(uint64_t), (void **)&link->leafRecvSig));
767: link->use_nvshmem = PETSC_TRUE;
768: link->rootmtype = PETSC_MEMTYPE_DEVICE; /* Only need 0/1-based mtype from now on */
769: link->leafmtype = PETSC_MEMTYPE_DEVICE;
770: /* Overwrite some function pointers set by PetscSFLinkSetUp_CUDA */
771: link->Destroy = PetscSFLinkDestroy_NVSHMEM;
772: if (sf->use_nvshmem_get) { /* get-based protocol */
773: link->PrePack = PetscSFLinkWaitSignalsOfCompletionOfGettingData_NVSHMEM;
774: link->StartCommunication = PetscSFLinkGetDataBegin_NVSHMEM;
775: link->FinishCommunication = PetscSFLinkGetDataEnd_NVSHMEM;
776: } else { /* put-based protocol */
777: link->StartCommunication = PetscSFLinkPutDataBegin_NVSHMEM;
778: link->FinishCommunication = PetscSFLinkPutDataEnd_NVSHMEM;
779: link->PostUnpack = PetscSFLinkSendSignalsToAllowPuttingData_NVSHMEM;
780: }
782: PetscCallCUDA(cudaDeviceGetStreamPriorityRange(NULL, &greatestPriority));
783: PetscCallCUDA(cudaStreamCreateWithPriority(&link->remoteCommStream, cudaStreamNonBlocking, greatestPriority));
785: PetscCallCUDA(cudaEventCreateWithFlags(&link->dataReady, cudaEventDisableTiming));
786: PetscCallCUDA(cudaEventCreateWithFlags(&link->endRemoteComm, cudaEventDisableTiming));
788: found:
789: if (rootdirect[PETSCSF_REMOTE]) {
790: link->rootbuf[PETSCSF_REMOTE][PETSC_MEMTYPE_DEVICE] = (char *)rootdata + bas->rootstart[PETSCSF_REMOTE] * link->unitbytes;
791: } else {
792: if (!link->rootbuf_alloc[PETSCSF_REMOTE][PETSC_MEMTYPE_DEVICE]) PetscCall(PetscNvshmemMalloc(bas->rootbuflen_rmax * link->unitbytes, (void **)&link->rootbuf_alloc[PETSCSF_REMOTE][PETSC_MEMTYPE_DEVICE]));
793: link->rootbuf[PETSCSF_REMOTE][PETSC_MEMTYPE_DEVICE] = link->rootbuf_alloc[PETSCSF_REMOTE][PETSC_MEMTYPE_DEVICE];
794: }
796: if (leafdirect[PETSCSF_REMOTE]) {
797: link->leafbuf[PETSCSF_REMOTE][PETSC_MEMTYPE_DEVICE] = (char *)leafdata + sf->leafstart[PETSCSF_REMOTE] * link->unitbytes;
798: } else {
799: if (!link->leafbuf_alloc[PETSCSF_REMOTE][PETSC_MEMTYPE_DEVICE]) PetscCall(PetscNvshmemMalloc(sf->leafbuflen_rmax * link->unitbytes, (void **)&link->leafbuf_alloc[PETSCSF_REMOTE][PETSC_MEMTYPE_DEVICE]));
800: link->leafbuf[PETSCSF_REMOTE][PETSC_MEMTYPE_DEVICE] = link->leafbuf_alloc[PETSCSF_REMOTE][PETSC_MEMTYPE_DEVICE];
801: }
803: link->rootdirect[PETSCSF_REMOTE] = rootdirect[PETSCSF_REMOTE];
804: link->leafdirect[PETSCSF_REMOTE] = leafdirect[PETSCSF_REMOTE];
805: link->rootdata = rootdata; /* root/leafdata are keys to look up links in PetscSFXxxEnd */
806: link->leafdata = leafdata;
807: link->next = bas->inuse;
808: bas->inuse = link;
809: *mylink = link;
810: PetscFunctionReturn(PETSC_SUCCESS);
811: }
813: #if PetscDefined(USE_REAL_SINGLE)
814: PetscErrorCode PetscNvshmemSum(PetscInt count, float *dst, const float *src)
815: {
816: PetscMPIInt num; // Assume nvshmem's int is MPI's int
817: PetscDeviceContext dctx;
818: cudaStream_t *stream;
820: PetscFunctionBegin;
821: PetscCall(PetscDeviceContextGetCurrentContextAssertType_Internal(&dctx, PETSC_DEVICE_CUDA));
822: PetscCall(PetscDeviceContextGetStreamHandle(dctx, (void **)&stream));
823: PetscCall(PetscMPIIntCast(count, &num));
824: nvshmemx_float_sum_reduce_on_stream(NVSHMEM_TEAM_WORLD, dst, src, num, *stream);
825: PetscCall(PetscDeviceContextSynchronizeIfWithBarrier_Internal(dctx));
826: PetscFunctionReturn(PETSC_SUCCESS);
827: }
829: PetscErrorCode PetscNvshmemMax(PetscInt count, float *dst, const float *src)
830: {
831: PetscMPIInt num;
832: PetscDeviceContext dctx;
833: cudaStream_t *stream;
835: PetscFunctionBegin;
836: PetscCall(PetscDeviceContextGetCurrentContextAssertType_Internal(&dctx, PETSC_DEVICE_CUDA));
837: PetscCall(PetscDeviceContextGetStreamHandle(dctx, (void **)&stream));
838: PetscCall(PetscMPIIntCast(count, &num));
839: nvshmemx_float_max_reduce_on_stream(NVSHMEM_TEAM_WORLD, dst, src, num, *stream);
840: PetscCall(PetscDeviceContextSynchronizeIfWithBarrier_Internal(dctx));
841: PetscFunctionReturn(PETSC_SUCCESS);
842: }
843: #elif PetscDefined(USE_REAL_DOUBLE)
844: PetscErrorCode PetscNvshmemSum(PetscInt count, double *dst, const double *src)
845: {
846: PetscMPIInt num;
847: PetscDeviceContext dctx;
848: cudaStream_t *stream;
850: PetscFunctionBegin;
851: PetscCall(PetscDeviceContextGetCurrentContextAssertType_Internal(&dctx, PETSC_DEVICE_CUDA));
852: PetscCall(PetscDeviceContextGetStreamHandle(dctx, (void **)&stream));
853: PetscCall(PetscMPIIntCast(count, &num));
854: nvshmemx_double_sum_reduce_on_stream(NVSHMEM_TEAM_WORLD, dst, src, num, *stream);
855: PetscCall(PetscDeviceContextSynchronizeIfWithBarrier_Internal(dctx));
856: PetscFunctionReturn(PETSC_SUCCESS);
857: }
859: PetscErrorCode PetscNvshmemMax(PetscInt count, double *dst, const double *src)
860: {
861: PetscMPIInt num;
862: PetscDeviceContext dctx;
863: cudaStream_t *stream;
865: PetscFunctionBegin;
866: PetscCall(PetscDeviceContextGetCurrentContextAssertType_Internal(&dctx, PETSC_DEVICE_CUDA));
867: PetscCall(PetscDeviceContextGetStreamHandle(dctx, (void **)&stream));
868: PetscCall(PetscMPIIntCast(count, &num));
869: nvshmemx_double_max_reduce_on_stream(NVSHMEM_TEAM_WORLD, dst, src, num, *stream);
870: PetscCall(PetscDeviceContextSynchronizeIfWithBarrier_Internal(dctx));
871: PetscFunctionReturn(PETSC_SUCCESS);
872: }
873: #endif