# Composite types array, and composite type with array fields

**URL:** <https://discourse.julialang.org/t/composite-types-array-and-composite-type-with-array-fields/32466>\
**Category:** GPU\
**Tags:** cudanative, cuda\
**Created:** [December 19, 2019, 1:04am UTC](https://discourse.julialang.org/t/composite-types-array-and-composite-type-with-array-fields/32466 "2019-12-19T01:04:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![garychow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/garychow/32/11734_2.png) [@garychow](https://discourse.julialang.org/u/garychow)\
**Post date:** [December 19, 2019, 1:04am UTC](https://discourse.julialang.org/t/composite-types-array-and-composite-type-with-array-fields/32466/1 "2019-12-19T01:04:05Z")

</div>

Hi,

I am using CUDAnative now and wonder if anyone could point me to a correct direction such that these operations is possible. Is there anyone else found that it will be easier if these 2 functions are supported? I understand that would require some coding in Pointer conversion etc…

1. composite type array for GPU

```julia
struct Type1
    field1::Int32
    field2::Int32
end

array = Vector{Type1}

function kernel_1(a::array)
end

```

1. composite with array field

```julia
struct Type2
    field1::Vector{Int32}
    field2::Int32
end

function kernel_2(b::Type2)
end

```

---

<div class="post-metadata">

**Author:** ![maleadt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maleadt/32/10097_2.png) [@maleadt](https://discourse.julialang.org/u/maleadt)\
**Post date:** [December 19, 2019, 7:04am UTC](https://discourse.julialang.org/t/composite-types-array-and-composite-type-with-array-fields/32466/2 "2019-12-19T07:04:56Z")

</div>

Did you try it out yourself? Type1 just works:

```julia
julia> struct Type1
           field1::Int32
           field2::Int32
       end

julia> array = [Type1(1,2)]
1-element Array{Type1,1}:
 Type1(1, 2)

julia> gpu_array = adapt(CuArray, array) # same as `cu` but without the Float32 conversions
1-element CuArray{Type1,1,Nothing}:
 Type1(1, 2)

julia> @cuda ((a)->(@cuprintln(a[1].field1); nothing))(gpu_array)

julia> 1

```

for `Type2` you need to register a GPU-compatible counterpart structs and how to convert to them. But there’s a couple of catches: given how `Type2` is currently declared with a hard-coded `Vector` field, you’d need a `Type2GPU` field to represent the type with the vector on the GPU, and another for actual use on the GPU. Furthermore, Adapt (the machinery underpinning these conversions) doesn’t change the element type of an array. So it’s probably easier if you use NamedTuple’s instead:

```julia
julia> array = [(field1=[1], field2=2)]
1-element Array{NamedTuple{(:field1, :field2),Tuple{Array{Int64,1},Int64}},1}:
 (field1 = [1], field2 = 2)

julia> gpu(x) = adapt(CuArray, x)
gpu (generic function with 1 method)

julia> gpu_array = gpu(gpu.(array))
1-element CuArray{NamedTuple{(:field1, :field2),Tuple{CuArray{Int64,1,Nothing},Int64}},1,Nothing}:
 (field1 = [1], field2 = 2)

```

---

<div class="post-metadata">

**Author:** ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)\
**Post date:** [December 19, 2019, 12:11pm UTC](https://discourse.julialang.org/t/composite-types-array-and-composite-type-with-array-fields/32466/3 "2019-12-19T12:11:52Z")

</div>

> or `Type2` you need to register a GPU-compatible counterpart structs

Would it be possible to have a generic counterpart struct for [`StructArrays.StructArray`](https://github.com/JuliaArrays/StructArrays.jl)? That might cover a lot of use cases.

---

<div class="post-metadata">

**Author:** ![oschulz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oschulz/32/2998_2.png) [@oschulz](https://discourse.julialang.org/u/oschulz)\
**Post date:** [December 19, 2019, 12:14pm UTC](https://discourse.julialang.org/t/composite-types-array-and-composite-type-with-array-fields/32466/4 "2019-12-19T12:14:09Z")

</div>

Or, since `StructArray` can take any kind of array, would it be enough to make Adapt aware of it somehow?

CC @piever

---

<div class="post-metadata">

**Author:** ![piever](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/piever/32/1815_2.png) [@piever](https://discourse.julialang.org/u/piever)\
**Post date:** [December 19, 2019, 12:43pm UTC](https://discourse.julialang.org/t/composite-types-array-and-composite-type-with-array-fields/32466/5 "2019-12-19T12:43:15Z")

</div>

There is a [PR](https://github.com/JuliaArrays/StructArrays.jl/pull/87) for that, but it seems a bit too complex. In principle I’d be happy to make StructArrays fully GPU compliant though.

---

<div class="post-metadata">

**Author:** ![garychow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/garychow/32/11734_2.png) [@garychow](https://discourse.julialang.org/u/garychow)\
**Post date:** [December 20, 2019, 3:43am UTC](https://discourse.julialang.org/t/composite-types-array-and-composite-type-with-array-fields/32466/6 "2019-12-20T03:43:18Z")

</div>

Hi,

Yes thanks for your comments it really works. And I just realized if everything in type2 is tuple instead of vector then everything that I wanted will just work, due to the static allocation.

Thanks
