-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy pathbuild_pymfem.py
More file actions
318 lines (259 loc) · 10.6 KB
/
Copy pathbuild_pymfem.py
File metadata and controls
318 lines (259 loc) · 10.6 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
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
# ----------------------------------------------------------------------------------------
# Routines for PyMFEM Wrapper Generation/Compile
# ----------------------------------------------------------------------------------------
import sys
import os
import re
import subprocess
__all__ = ["write_setup_local", "generate_wrapper",
"clean_wrapper", "make_mfem_wrapper"]
from build_utils import *
from build_consts import *
import build_globals as bglb
def write_setup_local():
'''
create setup_local.py. parameters written here will be read
by setup.py in mfem._ser and mfem._par
'''
mfemser = bglb.mfems_prefix
mfempar = bglb.mfemp_prefix
hyprelibpath = os.path.dirname(
find_libpath_from_prefix('HYPRE', bglb.hypre_prefix))
metislibpath = os.path.dirname(
find_libpath_from_prefix('metis', bglb.metis_prefix))
mfems_tpl = read_mfem_tplflags(bglb.mfems_prefix)
mfemp_tpl = read_mfem_tplflags(
bglb.mfemp_prefix) if bglb.build_parallel else ''
print(mfems_tpl, mfemp_tpl)
params = {'cxx_ser': bglb.cxx_command,
'cc_ser': bglb.cc_command,
'cxx_par': bglb.mpicxx_command,
'cc_par': bglb.mpicc_command,
'whole_archive': '--whole-archive',
'no_whole_archive': '--no-whole-archive',
'nocompactunwind': '',
'swigflag': '-Wall -c++ -python -fastproxy -olddefs -keyword',
'hypreinc': os.path.join(bglb.hypre_prefix, 'include'),
'hyprelib': hyprelibpath,
'metisinc': os.path.join(bglb.metis_prefix, 'include'),
'metis5lib': metislibpath,
'numpyinc': get_numpy_inc(),
'mpi4pyinc': '',
'mpiinc':bglb.mpiinc,
'mfem_outside': '1' if bglb.mfem_outside else '0',
'mfembuilddir': os.path.join(mfempar, 'include'),
'mfemincdir': os.path.join(mfempar, 'include', 'mfem'),
'mfemlnkdir': os.path.join(mfempar, 'lib'),
'mfemserbuilddir': os.path.join(mfemser, 'include'),
'mfemserincdir': os.path.join(mfemser, 'include', 'mfem'),
'mfemserlnkdir': os.path.join(mfemser, 'lib'),
'mfemsrcdir': os.path.join(bglb.mfem_source),
'mfemstpl': mfems_tpl,
'mfemptpl': mfemp_tpl,
'add_pumi': '',
'add_strumpack': '',
'add_cuda': '',
'add_libceed': '',
'add_suitesparse': '',
'add_gslib': '',
'add_gslibp': '',
'add_gslibs': '',
'libceedinc': os.path.join(bglb.libceed_prefix, 'include'),
'gslibsinc': os.path.join(bglb.gslibs_prefix, 'include'),
'gslibpinc': os.path.join(bglb.gslibp_prefix, 'include'),
'cxxstdflag': bglb.cxxstd_flag,
'build_mfem': '1' if bglb.build_mfem else '0',
'bdist_wheel_dir': bglb.bdist_wheel_dir,
}
if bglb.build_parallel:
params['mpi4pyinc'] = get_mpi4py_inc()
def add_extra(xxx, inc_sub=None):
params['add_' + xxx] = '1'
ex_prefix = getattr(bglb, xxx + '_prefix')
if inc_sub is None:
params[xxx +
'inc'] = os.path.join(ex_prefix, 'include')
else:
params[xxx +
'inc'] = os.path.join(ex_prefix, 'include', inc_sub)
params[xxx + 'lib'] = os.path.join(ex_prefix, 'lib')
if bglb.enable_pumi:
add_extra('pumi')
if bglb.enable_strumpack:
add_extra('strumpack')
if bglb.enable_cuda:
add_extra('cuda')
if bglb.enable_libceed:
add_extra('libceed')
if bglb.enable_suitesparse:
add_extra('suitesparse', inc_sub='suitesparse')
if bglb.enable_gslib:
add_extra('gslibs')
if bglb.enable_gslib:
add_extra('gslibp')
pwd = chdir(rootdir)
fid = open('setup_local.py', 'w')
fid.write("# setup_local.py \n")
fid.write("# generated from setup.py\n")
fid.write("# do not edit this directly\n")
for key, value in params.items():
text = key.lower() + ' = "' + value + '"'
fid.write(text + "\n")
fid.close()
os.chdir(pwd)
def generate_wrapper(do_parallel):
'''
run swig.
'''
# this should work as far as we are in the same directory ?
from multiprocessing import Pool, cpu_count
import build_globals as bglb
if bglb.dry_run or bglb.verbose:
print("generating SWIG wrapper")
print("using MFEM source", os.path.abspath(bglb.mfem_source))
if not os.path.exists(os.path.abspath(bglb.mfem_source)):
assert False, "MFEM source directory. Use --mfem-source=<path>"
def ifiles():
ifiles = os.listdir()
ifiles = [x for x in ifiles if x.endswith('.i')]
ifiles = [x for x in ifiles if not x.startswith('#')]
ifiles = [x for x in ifiles if not x.startswith('.')]
return ifiles
def check_new(ifile):
wfile = ifile[:-2] + '_wrap.cxx'
if not os.path.exists(wfile):
return True
return os.path.getmtime(ifile) > os.path.getmtime(wfile)
def update_integrator_exts():
pwd = chdir(os.path.join(rootdir, 'mfem', 'common'))
command1 = [sys.executable, "generate_lininteg_ext.py"]
command2 = [sys.executable, "generate_bilininteg_ext.py"]
make_call(command1)
make_call(command2)
os.chdir(pwd)
def update_header_exists(mfem_source):
print("updating the list of existing headers")
list_of_headers = []
L = len(mfem_source.split(os.sep))
for (dirpath, dirnames, filenames) in os.walk(mfem_source):
for filename in filenames:
if filename.endswith('.hpp'):
dirs = dirpath.split(os.sep)[L:]
dirs.append(filename[:-4])
tmp = '_'.join(dirs)
xx = re.split('_|-', tmp)
new_name = 'FILE_EXISTS_'+'_'.join([x.upper() for x in xx])
if new_name not in list_of_headers:
list_of_headers.append(new_name)
pwd = chdir(os.path.join(rootdir, 'mfem', 'common'))
fid = open('existing_mfem_headers.i', 'w')
for x in list_of_headers:
fid.write("#define " + x + "\n")
fid.close()
os.chdir(pwd)
mfemser = bglb.mfems_prefix
mfempar = bglb.mfemp_prefix
update_header_exists(bglb.mfem_source)
swigflag = '-Wall -c++ -python -std=c++17 -fastproxy -olddefs -keyword'.split(' ')
pwd = chdir(os.path.join(rootdir, 'mfem', '_ser'))
serflag = ['-I' + os.path.join(mfemser, 'include'),
'-I' + os.path.join(mfemser, 'include', 'mfem'),
'-I' + os.path.abspath(bglb.mfem_source)]
if bglb.enable_suitesparse:
serflag.append('-I' + os.path.join(bglb.suitesparse_prefix,
'include', 'suitesparse'))
for filename in ['lininteg.i', 'bilininteg.i']:
command = [swig_command] + swigflag + serflag + [filename]
make_call(command)
update_integrator_exts()
commands = []
for filename in ifiles():
if not check_new(filename):
continue
command = [swig_command] + swigflag + serflag + [filename]
commands.append(command)
mp_pool = Pool(max((cpu_count() - 1, 1)))
with mp_pool:
mp_pool.map(subprocess.run, commands)
if not do_parallel:
os.chdir(pwd)
return
chdir(os.path.join(rootdir, 'mfem', '_par'))
parflag = ['-I' + os.path.join(mfempar, 'include'),
'-I' + os.path.join(mfempar, 'include', 'mfem'),
'-I' + os.path.abspath(bglb.mfem_source),
'-I' + os.path.join(bglb.hypre_prefix, 'include'),
'-I' + os.path.join(bglb.metis_prefix, 'include'),
'-I' + get_mpi4py_inc()]
if bglb.enable_pumi:
parflag.append('-I' + os.path.join(bglb.pumi_prefix, 'include'))
if bglb.enable_strumpack:
parflag.append('-I' + os.path.join(bglb.strumpack_prefix, 'include'))
if bglb.enable_suitesparse:
parflag.append('-I' + os.path.join(bglb.suitesparse_prefix,
'include', 'suitesparse'))
commands = []
for filename in ifiles():
if filename == 'strumpack.i' and not bglb.enable_strumpack:
continue
if not check_new(filename):
continue
command = [swig_command] + swigflag + parflag + [filename]
commands.append(command)
mp_pool = Pool(max((cpu_count() - 1, 1)))
with mp_pool:
mp_pool.map(subprocess.run, commands)
os.chdir(pwd)
def clean_wrapper():
from pathlib import Path
# serial
pwd = chdir(os.path.join(rootdir, 'mfem', '_ser'))
wfiles = [x for x in os.listdir() if x.endswith('_wrap.cxx')]
print(os.getcwd(), wfiles)
remove_files(wfiles)
wfiles = [x for x in os.listdir() if x.endswith('_wrap.h')]
remove_files(wfiles)
wfiles = [x for x in os.listdir() if x.endswith('.py')]
wfiles.remove("__init__.py")
wfiles.remove("setup.py")
wfiles.remove("tmop_modules.py")
remove_files(wfiles)
ifiles = [x for x in os.listdir() if x.endswith('.i')]
for x in ifiles:
Path(x).touch()
# parallel
chdir(os.path.join(rootdir, 'mfem', '_par'))
wfiles = [x for x in os.listdir() if x.endswith('_wrap.cxx')]
remove_files(wfiles)
wfiles = [x for x in os.listdir() if x.endswith('_wrap.h')]
remove_files(wfiles)
wfiles = [x for x in os.listdir() if x.endswith('.py')]
wfiles.remove("__init__.py")
wfiles.remove("setup.py")
wfiles.remove("tmop_modules.py")
remove_files(wfiles)
ifiles = [x for x in os.listdir() if x.endswith('.i')]
for x in ifiles:
Path(x).touch()
chdir(pwd)
def make_mfem_wrapper(serial=True):
'''
compile PyMFEM wrapper code
'''
from multiprocessing import cpu_count
import build_globals as bglb
if bglb.dry_run or bglb.verbose:
print("compiling wrapper code, serial=" + str(serial))
if not os.path.exists(os.path.abspath(bglb.mfem_source)):
assert False, "MFEM source directory. Use --mfem-source=<path>"
record_mfem_sha(bglb.mfem_source)
write_setup_local()
if serial:
pwd = chdir(os.path.join(rootdir, 'mfem', '_ser'))
else:
pwd = chdir(os.path.join(rootdir, 'mfem', '_par'))
python = sys.executable
command = [python, 'setup.py', 'build_ext', '--inplace', '--parallel',
str(max((cpu_count() - 1, 1)))]
make_call(command, force_verbose=True)
os.chdir(pwd)