Skip to content

Commit

Permalink
Workaround for Windows omp_lock_t issue (#3910)
Browse files Browse the repository at this point in the history
  • Loading branch information
WeiqunZhang committed May 21, 2024
1 parent 9cc3de8 commit 8c5d761
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
8 changes: 8 additions & 0 deletions Src/Base/AMReX_OpenMP.H
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,15 @@ namespace amrex::OpenMP {
void Initialize ();
void Finalize ();

#if defined(_WIN32)
void** get_lock_impl (int ilock);

inline omp_lock_t* get_lock (int ilock) {
return (omp_lock_t*)(*(get_lock_impl(ilock)));
}
#else
omp_lock_t* get_lock (int ilock);
#endif
}

#else // AMREX_USE_OMP
Expand Down
24 changes: 24 additions & 0 deletions Src/Base/AMReX_OpenMP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,11 @@ namespace amrex::OpenMP
{
namespace {
constexpr int nlocks = 128;
#if defined(_WIN32)
void* omp_locks[nlocks];
#else
omp_lock_t omp_locks[nlocks];
#endif
unsigned int initialized = 0;
}

Expand Down Expand Up @@ -183,9 +187,17 @@ namespace amrex::OpenMP
}
}

#if defined(_WIN32)
for (auto& vp : omp_locks) {
auto* p = new omp_lock_t;
omp_init_lock(p);
vp = (void*) p;
}
#else
for (auto& lck : omp_locks) {
omp_init_lock(&lck);
}
#endif

++initialized;
}
Expand All @@ -195,14 +207,26 @@ namespace amrex::OpenMP
if (initialized) {
--initialized;
if (initialized == 0) {
#if defined(_WIN32)
for (auto vp : omp_locks) {
auto* p = (omp_lock_t*)vp;
omp_destroy_lock(p);
delete p;
}
#else
for (auto& lck : omp_locks) {
omp_destroy_lock(&lck);
}
#endif
}
}
}

#if defined(_WIN32)
void** get_lock_impl(int ilock)
#else
omp_lock_t* get_lock (int ilock)
#endif
{
ilock = ilock % nlocks;
if (ilock < 0) { ilock += nlocks; }
Expand Down

0 comments on commit 8c5d761

Please sign in to comment.