4.1_CUDA_Array_Interface
CUDA Array Interface
Because moving data from the CPU to GPU is expensive we want to keep as much data located on the GPU as possible at all times.
Sometimes in our workflow we want to change which tool we are using too. Perhaps we load an array of data with cupy but we want to write a custom CUDA kernel with numba. Or perhaps we want to switch to using a Deep Learning framework like pytorch.
When any of these libraries load data onto the GPU the array in memory is pretty much the same, the differences between a cupy ndarray and a numba DeviceNDArray just boil down to how that array is wrapped and hooked into Python.
Thankfully with utilities like DLPack and __ cuda_array__interface __ we can convert from one type to another without modifying the data on the GPU. We just create a new Python wrapper object and transfer all the device pointers accross.
Ensuring compatibility between popular GPU Python libraries is one of the core goals of the RAPIDS community.
Image unavailable: (unlabeled image) — original archive file images/array-interface.png is absent.
Let's see this in action!
We start off my creating an array with cupy.
import cupy as cp
cp_arr = cp.random.random((1, 100_000, 10_000))
cp_arrtype(cp_arr)Now let's convert this to a Numba array.
from numba import cuda
numba_arr = cuda.to_device(cp_arr)
numba_arrNotice that the GPU memory usage stays the same. This is because both cp_arr and numba_arr reference the same underlying data array, but are different types.
We can also convert our array to a pytorch Tensor object.
import torch # Requires pytorchtorch_arr = torch.as_tensor(numba_arr, device='cuda')
torch_arrtype(torch_arr)