Compilation options question
- To: mathgroup at smc.vnet.net
- Subject: [mg115811] Compilation options question
- From: Ramiro <ramiro.barrantes at gmail.com>
- Date: Fri, 21 Jan 2011 04:33:04 -0500 (EST)
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}