MATLAB: Parfor: why is 2x memory being used for sliced variable

image processingMATLABParallel Computing Toolboxparfor

I would like to use parfor to register a series of 2D images, which are stored in a 3D matrix called cube. In order to do this with parfor I'm storing the image cube as a cell array cube_cell where each element cube_cell{i} is a 2D array. I perform the loop as follows:
fixed = cube_cell{1};
[optimizer, metric] = imregconfig('monomodal');
parfor i=2:nframes
cube_cell{i} = imregister(cube_cell{i},fixed,'translation');
end
I thought cube_cell would be a slice variable, so that the entire cell array would be broken into pieces, but I found that the memory consumption jumps by a factor of 2 once the parfor loop gets started. I have many images, so this is a lot of memory. I guess 2x is better than 8x since I have 8 cores, but am I doing something wrong?

Best Answer

Seems good to me.
The memory consumption may not always be due to copying the entire cube_cell to all worker. Initializing a parpool via parfor will increase memory consumption as your computer opens up another MATLAB worker to do the parallel processing. Once the other worker is ready, THEN a slice of cube_cell is sent to the worker to do the math.
If numel(cube_cell) == 9 and you use 8 cores, then you could have 2 copies of cube_cell in your computer (1 full copy + 8 slices in 8 workers) until the computation is finished. The trade-off for parallel processing is time vs memory.
To save memory, see if you can process images in uint32 matrices instead of double matrices.