Skip to content
Merged
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: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Strided"
uuid = "5e0ebb24-38b0-5f93-81fe-25c709ecae67"
version = "2.6.3"
version = "2.6.4"
authors = ["Lukas Devos <lukas.devos@ugent.be>", "Maarten Van Damme <maartenvd1994@gmail.com>", "Jutho Haegeman <jutho.haegeman@ugent.be>"]

[deps]
Expand Down
20 changes: 20 additions & 0 deletions ext/StridedGPUArraysExt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,26 @@ function Strided._mapreduce(
return Array(out)[1]
end

# 0-dimensional fast path: bypass @generated kernel
# needs GPU specific extension to avoid scalar indexing error
function Strided._mapreduce_scalar!(
@nospecialize(f), @nospecialize(op), @nospecialize(initop),
arrays::Tuple{GPUStridedView{TO, 0}, Vararg{GPUStridedView{<:Any, 0}}}
) where {TO}
out = arrays[1]
iout = ParentIndex(Strided.offset(out) + 1)
@allowscalar begin
v = f(map(a -> a[ParentIndex(Strided.offset(a) + 1)], Base.tail(arrays))...)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@kshyatt I meant here, since if I understand this correctly this is effectively just first bringing everything to the CPU, and then computing f? I'm not sure it really matters to be honest, since I don't think you ever want to end up in this situation and need it to be performant to begin with

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah I think it doesn't matter either way, this was only being triggered for AMD because I think cuTENSOR bypasses this. But it was getting picked up by one of the amd/planar tests for a previous issue, so there is a way to reach this code.

if op === nothing
out[iout] = v
else
o = initop === nothing ? out[iout] : initop(out[iout])
out[iout] = op(o, v)
end
end
return nothing
end

function Strided._mapreduce_block!(
f, op, initop,
dims::Dims{N},
Expand Down
61 changes: 61 additions & 0 deletions test/gpu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,64 @@ end
@test compare(x -> prod(exp, x), AT, A3)
end
end

@testset "0-dimensional (scalar) StridedView ($AT)" for AT in ATs
@testset for T in (Float32, ComplexF32)
R = fill(rand(T)) # 0-dimensional Array
A = StridedView(AT(R))
@test ndims(A) == 0

# full reductions
@test sum(A) == sum(R)
@test prod(A) == prod(R)
@test mapreduce(abs2, +, A) == mapreduce(abs2, +, R)
@test maximum(abs, A) == maximum(abs, R)
@test minimum(abs, A) == minimum(abs, R)
@test sum(abs2, A) == sum(abs2, R)
@test mapreduce(identity, +, A; init = one(T)) ==
mapreduce(identity, +, R; init = one(T))

# map / map! / copy! / fill!
mapx = map(x -> 2x, A)
GPUArrays.@allowscalar begin
@test mapx[] == 2 * R[]
end
B = StridedView(AT(fill(zero(T))))
map!(x -> x + one(T), B, A)
GPUArrays.@allowscalar begin
@test B[] == collect(R)[] + one(T)
end
copy!(B, A)
GPUArrays.@allowscalar begin
@test B[] == R[]
end
fill!(B, one(T))
GPUArrays.@allowscalar begin
@test B[] == one(T)
end

# offset handling: 0-dim views into a larger parent
Psrc = AT(rand(T, 5))
Pdst = AT(rand(T, 5))
s = sreshape(StridedView(Psrc)[4:4], ())
d = sreshape(StridedView(Pdst)[3:3], ())
GPUArrays.@allowscalar begin
@test sum(s) == Psrc[4]
end
copy!(d, s)
GPUArrays.@allowscalar begin
@test Pdst[3] == Psrc[4]
end

# low-level in-place reduction with a custom initop
Pd = AT(rand(T, 5))
d2 = sreshape(StridedView(Pd)[2:2], ())
GPUArrays.@allowscalar begin
prev = Pd[2]
end
Strided._mapreducedim!(sin, +, identity, (), (d2, A))
GPUArrays.@allowscalar begin
@test Pd[2] == prev + sin(R[])
end
end
end
Loading