- MEX functions: https://www.mathworks.com/help/matlab/call-mex-functions.html
- Troubleshooting MEX API Incompatibilities: https://www.mathworks.com/help/matlab/matlab_external/troubleshooting-mex-api-incompatibilities.html
MEX-Function execution time issue in Simulink model
3 次查看(过去 30 天)
显示 更早的评论
I am working on integrating a MEX function into a Simulink model to protect the source code of an algorithm from direct visibility. To achieve this, I converted the original MATLAB function into a MEX function, which I then call within a MATLAB Function block using the coder.extrinsic command.
During my testing, I've observed that the MEX function executes at each simulation step and is significantly slower compared to the original MATLAB function, resulting in a much higher overall simulation time.
I have several questions and would appreciate insights from the community:
- Why might the MEX function be performing slower than the MATLAB function within the Simulink environment?
- How does Simulink handle MEX functions during simulation and compilation?
- What are the best practices or correct methods for integrating a MEX function into a Simulink model to ensure optimal performance?
Any detailed explanations or guidance on troubleshooting and improving the performance of MEX functions in Simulink would be highly appreciated. Thank you!
0 个评论
回答(2 个)
Balavignesh
2024-6-17
Hi Milo,
Integrating MEX functions into a Simulink model to encapsulate the source code of an algorithm is a common approach for protecting intellectual property and enhancing performance. While I would need access to your model and MEX functions to fully reproduce the issue on my side, I can offer a general explanation for your questions.
1.) The performance of a MEX function heavily depends on the quality of its implementation in C/C++. If the algorithm isn't well-optimized for performance in C/C++, or if it doesn't leverage efficient memory management and computational techniques, it can run slower than MATLAB's highly optimized built-in functions. Each call to an external function like a MEX function can introduce overhead, especially if the function is called frequently (e.g., at every simulation step). This overhead can accumulate, leading to slower performance compared to native MATLAB functions optimized for vectorized operations.
2.) During simulation, Simulink invokes the MEX function at the specified simulation steps. This process includes transitioning from Simulink's simulation environment to the external MEX environment, which can add overhead. When compiling a model, Simulink needs to ensure that the MEX function is compatible with the simulation's code generation requirements. This doesn't usually affect performance but is crucial for ensuring the MEX function can be called correctly.
3.) To ensure optimal performance when incorporating MEX functions into Simulink, make sure the MEX function is as efficient as possible. Use efficient algorithms, memory management, and computational methods. Profiling your code can help identify bottlenecks. If possible, reduce the frequency of calls to the MEX function. For instance, consider calling the function less frequently by using a slower sample time for the block that encapsulates the MEX call.
Kindly have a look at the following documentation link to have more information on:
Hope that helps!
Balavignesh
0 个评论
Reid Spence
2024-8-1
From the doc for coder.extrinsic:
Extrinsic function calls can affect performance, because the code generator copies the data that you pass to an extrinsic function and sends the data to MATLAB for execution. Conversely, MATLAB copies any output data for transfer back into the MEX function environment.
Basically there is a heavy overhead for extrinsic calls. Plus using coder.extrinsic will prevent you from generating standalone code from this model.
Consider other ways to protect IP, including protected model:
Or instead compile your source code into a static or dynamic library and call that from the MATLAB Function Block
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!