NVIDIA cuRAND
The `nvidia-curand` library provides the native CURAND shared libraries, making them discoverable via `pip` for Python packages that utilize GPU-accelerated random number generation. It is not intended for direct Python API calls but serves as a crucial dependency for libraries like CuPy and Numba. The current version is 10.4.2.51, and its releases are typically tied to updates of the NVIDIA CUDA Toolkit.
Warnings
- gotcha The `nvidia-curand` package primarily provides native shared libraries and is not designed for direct Python API calls or symbol imports. Python users typically interact with cuRAND through higher-level libraries like CuPy or Numba.
- gotcha `nvidia-curand` requires an NVIDIA GPU and compatible NVIDIA drivers to be installed on your system. Without these, the native libraries cannot be utilized, leading to runtime errors in dependent packages.
- breaking Incompatibility between the installed `nvidia-curand` version, your system's CUDA Toolkit, and your GPU drivers can lead to runtime failures (`CUDARuntimeError`). Versioning is critical for NVIDIA's CUDA ecosystem.
- gotcha While `pip install nvidia-curand` makes the native libraries available, you might still need to configure environment variables (e.g., `LD_LIBRARY_PATH` on Linux) in certain complex setups or if other CUDA installations interfere, to ensure dependent libraries find the correct `cuRAND` shared objects.
Install
-
pip install nvidia-curand
Imports
- None (native library)
This package is not typically imported directly for Python symbols.
Quickstart
import cupy as cp
import os
# This quickstart demonstrates how CuPy, which uses cuRAND (provided by nvidia-curand),
# generates random numbers on the GPU. Ensure an NVIDIA GPU and drivers are installed.
# Optional: Set CUDA_VISIBLE_DEVICES if you have multiple GPUs and want to specify one
# os.environ['CUDA_VISIBLE_DEVICES'] = '0'
try:
# Generate 5 random floats on the GPU using CuPy
gpu_random_numbers = cp.random.rand(5)
print("GPU Random Numbers (CuPy using cuRAND):", gpu_random_numbers)
# Verify the device
print(f"Generated on device: {gpu_random_numbers.device}")
# Perform a basic GPU calculation with another random array
gpu_array_a = cp.array([10.0, 20.0, 30.0], dtype=cp.float32)
gpu_array_b = cp.random.rand(3, dtype=cp.float32) # Uses cuRAND
gpu_result = gpu_array_a * gpu_array_b
print("GPU Array A:", gpu_array_a)
print("GPU Array B (random):", gpu_array_b)
print("GPU Result (A * B):", gpu_result)
except cp.cuda.runtime.CUDARuntimeError as e:
print(f"CUDA Error encountered: {e}")
print("Please ensure you have an NVIDIA GPU, compatible drivers, and CUDA toolkit installed.")
print("Also, verify that CuPy is installed and configured correctly.")
except Exception as e:
print(f"An unexpected error occurred: {e}")