SetSharedVariable

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)

Make xs be a (global) shared variable:

Each subkernel increments the (global) shared variable value:

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

Scope  (8)

Assignment  (3)

Share a single value:

Assign a new value to a shared variable:

Only the last assignment performed remains:

Append to a shared list:

Parts of a shared variable  (3)

Access a single element of a shared list efficiently:

Change a single element of a shared variable:

Modify a part of a shared variable:

Associations  (2)

Assign to a specific key:

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

Modify a value in an association:

Generalizations & Extensions  (1)

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

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

Applications  (5)

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

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

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

Use a shared variable to hand out tasks to do:

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

Return results as they are found:

Watch the results appear in real time:

Properties & Relations  (3)

$KernelCount is effectively a shared variable:

Shared variables are listed in $SharedVariables:

To share definitions of functions, use SetSharedFunction:

Shared functions are effectively evaluated on the master kernel:

Possible Issues  (3)

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

Use the Part notation instead:

Separate read and write operations are not thread-safe:

Use atomic updates for synchronization:

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

Create a shared variable whose value is a large matrix:

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

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

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.

CMS

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

BibTeX

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

BibLaTeX

@online{reference.wolfram_2024_setsharedvariable, organization={Wolfram Research}, title={SetSharedVariable}, year={2008}, url={https://reference.wolfram.com/language/ref/SetSharedVariable.html}, note=[Accessed: 22-November-2024 ]}