Sliced variables in parfor loop (restricted indexing)

5 次查看(过去 30 天)
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