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 Center および File ExchangeParallel for-Loops (parfor) についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by