Re: Help on compiling a function
- To: mathgroup at smc.vnet.net
- Subject: [mg115827] Re: Help on compiling a function
- From: Oliver Ruebenkoenig <ruebenko at wolfram.com>
- Date: Fri, 21 Jan 2011 04:52:59 -0500 (EST)
- References: <201101191024.FAA03319@smc.vnet.net>
On Wed, 19 Jan 2011, Ramiro wrote: > Thanks so much, this was the answer exactly. Just a small > clarification: > > I have been going through the compiler documenttaion. If I do > On[ Compile::noinfo] > > I get the following: > > example1 = > Compile[{{n, _Real, 1}, {a, _Real}, {b, _Real}, {t, _Real, 1}}, > Block[{tn = Total[n]}, > b^a*Exp[LogGamma[tn + a] - (Total[LogGamma[n + 1]] + LogGamma[a]) + > Total[n*Log[t]] - (tn + a)*Log[Total[t] + b]]]]; > example1[{97.6203, 8.4788, 21.4204, 46.1755}, 1, > 1, {39.9342, 7.5820, 5.8656, 10.0553}] // AbsoluteTiming > > Compile::noinfo: No information is available for compilation of > LogGamma[Compile`$3]. The compiler will use an external evaluation and > make assumptions about the return type. >> > > Compile::noinfo: No information is available for compilation of > LogGamma[n+1]. The compiler will use an external evaluation and make > assumptions about the return type. >> > > Compile::noinfo: No information is available for compilation of > LogGamma[a]. The compiler will use an external evaluation and make > assumptions about the return type. >> > {0.000087, 1.06265*10^-11} > > But if I add the corresponding information about LogGamma and Total, > the function performs a bit slower: > > In[109]:= Clear[example1]; > example1 = > Compile[{{n, _Real, 1}, {a, _Real}, {b, _Real}, {t, _Real, 1}}, > Block[{tn = Total[n]}, > b^a*Exp[LogGamma[tn + a] - (Total[LogGamma[n + 1]] + LogGamma[a]) + > Total[n*Log[t]] - (tn + a)* > Log[Total[t] + > b]]], {{LogGamma[_], _Real}, {Total[_], _Real}}]; > example1[{97.6203, 8.4788, 21.4204, 46.1755}, 1, > 1, {39.9342, 7.5820, 5.8656, 10.0553}] // AbsoluteTiming > > Out[110]= {0.000141, 1.06265*10^-11} > > Why is this? > > Thanks to everyone for their help again, > > Ramiro > > Ramiro, if you look at Needs["CompiledFunctionTools`"] CompilePrint[example1] you'll see calls to the main evaluator. The "problematic" case is the LogGamma. Here is a suggestion from a colleague to re-write that into cfLogGamma = Compile[{{z, _Real}}, Module[{x = z, prod = 1.0}, While[x < 21.0, prod /= x; x++]; Log[prod] + 1/2 (Log[2 Pi] - Log[x]) + x (Log[x + 1/(12 x - 1/(10 x))] - 1)]]; you can then inline that into the compiled function example2 = With[{clg = cfLogGamma}, Compile[{{n, _Real, 1}, {a, _Real}, {b, _Real}, {t, _Real, 1}}, Block[ {tn = Total[n]}, b^a*Exp[ clg[tn + a] - (Total[clg /@ (n + 1)] + clg[a]) + Total[n*Log[t]] - (tn + a)*Log[Total[t] + b]] ], CompilationOptions -> {"InlineCompiledFunctions" -> True}]]; CompilePrint[example2] this then eliminates the call to main eval. Hope this helps, Oliver
- References:
- Re: Help on compiling a function
- From: Ramiro <ramiro.barrantes@gmail.com>
- Re: Help on compiling a function