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
36 changes: 20 additions & 16 deletions src/host/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,11 +94,15 @@ function Base.map(f, x1::AnyGPUArray, xrest::Union{AnyGPUArray, AbstractRange}..
return Broadcast.broadcast_preserving_zero_d(f, xs...)
end

# if not, treat them as iterators
indices = LinearIndices.(xs)
common_length = minimum(length.(indices))
# if not, follow Base, which iterates the inputs together like `zip` does: multi-
# dimensional inputs of the same dimensionality must have the same size, and otherwise
# the inputs are traversed in memory order up to the shortest one, producing a vector.
zs = zip(xs...)
if Base.IteratorSize(zs) isa Base.HasShape
axes(zs) # throws the same DimensionMismatch as Base
end
common_length = minimum(length, xs)

# construct a broadcast to figure out the destination container
ElType = Broadcast.combine_eltypes(f, xs)
if ElType == Union{} || !Base.allocatedinline(ElType)
# see `broadcast`
Expand All @@ -109,27 +113,27 @@ function Base.map(f, x1::AnyGPUArray, xrest::Union{AnyGPUArray, AbstractRange}..
return map!(f, dest, xs...)
end

# the i-th element in memory order; linear indices need not start at 1
@inline nth_element(x, i) = @inbounds x[firstindex(x) + i - 1]

function Base.map!(f, dest::AnyGPUArray, x::AbstractArray, xs::AbstractArray...)
# custom broadcast, ignoring the container size mismatches
# (avoids the reshape + view that our mapreduce impl has to do)
indices = LinearIndices.((dest, x, xs...))
common_length = minimum(length.(indices))
# like Base, pair up elements in memory order regardless of the containers' shapes,
# stopping at the shortest one (destination included)
common_length = minimum(length, (dest, x, xs...))
common_length==0 && return dest

bc = Broadcast.instantiate(Broadcast.broadcasted(f, x, xs...))
if bc isa Broadcast.Broadcasted
bc = Broadcast.preprocess(dest, bc)
end
srcs = map(src -> Broadcast.broadcast_unalias(dest, src), (x, xs...))

@kernel function map_kernel(dest, bc)
@kernel function map_kernel(f, dest, srcs)
i = @index(Global, Linear)
I = CartesianIndices(axes(bc))[i]
@inbounds dest[i] = bc[I]
# keep `f` itself out of `@inbounds`
val = f(map(src -> nth_element(src, i), srcs)...)
@inbounds dest[firstindex(dest) + i - 1] = val
end

kernel = map_kernel(get_backend(dest))
config = KernelAbstractions.launch_config(kernel, common_length, nothing)
kernel(dest, bc; ndrange = config[1], workgroupsize = config[2])
kernel(f, dest, srcs; ndrange = config[1], workgroupsize = config[2])

if eltype(dest) <: BrokenBroadcast
throw(ArgumentError("Map operation resulting in $(eltype(eltype(dest))) is not GPU compatible"))
Expand Down
34 changes: 34 additions & 0 deletions test/testsuite/broadcasting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,28 @@ function broadcasting(AT, eltypes)
@test compare(AT, rand(ET, 0), rand(ET, 0)) do x, y
map!(+, x, y)
end
# elements are paired in memory order, up to the shortest container
@test compare(AT, rand(ET, 4), rand(ET, 2, 2), 1:4) do x, y, z
map!(+, x, y, z)
end
@test compare(AT, rand(ET, 2, 2), rand(ET, 4), rand(ET, 3, 2)) do x, y, z
map!(+, x, y, z)
end
@test compare(AT, rand(ET, 3), rand(ET, 4), rand(ET, 2, 2)) do x, y, z
map!(+, x, y, z)
end
# linear indices that do not start at 1
@test compare(AT, rand(ET, 4), Base.IdentityUnitRange(0:3)) do x, y
map!(+, x, x, y)
end
# overlapping source and destination, large enough for a race to show
# (Base's `map!` does not unalias, so this only holds for GPU arrays)
if AT <: AbstractGPUArray
x = AT(rand(ET, 2^20))
y = Array(x)
map!(identity, view(x, :), view(x, reverse(axes(x, 1))))
@test Array(x) == reverse(y)
end
end

@testset "map $ET" begin
Expand All @@ -191,6 +213,18 @@ function broadcasting(AT, eltypes)
@test compare(AT, rand(ET, 2), 1:2) do x, y
map(+, x, y)
end
# issue #882: inputs of different shapes are paired in memory order, as by `zip`
@test compare(AT, rand(ET, 1, 4), rand(ET, 4)) do x, y
map(+, x, y)
end
@test compare(AT, rand(ET, 2, 2), 1:4) do x, y
map(+, x, y)
end
@test compare(AT, rand(ET, 2, 2), rand(ET, 2, 1), rand(ET, 3)) do x, y, z
map(+, x, y, z)
end
# ... but like Base, inputs of the same dimensionality must have the same size
@test_throws DimensionMismatch map(+, AT(rand(ET, 2, 2)), AT(rand(ET, 2, 1)))
############
# issue #598
@test compare(AT, rand(ET, ()), rand(ET, ())) do x, y
Expand Down
Loading