NBody simulation examples
Various N-body implementations, demonstrating multiple CPU and CUDA optimizations
Building from source
Prerequisites
- A C++ 17 compiler (GCC, Clang, MSVC)
- CUDA SDK 11.0 or newer
- CMake 3.25 or newer
Getting the Source Code
This source tree uses [Git submodules][5] for third party libraries,
and --recursive is required when cloning:
git clone --recursive <repository>
Building on Windows
When building on Windows with MSVC, the CMake configuration must be done from
a MSVC command prompt (ex. run vcvarsall.bat)
Building on Linux
A few additional system tools and libraries may be required. The CMake configuration phase might fail if they are missing, and it would log instructions on how to install them.
For example, on a recent Ubuntu / Debian system, use the following commands:
sudo apt-get install curl zip unzip tar
sudo apt-get install libxmu-dev libxi-dev libgl-dev
sudo apt install libxinerama-dev libxcursor-dev xorg-dev libglu1-mesa-dev
Building with CMake
Tip: CMake uses the
CUDA_PATHenvironment variable. Make sure it is set and points to the right location before runningcmake.
This project uses CMake, so the general build process looks like:
mkdir build && cd build cmake .. -DCMAKE_BUILD_TYPE=Release make -j8 # replace 8 with the number of cores
Running the N-body simulation
Usage: nbody [--help] [--version] [--width VAR] [--height VAR] [--bodies-count VAR] [--static-velocities] --plugin VAR
Optional arguments:
-h, --help shows help message and exits
-v, --version prints version information and exits
-w, --width Simulation window width [nargs=0..1] [default: 1024]
-h, --height Simulation window height [nargs=0..1] [default: 1024]
-n, --bodies-count Bodies count [nargs=0..1] [default: 5000]
-s, --static-velocities Initialize the bodies with zero initial velocities
--plugin Implementation plugin, valid options:
- cpu_openmp
- cpu_threadpool
- cuda
- cuda_rsqrt
- cuda_soa
- cuda_surface_draw
- cuda_tiled
- cuda_vbo
- dummy [nargs=0..1] [default: "cuda"]
Running on Linux + Wayland
If the app exits with the following initialization error:
CUDA API Error: 999 (unknown error)
> cudaGraphicsGLRegisterImage(...)
... then try setting the following environment variables first:
export __NV_PRIME_RENDER_OFFLOAD=1 __GLX_VENDOR_LIBRARY_NAME=nvidia
Math
This 2D N-body simulation is based on a simple Verlet integration using the following equations: