Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2011

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

Search the Archive

Re: Compilation options question

  • To: mathgroup at smc.vnet.net
  • Subject: [mg115865] Re: Compilation options question
  • From: Oliver Ruebenkoenig <ruebenko at wolfram.com>
  • Date: Sun, 23 Jan 2011 05:36:13 -0500 (EST)

On Fri, 21 Jan 2011, Ramiro wrote:

> Hello,
>
> I have successfully compiled the main parts of my program and runs
> much faster than before.  I do have a question.  Consider the
> following 4 examples, all compiled programs but with different
> options. Example0 is just plain compile, example1 has what I thought
> would be the optimal: parallelization and compilationtarget->"C" and
> declaration of the return types for the external calls, but it takes
> twice as long!!!  Why is that?
>
> If I remove the declaration of the return types it goes a bit faster
> than plain Compile even though it has parallelization and
> CompilationTarget-> "C", and if I remove  CompilationTarget-> "C" it's
> about the same.
>
> Any insight? Thanks so much to everyone for their help with Compile,
> my MCMC simulations will be much faster now.
>
> Ramiro
>
> p.s. My main program is basically multiplying the function in question
> (exampleN) many many times, that's why I put multiply over the same
> call.
>
>
>
> In[170]:=
> example0 =
>  Compile[{{n, _Real, 1}, {a, _Real}, {b, _Real}, {t, _Real, 1}},
>   With[{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]]]];
> Times @@ Table[
>   example0[{1, 1, 1, 1}, 1, 1, {3, 3, 3, 3}], {i,
>    10000}] // AbsoluteTiming
>
> Out[171]= {0.210959, 6.2372127891421*10^-22811}
>
> In[172]:=
> example1 =
>  Compile[{{n, _Real, 1}, {a, _Real}, {b, _Real}, {t, _Real, 1}},
>   With[{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}},
>   Parallelization -> True, CompilationTarget -> "C"];
> Times @@ Table[
>   example1[{1, 1, 1, 1}, 1, 1, {3, 3, 3, 3}], {i,
>    10000}] // AbsoluteTiming
>
> Out[173]= {0.414509, 6.2372127890803*10^-22811}
>
> In[174]:=
> example2 =
>  Compile[{{n, _Real, 1}, {a, _Real}, {b, _Real}, {t, _Real, 1}},
>   With[{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]]],
>   Parallelization -> True, CompilationTarget -> "C"];
> Times @@ Table[
>   example2[{1, 1, 1, 1}, 1, 1, {3, 3, 3, 3}], {i,
>    10000}] // AbsoluteTiming
>
> Out[175]= {0.188601, 6.2372127890803*10^-22811}
>
> In[176]:=
> calculatePoissonsGamma =
>  Compile[{{n, _Real, 1}, {a, _Real}, {b, _Real}, {t, _Real, 1}},
>   With[{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]]],
>   Parallelization -> True];
> Times @@ Table[
>   calculatePoissonsGamma[{1, 1, 1, 1}, 1, 1, {3, 3, 3, 3}], {i,
>    10000}] // AbsoluteTiming
>
> Out[177]= {0.219753, 6.2372127891421*10^-22811}
>
>


You could have a look and get the Table command "inside" the call to
compile. In the documentation have a look at RuntimeAttributes ->
{Listable} and the go for the parallelization.

a = 1;
b = 1;
calculatePoissonsGamma =
  With[{a = a, b = b},
   Compile[{{n, _Real, 1}, {t, _Real, 1}},
    With[{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]]],
    RuntimeAttributes -> {Listable}]]


nData = Table[{1, 1, 1, 1}, {i, 10000}];
tData = Table[{3, 3, 3, 3}, {i, 10000}];

(* this is already faster calling from Table *)
AbsoluteTiming[
  res2 = calculatePoissonsGamma[nData, tData];
  ]

next do all the replacements of the LogGamma, and then you could try to
add the Parallelization -> True option.

Oliver



  • Prev by Date: Re: problem with LessEqual::nord:
  • Next by Date: Re: Plot works in Mathematca 7 but not in Mathematica 8
  • Previous by thread: Re: Compilation options question
  • Next by thread: Re: Help with non-linear system of equations