Parallel pool constant with a parfor and for loop

3 次查看(过去 30 天)
Hi,
as far as I understood the usage of parallel pool constants, it really helps when a parfor loop is called several times, like in the example provided in the Help
data = rand(1000);
c = parallel.pool.Constant(data);
for ii = 1:10
% Run multiple PARFOR loops accessing the data.
parfor jj = 1:10
x(ii,jj) = c.Value(ii,jj);
end
end
However, what if I have the opposite, meaning, I have an external parfor, and the inner loop is a for loop. For example, I have something like this
parfor m = 1 : M
for n = 1 : N
matrix(m,n) = w1(n);
end
end
Matlab complains that w1 is a broadcast variable.
Would it make sense if I would convert w1 to a parallel.pool.Constant?

采纳的回答

Edric Ellis
Edric Ellis 2021-10-15
Yes, parallel.pool.Constant is generally most useful when there are multiple parfor loops. (There are other use-cases such as when you need to construct some data directly on the workers)
In your specific case, if you have only a single parfor loop as you show, then there is no benefit to using parallel.pool.Constant for w1, and you can safely ignore the code analyzer message.
  5 个评论
Edric Ellis
Edric Ellis 2021-10-15
The function that you evaluate using batch is run on worker 1. If you use the 'Pool' parameter to batch, then parallel language constructs such as parfor run on workers 2:N. So yes - a matrix created by your batch function will be created on worker 1.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 MATLAB Parallel Server 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by