Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/157.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 加权外积的矢量化_C++_Vectorization_Eigen - Fatal编程技术网

C++ 加权外积的矢量化

C++ 加权外积的矢量化,c++,vectorization,eigen,C++,Vectorization,Eigen,我希望加速计算近似加权协方差 具体来说,我有一个特征::向量xd(N)w和一个特征::矩阵xd(M,N)点。我想计算w(I)*points.col(I)*(points.col(I).transpose())的和 我正在使用for循环,但想看看是否可以更快: Eigen::VectorXd w=Eigen::VectorXd(N); 特征::矩阵xxd点=特征::矩阵xxd(M,N); 特征::矩阵xxd tempMatrix=特征::矩阵xxd(M,M); 对于(int i=0;i

我希望加速计算近似加权协方差

具体来说,我有一个
特征::向量xd(N)w
和一个
特征::矩阵xd(M,N)点
。我想计算
w(I)*points.col(I)*(points.col(I).transpose())
的和

我正在使用for循环,但想看看是否可以更快:

Eigen::VectorXd w=Eigen::VectorXd(N);
特征::矩阵xxd点=特征::矩阵xxd(M,N);
特征::矩阵xxd tempMatrix=特征::矩阵xxd(M,M);
对于(int i=0;i

期待着看看能做些什么

以下方法应该有效:

Eigen::MatrixXd tempMatrix; // not necessary to pre-allocate
// assigning the product allocates tempMatrix if needed
// noalias() tells Eigen that no factor on the right aliases with tempMatrix
tempMatrix.noalias() = points * w.asDiagonal() * points.adjoint();
或直接:

Eigen::MatrixXd tempMatrix = points * w.asDiagonal() * points.adjoint();
如果
M
非常大,只需计算一面并复制它(如果需要)就可以大大加快速度:

如果你想这样做,你需要写:

Eigen::MatrixXd tempMatrix = Eigen::MatrixXd::Zero(M,M);

我想到两件事<代码>对齐方式
std::execution::par
。也就是说,使用
std::hardware\u destroctive\u interference\u size
对齐数据以避免错误共享。然后使用标准库使用执行策略
std::execution::par
@RHertel来处理数据。它们对于非复杂值是等效的。我喜欢编写
.adjunct()
,当复杂结构需要它来保持一致性时(或者在我实际需要使代码通用的情况下)。实际上我使用了<代码>转置()/<代码意外地在中间(改变了,并添加了一个注释)。
Eigen::MatrixXd tempMatrix = Eigen::MatrixXd(M,M);
Eigen::MatrixXd tempMatrix = Eigen::MatrixXd::Zero(M,M);