A high-performance GPU-accelerated Mandelbrot set renderer with multiple backend support: OpenCL for cross-platform compatibility and CUDA for maximum NVIDIA GPU performance, plus OpenGL for hardware-accelerated rendering.
# Check what GPU hardware you have
make check-opencl # Check OpenCL support
make check-cuda # Check CUDA support (NVIDIA only)
# Install dependencies for your system
make install-deps-ubuntu # Ubuntu/Debian OpenCL deps
make install-cuda-ubuntu # Ubuntu/Debian CUDA deps (if NVIDIA)
# Build and run
make opencl # Build OpenCL version (recommended for all GPUs)
build/mandelbrot_opencl # Run OpenCL version
# OR for NVIDIA GPUs with CUDA installed:
make cuda # Build CUDA version
build/mandelbrot_cuda # Run CUDA version (maximum performance)
# OR for comparison:
make cpu # Build CPU version
build/mandelbrot_cpu # Run CPU version
- Multiple GPU Backends:
- OpenCL: Cross-platform GPU acceleration (NVIDIA, AMD, Intel)
- CUDA: Optimized for NVIDIA GPUs with maximum performance
- Hardware Rendering: OpenGL for fast texture-based rendering
- Interactive Navigation: Real-time pan and zoom with mouse and keyboard
- Multiple Implementations: Different optimization levels and comparison versions
- Cross-Platform: Works on Linux, macOS, and Windows
- High Performance: 10-100x faster than CPU implementations
Program | Backend | Description | Best For |
---|---|---|---|
build/mandelbrot_opencl |
OpenCL | Primary GPU-accelerated version | Cross-platform compatibility |
build/mandelbrot_cuda |
CUDA | NVIDIA-optimized version | Maximum NVIDIA GPU performance |
build/mandelbrot_cpu |
CPU | CPU-only implementation | Performance comparison |
build/mandelbrot_opencl_full |
OpenCL | Full-featured OpenCL version | Advanced OpenCL features |
src/
├── opencl/ # OpenCL implementations
│ ├── mandelbrot_opencl.cpp # Main OpenCL version
│ ├── mandelbrot_opencl_full.cpp # Full-featured OpenCL
│ ├── mandelbrot_opencl_c.cpp # C-style OpenCL
│ └── mandelbrot_kernel.cl # OpenCL compute kernel
├── cuda/ # CUDA implementation
│ ├── mandelbrot_cuda.cpp # CUDA version
│ └── mandelbrot_kernel.cu # CUDA compute kernel
└── cpu/ # CPU implementation
└── mandelbrot_cpu.cpp # CPU-only version
build/ # Compiled executables
scripts/ # Utility scripts
All versions share the same controls:
- Mouse drag: Pan around the fractal
- Mouse wheel: Zoom in/out
- Arrow keys: Pan with keyboard
- +/-: Increase/decrease iteration count
- C: Cycle through color schemes (Ultra Fractal, Fire, Ocean, Psychedelic)
- R: Reset view to default
- ESC: Exit application
make install-deps-ubuntu
make install-deps-fedora
make install-deps-arch
make install-cuda-ubuntu
make install-cuda-fedora
make install-cuda-arch
Common requirements:
- OpenGL development libraries
- GLFW3 (window management)
- GLEW (OpenGL extension loading)
For OpenCL:
- OpenCL headers and runtime
- GPU-specific OpenCL drivers (NVIDIA, AMD, or Intel)
For CUDA:
- NVIDIA CUDA Toolkit (11.0+)
- NVIDIA GPU with Compute Capability 5.0+
- NVIDIA drivers (470+)
# Build OpenCL versions (default)
make
# Build specific versions
make opencl # OpenCL GPU version (all platforms)
make cpu # CPU version
make cuda # CUDA GPU version (NVIDIA only)
# Build with debug symbols
make debug
# Build optimized release version
make release
# Clean build files
make clean
# Check OpenCL devices
make check-opencl
# Check CUDA devices
make check-cuda
# Run OpenCL GPU version (recommended)
build/mandelbrot_opencl
# Run CUDA version (NVIDIA GPUs only)
build/mandelbrot_cuda
# Run CPU version (for comparison)
build/mandelbrot_cpu
# Or use make target
make run # Builds and runs mandelbrot_opencl
Use build/mandelbrot_opencl
(OpenCL) when:
- You have any modern GPU (NVIDIA, AMD, Intel)
- You want cross-platform compatibility
- You're unsure which to choose
Use build/mandelbrot_cuda
(CUDA) when:
- You have an NVIDIA GPU
- You want maximum performance
- You need CUDA-specific optimizations
Use build/mandelbrot_cpu
when:
- You don't have a compatible GPU
- You want to compare performance
- Debugging or development
This program works with any OpenCL-compatible device:
- NVIDIA GPUs: Install NVIDIA OpenCL drivers
- AMD GPUs: Install ROCm or AMDGPU-PRO drivers
- Intel GPUs: Intel OpenCL runtime
- CPU fallback: Most OpenCL implementations include CPU support
The CUDA version requires:
- NVIDIA GPU: GeForce GTX 750 or newer (Compute Capability 5.0+)
- NVIDIA CUDA Toolkit: Version 11.0 or later
- NVIDIA Drivers: Version 470 or later
# Check OpenCL devices
make check-opencl
# Check CUDA devices
make check-cuda
# Get detailed GPU info
nvidia-smi # For NVIDIA GPUs
lspci | grep VGA # List all graphics cards
- Parallel Processing: Utilizes hundreds/thousands of GPU cores
- Memory Bandwidth: Leverages high GPU memory bandwidth
- Real-time Interaction: Smooth navigation even at high iteration counts
- Optimized Kernels: Hand-tuned for both OpenCL and CUDA
- Use CUDA version for NVIDIA GPUs when possible
- Increase iteration count gradually to find sweet spot
- Lower resolution if experiencing lag
- Ensure GPU drivers are up to date
-
OpenCL Implementation (
src/opencl/mandelbrot_opencl.cpp
):- Cross-platform GPU acceleration
- OpenCL kernel execution (
src/opencl/mandelbrot_kernel.cl
) - OpenGL rendering and window management
- User input handling
-
CUDA Implementation (
src/cuda/mandelbrot_cuda.cpp
):- NVIDIA-optimized GPU acceleration
- CUDA kernel execution (
src/cuda/mandelbrot_kernel.cu
) - CUDA-OpenGL interoperability for maximum performance
- Same user interface as OpenCL version
-
CPU Implementation (
src/cpu/mandelbrot_cpu.cpp
):- CPU-only computation for comparison
- Same controls and functionality
- Multi-threaded CPU parallelization
-
Compute Kernels:
- OpenCL kernel (
src/opencl/mandelbrot_kernel.cl
): Cross-platform GPU code - CUDA kernel (
src/cuda/mandelbrot_kernel.cu
): NVIDIA-optimized GPU code - Both include optimized Mandelbrot computation and coloring
- OpenCL kernel (
- User input updates view parameters (center, zoom, iterations)
- OpenCL kernel computes Mandelbrot set on GPU
- Results written to OpenGL texture via OpenCL-OpenGL interop
- OpenGL renders fullscreen quad with texture
- Display updates in real-time
- User input updates view parameters (center, zoom, iterations)
- CUDA kernel computes Mandelbrot set on GPU
- Results transferred via CUDA-OpenGL interoperability
- OpenGL renders fullscreen quad with texture
- Display updates in real-time (typically faster than OpenCL)
You can modify the main program to use different kernels:
mandelbrot
: Basic versionmandelbrot_smooth
: Better visual qualitymandelbrot_optimized
: Maximum performance
The kernels include multiple color mapping functions. You can:
- Modify the
mapColor
function in the kernel - Add new color palettes
- Implement user-selectable color schemes
- Adjust work group sizes for your GPU
- Modify iteration unrolling in optimized kernel
- Experiment with different precision levels
-
No OpenCL devices found:
- Install proper GPU drivers
- Install OpenCL runtime for your hardware
- Check with
make check-opencl
orclinfo
command - Try
mandelbrot_cpu
as fallback
-
Compilation errors:
- Verify OpenCL headers are installed
- Check compiler version (C++17 required)
- Try:
sudo apt install opencl-headers ocl-icd-opencl-dev
-
Runtime errors:
- Ensure OpenGL context is created before OpenCL
- Check GPU memory availability
- Try running with lower iteration count
-
CUDA compilation fails:
- Check if CUDA toolkit is installed:
nvcc --version
- Install CUDA:
make install-cuda-ubuntu
(or your distro) - Verify NVIDIA drivers:
nvidia-smi
- Check if CUDA toolkit is installed:
-
No CUDA devices found:
- Check GPU compatibility:
make check-cuda
- Ensure NVIDIA GPU with Compute Capability 5.0+
- Update NVIDIA drivers to 470+
- Try
build/mandelbrot_opencl
(OpenCL) as alternative
- Check GPU compatibility:
-
CUDA runtime errors:
- Check GPU memory with
nvidia-smi
- Ensure no other CUDA applications are running
- Try reducing window resolution
- Check GPU memory with
-
Slow rendering:
- Try CUDA version for NVIDIA GPUs:
build/mandelbrot_cuda
- Reduce iteration count with
-/+
keys - Check if using integrated vs dedicated GPU
- Monitor GPU usage with
nvidia-smi
orradeontop
- Try CUDA version for NVIDIA GPUs:
-
Input lag:
- Enable VSync in graphics drivers
- Reduce window size for testing
- Check system load with
htop
-
Build issues:
- Install dependencies:
make install-deps-ubuntu
- For CUDA:
make install-cuda-ubuntu
- Check compiler:
gcc --version
(need GCC 7+)
- Install dependencies:
# Test different versions
build/mandelbrot_opencl # Try OpenCL first
build/mandelbrot_cuda # Try CUDA if you have NVIDIA
build/mandelbrot_cpu # CPU fallback
# Check system compatibility
make check-opencl # Check OpenCL devices
make check-cuda # Check CUDA devices
lspci | grep VGA # List graphics cards
# Debug build for more error info
make debug
build/mandelbrot_opencl
Your Hardware | Recommended | Command | Performance |
---|---|---|---|
NVIDIA GPU + CUDA | mandelbrot_cuda |
make cuda && build/mandelbrot_cuda |
⭐⭐⭐⭐⭐ Best |
NVIDIA GPU (no CUDA) | mandelbrot_opencl |
make opencl && build/mandelbrot_opencl |
⭐⭐⭐⭐ Excellent |
AMD GPU | mandelbrot_opencl |
make opencl && build/mandelbrot_opencl |
⭐⭐⭐⭐ Excellent |
Intel GPU | mandelbrot_opencl |
make opencl && build/mandelbrot_opencl |
⭐⭐⭐ Good |
No GPU/Issues | mandelbrot_cpu |
make cpu && build/mandelbrot_cpu |
⭐⭐ Baseline |
# For most users (OpenCL):
make install-deps-ubuntu && make opencl && build/mandelbrot_opencl
# For NVIDIA users wanting maximum performance (CUDA):
make install-cuda-ubuntu && make cuda && build/mandelbrot_cuda
Inspired by Javidx9's Mandelbrot on olcPixelGameEngine
This project is licensed under the MIT License - see the LICENSE file for details.
Feel free to submit improvements:
- Additional kernel optimizations
- New color schemes
- Platform-specific enhancements
- Bug fixes and documentation updates