Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #7640 > unrolled thread

Prevent synchronizing a certain symbol between main and parallel

Started bySzabolcs Horvát <szhorvat@gmail.com>
First post2012-01-07 10:27 +0000
Last post2012-01-08 09:31 +0000
Articles 3 — 2 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Prevent synchronizing a certain symbol between main and parallel Szabolcs Horvát <szhorvat@gmail.com> - 2012-01-07 10:27 +0000
    Re: Prevent synchronizing a certain symbol between main and parallel "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> - 2012-01-08 09:28 +0000
    Re: Prevent synchronizing a certain symbol between main and parallel "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> - 2012-01-08 09:31 +0000

#7640 — Prevent synchronizing a certain symbol between main and parallel

FromSzabolcs Horvát <szhorvat@gmail.com>
Date2012-01-07 10:27 +0000
SubjectPrevent synchronizing a certain symbol between main and parallel
Message-ID<je96n4$j6k$1@smc.vnet.net>
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

[toc] | [next] | [standalone]


#7655

From"Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com>
Date2012-01-08 09:28 +0000
Message-ID<jebnkp$1d5$1@smc.vnet.net>
In reply to#7640
On Sat, 07 Jan 2012 10:27:48 -0000, Szabolcs Horv=E1t <szhorvat@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}

[toc] | [prev] | [next] | [standalone]


#7666

From"Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com>
Date2012-01-08 09:31 +0000
Message-ID<jebnph$1hm$1@smc.vnet.net>
In reply to#7640
On Sat, 07 Jan 2012 22:06:18 -0000, Oleksandr Rasputinov  
<oleksandr_rasputinov@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.)

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web