OpenCL Examples
Simple examples of OpenCL code for learning heterogeneous and GPU computing with OpenCL.
Examples marked with * have been reproduced to work in 2025 on AMD GPU.
| Example | Source directory | Description |
|---|---|---|
| Hello World | Hello_World/ |
OpenCL "Hello World" by Apple — computes X² for a buffer of floats |
add_numbers * |
add_numbers/ |
Adds a list of numbers together; includes detailed error handling |
sum_array * |
sum_array/ |
Sums two arrays (vector addition without error-handling boilerplate) |
square_array * |
square_array/ |
Computes array² with a serial comparison version |
Mandelbrot * |
mandelbrot/ |
Mandelbrot set computation with serial comparison |
Waste * |
waste/ |
Compute cycle waster for benchmarking; includes serial comparison |
| RNG | rng/ |
Random number generation on host and GPU using clRNG |
Auger * |
auger/ |
Cosmic ray Monte Carlo simulation — 210x GPU speedup |
| N-Body Simulation | N-BodySimulation/ |
Gravity field simulation across CPU, GPU, and hybrid — requires Xcode |
| Ray Traced Julia Set | RayTraced_Quaternion_Julia-Set_Example/ |
Ray-traced 4D quaternion Julia set with OpenGL rendering |
| cf4cl | cf4cl/ |
Example using the cf4ocl OpenCL wrapper for C |
The examples that most clearly demonstrate GPU computational advantage are N-BodySimulation, RayTraced_Quaternion_Julia-Set_Example (both by Apple), and auger (>200x speedup over serial CPU code).
Dependencies
| Dependency | Purpose | Install |
|---|---|---|
| OpenCL | GPU compute framework | System-provided on macOS |
| clRNG | GPU random number generation | Required by auger and rng |
| cf4ocl | OpenCL C wrapper | Required by cf4cl |
| OpenGL/GLUT | Rendering | Required by RayTraced_Quaternion_Julia-Set_Example on Linux |
| clinfo | Device inspection | brew install clinfo |
To preview locally: mkdocs serve. To publish to GitHub Pages: mkdocs gh-deploy.