Skip to content

Commit c96ece4

Browse files
committed
add docs and CUDAExt
1 parent 0de3051 commit c96ece4

File tree

4 files changed

+100
-3
lines changed

4 files changed

+100
-3
lines changed

Project.toml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ authors = ["Michael Schlottke-Lakemper <[email protected]>", "
44
version = "0.11.12-DEV"
55

66
[deps]
7-
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
87
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
8+
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
99
CodeTracking = "da1fd8a2-8d9e-5ec2-8556-3022fb5608a2"
1010
ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
1111
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
@@ -57,15 +57,18 @@ Convex = "f65535da-76fb-5f13-bab9-19810c17039a"
5757
ECOS = "e2685f51-7e38-5353-a97d-a921fd2c8199"
5858
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
5959
NLsolve = "2774e3e8-f4cf-5e23-947b-6d7e65073b56"
60+
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
6061

6162
[extensions]
6263
TrixiConvexECOSExt = ["Convex", "ECOS"]
6364
TrixiMakieExt = "Makie"
6465
TrixiNLsolveExt = "NLsolve"
66+
TrixiCUDAExt = "CUDA"
6567

6668
[compat]
67-
Adapt = "4"
6869
Accessors = "0.1.36"
70+
Adapt = "4"
71+
CUDA = "5"
6972
CodeTracking = "1.0.5"
7073
ConstructionBase = "1.5"
7174
Convex = "0.16"

docs/make.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,8 @@ makedocs(
163163
"Style guide" => "styleguide.md",
164164
"Testing" => "testing.md",
165165
"Performance" => "performance.md",
166-
"Parallelization" => "parallelization.md"
166+
"Parallelization" => "parallelization.md",
167+
"Heterogeneous" => "heterogeneous.md"
167168
],
168169
"Troubleshooting and FAQ" => "troubleshooting.md",
169170
"Reference" => [

docs/src/heterogeneous.md

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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 JuliaGPU family that allows for
8+
the translation of nested data structures. The primary goal is to allow the substitution of `Array`
9+
at the storage leaves with a GPU array like `CuArray`.
10+
11+
To facilitate this data structures must be parameterized, so instead of:
12+
13+
```julia
14+
struct Container
15+
data::Array{Float64,2}
16+
end
17+
```
18+
19+
They must be written as:
20+
21+
```julia
22+
struct Container{D<:AbstractArray} <: Trixi.AbstractContainer
23+
data::D
24+
end
25+
```
26+
27+
furthermore, we need to define a function that allows for the conversion of storage
28+
of our types:
29+
30+
```julia
31+
function Adapt.adapt_structure(to, C::Container)
32+
return Container(adapt(to, C.data))
33+
end
34+
```
35+
36+
or simply
37+
38+
```julia
39+
Adapt.@adapt_structure(Container)
40+
```
41+
42+
additionally, we must define `Adapt.parent_type`.
43+
44+
```julia
45+
function Adapt.parent_type(::Type{<:Container{D}}) where D
46+
return D
47+
end
48+
```
49+
50+
```julia-repl
51+
julia> C = Container(zeros(3))
52+
Container{Vector{Float64}}([0.0, 0.0, 0.0])
53+
54+
julia> Trixi.storage_type(C)
55+
Array
56+
57+
julia> using CUDA
58+
59+
julia> GPU_C = adapt(CuArray, C)
60+
Container{CuArray{Float64, 1, CUDA.DeviceMemory}}([0.0, 0.0, 0.0])
61+
62+
julia> Trixi.storage_type(C)
63+
CuArray
64+
```
65+
66+
## Element-type conversion with `Trixi.trixi_adapt`.
67+
68+
We can use Trixi.trixi_adapt to perform both an element-type and a storage-type adoption
69+
70+
```julia-repl
71+
julia> C = Container(zeros(3))
72+
Container{Vector{Float64}}([0.0, 0.0, 0.0])
73+
74+
julia> Trixi.trixi_adapt(Array, Float32, C)
75+
Container{Vector{Float32}}(Float32[0.0, 0.0, 0.0])
76+
77+
julia> Trixi.trixi_adapt(CuArray, Float32, C)
78+
Container{CuArray{Float32, 1, CUDA.DeviceMemory}}(Float32[0.0, 0.0, 0.0])
79+
```
80+
81+
!!! note
82+
`adapt(Array{Float32}, C)` is tempting but will do the wrong thing in the presence of `StaticArrays`.

ext/TrixiCUDAExt.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Package extension for adding CUDA-based features to Trixi.jl
2+
module TrixiCUDAExt
3+
4+
import CUDA: CuArray
5+
import Trixi
6+
7+
function Trixi.storage_type(::Type{<:CuArray})
8+
return CuArray
9+
end
10+
11+
end

0 commit comments

Comments
 (0)