MATLAB Answers

An Issue with SPMD

21 ビュー (過去 30 日間)
Jim Chang
Jim Chang 2020 年 7 月 17 日
回答済み: Edric Ellis 2020 年 7 月 17 日
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:
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)];
Thanks for your help!

  0 件のコメント



Edric Ellis
Edric Ellis 2020 年 7 月 17 日
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)
x = magic(labindex); % x is type 'double'
class(x) % gets 'Composite'
size(x) % gets [1,4]
size(x{2}) % gets [2,2] - the element from worker with labindex==2
class(x) % on the worker, we see the 'double'
size(x) % gets [labindex,labindex] on each worker
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