This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Eigen3 Slow =/

Tags: None
(comma "," separated)
alexlzzymeek
Registered Member
Posts
2
Karma
0

Eigen3 Slow =/

Fri Aug 01, 2014 11:01 am
Hello everyone.

I am in process of implementing mesh deformations algorithm. But i am having some performance issues:

Here is the code:
Code: Select all
   
MatrixXf AqqInverse(9, 9);
   AqqInverse = MatrixXf::Identity(9, 9);
   XMFLOAT3* q;

   MatrixXf qT(9, 1);
   qT = MatrixXf::Identity(9, 1);

   MatrixXf qnt(9, 1);
   qnt = MatrixXf::Identity(9, 1);

   MatrixXf res;

   float m;

   for (unsigned int point = 0; point < object->m_controlPoints.size(); point++)
   {
      m = object->m_controlPoints[point]->m_mass;
      q = &object->m_controlPoints[point]->m_originalPos;

      qT << q->x,
         q->y,
         q->z,
         q->x * q->x,
         q->y * q->y,
         q->z * q->z,
         q->x * q->y,
         q->y * q->z,
         q->z * q->x;

      qnt = qT;
      qnt.transposeInPlace();
      res = qT * qnt;
      AqqInverse += res;
   }
   object->m_Aqq_tilde = AqqInverse.inverse();


qnt.transposeInPlace(); is a (9,1) Matrix, which i try to transpose. It seems to slow down the loop quite dramatically, is this normal behavior, or is there an optimization procedure?

I am using Visual-studio 2013 ( 120 toolset );
Thanks!
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Eigen3 Slow =/

Fri Aug 01, 2014 1:57 pm
Please, use VectorXf for vectors, and then simply do to fully exploit Eigen:

AqqInverse += qT * qT.transpose();

Also, do not benchmark in "debug" mode ;) It is at least 10 times slower without compiler optimizations.
alexlzzymeek
Registered Member
Posts
2
Karma
0

Re: Eigen3 Slow =/

Fri Aug 01, 2014 2:30 pm
Thanks, that helped in a way. But when i try to use VectorXf instead of MAtrixXf , i get worse performance.
I do test everything in release build. Why would this be the case?
Also using .noalias() helped quite bit "AqqInverse.noalias() += qT * qT.transpose();"
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Eigen3 Slow =/

Sat Aug 02, 2014 12:21 am
Hm, strange because I observe a x6 speedup. You can even get higher performance by assembling a larger matrix, and do a big matrix-matrix product:

Code: Select all


#include <iostream>
#include <bench/BenchTimer.h>
#include <Eigen/Dense>
using namespace Eigen;

EIGEN_DONT_INLINE void foo1(MatrixXf &res, Matrix<float, 3, Dynamic> &points)
{
  MatrixXf AqqInverse(9, 9);
  AqqInverse = MatrixXf::Identity(9, 9);

  MatrixXf qT(9, 1);
  qT = MatrixXf::Identity(9, 1);

  MatrixXf qnt(9, 1);
  qnt = MatrixXf::Identity(9, 1);

  for (int i = 0; i < points.cols(); i++)
  {
    Vector3f q = points.col(i);
    qT << q.x(),q.y(),q.z(), q.x()*q.x(),q.y()*q.y(),q.z()*q.z(), q.x()*q.y(),q.y()*q.z(),q.z()*q.x();

    qnt = qT;
    qnt.transposeInPlace();
    res.noalias() = qT * qnt;
    AqqInverse += res;
  }
  res = AqqInverse.inverse();
}


EIGEN_DONT_INLINE void foo2(MatrixXf &res, Matrix<float, 3, Dynamic> &points)
{
  MatrixXf AqqInverse(9, 9);
  AqqInverse = MatrixXf::Identity(9, 9);

  Matrix<float,9,1> qT;
  for (int i = 0; i < points.cols(); i++)
  {
    Vector3f q = points.col(i);
    qT << q.x(),q.y(),q.z(), q.x()*q.x(),q.y()*q.y(),q.z()*q.z(), q.x()*q.y(),q.y()*q.z(),q.z()*q.x();

    AqqInverse.noalias() += qT * qT.transpose();
  }
  res = AqqInverse.inverse();
}

EIGEN_DONT_INLINE void foo3(MatrixXf &res, Matrix<float, 3, Dynamic> &points)
{
  MatrixXf AqqInverse(9, 9);
  AqqInverse = MatrixXf::Identity(9, 9);
 
  MatrixXf Q(9,points.cols());
  for (int i = 0; i < points.cols(); i++)
  {
    Vector3f q = points.col(i);
    Q.col(i) << q.x(),q.y(),q.z(), q.x()*q.x(),q.y()*q.y(),q.z()*q.z(), q.x()*q.y(),q.y()*q.z(),q.z()*q.x();
  }
 
  AqqInverse.noalias() += Q * Q.transpose();
  res = AqqInverse.inverse();
}

int main()
{
  int n = 1000;
  int tries = 10;
  int repeats = 100;
  BenchTimer t;
 
  Matrix<float, 3, Dynamic> points(3,n);
  points.setRandom();
  MatrixXf res;
 
  BENCH(t, tries, repeats, foo1(res, points));
  std::cout << "foo1: " << t.best() << "\n";
 
  BENCH(t, tries, repeats, foo2(res, points));
  std::cout << "foo2: " << t.best() << "\n";
 
  BENCH(t, tries, repeats, foo3(res, points));
  std::cout << "foo3: " << t.best() << "\n";
}


I get:
Code: Select all
foo1: 0.025141
foo2: 0.00425602
foo3: 0.00141648


Bookmarks



Who is online

Registered users: Bing [Bot], claydoh, Google [Bot], rblackwell, Yahoo [Bot]