CuPy (CUDA 13.x)

14.0.1 · active · verified Thu Apr 16

CuPy is a NumPy/SciPy-compatible array library for GPU-accelerated computing with Python, acting as a drop-in replacement for existing NumPy/SciPy code on NVIDIA CUDA platforms. It leverages CUDA Toolkit libraries like cuBLAS and cuFFT for significant speedups in numerical computations on GPUs. The current version is 14.0.1, and major releases occur less frequently (e.g., v14 was the first in two years), with minor and revision updates more common.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic CuPy array creation, arithmetic operations on the GPU, transferring data between GPU and CPU, and performing a NumPy-like aggregation. It includes a check for GPU availability and the use of `cp.cuda.Stream.null.synchronize()` for explicit GPU synchronization, which is important for accurate performance measurement.

import cupy as cp
import numpy as np

# Check if a GPU is available
if cp.cuda.is_available():
    print(f"CuPy is available. Current device: {cp.cuda.Device().id}")

    # Create a CuPy array on the GPU
    x_gpu = cp.arange(10, dtype=cp.float32).reshape(2, 5)
    print(f"GPU array:\n{x_gpu}")
    print(f"Type of GPU array: {type(x_gpu)}")

    # Perform a computation on the GPU
    y_gpu = x_gpu * 2 + 1
    print(f"Result of computation on GPU:\n{y_gpu}")

    # Transfer the result back to CPU NumPy array
    y_cpu = cp.asnumpy(y_gpu)
    print(f"CPU array (from GPU):\n{y_cpu}")
    print(f"Type of CPU array: {type(y_cpu)}")

    # Demonstrate a simple NumPy-like operation
    sum_gpu = x_gpu.sum(axis=1)
    print(f"Sum along axis 1 on GPU: {sum_gpu}")
    print(f"Type of sum on GPU: {type(sum_gpu)}")

    # Ensure all GPU operations complete before proceeding (useful for timing)
    cp.cuda.Stream.null.synchronize()
else:
    print("No NVIDIA GPU found or CuPy is not properly installed for CUDA.")
    print("Falling back to NumPy for demonstration.")
    x_cpu = np.arange(10, dtype=np.float32).reshape(2, 5)
    print(f"CPU array:\n{x_cpu}")

view raw JSON →