MATLAB: Parfor loop memory keep increasing

algorithmMATLABmemoryparallel computingparfor

I am using R2018b on Windows 10. My code have 3 for loops and I want to use parfor above them. In each parfor loop, it is expected to save a 2D array of size N. I want to keep N around 500. The code is given below
clear; clc;
N = 500;
Ts = 0.1:0.1:10; %range of outter most "for" loop
xmin = -pi; xmax = pi; %range of first "for" loop
ymin = -pi; ymax = pi; %range of second "for" loop
wmin = 3; wmax = 15; %range of "parfor" loop
% building x,y and w arrays
dx = (xmax-xmin)/(N-1);
dy = (ymax-ymin)/(N-1);
dw = (wmax-wmin)/(2*N-1);
xs = xmin : dx : xmax;
ys = ymin : dy : ymax;
ws = wmin : dw : wmax;
L = length(ws); %length of parfor loop
%allocating memory for final results:
D3_I1 = zeros(1,length(Ts));
D3_I2 = zeros(1,length(Ts));
D3_I3 = zeros(1,length(Ts));
D3_I4 = zeros(1,length(Ts));
nT = 1;
for T = Ts
T
%allocating memory for parfor loop results:
D2_I1 = zeros(1,L);
D2_I2 = zeros(1,L);
D2_I3 = zeros(1,L);
D2_I4 = zeros(1,L);
parfor nw = 1:L
w = ws(nw);
% allocating memory for "for" loop results:
R1 = zeros(N,N);
R2 = zeros(N,N);
R3 = zeros(N,N);
R4 = zeros(N,N);
ny = 1;
for ky = ys
nx = 1;
for kx = xs
%following are the main calculations A1,2,3,4.
%in my original code these are actually
%some 3-by-3 matrices, but the final value of A1
%A2,A3 and A4 is just a complex number. this part is
%not very lengthy. it's around 12-15 expressions which
%include matrix multiplications, inverse and trace
%operations on (3by3) matrices.
A1 = x*y;
A2 = x*y;
A3 = x*y;
A4 = x*y;
R1(nx,ny) = A1;
R2(nx,ny) = A4;
R3(nx,ny) = A3;
R4(nx,ny) = A4;
nx = nx + 1;
end
ny = ny + 1;
end
D2_I1(nw) = sum(R1(:))*dx*dy;
D2_I2(nw) = sum(R2(:))*dx*dy;
D2_I3(nw) = sum(R3(:))*dx*dy;
D2_I4(nw) = sum(R4(:))*dx*dy;
WSaved(nw) = w;
end
D3_I1(nT) = sum(D2_I1)*dw;
D3_I2(nT) = sum(D2_I2)*dw;
D3_I3(nT) = sum(D2_I3)*dw;
D3_I4(nT) = sum(D2_I4)*dw;
nT = nT+1;
end
The memory just keep increasing. I have 128GB with 28 cores. I am using 22 cores for parfor. I was able to run it till T = 5. But then I get "memory error".
Why does parfor memory keep increasing? Should not it be resetted for every next T?

Best Answer

Hi Luqman,
Two suggestions:
  1. Look at ticBytes/tocBytes to see how much data is being passed through to parfor
  2. If you're able to upgrade to R2020a, consider using a threads pool, rather than a local pool. See the following for more information on threads vs local
Thanks,
Raymond