USING MEX TO SPEED UP THE CODE
50 ビュー (過去 30 日間)
古いコメントを表示
YOGENDRA SINGH BHANDARI
2020 年 8 月 1 日
コメント済み: YOGENDRA SINGH BHANDARI
2020 年 8 月 1 日
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
0 件のコメント
採用された回答
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
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 件のコメント
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 Center および File Exchange で MATLAB Compiler についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!