ParallelTry

ParallelTry[f,{arg1,arg2,}]

evaluates f[argi] in parallel, returning the first result received.

ParallelTry[f,{arg1,arg2,},k]

returns a list of the first k results.

Details and Options

  • 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[argi] is $Failed, then it is ignored.
  • If no results other than $Failed are obtained, ParallelTry returns $Failed.
  • ParallelTry[f,{arg1,arg2,}] allocates evaluations of the f[argi] to different kernels and processors, starting with arg1.
  • If there are more argi than kernels, then some of the argi will not be tried unless earlier ones return $Failed.
  • ParallelTry takes the same DistributedContexts option as ParallelTable.

Examples

open allclose all

Basic Examples  (2)

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

Nondeterministically find a prime in a given range:

Scope  (2)

Return the first two successful results:

If no successful results are found, ParallelTry returns $Failed:

Options  (5)

DistributedContexts  (5)

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

Do not distribute any definitions of functions:

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

Distribute only definitions in the given contexts:

Restore the value of the DistributedContexts option to its default:

Applications  (5)

Find a random prime in parallel:

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

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

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

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

In this case, only one method succeeded:

Properties & Relations  (5)

ParallelTry generally returns the first successful result received:

ParallelTry works like ParallelMap, but returns only one of the results:

ParallelTry is essentially implemented in terms of WaitNext:

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

Distribute definitions manually and disable automatic distribution:

For functions from a package, use ParallelNeeds rather than DistributeDefinitions:

Possible Issues  (3)

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

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

Define the function on all parallel kernels:

The function is now evaluated on the parallel kernels:

Definitions of functions in the current context are distributed automatically:

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

Use DistributeDefinitions to distribute such definitions:

Alternatively, set the DistributedContexts option to include all contexts:

Wolfram Research (2008), ParallelTry, Wolfram Language function, https://reference.wolfram.com/language/ref/ParallelTry.html (updated 2010).

Text

Wolfram Research (2008), ParallelTry, Wolfram Language function, https://reference.wolfram.com/language/ref/ParallelTry.html (updated 2010).

CMS

Wolfram Language. 2008. "ParallelTry." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 2010. https://reference.wolfram.com/language/ref/ParallelTry.html.

APA

Wolfram Language. (2008). ParallelTry. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ParallelTry.html

BibTeX

@misc{reference.wolfram_2024_paralleltry, author="Wolfram Research", title="{ParallelTry}", year="2010", howpublished="\url{https://reference.wolfram.com/language/ref/ParallelTry.html}", note=[Accessed: 23-November-2024 ]}

BibLaTeX

@online{reference.wolfram_2024_paralleltry, organization={Wolfram Research}, title={ParallelTry}, year={2010}, url={https://reference.wolfram.com/language/ref/ParallelTry.html}, note=[Accessed: 23-November-2024 ]}