Trouble With Indexing and Parfor

조회 수: 1 (최근 30일)
Jacob Mevorach
Jacob Mevorach 2017년 3월 30일
댓글: Jacob Mevorach 2017년 4월 5일
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

채택된 답변

Matt J
Matt J 2017년 4월 4일
편집: Matt J 2017년 4월 4일
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
Jacob Mevorach
Jacob Mevorach 2017년 4월 5일
This worked beautifully! Thank you so much!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Faiz Gouri
Faiz Gouri 2017년 4월 3일
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

카테고리

Help CenterFile Exchange에서 Parallel for-Loops (parfor)에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by