How does labindex in spmd works?

4 views (last 30 days)
Based on the spmd labindex coding provided by @Walter Roberson here, I wonder when do the following labindex (labindex 2, 3 and 4) execute their jobs? Is it based on clock cycle or any enable port?
Or labindex 2, 3 and 4 execute their jobs once they received data and in waiting (pause) condition when there is no data received?
framesize = 4096;
P = parpool(4);
spmd
if labindex == 1
end
end
spmd
if labindex == 1
afr = dsp.AudioFileReader(Filename, 'SamplesPerFrame', framesize);
while ~isDone(afr)
frame = step(afr);
labSend(frame, 2);
end
labSend([], 2)
release(afr)
elseif labindex == 2
while true
frame = labReceive(1);
if isempty(frame)
labSend([], 3)
break
end
Bres = BlockB(frame);
labSend(Bres, 3);
end
elseif labindex == 3
while true
Bres = labReceive(2);
if isempty(Bres)
labSend([], 4)
break
end
Cres = BlockC(Bres) ;
labSend(Cres, 4)
end
else
while true
Cres = labReceive(3);
if isempty(Cres)
break
end
Dres = BlockD(Cres) ;
end
end

Accepted Answer

Walter Roberson
Walter Roberson on 30 Aug 2019
P = parpool(4); creates four processes. All four of them start executing the same block of code, with nearly the only difference between the processes being that if they call labIndex they will get different results from 1 to 4.
All four of the processes will begin executing at the same time, without waiting for anything. They will not wait for input.
As soon as any of the processes has nothing left to do, it will go into a "waiting to exit" state and will not participate in any further work except to help collect the outputs when the last of the labs exits. This applies even if another lab specifically does labSend() to it.
If your workers interact, then it is common to write the smpd block in terms of "for" or "while" loops. You can use either design pattern:
spmd
for K = 1 : whatever
if labIndex == 1
something
elseif labIndex == 2
something else
end
end
end
or
spmd
if labIndex == 1
for K = 1 : whatever
something
end
elseif labIndex == 2
for K = 1 : whatever
something else
end
end
end
The second of those is more common, as it permits the different labs to have different varieties of work.
The way to get a lab to wait for work is to have the lab execute labReceive() or labBarrier()
There is no clock and no enable port being used here. Each of the labs is a different process that runs as quickly as it can, and the different labs do not try to synchronize until you use labSend() / labReceive(0 or labBarrier(), or until they finish.
  4 Comments
Walter Roberson
Walter Roberson on 12 Sep 2019
The default number of cores is 1 per worker, but I believe that can be increased using the techniques you have indicated.
Star Rats
Star Rats on 18 Jul 2020
Thanks @Walter. The Matlab magician!

Sign in to comment.

More Answers (0)

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!