Actual source code: deviceimpl.h
1: #pragma once
3: #include <petscdevice.h>
4: #include <petsc/private/petscimpl.h>
5: #include <petscpkg_version.h>
7: #if PetscDefined(HAVE_CUPM)
8: PETSC_INTERN int PetscDeviceCUPMRuntimeArch; // The real CUDA/HIP arch the code is run with. For log view and error diagnosis
9: #endif
11: /* logging support */
12: PETSC_INTERN PetscLogEvent CUBLAS_HANDLE_CREATE;
13: PETSC_INTERN PetscLogEvent CUSOLVER_HANDLE_CREATE;
14: PETSC_INTERN PetscLogEvent CUSPARSE_HANDLE_CREATE;
15: PETSC_INTERN PetscLogEvent HIPSPARSE_HANDLE_CREATE;
16: PETSC_INTERN PetscLogEvent HIPSOLVER_HANDLE_CREATE;
17: PETSC_INTERN PetscLogEvent HIPBLAS_HANDLE_CREATE;
19: PETSC_INTERN PetscLogEvent DCONTEXT_Create;
20: PETSC_INTERN PetscLogEvent DCONTEXT_Destroy;
21: PETSC_INTERN PetscLogEvent DCONTEXT_ChangeStream;
22: PETSC_INTERN PetscLogEvent DCONTEXT_SetDevice;
23: PETSC_INTERN PetscLogEvent DCONTEXT_SetUp;
24: PETSC_INTERN PetscLogEvent DCONTEXT_Duplicate;
25: PETSC_INTERN PetscLogEvent DCONTEXT_QueryIdle;
26: PETSC_INTERN PetscLogEvent DCONTEXT_WaitForCtx;
27: PETSC_INTERN PetscLogEvent DCONTEXT_Fork;
28: PETSC_INTERN PetscLogEvent DCONTEXT_Join;
29: PETSC_INTERN PetscLogEvent DCONTEXT_Sync;
30: PETSC_INTERN PetscLogEvent DCONTEXT_Mark;
32: /* type cast macros for some additional type-safety in C++ land */
33: #if defined(__cplusplus)
34: #define PetscStreamTypeCast(...) static_cast<PetscStreamType>(__VA_ARGS__)
35: #define PetscDeviceTypeCast(...) static_cast<PetscDeviceType>(__VA_ARGS__)
36: #define PetscDeviceInitTypeCast(...) static_cast<PetscDeviceInitType>(__VA_ARGS__)
37: #else
38: #define PetscStreamTypeCast(...) ((PetscStreamType)(__VA_ARGS__))
39: #define PetscDeviceTypeCast(...) ((PetscDeviceType)(__VA_ARGS__))
40: #define PetscDeviceInitTypeCast(...) ((PetscDeviceInitType)(__VA_ARGS__))
41: #endif
43: #if PetscDefined(CLANG_STATIC_ANALYZER)
44: template <typename T>
46: template <typename T, typename U>
47: extern void PetscCheckCompatibleDeviceTypes(T, int, U, int);
48: template <typename T>
50: template <typename T>
52: template <typename T, typename U>
53: extern void PetscCheckCompatibleDevices(T, int, U, int);
54: template <typename T>
56: template <typename T>
58: template <typename T, typename U>
59: extern void PetscCheckCompatibleDeviceContexts(T, int, U, int);
60: #elif PetscDefined(DEVICELANGUAGE_CXX) && (PetscDefined(USE_DEBUG) || PetscDefined(DEVICE_KEEP_ERROR_CHECKING_MACROS))
62: do { \
63: PetscDeviceType pvdt_dtype_ = PetscDeviceTypeCast(dtype); \
64: int pvdt_argno_ = (int)(argno); \
65: PetscCheck(((int)pvdt_dtype_ >= (int)PETSC_DEVICE_HOST) && ((int)pvdt_dtype_ <= (int)PETSC_DEVICE_MAX), PETSC_COMM_SELF, PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown PetscDeviceType '%d': Argument #%d", pvdt_dtype_, pvdt_argno_); \
66: if (PetscUnlikely(!PetscDeviceConfiguredFor_Internal(pvdt_dtype_))) { \
67: PetscCheck((int)pvdt_dtype_ != (int)PETSC_DEVICE_MAX, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Invalid PetscDeviceType '%s': Argument #%d", PetscDeviceTypes[pvdt_dtype_], pvdt_argno_); \
68: SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, \
69: "Not configured for PetscDeviceType '%s': Argument #%d;" \
70: " run configure --help %s for available options", \
71: PetscDeviceTypes[pvdt_dtype_], pvdt_argno_, PetscDeviceTypes[pvdt_dtype_]); \
72: } \
73: } while (0)
75: #define PetscCheckCompatibleDeviceTypes(dtype1, argno1, dtype2, argno2) \
76: do { \
77: PetscDeviceType pccdt_dtype1_ = PetscDeviceTypeCast(dtype1); \
78: PetscDeviceType pccdt_dtype2_ = PetscDeviceTypeCast(dtype2); \
81: PetscCheck(pccdt_dtype1_ == pccdt_dtype2_, PETSC_COMM_SELF, PETSC_ERR_ARG_NOTSAMETYPE, "PetscDeviceTypes are incompatible: Arguments #%d and #%d. Expected PetscDeviceType '%s' but have '%s' instead", argno1, argno2, PetscDeviceTypes[pccdt_dtype1_], PetscDeviceTypes[pccdt_dtype2_]); \
82: } while (0)
85: do { \
86: PetscDevice pvd_dev_ = dev; \
87: int pvd_argno_ = (int)(argno); \
88: PetscAssertPointer(pvd_dev_, pvd_argno_); \
90: PetscCheck(pvd_dev_->id >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid PetscDevice: Argument #%d; id %" PetscInt_FMT " < 0", pvd_argno_, pvd_dev_->id); \
91: PetscCheck(pvd_dev_->refcnt >= 0, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid PetscDevice: Argument #%d; negative reference count %" PetscInt_FMT, pvd_argno_, pvd_dev_->refcnt); \
92: } while (0)
95: do { \
96: PetscDeviceAttribute pvda_attr_ = (dattr); \
97: int pvda_argno_ = (int)(argno); \
98: PetscCheck((((int)pvda_attr_) >= 0) && (pvda_attr_ <= PETSC_DEVICE_ATTR_MAX), PETSC_COMM_SELF, PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown PetscDeviceAttribute '%d': Argument #%d", (int)pvda_attr_, pvda_argno_); \
99: PetscCheck(pvda_attr_ != PETSC_DEVICE_ATTR_MAX, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Invalid PetscDeviceAttribute '%s': Argument #%d", PetscDeviceAttributes[pvda_attr_], pvda_argno_); \
100: } while (0)
102: /*
103: for now just checks strict equality, but this can be changed as some devices (i.e. kokkos and
104: any cupm should be compatible once implemented)
105: */
106: #define PetscCheckCompatibleDevices(dev1, argno1, dev2, argno2) \
107: do { \
108: PetscDevice pccd_dev1_ = (dev1), pccd_dev2_ = (dev2); \
109: int pccd_argno1_ = (int)(argno1), pccd_argno2_ = (int)(argno2); \
112: PetscCheckCompatibleDeviceTypes(pccd_dev1_->type, pccd_argno1_, pccd_dev2_->type, pccd_argno2_); \
113: } while (0)
116: do { \
117: PetscStreamType pvst_stype_ = PetscStreamTypeCast(stype); \
118: int pvst_argno_ = (int)(argno); \
119: PetscCheck(((int)pvst_stype_ >= 0) && ((int)pvst_stype_ <= (int)PETSC_STREAM_MAX), PETSC_COMM_SELF, PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown PetscStreamType '%d': Argument #%d", pvst_stype_, pvst_argno_); \
120: PetscCheck((int)pvst_stype_ != (int)PETSC_STREAM_MAX, PETSC_COMM_SELF, PETSC_ERR_ARG_INCOMP, "Invalid PetscStreamType '%s': Argument #%d", PetscStreamTypes[pvst_stype_], pvst_argno_); \
121: } while (0)
124: do { \
125: PetscDeviceContext pvdc_dctx_ = dctx; \
126: int pvdc_argno_ = (int)(argno); \
127: PetscCheck(pvdc_dctx_ != PetscDeviceContextDefault, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Argument #%d does not accept PetscDeviceContextDefault; pass a concrete PetscDeviceContext", pvdc_argno_); \
130: if (pvdc_dctx_->device) { \
132: } else { \
133: PetscCheck(!pvdc_dctx_->setup, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, \
134: "Invalid PetscDeviceContext: Argument #%d; " \
135: "PetscDeviceContext is setup but has no PetscDevice", \
136: pvdc_argno_); \
137: } \
138: PetscCheck(((PetscObject)pvdc_dctx_)->id >= 1, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid PetscDeviceContext: Argument #%d; id %" PetscInt64_FMT " < 1", pvdc_argno_, ((PetscObject)pvdc_dctx_)->id); \
139: PetscCheck(pvdc_dctx_->numChildren <= pvdc_dctx_->maxNumChildren, PETSC_COMM_SELF, PETSC_ERR_ARG_CORRUPT, "Invalid PetscDeviceContext: Argument #%d; number of children %" PetscInt_FMT " > max number of children %" PetscInt_FMT, pvdc_argno_, \
140: pvdc_dctx_->numChildren, pvdc_dctx_->maxNumChildren); \
141: } while (0)
143: #define PetscCheckCompatibleDeviceContexts(dctx1, argno1, dctx2, argno2) \
144: do { \
145: PetscDeviceContext pccdc_dctx1_ = (dctx1), pccdc_dctx2_ = (dctx2); \
146: int pccdc_argno1_ = (int)(argno1), pccdc_argno2_ = (int)(argno2); \
149: if (pccdc_dctx1_->device && pccdc_dctx2_->device) PetscCheckCompatibleDevices(pccdc_dctx1_->device, pccdc_argno1_, pccdc_dctx2_->device, pccdc_argno2_); \
150: } while (0)
151: #else /* PetscDefined(USE_DEBUG) */
153: #define PetscCheckCompatibleDeviceTypes(dtype1, argno1, dtype2, argno2)
156: #define PetscCheckCompatibleDevices(dev1, argno1, dev2, argno2)
159: #define PetscCheckCompatibleDeviceContexts(dctx1, argno1, dctx2, argno2) \
160: do { \
161: } while (0)
162: #endif /* PetscDefined(USE_DEBUG) */
164: /* if someone is ready to rock with more than 128 GPUs on hand then we're in real trouble */
165: #define PETSC_DEVICE_MAX_DEVICES 128
167: // If hwloc is not present, this macro will not be defined in petscpkg_version.h and block following will fail to compile
168: #if !defined(PETSC_PKG_HWLOC_VERSION_GE)
169: #define PETSC_PKG_HWLOC_VERSION_GE(MAJ, MIN, PAT) 0
170: #endif
172: #if PetscDefined(HAVE_HWLOC) && PetscDefined(HAVE_CUPM) && PETSC_PKG_HWLOC_VERSION_GE(2, 0, 0)
173: #define PETSC_DEVICE_TOPOLOGY_AWARE -2
174: // help string and lower bound for the "-device_select" option, shared by device.cxx and impldevicebase.hpp
175: #define PETSC_DEVICE_SELECT_HELP \
176: "Which device to use. Pass " PetscStringize(PETSC_DECIDE) " to have PETSc decide, " PetscStringize(PETSC_DEVICE_TOPOLOGY_AWARE) " to use a topology-aware selection algorithm or (given they exist) [0-" PetscStringize(PETSC_DEVICE_MAX_DEVICES) ") for a specific device"
177: #define PETSC_DEVICE_SELECT_LOWER_BOUND PETSC_DEVICE_TOPOLOGY_AWARE
178: #else
179: #define PETSC_DEVICE_SELECT_HELP "Which device to use. Pass " PetscStringize(PETSC_DECIDE) " to have PETSc decide or (given they exist) [0-" PetscStringize(PETSC_DEVICE_MAX_DEVICES) ") for a specific device"
180: #define PETSC_DEVICE_SELECT_LOWER_BOUND PETSC_DECIDE
181: #endif
183: /*
184: the configure-time default device type, used as the initial the value of
185: PETSC_DEVICE_DEFAULT() as well as what it is restored to during PetscFinalize()
186: */
187: #if PetscDefined(HAVE_HIP)
188: #define PETSC_DEVICE_HARDWARE_DEFAULT_TYPE PETSC_DEVICE_HIP
189: #elif PetscDefined(HAVE_CUDA)
190: #define PETSC_DEVICE_HARDWARE_DEFAULT_TYPE PETSC_DEVICE_CUDA
191: #elif PetscDefined(HAVE_SYCL)
192: #define PETSC_DEVICE_HARDWARE_DEFAULT_TYPE PETSC_DEVICE_SYCL
193: #else
194: #define PETSC_DEVICE_HARDWARE_DEFAULT_TYPE PETSC_DEVICE_HOST
195: #endif
197: #define PETSC_DEVICE_CONTEXT_DEFAULT_DEVICE_TYPE PETSC_DEVICE_HARDWARE_DEFAULT_TYPE
198: #define PETSC_DEVICE_CONTEXT_DEFAULT_STREAM_TYPE PETSC_STREAM_DEFAULT
200: typedef struct _DeviceOps *DeviceOps;
201: struct _DeviceOps {
202: /* the creation routine for the corresponding PetscDeviceContext, this is NOT intended
203: * to be called by the PetscDevice itself */
204: PetscErrorCode (*createcontext)(PetscDeviceContext);
205: PetscErrorCode (*configure)(PetscDevice);
206: PetscErrorCode (*view)(PetscDevice, PetscViewer);
207: PetscErrorCode (*getattribute)(PetscDevice, PetscDeviceAttribute, void *);
208: };
210: struct _n_PetscDevice {
211: struct _DeviceOps ops[1];
212: void *data; /* placeholder */
213: PetscInt refcnt; /* reference count for the device */
214: PetscInt id; /* unique id per created PetscDevice */
215: PetscInt deviceId; /* the id of the underlying device, i.e. the return of
216: * cudaGetDevice() for example */
217: PetscDeviceType type; /* type of device */
218: };
220: typedef struct _n_PetscEvent *PetscEvent;
221: struct _n_PetscEvent {
222: PetscDeviceType dtype; // this cannot change for the lifetime of the event
223: PetscObjectId dctx_id; // id of last dctx to record this event
224: PetscObjectState dctx_state; // state of last dctx to record this event
225: void *data; // event handle
226: PetscErrorCode (*destroy)(PetscEvent);
227: };
229: typedef struct _DeviceContextOps *DeviceContextOps;
230: struct _DeviceContextOps {
231: PetscErrorCode (*destroy)(PetscDeviceContext);
232: PetscErrorCode (*changestreamtype)(PetscDeviceContext, PetscStreamType);
233: PetscErrorCode (*setup)(PetscDeviceContext);
234: PetscErrorCode (*query)(PetscDeviceContext, PetscBool *);
235: PetscErrorCode (*waitforcontext)(PetscDeviceContext, PetscDeviceContext);
236: PetscErrorCode (*synchronize)(PetscDeviceContext);
237: PetscErrorCode (*getblashandle)(PetscDeviceContext, void *);
238: PetscErrorCode (*getsolverhandle)(PetscDeviceContext, void *);
239: PetscErrorCode (*getsparsehandle)(PetscDeviceContext, void *);
240: PetscErrorCode (*getstreamhandle)(PetscDeviceContext, void **);
241: PetscErrorCode (*begintimer)(PetscDeviceContext);
242: PetscErrorCode (*endtimer)(PetscDeviceContext, PetscLogDouble *);
243: PetscErrorCode (*getpower)(PetscDeviceContext, PetscLogDouble *);
244: PetscErrorCode (*beginenergymeter)(PetscDeviceContext);
245: PetscErrorCode (*endenergymeter)(PetscDeviceContext, PetscLogDouble *);
246: PetscErrorCode (*memalloc)(PetscDeviceContext, PetscBool, PetscMemType, size_t, size_t, void **); // optional
247: PetscErrorCode (*memfree)(PetscDeviceContext, PetscMemType, void **); // optional
248: PetscErrorCode (*memcopy)(PetscDeviceContext, void *PETSC_RESTRICT, const void *PETSC_RESTRICT, size_t, PetscDeviceCopyMode); // optional
249: PetscErrorCode (*memset)(PetscDeviceContext, PetscMemType, void *, PetscInt, size_t); // optional
250: PetscErrorCode (*createevent)(PetscDeviceContext, PetscEvent); // optional
251: PetscErrorCode (*recordevent)(PetscDeviceContext, PetscEvent); // optional
252: PetscErrorCode (*waitforevent)(PetscDeviceContext, PetscEvent); // optional
253: PetscErrorCode (*delay)(PetscDeviceContext, PetscReal); // optional
254: };
256: struct _p_PetscDeviceContext {
257: PETSCHEADER(struct _DeviceContextOps);
258: PetscDevice device; /* the device this context stems from */
259: void *data; /* solver contexts, event, stream */
260: PetscObjectId *childIDs; /* array containing ids of contexts currently forked from this one */
261: PetscInt numChildren; /* how many children does this context expect to destroy */
262: PetscInt maxNumChildren; /* how many children can this context have room for without realloc'ing */
263: PetscStreamType streamType; /* how should this contexts stream behave around other streams? */
264: PetscBool setup;
265: PetscBool usersetdevice;
266: };
268: // ===================================================================================
269: // PetscDevice Internal Functions
270: // ===================================================================================
271: PETSC_INTERN PetscErrorCode PetscDeviceInitializeFromOptions_Internal(MPI_Comm);
272: PETSC_SINGLE_LIBRARY_INTERN PetscErrorCode PetscDeviceGetDefaultForType_Internal(PetscDeviceType, PetscDevice *);
274: static inline PetscErrorCode PetscDeviceReference_Internal(PetscDevice device)
275: {
276: PetscFunctionBegin;
277: if (PetscDefined(DEVICELANGUAGE_CXX)) ++device->refcnt;
278: PetscFunctionReturn(PETSC_SUCCESS);
279: }
281: #if PetscDefined(DEVICELANGUAGE_CXX)
282: static inline PetscErrorCode PetscDeviceDereference_Internal(PetscDevice device)
283: {
284: PetscFunctionBegin;
285: --device->refcnt;
286: PetscAssert(device->refcnt >= 0, PETSC_COMM_SELF, PETSC_ERR_ARG_CORRUPT, "PetscDevice has negative reference count %" PetscInt_FMT, device->refcnt);
287: PetscFunctionReturn(PETSC_SUCCESS);
288: }
290: static inline PetscErrorCode PetscDeviceCheckDeviceCount_Internal(PetscInt count)
291: {
292: PetscFunctionBegin;
293: PetscAssert(count < PETSC_DEVICE_MAX_DEVICES, PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Detected %" PetscInt_FMT " devices, which is larger than maximum supported number of devices %d", count, PETSC_DEVICE_MAX_DEVICES);
294: PetscFunctionReturn(PETSC_SUCCESS);
295: }
296: #endif /* PETSC_DEVICELANGUAGE_CXX for PetscDevice Internal Functions */
298: /* More general form of PetscDeviceDefaultType_Internal(), as it calls the former using
299: * the automatically selected default PetscDeviceType */
300: #define PetscDeviceGetDefault_Internal(device) PetscDeviceGetDefaultForType_Internal(PETSC_DEVICE_DEFAULT(), device)
302: static inline PETSC_CONSTEXPR_14 PetscBool PetscDeviceConfiguredFor_Internal(PetscDeviceType type)
303: {
304: switch (type) {
305: case PETSC_DEVICE_HOST:
306: return PETSC_TRUE;
307: /* casts are needed in C++ */
308: case PETSC_DEVICE_CUDA:
309: return (PetscBool)PetscDefined(HAVE_CUDA);
310: case PETSC_DEVICE_HIP:
311: return (PetscBool)PetscDefined(HAVE_HIP);
312: case PETSC_DEVICE_SYCL:
313: return (PetscBool)PetscDefined(HAVE_SYCL);
314: case PETSC_DEVICE_MAX:
315: return PETSC_FALSE;
316: /* Do not add default case! Will make compiler warn on new additions to PetscDeviceType! */
317: }
318: PetscUnreachable();
319: return PETSC_FALSE;
320: }
322: // ===================================================================================
323: // PetscDeviceContext Internal Functions
324: // ===================================================================================
325: // Validate a public argument without initializing the default context.
326: #if PetscDefined(USE_DEBUG) || PetscDefined(DEVICE_KEEP_ERROR_CHECKING_MACROS) || PetscDefined(CLANG_STATIC_ANALYZER)
328: do { \
329: PetscDeviceContext pvdcod_dctx_ = (dctx); \
331: } while (0)
332: #else
334: #endif
336: static inline PetscErrorCode PetscDeviceContextResolveDefault_Private(PetscDeviceContext *dctx)
337: {
338: PetscFunctionBegin;
339: if (*dctx == PetscDeviceContextDefault) PetscCall(PetscDeviceContextGetDefaultContext(dctx));
340: PetscFunctionReturn(PETSC_SUCCESS);
341: }
343: #if PetscDefined(DEVICELANGUAGE_CXX)
344: static inline PetscErrorCode PetscDeviceContextGetBLASHandle_Internal(PetscDeviceContext dctx, void *handle)
345: {
346: PetscFunctionBegin;
347: /* we do error checking here as this routine is an entry-point */
349: PetscUseTypeMethod(dctx, getblashandle, handle);
350: PetscFunctionReturn(PETSC_SUCCESS);
351: }
353: static inline PetscErrorCode PetscDeviceContextGetSOLVERHandle_Internal(PetscDeviceContext dctx, void *handle)
354: {
355: PetscFunctionBegin;
356: /* we do error checking here as this routine is an entry-point */
358: PetscUseTypeMethod(dctx, getsolverhandle, handle);
359: PetscFunctionReturn(PETSC_SUCCESS);
360: }
362: static inline PetscErrorCode PetscDeviceContextGetSPARSEHandle_Internal(PetscDeviceContext dctx, void *handle)
363: {
364: PetscFunctionBegin;
365: /* we do error checking here as this routine is an entry-point */
367: PetscUseTypeMethod(dctx, getsparsehandle, handle);
368: PetscFunctionReturn(PETSC_SUCCESS);
369: }
371: static inline PetscErrorCode PetscDeviceContextGetStreamHandle_Internal(PetscDeviceContext dctx, void **handle)
372: {
373: PetscFunctionBegin;
374: /* we do error checking here as this routine is an entry-point */
376: PetscAssertPointer(handle, 2);
377: PetscUseTypeMethod(dctx, getstreamhandle, handle);
378: PetscFunctionReturn(PETSC_SUCCESS);
379: }
381: static inline PetscErrorCode PetscDeviceContextBeginTimer_Internal(PetscDeviceContext dctx)
382: {
383: PetscFunctionBegin;
384: /* we do error checking here as this routine is an entry-point */
386: PetscUseTypeMethod(dctx, begintimer);
387: PetscFunctionReturn(PETSC_SUCCESS);
388: }
390: static inline PetscErrorCode PetscDeviceContextEndTimer_Internal(PetscDeviceContext dctx, PetscLogDouble *elapsed)
391: {
392: PetscFunctionBegin;
393: /* we do error checking here as this routine is an entry-point */
395: PetscAssertPointer(elapsed, 2);
396: PetscUseTypeMethod(dctx, endtimer, elapsed);
397: PetscFunctionReturn(PETSC_SUCCESS);
398: }
400: #if PetscDefined(HAVE_NVML)
401: static inline PetscErrorCode PetscDeviceContextGetPower_Internal(PetscDeviceContext dctx, PetscLogDouble *power)
402: {
403: PetscFunctionBegin;
405: PetscAssertPointer(power, 2);
406: PetscUseTypeMethod(dctx, getpower, power);
407: PetscFunctionReturn(PETSC_SUCCESS);
408: }
409: #endif
411: static inline PetscErrorCode PetscDeviceContextBeginEnergyMeter_Internal(PetscDeviceContext dctx)
412: {
413: PetscFunctionBegin;
414: /* we do error checking here as this routine is an entry-point */
416: PetscUseTypeMethod(dctx, beginenergymeter);
417: PetscFunctionReturn(PETSC_SUCCESS);
418: }
420: static inline PetscErrorCode PetscDeviceContextEndEnergyMeter_Internal(PetscDeviceContext dctx, PetscLogDouble *energy)
421: {
422: PetscFunctionBegin;
423: /* we do error checking here as this routine is an entry-point */
425: PetscAssertPointer(energy, 2);
426: PetscUseTypeMethod(dctx, endenergymeter, energy);
427: PetscFunctionReturn(PETSC_SUCCESS);
428: }
429: #endif /* PETSC_DEVICELANGUAGE_CXX for PetscDeviceContext Internal Functions */
431: /* note, only does assertion checking in debug mode */
432: static inline PetscErrorCode PetscDeviceContextGetCurrentContextAssertType_Internal(PetscDeviceContext *dctx, PetscDeviceType type)
433: {
434: PetscFunctionBegin;
435: PetscCall(PetscDeviceContextGetCurrentContext(dctx));
436: if (PetscDefined(USE_DEBUG)) {
437: PetscDeviceType dtype;
440: PetscCall(PetscDeviceContextGetDeviceType(*dctx, &dtype));
441: PetscCheckCompatibleDeviceTypes(dtype, 1, type, 2);
442: } else (void)type;
443: PetscFunctionReturn(PETSC_SUCCESS);
444: }
446: PETSC_INTERN PetscErrorCode PetscDeviceGetAllocationAlignment_Private(size_t, size_t, PetscBool, size_t *);
447: #if PetscDefined(DEVICELANGUAGE_CXX)
448: PETSC_INTERN PetscErrorCode PetscDeviceContextRecordMemoryAccess_Private(PetscDeviceContext, PetscObjectId);
449: #endif
451: /* Experimental API -- it will eventually become public */
452: PETSC_EXTERN PetscErrorCode PetscDeviceRegisterMemory(const void *PETSC_RESTRICT, PetscMemType, size_t);
453: PETSC_EXTERN PetscErrorCode PetscDeviceGetAttribute(PetscDevice, PetscDeviceAttribute, void *);
454: #if PetscDefined(DEVICELANGUAGE_CXX)
455: PETSC_EXTERN PetscErrorCode PetscDeviceContextMarkIntentFromID(PetscDeviceContext, PetscObjectId, PetscMemoryAccessMode, const char name[]);
456: #endif
457: // Used for testing purposes, internal use ONLY
458: PETSC_EXTERN PetscErrorCode PetscGetMarkedObjectMap_Internal(size_t *, PetscObjectId **, PetscMemoryAccessMode **, size_t **, PetscEvent ***);
459: PETSC_EXTERN PetscErrorCode PetscRestoreMarkedObjectMap_Internal(size_t, PetscObjectId **, PetscMemoryAccessMode **, size_t **, PetscEvent ***);
460: #if PetscDefined(DEVICELANGUAGE_CXX) && defined(__cplusplus)
461: namespace
462: {
464: inline PetscErrorCode PetscDeviceContextMarkIntentFromID(PetscDeviceContext dctx, PetscObject obj, PetscMemoryAccessMode mode, const char name[])
465: {
466: PetscFunctionBegin;
467: PetscCall(PetscDeviceContextMarkIntentFromID(dctx, obj->id, mode, name));
468: PetscFunctionReturn(PETSC_SUCCESS);
469: }
471: } // anonymous namespace
472: #endif
474: PETSC_INTERN PetscErrorCode PetscDeviceContextCreate_HOST(PetscDeviceContext);
475: #if PetscDefined(HAVE_CUDA)
476: PETSC_INTERN PetscErrorCode PetscDeviceContextCreate_CUDA(PetscDeviceContext);
477: #endif
478: #if PetscDefined(HAVE_HIP)
479: PETSC_INTERN PetscErrorCode PetscDeviceContextCreate_HIP(PetscDeviceContext);
480: #endif
481: #if PetscDefined(HAVE_SYCL)
482: PETSC_INTERN PetscErrorCode PetscDeviceContextCreate_SYCL(PetscDeviceContext);
483: #endif
485: static inline PetscErrorCode PetscDeviceContextSynchronizeIfWithBarrier_Internal(PetscDeviceContext dctx)
486: {
487: PetscStreamType stream_type;
489: PetscFunctionBegin;
490: PetscCall(PetscDeviceContextGetStreamType(dctx, &stream_type));
491: if (stream_type == PETSC_STREAM_DEFAULT_WITH_BARRIER || stream_type == PETSC_STREAM_NONBLOCKING_WITH_BARRIER) PetscCall(PetscDeviceContextSynchronize(dctx));
492: PetscFunctionReturn(PETSC_SUCCESS);
493: }
495: #if PetscDefined(HAVE_NVML)
497: #define PetscCallNVML(...) \
498: do { \
499: nvmlReturn_t nvmlerr = __VA_ARGS__; \
500: PetscCheck(nvmlerr == NVML_SUCCESS, PETSC_COMM_SELF, PETSC_ERR_LIB, "Error in %s, error string: %s", __func__, nvmlErrorString(nvmlerr)); \
501: } while (0)
503: #endif