Shared variable / memory usage / parfor
31 次查看(过去 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!