Debian Codegraph @

hipamd/include/hip/amd_detail/hip_cooperative_groups_helper.h

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 /*
2 Copyright (c) 2015 - 2023 Advanced Micro Devices, Inc. All rights reserved.
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 */
22
23 /**
24 * @file amd_detail/hip_cooperative_groups_helper.h
25 *
26 * @brief Device side implementation of cooperative group feature.
27 *
28 * Defines helper constructs and APIs which aid the types and device API
29 * wrappers defined within `amd_detail/hip_cooperative_groups.h`.
30 */
31 #ifndef HIP_INCLUDE_HIP_AMD_DETAIL_HIP_COOPERATIVE_GROUPS_HELPER_H
32 #define HIP_INCLUDE_HIP_AMD_DETAIL_HIP_COOPERATIVE_GROUPS_HELPER_H
33
34 #if __cplusplus
35 #if !defined(__HIPCC_RTC__)
36 #include <hip/amd_detail/amd_hip_runtime.h> // threadId, blockId
37 #include <hip/amd_detail/amd_device_functions.h>
38 #endif
39 #if !defined(__align__)
40 #define __align__(x) __attribute__((aligned(x)))
41 #endif
42
43 #if !defined(__CG_QUALIFIER__)
44 #define __CG_QUALIFIER__ __device__ __forceinline__
45 #endif
46
47 #if !defined(__CG_STATIC_QUALIFIER__)
48 #define __CG_STATIC_QUALIFIER__ __device__ static __forceinline__
49 #endif
50
51 #if !defined(_CG_STATIC_CONST_DECL_)
52 #define _CG_STATIC_CONST_DECL_ static constexpr
53 #endif
54
55 #if defined(__SPIRV__) && !defined(__AMDGCN_WAVEFRONT_SIZE)
56 #error "TEMPORARY LIMITATION: when targeting AMDGCN SPIR-V"
57 "__AMDGCN_WAVEFRONT_SIZE is not defined, and must be defined by the user"
58 #endif
59 #if __AMDGCN_WAVEFRONT_SIZE == 32
60 using lane_mask = unsigned int;
61 #else
62 using lane_mask = unsigned long long int;
63 #endif
64
65 namespace cooperative_groups {
66
67 /* Global scope */
68 template <unsigned int size>
69 using is_power_of_2 = std::integral_constant<bool, (size & (size - 1)) == 0>;
70
71 template <unsigned int size>
72 using is_valid_wavefront = std::integral_constant<bool, (size <= __AMDGCN_WAVEFRONT_SIZE)>;
73
74 template <unsigned int size>
75 using is_valid_tile_size =
76 std::integral_constant<bool, is_power_of_2<size>::value && is_valid_wavefront<size>::value>;
77
78 template <typename T>
79 using is_valid_type =
80 std::integral_constant<bool, std::is_integral<T>::value || std::is_floating_point<T>::value>;
81
82 namespace internal {
83
84 /**
85 * @brief Enums representing different cooperative group types
86 * @note This enum is only applicable on Linux.
87 *
88 */
89 typedef enum {
90 cg_invalid,
91 cg_multi_grid,
92 cg_grid,
93 cg_workgroup,
94 cg_tiled_group,
95 cg_coalesced_group
96 } group_type;
97 /**
98 * @ingroup CooperativeG
99 * @{
100 * This section describes the cooperative groups functions of HIP runtime API.
101 *
102 * The cooperative groups provides flexible thread parallel programming algorithms, threads
103 * cooperate and share data to perform collective computations.
104 *
•105 * @note Cooperative groups feature is implemented on Linux, under development
106 * on Windows.
107 *
108 */
109 namespace helper {
110 /**
111 * @brief Create output mask from input_mask at places where base_mask is set
112 *
113 * Example: base_mask = 0101'0101, input_mask = 1111'0000
114 * Output mask: 1100
•115 * Explanation:
116 * | | | | | | | |
117 * base: 0|1|0|1|'0|1|0|1| // Which bits are set
118 * input: 1|1|1|1|'0|0|0|0| // Which values are picked
119 * | | | | | | | |
120 * output: 1 1 0 0
121 */
122 __CG_STATIC_QUALIFIER__ unsigned long long adjust_mask(
123 unsigned long long base_mask, unsigned long long input_mask) {
124 unsigned long long out = 0;
125 for (unsigned int i = 0, index = 0; i < warpSize; i++) {
126 auto lane_active = base_mask & (1ull << i);
127 if (lane_active) {
128 auto result = input_mask & (1ull << i);
129 out |= ((result ? 1ull : 0ull) << index);
130 index++;
131 }
132 }
133 return out;
134 }
135 } // namespace helper
136 /**
137 *
138 * @brief Functionalities related to multi-grid cooperative group type
139 * @note The following cooperative groups functions are only applicable on Linux.
140 *
141 */
142 namespace multi_grid {
143
144 __CG_STATIC_QUALIFIER__ uint32_t num_grids() {
145 return static_cast<uint32_t>(__ockl_multi_grid_num_grids()); }
146
147 __CG_STATIC_QUALIFIER__ uint32_t grid_rank() {
148 return static_cast<uint32_t>(__ockl_multi_grid_grid_rank()); }
149
150 __CG_STATIC_QUALIFIER__ uint32_t size() { return static_cast<uint32_t>(__ockl_multi_grid_size()); }
151
152 __CG_STATIC_QUALIFIER__ uint32_t thread_rank() {
153 return static_cast<uint32_t>(__ockl_multi_grid_thread_rank()); }
154
155 __CG_STATIC_QUALIFIER__ bool is_valid() { return static_cast<bool>(__ockl_multi_grid_is_valid()); }
156
157 __CG_STATIC_QUALIFIER__ void sync() { __ockl_multi_grid_sync(); }
158
159 } // namespace multi_grid
160
161 /**
162 * @brief Functionalities related to grid cooperative group type
163 * @note The following cooperative groups functions are only applicable on Linux.
164 */
165 namespace grid {
166
167 __CG_STATIC_QUALIFIER__ uint32_t size() {
168 return static_cast<uint32_t>((blockDim.z * gridDim.z) * (blockDim.y * gridDim.y) *
169 (blockDim.x * gridDim.x));
170 }
171
172 __CG_STATIC_QUALIFIER__ uint32_t thread_rank() {
173 // Compute global id of the workgroup to which the current thread belongs to
174 uint32_t blkIdx = static_cast<uint32_t>((blockIdx.z * gridDim.y * gridDim.x) +
175 (blockIdx.y * gridDim.x) + (blockIdx.x));
176
177 // Compute total number of threads being passed to reach current workgroup
178 // within grid
179 uint32_t num_threads_till_current_workgroup =
180 static_cast<uint32_t>(blkIdx * (blockDim.x * blockDim.y * blockDim.z));
181
182 // Compute thread local rank within current workgroup
183 uint32_t local_thread_rank = static_cast<uint32_t>((threadIdx.z * blockDim.y * blockDim.x) +
184 (threadIdx.y * blockDim.x) + (threadIdx.x));
185
186 return (num_threads_till_current_workgroup + local_thread_rank);
187 }
188
189 __CG_STATIC_QUALIFIER__ bool is_valid() { return static_cast<bool>(__ockl_grid_is_valid()); }
190
191 __CG_STATIC_QUALIFIER__ void sync() { __ockl_grid_sync(); }
192
193 } // namespace grid
194
195 /**
196 * @brief Functionalities related to `workgroup` (thread_block in CUDA terminology)
197 * cooperative group type
198 * @note The following cooperative groups functions are only applicable on Linux.
199 */
200 namespace workgroup {
201
202 __CG_STATIC_QUALIFIER__ dim3 group_index() {
203 return (dim3(static_cast<uint32_t>(blockIdx.x), static_cast<uint32_t>(blockIdx.y),
204 static_cast<uint32_t>(blockIdx.z)));
205 }
206
207 __CG_STATIC_QUALIFIER__ dim3 thread_index() {
208 return (dim3(static_cast<uint32_t>(threadIdx.x), static_cast<uint32_t>(threadIdx.y),
209 static_cast<uint32_t>(threadIdx.z)));
210 }
211
212 __CG_STATIC_QUALIFIER__ uint32_t size() {
213 return (static_cast<uint32_t>(blockDim.x * blockDim.y * blockDim.z));
214 }
215
216 __CG_STATIC_QUALIFIER__ uint32_t thread_rank() {
217 return (static_cast<uint32_t>((threadIdx.z * blockDim.y * blockDim.x) +
218 (threadIdx.y * blockDim.x) + (threadIdx.x)));
219 }
220
221 __CG_STATIC_QUALIFIER__ bool is_valid() {
222 return true;
223 }
224
225 __CG_STATIC_QUALIFIER__ void sync() { __syncthreads(); }
226
227 __CG_STATIC_QUALIFIER__ dim3 block_dim() {
228 return (dim3(static_cast<uint32_t>(blockDim.x), static_cast<uint32_t>(blockDim.y),
229 static_cast<uint32_t>(blockDim.z)));
230 }
231
232 } // namespace workgroup
233
234 namespace tiled_group {
235
•236 // enforce ordering for memory instructions
237 __CG_STATIC_QUALIFIER__ void sync() { __builtin_amdgcn_fence(__ATOMIC_ACQ_REL, "agent"); }
238
239 } // namespace tiled_group
240
241 namespace coalesced_group {
242
•243 // enforce ordering for memory instructions
244 __CG_STATIC_QUALIFIER__ void sync() { __builtin_amdgcn_fence(__ATOMIC_ACQ_REL, "agent"); }
245
246 // Masked bit count
247 //
248 // For each thread, this function returns the number of active threads which
249 // have i-th bit of x set and come before the current thread.
250 __CG_STATIC_QUALIFIER__ unsigned int masked_bit_count(lane_mask x, unsigned int add = 0) {
251 unsigned int counter=0;
252 if (warpSize == 32) {
253 counter = __builtin_amdgcn_mbcnt_lo(x, add);
254 } else {
255 counter = __builtin_amdgcn_mbcnt_lo(static_cast<lane_mask>(x), add);
256 counter = __builtin_amdgcn_mbcnt_hi(static_cast<lane_mask>(x >> 32), counter);
257 }
258
259 return counter;
260 }
261
262 } // namespace coalesced_group
263
264
265 } // namespace internal
266
267 } // namespace cooperative_groups
268 /**
269 * @}
270 */
271
272 #endif // __cplusplus
273 #endif // HIP_INCLUDE_HIP_AMD_DETAIL_HIP_COOPERATIVE_GROUPS_HELPER_H