-
Notifications
You must be signed in to change notification settings - Fork 79
Introducing @reduce for group level reduction #379
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
Changes from 5 commits
fbc0ced
899bc98
a34ac1a
66163f4
ab3d6d1
db024ed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -323,6 +323,8 @@ import CUDA: @device_override | |
| import KernelAbstractions: CompilerMetadata, DynamicCheck, LinearIndices | ||
| import KernelAbstractions: __index_Local_Linear, __index_Group_Linear, __index_Global_Linear, __index_Local_Cartesian, __index_Group_Cartesian, __index_Global_Cartesian, __validindex, __print | ||
| import KernelAbstractions: mkcontext, expand, __iterspace, __ndrange, __dynamic_checkbounds | ||
| import KernelAbstractions: __reduce | ||
|
|
||
|
|
||
| function mkcontext(kernel::Kernel{<:CUDADevice}, _ndrange, iterspace) | ||
| CompilerMetadata{KernelAbstractions.ndrange(kernel), DynamicCheck}(_ndrange, iterspace) | ||
|
|
@@ -407,4 +409,40 @@ Adapt.adapt_storage(to::ConstAdaptor, a::CUDA.CuDeviceArray) = Base.Experimental | |
| # Argument conversion | ||
| KernelAbstractions.argconvert(k::Kernel{<:CUDADevice}, arg) = CUDA.cudaconvert(arg) | ||
|
|
||
| # TODO: make variable block size possible | ||
| # TODO: figure out where to place this | ||
| # reduction functionality for a group | ||
| @device_override @inline function __reduce(__ctx__ , op, val, neutral, ::Type{T}) where T | ||
| threads = KernelAbstractions.@groupsize()[1] | ||
| threadIdx = KernelAbstractions.@index(Local) | ||
|
|
||
| # shared mem for a complete reduction | ||
| shared = KernelAbstractions.@localmem(T, 1024) | ||
| @inbounds shared[threadIdx] = val | ||
|
|
||
| # perform the reduction | ||
| d = 1 | ||
| while d < threads | ||
| KernelAbstractions.@synchronize() | ||
|
||
| index = 2 * d * (threadIdx-1) + 1 | ||
| @inbounds if index <= threads | ||
| other_val = if index + d <= threads | ||
| shared[index+d] | ||
| else | ||
| neutral | ||
| end | ||
| shared[index] = op(shared[index], other_val) | ||
| end | ||
| d *= 2 | ||
| end | ||
|
|
||
| # load the final value on the first thread | ||
| if threadIdx == 1 | ||
| val = @inbounds shared[threadIdx] | ||
| end | ||
| # every thread will return the reduced value of the group | ||
| return val | ||
| end | ||
|
|
||
| end | ||
|
|
||
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.
Maybe this is the moment we need dynamic shared memory support?
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.
x-ref: #11