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 allBasic Examples (1)
Scope (8)
Assignment (3)
Parts of a shared variable (3)
Associations (2)
If the key is a string, Key can be omitted:
Generalizations & Extensions (1)
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:
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:
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