Urist-McDeveloper / nbody

2D N-body simulation on CPU and GPU

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

2D N-body simulation on CPU and GPU

Written in C, powered by Vulkan and AVX (or SSE), shown on screen with raylib.

(videos below are quite heavily compressed)

example-1.mp4
example-2.mp4

Build and run

Build prerequisites

  1. C compiler:
    • C99 standard;
    • unless SIMD is disabled through build options:
      • AVX or SSE intrinsics (immintrin.h and xmmintrin.h respectively);
      • one of:
        • aligned_alloc (C11 standard);
        • _aligned_malloc (Windows);
        • posix_memalign (POSIX)
    • (optional) OpenMP.
  2. Vulkan SDK, including glslc and validation layers. Only Vulkan 1.0 features are used.
  3. CMake version 3.20 or later.

If you don't have raylib installed on your system, it will be built with this project. For more information on building raylib please refer to https://github.com/raysan5/raylib/tree/4.2.0#build-and-installation.

Build is tested on

  • Linux:
    • GCC 12.1;
    • Clang 15;
    • TCC 0.9:
      • raylib is provided externally;
      • SIMD_SET is none.
  • Windows:
    • MinGW-w64 10.

Misc.

Target nbody-bench uses Linux-only monotonic clock and therefore is not available on other platforms.

How to build

git clone --recurse-submodules https://github.com/Urist-McDeveloper/nbody.git
cd nbody

Then build like any other CMake project. Example on Linux:

mkdir build && cd build
cmake -DCMAKE_BUILD_TYPE=Debug ..
make

Build options

  • SIMD_SET (default AVX) -- which SIMD instruction set to use; possible values: AVX, SSE or none.

What to do

Base controls:

  • Q or ESC to quit
  • SPACE to pause/unpause

Camera controls:

  • WASD to move
  • Press middle mouse button to drag the screen
  • Scroll mouse wheel to zoom

Simulation controls:

  • TAB to switch between CPU and GPU simulation
  • LEFT to decrease simulation speed (fewer updates per second)
  • RIGHT to increase simulation speed (more updates per second)
  • UP to increase simulation step (less accurate, simulation speeds up)
  • DOWN to decrease simulation step (more accurate, simulation slows down)

How to change parameters

By changing some macros:

TODO list

  • Select optimal VkPhysicalDevice, not the first one in the list
  • Write Vulkan renderer so that particle data never has to leave GPU
  • Allow setting simulation parameters through command line arguments
  • Write tests that actually test something

Done:

  • Make GPU simulation respect simulation step change
  • Use specialization constants to make sure CPU and GPU simulations always have the same parameters
  • Make GPU buffers device-local for performance improvements
  • Allow performing multiple updates in a single UpdateWorld_GPU call by chaining pipeline dispatches
  • Use AVX for CPU simulation
  • Add CMake option to disable AVX and fall back to SSE
  • A moving camera would be nice

About

2D N-body simulation on CPU and GPU

License:zlib License


Languages

Language:C 93.7%Language:CMake 4.4%Language:GLSL 1.9%