-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use StaticArrays to compute histogram (#94)
- Loading branch information
Showing
12 changed files
with
105 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using CUDA | ||
using Folds | ||
using FoldsCUDA | ||
using StaticArrays | ||
using Transducers | ||
using Transducers: whencompletebasecase | ||
|
||
# TODO: pretty FLoops syntax | ||
function countints_svector_functional(indices, ::Val{n}; ex = PreferParallel()) where {n} | ||
|
||
function init() | ||
# On initialization, create a `MVector` as a basecase-local | ||
# sub-histogram buffer `b`: | ||
zero(MVector{n,Int}) | ||
end | ||
|
||
function inc!(b, i) | ||
@inbounds b[max(begin, min(i, end))] += 1 | ||
b | ||
end | ||
|
||
function completebasecase(b) | ||
# After basecase computing is done, convert the buffer `b` to an | ||
# immutable value `SVector` to share the value across threads: | ||
SVector(b) | ||
end | ||
|
||
function combine(h, b) | ||
# Cross-thread reduction is simply point-wise addition: | ||
h .+ b | ||
end | ||
|
||
rf = | ||
inc! |> | ||
wheninit(init) |> | ||
whencompletebasecase(completebasecase) |> | ||
whencombine(combine) | ||
|
||
Folds.reduce(rf, indices, ex) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
module TestHistograms | ||
|
||
include("../../../examples/histograms.jl") | ||
using Test | ||
|
||
function test_one_to_ten() | ||
indices = CuVector(1:10) | ||
h = countints_svector_functional(indices, Val(10)) | ||
@test collect(h) == fill(1, 10) | ||
end | ||
|
||
function test_rand() | ||
indices = (floor(Int, x * 10) + 1 for x in CUDA.rand(2^30)) | ||
h = countints_svector_functional(indices, Val(10)) | ||
h = collect(h) | ||
@test h ./ h[1] ≈ fill(1, 10) atol = 0.01 | ||
end | ||
|
||
end # module |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters