Skip to content

Commit ccb94c4

Browse files
committed
Add CUDA toggle scaffolding
1 parent 78172ee commit ccb94c4

File tree

7 files changed

+78
-4
lines changed

7 files changed

+78
-4
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ To color exported cell grids by particle counts, set
9090
one plus the particles in its neighbor stencil.
9191
Neighbor rebuilds use a per-cell ordering buffer without reordering particle
9292
storage.
93+
To toggle the CUDA path (requires CUDA.jl), call `EnableCUDA!(SimMetaData)` or
94+
set `SimMetaData.CUDASettings.Enabled = true` before running a simulation.
9395

9496
## Help
9597

example/StillWedgeMDBC.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ let
77
SimConstantsWedge = SimulationConstants{FloatType}(dx=0.02,c₀=42.48576250492629, δᵩ = 0.1, CFL=0.5)
88
# SimConstantsWedge = SimulationConstants{FloatType}(dx=0.01,c₀=43.4, δᵩ = 0.1, CFL=0.2)
99
#
10-
SimMetaDataWedge = SimulationMetaData{Dimensions,FloatType,NoShifting,NoKernelOutput,NoMDBC,StoreLog}(
10+
SimMetaDataWedge = SimulationMetaData{Dimensions,FloatType,NoShifting,NoKernelOutput,SimpleMDBC,StoreLog}(
1111
SimulationName="StillWedge",
1212
SaveLocation="W:/Simulations/StillWedge2D_MDBC",
1313
SimulationTime=4.0,
@@ -18,6 +18,8 @@ let
1818
OpenLogFile=true,
1919
)
2020

21+
EnableCUDA!(SimMetaDataWedge)
22+
2123
# If save directory is not already made, make it
2224
if !isdir(SimMetaDataWedge.SaveLocation)
2325
mkdir(SimMetaDataWedge.SaveLocation)

src/SPHCellList.jl

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ using ..SimulationEquations
99
using ..SimulationGeometry
1010
using ..AuxiliaryFunctions
1111
using ..SimulationMetaDataConfiguration
12+
using ..SimulationCUDAConfiguration: IsCUDAEnabled
1213
using ..SimulationConstantsConfiguration
1314
using ..SimulationLoggerConfiguration
1415
using ..PreProcess
@@ -19,6 +20,7 @@ using ..SPHKernels
1920
using ..SPHViscosityModels
2021
using ..SPHDensityDiffusionModels
2122
using ..SPHNeighborList: BuildNeighborCellLists!, ComputeCellNeighborCounts, ComputeCellParticleCounts, ConstructStencil, ExtractCells!, MapFloor, UpdateNeighbors!, UpdateΔx!
23+
using ..SPHNeighborListCUDA: UpdateNeighborsCUDA!
2224

2325
using StaticArrays
2426
using StructArrays: StructArray, foreachfield
@@ -747,7 +749,11 @@ using LinearAlgebra
747749
# Remove if statement logic if you want to update each iteration
748750
# if mod(SimMetaData.Iteration, ceil(Int, SimKernel.H / (SimConstants.c₀ * dt * (1/SimConstants.CFL)) )) == 0 || SimMetaData.Iteration == 1
749751
if ShouldRebuild
750-
@timeit SimMetaData.HourGlass "01a Actual Calculate IndexCounter" SimMetaData.IndexCounter = UpdateNeighbors!(SimParticles, SimKernel.H⁻¹, ParticleRanges, UniqueCells, CellDict, ParticleOrder, CellOffsets)
752+
if IsCUDAEnabled(SimMetaData)
753+
@timeit SimMetaData.HourGlass "01a Actual Calculate IndexCounter" SimMetaData.IndexCounter = UpdateNeighborsCUDA!(SimParticles, SimKernel.H⁻¹, ParticleRanges, UniqueCells, CellDict, ParticleOrder, CellOffsets)
754+
else
755+
@timeit SimMetaData.HourGlass "01a Actual Calculate IndexCounter" SimMetaData.IndexCounter = UpdateNeighbors!(SimParticles, SimKernel.H⁻¹, ParticleRanges, UniqueCells, CellDict, ParticleOrder, CellOffsets)
756+
end
751757
SimMetaData.Δx = zero(eltype(dρdtI))
752758
UniqueCellsView = view(UniqueCells, 1:SimMetaData.IndexCounter)
753759
BuildNeighborCellLists!(NeighborCellLists, FullStencil, UniqueCellsView, ParticleRanges, CellDict)

src/SPHExample.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ module SPHExample
55
include("SPHViscosityModels.jl")
66
include("ProduceHDFVTK.jl")
77
include("SimulationGeometry.jl")
8+
include("SimulationCUDAConfiguration.jl");
89
include("SimulationMetaDataConfiguration.jl");
910
include("SimulationEquations.jl");
1011
include("TimeStepping.jl");
@@ -14,6 +15,7 @@ module SPHExample
1415
include("OpenExternalPrograms.jl")
1516
include("SPHDensityDiffusionModels.jl")
1617
include("SPHNeighborList.jl")
18+
include("SPHNeighborListCUDA.jl")
1719
include("SPHCellList.jl") #Must be last
1820

1921
# Re-export desired functions from each submodule
@@ -51,7 +53,11 @@ module SPHExample
5153
export SimulationMetaData, ShiftingMode, NoShifting, PlanarShifting,
5254
KernelOutputMode, NoKernelOutput, StoreKernelOutput,
5355
MDBCMode, NoMDBC, SimpleMDBC,
54-
LogMode, NoLog, StoreLog
56+
LogMode, NoLog, StoreLog,
57+
CUDASettings, EnableCUDA!, DisableCUDA!, IsCUDAEnabled
58+
59+
using .SimulationCUDAConfiguration
60+
export CUDASettings, EnableCUDA!, DisableCUDA!, IsCUDAEnabled
5561

5662
using .SimulationConstantsConfiguration
5763
export SimulationConstants
@@ -60,6 +66,9 @@ module SPHExample
6066
export ConstructStencil, ExtractCells!, UpdateNeighbors!,
6167
BuildNeighborCellLists!, ComputeCellParticleCounts, ComputeCellNeighborCounts
6268

69+
using .SPHNeighborListCUDA
70+
export UpdateNeighborsCUDA!
71+
6372
using .SPHCellList
6473
export NeighborLoop!, ComputeInteractions!, RunSimulation
6574

src/SPHNeighborListCUDA.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module SPHNeighborListCUDA
2+
3+
export UpdateNeighborsCUDA!
4+
5+
using ..SPHNeighborList: UpdateNeighbors!
6+
using ..SimulationCUDAConfiguration: AssertCUDAAvailable
7+
8+
function UpdateNeighborsCUDA!(Particles, InverseCutOff, ParticleRanges,
9+
UniqueCells, CellDict, ParticleOrder, CellOffsets)
10+
AssertCUDAAvailable()
11+
return UpdateNeighbors!(Particles, InverseCutOff, ParticleRanges,
12+
UniqueCells, CellDict, ParticleOrder, CellOffsets)
13+
end
14+
15+
end

src/SimulationCUDAConfiguration.jl

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
module SimulationCUDAConfiguration
2+
3+
export CUDASettings, EnableCUDA!, DisableCUDA!, IsCUDAEnabled, AssertCUDAAvailable
4+
5+
using Parameters
6+
7+
@with_kw mutable struct CUDASettings
8+
Enabled::Bool = false
9+
end
10+
11+
function AssertCUDAAvailable()
12+
if Base.find_package("CUDA") === nothing
13+
error("CUDA.jl not available. Add CUDA.jl to Project.toml and instantiate.")
14+
end
15+
return nothing
16+
end
17+
18+
function EnableCUDA!(Settings::CUDASettings)
19+
Settings.Enabled = true
20+
AssertCUDAAvailable()
21+
return nothing
22+
end
23+
24+
function DisableCUDA!(Settings::CUDASettings)
25+
Settings.Enabled = false
26+
return nothing
27+
end
28+
29+
IsCUDAEnabled(Settings::CUDASettings) = Settings.Enabled
30+
31+
end

src/SimulationMetaDataConfiguration.jl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
module SimulationMetaDataConfiguration
22

33
using Parameters
4+
5+
using ..SimulationCUDAConfiguration: CUDASettings, EnableCUDA!, DisableCUDA!, IsCUDAEnabled
46
using TimerOutputs
57

8+
import ..SimulationCUDAConfiguration: EnableCUDA!, DisableCUDA!, IsCUDAEnabled
69

710
export SimulationMetaData, UpdateMetaData!, ShiftingMode, NoShifting, PlanarShifting,
811
KernelOutputMode, NoKernelOutput, StoreKernelOutput,
912
MDBCMode, NoMDBC, SimpleMDBC,
10-
LogMode, NoLog, StoreLog
13+
LogMode, NoLog, StoreLog,
14+
CUDASettings, EnableCUDA!, DisableCUDA!, IsCUDAEnabled
1115

1216
abstract type ShiftingMode end
1317
struct NoShifting <: ShiftingMode end
@@ -48,6 +52,7 @@ struct StoreLog <: LogMode end
4852
ExportSingleVTKHDF::Bool = true
4953
ExportGridCells::Bool = false
5054
ExportGridCellParticleCounts::Bool = false
55+
CUDASettings::CUDASettings = CUDASettings()
5156
OutputVariables::Vector{String} = [
5257
"Density",
5358
"Pressure",
@@ -59,6 +64,10 @@ struct StoreLog <: LogMode end
5964
OpenLogFile::Bool = true
6065
Δx::FloatType = zero(FloatType)
6166
end
67+
68+
EnableCUDA!(SimMetaData::SimulationMetaData) = EnableCUDA!(SimMetaData.CUDASettings)
69+
DisableCUDA!(SimMetaData::SimulationMetaData) = DisableCUDA!(SimMetaData.CUDASettings)
70+
IsCUDAEnabled(SimMetaData::SimulationMetaData) = IsCUDAEnabled(SimMetaData.CUDASettings)
6271
SimulationMetaData{D,T,S,K,B}(; kwargs...) where {D,T,S<:ShiftingMode,K<:KernelOutputMode,B<:MDBCMode} =
6372
SimulationMetaData{D,T,S,K,B,NoLog}(; kwargs...)
6473
SimulationMetaData{D,T,S,K}(; kwargs...) where {D,T,S<:ShiftingMode,K<:KernelOutputMode} =

0 commit comments

Comments
 (0)