({{, (RefLink[ParallelEvaluate, paclet:ref/ParallelEvaluate])[expr]
evaluates the expression expr on all available parallel kernels and returns the list of results obtained.}, {, (RefLink[ParallelEvaluate, paclet:ref/ParallelEvaluate])[expr,kernel] 
evaluates expr on the parallel kernel specified. }, {, (RefLink[ParallelEvaluate, paclet:ref/ParallelEvaluate])[expr,{(Subscript[ker, 1]),(Subscript[ker, 2]),…}]
evaluates expr on the parallel kernels (Subscript[ker, i]).}})

OpenersNotesSection(Spacer1)Details and Options

(RefLink[ParallelEvaluate, paclet:ref/ParallelEvaluate]) has attribute (RefLink[HoldFirst, paclet:ref/HoldFirst]).

Kernels can be specified either using (RefLink[KernelObject, paclet:ref/KernelObject]), as returned by (RefLink[Kernels, paclet:ref/Kernels])[], or using their integer IDs, as returned by (RefLink[$KernelID, paclet:ref/$KernelID]).

The (RefLink[DistributedContexts, paclet:ref/DistributedContexts]) option for (RefLink[ParallelEvaluate, paclet:ref/ParallelEvaluate]) specifies which symbols appearing in expr have their definitions automatically distributed to all available kernels before the computation.

The default value is (RefLink[DistributedContexts, paclet:ref/DistributedContexts]):>(RefLink[$DistributedContexts, paclet:ref/$DistributedContexts]) with (RefLink[$DistributedContexts, paclet:ref/$DistributedContexts]):=(RefLink[$Context, paclet:ref/$Context]), which distributes definitions of all symbols in the current context, but does not distribute definitions of symbols from packages.

OpenersPrimaryExamplesSection(Spacer1)Examples  (24)

OpenersExampleSection0(Spacer1)Basic Examples  (1)

Obtain each parallel kernel's unique ID number:

ParallelEvaluate[$KernelID]

Obtain each parallel kernel's system process ID:

ParallelEvaluate[$ProcessID]

OpenersExampleSection1(Spacer1)Scope  (5)

Use (RefLink[ParallelEvaluate, paclet:ref/ParallelEvaluate]) to perform initializations on all parallel kernels:

ParallelEvaluate[$RecursionLimit=500]

Make definitions of local functions on all parallel kernels:

ParallelEvaluate[f[n_]:=n^2]

ParallelTable[f[i],{i,10}]

Obtain local properties of all parallel kernels:

ParallelEvaluate[TimeUsed[]]

Obtain a local property from a single kernel:

ParallelEvaluate[$InstallationDirectory,First[Kernels[]]]

Access data from sources local to the parallel kernels:

ParallelEvaluate[lp=ExampleData[{"LinearProgramming","afiro"}];]

Save results computed on the parallel kernels locally in unique files:

ParallelEvaluate[Save["results-"<>ToString[$KernelID]<>".m",result]]

Verify that the files have been written:

ParallelEvaluate[FileNames["results-*.m"]]

OpenersExampleSection2(Spacer1)Generalizations & Extensions  (1)

Specify the kernels to query by a kernel object or a kernel ID:

Kernels[]

ParallelEvaluate[$ProcessID,%[[1]]]

ParallelEvaluate[$ProcessID,1]

Query several kernels:

ParallelEvaluate[$KernelID,{1,2,3}]

OpenersExampleSection3(Spacer1)Options  (5)

OpenersExampleSubsection0(Spacer1)DistributedContexts  (5)

By default, definitions in the current context are distributed automatically:

remote[]:={$KernelID}

ParallelEvaluate[remote[]]

Do not distribute any definitions of functions:

local[]:={$KernelID}

ParallelEvaluate[local[],DistributedContexts->None]

Distribute definitions for all symbols in all contexts appearing in a parallel computation:

a`f[]:={$KernelID}

ParallelEvaluate[a`f[],DistributedContexts->Automatic]

Distribute only definitions in the given contexts:

b`g[]:={$KernelID}

ParallelEvaluate[b`g[],DistributedContexts->{"a`"}]

Restore the value of the (RefLink[DistributedContexts, paclet:ref/DistributedContexts]) option to its default:

SetOptions[ParallelEvaluate,DistributedContexts:>$DistributedContexts]

OpenersExampleSection4(Spacer1)Applications  (4)

Tabulate properties of all parallel kernels:

TableForm[ParallelEvaluate[{$KernelID,$MachineName,$SystemID,$VersionNumber,$ProcessID,TimeUsed[], MaxMemoryUsed[],"ProcessPriority"/.SystemOptions["ProcessPriority"]}],TableHeadings->{None,{"ID","host", "OS","Version", "Process","CPU Time","Memory", "Priority"}},TableDepth->2]

Parallelize a Monte Carlo simulation by running the same code on all parallel kernels:

With[{n=10^5},ParallelEvaluate[4.0 Count[RandomReal[{0,1},{n,2}],xy_/;Norm[xy]<1]/n]]

Combine the individual averages for a more accurate overall result:

Mean[%]

Store large intermediate results locally on each parallel kernel:

ParallelEvaluate[mat=RandomReal[{-1,1},{2000,2000}];]

Work with data stored locally:

ParallelEvaluate[Timing[inv=Inverse[mat];]]

Check whether all of these polynomials are irreducible:

Module[{x},poly[n_,x_:x]:=(∑_(i=1)^(n+1))i (x^(i-1))]

poly[10,x]

Shared variables used for synchronization:

