USING MEX TO SPEED UP THE CODE

36 次查看(过去 30 天)
I have a matlab code which is computationally intensive and takes more than affordable time to run. I am told mex can be of help. So I have tried converting the code into its C equivalent using Matlab Coder, but am not sure how exactly to compile that in matlab now.
Please help. Thanks in advance

采纳的回答

John D'Errico
John D'Errico 2020-8-1
编辑:John D'Errico 2020-8-1
As a counterpoint, there are some issues to consider. Much of MATLAB is already compiled code, at least in the hard working parts. And what is not already compiled code is interpreted fairly efficiently. They have spent a LOT of effort to make that efficient, but there are some places where you may see gains. The result is from what has been stated here before, sometimes a gain, but sometimes even a loss in speed. Compiled code that is automatically generated need not be the most efficient code.
I see from some comments of your that your code involves multiple matrix inverses. Remember this is already something that is done by code that has been pretty heavily optimized, and it is already compiled. So if that is where your time is being spent, you will see essentially no gain from just an automatic compilation.
The net is, while you may see some gains if any, they are not going to be orders of magnitude improvements. And it may be a net loss. But if it is too slow for you, spending this effort for a 10-20% gain is arguably the wrong approach.
So what can you do for a real gain?
One option is to write your C from scratch. You need to know what you are doing, to really understand the algorithms, and to understand C. You can gain here by avoiding function call overhead, avoiding data checks, passing data more efficiently, etc.
The alternative is to work in MATLAB. And here is where you can start to make some real gains, if you can use tools like the profile tool to determine where the bottlenecks are in your code, and how to reduce/avoid them.
One approach that can sometimes be a gain is to use the parallel computing toolbox. This can be of use if the code is not already automatically using multiple cores on your machine. MATLAB often does this on bogger problems, when possible.
Best can be the algorithmic improvements. Here you can indeed find various algorithms that can offer orders of magnitude improvements. But this requires fully rethinking how you are soving the problem.

更多回答(1 个)

Sriram Tadavarty
Sriram Tadavarty 2020-8-1
Hi Yogendra,
Once you generated the mex (MATLAB executable) using MATLAB Coder. Then, observe that there will be a file generated with extensions '_mex'. Use this file to run instead of the actual file, and observe the time taken.
For example, for a function foo, there will be a generated mex file with name foo_mex. Use foo_mex in the same way, the foo is used.
Hope this helps.
Regards,
Sriram
  2 个评论
YOGENDRA SINGH BHANDARI
Thanks Sriram for answering. So in the testbench I replaced the original function with original function_mex. But there seems almost no speedup. I am not sure if I am doing things right.
Original Function(6s)
Mex Version(7s)
Sriram Tadavarty
Sriram Tadavarty 2020-8-1
Hi Yogendra,
Thanks for sharing the results. I am actually not sure as what inbuilt MATLAB functions are used in R2RMODEL3.
There could be sometimes, even the mex would take longer time, than native MATLAB code. It depends on the functions that are used. For more information about mex being slower is answered here.
Please have a look and see if it was helpful.
Regards,
Sriram

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by