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

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

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.

2 Likes