|
| 1 | +# Heterogeneous computing |
| 2 | + |
| 3 | +Support for heterogeneous computing is currently being worked on. |
| 4 | + |
| 5 | +## The use of Adapt.jl |
| 6 | + |
| 7 | +[Adapt.jl](https://github.com/JuliaGPU/Adapt.jl) is a package in the |
| 8 | +[JuliaGPU](https://github.com/JuliaGPU) family that allows for |
| 9 | +the translation of nested data structures. The primary goal is to allow the substitution of `Array` |
| 10 | +at the storage level with a GPU array like `CuArray` from [CUDA.jl](https://github.com/JuliaGPU/CUDA.jl). |
| 11 | + |
| 12 | +To facilitate this, data structures must be parameterized, so instead of: |
| 13 | + |
| 14 | +```julia |
| 15 | +struct Container <: Trixi.AbstractContainer |
| 16 | + data::Array{Float64, 2} |
| 17 | +end |
| 18 | +``` |
| 19 | + |
| 20 | +They must be written as: |
| 21 | + |
| 22 | +```jldoctest adapt; output = false, setup=:(import Trixi) |
| 23 | +struct Container{D<:AbstractArray} <: Trixi.AbstractContainer |
| 24 | + data::D |
| 25 | +end |
| 26 | +
|
| 27 | +# output |
| 28 | +
|
| 29 | +``` |
| 30 | + |
| 31 | +furthermore, we need to define a function that allows for the conversion of storage |
| 32 | +of our types: |
| 33 | + |
| 34 | +```jldoctest adapt; output = false |
| 35 | +using Adapt |
| 36 | +
|
| 37 | +function Adapt.adapt_structure(to, C::Container) |
| 38 | + return Container(adapt(to, C.data)) |
| 39 | +end |
| 40 | +
|
| 41 | +# output |
| 42 | +
|
| 43 | +``` |
| 44 | + |
| 45 | +or simply |
| 46 | + |
| 47 | +```julia |
| 48 | +Adapt.@adapt_structure(Container) |
| 49 | +``` |
| 50 | + |
| 51 | +additionally, we must define `Adapt.parent_type`. |
| 52 | + |
| 53 | +```jldoctest adapt; output = false |
| 54 | +function Adapt.parent_type(::Type{<:Container{D}}) where D |
| 55 | + return D |
| 56 | +end |
| 57 | +
|
| 58 | +# output |
| 59 | +
|
| 60 | +``` |
| 61 | + |
| 62 | +All together we can use this machinery to perform conversions of a container. |
| 63 | + |
| 64 | +```jldoctest adapt |
| 65 | +julia> C = Container(zeros(3)) |
| 66 | +Container{Vector{Float64}}([0.0, 0.0, 0.0]) |
| 67 | +
|
| 68 | +julia> Trixi.storage_type(C) |
| 69 | +Array |
| 70 | +``` |
| 71 | + |
| 72 | + |
| 73 | +```julia-repl |
| 74 | +julia> using CUDA |
| 75 | +
|
| 76 | +julia> GPU_C = adapt(CuArray, C) |
| 77 | +Container{CuArray{Float64, 1, CUDA.DeviceMemory}}([0.0, 0.0, 0.0]) |
| 78 | +
|
| 79 | +julia> Trixi.storage_type(C) |
| 80 | +CuArray |
| 81 | +``` |
| 82 | + |
| 83 | +## Element-type conversion with `Trixi.trixi_adapt`. |
| 84 | + |
| 85 | +We can use [`Trixi.trixi_adapt`](@ref) to perform both an element-type and a storage-type adoption: |
| 86 | + |
| 87 | +```jldoctest adapt |
| 88 | +julia> C = Container(zeros(3)) |
| 89 | +Container{Vector{Float64}}([0.0, 0.0, 0.0]) |
| 90 | +
|
| 91 | +julia> Trixi.trixi_adapt(Array, Float32, C) |
| 92 | +Container{Vector{Float32}}(Float32[0.0, 0.0, 0.0]) |
| 93 | +``` |
| 94 | + |
| 95 | +```julia-repl |
| 96 | +julia> Trixi.trixi_adapt(CuArray, Float32, C) |
| 97 | +Container{CuArray{Float32, 1, CUDA.DeviceMemory}}(Float32[0.0, 0.0, 0.0]) |
| 98 | +``` |
| 99 | + |
| 100 | +!!! note |
| 101 | + `adapt(Array{Float32}, C)` is tempting, but it will do the wrong thing |
| 102 | + in the presence of `SVector`s and similar arrays from StaticArrays.jl. |
| 103 | + |
| 104 | + |
| 105 | +## Writing GPU kernels |
| 106 | + |
| 107 | +Offloading computations to the GPU is done with |
| 108 | +[KernelAbstractions.jl](https://github.com/JuliaGPU/KernelAbstractions.jl), |
| 109 | +allowing for vendor-agnostic GPU code. |
| 110 | + |
| 111 | +### Example |
| 112 | + |
| 113 | +Given the following Trixi.jl code, which would typically be called from within `rhs!`: |
| 114 | + |
| 115 | +```julia |
| 116 | +function trixi_rhs_fct(mesh, equations, solver, cache, args) |
| 117 | + @threaded for element in eachelement(solver, cache) |
| 118 | + # code |
| 119 | + end |
| 120 | +end |
| 121 | +``` |
| 122 | + |
| 123 | +1. Put the inner code in a new function `rhs_fct_per_element`. Besides the index |
| 124 | + `element`, pass all required fields as arguments, but make sure to `@unpack` them from |
| 125 | + their structs in advance. |
| 126 | + |
| 127 | +2. Where `trixi_rhs_fct` is called, get the backend, i.e., the hardware we are currently |
| 128 | + running on via `trixi_backend(x)`. |
| 129 | + This will, e.g., work with `u_ode`. Internally, KernelAbstractions.jl's `get_backend` |
| 130 | + will be called, i.e., KernelAbstractions.jl has to know the type of `x`. |
| 131 | + |
| 132 | + ```julia |
| 133 | + backend = trixi_backend(u_ode) |
| 134 | + ``` |
| 135 | + |
| 136 | +3. Add a new argument `backend` to `trixi_rhs_fct` used for dispatch. |
| 137 | + When `backend` is `nothing`, the legacy implementation should be used: |
| 138 | + ```julia |
| 139 | + function trixi_rhs_fct(backend::Nothing, mesh, equations, solver, cache, args) |
| 140 | + @unpack unpacked_args = cache |
| 141 | + @threaded for element in eachelement(solver, cache) |
| 142 | + rhs_fct_per_element(element, unpacked_args, args) |
| 143 | + end |
| 144 | + end |
| 145 | + ``` |
| 146 | + |
| 147 | +4. When `backend` is a `Backend` (a type defined by KernelAbstractions.jl), write a |
| 148 | + KernelAbstractions.jl kernel: |
| 149 | + ```julia |
| 150 | + function trixi_rhs_fct(backend::Backend, mesh, equations, solver, cache, args) |
| 151 | + nelements(solver, cache) == 0 && return nothing # return early when there are no elements |
| 152 | + @unpack unpacked_args = cache |
| 153 | + kernel! = rhs_fct_kernel!(backend) |
| 154 | + kernel!(unpacked_args, args, |
| 155 | + ndrange = nelements(solver, cache)) |
| 156 | + return nothing |
| 157 | + end |
| 158 | +
|
| 159 | + @kernel function rhs_fct_kernel!(unpacked_args, args) |
| 160 | + element = @index(Global) |
| 161 | + rhs_fct_per_element(element, unpacked_args, args) |
| 162 | + end |
| 163 | + ``` |
0 commit comments