liveBook · Manning

CuPy

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

CuPy is an open-source Python library providing a NumPy-compatible interface for GPU-accelerated numerical computing.

CuPy provides NumPy-like array processing on a GPU. Its familiar interface reduces the need to write GPU code directly, but switching libraries does not remove the need to consider where data lives and how GPU work is measured. section 9.0 section 9.3.3

import numpy as np
import cupy as cp
 
size = 5000
 
my_matrix = cp.ones((size, size), dtype=cp.uint8)
print(type(my_matrix))
np_matrix = my_matrix.get()
print(type(np_matrix))
2 * my_matrix
 
2 * np_matrix

The matrix example shows the boundary: `cp.ones` creates a CuPy array in GPU memory, so `2 * my_matrix` runs there. Calling `.get()` transfers the data and creates a separate NumPy array in CPU memory. section 9.3.3

Measure GPU work with `cupyx.time.repeat`, not ordinary Python timing alone: GPU execution proceeds independently of CPU execution. In 200 repetitions of doubling a 5000 × 5000 CuPy matrix, one reported machine averaged about 61 µs of CPU time and 786 µs of GPU time per execution. section 9.3.3

A Mandelbrot generator illustrates two ways to compute into CuPy arrays:

  • Launch a Numba-decorated GPU function with a CuPy output array. Convert the result to NumPy before creating an image with Pillow; that conversion transfers data back to CPU memory. section 9.3.4
  • Use `cp.RawKernel` to embed CUDA C, accepting CuPy inputs and outputs. One reported kernel timing was about 3.15 ms, versus about 70.6 ms for the Numba implementation; these are timings of the demonstrated kernels, not a universal library comparison. section 9.3.4 section 9.3.5

These examples target NVIDIA CUDA hardware. Running them requires a recent NVIDIA GPU, suitable drivers, the CUDA toolkit, and CuPy. section 9.0

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

How deep it goes
Works through array residency, transfers, GPU-aware timing, and Mandelbrot kernels; leaves CUDA C details largely unexplored.
Examples
Benchmarks doubling a 5000 × 5000 matrix and generating a Mandelbrot image with an embedded CUDA C kernel.
What it assumes
Assumes familiarity with Python and NumPy-style arrays while illustrating GPU memory transfers and timing with runnable examples.
What you can do after
Apply it via CuPy and cupyx.time.repeat to run GPU array operations and measure GPU execution time.
How it connects
Connects NumPy arrays, Numba GPU kernels, CUDA C kernels, and GPU profiling through CuPy arrays.

Also explained in

Compare how 2 books explain CuPy →

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.