Prevent synchronizing a certain symbol between main and parallel

215 views
Skip to first unread message

Szabolcs Horvát

unread,
Jan 7, 2012, 5:27:48 AM1/7/12
to

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?


Related:
http://stackoverflow.com/questions/8718256/remote-parallel-kernels-and-librarylink-how-to-make-them-work-together


--
Szabolcs Horvát
Mma QA site proposal: http://area51.stackexchange.com/proposals/37304

Oleksandr Rasputinov

unread,
Jan 8, 2012, 4:28:57 AM1/8/12
to
On Sat, 07 Jan 2012 10:27:48 -0000, Szabolcs Horv=E1t <szho...@gmail.com>
wrote:
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}

Oleksandr Rasputinov

unread,
Jan 8, 2012, 4:31:29 AM1/8/12
to
On Sat, 07 Jan 2012 22:06:18 -0000, Oleksandr Rasputinov
<oleksandr_...@hmamail.com> wrote:

> This behaviour is new in 8 and controlled using the DistributedContexts
> option (which has default value of $DefaultContexts).

Sorry, this should (obviously?) be $DistributedContexts, not
$DefaultContexts.

If you're looking for a context whose symbols can never be distributed
regardless of the value of $DistributedContexts or the DistributedContexts
option, I would suggest Private`.

(Actually, a full list of such contexts is given in the Options for the
undocumented new-in-8 function Language`ExtendedFullDefinition, but all of
the other possibilities are system contexts. If anyone is curious,
Language`ExtendedDefinition and Language`ExtendedFullDefinition are
analogous to Definition and FullDefinition but capture the definition of a
symbol in such a way as it can be reproduced in another kernel. For
example, defs = Language`ExtendedFullDefinition[sym] returns a
Language`DefinitionList object. The syntax used to restore the definition
is highly irregular: Language`ExtendedFullDefinition[] = defs, where defs
is a Language`DefinitionList. Note that Language`ExtendedFullDefinition
takes the ExcludedContexts option whereas Language`ExtendedDefinition does
not.)

Reply all
Reply to author
Forward
0 new messages