Skip to content

Commit

Permalink
omp_locks: Avoid extern global variable (#3798)
Browse files Browse the repository at this point in the history
Make `omp_locks` a variable in an unnamed namespace instead of an extern
global variable.

This might potentially fix the Windows symbol issue (#3795).
  • Loading branch information
WeiqunZhang committed Mar 14, 2024
1 parent ae3af43 commit 431ad98
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
4 changes: 1 addition & 3 deletions Src/Base/AMReX_BaseFab.H
Expand Up @@ -3360,9 +3360,7 @@ BaseFab<T>::lockAdd (const BaseFab<T>& src, const Box& srcbox, const Box& destbo
while (planes_left > 0) {
AMREX_ASSERT(mm < nplanes);
auto const m = mm + plo;
int ilock = m % OpenMP::nlocks;
if (ilock < 0) { ilock += OpenMP::nlocks; }
auto* lock = &(OpenMP::omp_locks[ilock]);
auto* lock = OpenMP::get_lock(m);
if (omp_test_lock(lock))
{
auto lo = dlo;
Expand Down
3 changes: 1 addition & 2 deletions Src/Base/AMReX_OpenMP.H
Expand Up @@ -17,8 +17,7 @@ namespace amrex::OpenMP {
void Initialize ();
void Finalize ();

static constexpr int nlocks = 128;
extern AMREX_EXPORT omp_lock_t omp_locks[nlocks];
omp_lock_t* get_lock (int ilock);
}

#else // AMREX_USE_OMP
Expand Down
11 changes: 9 additions & 2 deletions Src/Base/AMReX_OpenMP.cpp
Expand Up @@ -135,9 +135,9 @@ namespace amrex
#ifdef AMREX_USE_OMP
namespace amrex::OpenMP
{
omp_lock_t omp_locks[nlocks];

namespace {
constexpr int nlocks = 128;
omp_lock_t omp_locks[nlocks];
unsigned int initialized = 0;
}

Expand Down Expand Up @@ -204,5 +204,12 @@ namespace amrex::OpenMP
}
}

omp_lock_t* get_lock (int ilock)
{
ilock = ilock % nlocks;
if (ilock < 0) { ilock += nlocks; }
return omp_locks + ilock;
}

} // namespace amrex::OpenMP
#endif // AMREX_USE_OMP

0 comments on commit 431ad98

Please sign in to comment.