Profiling and parallelizing a satellite/gravity simulation across CPU cores with OpenMP and the GPU with OpenCL.
This project takes a single-threaded physics-and-graphics simulation and parallelizes its two hot loops, one on the CPU and one on the GPU. The simulation models 64 satellites orbiting a central "black hole" in a 1024x1024 window. Each frame runs two expensive stages:
- A physics engine that advances every satellite using Euler integration, with 100,000 sub-steps per frame for numerical stability.
- A graphics engine that colors all 1,048,576 pixels by computing, for every pixel, a distance-weighted blend of all 64 satellites.
The exercise demonstrates a practical performance-engineering workflow: keep a verified sequential reference, parallelize the hot paths, and check that the parallel output still matches the reference within tolerance.
The code keeps a sequential reference implementation (sequentialPhysicsEngine and sequentialGraphicsEngine) alongside the optimized versions. On the first frames the program runs both and compares the results pixel by pixel (errorCheck, with an allowed floating-point error), so correctness is validated against the original before any timings matter.
Optimizations were applied in layers:
- Compiler and SIMD. The host code is built with
-O3 -ffast-math -mavx2 -msse4.2, enabling aggressive optimization and AVX2/SSE4.2 vectorization. - CPU multicore with OpenMP. The physics loop (
parallelPhysicsEngine) is parallelized with#pragma omp parallel foracross the 64 satellites. Per-satellite state is cached in local variables during the inner integration loop, and the write-back into shared satellite storage is guarded with#pragma omp critical. - GPU offload with OpenCL. The pixel-coloring stage, the most expensive loop, is offloaded to the GPU. The host (
parallelGraphicsEngine) sets up an OpenCL context, uploads satellite data, and launches theparallelOpenCLkernel (parallel.cl) over a 2D range matching the window size. Each work item computes one pixel: a direct hit test against every satellite, then an inverse-distance weighted color blend. The device is chosen at runtime, preferring a GPU and falling back to a CPU device if no GPU is available.
The program prints per-frame and running-average frame times at runtime (satellite movement time plus pixel coloring time). The recorded measurements for this project are:
| Stage | Frame time | Relative speedup |
|---|---|---|
| Optimized CPU | 537 ms | reference |
| OpenCL GPU | 69 ms | about 7.8x faster than the optimized CPU stage |
These figures are the author's own measured frame times on the development machine; they are not reproduced from a committed benchmark log, and results will vary with CPU, GPU, and OpenCL runtime. The overall speedup relative to the original single-threaded baseline is larger, but no raw baseline measurement is committed to the repository, so treat any end-to-end figure as an approximate, machine-dependent result rather than a verified number.
- A C compiler with OpenMP support (for example GCC or Clang).
- An OpenCL SDK and runtime, including headers (
CL/cl.h), targeting OpenCL 3.0. - OpenGL, GLUT (freeglut), and SDL2 for the display and windowing.
- CMake 3.9 or newer.
- On Linux, the math library (
libm).
mkdir -p build && cd build
cmake ..
make
./parallelAn optional integer argument seeds the random satellite layout for reproducible runs:
./parallel 12345CMake copies the OpenCL kernel parallel.cl next to the executable as part of the build. Note that the kernel is copied on build of the host program; if you edit only parallel.cl, force a full rebuild so the updated kernel is copied.
Note: CMakeLists.txt currently hardcodes some library and include paths for a Debian/Ubuntu x86_64 layout (for example /usr/lib/x86_64-linux-gnu and /usr/include/GL). Adjust these to match your system if the configure or link step fails.
parallel.c Host program: sequential reference, OpenMP physics, and OpenCL host code
parallel.cl OpenCL kernel that colors each pixel on the GPU
CMakeLists.txt CMake build (compiler flags, OpenMP, OpenCL, OpenGL/GLUT/SDL2)
build/ CMake build directory, includes a build_project.sh helper script
The base simulation framework is a parallelization exercise from the Tampere University course COMP.CE.350, originally authored by Matias Koskela, Heikki Kultala, and Topi Leppanen (see the copyright header in parallel.c). The original contribution in this repository is the parallelization work: the OpenMP physics loop and the OpenCL rendering kernel.