Parfor Closed loop variable
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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일
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
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
Matt J
2014년 2월 14일
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.
Abhishek M
2014년 2월 14일
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.]
Matt J
2014년 2월 19일
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)
