From 971fea6e2e117af0e2d5c7a1eabd6a10e20e2fce Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Wed, 2 Dec 2026 18:09:12 +0100 Subject: [PATCH] Pair up `map` arguments in memory order, like Base `map` and `map!` on GPU arrays with differently shaped arguments broadcast them and kept the first results. That only agrees with Base when broadcasting happens to pair the same elements: `map(+, a_1x4, b_4)` added `a[1,1]` to every element of `b`, `map(min, a_2x2, b_2x1)` returned two elements where Base throws a `DimensionMismatch`, and mapping over a matrix and a range of the same length, or into a destination of another shape, threw. `map!` now indexes every container by its n-th linear index, stopping at the shortest one, and `map` uses `zip`'s shape rules to decide between Base's `DimensionMismatch` and a vector of the shortest length. Arguments of equal size still go through `broadcast`. Fixes #882 --- src/host/broadcast.jl | 36 +++++++++++++++++++--------------- test/testsuite/broadcasting.jl | 34 ++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 16 deletions(-) diff --git a/src/host/broadcast.jl b/src/host/broadcast.jl index a1e738189..7b7daaf35 100644 --- a/src/host/broadcast.jl +++ b/src/host/broadcast.jl @@ -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` @@ -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")) diff --git a/test/testsuite/broadcasting.jl b/test/testsuite/broadcasting.jl index d6cb67597..d00864d9a 100644 --- a/test/testsuite/broadcasting.jl +++ b/test/testsuite/broadcasting.jl @@ -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 @@ -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