From: pratip on
Dear Group Members,
Here is a typical problem of mathematica parallel computation tools.
First form a airfoil in 2D
(*Geometry*)
Clear[x];
n0;
c=0.09;
p=0.24;
t=0.05;
camber:=c If[x<p,(2p x-x^2)/p^2,((1-2p)+2p x-x^2)/(1-p)^2];
theta=ArcTan[D[camber,x]];
p=Table[
x=0.5(1-Cos[Pi s]);
x1=1.00893x;
thk=5t (0.2969Sqrt[x1]-0.126x1-0.3516x1^2+0.2843x1^3-0.1015x1^4);
{x,camber}+Sign[s]thk {-Sin[theta],Cos[theta]},
{s,-1,1,2/(n-1)}];
ListPlot[10p,AspectRatio->Automatic]

Now we set some values
(* Some variable are set*)
pt=10p;
alpha=Pi/19;
pc=Table[(pt[[i]]+pt[[i+1]])/2,{i,1,n-1}];
s=Table[v=pt[[i+1]]-pt[[i]];Sqrt[v.v],{i,1,n-1}];
theta=Table[v=pt[[i+1]]-pt[[i]];
ArcTan[v[[1]],v[[2]]],
{i,1,n-1}];
sin=Sin[theta];
cos=Cos[theta];
Cn1=Cn2=Ct1=Ct2=Table[0,{n-1},{n-1}];

Distribute to the parallel kernels
(*Definitions distribution*)
DistributeDefinitions[p, alpha, pc, s, theta, sin, cos, Cn1, Cn2, Ct1,
Ct2, n, c, pt, t]

Now comes the problem

(*Parallel Computation*)
tim=AbsoluteTime[];
ParallelDo[
If[i==j,
{Cn1[[i,j]]=-1;
Cn2[[i,j]]=1;
Ct1[[i,j]]=Ct2[[i,j]]=Pi/2},
{v=pc[[i]]-pt[[j]];
a=-v.{cos[[j]],sin[[j]]};
b=v.v;
t=theta[[i]]-theta[[j]];
c=Sin[t];
d=Cos[t];
e=v.{sin[[j]],-cos[[j]]};
f=Log[1+s[[j]](s[[j]]+2a)/b];
g=ArcTan[b+a s[[j]],e s[[j]]];
t=theta[[i]]-2theta[[j]];
p1=v.{Sin[t],Cos[t]};
q=v.{Cos[t],-Sin[t]};
Cn2[[i,j]]=d+(0.5q f-(a c+d e)g)/s[[j]];
Cn1[[i,j]]=0.5d f+c g-Cn2[[i,j]];
Ct2[[i,j]]=c+0.5p1 f/s[[j]]+(a d-c e)g/s[[j]];
Ct1[[i,j]]=0.5c f-d g-Ct2[[i,j]]}]
,{i,1,n-1},{j,1,n-1}];
AbsoluteTime[]-tim

The error is returned as

Set::noval: Symbol Cn1 in part assignment does not have an immediate
value.
Set::noval: Symbol Cn2 in part assignment does not have an immediate
value.

