Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #5086 > unrolled thread
| Started by | Oliver Ruebenkoenig <ruebenko@wolfram.com> |
|---|---|
| First post | 2011-09-19 11:14 +0000 |
| Last post | 2011-09-20 10:09 +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: Compilation: Avoiding inlining Oliver Ruebenkoenig <ruebenko@wolfram.com> - 2011-09-19 11:14 +0000
Re: Compilation: Avoiding inlining David Bailey <dave@removedbailey.co.uk> - 2011-09-20 10:09 +0000
| From | Oliver Ruebenkoenig <ruebenko@wolfram.com> |
|---|---|
| Date | 2011-09-19 11:14 +0000 |
| Subject | Re: Compilation: Avoiding inlining |
| Message-ID | <j57879$jbs$1@smc.vnet.net> |
On Sat, 17 Sep 2011, DmitryG wrote:
> Hi Oliver,
>
> thank you for your response! I am interested now in the systems of
> equations that are non-vectorizable. By mistake, in my first post I
> have used a system of equations that can be vectorized and this turned
> the discussion away from the topic. But in my second post, I have a
> non-vectorizable system of equations.
>
> I have identified three ways to define the system of equations. Below
> are the results with Mathematica compilation.
>
> 1)
> (* Definition of the equations *)
> NN = 1000;
> F = Function[{t, x}, Table[ -x[[i]] Sin[0.1 t]^2/(1 +100 Sum[x[[i +
> j]], {j, 1, Min[3, NN - i]}]^2), {i, 1, NN}]];
>
>>> Evaluation time 24.4983979 - slow execution. No inlining. Compiled code 189 lines independently of NN. Compilation in C gives practically the same speed
>
> 2)
> (* Definition of the equations *)
> NN = 1000;
> F = Function[{t, x}, Table[ Unevaluated[-x[[i]] Sin[0.1 t]^2/(1 +100
> Sum[x[[i + j]], {j, 1, Min[3, NN - i]}]^2)], {i, 1, NN}]];
>
>>> Evaluation time 4.7582721. No inlining. Compiled code 93 lines independently of NN. Compilation in C gives practically the same speed
>
> 3)
> (* Definition of the equations *)
> NN = 1000;
> F = Function[{t, x}, Evaluate[Table[ -x[[i]] Sin[0.1 t]^2/(1 +100
> Sum[x[[i + j]], {j, 1, Min[3, NN - i]}]^2), {i, 1, NN}]]];
>
>>> Evaluation time 1.0680555 - fastest execution. Here we have inlining of the code, the size of the compiled code increases with NN. Compilation in C is impossible.
>
>
> One can see that there are many different ways with very different
> results, and it is very important to find the best one - more
> important than to buy a faster computer;-))
>
> Does in make sense to introduce F as a compiled function, as you have
> done in your response? We already make compilation of the RK-4
> procedure with F injected into it.
>
> Best,
>
> Dmitry
>
>
Dmitry,
here is a version that, I hope, is a little less trick to understand:
ClearAll[makeCompRK]
makeCompRK[f_] :=
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"*)
,
CompilationOptions -> {"InlineCompiledFunctions" -> True}]
Concerning the function:
NN = 1000;
cRHS = With[{NN = NN},
Compile[{{t, _Real, 0}, {x, _Real, 1}}, -x*
Sin[0.1 t]^2/
Table[(1 + 100 Sum[x[[i + j]], {j, 1, Min[3, NN - i]}]^2), {i,
1, NN}](*,CompilationTarget->"C"*)]];
Needs["CompiledFunctionTools`"]
(*CompilePrint[cRHS]*)
(*Compilation*)
tt0 = AbsoluteTime[];
Timing[RK4Comp2 = makeCompRK[cRHS];]
AbsoluteTime[] - tt0
(* CompilePrint[RK4Comp2] *)
(* switch inling to True/False to see what is happening *)
(*Setting parameters and Calculation*)x0 =
Table[RandomReal[{0, 1}], {i, 1, NN}]; t0 = 0; tMax = 100; n = 500;
tt0 = AbsoluteTime[];
Sol2 = RK4Comp2[x0, t0, tMax, n];
AbsoluteTime[] - tt0
needs about 0.7s to run to completion, your fastest code needs about 1s
on my computer. The .. -> "C" version about needs about 0.12s on my
computer.
Hope this helps,
Oliver
[toc] | [next] | [standalone]
| From | David Bailey <dave@removedbailey.co.uk> |
|---|---|
| Date | 2011-09-20 10:09 +0000 |
| Message-ID | <j59opd$ne$1@smc.vnet.net> |
| In reply to | #5086 |
On 19/09/2011 12:14, Oliver Ruebenkoenig wrote: > On Sat, 17 Sep 2011, DmitryG wrote: > >> Hi Oliver, >> >> thank you for your response! I am interested now in the systems of >> equations that are non-vectorizable. I explored your code a bit, and it would seem that the function cRHS is called 2000 times. I wondered if most of the time was being spent in the function rather than the DE solving code itself, and it would seem that this is probably the case. I tried: m00;While[m>0,cRHS[0.1,x0];m--;]//AbsoluteTiming This gave a timing that was little less than the whole computation. (I actually scaled things up a bit, because I prefer to do timing experiments which are scaled to last about 20 secs to minimise jitter from other sources.) David Bailey http://www.dbaileyconsultancy.co.uk
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web