StructsOfArrays.jl icon indicating copy to clipboard operation
StructsOfArrays.jl copied to clipboard

Make a package and add CUDA support

Open lcw opened this issue 5 years ago • 2 comments

This updates StructsOfArrays to work with Julia 1.4.1 and turns it into a package. In addition basic CUDA support has been added (thanks @vchuravy!) so that StructOfArrays can be passed into kernels, e.g.,

using CuArrays, CUDAnative, StaticArrays, StructsOfArrays
CuArrays.allowscalar(false)

A = rand(SArray{Tuple{3},Float64,1,3}, 10, 10)
B = replace_storage(CuArray, convert(StructOfArrays, A))

function kernel!(A)
    i = (blockIdx().x-1)*blockDim().x + threadIdx().x
    if i <= length(A)
        A[i] += A[i]
    end
    return nothing
end
threads = 256
blocks = cld(length(B), threads)
@cuda threads=threads blocks=blocks kernel!(B)

and used in broadcasting statements, e.g.,

using LinearAlgebra
n = norm.(B)
B .+= B

I first tried to add this functionality to StructArrays.jl but found it hard to get CUDA compatible getindex and setindex! functions.

If this pr gets accepted I would also like to register this as a package. If needed I am happy to help maintain this code.

cc: @vchuravy @simonbyrne

lcw avatar Apr 21 '20 01:04 lcw

Why not use StructArrays?

cossio avatar Apr 21 '20 06:04 cossio

@cossio I tried, see https://github.com/JuliaArrays/StructArrays.jl/issues/86, https://github.com/JuliaArrays/StructArrays.jl/pull/87, and https://github.com/JuliaArrays/StructArrays.jl/pull/114 but I could not get it to work for my use case, see https://github.com/JuliaArrays/StructArrays.jl/pull/114#issuecomment-571244310. The recursive nature of foreachfield is causing problems in StructArrays. That recursion needs to happen at compile time and not run time.

lcw avatar Apr 21 '20 16:04 lcw