File: openmp_ext.py

package info (click to toggle)
python-bumps 0.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 10,688 kB
  • sloc: python: 24,446; ansic: 4,973; cpp: 4,849; javascript: 639; xml: 493; makefile: 147; perl: 108; sh: 94
file content (69 lines) | stat: -rw-r--r-- 2,128 bytes parent folder | download | duplicates (3)
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
"""
Compile openmp extensions with distutils.

:func:`openmp_ext` returns a replacement *build_ext* command that adds
OpenMP command line parameters to the C compiler for your system.  Use
this is setup.py for any modules that need to be compiled for OpenMP.
"""
import sys
from distutils.command.build_ext import build_ext

__all__ = ['openmp_ext']


def openmp_ext(default=True):
    """
    Enable openmp.

    Add the following to setup.py::

        setup(..., cmdclass={'build_ext': openmp_build_ext()}, ...)

    Enable openmp using "--with-openmp" as a setup parameter, or disable
    it using "--without-openmp".  If no option is specfied, the developer
    *default* value will be used.

    On OS X you will need to specify an openmp compiler::

        CC=openmp-cc CXX=openmp-c++ python setup.py --with-openmp

    Note: when using openmp, you should not use multiprocessing parallelism
    otherwise python will hang.  This is a known bug in the current version
    of python and gcc.  If your modeling code is compiled with openmp, you
    can set OMP_NUM_THREADS=1 in the environment to suppress openmp threading
    when you are running --parallel fits in batch.
    """
    with_openmp = default
    if '--with-openmp' in sys.argv:
        with_openmp = True
        sys.argv.remove('--with-openmp')
    elif '--without-openmp' in sys.argv:
        with_openmp = False
        sys.argv.remove('--without-openmp')

    if not with_openmp:
        return build_ext

    compile_opts = {
        'msvc': ['/openmp'],
        'mingw32': ['-fopenmp'],
        'unix': ['-fopenmp'],
    }
    link_opts = {
        'mingw32': ['-fopenmp'],
        'unix': ['-lgomp'],
    }

    class OpenMPExt(build_ext):

        def build_extensions(self):
            c = self.compiler.compiler_type
            if c in compile_opts:
                for e in self.extensions:
                    e.extra_compile_args = compile_opts[c]
            if c in link_opts:
                for e in self.extensions:
                    e.extra_link_args = link_opts[c]
            build_ext.build_extensions(self)

    return OpenMPExt