Parfor Loop Error with Classification
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Paul Safier
il 28 Feb 2021
Commentato: Paul Safier
il 2 Mar 2021
When trying to convert the outer loop of the code below to a parfor loop, the error is:
Error: The variable z_new in a parfor cannot be classified.
See Parallel for Loops in MATLAB, "Overview"
Following the instruction on another post, I used a temporary variable to represent z_new, but that did not work either. Can anyone see what needs to be changed to permit this to run with parfor?
x = linspace(0,6,100); y = x; [xx,yy] = meshgrid(x,y);
z = sin(xx + 0.25*yy);
dx = 1; dy = 1; Dep = 100*ones(size(z));
z_new = z + Dep;
[nr,nc] = size(z);
nnx = round(Dep/dx,0); nny = round(Dep/dy,0);
parfor i = 1:nc
for j = 1:nr
for ii = i-nnx(j,i):i+nnx(j,i)
for jj = j-nny(j,i):j+nny(j,i)
iinew = ii;
jjnew = jj;
Thk = Dep.*Dep - dx*dx*(i-ii)*(i-ii) - dy*dy*(j-jj)*(j-jj);
if (ii > 0 && ii <= nc && jj > 0 && jj <= nr && Thk(j,i) >= 0)
Th_tmp = z(j,i) + sqrt(Thk(j,i));
if (iinew > 0 && iinew <= nc && jjnew > 0 && jjnew <= nr)
if z_new(jjnew,iinew) < Th_tmp
z_new(jjnew,iinew) = Th_tmp;
end
end
end
end
end
end
end
0 Commenti
Risposta accettata
Edric Ellis
il 1 Mar 2021
I think you can adapt this to work with parfor, but it's going to be a little bit of a stretch. Your variable z_new can be considered to be a parfor reduction variable. Normally, simple "reduction variables" are fine for computing a maximum value. Consider the simplest case I can think of:
largest = -Inf;
parfor i = 1:10
newValue = rand();
largest = max(newValue, largest);
end
This looks slightly different to your example in a couple of ways: firstly, there's no indexing into largest - we'll deal with that later; secondly - I've used max rather than if to convince parfor that largest is indeed a reduction variable.
So, next we need to deal with the indexing. This is a bit more complicated. We can do this like so:
largest = -Inf(1, 3); % Starting point
parfor i = 1:10
% Allocate an update for this iteration of the parfor loop
update = -Inf(size(largest));
for j = 1:3
% Fill out an element of 'update'
update(j) = rand();
end
% Finally, apply the update all at once
largest = max(largest, update);
end
The trick here is to create a single update array that we can use with a single "reduction" operation max. In your case, you'll need to allocate your equivalent of update just ahead of the loops over ii and jj and then apply it just after those loops.
4 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Parallel for-Loops (parfor) in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!