Skip to content

Commit 9015152

Browse files
committed
first stab at #53
1 parent a2a58e2 commit 9015152

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

src/abstractarray.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,3 +504,24 @@ function Base.reshape(a::AbstractAccArray{T}, dims::NTuple{N,Int}) where T where
504504
end
505505
unsafe_reinterpret(T, a, dims)
506506
end
507+
508+
509+
function mapreducedim_kernel(state, f, op, R::AbstractArray{T1, N}, A::AbstractArray{T, N}, slice_size, sizeA, dim) where {T1, T, N}
510+
ilin = Cuint(linear_index(R, state))
511+
accum = zero(T1)
512+
@inbounds for i = Cuint(1):slice_size
513+
idx = ifelse.(ntuple(Cuint, Val{N}) .== dim, i, ilin)
514+
i2d = gpu_sub2ind(sizeA, idx)
515+
accum = op(accum, f(A[i2d]))
516+
end
517+
R[ilin] = accum
518+
return
519+
end
520+
function Base._mapreducedim!(f, op, R::AbstractAccArray, A::AbstractAccArray)
521+
sizeR = size(R)
522+
@assert count(x-> x == 1, sizeR) == (ndims(R) - 1) "Not implemented"
523+
dim = findfirst(x-> x == 1, sizeR)
524+
slice_size = size(A, dim)
525+
gpu_call(mapreducedim_kernel, R, (f, op, R, A, Cuint(slice_size), Cuint.(size(A)), Cuint(dim)))
526+
return R
527+
end

test/shared.jl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,23 @@ end
153153
gpu_call(ntuple_test, result, (result, Val{3}()))
154154
@test result[1] == (77, 2*77, 3*77)
155155
end
156+
157+
158+
@allbackends "mapreduce" backend begin
159+
if backend != :opencl
160+
y = rand(Float32, 40, 40)
161+
x = GPUArray(y)
162+
@test sum(y, 2) Array(sum(x, 2))
163+
@test sum(y, 1) Array(sum(x, 1))
164+
165+
y = rand(Float32, 40, 10)
166+
x = GPUArray(y)
167+
@test sum(y, 2) Array(sum(x, 2))
168+
@test sum(y, 1) Array(sum(x, 1))
169+
170+
y = rand(Float32, 10, 40)
171+
x = GPUArray(y)
172+
@test sum(y, 2) Array(sum(x, 2))
173+
@test sum(y, 1) Array(sum(x, 1))
174+
end
175+
end

0 commit comments

Comments
 (0)