Skip to content

Switch over to AK implementation for reductions #607

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

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ uuid = "0c68f7d7-f131-5f86-a1c3-88cf8149b2d7"
version = "11.2.3"

[deps]
AcceleratedKernels = "6a4ca0a5-0e36-4168-a932-d9be78d558f1"
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
GPUArraysCore = "46192b85-c4d5-4398-a991-12ede77f4527"
KernelAbstractions = "63c18a36-062a-441e-b654-da1e3ab1ce7c"
Expand All @@ -22,6 +23,7 @@ JLD2 = "033835bb-8acc-5ee8-8aae-3f567f8a3819"
JLD2Ext = "JLD2"

[compat]
AcceleratedKernels = "0.4"
Adapt = "4.0"
GPUArraysCore = "= 0.2.0"
JLD2 = "0.4, 0.5"
Expand Down
1 change: 1 addition & 0 deletions src/GPUArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ using Reexport
@reexport using GPUArraysCore

using KernelAbstractions
import AcceleratedKernels as AK

# device functionality
include("device/abstractarray.jl")
Expand Down
2 changes: 1 addition & 1 deletion src/host/indexing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ function findminmax(binop, A::AnyGPUArray; init, dims)
(x, i), (y, j) = t1, t2

binop(x, y) && return t2
x == y && return (x, min(i, j))
isequal(x, y) && return (x, min(i, j))
return t1
end

Expand Down
36 changes: 26 additions & 10 deletions src/host/mapreduce.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ neutral_element(::typeof(Base._extrema_rf), ::Type{<:NTuple{2,T}}) where {T} = t

# resolve ambiguities
Base.mapreduce(f, op, A::AnyGPUArray, As::AbstractArrayOrBroadcasted...;
dims=:, init=nothing) = _mapreduce(f, op, A, As...; dims=dims, init=init)
dims=:, init=nothing) = _mapreduce(f, op, A, As...; dims, init)
Base.mapreduce(f, op, A::Broadcast.Broadcasted{<:AbstractGPUArrayStyle}, As::AbstractArrayOrBroadcasted...;
dims=:, init=nothing) = _mapreduce(f, op, A, As...; dims=dims, init=init)
dims=:, init=nothing) = _mapreduce(f, op, A, As...; dims, init)

function _mapreduce(f::F, op::OP, As::Vararg{Any,N}; dims::D, init) where {F,OP,N,D}
# figure out the destination container type by looking at the initializer element,
Expand All @@ -40,7 +40,7 @@ function _mapreduce(f::F, op::OP, As::Vararg{Any,N}; dims::D, init) where {F,OP,
(ET === Union{} || ET === Any) &&
error("mapreduce cannot figure the output element type, please pass an explicit init value")

init = neutral_element(op, ET)
init = AK.neutral_element(op, ET)
else
ET = typeof(init)
end
Expand All @@ -66,9 +66,25 @@ function _mapreduce(f::F, op::OP, As::Vararg{Any,N}; dims::D, init) where {F,OP,
end

# allocate an output container
block_size = 256 # Hard-code AK default to prevent mismatches
sz = size(A)
red = ntuple(i->(dims==Colon() || i in dims) ? 1 : sz[i], length(sz))
R = similar(A, ET, red)
R = if dims isa Colon
num_per_block = 2 * block_size
blocks = (prod(sz) + num_per_block - 1) ÷ num_per_block
similar(A, ET, 2 * blocks)
else
similar(A, ET, red)
end

# Use AcceleratedKernels if possible
if dims isa Colon || dims isa Integer
return AK.mapreduce(f, op, Base.materialize(A), get_backend(R);
block_size, init,
neutral=init,
dims=dims isa Colon ? nothing : dims,
temp = R)
end

# perform the reduction
if prod(sz) == 0
Expand All @@ -85,14 +101,14 @@ function _mapreduce(f::F, op::OP, As::Vararg{Any,N}; dims::D, init) where {F,OP,
end
end

Base.any(A::AnyGPUArray{Bool}) = mapreduce(identity, |, A)
Base.all(A::AnyGPUArray{Bool}) = mapreduce(identity, &, A)
Base.any(A::AnyGPUArray{Bool}) = AK.any(identity, A)
Base.all(A::AnyGPUArray{Bool}) = AK.all(identity, A)

Base.any(f::Function, A::AnyGPUArray) = mapreduce(f, |, A)
Base.all(f::Function, A::AnyGPUArray) = mapreduce(f, &, A)
Base.any(f::Function, A::AnyGPUArray) = AK.any(f, A)
Base.all(f::Function, A::AnyGPUArray) = AK.all(f, A)

Base.count(pred::Function, A::AnyGPUArray; dims=:, init=0) =
mapreduce(pred, Base.add_sum, A; init=init, dims=dims)
AK.count(pred, A; init, dims=dims isa Colon ? nothing : dims)

# avoid calling into `initarray!`
for (fname, op) in [(:sum, :(Base.add_sum)), (:prod, :(Base.mul_prod)),
Expand All @@ -101,7 +117,7 @@ for (fname, op) in [(:sum, :(Base.add_sum)), (:prod, :(Base.mul_prod)),
fname! = Symbol(fname, '!')
@eval begin
Base.$(fname!)(f::Function, r::AnyGPUArray, A::AnyGPUArray{T}) where T =
GPUArrays.mapreducedim!(f, $(op), r, A; init=neutral_element($(op), T))
GPUArrays.mapreducedim!(f, $(op), r, A; init=AK.neutral_element($(op), T))
end
end

Expand Down
Loading