Parallel pool constant with a parfor and for loop

7 views (last 30 days)
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);
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);
Matlab complains that w1 is a broadcast variable.
Would it make sense if I would convert w1 to a parallel.pool.Constant?

Accepted Answer

Edric Ellis
Edric Ellis on 15 Oct 2021
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.

Sign in to comment.

More Answers (0)


Find more on Parallel for-Loops (parfor) 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!