How to parallize for loop with sim command

3 次查看(过去 30 天)
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 中查找有关 Data Import from MATLAB 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by