As our code becomes more complex we might have the necessity to pass data between different labs. Don't worry tho, Matlab is equipped with a few functions to deal with this necessity, shall it arrive. This functions will no doubt fulfill your lab communications needs.
The basic commands used to send and receive data are labSend(data, destination) and labReceive(source). Example:
spmd
switch labindex
case 3
string = 'Hello lab 1.';
labSend(string,1)
case 1
labReceive(3)
end
end
If you try to run this code you'll see that the string was sent from lab 3 to lab 1 successfully. If you run the command labReceive() in any of the labs without running the corresponding labSend() command from another lab an error will show up.
If you want to send data from a lab to any other lab willing to receive it you can use the command labBroadcast(senderlab,data) (from the lab who's sending the data) and labBroadcast(senderlab) (from a lab who wants to receive the data). Example:
spmd
switch labindex
case 3
string = 'Hello lab 1.';
labBroadcast(3,string)
case 1
data = labBroadcast(3) % The string sent from lab 3 will be
end % stored in the variable data from lab 1.
end
The last command I think is worth mentioning is labBarrier. When a lab arrives this command it will stop running until all the labs arrive at the same point in the algorithm. This can be very useful whenever you get to a point where an individual lab can't run unless some information is passed around.
spmd
switch labindex
case 1
for n=1:250
eig(magic(n));
end
case 2
tic
end
if labindex==2
time = toc; % Point A
end
end
If I run this code the lab 2 will take no time to arrive at point A. If, however, I insert a labBarrier before point A it will take lab 2 about 4 seconds ( the time it takes for the lab 1 to run his loop) to get to point A, since he won't arrive until lab 1 is done.
There are a few more commands that I didn't write about, but those three should serve you well. If they aren't enough you can always try looking inside Matlab documentation for further commands that could better suit your needs.
Cheers!
No comments:
Post a Comment