How to parallize for loop with sim command
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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:
- 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.
- define a new matrix variable called PartialArrayVar, in order to use it in the parfor to send the input matrix PartialArray to the simulink.
- 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.
0 comentarios
Respuestas (2)
Shivam Chaturvedi
el 19 de Abr. de 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.
0 comentarios
Ver también
Categorías
Más información sobre Data Import from MATLAB en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!