# Passing a structure with constructor on GPU

**URL:** <https://discourse.julialang.org/t/passing-a-structure-with-constructor-on-gpu/115596>\
**Category:** GPU\
**Tags:** question\
**Created:** [June 13, 2024, 12:23pm UTC](https://discourse.julialang.org/t/passing-a-structure-with-constructor-on-gpu/115596 "2024-06-13T12:23:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Baptiste\_Fabre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baptiste_fabre/32/209711_2.png) [@Baptiste\_Fabre](https://discourse.julialang.org/u/Baptiste_Fabre)\
**Post date:** [June 13, 2024, 12:23pm UTC](https://discourse.julialang.org/t/passing-a-structure-with-constructor-on-gpu/115596/1 "2024-06-13T12:23:46Z")

</div>

Dear all,

I have a question about how to pass a structure built with a constructor on a GPU card. My structure is defined as

struct FieldParams{T}  
I₀ :: T  
λ₀ :: T  
α :: T  
r :: T  
ω :: T  
E₀ :: T  
Up :: T  
T₀ :: T  
NbCycles :: Int

```
function FieldParams{T}(I₀, λ₀, α,r,NbCycles) where {T}
    c = 299792458.0
    ω = 2 * π * c / (λ₀ * 1e-9) * 2.4e-17
    E₀ = sqrt(I₀)
    Up = I₀ / (4.0 * ω^2)
    T₀ = 2.0 * π / ω
    new{T}(I₀, λ₀, α, r, ω, E₀, Up, T₀,NbCycles)
end

```

end

Where T=Float32 for example.

I call it using  
T = Float32 #Better to work with Flux and CUDA

# Field parameters

λ₀ = T(800.) #En nanometres  
I₀ = T(3.e13/3.5e16) #(3.0e13/3.5e16)  
r = T(0.1)  
α = T(-1.)  
NbCycles = 4  
fdp=FieldParams{T}(I₀,λ₀,α,r,NbCycles)

I tried with the Adapt.jl but without any success. Do you have some recommendations about how I can manage to put this structure on my GPU.

Thanks a lot.

Baptiste

---

<div class="post-metadata">

**Author:** ![eldee](https://avatars.discourse-cdn.com/v4/letter/e/b5a626/32.png) [@eldee](https://discourse.julialang.org/u/eldee)\
**Post date:** [June 14, 2024, 12:20pm UTC](https://discourse.julialang.org/t/passing-a-structure-with-constructor-on-gpu/115596/2 "2024-06-14T12:20:50Z")

</div>

Hi Baptiste,

I don’t think you need to do anything special, because your struct is already `isbits`. E.g.

```julia
julia> a = CUDA.fill(fdp, 2)
2-element CuArray{FieldParams{Float32}, 1, CUDA.DeviceMemory}:
 FieldParams{Float32}(0.00085714285f0, 800.0f0, -1.0f0, 0.1f0, 0.056509547f0, 0.029277002f0, 0.067104176f0, 111.188034f0, 4)
 FieldParams{Float32}(0.00085714285f0, 800.0f0, -1.0f0, 0.1f0, 0.056509547f0, 0.029277002f0, 0.067104176f0, 111.188034f0, 4)

```

and

```julia
julia> map(fp -> fp.r, a)
2-element CuArray{Float32, 1, CUDA.DeviceMemory}:
 0.1
 0.1

```

already work.

---

<div class="post-metadata">

**Author:** ![Baptiste\_Fabre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baptiste_fabre/32/209711_2.png) [@Baptiste\_Fabre](https://discourse.julialang.org/u/Baptiste_Fabre)\
**Post date:** [June 17, 2024, 1:06pm UTC](https://discourse.julialang.org/t/passing-a-structure-with-constructor-on-gpu/115596/3 "2024-06-17T13:06:32Z")

</div>

Thanks a lot for your answer.  
I am not sure to understand everything, but the main thing is that it works.  
Indeed, I was surprised because I tried to write a write a function to transform the variables in the structure in CuArrays, but performance on the GPU was not as good.  
So in the end, if I understand correctly, I don’t have to change anything.  
Thanks again for your reply.

Baptiste
