SPMD to process different iteration on different worker

4 次查看(过去 30 天)
I'm trying to use the spmd to process different iteration. The current problem I have is the spmd function is repeating the same processing on different worker. I have tried to use parfor but the parfor cannot guarantee the iteration is process in order. What I am looking for is to have the iteration process in order. For example, from 0>1>2>3>4,.... Below is the sample code.
clear
clc
iCellNum = 1;
activeUeNums = 1;
tic
ticBytes(gcp)
for ttiNum = 1 : 20
spmd
if true
fprintf('%s processing subframe: %d\n', getTimeNow, ttiNum-1);
end
end
end
toc
tocBytes(gcp)

回答(1 个)

Edric Ellis
Edric Ellis 2022-8-2
It's not entirely clear to me quite what you're after. You might want for (drange) which spreads the iterations of a for loop over different workers in spmd.
parpool("local");
Starting parallel pool (parpool) using the 'local' profile ... Connected to the parallel pool (number of workers: 2).
spmd
for ii = drange(1:20)
fprintf('Worker %d processing iteration %d\n', labindex, ii);
end
end
Worker 1: Worker 1 processing iteration 1 Worker 1 processing iteration 2 Worker 1 processing iteration 3 Worker 1 processing iteration 4 Worker 1 processing iteration 5 Worker 1 processing iteration 6 Worker 1 processing iteration 7 Worker 1 processing iteration 8 Worker 1 processing iteration 9 Worker 1 processing iteration 10 Worker 2: Worker 2 processing iteration 11 Worker 2 processing iteration 12 Worker 2 processing iteration 13 Worker 2 processing iteration 14 Worker 2 processing iteration 15 Worker 2 processing iteration 16 Worker 2 processing iteration 17 Worker 2 processing iteration 18 Worker 2 processing iteration 19 Worker 2 processing iteration 20
  4 个评论
Yi Xien Yap
Yi Xien Yap 2022-8-3
I see what you mean, the worker will start whenever they are free.
Walter Roberson
Walter Roberson 2022-8-3
Do you need spmd specifically? That is, are you doing labSend(), or using labBarrier to control access to a resource?
If not then consider queuing a bunch of jobs using parfeval.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Loops and Conditional Statements 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by