Re: Re: Re: ParrallelDo and set::noval
- To: mathgroup at smc.vnet.net
- Subject: [mg102727] Re: [mg102704] Re: [mg102697] Re: ParrallelDo and set::noval
- From: Patrick Scheibe <pscheibe at trm.uni-leipzig.de>
- Date: Sat, 22 Aug 2009 03:37:06 -0400 (EDT)
- References: <h63cgg$1b6$1@smc.vnet.net> <h6duf8$h1c$1@smc.vnet.net>
Hi,
DistributeDefintions does exactly what the name suggests even when the
documentation (not the tutorial!) is not really extensive.
Do for instance something like
data = Table[RandomReal[], {10000000}];
DistributeDefinitions[data]
ParallelTable[i, {i, 1, 100}];
you see in your "System Monitor" (the taskmanager in other OSs) the
every started kernel has equal memory usage. This means
DistributeDefinitions really copies the data (or better: the
definitions) to every kernel. Changing them inside a parallel loop
wouldn't make sense since every kernel has only access to its own
"data".
Doing something like
data = Table[RandomReal[], {10000000}];
SetSharedVariable[data]
ParallelTable[i, {i, 1, 100}];
shows you that new started kernel have much less memory usage. Meaning
only one global instance of data and every kernel is using it.
I never tested how fast the read/writing process from this shared
variables is in Mathemtica since I try to prevent such situations.
I even don't know whether the code I posted
grid = Range[4, 50, 0.5];
testArray = Array[Null, {Length[grid]}];
DistributeDefinitions[grid];
SetSharedVariable[testArray];
ParallelDo[iroot = 0;
rr = grid[[i]];
Which[4 <= rr <= 50, iroot = 1];
testArray[[i]] = {i, iroot};, {i, 1, Length[grid]}]
will always work. Usually when accessing a shared variable (especially
for writing) you have to make a CriticalSection around it saying that
only one thread per time can access the memory. In some circumstance
when you are sure that threads/kernels never access the same datacell
(of an array for instance) you can skip this in some parallel
environments.
The documentation of Mathematica says
"Unsynchronized *reading* and writing access to a shared variable gives
wrong results:"
Have a look at the ParallelTools tutorial, it is quite comprehensive.
Cheers
Patrick
On Thu, 2009-08-20 at 22:57 -0500, DrMajorBob wrote:
> This implies that "grid" cannot be changed inside ParallelDo in your code,
> yes? And a simple test verifies as much.
>
> But then, surely, that should be mentioned in the documentation for
> DistributeDefinitions!
>
> Bobby
>
> On Thu, 20 Aug 2009 03:55:30 -0500, Patrick Scheibe
> <pscheibe at trm.uni-leipzig.de> wrote:
>
> > Hi,
> >
> > why do you create the table in the first place? Just let ParallelTable
> > collect your data:
> >
> > grid = Range[4, 50, 0.5];
> > DistributeDefinitions[grid];
> > testArray1 = ParallelTable[
> > {i, If[4 <= grid[[i]] <= 50, 1, 0]}, {i, 1, Length[grid]}]
> >
> > but if you insist to know what was wrong with your code, please have a
> > look at SetSharedVariable[...]
> >
> > grid = Range[4, 50, 0.5];
> > testArray = Array[Null, {Length[grid]}];
> > DistributeDefinitions[grid];
> > SetSharedVariable[testArray];
> > ParallelDo[iroot = 0;
> > rr = grid[[i]];
> > Which[4 <= rr <= 50, iroot = 1];
> > testArray[[i]] = {i, iroot};, {i, 1, Length[grid]}]
> >
> > testArray===testArray1
> >
> > Out[12]= True
> >
> > Cheers
> > Patrick
> >
> > On Wed, 2009-08-19 at 07:02 -0400, guerom00 wrote:
> >> As requested, a minimal example which illustrate the problem.
> >>
> >> The following sequential code works fine :
> >>
> >> grid = Range[4, 50, 0.5];
> >> testArray = Array[Null, {Length[grid]}];
> >>
> >> Do[
> >> iroot = 0;
> >> rr = grid[[i]];
> >> Which[
> >> 4 <= rr <= 50, iroot = 1
> >> ];
> >> testArray[[i]] = {i, iroot};
> >> , {i, 1, Length[grid]}]
> >>
> >>
> >> The equivalent parallel code :
> >>
> >> grid = Range[4, 50, 0.5];
> >> testArray = Array[Null, {Length[grid]}];
> >>
> >> DistributeDefinitions[grid,testArray];
> >>
> >> ParallelDo[
> >> iroot = 0;
> >> rr = grid[[i]];
> >> Which[
> >> 4 <= rr <= 50, iroot = 1
> >> ];
> >> testArray[[i]] = {i, iroot};
> >> , {i, 1, Length[grid]}]
> >>
> >> returns "Set::noval: Symbol testArray in part assignment does not have
> >> an immediate value." Somehow the DistributeDefinitions[] is not
> >> sufficient to indicate to each kernel that I have initialized the list
> >> testArray.
> >>
> >> Thanks in advance for any help :)
> >>
> >
> >
>
>
>