Actual source code: petscdevice.h

  1: #pragma once

  3: #include <petscsys.h>
  4: #include <petscdevicetypes.h>

  6: #if PETSC_CPP_VERSION >= 11 // C++11
  7:   #define PETSC_DEVICE_ALIGNOF(...) alignof(decltype(__VA_ARGS__))
  8: #elif PETSC_C_VERSION >= 11 // C11
  9:   #if defined(__GNUC__)
 10:     #define PETSC_DEVICE_ALIGNOF(...) _Alignof(__typeof__(__VA_ARGS__))
 11:   #else
 12:     #include <stddef.h> // max_align_t
 13:     // Note we cannot just do _Alignof(expression) since clang warns that "'_Alignof' applied to an
 14:     // expression is a GNU extension", so we just default to max_align_t which is ultra safe
 15:     #define PETSC_DEVICE_ALIGNOF(...) _Alignof(max_align_t)
 16:   #endif // __GNUC__
 17: #else
 18:   #define PETSC_DEVICE_ALIGNOF(...) PETSC_MEMALIGN
 19: #endif

 21: /* MANSEC = Sys */
 22: /* SUBMANSEC = Device */

 24: // REVIEW ME: this should probably go somewhere better, configure-time?
 25: #define PETSC_HAVE_HOST 1

 27: /* logging support */
 28: PETSC_EXTERN PetscClassId PETSC_DEVICE_CLASSID;
 29: PETSC_EXTERN PetscClassId PETSC_DEVICE_CONTEXT_CLASSID;

 31: PETSC_EXTERN PetscErrorCode PetscDeviceInitializePackage(void);
 32: PETSC_EXTERN PetscErrorCode PetscDeviceFinalizePackage(void);
 33: PETSC_EXTERN PetscErrorCode PetscGetMemType(const void *, PetscMemType *);

 35: /* PetscDevice */
 36: PETSC_EXTERN PetscErrorCode  PetscDeviceCreate(PetscDeviceType, PetscInt, PetscDevice *);
 37: PETSC_EXTERN PetscErrorCode  PetscDeviceDestroy(PetscDevice *);
 38: PETSC_EXTERN PetscErrorCode  PetscDeviceConfigure(PetscDevice);
 39: PETSC_EXTERN PetscErrorCode  PetscDeviceView(PetscDevice, PetscViewer);
 40: PETSC_EXTERN PetscErrorCode  PetscDeviceGetType(PetscDevice, PetscDeviceType *);
 41: PETSC_EXTERN PetscErrorCode  PetscDeviceGetDeviceId(PetscDevice, PetscInt *);
 42: PETSC_EXTERN PetscDeviceType PETSC_DEVICE_DEFAULT(void);
 43: PETSC_EXTERN PetscErrorCode  PetscDeviceSetDefaultDeviceType(PetscDeviceType);
 44: PETSC_EXTERN PetscErrorCode  PetscDeviceInitialize(PetscDeviceType);
 45: PETSC_EXTERN PetscBool       PetscDeviceInitialized(PetscDeviceType);

 47: /* PetscDeviceContext */
 48: /*MC
 49:   PetscDeviceContextDefault - Placeholder for the default-stream context on the current device

 51:   Level: beginner

 53:   Notes:
 54:   Pass this symbol to device-memory operations or to device-context operations that inspect,
 55:   synchronize, duplicate, or fork a context. It is also accepted by `PetscDeviceContextSetUp()`,
 56:   `PetscDeviceContextSetCurrentContext()`, and as the parent argument of `PetscDeviceContextJoin()`.
 57:   When a call needs a context, it resolves this symbol to a PETSc-owned `PETSC_STREAM_DEFAULT` context
 58:   for the backend and device attached to the current context, creating that context lazily. It does
 59:   not select the current stream. No-work returns do not resolve the placeholder or initialize a device.

 61:   Saving this placeholder does not capture a context. After changing the current device, another call
 62:   resolves it for the new device. Use `PetscDeviceContextGetDefaultContext()` to capture a concrete
 63:   handle. A fork and its destroying join must resolve to the same parent; keep the device unchanged
 64:   between those calls or capture the parent first. Join child arrays must contain concrete handles.

 66:   This symbol cannot be destroyed, reconfigured with `PetscDeviceContextSetDevice()`,
 67:   `PetscDeviceContextSetStreamType()`, or `PetscDeviceContextSetFromOptions()`, or passed to generic
 68:   `PetscObject` routines. It does not represent a `NULL` pointer. Default-stream selection does not
 69:   synchronize unrelated nonblocking streams or establish dependencies on them.

 71:   Developer Note:
 72:   This placeholder is for application use. Resolve it at public API boundaries before ordinary
 73:   object validation, object access, or backend dispatch.

 75:   Some private helpers accept a `NULL` context to request the current context. They resolve it to
 76:   a concrete context before passing it to a public device API or accessing its state.

 78: .seealso: `PetscDeviceContext`, `PetscDeviceContextGetDefaultContext()`, `PetscDeviceContextGetCurrentContext()`, `PetscStreamType`
 79: M*/
 80: PETSC_EXTERN PetscDeviceContext const PetscDeviceContextDefault;

 82: PETSC_EXTERN PetscErrorCode PetscDeviceContextCreate(PetscDeviceContext *);
 83: PETSC_EXTERN PetscErrorCode PetscDeviceContextDestroy(PetscDeviceContext *);
 84: PETSC_EXTERN PetscErrorCode PetscDeviceContextSetStreamType(PetscDeviceContext, PetscStreamType);
 85: PETSC_EXTERN PetscErrorCode PetscDeviceContextGetStreamType(PetscDeviceContext, PetscStreamType *);
 86: PETSC_EXTERN PetscErrorCode PetscDeviceContextSetDevice(PetscDeviceContext, PetscDevice);
 87: PETSC_EXTERN PetscErrorCode PetscDeviceContextGetDevice(PetscDeviceContext, PetscDevice *);
 88: PETSC_EXTERN PetscErrorCode PetscDeviceContextGetDeviceType(PetscDeviceContext, PetscDeviceType *);
 89: PETSC_EXTERN PetscErrorCode PetscDeviceContextSetUp(PetscDeviceContext);
 90: PETSC_EXTERN PetscErrorCode PetscDeviceContextDuplicate(PetscDeviceContext, PetscDeviceContext *);
 91: PETSC_EXTERN PetscErrorCode PetscDeviceContextQueryIdle(PetscDeviceContext, PetscBool *);
 92: PETSC_EXTERN PetscErrorCode PetscDeviceContextWaitForContext(PetscDeviceContext, PetscDeviceContext);
 93: PETSC_EXTERN PetscErrorCode PetscDeviceContextForkWithStreamType(PetscDeviceContext, PetscStreamType, PetscInt, PetscDeviceContext **);
 94: PETSC_EXTERN PetscErrorCode PetscDeviceContextFork(PetscDeviceContext, PetscInt, PetscDeviceContext **);
 95: PETSC_EXTERN PetscErrorCode PetscDeviceContextJoin(PetscDeviceContext, PetscInt, PetscDeviceContextJoinMode, PetscDeviceContext **);
 96: PETSC_EXTERN PetscErrorCode PetscDeviceContextSynchronize(PetscDeviceContext);
 97: PETSC_EXTERN PetscErrorCode PetscDeviceContextDelay(PetscDeviceContext, PetscReal);
 98: PETSC_EXTERN PetscErrorCode PetscDeviceContextSetFromOptions(MPI_Comm, PetscDeviceContext);
 99: PETSC_EXTERN PetscErrorCode PetscDeviceContextView(PetscDeviceContext, PetscViewer);
