Skip to content

Commit

Permalink
Fix OpenMP 5.1 atomics
Browse files Browse the repository at this point in the history
* Workaround clang not recognizing ternay expression
* Implement atomicInc/atomicDec via omp critical

Fixes: #2170
  • Loading branch information
bernhardmgruber committed Sep 27, 2023
1 parent 1ee0426 commit 1846aa1
Showing 1 changed file with 14 additions and 30 deletions.
44 changes: 14 additions & 30 deletions include/alpaka/atomic/AtomicOmpBuiltIn.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ namespace alpaka
# pragma omp atomic capture compare
{
old = ref;
ref = (ref <= value) ? ref : value;
if(value < ref)
ref = value;
}
return old;
}
Expand All @@ -205,7 +206,8 @@ namespace alpaka
# pragma omp atomic capture compare
{
old = ref;
ref = (ref >= value) ? ref : value;
if(value > ref)
ref = value;
}
return old;
}
Expand All @@ -217,21 +219,12 @@ namespace alpaka
{
ALPAKA_FN_HOST static auto atomicOp(AtomicOmpBuiltIn const&, T* const addr, T const& value) -> T
{
// TODO(bgruber): atomic increment with wrap around is not implementable in OpenMP 5.1
T old;
auto& ref(*addr);
// atomically update ref, but capture the original value in old
# if BOOST_COMP_GNUC
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wconversion"
# endif
# pragma omp atomic capture compare
# pragma omp critical(AlpakaOmpAtomicOp)
{
old = ref;
ref = ((ref >= value) ? 0 : (ref + 1));
old = AtomicInc{}(addr, value);
}
# if BOOST_COMP_GNUC
# pragma GCC diagnostic pop
# endif
return old;
}
};
Expand All @@ -242,21 +235,12 @@ namespace alpaka
{
ALPAKA_FN_HOST static auto atomicOp(AtomicOmpBuiltIn const&, T* const addr, T const& value) -> T
{
// TODO(bgruber): atomic decrement with wrap around is not implementable in OpenMP 5.1
T old;
auto& ref(*addr);
// atomically update ref, but capture the original value in old
# if BOOST_COMP_GNUC
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wconversion"
# endif
# pragma omp atomic capture compare
# pragma omp critical(AlpakaOmpAtomicOp)
{
old = ref;
ref = ((ref == 0) || (ref > value)) ? value : (ref - 1);
old = AtomicDec{}(addr, value);
}
# if BOOST_COMP_GNUC
# pragma GCC diagnostic pop
# endif
return old;
}
};
Expand Down Expand Up @@ -293,8 +277,8 @@ namespace alpaka
ALPAKA_FN_HOST static auto atomicOp(AtomicOmpBuiltIn const&, T* const addr, T const& value) -> T
{
T old;
// \TODO: Currently not only the access to the same memory location is protected by a mutex but all atomic ops on all
// threads.
// \TODO: Currently not only the access to the same memory location is protected by a mutex but all
// atomic ops on all threads.
# pragma omp critical(AlpakaOmpAtomicOp)
{
old = TOp()(addr, value);
Expand All @@ -309,8 +293,8 @@ namespace alpaka
T const& value) -> T
{
T old;
// \TODO: Currently not only the access to the same memory location is protected by a mutex but all atomic ops on all
// threads.
// \TODO: Currently not only the access to the same memory location is protected by a mutex but all
// atomic ops on all threads.
# pragma omp critical(AlpakaOmpAtomicOp2)
{
old = TOp()(addr, compare, value);
Expand Down

0 comments on commit 1846aa1

Please sign in to comment.