Codistributed array as class property
10 views (last 30 days)
Show older comments
Hi all,
I have a large array (a database let's say, which stays fixed), which I load into a codistributed array directly to avoid communication overhead. I do this as follows:
spmd
% Allocate memory in codistributed array
database = codistributed.zeros(some_size);
% Get the local part on the worker
lp = getLocalPart(database);
% Load the data from files into the local part
lp = loadDataFromFiles();
% Put the local part back
database = codistributed.build(lp,getCodistributor(database));
end
I do this to allow several workers to process parts of the database simultaneously. This works fine, as long as I perform this in the 'main' script of the program, and the database variable is available in the workspace. However, when I want to put all functionality into a class, things stopped working. Let's say I have something as follows:
classdef database
properties
data
end
methods
function obj = database()
% Load the data
spmd
% Allocate memory in codistributed array
database = codistributed.zeros(100);
end
obj.data = database;
end
function queryData(obj)
spmd
% Perform some operations on the database, which is stored
% as a distributed array in obj.data.
size(getLocalPart(obj.data))
end
end
end
end
Matlab let's me create an object of the class, and the property data is initialised. When checking the properties of the class object, it says that obj.data is a distributed array, as I would expect.
However, when I try to execute the queryData method, I get the following error:
Error using distributed/getLocalPart (line 195)
It is not possible to call "getLocalPart" directly on a distributed array. To call
"getLocalPart", you must enter an SPMD block and operate on the corresponding codistributed
array.
I can't seem to find any related topics. So my question obviously is: what am I doing wrong? And is it even possible to 'hide' a distributed array in a class property?
Thanks!
0 Comments
Accepted Answer
Edric Ellis
on 27 May 2016
spmd blocks can only transform correctly between the distributed and codistributed representation when the distributed array is a separate variable, i.e. not a field of a struct or object. So, I think you need to work around this limitation like so:
function queryData(obj)
% pull out the distributed array into its own variable
tmpData = obj.data;
spmd
% Perform some operations on the database, which is stored
% as a distributed array in tmpData
size(getLocalPart(tmpData))
end
end
If your method modifies the underlyin distributed array, then you'd also need to assign back into obj.data after the spmd block.
3 Comments
Edric Ellis
on 31 May 2016
tmpData is a distributed array - this means the data is not transferred back and forth between the client and workers. The client-side view of tmpData is effectively a series of "pointers" (or something like that) which refer to the codistributed parts that are stored on the workers.
More Answers (0)
See Also
Categories
Find more on Distributed Arrays in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!