Actual source code: devicereg.c

  1: #include <petsc/private/deviceimpl.h>

  3: PetscLogEvent CUBLAS_HANDLE_CREATE;
  4: PetscLogEvent CUSOLVER_HANDLE_CREATE;
  5: PetscLogEvent HIPSOLVER_HANDLE_CREATE;
  6: PetscLogEvent HIPBLAS_HANDLE_CREATE;

  8: PetscLogEvent DCONTEXT_Create;
  9: PetscLogEvent DCONTEXT_Destroy;
 10: PetscLogEvent DCONTEXT_ChangeStream;
 11: PetscLogEvent DCONTEXT_SetDevice;
 12: PetscLogEvent DCONTEXT_SetUp;
 13: PetscLogEvent DCONTEXT_Duplicate;
 14: PetscLogEvent DCONTEXT_QueryIdle;
 15: PetscLogEvent DCONTEXT_WaitForCtx;
 16: PetscLogEvent DCONTEXT_Fork;
 17: PetscLogEvent DCONTEXT_Join;
 18: PetscLogEvent DCONTEXT_Sync;
 19: PetscLogEvent DCONTEXT_Mark;

 21: // DO NOT MOVE THESE (literally, they must be exactly here)!
 22: //
 23: // pgcc has a _very_ strange bug, where if both of these are defined at the top of this file,
 24: // then building src/sys/objects/device/test/ex2.c results in "undefined reference to
 25: // PETSC_DEVICE_CONTEXT_CLASSID". If you initialize PETSC_DEVICE_CONTEXT_CLASSID it goes
 26: // away. If you move the definition down, it goes away.
 27: PetscClassId PETSC_DEVICE_CLASSID;
 28: PetscClassId PETSC_DEVICE_CONTEXT_CLASSID;

 30: // clang-format off
 31: const char *const PetscStreamTypes[] = {
 32:   "global_blocking",
 33:   "default_blocking",
 34:   "global_nonblocking",
 35:   "max",
 36:   "PetscStreamType",
 37:   "PETSC_STREAM_",
 38:   PETSC_NULLPTR
 39: };

 41: const char *const PetscDeviceContextJoinModes[] = {
 42:   "destroy",
 43:   "sync",
 44:   "no_sync",
 45:   "PetscDeviceContextJoinMode",
 46:   "PETSC_DEVICE_CONTEXT_JOIN_",
 47:   PETSC_NULLPTR
 48: };

 50: const char *const PetscDeviceTypes[] = {
 51:   "host",
 52:   "cuda",
 53:   "hip",
 54:   "sycl",
 55:   "max",
 56:   "PetscDeviceType",
 57:   "PETSC_DEVICE_",
 58:   PETSC_NULLPTR
 59: };

 61: const char *const PetscDeviceInitTypes[] = {
 62:   "none",
 63:   "lazy",
 64:   "eager",
 65:   "PetscDeviceInitType",
 66:   "PETSC_DEVICE_INIT_",
 67:   PETSC_NULLPTR
 68: };

 70: #ifdef __cplusplus
 71: #include <petsc/private/cpp/type_traits.hpp>

 73: static_assert(Petsc::util::to_underlying(PETSC_DEVICE_INIT_NONE) == 0, "");
 74: static_assert(Petsc::util::to_underlying(PETSC_DEVICE_INIT_LAZY) == 1, "");
 75: static_assert(Petsc::util::to_underlying(PETSC_DEVICE_INIT_EAGER) == 2, "");

 77: static_assert(
 78:   PETSC_STATIC_ARRAY_LENGTH(PetscDeviceInitTypes) == 6,
 79:   "Must change CUPMDevice<T>::initialize number of enum values in -device_enable_cupm to match!"
 80: );
 81: #endif

 83: const char *const PetscDeviceAttributes[] = {
 84:   "shared_mem_per_block",
 85:   "max",
 86:   "PetscDeviceAttribute",
 87:   "PETSC_DEVICE_ATTR_",
 88:   PETSC_NULLPTR
 89: };
 90: // clang-format on

 92: static PetscBool registered = PETSC_FALSE;

 94: static PetscErrorCode PetscDeviceRegisterEvent_Private(const char name[], PetscClassId id, PetscLogEvent *event)
 95: {
 96:   PetscFunctionBegin;
 97:   PetscCall(PetscLogEventRegister(name, id, event));
 98:   PetscCall(PetscLogEventSetCollective(*event, PETSC_FALSE));
 99:   PetscFunctionReturn(PETSC_SUCCESS);
100: }

102: /*@C
103:   PetscDeviceFinalizePackage - This function cleans up all components of the `PetscDevice`
104:   package. It is called from `PetscFinalize()`.

106:   Developer Notes:
107:   This function is automatically registered to be called during `PetscFinalize()` by
108:   `PetscDeviceInitializePackage()` so there should be no need to call it yourself.

110:   Level: developer

112: .seealso: `PetscFinalize()`, `PetscDeviceInitializePackage()`
113: @*/
114: PetscErrorCode PetscDeviceFinalizePackage(void)
115: {
116:   PetscFunctionBegin;
117:   registered = PETSC_FALSE;
118:   PetscFunctionReturn(PETSC_SUCCESS);
119: }

