几周前,我问了一个关于矩阵乘法性能的问题。
有人告诉我,为了提高程序的性能,我应该使用一些专门的矩阵类而不是我自己的类。
StackOverflow 用户推荐:
- uBLAS
- 本征
- 布拉斯
起初我想使用 uBLAS 但是阅读文档发现这个库不支持矩阵矩阵乘法。
毕竟我决定使用 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 矩阵运算,我会获得更好的性能??(但是我有一台带集成显卡的笔记本电脑)。