How to parallize for loop with sim command

3 views (last 30 days)
sali
sali on 15 Apr 2016
Commented: sali on 27 Apr 2016
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.

Answers (2)

Shivam Chaturvedi
Shivam Chaturvedi on 19 Apr 2016
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 on 25 Apr 2016
Thank you for your reply I'm going to check that.
  1 Comment
sali
sali on 27 Apr 2016
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?

Sign in to comment.

Categories

Find more on Data Import from MATLAB in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!