okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge branch 'f-1402-y' into f-14025af104b51

main
3.4 KiB80 linesraw
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) {
20    return __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) {
25    if constexpr (O == 16) return xmov16(v);
26    else 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) {
36    return dpct::experimental::permute_sub_group_by_xor(
37               0xffffffffu, sycl::ext::oneapi::this_work_item::get_sub_group(),
38               v, 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) {
46    if constexpr (O >= 1) { x += xmov<O>(x); return tplain<O / 2>(x); }
47    else 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) {
51    if constexpr (CNT > 1) {
52        constexpr int H = CNT / 2;
53        const bool hi = (lane & O) != 0;
54#pragma unroll
55        for (int j = 0; j < H; ++j) {
56            const float send = hi ? v[j] : v[j + H];
57            const float keep = hi ? v[j + H] : v[j];
58            v[j] = keep + xmov<O>(send);
59        }
60        return tsum<H, O / 2>(v, lane);
61    } else {
62        return tplain<O>(v[0]);
63    }
64}
65template <int P> __dpct_inline__ int tsum_token(int lane) {
66    constexpr int L = log2c(P);
67    int k = 0;
68#pragma unroll
69    for (int b = 0; b < L; ++b) k = (k << 1) | ((lane >> (4 - b)) & 1);
70    return k;
71}
72template <int P> __dpct_inline__ int tsum_lane(int k) {
73    constexpr int L = log2c(P);
74    int lane = 0;
75#pragma unroll
76    for (int b = 0; b < L; ++b) lane |= ((k >> (L - 1 - b)) & 1) << (4 - b);
77    return lane;
78}
79
80}  // namespace strata::kernels::s26ts