Tasking Modes and Execution Order
You can simulate and execute code generated from this simple multirate model, which is configured to use a fixed-step solver, in single-tasking or multitasking mode.
The tasking mode that gets applied depends on the setting of the model configuration parameter Treat each discrete rate as a separate task. When cleared, the parameter specifies single-tasking mode. When selected, the parameter specifies multitasking mode.
The execution order of the blocks (indicated in the upper right corner of each block) is forced into the order shown by assigning higher priorities 0:1, 0:0, and 0:2 to blocks F, E, and D, respectively. This ordering is one possible valid execution ordering for this model. For more information, see Simulation Phases in Dynamic Systems.
The execution order is determined by data dependencies between blocks. In a real-time system, the execution order determines the order in which blocks execute within a given time interval or task. This topic treats the model execution order as a given, because it is concerned with the allocation of block computations to tasks, and the scheduling of task execution.
Single-Tasking Execution
When a model is configured for single-tasking execution and you select model configuration parameter Block reduction, the Simulink® engine optimizes the model by removing fast-to-slow Rate Transition blocks from the model. For this example, Block reduction is selected. Therefore, block B does not appear in the table or timing diagram below. For more information, see Block reduction.
This table shows the execution order, sample time, and whether a block has output and update computations. Block A does not have an update computation because it does not have discrete states.
Blocks | Sample Time | Output Computation | Update Computation |
---|---|---|---|
E | 0.1 | Y | Y |
F | 0.1 | Y | Y |
D | 1 | Y | Y |
A | 0.1 | Y | N |
C | 1 | Y | Y |
Note
The timing diagrams in this topic are based on the assumption that the Rate Transition blocks are used in the default (protected) mode, with block parameters Ensure data integrity during data transfer and Ensure deterministic data transfer (maximum delay) selected.
Real-Time Single-Tasking Execution
This figure shows the scheduling of computations when the generated code is deployed in a real-time system in single-tasking mode. The generated program runs in real time under control of interrupts from a 10 Hz timer.
At times 0.0, 1.0, and every second thereafter, the slow and fast blocks execute output computations followed by update computations for blocks that have states. Within a given time interval, output and update computations are sequenced in block execution order.
The fast blocks execute on every tick, at intervals of 0.1 second. Output computations are followed by update computations.
The system spends some portion of each time interval (labeled “wait”) idling. During the intervals when only the fast blocks execute, a larger portion of the interval is spent idling. This illustrates an inherent inefficiency of single-tasking mode.
Simulated Single-Tasking Execution
This figure shows the execution of the model during a Simulink simulation loop.
Because time is simulated, the placement of ticks represents the iterations of the simulation loop. Blocks execute in the same order as in the previous figure, but without the constraint of a real-time clock. Idle time is not present between simulated sample periods.
Multitasking Execution
When this model is configured for multitasking, block computations are executed under two tasks, prioritized by rate:
The slower task, which has lower priority, is scheduled to run once every second. This is called the 1-second task.
The faster task, which has higher priority, is scheduled to run 10 times per second. This is called the 0.1-second task. The 0.1-second task can preempt the 1-second task.
The following tables show the block execution order for the 0.1-second task and 1-second task and whether a block has output and update computations.
This table shows the execution order for the task for blocks that use a 0.1-second sample time.
Blocks | Output Computation | Update Computation |
---|---|---|
E | Y | Y |
F | Y | Y |
D | Y | Y |
A | Y | N |
B | Y | N |
This table shows the execution order for the task for blocks that use a 1-second sample time.
Blocks | Output Computation | Update Computation |
---|---|---|
D | Y | Y |
B | Y | N |
C | Y | Y |
Blocks A and B do not have discrete states, and so they do not have update computations. Rate Transition blocks D and B appear in both tables. They use port-based sample times. For Rate Transition block D, the output computation runs at the output port sample time under the 0.1-second task. The update computation runs at the input port sample time under the 1-second task. For Rate Transition block B, the output computation runs at the output port sample time under the 0.1-second task. For more information on port-based sample times, see Referenced Model Sample Times.
Real-Time Multitasking Execution
This figure shows the scheduling of computations when the generated code is deployed in a real-time system in multitasking mode. The generated program is shown running in real time as two tasks under control of interrupts from a 10 Hz timer.
Simulated Multitasking Execution
This figure shows a simulation of the model in multitasking mode. The Simulink engine runs the blocks in one thread of execution, simulating multitasking. Preemption does not occur.