Skip to content

Commit a65ac29

Browse files
authored
Cuda sn solver (#63)
* fix lattice perimeter definitions * fix max outflow MPI * fix output of varAbsorption * fixed rms bug with mpi exec * fix linspace vulnerability * fixed MPI flag * recomputation of scalar flux missing after heun averaging * more guardrails around greenprobing * made the probing blocks move with the center of the capsule * update restart sim time * fixed validation tests * stage 1 cuda fvm solver * cuda solver validation ok * lattice runs, but has some artifacts near boundary * update installers * add singularity cuda installer * update reame
1 parent 0840453 commit a65ac29

21 files changed

Lines changed: 2971 additions & 89 deletions

‎CMakeLists.txt‎

Lines changed: 69 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,24 @@ option( BUILD_UNITY "enables unity build for faster compile times" ON )
99
option( BUILD_CODE_COV "enables compiler option required for code coverage analysis" OFF )
1010
option( BUILD_ML "enables build with tensorflow backend access" OFF )
1111
option( BUILD_MPI "enables build with MPI access" OFF )
12+
option( BUILD_CUDA_HPC "enables CUDA backend for SN HPC solver (single GPU)" OFF )
1213
#################################################
1314

1415

1516
### COMPILER ####################################
1617
set( CMAKE_CXX_STANDARD 17 )
1718
set( CMAKE_CXX_STANDARD_REQUIRED ON )
19+
if( BUILD_CUDA_HPC )
20+
enable_language( CUDA )
21+
if( CMAKE_VERSION VERSION_LESS 3.18 )
22+
# CMake 3.16 does not ship CUDA17 mappings by default.
23+
set( CMAKE_CUDA17_STANDARD_COMPILE_OPTION "--std=c++17" )
24+
set( CMAKE_CUDA17_EXTENSION_COMPILE_OPTION "--std=c++17" )
25+
endif()
26+
set( CMAKE_CUDA_STANDARD 17 )
27+
set( CMAKE_CUDA_STANDARD_REQUIRED ON )
28+
set( CMAKE_CUDA_EXTENSIONS OFF )
29+
endif()
1830
set( KITRT_RELEASE_OPTIONS -march=native -w )
1931
set( KITRT_RELWITHDEBINFO_OPTIONS -march=native -pg -no-pie )
2032
set( KITRT_DEBUG_OPTIONS -Wall -Wextra -Wpedantic )
@@ -30,6 +42,21 @@ set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake ${CMAKE_MODULE_PATH})
3042

3143
find_package( OpenMP REQUIRED )
3244

45+
if( BUILD_CUDA_HPC )
46+
add_compile_definitions( KITRT_ENABLE_CUDA_HPC )
47+
if( CMAKE_VERSION VERSION_GREATER_EQUAL 3.17 )
48+
find_package( CUDAToolkit QUIET )
49+
endif()
50+
if( DEFINED CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES )
51+
include_directories( ${CMAKE_CUDA_TOOLKIT_INCLUDE_DIRECTORIES} )
52+
elseif( EXISTS "/usr/local/cuda/include" )
53+
include_directories( /usr/local/cuda/include )
54+
endif()
55+
message( STATUS "CUDA HPC solver: enabled" )
56+
else()
57+
message( STATUS "CUDA HPC solver: disabled" )
58+
endif()
59+
3360
message(STATUS "MPI build flag: ${BUILD_MPI}")
3461
if( BUILD_MPI )
3562
add_definitions(-DIMPORT_MPI)
@@ -72,6 +99,20 @@ else()
7299
set( CORE_LIBRARIES ${LAPACK_LIBRARIES} ${BLAS_LIBRARIES} ${VTK_LIBRARIES} OpenMP::OpenMP_CXX -lstdc++fs )
73100
endif()
74101

102+
if( BUILD_CUDA_HPC )
103+
if( TARGET CUDA::cudart )
104+
list( APPEND CORE_LIBRARIES CUDA::cudart )
105+
else()
106+
find_library( CUDART_LIBRARY
107+
NAMES cudart
108+
HINTS ${CMAKE_CUDA_IMPLICIT_LINK_DIRECTORIES} /usr/local/cuda/lib64 /usr/lib/x86_64-linux-gnu )
109+
if( NOT CUDART_LIBRARY )
110+
message( FATAL_ERROR "Could not find CUDA runtime library (cudart)." )
111+
endif()
112+
list( APPEND CORE_LIBRARIES ${CUDART_LIBRARY} )
113+
endif()
114+
endif()
115+
75116

