Trouble With Indexing and Parfor

1 view (last 30 days)
I've been trying to implement parfor in the following loop for a while but can't seem to find a way to get around the problem that parfor does not allow one to use indexes for variables that are not the loop variable. However given that the index in this case should be able to be sliced I'm wondering if there's a way I could implement parfor or maybe even spmd I'm not seeing.
% function tracks_Out = updateUnassignedTracks(tracks, unassignedTracks)
tracks_Out = tracks;
for i = 1:length(unassignedTracks)
ind = unassignedTracks(i);
tracks_Out(ind).age = tracks_Out(ind).age + 1;
tracks_Out(ind).consecutiveInvisibleCount = ...
tracks_Out(ind).consecutiveInvisibleCount + 1;
end
end

Accepted Answer

Matt J
Matt J on 4 Apr 2017
Edited: Matt J on 4 Apr 2017
Your loop iterations are not independent - and hence not suitable for parfor - unless all the values in "unassignedTracks" are unique. If you are confident that they are unique, you can do as follows,
function tracks_Out = updateUnassignedTracks(tracks, unassignedTracks)
u=unique(unassignedTracks);
N=length(u);
if N~=length(unassignedTracks)
error 'unassignedTracks are not unique'
end
tmp = tracks(unassignedTracks) ;
parfor ind = 1:N
tmp(ind).age = tmp(ind).age + 1;
tmp(ind).consecutiveInvisibleCount = ...
tmp(ind).consecutiveInvisibleCount + 1;
end
tracks_Out=tracks;
tracks_Out(unassignedTracks)=tmp;
end

More Answers (1)

Faiz Gouri
Faiz Gouri on 3 Apr 2017
I understand that you would like to use indexes for variable that is not the loop variable in parfor loop. When MATLAB recognizes a name in a parfor-loop as a variable, the variable is classified in one of several categories(Loop. sliced, broadcast, reduction, temporary). Here "ind" is a temporary variable and you cannot use it as loop variable. Refer this document for more details on parfor variables

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!