I have a project in which the results of each of about 2000 test cases need to be plotted and saved as a PDF (the need to save so many plots is beyond my control). The math to compute the test cases is several times faster than the plotting and PDF generation, so I have been using parfeval to make the PDFs in parallel workers. That works great, with a nearly linear speedup until I catch up with the test case calculations.
I then learned that the plotting code itself could be more efficient by re-using the figure, legends, and other items which are constant between cases (news to me - I just started with MATLAB last month). Running serially, this also makes a big difference, saving over 40% in the PDF generation.
Here's the problem. I used persistent variables to be able to re-use things between calls. When I go back to parallel computation, I still get the correct outputs and there are no error messages, but there is a big memory leak. Workers which used to stabilize at about 500 MB are now getting to almost 900 MB after just 25 calls per worker. The code progressively slows down.
What is the expected behavior of persistent variables with parfeval? I had assumed that each worker would have its own independent copy, but maybe that's not what's happening.
MATLAB 2016a, Windows 7, 6 cores, 16 GB.
Thanks for any help!
persistent figHandle;
persistent plotSST1 plotSST2;
persistent plotSD1 plotSD2;
if isempty(figHandle) || ~isvalid(figHandle)
close all;
figHandle = figure(1);
set(gcf,'Visible', 'off');
first = true;
else
first = false;
set(0, 'CurrentFigure', figHandle)
set(plotSST1, 'YData', temp);
set(plotSST2, 'YData', [hist hist]);
set(plotSD1, 'YData', S(:,1)./C(:,1));
set(plotSD2, 'YData', S(:,2)./C(:,2));
end
if first
Lots of code deleted which uses subplot to create 6 plots with 2 to
3 curves each. It is run only when first is true.
end
print('-dpdf', someFileName);