okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
5af104b51
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#define DPCT_PROFILING_ENABLED 11#include <sycl/sycl.hpp> 12#include <dpct/dpct.hpp> 13// (before the first use of __forceinline__ / __float_as_int: a gfx906 build 14// gets them from its shim) 15 16namespace strata::kernels::s26ts { 17 18#if defined(__HIP_PLATFORM_AMD__ )&& !defined(STRATA_HIP_GFX906 )// (permlanex16 / DPP row_xmask: gfx10+ only) 19__device__ __forceinline__ float xmov16 (float v ) { 20return __int_as_float (__builtin_amdgcn_permlanex16 (__float_as_int (v ),__float_as_int (v ),0x76543210u ,0xfedcba98u , 21 false, false)); 22} 23template < int O > 24__device__ __forceinline__ float xmov (float v ) { 25if constexpr (O == 16 )return xmov16 (v ); 26else return __int_as_float (__builtin_amdgcn_update_dpp (0 ,__float_as_int (v ),0x160 + O ,0xf ,0xf , false)); 27} 28#else 29template < int O > 30/* 31DPCT1108: '__shfl_xor_sync' was migrated with the experimental feature 32masked sub_group function which may not be supported by all compilers or 33runtimes. You may need to adjust the code. 34*/ 35__dpct_inline__ float xmov (float v ) { 36return dpct::experimental::permute_sub_group_by_xor ( 370xffffffffu , sycl::ext::oneapi::this_work_item::get_sub_group (), 38v ,O ); 39} 40#endif 41 42constexpr int pow2_ceil (int t ) {return t <=1 ?1 :t <=2 ?2 :t <=4 ?4 :t <=8 ?8 :t <=16 ?16 :32 ; } 43constexpr int log2c (int p ) {return p <=1 ?0 :p == 2 ?1 :p == 4 ?2 :p == 8 ?3 :p == 16 ?4 :5 ; } 44 45template < int O > __dpct_inline__ float tplain (float x ) { 46if constexpr (O >=1 ) {x += xmov < O > (x );return tplain < O /2 > (x ); } 47else return x ; 48} 49// v[0..CNT) (CNT a power of two <= 32): this lane's values; returns the sum of value tsum_token<CNT>(lane) 50template < int CNT ,int O = 16 > __dpct_inline__ float tsum (float * v ,int lane ) { 51if constexpr (CNT > 1 ) { 52constexpr int H = CNT /2 ; 53const bool hi = (lane & O )!= 0 ; 54#pragma unroll 55for (int j = 0 ;j < H ;++ j ) { 56const float send = hi ?v [j ] :v [j + H ]; 57const float keep = hi ?v [j + H ] :v [j ]; 58v [j ]= keep + xmov < O > (send ); 59 } 60return tsum < H ,O /2 > (v ,lane ); 61 }else { 62return tplain < O > (v [0 ]); 63 } 64} 65template < int P > __dpct_inline__ int tsum_token (int lane ) { 66constexpr int L = log2c (P ); 67int k = 0 ; 68#pragma unroll 69for (int b = 0 ;b < L ;++ b )k = (k <<1 ) | ((lane >> (4 - b ))& 1 ); 70return k ; 71} 72template < int P > __dpct_inline__ int tsum_lane (int k ) { 73constexpr int L = log2c (P ); 74int lane = 0 ; 75#pragma unroll 76for (int b = 0 ;b < L ;++ b )lane |= ((k >> (L - 1 - b ))& 1 ) << (4 - b ); 77return lane ; 78} 79 80}// namespace strata::kernels::s26ts