GPU: map on the GPU
● Advanced3 min
list.map(f) runs on the GPU when f is a gpu fn, the items are 32-bit numbers, and the list is long enough to be worth it.
A million pixels, each through 100 steps. On a machine with a GPU, this map runs there:
gpu fn glow(x: Float32, gain: Float32) -> Float32 {
let v = x
for _ in 0..100 {
v = v * 0.99 + gain
}
v
}
fn main() {
let pixels: [Float32] = []
for i in 0..1_000_000 {
pixels.push(Float32(i % 256) / 255.0)
}
const lit = pixels.map(p => glow(p, 0.25))
print(lit.len())
print(lit.all(v => 15.8 < v < 16.3))
}kavach run --native million.kav printed:
kavach run --native million.kav
1000000true
Only a compiled program uses the GPU: kavach build or kavach run --native, as here. Plain kavach run interprets the program, so the same map runs on the CPU, with the same results and much more slowly, whatever KAVACH_GPU says.
The lambda can capture values from outside, such as gain, as long as they are one of the four GPU types. The program checks its own results instead of printing a million numbers.