SPMD - store all values and return

8 views (last 30 days)
Teng Zeng
Teng Zeng on 6 Mar 2020
Commented: Teng Zeng on 9 Mar 2020
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!!

Accepted Answer

Edric Ellis
Edric Ellis on 9 Mar 2020
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 Comment
Teng Zeng
Teng Zeng on 9 Mar 2020
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!

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 7 Mar 2020
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 Comment
Teng Zeng
Teng Zeng on 7 Mar 2020
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

Sign in to comment.

Categories

Find more on MATLAB 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!