Passing a structure with constructor on GPU

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

Hi Baptiste,

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

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> map(fp -> fp.r, a)
2-element CuArray{Float32, 1, CUDA.DeviceMemory}:
 0.1
 0.1

already work.

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