Skip to content
Closed
Show file tree
Hide file tree
Changes from 14 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
7 changes: 6 additions & 1 deletion src/KernelAbstractions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,10 @@ function unsafe_free! end
# - @groupsize
# - @ndrange
###

function groupsize end
function ndrange end


"""
@groupsize()

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


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


# CPU backend

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

# groupreduce
include("reduce.jl")

end #module
49 changes: 49 additions & 0 deletions src/reduce.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
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
- `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)
quote
$__groupreduce($(esc(:__ctx__)),$(esc(op)), $(esc(val)), $(esc(neutral)), $(esc(typeof(val))))
end
end

@inline function __groupreduce(__ctx__, op, val, neutral, ::Type{T}) 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)

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