-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
groupreduction and subgroupreduction #421
Closed
Closed
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
cc510ac
kernel config struct
brabreda 1c1e459
group- and warpreduce
brabreda dd3a0ca
fixes
brabreda 546e8c9
add warpsize to config
brabreda 3602808
adding & subgroupreduce
brabreda 42a7960
deps
brabreda c96a24a
added docs & removed part for GPUArrays
brabreda d2d65be
added docs & tests
brabreda 128a5f0
Remove deps for PR
brabreda b899685
Ensure NTuple index functions are inlined (#414)
vchuravy 1cdb6d6
manifest.toml, reset project.toml
brabreda 1fea4cc
Merge branch 'JuliaGPU:main' into feature/reduction
brabreda 41356d3
move groupreduce with subgroups to backends
brabreda 88662f8
Merge branch 'feature/reduction' of https://github.com/brabreda/Kerne…
brabreda 45844ce
cleanup and wire-up tests
vchuravy c5dc356
fixup use of groupsize
brabreda e2c8f84
Merge branch 'feature/reduction' of https://github.com/brabreda/Kerne…
brabreda 700d5f2
Merge branch 'JuliaGPU:main' into feature/reduction
brabreda File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
export @groupreduce | ||
|
||
""" | ||
@groupreduce(op, val, neutral, use_subgroups) | ||
|
||
Reduce values across a block | ||
- `op`: the operator of the reduction | ||
- `val`: value that each thread contibutes to the values that need to be reduced | ||
- `neutral`: value of the operator, so that `op(netural, neutral) = neutral`` | ||
- `groupsize` (optional): specify the groupszie. If not specified @groupsize is used but this is generally slower. | ||
""" | ||
macro groupreduce(op, val, neutral) | ||
quote | ||
$__groupreduce($(esc(:__ctx__)),$(esc(op)), $(esc(val)), $(esc(neutral)), Val(prod(groupsize($(esc(:__ctx__)))))) | ||
end | ||
end | ||
|
||
macro groupreduce(op, val, neutral, groupsize) | ||
quote | ||
$__groupreduce($(esc(:__ctx__)),$(esc(op)), $(esc(val)), $(esc(neutral)), $(esc(groupsize))) | ||
end | ||
end | ||
|
||
@inline function __groupreduce(__ctx__, op, val::T, neutral, ::Val{GROUPSIZE}) where {T, GROUPSIZE} | ||
idx_in_group = @index(Local) | ||
|
||
localmem = @localmem(T, GROUPSIZE) | ||
|
||
@inbounds localmem[idx_in_group] = val | ||
|
||
# perform the reduction | ||
d = 1 | ||
while d < GROUPSIZE | ||
@synchronize() | ||
index = 2 * d * (idx_in_group-1) + 1 | ||
@inbounds if index <= GROUPSIZE | ||
other_val = if index + d <= GROUPSIZE | ||
localmem[index+d] | ||
else | ||
neutral | ||
end | ||
localmem[index] = op(localmem[index], other_val) | ||
end | ||
d *= 2 | ||
end | ||
|
||
# load the final value on the first thread | ||
if idx_in_group == 1 | ||
val = @inbounds localmem[idx_in_group] | ||
end | ||
|
||
return val | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
using KernelAbstractions, Test | ||
|
||
@kernel function reduce(a, b, op, neutral) | ||
I = @index(Global) | ||
gI = @index(Group, Linear) | ||
val = a[I] | ||
|
||
val = @groupreduce(op, val, neutral) | ||
|
||
b[gI] = val | ||
end | ||
|
||
function reduce_testset(backend, ArrayT) | ||
@testset "groupreduce one group" begin | ||
@testset for op in (+, *, max, min) | ||
@testset for type in (Int32, Float32, Float64) | ||
@test test_groupreduce(backend, ArrayT, op, type, op(neutral), 8) | ||
@test test_groupreduce(backend, ArrayT, op, type, op(neutral), 16) | ||
@test test_groupreduce(backend, ArrayT, op, type, op(neutral), 32) | ||
@test test_groupreduce(backend, ArrayT, op, type, op(neutral), 64) | ||
end | ||
end | ||
end | ||
end | ||
|
||
function test_groupreduce(backend, ArrayT, op, type, neutral, N) | ||
a = rand(type, N) | ||
b = ArrayT(a) | ||
|
||
gsz = 64 | ||
ngroups = ceil(N/gsz) | ||
c = similar(b, ngroups) | ||
kernel = reduce(backend, (gsz,)) | ||
kernel(a, c, op, neutral) | ||
|
||
expected = mapreduce(x->x^2, +, a) | ||
actual = c[1] | ||
return expected = actual | ||
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#262
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Workaround?