Esempio n. 1
0
File: rc.py Progetto: RCBiczok/VTK
def profile(name="MPE", **kargs):
    """
    MPI profiling interface
    """
    import sys, os, imp

    #
    try:
        from mpi4py.dl import dlopen, RTLD_NOW, RTLD_GLOBAL
        from mpi4py.dl import dlerror
    except ImportError:
        from ctypes import CDLL as dlopen, RTLD_GLOBAL

        try:
            from DLFCN import RTLD_NOW
        except ImportError:
            RTLD_NOW = 2
        dlerror = None
    #
    logfile = kargs.pop("logfile", None)
    if logfile:
        if name in ("mpe", "MPE"):
            if "MPE_LOGFILE_PREFIX" not in os.environ:
                os.environ["MPE_LOGFILE_PREFIX"] = logfile
        if name in ("vt", "vt-mpi", "vt-hyb"):
            if "VT_FILE_PREFIX" not in os.environ:
                os.environ["VT_FILE_PREFIX"] = logfile
    #
    prefix = os.path.dirname(__file__)
    so = imp.get_suffixes()[0][0]
    if name == "MPE":  # special case
        filename = os.path.join(prefix, name + so)
    else:
        format = [("", so)]
        if sys.platform.startswith("win"):
            format.append(("", ".dll"))
        elif sys.platform == "darwin":
            format.append(("lib", ".dylib"))
        elif os.name == "posix":
            format.append(("lib", ".so"))
        for (lib, _so) in format:
            basename = lib + name + _so
            filename = os.path.join(prefix, "lib-pmpi", basename)
            if os.path.isfile(filename):
                break
            filename = None
    if filename is None:
        relpath = os.path.join(os.path.basename(prefix), "lib-pmpi")
        raise ValueError("profiler '%s' not found in '%s'" % (name, relpath))
    #
    global _pmpi_
    handle = dlopen(filename, RTLD_NOW | RTLD_GLOBAL)
    if handle:
        _pmpi_.append((name, (handle, filename)))
    elif dlerror:
        from warnings import warn

        warn(dlerror())
    #
    return filename
Esempio n. 2
0
def profile(name='MPE', **kargs):
    """
    MPI profiling interface
    """
    import sys, os, imp
    #
    try:
        from mpi4py.dl import dlopen, RTLD_NOW, RTLD_GLOBAL
        from mpi4py.dl import dlerror
    except ImportError:
        from ctypes import CDLL as dlopen, RTLD_GLOBAL
        try:
            from DLFCN import RTLD_NOW
        except ImportError:
            RTLD_NOW = 2
        dlerror = None
    #
    logfile = kargs.pop('logfile', None)
    if logfile:
        if name in ('mpe', 'MPE'):
            if 'MPE_LOGFILE_PREFIX' not in os.environ:
                os.environ['MPE_LOGFILE_PREFIX'] = logfile
        if name in ('vt', 'vt-mpi', 'vt-hyb'):
            if 'VT_FILE_PREFIX' not in os.environ:
                os.environ['VT_FILE_PREFIX'] = logfile
    #
    prefix = os.path.dirname(__file__)
    so = imp.get_suffixes()[0][0]
    if name == 'MPE':  # special case
        filename = os.path.join(prefix, name + so)
    else:
        format = [('', so)]
        if sys.platform.startswith('win'):
            format.append(('', '.dll'))
        elif sys.platform == 'darwin':
            format.append(('lib', '.dylib'))
        elif os.name == 'posix':
            format.append(('lib', '.so'))
        for (lib, _so) in format:
            basename = lib + name + _so
            filename = os.path.join(prefix, 'lib-pmpi', basename)
            if os.path.isfile(filename): break
            filename = None
    if filename is None:
        relpath = os.path.join(os.path.basename(prefix), 'lib-pmpi')
        raise ValueError("profiler '%s' not found in '%s'" % (name, relpath))
    #
    global _pmpi_
    handle = dlopen(filename, RTLD_NOW | RTLD_GLOBAL)
    if handle:
        _pmpi_.append((name, (handle, filename)))
    elif dlerror:
        from warnings import warn
        warn(dlerror())
    #
    return filename
