Main Content

在多个核上运行共模拟组件

金宝app®supports co-simulation between components using local solvers or that involves simulation tools. For example, co-simulation can involve an S-function implemented 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 or aModel块加速器模式。

为了提高性能,考虑运行模型,如果:

  • You are integrating multiple co-simulation components

  • 在组件级别的集成是计算的强烈的

本主题假定您熟悉多线程编程和概念。

Simulink lets you run C MEX S-functions, Co-Simulation FMU blocks, andModelblocks configured for accelerator mode on multiple threads if they are threadsafe. Being threadsafe means that the block can work with multiple threads accessing shared data, resources, and objects without any conflicts.

You can also run a model on multiple threads if the model has a nondirect feedthrough block followed by a direct feedthrough block (seeSinglethreaded Versus Multithreaded Simulations)。直接馈通意味着在同一时间步骤中从其输入端口信号的值计算块的输出端口信号。

笔记

Multithreaded co-simulation forLevel-2 MATLAB S-Functionblocks is not supported.

By default, Simulink configures all models to run on multiple threads. If performance can be improved, Simulink automatically runs all models on multiple threads. However, not all models have co-simulation components that can run on multiple threads, and not all models benefit from running on multiple threads.

Singlethreaded Versus Multithreaded Simulations

When evaluating a model for possible concurrencies in your system for multithread execution, Simulink uses a graph-based algorithm to analyze data dependencies among eligible blocks and form multiple task groups. This algorithm ensures that the different task groups:

  • Have no data dependencies on each other

  • Can execute in parallel using multiple cores

例如,Simulink可能金宝app会查找非直接馈通块,然后是直接馈通块。如果以下直接馈通块仅取决于非向馈通块,则软件可以将块分组在同一分支中进行并行执行。

This example compares the serial execution of a series of blocks partitioned for parallel execution.

graph-based execution multithread conceptual diagram

Using Multithreaded Simulation to Speed up Simulation on Multiple Cores

Consider theGraph-Based Multithread Simulationexample, which uses four nondirect feedthrough blocks followed by four direct feedthrough blocks. Direct feedthrough means that the output port signal of a block is computed from the values of its input port signals in the same time step.

金宝appSimulink分析数据依赖性并形成四个分支。每个分支都包含非向馈通块,然后是直接馈通块。然后,Sim金宝appulink使用多线程来运行这四个分支。通过这种方式,与同一台机器金宝app上的SingleDethreaded模拟相比,Simulink可以在四个或更多核心机器上加速模拟到四个或更多核心机器。您可以在更改之前和之后手动测量模型的模拟MultithreadedSim范围。

slexGraphBasedMultiThreadSimExample model

您可以在更改之前和之后手动测量模型的模拟MultithreadedSim范围。此代码显示如何测量模拟Graph-Based Multithread Simulationexample:

openExample('slexGraphBasedMultiThreadSimExample');......set_param(gcs,'MultithreadedSim''off');tic; sim('slexGraphBasedMultiThreadSimExample'); TOC.
经过时间为39.406743秒。
set_param(gcs,'MultithreadedSim''在');tic; sim('slexGraphBasedMultiThreadSimExample');TOC.
经过时间为13.619744秒。

In this example, the speedup is almost three times (instead of four times) on a four core machine. This is due to the extra overhead introduced by the operating system thread context switch.

使用MultithreadedSim范围

您可以指定在多个线程上运行的整个型号,或指定使用多个线程运行的特定块MultithreadedSim范围。如果模型中的所有共模块是Threadsafe,则指定整个模型在多个线程上运行。如果有些,但不是所有块是Threadsafe,则只能识别在多个线程上运行的块。模型和块使用MultithreadedSim参数如下:

Setting Description

'汽车'[一种]

(Default) Let Simulink decide if the block can run on multiple threads.

'off'

禁用块或模型在多个线程上运行。

[一种]从R2021A开始,当您打开在上一个版本中创建的模型时,Simulink解释了金宝app'在'setting to be the same as'汽车'

Enabling theMultithreadedSimparameter does not mean that the block or model simulates on multiple threads. Simulation on multiple threads occurs whenMultithreadedSim已启用和:

  • The block and/or model operate at a single rate.

  • 块和/或模型是Threadsafe。(例如,它们不使用静态或全局数据)。

  • The block and/or model is exception-free. For S-function blocks, use thessSetOptions函数设置SS_OPTION_EXCEPTION_FREE_CODE

