OSX中的Cython openMP..没有构建



我现在要战斗3天来设置我的venv。。。我需要cython,openmp。。。。我的IDE是macOS中的PyCharm教授。。。我尝试了很多解决方案,但都没有结果。。。

当我尝试用PyCharm构建。。。构建结果是:

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

所以,当我尝试:python setup.py install build_ext --inplace不再生成。。。。错误如下:

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

我已经安装了:brew install llvmbrew install libomp

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

setup.py根据https://cython.readthedocs.io/en/latest/src/userguide/parallelism.html但我已经尝试了很多不同的模式…我现在正在尝试设置3天。。。

最后。。。我知道编译是有错误的。虽然如果我试着运行它…我有以下结果。。。也许它有帮助。。。

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

我在某个地方读到,使用openMP无法在OSX Cython应用程序中实现,但我仍然相信有一种方法。。。我的系统是macOS Catalina 10.15.3。。。我需要你的帮助!!!

终于找到了解决方案。。。

  1. 在您的供应商中:

pip install Cython setuptools

  1. 查找gcc编译器的位置。。。如果您已经安装了,请执行以下操作,在其他情况下,请查找并安装一个

$ mdfind gcc | grep gcc

  1. 将整个gcc目录放在项目的venv中。(路径:/My_Project/venv/gcc(

  2. setup.py应该如下。。。(路径:/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. 您的cython文件:filename.pyx…可以在filename.pyx顶部以编译器指令注释开头,如下所示:
# distutils: extra_compile_args = -fopenmp
# distutils: extra_link_args = -fopenmp
  1. 在您的venv中运行编译命令:

$ python setup.py build_ext -i

  1. 最后,您可以在代码中导入cython文件。。。在您的python文件等的顶部。mycode.py
import filename
filename.myCythonFunc()

上面给出了OSX-10.15.3 macOS Catalina的解决方案。

最新更新