100: PETSC_EXTERN PetscErrorCode PetscDeviceContextViewFromOptions(PetscDeviceContext, PetscObject, const char[]);
101: PETSC_EXTERN PetscErrorCode PetscDeviceContextGetCurrentContext(PetscDeviceContext *);
102: PETSC_EXTERN PetscErrorCode PetscDeviceContextSetCurrentContext(PetscDeviceContext);
103: PETSC_EXTERN PetscErrorCode PetscDeviceContextGetStreamHandle(PetscDeviceContext, void **);
104: PETSC_EXTERN PetscErrorCode PetscDeviceContextGetDefaultContext(PetscDeviceContext *);

106: /* memory */
107: PETSC_EXTERN PetscErrorCode PetscDeviceAllocate_Private(PetscDeviceContext, PetscBool, PetscMemType, size_t, size_t, size_t, void **PETSC_RESTRICT);
108: PETSC_EXTERN PetscErrorCode PetscDeviceDeallocate_Private(PetscDeviceContext, void *PETSC_RESTRICT);
109: PETSC_EXTERN PetscErrorCode PetscDeviceMemcpy(PetscDeviceContext, void *PETSC_RESTRICT, const void *PETSC_RESTRICT, size_t);
110: PETSC_EXTERN PetscErrorCode PetscDeviceMemset(PetscDeviceContext, void *PETSC_RESTRICT, PetscInt, size_t);

