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:
- 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
답변 (2개)
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.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Data Import from MATLAB에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!