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
5 changes: 5 additions & 0 deletions src/host/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ using Base.Broadcast

using Base.Broadcast: BroadcastStyle, Broadcasted, AbstractArrayStyle, instantiate

# split `ComposedFunction` so the kernel closure doesn't hit its kwarg-accepting
# call, whose kwsorter dispatch GPUCompiler can't resolve statically.
@inline Broadcast.broadcasted(S::AbstractGPUArrayStyle, c::ComposedFunction, args...) =
Broadcast.broadcasted(S, c.outer, Broadcast.broadcasted(S, c.inner, args...))

# but make sure we don't dispatch to the optimized copy method that directly indexes
function Broadcast.copy(bc::Broadcasted{<:AbstractGPUArrayStyle{0}})
ElType = Broadcast.combine_eltypes(bc.f, bc.args)
Expand Down
19 changes: 19 additions & 0 deletions test/testsuite/broadcasting.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
broadcasting(AT, eltypes)
vec3(AT, eltypes)
unknown_wrapper(AT, eltypes)
composed_function(AT, eltypes)
end

test_idx(idx, A::AbstractArray{T}) where T = A[idx] * T(2)
Expand Down Expand Up @@ -228,3 +229,21 @@ function unknown_wrapper(AT, eltypes)
end
end
end

function composed_function(AT, eltypes)
sq(x) = x*x
for ET in eltypes
@testset "ComposedFunction $ET" begin
a = AT(rand(ET, 8))
b = AT(rand(ET, 8))
ca, cb = Array(a), Array(b)

@test Array(broadcast(sq ∘ (+), a, b)) ≈ (ca .+ cb).^2
@test Array((sq ∘ (+)).(a, b)) ≈ (ca .+ cb).^2
@test Array((sq ∘ sq ∘ (+)).(a, b)) ≈ ((ca .+ cb).^2).^2
@test Array((sq ∘ identity).(a)) ≈ ca.^2
@test Array((sq ∘ (+)).(a, Ref(ET(2)))) ≈ (ca .+ ET(2)).^2
@test Array((identity ∘ (-)).(a, b)) ≈ ca .- cb
end
end
end