Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Alibaba MNN OpenCL CL Header

From Leeroopedia


Knowledge Sources
Domains GPU_Computing, OpenCL
Last Updated 2026-02-10 12:00 GMT

Overview

Vendored Khronos OpenCL C API header declaring the complete set of core OpenCL functions for device management, memory allocation, kernel execution, and command queue operations used by MNN's GPU backend.

Description

cl.h is the standard Khronos OpenCL C API header that declares all core OpenCL runtime functions spanning platform queries, device enumeration, context creation, command queue management, memory buffer allocation, image objects, sampler configuration, program compilation, kernel dispatch, and event synchronization. MNN vendors this header under 3rd_party/OpenCLHeaders/CL/ so that the OpenCL backend can compile against a known API version without requiring a system-installed OpenCL SDK. The header covers OpenCL versions 1.0 through 2.2, with version-gated preprocessor blocks for newer API additions.

Usage

This header is included internally by MNN's OpenCL backend. It is not directly imported by end users.

Code Reference

Source Location

Signature

// Platform APIs
extern CL_API_ENTRY cl_int CL_API_CALL
clGetPlatformIDs(cl_uint          num_entries,
                 cl_platform_id * platforms,
                 cl_uint *        num_platforms) CL_API_SUFFIX__VERSION_1_0;

// Device APIs
extern CL_API_ENTRY cl_int CL_API_CALL
clGetDeviceIDs(cl_platform_id   platform,
               cl_device_type   device_type,
               cl_uint          num_entries,
               cl_device_id *   devices,
               cl_uint *        num_devices) CL_API_SUFFIX__VERSION_1_0;

// Context APIs
extern CL_API_ENTRY cl_context CL_API_CALL
clCreateContext(const cl_context_properties * properties,
                cl_uint                 num_devices,
                const cl_device_id *    devices,
                void (CL_CALLBACK * pfn_notify)(...),
                void *                  user_data,
                cl_int *                errcode_ret) CL_API_SUFFIX__VERSION_1_0;

// Memory Object APIs
extern CL_API_ENTRY cl_mem CL_API_CALL
clCreateBuffer(cl_context   context,
               cl_mem_flags flags,
               size_t       size,
               void *       host_ptr,
               cl_int *     errcode_ret) CL_API_SUFFIX__VERSION_1_0;

// Kernel Execution APIs
extern CL_API_ENTRY cl_int CL_API_CALL
clEnqueueNDRangeKernel(cl_command_queue command_queue,
                       cl_kernel        kernel,
                       cl_uint          work_dim,
                       const size_t *   global_work_offset,
                       const size_t *   global_work_size,
                       const size_t *   local_work_size,
                       cl_uint          num_events_in_wait_list,
                       const cl_event * event_wait_list,
                       cl_event *       event) CL_API_SUFFIX__VERSION_1_0;

Import

#include "3rd_party/OpenCLHeaders/CL/cl.h"

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 functions OpenCL API function declarations and type definitions for platform, device, context, memory, program, kernel, and event management

Usage Examples

// MNN internal usage (not user-facing)
#include "CL/cl.h"

// Query available platforms
cl_platform_id platform;
clGetPlatformIDs(1, &platform, NULL);

// Get a GPU device
cl_device_id device;
clGetDeviceIDs(platform, CL_DEVICE_TYPE_GPU, 1, &device, NULL);

// Create a context and command queue
cl_int err;
cl_context context = clCreateContext(NULL, 1, &device, NULL, NULL, &err);
cl_command_queue queue = clCreateCommandQueueWithProperties(context, device, NULL, &err);

// Allocate a buffer and enqueue a kernel
cl_mem buffer = clCreateBuffer(context, CL_MEM_READ_WRITE, 1024, NULL, &err);
clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &global_size, &local_size, 0, NULL, NULL);

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment