parfor error: The variable objectEdges in a parfor cannot be classified.

2 vues (au cours des 30 derniers jours)
John
John le 4 Juin 2015
Commenté : Walter Roberson le 9 Juin 2015
brief aside: Isn't the sequential processing of loop iterations bye parfor the same as the for loop?
The goal is to process 100 images. A snipet of the processing code is included below. A supercomputer cluster is available for processing. What is the best way to approach the problem? Each output needs to be saved separately. How is this solved?
Also, help parpool parpool not found.
ERROR: Error: The variable objectEdges in a parfor cannot be classified. See Parallel for Loops in MATLAB, "Overview".
CODE:
kBW = kirschImage > 0;
kBinaryFill = imfill(kBW,'holes');
kProps = regionprops(kBinaryFill,'PixelIdxList');
objectEdges(1:length(kProps)) = struct('pixelList',[]);
for j = 1:length(kProps)
object = kProps(j).PixelIdxList;
for w = 1:length(object)
[X,Y] = ind2sub([N,M],object(w));
if ( (X-N)~=0 & (Y-M)~=0 )
hoodSearch = [kBinaryFill(X-1,Y),kBinaryFill(X,Y),kBinaryFill(X+1,Y),...
kBinaryFill(X,Y-1),kBinaryFill(X,Y+1)];
hoodFind = length(find(hoodSearch));
if ( hoodFind~=5 )
linInd = sub2ind([N,M],X,Y);
objectEdges(j).pixelList = [objectEdges(j).pixelList; linInd];
end
end
end
end
kEdge = zeros(N,M);
for j = 1:length(objectEdges)
kEdge(objectEdges(j).pixelList) = 1;
end
  2 commentaires
Edric Ellis
Edric Ellis le 4 Juin 2015
It's not clear where your parfor loop is here. Please could you post a minimal reproduction that shows the error that you're encountering.
What release of MATLAB/PCT do you have installed? Before R2013b, you need to use matlabpool rather than parpool.
Walter Roberson
Walter Roberson le 9 Juin 2015
To answer the question "Isn't the sequential processing of loop iterations bye parfor the same as the for loop?":
No. The order that parfor uses to execute the iterations is unspecified and dynamic according to how long the iterations take.
In the case of a single worker, historically parfor will execute the loop in reverse order from sequential. Or perhaps it uses "maximum to minimum" (which might be the same order as normal if the loop increment was negative.) Or perhaps it analyzes the form of the indexing expressions and figures out whether the smallest or largest loop value will produce the largest index (since you might have CONSTANT-INDEX) and runs the loop in the order that processes the largest first. It isn't specified. For loops that count up and which do not subtract the index from something, the known order is reverse (largest first), which has the side effect of allocating the entire output array because it writes into the largest offset.

Connectez-vous pour commenter.

Réponses (2)

Cindy Solomon
Cindy Solomon le 5 Juin 2015
I second Edric to please provide the minimum reproduction steps and clarification what you are using parfor for. I have seen a similar error when trying to write field values of a structure in a "parfor" loop. As a workaround, you could create a temporary variable in a "parfor" loop and then assign it back to the field in a separate "for" loop. However, I am not sure if this is the case with your code- clarification would be very helpful.
Hope this helps!

Walter Roberson
Walter Roberson le 5 Juin 2015
If the whole thing is inside a parfor that is not shown here, then your line
objectEdges(1:length(kProps)) = struct('pixelList',[]);
is a problem if objectEdges is an output variable instead of a local variable. Output variables must be indexed by an expression involving the parfor loop variable.
  2 commentaires
John
John le 9 Juin 2015
Hello Walter,
I changed the code to read
for j = 1:length(kProps)
objectEdges = struct('pixelList',[]);
...
perform for loop w over all pixels in object j
pixelList(w) = linInd;
end
objectEdges(j).pixelList = pixelList;
end
Which should have followed MATLAB's rules for first-level indexing in sliced variables. However, this caused a problem for variable pixelList. If I initialized pixelList before the for loop w, then again objectEdges produced an error.
Finally, the entire image segmentation code (300 lines) was made a function, which was then sliced by MATLAB's parfor successfully.
Tip for other parfor beginners: create a function for your algorithm and parfor the function with broadcast variable inputs.
Now on to parpool in MATLAB 2015 for true parallel processing.
Walter Roberson
Walter Roberson le 9 Juin 2015
Your first line in your "for" is
objectEdges = struct('pixelList',[]);
which writes into all of objectEdges.
Your last line of your "for j" is
objectEdges(j).pixelList = pixelList;
which writes into the j'th element of the objectEdges that was created for this iteration. The very next iteration of "j" that change is going to be lost, as all of objectEdges is going to be overwritten.

Connectez-vous pour commenter.

Catégories

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

Community Treasure Hunt

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

Start Hunting!

Translated by