Search code examples
pythonmacosgccopenmpcython

Cython openMP in OSX... no build


I 'm fighting 3 days now to setup my venv... I need cython, openmp.... My IDE is PyCharm Prof in macOS... I have tried plenty solutions with no result...

when i try to build with PyCharm... the building result is:

UserWarning: Unknown distribution option: 'cmd_class' warnings.warn(msg)
usage: setup.py [global_opts] cmd1 [cmd1_opts] [cmd2 [cmd2_opts] ...]
   or: setup.py --help [cmd1 cmd2 ...]
   or: setup.py --help-commands
   or: setup.py cmd --help

error: no commands supplied

so, when i try with: python setup.py install build_ext --inplace No build again.... The error is following:

ld: warning: directory not found for option '-L/install/prefix/lib'
ld: warning: -L path '/usr/local/Cellar/llvm/9.0.1/lib/libomp.dylib' is not a directory
ld: warning: directory not found for option '-L/install/prefix/lib'
ld: warning: directory not found for option '-L/usr/local/Cellar/gcc/9/gcc/x86_64-apple-darwin19/9.3.0/include/omp.h'
ld: library not found for -lomp
clang-9: error: linker command failed with exit code 1 (use -v to see invocation)
error: command '/usr/local/opt/llvm/bin/clang++' failed with exit status 1

i have already install: brew install llvm brew install libomp

clang version 9.0.1
Target: x86_64-apple-darwin19.3.0
Thread model: posix
InstalledDir: /usr/local/opt/llvm/bin

The setup.py is according to https://cython.readthedocs.io/en/latest/src/userguide/parallelism.html but i have try a lot of differents mods... i am trying to setup 3-days now...

Finally... i know that the compilation is completed with errors. Although if i try to run it... i have the following resutlts... maybe it helps...

ImportError: dlopen(myfile.pyx, 2): Symbol not found: _omp_get_num_threads
  Referenced from: myfile.pyx
  Expected in: flat namespace
 in myfile.pyx.cpython-36m-darwin.so

I 've read somewhere that there is no way to implement in OSX Cython-App with usage of openMP but i m still believe that there is a way... My system is macOS Catalina 10.15.3 ... I need your help!!!


Solution

  • Finally i find the solution...

    1. In your venv:

    pip install Cython setuptools

    1. Find where is your gcc compiler... if you have already install do as follows, in other case find and install one...

    $ mdfind gcc | grep gcc

    1. Put the whole gcc directory in your project's venv. (path: /My_Project/venv/gcc)

    2. and setup.py should be the following... (path: /My_Project/package/setup.py)

    from distutils.core import setup
    from distutils.extension import Extension
    from Cython.Build import cythonize
    from Cython.Distutils import build_ext
    import os
    
    os.environ["CC"]="../venv/gcc/9.3.0/bin/gcc-9"
    os.environ["CXX"]="../venv/gcc/9.3.0/bin/gcc-9"
    
    ext_modules = [Extension(
            "filename",
            ["filename.pyx"],
            language='c',
            extra_compile_args=['-fopenmp',"-Os",],
            extra_link_args=['-fopenmp', ],
        )
    ]
    
    setup(
        name='filename',
        cmd_class = {'build_ext': build_ext},
        ext_modules= cythonize(ext_modules),
    )
    
    1. Your cython file: filename.pyx ... could starts with a compiler directive comment at the top of filename.pyx as following:
    # distutils: extra_compile_args = -fopenmp
    # distutils: extra_link_args = -fopenmp
    
    1. Run in your venv the compilation command:

    $ python setup.py build_ext -i

    1. Finally you can import your cython file in your code... at the top of your python file etc. mycode.py :
    import filename
    
    filename.myCythonFunc()
    

    The above gives the solution for OSX-10.15.3 macOS Catalina.