How to make Matlab MEX application to be real time one on SMP general purpose multicore platform

5 次查看(过去 30 天)
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:
  1. How that parallelism get transferred to the MEX application
  2. 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.

回答(2 个)

Walter Roberson
Walter Roberson 2022-3-13
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 个评论
Dusko Vujadinovic
Dusko Vujadinovic 2022-3-18
I appreciate your time and the conversation, even more if you do not work for Mathworks....
You actually say that the code generated by the Embedded Coder cannot be used in MEX application??? That is very odd restriction in my view and I do not understand where that restriction comes from?
I am aware that SIMD is not a magic concept but it relies on having specific processor architecture that support multiple registers to be able to run multiple data in single instruction cycle. In my example, we have actually a non embedded SMP Intel processor which supports the SIMD AVX instruction set. So that fact that SIMD is supported as part of Embedded Coder only, does not seem it matters? It matters that we have processor that supports the SIMD AVX instruction set which our non embedded platform supports. The embedded coder should not have any dependencies to (real time) operating system as you implicate here:
"Embedded Coder is for embedded systems, which either have no operating system or have some kind of custom or real-time operating system.".
It should care only about specific processor architecture - irrespective being embedded or not as the Matlab Coder does. Just Matlab Coder provides the code that is portable to any general purpose processor whereas the Embedded Coder need to vectorise code for specific processor architecture that supports some of SIMD concepts.
"You can generate single instruction, multiple data (SIMD) code from certain MATLAB functions by using Intel SSE and, if you have Embedded Coder®, Intel AVX technology. SIMD is a computing paradigm in which a single instruction processes multiple data. "
So again I logically cannot see why the Embedded Coder would not be possible for me to use. I would appreciate if you could provide inside to the restrictions that still might exist.
- I have been in wireless modem area and hence hard real time for almost 22 years, so I know very well what hard real time is, difference between embedded and real time, how the scheduling works etc. As far as I can see the discussion about that, just adds to noise to our main discussion - where I am trying to simply get an answer - is it possible to use the Embedded Coder:
1. To produce the code that can me MEX-ed on laptop machine that supports SIMD AVX instruction set - on which I run Matlab.
2. To produce the code that can be run on Intel industrial PC platform that supports SIMD AVX instruction set.
- Please ignore also the Parallel Computing Toolbox - I realised already that I cannot get anything useful from there that I need. I do not think that the requirement there were right. So, again less noise helps to get to the bottom of what I need.
- Also the wireless modem I talk about is not a real modem by modem application run on very powerful general purpose SMP platform - I handle that part well - I do not tnink it is relevant for my questions.
Thanks a lot for your time Walter
Mikhail
Mikhail 2022-3-19
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
Dusko Vujadinovic 2022-3-19
Hi Mikhail,
Thanks for trying to help.
I should had narrowed the scope of the question - to prevent that the discussion goes to different directions.
The MEX is absolutely central for what I want. To reiterate, to speed up the Matlab code execution when testing it, since it is very slow (I do not think my code is not efficient though - simply it is too much to do) I need to use MEXing.
Hence it is absolutely essential that I run everything from Matlab environment (forget about the target) . I develop the code in Matlab but when I want to run the regression tests, I run them by running the MEXed code rather than Matlab code itself.
The MEX is the way that I can glue the C code produced by the Matlab Coder that should have been potentially faster than the Matlab code. However that is only true for the control code. For DSP code the bottleneck is that the generated code is not vectorised, so as consequence the MEX application is even a bit slower than when running Matlab code.
One way is to write that code on my own in C and put it to be part as Code Replacement Library as you suggested. However, I do not want to waste my time on that. I would rather use the Embedded Coder to vectorise the code for my host PC that supports SIMD AVX instruction set.
According my understanding that would radically speed up my MEX application. The real time I mentioned here is just an ideal stretch goal, taking into account that the Matlab code that runs the MEX files is really light - but I should not have mentioned that - it just moved the discussion to another direction and made it difficult to follow. So simply ingore any real time.
As I have mentioned - I am aware that I cannot speed up the code by using the Parallel Computing Toolbox - I have to do someting on my own - so should not have mentioned it in first place. However, I did not expect that the Embdeed coder cannot produce the code that can be MEX-ed as Walter suggested - so my original focus was to Parallel Computing Toolbox.
But the question now is very simple.
Can I use code generated from the Embedded Coder to be run as part of MEX application?
Maybe I should open new question stated as simple as that and close this one.

类别

Help CenterFile Exchange 中查找有关 Simulink Coder 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by