MathGroup Archive 2012

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Prevent synchronizing a certain symbol between main and parallel

  • To: mathgroup at smc.vnet.net
  • Subject: [mg124084] Re: Prevent synchronizing a certain symbol between main and parallel
  • From: "Oleksandr Rasputinov" <oleksandr_rasputinov at hmamail.com>
  • Date: Sun, 8 Jan 2012 04:25:54 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <je96n4$j6k$1@smc.vnet.net>

On Sat, 07 Jan 2012 10:27:48 -0000, Szabolcs Horv=E1t <szhorvat at gmail.com>  
wrote:

>
> Hello,
>
> I am trying to use LibraryLink with remote parallel kernels.  I end up
> with a library function (let's call it libFun[]) that has a different
> value on the main and parallel kernels due to different paths on the two
> machines.
>
> Unfortunately it is very easy to inadvertently synchronize the value of
> a symbol between the main and subkernels.  My question is about how I
> can ensure that this syncing will not happen for libFun[].
>
>
> I'll show in an isolated example how this might accidentally happen:
>
> In[1]:= LaunchKernels[2]
> Out[1]= {KernelObject[1, "local"], KernelObject[2, "local"]}
>
> Set value of x in main kernel:
>
> In[2]:= x = 1
> Out[2]= 1
>
> Note that it gets the same value in remote kernels too:
>
> In[3]:= ParallelEvaluate[x]
> Out[3]= {1, 1}
>
> Set a different value for x in the parallel kernels and verify that they
> keep it:
>
> In[4]:= ParallelEvaluate[x = 2]
> Out[4]= {2, 2}
>
> In[5]:= {x, ParallelEvaluate[x]}
> Out[5]= {1, {2, 2}}
>
> Now "innocently" use Parallelize on something containing x:
>
> In[6]:= Parallelize[Table[x, {10}]]
> Out[6]= {1, 1, 1, 1, 1, 1, 1, 1, 1, 1}
>
> And see how the value of x got re-synced between the main and subkernels.
>
> In[7]:= {x, ParallelEvaluate[x]}
> Out[7]= {1, {1, 1}}
>
>
> So the question is:
>
> How can I prevent a certain symbol from ever auto-syncing between the
> main and the subkernels?
>

This behaviour is new in 8 and controlled using the DistributedContexts 
option (which has default value of $DefaultContexts). It applies to all 
Parallel` functions that in some sense iterate over a list (these are now  
built around the new internal function parallelIterate which performs the  
distribution of values). Here Parallelize[Table[...]] gets changed into 
ParallelTable which meets this criterion. Apart from changing the option 
value, an easy way to avoid distribution for specific symbols is to define  
them in a context that is not a member of $DistributedContexts:

In[1]:= LaunchKernels[2]
Out[1]= {KernelObject[1, "local"], KernelObject[2, "local"]}

In[2]:= InputForm[$DistributedContexts]
Out[2]//InputForm= "Global`"

In[3]:= undistributed`x = 1
Out[3]= 1

In[4]:= ParallelEvaluate[undistributed`x]
Out[4]= {1, 1}

In[5]:= ParallelEvaluate[undistributed`x = 2]
Out[5]= {2, 2}

In[6]:= Parallelize[Table[undistributed`x, {10}]]
Out[6]= {2, 2, 2, 2, 2, 2, 2, 2, 2, 2}



  • Prev by Date: Re: ParametricPlot3D vs Reduce
  • Next by Date: Help! Mathematica 7 notebook is not working in Mathematica 8
  • Previous by thread: Re: Prevent synchronizing a certain symbol between main and parallel
  • Next by thread: FindFit for the 4-parameter beta distribution