运行 Simulink 模型并行

Run Simulink model parallel

我想 运行 在具有不同数据的许多内核上的 parfor 循环中创建一个复杂的 Simulink 模型。 但是,我还没有成功,所以我创建了一个简单的模型并尝试 运行 它与相同的数据并行。

模型看起来像这样,添加了两个信号: 使用代码:

function Result  = Add (X, Y)
Result = X + Y;

运行 模型的脚本如下:

if matlabpool('size') == 0
    matlabpool('open',4);
end

parfor i = 1:4
    data=ones(1,20);
    X=timeseries(data);
    Y=timeseries(data);
    output = sim('model_test','StopTime','5');
    Result = output.get('Res');
end

但是出现如下错误:

我不明白为什么变量不存在。我知道并行计算在变量访问方面总是至关重要的,但我还没有成功使用 simulink parallel 运行ning。你能向我解释错误以及如何解决吗? 非常感谢你!

对 am304 的回答:谢谢,这个回答帮助了我,我现在知道如何在 parfor 循环中使用 set_param 更改常量,我明白了为什么它不适用于时间序列。 但是对于时间序列,我仍在努力。 我试了几个版本,还有这个:

if matlabpool('size') == 0
matlabpool('open',4);
end

data=ones(1,20);
X=timeseries(data);
Ybase=timeseries(data);

parfor i = 1:4
    Y = evalin('base', 'Ybase');
    output = sim('model_test','StopTime','5');
    Result{i} = output.get('Res');
end

工作空间中存在变量Ybase,但出现如下错误:

如您所见,基础工作区中存在变量Ybase。你知道如何使用evalin或assignin正确访问吗?

谢谢和问候!

我怀疑这是因为您的数据 data 仅存在于主 MATLAB 的工作区中,而不存在于由 matlabpool 对工人启动的任何实例中。查看文档中的 Workspace Access Issues 以了解有关如何解决此问题的更多详细信息,并提供一些示例来说明这两种方法:

The MATLAB workers, however, do not have access to the workspace of the MATLAB client session where the model and its associated workspace variables have been loaded. Hence, if you load a model and define its associated workspace variables outside of and before a parfor loop, then neither is the model loaded, nor are the workspace variables defined in the MATLAB worker sessions where the parfor iterations are executed. This is typically the case when you define model parameters or external inputs in the base workspace of the client session. These scenarios constitute workspace access issues.

[...]

Resolving Workspace Access Issues

When a Simulink model is loaded into memory in a MATLAB client session, it is only visible and accessible in that MATLAB session; it is not accessible in the memory of the MATLAB worker sessions. Similarly, the workspace variables associated with a model that are defined in a MATLAB client session (such as parameters and external inputs) are not automatically available in the worker sessions. You must therefore ensure that the model is loaded and that the workspace variables referenced in the model are defined in the MATLAB worker session by using the following two methods.

  • In the parfor loop, use the sim command to load the model and to set parameters that change with each iteration. (Alternative: load the model and then use the g(s)et_param command(s) to set the parameters in the parfor loop)
  • In the parfor loop, use the MATLAB evalin and assignin commands to assign data values to variables. Alternatively, you can simplify the management of workspace variables by defining them in the model workspace. These variables will then be automatically loaded when the model is loaded into the worker sessions. There are, however, limitations to this method. For example, you cannot have tunable parameters in a model workspace. For a detailed discussion on the model workspace, see Model Workspaces.

编辑

这是我在您的特定示例中要做的事情:

if matlabpool('size') == 0
   matlabpool('open',4);
end

data=ones(1,20);
X=timeseries(data);
Y=timeseries(data);

parfor i = 1:4
    assignin('base','Y',Y);
    output = sim('model_test','StopTime','5');
    Result{i} = output.get('Res');
end

另一种选择是在模型工作区中包含 XY,以便模型是独立的。