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.
n
are that will be involved, you could potentially just generate a listexpensiveList
off[n]
for the necessaryn
, export it for backup safety, and useexpensiveList[[k[n]]]
instead off[n]
, wherek[n]
is the index off[n]
inexpensiveList
. 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$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$