I have a top-level function which operates on some data:


which makes parallelized calls to an optimization routine which tries to find the best integers associated with the data point. The optimization function is shown here in highly simplified form:


which itself makes many calls to a memoized cost function (the real optimization method I'm using makes a fair number of duplicate calls to the cost function, and achieves a ~2x performance boost from memoization):


After running this for a long time on a large data set, I noticed the subkernels' memory footprints expanding to hundreds of megabytes, due to the memoized cost functions being cached locally, and I worry about slowdowns from the garbage piling up. However, I also know that data will never contain duplicate points, due to it being essentially random floating point numbers.

That means if I write the top-level function as a loop which optimizes for a batch of datapoints in parallel, there should be no downside to clearing the memoized functions in all subkernels to free up memory before moving on to the next batch of datapoints.

I have been unable to get this to work (referencing this question: Clearing distributed definitions from remote kernels). After a ParallelEvaluate[Clear[cost]], I'm unable to refresh the function definition on the subkernels, and errors result.

Furthermore, I'm not certain there is any speed to be gained by doing this. I suppose it depends on the data structure used for memoization within each subkernel. Hash tables, for example, have basically constant lookup time.

Does anyone know if there is performance to be gained, and if so, how to implement this sort of parallel-memoized garbage collection?


1 Answer 1


I can't say for sure but I wouldn't expect much speedup from deleting the existing memoized values as long as everything fits into memory. I don't know the internals so I can't say how exactly the lookup is implemented but could well imagine that hash tables are used for constant arguments, at least largish amounts of memoized values haven't bothered me in the past. Anyway, here is how you can delete just the memoized values (of course that does a somewhat inefficient pattern matching itself...):

ParallelEvaluate[DownValues[cost] = DeleteCases[
  Verbatim[RuleDelayed][HoldPattern[_[cost[{__Integer}, {__Integer}]]], _]

for other definitions of cost you might need to adopt the pattern. You could also try something like:

Select[DownValues[cost], Not[FreeQ[#, Pattern]] &]

instead of the DeleteCases. If you have the impression that deleting the memoized values could help but the deletion with the above is too slow I'm sure there is more efficient code which would achieve the same but probably be more effort to get working...

  • $\begingroup$ Thanks, I will experiment with this method. $\endgroup$
    – phosgene
    Apr 27, 2014 at 16:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.