kokkos-execution 0.0.1
Loading...
Searching...
No Matches
atomic.hpp
Go to the documentation of this file.
1#ifndef KOKKOS_EXECUTION_TESTS_UTILS_ATOMIC_HPP
2#define KOKKOS_EXECUTION_TESTS_UTILS_ATOMIC_HPP
3
4#include "Kokkos_Macros.hpp"
5#ifdef KOKKOS_ENABLE_EXPERIMENTAL_CXX20_MODULES
6import kokkos.core;
7#else
8# include "Kokkos_Core.hpp"
9#endif
10
11#if defined(KOKKOS_ENABLE_CUDA)
12# include <cuda/atomic>
13#elif defined(KOKKOS_ENABLE_HIP)
14# include "desul/atomics/Adapt_HIP.hpp"
15#elif defined(KOKKOS_ENABLE_SYCL)
16# include "desul/atomics/Adapt_SYCL.hpp"
17#endif
18
38
39#if defined(KOKKOS_ENABLE_CUDA)
40namespace desul::Impl {
41template <class MemoryScope>
42struct CudaMemoryScope;
43
44template <>
45struct CudaMemoryScope<MemoryScopeDevice> {
46 static constexpr auto value = cuda::thread_scope_device;
47};
48
49template <>
50struct CudaMemoryScope<MemoryScopeSystem> {
51 static constexpr auto value = cuda::thread_scope_system;
52};
53
54template <class MemoryOrder>
55struct CudaMemoryOrder;
56
57template <>
58struct CudaMemoryOrder<MemoryOrderRelaxed> {
59 static constexpr auto value = cuda::memory_order_relaxed;
60};
61} // namespace desul::Impl
62#endif
63
64namespace Tests::Utils {
65
67template <typename Scope, typename Order, typename T>
68KOKKOS_FUNCTION auto atomic_fetch_add(T* ptr, const T val) {
69#if defined(KOKKOS_ENABLE_CUDA)
70 return cuda::atomic_ref<T, desul::Impl::CudaMemoryScope<Scope>::value>(*ptr)
71 .fetch_add(val, desul::Impl::CudaMemoryOrder<Order>::value);
72#elif defined(KOKKOS_ENABLE_HIP)
73 return __hip_atomic_fetch_add(
74 ptr, val, desul::Impl::HIPMemoryOrder<Order>::value, desul::Impl::HIPMemoryScope<Scope>::value);
75#elif defined(KOKKOS_ENABLE_SYCL)
76 return desul::Impl::sycl_atomic_ref<T, Order, Scope>{*ptr}.fetch_add(val);
77#else
78 static_assert(std::same_as<Order, desul::MemoryOrderRelaxed>);
79 return std::atomic_ref<T>(*ptr).fetch_add(val, std::memory_order_relaxed);
80#endif
81}
82
83} // namespace Tests::Utils
84
85#endif // KOKKOS_EXECUTION_TESTS_UTILS_ATOMIC_HPP
auto atomic_fetch_add(T *ptr, const T val)
Same effect as Kokkos::atomic_fetch_add, given memory scope and order.
Definition atomic.hpp:68