13 #ifdef ALPAKA_DISABLE_ATOMIC_ATOMICREF
24 template<
size_t THashTableSize>
25 class AtomicStdLibLock
28 template<
typename TAtomic,
typename TOp,
typename T,
typename THierarchy,
typename TSfinae>
29 friend struct trait::AtomicOp;
31 static constexpr
auto nextPowerOf2(
size_t const value,
size_t const bit = 0u) ->
size_t
33 return value <= (static_cast<size_t>(1u) << bit) ? (
static_cast<size_t>(1u) << bit)
34 : nextPowerOf2(value, bit + 1u);
41 template<
typename TPtr>
42 static auto hash(TPtr
const*
const ptr) ->
size_t
44 auto const ptrAddr =
reinterpret_cast<size_t>(ptr);
46 constexpr
size_t typeSizePowerOf2 = nextPowerOf2(
sizeof(TPtr));
48 return (ptrAddr / typeSizePowerOf2);
51 template<
typename TPtr>
52 auto getMutex(TPtr
const*
const ptr)
const -> std::mutex&
57 constexpr
size_t hashTableSize = THashTableSize == 0u ? 1u : nextPowerOf2(THashTableSize);
59 size_t const hashedAddr = hash(ptr) & (hashTableSize - 1u);
61 # pragma clang diagnostic push
62 # pragma clang diagnostic ignored "-Wexit-time-destructors"
69 # pragma clang diagnostic pop
71 return m_mtxAtomic[hashedAddr];
78 template<
typename TOp,
typename T,
typename THierarchy,
size_t THashTableSize>
79 struct AtomicOp<TOp, AtomicStdLibLock<THashTableSize>, T, THierarchy>
82 AtomicStdLibLock<THashTableSize>
const& atomic,
86 std::lock_guard<std::mutex> lock(atomic.getMutex(addr));
87 return TOp()(addr, value);
91 AtomicStdLibLock<THashTableSize>
const& atomic,
96 std::lock_guard<std::mutex> lock(atomic.getMutex(addr));
97 return TOp()(addr, compare, value);
The alpaka accelerator library.
ALPAKA_NO_HOST_ACC_WARNING ALPAKA_FN_HOST_ACC auto atomicOp(TAtomic const &atomic, T *const addr, T const &value, THierarchy const &=THierarchy()) -> T
Executes the given operation atomically.