Implementing Parfor With Tricky Indexes
7 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Parallel for-Loops (parfor) 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!