WOLFRAM

SetSharedVariable[s1,s2,]

declares the symbols si as shared variables whose values are synchronized among all parallel kernels.

Details

  • The unique value of a shared variable is maintained by the master kernel and every access on a parallel subkernel is synchronized through the master kernel.
  • Shared variables without a value evaluate to Null.
  • SetSharedVariable has attribute HoldAll.

Examples

open allclose all

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

Make xs be a (global) shared variable:

Each subkernel increments the (global) shared variable value:

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

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

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

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

Assignment  (3)

Share a single value:

Out[9]=9

Assign a new value to a shared variable:

Out[2]=2

Only the last assignment performed remains:

Out[3]=3

Append to a shared list:

Out[3]=3

Parts of a shared variable  (3)

Access a single element of a shared list efficiently:

Out[5]=5

Change a single element of a shared variable:

Out[3]=3

Modify a part of a shared variable:

Out[2]=2

Associations  (2)

Assign to a specific key:

Out[35]=35

If the key is a string, Key can be omitted:

Out[37]=37

Modify a value in an association:

Out[2]=2

Generalizations & Extensions  (1)Generalized and extended use cases

Delayed values defined on the master kernel are evaluated on the master kernel:

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

Delayed values defined on a subkernel are evaluated on the kernel asking for the value:

Out[7]=7
Out[8]=8

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

Use shared variables to synchronize input and output in an infinite search:

Find such that is prime, until the computation is manually aborted:

Out[2]=2

Use a shared variable to signal all evaluations to stop as soon as a prime has been found:

Out[2]=2

Use a shared variable to hand out tasks to do:

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

Monitor the progress of a calculation, counting major calculation steps:

Out[2]=2

Return results as they are found:

Out[2]=2

Watch the results appear in real time:

Out[3]=3

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

$KernelCount is effectively a shared variable:

Out[1]=1
Out[2]=2

Shared variables are listed in $SharedVariables:

Out[2]=2

To share definitions of functions, use SetSharedFunction:

Shared functions are effectively evaluated on the master kernel:

Out[3]=3

Possible Issues  (3)Common pitfalls and unexpected behavior

The single-bracket notation for association access is not supported by shared variables:

Out[61]=61

Use the Part notation instead:

Out[64]=64

Separate read and write operations are not thread-safe:

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

Use atomic updates for synchronization:

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

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

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

Create a shared variable whose value is a large matrix:

Accessing its elements causes the whole matrix to be transferred to the subkernels repeatedly:

Out[2]=2

Use Unevaluated to allow the special code for parts of shared variables to see the variable:

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

Text

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

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

CMS

Wolfram Language. 2008. "SetSharedVariable." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/SetSharedVariable.html.

Wolfram Language. 2008. "SetSharedVariable." Wolfram Language & System Documentation Center. Wolfram Research. https://reference.wolfram.com/language/ref/SetSharedVariable.html.

APA

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

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

BibTeX

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

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

BibLaTeX

@online{reference.wolfram_2025_setsharedvariable, organization={Wolfram Research}, title={SetSharedVariable}, year={2008}, url={https://reference.wolfram.com/language/ref/SetSharedVariable.html}, note=[Accessed: 30-April-2025 ]}

@online{reference.wolfram_2025_setsharedvariable, organization={Wolfram Research}, title={SetSharedVariable}, year={2008}, url={https://reference.wolfram.com/language/ref/SetSharedVariable.html}, note=[Accessed: 30-April-2025 ]}