Debian Codegraph @

ot/helpers/openmp_helpers.py

raw symbols SCIP
1 """Helpers for OpenMP support during the build."""
2
3 # This code is adapted for a large part from the astropy openmp helpers, which
4 # can be found at: https://github.com/astropy/extension-helpers/blob/master/extension_helpers/_openmp_helpers.py # noqa
5
6 import os
7 import sys
8 import textwrap
9 import subprocess
10
11 from setuptools.errors import CompileError, LinkError
12
13 from pre_build_helpers import compile_test_program
14
15
16 def get_openmp_flag(compiler):
17 """Get openmp flags for a given compiler"""
18
19 if hasattr(compiler, "compiler"):
20 compiler = compiler.compiler[0]
21 else:
22 compiler = compiler.__class__.__name__
23
24 if sys.platform == "win32" and ("icc" in compiler or "icl" in compiler):
25 omp_flag = ["/Qopenmp"]
26 elif sys.platform == "win32":
27 omp_flag = ["/openmp"]
28 elif sys.platform in ("darwin", "linux") and "icc" in compiler:
29 omp_flag = ["-qopenmp"]
30 elif sys.platform == "darwin" and "openmp" in os.getenv("CPPFLAGS", ""):
31 omp_flag = []
32 else:
33 # Default flag for GCC and clang:
34 omp_flag = ["-fopenmp"]
35 if sys.platform.startswith("darwin"):
36 omp_flag += ["-Xpreprocessor", "-lomp"]
37 return omp_flag
38
39
40 def check_openmp_support():
41 """Check whether OpenMP test code can be compiled and run"""
42
43 code = textwrap.dedent(
44 """\
45 #include <omp.h>
46 #include <stdio.h>
47 int main(void) {
48 #pragma omp parallel
49 printf("nthreads=%d\\n", omp_get_num_threads());
50 return 0;
51 }
52 """
53 )
54
55 extra_preargs = os.getenv("LDFLAGS", None)
56 if extra_preargs is not None:
57 extra_preargs = extra_preargs.strip().split(" ")
58 extra_preargs = [
59 flag
60 for flag in extra_preargs
61 if flag.startswith(("-L", "-Wl,-rpath", "-l"))
62 ]
63
64 extra_postargs = get_openmp_flag
65
66 try:
67 output, compile_flags = compile_test_program(
68 code, extra_preargs=extra_preargs, extra_postargs=extra_postargs
69 )
70
71 if output and "nthreads=" in output[0]:
72 nthreads = int(output[0].strip().split("=")[1])
73 openmp_supported = len(output) == nthreads
74 elif "PYTHON_CROSSENV" in os.environ:
75 # Since we can't run the test program when cross-compiling
76 # assume that openmp is supported if the program can be
77 # compiled.
78 openmp_supported = True
79 else:
80 openmp_supported = False
81
82 except (CompileError, LinkError, subprocess.CalledProcessError):
83 openmp_supported = False
84 compile_flags = []
85 return openmp_supported, compile_flags