Implementing Parfor With Tricky Indexes

조회 수: 7 (최근 30일)
Jacob Mevorach
Jacob Mevorach 2017년 4월 5일
댓글: Jacob Mevorach 2017년 4월 7일
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

채택된 답변

Matt J
Matt J 2017년 4월 5일
편집: Matt J 2017년 4월 5일
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
Jacob Mevorach
Jacob Mevorach 2017년 4월 7일
Something very similar to, but not quite, this ended up working. Thanks Matt!

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

추가 답변 (0개)

카테고리

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