is there anyway to optimize the code for parfor?
1 回表示 (過去 30 日間)
古いコメントを表示
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 件のコメント
回答 (2 件)
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.
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 件のコメント
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 Exchange で Parallel for-Loops (parfor) についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!