How to make Matlab MEX application to be real time one on SMP general purpose multicore platform
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.
Answers (2)
Walter Roberson
on 13 Mar 2022
0 votes
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
Dusko Vujadinovic
on 13 Mar 2022
Edited: Dusko Vujadinovic
on 14 Mar 2022
Walter Roberson
on 15 Mar 2022
There are a small number of ways a program can become real time:
- it can be analyzed by a dedicated real-time system to try to figure out the critical paths, and be compiled entirely in terms of real-time library calls, to produce a program that runs on an operating system that is dedicated to real-time work, such as on speedgoat hardware -- not Linux or Windows or MacOS
- it can be analyzed by a compiler for a generate multitasking operating system, with the compiler linking against libraries of real-time extensions to the general multitasking operating -- that is, the multi-tasking operating system has to have real-time extensions designed into it; a small number of Linux versions have this, and some other operating systems do as well -- not Windows
- It can be built with regular tools for a multitasking operating system, with it being given privileges to run with dedicated resources at higher priority than anything else on the system, on an operating system that offers this as an option, such as SGI IRIX used to offer -- not Windows
Just linking against a threaded library does not make a program work in real time. Threaded libraries just mean that the program gets more efficient access to switch tasks when the operating system decides to schedule you.
For example when a real-time program asks the operating system to read from disk, and the hardware meets the minimum requirements, then a real-time operating system would guarantee that the data would be returned within a bounded amount of time (or be told that it does not exist.) Whereas a merely threaded program might have to wait 15 or 20 minutes while a Disk Defragmenter happened to run, or might have to wait several hours while the operating system swapped in and out a program that was using hard drives as virtual memory.
Dusko Vujadinovic
on 16 Mar 2022
Walter Roberson
on 16 Mar 2022
MATLAB Coder and Embedded Coder do not support any real-time systems. Simulink Real-time is the only Mathworks product that supports real-time systems, and that is entirely within the context of a real-time operating system.
Simulink Real-time is already designed to analyze Simulink blocks to group computations into tasks that can be run independently at different rates with bounded times and with methods already in place to control what should be done if a task exceeds its time allotment. Simulink Real-time already has methods to estimate time requirements of a task.
The Parallel Computing Toolbox has no methods to analyze time requirements or prioritize computations or deal with missed deadlines or deal with multiple rates. It cannot, for example, know anything about starting a computation on a processor but suspending it after a time interval to turn the processor over to dealing with a sample having become available, then resuming the computation — which is a typical operation for a real-time system as real-time systems pre-plan based upon knowledge of resource requirements.
The Parallel Computing Toolbox was never designed for real-time operations. The Parallel Computing Toolbox was designed for multi-core operations each with independent flow of control and low interactions.
Dusko Vujadinovic
on 17 Mar 2022
Walter Roberson
on 17 Mar 2022
Embedded is not the same as real-time.
I have access to the logs of an access door that can be remotely operated. The control system is an embedded system. Occasionally the logs it generates are not written for an hour or more: it may be embedded but that function is not real-time.
Dusko Vujadinovic
on 17 Mar 2022
Walter Roberson
on 17 Mar 2022
you want to officially declare
I believe you have overlooked the fact that I do not work for Mathworks, and so cannot make any kind of official declaration. I did, though, do some real-time work in the early 1980s.
Earlier you talked about generating MEX, but you also talk about Embedded Coder. Embedded Coder is for embedded systems, which either have no operating system or have some kind of custom or real-time operating system.
But "A MEX function is a MATLAB executable." -- that is, it is to be called from inside a full MATLAB session, or from inside something built with MATLAB Compiler (and so is using MCR to provide the execution engine.) MATLAB itself and MATLAB Compiler are restricted to generating code for use on systems that MATLAB itself can execute on -- which is to say Linux, MacOS, or Windows.
"The embedded coder has ben advertised that it offers the SIMD instructions set for number of processor platforms - which is to speed up the code, so in my view, that is primarily real time accelerator on high scale, isn't it?"
NO!
Suppose you have a set of cooks (processors), and you have a series of meals to make, some due "as soon as practical" (get the appetizers in front of the people quickly), some due in groups (entres for a table should ideally all be ready at the same time), and some due according to schedules (catering business.)
The Parallel Computing Toolbox just gets told the total number of meals to make, and is not given any information about deadlines or how long each one takes to make, and is responsible for handing out work to each chef. If it hands chefs a list of items to prepare, the chef finishes everything on the list before going back for more work... regardless of whether the meals were ladles of soup, or making an entire roast hog. The same chef might happen to get assigned the roast hog and the all-day spaghetti-sauce, and the Parallel Computing toolbox doesn't know or care, and does not offer any way for the chefs to say "Hold on, give this to someone else, I'm going to be awhile on what I am doing." PCT itself doesn't care. Each individual chef cooks as quickly as they have resources for, but there is no inherent coordination or attempt to allocate resources to meet deadlines.
A real-time system, on the other hand, receives the deadlines and cooking time information, and is responsible for figuring out how to make everything work. It might be given more tasks over time, and it is responsible for looking through the work it knows it has to do and either accepting the task (and then ensuring it gets finished on time), or else rejecting it as not feasible. A real-time system does not try to make each chef continuously busy: it cares about the outcomes, about the meals being ready on time... and will, if necessary, interrupt a chef to do something higher priority.
The two situations are not the same.
Which one do you need? Well, you mentioned a wireless modem. On a wireless modem, anything to do with bit timing is Real-Time work. And there are hard-deadlines for some kinds of responses, after which the protocol might assume that your node is dead (or the link is blocked.) But if you are using something like TCP protocol, you are more often dealing with soft deadlines, where the consequence for not replying in time can be that the other node re-sends the packet. If your time to create a response and have it received by the other node is greater than the retry interval then you run the risk of having the reply work pile up... but it is still more towards the side of "soft real time".
But... if you want to maximize bandwidth, then you start getting back towards "hard real-time" work...
Dusko Vujadinovic
on 18 Mar 2022
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.
Dusko Vujadinovic
on 19 Mar 2022
0 votes
Categories
Find more on Multicore Processor Targets 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!