Parallel Loop cannot run because of a temporrary variable

1 view (last 30 days)
I have a script that extracts 3DSIFT descriptors from a video clip. The problem is that due to the large number of keypoints it take hours to process a single clip and I have hundreds of them.
I wanted to speed up the process by using parfor. The 3DSIFT script does not have nested for's so I thought it would be simple to convert this to parfor. Below is the script:
function keys = extract3DSIFT(video, descriptors, corner_points)
offset = 0;
% Generate descriptors at locations given by subs matrix
parfor i=1:descriptors
reRun = 1;
while reRun == 1
loc = corner_points(i+offset,:);
fprintf(1,'Calculating keypoint at location (%d, %d, %d)\n',loc);
% Create a 3DSIFT descriptor at the given location
[keys{i}, reRun] = Create_Descriptor(video,1,1,loc(1),loc(2),loc(3));
if reRun == 1
offset = offset + 1;
end
end
end
fprintf(1,'\nFinished...\n%d points thrown out do to poor descriptive ability.\n',offset);
Here video is the video clip to be processd, descriptors is the number of descriptors to extract (set to 1000) and corner_points are the x,y,z coordinates of keypoints.
If the script feels that the extracted descriptor is not good, it will offset the index by 1 and calculate at the next keypoint location.
parfor does not like the offset variable and fails to run. I get the following messages:
The temporary variable offset will be cleared at the beginning of each iteration of the parfor loop.
Any value assigned to it before the loop will be lost. If offset is used before it is
assigned in the parfor loop, a runtime error will occur.
Is there anyway to avoid this? I tried looking into attaching the variable to the pool with:
poolobj = parpool;
addAttachedFiles(poolobj,offset);
But that did not work as it is not of type cell.
Thanks for reading

Accepted Answer

Matt J
Matt J on 10 Nov 2017
Edited: Matt J on 10 Nov 2017
What if you just bring the offset inside the loop? Shouldn't it be there anyway? If not, your loop iterations are not independent, as required for parallelization.
% Generate descriptors at locations given by subs matrix
parfor i=1:descriptors
offset = 0;
reRun = 1;
while reRun == 1
....
  3 Comments
Matt J
Matt J on 10 Nov 2017
Edited: Matt J on 10 Nov 2017
I don't understand your code well enough to answer that. However, if offset is meant to evolve as the loop counter i changes, then your loop does not have parallelizable structure, and parfor does not apply.
Faraz
Faraz on 17 Nov 2017
Thanks, I managed to make this work by taking the parfor out of the function. Like so:
parfor i = 1: numel(descriptors)
siftkeys(i) = extract3DSIFT(video, descriptors, corner_points)
end

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!