|
1 | 1 | @testsuite "broadcasting" (AT, eltypes)->begin
|
2 | 2 | broadcasting(AT, eltypes)
|
3 | 3 | vec3(AT, eltypes)
|
| 4 | + unknown_wrapper(AT, eltypes) |
4 | 5 |
|
5 | 6 | @testset "type instabilities" begin
|
6 | 7 | f(x) = x ? 1.0 : 0
|
@@ -205,3 +206,34 @@ function vec3(AT, eltypes)
|
205 | 206 | @test all(map((a,b)-> all((1,2,3) .≈ (1,2,3)), Array(res2), res2c))
|
206 | 207 | end
|
207 | 208 | end
|
| 209 | + |
| 210 | +# A help struct to test style-based broadcast dispatch with unknown array wrapper. |
| 211 | +# `WrapArray(A)` behaves like `A` during broadcast. But its not a `BroadcastGPUArray`. |
| 212 | +struct WrapArray{T,N,P<:AbstractArray{T,N}} <: AbstractArray{T,N} |
| 213 | + data::P |
| 214 | +end |
| 215 | +Base.@propagate_inbounds Base.getindex(A::WrapArray, i::Integer...) = A.data[i...] |
| 216 | +Base.@propagate_inbounds Base.setindex!(A::WrapArray, v::Any, i::Integer...) = setindex!(A.data, v, i...) |
| 217 | +Base.size(A::WrapArray) = size(A.data) |
| 218 | +# For kernal support |
| 219 | +Adapt.adapt_structure(to, s::WrapArray) = WrapArray(Adapt.adapt(to, s.data)) |
| 220 | +# For broadcast support |
| 221 | +GPUArrays.backend(::Type{WrapArray{T,N,P}}) where {T,N,P} = GPUArrays.backend(P) |
| 222 | +Broadcast.BroadcastStyle(::Type{WrapArray{T,N,P}}) where {T,N,P} = Broadcast.BroadcastStyle(P) |
| 223 | + |
| 224 | +function unknown_wrapper(AT, eltypes) |
| 225 | + for ET in eltypes |
| 226 | + @views @testset "unknown wrapper $ET" begin |
| 227 | + A = AT(rand(ET, 10, 10)) |
| 228 | + WA = WrapArray(A) |
| 229 | + # test for dispatch with src's BroadcastStyle. |
| 230 | + @test Array(WA .+ ET(1)) == Array(A .+ ET(1)) |
| 231 | + @test Array(WA .+ WA) == Array(WA .+ A) == Array(A .+ A) |
| 232 | + @test Array(WA .+ A[:,1]) == Array(A .+ A[:,1]) |
| 233 | + @test Array(WA .+ A[1,:]) == Array(A .+ A[1,:]) |
| 234 | + # test for dispatch with dest's BroadcastStyle. |
| 235 | + WA .= ET(1) |
| 236 | + @test all(isequal(ET(1)), Array(A)) |
| 237 | + end |
| 238 | + end |
| 239 | +end |
0 commit comments