How to make Matlab MEX application to be real time one on SMP general purpose multicore platform
2 views (last 30 days)
Show older comments
Background:
Developing, for example, a wireless modem is a complex multi skill know-how set. It starts from creating algorithms simulation model, where Matlab is often used as a tool. The ultimate target is to create a chip that functionally does all as the simulation, but it is small peace of HW, it does the job quicker and consumes less power.
There are a number of ways to get that end goal. For one of them, there is a strong requirement to do and to test that simulation model in real time, on non-necessarily embedded platform, in first iteration, but on some general purpose multicore SMP platform. Only after making the simulation model to work in real time on that kind non embedded platform –we look in details what is the ideal embedded platform required to fit the application. This kind of process have multiple advantages. It allows the algorithm people, who do not necessarily have real time embedded systems profile, to build the real time model relatively quickly and to give quick answer to number of most important questions. Basically the 1st stage provides high level of confidence that the development process is on the right track that major unknows have been answered, so the 2nd stage is a steady low risk path.
Matlab already provides some pathway towards this process of development:
- The Coder allows building MEX application where generally slow Matlab control code gets compiled in much faster C/C++ application.
- The Embedded Coder supports SIMD instruction set so that the DSP code that gets vectorized on Matlab, gets also vectorized in MEX application, since the basic Coder itself does not generate SIMD code
- The Fixed Point Designer allows Fixed Point arithmetic that is common requirement for an embedded platform.
However, the area where I do not clearly see direction is how to make the MEX application as multithreading/multicore application, which is precondition that the application can run on real time. Only if we use multicores in optimal way we can achieve to process the natural incoming data rate in real time.
The Parallel Computing Toolbox offers a number of mechanism to use the parallelism (https://uk.mathworks.com/help/parallel-computing/choosing-a-parallel-computing-solution.html). But I cannot find:
- How that parallelism get transferred to the MEX application
- How the MEX application gets transferred to multicore platform
For No.1, I see that Matlab offers generation of MEX application to multithreads: https://uk.mathworks.com/help/dsp/ref/dspunfold.html.
However, I am not sure how much is that helpful, since if I do the architecture of the application, I need to control that rather than letting Matlab to do it under the hood. Hence, I expect that I need to break the application to multiple MEX files by specifying the functions I need to create the MEX files for.
But No.2 is the main question mark.
The scheduling mechanism https://uk.mathworks.com/help/parallel-computing/how-parallel-computing-products-run-a-job.html#f3-6782, seems the closest match, but not sure that the scheduling is the right thing for the real time application. The real time application threads, that are supposed to be entry to the Scheduler, need to have assigned strict processing deadlines that the scheduler is obligated to meet, but looking into the Job function interfaces, I cannot see that the processing deadline is passed to the Scheduler. It is not clear how the Scheduler breaks the Jobs to the Tasks.
- So, my conclusion is that the Parallel Toolbox Scheduler is sort of “Fair Scheduler” rather than real time scheduler? In other words, the Parallel Toolbox is not designed for real time applications?
- The question is also - are the Job interface functions supported by the Coder (Embedded Coder) in first place? The aim is to run the MEX application in real time (I do not expect the Matlab code, as scripting code, can be run quick enough for real time – at least I do not see that my code is close to that).
Please is there any way to run Matlab is real time on multicore powerful SMP platform?
Ideal solution, is my view, would be to support so called “Processor Affinity” like Linux does where we can chose which thread to get statically mapped to which core, but I have not seen that something like that is supported.
I see there is an option to replace Matlab Scheduler with 3rd party scheduler https://uk.mathworks.com/help/matlab-parallel-server/integrate-matlab-with-third-party-schedulers.html, but that looks complicated operation to do, requires reinstallation of Matlab. But surely since Matlab knows that is installed on a Linux machine for example, it should be possible adding API that wraps directly Linux OS part related to scheduling and simple enable flag to use it. Hence, using Linux Scheduler rather than Matlab own scheduler would be a dynamic easy option. That could enable the path to use Matlab in real time?
Anyway, there could be already some solution that would allow running Matlab (MEX) application in real time that I could not find - so I would apreciate some input please.
0 Comments
Answers (2)
Walter Roberson
on 13 Mar 2022
Mathwork's only real-time product is Simulink Real-TIme (which is designed to run on Speedgoat hardware.)
MATLAB itself, and Parallel Computing Toolbox in particular are not designed for real-time.
10 Comments
Mikhail
on 19 Mar 2022
Hi Dusko,
Let me put it simple for you.
For your use case - forget about Parallel Computing Toolbox, forget about MEX. They are not relevant (I could explain more, but this would just burst into another lengthy comment).
You need to use Embedded Coder, probably with Code Replacement (read about it in docs). What you need then is to take generated code and integrate into your environment. Be it real-time or not, it only depends on you and on your provided (hand-coded) main harness/scheduler etc.
See Also
Categories
Find more on Simulink Coder in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!