concept pyopencl in category opencl

appears as: PyOpenCL, PyOpenCL
OpenCL in Action: How to Accelerate Graphics and Computation

This is an excerpt from Manning's book OpenCL in Action: How to Accelerate Graphics and Computation.

Andreas Klöckner of the Courant Institute of Mathematical Sciences has extended Python’s breadth of capabilities by releasing PyOpenCL. The classes and functions in this package make it possible to construct host applications with the same features as those coded in regular C. But before we delve into the internals of PyOpenCL, it’s important to know how to obtain the toolset.

A Platform object represents an OpenCL framework installed on the host, such as Nvidia’s SDK or AMD’s SDK. To access the installed platforms, you need to call the get_platforms function of the pyopencl module. This returns a list of Platform objects, and you can examine the properties of each by calling get_info. This works like the getInfo function in the C++ Wrapper API and accepts a parameter that identifies the type of information being sought. For example, the following code prints the names of each platform installed on the system and their supported extensions:

import pyopencl
for platform in pyopencl.get_platforms():
   print("%s: %s" % (platform.get_info(pyopencl.platform_info.NAME),
                     platform.get_info(pyopencl.platform_info.EXTENSIONS)))

Repeating the full name of the pyopencl module can be tiresome, so the following code sets its name to cl and accomplishes the same result:

import pyopencl as cl
for platform in cl.get_platforms():
   print("%s: %s" % (platform.get_info(cl.platform_info.NAME),
                     platform.get_info(cl.platform_info.EXTENSIONS)))

As shown, the names of PyOpenCL’s constants and enumerated types can be found by removing the cl_/CL_ from the corresponding names in the C API. In the preceding example code, cl_platform_info becomes platform_info and CL_NAME becomes NAME. This naming convention holds true throughout PyOpenCL, so we won’t discuss the get_info function further.

Listing 9.5. Creating command queues with PyOpenCL: create_queue.py
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest