Trouble With Indexing and Parfor

1 Ansicht (letzte 30 Tage)
Jacob Mevorach
Jacob Mevorach am 30 Mär. 2017
Kommentiert: Jacob Mevorach am 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

Akzeptierte Antwort

Matt J
Matt J am 4 Apr. 2017
Bearbeitet: Matt J am 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
  1 Kommentar
Jacob Mevorach
Jacob Mevorach am 5 Apr. 2017
This worked beautifully! Thank you so much!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

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

Kategorien

Mehr zu Parallel for-Loops (parfor) finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by