Compiled application is very slow

I have a code that I need to compile into a stndalone application as I need to run it on multiple computers. The problem is that the compiled code runs twice as long as the code run directly from Matlab. I have read several questions but most of them mention the reason is the time needed to open/run the mcr.
RUNTIME:
  1. matlab - 1 hour
  2. compiled - 2 hours
The code is rather complex and contains: mex, parfor, objects, reading/writing to matfiles.
What causes the code to run so slowly?

4 个评论

Are you using MATLAB Compiler or MATLAB Coder? Did you use Coder to generate any of the mex files?
No chance left a DEBUG flag set on compilation, is there?
Or, is it on a machine that might be memory limited and causing disk-thrashing or virtual memory access?
Have you instrumented the app to find out where it might be spending most of its time?
Which OS operating on?
I have used Matlab Coder to generate mex files for the critical functions. Matlab Compiler was then used to generate a standalone application that is installed on an arbitrary PC.
The code uses a lot of memory and so occasional memory-disk issues are possible.
The problem is that on the same PC the compiled code is much slower than the code run directly in Matlab. I dont see any externalities that could be responsible for this.
I wonder if compiled code use static instead of dynamic memory allocation and so is max'ed out whereas the native version only uses/allocates memory as needed?
Need somebody w/ more knowledge of internals and has used the code generation toolset much more than I.

请先登录,再进行评论。

 采纳的回答

Peter
Peter 2022-6-8
I WAS WRONG - THERE IS NO SLOWDOWN!!!
After I repeatedly run the code it seems that the run times of the compiled code are comparable to the code run directly from MATLAB. Probably the problem was related only to the first run of the code. Anyway, thank you for the discussion as I was not aware of some of the characteristics of the Coder and Compiler.

更多回答(1 个)

The code generated by MATLAB Coder does not use the high-performance MKL or LAPACK or BLAS libraries, because Coder is often used for situations where those libraries are not available. You should expect performance only about what you would get running single-core.

8 个评论

Ok, but this means that the generated mex functions that are used in the code do not use these libraries in bot cases (run from matlab and compiled). The question is how does the Matlab Compiler (not Coder) hinders the performance of the program? Does Matlab Compiler use these libraries or does it run in parallel (Windows Task Manager says that it does).
MATLAB Compiler (not coder) pre-tokenizes all the .m that it figures are needed, and includes the tokenized versions in an archive.
In some cases it can prove that variables are a specific data type and only reference the methods for that data type instead of all methods with the same name (for example exp(x) with type of x not nailed down could potentially need to link the Control System Toolbox just-in-case x became a transfer function at run time.)
The tokenizing that is done is similar using pcode()
So MATLAB Compiler runs through the code, and builds a data structure of pre-tokenized code, and writes those into something similar to a zip file. Then it drops in a reference to a DLL and puts the whole thing into a self-extracting archive.
When executed, the self-extracting archive expands the file structure, and then checks to see whether the run-time version of the DLL is available, and if not then it downloads the run-time DLL. Then it calls into the DLL.
What is in the DLL? This: the same Execution Engine that is used for interactive MATLAB. Code compiled with MATLAB Compiler does not go through a different execution path: it goes through the same execution path. The same performance is expected once everything is initialized (or maybe slightly less due to a decryption layer.)
Objects compiled with MATLAB Compiler need the same host environment as interactive MATLAB, full operating system, can call upon native third-party libraries and so on. MATLAB Compiler is not designed as a performance enhancement technology: it is designed to bundle MATLAB Code into something that does not require a license to execute.
"... The same performance is expected once everything is initialized ..."
So we're still left trying to explain the 2X slowdown of OP's original Q?
They used MATLAB Coder to generate the mex, but Coder has a primary role of being able to generate for standalone and embedded systems and so does not generate multithreaded code.
This comments seems to be super usefull. So to summarize:
  1. MATLAB Coder that can be used to create mex functions (enhancing performance) only generate code that runs on a single core = parfor loops in the functions are ignored.
  2. MATLAB Compiler can be used to create standalone applications that can be run on computers without Matlab licence. You say that this should be without any computational cost but in my case the compiled code is 2x slower.
  3. Yair Altman in his book Accelerating Matlab Performanc writes that the mex functions can be used to enhance the performace (true for my code). However, he does not mention anything about Matlab Compiler slowing down the code.
What can I do if I want to preserve the performance of the code run in Matlab but I want the code to be run on PCs without Matlab? This is a critical question for me - even an answer that it is not possible is welcome.
Sorry, but I use regularly MATLAB compiler to deploy my codes. I never observe it run twice as slow as you describe. The speed IMH is pretty much the same. I use MEX, multthread MEX, and I use alsoo parrallel computing, in compiled stand alone app it is as fast as if it runs under MATLAB.
So I don't know what you are tallikng about until you can show a small example that candemonstrate the issue.
You were right. There is no slowdown. It was probably only related to the first run of the code.
What do you mean, 'first run of the code'? I have a simple standalone application. It takes less than 1 s to run in Matlab, and 11 s to run from Windows. I found this thread looking for info. If I run it muliple times from Windows, it still takes 11 s.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 MATLAB Coder 的更多信息

产品

版本

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by