is there anyway to optimize the code for parfor?
1 view (last 30 days)
Show older comments
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 Comments
Answers (2)
Walter Roberson
on 2 Jan 2017
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 Comment
Matt J
on 2 Jan 2017
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 Comments
Walter Roberson
on 2 Jan 2017
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.
See Also
Categories
Find more on Parallel for-Loops (parfor) in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!