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!