0
votes

I'm currently trying to convert matlab code to C++ using armadillo. I converted some matlab code by following the aramdillo documentation to C++. However the performance is disappointing compared to matlab.

In Matlab it takes about 0.1 sec to inverse a Matrix A of size (625x625) compared to over 3 seconds in C++.

In C++ I have tried both

solve()

as well as

inv() 

I'm aware of the fact that inv produces less accurate results, thus I do not prefer to use it. Besides I really need the inverse of matrix A, as I use the diagonal elements later in the algorithm.

The code that's producing these results:

Matlab

x=A\b
invA = A\eye(size(A))

C++

arma::mat x = solve(A,b)
arma::mat invA = solve(A,eye(625,625))

The versions I'm using:

C++:

  • Visual Studio 2013

  • Armadillo 8.300.1

  • Intel MKL 2018.1.156

Matlab:

  • matlab 2016b

  • version -blas

Intel(R) Math Kernel Library Version 11.3.1 Product Build 20151021 for Intel(R) 64 architecture applications, CNR branch AVX2

  • version -lapack

Intel(R) Math Kernel Library Version 11.3.1 Product Build 20151021 for Intel(R) 64 architecture applications, CNR branch AVX2 Linear Algebra PACKage Version 3.5.0

Does anyone have an idea how to overcome this lack of speed in C++ using armadillo?

1
Have you turned on optimizations? You could try eigen instead. If that doesn't help you will need to dig deeper into the generated code and see what it does that matlab doesn't do. But you can expect operations like matrix inverse to be very well optimized in matlab, so I wouldn't expect great performance improvements. You can also try a better compiler. VS2013 is lacking by today's standards. You could try g++ instead. - nwp
Do you actually require the inverse? If you're solving Ax=y then there are much faster ways than a brute-force inversion, depending on the structure of your matrix. - virgesmith
In Visual studio make sure you chose release option when comparing benchmarks, rather than debug option - Mikhail Genkin

1 Answers

0
votes

Have you tried : add to your code #ARMA_USE_LAPACK ? This allows your code to use more optimized version of .inv() function. Also check the documentation: http://arma.sourceforge.net/docs.html