Multithreading does not allow solver reset checks, and therefore skips over any use of thessSetSolverNeedsReset一种ndssblockstateforsolverChangedatmajorstep.functions. Conversely, in accelerator mode, if these functions are used or there are continuous states, multithreading is automatically turned off.

配置S函数块以运行单个或多线程

Whether an S-function block runs single or multithreaded depends on theMultithreadedSimparameter value and thesssetruntimethreadsafety deliance功能。

MultithreadedSimSetting sssetruntimethreadsafety delianceSetting Single or Multithread
'汽车' runtime_thread_safety_compliance_unknown. Single thread
'汽车' runtime_thread_safety_compliance_true. Multithread
'汽车' runtime_thread_safety_compliance_false. Single thread
'off' - 该设置被忽略,S函数块运行SingleReDreded

共模拟on Multiple Threads Limitations and Guidelines

  • The simulation runs on a single thread for accelerator and rapid accelerator modes. Multithreading is enabled when the simulation mode is normal.

    Set model simulation mode to normal.

  • 共模组件没有代码生成。

  • 未激活多线程,用于恒定采样时间的块。

  • 没有启用多线程when the Simulink debugger is on.

    Turn off Simulink debugger.

  • A block that depends on a non-thread-safe block cannot be multithreaded. Consider breaking the dependency, for example, by using aUnit Delay堵塞。

S函数Block Limitations

  • Must have a single rate.

    考虑修改您的模型,将多速率组件分解为单个单速率组件。

  • 没有启用多线程when an S-function has variable sample time.

    Consider using a different sample time (see指定采样时间)。

  • 没有启用多线程when an S-function has continuous states and solver is fixed-step, which together trigger a continuous states consistency check. To disable continuous states consistency checks, use thessSetSkipContStatesConsistencyCheck功能。

  • 必须是线程安全 - 进来sssetruntimemethreadsafety匹配(Simstruct * s,Int_t val)valmust beruntime_thread_safety_compliance_true.

    有关更多信息,请参阅Guidelines for Writing Thread-Safe S-Functions

  • Must be exception-free — InssSetOptions(SimStruct *S,uint_T options)options必须包括SS_OPTION_EXCEPTION_FREE_CODE

    有关更多信息,请参阅Guidelines for Writing Thread-Safe S-Functions

  • 没有启用多线程when the S-function Analyzer is on. Try multithreading in normal mode.

  • 没有启用多线程when S-function has continuous sample time. Consider using a different sample time (see指定采样时间)。

  • 多线程并发运行输出和更新methods. The block must have an output or update method.

FMU.Import Block Limitations

  • Must be in co-simulation mode.

    考虑从模型交换到共模的切换FMU模式。

  • Must be thread-safe, for example, multiple FMUs must not access the same file at the same time.

  • 没有启用多线程when FMU block logging displays in the MATLAB®command window. Redirect FMU block logging to a file using:

    set_param(blockName,'fmudebugloggingredirect''文件'的)
  • FMU运行流程时不支持多线程。金宝app要禁用此设置,请使用:

    set_param(blockName,'DebugExecutionForFMUViaOutOfProcess''off'的)

Model Block Limitations

  • 没有启用多线程when aModel块有事件端口。

  • Cannot be inside a对于每个子系统堵塞。

    Consider moving theModel阻止对于每个子系统堵塞。

  • Must be in accelerator mode.

  • Must have single rate.

    考虑修改您的模型,将多速率组件分解为单个单速率组件。

  • Cannot use blocks with variable sample time.

    Consider using a different sample time (see指定采样时间)。

  • Cannot have continuous states.

  • Consider breaking the dependency between blocks, for example, by using aUnit Delay堵塞。

  • 必须具备固定步骤求解器。

  • 无法访问任何全局数据存储。

  • 没有启用多线程when a model contains a金宝appSimulink功能堵塞。

  • 不能使用任何Simulink函金宝app数或来电块。

  • Cannot contain文件块。有关更多信息,请参阅导出模拟数据

  • Cannot contain从文件块。

    Consider feeding data into the referenced model via an inport from the top-level model.

也可以看看

|||

Related Topics