possible GPU memory leak in mmclab

17 views
Skip to first unread message

Ife E

unread,
Apr 10, 2026, 9:27:29 AMApr 10
to mcx-users
Dear Prof. Fang

Thanks again for maintaining such a useful package!

I've been running some code with multiple source locations in mmclab. As far as I understand there isn't support yet to do this directly so I'm looping over different source positions. Code is attached below

for i_incl = 7:size(incl,1)
% generate mmc cfg geometric info from DOGPUP mesh nand inclusion location
% and copy cfg mesh info to DOGPUP mesh
g = 0.85; % scattering anistropy
clear cfg
[cfg,mesh_temp] = dMesh2mmc(mesh,incl(i_incl,:),g,mua_i); % add inclusion to mesh and generate DOGPUP twin
mesh_temp = add_optode(mesh_temp,optode);
% sim init
cfg.nphoton = 5e8;
cfg.srctype = 'pencil';
cfg.tstart = 0;
cfg.tstep = dt;
cfg.tend = t(end)+dt;
cfg.outputtype = 'flux';
cfg.isnormalized = 1;
cfg.isreflect = 0;
cfg.isspecular = 0;
flu_td = zeros(size(cfg.node,1),length(t),size(optode.s_positions,1)); % fluence rate for all sources
for i_source = 1:size(optode.s_positions,1)
src_pos = optode.s_positions(i_source,:);
reset(gpuDevice);
% check source locations on mesh
TR = triangulation(cfg.elem,cfg.node);
[~,bary] = pointLocation(TR,optode.s_positions);
% shift sources that lie on vertex
if any(bary==1,2)
src_pos = src_pos + 1e-6.*[1 0 1];
end
pause(0.5)
cfg.srcpos = src_pos;
cfg.srcdir = optode.s_dirs(i_source,:);
fprintf('\n\nINCL: %d SRC: %d\n',i_incl,i_source)
fprintf('==========================\n')
struct_td = mmclab(cfg);
flu_td(:,:,i_source) = struct_td.data;
reset(gpuDevice);
end
data_td(:,:,i_incl) = meas_flu(mesh_temp,flu_td); % detected fluence
reset(gpuDevice);
save("data\sim_data\fwd_data_depth_mc.mat","data_td","incl");
end

After enough iterations eventually the code stalls, monitoring my GPU usage it seems each iteration usage of dedicated memory increases. Not sure if this is an issue with my implementation or something else happening with mmclab. Easy enough to work around in my case by saving at each step and manually resuming after a PC restart. Any advice would be appreciated!

All the best,
Ife

Qianqian Fang

unread,
Apr 10, 2026, 9:41:52 AMApr 10
to mcx-...@googlegroups.com, Ife E

hi Ife,

yes, there is a known memory leakage issue when using NVIDIA GPU with OpenCL (mmclab/mcxlabcl), see these previous threads

https://groups.google.com/g/mmc-users/c/Ryzx_JTBBsM/m/x97x54kMAgAJ

https://groups.google.com/g/mcx-users/c/f31KBsmj5aw/m/H_fabg3zAwAJ

https://groups.google.com/g/mmc-users/c/1XFeeeKz_nw/m/QymqydiICQAJ


since v2023, mmc supports triple-backend - sse4, opencl and cuda - in the trinity-build. running mmclab with cuda backend (cfg.compute='cuda') does not have this leakage issue.


in the mmclab download folder, we have provided a precompiled mex file support cuda, but only for Linux:

mmclab-trinity-linux-x86_64-v2025.10.zip, see https://sourceforge.net/projects/mcx/files/mmc/mmclab/2.8%20%28v2025.10%20Bubble%20Tea%29/


it is possible to build it for Windows, but I haven't found time to setup the build environment. you can't run cuda on MacOS unfortunately.


Qianqian

--
You received this message because you are subscribed to the Google Groups "mcx-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mcx-users+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/mcx-users/62f3c56f-8256-4303-9cab-0b0b11cc4603n%40googlegroups.com.

Ife E

unread,
Apr 10, 2026, 9:55:12 AMApr 10
to mcx-users
Hi Qianqian,

Thank you very much! I probably should have searched to see if users had the same issue first, nonetheless thanks for getting back to me.

Ife

Reply all
Reply to author
Forward
0 new messages