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

