Intel oneAPI OpenCL Runtime

2025.3.3 · active · verified Fri Apr 17

The `intel-opencl-rt` PyPI package provides the Intel® oneAPI OpenCL* Runtime library. This is not a Python library with direct importable modules but rather a runtime provider, installing shared libraries (.so, .dll) that other Python OpenCL binding libraries (e.g., `pyopencl`) can then utilize to access Intel GPUs or CPUs with integrated graphics for general-purpose computing. It is part of the broader Intel oneAPI ecosystem and is updated periodically, often in sync with major oneAPI releases.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `pyopencl` to perform a simple vector addition, leveraging the OpenCL runtime provided by `intel-opencl-rt`. You will first need to install `pyopencl` (e.g., `pip install pyopencl`). The `intel-opencl-rt` package installs the backend necessary for `pyopencl` to find and utilize Intel OpenCL devices.

import pyopencl as cl
import numpy

# This code assumes intel-opencl-rt has installed the necessary drivers/runtime.
# pyopencl will automatically find and use the available OpenCL platform.

try:
    platform = cl.get_platforms()[0] # Get the first OpenCL platform
    devices = platform.get_devices() # Get devices for the platform
    
    # Filter for Intel devices if desired, or use the first available.
    intel_devices = [d for d in devices if 'Intel' in d.vendor]
    device_to_use = intel_devices[0] if intel_devices else devices[0]

    ctx = cl.Context([device_to_use])
    queue = cl.CommandQueue(ctx)

    a = numpy.random.rand(50000).astype(numpy.float32)
    b = numpy.random.rand(50000).astype(numpy.float32)

    mf = cl.mem_flags
    a_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=a)
    b_buf = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=b)
    dest_buf = cl.Buffer(ctx, mf.WRITE_ONLY, a.nbytes)

    prg = cl.Program(ctx, "__kernel void sum(__global const float *a, __global const float *b, __global float *c) { int gid = get_global_id(0); c[gid] = a[gid] + b[gid]; }").build()

    prg.sum(queue, a.shape, None, a_buf, b_buf, dest_buf)
    c = numpy.empty_like(a)
    cl.enqueue_copy(queue, c, dest_buf).wait()

    print(f"OpenCL platform: {platform.name}")
    print(f"OpenCL device: {device_to_use.name}")
    print(f"Result calculated via OpenCL: {c[:5]}...")
    print(f"Expected result: {(a+b)[:5]}...")
    assert numpy.allclose(c, a + b)
    print("OpenCL computation successful!")

except cl.LogicError as e:
    print(f"Failed to initialize OpenCL: {e}")
    print("Please ensure OpenCL drivers are installed and a compatible device is present.")
except IndexError:
    print("No OpenCL platforms or devices found. Ensure Intel OpenCL runtime is correctly installed and compatible hardware is available.")

view raw JSON →