2
$\begingroup$

I am trying to redistribute a definition after removing the definition on parallel kernels.
Sample code on version 10.4.0.0:

x=3;  
DistributeDefinitions[x]  (* {x} *)
ParallelEvaluate[Print[x];];  (*  displayed as "3", "3", ... *)
ParallelEvaluate[Remove[x];];
DistributeDefinitions[x]  (* {} *)
ParallelEvaluate[Print[x];];  (* displayed as "x", "x", ... *)

The first behavior of parallel Print[x] is as expected, but the second is not. For unchanged x on the master kernel, is there a way to make DistribteDefinitions[x] work again ?

$\endgroup$
  • $\begingroup$ Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! $\endgroup$ – Michael E2 Aug 1 '16 at 18:54
1
$\begingroup$

You can run

x = 3;
DistributeDefinitions[x];
ParallelEvaluate[Print[x]];
ParallelEvaluate[Remove[x];];
x++;
DistributeDefinitions[x];
x--;
ParallelEvaluate[Print[x]];

If that works for you.

$\endgroup$
1
$\begingroup$

I found another answer in Shared variable can not be distributed again after UnsetShared.
Using Parallel`Developer`ClearDistributedDefinitions[],

SetOptions[{ParallelEvaluate}, DistributedContexts -> None]; (* for the avoidance of confusion in distributing definitions *)
x=3;  
DistributeDefinitions[x]
(* {x} *)
ParallelEvaluate[Print[x];];
ParallelEvaluate[Remove[x];];
Parallel`Developer`ClearDistributedDefinitions[];
DistributeDefinitions[x]
(* {x} *)
ParallelEvaluate[Print[x];];

I was able to observe the redistribution of x to parallel kernels.

$\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.