is there anyway to optimize the code for parfor?

조회 수: 1 (최근 30일)
Yu Li
Yu Li 2017년 1월 2일
댓글: Walter Roberson 2017년 1월 2일
I have loop runs good, but I want to optimize it for parfor. however when I change the first 'for' into 'parfor', it appears: Error: The variable A in a parfor cannot be classified.
is there anyway to fix this problem?
thanks!
parfor i=1:1:length(input_all(:,1))
set(g,'X', input_all(i,1:45), 'T',input_all(i,46), ...
'P',input_all(i,47)/101325*oneatm)
qn=rop_net(g);
nu_net = stoich_net(g);
for j=1:1:nSpecies(g)
A(j,:)=nu_net(j,:).*qn';
end
ROP(:,:,i)=A;
end
Li
  댓글 수: 2
KSSV
KSSV 2017년 1월 2일
What this loop will do?
for j=1:1:nSpecies(g)
A(j,:)=nu_net(j,:).*qn';
end
Yu Li
Yu Li 2017년 1월 2일
for every i, I renew the 'g' variable. calculate the qn, and nu_net. the size of nu_netis 45*155, and qn is 155*1, ROP is 45*155*1000, therefore I calculate A inside the second for-loop. give A to ROP, and go on next parfor-loop.
thanks! Li

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

답변 (2개)

Walter Roberson
Walter Roberson 2017년 1월 2일
It is not obvious that nSpecies(g) is constant, so it appears that variable portions of A are being initialized. In a regular for loop that would be okay because anything not assigned to would be left what it already was, but in parfor you cannot hold over a result from the previous iteration.
If nSpecies(g) is constant, then assign it to a variable before the parfor. If it is not constant, then in each parfor iteration you need to initialize A to zeros of the maximum size before writing over part of it.
  댓글 수: 1
Yu Li
Yu Li 2017년 1월 2일
편집: Walter Roberson 2017년 1월 2일
hi:
thanks for your reply.
the nSpecies is a constant, I changed it to a variable but still looks not work.
is there any other reason about this?
ROP=zeros(nSpecies(g),nReactions(g),length(input_all(:,1)));
num_Spe=nSpecies(g);
parfor i=1:1:length(input_all(:,1))
set(g,'X',input_all(i,1:45),'T',input_all(i,46),'P',input_all(i,47)/101325*oneatm)
qn=rop_net(g);
nu_net = stoich_net(g);
for j=1:1:num_Spe
A(j,:)=nu_net(j,:).*qn';
end
ROP(:,:,i)=A;
end
% code
end
for every i, I renew the 'g' variable.
calculate the qn, and nu_net.
the size of nu_netis 45*155, and qn is 155*1, ROP is 45*155*1000, therefore I calculate A inside the second for-loop. give A to ROP, and go on next parfor-loop.
thanks!
Li

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


Matt J
Matt J 2017년 1월 2일
Get rid of this loop
for j=1:1:nSpecies(g)
A(j,:)=nu_net(j,:).*qn';
end
and replace it with the single line,
A=bsxfun(@times,nu_net,qn');
Or, if you have R2016b, replace it with the single line,
A=nu_net.*qn';
  댓글 수: 2
Yu Li
Yu Li 2017년 1월 2일
hi: thanks for your reply. it might be some problem with the first command in the loop, therefore, it report wrong here:
if true
parfor i=1:1:length(T)
fprintf('calculating %1.0dth cell.\n',i)
set(g,'Y',mass_frac(i,:),'T',T(i),'P',P(i)/101325*oneatm)
end
% code
end
if true
% code
Warning: Element(s) of class 'Transport' do not match the current constructor definition. The element(s) have been converted to structures.
> In parallel.internal.pool.deserialize (line 9)
In parallel.internal.pool.deserializeFunction (line 12)
In remoteParallelFunction (line 33)
calculating 3042th cell.
Error using Untitled (line 12)
Conversion to double from struct is not possible.
end
the problem might be the variable 'g'.
thanks!
Walter Roberson
Walter Roberson 2017년 1월 2일
I think you will need to construct g each time. Or else take a copy of it and alter the copy. The analyzer is not smart enough to be able to recognize that the same properties are being set each time, so it will assume that some properties are being carried over from iteration to iteration.

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

카테고리

Help CenterFile Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by