Trouble With Indexing and Parfor

1 vue (au cours des 30 derniers jours)
Jacob Mevorach
Jacob Mevorach le 30 Mar 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

Réponse acceptée

Matt J
Matt J le 4 Avr 2017
Modifié(e) : Matt J le 4 Avr 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
  1 commentaire
Jacob Mevorach
Jacob Mevorach le 5 Avr 2017
This worked beautifully! Thank you so much!

Connectez-vous pour commenter.

Plus de réponses (1)

Faiz Gouri
Faiz Gouri le 3 Avr 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

Catégories

En savoir plus sur Parallel for-Loops (parfor) dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by