Clear Filters
Clear Filters

why calculation time become slow when I use mex file?

42 views (last 30 days)
When I use mex file on matlab, calculation time become too slow. Although c-compiler is faster than matlab. Why this situation was occurred?
Let's show data of the situation.
here is the calculation time of matrix-vector product(dimension is 16000 and 32000). my PC is Macbook (Intel Core i7 2.5GHz, 16GB memory, Mac OS High Sierra version 10.13.1)
Matlab using mex file
  • dimension, time
  • 16000, 6.7009 sec
  • 32000, 30.9207 sec
#include "mex.h"
/* The computational routine */
void MatPro(double *x, double *y, double *z, mwSize m, mwSize n)
{
int i;
int j;
/* main program */
for(i=0;i<m;i++){
z[i]=0;
for(j=0;j<m;j++){
z[i]=z[i]+x[m*j+i]*y[j];
//z[i]=fma(z[i],x[m*j+i],y[j]);
}
}
}
/* The gateway function */
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
double *in1; /* input scalar */
mwSize n; /* size of matrix */
mwSize m;
double *in2;
double *out; /* output matrix */
/* get the value of the input */
in1 =mxGetPr(prhs[0]);
/* create a pointer to the real data in the input matrix */
in2 =mxGetPr(prhs[1]);
/* get dimensions of the input matrix */
m = (mwSize)mxGetM(prhs[1]); //GYO
n = (mwSize)mxGetN(prhs[1]); //RETU
/* get dimensions of the input matrix */
/* create the output matrix */
plhs[0] = mxCreateDoubleMatrix(m,n,mxREAL);
/* get a pointer to the real data in the output matrix */
out = mxGetPr(plhs[0]);
/* call the computational routine */
MatPro(in1,in2,out,m,n);
}
This is c file to calculate matrix-vector product using mex file.
On the other hand, Matlab
  • dimension, time
  • 16000, 2.1798 sec
  • 32000, 8.898 sec
a=rand(16000)*rand(16000,1);
Furthermore, when I use c-compiler like
clang matrix_vecter_product_file.c
the calculation time is
  • dimension, time
  • 16000, 0.93364 sec
  • 32000, 3.67412 sec
According to the data, we can see c-compiler is much faster than matlab, matlab using mex file is too slow comparing to the others.
I think matlab using mex file should be faster than matlab because of the time of c-compiler and matlab.
However, the time of matrix-vecor product on matlab using mex file is too slow. Why is that?
  2 Comments
Jan
Jan on 12 Dec 2017
Edited: Jan on 12 Dec 2017
I do not understand the explanations.
  • What does "c-compiler is faster than matlab" mean? A C-compiler and Matlab are programs, which do not have a speed.
  • "matrix-vector product(dimension is 16000 and 32000)" Do you mean a [16'000 x 16'000] matrix and [16'000 x 1] vector?
  • "According to the data, we can see c-compiler is much faster than matlab, matlab using mex file is too slow comparing to the others." What are "the others"? I cannot find out, if this sentence mean that the C-mex function is faster or slower than the Matlab function.
  • a=rand(16000)*rand(16000,1) looks like you include the time for creating the random arrays. Please post the code you use for the measuring of the time. Then the readers do not have to guess the details.
  • "I think matlab using mex file should be faster than matlab because of the time of c-compiler and matlab." This is not clear. What exactly is "the time of the c-compiler"?
h
h on 13 Dec 2017
  • I mean C-compiler is the time of matrix-vector multiple using c-compiler.
  • Yes
  • The others mean matlab and c-compiler.C-compiler mean the c-file of matrix-vector multiple. I run the file on command line tool such as iterm and terminal.
  • Sorry. I want to tell you how do I write the code multile on matlab.
  • Comparing to the result of matrix-vector multiple on matlab and command line, c-compiler (command line ) is much faster than matlab. Therefore, I think the multiple using c-mex also fast because c-mex is compiled by c-compiler same as command line. However, the result of c-mex is too slow...

Sign in to comment.

Accepted Answer

Jan
Jan on 12 Dec 2017
Edited: Jan on 12 Dec 2017
I do not understand the question completely. Do you mean, that the C-mex function is slower or faster than a matrix-vector multiplication in Matlab?
Your MatPro() function is the naive implementation of the matrix-vector multiplication. It is expected to be remarkably slower than the optimized BLAS libraries, which are called by Matlab. Calling a Mex function has an additional overhead.
Please repeat the measurement:
M = rand(16000, 16000);
x = rand(16000, 1);
tic;
for k = 1:1e3
y = M * x;
end
toc
tic;
for k = 1:1e3
y = matrix_vecter_product_file(M, x); % Do you mean "vect*o*r"?
end
toc
What do you see? Note that the timeit command would be even more accurate.
  2 Comments
h
h on 13 Dec 2017
I want to know “Calling a Mex function has an additional overhead” more clearly.
My question is why too slow mex function is. I showed you how fast the result of c-compiler (command line). According to the data, I think c-mex function is more fast because it compiled by c-compiler.
Jan
Jan on 13 Dec 2017
If Matlab calls a Mex function, the transport of the variables as inputs and outputs takes some time. You can check this by creating a Mex function which does nothing and call it 1 million times. This overhead cannot be avoided by writing efficient C code.
I get a slight impression of what you are asking for: You compare 3 different things:
  1. A matrix vector multiplication in Matlab
  2. A C-mex function containing a naive implementation of this mutliplication
  3. A C function containing the naive implementation as standalone executable
Now it matters how you compare the speed exactly: How are the input data created and provided? Do you overwrite the output or create new arrays in each iteration? Which methods do you use to measure the runtimes? Which compiler flags are activated for the compilation? E.g. SSE or AVX code?
These details have a large influence on the runtime. As long as you do not explain, what exactly happens, a discussions will remain more or less arbitrary.

Sign in to comment.

More Answers (1)

James Tursa
James Tursa on 12 Dec 2017
For linear algebra calculations, such as matrix*vector products, MATLAB actually calls a 3rd party highly optimized multi-threaded BLAS library to do the work in the background. This library is very fast. You have no chance to beat that code for timing with your simple hand-written code.

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!