Parfor Closed loop variable
显示 更早的评论
Hi, Is it possible to have a closed loop model design which uses the initial value of inputs and later on the inputs are computed based on the output for further iterations, that is, the output is used again as the input for the next computation steps??
If yes, 1). Can it be implemented using parfor because my idea involves huge number of iterations consuming lot of time which I don't want to spend. 2). I tried using simin and simout for the input and output respectively. The model works in for loop but not in parfor:(. Can you suggest some other source and sink blocks for this implementation.
Thanks in Advance :)
采纳的回答
Matt J
2014-2-13
You can use the parfor to parallelize computation in individual iterations, possibly reducing time per iteration. However, you cannot parallelize across iterations. The iterations lack the parallel independence assumed by PARFOR.
11 个评论
Abhishek M
2014-2-14
编辑:Matt J
2014-2-14
Hi Matt,
My model is similiar to this code.
clear;
clc;
a=1;
tic;
for i=1:10
a=a+i;
b=i;
plot(a,b,'--xk')
hold all
end
toc;
This works properly but when i change the for loop to parfor the error occurs and the code doesnt execute. Could you please help me out with this.
This is just a demo code
As I said, your iterations are not independent, and therefore not what PARFOR is designed for. Your specific example can be rewritten in a parallelizable form
for i=1:10
a(i)=1+i;
b(i)=i;
end
plot(a,b,'xk');
and this will work with PARFOR as well. But parfor is not an instrument for accelerating any arbitrary for-loop.
thanx Matt,
But your rewritten example is not a closed loop.
a(i)=1+i;
But i want to use the value of 'a' again for the next iteration
a(i)=a(i-1)+i;
you mean to say my design will not get implemented in parfor??
Not the outer loop, at any rate. As I was saying, if each iteration individually is doing a heavy computation, (within the body of the outer loop that is) you might see if parfor could speed that up.
Abhishek M
2014-2-14
编辑:Matt J
2014-2-14
Hi Matt,
I am applying the parfor for my inner loop itself. i have attached a screen shot in this comment of my model "loopinloopmdl" which i am calling through my code which is represented as below. The variable 'x' is the input to the model which is loaded from the "WS_loop.mat" file which is also the output ofthe system which is used again as input for the 'i++' iteration
%%%%%%%%%%%%%%Code%%%%%%%%%
clear all
clc
model = 'loopinloopmdl';
% load_system(model)
tic;
for j=1:5
clear x;
load('WS_loop.mat');
load_system(model)
parfor i=1:10
simout= sim(model);
end
% x = get(si,'xout');
plot(x,'--xk')
hold all
c=x;
end
toc;
%%%%%%%%%%%%%%Code%%%%%%%%%
The above code is running fine with regular "for loop". But when i change the loop of 'i' iteration to parfor i get the following errors.
%%%%%%%%%%%%%%Error%%%%%%%%%%%%%%
Caused by:
Error using
parallel_function>make_general_channel/channel_general
(line 906)
Error evaluating parameter 'VariableName' in
'loopinloopmdl/From Workspace'
Error using
parallel_function>make_general_channel/channel_general
(line 906)
Undefined function or variable 'x'.
%%%%%%%%%%%%%%Error%%%%%%%%%%%%%%
Could you please help me out with this..
Thanks in advance.
Ah, well then in that case, it looks like you should be using parfor on the outer loop over j. Those iterations do look independent, based don what you've shown.
parfor j=1:5
S=load('WS_loop.mat');
x=S.x;
for i=1:10
simout= sim(model);
end
end
Incidentally, it is bad to introduce variables inexplicitly into the workspace with statements like
load('WS_loop.mat');
Notice the alternative way that I call load above, using an output S.
Abhishek M
2014-2-17
编辑:Abhishek M
2014-2-17
Thanx a ton Matt.
If i use parfor for the outer loop i cant use 'clear x' command inside the parfor loop for clearing the variable "x" for the next j'th iteration. Is there any alternative for "clear" command for clearing the variable inside parfor??
There is no clear need to clear x that I can see, even in the normal for-loop. It gets over-written anyway when you load a new x from WS_loop.mat.
Abhishek M
2014-2-18
编辑:Abhishek M
2014-2-18
Thanx Matt,
The model is executing without any errors but the output of for and parfor are not the same. When i run the parfor code the workspace remains empty. I am not sure whether the the parfor code is simulating my model. Is there any instruction which i need to give to get the output.
This is my present code.
clear all
clc
model = 'loopinloopmdl';
tic;
parfor j=1:5
S=load('WS_loop');
x=S.x;
load_system(model)
for i=1:10
%assignin('base','x',x)
simout= sim(model);
end
plot(x,'--xk')
hold all
end
toc;
[Note: I am running my model on a single machine with 8 core processors.]
As far as I can see, you aren't currently doing anything to save the results of the different outer loop iterations, j, even in your regular for-loop. If x is supposed to be the output, you could do, e.g.,
parfor j=1:5
...
X{j}=x;
end
Incidentally, the code you show is using the same input file 'WS_loop' for every outer iteration, j. Since the input is always the same, the results of all those iterations will be identical, meaning you're just doing unnecessary repeat work.
Abhishek M
2014-2-20
编辑:Abhishek M
2014-2-21
Yeah Matt, i know i am repeating the same thing again n again but as i said this is just a demo model. The actual implementation model involves random numbers and huge logical calculations whose output varies with each simulation for the same inputs.
Anyways, thanx a lot for your help and suggestions on this.
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 Loops and Conditional Statements 的更多信息
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!选择网站
选择网站以获取翻译的可用内容,以及查看当地活动和优惠。根据您的位置,我们建议您选择:。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
