MATLAB: An Issue with SPMD

Parallel Computing Toolboxspmd

Hi, guys,
I just started to delve in the PCT of MATLAB. My code is a bit lengthy, so I would only place here the part that has caused " Brace indexing is not supported for variables of this type." error. The following is the snippet:
parpool(nb);
spmd
dataReceived = [ ]; % Create a buffer for the received data
NBRList = nodeNeighbors{labindex}; % The neighbor list of Agent labindex. E.g., labindex = 2, and it has neighbors 1 and 6; then NBRList = [ 1, 6 ]. NBRList is of % composite type while nodeNeighbors is of cell array type.
NBRLenth = length(NBRList); % Agent labindex's no. of neighbors
for i=1:NBRLenth
kPos =find(nodeID{NBRList(i)}==labindex); % Get where labindex lies in NBRList(i)'s neighbor list. nodeID (of cell array type) stores neighbor IDs plus labindex in % an ascending order.
thisBk =Bk{NBRList(i)}; % Composite objects only support simple subscripting, so I used thisBk. The abovementioned error pops up when this statement % is present.
% Send and receive relevant data to and from neighbors.
dataSent = thisBk([kPos, kPos+length(nodeID{NBRList(i)})]);
dataReceived = [dataReceived, labSendReceive(labindex,NBRList(i), dataSent)];
end
end
Thanks for your help!

Best Answer

  • I think the problem here is the transformation of data types from outside spmd to inside. If you create a variable inside an spmd block, outside the spmd block you get a Composite array. The Composite at your client behaves a bit like a cell array, but the contents are actually stored on the workers. When you go back into the spmd block, the Composite data is automatically converted back to the underlying data for each worker. A simple example might help:
    parpool('local', 4)
    spmd
    x = magic(labindex); % x is type 'double'
    end
    class(x) % gets 'Composite'
    size(x) % gets [1,4]
    size(x{2}) % gets [2,2] - the element from worker with labindex==2
    spmd
    class(x) % on the worker, we see the 'double'
    size(x) % gets [labindex,labindex] on each worker
    end
    So, in conclusion, data that appears as a Composite outside the spmd context will be converted back to the underlying data inside the spmd block.