MathGroup Archive 2011

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

Search the Archive

Re: help to make code run faster (mathematica v8.01)

  • To: mathgroup at smc.vnet.net
  • Subject: [mg121375] Re: help to make code run faster (mathematica v8.01)
  • From: kristoph <kristophs.post at web.de>
  • Date: Tue, 13 Sep 2011 07:18:11 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • References: <j4i64g$but$1@smc.vnet.net>

On 11 Sep., 13:30, Daniel Lichtblau <d... at wolfram.com> wrote:
> ----- Original Message -----
> > From: "kristoph" <kristophs.p... at web.de>
> > To: mathgr... at smc.vnet.net
> > Sent: Saturday, September 10, 2011 6:28:51 AM
> > Subject: help to make code run faster (mathematica v8.01)
> > Hi
>
> > I'm running out of options in order to make my code run faster. I do
> > appreciate any help. I programmed a function named tStat that
> > basically sums over two compiled functions. Although, I run the
> > function parallel it is still rather slow. Thanks in advance for help.
> > Here is what I mean:
>
> > (*the following 5 lines is just random input data to test the
> > function*)
>
> > resp=RandomReal[10,250];
> > reg=RandomReal[1,250];
> > des=DesignMatrix[Table[{reg[[i]],resp[[i]]},{i,1,Length[reg]}],x,x];
> > fit=LinearModelFit[{des,resp}];
> > h=1.06 StandardDeviation[reg] Length[reg]^(-1/5);
>
> > (*the two compiled functions which are inputs for the function tStat*)
>
> > epanKern=Compile[{u},
> > If[Abs[u]<1,3/4 (1-u^2),0]
> > ];
>
> > val1=Compile[{{rk,_Real},{rj,_Real},{dk,_Real},{dj,_Real},
> > {band,_Real}},
> > rk rj epanKern[(dk-dj)/band]];
>
> > (*the following function is rather slow*)
>
> > tStat[data_,band_,residuals_,leg_]:=Module[{k,j,res=0,var=0},
> > res=ParallelSum[val1[residuals[[k]],residuals[[j]],data[[k]],data[[j]],band ],
> > {k,1,leg},{j,k+1,leg}];
> > 2 res
> > ];
>
> > (*executing the function*)
> > tStat[reg,h,fit["FitResiduals"],Length[reg]]//AbsoluteTiming
>
> Use of ParallelSum is possibly dangerous. If it is like Sum it may invoke symbolic preprocessing.
>
> Also you can make sure the Compile of any function using another does inlining. And you can compile tStat after minor changes. So here is one way to rewrite all this.
>
> epanKern = Compile[{u}, If[Abs[u] < 1., .75 (1. - u^2), 0.]];
>
> val1 = With[{epanKern = epanKern},
>    Compile[{{rk, _Real}, {rj, _Real}, {dk, _Real}, {dj, _Real}, \
> {band, _Real}}, rk rj epanKern[(dk - dj)/band],
>     CompilationOptions -> {"InlineExternalDefinitions" -> True}]];
>
> tStatC = Compile[{{data, _Real, 1}, {band, _Real}, {residuals, _Real,
>      1}, {leg, _Integer}},
>    Module[{k, j, res = 0.},
>     Do[res +=
>       val1[residuals[[k]], residuals[[j]], data[[k]], data[[j]],
>        band], {k, 1, leg}, {j, k + 1, leg}];
>     2.* res],
>    CompilationOptions -> {"InlineExternalDefinitions" -> True}];
>
> Your example:
>
> In[164]:=
> tStatC[reg, h, fit["FitResiduals"], Length[reg]] // AbsoluteTiming
>
> Out[164]= {0.0270000, -921.9237675987494}
>
> The original code took around 2.5 seconds on same.
>
> Daniel Lichtblau
> Wolfram Research

Thanks a lot for your inspiring help.




  • Prev by Date: Re: How create new style sheet- Mathematica 8
  • Next by Date: Re: help to make code run faster (mathematica v8.01)
  • Previous by thread: Re: help to make code run faster (mathematica v8.01)
  • Next by thread: execution model: Function vs. delayed execution