How does labindex in spmd works?
5 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
Walter Roberson
2019-8-30
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 个评论
Walter Roberson
2019-9-12
The default number of cores is 1 per worker, but I believe that can be increased using the techniques you have indicated.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Startup and Shutdown 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!