
Parallel EvaluationSending Commands to Remote KernelsRecall that connections to remote kernels, as opened by LaunchSlave, are represented as kernel objects. See the chapter Starting Remote Kernels for details. The commands in this section take such kernel objects as arguments and send or receive Mathematica expressions to remote kernels using these links. In the following, the variable link is a kernel returned by LinkLaunch or LinkConnect. LowLevel Send and ReceiveSend[link, cmd]  sends cmd for evaluation to the remote kernel connected to link; returns link 
Send[{links...}, cmd]  sends cmd for evaluation to all remote kernels listed 
ReceiveIfReady[link]  returns a result waiting on the given link; returns $NotReady if no result is waiting 
ReceiveIfReady[link, h]  wraps the result in h[...] before returning it 
Receive[link]  waits for a result on the given link and returns it 
Receive[{links...}]  waits for a result on each of the links and returns them in a list 
Receive[links, h]  wraps the results in h[...] before returning them 
RemoteEvaluate[cmd, link]  sends cmd for evaluation to the remote kernel connected to link, then waits for the result and returns it 
RemoteEvaluate[cmd, {links...}]  sends cmd for evaluation to the remote kernels given, then waits for the results and returns them 
RemoteEvaluate[cmd]  sends cmd for evaluation to all remote kernels and returns the list of results. Equivalent to RemoteEvaluate[cmd, $Slaves] 
Sending and receiving commands to and from remote kernels. Send has the attribute HoldRest so that the given command is not evaluated before it is sent to the remote kernel. RemoteEvaluate has the attribute HoldFirst. RemoteEvaluate[cmd, link] is equivalent to the combination Send[link, cmd]; Receive[link]. With ReceiveIfReady[link], you can poll several open links for results. You cannot use RemoteEvaluate while a concurrent computation involving Queue or Wait is in progress. See the chapter Concurrency: Managing Parallel Processes for details. Values of VariablesValues of variables defined on the local master kernel are usually not available to remote kernels. If a command you send for evaluation refers to a variable, it usually will not work as expected. The following piece of code will return False because the symbol a will most likely not have any value at all on the remote kernel. A convenient way to insert variable values into unevaluated commands is to use With, as demonstrated in the following command. The symbol a is replaced by 2, then the expression 2 =2 is sent to the remote kernel where it evaluates to True. If you need variable values and definitions carried over to the remote kernels, use ExportEnvironment or the package Parallel`VirtualShared`, which is part of Parallel Computing Toolkit. Iterators, such as Table and Do, work in the same way with respect to the iterator variable. Therefore, a statement like the following will not do the expected thing. The variable i will not have a value on the remote kernel. You can use the following command to accomplish the intended iteration on the remote kernel. This substitutes the value of i into the argument of RemoteEvaluate. Pattern variables, constants, and pure function variables will work as expected on the remote kernel. Each of the following three examples will produce the expected result. Parallel Evaluation of ExpressionsDispatching Evaluations to Remote KernelsParallelDispatch[h[e_{1},e_{2},...,e_{n}],{k_{1},k_{2},...,k_{m}}] 
 evaluatese_{i}on kernel k_{i} and returns h[ r_{1}, r_{2}, ... r_{n}],wherer_{i}is the result of evaluatinge_{i}. The default list of kernels is $Slaves 
ParallelEvaluate[h[e_{1},e_{2},...,e_{n}],f,comb] 
 evaluates f[h[e_{1},e_{2},...,e_{n}]] in parallel by distributing chunks f[h[e_{i},e_{i+1},...,e_{i+k}]] to all kernels and combining the results with comb[]. 
ParallelEvaluate[h[e_{1},e_{2},...,e_{n}],f] 
 the default combiner comb is h, if h has attribute Flat, and Join otherwise 
