({{, (RefLink[ParallelArray, paclet:ref/ParallelArray])[f,n]
generates in parallel a list of length n, with elements f[i], evaluated.}, {, (RefLink[ParallelArray, paclet:ref/ParallelArray])[f,{(Subscript[n, 1]),(Subscript[n, 2]),…}]
generates in parallel an (Subscript[n, 1])×(Subscript[n, 2])×… array of nested lists, with elements f[(Subscript[i, 1]),(Subscript[i, 2]),…]. }, {, (RefLink[ParallelArray, paclet:ref/ParallelArray])[f,{(Subscript[n, 1]),(Subscript[n, 2]),…},{(Subscript[r, 1]),(Subscript[r, 2]),…}]
generates in parallel a list using the index origins (Subscript[r, i]) (default 1). }, {, (RefLink[ParallelArray, paclet:ref/ParallelArray])[f,dims,origin,h]
uses head h, rather than (RefLink[List, paclet:ref/List]), for each level of the array. }})

OpenersNotesSection(Spacer1)Details and Options

(RefLink[ParallelArray, paclet:ref/ParallelArray]) is a parallel version of (RefLink[Array, paclet:ref/Array]), which automatically distributes different evaluations of expr among different kernels and processors.

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

(RefLink[Parallelize, paclet:ref/Parallelize])[(RefLink[Array, paclet:ref/Array])[f,n]] is equivalent to (RefLink[ParallelArray, paclet:ref/ParallelArray])[f,n].

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

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

OpenersPrimaryExamplesSection(Spacer1)Examples  (16)

OpenersExampleSection0(Spacer1)Basic Examples  (3)

(RefLink[ParallelArray, paclet:ref/ParallelArray]) works like (RefLink[Array, paclet:ref/Array]), but in parallel:

Array[(Pause[1];f[#])&,4]//AbsoluteTiming

ParallelArray[(Pause[1];f[#])&,4]//AbsoluteTiming

Generate a 3×2 array:

ParallelArray[f,{3,2}]

Generate a 3×4 array:

ParallelArray[10#1+#2&,{3,4}]

Use index origin 0 instead of 1:

ParallelArray[f,10,0]

Start with indices 0 and 4 instead of 1:

ParallelArray[f,{2,3},{0,4}]

OpenersExampleSection1(Spacer1)Options  (11)

OpenersExampleSubsection0(Spacer1)Method  (6)

Break the computation into the smallest possible subunits:

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

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

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

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

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

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

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

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

ParallelArray[Labeled[Framed[#],$KernelID]&,18,Method->Automatic]

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

ParallelArray[PrimeQ[2^#-1]&,20,4410,Method->"FinestGrained"]

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

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

OpenersExampleSubsection1(Spacer1)DistributedContexts  (5)

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

remote[x_]:=Labeled[Framed[x],$KernelID]

ParallelArray[remote,4]

Do not distribute any definitions of functions:

local[x_]:=Labeled[Framed[x],$KernelID]

ParallelArray[local,4,DistributedContexts->None]

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

a`f[x_]:=Labeled[Framed[x],$KernelID]

ParallelArray[a`f,4,DistributedContexts->Automatic]

Distribute only definitions in the given contexts:

b`g[x_]:=Labeled[Framed[x],$KernelID]

ParallelArray[b`f,4,DistributedContexts->{"a`"}]

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

SetOptions[ParallelArray,DistributedContexts:>$DistributedContexts]

OpenersExampleSection2(Spacer1)Possible Issues  (2)

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]

ParallelArray[ftest,10,DistributedContexts->None]

Define the function on all parallel kernels:

DistributeDefinitions[ftest]

The function is now evaluated on the parallel kernels:

ParallelArray[ftest,10,DistributedContexts->None]

Definitions of functions in the current context are distributed automatically:

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

ParallelArray[gtest,10]

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

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

ParallelArray[ctx`mtest,10]

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

DistributeDefinitions[ctx`mtest];

ParallelArray[ctx`mtest,10]

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

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

ParallelArray[cty`mtest,10,DistributedContexts->Automatic]

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

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Data Parallelism, paclet:guide/DataParallelism])}})}})