SPMD - store all values and return
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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!!
0 Commenti
Risposta accettata
Edric Ellis
il 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.
Più risposte (1)
Walter Roberson
il 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.
Vedere anche
Categorie
Scopri di più su Data Type Identification in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!