SPMD - store all values and return

3 次查看(过去 30 天)
Hi,
I have been stuck on this problem for a while, regarding the return values from SPMD operation. I have a function structured as follows:
function [a,b] = func_cal_ab()
spmd(2)
if labindex == 1
% do certain things, assign dataToSend
labSend(dataToSend, 2)
else
a = containers.Map();
b = containers.Map();
receivemsg = labReceive(1)
for i = 1:100
% assign new key and values to a and b
a('i') = value1;
b('i') = value2;
end
end
end
end
I understand that after completing the SPMD runs, I need to index the Composite object with the worker ID to retrieve 'a' and 'b'. However, the problem that I am experiencing is that I try to retrieve values from 'a' and 'b'; however, both 'a' and 'b' have only one key and one value, meaning that they only record the last run of the for loop. Is there a way that I record all the 100 keys (in this case) in the SPMD runs??
Any help is much appreciated!!

采纳的回答

Edric Ellis
Edric Ellis 2020-3-9
I tried the following, which worked as expected:
spmd(2)
if labindex == 1
labSend(magic(4), 2);
else
data = labReceive(1);
a = containers.Map();
b = containers.Map();
for idx = 1:100
thisKey = num2str(idx);
a(thisKey) = data;
b(thisKey) = data.';
end
end
end
% Extract Composite contents
aa = a{2};
bb = b{2};
% Assert correct contents
for idx = 1:100
thisKey = num2str(idx);
assert(isequal(aa(thisKey), bb(thisKey).'));
end
Perhaps you could try that and see where your code differs from this.
  1 个评论
Teng Zeng
Teng Zeng 2020-3-9
Thanks Edric, I did figure out what have happened to my codes. It has something to do with the labSend and labReceive numbers have to match, etc. Now I have much better undertanding about SPMD. I will give you credit on this one. Thanks a lot for the help!

请先登录,再进行评论。

更多回答(1 个)

Walter Roberson
Walter Roberson 2020-3-7
a('i') = value1;
That creates a container entry associated with the letter i (lower-case I) and assigns the value to it. Every iteration of your loop you are writing at the same key. Writing at key literal 'i' has nothing at all to do with the current value of the loop control variable named i
If you want to write into the container using the numeric value associated with i as the key then a(i) = whatever.
If you want to construct a character vector key from i and use that as the key for some reason then
a(sprintf('%d', i)) = whatever
Or more compactly
a(int2str(i)) = whatever
It is not clear why you are not using a numeric array or a cell array though.
  1 个评论
Teng Zeng
Teng Zeng 2020-3-7
Hi Walter, bad editing, my apology. What I actually meant in the question regarding 'i' is what you described
a(int2str(i)) = whatever
More specifically, it is
j = strcat(int2str(i),'_',int2str(i)); % for exmaple
a(j) = i;
So then, the problem is what I have described above, only the last element (when i=100, j = '100_100') is returned. Basially not all the information is stored and returned.
Thanks

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by