@@ -28,6 +28,10 @@ int opal_accelerator_rocm_verbose = 0;
28
28
size_t opal_accelerator_rocm_memcpyD2H_limit = 1024 ;
29
29
size_t opal_accelerator_rocm_memcpyH2D_limit = 1048576 ;
30
30
31
+ /* Initialization lock for lazy rocm initialization */
32
+ static opal_mutex_t accelerator_rocm_init_lock ;
33
+ static bool accelerator_rocm_init_complete = false;
34
+
31
35
hipStream_t opal_accelerator_rocm_MemcpyStream = NULL ;
32
36
33
37
/*
@@ -154,8 +158,42 @@ static int accelerator_rocm_component_register(void)
154
158
return OPAL_SUCCESS ;
155
159
}
156
160
161
+ int opal_accelerator_rocm_lazy_init ()
162
+ {
163
+ int err = OPAL_SUCCESS ;
164
+
165
+ /* Double checked locking to avoid having to
166
+ * grab locks post lazy-initialization. */
167
+ opal_atomic_rmb ();
168
+ if (true == accelerator_rocm_init_complete ) {
169
+ return OPAL_SUCCESS ;
170
+ }
171
+ OPAL_THREAD_LOCK (& accelerator_rocm_init_lock );
172
+
173
+ /* If already initialized, just exit */
174
+ if (true == accelerator_rocm_init_complete ) {
175
+ goto out ;
176
+ }
177
+
178
+ err = hipStreamCreate (& opal_accelerator_rocm_MemcpyStream );
179
+ if (hipSuccess != err ) {
180
+ opal_output (0 , "Could not create hipStream, err=%d %s\n" ,
181
+ err , hipGetErrorString (err ));
182
+ goto out ;
183
+ }
184
+
185
+ err = OPAL_SUCCESS ;
186
+ opal_atomic_wmb ();
187
+ accelerator_rocm_init_complete = true;
188
+ out :
189
+ OPAL_THREAD_UNLOCK (& accelerator_rocm_init_lock );
190
+ return err ;
191
+ }
192
+
157
193
static opal_accelerator_base_module_t * accelerator_rocm_init (void )
158
194
{
195
+ OBJ_CONSTRUCT (& accelerator_rocm_init_lock , opal_mutex_t );
196
+
159
197
hipError_t err ;
160
198
161
199
if (opal_rocm_runtime_initialized ) {
@@ -169,13 +207,6 @@ static opal_accelerator_base_module_t* accelerator_rocm_init(void)
169
207
return NULL ;
170
208
}
171
209
172
- err = hipStreamCreate (& opal_accelerator_rocm_MemcpyStream );
173
- if (hipSuccess != err ) {
174
- opal_output (0 , "Could not create hipStream, err=%d %s\n" ,
175
- err , hipGetErrorString (err ));
176
- return NULL ;
177
- }
178
-
179
210
opal_atomic_mb ();
180
211
opal_rocm_runtime_initialized = true;
181
212
@@ -192,5 +223,6 @@ static void accelerator_rocm_finalize(opal_accelerator_base_module_t* module)
192
223
opal_accelerator_rocm_MemcpyStream = NULL ;
193
224
}
194
225
226
+ OBJ_DESTRUCT (& accelerator_rocm_init_lock );
195
227
return ;
196
228
}
0 commit comments