| 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 |
#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> |
| 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 |
|
| 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 |
|
| 86 |
|
| 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 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| •105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
namespace helper { |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| •115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 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 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 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 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 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 |
|
| 174 |
uint32_t blkIdx = static_cast<uint32_t>((blockIdx.z * gridDim.y * gridDim.x) + |
| 175 |
(blockIdx.y * gridDim.x) + (blockIdx.x)); |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
uint32_t num_threads_till_current_workgroup = |
| 180 |
static_cast<uint32_t>(blkIdx * (blockDim.x * blockDim.y * blockDim.z)); |
| 181 |
|
| 182 |
|
| 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 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 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 |
} |
| 233 |
|
| 234 |
namespace tiled_group { |
| 235 |
|
| •236 |
|
| 237 |
__CG_STATIC_QUALIFIER__ void sync() { __builtin_amdgcn_fence(__ATOMIC_ACQ_REL, "agent"); } |
| 238 |
|
| 239 |
} |
| 240 |
|
| 241 |
namespace coalesced_group { |
| 242 |
|
| •243 |
|
| 244 |
__CG_STATIC_QUALIFIER__ void sync() { __builtin_amdgcn_fence(__ATOMIC_ACQ_REL, "agent"); } |
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 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 |
} |
| 263 |
|
| 264 |
|
| 265 |
} |
| 266 |
|
| 267 |
} |
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
#endif |
| 273 |
#endif |