user3705273
user3705273

Reputation: 335

How to speed up this C++ program with eigen library against matlab?

I want to use C++ for big linear algebra computation. As a starting step, these comparison programs I created in C++ and matlab. I am also giving astonishing execution time here. Can you suggest way to beat matlab or atleast get comparable performance? I know that C++ uses highly vectorized methods for computations. So in large scientific programming involving linear algebra, should one always go for matlab instead of C++? I personally think that matlab doesn't give good performance for large computations therefore C++ is preferred to matlab in such cases. However my program results go contrary to this belief.

C++ program compiled with gcc:

#include <iostream>
#include <Eigen\Dense> //EIGEN library

using namespace Eigen;
using namespace std;

int main()
{
MatrixXd A;
A.setRandom(1000, 1000);

MatrixXd B;
B.setRandom(1000, 1000);

MatrixXd C;
C=A*B;
}

Execution time: 24.141 s

Here is matlab program:

function [  ] = Trial(  )
clear all;
close all;
clc;

tic;
A=rand([1000,1000]);
B=rand([1000,1000]);
C=A*B;
toc

end

Elapsed time is 0.073883 seconds.

Upvotes: 3

Views: 5781

Answers (3)

ggael
ggael

Reputation: 29205

To get the best of Eigen, compile with optimizations ON (e.g. -O3 compiler flag), with OpenMP enabled (e.g., -fopenmp), and disable hyper-threading or specify to openmp the true number of physical cores (e.g., export OMP_NUM_THREADS=4 if you have 8 hyper-threaded "cores", but 4 physical cores).

Finally, you might also consider using the devel branch and enable AVX (e.g., -mavx) and FMA if your CPU does support FMA (e.g., -mfma).

Upvotes: 5

meneldal
meneldal

Reputation: 1737

Actually Matlab (if you don't buy the expensive parallel computing toolbox) hardly use multi-threading. It's only used in the libraries called by Matlab which are probably more efficient that what you're using now.

You can check this link to understand (and check) what libraries your Matlab uses http://undocumentedmatlab.com/blog/math-libraries-version-info-upgrade

It's also possible to use them in your C program (though they might have hidden the headers or something, at least you still have the .dll since they need that to run Matlab)

Upvotes: 0

vsoftco
vsoftco

Reputation: 56547

It is extremely hard to beat MATLAB, even with all optimizations turned on. To get the most out of Eigen you need to compile with parallel support (-fopenmp in gcc), and turn optimizations on (-O3). Even in this case, MATLAB will be slightly faster, mainly because it is using the Intel MKL proprietary library to get the most out of Intel chips, so unless you buy it I don't think you will be able to beat it. I am currently using Eigen for a project and wasn't able to beat MATLAB (at least not for dense matrix multiplication).

For example, for A*B where A and B are 1000 x 1000 complex matrices, the best average time I can get is:

MATLAB: 0.32 seconds Eigen: 0.44 seconds

For 2000 x 2000,

MATLAB: 2.80 seconds Eigen: 3.45 seconds

System: MacbookPro 2013, OS X.

PS: you should make absolutely sure that you turn optimizations on (-O3) and also compile with parallel support, -fopenmp. This is the reason you're probably getting this huge difference in running time. So you should compile your program as:

g++ -O3 -fopenmp <other compiling flags/parameters> main.cpp

Upvotes: 5

Related Questions