vecmem 1.14.0
Loading...
Searching...
No Matches
sycl_custom_device_atomic_ref.ipp
1/*
2 * VecMem project, part of the ACTS project (R&D line)
3 *
4 * (c) 2022-2024 CERN for the benefit of the ACTS project
5 *
6 * Mozilla Public License Version 2.0
7 */
8#pragma once
9
10// SYCL include(s).
11#include <sycl/sycl.hpp>
12
13namespace vecmem {
14namespace sycl {
15namespace details {
16
17template <device_address_space address>
19
20template <>
22 static constexpr ::sycl::access::address_space add =
23 ::sycl::access::address_space::global_space;
24
25 template <typename T>
26 using ptr_t = ::sycl::global_ptr<T>;
27};
28
29template <>
31 static constexpr ::sycl::access::address_space add =
32 ::sycl::access::address_space::local_space;
33 template <typename T>
34 using ptr_t = ::sycl::local_ptr<T>;
35};
36
37} // namespace details
38
39#define __VECMEM_SYCL_ATOMIC_CALL0(FNAME, PTR) \
40 ::sycl::atomic_##FNAME<value_type, \
41 details::custom_address_space<address>::add>( \
42 ::sycl::atomic<value_type, \
43 details::custom_address_space<address>::add>( \
44 typename details::custom_address_space<address>::template ptr_t< \
45 value_type>(PTR)))
46#define __VECMEM_SYCL_ATOMIC_CALL1(FNAME, PTR, ARG1) \
47 ::sycl::atomic_##FNAME<value_type, \
48 details::custom_address_space<address>::add>( \
49 ::sycl::atomic<value_type, \
50 details::custom_address_space<address>::add>( \
51 typename details::custom_address_space<address>::template ptr_t< \
52 value_type>(PTR)), \
53 ARG1)
54#define __VECMEM_SYCL_ATOMIC_CALL2(FNAME, PTR, ARG1, ARG2) \
55 ::sycl::atomic_##FNAME<value_type, \
56 details::custom_address_space<address>::add>( \
57 ::sycl::atomic<value_type, \
58 details::custom_address_space<address>::add>( \
59 typename details::custom_address_space<address>::template ptr_t< \
60 value_type>(PTR)), \
61 ARG1, ARG2)
62
63template <typename T, device_address_space address>
66
67template <typename T, device_address_space address>
71
72template <typename T, device_address_space address>
74 -> value_type {
75
76 store(data);
77 return data;
78}
79
80template <typename T, device_address_space address>
82 memory_order) const {
83
84 __VECMEM_SYCL_ATOMIC_CALL1(store, m_ptr, data);
85}
86
87template <typename T, device_address_space address>
89 -> value_type {
90
91 return __VECMEM_SYCL_ATOMIC_CALL0(load, m_ptr);
92}
93
94template <typename T, device_address_space address>
96 memory_order) const
97 -> value_type {
98
99 return __VECMEM_SYCL_ATOMIC_CALL1(exchange, m_ptr, data);
100}
101
102template <typename T, device_address_space address>
105
106 if (order == memory_order::acq_rel) {
107 return compare_exchange_strong(expected, desired, order,
108 memory_order::acquire);
109 } else if (order == memory_order::release) {
110 return compare_exchange_strong(expected, desired, order,
111 memory_order::relaxed);
112 } else {
113 return compare_exchange_strong(expected, desired, order, order);
114 }
115}
116
117template <typename T, device_address_space address>
120 memory_order failure) const {
121
122 (void)failure;
123 assert(failure != memory_order::release &&
124 failure != memory_order::acq_rel);
125
126 return __VECMEM_SYCL_ATOMIC_CALL2(compare_exchange_strong, m_ptr, expected,
127 desired);
128}
129
130template <typename T, device_address_space address>
132 memory_order) const
133 -> value_type {
134
135 return __VECMEM_SYCL_ATOMIC_CALL1(fetch_add, m_ptr, data);
136}
137
138template <typename T, device_address_space address>
140 memory_order order) const
141 -> value_type {
142
143 return __VECMEM_SYCL_ATOMIC_CALL1(fetch_sub, m_ptr, data);
144}
145
146template <typename T, device_address_space address>
148 memory_order) const
149 -> value_type {
150
151 return __VECMEM_SYCL_ATOMIC_CALL1(fetch_and, m_ptr, data);
152}
153
154template <typename T, device_address_space address>
156 memory_order) const
157 -> value_type {
158
159 return __VECMEM_SYCL_ATOMIC_CALL1(fetch_or, m_ptr, data);
160}
161
162template <typename T, device_address_space address>
164 memory_order) const
165 -> value_type {
166
167 return __VECMEM_SYCL_ATOMIC_CALL1(fetch_xor, m_ptr, data);
168}
169
170#undef __VECMEM_SYCL_ATOMIC_CALL0
171#undef __VECMEM_SYCL_ATOMIC_CALL1
172#undef __VECMEM_SYCL_ATOMIC_CALL2
173
174} // namespace sycl
175} // namespace vecmem
Custom implementation for atomic operations in SYCL device code.
Definition sycl_custom_device_atomic_ref.hpp:27
void store(value_type data, memory_order order=memory_order::seq_cst) const
Set the variable to the desired value.
Definition sycl_custom_device_atomic_ref.ipp:81
value_type fetch_sub(value_type data, memory_order order=memory_order::seq_cst) const
Substitute a chosen value from the stored variable.
Definition sycl_custom_device_atomic_ref.ipp:139
T value_type
Type managed by the object.
Definition sycl_custom_device_atomic_ref.hpp:34
value_type exchange(value_type data, memory_order order=memory_order::seq_cst) const
Exchange the current value of the variable with a different one.
Definition sycl_custom_device_atomic_ref.ipp:95
custom_device_atomic_ref(reference ref)
Constructor, with a pointer to the managed variable.
Definition sycl_custom_device_atomic_ref.ipp:64
value_type fetch_or(value_type data, memory_order order=memory_order::seq_cst) const
Replace the current value with the specified value OR-d to it.
Definition sycl_custom_device_atomic_ref.ipp:155
custom_device_atomic_ref & operator=(const custom_device_atomic_ref &)=delete
Disable the assignment operator.
value_type fetch_xor(value_type data, memory_order order=memory_order::seq_cst) const
Replace the current value with the specified value XOR-d to it.
Definition sycl_custom_device_atomic_ref.ipp:163
value_type load(memory_order order=memory_order::seq_cst) const
Get the value of the variable.
Definition sycl_custom_device_atomic_ref.ipp:88
value_type & reference
Reference to a value given by the user.
Definition sycl_custom_device_atomic_ref.hpp:40
value_type fetch_add(value_type data, memory_order order=memory_order::seq_cst) const
Add a chosen value to the stored variable.
Definition sycl_custom_device_atomic_ref.ipp:131
value_type fetch_and(value_type data, memory_order order=memory_order::seq_cst) const
Replace the current value with the specified value AND-ed to it.
Definition sycl_custom_device_atomic_ref.ipp:147
bool compare_exchange_strong(reference expected, value_type desired, memory_order success, memory_order failure) const
Compare against the current value, and exchange only if different.
Definition sycl_custom_device_atomic_ref.ipp:118
Main namespace for the vecmem classes/functions.
Definition atomic_ref.hpp:16
device_address_space
Custom definition for the device memory adress space.
Definition device_address_space.hpp:12
std::vector< T, vecmem::polymorphic_allocator< T > > vector
Alias type for vectors with our polymorphic allocator.
Definition vector.hpp:35
memory_order
Custom (dummy) definition for the memory order.
Definition memory_order.hpp:31
Definition sycl_custom_device_atomic_ref.ipp:18