diff --git a/test/test_projects/Diffusion3D/Project.toml b/test/test_projects/Diffusion3D/Project.toml new file mode 100644 index 00000000..335a2167 --- /dev/null +++ b/test/test_projects/Diffusion3D/Project.toml @@ -0,0 +1,18 @@ +name = "Diffusion3D" +uuid = "aac712e6-b0d5-474a-aefa-bdd351e14480" +authors = ["Samuel Omlin "] +version = "0.1.0" + +[deps] +ParallelStencil = "94395366-693c-11ea-3b26-d9b7aac5d958" + +[compat] +ParallelStencil = ">= 0.4.0" + +[weakdeps] +AMDGPU = "21141c5a-9bdb-4563-92ae-f87d6854732e" +CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba" + +[extensions] +#AMDGPUExt = "AMDGPU" +CUDAExt = "CUDA" diff --git a/test/test_projects/Diffusion3D/ext/AMDGPUExt.jl b/test/test_projects/Diffusion3D/ext/AMDGPUExt.jl new file mode 100644 index 00000000..2e75b848 --- /dev/null +++ b/test/test_projects/Diffusion3D/ext/AMDGPUExt.jl @@ -0,0 +1,9 @@ +module AMDGPUExt + using Diffusion3D + import AMDGPU + using ParallelStencil + using ParallelStencil.FiniteDifferences3D + @init_parallel_stencil(AMDGPU, Float64, 3) + Diffusion3D.diffusion3D(backend::Type{<:AMDGPUBackend}) = (@info "using AMDGPU backend"; diffusion3D()) + include(joinpath(@__DIR__, "..", "src", "backends", "diffusion3D.jl")) +end diff --git a/test/test_projects/Diffusion3D/ext/CUDAExt.jl b/test/test_projects/Diffusion3D/ext/CUDAExt.jl new file mode 100644 index 00000000..e6350e5b --- /dev/null +++ b/test/test_projects/Diffusion3D/ext/CUDAExt.jl @@ -0,0 +1,9 @@ +module CUDAExt + using Diffusion3D + import CUDA + using ParallelStencil + using ParallelStencil.FiniteDifferences3D + @init_parallel_stencil(CUDA, Float64, 3) + Diffusion3D.diffusion3D(backend::Type{<:CUDABackend}) = (@info "using CUDA backend"; diffusion3D()) + include(joinpath(@__DIR__, "..", "src", "backends", "diffusion3D.jl")) +end diff --git a/test/test_projects/Diffusion3D/src/CPU.jl b/test/test_projects/Diffusion3D/src/CPU.jl new file mode 100644 index 00000000..f46523e6 --- /dev/null +++ b/test/test_projects/Diffusion3D/src/CPU.jl @@ -0,0 +1,8 @@ +module CPU + using ..Diffusion3D + using ParallelStencil + using ParallelStencil.FiniteDifferences3D + @init_parallel_stencil(Threads, Float64, 3) + Diffusion3D.diffusion3D(backend::Type{<:CPUBackend}) = (@info "using CPU backend"; diffusion3D()) + include(joinpath(@__DIR__, "..", "src", "backends", "diffusion3D.jl")) +end diff --git a/test/test_projects/Diffusion3D/src/Diffusion3D.jl b/test/test_projects/Diffusion3D/src/Diffusion3D.jl new file mode 100644 index 00000000..e35add44 --- /dev/null +++ b/test/test_projects/Diffusion3D/src/Diffusion3D.jl @@ -0,0 +1,11 @@ +module Diffusion3D + export CPUBackend, CUDABackend, AMDGPUBackend, diffusion3D + + abstract type AbstractBackend end + struct CPUBackend <: AbstractBackend end + struct CUDABackend <: AbstractBackend end + struct AMDGPUBackend <: AbstractBackend end + + diffusion3D(backend::Type{<:AbstractBackend}) = error("module not loaded or diffusion3D not implemented for backend: $backend") + include("CPU.jl") +end diff --git a/test/test_projects/Diffusion3D/src/backends/diffusion3D.jl b/test/test_projects/Diffusion3D/src/backends/diffusion3D.jl new file mode 100644 index 00000000..c60663cd --- /dev/null +++ b/test/test_projects/Diffusion3D/src/backends/diffusion3D.jl @@ -0,0 +1,39 @@ +@parallel function diffusion3D_step!(T2, T, Ci, lam, dt, dx, dy, dz) + @inn(T2) = @inn(T) + dt*(lam*@inn(Ci)*(@d2_xi(T)/dx^2 + @d2_yi(T)/dy^2 + @d2_zi(T)/dz^2)); + return +end + +function diffusion3D() +# Physics +lam = 1.0; # Thermal conductivity +cp_min = 1.0; # Minimal heat capacity +lx, ly, lz = 10.0, 10.0, 10.0; # Length of computational domain in dimension x, y and z + +# Numerics +nx, ny, nz = 8, 8, 8; # Number of gridpoints in dimensions x, y and z +nt = 3; # Number of time steps +dx = lx/(nx-1); # Space step in x-dimension +dy = ly/(ny-1); # Space step in y-dimension +dz = lz/(nz-1); # Space step in z-dimension + +# Array initializations +T = @zeros(nx, ny, nz); +T2 = @zeros(nx, ny, nz); +Ci = @zeros(nx, ny, nz); + +# Initial conditions (heat capacity and temperature with two Gaussian anomalies each) +Ci .= 1.0./( cp_min .+ Data.Array([5*exp(-(((ix-1)*dx-lx/1.5))^2-(((iy-1)*dy-ly/2))^2-(((iz-1)*dz-lz/1.5))^2) + + 5*exp(-(((ix-1)*dx-lx/3.0))^2-(((iy-1)*dy-ly/2))^2-(((iz-1)*dz-lz/1.5))^2) for ix=1:size(T,1), iy=1:size(T,2), iz=1:size(T,3)]) ) +T .= Data.Array([100*exp(-(((ix-1)*dx-lx/2)/2)^2-(((iy-1)*dy-ly/2)/2)^2-(((iz-1)*dz-lz/3.0)/2)^2) + + 50*exp(-(((ix-1)*dx-lx/2)/2)^2-(((iy-1)*dy-ly/2)/2)^2-(((iz-1)*dz-lz/1.5)/2)^2) for ix=1:size(T,1), iy=1:size(T,2), iz=1:size(T,3)]) +T2 .= T; # Assign also T2 to get correct boundary conditions. + +# Time loop +dt = min(dx^2,dy^2,dz^2)*cp_min/lam/8.1; # Time step for the 3D Heat diffusion +for it = 1:nt + @parallel diffusion3D_step!(T2, T, Ci, lam, dt, dx, dy, dz); + T, T2 = T2, T; +end + +return Data.Array +end diff --git a/test/test_projects/Diffusion3D/test/localtest_diffusion_AMDGPU.jl b/test/test_projects/Diffusion3D/test/localtest_diffusion_AMDGPU.jl new file mode 100644 index 00000000..f771deae --- /dev/null +++ b/test/test_projects/Diffusion3D/test/localtest_diffusion_AMDGPU.jl @@ -0,0 +1,9 @@ +push!(LOAD_PATH, "@stdlib") # NOTE: this is needed to enable this test to run from the Pkg manager +push!(LOAD_PATH, joinpath(@__DIR__, "..")) +using Test +using Pkg +Pkg.activate(joinpath(@__DIR__, "test_projects", "Diffusion3D")) +Pkg.instantiate() +import AMDGPU +using Diffusion3D +@test diffusion3D(AMDGPUBackend) <: AMDGPU.ROCArray diff --git a/test/test_projects/Diffusion3D/test/localtest_diffusion_CUDA.jl b/test/test_projects/Diffusion3D/test/localtest_diffusion_CUDA.jl new file mode 100644 index 00000000..52445ee7 --- /dev/null +++ b/test/test_projects/Diffusion3D/test/localtest_diffusion_CUDA.jl @@ -0,0 +1,9 @@ +push!(LOAD_PATH, "@stdlib") # NOTE: this is needed to enable this test to run from the Pkg manager +push!(LOAD_PATH, joinpath(@__DIR__, "..")) +using Test +using Pkg +Pkg.activate(joinpath(@__DIR__, "test_projects", "Diffusion3D")) +Pkg.instantiate() +import CUDA +using Diffusion3D +@test diffusion3D(CUDABackend) <: CUDA.CuArray diff --git a/test/test_projects/Diffusion3D/test/localtest_diffusion_Threads.jl b/test/test_projects/Diffusion3D/test/localtest_diffusion_Threads.jl new file mode 100644 index 00000000..6d583222 --- /dev/null +++ b/test/test_projects/Diffusion3D/test/localtest_diffusion_Threads.jl @@ -0,0 +1,8 @@ +push!(LOAD_PATH, "@stdlib") # NOTE: this is needed to enable this test to run from the Pkg manager +push!(LOAD_PATH, joinpath(@__DIR__, "..")) +using Test +using Pkg +Pkg.activate(joinpath(@__DIR__, "test_projects", "Diffusion3D")) +Pkg.instantiate() +using Diffusion3D +@test diffusion3D(CPUBackend) <: Array