使用parfor的Simulink-Simulation(并行计算) [英] Simulink-Simulation with parfor (Parallel Computing)

查看:559
本文介绍了使用parfor的Simulink-Simulation(并行计算)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我今天问了一个关于使用Matlab-Simulink进行并行计算的问题.由于我之前的问题有点混乱,并且代码中有很多东西并不真正属于这个问题.

I asked today a question about Parallel Computing with Matlab-Simulink. Since my earlier question is a bit messy and there are a lot of things in the code which doesnt really belong to the problem.

我的问题是

我想在parfor-Loop中模拟某些东西,而我的Simulink-Simulation使用"From Workspace"块将所需的数据从工作空间集成到模拟中.由于某些原因,它不起作用.

I want to simulate something in a parfor-Loop, while my Simulink-Simulation uses the "From Workspace" block to integrate the needed Data from the workspace into the simulation. For some reason it doesnt work.

我的代码如下:

load DemoData
path = pwd;

apool = gcp('nocreate');
if isempty(apool)
    apool = parpool('local');
end

parfor k = 1 : 2
    load_system(strcat(path,'\DemoMDL'))
    set_param('DemoMDL/Mask', 'DataInput', 'DemoData')


       SimOut(k) = sim('DemoMDL')
    end

delete(apool);

我的模拟如下

DemoData-文件只是一个零(100,20)-矩阵.这是数据示例.

The DemoData-File is just a zeros(100,20)-Matrix. It's an example for Data.

现在,如果我模拟脚本,则会出现以下错误消息:

Now if I simulate the Script following error message occures:

错误

使用DemoScript时出错(第9行)

Error using DemoScript (line 9)

评估'DemoMDL/Mask'中的参数'DataInput'时出错

Error evaluating parameter 'DataInput' in 'DemoMDL/Mask'

原因:

Error using parallel_function>make_general_channel/channel_general (line 907)
Error evaluating parameter 'DataInput' in 'DemoMDL/Mask'
    Error using parallel_function>make_general_channel/channel_general (line 907)

    Undefined function or variable 'DemoData'.

现在您知道为什么会这样吗?

Now do you have an idea why this happens??

奇怪的是,如果我尝试访问parfor-Loop中的'DemoData',它会起作用.对于具有该代码的示例:

The strange thing is, that if I try to acces the 'DemoData' inside the parfor-Loop it works. For excample with that code:

load DemoData
path = pwd;

apool = gcp('nocreate');
if isempty(apool)
    apool = parpool('local');
end

parfor k = 1 : 2
    load_system(strcat(path,'\DemoMDL'))
    set_param('DemoMDL/Mask', 'DataInput', 'DemoData')
    fprintf(num2str(DemoData))
end

delete(apool);

这就是我的输出,而没有模拟和显示数据

'>>'DemoScript 00000000000000000 .....

'>>'DemoScript 00000000000000000 .....

非常感谢.这是原始问题,其中包含更多(不必要的)详细信息:

Thanks a lot. That's the original question with a lot more (unnecessary) details:

EarlierQuestion

推荐答案

我怀疑问题是,当MATLAB预处理parfor循环以确定需要将哪些变量传递给工作程序时,它不知道是什么DemoData是.在第一个示例中,它只是一个字符串,因此没有数据被发送出去.在您的第二个示例中,它明确了解变量,因此确实将其传递了.

I suspect the issue is that when MATLAB is pre-processing the parfor loop to determine what variables need to be passed to the workers it does not know what DemoData is. In your first example it's just a string, so no data gets sent over. In your second example it explicitly knows about the variable and hence does pass it over.

您可以尝试使用模型工作区",也可以只插入行

You could try either using the Model Workspace, or perhaps just inserting the line

DemoData = DemoData;

parfor循环代码中.

这篇关于使用parfor的Simulink-Simulation(并行计算)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