1#version 450
 2
 3#include "types.glsl"
 4#include "sum_rows.glsl"
 5
 6#extension GL_EXT_control_flow_attributes : enable
 7#extension GL_KHR_shader_subgroup_arithmetic : enable
 8#extension GL_KHR_shader_subgroup_basic : enable
 9
10layout(local_size_x_id = 0, local_size_y = 1, local_size_z = 1) in;
11
12layout (binding = 0) readonly buffer A {A_TYPE data_a[];};
13layout (binding = 1) buffer D {D_TYPE data_d[];};
14layout (binding = 2) readonly buffer T {D_TYPE data_t[];};
15
16layout (constant_id = 0) const uint BLOCK_SIZE = 128;
17layout (constant_id = 1) const uint SUBGROUP_SIZE = 32;
18
19#define CEIL_DIV(a, b) (((a) + (b) - 1) / (b))
20
21shared FLOAT_TYPE temp[BLOCK_SIZE / SUBGROUP_SIZE];
22
23void main() {
24    const uint row = gl_WorkGroupID.y;
25    const uint tid = gl_LocalInvocationID.x;
26
27    const uint i03 = fastdiv(row, p.ne0_12mp, p.ne0_12L);
28    const uint i03_offset = i03 * p.ne01*p.ne02;
29    const uint i02 = fastdiv(row - i03_offset, p.ne0_1mp, p.ne0_1L);
30    const uint i01 = row - i03_offset - i02*p.ne01;
31
32    const uint src_idx = get_aoffset() + i01 * p.nb01 + i02 * p.nb02 + i03 * p.nb03;
33    const uint dst_idx = get_doffset() + i01 * p.nb11 + i02 * p.nb12 + i03 * p.nb13;
34
35    const uint col = gl_GlobalInvocationID.x;
36
37    float v = 0;
38    // prefetch value we're adding to
39    if (col < p.n_cols) {
40        v = data_d[dst_idx + col];
41    }
42
43    // compute the sum of all previous blocks
44    uint c = tid;
45    float sum = 0;
46    while (c < gl_WorkGroupID.x) {
47        sum += data_t[c + gl_NumWorkGroups.x * row];
48        c += BLOCK_SIZE;
49    }
50
51    sum = subgroupAdd(sum);
52    if (gl_SubgroupInvocationID == 0) {
53        temp[gl_SubgroupID] = sum;
54    }
55    barrier();
56    sum = 0;
57    [[unroll]] for (uint s = 0; s < BLOCK_SIZE / SUBGROUP_SIZE; ++s) {
58        sum += temp[s];
59    }
60
61    // Add the sum to what the first pass computed
62    if (col < p.n_cols) {
63        data_d[dst_idx + col] = v + sum;
64    }
65}
66