Implementation:Ggml org Ggml Opencl backend api
Metadata
| Field | Value |
|---|---|
| Page Type | Implementation (API Doc) |
| Knowledge Sources | GGML |
| Domains | ML_Infrastructure, Tensor_Computing, GPU_Computing |
| Last Updated | 2025-05-15 12:00 GMT |
Overview
Public C header declaring the OpenCL backend interface for running tensor operations on GPUs and accelerators that support the OpenCL standard.
Description
ggml-opencl.h is a minimal header (26 lines) that exposes the OpenCL backend's public API. It declares five functions:
ggml_backend_opencl_init: Initializes and returns an OpenCL backend instance, discovering the best available OpenCL device.ggml_backend_is_opencl: Type-checks whether a given backend handle is an OpenCL backend.ggml_backend_opencl_buffer_type: Returns the buffer type for OpenCL device memory allocations.ggml_backend_opencl_host_buffer_type: Returns the buffer type for host-pinned memory, enabling faster CPU-GPU transfers via DMA.ggml_backend_opencl_reg: Returns the backend registration handle for the auto-discovery system.
All functions are marked with GGML_BACKEND_API for proper symbol visibility and are wrapped in extern "C" for C++ compatibility.
Usage
Include this header in application code to access the OpenCL backend. In most cases, the backend is loaded automatically via ggml_backend_load_all(), and this header is only needed for explicit initialization or type checking.
Code Reference
Source Location
GGML repo, file: include/ggml-opencl.h (26 lines).
Signatures
GGML_BACKEND_API ggml_backend_t ggml_backend_opencl_init(void);
GGML_BACKEND_API bool ggml_backend_is_opencl(ggml_backend_t backend);
GGML_BACKEND_API ggml_backend_buffer_type_t ggml_backend_opencl_buffer_type(void);
GGML_BACKEND_API ggml_backend_buffer_type_t ggml_backend_opencl_host_buffer_type(void);
GGML_BACKEND_API ggml_backend_reg_t ggml_backend_opencl_reg(void);
Import
#include "ggml-opencl.h"
I/O Contract
Inputs
| Parameter | Type | Required | Description |
|---|---|---|---|
| (none for most functions) | -- | -- | ggml_backend_opencl_init, buffer type, and registration functions take no parameters.
|
backend |
ggml_backend_t |
Yes | Backend handle to type-check (for ggml_backend_is_opencl).
|
Outputs
| Output | Type | Description |
|---|---|---|
| Backend handle | ggml_backend_t |
Initialized OpenCL backend, or NULL on failure.
|
| Type check | bool |
true if the backend is OpenCL-based.
|
| Buffer type | ggml_backend_buffer_type_t |
Buffer type handle for device or host-pinned memory. |
| Registration | ggml_backend_reg_t |
Registration handle for backend auto-discovery. |
Usage Examples
#include "ggml-opencl.h"
// Initialize the OpenCL backend
ggml_backend_t backend = ggml_backend_opencl_init();
// Verify it is an OpenCL backend
if (backend && ggml_backend_is_opencl(backend)) {
// Get the device buffer type for allocations
ggml_backend_buffer_type_t buft = ggml_backend_opencl_buffer_type();
// ... use backend for computation ...
ggml_backend_free(backend);
}