I'm starting with c++ atm and want to work with matrices and speed up things in general. Worked with Python+Numpy+OpenBLAS before. Thought c++ + Eigen + MKL might be faster or at least not slower.
My c++ code:
#define EIGEN_USE_MKL_ALL
#include <iostream>
#include <Eigen/Dense>
#include <Eigen/LU>
#include <chrono>
using namespace std;
using namespace Eigen;
int main()
{
int n = Eigen::nbThreads( );
cout << "#Threads: " << n << endl;
uint16_t size = 4000;
MatrixXd a = MatrixXd::Random(size,size);
clock_t start = clock ();
PartialPivLU<MatrixXd> lu = PartialPivLU<MatrixXd>(a);
float timeElapsed = double( clock() - start ) / CLOCKS_PER_SEC;
cout << "Elasped time is " << timeElapsed << " seconds." << endl ;
}
My Python code:
import numpy as np
from time import time
from scipy import linalg as la
size = 4000
A = np.random.random((size, size))
t = time()
LU, piv = la.lu_factor(A)
print(time()-t)
My timings:
C++ 2.4s
Python 1.2s
Why is c++ slower than Python?
I am compiling c++ using:
g++ main.cpp -o main -lopenblas -O3 -fopenmp -DMKL_LP64 -I/usr/local/include/mkl/include
MKL is definiely working: If I disable it the running time is around 13s.
I also tried C++ + OpenBLAS which gives me around 2.4s as well.
Any ideas why C++ and Eigen are slower than numpy/scipy?