C++ 如何实现Eigen';s建议禁用并行化/多线程——在哪里以及有多少预处理器令牌?
我有一个小程序,它在多个cpp类文件中使用C++ 如何实现Eigen';s建议禁用并行化/多线程——在哪里以及有多少预处理器令牌?,c++,parallel-processing,openmp,eigen3,C++,Parallel Processing,Openmp,Eigen3,我有一个小程序,它在多个cpp类文件中使用Eigen的矩阵,比如说main.cpp和MyClass.h. #include <Eigen/Dense> #include <parallel/algorithm> using namespace std; int main(int argc, char** argv) { Eigen::MatrixXd hello = Eigen::MatrixXd::Random(1000,2); // make a large
Eigen
的矩阵,比如说main.cpp
和MyClass.h.
#include <Eigen/Dense>
#include <parallel/algorithm>
using namespace std;
int main(int argc, char** argv) {
Eigen::MatrixXd hello = Eigen::MatrixXd::Random(1000,2); // make a large matrix so that
//Eigen would want to use parallelisation
vector<Eigen::MatrixXd> my_container(1000,Eigen::MatrixXd::Random(1000,1000)); //make a large
//container so __gnu_parallel would want to use parallelisation
//a parallel loop here
__gnu_parallel::for_each(my_container.begin(),my_container.end(),[&hello]
(const Eigen::Ref<const Eigen::MatrixXd>& MyMat){
MyMat * hello; //dense matrix multiplication which Eigen would parallelise
});
return 0;
}
我的问题
\u gnu\u parallel
和Eigen
的并行化会相互干扰吗?我的工作假设是肯定的#include
之前或之后,我将令牌定义放置在何处MyClass.h
,其中我使用Eigen
的矩阵定义类和方法MyClass::MyMethod
,该矩阵对Eigen库具有单独的包含,我是否需要沿着以下代码段的行使用单独的标记(最好带有保护)#define EIGEN_DONT_PARALLELIZE
#ifndef EIGEN_DONT_PARALLELIZE
#define EIGEN_DONT_PARALLELIZE
#endif
#include <Eigen/Dense>
class MyClass
{ //code using Eigen's matrices here};
#if (defined _OPENMP) && (!defined EIGEN_DONT_PARALLELIZE)
#define EIGEN_HAS_OPENMP
#endif.