Skip to main content

The setuptools-cpp-cuda is a module that extends setuptools functionality for building hybrid C++ and CUDA extensions

Project description

Setuptools C++ CUDA

PyPI - Version PyPI - Python Version Gitlab - Dependencies Gitlab - Issues Gitlab - Last commit

The setuptools-cpp-cuda is a module that extends setuptools functionality for building hybrid C++ and CUDA extensions for Python wrapper modules.


Table of Contents

Summary

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+).

Features

  • Python version >= 3.6 .
  • SM(StreamingMultiprocessor)/Compute architecture >= 3.0 .
  • Cython compatible but not mandatory.
  • Any CUDA version (since you can configure nvcc flags).
  • Preloaded flags for cpp and CUDA compilers.
  • Mixed compilations (.cpp and .cu files can be included in a single extension).
  • Advanced find_cuda features (automatically try to find the CUDAHOME directory).
  • Include NVIDIA Management Library (NVML) capabilities info.

Installation

pip install setuptools-cpp-cuda

Usage

Add the library to your project configuration files ("pyproject.toml" and/or "setup.py/.cfg").

1. Example for "legacy build" (old python versions with setuptools < 61.0.0):

setup.py

from pathlib import Path
from setuptools import setup
from setuptools_cpp_cuda 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.

2. Example for "pyproject.toml build" (with setuptools >= 61.0.0):

pyproject.toml

[build-system]
requires = ["setuptools-cpp-cuda", "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:

setup.py

from pathlib import Path
from setuptools import setup
from setuptools_cpp_cuda 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},
)

Issues

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

License

setuptools-cpp-cuda is distributed under the terms of the MIT license.

Acknowledgements

The package is based on cpp_extension, but it also includes:

  • Support for deprecated older architectures (from sm / compute 3.0).
  • Improved find_cuda system.
  • Pathlib library and Windows missing dll support.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

setuptools-cpp-cuda-0.1.6.tar.gz (23.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

setuptools_cpp_cuda-0.1.6-py3-none-any.whl (18.3 kB view details)

Uploaded Python 3

File details

Details for the file setuptools-cpp-cuda-0.1.6.tar.gz.

File metadata

  • Download URL: setuptools-cpp-cuda-0.1.6.tar.gz
  • Upload date:
  • Size: 23.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-requests/2.27.1

File hashes

Hashes for setuptools-cpp-cuda-0.1.6.tar.gz
Algorithm Hash digest
SHA256 1582f9473ac857a95408385d78eba86058b02c95dd9a6a6bbe9b7982a833934c
MD5 2f63be77a9d4b6d1d4745f0254c862aa
BLAKE2b-256 5a7f82ddf750fb09345ebd7547420cee3cee2a640457b0b2836d00ab792b5a6c

See more details on using hashes here.

File details

Details for the file setuptools_cpp_cuda-0.1.6-py3-none-any.whl.

File metadata

File hashes

Hashes for setuptools_cpp_cuda-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 fd270a4dc4632a49d65acb35c16e52d4c5128331329d9b6fae29b9e77372dd5c
MD5 268063544d131fe9ea97f7387a1cc839
BLAKE2b-256 3a1051760d5b3a2164b6399150298ca75a5a9d75b4e17c8248ce1b5ec20343d1

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page