Effacer les filtres
Effacer les filtres

Sliced variables in parfor loop (restricted indexing)

5 vues (au cours des 30 derniers jours)
Andrew Sykes
Andrew Sykes le 31 Mar 2014
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!

Réponse acceptée

Edric Ellis
Edric Ellis le 1 Avr 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 commentaires
Ryan Livingston
Ryan Livingston le 8 Avr 2014
Modifié(e) : Ryan Livingston le 8 Avr 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 le 15 Mar 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

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Parallel for-Loops (parfor) dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by