Debian Codegraph @

openmp/runtime/src/include/ompx.h.var

raw
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8
9 #ifndef __OMPX_H
10 #define __OMPX_H
11
12 #if (defined(__NVPTX__) || defined(__AMDGPU__))
13 #include <gpuintrin.h>
14 #define __OMPX_TARGET_IS_GPU
15 #endif
16
17 typedef unsigned long uint64_t;
18 typedef unsigned int uint32_t;
19
20 static inline uint32_t __warpSize(void) {
21 #ifdef __OMPX_TARGET_IS_GPU
22 return __gpu_num_lanes();
23 #else
24 __builtin_trap();
25 #endif
26 }
27
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
31
32 int omp_get_ancestor_thread_num(int);
33 int omp_get_team_size(int);
34
35 #ifdef __cplusplus
36 }
37 #endif
38
39 /// Target kernel language extensions
40 ///
41 /// These extensions exist for the host to allow fallback implementations,
42 /// however, they cannot be arbitrarily composed with OpenMP. If the rules of
43 /// the kernel language are followed, the host fallbacks should behave as
44 /// expected since the kernel is represented as 3 sequential outer loops, one
45 /// for each grid dimension, and three (nested) parallel loops, one for each
46 /// block dimension. This fallback is not supposed to be optimal and should be
47 /// configurable by the user.
48 ///
49 ///{
50
51 #ifdef __cplusplus
52 extern "C" {
53 #endif
54
55 enum {
56 ompx_relaxed = __ATOMIC_RELAXED,
57 ompx_aquire = __ATOMIC_ACQUIRE,
58 ompx_release = __ATOMIC_RELEASE,
59 ompx_acq_rel = __ATOMIC_ACQ_REL,
60 ompx_seq_cst = __ATOMIC_SEQ_CST,
61 };
62
63 enum {
64 ompx_dim_x = 0,
65 ompx_dim_y = 1,
66 ompx_dim_z = 2,
67 };
68
69 // TODO: The following implementation is for host fallback. We need to disable
70 // generation of host fallback in kernel language mode.
71 #pragma omp begin declare variant match(device = {kind(cpu)})
72
73 /// ompx_{thread,block}_{id,dim}
74 ///{
75 #define _TGT_KERNEL_LANGUAGE_HOST_IMPL_GRID_C(NAME, VALUE) \
76 static inline int ompx_##NAME(int Dim) { return VALUE; }
77
78 _TGT_KERNEL_LANGUAGE_HOST_IMPL_GRID_C(thread_id,
79 omp_get_ancestor_thread_num(Dim + 1))
80 _TGT_KERNEL_LANGUAGE_HOST_IMPL_GRID_C(block_dim, omp_get_team_size(Dim + 1))
81 _TGT_KERNEL_LANGUAGE_HOST_IMPL_GRID_C(block_id, 0)
82 _TGT_KERNEL_LANGUAGE_HOST_IMPL_GRID_C(grid_dim, 1)
83 #undef _TGT_KERNEL_LANGUAGE_HOST_IMPL_GRID_C
84 ///}
85
86 /// ompx_{sync_block}_{,divergent}
87 ///{
88 #define _TGT_KERNEL_LANGUAGE_HOST_IMPL_SYNC_C(RETTY, NAME, ARGS, BODY) \
89 static inline RETTY ompx_##NAME(ARGS) { BODY; }
90
91 _TGT_KERNEL_LANGUAGE_HOST_IMPL_SYNC_C(void, sync_block, int Ordering,
92 _Pragma("omp barrier"))
93 _TGT_KERNEL_LANGUAGE_HOST_IMPL_SYNC_C(void, sync_block_acq_rel, void,
94 ompx_sync_block(ompx_acq_rel))
95 _TGT_KERNEL_LANGUAGE_HOST_IMPL_SYNC_C(void, sync_block_divergent, int Ordering,
96 ompx_sync_block(Ordering))
97 #undef _TGT_KERNEL_LANGUAGE_HOST_IMPL_SYNC_C
98 ///}
99
100 static inline uint64_t ompx_ballot_sync(uint64_t mask, int pred) {
101 __builtin_trap();
102 }
103
104 /// ompx_shfl_down_sync_{i,f,l,d}
105 ///{
106 #define _TGT_KERNEL_LANGUAGE_SHFL_DOWN_SYNC_HOST_IMPL(TYPE, TY) \
107 static inline TYPE ompx_shfl_down_sync_##TY(uint64_t mask, TYPE var, \
108 unsigned delta, int width) { \
109 __builtin_trap(); \
110 }
111
112 _TGT_KERNEL_LANGUAGE_SHFL_DOWN_SYNC_HOST_IMPL(int, i)
113 _TGT_KERNEL_LANGUAGE_SHFL_DOWN_SYNC_HOST_IMPL(float, f)
114 _TGT_KERNEL_LANGUAGE_SHFL_DOWN_SYNC_HOST_IMPL(long, l)
115 _TGT_KERNEL_LANGUAGE_SHFL_DOWN_SYNC_HOST_IMPL(double, d)
116
117 #undef _TGT_KERNEL_LANGUAGE_SHFL_DOWN_SYNC_HOST_IMPL
118 ///}
119
120 #pragma omp end declare variant
121
122 /// ompx_{sync_block}_{,divergent}
123 ///{
124 #define _TGT_KERNEL_LANGUAGE_DECL_SYNC_C(RETTY, NAME, ARGS) \
125 RETTY ompx_##NAME(ARGS);
126
127 _TGT_KERNEL_LANGUAGE_DECL_SYNC_C(void, sync_block, int Ordering)
128 _TGT_KERNEL_LANGUAGE_DECL_SYNC_C(void, sync_block_acq_rel, void)
129 _TGT_KERNEL_LANGUAGE_DECL_SYNC_C(void, sync_block_divergent, int Ordering)
130 #undef _TGT_KERNEL_LANGUAGE_DECL_SYNC_C
131 ///}
132
133 /// ompx_{thread,block}_{id,dim}_{x,y,z}
134 ///{
135 #define _TGT_KERNEL_LANGUAGE_DECL_GRID_C(NAME) \
136 int ompx_##NAME(int Dim); \
137 static inline int ompx_##NAME##_x() { return ompx_##NAME(ompx_dim_x); } \
138 static inline int ompx_##NAME##_y() { return ompx_##NAME(ompx_dim_y); } \
139 static inline int ompx_##NAME##_z() { return ompx_##NAME(ompx_dim_z); }
140
141 _TGT_KERNEL_LANGUAGE_DECL_GRID_C(thread_id)
142 _TGT_KERNEL_LANGUAGE_DECL_GRID_C(block_dim)
143 _TGT_KERNEL_LANGUAGE_DECL_GRID_C(block_id)
144 _TGT_KERNEL_LANGUAGE_DECL_GRID_C(grid_dim)
145 #undef _TGT_KERNEL_LANGUAGE_DECL_GRID_C
146 ///}
147
148 uint64_t ompx_ballot_sync(uint64_t mask, int pred);
149
150 /// ompx_shfl_down_sync_{i,f,l,d}
151 ///{
152 #define _TGT_KERNEL_LANGUAGE_SHFL_DOWN_SYNC(TYPE, TY) \
153 TYPE ompx_shfl_down_sync_##TY(uint64_t mask, TYPE var, unsigned delta, \
154 int width);
155
156 _TGT_KERNEL_LANGUAGE_SHFL_DOWN_SYNC(int, i)
157 _TGT_KERNEL_LANGUAGE_SHFL_DOWN_SYNC(float, f)
158 _TGT_KERNEL_LANGUAGE_SHFL_DOWN_SYNC(long, l)
159 _TGT_KERNEL_LANGUAGE_SHFL_DOWN_SYNC(double, d)
160
161 #undef _TGT_KERNEL_LANGUAGE_SHFL_DOWN_SYNC
162 ///}
163
164 #ifdef __cplusplus
165 }
166 #endif
167
168 #ifdef __cplusplus
169
170 namespace ompx {
171
172 enum {
173 dim_x = ompx_dim_x,
174 dim_y = ompx_dim_y,
175 dim_z = ompx_dim_z,
176 };
177
178 enum {
179 relaxed = ompx_relaxed ,
180 aquire = ompx_aquire,
181 release = ompx_release,
182 acc_rel = ompx_acq_rel,
183 seq_cst = ompx_seq_cst,
184 };
185
186 /// ompx::{thread,block}_{id,dim}_{,x,y,z}
187 ///{
188 #define _TGT_KERNEL_LANGUAGE_HOST_IMPL_GRID_CXX(NAME) \
189 static inline int NAME(int Dim) noexcept { return ompx_##NAME(Dim); } \
190 static inline int NAME##_x() noexcept { return NAME(ompx_dim_x); } \
191 static inline int NAME##_y() noexcept { return NAME(ompx_dim_y); } \
192 static inline int NAME##_z() noexcept { return NAME(ompx_dim_z); }
193
194 _TGT_KERNEL_LANGUAGE_HOST_IMPL_GRID_CXX(thread_id)
195 _TGT_KERNEL_LANGUAGE_HOST_IMPL_GRID_CXX(block_dim)
196 _TGT_KERNEL_LANGUAGE_HOST_IMPL_GRID_CXX(block_id)
197 _TGT_KERNEL_LANGUAGE_HOST_IMPL_GRID_CXX(grid_dim)
198 #undef _TGT_KERNEL_LANGUAGE_HOST_IMPL_GRID_CXX
199 ///}
200
201 /// ompx_{sync_block}_{,divergent}
202 ///{
203 #define _TGT_KERNEL_LANGUAGE_HOST_IMPL_SYNC_CXX(RETTY, NAME, ARGS, CALL_ARGS) \
204 static inline RETTY NAME(ARGS) { \
205 return ompx_##NAME(CALL_ARGS); \
206 }
207
208 _TGT_KERNEL_LANGUAGE_HOST_IMPL_SYNC_CXX(void, sync_block, int Ordering = acc_rel,
209 Ordering)
210 _TGT_KERNEL_LANGUAGE_HOST_IMPL_SYNC_CXX(void, sync_block_divergent,
211 int Ordering = acc_rel, Ordering)
212 #undef _TGT_KERNEL_LANGUAGE_HOST_IMPL_SYNC_CXX
213 ///}
214
215 static inline uint64_t ballot_sync(uint64_t mask, int pred) {
216 return ompx_ballot_sync(mask, pred);
217 }
218
219 /// shfl_down_sync
220 ///{
221 #define _TGT_KERNEL_LANGUAGE_SHFL_DOWN_SYNC(TYPE, TY) \
222 static inline TYPE shfl_down_sync(uint64_t mask, TYPE var, unsigned delta, \
223 int width = __warpSize()) { \
224 return ompx_shfl_down_sync_##TY(mask, var, delta, width); \
225 }
226
227 _TGT_KERNEL_LANGUAGE_SHFL_DOWN_SYNC(int, i)
228 _TGT_KERNEL_LANGUAGE_SHFL_DOWN_SYNC(float, f)
229 _TGT_KERNEL_LANGUAGE_SHFL_DOWN_SYNC(long, l)
230 _TGT_KERNEL_LANGUAGE_SHFL_DOWN_SYNC(double, d)
231
232 #undef _TGT_KERNEL_LANGUAGE_SHFL_DOWN_SYNC
233 ///}
234
235 } // namespace ompx
236 #endif
237
238 ///}
239
240 #endif /* __OMPX_H */