几周前我 asked a question关于矩阵乘法的性能。
有人告诉我,为了提高我的程序的性能,我应该使用一些专门的矩阵类而不是我自己的类。
StackOverflow 用户推荐:
- uBLAS
- Eigen
- BLAS
起初我想使用 uBLAS 但是阅读 documentation原来这个库不支持矩阵-矩阵乘法。
毕竟我决定使用 EIGEN 库。所以我将我的矩阵类交换为 Eigen::MatrixXd
- 但事实证明,现在我的应用程序运行速度比以前更慢。
使用 EIGEN 之前的时间是 68 秒,将我的矩阵类交换为 EIGEN 矩阵程序运行 87 秒之后。
花费最多时间的程序部分看起来像这样
TemplateClusterBase* TemplateClusterBase::TransformTemplateOne( vector<Eigen::MatrixXd*>& pointVector, Eigen::MatrixXd& rotation ,Eigen::MatrixXd& scale,Eigen::MatrixXd& translation )
{
for (int i=0;i<pointVector.size();i++ )
{
//Eigen::MatrixXd outcome =
Eigen::MatrixXd outcome = (rotation*scale)* (*pointVector[i]) + translation;
//delete prototypePointVector[i]; // ((rotation*scale)* (*prototypePointVector[i]) + translation).ConvertToPoint();
MatrixHelper::SetX(*prototypePointVector[i],MatrixHelper::GetX(outcome));
MatrixHelper::SetY(*prototypePointVector[i],MatrixHelper::GetY(outcome));
//assosiatedPointIndexVector[i] = prototypePointVector[i]->associatedTemplateIndex = i;
}
return this;
}
和
Eigen::MatrixXd AlgorithmPointBased::UpdateTranslationMatrix( int clusterIndex )
{
double membershipSum = 0,outcome = 0;
double currentPower = 0;
Eigen::MatrixXd outcomePoint = Eigen::MatrixXd(2,1);
outcomePoint << 0,0;
Eigen::MatrixXd templatePoint;
for (int i=0;i< imageDataVector.size();i++)
{
currentPower =0;
membershipSum += currentPower = pow(membershipMatrix[clusterIndex][i],m);
outcomePoint.noalias() += (*imageDataVector[i] - (prototypeVector[clusterIndex]->rotationMatrix*prototypeVector[clusterIndex]->scalingMatrix* ( *templateCluster->templatePointVector[prototypeVector[clusterIndex]->assosiatedPointIndexVector[i]]) ))*currentPower ;
}
outcomePoint.noalias() = outcomePoint/=membershipSum;
return outcomePoint; //.ConvertToMatrix();
}
如您所见,这些函数执行大量矩阵运算。这就是为什么我认为使用 Eigen 可以加快我的应用程序的原因。不幸的是(正如我上面提到的),该程序运行速度较慢。
有什么方法可以加快这些功能吗?
也许如果我使用 DirectX 矩阵运算我会获得更好的性能?? (不过我有一台带集成显卡的笔记本电脑)。
最佳答案
如果您使用的是 Eigen 的 MatrixXd
类型,这些类型是动态调整大小的。使用固定大小的类型,例如 Matrix4d
、Vector4d
,您应该会得到更好的结果。
此外,请确保您正在编译,以便代码可以矢量化;查看relevant Eigen documentation .
关于使用 Direct3D 扩展库(D3DXMATRIX 等)的想法:图形几何(4x4 变换等)没问题(如果有点过时),但它肯定不是 GPU 加速的(只是很好的旧 SSE,我认为).另外,请注意它只是浮点精度(你似乎被设置为使用 double )。就个人而言,我更喜欢使用 Eigen,除非我实际上是在编写 Direct3D 应用程序。
关于c++ - 使用 Eigen 的性能比使用我自己的类更差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6193546/