Behavior of persistent variables with parfeval

2 views (last 30 days)
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!
% New code example (a chopped down version of the function)
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)
% Replace the Y data in each curve
set(plotSST1, 'YData', temp);
set(plotSST2, 'YData', [hist hist]);
set(plotSD1, 'YData', S(:,1)./C(:,1));
set(plotSD2, 'YData', S(:,2)./C(:,2));
% and so on for all the plots...
end
if first % Sorry about the odd logic with "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);
  2 Comments
James Ryan
James Ryan on 6 Sep 2016
Just for fun, here's the speedup I'm seeing. If the new code would accelerate as well as the old, I'd be very happy.
Now normalized to the speed using one worker:
GTRI_user
GTRI_user on 14 Jul 2018
Has this been clarified anywhere in the documentation yet? I still haven't seen Mathworks state what the correct behavior is supposed to be for persistent variables in functions called via parfeval. I think you are saying that they DO behave as normal, i.e., they keep their values from one parfeval call to another right?

Sign in to comment.

Answers (0)

Categories

Find more on Asynchronous Parallel Programming in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!