SetSharedFunction
✖
SetSharedFunction
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)Summary of the most common use cases
https://wolfram.com/xid/0fq22aze120ya-jhbyig
When called from each subkernel, the definition of getPID is evaluated on the master kernel:
https://wolfram.com/xid/0fq22aze120ya-ycnpzk
https://wolfram.com/xid/0fq22aze120ya-hq6smc
Without the shared function, the values in the subkernels are returned:
https://wolfram.com/xid/0fq22aze120ya-g5sr5k
Define a shared function to manipulate the local variable results on the master kernel:
https://wolfram.com/xid/0fq22aze120ya-4sg4y9
Call the function from the subkernels:
https://wolfram.com/xid/0fq22aze120ya-55ka5l
The variable on the master has been modified:
https://wolfram.com/xid/0fq22aze120ya-kl74le
Scope (3)Survey of the scope of standard use cases
https://wolfram.com/xid/0fq22aze120ya-j0zcjx
When called from each subkernel, the function updates next on the master kernel:
https://wolfram.com/xid/0fq22aze120ya-g0j8ru
Set up a shared function with a default rule:
https://wolfram.com/xid/0fq22aze120ya-fmarg
Make additional definitions on the subkernels:
https://wolfram.com/xid/0fq22aze120ya-etsxkx
Since the function is shared, the master kernel knows about the additional rules:
https://wolfram.com/xid/0fq22aze120ya-hxxd08
Maintain indexed shared variables with a default value:
https://wolfram.com/xid/0fq22aze120ya-p1944
https://wolfram.com/xid/0fq22aze120ya-2gf5y
https://wolfram.com/xid/0fq22aze120ya-fzj0sw
Generalizations & Extensions (1)Generalized and extended use cases
A delayed definition for a shared function made on the master kernel:
https://wolfram.com/xid/0fq22aze120ya-d6r1e7
https://wolfram.com/xid/0fq22aze120ya-cd3e01
Such definitions are always evaluated on the master kernel, whichever kernel evaluates it:
https://wolfram.com/xid/0fq22aze120ya-j5riw5
A definition for a shared function made on one of the parallel kernels:
https://wolfram.com/xid/0fq22aze120ya-i1ljl
https://wolfram.com/xid/0fq22aze120ya-e6ents
Such definitions are always evaluated on the kernel that asks for the value:
https://wolfram.com/xid/0fq22aze120ya-ev0zdp
Applications (3)Sample problems that can be solved with this function
Shared functions can be used for synchronization:
https://wolfram.com/xid/0fq22aze120ya-dxlzru
https://wolfram.com/xid/0fq22aze120ya-cou7ta
https://wolfram.com/xid/0fq22aze120ya-i4rxzy
Less efficient is the use of a shared variable and critical section:
https://wolfram.com/xid/0fq22aze120ya-cy33m
https://wolfram.com/xid/0fq22aze120ya-j7g162
https://wolfram.com/xid/0fq22aze120ya-jlv1b
A constructor for a simple queue data type:
https://wolfram.com/xid/0fq22aze120ya-iro33j
https://wolfram.com/xid/0fq22aze120ya-holcof
https://wolfram.com/xid/0fq22aze120ya-cbhaey
https://wolfram.com/xid/0fq22aze120ya-bc0cp6
Work on the elements of the input queue in parallel and put results into the output queue:
https://wolfram.com/xid/0fq22aze120ya-m99qqu
https://wolfram.com/xid/0fq22aze120ya-cahuy0
https://wolfram.com/xid/0fq22aze120ya-dzfwa0
Use a single shared function to communicate both input and result:
https://wolfram.com/xid/0fq22aze120ya-diuflt
Set up a search and display its progress until it is manually aborted:
https://wolfram.com/xid/0fq22aze120ya-eszi1q
The results found so far—a list of the number of factors of :
https://wolfram.com/xid/0fq22aze120ya-ci3a5x
Properties & Relations (3)Properties of the function, and connections to other functions
Use a shared append function for a local variable to collect results:
https://wolfram.com/xid/0fq22aze120ya-6wfi4j
https://wolfram.com/xid/0fq22aze120ya-fkc40v
Using AppendTo on a shared variable has the same effect:
https://wolfram.com/xid/0fq22aze120ya-cjykci
https://wolfram.com/xid/0fq22aze120ya-7u9i39
Make fs be a (global) shared function:
https://wolfram.com/xid/0fq22aze120ya-iocmyr
Each subkernel makes a definition for the same shared variable:
https://wolfram.com/xid/0fq22aze120ya-gybj65
https://wolfram.com/xid/0fq22aze120ya-e59j0d
Without sharing, each subkernel has its own (local) copy of the function:
https://wolfram.com/xid/0fq22aze120ya-ksnsxl
https://wolfram.com/xid/0fq22aze120ya-hx6ol
https://wolfram.com/xid/0fq22aze120ya-kq2qbq
Shared functions are listed in $SharedFunctions:
https://wolfram.com/xid/0fq22aze120ya-cwtrf6
https://wolfram.com/xid/0fq22aze120ya-l8kw6o
Possible Issues (3)Common pitfalls and unexpected behavior
A shared function is inefficient for mere code distribution and leads to sequential evaluation:
https://wolfram.com/xid/0fq22aze120ya-yn4sz
https://wolfram.com/xid/0fq22aze120ya-dsa8g1
Simply distribute the definitions of any function needed on the parallel kernels:
https://wolfram.com/xid/0fq22aze120ya-rkvow
https://wolfram.com/xid/0fq22aze120ya-brazot
Separate read and write operations on a shared variable are not thread-safe:
https://wolfram.com/xid/0fq22aze120ya-kxo973
https://wolfram.com/xid/0fq22aze120ya-fybf7m
https://wolfram.com/xid/0fq22aze120ya-n1d1rr
Use a shared function to synchronize access to an (unshared) variable:
https://wolfram.com/xid/0fq22aze120ya-iyq090
https://wolfram.com/xid/0fq22aze120ya-sdcwj
https://wolfram.com/xid/0fq22aze120ya-j2o87a
Alternatively, use CriticalSection to make a whole code section atomic:
https://wolfram.com/xid/0fq22aze120ya-dyz7v5
https://wolfram.com/xid/0fq22aze120ya-hdb28c
https://wolfram.com/xid/0fq22aze120ya-b7qr5n
https://wolfram.com/xid/0fq22aze120ya-dpa965
https://wolfram.com/xid/0fq22aze120ya-iambka
https://wolfram.com/xid/0fq22aze120ya-hva4g1
https://wolfram.com/xid/0fq22aze120ya-intqyi
Neat Examples (1)Surprising or curious use cases
A parallel version of Sow:
https://wolfram.com/xid/0fq22aze120ya-hhvz1x
https://wolfram.com/xid/0fq22aze120ya-kr8kt
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
]}
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
]}