Skip to content

Use Adapt.jl to change storage and element type #2212

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
2 changes: 2 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ authors = ["Michael Schlottke-Lakemper <[email protected]>", "Gregor
version = "0.11.2-DEV"

[deps]
Adapt = "79e6a3ab-5dfb-504d-930d-738a2a938a0e"
Accessors = "7d9f7c33-5ae7-4f3b-8dc6-eff91059b697"
CodeTracking = "da1fd8a2-8d9e-5ec2-8556-3022fb5608a2"
ConstructionBase = "187b0558-2788-49d3-abe0-74a17ed4e7c9"
Expand Down Expand Up @@ -63,6 +64,7 @@ TrixiMakieExt = "Makie"
TrixiNLsolveExt = "NLsolve"

[compat]
Adapt = "4"
Accessors = "0.1.36"
CodeTracking = "1.0.5"
ConstructionBase = "1.5"
Expand Down
2 changes: 2 additions & 0 deletions src/Trixi.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import SciMLBase: get_du, get_tmp_cache, u_modified!,

using DelimitedFiles: readdlm
using Downloads: Downloads
using Adapt: Adapt, adapt
using CodeTracking: CodeTracking
using ConstructionBase: ConstructionBase
using DiffEqBase: DiffEqBase, get_tstops, get_tstops_array
Expand Down Expand Up @@ -125,6 +126,7 @@ include("basic_types.jl")

# Include all top-level source files
include("auxiliary/auxiliary.jl")
include("auxiliary/vector_of_arrays.jl")
include("auxiliary/mpi.jl")
include("auxiliary/p4est.jl")
include("auxiliary/t8code.jl")
Expand Down
62 changes: 62 additions & 0 deletions src/auxiliary/containers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -314,4 +314,66 @@ end
function raw_copy!(c::AbstractContainer, from::Int, destination::Int)
raw_copy!(c, c, from, from, destination)
end

# Containers that support multiple storage backends
# should be subtypes of this type.
#
# The first type parameter must be `Array` by default and if
# `Adapt.adapt_structure(to, container)` is called then this must be
# `typeof(to)`. This is used in downstream code, e.g. for calling
# `wrap_array` with an appropriate type after `resize!`ing.
Comment on lines +321 to +324
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand this comment, in particular

The first type parameter must be Array by default and if Adapt.adapt_structure(to, container) is called then this must be typeof(to).

What is "this" referring to? To me, the sentence roughly reads like "the first type parameter must by Array and Array must always be the result of typeof(to)", which is very likely not the actual intent 😅

Could you please clarify a little?

abstract type AbstractHeterogeneousContainer{T} <: AbstractContainer end
array_type(::AbstractHeterogeneousContainer{T}) where {T} = T

# Subtypes must implement a method for these functions
function Adapt.adapt_structure(to, ::AbstractHeterogeneousContainer)
error("required method not implemented")
end

# For some storage backends like CUDA.jl, empty arrays do seem to simply be
# null pointers which can cause `unsafe_wrap` to fail when calling
# Adapt.adapt (ArgumentError, see
# https://github.com/JuliaGPU/CUDA.jl/blob/v5.4.2/src/array.jl#L212-L229).
# To circumvent this, on length zero arrays this allocates
# a separate empty array instead of wrapping.
# However, since zero length arrays are not used in calculations,
# it should be okay if the underlying storage vectors and wrapped arrays
# are not the same as long as they are properly wrapped when `resize!`d etc.
function unsafe_wrap_or_alloc(to, vec, size)
if length(vec) == 0
return similar(vec, size)
else
return unsafe_wrap(to, pointer(vec), size)
end
end
Comment on lines +342 to +348
Copy link
Member

@sloede sloede Feb 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
function unsafe_wrap_or_alloc(to, vec, size)
if length(vec) == 0
return similar(vec, size)
else
return unsafe_wrap(to, pointer(vec), size)
end
end
function unsafe_wrap_or_alloc(to, vector, size)
if length(vector) == 0
return similar(vector, size)
else
return unsafe_wrap(to, pointer(vector), size)
end
end

To not shadow Base.vec?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed the suggestion above. It's not necessary, from my point of view, to avoid shadowing here, but it's fine to be explicit and verbose.


struct TrixiAdaptor{Storage, Real} end

function trixi_adapt(storage, real, x)
adapt(TrixiAdaptor{storage, real}(), x)
end

# Custom rules
# 1. handling of StaticArrays
function Adapt.adapt_storage(::TrixiAdaptor{<:Any, Real}, x::StaticArrays.StaticArray{S, T, N}) where {Real,S,T,N}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
function Adapt.adapt_storage(::TrixiAdaptor{<:Any, Real}, x::StaticArrays.StaticArray{S, T, N}) where {Real,S,T,N}
function Adapt.adapt_storage(::TrixiAdaptor{<:Any, Real},
x::StaticArrays.StaticArray{S, T, N}) where {Real, S, T, N}

StaticArrays.similar_type(x, Real)(x)
end

# 2. Handling of Arrays
function Adapt.adapt_storage(::TrixiAdaptor{Storage, Real}, x::AbstractArray{T}) where{Storage, Real, T<:AbstractFloat}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
function Adapt.adapt_storage(::TrixiAdaptor{Storage, Real}, x::AbstractArray{T}) where{Storage, Real, T<:AbstractFloat}
function Adapt.adapt_storage(::TrixiAdaptor{Storage, Real},
x::AbstractArray{T}) where {Storage, Real,
T <: AbstractFloat}

adapt(Storage{Real}, x)
end

function Adapt.adapt_storage(::TrixiAdaptor{Storage, Real}, x::AbstractArray{T}) where {Storage, Real,T<:StaticArrays.StaticArray}
adapt(Storage{StaticArrays.similar_type(T, Real)},x)
Comment on lines +367 to +368
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
function Adapt.adapt_storage(::TrixiAdaptor{Storage, Real}, x::AbstractArray{T}) where {Storage, Real,T<:StaticArrays.StaticArray}
adapt(Storage{StaticArrays.similar_type(T, Real)},x)
function Adapt.adapt_storage(::TrixiAdaptor{Storage, Real},
x::AbstractArray{T}) where {Storage, Real,
T <: StaticArrays.StaticArray}
adapt(Storage{StaticArrays.similar_type(T, Real)}, x)

end

function Adapt.adapt_storage(::TrixiAdaptor{Storage, Real}, x::AbstractArray) where{Storage, Real}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
function Adapt.adapt_storage(::TrixiAdaptor{Storage, Real}, x::AbstractArray) where{Storage, Real}
function Adapt.adapt_storage(::TrixiAdaptor{Storage, Real},
x::AbstractArray) where {Storage, Real}

adapt(Storage, x)
end

# 3. TODO: Should we have a fallback? But that would imply implementing things for NamedTuple again

unsafe_wrap_or_alloc(::TrixiAdaptor{Storage}, vec, size) where {Storage} = unsafe_wrap_or_alloc(Storage, vec, size)

Comment on lines +377 to +378
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
unsafe_wrap_or_alloc(::TrixiAdaptor{Storage}, vec, size) where {Storage} = unsafe_wrap_or_alloc(Storage, vec, size)
unsafe_wrap_or_alloc(::TrixiAdaptor{Storage}, vec, size) where {Storage} = unsafe_wrap_or_alloc(Storage,
vec,
size)

end # @muladd
25 changes: 25 additions & 0 deletions src/auxiliary/vector_of_arrays.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# By default, Julia/LLVM does not use fused multiply-add operations (FMAs).
# Since these FMAs can increase the performance of many numerical algorithms,
# we need to opt-in explicitly.
# See https://ranocha.de/blog/Optimizing_EC_Trixi for further details.
@muladd begin
#! format: noindent

# Wraps a Vector of Arrays, forwards `getindex` to the underlying Vector.
# Implements `Adapt.adapt_structure` to allow offloading to the GPU which is
# not possible for a plain Vector of Arrays.
struct VecOfArrays{T <: AbstractArray}
arrays::Vector{T}
end
Base.getindex(v::VecOfArrays, i::Int) = Base.getindex(v.arrays, i)
Base.IndexStyle(v::VecOfArrays) = Base.IndexStyle(v.arrays)
Base.size(v::VecOfArrays) = Base.size(v.arrays)
Base.length(v::VecOfArrays) = Base.length(v.arrays)
Base.eltype(v::VecOfArrays{T}) where {T} = T
function Adapt.adapt_structure(to, v::VecOfArrays)
return VecOfArrays([Adapt.adapt(to, arr) for arr in v.arrays])
end
function Base.convert(::Type{<:VecOfArrays}, v::Vector{<:AbstractArray})
VecOfArrays(v)
end
Comment on lines +11 to +24
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
struct VecOfArrays{T <: AbstractArray}
arrays::Vector{T}
end
Base.getindex(v::VecOfArrays, i::Int) = Base.getindex(v.arrays, i)
Base.IndexStyle(v::VecOfArrays) = Base.IndexStyle(v.arrays)
Base.size(v::VecOfArrays) = Base.size(v.arrays)
Base.length(v::VecOfArrays) = Base.length(v.arrays)
Base.eltype(v::VecOfArrays{T}) where {T} = T
function Adapt.adapt_structure(to, v::VecOfArrays)
return VecOfArrays([Adapt.adapt(to, arr) for arr in v.arrays])
end
function Base.convert(::Type{<:VecOfArrays}, v::Vector{<:AbstractArray})
VecOfArrays(v)
end
struct VectorOfArrays{T <: AbstractArray}
arrays::Vector{T}
end
Base.getindex(v::VectorOfArrays, i::Int) = Base.getindex(v.arrays, i)
Base.IndexStyle(v::VectorOfArrays) = Base.IndexStyle(v.arrays)
Base.size(v::VectorOfArrays) = Base.size(v.arrays)
Base.length(v::VectorOfArrays) = Base.length(v.arrays)
Base.eltype(v::VectorOfArrays{T}) where {T} = T
function Adapt.adapt_structure(to, v::VectorOfArrays)
return VectorOfArrays([Adapt.adapt(to, arr) for arr in v.arrays])
end
function Base.convert(::Type{<:VectorOfArrays}, v::Vector{<:AbstractArray})
VectorOfArrays(v)
end

We try to avoid abbreviations unless really necessary for sanity 😬 Also, it would be consistent with the file name.

Note, however, that I do not have a super strong opinion here, thus if you deem VecOfArrays to be better, we can stay like this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just saw that RecursiveArrayTools.jl seems to already have a VectorOfArrays type (https://github.com/trixi-framework/Trixi.jl/pull/2150/files#diff-6058d487915aa254cfe1a5f8da6315b8c1fef4da04df38b0089a599803e28e5aR63). Should we use that one instead?

If not, then of course we should use a different name for our own data structure, and keeping VecOfArrays as the name seems reasonable to me. In that case, it might be good to leave a short note in the comment why we do not want to use the SciML package's type for future reference.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using RecursiveArrayTools: VectorOfArray is added in #2150 for DGMulti support

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I saw that. I was just wondering whether their data type could (or should) be used instead of rolling our own.

end # @muladd
51 changes: 30 additions & 21 deletions src/semidiscretization/semidiscretization_hyperbolic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,6 @@ mutable struct SemidiscretizationHyperbolic{Mesh, Equations, InitialCondition,
solver::Solver
cache::Cache
performance_counter::PerformanceCounter

function SemidiscretizationHyperbolic{Mesh, Equations, InitialCondition,
BoundaryConditions, SourceTerms, Solver,
Cache}(mesh::Mesh, equations::Equations,
initial_condition::InitialCondition,
boundary_conditions::BoundaryConditions,
source_terms::SourceTerms,
solver::Solver,
cache::Cache) where {Mesh, Equations,
InitialCondition,
BoundaryConditions,
SourceTerms,
Solver,
Cache}
performance_counter = PerformanceCounter()

new(mesh, equations, initial_condition, boundary_conditions, source_terms,
solver, cache, performance_counter)
end
end

"""
Expand Down Expand Up @@ -74,15 +55,43 @@ function SemidiscretizationHyperbolic(mesh, equations, initial_condition, solver

check_periodicity_mesh_boundary_conditions(mesh, _boundary_conditions)

performance_counter = PerformanceCounter()

SemidiscretizationHyperbolic{typeof(mesh), typeof(equations),
typeof(initial_condition),
typeof(_boundary_conditions), typeof(source_terms),
typeof(solver), typeof(cache)}(mesh, equations,
initial_condition,
_boundary_conditions,
source_terms, solver,
cache)
end
cache,
performance_counter)
end

@eval Adapt.@adapt_structure(SemidiscretizationHyperbolic)
# function Adapt.adapt_structure(to, semi::SemidiscretizationHyperbolic)
# if !(typeof(semi.mesh) <: P4estMesh)
# error("Adapt.adapt is only supported for semidiscretizations based on P4estMesh")
# end

# mesh = semi.mesh
# equations = adapt(to, semi.equations)
# initial_condition = adapt(to, semi.initial_condition)
# boundary_conditions = adapt(to, semi.boundary_conditions)
# source_terms = adapt(to, semi.source_terms)
# solver = adapt(to, semi.solver)
# cache = adapt(to, semi.cache)
# performance_counter = semi.performance_counter

# SemidiscretizationHyperbolic{typeof(mesh), typeof(equations),
# typeof(initial_condition),
# typeof(boundary_conditions), typeof(source_terms),
# typeof(solver), typeof(cache)}(mesh, equations,
# initial_condition,
# boundary_conditions,
# source_terms, solver,
# cache, performance_counter)
# end

# Create a new semidiscretization but change some parameters compared to the input.
# `Base.similar` follows a related concept but would require us to `copy` the `mesh`,
Expand Down
2 changes: 2 additions & 0 deletions src/solvers/dg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,8 @@ struct DG{Basis, Mortar, SurfaceIntegral, VolumeIntegral}
volume_integral::VolumeIntegral
end

@eval Adapt.@adapt_structure(DG)

function Base.show(io::IO, dg::DG)
@nospecialize dg # reduce precompilation time

Expand Down
36 changes: 36 additions & 0 deletions src/solvers/dgsem/basis_lobatto_legendre.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,32 @@ struct LobattoLegendreBasis{RealT <: Real, NNODES,
# negative adjoint wrt the SBP dot product
end

function Adapt.adapt_structure(to, basis::LobattoLegendreBasis)
inverse_vandermonde_legendre = adapt(to, basis.inverse_vandermonde_legendre)
RealT = eltype(inverse_vandermonde_legendre)

nodes = SVector{<:Any, RealT}(basis.nodes)
weights = SVector{<:Any, RealT}(basis.weights)
inverse_weights = SVector{<:Any, RealT}(basis.inverse_weights)
boundary_interpolation = adapt(to, basis.boundary_interpolation)
derivative_matrix = adapt(to, basis.derivative_matrix)
derivative_split = adapt(to, basis.derivative_split)
derivative_split_transpose = adapt(to,basis.derivative_split_transpose)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
derivative_split_transpose = adapt(to,basis.derivative_split_transpose)
derivative_split_transpose = adapt(to, basis.derivative_split_transpose)

derivative_dhat = adapt(to, basis.derivative_dhat)
return LobattoLegendreBasis{RealT, nnodes(basis), typeof(nodes),
typeof(inverse_vandermonde_legendre),
typeof(boundary_interpolation),
typeof(derivative_matrix)}(nodes,
weights,
inverse_weights,
inverse_vandermonde_legendre,
boundary_interpolation,
derivative_matrix,
derivative_split,
derivative_split_transpose,
derivative_dhat)
end

function LobattoLegendreBasis(RealT, polydeg::Integer)
nnodes_ = polydeg + 1

Expand Down Expand Up @@ -155,6 +181,16 @@ struct LobattoLegendreMortarL2{RealT <: Real, NNODES,
reverse_lower::ReverseMatrix
end

function Adapt.adapt_structure(to, mortar::LobattoLegendreMortarL2)
forward_upper = adapt(to, mortar.forward_upper)
forward_lower = adapt(to, mortar.forward_lower)
reverse_upper = adapt(to, mortar.reverse_upper)
reverse_lower = adapt(to, mortar.reverse_lower)
return LobattoLegendreMortarL2{eltype(forward_upper), nnodes(mortar), typeof(forward_upper),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
return LobattoLegendreMortarL2{eltype(forward_upper), nnodes(mortar), typeof(forward_upper),
return LobattoLegendreMortarL2{eltype(forward_upper), nnodes(mortar),
typeof(forward_upper),

typeof(reverse_upper)}(forward_upper, forward_lower,
reverse_upper, reverse_lower)
end

function MortarL2(basis::LobattoLegendreBasis)
RealT = real(basis)
nnodes_ = nnodes(basis)
Expand Down
Loading
Loading