okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge branch 'f-139b-arc' into f-139-relad33b8074

main
40.0 KiB868 linesraw
1//==---- atomic.hpp -------------------------------*- C++ -*----------------==//
2//
3// Copyright (C) Intel Corporation
4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5// See https://llvm.org/LICENSE.txt for license information.
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef __DPCT_ATOMIC_HPP__
10#define __DPCT_ATOMIC_HPP__
11
12#include <sycl/sycl.hpp>
13
14namespace dpct {
15
16/// Atomically add the value operand to the value at the addr and assign the
17/// result to the value at addr.
18/// \param [in, out] addr The pointer to the data.
19/// \param operand The value to add to the value at \p addr.
20/// \param memoryOrder The memory ordering used.
21/// \returns The value at the \p addr before the call.
22template <typename T,
23          sycl::access::address_space addressSpace =
24              sycl::access::address_space::global_space,
25          sycl::memory_order memoryOrder = sycl::memory_order::relaxed,
26          sycl::memory_scope memoryScope = sycl::memory_scope::device>
27inline T atomic_fetch_add(T *addr, T operand) {
28  auto atm =
29      sycl::atomic_ref<T, memoryOrder, memoryScope, addressSpace>(addr[0]);
30  return atm.fetch_add(operand);
31}
32
33template <sycl::access::address_space addressSpace =
34              sycl::access::address_space::global_space,
35          sycl::memory_order memoryOrder = sycl::memory_order::relaxed,
36          sycl::memory_scope memoryScope = sycl::memory_scope::device,
37          typename T1, typename T2>
38inline T1 atomic_fetch_add(T1 *addr, T2 operand) {
39  auto atm =
40      sycl::atomic_ref<T1, memoryOrder, memoryScope, addressSpace>(addr[0]);
41  return atm.fetch_add(operand);
42}
43
44template <sycl::access::address_space addressSpace =
45              sycl::access::address_space::global_space,
46          sycl::memory_order memoryOrder = sycl::memory_order::relaxed,
47          sycl::memory_scope memoryScope = sycl::memory_scope::device>
48inline sycl::half2 atomic_fetch_add(sycl::half2 *addr, sycl::half2 operand) {
49  auto atm = sycl::atomic_ref<unsigned, memoryOrder, memoryScope, addressSpace>(
50      *reinterpret_cast<unsigned *>(addr));
51
52  union {
53    unsigned i;
54    sycl::half2 h;
55  } old{0}, output{0};
56
57  while (true) {
58    old.i = atm.load();
59    output.h = old.h + operand;
60    if (atm.compare_exchange_strong(old.i, output.i))
61      break;
62  }
63
64  return output.h;
65}
66
67/// Atomically add the value operand to the value at the addr and assign the
68/// result to the value at addr.
69/// \param [in, out] addr The pointer to the data.
70/// \param operand The value to add to the value at \p addr.
71/// \param memoryOrder The memory ordering used.
72/// \returns The value at the \p addr before the call.
73template <typename T, sycl::access::address_space addressSpace =
74                          sycl::access::address_space::global_space>
75inline T atomic_fetch_add(T *addr, T operand,
76                          sycl::memory_order memoryOrder) {
77  switch (memoryOrder) {
78  case sycl::memory_order::relaxed:
79    return atomic_fetch_add<T, addressSpace, sycl::memory_order::relaxed,
80                            sycl::memory_scope::device>(addr, operand);
81  case sycl::memory_order::acq_rel:
82    return atomic_fetch_add<T, addressSpace, sycl::memory_order::acq_rel,
83                            sycl::memory_scope::device>(addr, operand);
84  case sycl::memory_order::seq_cst:
85    return atomic_fetch_add<T, addressSpace, sycl::memory_order::seq_cst,
86                            sycl::memory_scope::device>(addr, operand);
87  default:
88    assert(false && "Invalid memory_order for atomics. Valid memory_order for "
89                    "atomics are: sycl::memory_order::relaxed, "
90                    "sycl::memory_order::acq_rel, sycl::memory_order::seq_cst!");
91  }
92}
93
94template <sycl::access::address_space addressSpace =
95              sycl::access::address_space::global_space,
96          typename T1, typename T2>
97inline T1 atomic_fetch_add(T1 *addr, T2 operand,
98                           sycl::memory_order memoryOrder) {
99  atomic_fetch_add<T1, addressSpace>(addr, operand, memoryOrder);
100}
101
102/// Atomically subtract the value operand from the value at the addr and assign
103/// the result to the value at addr.
104/// \param [in, out] addr The pointer to the data.
105/// \param operand The value to subtract from the value at \p addr
106/// \param memoryOrder The memory ordering used.
107/// \returns The value at the \p addr before the call.
108template <typename T,
109          sycl::access::address_space addressSpace =
110              sycl::access::address_space::global_space,
111          sycl::memory_order memoryOrder = sycl::memory_order::relaxed,
112          sycl::memory_scope memoryScope = sycl::memory_scope::device>
113inline T atomic_fetch_sub(T *addr, T operand) {
114  auto atm =
115      sycl::atomic_ref<T, memoryOrder, memoryScope, addressSpace>(addr[0]);
116  return atm.fetch_sub(operand);
117}
118
119template <sycl::access::address_space addressSpace =
120              sycl::access::address_space::global_space,
121          sycl::memory_order memoryOrder = sycl::memory_order::relaxed,
122          sycl::memory_scope memoryScope = sycl::memory_scope::device,
123          typename T1, typename T2>
124inline T1 atomic_fetch_sub(T1 *addr, T2 operand) {
125  auto atm =
126      sycl::atomic_ref<T1, memoryOrder, memoryScope, addressSpace>(addr[0]);
127  return atm.fetch_sub(operand);
128}
129
130/// Atomically subtract the value operand from the value at the addr and assign
131/// the result to the value at addr.
132/// \param [in, out] addr The pointer to the data.
133/// \param operand The value to subtract from the value at \p addr
134/// \param memoryOrder The memory ordering used.
135/// \returns The value at the \p addr before the call.
136template <typename T, sycl::access::address_space addressSpace =
137                          sycl::access::address_space::global_space>
138inline T atomic_fetch_sub(T *addr, T operand,
139                          sycl::memory_order memoryOrder) {
140  switch (memoryOrder) {
141  case sycl::memory_order::relaxed:
142    return atomic_fetch_sub<T, addressSpace, sycl::memory_order::relaxed,
143                            sycl::memory_scope::device>(addr, operand);
144  case sycl::memory_order::acq_rel:
145    return atomic_fetch_sub<T, addressSpace, sycl::memory_order::acq_rel,
146                            sycl::memory_scope::device>(addr, operand);
147  case sycl::memory_order::seq_cst:
148    return atomic_fetch_sub<T, addressSpace, sycl::memory_order::seq_cst,
149                            sycl::memory_scope::device>(addr, operand);
150  default:
151    assert(false && "Invalid memory_order for atomics. Valid memory_order for "
152                    "atomics are: sycl::memory_order::relaxed, "
153                    "sycl::memory_order::acq_rel, sycl::memory_order::seq_cst!");
154  }
155}
156
157template <sycl::access::address_space addressSpace =
158              sycl::access::address_space::global_space,
159          typename T1, typename T2>
160inline T1 atomic_fetch_sub(T1 *addr, T2 operand,
161                           sycl::memory_order memoryOrder) {
162  atomic_fetch_sub<T1, addressSpace>(addr, operand, memoryOrder);
163}
164
165/// Atomically perform a bitwise AND between the value operand and the value at the addr
166/// and assign the result to the value at addr.
167/// \param [in, out] addr The pointer to the data.
168/// \param operand The value to use in bitwise AND operation with the value at the \p addr.
169/// \param memoryOrder The memory ordering used.
170/// \returns The value at the \p addr before the call.
171template <typename T,
172          sycl::access::address_space addressSpace =
173              sycl::access::address_space::global_space,
174          sycl::memory_order memoryOrder = sycl::memory_order::relaxed,
175          sycl::memory_scope memoryScope = sycl::memory_scope::device>
176inline T atomic_fetch_and(T *addr, T operand) {
177  auto atm =
178      sycl::atomic_ref<T, memoryOrder, memoryScope, addressSpace>(addr[0]);
179  return atm.fetch_and(operand);
180}
181
182template <sycl::access::address_space addressSpace =
183              sycl::access::address_space::global_space,
184          sycl::memory_order memoryOrder = sycl::memory_order::relaxed,
185          sycl::memory_scope memoryScope = sycl::memory_scope::device,
186          typename T1, typename T2>
187inline T1 atomic_fetch_and(T1 *addr, T2 operand) {
188  auto atm =
189      sycl::atomic_ref<T1, memoryOrder, memoryScope, addressSpace>(addr[0]);
190  return atm.fetch_and(operand);
191}
192
193/// Atomically perform a bitwise AND between the value operand and the value at the addr
194/// and assign the result to the value at addr.
195/// \param [in, out] addr The pointer to the data.
196/// \param operand The value to use in bitwise AND operation with the value at the \p addr.
197/// \param memoryOrder The memory ordering used.
198/// \returns The value at the \p addr before the call.
199template <typename T, sycl::access::address_space addressSpace =
200                          sycl::access::address_space::global_space>
201inline T atomic_fetch_and(T *addr, T operand,
202                          sycl::memory_order memoryOrder) {
203  switch (memoryOrder) {
204  case sycl::memory_order::relaxed:
205    return atomic_fetch_and<T, addressSpace, sycl::memory_order::relaxed,
206                            sycl::memory_scope::device>(addr, operand);
207  case sycl::memory_order::acq_rel:
208    return atomic_fetch_and<T, addressSpace, sycl::memory_order::acq_rel,
209                            sycl::memory_scope::device>(addr, operand);
210  case sycl::memory_order::seq_cst:
211    return atomic_fetch_and<T, addressSpace, sycl::memory_order::seq_cst,
212                            sycl::memory_scope::device>(addr, operand);
213  default:
214    assert(false && "Invalid memory_order for atomics. Valid memory_order for "
215                    "atomics are: sycl::memory_order::relaxed, "
216                    "sycl::memory_order::acq_rel, sycl::memory_order::seq_cst!");
217  }
218}
219
220template <sycl::access::address_space addressSpace =
221              sycl::access::address_space::global_space,
222          typename T1, typename T2>
223inline T1 atomic_fetch_and(T1 *addr, T2 operand,
224                           sycl::memory_order memoryOrder) {
225  atomic_fetch_and<T1, addressSpace>(addr, operand, memoryOrder);
226}
227
228/// Atomically or the value at the addr with the value operand, and assign
229/// the result to the value at addr.
230/// \param [in, out] addr The pointer to the data.
231/// \param operand The value to use in bitwise OR operation with the value at the \p addr.
232/// \param memoryOrder The memory ordering used.
233/// \returns The value at the \p addr before the call.
234template <typename T,
235          sycl::access::address_space addressSpace =
236              sycl::access::address_space::global_space,
237          sycl::memory_order memoryOrder = sycl::memory_order::relaxed,
238          sycl::memory_scope memoryScope = sycl::memory_scope::device>
239inline T atomic_fetch_or(T *addr, T operand) {
240  auto atm =
241      sycl::atomic_ref<T, memoryOrder, memoryScope, addressSpace>(addr[0]);
242  return atm.fetch_or(operand);
243}
244
245template <sycl::access::address_space addressSpace =
246              sycl::access::address_space::global_space,
247          sycl::memory_order memoryOrder = sycl::memory_order::relaxed,
248          sycl::memory_scope memoryScope = sycl::memory_scope::device,
249          typename T1, typename T2>
250inline T1 atomic_fetch_or(T1 *addr, T2 operand) {
251  auto atm =
252      sycl::atomic_ref<T1, memoryOrder, memoryScope, addressSpace>(addr[0]);
253  return atm.fetch_or(operand);
254}
255
256/// Atomically or the value at the addr with the value operand, and assign
257/// the result to the value at addr.
258/// \param [in, out] addr The pointer to the data.
259/// \param operand The value to use in bitwise OR operation with the value at the \p addr.
260/// \param memoryOrder The memory ordering used.
261/// \returns The value at the \p addr before the call.
262template <typename T, sycl::access::address_space addressSpace =
263                          sycl::access::address_space::global_space>
264inline T atomic_fetch_or(T *addr, T operand,
265                         sycl::memory_order memoryOrder) {
266  switch (memoryOrder) {
267  case sycl::memory_order::relaxed:
268    return atomic_fetch_or<T, addressSpace, sycl::memory_order::relaxed,
269                           sycl::memory_scope::device>(addr, operand);
270  case sycl::memory_order::acq_rel:
271    return atomic_fetch_or<T, addressSpace, sycl::memory_order::acq_rel,
272                           sycl::memory_scope::device>(addr, operand);
273  case sycl::memory_order::seq_cst:
274    return atomic_fetch_or<T, addressSpace, sycl::memory_order::seq_cst,
275                           sycl::memory_scope::device>(addr, operand);
276  default:
277    assert(false && "Invalid memory_order for atomics. Valid memory_order for "
278                    "atomics are: sycl::memory_order::relaxed, "
279                    "sycl::memory_order::acq_rel, sycl::memory_order::seq_cst!");
280  }
281}
282
283template <sycl::access::address_space addressSpace =
284              sycl::access::address_space::global_space,
285          typename T1, typename T2>
286inline T1 atomic_fetch_or(T1 *addr, T2 operand,
287                           sycl::memory_order memoryOrder) {
288  atomic_fetch_or<T1, addressSpace>(addr, operand, memoryOrder);
289}
290
291/// Atomically xor the value at the addr with the value operand, and assign
292/// the result to the value at addr.
293/// \param [in, out] addr The pointer to the data.
294/// \param operand The value to use in bitwise XOR operation with the value at the \p addr.
295/// \param memoryOrder The memory ordering used.
296/// \returns The value at the \p addr before the call.
297template <typename T,
298          sycl::access::address_space addressSpace =
299              sycl::access::address_space::global_space,
300          sycl::memory_order memoryOrder = sycl::memory_order::relaxed,
301          sycl::memory_scope memoryScope = sycl::memory_scope::device>
302inline T atomic_fetch_xor(T *addr, T operand) {
303  auto atm =
304      sycl::atomic_ref<T, memoryOrder, memoryScope, addressSpace>(addr[0]);
305  return atm.fetch_xor(operand);
306}
307
308template <sycl::access::address_space addressSpace =
309              sycl::access::address_space::global_space,
310          sycl::memory_order memoryOrder = sycl::memory_order::relaxed,
311          sycl::memory_scope memoryScope = sycl::memory_scope::device,
312          typename T1, typename T2>
313inline T1 atomic_fetch_xor(T1 *addr, T2 operand) {
314  auto atm =
315      sycl::atomic_ref<T1, memoryOrder, memoryScope, addressSpace>(addr[0]);
316  return atm.fetch_xor(operand);
317}
318
319/// Atomically xor the value at the addr with the value operand, and assign
320/// the result to the value at addr.
321/// \param [in, out] addr The pointer to the data.
322/// \param operand The value to use in bitwise XOR operation with the value at the \p addr.
323/// \param memoryOrder The memory ordering used.
324/// \returns The value at the \p addr before the call.
325template <typename T, sycl::access::address_space addressSpace =
326                          sycl::access::address_space::global_space>
327inline T atomic_fetch_xor(T *addr, T operand,
328                          sycl::memory_order memoryOrder) {
329  switch (memoryOrder) {
330  case sycl::memory_order::relaxed:
331    return atomic_fetch_xor<T, addressSpace, sycl::memory_order::relaxed,
332                            sycl::memory_scope::device>(addr, operand);
333  case sycl::memory_order::acq_rel:
334    return atomic_fetch_xor<T, addressSpace, sycl::memory_order::acq_rel,
335                            sycl::memory_scope::device>(addr, operand);
336  case sycl::memory_order::seq_cst:
337    return atomic_fetch_xor<T, addressSpace, sycl::memory_order::seq_cst,
338                            sycl::memory_scope::device>(addr, operand);
339  default:
340    assert(false && "Invalid memory_order for atomics. Valid memory_order for "
341                    "atomics are: sycl::memory_order::relaxed, "
342                    "sycl::memory_order::acq_rel, sycl::memory_order::seq_cst!");
343  }
344}
345
346template <sycl::access::address_space addressSpace =
347              sycl::access::address_space::global_space,
348          typename T1, typename T2>
349inline T1 atomic_fetch_xor(T1 *addr, T2 operand,
350                           sycl::memory_order memoryOrder) {
351  atomic_fetch_xor<T1, addressSpace>(addr, operand, memoryOrder);
352}
353
354/// Atomically calculate the minimum of the value at addr and the value operand
355/// and assign the result to the value at addr.
356/// \param [in, out] addr The pointer to the data.
357/// \param operand.
358/// \param memoryOrder The memory ordering used.
359/// \returns The value at the \p addr before the call.
360template <typename T,
361          sycl::access::address_space addressSpace =
362              sycl::access::address_space::global_space,
363          sycl::memory_order memoryOrder = sycl::memory_order::relaxed,
364          sycl::memory_scope memoryScope = sycl::memory_scope::device>
365inline T atomic_fetch_min(T *addr, T operand) {
366  auto atm =
367      sycl::atomic_ref<T, memoryOrder, memoryScope, addressSpace>(addr[0]);
368  return atm.fetch_min(operand);
369}
370
371template <sycl::access::address_space addressSpace =
372              sycl::access::address_space::global_space,
373          sycl::memory_order memoryOrder = sycl::memory_order::relaxed,
374          sycl::memory_scope memoryScope = sycl::memory_scope::device,
375          typename T1, typename T2>
376inline T1 atomic_fetch_min(T1 *addr, T2 operand) {
377  auto atm =
378      sycl::atomic_ref<T1, memoryOrder, memoryScope, addressSpace>(addr[0]);
379  return atm.fetch_min(operand);
380}
381
382/// Atomically calculate the minimum of the value at addr and the value operand
383/// and assign the result to the value at addr.
384/// \param [in, out] addr The pointer to the data.
385/// \param operand.
386/// \param memoryOrder The memory ordering used.
387/// \returns The value at the \p addr before the call.
388template <typename T, sycl::access::address_space addressSpace =
389                          sycl::access::address_space::global_space>
390inline T atomic_fetch_min(T *addr, T operand,
391                          sycl::memory_order memoryOrder) {
392  switch (memoryOrder) {
393  case sycl::memory_order::relaxed:
394    return atomic_fetch_min<T, addressSpace, sycl::memory_order::relaxed,
395                            sycl::memory_scope::device>(addr, operand);
396  case sycl::memory_order::acq_rel:
397    return atomic_fetch_min<T, addressSpace, sycl::memory_order::acq_rel,
398                            sycl::memory_scope::device>(addr, operand);
399  case sycl::memory_order::seq_cst:
400    return atomic_fetch_min<T, addressSpace, sycl::memory_order::seq_cst,
401                            sycl::memory_scope::device>(addr, operand);
402  default:
403    assert(false && "Invalid memory_order for atomics. Valid memory_order for "
404                    "atomics are: sycl::memory_order::relaxed, "
405                    "sycl::memory_order::acq_rel, sycl::memory_order::seq_cst!");
406  }
407}
408
409template <sycl::access::address_space addressSpace =
410              sycl::access::address_space::global_space,
411          typename T1, typename T2>
412inline T1 atomic_fetch_min(T1 *addr, T2 operand,
413                           sycl::memory_order memoryOrder) {
414  atomic_fetch_min<T1, addressSpace>(addr, operand, memoryOrder);
415}
416
417/// Atomically calculate the maximum of the value at addr and the value operand
418/// and assign the result to the value at addr.
419/// \param [in, out] addr The pointer to the data.
420/// \param operand.
421/// \param memoryOrder The memory ordering used.
422/// \returns The value at the \p addr before the call.
423template <typename T,
424          sycl::access::address_space addressSpace =
425              sycl::access::address_space::global_space,
426          sycl::memory_order memoryOrder = sycl::memory_order::relaxed,
427          sycl::memory_scope memoryScope = sycl::memory_scope::device>
428inline T atomic_fetch_max(T *addr, T operand) {
429  auto atm =
430      sycl::atomic_ref<T, memoryOrder, memoryScope, addressSpace>(addr[0]);
431  return atm.fetch_max(operand);
432}
433
434template <sycl::access::address_space addressSpace =
435              sycl::access::address_space::global_space,
436          sycl::memory_order memoryOrder = sycl::memory_order::relaxed,
437          sycl::memory_scope memoryScope = sycl::memory_scope::device,
438          typename T1, typename T2>
439inline T1 atomic_fetch_max(T1 *addr, T2 operand) {
440  auto atm =
441      sycl::atomic_ref<T1, memoryOrder, memoryScope, addressSpace>(addr[0]);
442  return atm.fetch_max(operand);
443}
444
445/// Atomically calculate the maximum of the value at addr and the value operand
446/// and assign the result to the value at addr.
447/// \param [in, out] addr The pointer to the data.
448/// \param operand.
449/// \param memoryOrder The memory ordering used.
450/// \returns The value at the \p addr before the call.
451template <typename T, sycl::access::address_space addressSpace =
452                          sycl::access::address_space::global_space>
453inline T atomic_fetch_max(T *addr, T operand,
454                          sycl::memory_order memoryOrder) {
455  switch (memoryOrder) {
456  case sycl::memory_order::relaxed:
457    return atomic_fetch_max<T, addressSpace, sycl::memory_order::relaxed,
458                            sycl::memory_scope::device>(addr, operand);
459  case sycl::memory_order::acq_rel:
460    return atomic_fetch_max<T, addressSpace, sycl::memory_order::acq_rel,
461                            sycl::memory_scope::device>(addr, operand);
462  case sycl::memory_order::seq_cst:
463    return atomic_fetch_max<T, addressSpace, sycl::memory_order::seq_cst,
464                            sycl::memory_scope::device>(addr, operand);
465  default:
466    assert(false && "Invalid memory_order for atomics. Valid memory_order for "
467                    "atomics are: sycl::memory_order::relaxed, "
468                    "sycl::memory_order::acq_rel, sycl::memory_order::seq_cst!");
469  }
470}
471
472template <sycl::access::address_space addressSpace =
473              sycl::access::address_space::global_space,
474          typename T1, typename T2>
475inline T1 atomic_fetch_max(T1 *addr, T2 operand,
476                           sycl::memory_order memoryOrder) {
477  atomic_fetch_max<T1, addressSpace>(addr, operand, memoryOrder);
478}
479
480/// Atomically set \p operand to the value stored in \p addr, if old value stored in
481/// \p addr is equal to zero or greater than \p operand, else decrease the value stored
482/// in \p addr.
483/// \param [in, out] addr The pointer to the data.
484/// \param operand The threshold value.
485/// \param memoryOrder The memory ordering used.
486/// \returns The old value stored in \p addr.
487template <sycl::access::address_space addressSpace = sycl::access::address_space::global_space,
488          sycl::memory_order memoryOrder = sycl::memory_order::relaxed,
489          sycl::memory_scope memoryScope = sycl::memory_scope::device>
490inline unsigned int atomic_fetch_compare_dec(unsigned int *addr,
491                                             unsigned int operand) {
492  auto atm = sycl::atomic_ref<unsigned int, memoryOrder, memoryScope,
493                                  addressSpace>(addr[0]);
494  unsigned int old;
495
496	while (true) {
497	  old = atm.load();
498	  if (old == 0 || old > operand) {
499		  if (atm.compare_exchange_strong(old, operand))
500        break;
501	  } else if (atm.compare_exchange_strong(old, old - 1))
502      break;
503	}
504
505  return old;
506}
507
508/// Atomically increment the value stored in \p addr if old value stored in \p
509/// addr is less than \p operand, else set 0 to the value stored in \p addr.
510/// \param [in, out] addr The pointer to the data.
511/// \param operand The threshold value.
512/// \param memoryOrder The memory ordering used.
513/// \returns The old value stored in \p addr.
514template <sycl::access::address_space addressSpace =
515              sycl::access::address_space::global_space,
516          sycl::memory_order memoryOrder = sycl::memory_order::relaxed,
517          sycl::memory_scope memoryScope = sycl::memory_scope::device>
518inline unsigned int atomic_fetch_compare_inc(unsigned int *addr,
519                                             unsigned int operand) {
520  auto atm = sycl::atomic_ref<unsigned int, memoryOrder, memoryScope,
521                                  addressSpace>(addr[0]);
522  unsigned int old;
523  while (true) {
524    old = atm.load();
525    if (old >= operand) {
526      if (atm.compare_exchange_strong(old, 0))
527        break;
528    } else if (atm.compare_exchange_strong(old, old + 1))
529      break;
530  }
531  return old;
532}
533
534/// Atomically increment the value stored in \p addr if old value stored in \p
535/// addr is less than \p operand, else set 0 to the value stored in \p addr.
536/// \param [in, out] addr The pointer to the data.
537/// \param operand The threshold value.
538/// \param memoryOrder The memory ordering used.
539/// \returns The old value stored in \p addr.
540template <sycl::access::address_space addressSpace =
541              sycl::access::address_space::global_space>
542inline unsigned int
543atomic_fetch_compare_inc(unsigned int *addr, unsigned int operand,
544                         sycl::memory_order memoryOrder) {
545  switch (memoryOrder) {
546  case sycl::memory_order::relaxed:
547    return atomic_fetch_compare_inc<addressSpace, sycl::memory_order::relaxed,
548                                    sycl::memory_scope::device>(addr,
549                                                                   operand);
550  case sycl::memory_order::acq_rel:
551    return atomic_fetch_compare_inc<addressSpace, sycl::memory_order::acq_rel,
552                                    sycl::memory_scope::device>(addr,
553                                                                   operand);
554  case sycl::memory_order::seq_cst:
555    return atomic_fetch_compare_inc<addressSpace, sycl::memory_order::seq_cst,
556                                    sycl::memory_scope::device>(addr,
557                                                                   operand);
558  default:
559    assert(false && "Invalid memory_order for atomics. Valid memory_order for "
560                    "atomics are: sycl::memory_order::relaxed, "
561                    "sycl::memory_order::acq_rel, sycl::memory_order::seq_cst!");
562  }
563}
564
565/// Atomically exchange the value at the address addr with the value operand.
566/// \param [in, out] addr The pointer to the data.
567/// \param operand The value to be exchanged with the value pointed by \p addr.
568/// \param memoryOrder The memory ordering used.
569/// \returns The value at the \p addr before the call.
570template <typename T,
571          sycl::access::address_space addressSpace =
572              sycl::access::address_space::global_space,
573          sycl::memory_order memoryOrder = sycl::memory_order::relaxed,
574          sycl::memory_scope memoryScope = sycl::memory_scope::device>
575inline T atomic_exchange(T *addr, T operand) {
576  auto atm =
577      sycl::atomic_ref<T, memoryOrder, memoryScope, addressSpace>(addr[0]);
578  return atm.exchange(operand);
579}
580
581template <sycl::access::address_space addressSpace =
582              sycl::access::address_space::global_space,
583          sycl::memory_order memoryOrder = sycl::memory_order::relaxed,
584          sycl::memory_scope memoryScope = sycl::memory_scope::device,
585          typename T1, typename T2>
586inline T1 atomic_exchange(T1 *addr, T2 operand) {
587  auto atm =
588      sycl::atomic_ref<T1, memoryOrder, memoryScope, addressSpace>(addr[0]);
589  return atm.exchange(operand);
590}
591
592/// Atomically exchange the value at the address addr with the value operand.
593/// \param [in, out] addr The pointer to the data.
594/// \param operand The value to be exchanged with the value pointed by \p addr.
595/// \param memoryOrder The memory ordering used.
596/// \returns The value at the \p addr before the call.
597template <typename T, sycl::access::address_space addressSpace =
598                          sycl::access::address_space::global_space>
599inline T atomic_exchange(T *addr, T operand,
600                         sycl::memory_order memoryOrder) {
601  switch (memoryOrder) {
602  case sycl::memory_order::relaxed:
603    return atomic_exchange<T, addressSpace, sycl::memory_order::relaxed,
604                           sycl::memory_scope::device>(addr, operand);
605  case sycl::memory_order::acq_rel:
606    return atomic_exchange<T, addressSpace, sycl::memory_order::acq_rel,
607                           sycl::memory_scope::device>(addr, operand);
608  case sycl::memory_order::seq_cst:
609    return atomic_exchange<T, addressSpace, sycl::memory_order::seq_cst,
610                           sycl::memory_scope::device>(addr, operand);
611  default:
612    assert(false && "Invalid memory_order for atomics. Valid memory_order for "
613                    "atomics are: sycl::memory_order::relaxed, "
614                    "sycl::memory_order::acq_rel, sycl::memory_order::seq_cst!");
615  }
616}
617
618template <sycl::access::address_space addressSpace =
619              sycl::access::address_space::global_space,
620          typename T1, typename T2>
621inline T1 atomic_exchange(T1 *addr, T2 operand,
622                           sycl::memory_order memoryOrder) {
623  atomic_exchange<T1, addressSpace>(addr, operand, memoryOrder);
624}
625
626/// Atomically compare the value at \p addr to the value expected and exchange
627/// with the value desired if the value at \p addr is equal to the value expected.
628/// Returns the value at the \p addr before the call.
629/// \param [in, out] addr Multi_ptr.
630/// \param expected The value to compare against the value at \p addr.
631/// \param desired The value to assign to \p addr if the value at \p addr is expected.
632/// \param success The memory ordering used when comparison succeeds.
633/// \param fail The memory ordering used when comparison fails.
634/// \returns The value at the \p addr before the call.
635template <typename T,
636          sycl::access::address_space addressSpace =
637              sycl::access::address_space::global_space,
638          sycl::memory_order memoryOrder = sycl::memory_order::relaxed,
639          sycl::memory_scope memoryScope = sycl::memory_scope::device>
640T atomic_compare_exchange_strong(
641    sycl::multi_ptr<T, addressSpace> addr, T expected, T desired,
642    sycl::memory_order success = sycl::memory_order::relaxed,
643    sycl::memory_order fail = sycl::memory_order::relaxed) {
644  auto atm = sycl::atomic_ref<T, memoryOrder, memoryScope, addressSpace>(*addr);
645
646  atm.compare_exchange_strong(expected, desired, success, fail);
647  return expected;
648}
649
650template <sycl::access::address_space addressSpace =
651              sycl::access::address_space::global_space,
652          sycl::memory_order memoryOrder = sycl::memory_order::relaxed,
653          sycl::memory_scope memoryScope = sycl::memory_scope::device,
654          typename T1, typename T2, typename T3>
655T1 atomic_compare_exchange_strong(
656    sycl::multi_ptr<T1, addressSpace> addr, T2 expected, T3 desired,
657    sycl::memory_order success = sycl::memory_order::relaxed,
658    sycl::memory_order fail = sycl::memory_order::relaxed) {
659  auto atm =
660      sycl::atomic_ref<T1, memoryOrder, memoryScope, addressSpace>(*addr);
661  T1 expected_value = expected;
662  atm.compare_exchange_strong(expected_value, desired, success, fail);
663  return expected_value;
664}
665
666/// Atomically compare the value at \p addr to the value expected and exchange
667/// with the value desired if the value at \p addr is equal to the value expected.
668/// Returns the value at the \p addr before the call.
669/// \param [in] addr The pointer to the data.
670/// \param expected The value to compare against the value at \p addr.
671/// \param desired The value to assign to \p addr if the value at \p addr is expected.
672/// \param success The memory ordering used when comparison succeeds.
673/// \param fail The memory ordering used when comparison fails.
674/// \returns The value at the \p addr before the call.
675template <typename T,
676          sycl::access::address_space addressSpace =
677              sycl::access::address_space::global_space,
678          sycl::memory_order memoryOrder = sycl::memory_order::relaxed,
679          sycl::memory_scope memoryScope = sycl::memory_scope::device>
680T atomic_compare_exchange_strong(
681    T *addr, T expected, T desired,
682    sycl::memory_order success = sycl::memory_order::relaxed,
683    sycl::memory_order fail = sycl::memory_order::relaxed) {
684  auto atm =
685      sycl::atomic_ref<T, memoryOrder, memoryScope, addressSpace>(addr[0]);
686  atm.compare_exchange_strong(expected, desired, success, fail);
687  return expected;
688}
689
690template <sycl::access::address_space addressSpace =
691              sycl::access::address_space::global_space,
692          sycl::memory_order memoryOrder = sycl::memory_order::relaxed,
693          sycl::memory_scope memoryScope = sycl::memory_scope::device,
694          typename T1, typename T2, typename T3>
695T1 atomic_compare_exchange_strong(
696    T1 *addr, T2 expected, T3 desired,
697    sycl::memory_order success = sycl::memory_order::relaxed,
698    sycl::memory_order fail = sycl::memory_order::relaxed) {
699  T1 expected_value = expected;
700  auto atm =
701      sycl::atomic_ref<T1, memoryOrder, memoryScope, addressSpace>(addr[0]);
702  atm.compare_exchange_strong(expected_value, desired, success, fail);
703  return expected_value;
704}
705
706/// Atomic extension to implement standard APIs in std::atomic
707namespace detail{
708template <typename T> struct IsValidAtomicType {
709  static constexpr bool value =
710      (std::is_same<T, int>::value || std::is_same<T, unsigned int>::value ||
711       std::is_same<T, long>::value || std::is_same<T, unsigned long>::value ||
712       std::is_same<T, long long>::value ||
713       std::is_same<T, unsigned long long>::value ||
714       std::is_same<T, float>::value || std::is_same<T, double>::value ||
715       std::is_pointer<T>::value);
716};
717} // namespace detail
718
719template <typename T,
720          sycl::memory_scope DefaultScope = sycl::memory_scope::system,
721#ifdef __AMDGPU__
722          sycl::memory_order DefaultOrder = sycl::memory_order::acq_rel,
723#else
724          sycl::memory_order DefaultOrder = sycl::memory_order::seq_cst,
725#endif
726          sycl::access::address_space Space =
727              sycl::access::address_space::generic_space>
728class atomic {
729  static_assert(
730    detail::IsValidAtomicType<T>::value,
731    "Invalid atomic type.  Valid types are int, unsigned int, long, "
732      "unsigned long, long long, unsigned long long, float, double "
733      "and pointer types");
734  T __d;
735
736public:
737  /// default memory synchronization order
738  static constexpr sycl::memory_order default_read_order =
739      sycl::atomic_ref<T, DefaultOrder, DefaultScope, Space>::default_read_order;
740  static constexpr sycl::memory_order default_write_order =
741      sycl::atomic_ref<T, DefaultOrder, DefaultScope, Space>::default_write_order;
742  static constexpr sycl::memory_scope default_scope = DefaultScope;
743  static constexpr sycl::memory_order default_read_modify_write_order =
744      DefaultOrder;
745
746  /// Default constructor.
747  constexpr atomic() noexcept = default;
748  /// Constructor with initialize value.
749  constexpr atomic(T d) noexcept : __d(d){};
750
751  /// atomically replaces the value of the referenced object with a non-atomic argument
752  /// \param operand The value to replace the pointed value.
753  /// \param memoryOrder The memory ordering used.
754  /// \param memoryScope The memory scope used.
755  void store(T operand, sycl::memory_order memoryOrder = default_write_order,
756             sycl::memory_scope memoryScope = default_scope) noexcept {
757    sycl::atomic_ref<T, DefaultOrder, DefaultScope, Space> atm(__d);
758    atm.store(operand, memoryOrder, memoryScope);
759  }
760
761  /// atomically obtains the value of the referenced object
762  /// \param memoryOrder The memory ordering used.
763  /// \param memoryScope The memory scope used.
764  /// \returns The value of the referenced object
765  T load(sycl::memory_order memoryOrder = default_read_order,
766         sycl::memory_scope memoryScope = default_scope) const noexcept {
767    sycl::atomic_ref<T, DefaultOrder, DefaultScope, Space> atm(
768      const_cast<T &>(__d));
769    return atm.load(memoryOrder, memoryScope);
770  }
771
772  /// atomically replaces the value of the referenced object and obtains the value held previously
773  /// \param operand The value to replace the pointed value.
774  /// \param memoryOrder The memory ordering used.
775  /// \param memoryScope The memory scope used.
776  /// \returns The value of the referenced object before the call.
777  T exchange(T operand,
778             sycl::memory_order memoryOrder = default_read_modify_write_order,
779             sycl::memory_scope memoryScope = default_scope) noexcept {
780
781    sycl::atomic_ref<T, DefaultOrder, DefaultScope, Space> atm(__d);
782    return atm.exchange(operand, memoryOrder, memoryScope);
783  }
784
785  /// atomically compares the value of the referenced object with non-atomic argument 
786  /// and performs atomic exchange if equal or atomic load if not
787  /// \param expected The value expected to be found in the object referenced by the atomic_ref object
788  /// \param desired  The value to store in the referenced object if it is as expected
789  /// \param success The memory models for the read-modify-write
790  /// \param failure The memory models for load operations
791  /// \param memoryScope The memory scope used.
792  /// \returns true if the referenced object was successfully changed, false otherwise.
793  bool compare_exchange_weak(
794      T &expected, T desired,
795      sycl::memory_order success, sycl::memory_order failure,
796      sycl::memory_scope memoryScope = default_scope) noexcept {
797    sycl::atomic_ref<T, DefaultOrder, DefaultScope, Space> atm(__d);
798    return atm.compare_exchange_weak(expected, desired, success, failure, memoryScope);
799  }
800  /// \param expected The value expected to be found in the object referenced by the atomic_ref object
801  /// \param desired  The value to store in the referenced object if it is as expected
802  /// \param memoryOrder 	The memory synchronization ordering for operations
803  /// \param memoryScope The memory scope used.
804  /// \returns true if the referenced object was successfully changed, false otherwise.
805  bool compare_exchange_weak(T &expected, T desired,
806                  sycl::memory_order memoryOrder = default_read_modify_write_order,
807                  sycl::memory_scope memoryScope = default_scope) noexcept {
808    sycl::atomic_ref<T, DefaultOrder, DefaultScope, Space> atm(__d);
809    return atm.compare_exchange_weak(expected, desired, memoryOrder, memoryScope);
810  }
811
812  /// atomically compares the value of the referenced object with non-atomic argument 
813  /// and performs atomic exchange if equal or atomic load if not
814  /// \param expected The value expected to be found in the object referenced by the atomic_ref object
815  /// \param desired  The value to store in the referenced object if it is as expected
816  /// \param success The memory models for the read-modify-write
817  /// \param failure The memory models for load operations
818  /// \param memoryScope The memory scope used.
819  /// \returns true if the referenced object was successfully changed, false otherwise.
820  bool compare_exchange_strong(
821      T &expected, T desired,
822      sycl::memory_order success, sycl::memory_order failure,
823      sycl::memory_scope memoryScope = default_scope) noexcept {
824
825    sycl::atomic_ref<T, DefaultOrder, DefaultScope, Space> atm(__d);
826    return atm.compare_exchange_strong(expected, desired, success, failure, memoryScope);
827  }
828  /// \param expected The value expected to be found in the object referenced by the atomic_ref object
829  /// \param desired  The value to store in the referenced object if it is as expected
830  /// \param memoryOrder 	The memory synchronization ordering for operations
831  /// \param memoryScope The memory scope used.
832  /// \returns true if the referenced object was successfully changed, false otherwise.
833  bool compare_exchange_strong(T &expected, T desired,
834                    sycl::memory_order memoryOrder = default_read_modify_write_order,
835                    sycl::memory_scope memoryScope = default_scope) noexcept {
836    sycl::atomic_ref<T, DefaultOrder, DefaultScope, Space> atm(__d);
837    return atm.compare_exchange_strong(expected, desired, memoryOrder, memoryScope);
838  }
839
840  /// atomically adds the argument to the value stored in the atomic object and obtains the value held previously
841  /// \param operand 	The other argument of arithmetic addition
842  /// \param memoryOrder The memory ordering used.
843  /// \param memoryScope The memory scope used.
844  /// \returns The value of the referenced object before the call.
845  T fetch_add(T operand,
846              sycl::memory_order memoryOrder = default_read_modify_write_order,
847              sycl::memory_scope  memoryScope = default_scope) noexcept {
848
849    sycl::atomic_ref<T, DefaultOrder, DefaultScope, Space> atm(__d);
850    return atm.fetch_add(operand, memoryOrder,  memoryScope);
851  }
852
853  /// atomically subtracts the argument from the value stored in the atomic object and obtains the value held previously
854  /// \param operand 	The other argument of arithmetic subtraction
855  /// \param memoryOrder The memory ordering used.
856  /// \param memoryScope The memory scope used.
857  /// \returns The value of the referenced object before the call.
858  T fetch_sub(T operand,
859              sycl::memory_order memoryOrder = default_read_modify_write_order,
860              sycl::memory_scope memoryScope = default_scope) noexcept {
861
862    sycl::atomic_ref<T, DefaultOrder, DefaultScope, Space> atm(__d);
863    return atm.fetch_sub(operand, memoryOrder, memoryScope);
864  }
865};
866
867} // namespace dpct
868#endif // __DPCT_ATOMIC_HPP__