-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
_openmp_helpers.py
361 lines (282 loc) · 10.2 KB
/
_openmp_helpers.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
# This module defines functions that can be used to check whether OpenMP is
# available and if so what flags to use. To use this, import the
# add_openmp_flags_if_available function in a setup_package.py file where you
# are defining your extensions:
#
# from extension_helpers.openmp_helpers import add_openmp_flags_if_available
#
# then call it with a single extension as the only argument:
#
# add_openmp_flags_if_available(extension)
#
# this will add the OpenMP flags if available.
__doctest_skip__ = ["_get_flag_value_from_var"]
import datetime
import glob
import logging
import os
import subprocess
import sys
import tempfile
import time
from setuptools.command.build_ext import (
customize_compiler,
get_config_var,
new_compiler,
)
from ._setup_helpers import get_compiler
__all__ = ["add_openmp_flags_if_available"]
try:
# Check if this has already been instantiated, only set the default once.
_EXTENSION_HELPERS_DISABLE_OPENMP_SETUP_ # noqa: B018
except NameError:
import builtins
# It hasn't, so do so.
builtins._EXTENSION_HELPERS_DISABLE_OPENMP_SETUP_ = False
log = logging.getLogger(__name__)
CCODE = """
#include <omp.h>
#include <stdio.h>
int main(void) {
#pragma omp parallel
printf("nthreads=%d\\n", omp_get_num_threads());
return 0;
}
"""
CCODE_ICX = """
#ifndef __INTEL_LLVM_COMPILER
#error This is not the Intel oneAPI compiler
#endif
"""
def _get_flag_value_from_var(flag, var, delim=" "):
"""
Extract flags from an environment variable.
Parameters
----------
flag : str
The flag to extract, for example '-I' or '-L'
var : str
The environment variable to extract the flag from, e.g. CFLAGS or LDFLAGS.
delim : str, optional
The delimiter separating flags inside the environment variable
Examples
--------
Let's assume the LDFLAGS is set to '-L/usr/local/include -customflag'. This
function will then return the following:
>>> _get_flag_value_from_var('-L', 'LDFLAGS')
'/usr/local/include'
Notes
-----
Environment variables are first checked in ``os.environ[var]``, then in
``sysconfig.get_config_var(var)``.
This function is not supported on Windows.
"""
if sys.platform.startswith("win"):
return None
# Simple input validation
if not var or not flag:
return None
flag_length = len(flag)
if not flag_length:
return None
# Look for var in os.eviron then in get_config_var
if var in os.environ:
flags = os.environ[var]
else:
try:
flags = get_config_var(var)
except KeyError:
return None
# Extract flag from {var:value}
if flags:
for item in flags.split(delim):
if item.startswith(flag):
return item[flag_length:]
def _check_if_compiler_is_icx():
"""
Check whether the compiler is the Intel oneAPI compiler.
Returns
-------
result : bool
`True` if the test passed, `False` otherwise.
"""
ccompiler = new_compiler()
customize_compiler(ccompiler)
with tempfile.TemporaryDirectory() as tmp_dir:
start_dir = os.path.abspath(".")
try:
os.chdir(tmp_dir)
# Write test program
with open("test_icx.c", "w") as f:
f.write(CCODE_ICX)
os.mkdir("objects")
# Compile program
ccompiler.compile(["test_icx.c"], output_dir="objects")
except Exception:
is_icx = False
else:
is_icx = True
finally:
os.chdir(start_dir)
return is_icx
def get_openmp_flags():
"""
Utility for returning compiler and linker flags possibly needed for
OpenMP support.
Returns
-------
result : `{'compiler_flags':<flags>, 'linker_flags':<flags>}`
Notes
-----
The flags returned are not tested for validity, use
`check_openmp_support(openmp_flags=get_openmp_flags())` to do so.
"""
compile_flags = []
link_flags = []
if get_compiler() == "msvc":
compile_flags.append("-openmp")
else:
include_path = _get_flag_value_from_var("-I", "CFLAGS")
if include_path:
compile_flags.append("-I" + include_path)
lib_path = _get_flag_value_from_var("-L", "LDFLAGS")
if lib_path:
link_flags.append("-L" + lib_path)
link_flags.append("-Wl,-rpath," + lib_path)
if _check_if_compiler_is_icx():
openmp_flags = "-qopenmp"
else:
openmp_flags = "-fopenmp"
compile_flags.append(openmp_flags)
link_flags.append(openmp_flags)
return {"compiler_flags": compile_flags, "linker_flags": link_flags}
def check_openmp_support(openmp_flags=None):
"""
Check whether OpenMP test code can be compiled and run.
Parameters
----------
openmp_flags : dict, optional
This should be a dictionary with keys ``compiler_flags`` and
``linker_flags`` giving the compilation and linking flags respectively.
These are passed as `extra_postargs` to `compile()` and
`link_executable()` respectively. If this is not set, the flags will
be automatically determined using environment variables.
Returns
-------
result : bool
`True` if the test passed, `False` otherwise.
"""
ccompiler = new_compiler()
customize_compiler(ccompiler)
if not openmp_flags:
# customize_compiler() extracts info from os.environ. If certain keys
# exist it uses these plus those from sysconfig.get_config_vars().
# If the key is missing in os.environ it is not extracted from
# sysconfig.get_config_var(). E.g. 'LDFLAGS' get left out, preventing
# clang from finding libomp.dylib because -L<path> is not passed to
# linker. Call get_openmp_flags() to get flags missed by
# customize_compiler().
openmp_flags = get_openmp_flags()
compile_flags = openmp_flags.get("compiler_flags")
link_flags = openmp_flags.get("linker_flags")
with tempfile.TemporaryDirectory() as tmp_dir:
start_dir = os.path.abspath(".")
try:
os.chdir(tmp_dir)
# Write test program
with open("test_openmp.c", "w") as f:
f.write(CCODE)
os.mkdir("objects")
# Compile, test program
ccompiler.compile(["test_openmp.c"], output_dir="objects", extra_postargs=compile_flags)
# Link test program
objects = glob.glob(os.path.join("objects", "*" + ccompiler.obj_extension))
ccompiler.link_executable(objects, "test_openmp", extra_postargs=link_flags)
# Run test program
output = subprocess.check_output("./test_openmp")
output = output.decode(sys.stdout.encoding or "utf-8").splitlines()
if "nthreads=" in output[0]:
nthreads = int(output[0].strip().split("=")[1])
if len(output) == nthreads:
is_openmp_supported = True
else:
log.warning(
"Unexpected number of lines from output of test OpenMP "
"program (output was {})".format(output)
)
is_openmp_supported = False
else:
log.warning("Unexpected output from test OpenMP program (output was %s)", output)
is_openmp_supported = False
except Exception:
is_openmp_supported = False
finally:
os.chdir(start_dir)
return is_openmp_supported
def is_openmp_supported():
"""
Determine whether the build compiler has OpenMP support.
"""
log_threshold = log.level
log.setLevel("CRITICAL")
ret = check_openmp_support()
log.setLevel(log_threshold)
return ret
def add_openmp_flags_if_available(extension):
"""
Add OpenMP compilation flags, if supported (if not a warning will be
printed to the console and no flags will be added.)
Returns `True` if the flags were added, `False` otherwise.
"""
if _EXTENSION_HELPERS_DISABLE_OPENMP_SETUP_: # noqa: F821
log.info("OpenMP support has been explicitly disabled.")
return False
openmp_flags = get_openmp_flags()
using_openmp = check_openmp_support(openmp_flags=openmp_flags)
if using_openmp:
compile_flags = openmp_flags.get("compiler_flags")
link_flags = openmp_flags.get("linker_flags")
log.info("Compiling Cython/C/C++ extension with OpenMP support")
extension.extra_compile_args.extend(compile_flags)
extension.extra_link_args.extend(link_flags)
else:
log.warning(
"Cannot compile Cython/C/C++ extension with OpenMP, reverting to non-parallel code"
)
return using_openmp
_IS_OPENMP_ENABLED_SRC = """
# Autogenerated by {packagename}'s setup.py on {timestamp!s}
def is_openmp_enabled():
\"\"\"
Determine whether this package was built with OpenMP support.
\"\"\"
return {return_bool}
"""[
1:
]
def generate_openmp_enabled_py(packagename, srcdir=".", disable_openmp=None):
"""
Generate ``package.openmp_enabled.is_openmp_enabled``, which can then be used
to determine, post build, whether the package was built with or without
OpenMP support.
"""
epoch = int(os.environ.get("SOURCE_DATE_EPOCH", time.time()))
if sys.version_info >= (3, 11):
timestamp = datetime.datetime.fromtimestamp(epoch, datetime.UTC)
else:
timestamp = datetime.datetime.utcfromtimestamp(epoch)
if disable_openmp is not None:
import builtins
builtins._EXTENSION_HELPERS_DISABLE_OPENMP_SETUP_ = disable_openmp
if _EXTENSION_HELPERS_DISABLE_OPENMP_SETUP_: # noqa: F821
log.info("OpenMP support has been explicitly disabled.")
openmp_support = False
else:
openmp_support = is_openmp_supported()
src = _IS_OPENMP_ENABLED_SRC.format(
packagename=packagename, timestamp=timestamp, return_bool=openmp_support
)
package_srcdir = os.path.join(srcdir, *packagename.split("."))
is_openmp_enabled_py = os.path.join(package_srcdir, "openmp_enabled.py")
with open(is_openmp_enabled_py, "w") as f:
f.write(src)