I'm recently learning how to use mex files. The case that I try to reproduce is from https://www.youtube.com/watch?v=TAKjSi-77Ns&list=PLcgIaTuuWp3kWI8d1C1wZDl0SfQDxm-CK&index=61. I tried to verify that mexw64 file genrated by coder runs faster than .m file.To my surprise, I followed the the lesson step by step, but the result was inconsistent. Here is the function:
function Sum = SUModd(N)
%#codegen
Sum=0; count=1;
while ne(count,N)
if ne(mod(count,2),0)
Sum=Sum+count;
else
Sum=Sum;
end
count=count+1;
end
end
Then I generated mexw64 file following the instructions, compared the execution time between the .m file and the .mexw64 file:
%% test the consuption time
clear all;
N=1e8;
t = cputime;
SUModd(N)
Tmat = cputime-t;
fprintf('M-file comp. Time is %5.5f sec \n',Tmat);
%% mex function generated from coder
T_mex = cputime;
SUModd_mex(N)
TT = cputime-T_mex;
fprintf('Mex-file comp. Time is %5.5f sec \n',TT);
the result confused me because mexw64 cost more time than pure matlab file which is inconsistent with video:
M-file comp. Time is 9.71875 sec
Mex-file comp. Time is 9.75000 sec
To find out what go wrong,I rewrite the SUModd function in C, and mex it:
#include "mex.h"
double summation(double N) {
// double N=1e8;
double sum = 0;
int cout = 1;
while (cout != N) {
if ((cout % 2) != 0) {
sum = sum + (double)cout;
}
else {
sum = sum;
}
cout = cout + 1;
// printf("%d \n",cout);
}
return sum;
}
void mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
{
double x,result;
if (nrhs != 1)
mexErrMsgTxt("Wrong number of input arguments.\n");
// 检查输入变量数量是否正确,否则报错
if (nlhs > 1)
mexErrMsgTxt("Too many output argumnents.\n");
x = mxGetScalar(prhs[0]);
// result = summation(x);
double *finaldata;
plhs[0] = mxCreateDoubleMatrix(1,1,mxREAL);
finaldata = mxGetPr(plhs[0]);
finaldata[0] = summation(x);
}
the time consuption was just 0.12500 sec which was consistent with the video.
So my question is: Why the mexw64 file generated by coder didn't improve the computational efficiency? Or Did I missed some details when I ws using coder?

 採用された回答

David Fink
David Fink 2022 年 9 月 4 日

0 投票

The C code generated by MATLAB Coder can be found in the codegen/mex/SUModd/ folder, so you can compare that against the handwritten version.
Various configuration options affect the generated code and C compiler options, including IntegrityChecks, ResponsivenessChecks, EnableDebugging, EnableMexProfiling, etc. See the full list of options on the coder.MexCodeConfig page:
If the generated code is missing an optimization opportunity, please report this to MathWorks Technical Support:
In general, measuring the performance of generated code will be more accurate by using "tic" and "toc" calls inside the function, since this won't include the overhead of passing input and output data between MATLAB and the generated code. However, in this case (double -> double), that overhead should be tiny.

10 件のコメント

zhou caiwei
zhou caiwei 2022 年 9 月 5 日
編集済み: zhou caiwei 2022 年 9 月 5 日
I translated .m file into .c using matlab coder. It only takes about 2 seconds excuting the C code. I also want to compare the time consumption between the related mexw64 file and C file. How could I build mexw64 file from such a C++ project(with many C++ independent function files included)?
David Fink
David Fink 2022 年 9 月 5 日
Based on the original question, it seems like you already know how to compare performance of the MEX vs the handwritten C code.
For building a MEX file from handwritten C++ files, the mex function accepts C++ files as input:
Paul
Paul 2022 年 9 月 5 日
IMO, the overhead of passing ipnput/output data should be part of the timing test, unless the purpose of the timing test is determine if all of the time is being spent in passing that data. But as you noted, that overhead should be tiny in this example. Regardless, the time it takes to run the generated code seems extreme.
It may be helpful to edit your question or add a comment showing the exact command used to generate the .mexw64 file from the .m file and the exact mex coommand used to create the .mexw64 file from the handwritten C code so that others can copy/paste and try to replicate and explore the results.
zhou caiwei
zhou caiwei 2022 年 9 月 7 日
sorry, what I means was how to mex multiple C\C++ files. The example I listed just contains single C function. If I have multiple C functions files , how should I mex them into one mexw64?
Bruno Luong
Bruno Luong 2022 年 9 月 7 日
Just put them in the same mex command
mex mainfile.cpp file2.cpp file3.cpp
zhou caiwei
zhou caiwei 2022 年 9 月 14 日
thank you!
zhou caiwei
zhou caiwei 2022 年 9 月 14 日
編集済み: zhou caiwei 2022 年 9 月 14 日
This is the folder generated by coder. It contains all the source code and related resource files.
What I'm curious about is what is the purpose to generate the files in this folder.The files inside the interface folder are:
What is the role of the files in this interface? I'm guessing these files can be used to generate mexw64 files, but how to use them?
Matan Silver
Matan Silver 2022 年 9 月 14 日
Hello Zhou,
For DLL and LIB code generation, the "interface" folder is needed by MATLAB Coder for SIL/PIL support. You should avoid using the code in the "interface" folder directly. Instead, use SUModd.h and other headers in the top level directly to call the entrypoint directly.
Hope that helps,
Matan
zhou caiwei
zhou caiwei 2022 年 9 月 15 日
Alright, Is there a generic mexFunction template or built-in function to compile the converted C\C++ code into mex files?
zhou caiwei
zhou caiwei 2022 年 9 月 15 日
Because somehow the mex file directly converted from the matlab file is very inefficient.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で MATLAB Coder についてさらに検索

製品

リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by