d=200;results={};
SetSharedVariable[d,results];
DistributeDefinitions[poly];

Run one search loop on each kernel until the computation is aborted:

PrintTemporary[Dynamic[{d,results}]];
CheckAbort[ParallelEvaluate[While[True,
With[{d=d++},If[!IrreduciblePolynomialQ[poly[d]],AppendTo[results,d]]]
]],{d,results}]

OpenersExampleSection5(Spacer1)Properties & Relations  (5)

(RefLink[ParallelEvaluate, paclet:ref/ParallelEvaluate]) performs the same evaluation on each subkernel:

ParallelEvaluate@Table[foo[i],{i,3}]

(RefLink[Parallelize, paclet:ref/Parallelize]) distributes parts of an evaluation to each subkernel:

Parallelize@Table[foo[i],{i,3}]

Deterministic calculations give the same result on each parallel kernel:

ParallelEvaluate[N[Pi]]

Calculations involving randomness give independent results on each parallel kernel:

ParallelEvaluate[RandomReal[]]

Force the same result by setting (RefLink[SeedRandom, paclet:ref/SeedRandom]):

ParallelEvaluate[SeedRandom[123]]

ParallelEvaluate[RandomReal[]]

Each parallel kernel has a distinct ID, which can be used to make expressions unique:

ParallelEvaluate[$KernelID]

(RefLink[DistributeDefinitions, paclet:ref/DistributeDefinitions]) uses (RefLink[ParallelEvaluate, paclet:ref/ParallelEvaluate]) to transport definitions to all kernels:

f[n_]:=PrimeQ[2^n-1]

DistributeDefinitions[f];

An explicit (RefLink[ParallelEvaluate, paclet:ref/ParallelEvaluate]) does the same:

ParallelEvaluate[g[n_]:=PrimeQ[2^n+1]];

ParallelTable[Through[{f,g}[i]],{i,10},DistributedContexts->None]

Distributed definitions are remembered for new kernels:

CloseKernels[];LaunchKernels[];

The effects of (RefLink[ParallelEvaluate, paclet:ref/ParallelEvaluate]) are not remembered:

ParallelTable[Through[{f,g}[i]],{i,10},DistributedContexts->None]

(RefLink[ParallelNeeds, paclet:ref/ParallelNeeds]) uses (RefLink[ParallelEvaluate, paclet:ref/ParallelEvaluate]) to run (RefLink[Needs, paclet:ref/Needs]) on all parallel kernels:

Needs["ComputerArithmetic`"]

ParallelNeeds["ComputerArithmetic`"]

Additionally all uses are remembered, so that new kernels also load needed packages:

CloseKernels[];LaunchKernels[];

ParallelEvaluate[Ulp[$MaxMachineNumber]]

(RefLink[ParallelEvaluate, paclet:ref/ParallelEvaluate]) automatically distributes definitions of functions used:

ftest[n_]:=Labeled[Framed[PrimeQ[2^n-1]],$KernelID]

ParallelEvaluate[{#,ftest[#]}&[RandomInteger[20]]]

OpenersExampleSection6(Spacer1)Possible Issues  (2)

Side effects cannot be used between different parallel kernels:

i=0;
ParallelEvaluate[i++]

Use a shared variable to support side effects:

SetSharedVariable[j];j=0;

ParallelEvaluate[j++]

j

Suppress the automatic distribution of definitions when making definitions with (RefLink[ParallelEvaluate, paclet:ref/ParallelEvaluate]):

f[n_?EvenQ]:=n^3

ParallelEvaluate[f[n_]:=n^2,DistributedContexts->None]

ParallelTable[f[i],{i,10},DistributedContexts->None]

OpenersExampleSection7(Spacer1)Neat Examples  (1)

Find n such that n!+1 is prime, until the computation is manually aborted:

n=1;primes={}; SetSharedVariable[n,primes];

PrintTemporary[Dynamic[{n,primes}]];
CheckAbort[
ParallelEvaluate[While[True,
With[{n=n++},If[PrimeQ[n!+1],AppendTo[primes,n]]]]],
{n,primes}]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[Parallelize, paclet:ref/Parallelize])  ▪  (RefLink[ParallelTry, paclet:ref/ParallelTry])  ▪  (RefLink[DistributeDefinitions, paclet:ref/DistributeDefinitions])  ▪  (RefLink[ParallelNeeds, paclet:ref/ParallelNeeds])  ▪  (RefLink[Kernels, paclet:ref/Kernels])  ▪  (RefLink[$KernelCount, paclet:ref/$KernelCount])  ▪  (RefLink[CloudEvaluate, paclet:ref/CloudEvaluate])  ▪  (RefLink[LocalSubmit, paclet:ref/LocalSubmit])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Parallel Computing, paclet:guide/ParallelComputing])}, {▪(RefLinkPlain[Parallel Computation Setup & Configuration, paclet:guide/ParallelComputationSetupAndConfiguration])}, {▪(RefLinkPlain[Remote Computation, paclet:guide/RemoteComputation])}, {▪(RefLinkPlain[Managing Remote and Parallel Kernels, paclet:guide/ManagingRemoteAndParallelKernels])}, {▪(RefLinkPlain[Resource Sharing in Parallel Computing, paclet:guide/ResourceSharingInParallelComputing])}, {▪(RefLinkPlain[Raspberry Pi, paclet:guide/RaspberryPi])}, {▪(RefLinkPlain[Background & Scheduled Tasks, paclet:guide/BackgroundAndScheduledTasks])}})}})