Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Parallel Computing: CPU and GPU Optimization

Profiling and parallelizing a satellite/gravity simulation across CPU cores with OpenMP and the GPU with OpenCL.

Language OpenMP OpenCL Build

Overview

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.

Approach

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:

  1. Compiler and SIMD. The host code is built with -O3 -ffast-math -mavx2 -msse4.2, enabling aggressive optimization and AVX2/SSE4.2 vectorization.
  2. CPU multicore with OpenMP. The physics loop (parallelPhysicsEngine) is parallelized with #pragma omp parallel for across 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.
  3. 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 the parallelOpenCL kernel (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.

Results / Performance

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.

Prerequisites

  • 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).

Building and running

mkdir -p build && cd build
cmake ..
make
./parallel

An optional integer argument seeds the random satellite layout for reproducible runs:

./parallel 12345

CMake 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.

Project layout

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

Attribution

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.

About

Physics-engine optimization with OpenMP and OpenCL — up to 34x speedup

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages