# Need a basic example on using custom structs in CUDA.jl with Adapt.jl

**URL:** <https://discourse.julialang.org/t/need-a-basic-example-on-using-custom-structs-in-cuda-jl-with-adapt-jl/118829>\
**Category:** GPU\
**Tags:** cuda, struct, adapt\
**Created:** [August 30, 2024, 10:49pm UTC](https://discourse.julialang.org/t/need-a-basic-example-on-using-custom-structs-in-cuda-jl-with-adapt-jl/118829 "2024-08-30T22:49:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![NonDairyNeutrino](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nondairyneutrino/32/221496_2.png) [@NonDairyNeutrino](https://discourse.julialang.org/u/NonDairyNeutrino)\
**Post date:** [August 30, 2024, 10:49pm UTC](https://discourse.julialang.org/t/need-a-basic-example-on-using-custom-structs-in-cuda-jl-with-adapt-jl/118829/1 "2024-08-30T22:49:53Z")

</div>

I’m having a hard time understanding both the purpose of and how to use Adapt.jl to pass custom structs to CUDA kernels (that might not even be right). I’ve tried reading the CUDA.jl [documentation](https://cuda.juliagpu.org/stable/tutorials/custom_structs/) and implementing a trivial example (below) but it errors as the passed struct isn’t a bitstype.

```julia
using CUDA
using Adapt

struct TestStruct
    testField
end

Adapt.@adapt_structure TestStruct

test = TestStruct([1,2,3] |> cu)

function squareGPU!(test)
    testField = test.testField
    index = threadIdx().x
    testField[index] = testField[index]^2
    return nothing
end

@cuda threads=3 squareGPU!(test)

```

> ERROR: LoadError: GPU compilation of MethodInstance for squareGPU!(::TestStruct) failed  
> KernelError: passing and using non-bitstype argument  
> Argument 2 to your kernel function is of type TestStruct, which is not isbits:  
> .testField is of type Any which is not isbits.

To try to address the second part of the error, I tried restricting `testField` to `CuArray`:

```julia
using CUDA
using Adapt

struct TestStruct
    testField :: CuArray
end

Adapt.@adapt_structure TestStruct

test = TestStruct([1,2,3] |> cu)

function squareGPU!(test)
    testField = test.testField
    index = threadIdx().x
    testField[index] = testField[index]^2
    return nothing
end

@cuda threads=3 squareGPU!(test)

```

But this still results in an (seemingly better?) error.

> LoadError: This function is not intended for use on the CPU

I appreciate y’alls advice as I might just have a fundamental misunderstanding of how to use Adapt.

---

<div class="post-metadata">

**Author:** ![SteffenPL](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/steffenpl/32/206270_2.png) [@SteffenPL](https://discourse.julialang.org/u/SteffenPL)\
**Post date:** [August 31, 2024, 4:05am UTC](https://discourse.julialang.org/t/need-a-basic-example-on-using-custom-structs-in-cuda-jl-with-adapt-jl/118829/2 "2024-08-31T04:05:31Z")

</div>

As far as I understand it, it is important here to use parametric types.

```julia
using CUDA, Adapt

struct TestStruct{A}
    testField::A
end

Adapt.@adapt_structure TestStruct

test = TestStruct([1,2,3]) |> cu
# but this also works: test = TestStruct([1,2,3] |> cu) 

function squareGPU!(test)
    testField = test.testField
    index = threadIdx().x
    testField[index] = testField[index]^2
    return nothing
end

@cuda threads=3 squareGPU!(test)

```

Notice that you can first construct the CPU type `TestStruct{Vector{Int64}}` and then `cu` will automatically convert it to the fully specified GPU type `TestStruct{CuArray{Int64, 1, CUDA.DeviceMemory}}`.

The error message about the is bits type indicated that `TestStruct` as a type itself does not specify the type precise enough, also `CuArray` is not precise enough as it could be different types, dimension, etc.

---

<div class="post-metadata">

**Author:** ![NonDairyNeutrino](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nondairyneutrino/32/221496_2.png) [@NonDairyNeutrino](https://discourse.julialang.org/u/NonDairyNeutrino)\
**Post date:** [August 31, 2024, 7:11pm UTC](https://discourse.julialang.org/t/need-a-basic-example-on-using-custom-structs-in-cuda-jl-with-adapt-jl/118829/3 "2024-08-31T19:11:26Z")

</div>

Huh, simple enough! Amazing that it “just works”; Julia really is a wonderful thing.