112: /*MC
113:   PetscDeviceMalloc - Allocate device-aware memory

115:   Synopsis:
116: #include <petscdevice.h>
117:   PetscErrorCode PetscDeviceMalloc(PetscDeviceContext dctx, PetscMemType mtype, size_t n, size_t alignment, Type **ptr)

119:   Not Collective, Asynchronous, Auto-dependency aware

121:   Input Parameters:
122: + dctx      - The `PetscDeviceContext` used to allocate the memory, or `PetscDeviceContextDefault`
123: . mtype     - The type of memory to allocate
124: . n         - The amount (in elements) to allocate
125: - alignment - The requested alignment in bytes, a positive power of two or `PETSC_DECIDE`

127:   Output Parameter:
128: . ptr - The pointer to store the result in

130:   Level: beginner

132:   Notes:
133:   Memory allocated with this function must be freed with `PetscDeviceFree()`.

135:   If `n` is zero, then `ptr` is set to `PETSC_NULLPTR`.

137:   CUDA and HIP contexts allocate `PETSC_MEMTYPE_HOST` memory from a pinned host pool and
138:   `PETSC_MEMTYPE_DEVICE` memory from their device pool. There is no separate pinned host
139:   memory type. The supplied context selects the backend. Obtain the current context with
140:   `PetscDeviceContextGetCurrentContext()` to use the same context as high-level PETSc operations.

142:   If the context has no memory allocation operation, only `PETSC_MEMTYPE_HOST` is supported
143:   and allocation uses `PetscMalloc1()`. In the C device-interface fallback, `dctx` and `mtype`
144:   are ignored and allocation always uses `PetscMalloc1()`. Backend allocation failures do
145:   not trigger a retry with host allocation.

147:   For CUDA and HIP pools, `PETSC_DECIDE` uses the alignment inferred for the pointed-to type
148:   in the calling translation unit. When type inference is unavailable, PETSc uses the alignment
149:   of `max_align_t` or `PETSC_MEMALIGN`. An explicit alignment is honored, or increased to the
150:   inferred alignment if necessary. This applies to both pinned host and device memory.
151:   The alignment inferred from a C complex type may be smaller than that required by the
152:   corresponding CUDA or HIP complex type. If the memory will be accessed using the CUDA
153:   or HIP type, pass that type's required alignment as `alignment`.

155:   The `PetscMalloc1()` fallback guarantees `PETSC_MEMALIGN` alignment. An explicit request
156:   that does not divide `PETSC_MEMALIGN` produces `PETSC_ERR_SUP`; `PETSC_DECIDE` retains the
157:   ordinary host allocation behavior. A zero element count returns `NULL` without allocating.

159:   Alignment of an allocation does not guarantee the same alignment for interior pointers.
160:   For structures shared with device code, the element layout and stride must also agree.

162:   Allocation, initialization, and pool reuse are ordered on `dctx`, including for pinned host
163:   memory. Contexts with `PETSC_STREAM_DEFAULT_WITH_BARRIER` or `PETSC_STREAM_NONBLOCKING_WITH_BARRIER`
164:   synchronize before returning. For other stream types, synchronize `dctx` before accessing the
165:   contents from the CPU. The alignment argument does not change this synchronization policy.

167:   This routine uses the `sizeof()` of the memory type requested to determine the total memory
168:   to be allocated, therefore you should not multiply the number of elements requested by the
169:   `sizeof()` the type\:

171: .vb
172:   PetscInt *arr;

174:   // correct
175:   PetscDeviceMalloc(dctx, PETSC_MEMTYPE_DEVICE, n, PETSC_DECIDE, &arr);

177:   // incorrect
178:   PetscDeviceMalloc(dctx, PETSC_MEMTYPE_DEVICE, n * sizeof(*arr), PETSC_DECIDE, &arr);
179: .ve

181:   Note result stored `ptr` is immediately valid and the user may freely inspect or manipulate
182:   its value on function return, i.e.\:

184: .vb
185:   PetscInt *ptr;

187:   PetscDeviceMalloc(dctx, PETSC_MEMTYPE_DEVICE, 20, PETSC_DECIDE, &ptr);

189:   PetscInt *sub_ptr = ptr + 10; // OK, no need to synchronize

191:   ptr[0] = 10; // ERROR, directly accessing contents of ptr is undefined until synchronization
192: .ve

194:   DAG representation:
195: .vb
196:   time ->

198:   -> dctx - |= CALL =| -\- dctx -->
199:                          \- ptr ->
200: .ve

202: .N ASYNC_API

204: .seealso: `PetscDeviceFree()`, `PetscDeviceCalloc()`, `PetscDeviceArrayCopy()`,
205: `PetscDeviceArrayZero()`
206: M*/
207: #define PetscDeviceMalloc(dctx, mtype, n, alignment, ptr) PetscDeviceAllocate_Private((dctx), PETSC_FALSE, (mtype), (size_t)(n) * sizeof(**(ptr)), (size_t)(alignment), PETSC_DEVICE_ALIGNOF(**(ptr)), (void **)(ptr))

