user4512098
user4512098

Reputation:

LAPACK dgesv VS MATLAB mldivide

I'm writing finite difference method program.
I'm using Intel Math Kernel Library.

For example, 1000x1000 matrix A and 1000x1000 matrix B.
In Intel MKL, A*B using cblas_dgemm() function took about 600 ms.
In MATLAB, A*B took about 800 ms.
I thought MKL is very fast.

However, 1000x1000 matrix A and 1000x1 vector B,
and A\B (mldivide) in MATLAB, it took 40 ms,
but in MKL, using LAPACKE_dgesv, it took 400 ms!

so my question is,
Why in mldivide, MATLAB is so fast and MKL is so slow?

matrix A and vector B is whole filled with random values.

I'm using
MATLAB R2012b
Visual Studio 2015
Intel Parallel Studio XE 2016 Update 3 Cluster Edition

Thank you.

EDITED

first, C++ code.

#include "mkl.h"
#include "time.h"

int n = 1000;
double *a = (double *)malloc(sizeof(double) * n * n);
for(int i = 0;i < n * n;i++) a[i] = rand();
double *b = (double *)malloc(sizeof(double) * n);
for(int i = 0;i < n;i++) b[i] = rand();
int *ipiv = (int *)malloc(sizeof(int) * n);
time_t now = clock();
int info = LAPACKE_dgesv(LAPACK_ROW_MAJOR,n,1,a,n,ipiv,b,1);
time_t ms = clock() - now;
printf("%d ms",ms);

Second, MATLAB code.

n = 1000;
a = rand(n,n);
b = rand(n,1);
tic;
c = a\b;
toc * 1000

I think I didn't mistake in measuring time.
Thank you.

Upvotes: 1

Views: 909

Answers (1)

kangshiyin
kangshiyin

Reputation: 9779

You have a problem on timing. Both MKL and MATLAB show warm-up effect on this operation. If you time MKL twice in your C code, you will find the second is much faster as

247.349940 ms
14.353588 ms

For Matlab, it is

ans =

  825.5090

ans =

   21.7870

Please note the results also highly depends on your multi-thread settings.

The difference is that unless you quit Matlab and start it again, you won't see the warm-up effect again, as Matlab holds the resources created in the warm-up stage until you quit. If you run the Matlab script again, the resources will be reused. But for MKL, if you time only once, you always time the solving with warm-up phase, quit the c program and release the resources without reusing them.

Warm-up is not a Matlab-specific phenomenon. The resources could be pre-allocated reusable memory buffers for solving the equations in MKL's LAPACK, which affects both the C program and Matlab script.

Upvotes: 2

Related Questions