Hi folks:
I came across some interesting behaviour when calling some MEX files and was curious if this is a known issue, if perhaps I'm doing something wrong, or if it's an artefact of the installation I have.
Specifically, I iterate over a list of model files, and for each timestep I 1) extract points along a grounding line; 2) extract data from the model mesh on these points. I use the InterpFromMesh2d()
function to get extract the data from the model mesh. I notice that the memory used by MATLAB incrementally increases every time I make a call to this function. It is as if the system is holding on to existing arrays of H, Vx, and Vy. The memory is not released if I clear these variables, or use clear all
. The only way I've managed to 'reset' the memory usage is to restart MATLAB. Eventually, I receive a "Killed: 9" error, or when running with the GUI I get a pop-up that I am out of application memory. I track the memory usage simply using the Activity Monitor. I'm running on a Mac with an M1 chip, using MATLAB R2022b (although I had the same behaviour with R2022a). I installed ISSM from source and I use the MEX files from the pre-compiled binaries.
I see the same behaviour when I use a different MEX interpolation, InterpFromMeshToMesh2D()
. I've overcome this by simply using the griddata()
function, but it's much slower. I am curious if anyone has any ideas why I might be having these issues with the MEX files?
I've included a simply working example of the issue that I am facing below. I repeat the code 3 times here, but in reality I run into complete failures when I've iterated through ~ 25 models (with 200 timesteps) in one go.
md = loadmodel('model.mat');
t = [md.results.TransientSolution(:).time];
nT = length(t);
for k = 1:3
for i = 1:nT
gl = isoline(md, md.results.TransientSolution(i).MaskOceanLevelset, 'value', 0, 'output', 'matrix');
x = gl(:,1); y = gl(:,2);
H = InterpFromMesh2d(md.mesh.elements, md.mesh.x, md.mesh.y, md.results.TransientSolution(i).Thickness, x, y);
Vx = InterpFromMesh2d(md.mesh.elements, md.mesh.x, md.mesh.y, md.results.TransientSolution(i).Vx, x, y);
Vy = InterpFromMesh2d(md.mesh.elements, md.mesh.x, md.mesh.y, md.results.TransientSolution(i).Vy, x, y);
end
end
Thanks!
Lawrence