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

Constant matrix partial evaluation

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

Constant matrix partial evaluation

Mon May 25, 2009 11:55 pm
Hi

In my particular problem i am working with some very sparse constant 6x6 matrices, which i am multiplying with a vector at runtime - as fast as possible. The fastest approach would be to manually unroll the multiplication and manually remove the multiplications by zero, however i am also trying to achieve readability.

My question is, is there a way to encourage compilers to unroll the such an expression and remove the multiplication of zero constants? Or are my only options manual unrolling or some meta programming approach?. So far i haven't had any success with any compiler optimizations (ie the density of the constant matrix makes no difference to speed).

Thanks for any assistance.
User avatar
bjacob
Registered Member
Posts
658
Karma
3
Honestly, if your 6x6 matrices are really very sparse, maybe they have such a special form that you can reinterpret the matrix products in a completly different way. An example is permutation matrices...

Otherwise, if you really think you have to do it, you can do the following. First, play with EIGEN_UNROLLING_LIMIT to make sure that your operations are completely unrolled. For matrix-vector product, the default should be OK.
Then write your own "very sparse matrix expression". An expression is a class inheriting Eigen::MatrixBase. For an example, you can have a look at the CwiseNullaryOp expression. The crucial part will be the coeff() method. For example, for a matrix that is zero everywhere except that m(2,3)=1, your coeff() method would be:

Code: Select all
EIGEN_STRONG_INLINE float MyExpression::coeff(int row,int col) const
{
  if(row == 2 && col == 3) return 1;
  else return 0;
}


(the EIGEN_STRONG_INLINE here is because if you want this to evaluate at compile-time, you've got to make sure that this function is inlined)

The idea is that if everything gets completely unrolled, then coeff(row,col) will only get called with values of row and col that are known at compile-time. Then if it is inlined, the compiler will be able to resolve the if's at compile-time. You'll need a good compiler here.

Last edited by bjacob on Tue May 26, 2009 1:27 am, edited 1 time in total.


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!
schnootly
Registered Member
Posts
2
Karma
0
Thank you for your help bjacob, i will give that a try.


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Sogou [Bot]