Parallel computing on cell array.

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 comentarios

Matt J
Matt J el 12 de Mayo de 2024
Because there are lots of particles, these are all stored in a cell array (which can handle any data type).
Why is that a reason to use cell arrays? Why can't you just make a vector of particle objects?
Phillip
Phillip el 12 de Mayo de 2024
Hi Matt
I've used a cell array because they can store any data type. Not just numeric types and character types, but custome defined types - and this is what the particle type is.
A standard array can only store numeric and character types as well as some Matlab types.
I have actually tried storing objects of particle type to a basic array, and this results in an error because Matlab tries to convert the particle object to a type double. So I think the cell array is necessary.
Thanks.
Phillip
Phillip el 13 de Mayo de 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 el 13 de Mayo de 2024
So don't mark the array as distributed; let the array be sliced automatically.

Iniciar sesión para comentar.

Respuestas (1)

Walter Roberson
Walter Roberson el 12 de Mayo de 2024
Use a cell array.
Inside the parfor:
p1 = pArray{i};
x = p1.positionx;
y = p1.positiony;
and so on

1 comentario

Phillip
Phillip el 12 de Mayo de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Productos

Versión

R2024a

Preguntada:

el 12 de Mayo de 2024

Comentada:

el 13 de Mayo de 2024

Community Treasure Hunt

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

Start Hunting!

Translated by