Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #4870 > unrolled thread
| Started by | DrMajorBob <btreat1@austin.rr.com> |
|---|---|
| First post | 2011-09-11 11:33 +0000 |
| Last post | 2011-09-13 11:24 +0000 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: help to make code run faster (mathematica v8.01) DrMajorBob <btreat1@austin.rr.com> - 2011-09-11 11:33 +0000
Re: help to make code run faster (mathematica v8.01) kristoph <kristophs.post@web.de> - 2011-09-13 11:24 +0000
| From | DrMajorBob <btreat1@austin.rr.com> |
|---|---|
| Date | 2011-09-11 11:33 +0000 |
| Subject | Re: help to make code run faster (mathematica v8.01) |
| Message-ID | <j4i6b6$c0d$1@smc.vnet.net> |
You should make this change just for sanity:
tStat[data_, band_, residuals_] :=
ParallelSum[
val1[residuals[[k]], residuals[[j]], data[[k]], data[[j]],
band], {k, 1, -1 + Length@data}, {j, k + 1, Length@data}]
tStat[reg, h, fit["FitResiduals"]] // AbsoluteTiming
and this for simplicity/transparency:
des = Thread[{1., reg}];
For speed, try this:
n = 250;
resp = RandomReal[10, n];
reg = RandomReal[1, n];
des = Thread[{1., reg}];
fit = LinearModelFit[{des, resp}];
h = 1.06 n^(-1/5) StandardDeviation@reg;
epanKern = Compile[{u}, If[Abs[u] < 1, 3/4 (1 - u^2), 0]];
Timing[
res = fit["FitResiduals"];
t1 = Flatten[Table[res[[k]] res[[j]], {k, 1, n - 1}, {j, k + 1, n}],
1];
t2 = Map[epanKern,
Flatten[Table[reg[[k]] - reg[[j]], {k, 1, n - 1}, {j, k + 1, n}],
1]/h];
t1.t2]
{0.139224, -379.538}
or
epanKern = Compile[{u}, Max[u, 0]];
Timing[
res = fit["FitResiduals"];
t1 = Flatten[Table[res[[k]] res[[j]], {k, 1, n - 1}, {j, k + 1, n}],
1];
t2 = 1 - (Flatten[
Table[reg[[k]] - reg[[j]], {k, 1, n - 1}, {j, k + 1, n}], 1]/
h)^2;
3/4 t1.epanKern /@ t2]
{0.124846, -379.538}
or
Timing[
res = fit["FitResiduals"];
t1 = Flatten[Table[res[[k]] res[[j]], {k, 1, n - 1}, {j, k + 1, n}],
1];
t2 = 1 - (Flatten[
Table[reg[[k]] - reg[[j]], {k, 1, n - 1}, {j, k + 1, n}], 1]/
h)^2;
t2 = Unitize[1 + Sign@t2] t2;
3/4 t1.t2]
{0.118036, -379.538}
When possible, take advantage of vectorized functions like Dot, Sign and
Unitize, rather than ParallelSum or Compile. You can get more from one
kernel than you might get from several.
Bobby
On Sat, 10 Sep 2011 06:28:51 -0500, kristoph <kristophs.post@web.de> wrote:
> 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
>
--
DrMajorBob@yahoo.com
[toc] | [next] | [standalone]
| From | kristoph <kristophs.post@web.de> |
|---|---|
| Date | 2011-09-13 11:24 +0000 |
| Message-ID | <j4nei4$52h$1@smc.vnet.net> |
| In reply to | #4870 |
On 11 Sep., 13:33, DrMajorBob <btre...@austin.rr.com> wrote:
> You should make this change just for sanity:
>
> tStat[data_, band_, residuals_] :=
> ParallelSum[
> val1[residuals[[k]], residuals[[j]], data[[k]], data[[j]],
> band], {k, 1, -1 + Length@data}, {j, k + 1, Length@data}]
> tStat[reg, h, fit["FitResiduals"]] // AbsoluteTiming
>
> and this for simplicity/transparency:
>
> des = Thread[{1., reg}];
>
> For speed, try this:
>
> n = 250;
> resp = RandomReal[10, n];
> reg = RandomReal[1, n];
> des = Thread[{1., reg}];
> fit = LinearModelFit[{des, resp}];
> h = 1.06 n^(-1/5) StandardDeviation@reg;
> epanKern = Compile[{u}, If[Abs[u] < 1, 3/4 (1 - u^2), 0]];
> Timing[
> res = fit["FitResiduals"];
> t1 = Flatten[Table[res[[k]] res[[j]], {k, 1, n - 1}, {j, k + 1, n}]=
,
> 1];
> t2 = Map[epanKern,
> Flatten[Table[reg[[k]] - reg[[j]], {k, 1, n - 1}, {j, k + 1, n}],
> 1]/h];
> t1.t2]
>
> {0.139224, -379.538}
>
> or
>
> epanKern = Compile[{u}, Max[u, 0]];
> Timing[
> res = fit["FitResiduals"];
> t1 = Flatten[Table[res[[k]] res[[j]], {k, 1, n - 1}, {j, k + 1, n}]=
,
> 1];
> t2 = 1 - (Flatten[
> Table[reg[[k]] - reg[[j]], {k, 1, n - 1}, {j, k + 1, n}], =
1]/
> h)^2;
> 3/4 t1.epanKern /@ t2]
>
> {0.124846, -379.538}
>
> or
>
> Timing[
> res = fit["FitResiduals"];
> t1 = Flatten[Table[res[[k]] res[[j]], {k, 1, n - 1}, {j, k + 1, n}]=
,
> 1];
> t2 = 1 - (Flatten[
> Table[reg[[k]] - reg[[j]], {k, 1, n - 1}, {j, k + 1, n}], =
1]/
> h)^2;
> t2 = Unitize[1 + Sign@t2] t2;
> 3/4 t1.t2]
>
> {0.118036, -379.538}
>
> When possible, take advantage of vectorized functions like Dot, Sign and =
> Unitize, rather than ParallelSum or Compile. You can get more from one =
> kernel than you might get from several.
>
> Bobby
>
>
>
>
>
>
>
>
>
> On Sat, 10 Sep 2011 06:28:51 -0500, kristoph <kristophs.p...@web.de> wrot=
e:
> > 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
>
> --
> DrMajor...@yahoo.com
As always very nice. Thanks for your kind help.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web