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

Cwise questions

Tags: None
(comma "," separated)
rfcantao
Registered Member
Posts
3
Karma
0
OS

Cwise questions

Mon Oct 05, 2009 3:31 pm
Hi Eigen Team,

First of all, awesome work! Thank you very much!

I am quite new to Eigen (moving from FreePooma) and I have a few questions:

1. It's very convenient to be able to define a Scalar -> Scalar function and apply it cwisely:
Code: Select all
double foo(double);

A.unaryExpr(foo);

I have some N-ary operations (for instance, e-0.5*(u*u+v*v+w*w)) and I would like to transform them info functions in order to do
Code: Select all
A.NaryExpr( e, u, v, w )

instead of the macro approach I am using right now
Code: Select all
A = MY_FUNCTION( e, u, v, w)


So, is it possible to define something like "NaryExpr" in a way that does not hurt perfomance?

2. I have a lot of big expressions ranging from 3 to 20 terms, already factored out, all applied in a cwise fashion. Is there a "not so obvious" way to speed up evaluation of these expressions, or I can simply close my eyes and let Eigen do all the work?

Thanks a lot,
Renato
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: Cwise questions

Mon Oct 05, 2009 3:44 pm
1- actually, thanks to expression template and the rich API of Eigen it is very rarely the case that you need to write custom cwise functors. Also, custom cwise functors cannot be vectorized while pure Eigen code will be, e.g.:

e + 0.5 * (u.cwise().square() + v.cwise().square() + w.cwise().square())

and then you can even write a template generic function implementing this expression if you want. No need to use cwise functors for that.


2 - Eigen already does unrolling and vectorization for you, so the only way you can help Eigen is to try to evaluate sub expressions which cannot be vectorized such that other parts of the expression get vectorized but this require a deep knowledge about Eigen's current vectorization support...
rfcantao
Registered Member
Posts
3
Karma
0
OS

Re: Cwise questions

Mon Oct 05, 2009 4:28 pm
Hi Gaël,

Thanks for the answers!

1. Ok, undestood, but it raises another question. I am using a macro that implements exactly what you said, using cwise().square(). If I am willing to write a function, I can see two options:
Code: Select all
template<...>
void foo( MatrixBase<...> const& u, MatrixBase<...> const& v, MatrixBase<...> const& w, MatrixBase<...> const& e, MatrixBase<...>& res ) {
  res = e + 0.5*( u.cwise().square() +... etc );
}

in this case, the return type is also an argument of the function. Not so expressive, but it should work fine.

The other option
Code: Select all
template<...>
HERE??? foo( MatrixBase<...> const& u, MatrixBase<...> const& v, MatrixBase<...> const& w, MatrixBase<...> const& e ) {
  return e + 0.5*( u.cwise().square() +... etc );
}

should return what? I guess is probably an expression, but I don't know how to declare it.

2. Ok, so let Eigen do the job :)

Thanks again,
Renato
User avatar
bjacob
Registered Member
Posts
658
Karma
3

Re: Cwise questions

Mon Oct 05, 2009 5:26 pm
About 1.

The thing that you should put in "HERE" is the type of the resulting expression which looks very, very ugly, so I wouldn't really recommend that you enter into this; moreover with the current code you'd have to put NestByValue at the right place although that latter point is likely to go away in the next release.

So:
- if you just want optimal performance, go with your solution taking the result object as argument.
- if you want optimal performance AND syntactic sugar, two options:
a) let your function return a ReturnByValue object (only with the development version of eigen2)
b) use C++1x's new "rvalue references" feature
- if your expression is complex enough that adding one redundant copy operation is negligible, and if your matrices are not too big, then just let your function return a Matrix by value.


Join us on Eigen's IRC channel: #eigen on irc.freenode.net
Have a serious interest in Eigen? Then join the mailing list!
rfcantao
Registered Member
Posts
3
Karma
0
OS

Re: Cwise questions [SOLVED]

Mon Oct 05, 2009 6:02 pm
Yes, I have guessed I would need to put the resulting expression type on the return argument, which can be very, very ugly.

I will go the optimal performance route using the return as argument. Who likes sugar anyway? :)

Thanks a lot,
Renato


Bookmarks



Who is online

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