Implementing Parfor With Tricky Indexes

7 views (last 30 days)
I'm running a multi-object tracking script and I'm looking to implement parallel processing for one of my functions.
The problem is MATLAB only allows one to implement parfor when all indexes in a loop is done in terms of the loop variable. What I'm wondering is if anyone sees a way, provided all my assignments are unique, to implement parfor in the below function. I would greatly appreciate any help with this.
function tracks_Out = updateAssignedTracks(tracks, centroids, bboxes, assignments)
tracks_Out=tracks;
numAssignedTracks = size(assignments, 1);
for i = 1:numAssignedTracks
trackIdx = assignments(i, 1);
detectionIdx = assignments(i, 2);
centroid = centroids(detectionIdx, :);
bbox = bboxes(detectionIdx, :);
% Correct the estimate of the object's location
% using the new detection.
correct(tracks_Out(trackIdx).kalmanFilter, centroid);
% Replace predicted bounding box with detected
% bounding box.
tracks_Out(trackIdx).bbox = bbox;
% Update track's age.
tracks_Out(trackIdx).age = tracks_Out(trackIdx).age + 1;
% Update visibility.
tracks_Out(trackIdx).totalVisibleCount = ...
tracks_Out(trackIdx).totalVisibleCount + 1;
tracks_Out(trackIdx).consecutiveInvisibleCount = 0;
end
end

Accepted Answer

Matt J
Matt J on 5 Apr 2017
Edited: Matt J on 5 Apr 2017
The problem is MATLAB only allows one to implement parfor when all indexes in a loop is done in terms of the loop variable.
Note - this is only true for variables on which indexed assignment is performed. For indexed lookup, you don't have that restriction.
If speed is a priority, then the way you've organized your data - splitting it across elements of a structure array - is working against you. A lot of the work in your loop could be done in single, vectorized statements if the fields were concatentated into arrays of their own. For example, if you were to put all totalVisibleCount data in a vector of its own, you could update it without looping at all, just using the statements,
trackIndices = assignments(:, 1);
detectionIndices = assignments(:, 2);
totalVisibleCount(trackIndices)= totalVisibleCount(trackIndices)+1;
The only thing that really might require a loop is the call to correct(). This can be reduced to a parfor loop as follows, using a similar technique as we discussed in your previous thread.
KalmanFilters={tracks_Out(trackIndices).kalmanFilter};
Centroids = centroids(detectionIndices, :);
parfor i=1:length(KalmanFilters)
correct(KalmanFilters{i}, Centroids(i,:) );
end
although it's not entirely clear to me what correct() is doing because your code shows no output arguments.
  1 Comment
Jacob Mevorach
Jacob Mevorach on 7 Apr 2017
Something very similar to, but not quite, this ended up working. Thanks Matt!

Sign in to comment.

More Answers (0)

Categories

Find more on Parallel for-Loops (parfor) in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!