MATLAB parfor index exceeds the number of array elements

69 vues (au cours des 30 derniers jours)
Preetham Manjunatha
Preetham Manjunatha le 26 Juil 2020
Commenté : Jungmin kim le 20 Juin 2021
Why MATLAB throws an error even if it shouldn't go to the first case of the switch statement? Below is the minimal example:
mycase = 2;
non_crack_bytes = num2cell(1:6000);
syn_crack_bytes = num2cell(1:10000);
imgCount = 10000;
parfor j = 1 : imgCount
switch mycase
case 1
if ~(non_crack_bytes{j} == 0)
% Do something
else
continue;
end
case 2
if ~(syn_crack_bytes{j} == 0)
% Do something
else
continue;
end
end
end
The error I am getting is:
Error using ScratchPaperFile>(parfor supply)
Index exceeds the number of array elements (6000).
Error in ScratchPaperFile (line 10)
parfor j = 1 : imgCount
  2 commentaires
Image Analyst
Image Analyst le 26 Juil 2020
Why are you using cell arrays? That's not efficient. Don't do that when you have no reason to.
For me your code just hangs. However if I use "for" instead of "parfor" it finished in a split second.
Preetham Manjunatha
Preetham Manjunatha le 26 Juil 2020
Even with the a regular array, it throws the same error.

Connectez-vous pour commenter.

Réponse acceptée

Edric Ellis
Edric Ellis le 27 Juil 2020
To run a parfor loop, MATLAB analyses each variable used within the loop and classifies them. In the original code, non_crack_bytes is classified as a "sliced input" variable - in other words, MATLAB considers that each iteration of the loop needs a single value from non_crack_bytes corresponding to the loop index j. The error occurs long before the worker tries to read from non_crack_bytes - the error occurs on the client trying to send the elements of non_crack_bytes that it thinks the worker will need. (This is why the error mentions "parfor supply", a bit of internal jargon relating to sending sliced loop inputs).
To fix this, one way is to introduce an additional access of non_crack_bytes inside the loop which changes it from a "sliced input" variable to a "broadcast" variable. That means that the client sends the whole of non_crack_bytes to each worker. One way to achieve that is like this:
parfor j = 1:imgCount
size(non_crack_bytes); % access whole of non_crack_bytes
switch mycase
case 1
if non_crack_bytes(j) ~= 0
... % etc.
  1 commentaire
Jungmin kim
Jungmin kim le 20 Juin 2021
This answer saves my time...
Thanks for the kind explanation :)

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Parallel for-Loops (parfor) dans Help Center et File Exchange

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by