我目前正在寻求使用多处理的能力来加速我的代码.然而,在从python调用编译后的代码时,我遇到了一些问题,因为当编译后的文件包含任何形式的多处理时,编译后的文件似乎会从代码的视图中消失.
例如,使用以下测试代码:
#include <omp.h>
int main() {
int thread_id;
#pragma omp parallel
{
thread_id = omp_get_thread_num();
}
return 0;
}
在这里,我编译程序,然后使用以下命令将其转换为.so文件
gcc -fopenmp -o theories/test.so -shared -fPIC -O2 test.c
然后,我try 从test.py运行上述代码:
from ctypes import CDLL
import os
absolute_path = os.path.dirname(os.path.abspath(__file__))
# imports the c libraries
test_lib_path = absolute_path + '/theories/test.so'
test = CDLL(test_lib_path)
test.main()
print('complete')
我得到以下错误:
FileNotFoundError: Could not find module 'C:\[my path]\theories\test.so' (or one of its dependencies). Try using the full path with constructor syntax.
但是,当我注释掉多处理元素以获得以下代码时:
#include <omp.h>
int main() {
int thread_id;
/*
#pragma omp parallel
{
thread_id = omp_get_thread_num();
}
*/
return 0;
}
然后,我可以完美地执行,并在最后打印出"Complete".
我想知道这是怎么发生的,为什么代码看起来编译得很好,但只有在从Python调用它之后才会引发问题(我也判断了一下,文件实际上已经创建了).
最新情况:
- 我现在已经判断了我是否安装了libgomp-1.dll
- 我已经卸载并重新安装了MinGW,没有发生任何变化.
- 我安装了不同的64位版本的GCC,并且使用不同的(64位的python3.10)版本的python会重现相同的错误.这里还有libgomp-1.dll.