Multithread Co-Simulation
This example shows how to run co-simulation on multiple threads.
Simulink® is an integration platform that supports co-simulation between components with local solvers or that involves simulation tools. For example, co-simulation can involve an S-function as a co-simulation gateway between Simulink and third-party tools or custom code. It can also involve an FMU in co-simulation mode imported to Simulink.
By default, Simulink configures all models to run on multiple threads with the MultiThreadCoSim
parameter.
This example shows how to run multithreaded co-simulation of three components. The Co-Simulation Component 1
and Co-Simulation Component 3
blocks are implemented in C-MEX S-Functions. The Co-Simulation Component 2
block is implemented using FMU Co-Simulation v2.0. These components are nondirect feedthrough, therefore they do not have data dependencies on each other. In other words, the outputs at time t
do not depend upon the inputs at time t
.
Multithreaded co-simulation best suits models with computationally intensive and loosely coupled components. In this example, each component computes prime numbers to find the maximum prime number that is smaller than or equal to the given input using a brute-force search. The intensity of the computation is directly proportional to the given range, which is a block input that you can experiment with. Also, components exchange a scalar signal, which is the maximum prime number within the given range, at communication times.
Experiment toggling the MultithreadedSim
parameter and measuring the wall clock time.
open_system('slexCoSimPrimeExample') sim('slexCoSimPrimeExample');