Implementation:Alibaba MNN OpenCL CL2 HPP Header
| Knowledge Sources | |
|---|---|
| Domains | GPU_Computing, OpenCL |
| Last Updated | 2026-02-10 12:00 GMT |
Overview
Vendored modern Khronos OpenCL C++ bindings header (cl2.hpp) providing updated C++11/14 RAII wrappers for OpenCL 2.0+ features including SVM, pipes, and improved memory model for MNN's GPU backend.
Description
cl2.hpp is the modern replacement for the deprecated cl.hpp, redesigned to leverage C++11 and C++14 features such as move semantics, std::array, and variadic templates. It provides the same RAII wrapper classes (cl::Context, cl::CommandQueue, cl::Buffer, cl::Kernel, cl::Program) as its predecessor but adds support for OpenCL 2.0+ constructs including Shared Virtual Memory (SVM) allocators (cl::SVMAllocator, cl::SVMTraitReadWrite, cl::SVMTraitFine, cl::SVMTraitAtomic), pipe objects, and the unified memory model. MNN vendors this header to enable its OpenCL backend to take advantage of modern GPU capabilities when available while maintaining backward compatibility through compile-time version detection.
Usage
This header is included internally by MNN's OpenCL backend. It is not directly imported by end users.
Code Reference
Source Location
- Repository: Alibaba_MNN
- File: 3rd_party/OpenCLHeaders/CL/cl2.hpp
- Lines: 1-9685
Signature
namespace cl {
class Device : public detail::Wrapper<cl_device_id> {
public:
Device() {}
Device(cl_device_id device) { ... }
static Device getDefault(cl_int *errResult = NULL);
};
class Context : public detail::Wrapper<cl_context> {
public:
Context(const vector<Device>& devices, ...);
static Context getDefault(cl_int *errResult = NULL);
};
class Buffer : public Memory {
public:
Buffer(const Context& context, cl_mem_flags flags, size_t size, ...);
Buffer(cl_mem_flags flags, size_t size, ...); // uses default context
};
// OpenCL 2.0+ SVM support
template<class SVMTrait>
class SVMAllocator {
public:
typedef typename SVMTrait::value_type value_type;
pointer allocate(size_type size);
void deallocate(pointer p, size_type size);
};
class SVMTraitReadWrite { ... };
class SVMTraitReadOnly { ... };
class SVMTraitFine { ... };
class SVMTraitAtomic { ... };
} // namespace cl
Import
#include "3rd_party/OpenCLHeaders/CL/cl2.hpp"
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| N/A | N/A | N/A | Header-only; no runtime inputs |
Outputs
| Name | Type | Description |
|---|---|---|
| API declarations | C++ classes | Modern C++11/14 RAII wrapper classes for OpenCL objects, including SVM allocators and OpenCL 2.0+ constructs |
Usage Examples
// MNN internal usage (not user-facing)
#define CL_HPP_TARGET_OPENCL_VERSION 200
#include "CL/cl2.hpp"
// Use default platform and device (C++11 style)
cl::Device device = cl::Device::getDefault();
cl::Context context = cl::Context::getDefault();
cl::CommandQueue queue = cl::CommandQueue::getDefault();
// Create buffer using default context
cl::Buffer buffer(CL_MEM_READ_WRITE, 1024);
// Build program and create kernel
cl::Program program(context, sourceCode);
program.build("-cl-std=CL2.0");
cl::Kernel kernel(program, "my_kernel");
// SVM allocation (OpenCL 2.0+)
cl::SVMAllocator<int, cl::SVMTraitReadWrite<>> svmAlloc(context);
int* svmPtr = svmAlloc.allocate(256);
// Execute kernel
queue.enqueueNDRangeKernel(kernel, cl::NullRange, cl::NDRange(256));
queue.finish();
Related Pages
- Environment:Alibaba_MNN_GPU_OpenCL_Environment
- Alibaba_MNN_OpenCL_CL_Header - Underlying C API that this header wraps
- Alibaba_MNN_OpenCL_CL_HPP_Header - Deprecated predecessor C++ bindings (cl.hpp)
- Alibaba_MNN_OpenCL_CL_Platform_Header - Platform type definitions