       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>

```Mathematica isn't compiling EITHER of your user functions, error message
or not. Both functions are SetDelayed and Compile has Attribute HoldAll,

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,
>
>
> Best,
> Neil.
>

--
DrMajorBob at yahoo.com

```

• Prev by Date: Re: Overriding the definition of Plus so that it does exact arithmetic: Bug or Feature?
• Next by Date: Re: Numerical accuracy/precision - this is a bug or a feature?
• Previous by thread: Compile and Total
• Next by thread: Re: Compile and Total