Introduction
In the realm of heterogeneous computing, OpenCL provides a standard for executing code across diverse hardware like CPUs and GPUs. A critical performance factor in OpenCL is the selection of "workgroup size"—the number of threads that collaborate on a local data chunk. For stencil patterns, which are ubiquitous in scientific computing and image processing, the optimal workgroup size is highly dependent on the specific hardware architecture, the kernel complexity, and the input data dimensions.
As shown in Figure 1, stencil operations involve updating a cell's value based on its neighbors, requiring careful management of data tiles and workgroup boundaries. Because the performance landscape is non-linear and often "noisy," developers frequently rely on suboptimal "magic numbers" or exhaustive manual tuning. This paper presents a machine learning-based autotuning framework designed to automate this selection process.
Methodology
The authors frame workgroup size selection as a supervised learning problem, utilizing 102 features that characterize the hardware (via OpenCL API), the kernel (via static analysis of LLVM IR), and the dataset (matrix dimensions). Three primary machine learning strategies were evaluated:
- Classification: Directly predicting the single best ("Oracle") workgroup size.
- Runtime Regression: Predicting the absolute execution time for a given configuration.
- Relative Performance Regression: Predicting the speedup of a configuration relative to a safe baseline.
The study found that Relative Performance Regression using Random Forests was the most effective strategy. By focusing on relative speedup rather than absolute time, the model better accounts for hardware-level complexities and dynamic behaviors that are difficult to capture through static analysis alone.
Experimental Evaluation
The framework was tested across a diverse set of seven hardware platforms, including Intel CPUs, an AMD Tahiti GPU, and multiple generations of Nvidia GPUs. The dataset comprised thousands of unique stencil programs generated via a synthetic template engine, alongside six real-world benchmarks such as Canny Edge Detection and Gaussian Blur.
A significant challenge identified during testing was the "Refusal" problem: many workgroup sizes that theoretically fit within hardware limits were rejected by drivers with CL_OUT_OF_RESOURCES errors. This was particularly common on Intel CPUs, necessitating robust fallback strategies like "Nearest Neighbor" to ensure the autotuner always selects a valid configuration.
Key Results
The autotuning framework demonstrated substantial performance improvements over fixed-size baselines. The system achieved a median 3.79× speedup compared to the best possible fixed workgroup setting and reached 94% of the theoretical maximum performance determined by exhaustive search.
As illustrated in Figure 6, the regression-based approach (b) consistently outperformed simple classification (a) across various test scenarios, including cross-kernel and cross-device evaluations. While classification offers lower prediction overhead, the superior accuracy and speedup of relative regression justify its use in high-performance contexts.
Conclusion
This research highlights the necessity of autotuning for achieving portable high performance in OpenCL. By leveraging machine learning to navigate the complex trade-offs of workgroup sizing, the proposed framework nears the performance of exhaustive search while remaining practical for real-world deployment. Future work includes exploring online learning techniques to adapt to runtime-only variables and extending the framework to other parallel computing patterns.
Summary updated