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
#include <metal_stdlib>
using namespace metal;
struct Params {
uint count;
float scale;
};
struct DataBuffer {
array<float, 64> values;
};
kernel void main(uint3 global_id [[thread_position_in_grid]],
uint3 local_id [[thread_position_in_threadgroup]],
device Params* params [[buffer(0)]],
device DataBuffer* data [[buffer(1)]]) {
threadgroup float scratch[64];
uint idx = local_id.x;
scratch[idx] = (data.values[idx] * params.scale);
threadgroup_barrier(mem_flags::mem_threadgroup);
if ((idx < 32)) {
scratch[idx] = (scratch[idx] + scratch[(idx + 32)]);
}
threadgroup_barrier(mem_flags::mem_threadgroup);
data.values[idx] = scratch[idx];
}