Different results using PARFOR and FOR

Sorry, at this time, I cannot bring my code to a minimal example. This is a huge pile of image processing code.

I have a loop that iterates over images (descriptors in stphogs variables) and detection is performed for each image.

function hogpatches = extractDetectionsFromImages(stphogs, poselet) hogpatches = cell(1,length(stphogs)); parfor i = 1:length(stphogs) tmp = extractDetectionsFromImage(stphogs(i), poselet); %eg 1x6 struct if ~isempty(tmp) hogpatches{i} = tmp; end end hogpatches = cell2mat(hogpatches); end 

So this is the main loop. But the function calls extractDetectionsFromImage very deeply.

My problem: Running this with a regular for loop gives the correct result. When using PARFOR, as mentioned above, hogpatches contains only 5 instead of 18 structures.

Where can I start looking for an error? I had a global variable that the program changed. I already deleted it. There is still a global variable 'config', which, however, is only readable. Any other clues? What could be the problem?

EDIT : Even if I just run one iteration (stphogs size is 1), parfor fails. It has nothing to do with the isempty part. The problem persists if I remove it.

EDIT2 : Okay, here I have boiled it to a minimal working example. This is really caused by a global variable:

 function parGlobalTest() global testVar; testVar = 123; parfor i = 1:1 fprintf('A Value: %d\n', testVar); testFunction(); end end function testFunction() global testVar; fprintf('B Value: %d\n', testVar); end 

In this example. The output for A will be 123, for B it will be nothing (undefined). Why is this?

+4
source share
1 answer

Ok, here is my solution:

 function syncTestVar() global testVar; save('syncvar.mat', 'testVar'); pctRunOnAll global testVar; pctRunOnAll load('syncvar.mat'); end 

If anyone has a better approach, please tell me ... It works though

Btw: saving / loading is necessary because in my real program testVar is a complex structure

+1
source

All Articles