Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a function in the following form:

function[...] := With[
  {...},
  Table[
    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.

share|improve this question
1  
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). – Szabolcs Dec 20 '13 at 18:07
up vote 6 down vote accepted

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).

share|improve this answer
1  
+1. Also note from the documentation: "DistributeDefinitions applies itself recursively to any symbols appearing in the definitions of the symbols s_i." – Andreas Lauschke Dec 20 '13 at 21:21
    
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. – Emilio Pisanty Apr 24 at 18:58
    
@EmilioPisanty You know why that is. Describing every corner case is not always the most useful answer to a question. – Szabolcs Apr 24 at 19:14
    
@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 =). – Emilio Pisanty Apr 24 at 19:23

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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