Filter löschen
Filter löschen

Sliced variables in parfor loop (restricted indexing)

5 Ansichten (letzte 30 Tage)
Andrew Sykes
Andrew Sykes am 31 Mär. 2014
Kommentiert: Nikita Balyschew am 15 Mär. 2018
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!

Akzeptierte Antwort

Edric Ellis
Edric Ellis am 1 Apr. 2014
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 Kommentare
Ryan Livingston
Ryan Livingston am 8 Apr. 2014
Bearbeitet: Ryan Livingston am 8 Apr. 2014
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 am 15 Mär. 2018
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

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

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