Memory Parfor Needs more memory than normal
4 ビュー (過去 30 日間)
古いコメントを表示
Giannis Lantzanakis
2020 年 5 月 28 日
コメント済み: Giannis Lantzanakis
2020 年 6 月 6 日
Hello, i need your help. I dont understand something with parfor loop.
I load an image which is 8GB.
I use parfor loop with 4 threads.
So the total usage of Ram it had to be 8GB + 4thread*8GB = 40GB.
But when the parfor loops starts, the momory increases more than 64GB (this is my total memory)
everything lags, and after some minutes the memory goes back to 40GB and the program runs normally.
I dont understand. Why does it happen? Is there any possible solution? If it needs 40GB why it gets more memory in the beggining?
For bigger data or if i increase the processors i get a blue screen. Pls help!
0 件のコメント
採用された回答
Edric Ellis
2020 年 5 月 29 日
When you transfer data to the workers for a parfor loop, there is a transient increase in memory over and above what you'd expect due to the way data is transferred. You can perhaps work around this by loading the image data directly on the workers. One way to do this is by using parallel.pool.Constant. When constructing your Constant, you can pass in a functino handle to load the data, and this gets executed on the worker. Something like this perhaps:
c = parallel.pool.Constant(@() imread('myLargeImage.png'));
parfor idx = 1:N
im = c.Value; % Get the actual image from within the Constant
out(idx) = myImageProcessingFunction(im, idx); % or whatever.
end
3 件のコメント
Edric Ellis
2020 年 6 月 1 日
You can build a parallel.pool.Constant from a Composite, so you might try something like this:
spmd
for idx = 1:numlabs
if idx == labindex
% It's my turn to load the data
c_comp = imread('myLargeImage.png');
end
% Force all workers to wait
labBarrier();
end
end
c = parallel.pool.Constant(c_comp);
parfor idx = 1:N
im = c.Value; % Get the actual image from within the Constant
out(idx) = myImageProcessingFunction(im, idx); % or whatever.
end
A few things to note here:
- It is important to ensure that you only load up the image directly on the workers - not on the client and then send to the workers
- The spmd block loops over workers, forcing them to load the image one at a time. labBarrier causes all workers to synchronise so that you know only one is executing imread at any one time.
- Creating the parallel.pool.Constant from the Composite does not need any large data transfers.
If you follow the pattern as shown, then the image will never be transferred from the client to the workers, and each worker will load the image in strict sequence, one at a time. This may or may not be sufficient to prevent the out-of-memory errors you're hitting. If it is not, then you might need to look at a different way of parallelising your algorithm - perhaps by having each worker operate on only a panel of the image.
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Image Processing Toolbox についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!