Clear Filters
Clear Filters

spmd - tag result of computation and send to only one worker while avoid deadlocking

2 views (last 30 days)
I am creating an array within a spmd bloc, and would like to tag the array as 'c' . I don't care which worker gets that array, but only care that I am tagging the result of the subtraction as 'c'. So for that I am using labSend with the 'c' tag. Is there some paradigmatic way to send to another worker (not itself, so as to avoid deadlock).
for c=1:10
spmd(100) % use 100 procs
A=rand(100,c); % create array within spmd bloc
B=A - rand(100,c); % subtract something.
labSend(B,labindex+1,c) % send to
end %spmd
end %c
Currently I am sending to the next worker over, but this isn't always going to work for a lot of reasons. I've seen in the documentation for labSendReceive that the source and destination is done as,
spmd
A = 2*labindex;
destination = 1 + mod((labindex + 1) - 1, numlabs);
source = 1 + mod((labindex - 1) - 1, numlabs);
A = labSendReceive(source, destination, A)
end
Is this somewhat arbitrary to define destination and source using the mod function (and serves the purpose of avoiding deadlocking?)
  1 Comment
Edric Ellis
Edric Ellis on 11 Oct 2022
The lab* communication functions all require that both sender and receiver agree when a message is to be sent. In particular, labSend will sometimes not complete until the corresponding call to labReceive has started (this is generally the case for larger messages). The purpose of labSendReceive is to allow cyclic communication patterns where each worker wishes to send and receive at the same time (in general, trying to use separate labSend and labReceive calls for this will result in deadlock). The example code using mod to choose source and destination is common, but by no means required. An alternative pattern is to replace source or destination with [] to indicate no message, like this:
spmd
A = 2 * labindex;
destination = labindex + 1;
if destination > numlabs
destination = [];
end
source = labindex - 1;
if source < 1
source = [];
end
A = labSendReceive(source, destination, A);
end

Sign in to comment.

Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!