ParallelEvaluate[h[e_{1},e_{2},...,e_{n}]] 
Basic parallel dispatch of evaluations. In ParallelDispatch[h [e _{1},e _{2},...,e _{n}],{k _{1},k _{2},...,k _{m}}], the number m of kernels must be at least as large as the number n of expressions. ParallelDispatch has the attribute HoldFirst so that h [e _{1},e _{2},...,e _{n}] is not evaluated on the master kernel before the parallelization. ParallelDispatch[{e _{1},e _{2},...,e _{n}},{k _{1},k _{2},...,k _{n}}] is equivalent to Receive[Inner[Send,{k _{1},k _{2},...,k _{n}},{e _{1},e _{2},...,e _{n}}]]. ParallelEvaluate[h [e _{1},e _{2},...,e _{n}],f,comb] breaks h [e _{1},e _{2},...,e _{n}] into as many pieces h [e _{i},e _{i+1},...,e _{i+k}] as there are remote kernels, evaluates f[h [e _{i},e _{i+1},...,e _{i+k}]] in parallel (using ParallelDispatch[]), then combines the results r _{i} using comb[r _{1},r _{2},...,r _{m}]. ParallelEvaluate has the attribute HoldFirst so that h [e _{1},e _{2},...,e _{n}] is not evaluated on the master kernel before the parallelization. The size of the pieces of the input expression is chosen to be proportional to the remote processor speed estimates for optimal load balancing. ParallelEvaluateParallelEvaluate is a general and powerful command with default values for its arguments that are suitable for evaluating elements of containers such as lists and associative functions. Evaluating Listlike ContainersIf the result of applying the function f to a list is again a list, ParallelEvaluate[{e _{1},e _{2},...,e _{n}},f] simply applies f to pieces of the input list and joins the partial results together. Out[2]=  
The result is the same as that of Prime[{1,2,3,4,5,6,7,8,9}], but the computation is done in parallel. The default function is Identity, therefore, ParallelEvaluate[{e _{1},e _{2},...,e _{n}}] simply evaluates the elements e_{i} in parallel. Out[3]=  
If the result of applying the function f to a list is not a list, a custom combiner has to be chosen. The function Function[li,Count[li,_?OddQ] counts the number of odd elements in a list. To find the total number of odd elements, add the partial results together. Out[4]=  
Evaluating Associative OperationsIf the operation h in h [e _{1},e _{2},...,e _{n}] is associative (has attribute Flat), the identity holds; with the default combiner being h itself, the operation is parallelized in a natural way. Here all numbers are added in parallel. Out[5]=  
Out[6]=  
Parallel Mapping and IteratorsThe commands in this section are fundamental to parallel programming in Mathematica. ParallelEvaluate[h[e_{1},e_{2},...]]  evaluates the elements e_{i} in parallel and returns h[ r_{1}, r_{2}, ...], where r_{i} is the result of evaluating e_{i} 
ParallelMap[ f, h[ e_{1}, e_{2}, ...]]  evaluates h[ f[e_{1}], f[e_{2}] ,...] in parallel. 
ParallelTable[expr,{i,i_{0},i_{1},di},{j,j_{0},j_{1},dj},...] 
 builds Table[expr,{i,i_{0},i_{1},di},{j,j_{0},j_{1},dj},...] in parallel; parallelization occurs along the first (outermost) iterator {i, i_{0}, i_{1},di } 
ParallelSum[...], ParallelProduct[...] 
 computes sums and products in parallel 
Parallel evaluation, mapping, and tables.
ParallelEvaluate has attribute HoldFirst, so that its argument is not evaluated on the local kernel. After receiving the results, r _{i}, the expression h[ r_{1}, r_{2},...] is further evaluated normally on the local kernel. The symbol List is often used as the head h. is a parallel version of evaluating the individual f[e_{i}]in parallel rather than sequentially. ParallelEvaluate and related commands use all available remote kernels on the list $Slaves. Side EffectsUnless you use shared variables, the parallel evaluations performed are completely independent and cannot influence each other. Furthermore, any side effects, such as assignments to variables, that happen as part of evaluations will be lost. The only effect of a parallel evaluation is that its result is returned at the end. ExamplesFirst, load the package and then start several remote kernels. The sine function is applied to the given arguments. Each computation takes place on a remote kernel. Out[2]=  
This particular computation is almost certainly too trivial to benefit from parallel evaluation. The overhead required to send the expressions Sin[0], Sin[], and so on to the remote kernels and to collect the results will be larger than the gain obtained from parallelization. Factoring integers of the form 111...1 takes more time, so this computation can benefit from parallelization. Out[3]=  
Alternatively, you can use ParallelTable. Here a list of the number of factors in is generated. Out[4]//TableForm=  
Automatic ParallelizationParallelEvaluate[cmd[list,arguments...]] recognizes if cmd is a Mathematica function that operates on a list or other long expression in a way that can be easily parallelized and performs the parallelization automatically. You do not need to figure out suitable f and comb for ParallelEvaluate[list,f,comb]. The list of commands that can be parallelized is kept in the variable $ParallelCommands. Out[5]=  
Out[6]=  
Out[7]=  
Out[8]=  
Not all uses of these commands can be parallelized. A message is generated and the evaluation is performed sequentially on the master kernel if necessary. Out[9]=  
