0

I'm using eigen to do some matrix operation, but the compile time for the src files using eigen is very slow, by slow I mean it take about 40s when the file is only 300 lines. I only use Matrix smaller than Matrix4f and not even use dynamic size matrix, for only some matrix multiplication and matrix decomposition(SVD and FullPivLU).

In my other project where a cpp file is 1000 lines, it take minutes to compile, and the output .so file is really big, about 100M. I have to open the -bigobj choice.

This happens both in debug(where I set the opimization to -O0) and release(-O3) mode. I've tried add #define NDEBUG and EIGEN_NO_DEBUG in the header, not help.

I write this piece of very small code test.cpp below:

#include </home/user/mywork/software/eigen-3.3.9/Eigen/Dense>
#include "iostream"
using namespace std;
using namespace Eigen;

void test() 
{
    Matrix3f A;
    A << 1,2,3,4,2,8,5,4,9;
    BDCSVD<Matrix3f> svd(A, ComputeFullU | ComputeFullV);
    Matrix3f U = svd.matrixU();
    Matrix3f V = svd.matrixV();
    cout << "------Eigen------" << endl;
    cout << "A" << A << endl;
    cout << "U" << U << endl;
    cout << "V" << V << endl;
}

int main()
{
    test();
    return 0;
}

compile command

g++ test.cpp -o test_eigen

it take 20s to compile, and the output 'test_eigen' is 5.6M!!!

my os is ubuntu 16.04 and I use cmake, in the CMakelist, only thing related to eigen is include its directory. eigen version is 3.3.9.

does anyone have any clue? Thanks a lot.

phoebe.mu
  • 21
  • 4
  • 1
    Did the compiler output any warnings? Unexpected behavior like this should produce some kind of log. – Sorevan May 14 '21 at 06:45
  • @Sorevan no, I add -Wall, all other warnings in code not concern Eigen is output very quickly, then is a long pause without any ouput before the compilation of this cpp ends. One clue is the output .so file is very big when I compile my alogrithm into a library. – phoebe.mu May 14 '21 at 07:18
  • To reduce the binary size (and the runtime), you should at least compile with `-Og` or `-O1`, better with `-O2`. Also `BDCSVD` will fall down to `JacobiSVD` for small sizes, so if you know already that your sizes are not larger than 4, just use the latter. – chtz May 14 '21 at 08:46
  • @chtz thanks, I tried your recommendation, the compile time did compress a lot and the binary size is reduced to 250k. Does this size normal for such code? – phoebe.mu May 14 '21 at 08:53
  • A size of 250k sounds ok. If that is a concern, you can [strip](https://stackoverflow.com/questions/1413171/) your binary or compile with `-s`. – chtz May 14 '21 at 09:04
  • @chtz but in my project the compile timeof a file still takes 30s, do you think this is normal? I only use two JacobiSVD and FullPivLU, other than that are all simple matrix operations. – phoebe.mu May 14 '21 at 09:21

1 Answers1

1

Eigen consists of 7.8MB of pure header files. So any .cpp including it will have a hard life compiling (uses giga-bytes of memory and extremely slow). In addition, due to extensive use of templates and inline functions, the DEBUG version will be so slow and unusable for debug purpose. (For example, google's Cartographer will warn about debug version) and I also made some tests.

So I think Eigen should be used as follow:

  1. Encapsulate Eigen. Create simple interface according to your own needs. (For example, create matrix_4x4.h, vector4.h, standard_deviation_calculaor.h, etc.) Include Eigen only in .cpp files. The exposed .h files will be so simple and will not propagate to slow down compiling of other files.

  2. Proper Modularization. Precompile the library into static/shared libraries. And use the simple headers in other parts of your program.

  3. Restrain Use eigen only in performance-critical parts of my program. For example, the following function will not use eigen and should be inlined(exposed in header):

    // vector3.h
    inline Vector3 operator + (const Vector3& l, const Vector3& r) {
       return Vector3(l.x + r.x, l.y + r.y, l.z + r.z);
    }
    

    But Matrix4x4::multiplyInplace(const Matrix4x4& r) will be implemented in .cpp and use Eigen.

Zhaolin Feng
  • 428
  • 4
  • 8