76117
#################################################
77118

@@ -89,6 +130,12 @@ add_compile_definitions( GIT_HASH="${GIT_HASH}" )
89130

90131
### BUILD KIT-RT ################################
91132
file( GLOB_RECURSE SRCS RELATIVE ${CMAKE_SOURCE_DIR} "src/*.cpp" "include/*.h" )
133+
if( BUILD_CUDA_HPC )
134+
list( APPEND SRCS "src/solvers/snsolver_hpc.cu" "include/solvers/snsolver_hpc_cuda.hpp" )
135+
# Force nvcc to parse this translation unit as C++17 even if transitive
136+
# imported target features inject an older fallback standard.
137+
set_source_files_properties( src/solvers/snsolver_hpc.cu PROPERTIES COMPILE_FLAGS "--std=c++17" )
138+
endif()
92139
set( EXCLUDE_DIR "/gui/" )
93140
foreach( TMP_PATH ${SRCS} )
94141
string( FIND ${TMP_PATH} ${EXCLUDE_DIR} EXCLUDE_DIR_FOUND )
@@ -110,9 +157,15 @@ if( BUILD_ML )
110157
target_link_libraries( ${CMAKE_PROJECT_NAME} ${CORE_LIBRARIES} ${TENSORFLOW_LIB})
111158
endif()
112159
target_link_libraries( ${CMAKE_PROJECT_NAME} ${CORE_LIBRARIES} )
113-
target_compile_options( ${CMAKE_PROJECT_NAME} PUBLIC "$<$<CONFIG:DEBUG>:${KITRT_DEBUG_OPTIONS}>" )
114-
target_compile_options( ${CMAKE_PROJECT_NAME} PUBLIC "$<$<CONFIG:RELWITHDEBINFO>:${KITRT_RELWITHDEBINFO_OPTIONS}>" )
115-
target_compile_options( ${CMAKE_PROJECT_NAME} PUBLIC "$<$<CONFIG:RELEASE>:${KITRT_RELEASE_OPTIONS}>" )
160+
if( BUILD_CUDA_HPC )
161+
set_target_properties(
162+
${CMAKE_PROJECT_NAME}
163+
PROPERTIES CUDA_STANDARD 17 CUDA_STANDARD_REQUIRED ON CUDA_EXTENSIONS OFF
164+
)
165+
endif()
166+
target_compile_options( ${CMAKE_PROJECT_NAME} PUBLIC "$<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CONFIG:DEBUG>>:${KITRT_DEBUG_OPTIONS}>" )
167+
target_compile_options( ${CMAKE_PROJECT_NAME} PUBLIC "$<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CONFIG:RELWITHDEBINFO>>:${KITRT_RELWITHDEBINFO_OPTIONS}>" )
168+
target_compile_options( ${CMAKE_PROJECT_NAME} PUBLIC "$<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CONFIG:RELEASE>>:${KITRT_RELEASE_OPTIONS}>" )
116169
#################################################
117170

