okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
a751fbb12
main
1// S26: several warp sums at once (P per-lane values -> P sums), bitwise equal to P separate xor-butterfly warp sums 2// (`for (o = 16; o > 0; o >>= 1) v += shfl_xor(v, o)`). At offset 16 a lane keeps half of its P values and adds the 3// partner's copy of that half, at 8 half again, ... until one value is left, then plain xor steps for the remaining 4// offsets. Every level adds the same pair of values as the butterfly does (fp add commutes, so x + y == y + x 5// bitwise), only each lane carries fewer of them: P - 1 + (5 - log2 P) cross-lane moves instead of 5 P. Value j's sum 6// ends in the lanes whose bits 4, 3, .. (MSB first) spell j: tsum_lane<P>(j) is one of them. On AMD the moves are 7// DPP row_xmask / permlanex16 instead of ds_bpermute. 8#pragma once 9 10#include <cuda_runtime.h> // (before the first use of __forceinline__ / __float_as_int: a gfx906 build gets them from its shim) 11 12namespace strata::kernels::s26ts { 13 14#if defined(__HIP_PLATFORM_AMD__) && !defined(STRATA_HIP_GFX906) // (permlanex16 / DPP row_xmask: gfx10+ only) 15__device__ __forceinline__ float xmov16(float v) { 16 return __int_as_float(__builtin_amdgcn_permlanex16(__float_as_int(v), __float_as_int(v), 0x76543210u, 0xfedcba98u, 17 false, false)); 18} 19template <int O> 20__device__ __forceinline__ float xmov(float v) { 21 if constexpr (O == 16) return xmov16(v); 22 else return __int_as_float(__builtin_amdgcn_update_dpp(0, __float_as_int(v), 0x160 + O, 0xf, 0xf, false)); 23} 24#else 25template <int O> 26__device__ __forceinline__ float xmov(float v) { return __shfl_xor_sync(0xffffffffu, v, O); } 27#endif 28 29__host__ __device__ constexpr int pow2_ceil(int t) { return t <= 1 ? 1 : t <= 2 ? 2 : t <= 4 ? 4 : t <= 8 ? 8 : t <= 16 ? 16 : 32; } 30__host__ __device__ constexpr int log2c(int p) { return p <= 1 ? 0 : p == 2 ? 1 : p == 4 ? 2 : p == 8 ? 3 : p == 16 ? 4 : 5; } 31 32template <int O> 33__device__ __forceinline__ float tplain(float x) { 34 if constexpr (O >= 1) { x += xmov<O>(x); return tplain<O / 2>(x); } 35 else return x; 36} 37// v[0..CNT) (CNT a power of two <= 32): this lane's values; returns the sum of value tsum_token<CNT>(lane) 38template <int CNT, int O = 16> 39__device__ __forceinline__ float tsum(float* v, int lane) { 40 if constexpr (CNT > 1) { 41 constexpr int H = CNT / 2; 42 const bool hi = (lane & O) != 0; 43#pragma unroll 44 for (int j = 0; j < H; ++j) { 45 const float send = hi ? v[j] : v[j + H]; 46 const float keep = hi ? v[j + H] : v[j]; 47 v[j] = keep + xmov<O>(send); 48 } 49 return tsum<H, O / 2>(v, lane); 50 } else { 51 return tplain<O>(v[0]); 52 } 53} 54template <int P> 55__device__ __forceinline__ int tsum_token(int lane) { 56 constexpr int L = log2c(P); 57 int k = 0; 58#pragma unroll 59 for (int b = 0; b < L; ++b) k = (k << 1) | ((lane >> (4 - b)) & 1); 60 return k; 61} 62template <int P> 63__device__ __forceinline__ int tsum_lane(int k) { 64 constexpr int L = log2c(P); 65 int lane = 0; 66#pragma unroll 67 for (int b = 0; b < L; ++b) lane |= ((k >> (L - 1 - b)) & 1) << (4 - b); 68 return lane; 69} 70 71} // namespace strata::kernels::s26ts