Manual context creation

So far, we have been importing pycuda.autoinit at the beginning of all of our PyCUDA programs; this effectively creates a context at the beginning of our program and has it destroyed at the end.

Let's try doing this manually. We will make a small program that just copies a small array to the GPU, copies it back to the host, prints the array, and exits.

We start with the imports:

import numpy as npfrom pycuda import gpuarrayimport pycuda.driver as drv

First, we initialize CUDA with the pycuda.driver.init function, which is here aliased as drv:

drv.init()

Now we choose which GPU we wish to work with; this is necessary for the cases where one has more than one GPU. We can select a specific GPU with pycuda.driver.Device ...

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.