|
| 1 | +# Advanced techniques |
| 2 | + |
| 3 | +## Structures with non-standard data layout |
| 4 | + |
| 5 | +StructArrays support structures with custom data layout. The user is required to overload `staticschema` in order to define the custom layout, `component` to access fields of the custom layout, and `createinstance(T, fields...)` to create an instance of type `T` from its custom fields `fields`. In other word, given `x::T`, `createinstance(T, (component(x, f) for f in fieldnames(staticschema(T)))...)` should successfully return an instance of type `T`. |
| 6 | + |
| 7 | +Here is an example of a type `MyType` that has as custom fields either its field `data` or fields of its field `rest` (which is a named tuple): |
| 8 | + |
| 9 | +```julia |
| 10 | +using StructArrays |
| 11 | + |
| 12 | +struct MyType{T, NT<:NamedTuple} |
| 13 | + data::T |
| 14 | + rest::NT |
| 15 | +end |
| 16 | + |
| 17 | +MyType(x; kwargs...) = MyType(x, values(kwargs)) |
| 18 | + |
| 19 | +function StructArrays.staticschema(::Type{MyType{T, NamedTuple{names, types}}}) where {T, names, types} |
| 20 | + return NamedTuple{(:data, names...), Base.tuple_type_cons(T, types)} |
| 21 | +end |
| 22 | + |
| 23 | +function StructArrays.component(m::MyType, key::Symbol) |
| 24 | + return key === :data ? getfield(m, 1) : getfield(getfield(m, 2), key) |
| 25 | +end |
| 26 | + |
| 27 | +# generate an instance of MyType type |
| 28 | +function StructArrays.createinstance(::Type{MyType{T, NT}}, x, args...) where {T, NT} |
| 29 | + return MyType(x, NT(args)) |
| 30 | +end |
| 31 | + |
| 32 | +s = [MyType(rand(), a=1, b=2) for i in 1:10] |
| 33 | +StructArray(s) |
| 34 | +``` |
| 35 | + |
| 36 | +In the above example, our `MyType` was composed of `data` of type `Float64` and `rest` of type `NamedTuple`. In many practical cases where there are custom types involved it's hard for StructArrays to automatically widen the types in case they are heterogeneous. The following example demonstrates a widening method in that scenario. |
| 37 | + |
| 38 | +```julia |
| 39 | +using Tables |
| 40 | + |
| 41 | +# add a source of custom type data |
| 42 | +struct Location{U} |
| 43 | + x::U |
| 44 | + y::U |
| 45 | +end |
| 46 | +struct Region{V} |
| 47 | + area::V |
| 48 | +end |
| 49 | + |
| 50 | +s1 = MyType(Location(1, 0), place = "Delhi", rainfall = 200) |
| 51 | +s2 = MyType(Location(2.5, 1.9), place = "Mumbai", rainfall = 1010) |
| 52 | +s3 = MyType(Region([Location(1, 0), Location(2.5, 1.9)]), place = "North India", rainfall = missing) |
| 53 | + |
| 54 | +s = [s1, s2, s3] |
| 55 | +# Now if we try to do StructArray(s) |
| 56 | +# we will get an error |
| 57 | + |
| 58 | +function meta_table(iter) |
| 59 | + cols = Tables.columntable(iter) |
| 60 | + meta_table(first(cols), Base.tail(cols)) |
| 61 | +end |
| 62 | + |
| 63 | +function meta_table(data, rest::NT) where NT<:NamedTuple |
| 64 | + F = MyType{eltype(data), StructArrays.eltypes(NT)} |
| 65 | + return StructArray{F}(; data=data, rest...) |
| 66 | +end |
| 67 | + |
| 68 | +meta_table(s) |
| 69 | +``` |
| 70 | + |
| 71 | +The above strategy has been tested and implemented in [GeometryBasics.jl](https://github.com/JuliaGeometry/GeometryBasics.jl). |
| 72 | + |
| 73 | +## Mutate-or-widen style accumulation |
| 74 | + |
| 75 | +StructArrays provides a function `StructArrays.append!!(dest, src)` (unexported) for "mutate-or-widen" style accumulation. This function can be used via [`BangBang.append!!`](https://juliafolds.github.io/BangBang.jl/dev/#BangBang.append!!) and [`BangBang.push!!`](https://juliafolds.github.io/BangBang.jl/dev/#BangBang.push!!) as well. |
| 76 | + |
| 77 | +`StructArrays.append!!` works like `append!(dest, src)` if `dest` can contain all element types in `src` iterator; i.e., it _mutates_ `dest` in-place: |
| 78 | + |
| 79 | +```julia |
| 80 | +julia> dest = StructVector((a=[1], b=[2])) |
| 81 | +1-element StructArray(::Array{Int64,1}, ::Array{Int64,1}) with eltype NamedTuple{(:a, :b),Tuple{Int64,Int64}}: |
| 82 | + (a = 1, b = 2) |
| 83 | + |
| 84 | +julia> StructArrays.append!!(dest, [(a = 3, b = 4)]) |
| 85 | +2-element StructArray(::Array{Int64,1}, ::Array{Int64,1}) with eltype NamedTuple{(:a, :b),Tuple{Int64,Int64}}: |
| 86 | + (a = 1, b = 2) |
| 87 | + (a = 3, b = 4) |
| 88 | + |
| 89 | +julia> ans === dest |
| 90 | +true |
| 91 | +``` |
| 92 | + |
| 93 | +Unlike `append!`, `append!!` can also _widen_ element type of `dest` array: |
| 94 | + |
| 95 | +```julia |
| 96 | +julia> StructArrays.append!!(dest, [(a = missing, b = 6)]) |
| 97 | +3-element StructArray(::Array{Union{Missing, Int64},1}, ::Array{Int64,1}) with eltype NamedTuple{(:a, :b),Tuple{Union{Missing, Int64},Int64}}: |
| 98 | + NamedTuple{(:a, :b),Tuple{Union{Missing, Int64},Int64}}((1, 2)) |
| 99 | + NamedTuple{(:a, :b),Tuple{Union{Missing, Int64},Int64}}((3, 4)) |
| 100 | + NamedTuple{(:a, :b),Tuple{Union{Missing, Int64},Int64}}((missing, 6)) |
| 101 | + |
| 102 | +julia> ans === dest |
| 103 | +false |
| 104 | +``` |
| 105 | + |
| 106 | +Since the original array `dest` cannot hold the input, a new array is created (`ans !== dest`). |
| 107 | + |
| 108 | +Combined with [function barriers](https://docs.julialang.org/en/latest/manual/performance-tips/#kernel-functions-1), `append!!` is a useful building block for implementing `collect`-like functions. |
| 109 | + |
| 110 | +## Using StructArrays in CUDA kernels |
| 111 | + |
| 112 | +It is possible to combine StructArrays with [CUDAnative](https://github.com/JuliaGPU/CUDAnative.jl), in order to create CUDA kernels that work on StructArrays directly on the GPU. Make sure you are familiar with the CUDAnative documentation (esp. kernels with plain `CuArray`s) before experimenting with kernels based on `StructArray`s. |
| 113 | + |
| 114 | +```julia |
| 115 | +using CUDAnative, CuArrays, StructArrays |
| 116 | +d = StructArray(a = rand(100), b = rand(100)) |
| 117 | + |
| 118 | +# move to GPU |
| 119 | +dd = replace_storage(CuArray, d) |
| 120 | +de = similar(dd) |
| 121 | + |
| 122 | +# a simple kernel, to copy the content of `dd` onto `de` |
| 123 | +function kernel!(dest, src) |
| 124 | + i = (blockIdx().x-1)*blockDim().x + threadIdx().x |
| 125 | + if i <= length(dest) |
| 126 | + dest[i] = src[i] |
| 127 | + end |
| 128 | + return nothing |
| 129 | +end |
| 130 | + |
| 131 | +threads = 1024 |
| 132 | +blocks = cld(length(dd),threads) |
| 133 | + |
| 134 | +@cuda threads=threads blocks=blocks kernel!(de, dd) |
| 135 | +``` |
| 136 | + |
0 commit comments