121: /*@C
122:   PetscDeviceInitializePackage - This function initializes everything in the `PetscDevice`
123:   package. It is called on the first call to `PetscDeviceContextCreate()` or
124:   `PetscDeviceCreate()` when using shared or static libraries.

126:   Level: developer

128: .seealso: `PetscInitialize()`, `PetscDeviceFinalizePackage()`, `PetscDeviceContextCreate()`,
129: `PetscDeviceCreate()`
130: @*/
131: PetscErrorCode PetscDeviceInitializePackage(void)
132: {
133:   PetscFunctionBegin;
134:   PetscCheck(PetscDeviceConfiguredFor_Internal(PETSC_DEVICE_DEFAULT()), PETSC_COMM_SELF, PETSC_ERR_SUP, "PETSc is not configured with device support (PETSC_DEVICE_DEFAULT = '%s')", PetscDeviceTypes[PETSC_DEVICE_DEFAULT()]);
135:   if (PetscLikely(registered)) PetscFunctionReturn(PETSC_SUCCESS);
136:   registered = PETSC_TRUE;
137:   PetscCall(PetscRegisterFinalize(PetscDeviceFinalizePackage));
138:   // class registration
139:   PetscCall(PetscClassIdRegister("PetscDevice", &PETSC_DEVICE_CLASSID));
140:   PetscCall(PetscClassIdRegister("PetscDeviceContext", &PETSC_DEVICE_CONTEXT_CLASSID));
141:   // events
142:   if (PetscDefined(HAVE_CUDA)) {
143:     PetscCall(PetscDeviceRegisterEvent_Private("cuBLAS Init", PETSC_DEVICE_CONTEXT_CLASSID, &CUBLAS_HANDLE_CREATE));
144:     PetscCall(PetscDeviceRegisterEvent_Private("cuSolver Init", PETSC_DEVICE_CONTEXT_CLASSID, &CUSOLVER_HANDLE_CREATE));
145:   }
146:   if (PetscDefined(HAVE_HIP)) {
147:     PetscCall(PetscDeviceRegisterEvent_Private("hipBLAS Init", PETSC_DEVICE_CONTEXT_CLASSID, &HIPBLAS_HANDLE_CREATE));
148:     PetscCall(PetscDeviceRegisterEvent_Private("hipSolver Init", PETSC_DEVICE_CONTEXT_CLASSID, &HIPSOLVER_HANDLE_CREATE));
149:   }
150:   PetscCall(PetscDeviceRegisterEvent_Private("DCtxCreate", PETSC_DEVICE_CONTEXT_CLASSID, &DCONTEXT_Create));
151:   PetscCall(PetscDeviceRegisterEvent_Private("DCtxDestroy", PETSC_DEVICE_CONTEXT_CLASSID, &DCONTEXT_Destroy));
152:   PetscCall(PetscDeviceRegisterEvent_Private("DCtxChangeStream", PETSC_DEVICE_CONTEXT_CLASSID, &DCONTEXT_ChangeStream));
153:   PetscCall(PetscDeviceRegisterEvent_Private("DCtxSetUp", PETSC_DEVICE_CONTEXT_CLASSID, &DCONTEXT_SetUp));
154:   PetscCall(PetscDeviceRegisterEvent_Private("DCtxSetDevice", PETSC_DEVICE_CONTEXT_CLASSID, &DCONTEXT_SetDevice));
155:   PetscCall(PetscDeviceRegisterEvent_Private("DCtxDuplicate", PETSC_DEVICE_CONTEXT_CLASSID, &DCONTEXT_Duplicate));
156:   PetscCall(PetscDeviceRegisterEvent_Private("DCtxQueryIdle", PETSC_DEVICE_CONTEXT_CLASSID, &DCONTEXT_QueryIdle));
157:   PetscCall(PetscDeviceRegisterEvent_Private("DCtxWaitForCtx", PETSC_DEVICE_CONTEXT_CLASSID, &DCONTEXT_WaitForCtx));
158:   PetscCall(PetscDeviceRegisterEvent_Private("DCtxFork", PETSC_DEVICE_CONTEXT_CLASSID, &DCONTEXT_Fork));
159:   PetscCall(PetscDeviceRegisterEvent_Private("DCtxJoin", PETSC_DEVICE_CONTEXT_CLASSID, &DCONTEXT_Join));
160:   PetscCall(PetscDeviceRegisterEvent_Private("DCtxSync", PETSC_DEVICE_CONTEXT_CLASSID, &DCONTEXT_Sync));
161:   PetscCall(PetscDeviceRegisterEvent_Private("DCtxMark", PETSC_DEVICE_CONTEXT_CLASSID, &DCONTEXT_Mark));
162:   {
163:     const PetscClassId classids[] = {PETSC_DEVICE_CONTEXT_CLASSID, PETSC_DEVICE_CLASSID};

165:     PetscCall(PetscInfoProcessClass("device", PETSC_STATIC_ARRAY_LENGTH(classids), classids));
166:   }
167:   PetscFunctionReturn(PETSC_SUCCESS);
168: }