while porting some array wrapper to a GPU, its quite nice to use Adapt.jl to adapt the underlying arrays.
However it feels a bit nasty to parametrize a struct on the full array type, lets say we have
struct Foo
x::CuArray{Bar,1}
end
in order to use Adapt.jl I'm currently doing
struct Foo{T}
x::T
end
which would end up in e.g. Foo{CuArray{Bar,1}}
Is there a way to do something like
struct Foo{T<:AbstractArray,arrtype}
x::T{arrtype,1}
end
In order to get back to Foo{Bar}
?
had the idea, immediately after writing the post, but maybe it's also helpful for others. One possible solution is
struct Foo{arrtype,T<:DenseArray{arrtype,1}}
x::T
end
it's not Foo{Bar}
but with this I can dispatch again on Foo{Bar}
It is, unfortunately, not possible to do "computation" in type parameters when defining a struct, so you can't ask it to "extract" some type parameters, if that makes sense
But I'm not clear exactly what you're asking. If you want to obtain a Foo{Bar}
, you can simply do Foo(x::Bar)
, right?
so, T
should be always some type of Array, CuArray
or when on device CuDeviceArray
. Now I still want the underlying array element type as a parameter of Foo
, but you're right I could just get it inside the function. Maybe that's still better than introducing another type parameter
Last updated: Nov 06 2024 at 04:40 UTC