- 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"?
why calculation time become slow when I use mex file?
6 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Jan
am 12 Dez. 2017
Bearbeitet: Jan
am 12 Dez. 2017
I do not understand the explanations.
Akzeptierte Antwort
Jan
am 12 Dez. 2017
Bearbeitet: Jan
am 12 Dez. 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 Kommentare
Jan
am 13 Dez. 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:
- A matrix vector multiplication in Matlab
- A C-mex function containing a naive implementation of this mutliplication
- 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.
Weitere Antworten (1)
James Tursa
am 12 Dez. 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.
0 Kommentare
Siehe auch
Kategorien
Mehr zu C++ 用の MATLAB エンジン API finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!