Trouble With Indexing and Parfor

1 visualizzazione (ultimi 30 giorni)
Jacob Mevorach
Jacob Mevorach il 30 Mar 2017
Commentato: Jacob Mevorach il 5 Apr 2017
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

Risposta accettata

Matt J
Matt J il 4 Apr 2017
Modificato: Matt J il 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

Più risposte (1)

Faiz Gouri
Faiz Gouri il 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

Categorie

Scopri di più su Parallel for-Loops (parfor) in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by