Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2010

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

Search the Archive

Re: Any answer for this ParallelDo error

  • To: mathgroup at smc.vnet.net
  • Subject: [mg108900] Re: Any answer for this ParallelDo error
  • From: Albert Retey <awnl at gmx-topmail.de>
  • Date: Tue, 6 Apr 2010 07:25:58 -0400 (EDT)
  • References: <hpcjfk$mu3$1@smc.vnet.net>

Hi,

> 
> Here is a harmless piece of code.
> 
> Clear[a];
> t=AbsoluteTime[];
> a=ParallelTable[0,{i,1,20},{j,1,50}];
> DistributeDefinitions[a];
> mat=ParallelDo[a[[i,j]]=If[PrimeQ[(i^3+j^5)]==True,1,0],{i,1,20},{j,
> 1,50}];
> AbsoluteTime[]-t
> 
> The error is something like
> 
> Set::noval: Symbol a in part assignment does not have an immediate value.
> Set::noval: Symbol a in part assignment does not have an immediate value.
> 
> I know there is a way of using SetSharedVariable but that version of
> the code is very slow.
> 
> Clear[a];
> t=AbsoluteTime[];
> a=ParallelTable[0,{i,1,20},{j,1,50}];
> SetSharedVariable[a];
> mat=ParallelDo[a[[i,j]]=If[PrimeQ[(i^3+j^5)]==True,1,0],{i,1,20},{j,
> 1,50}];
> Print[a//ArrayPlot];
> AbsoluteTime[]-t
> 
> Time taken: 6.1443514
> 
> The single processor version is much faster
> 
> Clear[a];
> t=AbsoluteTime[];
> a=Table[0,{i,1,20},{j,1,50}];
> mat=Do[a[[i,j]]=If[PrimeQ[(i^3+j^5)]==True,1,0],{i,1,20},{j,1,50}];
> Print[a//ArrayPlot];
> AbsoluteTime[]-t
> 
> Time taken: 0.0360021
> 
> My question is why I cant distribute the definition of a array to my
> processor kernels.

If you want to change the same variable from different kernels, you
_have_ to use SetSharedVariable, and there _must_ be some kind of
synchronisation between the kernels and this _must_ cause overhead. If
you want speed, you need to take another approach, e.g. create a part of
the table on each kernel and only join the results in the master kernel.
It really depends on your problem and you need to take advantage of what
you know about the problem, automatic parallelization will often fail to
show speedup.

You should also realize that there is _always_ overhead when
parallelizing code, so you can only hope to see speedup if the
calculation times are larger than the expected overhead from
parallelization. For a problem that solves in 0.0360021 Seconds on one
kernel I doubt there is a chance to see any speedup with parallelization
at all (and that holds not only for Mathematica)...

> Hope someone can give me an answer. I need to make this Do loop
> parallel.

for the toy problem you sent there is no chance to see speedup
whatsover. If your real problem is larger, you should show something
that at least takes a few seconds to run, otherwise it will usually not
be possible to see whether parallelization is gaining anything at all.
You should also not add the timing for ArrayPlot, eventual speedups will
be even harder to see with it. For what you have shown, I wonder why you
don't just use:

a=ParallelTable[If[PrimeQ[(i^3+j^5)]==True,1,0],{i,1,20},{j,1,50}]

which does not have the problem of synchronization and doesn't need
SetSharedVariable. On the other hand the problem still solves in 0.036
Seconds on one kernel, and you will still suffer from overhead and not
see any speedup... On my 2 processor machine, the parallel version
starts to be faster than the serial for i,j about 200:

start = AbsoluteTime[];
a1 = Table[
   If[PrimeQ[(i^3 + j^5)] == True, 1, 0], {i, 1, 300}, {j, 1, 300}];
AbsoluteTime[] - start

1.1875000

start = AbsoluteTime[];
a2 = ParallelTable[
   If[PrimeQ[(i^3 + j^5)] == True, 1, 0], {i, 1, 300}, {j, 1, 300}
   ];
AbsoluteTime[] - start

0.8906250

for i=j=1000 the parallel version shows a speedup of 14.9/8.7=1.7, which
is already quite close the maximum of 2 that you could expect.

hth,

albert



  • Prev by Date: Re: if using Mathematica to solve an algebraic problem
  • Next by Date: Re: a little nasty problem?
  • Previous by thread: Re: Any answer for this ParallelDo error
  • Next by thread: Show[] on 3D graphics, rotation by mouse, how to improve performance