MATLAB Answers

An Issue with SPMD

조회 수: 7(최근 30일)
Jim Chang
Jim Chang 17 Jul 2020
답변: Edric Ellis 17 Jul 2020
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!

  댓글 수: 0

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

채택된 답변

Edric Ellis
Edric Ellis 17 Jul 2020
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.

  댓글 수: 0

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

추가 답변(0개)

Community Treasure Hunt

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

Start Hunting!

Translated by