How to parallize for loop with sim command

조회 수: 2 (최근 30일)
sali
sali 2016년 4월 15일
댓글: sali 2016년 4월 27일
Dear all, I have this part of my code which I want to run it under parfor:
for i=1:100
for j=1:100
for row=i:i+2
d=d+1;
for col=j:j+2
PartialArray(d,k)=U(row,col);
k=k+1;
end
k=1;
end
csvwrite('/home/Documents/MATLAB/IC/PartialInputImg.csv', PartialArray);
sim('/home/Documents/MATLAB/TestingBigImgOldWeights.slx');
Out(i,j)=simout.signals.values(1);
Out(i,j)
counter
counter=counter+1;
imshow(Out)
d=0;
end
end
The PartailArray is the instant input matrix for the simulink in each iteration, as of my understanding I should prepare these input matrices in advance in order for the parfor to work probably also I cancelled the using of excel file that is used to read this input matrix by the simulink model what I did is:
  1. preparing the PartialArray before calling parfor and save it as 3 dimensions array, so I have for now 25 of 3x3 matrices arranged in one array called PartialArray.
  2. define a new matrix variable called PartialArrayVar, in order to use it in the parfor to send the input matrix PartialArray to the simulink.
  3. This assignment should be done in a separate function since it not working immediately under the parfor.
The new code is :
PartialArrayVar=zeros(3,3);
iterations=25;
simout(iterations) = Simulink.SimulationOutput;
f=0;
parfor (f=1:iterations)
fcn(PartialArray(:,:,f), PartialArrayVar);
simout(f)=sim('/home/Documents/MATLAB/TestingBigImgOldWeightsPARFOR.slx', 'SimulationMode', 'normal');
end
function fcn( PartialArrayVar,PartialArray )
assignin('base', 'PartialArrayVar', PartialArray);
end
But it seems dos not work probably! could someone help in this issue. I do not receive any output. My other question is how to let the matlab block function in the sim model to read some other data from the work space other than excel files since I have some blocks have to reach those excel files during the run time?
Any help I would appreciate it.

답변 (2개)

Shivam Chaturvedi
Shivam Chaturvedi 2016년 4월 19일
I'm not sure if it's a good idea to use assignin in a function being called from parfor since it can cause abnormal results as the variable can be written over from different workers.
Even if you're okay with that, your code should be changed from
assignin('base', 'PartialArrayVar', PartialArray);
to
assignin('base', PartialArrayVar, PartialArray);
and then you pass the string of the variable which will be stored in PartialArrayVar, so you call it like below:
fcn('x', 10)
or maybe use a variable that stores the string 'x'. Otherwise the assignin command looks for a variable named 'PartialArrayVar'
I would suggest looking at the example at this link for using sim command with parfor:
Hope that helps.

sali
sali 2016년 4월 25일
Thank you for your reply I'm going to check that.
  댓글 수: 1
sali
sali 2016년 4월 27일
Actually I'm still have a problem with parfor :Unknown command option. Is there any way other than using assignin to send different data(matrix) in the parfor iterations?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by