-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathsetup.py
425 lines (373 loc) · 12 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
# from future.utils import iteritems
import os
import sys
from os.path import join as pjoin
from setuptools import setup
from distutils.extension import Extension
from Cython.Distutils import build_ext
import numpy
import argparse
def find_in_path(name, path):
"""Find a file in a search path"""
# Adapted fom http://code.activestate.com/recipes/52224
for dir in path.split(os.pathsep):
binpath = pjoin(dir, name)
if os.path.exists(binpath):
return os.path.abspath(binpath)
return None
def locate_cuda():
"""Locate the CUDA environment on the system
Returns a dict with keys 'home', 'nvcc', 'include', and 'lib64'
and values giving the absolute path to each directory.
Starts by looking for the CUDAHOME env variable. If not found,
everything is based on finding 'nvcc' in the PATH.
"""
# First check if the CUDAHOME env variable is in use
if "CUDAHOME" in os.environ:
home = os.environ["CUDAHOME"]
nvcc = pjoin(home, "bin", "nvcc")
elif "CUDA_HOME" in os.environ:
home = os.environ["CUDA_HOME"]
nvcc = pjoin(home, "bin", "nvcc")
else:
# Otherwise, search the PATH for NVCC
nvcc = find_in_path("nvcc", os.environ["PATH"])
if nvcc is None:
raise EnvironmentError(
"The nvcc binary could not be "
"located in your $PATH. Either add it to your path, "
"or set $CUDAHOME"
)
home = os.path.dirname(os.path.dirname(nvcc))
cudaconfig = {
"home": home,
"nvcc": nvcc,
"include": pjoin(home, "include"),
"lib64": pjoin(home, "lib64"),
}
for k, v in iter(cudaconfig.items()):
if not os.path.exists(v):
raise EnvironmentError(
"The CUDA %s path could not be " "located in %s" % (k, v)
)
return cudaconfig
def customize_compiler_for_nvcc(self):
# track all the object files generated with cuda device code
self.cuda_object_files = []
# Tell the compiler it can processes .cu
self.src_extensions.append(".cu")
# Save references to the default compiler_so and _comple methods
default_compiler_so = self.compiler_so
super = self._compile
def _compile(obj, src, ext, cc_args, extra_postargs, pp_opts):
# generate a special object file that will contain linked in
# relocatable device code
if src == "zzzzzzzzzzzzzzzz.cu":
self.set_executable("compiler_so", CUDA["nvcc"])
postargs = extra_postargs["nvcclink"]
cc_args = self.cuda_object_files[1:]
src = self.cuda_object_files[0]
elif os.path.splitext(src)[1] == ".cu":
self.set_executable("compiler_so", CUDA["nvcc"])
postargs = extra_postargs["nvcc"]
self.cuda_object_files.append(obj)
else:
postargs = extra_postargs["gcc"]
super(obj, src, ext, cc_args, postargs, pp_opts)
self.compiler_so = default_compiler_so
self._compile = _compile
# Run the customize_compiler
class custom_build_ext(build_ext):
def build_extensions(self):
customize_compiler_for_nvcc(self.compiler)
build_ext.build_extensions(self)
try:
CUDA = locate_cuda()
run_cuda_install = True
except OSError:
run_cuda_install = False
parser = argparse.ArgumentParser()
parser.add_argument(
"--lapack_lib",
help="Directory of the lapack lib.",
default="/usr/local/opt/lapack/lib",
)
parser.add_argument(
"--lapack_include",
help="Directory of the lapack include.",
default="/usr/local/opt/lapack/include",
)
parser.add_argument(
"--lapack",
help="Directory of both lapack lib and include. '/include' and '/lib' will be added to the end of this string.",
)
parser.add_argument(
"--gsl_lib", help="Directory of the gsl lib.", default="/usr/local/opt/gsl/lib"
)
parser.add_argument(
"--gsl_include",
help="Directory of the gsl include.",
default="/usr/local/opt/gsl/include",
)
parser.add_argument(
"--gsl",
help="Directory of both gsl lib and include. '/include' and '/lib' will be added to the end of this string.",
)
args, unknown = parser.parse_known_args()
for key in [
args.gsl_include,
args.gsl_lib,
args.gsl,
"--gsl",
"--gsl_include",
"--gsl_lib",
args.lapack_include,
args.lapack_lib,
args.lapack,
"--lapack",
"--lapack_lib",
"--lapack_include",
]:
try:
sys.argv.remove(key)
except ValueError:
pass
# Obtain the numpy include directory. This logic works across numpy versions.
try:
numpy_include = numpy.get_include()
except AttributeError:
numpy_include = numpy.get_numpy_include()
if args.lapack is None:
lapack_include = [args.lapack_include]
lapack_lib = [args.lapack_lib]
else:
lapack_include = [args.lapack + "/include"]
lapack_lib = [args.lapack + "/lib"]
if args.gsl is None:
gsl_include = [args.gsl_include]
gsl_lib = [args.gsl_lib]
else:
gsl_include = [args.gsl + "/include"]
gsl_lib = [args.gsl + "/lib"]
import lisatools
path_to_lisatools = lisatools.__file__.split("__init__.py")[0]
path_to_lisatools_cutils = path_to_lisatools + "cutils/"
# try:
# exec(open("scripts/prebuild.py", "r").read())
# except FileNotFoundError:
# import warnings
# warnings.warn(
# "Trying to executre prebuild.py inside setup script, but getting FileNotFoundError. Assuming user will run scripts/prebuild.py manually."
# )
# if installing for CUDA, build Cython extensions for gpu modules
if run_cuda_install:
gpu_extension = dict(
libraries=["cudart", "cublas", "cusparse", "gsl", "gslcblas"],
library_dirs=[CUDA["lib64"]] + gsl_lib,
runtime_library_dirs=[CUDA["lib64"]],
language="c++",
# This syntax is specific to this build system
# we're only going to use certain compiler args with nvcc
# and not with gcc the implementation of this trick is in
# customize_compiler()
extra_compile_args={
"gcc": ["-std=c++11"], # '-g'],
"nvcc": [
"-arch=sm_80",
# "-gencode=arch=compute_50,code=sm_50",
# "-gencode=arch=compute_52,code=sm_52",
"-gencode=arch=compute_60,code=sm_60",
"-gencode=arch=compute_61,code=sm_61",
"-gencode=arch=compute_70,code=sm_70",
"-gencode=arch=compute_75,code=sm_75",
"-gencode=arch=compute_80,code=compute_80",
"-std=c++11",
"-c",
"--compiler-options",
"'-fPIC'",
# "-G",
# "-g",
# "-O0",
# "-lineinfo",
], # for debugging
},
include_dirs=[
numpy_include,
CUDA["include"],
"bbhx/cutils/include",
"/usr/include",
],
)
pyPhenomHM_ext = Extension(
"bbhx.cutils.pyPhenomHM",
sources=["bbhx/cutils/src/PhenomHM.cu", "bbhx/cutils/src/phenomhm.pyx"],
**gpu_extension,
)
pyFDResponse_ext = Extension(
"bbhx.cutils.pyFDResponse",
sources=[
path_to_lisatools_cutils + "src/Detector.cu",
"bbhx/cutils/src/Response.cu",
"bbhx/cutils/src/response.pyx",
"zzzzzzzzzzzzzzzz.cu",
],
libraries=["cudart", "cudadevrt", "cublas", "cusparse"],
library_dirs=[CUDA["lib64"]],
runtime_library_dirs=[CUDA["lib64"]],
language="c++",
# This syntax is specific to this build system
# we're only going to use certain compiler args with nvcc
# and not with gcc the implementation of this trick is in
# customize_compiler()
extra_compile_args={
"gcc": ["-std=c++11"],
"nvcc": ["-arch=sm_80", "-rdc=true", "--compiler-options", "'-fPIC'"],
"nvcclink": [
"-arch=sm_80",
"--device-link",
"--compiler-options",
"'-fPIC'",
],
},
include_dirs=[
numpy_include,
CUDA["include"],
"bbhx/cutils/include",
path_to_lisatools_cutils + "include",
"/usr/include",
],
)
pyInterpolate_ext = Extension(
"bbhx.cutils.pyInterpolate",
sources=["bbhx/cutils/src/Interpolate.cu", "bbhx/cutils/src/interpolate.pyx"],
**gpu_extension,
)
pyWaveformBuild_ext = Extension(
"bbhx.cutils.pyWaveformBuild",
sources=[
"bbhx/cutils/src/WaveformBuild.cu",
"bbhx/cutils/src/waveformbuild.pyx",
],
**gpu_extension,
)
pyLikelihood_ext = Extension(
"bbhx.cutils.pyLikelihood",
sources=["bbhx/cutils/src/Likelihood.cu", "bbhx/cutils/src/likelihood.pyx"],
**gpu_extension,
)
# gpu_extensions.append(Extension(extension_name, **temp_dict))
cpu_extension = dict(
libraries=["lapacke", "lapack", "gsl", "gslcblas"],
language="c++",
# This syntax is specific to this build system
# we're only going to use certain compiler args with nvcc
# and not with gcc the implementation of this trick is in
# customize_compiler()
extra_compile_args={
"gcc": ["-std=c++11"],
}, # '-g'],
include_dirs=[
numpy_include,
"bbhx/cutils/include",
path_to_lisatools_cutils + "include",
"/usr/include",
],
)
pyPhenomHM_cpu_ext = Extension(
"bbhx.cutils.pyPhenomHM_cpu",
sources=["bbhx/cutils/src/PhenomHM.cpp", "bbhx/cutils/src/phenomhm_cpu.pyx"],
**cpu_extension,
)
pyFDResponse_cpu_ext = Extension(
"bbhx.cutils.pyFDResponse_cpu",
sources=[
path_to_lisatools_cutils + "src/Detector.cpp",
"bbhx/cutils/src/Response.cpp",
"bbhx/cutils/src/response_cpu.pyx",
],
**cpu_extension,
)
pyInterpolate_cpu_ext = Extension(
"bbhx.cutils.pyInterpolate_cpu",
sources=["bbhx/cutils/src/Interpolate.cpp", "bbhx/cutils/src/interpolate_cpu.pyx"],
**cpu_extension,
)
pyWaveformBuild_cpu_ext = Extension(
"bbhx.cutils.pyWaveformBuild_cpu",
sources=[
"bbhx/cutils/src/WaveformBuild.cpp",
"bbhx/cutils/src/waveformbuild_cpu.pyx",
],
**cpu_extension,
)
pyLikelihood_cpu_ext = Extension(
"bbhx.cutils.pyLikelihood_cpu",
sources=["bbhx/cutils/src/Likelihood.cpp", "bbhx/cutils/src/likelihood_cpu.pyx"],
**cpu_extension,
)
extensions = [
pyPhenomHM_cpu_ext,
pyFDResponse_cpu_ext,
pyInterpolate_cpu_ext,
pyWaveformBuild_cpu_ext,
pyLikelihood_cpu_ext,
]
if run_cuda_install:
extensions = [
pyPhenomHM_ext,
pyFDResponse_ext,
pyInterpolate_ext,
pyWaveformBuild_ext,
pyLikelihood_ext,
] + extensions
setup(
name="bbhx",
author="Michael Katz",
author_email="[email protected]",
ext_modules=extensions,
packages=[
"bbhx",
"bbhx.utils",
"bbhx.waveforms",
"bbhx.response",
"bbhx.cutils",
"bbhx.cutils.src",
"bbhx.cutils.include",
],
# Inject our custom trigger
cmdclass={"build_ext": custom_build_ext},
# Since the package has c code, the egg cannot be zipped
zip_safe=False,
version="1.1.11",
python_requires=">=3.6",
package_data={
"bbhx.cutils.src": [
"Interpolate.cu",
"Interpolate.cpp",
"Likelihood.cu",
"Likelihood.cpp",
"PhenomHM.cu",
"PhenomHM.cpp",
"pycppdetector.pyx",
"Response.cu",
"Response.cpp",
"WaveformBuild.cu",
"WaveformBuild.cpp",
"interpolate.pyx",
"likelihood.pyx",
"phenomhm.pyx",
"response.pyx",
"waveformbuild.pyx",
],
"bbhx.cutils.include": [
"Interpolate.hh",
"Likelihood.hh",
"PhenomHM.hh",
"Response.hh",
"WaveformBuild.hh",
"constants.h",
"cuda_complex.hpp",
"global.h",
],
},
)