209: /*MC
210:   PetscDeviceCalloc - Allocate zeroed device-aware memory

212:   Synopsis:
213: #include <petscdevice.h>
214:   PetscErrorCode PetscDeviceCalloc(PetscDeviceContext dctx, PetscMemType mtype, size_t n, size_t alignment, Type **ptr)

216:   Not Collective, Asynchronous, Auto-dependency aware

218:   Input Parameters:
219: + dctx      - The `PetscDeviceContext` used to allocate the memory, or `PetscDeviceContextDefault`
220: . mtype     - The type of memory to allocate
221: . n         - The amount (in elements) to allocate
222: - alignment - The requested alignment in bytes, a positive power of two or `PETSC_DECIDE`

224:   Output Parameter:
225: . ptr - The pointer to store the result in

227:   Level: beginner

229:   Notes:
230:   Has the same allocation, alignment, fallback, and synchronization rules as `PetscDeviceMalloc()`,
231:   and additionally queues zero-initialization on `dctx`. Zero-initialization may still be pending
232:   when this routine returns. The ordinary host fallback uses `PetscCalloc1()`.

234: .N ASYNC_API

236: .seealso: `PetscDeviceFree()`, `PetscDeviceMalloc()`, `PetscDeviceArrayCopy()`,
237: `PetscDeviceArrayZero()`
238: M*/
239: #define PetscDeviceCalloc(dctx, mtype, n, alignment, ptr) PetscDeviceAllocate_Private((dctx), PETSC_TRUE, (mtype), (size_t)(n) * sizeof(**(ptr)), (size_t)(alignment), PETSC_DEVICE_ALIGNOF(**(ptr)), (void **)(ptr))

