({{, (RefLink[ParallelMap, paclet:ref/ParallelMap])[f,expr] 
applies f in parallel to each element on the first level in expr.}, {, (RefLink[ParallelMap, paclet:ref/ParallelMap])[f,expr,levelspec]
applies f in parallel to parts of expr specified by levelspec.}})

OpenersNotesSection(Spacer1)Details and Options

(RefLink[ParallelMap, paclet:ref/ParallelMap]) is a parallel version of (RefLink[Map, paclet:ref/Map]), which automatically distributes different applications of f among different kernels and processors.

(RefLink[ParallelMap, paclet:ref/ParallelMap]) will give the same results as (RefLink[Map, paclet:ref/Map]), except for side effects during the computation.

(RefLink[ParallelMap, paclet:ref/ParallelMap]) uses the same level specifications as (RefLink[Map, paclet:ref/Map]). Not all level specifications can be parallelized.

(RefLink[Parallelize, paclet:ref/Parallelize])[(RefLink[Map, paclet:ref/Map])[f,expr]] is equivalent to (RefLink[ParallelMap, paclet:ref/ParallelMap])[f,expr].

If an instance of (RefLink[ParallelMap, paclet:ref/ParallelMap]) cannot be parallelized, it is evaluated using (RefLink[Map, paclet:ref/Map]).

(RefLink[ParallelMap, paclet:ref/ParallelMap]) takes the same (RefLink[Method, paclet:ref/Method]) option as (RefLink[Parallelize, paclet:ref/Parallelize]).

(RefLink[ParallelMap, paclet:ref/ParallelMap]) takes the same (RefLink[DistributedContexts, paclet:ref/DistributedContexts]) option as (RefLink[ParallelTable, paclet:ref/ParallelTable]).

OpenersPrimaryExamplesSection(Spacer1)Examples  (40)

OpenersExampleSection0(Spacer1)Basic Examples  (3)

(RefLink[ParallelMap, paclet:ref/ParallelMap]) works like (RefLink[Map, paclet:ref/Map]), but in parallel:

