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 :)

 Accepted Answer

Matt J
Matt J on 13 Feb 2014
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 Comments

Abhishek M
Abhishek M on 14 Feb 2014
Edited: Matt J on 14 Feb 2014
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 on 14 Feb 2014
Edited: Matt J on 14 Feb 2014
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 on 14 Feb 2014
Edited: Matt J on 14 Feb 2014
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 on 14 Feb 2014
Edited: Matt J on 14 Feb 2014
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 on 17 Feb 2014
Edited: Abhishek M on 17 Feb 2014
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 on 17 Feb 2014
Edited: Matt J on 17 Feb 2014
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.
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 on 20 Feb 2014
Edited: Abhishek M on 21 Feb 2014
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 13 Feb 2014

Edited:

on 21 Feb 2014

Community Treasure Hunt

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

Start Hunting!