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

조회 수: 2 (최근 30일)
John
John 2015년 6월 4일
댓글: Walter Roberson 2015년 6월 9일
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
Edric Ellis
Edric Ellis 2015년 6월 4일
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 2015년 6월 9일
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.

댓글을 달려면 로그인하십시오.

답변 (2개)

Cindy Solomon
Cindy Solomon 2015년 6월 5일
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 2015년 6월 5일
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
John
John 2015년 6월 9일
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 2015년 6월 9일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by