Check whether OpenMP test code can be compiled and run
()
| 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 |