A high-performance N-body gravitational simulation implemented in C++ with both sequential CPU and parallel OpenCL implementations, featuring real-time SFML visualization.
- Sequential CPU Implementation: Traditional single-threaded CPU computation for smaller body counts
- Parallel OpenCL Implementation: GPU-accelerated computation using OpenCL for large-scale simulations
- Real-time Visualization: SFML-based graphics with smooth 165 FPS target
- Structure of Arrays (SOA): Optimized memory layout for parallel processing
- Toroidal Universe: Bodies wrap around screen edges for continuous simulation
- Dynamic Body Count: Supports from 5 to 1000+ bodies depending on hardware
- Performance Monitoring: Real-time FPS and computation time display
NBody/
├── NBody.cpp # Main application file
├── src/
│ └── Body.cpp # Body class implementation
├── include/
│ └── Body.h # Body class header
├── opencl/
│ └── NBody.cl # OpenCL kernel implementations
├── CMakeLists.txt # CMake build configuration
├── install_sfml.sh # SFML installation script
├── font.ttf # Font file for text rendering
└── README.md # This documentation
- C++ Compiler: GCC 7+ or Clang 6+ with C++17 support
- CMake: Version 3.16 or higher
- SFML: Version 2.5 or higher
- OpenCL: Version 1.2 or higher (optional, for GPU acceleration)
- CPU: Any modern multi-core processor
- GPU: OpenCL-compatible GPU (NVIDIA, AMD, or Intel) for parallel implementation
- RAM: At least 4GB (8GB+ recommended for large simulations)
Run the provided installation script:
chmod +x install_sfml.sh
./install_sfml.shOr install manually:
# Ubuntu/Debian
sudo apt-get update
sudo apt-get install libsfml-dev opencl-headers ocl-icd-libopencl1 ocl-icd-dev build-essential cmake pkg-config
# macOS (with Homebrew)
brew install sfml opencl-headers
# Windows (with vcpkg)
vcpkg install sfml openclmkdir build
cd build
cmake ..
make./NBodySimulationWhen you run the simulation, you'll be prompted to choose between:
- Sequential CPU: Single-threaded implementation suitable for learning and small simulations
- OpenCL GPU/CPU: Parallel implementation for high-performance computing
- Close Window: ESC or click the X button
- Performance Info: Displayed in the top-left corner
Key parameters can be modified in the source code:
// In NBody.cpp
constexpr size_t n_bodies = 1000; // Number of bodies
constexpr float G = 1.0f; // Gravitational constant
constexpr float dt = 0.1f; // Time step
constexpr float eps = 1e-1f; // Softening parameter
constexpr float TARGET_FPS = 165.0f; // Target frame rateThe simulation implements a simplified N-body gravitational system:
-
Force Calculation: For each body pair (i,j):
F_ij = G * m_j * m_i / (r_ij^2 + ε^2)^(3/2) -
Integration: Leapfrog integration scheme:
v(t+dt) = v(t) + a(t) * dt x(t+dt) = x(t) + v(t+dt) * dt -
Boundary Conditions: Toroidal universe with wraparound
- Structure of Arrays (SOA): Memory layout optimized for SIMD operations
- OpenCL Parallelization: Each body's force calculation runs in parallel
- Memory Coalescing: Efficient GPU memory access patterns
- Softening Parameter: Prevents numerical instabilities at close distances
- Color Coding: Body color varies with mass (red = heavy, blue = light)
- Size Scaling: Visual size reflects body mass
- Smooth Animation: 165 FPS target with frame limiting
- Real-time Metrics: FPS, body count, and computation time display
-
OpenCL Not Found
Error: No OpenCL platforms found- Install OpenCL drivers for your GPU
- The simulation will automatically fall back to CPU mode
-
SFML Not Found
Error: SFML development libraries not found- Run
./install_sfml.shor install SFML manually - Ensure pkg-config can find SFML
- Run
-
Font Loading Error
Failed to load font- Ensure
font.ttfexists in the project directory - The simulation will use default font if custom font fails
- Ensure
-
Poor Performance
- Reduce
n_bodiesconstant for better performance - Ensure GPU drivers are properly installed
- Check if system has sufficient RAM
- Reduce
For development and debugging:
cmake -DCMAKE_BUILD_TYPE=Debug ..
makeThis project demonstrates several important HPC concepts:
- Parallel Algorithm Design: Converting sequential algorithms to parallel
- Memory Layout Optimization: AoS vs SoA performance implications
- GPU Computing: OpenCL programming model and best practices
- Performance Analysis: Measuring and optimizing computational bottlenecks
- Numerical Methods: Stable integration schemes for physical simulation
Consider these enhancements for further learning:
- Advanced Integrators: Implement Runge-Kutta or Verlet integration
- Hierarchical Methods: Add Barnes-Hut algorithm for O(N log N) complexity
- Multiple Forces: Include electromagnetic or strong nuclear forces
- Collision Detection: Handle body mergers and fragmentation
- 3D Visualization: Extend to three-dimensional space
- Distributed Computing: MPI implementation for cluster computing
This project is developed for educational purposes as part of the High-Performance Computing course at the Institute for Computer Science.
- Prof. Dr. Ivan Kisel (Course Instructor)
- Robin Lakos (Teaching Assistant)
- Akhil Mithran (Teaching Assistant)
- Oddharak Tyagi (Teaching Assistant)