Skip to content

Instantly share code, notes, and snippets.

@athas
Created March 9, 2019 16:26
Show Gist options
  • Save athas/e0bad36bb30f8f791c7ed36bd591074b to your computer and use it in GitHub Desktop.
Save athas/e0bad36bb30f8f791c7ed36bd591074b to your computer and use it in GitHub Desktop.
type pixel = { z: f32
, colour: i32
}
let closest (a: pixel) (b: pixel): pixel =
if a.z < b.z
then a
else b
let raster_onepass (w: i32) (pixel_pos: []i32) (pixels: []pixel) =
reduce_by_index (replicate w {z=f32.inf, colour=0})
closest {z=f32.inf, colour=0}
pixel_pos pixels
|> map (.colour)
let raster_twopass (w: i32) (pixel_pos: []i32) (pixels: []pixel) =
let depthmap = reduce_by_index (replicate w f32.inf)
f32.min f32.inf
pixel_pos (map (.z) pixels)
in scatter (replicate w 0)
(map2 (\i p -> if p.z == unsafe depthmap[i] then i else -1)
pixel_pos pixels)
(map (.colour) pixels)
-- ==
-- entry: test_raster_onepass test_raster_twopass
-- random input { 1000000 [5000]i32 [5000]f32 }
-- random input { 1000000 [50000]i32 [50000]f32 }
-- random input { 1000000 [500000]i32 [500000]f32 }
-- random input { 100000 [5000000]i32 [5000000]f32 }
entry test_raster_onepass (w: i32) (is: []i32) (zs: []f32) =
raster_onepass w (map (%w) is) (map2 (\z colour -> {z, colour}) zs is)
entry test_raster_twopass (w: i32) (is: []i32) (zs: []f32) =
raster_twopass w (map (%w) is) (map2 (\z colour -> {z, colour}) zs is)
-- $ futhark bench raster.fut --backend=opencl
-- Compiling raster.fut...
-- Results for raster.fut:test_raster_onepass:
-- dataset 1000000i32 [5000]i32 [5000]f32: 344.00μs (avg. of 10 runs; RSD: 0.03)
-- dataset 1000000i32 [50000]i32 [50000]f32: 453.50μs (avg. of 10 runs; RSD: 0.02)
-- dataset 1000000i32 [500000]i32 [500000]f32: 1534.50μs (avg. of 10 runs; RSD: 0.29)
-- dataset 100000i32 [5000000]i32 [5000000]f32: 4425.30μs (avg. of 10 runs; RSD: 0.01)
-- Results for raster.fut:test_raster_twopass:
-- dataset 1000000i32 [5000]i32 [5000]f32: 231.90μs (avg. of 10 runs; RSD: 0.04)
-- dataset 1000000i32 [50000]i32 [50000]f32: 332.50μs (avg. of 10 runs; RSD: 0.03)
-- dataset 1000000i32 [500000]i32 [500000]f32: 1267.60μs (avg. of 10 runs; RSD: 0.29)
-- dataset 100000i32 [5000000]i32 [5000000]f32: 2893.70μs (avg. of 10 runs; RSD: 0.03)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment