/
compiler.py
481 lines (400 loc) · 17.1 KB
/
compiler.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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
from functools import partial
from hashlib import sha1
from os import environ, path
from distutils import version
from subprocess import DEVNULL, CalledProcessError, check_output, check_call
import platform
import warnings
import sys
import numpy.ctypeslib as npct
from codepy.jit import compile_from_string
from codepy.toolchain import GCCToolchain
from devito.archinfo import NVIDIAX, SKX, POWER8, POWER9
from devito.exceptions import CompilationError
from devito.logger import debug, warning, error
from devito.parameters import configuration
from devito.tools import (as_tuple, change_directory, filter_ordered,
memoized_meth, make_tempdir)
__all__ = ['GNUCompiler']
def sniff_compiler_version(cc):
"""
Detect the compiler version.
Adapted from: ::
https://github.com/OP2/PyOP2/
"""
try:
ver = check_output([cc, "--version"]).decode("utf-8")
except (CalledProcessError, UnicodeDecodeError):
return version.LooseVersion("unknown")
except FileNotFoundError:
error("The `%s` compiler isn't available on this system" % cc)
sys.exit(1)
if ver.startswith("gcc"):
compiler = "gcc"
elif ver.startswith("clang"):
compiler = "clang"
elif ver.startswith("Apple LLVM"):
compiler = "clang"
elif ver.startswith("icc"):
compiler = "icc"
else:
compiler = "unknown"
ver = version.LooseVersion("unknown")
if compiler in ["gcc", "icc"]:
try:
# gcc-7 series only spits out patch level on dumpfullversion.
ver = check_output([cc, "-dumpfullversion"], stderr=DEVNULL).decode("utf-8")
ver = '.'.join(ver.strip().split('.')[:3])
ver = version.StrictVersion(ver)
except CalledProcessError:
try:
ver = check_output([cc, "-dumpversion"], stderr=DEVNULL).decode("utf-8")
ver = '.'.join(ver.strip().split('.')[:3])
ver = version.StrictVersion(ver)
except (CalledProcessError, UnicodeDecodeError):
pass
except UnicodeDecodeError:
pass
# Pure integer versions (e.g., ggc5, rather than gcc5.0) need special handling
try:
ver = version.StrictVersion(float(ver))
except TypeError:
pass
return ver
def sniff_mpi_distro(mpiexec):
"""
Detect the MPI version.
"""
try:
ver = check_output([mpiexec, "--version"]).decode("utf-8")
if "open-mpi" in ver:
return 'OpenMPI'
elif "HYDRA" in ver:
return 'MPICH'
except (CalledProcessError, UnicodeDecodeError):
pass
return 'unknown'
class Compiler(GCCToolchain):
"""
Base class for all compiler classes.
The base class defaults all compiler specific settings to empty lists.
Preset configurations can be built by inheriting from `Compiler` and setting
specific flags to the desired values, e.g.:
.. code-block:: python
def class MyCompiler(Compiler):
def __init__(self):
self.cc = 'mycompiler'
self.cflags += ['list', 'of', 'all', 'compiler', 'flags']
The flags that can be set are:
* :data:`self.cc`
* :data:`self.cflag`
* :data:`self.ldflags`
* :data:`self.include_dirs`
* :data:`self.libraries`
* :data:`self.library_dirs`
* :data:`self.defines`
* :data:`self.src_ext`
* :data:`self.so_ext`
* :data:`self.undefines`
Parameters
----------
suffix : str, optional
The JIT compiler version to be used. For example, assuming ``CC=gcc`` and
``suffix='4.9'``, the ``gcc-4.9`` will be used as JIT compiler.
cpp : bool, optional
If True, JIT compile using a C++ compiler. Defaults to False.
mpi : bool, optional
If True, JIT compile using an MPI compiler. Defaults to False.
"""
fields = {'cc', 'ld'}
def __init__(self, **kwargs):
super(Compiler, self).__init__(**kwargs)
self.__lookup_cmds__()
self.suffix = kwargs.get('suffix')
if not kwargs.get('mpi'):
self.cc = self.CC if kwargs.get('cpp', False) is False else self.CXX
self.cc = self.cc if self.suffix is None else ('%s-%s' %
(self.cc, self.suffix))
else:
self.cc = self.MPICC if kwargs.get('cpp', False) is False else self.MPICXX
self.ld = self.cc # Wanted by the superclass
self.cflags = ['-O3', '-g', '-fPIC', '-Wall', '-std=c99']
self.ldflags = ['-shared']
self.include_dirs = []
self.libraries = ['m']
self.library_dirs = []
self.defines = []
self.undefines = []
self.src_ext = 'c' if kwargs.get('cpp', False) is False else 'cpp'
if platform.system() == "Linux":
self.so_ext = '.so'
elif platform.system() == "Darwin":
self.so_ext = '.dylib'
elif platform.system() == "Windows":
self.so_ext = '.dll'
else:
raise NotImplementedError("Unsupported platform %s" % platform)
if self.suffix is not None:
try:
self.version = version.StrictVersion(str(float(self.suffix)))
except (TypeError, ValueError):
self.version = version.LooseVersion(self.suffix)
else:
# Knowing the version may still be useful to pick supported flags
self.version = sniff_compiler_version(self.CC)
@memoized_meth
def get_jit_dir(self):
"""A deterministic temporary directory for jit-compiled objects."""
return make_tempdir('jitcache')
@memoized_meth
def get_codepy_dir(self):
"""A deterministic temporary directory for the codepy cache."""
return make_tempdir('codepy')
def load(self, soname):
"""
Load a compiled shared object.
Parameters
----------
soname : str
Name of the .so file (w/o the suffix).
Returns
-------
obj
The loaded shared object.
"""
return npct.load_library(str(self.get_jit_dir().joinpath(soname)), '.')
def save(self, soname, binary):
"""
Store a binary into a file within a temporary directory.
Parameters
----------
soname : str
Name of the .so file (w/o the suffix).
binary : obj
The binary data.
"""
sofile = self.get_jit_dir().joinpath(soname).with_suffix(self.so_ext)
if sofile.is_file():
debug("%s: `%s` was not saved in `%s` as it already exists"
% (self, sofile.name, self.get_jit_dir()))
else:
with open(str(sofile), 'wb') as f:
f.write(binary)
debug("%s: `%s` successfully saved in `%s`"
% (self, sofile.name, self.get_jit_dir()))
def make(self, loc, args):
"""Invoke the ``make`` command from within ``loc`` with arguments ``args``."""
hash_key = sha1((loc + str(args)).encode()).hexdigest()
logfile = path.join(self.get_jit_dir(), "%s.log" % hash_key)
errfile = path.join(self.get_jit_dir(), "%s.err" % hash_key)
with change_directory(loc):
with open(logfile, "w") as lf:
with open(errfile, "w") as ef:
command = ['make'] + args
lf.write("Compilation command:\n")
lf.write(" ".join(command))
lf.write("\n\n")
try:
check_call(command, stderr=ef, stdout=lf)
except CalledProcessError as e:
raise CompilationError('Command "%s" return error status %d. '
'Unable to compile code.\n'
'Compile log in %s\n'
'Compile errors in %s\n' %
(e.cmd, e.returncode, logfile, errfile))
debug("Make <%s>" % " ".join(args))
def jit_compile(self, soname, code):
"""
JIT compile some source code given as a string.
This function relies upon codepy's ``compile_from_string``, which performs
caching of compilation units and avoids potential race conditions due to
multiple processing trying to compile the same object.
Parameters
----------
soname : str
Name of the .so file (w/o the suffix).
code : str
The source code to be JIT compiled.
"""
target = str(self.get_jit_dir().joinpath(soname))
src_file = "%s.%s" % (target, self.src_ext)
cache_dir = self.get_codepy_dir().joinpath(soname[:7])
if configuration['jit-backdoor'] is False:
# Typically we end up here
# Make a suite of cache directories based on the soname
cache_dir.mkdir(parents=True, exist_ok=True)
else:
# Warning: dropping `code` on the floor in favor to whatever is written
# within `src_file`
try:
with open(src_file, 'r') as f:
code = f.read()
# Bypass the devito JIT cache
# Note: can't simply use Python's `mkdtemp()` as, with MPI, different
# ranks would end up creating different cache dirs
cache_dir = cache_dir.joinpath('jit-backdoor')
cache_dir.mkdir(parents=True, exist_ok=True)
except FileNotFoundError:
raise ValueError("Trying to use the JIT backdoor for `%s`, but "
"the file isn't present" % src_file)
# `catch_warnings` suppresses codepy complaining that it's taking
# too long to acquire the cache lock. This warning can only appear
# in a multiprocess session, typically (but not necessarily) when
# many processes are frequently attempting jit-compilation (e.g.,
# when running the test suite in parallel)
with warnings.catch_warnings():
warnings.simplefilter('ignore')
# Spinlock in case of MPI
sleep_delay = 0 if configuration['mpi'] else 1
_, _, _, recompiled = compile_from_string(
self, target, code, src_file,
cache_dir=cache_dir,
debug=configuration['debug-compiler'],
sleep_delay=sleep_delay)
return recompiled, src_file
def __lookup_cmds__(self):
self.CC = 'unknown'
self.CXX = 'unknown'
self.MPICC = 'unknown'
self.MPICXX = 'unknown'
def __str__(self):
return self.__class__.__name__
def __repr__(self):
return "JITCompiler[%s]" % self.__class__.__name__
def __getstate__(self):
# The superclass would otherwise only return a subset of attributes
return self.__dict__
def add_include_dirs(self, dirs):
self.include_dirs = filter_ordered(self.include_dirs + list(as_tuple(dirs)))
def add_library_dirs(self, dirs):
self.library_dirs = filter_ordered(self.library_dirs + list(as_tuple(dirs)))
def add_libraries(self, libs):
self.libraries = filter_ordered(self.libraries + list(as_tuple(libs)))
def add_ldflags(self, flags):
self.ldflags = filter_ordered(self.ldflags + list(as_tuple(flags)))
class GNUCompiler(Compiler):
def __init__(self, *args, **kwargs):
super(GNUCompiler, self).__init__(*args, **kwargs)
self.cflags += ['-march=native', '-Wno-unused-result', '-Wno-unused-variable',
'-Wno-unused-but-set-variable', '--fast-math']
try:
if self.version >= version.StrictVersion("4.9.0"):
# Append the openmp flag regardless of configuration['openmp'],
# since GCC4.9 and later versions implement OpenMP 4.0, hence
# they support `#pragma omp simd`
self.ldflags += ['-fopenmp']
except (TypeError, ValueError):
if configuration['openmp']:
self.ldflags += ['-fopenmp']
def __lookup_cmds__(self):
self.CC = 'gcc'
self.CXX = 'g++'
self.MPICC = 'mpicc'
self.MPICXX = 'mpicxx'
class ClangCompiler(Compiler):
def __init__(self, *args, **kwargs):
super(ClangCompiler, self).__init__(*args, **kwargs)
self.cflags += ['-Wno-unused-result', '-Wno-unused-variable', '-ffast-math']
if configuration['platform'] == NVIDIAX:
# Clang has offloading support via OpenMP
self.cflags.remove('-std=c99')
# TODO: Need a generic -march setup
# self.cflags += ['-Xopenmp-target', '-march=sm_37']
self.ldflags += ['-fopenmp', '-fopenmp-targets=nvptx64-nvidia-cuda']
else:
if configuration['platform'] in [POWER8, POWER9]:
# -march isn't supported on power architectures
self.cflags += ['-mcpu=native']
else:
self.cflags += ['-march=native']
if configuration['openmp']:
self.ldflags += ['-fopenmp']
def __lookup_cmds__(self):
self.CC = 'clang'
self.CXX = 'clang++'
self.MPICC = 'mpicc'
self.MPICXX = 'mpicxx'
class IntelCompiler(Compiler):
def __init__(self, *args, **kwargs):
super(IntelCompiler, self).__init__(*args, **kwargs)
self.cflags += ["-xhost"]
if configuration['platform'] is SKX:
# Systematically use 512-bit vectors on skylake
self.cflags += ["-qopt-zmm-usage=high"]
try:
if self.version >= version.StrictVersion("15.0.0"):
# Append the OpenMP flag regardless of configuration['openmp'],
# since icc15 and later versions implement OpenMP 4.0, hence
# they support `#pragma omp simd`
self.ldflags += ['-qopenmp']
except (TypeError, ValueError):
if configuration['openmp']:
# Note: fopenmp, not qopenmp, is what is needed by icc versions < 15.0
self.ldflags += ['-fopenmp']
# Make sure the MPI compiler uses `icc` underneath -- whatever the MPI distro is
if kwargs.get('mpi'):
ver = check_output([self.MPICC, "--version"]).decode("utf-8")
if not ver.startswith("icc"):
warning("The MPI compiler `%s` doesn't use the Intel "
"C/C++ compiler underneath" % self.MPICC)
def __lookup_cmds__(self):
self.CC = 'icc'
self.CXX = 'icpc'
# On some systems, the Intel distribution of MPI may be available, in
# which case the MPI compiler may be shipped either as `mpiicc` or `mpicc`.
# On other systems, there may be no Intel distribution of MPI available,
# thus the MPI compiler is expected to be the classic `mpicc`. Here,
# we try to use `mpiicc` first, while `mpicc` is our fallback, which may
# or may not be an Intel distribution
try:
check_output(["mpiicc", "--version"]).decode("utf-8")
self.MPICC = 'mpiicc'
self.MPICXX = 'mpiicpc'
except FileNotFoundError:
self.MPICC = 'mpicc'
self.MPICXX = 'mpicxx'
class IntelKNLCompiler(IntelCompiler):
def __init__(self, *args, **kwargs):
super(IntelKNLCompiler, self).__init__(*args, **kwargs)
self.cflags += ["-xMIC-AVX512"]
if not configuration['openmp']:
warning("Running on Intel KNL without OpenMP is highly discouraged")
class CustomCompiler(Compiler):
"""
Custom compiler based on standard environment flags.
Notes
-----
Currently honours CC, CFLAGS and LDFLAGS, with defaults similar to the
default GNU/gcc settings. If DEVITO_ARCH is enabled, the OpenMP linker
flags are read from OMP_LDFLAGS or otherwise default to ``-fopenmp``.
"""
def __init__(self, *args, **kwargs):
super(CustomCompiler, self).__init__(*args, **kwargs)
default = '-O3 -g -march=native -fPIC -Wall -std=c99'
self.cflags = environ.get('CFLAGS', default).split(' ')
self.ldflags = environ.get('LDFLAGS', '-shared').split(' ')
if configuration['openmp']:
self.ldflags += environ.get('OMP_LDFLAGS', '-fopenmp').split(' ')
def __lookup_cmds__(self):
self.CC = environ.get('CC', 'gcc')
self.CXX = environ.get('CXX', 'g++')
self.MPICC = environ.get('MPICC', 'mpicc')
self.MPICXX = environ.get('MPICXX', 'mpicxx')
compiler_registry = {
'custom': CustomCompiler,
'gnu': GNUCompiler,
'gcc': GNUCompiler,
'clang': ClangCompiler,
'osx': ClangCompiler,
'intel': IntelCompiler,
'icpc': IntelCompiler,
'icc': IntelCompiler,
'intel-knl': IntelKNLCompiler,
'knl': IntelKNLCompiler,
}
"""
Registry dict for deriving Compiler classes according to the environment variable
DEVITO_ARCH. Developers should add new compiler classes here.
"""
compiler_registry.update({'gcc-%s' % i: partial(GNUCompiler, suffix=i)
for i in ['4.9', '5', '6', '7', '8', '9']})