CUDA Python Bindings

13.2.0 · active · verified Sun Mar 29

cuda-bindings provides low-level Python wrappers for the NVIDIA CUDA C driver and runtime APIs. It is a core component of the broader NVIDIA 'CUDA Python' initiative, aiming to unify and simplify GPU-accelerated computing in Python. The current version is 13.2.0, with releases often tied to CUDA Toolkit versions and ongoing development to integrate Python as a first-class language in the CUDA ecosystem.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the CUDA driver, query the number of available CUDA devices, and print basic information for each, such as its name and total memory. It leverages the low-level CUDA C APIs exposed by `cuda-bindings`.

import cuda.cuda as cu
import cuda.cuda.runtime as rt
import ctypes # For C types like c_int, c_size_t

# Initialize CUDA Driver API
cu.cuInit(0)

# Get device count
count = ctypes.c_int()
cu.cuDeviceGetCount(ctypes.byref(count))
print(f"Found {count.value} CUDA devices.")

# Get properties for each device
for i in range(count.value):
    device = cu.CUdevice()
    cu.cuDeviceGet(ctypes.byref(device), i)

    name_buffer = ctypes.create_string_buffer(256)
    cu.cuDeviceGetName(name_buffer, len(name_buffer), device)
    print(f"  Device {i}: {name_buffer.value.decode().strip()}")

    total_mem = ctypes.c_size_t()
    cu.cuDeviceTotalMem(ctypes.byref(total_mem), device)
    print(f"    Total Memory: {total_mem.value / (1024**3):.2f} GB")

view raw JSON →