The setuptools-cuda-cpp is a module that extends setuptools functionality for building hybrid C++ and CUDA extensions for Python wrapper modules.
Table of Contents
This project meant to be a soft solution to include mixed c++/CUDA extensions in your projects, no matter if you are using old python version (3.6+) or old GPU drivers (sm/compute arch 3.0+).
pip install setuptools-cuda-cpp
Add the library to your project configuration files ("pyproject.toml" and/or "setup.py/.cfg").
from pathlib import Path
from setuptools import setup
from setuptools_cuda_cpp import CUDAExtension, BuildExtension, fix_dll
cuda_ext_path = Path('src/my_cuda_package/cuda_ext')
cuda_ext = CUDAExtension(
name='my_cuda_package.cuda_ext',
include_dirs=[cuda_ext_path / 'include'],
sources=[
cuda_ext_path / 'cuda_ext.cu',
cuda_ext_path / 'cuda_ext_wrapper.cpp',
],
libraries=fix_dll(['cudart']), # Use fix_dll() only for Windows compatibility (check documentation for more info).
extra_compile_args={
'cxx': ['-g'], # cpp compiler flags
'nvcc': ['-O2'], # nvcc flags
},
)
setup(
name='my-cuda-package',
version='0.0.1',
install_requires=['numpy', ],
extras_require={'cython': ['cython'], },
ext_modules=[cuda_ext],
cmdclass={'build_ext': BuildExtension},
)
You can also use pyproject.toml with Flit making a custom build-backend.
[build-system]
requires = ["setuptools-cuda-cpp", "flit_core >=3.2,<4", "wheel", "cython"]
build-backend = "flit_core.buildapi"
[project]
name = "my-cuda-package"
dependencies = ["numpy"]
dynamic = ["version", "description"]
# ...
And configure the setup.py for the different extensions you want to use:
from pathlib import Path
from setuptools import setup
from setuptools_cuda_cpp import CUDAExtension, BuildExtension, fix_dll
cuda_ext_path = Path('src/my_cuda_package/cuda_ext')
cuda_ext = CUDAExtension(
name='my_cuda_package.cuda_ext',
include_dirs=[cuda_ext_path / 'include'],
sources=[
cuda_ext_path / 'cuda_ext.cu',
cuda_ext_path / 'cuda_ext_wrapper.cpp',
],
libraries=fix_dll(['cudart']), # Use fix_dll() only for Windows compatibility (check documentation for more info).
extra_compile_args={
'cxx': ['-g'], # cpp compiler flags
'nvcc': ['-O2'], # nvcc flags
},
)
setup(
ext_modules=[cuda_ext],
cmdclass={'build_ext': BuildExtension},
)
If you receive a EnvironmentError exception you should set CUDAHOME environment variable pointing to the CUDA installation path. This would happen if the find_cuda() method is not capable of locate it. As reference the directory should contain:
CUDAHOME
├── bin
│ └── nvcc
├── include
│ └── cudart.h
├── lib
└── nvml
setuptools-cuda-cpp
is distributed under the terms of the MIT license.
The package is based on cpp_extension, but it also includes: