Clear Filters
Clear Filters

Parallel computing on cell array.

1 view (last 30 days)
Phillip
Phillip on 12 May 2024
Commented: Walter Roberson on 13 May 2024
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 Comments
Phillip
Phillip on 13 May 2024
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 on 13 May 2024
So don't mark the array as distributed; let the array be sliced automatically.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 12 May 2024
Use a cell array.
Inside the parfor:
p1 = pArray{i};
x = p1.positionx;
y = p1.positiony;
and so on
  1 Comment
Phillip
Phillip on 12 May 2024
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.

Sign in to comment.

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!