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

Comparison of Eigen matrix multiplication with MATLAB

Tags: None
(comma "," separated)
parmeet
Registered Member
Posts
9
Karma
0
OS
Hi All,

I am using Eigen for building C++ library on co-clustering algorithms. I am comparing performance with MATLAB codes, and my C++ library is about 10 times faster on small matrices. But as the data size increases the time consumption becomes comparable, worst is my library is slower for even larger size matrix data.

I Figured out that performance is mainly depending on speed of matrix multiplication and MATLAB is faster than Eigen when matrix size is big. Here is exemplar timing for multiplication of two square matrices of size 5000x5000

Eigen(with optimization -O3) : 21.19 seconds
Matlab(multi-threading disabled, also result is not cached meaning it is actually evaluated): 10.66 seconds

Please kindly let me know if it is possible that Eigen is optimizing matrix multiplication for small size matrices only (as also indicated by Benchmark http://eigen.tuxfamily.org/index.php?title=Benchmark where comparison only upto 1000 is shown). If not, please let me know if i am missing something..

Many thanks. I am looking forward for replies..

Sincerely,
Parmeet
manuels
Registered Member
Posts
47
Karma
0
are you using matrices with static or dynamic size in Eigen?
parmeet
Registered Member
Posts
9
Karma
0
OS
manuels
Registered Member
Posts
47
Karma
0
Can you post your code please?
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
the benchmark goes up to 3000 (log scale !). MatLab is actually using MKL for the matrix multiplication, so you actually benchmarking Eigen versus MKL. The speedup factor 2 is probably because you have a recent CPU with AVX instructions (exploited by MKL) while currently Eigen only exploits SSE. AVX supports should come within a few months.
parmeet
Registered Member
Posts
9
Karma
0
OS
Thanks for the comments..Yes indeed it is 3000! But our aim is to tackle much larger matrices (and potentially get big gain in speed by shifting from MATLAB to C++).. Is it possible that we might loose some optimization in matrix multiplication using Eigen when dealing with large size dynamic matrices as compared to MATLAB ?

Here is rather naive code to measure time consumption of matrix multiplication using static matrices:

Code: Select all
#include <iostream>
# include <time.h>
#include "eigen3.0.3/Eigen/Dense"
using namespace std;

int main()
{
    Eigen::MatrixXd eM1 = Eigen::MatrixXd::Random(5000,5000);
    Eigen::MatrixXd eM2 = Eigen::MatrixXd::Random(5000,5000);
    Eigen::MatrixXd eM3(5000,5000);
    double start=clock();
    eM3=eM1*eM2;
    double t1=(double(clock())-start)/CLOCKS_PER_SEC;
    start=clock();
    cout<<t1<<"\n";
}                           ;
User avatar
ggael
Moderator
Posts
3447
Karma
19
OS
MAtLab is extremely slow when dealing with small matrices because of the overhead induced by each call, etc. MatLab is also not very good for component-wise operations. Matlab is also limited to double while float can be twice as fast. However, thanks to the use of MKL, MatLab very good when dealing with large matrices for matrix products, decompositions or linear solving.


Bookmarks



Who is online

Registered users: Bing [Bot], claydoh, Google [Bot], rblackwell, Yahoo [Bot]