-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkernel_histogram.zig
More file actions
36 lines (33 loc) · 1.18 KB
/
kernel_histogram.zig
File metadata and controls
36 lines (33 loc) · 1.18 KB
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
28
29
30
31
32
33
34
35
36
// kernels/histogram.zig — GPU histogram kernel using atomics
//
// Features: atomicAdd, grid-stride loop, integer operations
const cuda = @import("zcuda_kernel");
/// Compute histogram of byte values (256 bins).
/// Each thread processes multiple elements via grid-stride loop and
/// atomically increments the corresponding bin.
export fn histogram256(
data: [*]const u32,
bins: [*]u32,
n: u32,
) callconv(.kernel) void {
var iter = cuda.types.gridStrideLoop(n);
while (iter.next()) |i| {
// Extract each byte from the u32 and increment its bin
const val = data[i];
_ = cuda.atomicAdd(&bins[val & 0xFF], @as(u32, 1));
_ = cuda.atomicAdd(&bins[(val >> 8) & 0xFF], @as(u32, 1));
_ = cuda.atomicAdd(&bins[(val >> 16) & 0xFF], @as(u32, 1));
_ = cuda.atomicAdd(&bins[(val >> 24) & 0xFF], @as(u32, 1));
}
}
/// Simple histogram — each element is treated as a bin index directly
export fn histogramSimple(
indices: [*]const u32,
bins: [*]u32,
n: u32,
) callconv(.kernel) void {
var iter = cuda.types.gridStrideLoop(n);
while (iter.next()) |i| {
_ = cuda.atomicAdd(&bins[indices[i]], @as(u32, 1));
}
}