Map[(Pause[1];f[#])&,{a,b,c,d}]//AbsoluteTiming

ParallelMap[(Pause[1];f[#])&,{a,b,c,d}]//AbsoluteTiming

(RefLink[ParallelMap, paclet:ref/ParallelMap]) can work with any function:

ParallelMap[Composition[Framed,FactorInteger],{1,11,111,1111,11111,111111}]

Use explicit pure functions:

ParallelMap[N[#,50]&,{E,Pi,Catalan}]

Functions defined interactively can immediately be used in parallel:

f1[n_]:=Length[FactorInteger[(10^n-1)/9]]

ParallelMap[f1,Range[50,60]]

OpenersExampleSection1(Spacer1)Scope  (6)

Map at level 1 (default):

ParallelMap[f,{{a,b},{c,d}}]

Map down to level 2:

ParallelMap[f,{{a,b},{c,d}},2]

Map at level 2:

ParallelMap[f,{{a,b},{c,d}},{2}]

Map down to level 3:

ParallelMap[f,{{{{{a}}}}},3]

Map on all levels, starting at level 1:

ParallelMap[f,{{{{{a,b}}}}},Infinity]

Negative levels:

ParallelMap[f,{{{{{a,b}}}}},-1]

ParallelMap[f,{{{{{a,b}}}}},-2]

ParallelMap[f,{{{{{a,b}}}}},-3]

Positive and negative levels can be mixed:

ParallelMap[f,{{{{{a}}}}},{2,-3}]

Different heads at each level:

ParallelMap[f,h0[h1[h2[h3[h4[a]]]]],{2,-3}]

Not all levels can be parallelized:

ParallelMap[f,{{{{{a}}}}},{0,2}]

OpenersExampleSection2(Spacer1)Generalizations & Extensions  (2)

(RefLink[ParallelMap, paclet:ref/ParallelMap]) can be used on expressions with any head:

ParallelMap[f,a+b+c+d]

ParallelMap[f,x^2+y^2,2]

Functions with attribute (RefLink[Listable, paclet:ref/Listable]) are mapped automatically:

Parallelize[Sqrt[{1,2,3,4}]]

ParallelMap[Sqrt,{1,2,3,4}]

OpenersExampleSection3(Spacer1)Options  (11)

OpenersExampleSubsection0(Spacer1)Method  (6)

Break the computation into the smallest possible subunits:

ParallelMap[Labeled[Framed[#],$KernelID]&,Range[10],Method->"FinestGrained"]

Break the computation into as many pieces as there are available kernels:

ParallelMap[Labeled[Framed[#],$KernelID]&,Range[10],Method->"CoarsestGrained"]

Break the computation into at most 2 evaluations per kernel for the entire job:

ParallelMap[Labeled[Framed[#],$KernelID]&,Range[12],Method->"EvaluationsPerKernel"->2]

Break the computation into evaluations of at most 5 elements each:

ParallelMap[Labeled[Framed[#],$KernelID]&,Range[18],Method->"ItemsPerEvaluation"->5]

The default option setting balances evaluation size and number of evaluations:

ParallelMap[Labeled[Framed[#],$KernelID]&,Range[20],Method->Automatic]

Calculations with vastly differing runtimes should be parallelized as finely as possible:

ParallelMap[PrimeQ[2^#-1]&,Range[30000,30031],Method->"FinestGrained"]

A large number of simple calculations should be distributed into as few batches as possible:

BinCounts[ParallelMap[Mod[Floor[#*Pi],10]&,Range[10000],Method->"CoarsestGrained"],{0,10}]

OpenersExampleSubsection1(Spacer1)DistributedContexts  (5)

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

remote[x_]:={$KernelID,x^3}

ParallelMap[remote,{1,2,3,4}]

Do not distribute any definitions of functions:

local[x_]:={$KernelID,x^2}

ParallelMap[local,{1,2,3,4},DistributedContexts->None]

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

a`f[x_]:={$KernelID,x}

ParallelMap[a`f,{1,2,3,4},DistributedContexts->Automatic]

Distribute only definitions in the given contexts:

b`g[x_]:={$KernelID,-x}

ParallelMap[b`g,{1,2,3,4},DistributedContexts->{"a`"}]

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

SetOptions[ParallelMap,DistributedContexts:>$DistributedContexts]

OpenersExampleSection4(Spacer1)Applications  (2)

Watch the results appear as they are found:

SetSharedVariable[primes]

Monitor[primes={};
ParallelMap[If[PrimeQ[2^#-1],AppendTo[primes,#]]&,Range[1000,5000]];primes,
primes]

Solve the arithmetic puzzle:

Framed[Grid[{{"SEND"},{"+MORE"},{"MONEY"}},Dividers->{None,{False,False,True,False}},Alignment->{Automatic,Right},BaseStyle->"Text"],FrameMargins->5,FrameStyle->None]

Write it in symbolic form:

eq=word[s,e,n,d]+word[m,o,r,e]==word[m,o,n,e,y]

Assign a single-digit number to each letter:

eq/.Thread[{d,e,m,n,o,r,s,y}->{7,5,1,6,0,8,9,2}]

And interpret each word as a number in base 10:

%/.word[l__]:>FromDigits[{l}]

Automate the checking for a particular digit assignment:

check[eq_,letters_,digits_]:=eq/.Thread[letters->digits]/.word[args___]:>FromDigits[{args}]

check[eq,{d,e,m,n,o,r,s,y},{7,5,1,6,0,8,9,2}]

To systematically solve this assignment, first get the list of letters:

alpha=Union[Reap[eq/.word[args___]:>Sow/@{args}][[2,1]]]

The puzzle can be solved by considering all permutations of all subsets of eight digits:

Join@@Map[Select[Permutations[#],check[eq,alpha,#]&]&,Subsets[Range[0,9],{Length[alpha]}]]

Parallelize it:

Join@@ParallelMap[Select[Permutations[#],check[eq,alpha,#]&]&,Subsets[Range[0,9],{Length[alpha]}]]

Only the solutions with m≠0 are usually considered:

Select[%,#[[3]]≠0&]

The search can be stopped as soon as a nontrivial solution is found using (RefLink[ParallelTry, paclet:ref/ParallelTry]):

ParallelTry[Catch[Scan[If[#[[3]]≠0&&check[eq,alpha,#],Throw[#]]&,Permutations[#]];$Failed]&,Subsets[Range[0,9],{Length[alpha]}]]

OpenersExampleSection5(Spacer1)Properties & Relations  (10)

The parallelization happens at the outermost level used:

ParallelMap[Labeled[Framed[#],$KernelID]&,{a,b,c,d},2]

ParallelMap[Labeled[Framed[#],$KernelID]&,{{a,b},{c,d}},2]

(RefLink[Map, paclet:ref/Map]) can be parallelized automatically, effectively using (RefLink[ParallelMap, paclet:ref/ParallelMap]):

Parallelize[Map[PrimeQ[2^#-1]&,Range[100,110]]]

ParallelMap[PrimeQ[2^#-1]&,Range[100,110]]

Show the effect of load balancing with tasks of known size:

times=Range[0,1,0.04]

AbsoluteTiming[ParallelMap[Pause,times,Method->"FinestGrained"];]

AbsoluteTiming[ParallelMap[Pause,times,Method->"CoarsestGrained"];]

Define a number of tasks with known runtimes:

tasks=Range[0.1,1,0.05];seq=Total[tasks]

Measure the time for parallel execution:

t1=First[AbsoluteTiming[ParallelMap[Pause,tasks]]]

The speedup obtained (more is better):

seq/t1

Finest-grained scheduling gives better load balancing and higher speedup:

t2=First[AbsoluteTiming[ParallelMap[Pause,tasks,Method->"FinestGrained"]]]

seq/t2

Scheduling large tasks first gives even better results:

t3=First[AbsoluteTiming[ParallelMap[Pause,Reverse[tasks],Method->"FinestGrained"]]]

seq/t3

A function of several arguments can be mapped with (RefLink[MapThread, paclet:ref/MapThread]):

MapThread[f,{{1,2,3},{a,b,c}}]

Get a parallel version by using (RefLink[Parallelize, paclet:ref/Parallelize]):

Parallelize[MapThread[Labeled[Framed[f[##]],$KernelID]&,{{1,2,3},{a,b,c}}]]

(RefLink[MapIndexed, paclet:ref/MapIndexed]) passes the index of an element to the mapped function:

MapIndexed[f,{a,b,c}]

Get a parallel version by using (RefLink[Parallelize, paclet:ref/Parallelize]):

Parallelize[MapIndexed[Labeled[Framed[f[##]],$KernelID]&,{a,b,c}]]

(RefLink[Scan, paclet:ref/Scan]) does the same as (RefLink[Map, paclet:ref/Map]), but without returning a result:

Parallelize[Map[Print,{"a","b"}]]

Parallelize[Scan[Print,{"a","b"}]]

Functions defined interactively are automatically distributed to all kernels when needed:

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

ParallelMap[ftest,Range[1275,1285]]

Distribute definitions manually and disable automatic distribution:

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

DistributeDefinitions[gtest];

ParallelMap[gtest,Range[1275,1285],DistributedContexts->None]

For functions from a package, use (RefLink[ParallelNeeds, paclet:ref/ParallelNeeds]) rather than (RefLink[DistributeDefinitions, paclet:ref/DistributeDefinitions]):

Needs["FiniteFields`"]

Map[FieldSize,Table[GF[2,i],{i,10}]]

ParallelNeeds["FiniteFields`"]

ParallelMap[FieldSize,Table[GF[2,i],{i,10}]]

Get the size of all Wolfram Language files in an installation of Mathematica in parallel:

ParallelMap[FileByteCount,FileNames["*.m",$InstallationDirectory,Infinity]]//Short

Total[%]

OpenersExampleSection6(Spacer1)Possible Issues  (6)

If a level specification prevents parallelization, (RefLink[ParallelMap, paclet:ref/ParallelMap]) evaluates like (RefLink[Map, paclet:ref/Map]):

ParallelMap[f,{a,b,c},0]

Side effects cannot be used in the function mapped in parallel:

primes={};
ParallelMap[If[PrimeQ[2^#-1],AppendTo[primes,#];#]&,Range[1000,2000]];

primes

Use a shared variable to support side effects:

SetSharedVariable[sprimes]

sprimes={};
ParallelMap[If[PrimeQ[2^#-1],AppendTo[sprimes,#]]&,Range[1000,2000]];

sprimes

A function used that is not known on the parallel kernels may lead to sequential evaluation:

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

ParallelMap[ftest,Range[1275,1285],DistributedContexts->None]

Define the function on all parallel kernels:

DistributeDefinitions[ftest]

The function is now evaluated on the parallel kernels:

ParallelMap[ftest,Range[1275,1285],DistributedContexts->None]

Definitions of functions in the current context are distributed automatically:

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

ParallelMap[gtest,Range[1275,1285]]

Definitions from contexts other than the default context are not distributed automatically:

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

ParallelMap[ctx`mtest,Range[1275,1285]]

Use (RefLink[DistributeDefinitions, paclet:ref/DistributeDefinitions]) to distribute such definitions:

DistributeDefinitions[ctx`mtest];

ParallelMap[ctx`mtest,Range[1275,1285]]

Alternatively, set the (RefLink[DistributedContexts, paclet:ref/DistributedContexts]) option to include all contexts:

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

ParallelMap[cty`mtest,Range[1275,1285],DistributedContexts->Automatic]

Trivial operations may take longer when parallelized:

AbsoluteTiming[ParallelMap[Composition[N,Sin],Range[1000]];]

AbsoluteTiming[Map[Composition[N,Sin],Range[1000]];]

Measure the minimum parallel communication overhead:

AbsoluteTiming[Do[ParallelEvaluate[Null,First[Kernels[]]],{100}]]

Compare with a simple calculation done on the master kernel itself:

AbsoluteTiming[Do[N[Sin[1]],{100}]]

Functions may simplify short argument lists, but not longer ones:

{Majority[a],Majority[b,c],Majority[d,e,f]}

Such simplification of partial expressions may make parallel mapping impossible:

ParallelMap[fun,Majority[a,b,c,d,e,f]]

Prevent simplification of partial expressions and apply the desired function only at the end:

Majority@@ParallelMap[fun,{a,b,c,d,e,f}]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[Map, paclet:ref/Map])  ▪  (RefLink[ParallelTry, paclet:ref/ParallelTry])  ▪  (RefLink[ParallelTable, paclet:ref/ParallelTable])  ▪  (RefLink[Parallelize, paclet:ref/Parallelize])  ▪  (RefLink[DistributedContexts, paclet:ref/DistributedContexts])  ▪  (RefLink[FileSystemMap, paclet:ref/FileSystemMap])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Data Parallelism, paclet:guide/DataParallelism])}, {▪(RefLinkPlain[Parallel Computing, paclet:guide/ParallelComputing])}, {▪(RefLinkPlain[Managing Remote and Parallel Kernels, paclet:guide/ManagingRemoteAndParallelKernels])}})}})