CUDA Pathfinder
cuda-pathfinder is a Python library from NVIDIA that provides utilities for locating CUDA components, such as dynamic libraries (.so, .dll) and CUDA Toolkit (CTK) header directories. It aims to be a one-stop solution for discovering CUDA artifacts within a Python environment. It is currently at version 1.5.0 and is part of the broader NVIDIA CUDA Python ecosystem, undergoing active development with regular releases.
Warnings
- gotcha Do not manually close the `LoadedDL` object's handle. The library internally caches and reuses handles, and closing them (e.g., `dlclose` on Linux, `FreeLibrary` on Windows) can lead to crashes or subtle failures in other parts of the application that might still be using the shared module.
- gotcha `cuda-pathfinder` is CUDA Toolkit (CTK) version-agnostic but adheres to NVIDIA's general CUDA Toolkit support policy, typically supporting the two most recent major versions. Relying on it to find components for very old or extremely new, unsupported CTK versions might lead to unexpected failures.
- gotcha Misconfigured CUDA environment variables (e.g., `LD_LIBRARY_PATH`, `CUDA_HOME`, `PATH`) can cause `cuda-pathfinder` to fail locating components or find incorrect versions, even if the CUDA Toolkit is installed. The tool's discovery mechanism interacts with system environment settings.
- breaking The `cuda-python` project, which `cuda-pathfinder` is a part of, has been restructured into a metapackage with independently versioned subpackages. While `cuda-pathfinder` maintains its own versioning, users previously relying on a monolithic `cuda-python` package might experience changes in installation patterns or implicit dependencies.
Install
-
pip install cuda-pathfinder
Imports
- load_nvidia_dynamic_lib
from cuda.pathfinder import load_nvidia_dynamic_lib
- find_nvidia_header_directory
from cuda.pathfinder import find_nvidia_header_directory
Quickstart
from cuda.pathfinder import load_nvidia_dynamic_lib, DynamicLibNotFoundError
import os
try:
# Locate and load the CUDA Runtime dynamic library (e.g., cudart.so, cudart.dll)
cudart_lib = load_nvidia_dynamic_lib('cudart')
print(f"Successfully loaded CUDA Runtime library: {cudart_lib.path}")
# Example: Accessing its handle (but don't close it!)
# The handle is a low-level OS object, specific usage depends on further operations
# For instance, if you were to interact with a CFFI interface.
print(f"Library handle (platform specific): {cudart_lib.handle_uint}")
# You can also find other components like header directories
# For this example, we're not using os.environ.get as it's not directly for auth.
# No direct quickstart for header directory loading that requires external auth.
# This part is illustrative of another feature.
# try:
# cuda_headers = find_nvidia_header_directory()
# print(f"CUDA header directory found at: {cuda_headers.path}")
# except Exception as e:
# print(f"Could not find CUDA header directory: {e}")
except DynamicLibNotFoundError:
print("CUDA Runtime library 'cudart' not found. Ensure CUDA Toolkit is installed and accessible.")
except Exception as e:
print(f"An unexpected error occurred: {e}")