xref: /petsc/src/sys/objects/device/util/devicereg.c (revision 4dfa11a44d5adf2389f1d3acbc8f3c1116dc6c3a)
1 #include <petsc/private/deviceimpl.h> /*I <petscdevice.h> I*/
2 
3 PetscLogEvent CUBLAS_HANDLE_CREATE;
4 PetscLogEvent CUSOLVER_HANDLE_CREATE;
5 PetscLogEvent HIPSOLVER_HANDLE_CREATE;
6 PetscLogEvent HIPBLAS_HANDLE_CREATE;
7 
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;
20 
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;
29 
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 };
40 
41 const char *const PetscDeviceContextJoinModes[] = {
42   "destroy",
43   "sync",
44   "no_sync",
45   "PetscDeviceContextJoinMode",
46   "PETSC_DEVICE_CONTEXT_JOIN_",
47   PETSC_NULLPTR
48 };
49 
50 const char *const PetscDeviceTypes[] = {
51   "host",
52   "cuda",
53   "hip",
54   "sycl",
55   "max",
56   "PetscDeviceType",
57   "PETSC_DEVICE_",
58   PETSC_NULLPTR
59 };
60 
61 const char *const PetscDeviceInitTypes[] = {
62   "none",
63   "lazy",
64   "eager",
65   "PetscDeviceInitType",
66   "PETSC_DEVICE_INIT_",
67   PETSC_NULLPTR
68 };
69 
70 #ifdef __cplusplus
71 #include <petsc/private/cpp/type_traits.hpp>
72 
73 static_assert(Petsc::util::integral_value(PETSC_DEVICE_INIT_NONE) == 0, "");
74 static_assert(Petsc::util::integral_value(PETSC_DEVICE_INIT_LAZY) == 1, "");
75 static_assert(Petsc::util::integral_value(PETSC_DEVICE_INIT_EAGER) == 2, "");
76 
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
82 
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
91 
92 static PetscBool registered = PETSC_FALSE;
93 
94 static PetscErrorCode PetscDeviceRegisterEvent_Private(const char name[], PetscClassId id, PetscLogEvent *event) {
95   PetscFunctionBegin;
96   PetscCall(PetscLogEventRegister(name, id, event));
97   PetscCall(PetscLogEventSetCollective(*event, PETSC_FALSE));
98   PetscFunctionReturn(0);
99 }
100 
101 /*@C
102   PetscDeviceFinalizePackage - This function cleans up all components of the `PetscDevice`
103   package. It is called from `PetscFinalize()`.
104 
105   Developer Note:
106   This function is automatically registered to be called during `PetscFinalize()` by
107   `PetscDeviceInitializePackage()` so there should be no need to call it yourself.
108 
109   Level: developer
110 
111 .seealso: `PetscFinalize()`, `PetscDeviceInitializePackage()`
112 @*/
113 PetscErrorCode PetscDeviceFinalizePackage(void) {
114   PetscFunctionBegin;
115   registered = PETSC_FALSE;
116   PetscFunctionReturn(0);
117 }
118 
119 /*@C
120   PetscDeviceInitializePackage - This function initializes everything in the `PetscDevice`
121   package. It is called on the first call to `PetscDeviceContextCreate()` or
122   `PetscDeviceCreate()` when using shared or static libraries.
123 
124   Level: developer
125 
126 .seealso: `PetscInitialize()`, `PetscDeviceFinalizePackage()`, `PetscDeviceContextCreate()`,
127 `PetscDeviceCreate()`
128 @*/
129 PetscErrorCode PetscDeviceInitializePackage(void) {
130   PetscFunctionBegin;
131   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()]);
132   if (PetscLikely(registered)) PetscFunctionReturn(0);
133   registered = PETSC_TRUE;
134   PetscCall(PetscRegisterFinalize(PetscDeviceFinalizePackage));
135   // class registration
136   PetscCall(PetscClassIdRegister("PetscDevice", &PETSC_DEVICE_CLASSID));
137   PetscCall(PetscClassIdRegister("PetscDeviceContext", &PETSC_DEVICE_CONTEXT_CLASSID));
138   // events
139   if (PetscDefined(HAVE_CUDA)) {
140     PetscCall(PetscDeviceRegisterEvent_Private("cuBLAS Init", PETSC_DEVICE_CONTEXT_CLASSID, &CUBLAS_HANDLE_CREATE));
141     PetscCall(PetscDeviceRegisterEvent_Private("cuSolver Init", PETSC_DEVICE_CONTEXT_CLASSID, &CUSOLVER_HANDLE_CREATE));
142   }
143   if (PetscDefined(HAVE_HIP)) {
144     PetscCall(PetscDeviceRegisterEvent_Private("hipBLAS Init", PETSC_DEVICE_CONTEXT_CLASSID, &HIPBLAS_HANDLE_CREATE));
145     PetscCall(PetscDeviceRegisterEvent_Private("hipSolver Init", PETSC_DEVICE_CONTEXT_CLASSID, &HIPSOLVER_HANDLE_CREATE));
146   }
147   PetscCall(PetscDeviceRegisterEvent_Private("DCtxCreate", PETSC_DEVICE_CONTEXT_CLASSID, &DCONTEXT_Create));
148   PetscCall(PetscDeviceRegisterEvent_Private("DCtxDestroy", PETSC_DEVICE_CONTEXT_CLASSID, &DCONTEXT_Destroy));
149   PetscCall(PetscDeviceRegisterEvent_Private("DCtxChangeStream", PETSC_DEVICE_CONTEXT_CLASSID, &DCONTEXT_ChangeStream));
150   PetscCall(PetscDeviceRegisterEvent_Private("DCtxSetUp", PETSC_DEVICE_CONTEXT_CLASSID, &DCONTEXT_SetUp));
151   PetscCall(PetscDeviceRegisterEvent_Private("DCtxSetDevice", PETSC_DEVICE_CONTEXT_CLASSID, &DCONTEXT_SetDevice));
152   PetscCall(PetscDeviceRegisterEvent_Private("DCtxDuplicate", PETSC_DEVICE_CONTEXT_CLASSID, &DCONTEXT_Duplicate));
153   PetscCall(PetscDeviceRegisterEvent_Private("DCtxQueryIdle", PETSC_DEVICE_CONTEXT_CLASSID, &DCONTEXT_QueryIdle));
154   PetscCall(PetscDeviceRegisterEvent_Private("DCtxWaitForCtx", PETSC_DEVICE_CONTEXT_CLASSID, &DCONTEXT_WaitForCtx));
155   PetscCall(PetscDeviceRegisterEvent_Private("DCtxFork", PETSC_DEVICE_CONTEXT_CLASSID, &DCONTEXT_Fork));
156   PetscCall(PetscDeviceRegisterEvent_Private("DCtxJoin", PETSC_DEVICE_CONTEXT_CLASSID, &DCONTEXT_Join));
157   PetscCall(PetscDeviceRegisterEvent_Private("DCtxSync", PETSC_DEVICE_CONTEXT_CLASSID, &DCONTEXT_Sync));
158   PetscCall(PetscDeviceRegisterEvent_Private("DCtxMark", PETSC_DEVICE_CONTEXT_CLASSID, &DCONTEXT_Mark));
159   {
160     const PetscClassId classids[] = {PETSC_DEVICE_CONTEXT_CLASSID, PETSC_DEVICE_CLASSID};
161 
162     PetscCall(PetscInfoProcessClass("device", PETSC_STATIC_ARRAY_LENGTH(classids), classids));
163   }
164   PetscFunctionReturn(0);
165 }
166