Let's define a simple recursion.

f[1] = 1;
f[n_] := f[n] = f[n - 1]*n;

If I evaluate f in parallel

ParallelTable[f[i], {i, 10}];

and then query f

f[n_]:=f[n]=f[n-1] n


How can I tell the sub kernels to build up the recursion, so that




f[n_]:=f[n]=f[n-1] n

Thanks !

PS: of course the actual function is not that trivial.

  • 1
    $\begingroup$ Does SetSharedFunction[f] do what you want? $\endgroup$ – sebhofer Aug 7 '12 at 16:49
  • $\begingroup$ Indeed, thank you! Should I remove my question? $\endgroup$ – chris Aug 7 '12 at 17:18
  • $\begingroup$ On the other hand it does not re-export to the different kernels the known definitions. Table[f[i], {i, 5000}]; // Timing (* {0.005126,Null} ) ParallelTable[f[i], {i, 5000}]; // Timing ( {6.69067,Null} *) $\endgroup$ – chris Aug 7 '12 at 17:22
  • $\begingroup$ Indeed. I can remember that I once ran into that problem but I couldn't find a solution to distribute DownValues from the subkernels. Maybe you can formulate your question more generally (with a different title?), so you get more attention! $\endgroup$ – sebhofer Aug 7 '12 at 18:05

@sebhofer provides a partial answer via the command


which collects to the master kernel the accumulated downvalues. On the other hand, the slave kernels seem unaware of these.


ParallelTable[f[i], {i, 5000}]; 
Table[f[i], {i, 5000}]; // Timing 

(* {0.005126,Null} *)

which suggests the master kernel does remember the previous definitions. On the other hand,

ParallelTable[f[i], {i, 5000}]; // Timing 

(* {6.69067,Null} *)

still takes quite some time.

| improve this answer | |
  • $\begingroup$ The reason the second invocation takes a while has changed, though--f exists only in the master kernel and is fetched using a callback every time it's referenced in one of the slaves. This is still basically a serial operation, but now it involves a lot of communication overhead. Unfortunately it isn't easy to do what you want given the way the Parallel` package works. I thought you could perhaps try something like f[n_]:=g[n]=f[n-1] n with SetSharedFunction[g]; DistributeDefinitions[f], but this doesn't seem to work... sorry, no time for a more considered response! $\endgroup$ – Oleksandr R. Aug 8 '12 at 8:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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