Re: [eigen] Best way of initializing a matrix of fixed size

[ Thread Index | Date Index | More lists.tuxfamily.org/eigen Archives ]


Thanks!
Indeed, optimization level seems to have changed some results, as I don't see the comma initializers anymore, and the valgrind cycle count went down big time (by half).
Also changed the inits as suggested, this seems to have improved a small fraction of the cycle count.

Regards,

Matthieu

2017-03-09 11:40 GMT+00:00 Christoph Hertzberg <chtz@xxxxxxxxxxxxxxxxxxxxxxxx>:
On 2017-03-08 18:02, Matthieu Brucher wrote:
Well, the elements are not simple numbers, they are results of different
expressions like these:
https://github..com/mbrucher/AudioTK/blob/master/ATK/Preamplifier/Triode2Filter.cpp#L85

Some suggestions (no guarantee that any of them will work in your case):
* For a Vector4d (or Vector4f or anything smaller) you can also use the corresponding constructor:
    Vector F(f1,f2, g1,g2);
* If you use the comma-initializer don't initialize your matrix with Zero() before (your compiler may be smart enough to optimize it away, but that operation is not necessary).
* Sometimes it may be easier for the compiler if you initialize your matrix coefficient-wise (reduces readability though ...), i.e.,
    Matrix M; // don't zero-initialize unless most entries are 0
    M(0,0)= -(Ib_Vbe /* + ... */ );
    M(1,0) = M(0,1) = M(1,3) = M(3,1) = 0.0;
    M(2,1) = -( M(1,2) = (Ib_Vce + Ic_Vce));
    // ...

But as Gael said, make sure you compile with full optimization and check your assembly ...


Christoph





2017-03-08 13:56 GMT+00:00 Gael Guennebaud <gael.guennebaud@xxxxxxxxx>:

Strange, it usually compiles to trivial code, e.g.:

void foo(Matrix4f &m)
{
  m <<  1,  2,  3,  4,
        5,  6,  7,  8,
        9, 10, 11, 12,
       13, 14, 15, 16;
}

compiles to:

movaps LCPI0_0(%rip), %xmm0    ## xmm0 = [1065353216,1084227584,
1091567616,1095761920]
movups %xmm0, (%rdi)
movaps LCPI0_1(%rip), %xmm0    ## xmm0 = [1073741824,1086324736,
1092616192,1096810496]
movups %xmm0, 16(%rdi)
movaps LCPI0_2(%rip), %xmm0    ## xmm0 = [1077936128,1088421888,
1093664768,1097859072]
movups %xmm0, 32(%rdi)
movaps LCPI0_3(%rip), %xmm0    ## xmm0 = [1082130432,1090519040,
1094713344,1098907648]
movups %xmm0, 48(%rdi)
popq %rbp
retq

make sure you compiled with full optimization. If so, check the respective
assembly.

gael

On Tue, Mar 7, 2017 at 1:57 PM, Matthieu Brucher <
matthieu.brucher@xxxxxxxxx> wrote:

Hi,

I'm wondering what is the best way of initializing a 4x4 matrix. I'm
currently using the comma initializer, but it shows up quite high on a
valgrind profile compared to taking the inverse of the same matrix or
preprocessing the coeffs (pow, ln...).
I'm using Eigen 3.2.4, but I can upgrade.

Cheers,

Matthieu
--
Information System Engineer, Ph.D.
Blog: http://blog.audio-tk.com/
LinkedIn: http://www.linkedin.com/in/matthieubrucher






--
 Dipl. Inf., Dipl. Math. Christoph Hertzberg

 Universität Bremen
 FB 3 - Mathematik und Informatik
 AG Robotik
 Robert-Hooke-Straße 1
 28359 Bremen, Germany

 Zentrale: +49 421 178 45-6611

 Besuchsadresse der Nebengeschäftsstelle:
 Robert-Hooke-Straße 5
 28359 Bremen, Germany

 Tel.:    +49 421 178 45-4021
 Empfang: +49 421 178 45-6600
 Fax:     +49 421 178 45-4150
 E-Mail:  chtz@xxxxxxxxxxxxxxxxxxxxxxxx

 Weitere Informationen: http://www.informatik.uni-bremen.de/robotik





--
Information System Engineer, Ph.D.
Blog: http://blog.audio-tk.com/
LinkedIn: http://www.linkedin.com/in/matthieubrucher


Mail converted by MHonArc 2.6.19+ http://listengine.tuxfamily.org/