cppyy调用含OPENMP的C++代码出现nullptr错误求助
在cppyy中调用含OpenMP并行化的C++代码时出现未解析符号错误
问题描述
用cppyy在Python中调用包含OpenMP并行化的C++代码时遭遇错误:
- 用GCC编译(参数
-fopenmp -O2)的可执行文件运行正常,并行化带来的性能提升符合预期; - 但C++代码保留
#pragma omp parallel for指令时,cppyy抛出未解析符号错误(如__kmpc_for_static_fini等),最终触发ValueError: nullptr result where temporary expected; - 注释掉该OpenMP指令后,无异常出现。
报错信息
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++测试代码
#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; }
系统环境
- Ubuntu 18.04.2 LTS,Intel® Core™ i7-10700 CPU @ 2.90GHz × 16,64位
- Python 3.9.0
- cppyy 2.4.0(pip安装)
- Eigen 3.4.0
已尝试操作
- 按照cppyy官方文档设置环境变量
EXTRA_CLING_ARGS='-fopenmp'及CLING_STANDARD_PCH - C++可执行文件编译命令:
g++-11 easy_example.cpp -fopenmp -O2 -I <path_to_Eigen_library here>
内容的提问来源于stack exchange,提问作者Thejasvi
相关产品推荐
相关产品推荐

