Skip to content
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
wants to merge 18 commits into from
Closed
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/KernelAbstractions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,31 @@ function unsafe_free! end
# - @groupsize
# - @ndrange
###

function groupsize end
function ndrange end

"""
@subgroupsize()

returns the GPUs subgroupsize.
"""
macro subgroupsize()
quote
$__subgroupsize()
end
end
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"""
@subgroupsize()
returns the GPUs subgroupsize.
"""
macro subgroupsize()
quote
$__subgroupsize()
end
end


"""
@subgroupsize()

returns the GPUs subgroupsize.
"""
macro subgroupsize()
quote
$__subgroupsize()
end
end

"""
@groupsize()

Expand Down Expand Up @@ -657,6 +678,10 @@ function __synchronize()
error("@synchronize used outside kernel or not captured")
end

function __subgroupsize()
error("@subgroupsize used outside kernel or not captured")
end

@generated function __print(items...)
str = ""
args = []
Expand Down Expand Up @@ -700,6 +725,7 @@ end
@inbounds A[I] = B[I]
end


# CPU backend

include("cpu.jl")
Expand All @@ -726,4 +752,7 @@ end
end
end

# group- and subgroupreduce
include("reduce.jl")

end #module
98 changes: 98 additions & 0 deletions src/reduce.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
export @groupreduce, @subgroupreduce

"""

@subgroupreduce(op, val)

reduce values across a subgroup. This operation is only supported if subgroups are supported by the backend.
"""
macro subgroupreduce(op, val)
quote
$__subgroupreduce($(esc(op)),$(esc(val)))
end
end

function __subgroupreduce(op, val)
error("@subgroupreduce used outside kernel, not captured, or not supported")
end

"""

@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
- `netral`: value of the operator, so that `op(netural, neutral) = neutral``
- `use_subgroups`: make use of the subgroupreduction of the groupreduction
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see the value of having a common implementation.

So I would define:

@reduce(op, val, neutral)
__reduce(op, val, neutral)

And then maybe:

__subgroupreduce & __subgroupsize (No @ version)
__can_subgroup_reduce(T) = false

And then we could define:

__reduce(__ctx___, op, val, neutral, ::Val{true})
__reduce(__ctx___, op, val, neutral, ::Val{false})

as you have here.

function __reduce(op, val::T, neutral::T) where T
     __reduce(op, val, neutral, Val(__can_subgroup_reduce(T)))
end

"""
macro groupreduce(op, val, neutral, use_subgroups)
quote
$__groupreduce($(esc(:__ctx__)),$(esc(op)), $(esc(val)), $(esc(neutral)), $(esc(typeof(val))), Val(use_subgroups))
end
end

@inline function __groupreduce(__ctx__, op, val, neutral, ::Type{T}, ::Val{true}) where {T}
idx_in_group = @index(Local)
groupsize = @groupsize()[1]
subgroupsize = @subgroupsize()

localmem = @localmem(T, subgroupsize)

idx_subgroup, idx_in_subgroup = fldmod1(idx_in_group, subgroupsize)

# first subgroup reduction
val = @subgroupreduce(op, val)

# store partial results in local memory
if idx_in_subgroup == 1
@inbounds localmem[idx_in_subgroup] = val
end

@synchronize()

val = if idx_in_subgroup <= fld1(groupsize, subgroupsize)
@inbounds localmem[idx_in_subgroup]
else
neutral
end

# second subgroup reduction to reduce partial results
if idx_in_subgroup == 1
val = @subgroupreduce(op, val)
end

return val
end

@inline function __groupreduce(__ctx__, op, val, neutral, ::Type{T}, ::Val{false}) where {T}
idx_in_group = @index(Local)
groupsize = @groupsize()[1]

localmem = @localmem(T, groupsize)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see if we can do a subgroupreduce the memory we need here is much reduced.


@inbounds localmem[idx_in_group] = val

# perform the reduction
d = 1
while d < groupsize
@synchronize()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Workaround?

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
39 changes: 39 additions & 0 deletions test/reduce.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using KernelAbstractions, Test




@kernel function reduce(a, b, op, neutral)
idx_in_group = @index(Local)

val = a[idx_in_group]

val = @groupreduce(op, val, netral, false)

b[1] = val
end

function(backend, ArrayT)
@testset "groupreduce one group" begin
@testset for op in (+,*,max,min)
@testset for type in (Int32, Float32, Float64)
@test test_1group_groupreduce(backend, ArrayT ,op, type, op(neutral))
end
end
end
end

function test_1group_groupreduce(backend,ArrayT, op, type, neutral)
a = rand(type, 32)
b = ArrayT(a)

c = similar(b,1)
reduce(a, c, op, neutral)

expected = mapreduce(x->x^2, +, a)
actual = c[1]
return expected = actual
end