Skip to content

Commit 5102120

Browse files
authored
RNG on GPU: Assertion it's not in OMP parallel region (#4799)
RNG on GPU is not OpenMP thread safe. Thank @AlexanderSinn for pointing this out.
1 parent 89409f8 commit 5102120

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

Src/Base/AMReX_RandomEngine.H

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
#include <AMReX_GpuQualifiers.H>
66
#include <AMReX_Extension.H>
7+
#ifdef AMREX_USE_OMP
8+
# include <AMReX_BLassert.H>
9+
# include <omp.h>
10+
#endif
711

812
#if defined(AMREX_USE_HIP)
913
#pragma clang diagnostic push
@@ -32,8 +36,13 @@ namespace amrex
3236

3337
extern sycl_rng_descr* rand_engine_descr;
3438

35-
AMREX_FORCE_INLINE
36-
sycl_rng_descr* getRandEngineDescriptor () { return rand_engine_descr; }
39+
inline
40+
sycl_rng_descr* getRandEngineDescriptor () {
41+
#ifdef AMREX_USE_OMP
42+
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(! omp_in_parallel(), "RNG on GPU is not OpenMP thread safe");
43+
#endif
44+
return rand_engine_descr;
45+
}
3746

3847
struct RandomEngine {
3948
sycl_rng_engine* engine;
@@ -52,8 +61,13 @@ namespace amrex
5261

5362
extern randState_t* gpu_rand_state;
5463

55-
AMREX_FORCE_INLINE
56-
randState_t* getRandState () { return gpu_rand_state; }
64+
inline
65+
randState_t* getRandState () {
66+
#ifdef AMREX_USE_OMP
67+
AMREX_ALWAYS_ASSERT_WITH_MESSAGE(! omp_in_parallel(), "RNG on GPU is not OpenMP thread safe");
68+
#endif
69+
return gpu_rand_state;
70+
}
5771

5872
struct RandomEngine {
5973
randState_t* rand_state;

0 commit comments

Comments
 (0)