简体   繁体   English

使用 CFFI 从 Python 调用带有 OpenMP 的 C 函数

[英]Using CFFI to call C-function with OpenMP from Python

I am using CFFI to call a C function with OpenMP from Python.我正在使用 CFFI 从 Python 调用带有 OpenMP 的 C 函数。 My code works on one of my computers, but not the other with a very similar setup.我的代码适用于我的一台计算机,但不适用于具有非常相似设置的另一台计算机。

import os
from cffi import FFI

# test 
os.system("gcc -fopenmp -c test.c -o test.o")
os.system("gcc -o test.exe test.o -fopenmp")
os.system("test.exe")

# gateway
ffi = FFI()
os.system("gcc -o test.so test.c -shared -fopenmp")
ffi.cdef(r''' int main(); ''')        
lib = ffi.dlopen(r'''test.so''')
lib.main()

The error is错误是

OSError: cannot load library test.so: error 0x45a

I am using Python 3.5 (the latest Anaconda distribution) and TDM-GCC 5.1.0.我正在使用 Python 3.5(最新的 Anaconda 发行版)和 TDM-GCC 5.1.0。 The test runs on both computers.测试在两台计算机上运行。 What can explain the difference behavior?什么可以解释差异行为?

Short version - you define your header (.h) and source files (.c) as usual with cffi.简短版本 - 像往常一样使用 cffi 定义头文件 (.h) 和源文件 (.c)。 For openmp you have to include extra compile and linker arguments as follows:对于 openmp,您必须包含额外的编译和链接器参数,如下所示:

import cffi

ffi = cffi.FFI()    
ffi.cdef(header_string)    
ffi.set_source(
    '_my_module_name',
    source_string,
    extra_compile_args=['-fopenmp'],
    extra_link_args=['-fopenmp'],
)
ffi.compile()
import _my_module_name

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM