4
$\begingroup$

I have an expensive function on discrete values,

f[n_] := f[n] = n^2 (* Expensive calculation *)

Since f[n] is an expensive computation, I only want to compute each value of f once, so I memoize the value.

Now I want to evaluate a complicated sum involving values of f[n]:

ParallelSum[f[i]f[j], {i, 1, 10}, {j, 0, i}] (* complicated sum *)

and it would be nice to evaluate this sum in parallel.

The problem is the code I just wrote doesn't work as expected. I am getting multiple evaluations of f[n] in each kernel. How can I fix my code so that a single computation of f is made for each value of n and this is communicated between kernels?

Note: You can assume that f[n] is so expensive that we can ignore the overhead involved in communicating the computed values of f[n] between kernels.

$\endgroup$
2
  • $\begingroup$ If you know in advance what the values of n are that will be involved, you could potentially just generate a list expensiveList of f[n] for the necessary n, export it for backup safety, and use expensiveList[[k[n]]] instead of f[n], where k[n] is the index of f[n] in expensiveList. Of course, it would be preferable to use memoization, but in case that's difficult you can always use this as an alternate method. $\endgroup$ Nov 24, 2014 at 23:00
  • 1
    $\begingroup$ You should look into SetSharedFunction. I'm not sure how much parallelization you will lose though. I tried some toy examples and it ended up slowing things down a lot. $\endgroup$
    – Greg Hurst
    Nov 24, 2014 at 23:23

0

Browse other questions tagged or ask your own question.