However if you replace the ParallelDo with simple Do results come in
7.0554036 sec.
I need to make the problem parallel. I have 8 cores to so for a simple
problem like this
I must get a timing like less than 1 sec.
Hope someone will help.
Another question is if I have a package (<<Mypackage`) and I want to
distribute all the function definitions of this package fully
available to the parallel kernels how to do that? Parallel computation
does not seem to be so simple as it turns out to be when you just
consider the documentation examples. How to make all the information
(variables, functions,data) available to the Master kernel also
available to the launched parallel kernels at once. Memeory is not a
problem.

best regards,
Pratip

From: dh on
Hi Pratip,
it is not clear to me why the DistributeDefinitions does not work. But
you may achieve the same with the somewhat slower "SahredVariable" concept:
Consider:
Clear[a];
a = Table[0, {2000}];
DistributeDefinitions[a];
ParallelDo[a[[i]] = 1, {i, 1, 4}]
this does not work, but the following will:
Clear[a];
a = Table[0, {2000}];
SetSharedVariable[a];
ParallelDo[a[[i]] = 1, {i, 1, 4}]

have fun, Daniel

On 01.04.2010 12:58, pratip wrote:
> Dear Group Members,
> Here is a typical problem of mathematica parallel computation tools.
> First form a airfoil in 2D
> (*Geometry*)
> Clear[x];
> n0;
> c=0.09;
> p=0.24;
> t=0.05;
> camber:=c If[x<p,(2p x-x^2)/p^2,((1-2p)+2p x-x^2)/(1-p)^2];
> theta=ArcTan[D[camber,x]];
> p=Table[
> x=0.5(1-Cos[Pi s]);
> x1=1.00893x;
> thk=5t (0.2969Sqrt[x1]-0.126x1-0.3516x1^2+0.2843x1^3-0.1015x1^4);
> {x,camber}+Sign[s]thk {-Sin[theta],Cos[theta]},
> {s,-1,1,2/(n-1)}];
> ListPlot[10p,AspectRatio->Automatic]
>
> Now we set some values
> (* Some variable are set*)
> pt=10p;
> alpha=Pi/19;
> pc=Table[(pt[[i]]+pt[[i+1]])/2,{i,1,n-1}];
> s=Table[v=pt[[i+1]]-pt[[i]];Sqrt[v.v],{i,1,n-1}];
> theta=Table[v=pt[[i+1]]-pt[[i]];
> ArcTan[v[[1]],v[[2]]],
> {i,1,n-1}];
> sin=Sin[theta];
> cos=Cos[theta];
> Cn1=Cn2=Ct1=Ct2=Table[0,{n-1},{n-1}];
>
> Distribute to the parallel kernels
> (*Definitions distribution*)
> DistributeDefinitions[p, alpha, pc, s, theta, sin, cos, Cn1, Cn2, Ct1,
> Ct2, n, c, pt, t]
>
> Now comes the problem
>
> (*Parallel Computation*)
> tim=AbsoluteTime[];
> ParallelDo[
> If[i==j,
> {Cn1[[i,j]]=-1;
> Cn2[[i,j]]=1;
> Ct1[[i,j]]=Ct2[[i,j]]=Pi/2},
> {v=pc[[i]]-pt[[j]];
> a=-v.{cos[[j]],sin[[j]]};
> b=v.v;
> t=theta[[i]]-theta[[j]];
> c=Sin[t];
> d=Cos[t];
> e=v.{sin[[j]],-cos[[j]]};
> f=Log[1+s[[j]](s[[j]]+2a)/b];
> g=ArcTan[b+a s[[j]],e s[[j]]];
> t=theta[[i]]-2theta[[j]];
> p1=v.{Sin[t],Cos[t]};
> q=v.{Cos[t],-Sin[t]};
> Cn2[[i,j]]=d+(0.5q f-(a c+d e)g)/s[[j]];
> Cn1[[i,j]]=0.5d f+c g-Cn2[[i,j]];
> Ct2[[i,j]]=c+0.5p1 f/s[[j]]+(a d-c e)g/s[[j]];
> Ct1[[i,j]]=0.5c f-d g-Ct2[[i,j]]}]
> ,{i,1,n-1},{j,1,n-1}];
> AbsoluteTime[]-tim
>
> The error is returned as
>
> Set::noval: Symbol Cn1 in part assignment does not have an immediate
> value.
> Set::noval: Symbol Cn2 in part assignment does not have an immediate
> value.
>
> However if you replace the ParallelDo with simple Do results come in
> 7.0554036 sec.
> I need to make the problem parallel. I have 8 cores to so for a simple
> problem like this
> I must get a timing like less than 1 sec.
> Hope someone will help.
> Another question is if I have a package (<<Mypackage`) and I want to
> distribute all the function definitions of this package fully
> available to the parallel kernels how to do that? Parallel computation
> does not seem to be so simple as it turns out to be when you just
> consider the documentation examples. How to make all the information
> (variables, functions,data) available to the Master kernel also
> available to the launched parallel kernels at once. Memeory is not a
> problem.
>
> best regards,
> Pratip
>


--

Daniel Huber
Metrohm Ltd.
Oberdorfstr. 68
CH-9100 Herisau
Tel. +41 71 353 8585, Fax +41 71 353 8907
E-Mail:<mailto:dh(a)metrohm.com>
Internet:<http://www.metrohm.com>