필터 지우기
필터 지우기

MEX-Function execution time issue in Simulink model

조회 수: 5 (최근 30일)
Milo
Milo 2024년 6월 7일
답변: Balavignesh 2024년 6월 17일
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:
  1. Why might the MEX function be performing slower than the MATLAB function within the Simulink environment?
  2. How does Simulink handle MEX functions during simulation and compilation?
  3. 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!

답변 (1개)

Balavignesh
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

카테고리

Help CenterFile Exchange에서 MATLAB Compiler에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by