# Parallel Evaluation

## Sending Commands to Remote Kernels

Recall that connections to remote kernels, as opened by LaunchKernels, are represented as kernel objects. See Connection Methods for details. The commands in this section take parallel kernels as arguments and use them to carry out computations.

### Low-Level Parallel Evaluation

ParallelEvaluate[cmd,kernel] | sends cmd for evaluation to the parallel kernel kernel, then waits for the result and returns |

ParallelEvaluate[cmd,{kernels}] | sends cmd for evaluation to the parallel kernels given, then waits for the results and returns them |

ParallelEvaluate[cmd] | sends cmd for evaluation to all parallel kernels and returns the list of results; equivalent to ParallelEvaluate[cmd, Kernels[]] |

Sending and receiving commands to and from remote kernels.

ParallelEvaluate has the attribute HoldFirst.

You cannot use ParallelEvaluate while a concurrent computation involving ParallelSubmit or WaitAll is in progress. See "Concurrency: Managing Parallel Processes" for details.

### Values of Variables

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

In[15]:= |

Out[15]= |

In[16]:= |

Out[17]= |

In[18]:= |

Out[18]= |

If you need variable values and definitions carried over to the remote kernels, use DistributeDefinitions or shared variables.

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.

In[20]:= |

Out[20]= |

In[21]:= |

Out[21]= |

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.

In[22]:= |

Out[22]= |

In[23]:= |

In[24]:= |

Out[24]= |

In[25]:= |

Out[25]= |

## Parallel Evaluation of Expressions

ParallelCombine[f,h[e_{1},e_{2},...,e_{n}],comb] | evaluates in parallel by distributing chunks to all kernels and combining the results with comb[] |

ParallelCombine[f,h[e_{1},e_{2},...,e_{n}]] | the default combiner comb is h, if h has attribute Flat, and Join otherwise |

ParallelCombine[f, h[e_{1}, e_{2}, ..., e_{n}], comb] breaks into pieces , evaluates in parallel, then combines the results using . ParallelCombine has the attribute HoldFirst, so that 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.

### ParallelCombine

ParallelCombine 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 List-like Containers

If the result of applying the function f to a list is again a list, ParallelCombine[f, h[e_{1}, e_{2}, ..., e_{n}], comb] simply applies f to pieces of the input list and joins the partial results together.

In[2]:= |

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.

If the function is Identity, ParallelCombine simply evaluates the elements in parallel.

In[14]:= |

Out[14]= |

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.

In[17]:= |

Out[17]= |

#### Evaluating Associative Operations

If the operation h in 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.

In[27]:= |

Out[27]= |

In[28]:= |

Out[28]= |

### Parallel Mapping and Iterators

The commands in this section are fundamental to parallel programming in *Mathematica*.

ParallelMap[f,h[e_{1},e_{2},...] | evaluates 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 |

ParallelSum[...],ParallelProduct[...] | computes sums and products in parallel |

Parallel evaluation, mapping, and tables.

ParallelMap[f, h[e_{1}, e_{2}, ...]] is a parallel version of evaluating the individual in parallel rather than sequentially.

### Side Effects

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

### Examples

First, start several remote kernels.

In[1]:= |

Out[1]= |

The sine function is applied to the given arguments. Each computation takes place on a remote kernel.

In[2]:= |

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 takes more time, so this computation can benefit from parallelization.

In[3]:= |

Out[3]= |

Alternatively, you can use ParallelTable. Here a list of the number of factors in is generated.

In[4]:= |

Out[4]//TableForm= | |

### Automatic Distribution of Definitions

Parallel commands such as ParallelTable will automatically distribute the values and functions needed, using effectively DistributeDefinitions.

In[1]:= |

In[3]:= |

Out[3]= |

This automatic distribution happens for any functions and variables you define interactively, within the same notebook (technically, for all symbols in the default context). Definitions from other contexts, such as functions from packages, are not distributed automatically.

Lower-level functions, such as ParallelEvaluate, do not perform any automatic distribution of values.

In[4]:= |

In[5]:= |

Out[5]= |

In[6]:= |

In[7]:= |

Out[7]= |

### Automatic Parallelization

Parallelize[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.

In[5]:= |

Out[5]= |

In[6]:= |

Out[6]= |

In[7]:= |

Out[7]= |

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.

In[8]:= |

Out[8]= |