WOLFRAM

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 all

Basic Examples  (2)Summary of the most common use cases

Define and share a function:

When called from each subkernel, the definition of getPID is evaluated on the master kernel:

Out[9]=9
Out[17]=17

Without the shared function, the values in the subkernels are returned:

Out[10]=10

Define a shared function to manipulate the local variable results on the master kernel:

Call the function from the subkernels:

The variable on the master has been modified:

Out[3]=3

Scope  (3)Survey of the scope of standard use cases

Define and share a function:

When called from each subkernel, the function updates next on the master kernel:

Out[2]=2

Set up a shared function with a default rule:

Make additional definitions on the subkernels:

Out[2]=2

Since the function is shared, the master kernel knows about the additional rules:

Out[3]=3

Maintain indexed shared variables with a default value:

Out[2]=2
Out[3]=3

Generalizations & Extensions  (1)Generalized and extended use cases

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:

Out[3]=3

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:

Out[6]=6

Applications  (3)Sample problems that can be solved with this function

Shared functions can be used for synchronization:

Out[3]=3

Less efficient is the use of a shared variable and critical section:

Out[6]=6

A constructor for a simple queue data type:

Create two shared queues:

Fill the input queue:

Out[4]=4

Work on the elements of the input queue in parallel and put results into the output queue:

Out[5]=5
Out[7]=7

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 fara list of the number of factors of :

Out[3]=3

Properties & Relations  (3)Properties of the function, and connections to other functions

Use a shared append function for a local variable to collect results:

Out[2]=2

Using AppendTo on a shared variable has the same effect:

Out[4]=4

Make fs be a (global) shared function:

Each subkernel makes a definition for the same shared variable:

Out[2]=2
Out[3]=3

Without sharing, each subkernel has its own (local) copy of the function:

Out[4]=4
Out[5]=5

Shared functions are listed in $SharedFunctions:

Out[2]=2

Possible Issues  (3)Common pitfalls and unexpected behavior

A shared function is inefficient for mere code distribution and leads to sequential evaluation:

Out[2]=2

Simply distribute the definitions of any function needed on the parallel kernels:

Out[3]=3
Out[4]=4

Separate read and write operations on a shared variable are not thread-safe:

Out[2]=2
Out[3]=3

Use a shared function to synchronize access to an (unshared) variable:

Out[5]=5
Out[6]=6

Alternatively, use CriticalSection to make a whole code section atomic:

Out[8]=8
Out[9]=9

Parallel dynamic programming:

Out[3]=3
Out[4]=4

Neat Examples  (1)Surprising or curious use cases

A parallel version of Sow:

Out[2]=2
Wolfram Research (2008), SetSharedFunction, Wolfram Language function, https://reference.wolfram.com/language/ref/SetSharedFunction.html.
Wolfram Research (2008), SetSharedFunction, Wolfram Language function, https://reference.wolfram.com/language/ref/SetSharedFunction.html.

Text

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

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.

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

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

BibTeX

@misc{reference.wolfram_2024_setsharedfunction, author="Wolfram Research", title="{SetSharedFunction}", year="2008", howpublished="\url{https://reference.wolfram.com/language/ref/SetSharedFunction.html}", note=[Accessed: 08-January-2025 ]}

@misc{reference.wolfram_2024_setsharedfunction, author="Wolfram Research", title="{SetSharedFunction}", year="2008", howpublished="\url{https://reference.wolfram.com/language/ref/SetSharedFunction.html}", note=[Accessed: 08-January-2025 ]}

BibLaTeX

@online{reference.wolfram_2024_setsharedfunction, organization={Wolfram Research}, title={SetSharedFunction}, year={2008}, url={https://reference.wolfram.com/language/ref/SetSharedFunction.html}, note=[Accessed: 08-January-2025 ]}

@online{reference.wolfram_2024_setsharedfunction, organization={Wolfram Research}, title={SetSharedFunction}, year={2008}, url={https://reference.wolfram.com/language/ref/SetSharedFunction.html}, note=[Accessed: 08-January-2025 ]}