118171
### BUILD UNIT TESTS ############################
@@ -145,20 +198,26 @@ if( BUILD_TESTING )
145198
target_compile_definitions( unit_tests PUBLIC BUILD_TESTING )
146199
target_compile_definitions( unit_tests PUBLIC TESTS_PATH="${CMAKE_SOURCE_DIR}/tests/" )
147200
target_link_libraries( unit_tests Catch ${CORE_LIBRARIES} )
148-
target_compile_options( unit_tests PUBLIC "$<$<CONFIG:DEBUG>:${KITRT_DEBUG_OPTIONS}>" )
201+
if( BUILD_CUDA_HPC )
202+
set_target_properties(
203+
unit_tests
204+
PROPERTIES CUDA_STANDARD 17 CUDA_STANDARD_REQUIRED ON CUDA_EXTENSIONS OFF
205+
)
206+
endif()
207+
target_compile_options( unit_tests PUBLIC "$<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CONFIG:DEBUG>>:${KITRT_DEBUG_OPTIONS}>" )
149208
if( BUILD_CODE_COV )
150209
if( CMAKE_COMPILER_IS_GNUCXX )
151210
set( CODE_COVERAGE_OPTIONS --coverage -g -O0 -w )
152-
target_compile_options( unit_tests PUBLIC "$<$<CONFIG:DEBUG>:${CODE_COVERAGE_OPTIONS}>" )
211+
target_compile_options( unit_tests PUBLIC "$<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CONFIG:DEBUG>>:${CODE_COVERAGE_OPTIONS}>" )
153212
target_link_libraries( unit_tests Catch gcov )
154213
else()
155214
message( FATAL_ERROR "Code coverage is currently only supported for gcc!" )
156215
endif()
157216
else()
158217
target_link_libraries( unit_tests Catch )
159218
endif()
160-
target_compile_options( unit_tests PUBLIC "$<$<CONFIG:RELWITHDEBINFO>:${KITRT_RELWITHDEBINFO_OPTIONS}>" )
161-
target_compile_options( unit_tests PUBLIC "$<$<CONFIG:RELEASE>:${KITRT_RELEASE_OPTIONS}>" )
219+
target_compile_options( unit_tests PUBLIC "$<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CONFIG:RELWITHDEBINFO>>:${KITRT_RELWITHDEBINFO_OPTIONS}>" )
220+
target_compile_options( unit_tests PUBLIC "$<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CONFIG:RELEASE>>:${KITRT_RELEASE_OPTIONS}>" )
162221
catch_discover_tests( unit_tests )
163222
endif()
164223
#################################################
@@ -179,9 +238,9 @@ if( BUILD_GUI )
179238
set( GUI_LIBRARIES Qt5::Core Qt5::Widgets Qt5::Gui Qt5::OpenGL ${VTK_LIBRARIES} )
180239
target_link_libraries( ${CMAKE_PROJECT_NAME}_gui ${CORE_LIBRARIES} ${GUI_LIBRARIES} )
181240
set_target_properties( ${CMAKE_PROJECT_NAME}_gui PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/bin" )
182-
target_compile_options( ${CMAKE_PROJECT_NAME}_gui PUBLIC "$<$<CONFIG:DEBUG>:${KITRT_DEBUG_OPTIONS}>" )
183-
target_compile_options( ${CMAKE_PROJECT_NAME}_gui PUBLIC "$<$<CONFIG:RELWITHDEBINFO>:${KITRT_RELWITHDEBINFO_OPTIONS}>" )
184-
target_compile_options( ${CMAKE_PROJECT_NAME}_gui PUBLIC "$<$<CONFIG:RELEASE>:${KITRT_RELEASE_OPTIONS}>" )
241+
target_compile_options( ${CMAKE_PROJECT_NAME}_gui PUBLIC "$<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CONFIG:DEBUG>>:${KITRT_DEBUG_OPTIONS}>" )
242+
target_compile_options( ${CMAKE_PROJECT_NAME}_gui PUBLIC "$<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CONFIG:RELWITHDEBINFO>>:${KITRT_RELWITHDEBINFO_OPTIONS}>" )
243+
target_compile_options( ${CMAKE_PROJECT_NAME}_gui PUBLIC "$<$<AND:$<COMPILE_LANGUAGE:CXX>,$<CONFIG:RELEASE>>:${KITRT_RELEASE_OPTIONS}>" )
185244
endif()
186245
#################################################
187246

‎README.md‎

Lines changed: 104 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ KiT-RT is an open-source, multi-fidelity **C++ PDE solver for radiative transpor
1616

1717
* **Modular, HPC-ready** architecture. Supports hybrid **MPI/OpenMP distributed parallelism**.
1818
* **Containerized** for portable deployment across HPC systems (Docker & Singularity).
19-
* **Python-wrapped** via [CharmKIT](https://github.com/KiT-RT/CharmKIT)
19+
* **Python-wrapped** via [charm_kit](https://github.com/KiT-RT/charm_kit)
2020
* Downstream applications:
2121
- Data generation for scientific **foundation models**.
2222
- high-resolution **reference solutions** for AI-based surrogate modeling.
@@ -66,79 +66,144 @@ Applications include:
6666

6767

6868

69-
## Installation
69+
## Installation and Run (By Parallelization)
7070

71-
### Plain cpp setup
71+
One-time setup:
7272
```bash
73-
# Clone repository
7473
git clone https://github.com/KiT-RT/kitrt_code.git
7574
cd kitrt_code
7675
git submodule update --init --recursive
76+
```
7777

78-
# Build with CMake
79-
mkdir build && cd build
80-
cmake -DCMAKE_BUILD_TYPE=Release ../
81-
make -j
78+
Then run all commands from the repository root.
8279

83-
```
84-
### Docker setup
85-
A preconfigured docker container can also be used to run the code.
86-
By running
80+
### 1. CPU (OpenMP only)
8781

82+
#### 1a) Plain installation (no container)
8883
```bash
89-
docker run --rm -ti -v $(pwd):/home kitrt/test:latest
84+
mkdir -p build_omp
85+
cd build_omp
86+
cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_MPI=OFF -DBUILD_CUDA_HPC=OFF -DBUILD_ML=OFF ..
87+
make -j
88+
cd ..
89+
./build_omp/KiT-RT tests/input/validation_tests/SN_solver/checkerboard_SN.cfg
9090
```
9191

92-
Bash scripts are provided in the folder tools/CI to get started with the docker environments. To start an interactive docker environment, execute
92+
#### 1b) Docker installation
9393
```bash
94-
docker run -i -t --rm -v $(pwd)/../..:/mnt kitrt/test:latest /bin/bash
94+
docker run --rm -it -v $(pwd):/mnt -w /mnt kitrt/test:latest /bin/bash
95+
mkdir -p build_docker_omp
96+
cd build_docker_omp
97+
cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_MPI=OFF -DBUILD_CUDA_HPC=OFF -DBUILD_ML=OFF ..
98+
make -j
99+
cd ..
100+
./build_docker_omp/KiT-RT tests/input/validation_tests/SN_solver/checkerboard_SN.cfg
95101
```
96102

97-
### Singularity setup
98-
Create the singularity container
103+
#### 1c) Singularity installation
99104
```bash
100-
mkdir build_singularity
101105
cd tools/singularity
102-
sudo sh build_container.sh
103-
chmod +x install_kitrt_singularity.sh
104-
singularity exec kit_rt.sif ./install_kitrt_singularity.sh
106+
sudo singularity build kit_rt.sif kit_rt.def
107+
cd ../..
108+
mkdir -p build_singularity_omp
109+
cd build_singularity_omp
110+
singularity exec ../tools/singularity/kit_rt.sif \
111+
cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_MPI=OFF -DBUILD_CUDA_HPC=OFF -DBUILD_ML=OFF ..
112+
singularity exec ../tools/singularity/kit_rt.sif make -j
113+
cd ..
114+
singularity exec tools/singularity/kit_rt.sif \
115+
./build_singularity_omp/KiT-RT tests/input/validation_tests/SN_solver/checkerboard_SN.cfg
116+
```
117+
118+
### 2. CPU (OpenMP + MPI)
119+
120+
#### 2a) Plain installation (no container)
121+
```bash
122+
mkdir -p build_mpi
123+
cd build_mpi
124+
cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_MPI=ON -DBUILD_CUDA_HPC=OFF -DBUILD_ML=OFF ..
125+
make -j
126+
cd ..
127+
mpirun -np 4 ./build_mpi/KiT-RT tests/input/validation_tests/SN_solver_hpc/lattice_hpc_200_cpu_order2.cfg
105128
```
106-
Run the singularity container
129+
130+
#### 2b) Singularity installation
107131
```bash
108-
singularity shell --bind $(pwd)/../..:/mnt kit_rt.sif
132+
cd tools/singularity
133+
sudo singularity build kit_rt_MPI.sif kit_rt_MPI.def
134+
cd ../..
135+
mkdir -p build_singularity_mpi
136+
cd build_singularity_mpi
137+
singularity exec ../tools/singularity/kit_rt_MPI.sif \
138+
cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_MPI=ON -DBUILD_CUDA_HPC=OFF -DBUILD_ML=OFF ..
139+
singularity exec ../tools/singularity/kit_rt_MPI.sif make -j
140+
cd ..
141+
singularity exec tools/singularity/kit_rt_MPI.sif \
142+
mpirun -np 4 ./build_singularity_mpi/KiT-RT tests/input/validation_tests/SN_solver_hpc/lattice_hpc_200_cpu_order2.cfg
109143
```
110144

111-
## Running simulations
112-
Within any of the above setups, navigate to the example folder and execute KiT-RT
145+
### 3. CPU + single GPU (OpenMP + CUDA)
146+
147+
#### 3a) Singularity installation
113148
```bash
114-
cd examples
115-
../<build_folder_name>/KiT-RT configs/lattice_SN.cfg
149+
cd tools/singularity
150+
sudo singularity build kit_rt_MPI_cuda.sif kit_rt_MPI_cuda.def
151+
cd ../..
152+
mkdir -p build_singularity_cuda
153+
cd build_singularity_cuda
154+
singularity exec --nv ../tools/singularity/kit_rt_MPI_cuda.sif \
155+
cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_MPI=OFF -DBUILD_CUDA_HPC=ON -DBUILD_ML=OFF ..
156+
singularity exec --nv ../tools/singularity/kit_rt_MPI_cuda.sif make -j
157+
cd ..
158+
singularity exec --nv tools/singularity/kit_rt_MPI_cuda.sif \
159+
./build_singularity_cuda/KiT-RT tests/input/validation_tests/SN_solver_hpc/lattice_hpc_200_cuda_order2.cfg
116160
```
117161

118-
## Tensorflow backend
119-
If you choose to enable the integrated machine learning tools via the BUILD_ML option, you need to install the tensorflow C-API:
162+
When compiled with `-DBUILD_CUDA_HPC=ON`, HPC runs use the CUDA backend if a GPU is visible, and fall back to CPU if no GPU is detected.
163+
164+
### 4. Build with TensorFlow backend (CPU + OpenMP only)
165+
120166
```bash
121167
FILENAME=libtensorflow-cpu-linux-x86_64-2.7.0.tar.gz
122168
wget -q --no-check-certificate https://storage.googleapis.com/tensorflow/libtensorflow/${FILENAME}
123-
tar -C /usr/local -xzf ${FILENAME}
124-
ldconfig /usr/local/lib
169+
sudo tar -C /usr/local -xzf ${FILENAME}
170+
sudo ldconfig /usr/local/lib
171+
rm ${FILENAME}
172+
mkdir -p build_ml
173+
cd build_ml
174+
cmake -DCMAKE_BUILD_TYPE=Release -DBUILD_MPI=OFF -DBUILD_CUDA_HPC=OFF -DBUILD_ML=ON ..
175+
make -j
176+
cd ..
177+
./build_ml/KiT-RT tests/input/validation_tests/MN_solver/checkerboard_MN_neural.cfg
125178
```
126-
and for a gpu based version (you need supported hardware and gpu drivers, see here ):
179+
180+
### 5. Debug build
181+
127182
```bash
128-
FILENAME=libtensorflow-gpu-linux-x86_64-2.7.0.tar.gz
129-
wget -q --no-check-certificate https://storage.googleapis.com/tensorflow/libtensorflow/${FILENAME}
130-
tar -C /usr/local -xzf ${FILENAME}
131-
ldconfig /usr/local/lib
183+
mkdir -p build_debug
184+
cd build_debug
185+
cmake -DCMAKE_BUILD_TYPE=Debug -DBUILD_MPI=OFF -DBUILD_CUDA_HPC=OFF -DBUILD_ML=OFF ..
186+
make -j
187+
cd ..
188+
./build_debug/KiT-RT tests/input/validation_tests/SN_solver/checkerboard_SN.cfg
132189
```
133-
Or use the docker container
190+
191+
### 6. Test + coverage build
192+
134193
```bash
135-
docker run --rm -ti -v $(pwd):/home kitrt/test_ml:latest
194+
mkdir -p build_coverage
195+
cd build_coverage
196+
cmake -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTING=ON -DBUILD_CODE_COV=ON -DBUILD_UNITY=OFF ..
197+
make -j
198+
./unit_tests
199+
ctest --output-on-failure
200+
gcovr -r .. --html-details coverage.html
136201
```
137202

138203

139204
## Python API
140205

141-
The Python interface is provided via [CharmKIT](https://github.com/KiT-RT/CharmKIT), allowing seamless integration into AI and outer-loop (UQ, Optimization) workflows.
206+
The Python interface is provided via [charm_kit](https://github.com/KiT-RT/charm_kit), allowing seamless integration into AI and outer-loop (UQ, Optimization) workflows.
142207
Check the corresponding readme for further info
143208

144209

‎include/solvers/snsolver_hpc.hpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ class SNSolverHPC
8080
std::vector<double> _flux; /*!< @brief dim = _nCells x _nSys */
8181

8282
// Output related members
83-
std::vector<double> _scalarFlux; /*!< @brief dim = _nCells */
83+
std::vector<double> _scalarFlux; /*!< @brief dim = _nCells */
84+
std::vector<double> _scalarFluxPrevIter; /*!< @brief scalar flux snapshot before current solver iteration */
8485

8586
// Lattice QOIS
8687
unsigned _nOutputMoments;

0 commit comments

Comments
 (0)