5
$\begingroup$

I have the following code where ParallelSubmit called on OverBar does not return the same result as non-parallel code even though I used DistributeDefinitions. Without the OverBar it works fine. The code

A = Sum[lambda[order] (r - rp)^order, {order, 0, 2}];
OverBar[x_ + y_] := OverBar[x] + OverBar[y];
OverBar[x_ y_] := OverBar[x] OverBar[y];
OverBar[x_ /; NumericQ[x]] := Conjugate[x]
OverBar[x_[xa__]] := OverBar[x][xa]
OverBar[x_ /; MemberQ[RealSymbols, x]] := x
DefaultRealSymbols = {Power, r, rp};
RealSymbols = DefaultRealSymbols;
DistributeDefinitions[A, OverBar];

Coefficient[A, r - rp, 2]
WaitAll[ParallelSubmit[Coefficient[A, r - rp, 2]]]
Coefficient[OverBar[A], r - rp, 2]
WaitAll[ParallelSubmit[Coefficient[OverBar[A], r - rp, 2]]]

returns

lambda[2]
lambda[2]
\!\(\*OverscriptBox[\(lambda\), \(_\)]\)[2]
0

where the 0 should be \!\(\*OverscriptBox[\(lambda\), \(_\)]\)[2].

What am I doing wrong?

$\endgroup$
2
  • $\begingroup$ Have you checked that evaluating the command using ParallelEvaluate works as expected? $\endgroup$
    – TimRias
    Commented Jun 13, 2018 at 18:09
  • 1
    $\begingroup$ You are doing nothing wrong. When I run your code with OverBar replaced with overbar, it works. The same applies when I use ParallelEvaluate instead of WaitAll. So it looks like a bug to me. At least, it is unexpected behaviour, as you said. $\endgroup$ Commented Jun 13, 2018 at 18:55

2 Answers 2

3
$\begingroup$

The problem seems to be that DistributeDefinitions does not work for any of the built-in operators without built-in meaning. You can check by running

ParallelEvaluate@DownValues@OverBar

that none of the custom DownValues have been distributed to the parallel kernels.

As a work around you could wrap your definitions for OverBar in a ParallelEvaluate to evaluate the definitions on the parallel kernels. Alternatively, you can put your definitions in a package, and load it using ParallelNeeds. This has the advantage that the definitions will reload on any newly (re)started kernel.

Finally, you could change you definitions to not use the built-in OverBar operator but say overbar. You can then get Mathematica to put the Over bar in the output using

Format[overbar[x_]] := OverBar[x]
$\endgroup$
3
$\begingroup$

As a follow-up on mmeent's answer, you can distribute the definitions for OverBar via (see my answer here)

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

which leads to your expected result

lambda[2]
lambda[2]
\!\(\*OverscriptBox[\(lambda\), \(_\)]\)[2]
\!\(\*OverscriptBox[\(lambda\), \(_\)]\)[2]
$\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.