4
$\begingroup$

Consider a symbol in System such as CircleDot. I want to redefine this symbol and distribute the definition to subkernel, to use with ParallelEvaluate or the likes.

With a standard symbol in Global I can simply do

foo[___] := 1;
Print @ DownValues @ foo
ParallelEvaluate[Print @ DownValues @ foo];

which will show that the definition is automatically distributed to subkernels.

However, doing the same with CircleDot doesn't work:

enter image description here

Using DistributeDefinitions@CircleDot doesn't seem to change this. Why does this happen?

I know I can force subkernels to accept the definition by directly doing something like ParallelEvaluate[CircleDot[___]:=1], but this might not be desirable in some cases, and also I would like to understand why some symbols seem to behave different in this context.

$\endgroup$
8
  • $\begingroup$ Does this answer your question? How are function definitions distributed to parallel kernels? $\endgroup$
    – Szabolcs
    Sep 14, 2021 at 19:32
  • $\begingroup$ See also mathematica.stackexchange.com/a/128287/12 $\endgroup$
    – Szabolcs
    Sep 14, 2021 at 19:32
  • $\begingroup$ @Szabolcs thanks, hadn't seen those. So a possible solution is changing $DistributedContexts. This is however in this case not desirable, as far as I can tell, as we don't want to let the whole of System` distribute. Is there a way to have DistributeDefinitions work on a specific function in System` ? The solution given in the linked post, DistributeDefinitions[const`precisionRate], doesn't seem to work in this case: DistributeDefinitions[System`CircleDot] doesn't do what it should $\endgroup$
    – glS
    Sep 14, 2021 at 19:41
  • 2
    $\begingroup$ In trying to find an automatic way to identify the symbols that need to be redefined in each parallel kernel, I find it weird that Language`ExtendedFullDefinition[System`CircleDot]is an empty definition list, even after being defined. In contrast, Definition[CircleDot] and Information[CircleDot, "Definitions"] shows the user definition. Thought i'd mention it. $\endgroup$
    – thorimur
    Sep 14, 2021 at 22:24
  • 1
    $\begingroup$ @thorimur to see the definition list you have to use Language`ExtendedFullDefinition[CircleDot, "ExcludedContexts" -> {}] (see my answer below) $\endgroup$
    – Hausdorff
    Nov 26, 2021 at 14:30

1 Answer 1

6
$\begingroup$

Some spelunking of DistributeDefinitions shows that all contexts listed in Parallel`Protected`$ExcludedContexts are by default excluded from being distributed. In particular, Parallel`Protected`DistDefs has the line

updates = ExtendedFullDefinition[expr, "ExcludedContexts"->$ExcludedContexts]

So to make sure that new definitions of symbols in System` are also distributed you can use something like

CircleDot[___]:=1;

Block[
  {Parallel`Protected`$ExcludedContexts=
    DeleteCases[Parallel`Protected`$ExcludedContexts,"System`*"]},
  DistributeDefinitions[CircleDot];
];

ParallelEvaluate[Print @ DownValues @ CircleDot];
{HoldPattern[CircleDot[___]]:>1}
{HoldPattern[CircleDot[___]]:>1}
{HoldPattern[CircleDot[___]]:>1}
{HoldPattern[CircleDot[___]]:>1}

This way we also only distribute new definitions for CircleDot and nothing else in System`.

Regarding the comment of @thorimur: you do not get the full definition of CircleDot from Language`ExtendedFullDefinition, as the default option "ExcludedContexts"->Automatic seems to filter System` as well. So by setting this option yourself you can also see the full definition

Language`ExtendedFullDefinition[CircleDot, "ExcludedContexts" -> {}]

Language`DefinitionList[HoldForm[CircleDot] -> {OwnValues -> {}, SubValues -> {}, UpValues -> {}, DownValues -> {HoldPattern[CircleDot[___]] :> 1}, NValues -> {}, FormatValues -> {}, DefaultValues -> {}, Messages -> {}, 
Attributes -> {}}, HoldForm[BlankNullSequence] -> {OwnValues -> {}, SubValues -> {}, UpValues -> {}, DownValues -> {}, 
NValues -> {}, FormatValues -> {}, DefaultValues -> {}, Messages -> {}, Attributes -> {Protected}}, HoldForm[Rule] -> {OwnValues -> {}, SubValues -> {}, UpValues -> {}, DownValues -> {}, NValues -> {}, FormatValues -> {}, 
DefaultValues -> {}, Messages -> {HoldPattern[Rule::rhs] -> "Pattern `1` appears on the right-hand side of rule `2`."}, 
Attributes -> {Protected, SequenceHold}}, HoldForm[List] -> {OwnValues -> {}, SubValues -> {}, UpValues -> {}, DownValues -> {}, 
NValues -> {}, FormatValues -> {}, DefaultValues -> {}, Messages -> {}, Attributes -> {Locked, Protected}}]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.