CuPy (CUDA 12.x)

14.0.1 · active · verified Thu Apr 09

CuPy is a NumPy/SciPy-compatible array library for GPU-accelerated computing with Python, utilizing NVIDIA CUDA or AMD ROCm platforms. It provides an `ndarray` and a rich set of routines with an API designed to be a drop-in replacement for NumPy and SciPy. The current version, 14.0.1, is a stable release, with the project generally following a bi-monthly release cadence for stable versions and major updates occurring less frequently.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates basic CuPy array creation, GPU computation, and data transfer between the GPU and CPU. It also includes a check for GPU availability and emphasizes `cp.cuda.Stream.null.synchronize()` for accurate benchmarking, as GPU operations are often asynchronous.

import cupy as cp
import numpy as np

# Check if a GPU is available
if cp.cuda.is_available():
    print("GPU 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("CuPy array on GPU:\n", x_gpu)

    # Perform an operation on the GPU
    y_gpu = x_gpu * 2 + 1
    print("Result of GPU operation:\n", y_gpu)

    # Transfer the result back to CPU (NumPy array)
    y_cpu = cp.asnumpy(y_gpu)
    print("Result on CPU (NumPy array):\n", y_cpu)

    # Example: Dot product
    a_gpu = cp.array([[1, 2], [3, 4]], dtype=cp.float32)
    b_gpu = cp.array([[5, 6], [7, 8]], dtype=cp.float32)
    c_gpu = a_gpu @ b_gpu
    print("\nMatrix multiplication on GPU:\n", c_gpu)

    # For accurate performance timings, ensure GPU operations complete
    cp.cuda.Stream.null.synchronize()
else:
    print("No GPU available. CuPy will not be functional.")

view raw JSON →