Re: Vector Runge-Kutta ODE solver with compilation?
- To: mathgroup at smc.vnet.net
- Subject: [mg116799] Re: Vector Runge-Kutta ODE solver with compilation?
- From: Daniel Lichtblau <danl at wolfram.com>
- Date: Mon, 28 Feb 2011 05:02:04 -0500 (EST)
----- Original Message ----- > [...big snip...] > > *************************************************************************** > > *************** > > (* Definition *) > > makeCompRK[fIn_]:=With[{f=fIn},Compile[{{x0,_Real,1},{t0,_Real}, > > {tMax,_Real},{n,_Integer}},Module[{h,K1,K2,K3,K4,SolList,x=x0,t}, > > h=(tMax-t0)/n; > > SolList=Table[x0,{n+1}]; > > Do[t=t0+k h; > > K1=h f[t,x]; > > K2=h f[t+(1/2) h,x+(1/2) K1]; > > K3=h f[t+(1/2) h,x+(1/2) K2]; > > K4=h f[t+h,x+K3]; > > x=x+(1/6) K1+(1/3) K2+(1/3) K3+(1/6) K4; > > SolList[[k+1]]=x > > > > ,{k,1,n}]; > > SolList],CompilationTarget->"C"]]; > > > > (* Calculation *) > > NN=30; > > RHS=Quiet[Table[-x[[i]]/(1+Sum[x[[j]],{j,1,NN}]^2),{i,1,NN}]]; (* > > Quiet to suppress complaints *) > > F=Function[{t,x},Evaluate[RHS]]; (* Evaluate to use actual form of > > RHS *) > > > > tt0=AbsoluteTime[]; > > Timing[RK4Comp=makeCompRK[F];] > > AbsoluteTime[]-tt0 > > Print[]; > > > > x0=Table[RandomReal[{0,1}],{i,1,NN}]; t0=0; tMax0; n= > =500; > > tt0=AbsoluteTime[]; > > Sol=RK4Comp[x0,t0,tMax,n]; > > AbsoluteTime[]-tt0 > > > > Print["Compilation: ",Developer`PackedArrayQ@Sol] > > > > (* Plotting time dependences *) > > > > x1List=Table[{1.t0+(tMax-t0)k/n,Sol[[k+1,1]]},{k,0,n}]; > > x2List=Table[{1.t0+(tMax-t0)k/n,Sol[[k+1,2]]},{k,0,n}]; > > x3List=Table[{1.t0+(tMax-t0)k/n,Sol[[k+1,3]]},{k,0,n}]; > > ListLinePlot[{x1List,x2List,x3List},PlotMarkers->Automatic,PlotStyle- > > > > >{Blue,Green,Red},PlotRange->{0,1}] > > > > Compile::nogen: A library could not be generated from the compiled > > function. >> > > Out[4]= {11.172,Null} > > Out[5]= 142.0625000 > > > > Out[10]= 0.0468750 > > Compilation: True > > > > *************************************************************************= > ** = > > ********************************** > > > > Whereas execution is fast, 0.0468750, compilation takes a long time. > > I > > measure both Timing and AbsoluteTime. The former, I believe, is the > > time used by Mathematica, whereas the latter is the total time used > > by > > Mathematica and my C compiler, Microsoft Visual C++ 2010 Express. > > Both > > times are long. Presumably, Mathematica generates some weird code > > that > > gives a hard time to C compiler. Here I also have an error message > > concerning a library but I haven' had this message as I ran this > > program before. > > > > Any help? > > > > Dmitry > > Another major problem: If in the code above one removes > CompilationTarget->"C", it is working well until NN becomes larger > then ~1000. For larger NN, say NN00, compilation breaks because of > lack of memory on my laptop. One can see how fast the memory is > getting consumed during compilation. This seems to be a deficiency of > the current version of the Mathematica compiler that probably makes it > inappropriate for solving larger problems where compilation is really > needed. And there seems to be an even bigger problem with using > external C compiler. > > Dmitry This is an unusual claim to make. The implication is something along the lines "Surely my usage is flawless, so all problems must be the fault of the underlying software." I code a fair amount in both Mathematica and C, and that sort of claim is one I would rarely make with either. Daniel Lichtblau Wolfram Research