({{, (RefLink[ParallelTry, paclet:ref/ParallelTry])[f,{(Subscript[arg, 1]),(Subscript[arg, 2]),…}] 
evaluates f[(Subscript[arg, i])] in parallel, returning the first result received.}, {, (RefLink[ParallelTry, paclet:ref/ParallelTry])[f,{(Subscript[arg, 1]),(Subscript[arg, 2]),…},k]
returns a list of the first k results.}})

OpenersNotesSection(Spacer1)Details and Options

(RefLink[ParallelTry, paclet:ref/ParallelTry]) automatically distributes computations among different available kernels and processors.

Any evaluations still underway after k results have been received are aborted.

If the result from evaluating one of the f[(Subscript[arg, i])] is (RefLink[$Failed, paclet:ref/$Failed]), then it is ignored.

If no results other than (RefLink[$Failed, paclet:ref/$Failed]) are obtained, (RefLink[ParallelTry, paclet:ref/ParallelTry]) returns (RefLink[$Failed, paclet:ref/$Failed]).

(RefLink[ParallelTry, paclet:ref/ParallelTry])[f,{(Subscript[arg, 1]),(Subscript[arg, 2]),…}] allocates evaluations of the f[(Subscript[arg, i])] to different kernels and processors, starting with (Subscript[arg, 1]).

If there are more (Subscript[arg, i]) than kernels, then some of the (Subscript[arg, i]) will not be tried unless earlier ones return (RefLink[$Failed, paclet:ref/$Failed]).

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

OpenersPrimaryExamplesSection(Spacer1)Examples  (22)

OpenersExampleSection0(Spacer1)Basic Examples  (2)

Find the factorization of a number that is easy to factorize:

ParallelTry[FactorInteger,Range[10^20+1,10^20+$KernelCount]]

Nondeterministically find a prime in a given range:

