I have a function in the following form:

function[...] := With[
    anotherfunction[i, j, ...],
    {i, ...},
    {j, ...},

If I'd like to replace Table with ParallelTable, do I need to put DistributeDefinitions["Global`"] somewhere? If yes, where? (Before With, before ParallelTable, or before calling function?)

anotherfunction is defined globally, and uses other global variables.

  • 1
    $\begingroup$ I added the backtick. Click the edit link and see the source on how to do it (you need to surround the code with $n+1$ backticks if you want to include $n$ backticks). $\endgroup$ – Szabolcs Dec 20 '13 at 18:07

ParallelTable will automatically distribute the definitions of symbols used in the body of the table, as well as any dependencies, in version 8 and later. It is not necessary to use DistributeDefinitions with ParallelTable in these versions.

Note that ParallelEvaluate does not automatically distribute definitions like Parallelize, ParallelTable, ParallelMap, ParallelDo, etc. do. You do need to use DistributeDefinitions if you rely on ParallelEvaluate, however, you do not need to distribute the definitions of all symbols. Just distribute the symbol that you are actually using, anotherfunction in this case. DistributeDefinitions is smart enough to auto-distribute all the dependencies as well (unless you do something unusual such as converting strings to symbol names).

  • 1
    $\begingroup$ +1. Also note from the documentation: "DistributeDefinitions applies itself recursively to any symbols appearing in the definitions of the symbols s_i." $\endgroup$ – Andreas Lauschke Dec 20 '13 at 21:21
  • $\begingroup$ I'm not sure the opening sentence is correct, or DistributeDefinitions would be of very-corner-case usefulness only. As a simple counterexample, try BeginPackage["Test`"]; Test`f = Function[x, $KernelID]; EndPackage[]; ParallelTable[f[x], {x, 8}], where the table is obviously returning the f[x] unevaluated for the master kernel to rework. Adding DistributeDefinitions["Test`"] makes the table return a correct list of nonzero IDs. $\endgroup$ – Emilio Pisanty Apr 24 '16 at 18:58
  • $\begingroup$ @EmilioPisanty You know why that is. Describing every corner case is not always the most useful answer to a question. $\endgroup$ – Szabolcs Apr 24 '16 at 19:14
  • $\begingroup$ @Szabolcs Apologies, I didn't mean to come across anywhere near aggressive. My point was that it would be helpful to add '(excluding functions in packages)', or something similar, just as a note to new users that there are special considerations in place if packages or other contexts are in use. To be frank, though, I still don't fully understand what does and does not get distributed by default by ParallelTable, so I didn't want to jump into the wiki and write something that's wrong. If you feel that it's too much load on an introductory answer, though, that's still fine =). $\endgroup$ – Emilio Pisanty Apr 24 '16 at 19:23

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.