NVIDIA cuFile Python Wrapper
cufile-python is a basic Python wrapper for the NVIDIA cuFile API, enabling Python programs to leverage GPUDirect Storage for high-performance I/O directly from GPU memory. The current version is 0.2.0. The project appears to be unmaintained, with its last release in 2021, meaning new features or bug fixes are unlikely.
Common errors
-
ImportError: No module named 'cufile'
cause The 'cufile-python' package is not installed or not accessible in your current Python environment.fixRun `pip install cufile-python` to install the package. -
RuntimeError: cuFile driver initialization failed
cause The underlying NVIDIA cuFile C library could not be initialized by the Python wrapper. This almost always indicates that the cuFile system library is not installed, cannot be found in the system's library path, or there's an issue with the GPU setup or drivers.fixVerify your NVIDIA GPU drivers and CUDA Toolkit are correctly installed. Ensure the cuFile library (e.g., `libcufile.so`) is present and discoverable by the system's linker (e.g., by checking `LD_LIBRARY_PATH` or system linker configurations). Consult NVIDIA cuFile documentation for specific installation and environment setup instructions. -
TypeError: expected a bytes-like object, not 'str'
cause You are passing a standard Python `str` object to a `cufile.lib` function (e.g., `open`, `read`, `write`) that expects a `bytes` object for arguments like file paths or data buffers.fixEncode your string to bytes before passing it to the function. For example, change `lib.cuFileDriver.open(filename, os.O_RDWR)` to `lib.cuFileDriver.open(filename.encode('utf-8'), os.O_RDWR)`.
Warnings
- gotcha This library is a Python wrapper for the NVIDIA cuFile C library. It absolutely requires an NVIDIA GPU, a compatible CUDA Toolkit installation, and the cuFile library components to be correctly installed and configured on the system. Installing `cufile-python` via pip alone is insufficient for functionality.
- deprecated The cufile-python project appears to be unmaintained. The last commit and PyPI release were in October 2021. Users should be aware that there is no active development, bug fixes, or official support, which introduces risks for long-term projects or compatibility with newer CUDA/Python versions.
- gotcha Many low-level functions within cufile-python that accept file paths or data buffers expect them to be `bytes` objects, not standard Python `str`. Providing `str` directly will result in a `TypeError`.
Install
-
pip install cufile-python
Imports
- lib
import cufile
import cufile.lib as lib
- cuFileDriver
from cufile import cuFileDriver
from cufile.lib import cuFileDriver
Quickstart
import cufile.lib as lib
import os
# --- Prerequisites: NVIDIA GPU, CUDA Toolkit, and cuFile library must be installed ---
# This example assumes cuFile is correctly set up and discoverable in the system's library path.
# Ensure a dummy file exists for I/O operations
filename = "/tmp/test_cufile.txt"
with open(filename, "w") as f:
f.write("Hello, cuFile GPU-Direct Storage!")
try:
# Initialize cuFile library
lib.cuFileDriver.init()
print("cuFile driver initialized successfully.")
# Open file with cuFile. Note: Filename must be encoded to bytes.
# os.O_RDWR is a standard file access flag.
fd = lib.cuFileDriver.open(filename.encode('utf-8'), os.O_RDWR)
print(f"Opened file '{filename}' with cuFile descriptor: {fd}")
# In a real application, you would now perform GPU-direct I/O operations
# using this file descriptor and GPU memory buffers.
# For this quickstart, we just demonstrate the basic open/close workflow.
# Close file
lib.cuFileDriver.close(fd)
print(f"Closed cuFile descriptor: {fd}")
except RuntimeError as e:
print(f"Error during cuFile operation: {e}\n")
print("HINT: Ensure NVIDIA cuFile library is correctly installed, and your GPU is configured.")
except Exception as e:
print(f"An unexpected error occurred: {e}")
finally:
# Deinitialize cuFile library
try:
lib.cuFileDriver.fini()
print("cuFile driver deinitialized.")
except Exception as e:
# Fini might fail if init failed or was never called successfully
print(f"Warning: Error during cuFile deinitialization: {e}")
# Clean up the dummy file
if os.path.exists(filename):
os.remove(filename)
print(f"Cleaned up temporary file: {filename}")