필터 지우기
필터 지우기

Sliced variables in parfor loop (restricted indexing)

조회 수: 9 (최근 30일)
Andrew Sykes
Andrew Sykes 2014년 3월 31일
댓글: Nikita Balyschew 2018년 3월 15일
Hi,
I am having trouble with some parallel for-loop coding. I believe my essential difficulties can be captured in the following example code.
The function "do_something(x)" takes an array as input and returns an array of equal size ("rand(size(pSlice))" would suffice for example), but this should be irrelevant. Matlab doesn't like the way I am indexing "p". After reading "documentation-center/classification-of-variables/sliced-variables", I understand (in principle) why Matlab doesnt accept "p" (incorrect form of indexing ), but what are my options to resolve this problem?
CellOccupations=round(rand(50,1)*20);
p=rand([sum(CellOccupations) 3]);
csO=cumsum(CellOccupations);
parfor iter=1:50
StartingPointIndex=csO(iter)-CellOccupations(iter)+1;
EndPointIndex=csO(iter);
pSlice=p(StartingPointIndex:EndPointIndex,:);
pSlice=do_something(pSlice);
p(StartingPointIndex:EndPointIndex,:)=pSlice;
end
I'll be happy to provide additional details and description of the problem if someone thinks they can help.
Thanks very much in advance for any help and effort!

채택된 답변

Edric Ellis
Edric Ellis 2014년 4월 1일
Although you are dividing up 'p' so that each loop iteration works on a different piece, unfortunately you aren't doing it in the simple manner required for a PARFOR sliced variable. I think in this case because each iteration works on a differently sized 'slice' of 'p', you need to divide up 'p' to be a cell array. Something like this might work:
CellOccupations = round(rand(50, 1) * 20);
p = rand([sum(CellOccupations), 3]);
pSliced = mat2cell(p, CellOccupations);
parfor idx = 1:numel(pSliced)
pSlice = pSliced{idx};
pSlice = do_something(pSlice);
pSliced{idx} = pSlice;
end
p = cell2mat(pSliced)
  댓글 수: 4
Ryan Livingston
Ryan Livingston 2014년 4월 8일
편집: Ryan Livingston 2014년 4월 8일
Hi Andrew,
Could you please make a new question for the codegen issue and add the product MATLAB Coder to it? Cell arrays are not supported for code generation so that may be the source of some errors.
Nikita Balyschew
Nikita Balyschew 2018년 3월 15일
Hi Andrew or Ryan,
is it possible to link to this with cell arrays occuring problem and opened question to get further information?
Thanks in advance, Nikita

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

추가 답변 (0개)

카테고리

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