Passing numpy arrays to your library
In our first integration we passed in numpy arrays to our library using ctypes
, and using numpy
is essentially ubiquitous in scientific computing with Python. It provides efficient, contiguous memory storage, vectorized operations, and uses accelerated CPU-based libraries. By integrating the C API of numpy
into our C extension, we will be able to use the data that has been created and preprocessed on the Python side to execute further calculations inside our GPU. This means we can put together the best of two worlds and but retain the Python feel. In many cases, the performance gains can be orders of magnitude higher than pure Python implementations, especially for operations involving large arrays.
We will change a little bit of our previous code to make the numpy
API available. The first change is to import it in our vector_add_np_wrapper.c
file with:
#include <numpy/arrayobject.h>
numpy
as np
Python allows us...