我一直试图在C++中通过cppyy运行一些并行化代码,但是遇到了一个错误。
该可执行文件(通过GCC使用-fopenmp -O2编译)运行时没有错误,并显示了运行时由于并行化而出现的预期下降。
当#pragma omp parallel for从C++代码中注释出来时,cppyy不会引发任何错误。但是,当杂注是代码的一部分时,我会得到以下错误:
IncrementalExecutor::executeFunction: symbol '__kmpc_for_static_fini' unresolved while linking symbol '__cf_4'!
IncrementalExecutor::executeFunction: symbol '__kmpc_for_static_init_4' unresolved while linking symbol '__cf_4'!
IncrementalExecutor::executeFunction: symbol '__kmpc_fork_call' unresolved while linking symbol '__cf_4'!
IncrementalExecutor::executeFunction: symbol '__kmpc_global_thread_num' unresolved while linking symbol '__cf_4'!
Traceback (most recent call last):
File "...../SO_troubleshooting/example_pll_cppyy_code.py", line 8, in <module>
output = cppyy.gbl.pll_somelinalgeb()
ValueError: std::vector<std::vector<Eigen::Matrix<double,-1,-1,0,-1,-1> > > ::pll_somelinalgeb() =>
ValueError: nullptr result where temporary expected下面是简短的Python脚本:
import cppyy
cppyy.add_include_path('../np_vs_eigen/eigen/')
cppyy.include('easy_example.cpp')
vector = cppyy.gbl.std.vector
import datetime as dt
print('Starting the function call now ')
start = dt.datetime.now()
output = cppyy.gbl.pll_somelinalgeb()
stop = dt.datetime.now()
print((stop-start), 'seconds')C++玩具代码如下。它用Eigen生成一个随机矩阵,计算其伪逆,然后休眠1ms.
#include <omp.h>
#include <iostream>
#include <Eigen/Dense>
#include <chrono>
#include <vector>
#include <thread>
using Eigen::VectorXd;
using Eigen::MatrixXd;
std::vector<MatrixXd> some_linearalgebra(){
std::vector<MatrixXd> solutions;
std::srand((unsigned int) time(0));//ensures a new random matrix each time
MatrixXd arraygeom(5,3);
arraygeom = MatrixXd::Random(5,3);
VectorXd row1 = arraygeom.block(0,0,1,3).transpose();
arraygeom.rowwise() -= row1.transpose();
MatrixXd pinv_arraygeom(3,5);
// calculate the pseudoinverse of arraygeom
pinv_arraygeom = arraygeom.completeOrthogonalDecomposition().pseudoInverse();
//std::cout << pinv_arraygeom << std::endl;
solutions.push_back(pinv_arraygeom);
solutions.push_back(pinv_arraygeom);
std::this_thread::sleep_for(std::chrono::milliseconds(1));
return solutions;
}
std::vector<std::vector<MatrixXd>> pll_somelinalgeb(){
int num_runs = 5000;
std::vector<std::vector<MatrixXd>> all_solns(num_runs);
#pragma omp parallel for
for (int i=0; i<num_runs; i++){
all_solns[i] = some_linearalgebra();
}
return all_solns;
}
int main(){
std::vector<MatrixXd> main_out;
main_out = some_linearalgebra();
auto start = std::chrono::system_clock::now();
std::vector<std::vector<MatrixXd>> main2_out;
main2_out = pll_somelinalgeb();
auto end = std::chrono::system_clock::now();
std::cout << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count() << " ms" << std::endl;
return 0;
}System + OS规范:
使用的非默认预编译cppyy头:
根据此链接,我在终端export EXTRA_CLING_ARGS='-fopenmp'上运行以下命令,然后使用cppyy_backend.loader运行代码,然后使用另一个export添加CLING_STANDARD_PCH环境变量。
用C++编译的g++-11 easy_example.cpp -fopenmp -O2 -I <path_to_Eigen_library here>可执行
发布于 2022-09-26 10:06:43
问题是将OpenMP库文件链接到运行cppyy的that编译器。
我在Linux和Windows 11中也遇到了同样的问题--这让我意识到这不是一个特定于操作系统的问题。最后起作用的是:
-fopenmp标志添加到您的EXTRA_CLING_ARGS环境变量( Unix中的export EXTRA_CLING_ARGS='-fopenmp',在Windows中则通过“开始”菜单并添加一个新的环境变量)。使用docs 这里中的代码编译一个新的预编译头,并使用$ locate libiomp5在Unix中,或者在Windows中搜索libiomp5 )。Unix文件应该是libiomp5.so,Windows版本是libiomp5md.dllcppyy.load_library(<insert path to .so or .dll file here>)库现在应该有并行代码了!
这可以让您使用工作代码,但可以肯定的是,这是一种相当手动的方法--如果听到更自动化的方法,您会很高兴。
https://stackoverflow.com/questions/73816713
复制相似问题