MATLAB Answers

Memory Parfor Needs more memory than normal

3 ビュー (過去 30 日間)
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
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.

  3 件のコメント

Giannis Lantzanakis
Giannis Lantzanakis 2020 年 5 月 31 日
Thanks for the response, i tried it. It was rly helpfull because i understanded what's going wrong!!
Here is my code
image2 = parallel.pool.Constant(image);
clear image;
parfor k=1:number_of_bands
results(:,:,k) = myfunction(c1,c2,k,image2,struct1,struct2,struct3,struct4,c3,c4,c5);
So by watching my memory (attached image) while i tried the line:
image2 = parallel.pool.Constant(image);
I saw the problem. It reads the image (4gb) and it pastes to memory (an other 4gb), and after that it removes the 1st one.
So when i use the cpde that you suggested it adds data in 1 proccesor at time, so i am not getting out of memory. The problem is that the algorithm can't be executed and it throws a ton of errors (i have a lot of sub functions in this function).
When i run the parfor without parallel.pool.Constant, everything works fine but i am getting out of memory, cause all processors reading data at the same time, and i can work only with 3-4 processors, while after the importing of data i have a lot of free memory.
So if you know some to way to say to the 2nd processor, pls w8 the 1st one processor to read its data and then start reading too, it will solve my problem!
Thanks in advance!
Edric Ellis
Edric Ellis 2020 年 6 月 1 日
You can build a parallel.pool.Constant from a Composite, so you might try something like this:
for idx = 1:numlabs
if idx == labindex
% It's my turn to load the data
c_comp = imread('myLargeImage.png');
% Force all workers to wait
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.
A few things to note here:
  1. 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
  2. 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.
  3. 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.
Giannis Lantzanakis
Giannis Lantzanakis 2020 年 6 月 6 日
I reconstruced my algorith and i cut down my image to smaller parts. Then i loaded to each proccesor a different part, and i merged them again at the end of paraller proccesing. Thanks for your help.


その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by