forked from kokkos/kokkos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Compare_Exchange_OpenACC.hpp
153 lines (139 loc) · 4.82 KB
/
Compare_Exchange_OpenACC.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
Copyright (c) 2019, Lawrence Livermore National Security, LLC
and DESUL project contributors. See the COPYRIGHT file for details.
Source: https://github.com/desul/desul
SPDX-License-Identifier: (BSD-3-Clause)
*/
#ifndef DESUL_ATOMICS_COMPARE_EXCHANGE_OPENACC_HPP_
#define DESUL_ATOMICS_COMPARE_EXCHANGE_OPENACC_HPP_
#include <openacc.h>
#include <desul/atomics/Common.hpp>
#include <desul/atomics/Thread_Fence_OpenACC.hpp>
#include <type_traits>
namespace desul {
namespace Impl {
#ifdef __NVCOMPILER
#pragma acc routine seq
template <class T, class MemoryOrder, class MemoryScope>
T device_atomic_exchange(T* dest, T value, MemoryOrder, MemoryScope /*scope*/) {
if constexpr (std::is_arithmetic_v<T> && ((sizeof(T) == 4) || (sizeof(T) == 8))) {
T return_val;
#pragma acc atomic capture
{
return_val = *dest;
*dest = value;
}
return return_val;
} else {
// FIXME_OPENACC
if (acc_on_device(acc_device_not_host)) {
printf(
"DESUL error in device_atomic_exchange(): Not supported atomic operation in "
"the OpenACC backend\n");
}
// Acquire a lock for the address
// while (!lock_address_openacc((void*)dest, scope)) {
// }
// device_atomic_thread_fence(MemoryOrderAcquire(), scope);
T return_val = *dest;
*dest = value;
// device_atomic_thread_fence(MemoryOrderRelease(), scope);
// unlock_address_openacc((void*)dest, scope);
return return_val;
}
}
#pragma acc routine seq
template <class T, class MemoryOrder, class MemoryScope>
T device_atomic_compare_exchange(
T* dest, T compare, T value, MemoryOrder, MemoryScope scope) {
// Floating point types treated separetely to work around compiler errors
// "parse invalid cast opcode for cast from 'i32' to 'float'".
// Also not just "forwarding" arguments to atomicCAS because it does not have an
// overload that takes int64_t
if constexpr (std::is_integral_v<T> && ((sizeof(T) == 4) || (sizeof(T) == 8))) {
static_assert(sizeof(unsigned int) == 4);
static_assert(sizeof(unsigned long long int) == 8);
using cas_t =
std::conditional_t<(sizeof(T) == 4), unsigned int, unsigned long long int>;
cas_t return_val = atomicCAS(reinterpret_cast<cas_t*>(dest),
reinterpret_cast<cas_t&>(compare),
reinterpret_cast<cas_t&>(value));
return reinterpret_cast<T&>(return_val);
#ifdef DESUL_CUDA_ARCH_IS_PRE_PASCAL
} else if constexpr (std::is_same_v<T, float>) {
#else
} else if constexpr (std::is_same_v<T, float> || std::is_same_v<T, double>) {
#endif
return atomicCAS(dest, compare, value);
} else {
// FIXME_OPENACC
if (acc_on_device(acc_device_not_host)) {
printf(
"DESUL error in device_atomic_compare_exchange(): Not supported atomic "
"operation in the OpenACC backend\n");
}
T current_val = *dest;
// Acquire a lock for the address
// while (!lock_address_openacc((void*)dest, scope)) {
//}
// device_atomic_thread_fence(MemoryOrderAcquire(), scope);
if (current_val == compare) {
*dest = value;
// device_atomic_thread_fence(MemoryOrderRelease(), scope);
}
// unlock_address_openacc((void*)dest, scope);
return current_val;
}
}
#else // not NVHPC
#pragma acc routine seq
template <class T, class MemoryOrder, class MemoryScope>
T device_atomic_exchange(T* dest, T value, MemoryOrder, MemoryScope) {
if constexpr (std::is_arithmetic_v<T>) {
T return_val;
#pragma acc atomic capture
{
return_val = *dest;
*dest = value;
}
return return_val;
} else {
// FIXME_OPENACC
printf(
"DESUL error in device_atomic_exchange(): Not supported atomic operation in "
"the OpenACC backend\n");
// Acquire a lock for the address
// while (!lock_address_openacc((void*)dest, scope)) {
// }
// device_atomic_thread_fence(MemoryOrderAcquire(), scope);
T return_val = *dest;
*dest = value;
// device_atomic_thread_fence(MemoryOrderRelease(), scope);
// unlock_address_openacc((void*)dest, scope);
return return_val;
}
}
#pragma acc routine seq
template <class T, class MemoryOrder, class MemoryScope>
T device_atomic_compare_exchange(
T* dest, T compare, T value, MemoryOrder, MemoryScope scope) {
// FIXME_OPENACC
printf(
"DESUL error in device_atomic_compare_exchange(): Not supported atomic operation "
"in the OpenACC backend\n");
T current_val = *dest;
// Acquire a lock for the address
// while (!lock_address_openacc((void*)dest, scope)) {
//}
// device_atomic_thread_fence(MemoryOrderAcquire(), scope);
if (current_val == compare) {
*dest = value;
// device_atomic_thread_fence(MemoryOrderRelease(), scope);
}
// unlock_address_openacc((void*)dest, scope);
return current_val;
}
#endif
} // namespace Impl
} // namespace desul
#endif