2
$\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$ – mmeent Jun 13 '18 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$ – Fred Simons Jun 13 '18 at 18:55
1
$\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$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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