Parallel Evaluation
This feature is not supported on the Wolfram Cloud.

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 these examples, a command is always sent to a single kernel.
In[15]:=
Click for copyable input
Out[15]=
The following program will return False because the symbol will most likely not have any value at all on the remote kernel.
In[16]:=
Click for copyable input
Out[17]=
A convenient way to insert variable values into unevaluated commands is to use With, as demonstrated in the following command. The symbol is replaced by , then the expression is sent to the remote kernel, where it evaluates to True.
In[18]:=
Click for copyable input
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.

The variable will not have a value on the remote kernel.
In[20]:=
Click for copyable input
Out[20]=
You can use the following command to accomplish the intended iteration on the remote kernel. This substitutes the value of into the argument of ParallelEvaluate.
In[21]:=
Click for copyable input
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.

Formal parameters of pure functions are inserted into the body before the expression is sent to the parallel kernels.
In[22]:=
Click for copyable input
Out[22]=
Pattern variables are also inserted on the right side of the definition.
In[23]:=
Click for copyable input
In[24]:=
Click for copyable input
Out[24]=
Constants are also inserted.
In[25]:=
Click for copyable input
Out[25]=

Parallel Evaluation of Expressions

ParallelCombine[f,h[e1,e2,,en],comb]evaluates in parallel by distributing chunks to all kernels and combining the results with comb[]
ParallelCombine[f,h[e1,e2,,en]]the default combiner comb is h, if h has attribute Flat, and Join otherwise
Basic parallel dispatch of evaluations.

ParallelCombine[f,h[e1,e2,,en],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[e1,e2,,en],comb] simply applies f to pieces of the input list and joins the partial results together.

In[2]:=
Click for copyable input
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]:=
Click for copyable input
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]:=
Click for copyable input
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]:=
Click for copyable input
Out[27]=
In[28]:=
Click for copyable input
Out[28]=

Parallel Mapping and Iterators

The commands in this section are fundamental to parallel programming in the Wolfram Language.

ParallelMap[f,h[e1,e2,]evaluates in parallel
ParallelTable[expr,{i,i0,i1,di},{j,j0,j1,dj},] builds Table[expr,{i,i0,i1,di,j,j0,j1,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[e1,e2,]] 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]:=
Click for copyable input
Out[1]=

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

In[2]:=
Click for copyable input
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]:=
Click for copyable input
Out[3]=

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

In[4]:=
Click for copyable input
Out[4]//TableForm=

Automatic Distribution of Definitions

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

For this parallel table, the function and the iterator bound will evaluate on the subkernels, so their definitions need to be distributed to make it work.
In[1]:=
Click for copyable input
In[3]:=
Click for copyable input
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.

The symbol will return the value of $KernelID when evaluated.
In[4]:=
Click for copyable input
On the remote kernels, $KernelID is nonzero, but the symbol is not evaluated there, only later on the master kernel, where the value is zero.
In[5]:=
Click for copyable input
Out[5]=
After the definition of is distributed, it is evaluated on the remote kernels.
In[6]:=
Click for copyable input
In[7]:=
Click for copyable input
Out[7]=

Automatic Parallelization

Parallelize[cmd[list,arguments]] recognizes if cmd is a Wolfram Language 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]:=
Click for copyable input
Out[5]=
In[6]:=
Click for copyable input
Out[6]=
In[7]:=
Click for copyable input
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]:=
Click for copyable input
Out[8]=