Performance-optimized array sum

For the final example of this book, we will now make a standard array summation kernel for a given array of doubles, except this time we will use every trick that we've learned in this chapter to make it as fast as possible. We will check the output of our summing kernel against NumPy's sum function, and then we will run some tests with the standard Python timeit function to compare how our function compares to PyCUDA's own sum function for gpuarray objects.

Let's get started by importing all of the necessary libraries, and then start with a laneid function, similar to the one we used in the previous section:

from __future__ import divisionimport numpy as npfrom pycuda.compiler import SourceModuleimport pycuda.autoinit ...

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.