Using the PyCUDA stream class

We will start with a simple PyCUDA program; all this will do is generate a series of random GPU arrays, process each array with a simple kernel, and copy the arrays back to the host. We will then modify this to use streams. Keep in mind this program will have no point at all, beyond illustrating how to use streams and some basic performance gains you can get. (This program can be seen in the file, under the 5 directory in the GitHub repository.)

Of course, we'll start by importing the appropriate Python modules, as well as the time function:

import pycuda.autoinitimport pycuda.driver as drvfrom pycuda import gpuarrayfrom pycuda.compiler import SourceModuleimport numpy as npfrom time import time ...

Get Hands-On GPU Programming with Python and CUDA now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.