Skip to content

Commit

Permalink
Set simple test for solvers
Browse files Browse the repository at this point in the history
  • Loading branch information
huiyuxie committed Aug 8, 2024
1 parent 6796e7e commit 60b2515
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 41 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/Manifest.toml

.DS_Store
.DS_Store

# For developer use only
/test/trixi
1 change: 1 addition & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ version = "1.0.0-DEV"
[deps]
BenchmarkTools = "6e4b80f9-dd63-53aa-95a3-0cdb28fa8baf"
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
StaticArrays = "90137ffa-7385-5640-81b9-e52037218182"
StrideArrays = "d1fa6d79-ef01-42a6-86c9-f7c551f8593b"
Trixi = "a7f1ee26-1774-49b1-8366-f1abc58fbfcb"
TrixiBase = "9a0f1c46-06d5-4909-a5a3-ce25d3fa3284"
Expand Down
12 changes: 8 additions & 4 deletions src/TrixiGPU.jl
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
module TrixiGPU

# Include other packages that are used in TrixiGPU.jl
# Include other packages that are used in TrixiGPU.jl (? reorder)
# using Reexport: @reexport

using CUDA: @cuda, CuArray, HostKernel, launch_configuration,
threadIdx, blockIdx, blockDim
using Trixi: AbstractEquations
using CUDA: @cuda, CuArray, HostKernel,
threadIdx, blockIdx, blockDim, similar,
launch_configuration
using Trixi: AbstractEquations, TreeMesh, VolumeIntegralWeakForm, DGSEM,
flux, ntuple, nvariables

import Trixi: get_node_vars, get_node_coords, get_surface_node_vars

using StrideArrays: PtrArray

using StaticArrays: SVector

# Include other source files
include("function.jl")
include("auxiliary/auxiliary.jl")
Expand Down
24 changes: 21 additions & 3 deletions src/solvers/dg_1d.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ function flux_kernel!(flux_arr, u, flux::Function, equations::AbstractEquations{
k = (blockIdx().y - 1) * blockDim().y + threadIdx().y

if (j <= size(u, 2) && k <= size(u, 3))
u_node = get_nodes_vars(u, equations, j, k)
u_node = get_node_vars(u, equations, j, k)

flux_node = flux(u_node, 1, equations)

Expand Down Expand Up @@ -44,8 +44,8 @@ function volume_flux_kernel!(volume_flux_arr, u, volume_flux::Function,
j1 = div(j - 1, size(u, 2)) + 1
j2 = rem(j - 1, size(u, 2)) + 1

u_node = get_nodes_vars(u, equations, j1, k)
u_node1 = get_nodes_vars(u, equations, j2, k)
u_node = get_node_vars(u, equations, j1, k)
u_node1 = get_node_vars(u, equations, j2, k)

volume_flux_node = volume_flux(u_node, u_node1, 1, equations)

Expand All @@ -58,3 +58,21 @@ function volume_flux_kernel!(volume_flux_arr, u, volume_flux::Function,

return nothing
end

function cuda_volume_integral!(du, u, mesh::TreeMesh{1}, nonconservative_terms, equations,
volume_integral::VolumeIntegralWeakForm, dg::DGSEM)
derivative_dhat = CuArray{Float32}(dg.basis.derivative_dhat)
flux_arr = similar(u)

size_arr = CuArray{Float32}(undef, size(u, 2), size(u, 3))

flux_kernel = @cuda launch=false flux_kernel!(flux_arr, u, flux, equations)
flux_kernel(flux_arr, u, flux, equations; configurator_2d(flux_kernel, size_arr)...)

weak_form_kernel = @cuda launch=false weak_form_kernel!(du, derivative_dhat, flux_arr,
equations)
weak_form_kernel(du, derivative_dhat, flux_arr, equations;
configurator_3d(weak_form_kernel, du)...,)

return nothing
end
1 change: 0 additions & 1 deletion src/solvers/dg_2d.jl

This file was deleted.

2 changes: 0 additions & 2 deletions src/solvers/dg_3d.jl

This file was deleted.

18 changes: 1 addition & 17 deletions test/test_auxiliary.jl
Original file line number Diff line number Diff line change
@@ -1,20 +1,4 @@
using TrixiGPU, CUDA
using Test

@testset "Test auxiliary functions" begin
@testset "CUDA congifurator 1D" begin
function kernel_add!(a, b, c)
i = threadIdx().x
c[i] = a[i] + b[i]
return
end

N = 256
a = CuArray(fill(1.0f0, N))
b = CuArray(fill(2.0f0, N))
c = CuArray(zeros(Float32, N))

sample_kernel = @cuda threads=N kernel_add!(a, b, c)
@test configurator_1d(sample_kernel, a) == (threads = 256, blocks = 1)
end
end
@testset "Test auxiliary functions" begin end
43 changes: 30 additions & 13 deletions test/test_solvers.jl
Original file line number Diff line number Diff line change
@@ -1,22 +1,39 @@
using Trixi, TrixiGPU
using CUDA: @cuda, CuArray
using Test

# @testset "Test solver functions" begin

# weak_form_kernel = @cuda launch=false weak_form_kernel!(du, derivative_dhat, flux_arr)
# end

# The header part for testing basic kernels in 1D
advection_velocity = 1.0f0
equations = LinearScalarAdvectionEquation1D(advection_velocity)

du = CuArray{Float64}(undef, 10, 10, 10)
derivative_dhat = CuArray{Float64}(undef, 10, 10)
flux_arr = CuArray{Float64}(undef, 10, 10, 10)
coordinates_min = -1.0f0
coordinates_max = 1.0f0
mesh = TreeMesh(coordinates_min,
coordinates_max,
initial_refinement_level = 4,
n_cells_max = 30_000)
solver = DGSEM(polydeg = 3, surface_flux = flux_lax_friedrichs)

function initial_condition_sine_wave(x, t, equations)
SVector(1.0f0 + 0.5f0 * sinpi(sum(x - equations.advection_velocity * t)))
end

semi = SemidiscretizationHyperbolic(mesh, equations, initial_condition_sine_wave, solver)

@unpack mesh, equations, initial_condition, boundary_conditions, source_terms, solver, cache = semi

t = 0.0f0
tspan = (0.0f0, 1.0f0)

ode = semidiscretize(semi, tspan)
u_ode = copy(ode.u0)
du_ode = similar(u_ode)
u = Trixi.wrap_array(u_ode, mesh, equations, solver, cache)
du = Trixi.wrap_array(du_ode, mesh, equations, solver, cache)

du, u = TrixiGPU.copy_to_device!(du, u)

weak_form_kernel = @cuda launch=false TrixiGPU.weak_form_kernel!(du, derivative_dhat,
flux_arr, equations)
weak_form_kernel(du,
derivative_dhat,
flux_arr, equations;
TrixiGPU.configurator_3d(weak_form_kernel, du)...,)
TrixiGPU.cuda_volume_integral!(du, u, mesh,
Trixi.have_nonconservative_terms(equations), equations,
solver.volume_integral, solver)

0 comments on commit 60b2515

Please sign in to comment.