Profiling and parallelizing a physics/graphics engine across CPU cores with OpenMP and the GPU with OpenCL, ending up with as much as a 34x speedup over the original.
The starting point was a single-threaded engine. From there I profiled to find where the time was actually going, then applied optimizations one layer at a time — compiler vectorization first, then multicore CPU parallelism with OpenMP, and finally a GPU port with OpenCL. Measuring after each step made it clear which changes actually paid off.
- Baseline: the single-threaded reference implementation
- Compiler and SIMD:
-O3 -ftree-vectorize -ffast-math, with AVX2 - CPU multicore: OpenMP (
-fopenmp) on the parallelizable loops - GPU: an OpenCL kernel (
parallel.cl) offloading the hot path
| Version | Frametime (80x80 workload) | Speedup |
|---|---|---|
| Optimized CPU | 537 ms | baseline |
| OpenCL GPU | 69 ms | about 7.8x over the optimized CPU version |
| End to end | — | up to 34x over the original |
mkdir -p build && cd build
cmake ..
make
./parallelDependencies: OpenGL, GLUT, an OpenCL SDK, and an OpenMP-capable compiler.
├── parallel.c host program (OpenMP plus OpenCL host code)
├── parallel.cl OpenCL GPU kernel
├── CMakeLists.txt
└── build/
OpenMP, OpenCL, GPU computing, SIMD and AVX2, performance profiling, CMake, C, and parallel algorithm design.