-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.py
275 lines (212 loc) · 9.88 KB
/
setup.py
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# -*- coding: utf-8 -*.t-
import os
from setuptools import find_packages, setup, Extension
from Cython.Distutils import build_ext as orig_build_ext
from Cython.Build import cythonize
from distutils.command.build_clib import build_clib
from distutils.dep_util import newer_group
import distutils.dist
from distutils import log
import distutils.command.build
distutils.dist.Distribution.cuda_sources = None
distutils.dist.Distribution.cuda_gpu_arch = None
def has_nvcc(self):
return self.distribution.cuda_sources
distutils.command.build.build.sub_commands.insert(0, ('build_nvcc', has_nvcc))
orig_build_ext.sub_commands.insert(0, ('build_nvcc', has_nvcc))
class build_nvcc(build_clib):
def run(self):
return build_clib.run(self)
def finalize_options(self):
# almost verbatim copy of the build_clib.finalize_options()
self.set_undefined_options('build',
('build_temp', 'build_clib'),
('build_temp', 'build_temp'),
('compiler', 'compiler'),
('debug', 'debug'),
('force', 'force'))
# commended out from build_clib.finalize_options()
# self.libraries = self.distribution.libraries
# if self.libraries:
# self.check_library_list(self.libraries)
if self.include_dirs is None:
self.include_dirs = self.distribution.include_dirs or []
if isinstance(self.include_dirs, str):
self.include_dirs = self.include_dirs.split(os.pathsep)
self.libraries = self.distribution.cuda_sources
def build_libraries(self, libraries):
self.compiler.src_extensions.append('.cu')
arch = self.distribution.cuda_gpu_arch
arch_arg = ["-arch=%s" % arch] if arch else []
# args = ["nvcc", "--device-c", "--ptxas-options=-v", "-c",
args = ["nvcc", "--device-c", "-c",
"--compiler-options", ','.join(['"%s"'%s if ('-Wp' in s) or ('-Wl' in s) else s for s in self.compiler.compiler_so[1:]])] + arch_arg
self.compiler.set_executables(compiler_so=args)
# args = ["nvcc", "--device-c", "--ptxas-options=-v", "-c", "-Xptxas", "-v"
args = ["nvcc", "--device-c", "-c", "-Xptxas", "-v",
"--compiler-options", ','.join(['"%s"'%s if ('-Wp' in s) or ('-Wl' in s) else s for s in self.compiler.compiler_so[1:]])] + arch_arg
self.compiler.set_executables(compiler_cxx=args)
args = ([
'nvcc', "--lib",
"--compiler-options", ','.join(self.compiler.archiver[1:])] + arch_arg +
["--output-file"]) # --output-file should be last one
self.compiler.set_executables(archiver=args)
sources = self.get_source_files()
deps = sum([lib[1].get('depends', []) for lib in self.libraries], [])
if not self.force:
for name in self.get_library_names():
target = self.compiler.library_filename(
name, output_dir=self.build_clib)
if newer_group(sources + deps, target, 'newer'):
break
else:
log.debug("skipping nvcc compiler (up-to-date)")
return
return build_clib.build_libraries(self, libraries)
class build_ext(orig_build_ext):
def run(self):
if has_nvcc(self):
# run clib build. Or, clib won't be build with such command as 'pip
# install -e .'
build_clib = self.get_finalized_command('build_nvcc')
build_clib.force = self.force
build_clib.compiler = None # bug in distutils?
build_clib.run()
clibdir = build_clib.build_clib
self.libraries.extend(build_clib.get_library_names() or [])
self.library_dirs.append(clibdir)
deps = [
build_clib.compiler.library_filename(name, output_dir=clibdir)
for name in build_clib.get_library_names()]
for ext in self.extensions:
ext.depends += deps
orig_build_ext.run(self)
def build_extensions(self):
arch = self.distribution.cuda_gpu_arch
arch_arg = ["-arch=%s" % arch] if arch else []
self.compiler.set_executables(linker_so=[
'nvcc', "--compiler-options", "-fPIC,-shared,-pthread"] + arch_arg)
# distutils uses compiler_cxx[0] for linker if language is c++.
self.compiler.set_executables(compiler_cxx=[
'nvcc', "--compiler-options", "-fPIC,-shared,-pthread"] + arch_arg)
orig_build_ext.build_extensions(self)
requires = [
"numpy", "scikit-image", "scikit-learn", "Cython>=0.24.0", "Pillow", "future"
]
IN_CI = False
if os.environ.get('CI', None):
IN_CI = True # In gitlab CI
ext_modules = []
cuda_sources = []
cuda_depends = ['renom/cuda/thrust/thrust_func_defs.pxi',
'renom/cuda/thrust/thrust_funcs.pxi',
'renom/cuda/thrust/thrust_funcs.h'
]
def setup_cuda():
cuda_home = os.environ.get("CUDA_HOME", "/usr/local/cuda")
if not cuda_home or not os.path.exists(cuda_home):
# CUDA is not installed
print('Failed to detect cuda.')
return
libraries = [
os.path.join(cuda_home, "lib64")
]
includes = [
os.path.join(cuda_home, "include"),
]
ext_base = Extension('renom.cuda.base.cuda_base',
sources=['renom/cuda/base/cuda_base.pyx'],
depends=cuda_depends,
libraries=['cublas', 'cuda', 'cudart', 'nvToolsExt'],
library_dirs=libraries,
language='c++',
include_dirs=includes,
)
ext_utils = Extension('renom.cuda.base.cuda_utils',
sources=['renom/cuda/base/cuda_utils.pyx'],
depends=cuda_depends,
libraries=['cublas', 'cuda', 'cudart'],
library_dirs=libraries,
language='c++',
include_dirs=includes,
)
ext_cublas = Extension('renom.cuda.cublas.cublas',
sources=['renom/cuda/cublas/cublas.pyx'],
depends=cuda_depends,
libraries=['cublas', 'cuda', 'cudart'],
library_dirs=libraries,
language='c++',
include_dirs=includes,
)
ext_cudnn = Extension('renom.cuda.cudnn.cudnn',
sources=['renom/cuda/cudnn/cudnn.pyx'],
depends=cuda_depends,
libraries=['cublas', 'cuda', 'cudart', 'cudnn'],
library_dirs=libraries,
language='c++',
include_dirs=includes,
)
ext_curand = Extension('renom.cuda.curand.curand',
sources=['renom/cuda/curand/curand.pyx'],
depends=cuda_depends,
libraries=['curand', 'cuda', 'cudart'],
library_dirs=libraries,
language='c++',
include_dirs=includes,
)
ext_thrust_float = Extension('renom.cuda.thrust.thrust_float',
sources=['renom/cuda/thrust/thrust_float.pyx'],
depends=cuda_depends,
libraries=['cublas', 'cuda', 'cudart'],
library_dirs=libraries,
language='c++',
include_dirs=includes,
)
ext_thrust_double = Extension('renom.cuda.thrust.thrust_double',
sources=['renom/cuda/thrust/thrust_double.pyx'],
depends=cuda_depends,
libraries=['cublas', 'cuda', 'cudart'],
library_dirs=libraries,
language='c++',
include_dirs=includes,
)
ext_gpuvalue = Extension('renom.cuda.gpuvalue.gpuvalue',
sources=['renom/cuda/gpuvalue/gpuvalue.py'],
depends=cuda_depends,
libraries=['cublas', 'cuda', 'cudart'],
library_dirs=libraries,
language='c++',
include_dirs=includes,
)
global ext_modules, cuda_sources
ext_modules = [ext_base,
ext_utils,
ext_cublas,
ext_cudnn,
ext_curand,
ext_thrust_float,
ext_thrust_double,
ext_gpuvalue,
]
cuda_sources = [('cuda_misc_a',
{'sources': [
'renom/cuda/thrust/thrust_funcs_float.cu',
'renom/cuda/thrust/thrust_funcs_double.cu', ],
'depends': [
'renom/cuda/thrust/thrust_funcs_double.h',
'renom/cuda/thrust/thrust_funcs_float.h',
'renom/cuda/thrust/thrust_funcs.inl'],
})]
setup_cuda()
os.chdir(os.path.normpath(os.path.join(os.path.abspath(__file__), os.pardir)))
import numpy
setup(
install_requires=requires,
cuda_sources=cuda_sources,
cuda_gpu_arch='sm_30',
ext_modules=ext_modules,
cmdclass={'build_ext': build_ext, 'build_nvcc': build_nvcc},
name='renom',
packages=find_packages(),
include_dirs=[numpy.get_include()],
version='2.7.4')