Search code examples
eigen

Eigen, get inverse failed, and inverse() is so slow


I want to get inverse matrix, so here is the code:

if (m.rows() == m.fullPivLu().rank())
{
    res = m.inverse();
}

The dimension of m and res are all 5000 times 5000. And When I run the code on a high performance computing machine(Linux, Tianhe 2 SuperComputer), the process was kill at res = m.inverse();, and there was no core file or dump information generated. The console return killed and the process exited.

But there is nothing wrong on my Ubuntu laptop.

And the inverse()'s performance is poor and it costs a lot time.

So, why inverse() was killed on the high performance machine? Thank you!


Solution

  • Full-pivoting LU is known to be very slow, regardless of its implementation.

    Better use PartialPivLU, which benefits from high performance matrix-matrix operations. Then to get the best of Eigen, use the 3.3-beta2 release and compile with both FMA (-mfma) and OpenMP (e.g., -fopenmp) supports, and don't forget to enable compiler optimizations -O3. This operation should not take more than a few seconds.

    Finally, do you really need to explicitly compute the inverse? If you only apply it to some vectors or matrices (i.e., A^-1 * B or B * A^-1) then better apply the inverse in factorized form rather than explicitly computing it. With Eigen 3.3:

    MatrixXd A = ...;
    PartialPivLU<MatrixXd> lu(A);
    x = lu.inverse() * b; // solve Ax=b, same as x = lu.solve(b);
    x = b * lu.inverse(); // solve xA=b
    

    In these expressions, the inverse is not explicitly computed!