Welcome to PyOpenCL’s documentation!¶
PyOpenCL gives you easy, Pythonic access to the OpenCL parallel computation API. What makes PyOpenCL special?
Object cleanup tied to lifetime of objects. This idiom, often called RAII in C++, makes it much easier to write correct, leak- and crash-free code.
Completeness. PyOpenCL puts the full power of OpenCL’s API at your disposal, if you wish. Every obscure
get_info()query and all CL calls are accessible.Automatic Error Checking. All errors are automatically translated into Python exceptions.
Speed. PyOpenCL’s base layer is written in C++, so all the niceties above are virtually free.
Helpful Documentation. You’re looking at it. ;)
Liberal license. PyOpenCL is open-source under the MIT license and free for commercial, academic, and private use.
Here’s an example, to give you an impression:
#!/usr/bin/env python
import numpy as np
import pyopencl as cl
rng = np.random.default_rng()
a_np = rng.random(50000, dtype=np.float32)
b_np = rng.random(50000, dtype=np.float32)
ctx = cl.create_some_context()
queue = cl.CommandQueue(ctx)
mf = cl.mem_flags
a_g = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=a_np)
b_g = cl.Buffer(ctx, mf.READ_ONLY | mf.COPY_HOST_PTR, hostbuf=b_np)
prg = cl.Program(ctx, """
__kernel void sum(
__global const float *a_g, __global const float *b_g, __global float *res_g)
{
int gid = get_global_id(0);
res_g[gid] = a_g[gid] + b_g[gid];
}
""").build()
res_g = cl.Buffer(ctx, mf.WRITE_ONLY, a_np.nbytes)
knl = prg.sum # Use this Kernel object for repeated calls
knl(queue, a_np.shape, None, a_g, b_g, res_g)
res_np = np.empty_like(a_np)
cl.enqueue_copy(queue, res_np, res_g)
# Check on CPU with Numpy:
error_np = res_np - (a_np + b_np)
print(f"Error:\n{error_np}")
print(f"Norm: {np.linalg.norm(error_np):.16e}")
assert np.allclose(res_np, a_np + b_np)
(You can find this example as
examples/demo.py in the PyOpenCL
source distribution.)
Tutorials¶
Gaston Hillar’s two-part article series in Dr. Dobb’s Journal provides a friendly introduction to PyOpenCL.
Simon McIntosh-Smith and Tom Deakin’s course Hands-on OpenCL contains both lecture slides and exercises (with solutions) (The course covers PyOpenCL as well as OpenCL’s C and C++ APIs.)
PyOpenCL course at DTU GPULab and Simula (2011): Lecture 1 Lecture 2 Problem set 1 Problem set 2
Ian Johnson’s PyOpenCL tutorial.
Contents¶
- OpenCL Runtime: Basics
- OpenCL Runtime: Constants
addressing_modechannel_orderchannel_typecommand_execution_statuscommand_queue_infocommand_queue_propertiescommand_typecontext_infocontext_propertiesdevice_affinity_domaindevice_atomic_capabilitiesdevice_device_enqueue_capabilitiesdevice_exec_capabilitiesdevice_fp_configdevice_infodevice_local_mem_typedevice_mem_cache_typedevice_partition_propertydevice_svm_capabilitiesdevice_topology_type_amddevice_typeevent_infofilter_modegl_context_infoimage_infokernel_arg_access_qualifierkernel_arg_address_qualifierkernel_arg_infokernel_arg_type_qualifierkernel_infokernel_sub_group_infokernel_work_group_infokhronos_vendor_idmap_flagsmem_flagsmem_infomem_migration_flagsmem_object_typepipe_infopipe_propertiesplatform_infoprofiling_infoprogram_binary_typeprogram_build_infoprogram_infoprogram_kindqueue_propertiessampler_infosampler_propertiesstatus_codesvm_mem_flagsversion_bitsNameVersionDeviceTopologyAmd
- OpenCL Runtime: Platforms, Devices and Contexts
- OpenCL Runtime: Command Queues and Events
- OpenCL Runtime: Memory
- OpenCL Runtime: Programs and Kernels
- OpenCL Runtime: OpenGL Interoperability
- Built-in Utilities
- Multi-dimensional arrays
- OpenCL Type Mapping
- Parallel Algorithms
- How-tos
- Installation
- Tips
- Guidelines
- User-visible Changes
- Unreleased
- Version 2022.2
- Version 2020.2
- Version 2018.2
- Version 2018.1
- Version 2017.2
- Version 2017.1
- Version 2016.2
- Version 2016.1
- Version 2015.2.4
- Version 2015.2.3
- Version 2015.2.2
- Version 2015.2.1
- Version 2015.2
- Version 2015.1
- Version 2014.1
- Version 2013.2
- Version 2013.1
- Version 2012.1
- Version 2011.2
- Version 2011.1.2
- Version 2011.1.1
- Version 2011.1
- Version 0.92
- Version 0.91.5
- Version 0.91.4
- Version 0.91.3
- Version 0.91.2
- Version 0.91.1
- Version 0.91
- Version 0.90.4
- Version 0.90.3
- Version 0.90.2
- Version 0.90.1
- Version 0.90
- License
- Frequently Asked Questions
- Citing PyOpenCL
- Acknowledgments
- Documentation Cross-References
- 🚀 Github
- đź’ľ Download Releases
Note that this guide does not explain OpenCL programming and technology. Please refer to the official Khronos OpenCL documentation for that.
PyOpenCL also has its own web site, where you can find updates, new versions, documentation, and support.