why calculation time become slow when I use mex file?
Ä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
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
am 13 Dez. 2017
Akzeptierte Antwort
Weitere Antworten (1)
James Tursa
am 12 Dez. 2017
1 Stimme
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.
Kategorien
Mehr zu C++ 用の MATLAB エンジン API finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!