-
Notifications
You must be signed in to change notification settings - Fork 6
/
setup.py
223 lines (173 loc) · 6.2 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
import os
from subprocess import check_output
from glob import glob
from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext
from Cython.Build import cythonize
import numpy
def main():
env_vars = ['PETSC_DIR', 'PETSC_ARCH', 'SLEPC_DIR']
if any(e not in os.environ for e in env_vars):
raise RuntimeError('Must set environment variables PETSC_DIR, '
'PETSC_ARCH and SLEPC_DIR before installing!')
write_build_headers()
setup(
ext_modules=cythonize(
extensions(), include_path=get_cython_includes()
),
cmdclass={'build_ext': MakeBuildExt},
package_dir={'': 'src'}
)
def get_cython_includes():
# the following two replace petsc4py.get_include() and
# slepc4py.get_include(), so we don't need those two installed
# during the build process
return [
os.path.join(
os.environ['PETSC_DIR'],
'src/binding/petsc4py/src'
),
os.path.join(
os.environ['SLEPC_DIR'],
'src/binding/slepc4py/src'
)
]
def write_build_headers():
'''
Write a Cython include file with some constants that become
hardcoded into the backend build.
'''
print('Writing header files...')
commit = check_output(['git', 'describe', '--always'],
cwd=os.path.dirname(os.path.realpath(__file__)),
universal_newlines=True).strip()
branch = check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD'],
cwd=os.path.dirname(os.path.realpath(__file__)),
universal_newlines=True).strip()
version = open('VERSION').read().strip()
header_path = os.path.join(
os.path.dirname(__file__),
'src/dynamite/_backend/config.pxi'
)
with open(header_path, 'w') as f:
f.write('USE_CUDA = %d\n' % int(check_cuda()))
f.write('DNM_BRANCH = "%s"\n' % branch)
f.write('DNM_COMMIT = "%s"\n' % commit)
f.write('DNM_VERSION = "%s"\n' % version)
def extensions():
extension_names = [
'bsubspace',
'bbuild',
'bpetsc'
]
header_only = {
'bsubspace',
}
cython_only = {
'bbuild',
}
exts = []
for name in extension_names:
depends = []
object_files = []
extra_compile_args = []
petsc_vars = get_petsc_variables()
CC_FLAGS = list(filter(None, petsc_vars["CC_FLAGS"].split(' ')))
CXX_FLAGS = list(filter(None, petsc_vars["CXX_FLAGS"].split(' ')))
if name not in cython_only:
depends += [f'src/dynamite/_backend/{name}_impl.h']
if name not in header_only:
depends += [f'src/dynamite/_backend/{name}_impl.c']
object_files = [f'src/dynamite/_backend/{name}_impl.o']
else:
extra_compile_args += CXX_FLAGS
else:
extra_compile_args += CC_FLAGS
if name == 'bpetsc':
extra_compile_args += CC_FLAGS
depends += ['src/dynamite/_backend/bsubspace.pxd'
'src/dynamite/_backend/bcuda_impl.h',
'src/dynamite/_backend/bcuda_impl.cu',
'src/dynamite/_backend/shellcontext.h',
'src/dynamite/_backend/bsubspace_impl.h']
if check_cuda():
object_files += ['src/dynamite/_backend/bcuda_impl.o']
exts += [
Extension(f'dynamite._backend.{name}',
sources=[f'src/dynamite/_backend/{name}.pyx'],
depends=depends,
extra_objects=object_files,
extra_compile_args=extra_compile_args,
**configure_paths())
]
return exts
def configure_paths():
PETSC_DIR = os.environ['PETSC_DIR']
PETSC_ARCH = os.environ['PETSC_ARCH']
SLEPC_DIR = os.environ['SLEPC_DIR']
includes = []
libs = []
includes += [os.path.join(PETSC_DIR, PETSC_ARCH, 'include'),
os.path.join(PETSC_DIR, 'include')]
libs += [os.path.join(PETSC_DIR, PETSC_ARCH, 'lib')]
includes += [os.path.join(SLEPC_DIR, PETSC_ARCH, 'include'),
os.path.join(SLEPC_DIR, 'include')]
libs += [os.path.join(SLEPC_DIR, PETSC_ARCH, 'lib')]
# python package includes
includes += [numpy.get_include()]
return dict(
include_dirs=includes,
library_dirs=libs,
runtime_library_dirs=libs,
libraries=['petsc', 'slepc']
)
class MakeBuildExt(build_ext):
def run(self):
# first remove any old object files which may
# correspond to a different PETSC_ARCH
for fname in glob('src/dynamite/_backend/*.o'):
os.remove(fname)
# build the object files
make = check_output(['make', 'bpetsc_impl.o'],
cwd='src/dynamite/_backend')
print(make.decode(), end='')
if check_cuda():
make = check_output(['make', 'bcuda_impl.o'],
cwd='src/dynamite/_backend')
print(make.decode(), end='')
petsc_vars = get_petsc_variables()
os.environ['CC'] = petsc_vars['CC']
os.environ['CXX'] = petsc_vars['CXX']
build_ext.run(self)
USE_CUDA = None
def check_cuda():
'''
Whether PETSc was built with CUDA support
'''
global USE_CUDA
if USE_CUDA is not None:
return USE_CUDA
with open(os.path.join(os.environ['PETSC_DIR'],
os.environ['PETSC_ARCH'],
'include/petscconf.h')) as f:
for line in f:
if 'PETSC_HAVE_CUDA 1' in line:
USE_CUDA = True
break
else:
USE_CUDA = False
return USE_CUDA
def get_petsc_variables():
petsc_varfname = os.path.join(
os.environ['PETSC_DIR'],
os.environ['PETSC_ARCH'],
'lib/petsc/conf/petscvariables'
)
petsc_vars = {}
with open(petsc_varfname) as f:
for line in f:
(key, val) = line.rstrip('\r\n').split(" = ")
petsc_vars[key] = val.strip()
return petsc_vars
if __name__ == '__main__':
main()