Search code examples
multithreadingsimulink

Is compiled Simulink-Simulation multithreaded?


Does the compiled (with simulink-coder-toolbox) simulink-model run with multiple threads or just with one thread/process? As far as I know, the simulation is a single process, if you do not have the parallel toolbox, but what about multithreading?

I am curious how simulink handles different stepsizes for simulation time in one model? For example, if there are 2 parallel paths in a model with different stepsizes (1 x complex work with 0.1s steptime and 100 x light work with 0.001s steptime), do these paths run one after the other or somehow in parallel fashion with threads to save execution time?


Solution

  • The Simulink Coder generates pretty plain vanilla C code, and by default compiles it as such. There's no inherent multi-threading, or parallelism going on in the code itself.

    Different sample rates in the model are given given task id's, and each step through the code will execute the code associated with the currently executing id. Tasks can also be split into different files allowing easier multitasking execution when deployed on an RTOS.

    How the multiple tasks execute is largely dependent on the target OS and the compilation process. If you're compiling to a shared library, or an exe, deployed on a non-real time OS (e.g. Windows) then you're not getting any multitasking. If you have an RTOS, have generated the code in an appropriate way, and compile appropriately then you will have multitasking.

    There is a discussion of how this works in the doc: Model Single-Core, Multitasking Platform Execution

    You have access to the code, and access to the build file (and can modify both should you wish.) The easiest way to see what is going on is to look at that code.