Nested loops within PARFOR loop

조회 수: 5 (최근 30일)
Jeremy
Jeremy 2012년 11월 9일
I am having issues using parfor. Getting the following error: The PARFOR loop cannot run due to the way variable 'ed' is used.
ed is some nm by 6 matrix that I am trying to fill in with certain values. BIN is a vector used to convert from binary to decimal.
If anyone has used PARFOR for parallel computing and could help it would be greatly appreciated (trying to reduce the run time which is currently at ~4 hrs).
Thanks,
-Jeremy
parfor i=1:popSize
O=0;
for j=1:nm
for k=1:ng
if edi(j,4)==k && pop(i,(k*7-6):(k*7))*BIN<=nsd && pop(i,(k*7-6):(k*7))*BIN~=0
ed(j,4:6)=sd(pop(i,(k*7-6):k*7)*BIN,2:4);
elseif pop(i,(k*7-6):(k*7))*BIN>nsd || pop(i,(k*7-6):(k*7))*BIN==0
O=1;
break
end
end
end
end
  댓글 수: 1
Jan
Jan 2012년 11월 9일
"O=0" is really confusing. Please do not use an uppercase "oh" as name of a variable.

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

답변 (1개)

Jan
Jan 2012년 11월 10일
Please use the profiler to find the bottlenecks. What is sd and pop?
Avoid calculating pop(i,(k*7-6):k*7)*BIN 4 times: Store the value in a temporary variable instead:
for i=1:popSize
P = 0;
for j=1:nm
c1 = edi(j, 4);
for k=1:ng
c2 = pop(i,(k*7-6):(k*7))*BIN;
if c1 == k && c2 <=nsd && c2 ~= 0
ed(j, 4:6) = sd(c2, 2:4);
elseif c2 > nsd || c2 == 0
P = 1;
break
end
end
end
end
Do you need "P" anywhere?!
  댓글 수: 1
Jeremy
Jeremy 2012년 11월 12일
편집: Jeremy 2012년 11월 12일
This is part of a genetic algorithm to optimize a certain structural analysis problem where pop is the binary population matrix, where I am attempting to retrieve different index values for sd. sd is a definition of different section values (area, moment of inertia, weight) and contributes to the definition of an element. Each index value for sd is defined by a 7 bit byte, of which there are several bytes in each row of pop.
P is used to ensure that the value gotten in c2 is within the dimensions of sd. As any byte could lead to a value greater than the number of rows in sd, I do not want it to try and access a non-existent point in sd.
Thank you for the advice on c1 and c2, I will definitely implement that. Any ideas as to why the parfor loop isn't working?

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by