Assign Tasks to Cores for Multicore Programming - MATLAB & Simulink (original) (raw)

This example shows how to take advantage of executing code on a multicore processor by graphical partitioning. This example requires Simulink® Coder™ software to generate multithreaded code.

Introduction

Multicore programming allows you to use the processing power of modern multicore processors to create high-performance applications. Simulink® software allows you to take advantage of multicore programming by allowing you to graphically partition your algorithms and to assign the code generated from those partitions to parallel threads on your multicore processor.

The parallel threads are typically scheduled by an operating system that assigns threads to cores dynamically. This allows the scheduler to make good use of the cores while providing a notion of fairness. However, in many applications, you may need additional control on how the cores are used or you may want to dedicate a set of cores to specific logic, for example to improve determinism. Graphical partitioning provides this level of control, as illustrated in this example.

Open the Multicore Project

A typical multicore scenario involves a multi-rate model for which you want to assign code for the fast rates to one core and the code for the slower rates to other cores. This may allow you to add more logic to these faster rates or to achieve more determinism. More generally, you may want to dedicate cores to application logic with the greatest demand on resources.

To begin, open the project:

openProject("slexMulticoreExample");

slexMulticoreExample model with task icons above each model reference block and areas indicating which blocks are assigned to Core0 and Core1.

Partition Model into Tasks

The slexMulticoreExample model is already partitioned into four concurrent tasks. To view the tasks and mapping for the model, open the Concurrent Execution tool.

  1. On the Setup tab, click Model Settings.
  2. From the Solvers pane, click Configure Tasks.
  3. From the left pane of the Concurrent Execution tool, click Tasks and Mapping.

The Tasks and Mapping view shows each block in the model, which node and task is assigned to the block, and whether the trigger is periodic or aperiodic. For example, the Fcn1 block is assigned to the Core1 node and mapped to the Task1 periodic trigger.

Tasks and Mapping view of the Concurrent Execution tool opened for the slexMulticoreExample model.

For more information on explicit partitioning, see Partition Your Model Using Explicit Partitioning.

Generate Multithreaded Code

Double-click the Generate Code and Profile Report button to generate multi-threaded code and profile its execution. This action runs and instruments the generated executable and produces both profiling results and a core occupancy map. The core occupancy map shows how the cores were used at each time step of execution. In this example, the first core was used for the fast rates (Task1 and Task2) and the second core was used for the slower rates (Task3 and Task4).

You can return to the Concurrent Execution tool to try different core assignments. In particular, you can try to specify no core affinity by leaving the Affinity property as []. This is the default setting which allows Simulink to generate multi-threaded code suitable for dynamic scheduling on your multicore processor.

slexMulticoreExampleProfile.png

See Also

Concurrent Execution | Simulink.architecture.config | Simulink.architecture.add | Simulink.architecture.profile

Topics