Esempio n. 3
0
    def testDL1(self):
        if sys.platform == 'darwin':
            libm = 'libm.dylib'
        else:
            libm = 'libm.so'

        handle = dl.dlopen(libm, dl.RTLD_LOCAL|dl.RTLD_LAZY)
        if handle == 0 and sys.platform.startswith('linux'):
            self.assertTrue(dl.dlerror() is not None)
            handle = dl.dlopen('libm.so.6', dl.RTLD_LOCAL|dl.RTLD_LAZY)
        self.assertTrue(handle != 0)
        self.assertTrue(dl.dlerror() is None)

        symbol = dl.dlsym(handle, 'sqrt')
        self.assertTrue(symbol != 0)
        self.assertTrue(dl.dlerror() is None)

        symbol = dl.dlsym(handle, 'xxxxx')
        self.assertTrue(symbol == 0)
        self.assertTrue(dl.dlerror() is not None)

        ierr = dl.dlclose(handle)
        self.assertTrue(ierr == 0)
        self.assertTrue(dl.dlerror() is None)
Esempio n. 4
0
    def testDL1(self):
        if sys.platform == 'darwin':
            libm = 'libm.dylib'
        else:
            libm = 'libm.so'

        handle = dl.dlopen(libm, dl.RTLD_LOCAL | dl.RTLD_LAZY)
        if handle == 0 and sys.platform.startswith('linux'):
            self.assertTrue(dl.dlerror() is not None)
            handle = dl.dlopen('libm.so.6', dl.RTLD_LOCAL | dl.RTLD_LAZY)
        self.assertTrue(handle != 0)
        self.assertTrue(dl.dlerror() is None)

        symbol = dl.dlsym(handle, 'sqrt')
        self.assertTrue(symbol != 0)
        self.assertTrue(dl.dlerror() is None)

        symbol = dl.dlsym(handle, 'xxxxx')
        self.assertTrue(symbol == 0)
        self.assertTrue(dl.dlerror() is not None)

        ierr = dl.dlclose(handle)
        self.assertTrue(ierr == 0)
        self.assertTrue(dl.dlerror() is None)
Esempio n. 5
0
def profile(name='MPE', **kargs):
    """
    MPI profiling interface
    """
    import sys, os, imp
    #
    try:
        from mpi4py.dl import dlopen, RTLD_NOW, RTLD_GLOBAL
    except ImportError:
        from ctypes import CDLL as dlopen, RTLD_GLOBAL
        try:
            from DLFCN import RTLD_NOW
        except ImportError:
            RTLD_NOW = 2
    #
    prefix = os.path.dirname(__file__)
    so = imp.get_suffixes()[0][0]
    if name == 'MPE': # special case
        filename = os.path.join(prefix, name + so)
        if 'MPE_LOGFILE_PREFIX' not in os.environ:
            logfile = kargs.pop('logfile', None)
            if logfile:
                os.environ['MPE_LOGFILE_PREFIX'] = logfile
    else:
        format = [('', so)]
        if sys.platform.startswith('win'):
            format.append(('', '.dll'))
        elif sys.platform == 'darwin':
            format.append(('lib', '.dylib'))
        elif os.name == 'posix':
            format.append(('lib', '.so'))
        for (lib, _so) in format:
            basename = lib + name + _so
            filename = os.path.join(prefix, 'lib-pmpi', basename)
            if not os.path.isfile(filename):
                filename = None
            else:
                break
        if filename is None:
            relpath = os.path.join(os.path.basename(prefix), 'lib-pmpi')
            raise ValueError(
                "profiler '%s' not found in '%s'" % (name, relpath))
    #
    global _pmpi_
    handle = dlopen(filename, RTLD_NOW|RTLD_GLOBAL)
    _pmpi_.append( (handle, filename) )
    #
    return filename
