4

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.

a06e
  • 11,327
  • 4
  • 48
  • 108
  • 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. – DumpsterDoofus Nov 24 '14 at 23:00
  • 1
    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. – Greg Hurst Nov 24 '14 at 23:23

0 Answers0