Main Content

Assign Tasks to Cores for Multicore Programming

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 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 multicore project:

slexMulticoreExample

Generate Multithreaded Code

This application model is already partitioned into four concurrent tasks through the Concurrent Execution dialog box you can access from the Solvers pane of the Configuration Parameters dialog box. Use this dialog box to specify logic, task, and core assignments. In this example, the application logic is partitioned as:

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 dialog box 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.

Related Topics