Esempio n. 6
0
    def testDL2(self):
        handle = dl.dlopen(None, dl.RTLD_GLOBAL|dl.RTLD_NOW)
        self.assertTrue(handle != 0)
        self.assertTrue(dl.dlerror() is None)

        symbol = dl.dlsym(handle, 'malloc')
        self.assertTrue(symbol != 0)
        self.assertTrue(dl.dlerror() is None)

        symbol = dl.dlsym(handle, '!@#$%^&*()')
        self.assertTrue(symbol == 0)
        self.assertTrue(dl.dlerror() is not None)

        ierr = dl.dlclose(handle)
        self.assertTrue(ierr == 0)
        self.assertTrue(dl.dlerror() is None)
Esempio n. 7
0
    def testDL2(self):
        handle = dl.dlopen(None, dl.RTLD_GLOBAL|dl.RTLD_NOW)
        self.assertTrue(handle != 0)
        self.assertTrue(dl.dlerror() is None)

        symbol = dl.dlsym(handle, 'malloc')
        self.assertTrue(symbol != 0)
        self.assertTrue(dl.dlerror() is None)

        symbol = dl.dlsym(handle, '!@#$%^&*()')
        self.assertTrue(symbol == 0)
        self.assertTrue(dl.dlerror() is not None)

        ierr = dl.dlclose(handle)
        self.assertTrue(ierr == 0)
        self.assertTrue(dl.dlerror() is None)
Esempio n. 8
0
    def testDL1(self):
        from ctypes.util import find_library
        libm = find_library('m')

        handle = dl.dlopen(libm, dl.RTLD_LOCAL | dl.RTLD_LAZY)
        self.assertTrue(handle != 0)
        self.assertTrue(dl.dlerror() is None)

        symbol = dl.dlsym(handle, 'sqrt')
        self.assertTrue(symbol != 0)
        self.assertTrue(dl.dlerror() is None)

        symbol = dl.dlsym(handle, 'xxxxx')
        self.assertTrue(symbol == 0)
        self.assertTrue(dl.dlerror() is not None)

        ierr = dl.dlclose(handle)
        self.assertTrue(ierr == 0)
        self.assertTrue(dl.dlerror() is None)
Esempio n. 9
0
 def testDL4(self):
     handle = dl.dlopen('xxxxx', dl.RTLD_LOCAL|dl.RTLD_LAZY)
     self.assertTrue(handle == 0)
     self.assertTrue(dl.dlerror() is not None)
Esempio n. 10
0
def profile(name='mpe', **kargs):
    """
    Support for the MPI profiling interface.

    Parameters
    ----------
    name : str, optional
       Name of the profiler to load.
    path : list of str, optional
       Additional paths to search for the profiler.
    logfile : str, optional
       Filename prefix for dumping profiler output.
    """
    # pylint: disable=too-many-locals
    # pylint: disable=too-many-branches
    import sys
    import os
    try:
        from mpi4py.dl import dlopen, RTLD_NOW, RTLD_GLOBAL
        from mpi4py.dl import dlerror
    except ImportError:
        from ctypes import CDLL as dlopen, RTLD_GLOBAL
        try:
            # pylint: disable=import-error
            from DLFCN import RTLD_NOW
        except ImportError:
            RTLD_NOW = 2  # pylint: disable=invalid-name
        dlerror = None

    def lookup_dylib(name, path):
        # pylint: disable=missing-docstring
        import imp
        pattern = []
        for suffix, _, kind in imp.get_suffixes():
            if kind == imp.C_EXTENSION:
                pattern.append(('', suffix))
        if sys.platform.startswith('win'):
            pattern.append(('', '.dll'))
        elif sys.platform == 'darwin':
            pattern.append(('lib', '.dylib'))
        elif os.name == 'posix':
            pattern.append(('lib', '.so'))
        pattern.append(('', ''))
        for pth in path:
            for (lib, dso) in pattern:
                filename = os.path.join(pth, lib + name + dso)
                if os.path.isfile(filename):
                    return filename
        return None
    #
    logfile = kargs.pop('logfile', None)
    if logfile:
        if name in ('mpe',):
            if 'MPE_LOGFILE_PREFIX' not in os.environ:
                os.environ['MPE_LOGFILE_PREFIX'] = logfile
        if name in ('vt', 'vt-mpi', 'vt-hyb'):
            if 'VT_FILE_PREFIX' not in os.environ:
                os.environ['VT_FILE_PREFIX'] = logfile
    path = kargs.pop('path', None)
    if path is None:
        path = []
    elif isinstance(path, str):
        path = [path]
    else:
        path = list(path)

    prefix = os.path.dirname(__file__)
    path.append(os.path.join(prefix, 'lib-pmpi'))
    filename = lookup_dylib(name, path)
    if filename is None:
        raise ValueError("profiler '%s' not found" % name)
    else:
        filename = os.path.abspath(filename)

    handle = dlopen(filename, RTLD_NOW | RTLD_GLOBAL)
    if handle:
        profile.registry.append((name, (handle, filename)))
    else:
        from warnings import warn
        if dlerror:
            message = dlerror()
        else:
            message = "error loading '%s'" % filename
        warn(message)
