Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #5013 > unrolled thread
| Started by | Oliver Ruebenkoenig <ruebenko@wolfram.com> |
|---|---|
| First post | 2011-09-16 11:08 +0000 |
| Last post | 2011-09-22 11:34 +0000 |
| Articles | 6 — 3 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-16 11:08 +0000
Re: Compilation: Avoiding inlining DmitryG <einschlag@gmail.com> - 2011-09-17 10:31 +0000
Re: Compilation: Avoiding inlining DmitryG <einschlag@gmail.com> - 2011-09-19 11:12 +0000
Re: Compilation: Avoiding inlining David Bailey <dave@removedbailey.co.uk> - 2011-09-20 10:08 +0000
Re: Compilation: Avoiding inlining DmitryG <einschlag@gmail.com> - 2011-09-21 09:41 +0000
Re: Compilation: Avoiding inlining DmitryG <einschlag@gmail.com> - 2011-09-22 11:34 +0000
| From | Oliver Ruebenkoenig <ruebenko@wolfram.com> |
|---|---|
| Date | 2011-09-16 11:08 +0000 |
| Subject | Re: Compilation: Avoiding inlining |
| Message-ID | <j4vana$dal$1@smc.vnet.net> |
On Fri, 16 Sep 2011, DmitryG wrote:
> Here is a program with (* Definition of the equations *) made one-
> step, that performs the same as in my previous post:
>
> **************************************************************************
>
> (* Runge-Kutta-4 routine *)
> 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"*)]];
>
> (* Definition of the equations *)
> NN = 100;
> F = Function[{t, x}, Evaluate[ Table[-x[[i]] Sin[ 0.3 t]^2/(1 + 100
> Sum[x[[i + j]], {j, 1, Min[3, NN - i]}]^2), {i, 1, NN}]]];
> Print[]
>
> (* Compilation *)
> tt0 = AbsoluteTime[];
> Timing[RK4Comp = makeCompRK[F];]
> AbsoluteTime[] - tt0
> Print[];
>
> (* Setting parameters and Calculation *)
> x0 = Table[ RandomReal[{0, 1}], {i, 1, NN}]; t0 = 0; tMax =
> 200; 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}]
>
> **********************************************************
>
> As I understand, it is the command Evaluate that is responsible for
> inlining, making the size of the compiled code grow with NN and
> impossibility to compile in C. However, the program compiled with
> Mathematica runs fast.
>
> Removing Evaluate makes the compiled code small and independent of NN,
> also compiling in C becomes possible. In this case, unfortunately, the
> program runs slowly.
>
> Dmitry
>
>
Most likely due to call backs to Mathematica, you could check that with
CompilePrint.
Oliver
[toc] | [next] | [standalone]
| From | DmitryG <einschlag@gmail.com> |
|---|---|
| Date | 2011-09-17 10:31 +0000 |
| Message-ID | <j51str$pg4$1@smc.vnet.net> |
| In reply to | #5013 |
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
[toc] | [prev] | [next] | [standalone]
| From | DmitryG <einschlag@gmail.com> |
|---|---|
| Date | 2011-09-19 11:12 +0000 |
| Message-ID | <j5782g$jaj$1@smc.vnet.net> |
| In reply to | #5025 |
On Sep 17, 6:31 am, DmitryG <einsch...@gmail.com> 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
One more way to define the equations:
4)
(* Definition of the equations *)
NN = 1000;
F = Function[{t, x}, Table[ -x[[i]] Sin[0.1 t]^2/(1 +100
Evaluate[Sum[x[[i +
j]], {j, 1, Min[3, NN - i]}]]^2), {i, 1, NN}]];
>> Evaluation time 89.35 - slowest execution!. No inlining. Compiled code 145 lines independently of NN. Compilation in C gives practically the same speed
In all the programs above make n=500 steps.
To summarize, although, theoretically, with compilation in C
Mathematica has to be as fast as C, practically it is not. There are
too many variants to define the functions in the RHS of the ODE's and
the speeds are very different. So far there is absolutely no advantage
of C over Mathematica's own compiler. Of all variants, inlining the
code (that is a bad way of programming) leads to fastest execution.
I still hope that Wolfram will come up with the optimal way to solve
generic non-vectorizable systems of ODE's with compilation, this can
be done one time for the whole future life.
Dmitry
[toc] | [prev] | [next] | [standalone]
| From | David Bailey <dave@removedbailey.co.uk> |
|---|---|
| Date | 2011-09-20 10:08 +0000 |
| Message-ID | <j59omg$l9$1@smc.vnet.net> |
| In reply to | #5013 |
On 16/09/2011 12:08, Oliver Ruebenkoenig wrote: > On Fri, 16 Sep 2011, DmitryG wrote: > >> Here is a program with (* Definition of the equations *) made one- >> step, that performs the same as in my previous post.................... I tried pasting your example into Mathematica, but unfortunately there seems to be a variable 'x' which is undefined - presumably some input data. It might be worth posting a complete example, so that people can explore how to get decent performance. Error: Part::partd: "Part specification x[[1]] is longer than depth of object." David Bailey http://www.dbaileyconsultancy.co.uk
[toc] | [prev] | [next] | [standalone]
| From | DmitryG <einschlag@gmail.com> |
|---|---|
| Date | 2011-09-21 09:41 +0000 |
| Message-ID | <j5cbfm$d1i$1@smc.vnet.net> |
| In reply to | #5098 |
On Sep 20, 6:08 am, David Bailey <d...@removedbailey.co.uk> wrote:
> On 16/09/2011 12:08, Oliver Ruebenkoenig wrote:
>
> > On Fri, 16 Sep 2011, DmitryG wrote:
>
> >> Here is a program with (* Definition of the equations *) made one-
> >> step, that performs the same as in my previous post....................
>
> I tried pasting your example into Mathematica, but unfortunately there
> seems to be a variable 'x' which is undefined - presumably some input
> data. It might be worth posting a complete example, so that people can
> explore how to get decent performance.
>
> Error:
>
> Part::partd: "Part specification x[[1]] is longer than depth of object."
>
> David Baileyhttp://www.dbaileyconsultancy.co.uk
Hi David,
The RK4 procedure here works with the solution vector x whose initial
value is defined after the RK4 procedure and the equations are
defined. Mathematica does not know the lenght of x at the beginning
and this is why it complains. You can ignore these complaints. Of the
several codes posted above, that in Oliver's 19 September post is the
best because it is fully compiled. Here is this code with plotting the
solution:
***************************************
(* Runge-Kutta-4 routine *)
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](*,Parallelization->True*), CompilationTarget -> "C",
CompilationOptions -> {"InlineCompiledFunctions" -> True}]
(* Defining equations *)
NN = 1000;
cRHS = With[{NN = NN}, Compile[{{t, _Real, 0}, {x, _Real, 1}},
Table[-x[[i]]*
Sin[0.1 t]^2/(1 +
100 Sum[x[[i + j]], {j, 1, Min[3, NN - i]}]^2), {i, 1, NN}]
(*,
CompilationTarget->"C"*)(*,
CompilationOptions->{"InlineExternalDefinitions"->True}*)]];
(*Compilation*)
tt0 = AbsoluteTime[];
Timing[RK4Comp = makeCompRK[cRHS];]
AbsoluteTime[] - tt0
(*CompilePrint[RK4Comp2]*)
(*Setting parameters and Calculation*)
x0 = Table[
RandomReal[{0, 1}], {i, 1, NN}]; t0 = 0; tMax = 200; n = 500;
tt0 = AbsoluteTime[];
Sol = RK4Comp[x0, t0, tMax, n];
AbsoluteTime[] - tt0
Print["Compilation: ", Developer`PackedArrayQ@Sol]
(* Plotting *)
tList = Table[1. t0 + (tMax - t0) k/n, {k, 0, n}];
x1List = Transpose[{tList, Transpose[Sol][[1]]}];
x2List = Transpose[{tList, Transpose[Sol][[2]]}];
x3List = Transpose[{tList, Transpose[Sol][[3]]}];
ListLinePlot[{x1List, x2List, x3List}, PlotMarkers -> Automatic,
PlotStyle -> {Blue, Green, Red}, PlotRange -> {0, 1}]
Best,
Dmitry
[toc] | [prev] | [next] | [standalone]
| From | DmitryG <einschlag@gmail.com> |
|---|---|
| Date | 2011-09-22 11:34 +0000 |
| Message-ID | <j5f6g9$q9p$1@smc.vnet.net> |
| In reply to | #5013 |
On Sep 21, 5:41 am, DmitryG <einsch...@gmail.com> wrote:
> On Sep 20, 6:08 am, David Bailey <d...@removedbailey.co.uk> wrote:
>
>
>
>
>
>
>
>
>
> > On 16/09/2011 12:08, Oliver Ruebenkoenig wrote:
>
> > > On Fri, 16 Sep 2011, DmitryG wrote:
>
> > >> Here is a program with (* Definition of the equations *) made one-
> > >> step, that performs the same as in my previous post....................
>
> > I tried pasting your example into Mathematica, but unfortunately there
> > seems to be a variable 'x' which is undefined - presumably some input
> > data. It might be worth posting a complete example, so that people can
> > explore how to get decent performance.
>
> > Error:
>
> > Part::partd: "Part specification x[[1]] is longer than depth of object. "
>
> > David Baileyhttp://www.dbaileyconsultancy.co.uk
>
> Hi David,
>
> The RK4 procedure here works with the solution vector x whose initial
> value is defined after the RK4 procedure and the equations are
> defined. Mathematica does not know the lenght of x at the beginning
> and this is why it complains. You can ignore these complaints. Of the
> several codes posted above, that in Oliver's 19 September post is the
> best because it is fully compiled. Here is this code with plotting the
> solution:
>
> ***************************************
> (* Runge-Kutta-4 routine *)
> 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](*,Parallelization->True*), CompilationTarget -> "C",
> CompilationOptions -> {"InlineCompiledFunctions" -> True}]
>
> (* Defining equations *)
> NN = 1000;
> cRHS = With[{NN = NN}, Compile[{{t, _Real, 0}, {x, _Real, 1}},
> Table[-x[[i]]*
> Sin[0.1 t]^2/(1 +
> 100 Sum[x[[i + j]], {j, 1, Min[4, NN - i]}]^2), {i, 1, NN}]
> (*,
> CompilationTarget->"C"*)(*,
> CompilationOptions->{"InlineExternalDefinitions"->True}*)]];
>
> (*Compilation*)
> tt0 = AbsoluteTime[];
> Timing[RK4Comp = makeCompRK[cRHS];]
> AbsoluteTime[] - tt0
> (*CompilePrint[RK4Comp2]*)
>
> (*Setting parameters and Calculation*)
> x0 = Table[
> RandomReal[{0, 1}], {i, 1, NN}]; t0 = 0; tMax = 300; n = 500;
> tt0 = AbsoluteTime[];
> Sol = RK4Comp[x0, t0, tMax, n];
> AbsoluteTime[] - tt0
>
> Print["Compilation: ", Developer`PackedArrayQ@Sol]
>
> (* Plotting *)
> tList = Table[1. t0 + (tMax - t0) k/n, {k, 0, n}];
> x1List = Transpose[{tList, Transpose[Sol][[1]]}];
> x2List = Transpose[{tList, Transpose[Sol][[2]]}];
> x3List = Transpose[{tList, Transpose[Sol][[3]]}];
> ListLinePlot[{x1List, x2List, x3List}, PlotMarkers -> Automatic,
> PlotStyle -> {Blue, Green, Red}, PlotRange -> {0, 1}]
>
> Best,
>
> Dmitry
The execution time of the program above on my laptop today is 1.0 for
compilation RK4 in Mathematica and 0.24 for compilation RK4 in C. For
other compiled functions, it does not matter if the compilation target
is C or Mathematica (why?).
In my actual research program, I have a lot of external definitions
and all of them have to be compiled. To model this situation, I have
rewritten the same program with external definitions as follows:
............
(* Defining equations *)
NN = 1000;
ff = Compile[{{t}}, Sin[0.1 t]^2];
su = With[{NN = NN}, Compile[{{i, _Integer}, {x, _Real, 1}}, Sum[x[[i
+ j]], {j, 1, Min[4, NN - i]}]]];
cRHS = With[{NN = NN}, Compile[{{t}, {x, _Real, 1}},Table[-
x[[i]]*ff[t]/(1 + 100 su[i, x]^2), {i, 1, NN}, CompilationOptions ->
{"InlineExternalDefinitions" -> True, "InlineCompiledFunctions" ->
True}]];
...................................................
Now the execution time is 2.2 for compilation in Mathematica and 1.42
for compilation in C. We see there is a considerable slowdown because
of the external definition (actually because of su). I wonder why does
it happen? The external definitions are compiled and inlined in the
compiled code of RK4Comp, thus, to my understanding the execution time
should be the same. What is wrong here?
Dmitry
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web