From 60b251560f4b0b4fb1ca7d713e5309e7d64d9bbd Mon Sep 17 00:00:00 2001 From: huiyuxie Date: Thu, 8 Aug 2024 13:43:42 -1000 Subject: [PATCH] Set simple test for solvers --- .gitignore | 5 ++++- Project.toml | 1 + src/TrixiGPU.jl | 12 ++++++++---- src/solvers/dg_1d.jl | 24 ++++++++++++++++++++--- src/solvers/dg_2d.jl | 1 - src/solvers/dg_3d.jl | 2 -- test/test_auxiliary.jl | 18 +----------------- test/test_solvers.jl | 43 +++++++++++++++++++++++++++++------------- 8 files changed, 65 insertions(+), 41 deletions(-) delete mode 100644 src/solvers/dg_2d.jl delete mode 100644 src/solvers/dg_3d.jl diff --git a/.gitignore b/.gitignore index 2a822eb..e89aa13 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,6 @@ /Manifest.toml -.DS_Store \ No newline at end of file +.DS_Store + +# For developer use only +/test/trixi \ No newline at end of file diff --git a/Project.toml b/Project.toml index c3125ab..3248cf5 100644 --- a/Project.toml +++ b/Project.toml @@ -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" diff --git a/src/TrixiGPU.jl b/src/TrixiGPU.jl index 33b7d69..0cac011 100644 --- a/src/TrixiGPU.jl +++ b/src/TrixiGPU.jl @@ -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") diff --git a/src/solvers/dg_1d.jl b/src/solvers/dg_1d.jl index f75285d..3cdb7a4 100644 --- a/src/solvers/dg_1d.jl +++ b/src/solvers/dg_1d.jl @@ -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) @@ -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) @@ -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 diff --git a/src/solvers/dg_2d.jl b/src/solvers/dg_2d.jl deleted file mode 100644 index 51433d9..0000000 --- a/src/solvers/dg_2d.jl +++ /dev/null @@ -1 +0,0 @@ -# Solver functions for 2D DG methods diff --git a/src/solvers/dg_3d.jl b/src/solvers/dg_3d.jl deleted file mode 100644 index 6213d7b..0000000 --- a/src/solvers/dg_3d.jl +++ /dev/null @@ -1,2 +0,0 @@ -# Solver functions for 3D DG methods - diff --git a/test/test_auxiliary.jl b/test/test_auxiliary.jl index e5a717d..815b275 100644 --- a/test/test_auxiliary.jl +++ b/test/test_auxiliary.jl @@ -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 diff --git a/test/test_solvers.jl b/test/test_solvers.jl index fb13f69..9ecd84a 100644 --- a/test/test_solvers.jl +++ b/test/test_solvers.jl @@ -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)