SPMD - store all values and return

조회 수: 7 (최근 30일)
Teng Zeng
Teng Zeng 2020년 3월 6일
댓글: Teng Zeng 2020년 3월 9일
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