Building applications with PyOpenCL

The first step in the construction of a program for PyOpenCL is the coding of the host application. This is performed on the CPU and has the task of managing the possible execution of the kernel on the GPU card (that is, the device).

A kernel is a basic unit of executable code, similar to a C function. It can be data-parallel or task-parallel. However, the cornerstone of PyOpenCL is the exploitation of parallelism.

A fundamental concept is a program, which is a collection of kernels and other functions, analogous to dynamic libraries. So, we can group instructions in a kernel and group different kernels into a program.

Programs can be called from applications. We have the execution queues that indicate the order in which the kernels are executed. However, in some cases, these can be launched without following the original order.

We can finally list the fundamental elements for developing an application with PyOpenCL:

  • Device: This identifies the hardware in which the kernel code is to be executed. Note that the PyOpenCL application can be run on both CPU and GPU boards (as well as PyCUDA) but also on embedded devices such as Field-Programmable Gate Arrays (FPGAs).
  • Program: This is a group of kernels that has the task of selecting which kernel must be run on the device.
  • Kernel: This is the code to execute on the device. A kernel is a C-like function, which means it can be compiled on any device that supports PyOpenCL drivers.
  • Command queue: This orders the execution of kernels on the device.
  • Context: This is a group of devices that allows devices to receive kernels and transfer data.

The following diagram shows how this data structure can work in a host application:

PyOpenCL programming model

Again, we observe that a program can contain more functions to run on the device and that each kernel encapsulates only a single function from the program.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.137.172.115