# Are there any way to copy a Dict or a custom datatype which contains an Array to GPU by CUDA?

**URL:** <https://discourse.julialang.org/t/are-there-any-way-to-copy-a-dict-or-a-custom-datatype-which-contains-an-array-to-gpu-by-cuda/50217>\
**Category:** GPU\
**Tags:** question\
**Created:** [November 16, 2020, 8:56am UTC](https://discourse.julialang.org/t/are-there-any-way-to-copy-a-dict-or-a-custom-datatype-which-contains-an-array-to-gpu-by-cuda/50217 "2020-11-16T08:56:58Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![HaoxuanGuo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/haoxuanguo/32/19052_2.png) [@HaoxuanGuo](https://discourse.julialang.org/u/HaoxuanGuo)\
**Post date:** [November 16, 2020, 8:56am UTC](https://discourse.julialang.org/t/are-there-any-way-to-copy-a-dict-or-a-custom-datatype-which-contains-an-array-to-gpu-by-cuda/50217/1 "2020-11-16T08:56:58Z")

</div>

I want to use the LinearAlgebra library to simplify my programming. So, I warp some Arrays in an struct. But I find I can not copy it to GPU because it is a non-bitstype.

Similarly, I want to look up some tables which is stored in a Dict. It is non-bitstype, too. Thus it can not be copied to GPU.

So, I wander that is there a trick way to copy them to GPU and complish these calculation?

---

<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:** [November 16, 2020, 9:07am UTC](https://discourse.julialang.org/t/are-there-any-way-to-copy-a-dict-or-a-custom-datatype-which-contains-an-array-to-gpu-by-cuda/50217/2 "2020-11-16T09:07:33Z")

</div>

You need to convert those arrays to GPU-compatible arrays, e.g. `CuArray` with CUDA.jl (e.g. parameterize your struct on the array type). Optionally, you can use Adapt.jl to make those conversions more generic.

Dictionaries won’t work.

---

<div class="post-metadata">

**Author:** ![HaoxuanGuo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/haoxuanguo/32/19052_2.png) [@HaoxuanGuo](https://discourse.julialang.org/u/HaoxuanGuo)\
**Post date:** [November 16, 2020, 10:06am UTC](https://discourse.julialang.org/t/are-there-any-way-to-copy-a-dict-or-a-custom-datatype-which-contains-an-array-to-gpu-by-cuda/50217/3 "2020-11-16T10:06:06Z")

</div>

This condition that CuArray wrapped by a struct is also not work. Are there any solutions?

```julia
using CUDA

struct TestType
    a::CuArray
    b::CuArray
    c::Float32
end

data = CUDA.fill(TestType([1., 2., 3.], [4., 5., 6.], 7.), 100)

```

It will get a

```julia
ERROR: LoadError: CuArray only supports bits types

```

as well.

Are there any tricks to make it worked?

---

<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:** [November 16, 2020, 10:11am UTC](https://discourse.julialang.org/t/are-there-any-way-to-copy-a-dict-or-a-custom-datatype-which-contains-an-array-to-gpu-by-cuda/50217/4 "2020-11-16T10:11:20Z")

</div>

You didn’t mention wrapping that struct in an array as well. That’s currently not supported by `CuArray`. You can work around it by eagerly converting the `CuArray`s to `CuDeviceArray` (which are `isbits`) using `cudaconvert` and storing those in a `CuArray`, see e.g. [Passing array of pointers to CUDA kernel](https://discourse.julialang.org/t/passing-array-of-pointers-to-cuda-kernel/33570). But to `cudaconvert` your `TestType` here you’ll need to make it parametric (`a::T`, `b::T`, `T<:AbstractArray`) and add Adapt.jl rules (`Adapt.adapt_structure(to, x::TestType) = TestType(adapt(to, x,a), ...)`), as I mentioned above.

---

<div class="post-metadata">

**Author:** ![HaoxuanGuo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/haoxuanguo/32/19052_2.png) [@HaoxuanGuo](https://discourse.julialang.org/u/HaoxuanGuo)\
**Post date:** [November 16, 2020, 10:14am UTC](https://discourse.julialang.org/t/are-there-any-way-to-copy-a-dict-or-a-custom-datatype-which-contains-an-array-to-gpu-by-cuda/50217/5 "2020-11-16T10:14:52Z")

</div>

OK. Thank you for your answer.

---

<div class="post-metadata">

**Author:** ![HaoxuanGuo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/haoxuanguo/32/19052_2.png) [@HaoxuanGuo](https://discourse.julialang.org/u/HaoxuanGuo)\
**Post date:** [November 17, 2020, 5:30am UTC](https://discourse.julialang.org/t/are-there-any-way-to-copy-a-dict-or-a-custom-datatype-which-contains-an-array-to-gpu-by-cuda/50217/6 "2020-11-17T05:30:03Z")

</div>

Sorry for another problem. How can I copy a CuDeviceArray back to memory?

---

<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:** [November 17, 2020, 7:13am UTC](https://discourse.julialang.org/t/are-there-any-way-to-copy-a-dict-or-a-custom-datatype-which-contains-an-array-to-gpu-by-cuda/50217/7 "2020-11-17T07:13:01Z")

</div>

`CuDeviceArray` is just a container for a pointer, you would have allocated a `CuArray` in the first place. Copy that one to CPU memory using `Array(::CuArray)`.
