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
Matt J 2014년 2월 13일

0 개 추천

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
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??
Matt J
Matt J 2014년 2월 14일
편집: Matt J 2014년 2월 14일
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
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.
Matt J
Matt J 2014년 2월 14일
편집: Matt J 2014년 2월 14일
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
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??
Matt J
Matt J 2014년 2월 17일
편집: Matt J 2014년 2월 17일
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
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
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에 대해 자세히 알아보기

질문:

2014년 2월 13일

편집:

2014년 2월 21일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by