This forum has been archived. All content is frozen. Please use KDE Discuss instead.

mat.inverse() is slow

Tags: None
(comma "," separated)
nelsons
Registered Member
Posts
23
Karma
0

mat.inverse() is slow

Tue Mar 07, 2017 7:26 pm
Hi,

Eigen::Map<Eigen::MatrixXd> matA((double*)A.getDataPointer(), (Eigen::Index)A.getDimensions().getRows(), (Eigen::Index)A.getDimensions().getColumns());
Eigen::Map<Eigen::MatrixXd> matR((double*)R.getDataPointer(), (Eigen::Index)R.getDimensions().getRows(), (Eigen::Index)R.getDimensions().getColumns());
tic();
matR = matA.inverse();
toc();

On my pc (VS 2015 release) with a matrix 1000x1000
Elapsed time is 0.590442 seconds

With a pure lapack code based on DLANGE, DGETRF, DGECON, DGETRI with same matrix 1000x1000
Elapsed time is 0.378 seconds.

Maybe, inverse could use lapacke ?

Thanks for your great work about eigen
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: mat.inverse() is slow

Tue Mar 07, 2017 10:30 pm
The problem might come from your compiler or compiling options. Here, using either gcc or clang, it takes 0.1s on a 2.6GHz i7 (haswell) and Eigen 3.3. If I disable AVX/FMA, it takes, as expected, 0.25s. So unless your computer is 10 years old, 0.59s seems indeed too slow.

Code: Select all
#include <iostream>
#include <Eigen/Dense>
#include <bench/BenchTimer.h>
using namespace Eigen;
using namespace std;

EIGEN_DONT_INLINE
void foo(const MatrixXd & A, MatrixXd & B)
{
  B = A.lu().matrixLU();
}

int main()
{
  int tries = 2;
  int rep = 1;
  int n = 1000;
  MatrixXd A(n,n), B(n,n);
  A.setRandom();
  B.setRandom();
  BenchTimer t;
  BENCH(t, tries, rep, foo(A,B));
  std::cout << "Time: " << t.best() << "s" << std::endl;
}


compiled with:
Code: Select all
clang++  -march=native -O3 -I ../eigen3.3 bench_inv.cpp && ./a.out



Moreover, do you really need to explicitly compute the inverse? It is usually much faster and more accurate to only factorize the matrix and call solve to apply the inverse:
Code: Select all
PartialPivLU<MatrixXd> lu(A);
v = lu.solve(u); // <-->  v = A^-1 * u
nelsons
Registered Member
Posts
23
Karma
0

Re: mat.inverse() is slow

Wed Mar 08, 2017 6:36 pm
Hi,

I tested this on Windows with a core i7 4 cores 8 threads @ 1.87Ghz

Compiled with VS 2015 /GS /GL /W3 /Gy /Gd /Oi /MD /openmp

I will try with intel compilers to see if there is a better bench.

Thanks
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS

Re: mat.inverse() is slow

Thu Mar 09, 2017 8:21 am
You're lacking /O2 I think.
nelsons
Registered Member
Posts
23
Karma
0

Re: mat.inverse() is slow

Sat Mar 11, 2017 5:28 pm
ggael wrote:You're lacking /O2 I think.


Thanks I checked on dual boot linux and bench are really better
It is a problem of flags on VS project

Thanks


Bookmarks



Who is online

Registered users: abc72656, Bing [Bot], daret, Google [Bot], Sogou [Bot], Yahoo [Bot]