Skip to content

JuliaFolds/UnionArrays.jl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

62 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

UnionArrays: storage-agnostic array type with Union elements

Stable Dev GitHub Actions Codecov GitHub last commit

UnionArrays.jl provides an array type with Union element types that is generic over the data storage type.

julia> using UnionArrays

julia> xs = UnionVector(undef, Vector, Union{Float32,Tuple{},UInt8}, 3);

julia> fill!(xs, ());

julia> xs[1]
()

julia> xs[2] = 1.0f0;

julia> xs[3] = UInt8(2);

julia> collect(xs)
3-element Vector{Union{Tuple{}, Float32, UInt8}}:
     ()
    1.0f0
 0x02

For example, it can be used for bringing Union element types to GPU:

julia> using CUDA

julia> xs = UnionVector(undef, CuVector, Union{Float32,Nothing}, 3);

julia> fill!(xs, nothing);

Packages like Transducers.jl and Folds.jl support computations with UnionArrays on GPU:

julia> using Folds, FoldsCUDA

julia> Folds.all(==(nothing), xs)
true

julia> CUDA.@allowscalar begin
           xs[2] = 1.0f0
           xs[3] = 2.0f0
       end;

julia> Folds.sum(x -> x === nothing ? 0.0f0 : x, xs; init = 0.0f0)
3.0f0