241: /*MC
242:   PetscDeviceFree - Free device-aware memory obtained with  `PetscDeviceMalloc()` or `PetscDeviceCalloc()`

244:   Synopsis:
245: #include <petscdevice.h>
246:   PetscErrorCode PetscDeviceFree(PetscDeviceContext dctx, void *ptr)

248:   Not Collective, Asynchronous, Auto-dependency aware

250:   Input Parameters:
251: + dctx - The `PetscDeviceContext` used to free the memory
252: - ptr  - The pointer to free, may be `NULL`

254:   Level: beginner

256:   Notes:
257:   `ptr` is set to `PETSC_NULLPTR` on successful deallocation.

259:   `ptr` must have been allocated using `PetscDeviceMalloc()`, `PetscDeviceCalloc()` not `PetscMalloc()` or related routines

261:   This routine falls back to using `PetscFree()` if PETSc was not configured with device
262:   support. The user should note that `PetscFree()` frees only host memory.

264:   DAG representation:
265: .vb
266:   time ->

268:   -> dctx -/- |= CALL =| - dctx ->
269:   -> ptr -/
270: .ve

272: .N ASYNC_API

274: .seealso: `PetscDeviceMalloc()`, `PetscDeviceCalloc()`
275: M*/
276: #define PetscDeviceFree(dctx, ptr) ((PetscErrorCode)(PetscDeviceDeallocate_Private((dctx), (ptr)) || ((ptr) = PETSC_NULLPTR, PETSC_SUCCESS)))

278: /*MC
279:   PetscDeviceArrayCopy - Copy memory in a device-aware manner

281:   Synopsis:
282: #include <petscdevice.h>
283:   PetscErrorCode PetscDeviceArrayCopy(PetscDeviceContext dctx, void *dest, const void *src, size_t n)

285:   Not Collective, Asynchronous, Auto-dependency aware

287:   Input Parameters:
288: + dctx - The `PetscDeviceContext` used to copy the memory
289: . dest - The pointer to copy to
290: . src  - The pointer to copy from
291: - n    - The amount (in elements) to copy

293:   Notes:
294:   Both `dest` and `src` must have been allocated using `PetscDeviceMalloc()` or
295:   `PetscDeviceCalloc()`.

297:   This uses the `sizeof()` of the `src` memory type requested to determine the total memory to
298:   be copied, therefore you should not multiply the number of elements by the `sizeof()` the
299:   type\:

301: .vb
302:   PetscInt *to,*from;

304:   // correct
305:   PetscDeviceArrayCopy(dctx,to,from,n);

307:   // incorrect
308:   PetscDeviceArrayCopy(dctx,to,from,n*sizeof(*from));
309: .ve

311:   See `PetscDeviceMemcpy()` for further discussion.

313:   Level: beginner

315: .N ASYNC_API

317: .seealso: `PetscDeviceMalloc()`, `PetscDeviceCalloc()`, `PetscDeviceFree()`,
318: `PetscDeviceArrayZero()`, `PetscDeviceMemcpy()`
319: M*/
320: #define PetscDeviceArrayCopy(dctx, dest, src, n) PetscDeviceMemcpy((dctx), (dest), (src), (size_t)(n) * sizeof(*(src)))

322: /*MC
323:   PetscDeviceArrayZero - Zero memory in a device-aware manner

325:   Synopsis:
326: #include <petscdevice.h>
327:   PetscErrorCode PetscDeviceArrayZero(PetscDeviceContext dctx, void *ptr, size_t n)

329:   Not Collective, Asynchronous, Auto-dependency aware

331:   Input Parameters:
332: + dctx  - The `PetscDeviceContext` used to zero the memory
333: . ptr   - The pointer to the memory
334: - n     - The amount (in elements) to zero

336:   Level: beginner

338:   Notes:
339:   `ptr` must have been allocated using `PetscDeviceMalloc()` or `PetscDeviceCalloc()`.

341:   This uses the `sizeof()` of the memory type requested to determine the total memory to be
342:   zeroed, therefore you should not multiply the number of elements by the `sizeof()` the type\:

344: .vb
345:   PetscInt *ptr;

347:   // correct
348:   PetscDeviceArrayZero(dctx,ptr,n);

350:   // incorrect
351:   PetscDeviceArrayZero(dctx,ptr,n*sizeof(*ptr));
352: .ve

354:   See `PetscDeviceMemset()` for further discussion.

356: .N ASYNC_API

358: .seealso: `PetscDeviceMalloc()`, `PetscDeviceCalloc()`, `PetscDeviceFree()`,
359: `PetscDeviceArrayCopy()`, `PetscDeviceMemset()`
360: M*/
361: #define PetscDeviceArrayZero(dctx, ptr, n) PetscDeviceMemset((dctx), (ptr), 0, (size_t)(n) * sizeof(*(ptr)))