3 ms·
Before you say 'oh, Haskell is still only half the speed of C++', you should keep in mind just how insane the C++ code they had to write looks. The Eigen code t
by kevingadd 14y ago
Before you say 'oh, Haskell is still only half the speed of C++', you should keep in mind just how insane the C++ code they had to write looks. The Eigen code they had to write is pretty nasty and getting it correct required some particular understanding.
In comparison, the performance they're getting out of Haskell is coming from comparatively mundane, easy to understand code. That's pretty impressive.
Or another way of putting it: It looks like naive Haskell outperforms naive C++, and naive Haskell is not that much slower than carefully-tuned C++.
template < typename Derived >
typename Derived::Scalar norm2( const MatrixBase <Derived >& v ) {
return v.dot(v);
}
Why is it a template function? Why does it return Derived::Scalar instead of double? Dear god, Eigen is magic.
- dchichkov 14y agoThe code (actual instructions) that beats C++ in their example is being generated by their experimental "stream fusion" framework. And NOT by a generic Haskell compiler. And now the question, do you really want to an unproven and unstable Haskell framework? While there are stable alternatives (lake Eigen, Theano, etc) available? So no. Haskell (production ready Haskell) doesn't even come close.
- ibotty 14y agoif i understand the paper correctly (and apply the knowledge to eigen), it't because of the same reason: you want to allow different stream fusion strategies to work.
- pbsd 14y agoWhile 'Derived::Scalar' could very well be replaced by 'double' in that case, you need the templated function to catch the temporary type (expression template) that represents the u-v computation. This lets Eigen compute u[i]-v[i] and the dot product inside the same inner loop. Without the templated function, the compiler would first generate the subtraction into a temporary vector, and then compute the dot product on that vector.
- kevingadd 14y agoThat makes sense, thanks! I figured the template was being used to magically substitute some sort of meta-type or container for temporaries.
- dfbrown 14y agoI'm not sure why they wrote this in the first place. Eigen's MatrixBase already has a squaredNorm function: http://eigen.tuxfamily.org/dox/classEigen_1_1MatrixBase.html#a229bd5cc6237359a1d85401743476ede http://eigen.tuxfamily.org/dox/classEigen_1_1MatrixBase.html... edit: I see, they address this in the article: In this specific case the Eigen library has the squared L2-norm in its API, but in more complicated examples there might be no such built-in provision
- marshray 14y agoThat code doesn't look particularly insane to me. Uh oh.