Error in Parallel for Loops in MATLAB R2020a

19 Ansichten (letzte 30 Tage)
Yacoub Sleibi
Yacoub Sleibi am 22 Aug. 2020
Kommentiert: Raymond Norris am 25 Aug. 2020
I am running a user written (BEAR) toolbox where it performs at some stage a parallel loop and iterations for impulse response functions.
The toolbox works well on MATLAB R2018a, but when I got the R2020a, I got the following error when I run the same code:
Error: File: irfres.m Line: 203 Column: 10
Assigning to the for-loop variable 'jj' is not supported in parfor-loops. For more information, see Parallel for Loops in MATLAB, "Nested for-Loops:
Requirements and Limitations".
Error in bear_toolbox_main_code (line 519)
[struct_irf_record D_record gamma_record]=irfres(beta_gibbs,sigma_gibbs,It,Bu,IRFperiods,n,m,p,k,signrestable,signresperiods);
Error in run (line 91)
evalin('caller', strcat(script, ';'));
Error in bear_4_2 (line 33)
run files/bear_toolbox_main_code;
I attach the irfres.m file.
I installed the Parallel computer toolbox but I dont think this made a difference. If I go back to the R2018a version then it works.
I feel there is a missing toolbox. I hope you folks can adivse.
Regards
  2 Kommentare
Walter Roberson
Walter Roberson am 23 Aug. 2020
I have not seen that error before, but the fix looks simple: rename jj to another variable such as jjj in lines 203 to 219.
Yacoub Sleibi
Yacoub Sleibi am 23 Aug. 2020
Walter, many thanks. Your fix did the trick. All works good now.
I wonder why it only happened in the newst verion of matlab and not the old.
Thank you again :)

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Raymond Norris
Raymond Norris am 23 Aug. 2020
Bearbeitet: Raymond Norris am 23 Aug. 2020
Hi Yacoub,
Here's what's happening (has nothing to do with missing toolboxes, etc.). You've started with something similar to
for idx = 1:10
for jidx = 1:10
rand;
end
jidx = 1;
while jidx<10
rand;
jidx = jidx+1;
end
end
You rewritten the outer loop as a parfor
parfor idx = 1:10
for jidx = 1:10
rand;
end
jidx = 1;
while jidx<10
rand;
jidx = jidx+1;
end
end
Which throws:
Assigning to the for-loop variable 'jidx' is not supported in parfor-loops. For
more information, see Parallel for Loops in MATLAB, "Nested for-Loops:
Requirements and Limitations".
Per the mentioned documentation:
Required (static): The index variable for the nested for-loop must never be explicitly assigned other than by its for statement.
This restriction is required, because changing the nested for-loop variable in the loop body cannot guarantee that the region indexed by the for-loop variable is available at each worker.
The two options are to use another while loop variable (other than jj in your case) or refactor your code, similar to the following
parfor idx = 1:10
unit_of_work;
end
function jidx = unit_of_work
for idx = 1:10
for jidx = 1:10
rand;
end
jidx = 1;
while jidx<10
rand;
jidx = jidx+1;
end
end
Raymond
  5 Kommentare
Walter Roberson
Walter Roberson am 23 Aug. 2020
The restriction of not assigning to a for loop index makes sense within the body of the for loop in the case where the for loop index is being used to index one of the variables being communicated with the client.
The restriction could also make sense if the for loop index value was being used after the loop to index a variable being communicated with the for loop variable not being modified in between.
In any case where the for loop index is only being used as a counter or as an index to a local variable, then restriction would not make sense.
There is a remaining case: a for loop variable used to index a communicated variable, then modified, then used to index a communicated variable, but the modification assigns a definite value such as 1, after the original loop. In such a case, it would be possible to track that the index is valid, and would be a Quality of Implementation to not track. Modifying to a definite value inside the for loop is more work as the for loop body might never be run in some cases.
Raymond Norris
Raymond Norris am 25 Aug. 2020
Walter,
Does this capture your last example:
M = 10;
A = rand(M);
parfor idx = 1:M
for jidx = 1:M
a = A(idx,jidx);
end
jidx = 1;
b = a(jidx);
for jidx = 1:M
c = A(idx,jidx)
end
end
This would have worked in R2019a.
Raymond

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Parallel for-Loops (parfor) finden Sie in Help Center und File Exchange

Produkte


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by