geo*_*rge 10 c++ performance matrix eigen
有人告诉我,为了提高程序的性能,我应该使用一些专门的矩阵类而不是我自己的类.
StackOverflow用户推荐:
起初我想使用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;
}
Run Code Online (Sandbox Code Playgroud)
和
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();
}
Run Code Online (Sandbox Code Playgroud)
如您所见,这些函数执行大量矩阵运算.这就是为什么我认为使用Eigen可以加速我的应用程序.不幸的是(正如我上面提到的),程序工作得更慢.
有没有办法加快这些功能?
也许如果我使用DirectX矩阵操作我会得到更好的性能?(但我有一台带有集成显卡的笔记本电脑).
tim*_*day 12
如果您使用Eigen的MatrixXd类型,那么它们是动态大小的.你应该多使用固定大小类型,例如更好的效果Matrix4d,Vector4d.
此外,请确保您正在编译,以便代码可以进行矢量化; 请参阅相关的Eigen文档.
重新考虑使用Direct3D扩展库的东西(D3DXMATRIX等):对于图形几何(4x4转换等)来说没关系(如果有点老式),但它肯定不是GPU加速的(我认为只是旧的SSE).另外,请注意它只是浮点精度(您似乎使用双精度设置).我个人更喜欢使用Eigen,除非我实际编写Direct3D应用程序.
Jak*_*kob 10
确保打开编译器优化(例如,gcc上至少为-O2).如果你不打开优化,Eigen是模糊的,并且表现不佳.
您应该首先分析然后优化算法,然后再实现.特别是,发布的代码非常有效:
for (int i=0;i<pointVector.size();i++ )
{
Eigen::MatrixXd outcome = (rotation*scale)* (*pointVector[i]) + translation;
Run Code Online (Sandbox Code Playgroud)
我不知道图书馆,所以我甚至不会尝试猜测你正在创建的不必要的临时数量,而是一个简单的重构:
Eigen::MatrixXd tmp = rotation*scale;
for (int i=0;i<pointVector.size();i++ )
{
Eigen::MatrixXd outcome = tmp*(*pointVector[i]) + translation;
Run Code Online (Sandbox Code Playgroud)
可以为您节省大量昂贵的乘法(再次,可能会立即丢弃新的临时矩阵.
您使用的是哪个版本的Eigen?他们最近发布了3.0.1,它应该比2.x更快.另外,请确保您使用编译器选项.例如,确保在Visual Studio中使用SSE:
C/C++ - >代码生成 - >启用增强指令集