Shared variable / memory usage / parfor
조회 수: 10 (최근 30일)
이전 댓글 표시
Hello,
I am wondering what are the real memory requirements to perform a computation like this:
A = ones(10^4);
parfor j=1:100
v = zeros(1,10^4);
r = v*A;
end
I've ran this code on R2018a and linux mint 19. By inspecting memory usage using 'top' it seems variable A is not shared but otherwise copied. Memory usage of each worker is almost size(A) in particular at the beginning of the computation. Later, memory usage of each worker drops to a number approximately size(A)/2. This is a real issue when the size of A is almost all the available RAM, since in theory I should be able to compute such a large A.
Please note that the real code I am trying to develop is not the one presented here, but it is analogous.
Is there any way to enforce communication rather than copy matrix A to every worker?
Thanks.
댓글 수: 0
답변 (1개)
Edric Ellis
2021년 7월 26일
The workers in a "local" parallel pool (the default) are separate processes. Therefore each worker process must have a complete copy of A in memory for this code as written. One thing to be aware of: large matrix operations are typically intrinsically multithreaded by MATLAB itself, so there is often no benefit to running N worker processes instead of N threads if most of the time in your program is taken up with large matrix operations (and, in fact, sending the data to and from the worker processes can often dominate the time taken).
댓글 수: 2
Edric Ellis
2021년 7월 27일
The local copy is required because the workers are separate processes, and there is currently no way using PCT for MATLAB to share the contents of an array across multiple processes. You might be able to try using https://www.mathworks.com/matlabcentral/fileexchange/28572-sharedmatrix . In recent versions of MATLAB, arrays can be shared across the threads of a thread parpool - https://www.mathworks.com/help/parallel-computing/parallel.threadpool.html .
참고 항목
카테고리
Help Center 및 File Exchange에서 Parallel Computing Fundamentals에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!