Parallel computing on cell array.

조회 수: 3 (최근 30일)
Phillip
Phillip 2024년 5월 12일
댓글: Walter Roberson 2024년 5월 13일
Hello,
In my code I've created an object oriented data type named 'particle' as a custom defined class.
particle wth numeric fields (positionx, positiony, velocityx, velocityy).
Creating a new particle is via the constructor of the particle class.
p1 = particle(1,1,4,4);
Because there are lots of particles, these are all stored in a cell array (which can handle any data type).
pArray = cell(1,n);
pArray{1} = p1;
Then access to the fields of any particle is simply by the dot notation.
pArray{1}.positionx;
pArray{1}.positiony;
pArray{1}.velocityx;
pArray{1}.velocityy;
and so forth....
I wanted to process the particles in pArray more quickly by dividing them among workers of the parallel pool. So I decided to convert pArray from a cell array to a distributed cell array.
pArray = distributed(pArray)
But now the fields of each particle cannot be accessed by this method using the dot notation. I've tried this both inside an spmd block and outside of an spmd block
The line here
pArray(1).positionx
gives the following error.
Error using indexing (line 40)
Distributed SUBSREF only supports () indexing unless the underlying data type is table.
Error in distributed/subsref>iSubsRefHelper (line 126)
[varargout{:}] = subsref(coDd, substruct(s_type, varargin));
Error in distributedutil.distributedSpmdWrapper>iInnerWrapper (line 82)
[varargout{:}] = fcnH( varargin{:} );
Error in spmd_feval_fcn>get_f/body (line 78)
[outCell{:}] = fcnH( inCell{:} );
Then within an spmd block these lines
spmd
pArray(1).positionx;
end
gives this error.
Error using matlab_tests3 (line 80)
Error detected on workers 3 6 7 8.
Caused by:
Error using indexing (line 27)
Distributed arrays only support simple subscripting.
I've just started using Matlab's parallel computing functionality so I'm just learning at this stage.
Is there a way to access the field data of an OO data structure stored in a cell array when using parallel computing techniques?
Any advice would be very helpful.
Thank you
Phil.
  댓글 수: 4
Phillip
Phillip 2024년 5월 13일
However, as you've indicated an object array could be used to store an object of the 'particle' class.
pXArray = createArray(1,4,"particle");
then access is as before;
pXArray(1).positionx;
pXArray(1).positiony;
But there still seems to be a problem if this object array is converted to a distributed array.
There is the following error.
Error using distributed (line 344)
A distributed array cannot be created with data of class: particle.
Error in matlab_tests3 (line 94)
pXXArray = distributed(pXArray)
Thanks
Phil.
Walter Roberson
Walter Roberson 2024년 5월 13일
So don't mark the array as distributed; let the array be sliced automatically.

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

답변 (1개)

Walter Roberson
Walter Roberson 2024년 5월 12일
Use a cell array.
Inside the parfor:
p1 = pArray{i};
x = p1.positionx;
y = p1.positiony;
and so on
  댓글 수: 1
Phillip
Phillip 2024년 5월 12일
Hi Walter
Thanks, yes will have a go with a parfor loop.
The cell array itself wasn't really the problem, but when it was converted to a distributed array then access to the object's field variables did not work.
Thanks.

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by