|
| 1 | +module EnzymeTestUtilsGPUArraysCoreExt |
| 2 | + |
| 3 | +using GPUArraysCore |
| 4 | +using EnzymeTestUtils |
| 5 | +using Enzyme |
| 6 | + |
| 7 | +function EnzymeTestUtils.acopyto!(dst, src::AbstractGPUArray) |
| 8 | + temp = Array{eltype(src)}(undef, size(src)) |
| 9 | + Base.copyto!(temp, src) |
| 10 | + EnzymeTestUtils.acopyto!(dst, temp) |
| 11 | +end |
| 12 | + |
| 13 | +# basic containers: loop over defined elements, recursively converting them to vectors |
| 14 | +function EnzymeTestUtils.to_vec(x::AbstractGPUArray{<:EnzymeTestUtils.ElementType}, seen_vecs::EnzymeTestUtils.AliasDict) |
| 15 | + has_seen = haskey(seen_vecs, x) |
| 16 | + is_const = Enzyme.Compiler.guaranteed_const(Core.Typeof(x)) |
| 17 | + if has_seen || is_const |
| 18 | + x_vec = Float32[] |
| 19 | + else |
| 20 | + x_vec = reshape(x, length(x)) |
| 21 | + seen_vecs[x] = x_vec |
| 22 | + end |
| 23 | + sz = size(x) |
| 24 | + function FastGPUArray_from_vec(x_vec_new::AbstractVector{<:EnzymeTestUtils.ElementType}, seen_xs::EnzymeTestUtils.AliasDict) |
| 25 | + if xor(has_seen, haskey(seen_xs, x)) |
| 26 | + throw(ErrorException("Arrays must be reconstructed in the same order as they are vectorized.")) |
| 27 | + end |
| 28 | + has_seen && return reshape(seen_xs[x], size(x)) |
| 29 | + is_const && return x |
| 30 | + x_new = reshape(x_vec_new, sz) |
| 31 | + if Core.Typeof(x_new) != Core.Typeof(x) |
| 32 | + x_new = Core.Typeof(x)(x_new) |
| 33 | + end |
| 34 | + seen_xs[x] = x_new |
| 35 | + return x_new |
| 36 | + end |
| 37 | + return x_vec, FastGPUArray_from_vec |
| 38 | +end |
| 39 | + |
| 40 | +# basic containers: loop over defined elements, recursively converting them to vectors |
| 41 | +function to_vec(x::AbstractGPUArray{<:Complex{<:EnzymeTestUtils.ElementType}}, seen_vecs::EnzymeTestUtils.AliasDict) |
| 42 | + has_seen = haskey(seen_vecs, x) |
| 43 | + is_const = Enzyme.Compiler.guaranteed_const(Core.Typeof(x)) |
| 44 | + if has_seen || is_const |
| 45 | + x_vec = Float32[] |
| 46 | + else |
| 47 | + y = reshape(x, length(x)) |
| 48 | + x_vec = vcat(real.(y), imag.(y)) |
| 49 | + seen_vecs[x] = x_vec |
| 50 | + end |
| 51 | + sz = size(x) |
| 52 | + function ComplexGPUArray_from_vec(x_vec_new::AbstractVector{<:EnzymeTestUtils.ElementType}, seen_xs::EnzymeTestUtils.AliasDict) |
| 53 | + if xor(has_seen, haskey(seen_xs, x)) |
| 54 | + throw(ErrorException("Arrays must be reconstructed in the same order as they are vectorized.")) |
| 55 | + end |
| 56 | + has_seen && return reshape(seen_xs[x], size(x)) |
| 57 | + is_const && return x |
| 58 | + x_new = Array{eltype(x)}(undef, sz) |
| 59 | + @inbounds @simd for i in 1:length(x) |
| 60 | + x_new[i] = eltype(x)(x_vec_new[i], x_vec_new[i + length(x)]) |
| 61 | + end |
| 62 | + x_new = Core.Typeof(x)(x_new) |
| 63 | + seen_xs[x] = x_new |
| 64 | + return x_new |
| 65 | + end |
| 66 | + return x_vec, ComplexGPUArray_from_vec |
| 67 | +end |
| 68 | + |
| 69 | +# basic containers: loop over defined elements, recursively converting them to vectors |
| 70 | +function to_vec(x::AbstractGPUArray, seen_vecs::EnzymeTestUtils.AliasDict) |
| 71 | + has_seen = haskey(seen_vecs, x) |
| 72 | + is_const = Enzyme.Compiler.guaranteed_const(Core.Typeof(x)) |
| 73 | + if has_seen || is_const |
| 74 | + x_vec = Float32[] |
| 75 | + else |
| 76 | + x_vecs = nothing |
| 77 | + from_vecs = [] |
| 78 | + subvec_inds = UnitRange{Int}[] |
| 79 | + l = 0 |
| 80 | + for i in eachindex(x) |
| 81 | + isassigned(x, i) || continue |
| 82 | + xi_vec, xi_from_vec = to_vec(x[i], seen_vecs) |
| 83 | + push!(subvec_inds, (l + 1):(l + length(xi_vec))) |
| 84 | + push!(from_vecs, xi_from_vec) |
| 85 | + x_vecs = EnzymeTestUtils.append_or_merge(x_vecs, xi_vec) |
| 86 | + l += length(xi_vec) |
| 87 | + end |
| 88 | + |
| 89 | + if x_vecs === nothing |
| 90 | + x_vecs = (Float32[], true) |
| 91 | + end |
| 92 | + x_vec = x_vecs[1] |
| 93 | + seen_vecs[x] = x_vec |
| 94 | + end |
| 95 | + function GPUArray_from_vec(x_vec_new::AbstractVector{<:EnzymeTestUtils.ElementType}, seen_xs::EnzymeTestUtils.AliasDict) |
| 96 | + if xor(has_seen, haskey(seen_xs, x)) |
| 97 | + throw(ErrorException("Arrays must be reconstructed in the same order as they are vectorized.")) |
| 98 | + end |
| 99 | + has_seen && return reshape(seen_xs[x], size(x)) |
| 100 | + is_const && return x |
| 101 | + x_new = Array{eltype(x_vew_new)}(undef, size(x)) |
| 102 | + k = 1 |
| 103 | + for i in eachindex(x) |
| 104 | + isassigned(x, i) || continue |
| 105 | + xi = from_vecs[k](@view(x_vec_new[subvec_inds[k]]), seen_xs) |
| 106 | + x_new[i] = xi |
| 107 | + k += 1 |
| 108 | + end |
| 109 | + x_new = Core.Typeof(x)(x_new) |
| 110 | + seen_xs[x] = x_new |
| 111 | + return x_new |
| 112 | + end |
| 113 | + return x_vec, GPUArray_from_vec |
| 114 | +end |
| 115 | + |
| 116 | +end # module |
0 commit comments