-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathsetup.py
More file actions
190 lines (170 loc) · 6.34 KB
/
setup.py
File metadata and controls
190 lines (170 loc) · 6.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import os
import platform
from setuptools import setup
from setuptools.extension import Extension
from setuptools.command.build_ext import build_ext
from distutils.sysconfig import get_python_inc, get_python_lib
import pybind11
import numpy as np
is_macos = platform.system() == "Darwin"
is_linux = platform.system() == "Linux"
is_nvcc_available = os.system("which nvcc > /dev/null 2>&1") == 0
enable_openmp = not is_macos
enable_cuda = is_linux and is_nvcc_available
class BuildExt(build_ext):
def build_extensions(self):
self.compiler.src_extensions.append(".cu")
for ext in self.extensions:
if any(source.endswith(".cu") for source in ext.sources):
if is_nvcc_available:
self.build_cuda_extension(ext)
else:
self.build_gcc_extension(ext)
else:
super().build_extension(ext)
def build_cuda_extension(self, ext):
# Compile CUDA source files
for source in ext.sources:
if source.endswith(".cu"):
self.compile_cuda(source)
# Compile non-CUDA source files
objects = []
for source in ext.sources:
if not source.endswith(".cu"):
obj = self.compiler.compile(
[source],
output_dir=self.build_temp,
extra_postargs=[
"-fPIC",
"-std=c++17",
"-fdiagnostics-color=always",
],
)
objects.extend(obj)
# Link all object files
self.compiler.link_shared_object(
objects + [os.path.join(self.build_temp, "starter_kit.o")],
self.get_ext_fullpath(ext.name),
libraries=ext.libraries,
library_dirs=ext.library_dirs,
runtime_library_dirs=ext.runtime_library_dirs,
extra_postargs=ext.extra_link_args,
target_lang=ext.language,
)
def build_gcc_extension(self, ext):
# Check if compiling on macOS
objects = []
for source in ext.sources:
if source.endswith(".cu"):
obj = self.compiler.compile(
[source],
output_dir=self.build_temp,
extra_preargs=["-x", "c++"],
extra_postargs=[
"-fPIC",
"-std=c++17",
"-fdiagnostics-color=always",
]
+ (["-fopenmp"] if enable_openmp else []),
include_dirs=ext.include_dirs,
)
else:
obj = self.compiler.compile(
[source],
output_dir=self.build_temp,
extra_postargs=[
"-fPIC",
"-std=c++17",
"-fdiagnostics-color=always",
]
+ (["-fopenmp"] if enable_openmp else []),
include_dirs=ext.include_dirs,
)
objects.extend(obj)
# Link all object files
self.compiler.link_shared_object(
objects,
self.get_ext_fullpath(ext.name),
libraries=[lib for lib in ext.libraries if not lib.startswith("cu")],
library_dirs=ext.library_dirs,
runtime_library_dirs=ext.runtime_library_dirs,
extra_postargs=ext.extra_link_args,
target_lang=ext.language,
)
def compile_cuda(self, source):
# Compile CUDA source file using NVCC
ext = self.extensions[0]
output_dir = self.build_temp
os.makedirs(output_dir, exist_ok=True)
include_dirs = self.compiler.include_dirs + ext.include_dirs
include_dirs = " ".join(f"-I{dir}" for dir in include_dirs)
output_file = os.path.join(output_dir, "starter_kit.o")
# Let's try inferring the compute capability from the GPU
arch_code = "90"
try:
import pycuda.driver as cuda
import pycuda.autoinit
device = cuda.Device(0) # Get the default device
major, minor = device.compute_capability()
arch_code = f"{major}{minor}"
except ImportError:
pass
cmd = (
f"nvcc -c {source} -o {output_file} -std=c++17 "
f"-gencode=arch=compute_{arch_code},code=sm_{arch_code} "
f"-Xcompiler -fPIC {include_dirs} -O3 -g"
)
if os.system(cmd) != 0:
raise RuntimeError(f"nvcc compilation of {source} failed")
__version__ = open("VERSION", "r").read().strip()
long_description = ""
this_directory = os.path.abspath(os.path.dirname(__file__))
with open(os.path.join(this_directory, "README.md"), "r", encoding="utf-8") as f:
long_description = f.read()
# Get Python library path dynamically
python_lib_dir = get_python_lib(standard_lib=True)
python_lib_name = os.path.basename(python_lib_dir).replace(".so", "")
ext_modules = [
Extension(
"starter_kit",
["starter_kit.cu"],
include_dirs=[
pybind11.get_include(),
np.get_include(),
get_python_inc(),
"cccl/cub/",
"cccl/libcudacxx/include",
"cccl/thrust/",
"/usr/local/cuda/include/",
"/usr/include/cuda/",
],
library_dirs=[
"/usr/local/cuda/lib64",
"/usr/lib/x86_64-linux-gnu",
"/usr/lib/wsl/lib",
python_lib_dir,
],
#
libraries=[python_lib_name.replace(".a", "")]
+ (["cudart", "cuda", "cublas"] if enable_cuda else [])
+ (["gomp"] if enable_openmp else []),
#
extra_link_args=[f"-Wl,-rpath,{python_lib_dir}"]
+ (["-fopenmp"] if enable_openmp else []),
language="c++",
),
]
setup(
name="PyBindToGPUs",
version=__version__,
author="Ash Vardanian",
author_email="1983160+ashvardanian@users.noreply.github.com",
url="https://github.com/ashvardanian/PyBindToGPUs",
description="Starter Kit project for CUDA- and OpenMP-accelerated Python projects.",
long_description=long_description,
ext_modules=ext_modules,
extras_require={"test": "pytest"},
cmdclass={"build_ext": BuildExt},
zip_safe=False,
python_requires=">=3.7",
)