Re: Compile and Total
- To: mathgroup at smc.vnet.net
- Subject: [mg120259] Re: Compile and Total
- From: DrMajorBob <btreat1 at austin.rr.com>
- Date: Fri, 15 Jul 2011 04:09:27 -0400 (EDT)
- References: <201107150120.VAA23682@smc.vnet.net>
- Reply-to: drmajorbob at yahoo.com
Mathematica isn't compiling EITHER of your user functions, error message
or not. Both functions are SetDelayed and Compile has Attribute HoldAll,
hence Compile knows zero, zip, nada about g and h.
Compile does replace Total with a compiled version:
<< CompiledFunctionTools`
fC = Compile[{{x, _Real, 1}}, Total[x]];
CompilePrint@fC
"
1 argument
1 Integer register
1 Real register
1 Tensor register
Underflow checking off
Overflow checking off
Integer overflow checking on
RuntimeAttributes -> {}
T(R1)0 = A1
I0 = 4
Result = R0
1 R0 = TotalAll[ T(R1)0, I0]]
2 Return
"
For g, no such luck:
g[x_] := Total[x]
gC = Compile[{{x, _Real, 1}}, g@x];
CompilePrint@gC
"
1 argument
2 Tensor registers
Underflow checking off
Overflow checking off
Integer overflow checking on
RuntimeAttributes -> {}
T(R1)0 = A1
Result = T(R1)1
1 T(R1)1 = MainEvaluate[ Hold[g][ T(R1)0]]
2 Return
"
And for h (despite the lack of an error message):
h[x_] := x/Total[x]
hC = Compile[{{x, _Real, 1}}, h[x], {{_Total, _Real}}];
CompilePrint@hC
"
1 argument
2 Tensor registers
Underflow checking off
Overflow checking off
Integer overflow checking on
RuntimeAttributes -> {}
T(R1)0 = A1
Result = T(R1)1
1 T(R1)1 = MainEvaluate[ Hold[h][ T(R1)0]]
2 Return
"
Bobby
On Thu, 14 Jul 2011 20:20:19 -0500, Neil Stewart
<neil.stewart at warwick.ac.uk> wrote:
> Hello,
>
> I'm having some trouble using Compile[]. I've stripped my trouble down
> to a
> few basic examples.
>
> Compiling Total[x] works:
>
> data = Range[1, 10]
> {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
>
> fC = Compile[{{x, _Real, 1}}, Total[x]]
> CompiledFunction[{x}, Total[x], -Compiled Code-]
>
> fC[data]
> 55
>
> But compiling a function g[x_]:=Total[x] does not:
>
> g[x_] := Total[x]
> gC = Compile[{{x, _Real, 1}}, g[x]]
> CompiledFunction[{x}, g[x], -Compiled Code-]
>
> gC[data]
> CompiledFunction::cfex: Could not complete external evaluation at
> instruction 1; proceeding with uncompiled evaluation. >>
> 55
>
> Yet it is not something about my user function as h[x_] := x/Total[x]
> works:
>
> h[x_] := x/Total[x]
> hC = Compile[{{x, _Real, 1}}, h[x]]
> CompiledFunction[{x}, h[x], -Compiled Code-]
>
> hC[data]
> {0.0181818, 0.0363636, 0.0545455, 0.0727273, 0.0909091, 0.109091,
> 0.127273, 0.145455, 0.163636, 0.181818}
>
> It's not something about Total[] as I get the same results with
> Apply[Plus,
> x] instead.
>
> Any comments or insight will be extremely gratefully received.
>
> Best,
> Neil.
>
--
DrMajorBob at yahoo.com
- Follow-Ups:
- Re: Compile and Total
- From: Fred Simons <f.h.simons@tue.nl>
- Re: Compile and Total
- From: Fred Simons <f.h.simons@tue.nl>
- Re: Compile and Total
- References:
- Compile and Total
- From: Neil Stewart <neil.stewart@warwick.ac.uk>
- Compile and Total