SetSharedFunction
SetSharedFunction[f1,f2,…]
declares the symbols fi as shared functions that are synchronized among all parallel kernels.
Details
- Expressions of the form f[…] evaluated on any parallel subkernel are sent to the master kernel, where they are evaluated, and the result is sent back to the parallel subkernel.
- Downvalues for a shared function defined on any parallel subkernel are maintained by the master kernel, and every access on any kernel is synchronized through the master kernel.
- Expressions of the form f[…] that would remain unevaluated give Null.
Examples
open allclose allBasic Examples (2)
Scope (3)
Generalizations & Extensions (1)
A delayed definition for a shared function made on the master kernel:
Such definitions are always evaluated on the master kernel, whichever kernel evaluates it:
A definition for a shared function made on one of the parallel kernels:
Such definitions are always evaluated on the kernel that asks for the value:
Applications (3)
Shared functions can be used for synchronization:
Less efficient is the use of a shared variable and critical section:
A constructor for a simple queue data type:
Work on the elements of the input queue in parallel and put results into the output queue:
Use a single shared function to communicate both input and result:
Set up a search and display its progress until it is manually aborted:
The results found so far—a list of the number of factors of :
Properties & Relations (3)
Use a shared append function for a local variable to collect results:
Using AppendTo on a shared variable has the same effect:
Make fs be a (global) shared function:
Each subkernel makes a definition for the same shared variable:
Without sharing, each subkernel has its own (local) copy of the function:
Shared functions are listed in $SharedFunctions:
Possible Issues (3)
A shared function is inefficient for mere code distribution and leads to sequential evaluation:
Simply distribute the definitions of any function needed on the parallel kernels:
Separate read and write operations on a shared variable are not thread-safe:
Use a shared function to synchronize access to an (unshared) variable:
Alternatively, use CriticalSection to make a whole code section atomic:
Neat Examples (1)
A parallel version of Sow:
Text
Wolfram Research (2008), SetSharedFunction, Wolfram Language function, https://reference.wolfram.com/language/ref/SetSharedFunction.html.
CMS
Wolfram Language. 2008. "SetSharedFunction." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/SetSharedFunction.html.
APA
Wolfram Language. (2008). SetSharedFunction. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/SetSharedFunction.html