This repository contains a high-performance implementation of the Sobel Filter for real-time video processing, optimized for GPU architectures using OpenCL. The project documents the transition from a sequential CPU baseline to a highly optimized GPU kernel achieving over 138x speedup on the NVIDIA K20 (Alma Cluster).
The core objective was to maximize throughput for high-resolution video frames (~120 million operations/sec) by optimizing memory access patterns and hardware occupancy.
The project followed a 4-stage optimization roadmap. Benchmarks were conducted on the NVIDIA K20 (Alma Cluster) processing 200 frames of high-resolution video.
| Implementation | Strategy | Avg. Kernel Time | Speedup vs. CPU |
|---|---|---|---|
| CPU Baseline | Sequential C++ | 76.65 ms | 1x |
| GPU Baseline | Global Memory Access | 0.867 ms | 88.4x |
| Math Optimization | native_sqrt & Relaxation |
0.828 ms | 92.6x |
| Local Tiling | Local Memory (SRAM) | 0.552 ms | 138.8x |
The final iteration (kernel_3.cl) utilizes a Local Memory Tiling strategy.
- SRAM Utilization: By loading pixel "tiles" into shared local memory, the kernel eliminates redundant global memory reads for the 3x3 convolution window.
- Strided Loading: Replaced boundary branching with strided loops to ensure all threads in a warp participate in data loading, effectively eliminating warp divergence.
- Bandwidth Efficiency: This approach bypassed L1 cache throughput limits, achieving the peak theoretical performance of the hardware.
filter_sobel.cpp: Original sequential CPU implementation for performance baselining.host_3.cpp: Advanced OpenCL host code featuring command queue management, profiling via OpenCL events, and dynamic work-group scaling.kernel_3.cl: Final optimized kernel using Local Memory (SRAM) tiling and strided memory access.speed.txt: Comprehensive performance analysis across various Work-Group sizes (8x1 to 32x32).
- Heterogeneous Programming: Managing Host (CPU) and Device (GPU) memory buffers.
- Memory Hierarchy Tuning: Minimizing Global Memory latency through Local Memory (SRAM) caching.
- Warp Divergence Mitigation: Using strided loads to ensure high SIMD efficiency.
- Precision Trade-offs: Utilizing
native_math functions for hardware-level acceleration.
- OpenCL Headers and ICD Loader
- GCC with C++17 support
g++ -O2 host_3.cpp -lOpenCL -o sobel_gpuThe host application is configured to process sequential PPM frames:
./sobel_gpuNote: The host code dynamically loads the .cl source file. Ensure the kernel file is present in the same directory. Developed as part of the MSc Computer Science curriculum at the University of Vienna.