Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace_storage ignores type parameters #248

Closed
cuihantao opened this issue Sep 26, 2022 · 2 comments
Closed

replace_storage ignores type parameters #248

cuihantao opened this issue Sep 26, 2022 · 2 comments

Comments

@cuihantao
Copy link

Hi,

I have the MWE below to replace the storage with CuArray data types. It looks like replace_storage recognizes CuArray out of CuArray{Float32} but ignores the Float32 part. Did I miss something? Thanks for your help!

using CUDA
using StructArrays

foo = StructArray(a=[1.0 for i = 1:10])

bar1 = replace_storage(CuArray, foo)  # defaults to CuArray{Float64}

@show typeof(bar1)

bar2 = replace_storage(CuArray{Float32}, foo) # still yields CuArray{Float64}

bar3 = convert(CuArray{Float32}, foo.a)  # works fine
@piever
Copy link
Collaborator

piever commented Sep 30, 2022

The docstring of replace_storage mentions that the eltype of the output is expected to be the same. The reason is that for complex types (not NamedTuple types but custom structs), it is a bit difficult to figure out what the output eltype should be. Basically, one would need a method createtype(T, type_1, type_2, ..., type_n) that computes the updated eltype based on column types rather than column values (this could be added to the interface).

If you have a StructArray of NamedTuple, you could instead use

julia> foo = StructArray(a=[1.0 for i = 1:3]);

julia> StructArray(map(Array{Float32}, StructArrays.components(foo)))
3-element StructArray(::Vector{Float32}) with eltype NamedTuple{(:a,), Tuple{Float32}}:
 (a = 1.0,)
 (a = 1.0,)
 (a = 1.0,)

Hope this helps!

@cuihantao
Copy link
Author

Thank you. I do have a StructArray of NamedTuple. This is very helpful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants