Skip to content

Commit

Permalink
Create cache for GPU arrays to optimize data transfer (#53)
Browse files Browse the repository at this point in the history
* Start

* Rename API

* Add comments

* Fix typos

* Complete 1D

* Complete 2D

* Complete 3D
  • Loading branch information
huiyuxie authored Sep 24, 2024
1 parent d8deac8 commit 80a3b75
Show file tree
Hide file tree
Showing 37 changed files with 387 additions and 178 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition_convergen
###############################################################################
# ODE solvers, callbacks etc.

ode = semidiscretize_gpu(semi, (0.0, 1.0)) # from TrixiCUDA.jl
ode = semidiscretizeGPU(semi, (0.0, 1.0)) # from TrixiCUDA.jl

summary_callback = SummaryCallback()

Expand Down
2 changes: 1 addition & 1 deletion examples/advection_basic_1d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition_convergen
# ODE solvers, callbacks etc.

# Create ODE problem with time span from 0.0 to 1.0
ode = semidiscretize_gpu(semi, (0.0, 1.0)) # from TrixiCUDA.jl
ode = semidiscretizeGPU(semi, (0.0, 1.0)) # from TrixiCUDA.jl

# At the beginning of the main loop, the SummaryCallback prints a summary of the simulation setup
# and resets the timers
Expand Down
2 changes: 1 addition & 1 deletion examples/advection_basic_2d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition_convergen
# ODE solvers, callbacks etc.

# Create ODE problem with time span from 0.0 to 1.0
ode = semidiscretize_gpu(semi, (0.0, 1.0)) # from TrixiCUDA.jl
ode = semidiscretizeGPU(semi, (0.0, 1.0)) # from TrixiCUDA.jl

# At the beginning of the main loop, the SummaryCallback prints a summary of the simulation setup
# and resets the timers
Expand Down
2 changes: 1 addition & 1 deletion examples/advection_basic_3d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition_convergen
# ODE solvers, callbacks etc.

# Create ODE problem with time span from 0.0 to 1.0
ode = semidiscretize_gpu(semi, (0.0, 1.0)) # from TrixiCUDA.jl
ode = semidiscretizeGPU(semi, (0.0, 1.0)) # from TrixiCUDA.jl

# At the beginning of the main loop, the SummaryCallback prints a summary of the simulation setup
# and resets the timers
Expand Down
2 changes: 1 addition & 1 deletion examples/advection_mortar_2d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition, solver)
# ODE solvers, callbacks etc.

tspan = (0.0, 1.0)
ode = semidiscretize_gpu(semi, tspan) # from TrixiCUDA.jl
ode = semidiscretizeGPU(semi, tspan) # from TrixiCUDA.jl

summary_callback = SummaryCallback()

Expand Down
2 changes: 1 addition & 1 deletion examples/advection_mortar_3d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition, solver)
# ODE solvers, callbacks etc.

tspan = (0.0, 5.0)
ode = semidiscretize_gpu(semi, tspan) # from TrixiCUDA.jl
ode = semidiscretizeGPU(semi, tspan) # from TrixiCUDA.jl

summary_callback = SummaryCallback()

Expand Down
2 changes: 1 addition & 1 deletion examples/euler_ec_1d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition, solver)
# ODE solvers, callbacks etc.

tspan = (0.0, 0.4)
ode = semidiscretize_gpu(semi, tspan) # from TrixiCUDA.jl
ode = semidiscretizeGPU(semi, tspan) # from TrixiCUDA.jl

summary_callback = SummaryCallback()

Expand Down
2 changes: 1 addition & 1 deletion examples/euler_ec_2d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition, solver,
# ODE solvers, callbacks etc.

tspan = (0.0, 0.4)
ode = semidiscretize_gpu(semi, tspan) # from TrixiCUDA.jl
ode = semidiscretizeGPU(semi, tspan) # from TrixiCUDA.jl

summary_callback = SummaryCallback()

Expand Down
2 changes: 1 addition & 1 deletion examples/euler_ec_3d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition, solver)
# ODE solvers, callbacks etc.

tspan = (0.0, 0.4)
ode = semidiscretize_gpu(semi, tspan) # from TrixiCUDA.jl
ode = semidiscretizeGPU(semi, tspan) # from TrixiCUDA.jl

summary_callback = SummaryCallback()

Expand Down
2 changes: 1 addition & 1 deletion examples/euler_shockcapturing_1d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition, solver)
# ODE solvers, callbacks etc.

tspan = (0.0, 1.0)
ode = semidiscretize_gpu(semi, tspan) # from TrixiCUDA.jl
ode = semidiscretizeGPU(semi, tspan) # from TrixiCUDA.jl

summary_callback = SummaryCallback()

Expand Down
2 changes: 1 addition & 1 deletion examples/euler_shockcapturing_2d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition, solver)
# ODE solvers, callbacks etc.

tspan = (0.0, 1.0)
ode = semidiscretize_gpu(semi, tspan) # from TrixiCUDA.jl
ode = semidiscretizeGPU(semi, tspan) # from TrixiCUDA.jl

summary_callback = SummaryCallback()

Expand Down
2 changes: 1 addition & 1 deletion examples/euler_shockcapturing_3d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition, solver)
# ODE solvers, callbacks etc.

tspan = (0.0, 0.4)
ode = semidiscretize_gpu(semi, tspan) # from TrixiCUDA.jl
ode = semidiscretizeGPU(semi, tspan) # from TrixiCUDA.jl

summary_callback = SummaryCallback()

Expand Down
2 changes: 1 addition & 1 deletion examples/euler_source_terms_1d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition, solver,
# ODE solvers, callbacks etc.

tspan = (0.0, 2.0)
ode = semidiscretize_gpu(semi, tspan) # from TrixiCUDA.jl
ode = semidiscretizeGPU(semi, tspan) # from TrixiCUDA.jl

summary_callback = SummaryCallback()

Expand Down
2 changes: 1 addition & 1 deletion examples/euler_source_terms_2d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition, solver,
# ODE solvers, callbacks etc.

tspan = (0.0, 2.0)
ode = semidiscretize_gpu(semi, tspan) # from TrixiCUDA.jl
ode = semidiscretizeGPU(semi, tspan) # from TrixiCUDA.jl

summary_callback = SummaryCallback()

Expand Down
2 changes: 1 addition & 1 deletion examples/euler_source_terms_3d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition, solver,
# ODE solvers, callbacks etc.

tspan = (0.0, 5.0)
ode = semidiscretize_gpu(semi, tspan) # from TrixiCUDA.jl
ode = semidiscretizeGPU(semi, tspan) # from TrixiCUDA.jl

summary_callback = SummaryCallback()

Expand Down
2 changes: 1 addition & 1 deletion examples/eulermulti_ec_1d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition, solver)
# ODE solvers, callbacks etc.

tspan = (0.0, 0.4)
ode = semidiscretize_gpu(semi, tspan) # from TrixiCUDA.jl
ode = semidiscretizeGPU(semi, tspan) # from TrixiCUDA.jl

summary_callback = SummaryCallback()

Expand Down
2 changes: 1 addition & 1 deletion examples/eulermulti_ec_2d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition, solver)
# ODE solvers, callbacks etc.

tspan = (0.0, 0.4)
ode = semidiscretize_gpu(semi, tspan) # from TrixiCUDA.jl
ode = semidiscretizeGPU(semi, tspan) # from TrixiCUDA.jl

summary_callback = SummaryCallback()

Expand Down
2 changes: 1 addition & 1 deletion examples/hypdiff_nonperiodic_1d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition, solver,
# ODE solvers, callbacks etc.

tspan = (0.0, 5.0)
ode = semidiscretize_gpu(semi, tspan) # from TrixiCUDA.jl
ode = semidiscretizeGPU(semi, tspan) # from TrixiCUDA.jl

summary_callback = SummaryCallback()

Expand Down
2 changes: 1 addition & 1 deletion examples/hypdiff_nonperiodic_2d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition, solver,
# ODE solvers, callbacks etc.

tspan = (0.0, 5.0)
ode = semidiscretize_gpu(semi, tspan) # from TrixiCUDA.jl
ode = semidiscretizeGPU(semi, tspan) # from TrixiCUDA.jl

summary_callback = SummaryCallback()

Expand Down
2 changes: 1 addition & 1 deletion examples/hypdiff_nonperiodic_3d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition, solver,
# ODE solvers, callbacks etc.

tspan = (0.0, 5.0)
ode = semidiscretize_gpu(semi, tspan) # from TrixiCUDA.jl
ode = semidiscretizeGPU(semi, tspan) # from TrixiCUDA.jl

summary_callback = SummaryCallback()

Expand Down
2 changes: 1 addition & 1 deletion examples/shallowwater_dirichlet_1d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition, solver,
# ODE solvers, callbacks etc.

tspan = (0.0, 1.0)
ode = semidiscretize_gpu(semi, tspan) # from TrixiCUDA.jl
ode = semidiscretizeGPU(semi, tspan) # from TrixiCUDA.jl

summary_callback = SummaryCallback()

Expand Down
2 changes: 1 addition & 1 deletion examples/shallowwater_dirichlet_2d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition, solver,
# ODE solvers, callbacks etc.

tspan = (0.0, 1.0)
ode = semidiscretize_gpu(semi, tspan) # from TrixiCUDA.jl
ode = semidiscretizeGPU(semi, tspan) # from TrixiCUDA.jl

summary_callback = SummaryCallback()

Expand Down
2 changes: 1 addition & 1 deletion examples/shallowwater_ec_1d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition, solver)
# ODE solver

tspan = (0.0, 2.0)
ode = semidiscretize_gpu(semi, tspan) # from TrixiCUDA.jl
ode = semidiscretizeGPU(semi, tspan) # from TrixiCUDA.jl

###############################################################################
# Callbacks
Expand Down
2 changes: 1 addition & 1 deletion examples/shallowwater_ec_2d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition, solver)
# ODE solver

tspan = (0.0, 2.0)
ode = semidiscretize_gpu(semi, tspan) # from TrixiCUDA.jl
ode = semidiscretizeGPU(semi, tspan) # from TrixiCUDA.jl

###############################################################################
# Workaround to set a discontinuous bottom topography and initial condition for debugging and testing.
Expand Down
2 changes: 1 addition & 1 deletion examples/shallowwater_source_terms_1d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition, solver,
# ODE solvers, callbacks etc.

tspan = (0.0, 1.0)
ode = semidiscretize_gpu(semi, tspan) # from TrixiCUDA.jl
ode = semidiscretizeGPU(semi, tspan) # from TrixiCUDA.jl

summary_callback = SummaryCallback()

Expand Down
2 changes: 1 addition & 1 deletion examples/shallowwater_source_terms_2d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition, solver,
# ODE solvers, callbacks etc.

tspan = (0.0, 1.0)
ode = semidiscretize_gpu(semi, tspan) # from TrixiCUDA.jl
ode = semidiscretizeGPU(semi, tspan) # from TrixiCUDA.jl

summary_callback = SummaryCallback()

Expand Down
15 changes: 11 additions & 4 deletions src/TrixiCUDA.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ module TrixiCUDA
# Include other packages that are used in TrixiCUDA.jl
# using Reexport: @reexport

using CUDA
using CUDA: @cuda, CuArray, HostKernel,
threadIdx, blockIdx, blockDim, similar, launch_configuration

using Trixi: AbstractEquations, TreeMesh, DGSEM,
using Trixi: AbstractEquations, True, False,
TreeMesh, DGSEM,
BoundaryConditionPeriodic, SemidiscretizationHyperbolic,
VolumeIntegralWeakForm, VolumeIntegralFluxDifferencing, VolumeIntegralShockCapturingHG,
flux, ntuple, nvariables,
True, False,
LobattoLegendreMortarL2,
flux, ntuple, nvariables, nnodes, nelements, nmortars,
local_leaf_cells, init_elements, init_interfaces, init_boundaries, init_mortars,
wrap_array, compute_coefficients, have_nonconservative_terms,
boundary_condition_periodic,
digest_boundary_conditions, check_periodicity_mesh_boundary_conditions,
set_log_type!, set_sqrt_type!

import Trixi: get_node_vars, get_node_coords, get_surface_node_vars
Expand All @@ -25,12 +29,15 @@ using StaticArrays: SVector

# Include other source files
include("auxiliary/auxiliary.jl")
include("semidiscretization/semidiscretization.jl")
include("solvers/solvers.jl")

# Change to use the Base.log and Base.sqrt - need to be fixed to avoid outputs
set_log_type!("log_Base")
set_sqrt_type!("sqrt_Base")

# Export the public APIs
export semidiscretize_gpu
export SemidiscretizationHyperbolicGPU
export semidiscretizeGPU

end
2 changes: 1 addition & 1 deletion src/auxiliary/auxiliary.jl
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
include("configurators.jl")
include("helpers.jl")
include("stable.jl")
13 changes: 9 additions & 4 deletions src/auxiliary/helpers.jl → src/auxiliary/stable.jl
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
# Some helper functions and function extensions from Trixi.jl.
# Some helper functions and function extensions that are invoked by GPU
# kernels, mainly to ensure the functions themselves or other functions
# remain stable on the GPU.

# Ref: `get_node_vars(u, equations, solver::DG, indices...)` in Trixi.jl
# See also `get_node_vars(u, equations, solver::DG, indices...)` in Trixi.jl
# `DG` type is not stable on GPU
@inline function get_node_vars(u, equations, indices...)
return SVector(ntuple(@inline(v->u[v, indices...]),
Val(nvariables(equations))))
end

# Ref: `get_node_coords(x, equations, solver::DG, indices...)` in Trixi.jl
# See also `get_node_coords(x, equations, solver::DG, indices...)` in Trixi.jl
# `DG` type is not stable on GPU
@inline function get_node_coords(x, equations, indices...)
return SVector(ntuple(@inline(idx->x[idx, indices...]),
Val(ndims(equations))))
end

# Ref: `get_surface_node_vars(u, equations, solver::DG, indices...)` in Trixi.jl
# See also `get_surface_node_vars(u, equations, solver::DG, indices...)` in Trixi.jl
# `DG` type is not stable on GPU
@inline function get_surface_node_vars(u, equations, indices...)
u_ll = SVector(ntuple(@inline(v->u[1, v, indices...]),
Val(nvariables(equations))))
Expand Down
1 change: 1 addition & 0 deletions src/semidiscretization/semidiscretization.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
include("semidiscretization_hyperbolic.jl")
27 changes: 27 additions & 0 deletions src/semidiscretization/semidiscretization_hyperbolic.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# This file is part of the package `Semidiscretizations.jl`.

function SemidiscretizationHyperbolicGPU(mesh, equations, initial_condition, solver;
source_terms = nothing,
boundary_conditions = boundary_condition_periodic,
# `RealT` is used as real type for node locations etc.
# while `uEltype` is used as element type of solutions etc.
RealT = real(solver), uEltype = RealT,
initial_cache = NamedTuple())
@assert ndims(mesh) == ndims(equations)

cache = (; create_cache_gpu(mesh, equations, solver, RealT, uEltype)...,
initial_cache...)
_boundary_conditions = digest_boundary_conditions(boundary_conditions, mesh, solver,
cache)

check_periodicity_mesh_boundary_conditions(mesh, _boundary_conditions)

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
Loading

0 comments on commit 80a3b75

Please sign in to comment.