liveBook · Manning

Cuda.jit

As explained in Fast Python: High performance techniques for large datasets

cuda.jit is a Numba decorator that compiles a Python function for execution as CUDA device code or a GPU kernel.

cuda.jit compiles a Python function with Numba for CUDA execution. Without options, the function acts as a GPU kernel: each invocation handles one element, writes through its arguments, and is launched across configured thread blocks. section 9.2.2

A basic kernel follows this execution model:

  • Use cuda.grid to calculate the current thread's position. section 9.2.2
  • Guard against threads whose positions exceed the array dimensions. section 9.2.2
  • Launch with kernel[blocks_per_grid, threads_per_block](arguments), rather than calling the function normally. section 9.2.2
  • Pass output arrays as arguments because ordinary kernel functions cannot return values. section 9.2.2

Doubling an array

A one-dimensional kernel uses one thread per array element: position = cuda.grid(1); if position < my_array.shape[0]: my_array[position] *= 2. For 1,000 elements, 50 blocks of 20 threads cover the array exactly; when coverage is not exact, bounds checks prevent invalid accesses.

Mandelbrot rendering

Use @cuda.jit(device=True) for a helper that computes one Mandelbrot point and returns its value. A separate two-dimensional kernel maps threads to image coordinates, fills a NumPy array, and launches a 2D grid rounded up with ceil(size / 16). section 9.2.3

How Fast Python: High performance techniques for large datasets covers it

How deep it goes
Explains decorator modes, thread indexing, launch geometry, bounds checks, return constraints, and unsupported Python inputs.
Examples
Demonstrates array doubling, matrix doubling, and Mandelbrot image generation with one- and two-dimensional launches.
What it assumes
Assumes Python and NumPy familiarity while teaching thread indexing, block-grid distribution, device calls, and GPU-specific constraints.
What you can do after
Build GPU kernels with Numba, configure their CUDA launch geometry, and diagnose unsupported inputs or incomplete coverage.
How it connects
Connects Numba to CUDA kernels, device functions, thread blocks, grids, and NumPy arrays.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Fast Python: High performance techniques for large datasets. The section links point to the parts of the book it draws on.