xtensor-python-cookiecutter cannot successfully build my module as expected, when using openmp

162 views Asked by At

I encountered a problem when using openmp in xtensor-python with xtensor-python-cookiecutter.

After executing python setup.py build, I encountered an error when import myLib: undefined symbol: omp_get_thread_num.

And below is my log of python setup.py build

$ py setup.py build
running build
running build_ext
creating tmp
gcc -pthread -B /home/joker/.miniconda3/envs/cpp/compiler_compat -Wl,--sysroot=/ -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/home/joker/.miniconda3/envs/cpp/include/python3.8 -c /tmp/tmp8bct6mhu.cpp -o tmp/tmp8bct6mhu.o -std=c++14
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++
gcc -pthread -B /home/joker/.miniconda3/envs/cpp/compiler_compat -Wl,--sysroot=/ -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/home/joker/.miniconda3/envs/cpp/include/python3.8 -c /tmp/tmpja5ljvir.cpp -o tmp/tmpja5ljvir.o -fvisibility=hidden
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++
building 'test' extension
creating build
creating build/temp.linux-x86_64-3.8
creating build/temp.linux-x86_64-3.8/src
gcc -pthread -B /home/joker/.miniconda3/envs/cpp/compiler_compat -Wl,--sysroot=/ -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -I/home/joker/.miniconda3/envs/cpp/include -I/home/joker/.miniconda3/envs/cpp/include -I/home/joker/.miniconda3/envs/cpp/lib/python3.8/site-packages/numpy/core/include -I/home/joker/.miniconda3/envs/cpp/include -I/home/joker/.miniconda3/envs/cpp/Library/include -I/home/joker/.miniconda3/envs/cpp/include/python3.8 -c src/main.cpp -o build/temp.linux-x86_64-3.8/src/main.o -fopenmp -DVERSION_INFO="0.1.0" -std=c++14 -fvisibility=hidden
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++
creating build/lib.linux-x86_64-3.8
g++ -pthread -shared -B /home/joker/.miniconda3/envs/cpp/compiler_compat -L/home/joker/.miniconda3/envs/cpp/lib -Wl,-rpath=/home/joker/.miniconda3/envs/cpp/lib -Wl,--no-as-needed -Wl,--sysroot=/ build/temp.linux-x86_64-3.8/src/main.o -o build/lib.linux-x86_64-3.8/test.cpython-38-x86_64-linux-gnu.so

Check the last command:

g++ -pthread -shared -B /home/joker/.miniconda3/envs/cpp/compiler_compat -L/home/joker/.miniconda3/envs/cpp/lib -Wl,-rpath=/home/joker/.miniconda3/envs/cpp/lib -Wl,--no-as-needed -Wl,--sysroot=/ build/temp.linux-x86_64-3.8/src/main.o -o build/lib.linux-x86_64-3.8/test.cpython-38-x86_64-linux-gnu.so

It seems that -fopenmp is missing. After I run it mannual with -fopenmp, i.e.

g++ -fopenmp -pthread -shared -B /home/joker/.miniconda3/envs/cpp/compiler_compat -L/home/joker/.miniconda3/envs/cpp/lib -Wl,-rpath=/home/joker/.miniconda3/envs/cpp/lib -Wl,--no-as-needed -Wl,--sysroot=/ build/temp.linux-x86_64-3.8/src/main.o -o build/lib.linux-x86_64-3.8/test.cpython-38-x86_64-linux-gnu.so

anything is well.

I have added the flag -fopenmp in setup.py, but I don't know why it doesn't work as expected

class BuildExt(build_ext):
    """A custom build extension for adding compiler-specific options."""
    c_opts = {
        'msvc': ['/EHsc'],
        'unix': ['-fopenmp'],
    }

Thanks for your reading.

and below is my full setup.py

from setuptools import setup, Extension
from setuptools.command.build_ext import build_ext
import sys
import os
import setuptools

__version__ = '0.1.0'


class get_pybind_include(object):
    """Helper class to determine the pybind11 include path

    The purpose of this class is to postpone importing pybind11
    until it is actually installed, so that the ``get_include()``
    method can be invoked. """

    def __init__(self, user=False):
        self.user = user

    def __str__(self):
        import pybind11
        return pybind11.get_include(self.user)


class get_numpy_include(object):
    """Helper class to determine the numpy include path

    The purpose of this class is to postpone importing numpy
    until it is actually installed, so that the ``get_include()``
    method can be invoked. """

    def __init__(self):
        pass

    def __str__(self):
        import numpy as np
        return np.get_include()


ext_modules = [
    Extension(
        'fold_3d_labelling',
        ['src/main.cpp'],
        include_dirs=[
            # Path to pybind11 headers
            get_pybind_include(),
            get_pybind_include(user=True),
            get_numpy_include(),
            os.path.join(sys.prefix, 'include'),
            os.path.join(sys.prefix, 'Library', 'include')
        ],
        language='c++'
    ),
]


def has_flag(compiler, flagname):
    """Return a boolean indicating whether a flag name is supported on
    the specified compiler.
    """
    import tempfile
    with tempfile.NamedTemporaryFile('w', suffix='.cpp') as f:
        f.write('int main (int argc, char **argv) { return 0; }')
        try:
            compiler.compile([f.name], extra_postargs=[flagname])
        except setuptools.distutils.errors.CompileError:
            return False
    return True


def cpp_flag(compiler):
    """Return the -std=c++14 compiler flag  and errors when the flag is
    no available.
    """
    if has_flag(compiler, '-std=c++14'):
        return '-std=c++14'
    else:
        raise RuntimeError('C++14 support is required by xtensor!')


class BuildExt(build_ext):
    """A custom build extension for adding compiler-specific options."""
    c_opts = {
        'msvc': ['/EHsc'],
        'unix': ['-fopenmp'],
    }

    if sys.platform == 'darwin':
        c_opts['unix'] += ['-stdlib=libc++', '-mmacosx-version-min=10.7']

    def build_extensions(self):
        ct = self.compiler.compiler_type
        opts = self.c_opts.get(ct, [])
        if ct == 'unix':
            opts.append('-DVERSION_INFO="%s"' % self.distribution.get_version())
            opts.append(cpp_flag(self.compiler))
            if has_flag(self.compiler, '-fvisibility=hidden'):
                opts.append('-fvisibility=hidden')
        elif ct == 'msvc':
            opts.append('/DVERSION_INFO=\\"%s\\"' % self.distribution.get_version())
        for ext in self.extensions:
            ext.extra_compile_args = opts
        build_ext.build_extensions(self)

setup(
    name='fold_3d_labelling',
    version=__version__,
    author='test',
    author_email='[email protected]',
    url='https://test',
    description= '',
    long_description='',
    ext_modules=ext_modules,
    install_requires=['pybind11>=2.0.1', 'numpy'],
    cmdclass={'build_ext': BuildExt},
    zip_safe=False,
)

Thanks again.

0

There are 0 answers