Esempio n. 11
0
 def testDL4(self):
     handle = dl.dlopen('xxxxx', dl.RTLD_LOCAL|dl.RTLD_LAZY)
     self.assertTrue(handle == 0)
     self.assertTrue(dl.dlerror() is not None)
Esempio n. 12
0
def profile(name='mpe', **kargs):
    """
    Support for the MPI profiling interface.

    Parameters
    ----------
    name : str, optional
       Name of the profiler to load.
    path : list of str, optional
       Additional paths to search for the profiler.
    logfile : str, optional
       Filename prefix for dumping profiler output.
    """
    # pylint: disable=too-many-locals
    # pylint: disable=too-many-branches
    import sys
    import os
    try:
        from mpi4py.dl import dlopen, RTLD_NOW, RTLD_GLOBAL
        from mpi4py.dl import dlerror
    except ImportError:
        from ctypes import CDLL as dlopen, RTLD_GLOBAL
        try:
            # pylint: disable=import-error
            from DLFCN import RTLD_NOW
        except ImportError:
            RTLD_NOW = 2  # pylint: disable=invalid-name
        dlerror = None

    def lookup_dylib(name, path):
        # pylint: disable=missing-docstring
        import imp
        pattern = []
        for suffix, _, kind in imp.get_suffixes():
            if kind == imp.C_EXTENSION:
                pattern.append(('', suffix))
        if sys.platform.startswith('win'):
            pattern.append(('', '.dll'))
        elif sys.platform == 'darwin':
            pattern.append(('lib', '.dylib'))
        elif os.name == 'posix':
            pattern.append(('lib', '.so'))
        pattern.append(('', ''))
        for pth in path:
            for (lib, dso) in pattern:
                filename = os.path.join(pth, lib + name + dso)
                if os.path.isfile(filename):
                    return filename
        return None

    #
    logfile = kargs.pop('logfile', None)
    if logfile:
        if name in ('mpe', ):
            if 'MPE_LOGFILE_PREFIX' not in os.environ:
                os.environ['MPE_LOGFILE_PREFIX'] = logfile
        if name in ('vt', 'vt-mpi', 'vt-hyb'):
            if 'VT_FILE_PREFIX' not in os.environ:
                os.environ['VT_FILE_PREFIX'] = logfile
    path = kargs.pop('path', None)
    if path is None:
        path = []
    elif isinstance(path, str):
        path = [path]
    else:
        path = list(path)

    prefix = os.path.dirname(__file__)
    path.append(os.path.join(prefix, 'lib-pmpi'))
    filename = lookup_dylib(name, path)
    if filename is None:
        raise ValueError("profiler '%s' not found" % name)
    else:
        filename = os.path.abspath(filename)

    handle = dlopen(filename, RTLD_NOW | RTLD_GLOBAL)
    if handle:
        profile.registry.append((name, (handle, filename)))
    else:
        from warnings import warn
        if dlerror:
            message = dlerror()
        else:
            message = "error loading '%s'" % filename
        warn(message)