ParallelTry[If[PrimeQ[#],#,$Failed]&,Range[10^100,10^100+1000]]

OpenersExampleSection1(Spacer1)Scope  (2)

Return the first two successful results:

ParallelTry[If[PrimeQ[#],#,$Failed]&,Range[10^100,10^100+1000],2]-10^100

If no successful results are found, (RefLink[ParallelTry, paclet:ref/ParallelTry]) returns (RefLink[$Failed, paclet:ref/$Failed]):

ParallelTry[If[PrimeQ[#],#,$Failed]&,Range[10^100,10^100+100]]

OpenersExampleSection2(Spacer1)Options  (5)

OpenersExampleSubsection0(Spacer1)DistributedContexts  (5)

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

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

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

Do not distribute any definitions of functions:

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

ParallelTry[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}

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

Distribute only definitions in the given contexts:

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

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

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

SetOptions[ParallelTry,DistributedContexts:>$DistributedContexts]

OpenersExampleSection3(Spacer1)Applications  (5)

Find a random prime in parallel:

ParallelTry[
(While[!PrimeQ[p=RandomInteger[{10^100,10^101}]],];p)&,Range[$ProcessorCount]]

Find an irreducible polynomial with a random search on each parallel kernel:

randPoly[n_Integer?Positive,x_,p_]:=Expand[Fold[x*#1+#2&,1,RandomInteger[{0,p-1},n]]]

findOne[n_,x_,p_?PrimeQ]:=Module[{poly},
While[!IrreduciblePolynomialQ[poly=randPoly[n,x,p],Modulus->p],Null];
poly]

ParallelTry[findOne[100,x,2]&,Range[$KernelCount]]

Search for a Mersenne prime starting at a given prime exponent:

search[n0_,dn_]:=
Module[{n=n0},While[!PrimeQ[2^Prime[n]-1],n+=dn];n]

With[{dn=$KernelCount,n0=500},
n=ParallelTry[search[n0+#,dn]&,Range[0,dn-1]];
{n,Prime[n]}
]

Try different methods for minimization and return the first one that succeeds:

ParallelTry[
{#,FindMinimum[(E^(Sin[50 x]))+Sin[60 (E^y)]+Sin[70 Sin[x]]+Sin[Sin[80 y]]-Sin[10 (x+y)]+(1/4) ((x^2)+(y^2)),{x,y},Method->#]}&,{"Gradient","ConjugateGradient","InteriorPoint","QuasiNewton","Newton"}]

Try different symbolic methods of summation and return the first one to succeed:

ParallelTry[(s=Sum[(((a^k) (-1+a-k))/((1+k) k!)),k,Method->#];If[Head@s===Sum,$Failed,s])&,{"Polynomial","PolynomialExponential","HypergeometricTermGosper","PolynomialTrigonometric"}]

In this case, only one method succeeded:

ParallelMap[(s=Sum[(((a^k) (-1+a-k))/((1+k) k!)),k,Method->#];If[Head@s===Sum,$Failed,s])&,{"Polynomial","PolynomialExponential","HypergeometricTermGosper","PolynomialTrigonometric"}]

OpenersExampleSection4(Spacer1)Properties & Relations  (5)

(RefLink[ParallelTry, paclet:ref/ParallelTry]) generally returns the first successful result received:

ParallelTry[(Pause[#];#)&,{4,3,2,1}]

(RefLink[ParallelTry, paclet:ref/ParallelTry]) works like (RefLink[ParallelMap, paclet:ref/ParallelMap]), but returns only one of the results:

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

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

(RefLink[ParallelTry, paclet:ref/ParallelTry]) is essentially implemented in terms of (RefLink[WaitNext, paclet:ref/WaitNext]):

parallelTry[f_,args_List]:=Module[{evs,ev,res=$Failed},
evs=Composition[ParallelSubmit,f]/@args;
While[res===$Failed&&Length[evs]>0,{res,ev,evs}=WaitNext[evs]];
AbortKernels[];
res
]

parallelTry[FactorInteger,Range[10^20+1,10^20+$KernelCount]]

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

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

ParallelTry[ftest,Range[1275,1285]]

Distribute definitions manually and disable automatic distribution:

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

DistributeDefinitions[gtest];

ParallelTry[ftest,Range[1275,1285],3,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`"]

ParallelTry[FieldSize,Table[GF[2,i],{i,10}],4]

OpenersExampleSection5(Spacer1)Possible Issues  (3)

If there are more expressions than kernels, some will never be tried:

ParallelTry[(Print[#];Pause[RandomReal[]];$KernelID)&,Range[$KernelCount+1]]

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

ptest[n_]:=If[PrimeQ[2^n-1],{n,$KernelID},$Failed]

ParallelTry[ptest,Range[20,40],DistributedContexts->None]

Define the function on all parallel kernels:

DistributeDefinitions[ptest]

The function is now evaluated on the parallel kernels:

ParallelTry[ptest,Range[20,40],DistributedContexts->None]

Definitions of functions in the current context are distributed automatically:

qtest[n_]:=If[PrimeQ[2^n-1],{n,$KernelID},$Failed]

ParallelTry[ptest,Range[20,70],2]

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

ctx`ptest[n_]:=If[PrimeQ[2^n-1],{n,$KernelID},$Failed]

ParallelTry[ctx`ptest,Range[20,40]]

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

DistributeDefinitions[ctx`ptest]

ParallelTry[ctx`ptest,Range[20,40]]

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

cty`ptest[n_]:=If[PrimeQ[2^n-1],{n,$KernelID},$Failed]

ParallelTry[cty`ptest,Range[20,70],2,DistributedContexts->Automatic]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[ParallelMap, paclet:ref/ParallelMap])  ▪  (RefLink[Parallelize, paclet:ref/Parallelize])  ▪  (RefLink[WaitNext, paclet:ref/WaitNext])  ▪  (RefLink[$KernelCount, paclet:ref/$KernelCount])}})}})

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