I want to evaluate a table of functions with ParallelTable
. Here is the serial code:
f[i_] := f[i] =
Quiet@
NIntegrate[
Sin[Sin[Sin[x^(i + 3)]]], {x, 0, 3},
MaxRecursion -> 50, PrecisionGoal -> 40
]
Table[f[i], {i, 1, 3}]
{0.288687, 0.24239, 0.204309}
When I do this, I can then access the memoized values of f[i]
immediately without recalculation:
f[3] // Timing
{0., 0.204309}
Now here is the parallel version:
ParallelTable[f[i], {i, 1, 3}]
{0.288687, 0.24239, 0.204309}
In this case, however, I don't have access to the memoized values of f[i]
; the values have to be recalculated, as evidenced by the longer time it takes to return them:
f[3] // Timing
{2.23, 0.204309}
What is the problem with the shared data?
f[3]
in the front end), the main kernel has no record of that value and has to recalculate it. The same problem was discussed in this question, where you may also find a possible solution: Parallel evaluation of function with memorization. $\endgroup$SetSharedFunction[]
. $\endgroup$