How mouadhaikal/cuda-barnes-hut works
Project Type and Purpose
• This is a high-performance, standalone N-body gravitational simulation application implementing the Barnes-Hut algorithm fully on the GPU (CUDA) with real-time visualization via Vulkan. It also contains a CPU baseline for comparison and an ImGui interface for interactive control.
File Structure Analysis
• Top-level files:
• CMakeLists.txt & build.sh: build orchestration
• README.md, LICENSE, .gitignore
• assets/ (fonts), shaders/ (GLSL and SPIR-V binaries, compile script)
• src/: core source divided into:
• main.cpp, app.cpp/hpp: application entry, initialization, window & device setup
• common.* & log.hpp: shared utilities, logging
• simulation.*: generic simulation interface
• cpu/: cpuSimulation.* — brute-force CPU kernel for correctness/benchmark
• gpu/:
– barnesHutSystem.cu/.cuh: octree build & force compute kernels
– gpuSimulation.*: orchestrates kernel launches and data buffers
– kernels.cu/.cuh: supporting device functions
– renderer.*: Vulkan swapchain, pipeline, shader modules
– camera.*, inputManager.*: user interaction, view transforms
– gui.*: ImGui setup & frame rendering
• vendor/: third-party dependencies (e.g., ImGui)
README Insights
• Technologies: CUDA Toolkit, Vulkan SDK, GLFW3, ImGui, C++17
• Emphasizes CUDA-Vulkan interoperability (data stays on GPU) and optimized parallel algorithms
a. Main Components:
1. Application Layer
– App / main.cpp: initializes windowing (GLFW), Vulkan context, CUDA context, ImGui
2. Simulation Layer
– CPU Module: cpuSimulation (brute force)
– GPU Module:
• Morton code generation & parallel radix sort
• Octree builder (Karras algorithm)
• Force computation & integration (Barnes-Hut kernels)
3. Rendering Layer
– Vulkan Renderer: swapchain, pipelines, shader modules (compiled SPV)
– GUI: ImGui integration
4. Utility Layer
– Input Manager, Camera controller, Logging, Common utilities
5. Assets & Shaders
– fonts, shader source (.vert/.frag), compiled SPV binaries
6. Build & Deployment
– CMake, build.sh, external libs (vendor)
b. Relationships & Data Flow:
• At startup: App → initializes Vulkan & CUDA → loads shaders & assets
• Per frame:
1. InputManager captures user events → updates Camera & simulation parameters via GUI
2. GPU Simulation orchestrator
• All data resides in GPU buffers (CUdevicePointers / Vulkan buffer objects)
4. GUI draws over frame via ImGui (Vulkan backend)
5. Present swapchain
• Optionally, CPU Module can be invoked for baseline runs; its results may be compared or visualized
c. Architectural Patterns & Principles
• Pipeline processing: each simulation step is a stage with clear input/output buffers
• Shared GPU memory between CUDA and Vulkan (zero-copy interop)
• Modular separation: simulation core vs rendering vs UI vs utilities
• Explicit synchronization points between kernels and graphics queue (e.g., CUDA → Vulkan pipeline barriers)
d. Key Technologies & Frameworks
• CUDA (kernels, streams)
• Vulkan (rendering, SPIR-V shaders)
• GLFW3 (window & context creation)
• ImGui (immediate mode GUI)
• CMake (build configuration)
• GLSL & glslc (shader authoring & compilation)