Discussion:
[eigen] Comma-initializer is quite expensive with compile-time known constants
Ola Røer Thorsen
2018-11-05 14:34:15 UTC
Permalink
Hi, we're using fixed-size Eigen matrices (eigen 3.3.5), mostly
3-element-vectors, quaternions and 3x3 matrices but also larger ones. We've
been initializing them with the comma initializer, but looking at the
assembly produced the compiler (gcc 7) is not able to optimize everything
away even for small code like

Eigen::Vector3f v;
v << 1,2,3;
example: https://godbolt.org/z/y5YUnJ

This is a bit expensive especially on embedded microcontrollers where we
try to avoid function calls whenever possible. So we'll avoid the comma
initializer completely there now.

This however is much more efficient:
v(0) = 1;
v(1) = 2;
v(2) = 3;
example: https://godbolt.org/z/zoLHJp

(Sure for these short vectors there is the equally efficient
Eigen::Vector3f v(1,2,3)).

Would it be possible to add some C++11 (or 14) feature to help in this
regard? Not sure if you want to keep Eigen completely free of
C++11-features. I've tried some variadic template "make_matrix" functions
that seem to do the trick, not having to do the error checks runtime.
Ideally, it would be nice to be able to write stuff like

const Eigen::Matrix3f m{ 1,2,3, 4,5,6, 7,8,9 };

Best regards,
Ola
Christoph Hertzberg
2018-11-05 14:57:56 UTC
Permalink
Post by Ola Røer Thorsen
Hi, we're using fixed-size Eigen matrices (eigen 3.3.5), mostly
3-element-vectors, quaternions and 3x3 matrices but also larger ones. We've
been initializing them with the comma initializer, but looking at the
assembly produced the compiler (gcc 7) is not able to optimize everything
away even for small code like
Eigen::Vector3f v;
v << 1,2,3;
example: https://godbolt.org/z/y5YUnJ
GCC sometimes has trouble to fully optimize code inside the main
function -- simply renaming your function will give fully optimized code
(I'm not sure whether that is a known issue or a "feature" of GCC).
Also, once you sufficiently tested your code, you should compile with
`-DNDEBUG` to disable assertions.

Comparison:
https://godbolt.org/z/ujpwVU
Post by Ola Røer Thorsen
[...]
Would it be possible to add some C++11 (or 14) feature to help in this
regard? Not sure if you want to keep Eigen completely free of
C++11-features. I've tried some variadic template "make_matrix" functions
that seem to do the trick, not having to do the error checks runtime.
Ideally, it would be nice to be able to write stuff like
const Eigen::Matrix3f m{ 1,2,3, 4,5,6, 7,8,9 };
Yes, this had been discussed a while ago:
http://eigen.tuxfamily.org/bz/show_bug.cgi?id=954

We are not strictly "C++03 only" anymore.
The main issues are to agree on things like memory order (should this
always be row-major, like the Comma-initializer?), or how to resolve
ambiguous cases.
Also, how to compose more complex expressions from existing sub-matrices:

// how to mark that A21 starts a new row?
Eigen::MatrixXf A{ A11, A12, A21, A22};
Eigen::MatrixXf A{ {A11, A12}, {A21, A22}};


Christoph
--
Dr.-Ing. Christoph Hertzberg

Besuchsadresse der Nebengeschäftsstelle:
DFKI GmbH
Robotics Innovation Center
Robert-Hooke-Straße 5
28359 Bremen, Germany

Postadresse der Hauptgeschäftsstelle Standort Bremen:
DFKI GmbH
Robotics Innovation Center
Robert-Hooke-Straße 1
28359 Bremen, Germany

Tel.: +49 421 178 45-4021
Zentrale: +49 421 178 45-0
E-Mail: ***@dfki.de

Weitere Informationen: http://www.dfki.de/robotik
-----------------------------------------------------------------------
Deutsches Forschungszentrum fuer Kuenstliche Intelligenz GmbH
Firmensitz: Trippstadter Straße 122, D-67663 Kaiserslautern
Geschaeftsfuehrung: Prof. Dr. Dr. h.c. mult. Wolfgang Wahlster
(Vorsitzender) Dr. Walter Olthoff
Vorsitzender des Aufsichtsrats: Prof. Dr. h.c. Hans A. Aukes
Amtsgericht Kaiserslautern, HRB 2313
Sitz der Gesellschaft: Kaiserslautern (HRB 2313)
USt-Id.Nr.: DE 148646973
Steuernummer: 19/672/50006
-----------------------------------------------------------------------
Ola Røer Thorsen
2018-11-07 07:57:30 UTC
Permalink
Post by Christoph Hertzberg
GCC sometimes has trouble to fully optimize code inside the main
function -- simply renaming your function will give fully optimized code
(I'm not sure whether that is a known issue or a "feature" of GCC).
Also, once you sufficiently tested your code, you should compile with
`-DNDEBUG` to disable assertions.
https://godbolt.org/z/ujpwVU
Thanks a lot, Christoph! Yeah that makes a big difference - I'll be sure to
keep that in mind when playing with the compiler explorer, guess I was too
quick in my assumptions this time.

Best regards,
Ola

Loading...