-1

Edit: I found that the MATLAB processor time is about the same as the C++ processor time, but still can't find a way to help the wall clock time of the C++ program. (About the same as the processor time printed below)

This might be an obvious result, but I've found that for determining eigenvalues and eigenvectors of a very large matrix (2k x 2k) matlab is about 3-4 times faster than LAPACK used in C++.

The following matlab code:

tic
A = 0:(2000*2000-1);
A = mod(A,4273);
A = reshape(A,[2000,2000]);
[U,D,W] = eig(A);
toc

runs in 8.3 seconds on my machine whereas the following c++ code:

#include <iostream>
#include <fstream>
#include <time.h>

using namespace std;

// dgeev_ is a symbol in the LAPACK library files
extern "C" {
extern int dgeev_(char*,char*,int*,double*,int*,double*, double*, double*, int*, double*, int*, double*, int*, int*);
}

int main(){
    clock_t t;
    int n,m;
    n = 2000;
    m = 2000;
    double *data;
    data = new double[n*m];
    for(int i = 0; i < n*m; i++){
        data[i] = i % 4273;
    }   

    char Nchar='V';
    double *eigReal=new double[n];
    double *eigImag=new double[n];
    double *vl=new double[n*m];
    double *vr=new double[n*m];
    int ld=n;
    int lwork=6*n;
    double *work=new double[lwork];
    int info;

    //print_array(data,n,m);
    // calculate eigenvalues using the DGEEV subroutine
    cout << "starting" << endl;
    t = clock();
    dgeev_(&Nchar,&Nchar,&n,data,&n,eigReal,eigImag,
           vl,&ld,vr,&ld,
           work,&lwork,&info);
    t = clock() - t;
    cout << "done" << endl;
    cout << "time taken: " << ((float)t)/CLOCKS_PER_SEC << endl;

    delete [] data;
    delete [] eigReal;
    delete [] eigImag;
    delete [] work;

    return 0;
}

requires 29.05 seconds on my machine.

Is it possible that MATLAB is just taking advantage of my hardware better?

Cory Nezin
  • 1,551
  • 10
  • 22
  • This is not a proper benchmark. The numbers you get are completely meaningless. – Henri Menke Apr 23 '18 at 00:22
  • How can I make it better? – Cory Nezin Apr 23 '18 at 00:34
  • Determine the speed in terms of FLOPS, not wallclock time. Make sure the cache is hot. Ideally, use a proper benchmarking library like [Google Benchmark](https://github.com/google/benchmark). Also make sure that your benchmark is actually computing something meaningful. Nobody cares whether one code is faster than the other if the output is actually garbage. – Henri Menke Apr 23 '18 at 00:38
  • Also, MATLAB is probably using a parallel implementation of LAPACK, like OpenBLAS. Try linking your C++ code against OpenBLAS instead of normal LAPACK. – Henri Menke Apr 23 '18 at 00:39
  • Please have a look here: https://stackoverflow.com/questions/7596612/benchmarking-python-vs-c-using-blas-and-numpy?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa – Kaveh Vahedipour Apr 24 '18 at 14:18

1 Answers1

1

No Matlab is not faster than lapack, such comparison is misguided because they are completely different things.

Matlab depends on lapack (or one of the different implementations) for core matrix operations. Most likely your Matlab installation uses intel's MKL which has been meticulously optimised down to the cache sizes of the CPU.

In your C++ implementation, you seem to be calling lapack routines directly. This will be several orders of magnitudes slower than optimised implementations.

Checkout openblas and ATLAS for implementations more commonly used in high performance computing.

Xero Smith
  • 1,968
  • 1
  • 14
  • 19