Discussion:
[eigen] Incorrect result when inverting 4x4 Matrix with gcc7.3 and -ffast-math
Michael Mößner
2018-02-28 10:41:02 UTC
Permalink
Hello

I get incorrect results, when I invert a 4x4 matrix with gcc7.3 and the
compiler option -ffast-math. Unfortunatly, I don't get access to the bug
tracker, so I post it here.

Example code: (compile with "g++ test.cpp -O3 -ffast-math")

#include <Eigen/Dense>
#include <iostream>
using namespace std;

int main(int argc, char **argv)
{
    Eigen::Matrix<double,4,4> test;
    test(0,0) = 1.; test(0,1) = 0.; test(0,2) = 0.; test(0,3) = 0.;
    test(1,0) = 0.; test(1,1) = 1.; test(1,2) = 0.; test(1,3) = 0.;
    test(2,0) = 0.; test(2,1) = 0.; test(2,2) = 1.; test(2,3) = 0.;
    test(3,0) = 0.; test(3,1) = 0.; test(3,2) = 0.; test(3,3) = 1.;

    cerr << test.inverse() << endl;
    return 1;
}

------------------------------

This results in

1  0  0  0
-0 -1 -0 -0
 0  0  1  0
-0 -0 -0 -1

------------------------------

The expected result is

1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1

---------------------------------

I also tested this code with gcc4.8.5, gcc5.2 and clang6.0.0 where the
problem does NOT occur.

I also tested with gcc7.2 where the problem occurs.

The problem does NOT occur when ommitting the -ffast-math or when
compiling with -O0

My Eigen version:

3.3.4

My system data:

SUSE Linux Enterprise Desktop 12 SP2

Intel(R) Xeon(R) CPU E3-1276 v3 @ 3.60GHz

---------------------------

I also tested the following systems where the bug also occurs:

----------------------------

SUSE Linux Enterprise Desktop 11 SP2

Intel(R) Xeon(R) CPU E3-1276 v3 @ 3.60GHz

------------------------------

SUSE Linux Enterprise Desktop 12 SP2

Intel(R) Xeon(R) CPU E3-1230 @ 3.20GHz

------------------------------

Best regards

Michael
Dan Čermák
2018-02-28 11:50:43 UTC
Permalink
Hi Michael,

I am not an Eigen developer, so I cannot give you a definitive answer,
but afaik -ffast-math enables the compiler to aggressively reorder
floating point operation to increase performance. That comes at the cost
of loosing IEEE compliance. I have myself seen numerical code break
spectacularly with -ffast-math because it relies the floating point
operations being precise.

So I my guess is that you are seeing a new optimization that gcc enables
backfiring and if anything is at fault, then maybe gcc.


Cheers,

Dan
Post by Michael Mößner
Hello
I get incorrect results, when I invert a 4x4 matrix with gcc7.3 and the
compiler option -ffast-math. Unfortunatly, I don't get access to the bug
tracker, so I post it here.
Example code: (compile with "g++ test.cpp -O3 -ffast-math")
#include <Eigen/Dense>
#include <iostream>
using namespace std;
int main(int argc, char **argv)
{
    Eigen::Matrix<double,4,4> test;
    test(0,0) = 1.; test(0,1) = 0.; test(0,2) = 0.; test(0,3) = 0.;
    test(1,0) = 0.; test(1,1) = 1.; test(1,2) = 0.; test(1,3) = 0.;
    test(2,0) = 0.; test(2,1) = 0.; test(2,2) = 1.; test(2,3) = 0.;
    test(3,0) = 0.; test(3,1) = 0.; test(3,2) = 0.; test(3,3) = 1.;
    cerr << test.inverse() << endl;
    return 1;
}
------------------------------
This results in
1  0  0  0
-0 -1 -0 -0
 0  0  1  0
-0 -0 -0 -1
------------------------------
The expected result is
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
---------------------------------
I also tested this code with gcc4.8.5, gcc5.2 and clang6.0.0 where the
problem does NOT occur.
I also tested with gcc7.2 where the problem occurs.
The problem does NOT occur when ommitting the -ffast-math or when
compiling with -O0
3.3.4
SUSE Linux Enterprise Desktop 12 SP2
---------------------------
----------------------------
SUSE Linux Enterprise Desktop 11 SP2
------------------------------
SUSE Linux Enterprise Desktop 12 SP2
------------------------------
Best regards
Michael
Patrik Huber
2018-02-28 13:31:25 UTC
Permalink
Hi Michael,

You could test whether this has anything to do with SSE/AVX. So you could
play around with a few -march=... flags - for example "core2" for SSE3
only, or "native" for "all your CPU has got". You can also selectively add
e.g. -msse4.2 or -mavx.
I don't think it has anything to do with your "problem" with -ffast-math
but I incidentially found some problems with >=gcc-6 as well this month (
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84280).

I would also encourage you to report this on the gcc bugtracker to let them
decide whether this is a bug or not. You can send an email to register (the
email address should be on https://gcc.gnu.org/bugzilla/). If your email
gets rejected for spam reasons (happened to me when I registered...) let me
know and I can (indirectly) help.

Best regards,

Patrik
Post by Dan Čermák
Hi Michael,
I am not an Eigen developer, so I cannot give you a definitive answer,
but afaik -ffast-math enables the compiler to aggressively reorder
floating point operation to increase performance. That comes at the cost
of loosing IEEE compliance. I have myself seen numerical code break
spectacularly with -ffast-math because it relies the floating point
operations being precise.
So I my guess is that you are seeing a new optimization that gcc enables
backfiring and if anything is at fault, then maybe gcc.
Cheers,
Dan
Post by Michael Mößner
Hello
I get incorrect results, when I invert a 4x4 matrix with gcc7.3 and the
compiler option -ffast-math. Unfortunatly, I don't get access to the bug
tracker, so I post it here.
Example code: (compile with "g++ test.cpp -O3 -ffast-math")
#include <Eigen/Dense>
#include <iostream>
using namespace std;
int main(int argc, char **argv)
{
Eigen::Matrix<double,4,4> test;
test(0,0) = 1.; test(0,1) = 0.; test(0,2) = 0.; test(0,3) = 0.;
test(1,0) = 0.; test(1,1) = 1.; test(1,2) = 0.; test(1,3) = 0.;
test(2,0) = 0.; test(2,1) = 0.; test(2,2) = 1.; test(2,3) = 0.;
test(3,0) = 0.; test(3,1) = 0.; test(3,2) = 0.; test(3,3) = 1.;
cerr << test.inverse() << endl;
return 1;
}
------------------------------
This results in
1 0 0 0
-0 -1 -0 -0
0 0 1 0
-0 -0 -0 -1
------------------------------
The expected result is
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
---------------------------------
I also tested this code with gcc4.8.5, gcc5.2 and clang6.0.0 where the
problem does NOT occur.
I also tested with gcc7.2 where the problem occurs.
The problem does NOT occur when ommitting the -ffast-math or when
compiling with -O0
3.3.4
SUSE Linux Enterprise Desktop 12 SP2
---------------------------
----------------------------
SUSE Linux Enterprise Desktop 11 SP2
------------------------------
SUSE Linux Enterprise Desktop 12 SP2
------------------------------
Best regards
Michael
--
Dr. Patrik Huber
Centre for Vision, Speech and Signal Processing
University of Surrey
Guildford, Surrey GU2 7XH
United Kingdom

Web: www.patrikhuber.ch
Mobile: +44 (0)7482 633 934
Loading...