SetSharedVariable[s1,s2,…]
declares the symbols si as shared variables whose values are synchronized among all parallel kernels.
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 all close allBasic Examples (1)
Make xs be a (global) shared variable:
xs = 0;SetSharedVariable[xs];Each subkernel increments the (global) shared variable value:
ParallelEvaluate[xs++]xsWithout sharing, each subkernel has its own (local) copy of the variable:
xu = 0; DistributeDefinitions[xu];ParallelEvaluate[xu++]xuScope (8)
Assignment (3)
sv = 17;SetSharedVariable[sv]ParallelEvaluate[sv]Assign a new value to a shared variable:
SetSharedVariable[sx]ParallelEvaluate[sx = $KernelID]Only the last assignment performed remains:
sxsl = {};SetSharedVariable[sl]ParallelEvaluate[AppendTo[sl, $KernelID]];slParts of a shared variable (3)
Access a single element of a shared list efficiently:
sl = Range[10 ^ 6];SetSharedVariable[sl]ParallelEvaluate[Part[Unevaluated[sl], $KernelID]]Change a single element of a shared variable:
sl = Table[0, {$KernelCount}];SetSharedVariable[sl]ParallelDo[sl[[i]] = $KernelID, {i, Length[sl]}]slModify a part of a shared variable:
ParallelDo[sl[[i]]++, {i, Length[sl]}]slAssociations (2)
as = <||>;SetSharedVariable[as]ParallelEvaluate[as[[Key[$KernelID]]] = $KernelID];
asIf the key is a string, Key can be omitted:
ParallelEvaluate[as[[StringTemplate["kernel-``"][$KernelID]]] = $KernelID];
asModify a value in an association:
as = AssociationMap[Identity, #["KernelID"]& /@ ParallelKernels[]];
SetSharedVariable[as]ParallelEvaluate[++as[[Key[$KernelID]]]];
asGeneralizations & Extensions (1)
Delayed values defined on the master kernel are evaluated on the master kernel:
SetSharedVariable[sx]sx := $KernelIDsxParallelEvaluate[sx]Delayed values defined on a subkernel are evaluated on the kernel asking for the value:
SetSharedVariable[sy]ParallelEvaluate[sy := $KernelID, First[Kernels[]]]syParallelEvaluate[sy]Applications (5)
Use shared variables to synchronize input and output in an infinite search:
n = 1;primes = {}; SetSharedVariable[n, primes];Find
such that
is prime, until the computation is manually aborted:
PrintTemporary[Dynamic[{n, primes}]];
CheckAbort[
ParallelEvaluate[While[True,
With[{n = n++}, If[PrimeQ[n! + 1], AppendTo[primes, n]]]]],
{n, primes}]Use a shared variable to signal all evaluations to stop as soon as a prime has been found:
stop = False;SetSharedVariable[stop]ParallelEvaluate[Module[{p, r = $Failed}, While[!stop,
p = RandomPrime[4500];
If[PrimeQ[2 ^ p - 1], stop = True;r = p];
];r]]Use a shared variable to hand out tasks to do:
n = 100;SetSharedVariable[n]evs = Table[ParallelSubmit[Module[{r = $Failed}, While[r === $Failed, With[{p = Prime[n++]}, If[PrimeQ[2 ^ p - 1], r = p]]];
r]], {$KernelCount}]{res, ev, evs} = WaitNext[evs];
AbortKernels[];
{n, res}Monitor the progress of a calculation, counting major calculation steps:
i = 0;SetSharedVariable[i]PrintTemporary[Dynamic[i]];
ParallelEvaluate[Module[{n}, While[True, n = RandomInteger[1000];i++;If[PrimeQ[n! + 1], Break[]]];n]]Return results as they are found:
res = {};SetSharedVariable[res]ParallelDo[If[PrimeQ[2 ^ Prime[p] - 1], AppendTo[res, p]], {p, 100}];
resWatch the results appear in real time:
res = {};
PrintTemporary[Dynamic[res]];
ParallelDo[If[PrimeQ[2 ^ Prime[p] - 1], AppendTo[res, p]], {p, 500}];
resProperties & Relations (3)
$KernelCount is effectively a shared variable:
$KernelCountParallelEvaluate[$KernelCount]Shared variables are listed in $SharedVariables:
SetSharedVariable[xs]$SharedVariablesTo share definitions of functions, use SetSharedFunction:
SetSharedFunction[fs];fs[x_] := {x, $ProcessID, $KernelID}Shared functions are effectively evaluated on the master kernel:
ParallelEvaluate[fs[$KernelID]]Possible Issues (3)
The single-bracket notation for association access is not supported by shared variables:
assoc = <||>;SetSharedVariable[assoc]ParallelEvaluate[assoc[$KernelID] = $ProcessID]Use the Part notation instead:
ParallelEvaluate[assoc[[Key[$KernelID]]] = $ProcessID];
assocSeparate read and write operations are not thread-safe:
n = 0;SetSharedVariable[n]ParallelEvaluate[n = n + 1]nUse atomic updates for synchronization:
n = 0;SetSharedVariable[n]ParallelEvaluate[n++]nAlternatively, use CriticalSection to make a whole code section atomic:
n = 0;SetSharedVariable[n];Clear[nlock]ParallelEvaluate[CriticalSection[{nlock}, n = n + 1]]nCreate a shared variable whose value is a large matrix:
list = RandomReal[1, {10 ^ 4, 10 ^ 4}];
SetSharedVariable[list]Accessing its elements causes the whole matrix to be transferred to the subkernels repeatedly:
AbsoluteTiming[ParallelEvaluate[Table[list[[i, $KernelID]], {i, 4}]]]Use Unevaluated to allow the special code for parts of shared variables to see the variable:
AbsoluteTiming[ParallelEvaluate[Table[Part[Unevaluated[list], i, $KernelID], {i, 4}]]]History
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_2026_setsharedvariable, author="Wolfram Research", title="{SetSharedVariable}", year="2008", howpublished="\url{https://reference.wolfram.com/language/ref/SetSharedVariable.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_setsharedvariable, organization={Wolfram Research}, title={SetSharedVariable}, year={2008}, url={https://reference.wolfram.com/language/ref/SetSharedVariable.html}, note=[Accessed: 13-June-2026]}