Numba is a just-in-time compiler that converts a subset of Python into fast native code. It supports numerical libraries such as NumPy and targets including GPUs. It is an alternative to reimplementing performance-critical code in a lower-level language such as Cython, and may offer comparable gains with less work. section 5.1
GPU programming requires more than decorating sequential CPU code. The CPU launches a kernel that runs across many GPU threads; the algorithm must be organized to exploit that parallelism. section 9.0 section 9.1.4 section 9.4
Double an array on a GPU
Instead of looping over the array inside the kernel, assign one element to each thread. Decorate the function with cuda.jit, use cuda.grid(1) to obtain the thread’s position, and modify the array in place. A kernel cannot return its result as an ordinary function value. section 9.2.2
from numba import cuda
@cuda.jit #1
def double(my_array):
position = cuda.grid(1) #2
my_array[position] *= 2
This kernel doubles one array element per thread; it assumes every launched position is valid. section 9.2.2
A kernel launch must specify blocks per grid and threads per block. For 1,000 elements, 50 blocks of 20 threads provide exactly 1,000 positions. Check the result: too few threads leave elements unchanged. section 9.2.2
import numpy as np blocks_per_grid = 50 threads_per_block = 20 my_array = np.ones(1000) double[blocks_per_grid, threads_per_block](my_array) #1 assert (my_array == 2).all() #2
This launch applies the kernel to a NumPy array and checks that every element was doubled. section 9.2.2
When the launch covers more positions than the array contains, guard against out-of-bounds access. The explicit index calculation combines the block index, block size, and thread index; the guard must reject positions greater than or equal to the array length. section 9.2.2
@cuda.jit
def double_safe_explicit(my_array):
position = cuda.blockIdx.x * cuda.blockDim.x + cuda.threadIdx.x
if position >= my_array.shape[0]:
return
my_array[position] *= 2
This version calculates the position explicitly and returns before accessing an invalid element. section 9.2.2
Before running CUDA code, install the appropriate GPU drivers and CUDA toolkit. Run numba -s to inspect detected hardware and library availability; some older GPUs may not be supported. section 9.2.1
