Hi everyone. I am working with the StructArrays.jl
package and trying to program a function to react differently for different types of arrays inside of a StructArray.
Specifically I want to differentiate between Array and CuArray of ComplexF32 values. I have found the most straighforward (and honestly ugly looking) way to do this.
# Creating the arrays
cpuarray = Array{ComplexF32}(undef, 2)
gpuarray = CuArray(cpuarray)
scpuarray = StructArray{ComplexF32}((real(cpuarray),imag(cpuarray)))
sgpuarray = StructArray{ComplexF32}((real(gpuarray),imag(gpuarray)))
# Checking types
julia> typeof(scpuarray)
StructArray{Complex{Float32},1,NamedTuple{(:re, :im),Tuple{Array{Float32,1},Array{Float32,1}}},Int64}
julia> typeof(sgpuarray)
StructArray{Complex{Float32},1,NamedTuple{(:re, :im),Tuple{CuArray{Float32,1,Nothing},CuArray{Float32,1,Nothing}}},Int64}
# Defining a basic function for both scpuarray and sgpuarray
function example(
input::StructArray{Complex{Float32},1,NamedTuple{(:re, :im),Tuple{Array{Float32,1},Array{Float32,1}}},Int64}
)
return 1
end
function example(
input::StructArray{Complex{Float32},1,NamedTuple{(:re, :im),Tuple{CuArray{Float32,1,Nothing},CuArray{Float32,1,Nothing}}},Int64}
)
return 2
end
# Checking the definitions
julia> example(scpuarray)
1
julia> example(sgpuarray)
2
Are there any other ideas on how achieve this in a more elegant way?
Ideally I would just like to write something like input::StructArray(CuArray)
Would appreciate any help.