Parallel pool constant with a parfor and for loop

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일

0 개 추천

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

Maria
Maria 2021년 10월 15일
when you mention "other use-cases", could the construction of a matrix be such a case?
The matrix I am filling in is quite big. It is actually a 3D matrix that easily occupies 15 GB. As I am using parfor, I cannot use distributed array. I am running some tests and I see that only the first node in my cluster gets its memory full (16 GB total, so I am at the limit), while the other 7 don't.
I am wondering, would it help if I would declare the matrix as parallel constant?
Yes, building a large matrix directly on the workers can be a reason to use parallel.pool.Constant as it allows you to avoid transferring it from the client. So, you might do:
c = parallel.pool.Constant(@() ones(10000)) % calls ones(10000) directly on the worker
Maria
Maria 2021년 10월 15일
편집: Maria 2021년 10월 15일
okay that's good to know! Just another question. When I send a batch job, and the matrix is created inside the function that is called from the batch, the matrix should be automatically created on one of the worker (if I do not use distributed), correct? Or also in this case I should use a parallel constant?
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.
Maria
Maria 2021년 10월 15일
very clear, thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

릴리스

R2021a

질문:

2021년 10월 14일

댓글:

2021년 10월 15일

Community Treasure Hunt

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

Start Hunting!

Translated by