Re: Compile and Total

```A minor remark on Bobby's explanation: we can force compilation of the
functions g and h by defining then as anonymous functions:

<< CompiledFunctionTools`;

In[44]:= g=Total;
With[{g=g}, gC=Compile[{{x,_Real,1}}, g[x],
CompilationOptions->{"InlineExternalDefinitions"->True}]];
CompilePrint[gC]

Out[46]=
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

In[47]:= h=#/Total[#]&;
With[{h=h},hC=Compile[{{x,_Real,1}},h[x],
CompilationOptions->{"InlineExternalDefinitions"->True}]];
CompilePrint[hC]

Out[49]=
1 argument
1 Integer register
2 Real registers
3 Tensor registers
Underflow checking off
Overflow checking off
Integer overflow checking on
RuntimeAttributes -> {}

T(R1)0 = A1
I0 = 4
Result = T(R1)2

1    T(R1)1 = CopyTensor[ T(R1)0]]
2    R0 = TotalAll[ T(R1)1, I0]]
3    R1 = Reciprocal[ R0]
4    T(R1)2 = R1 * T(R1)1
5    Return

Fred Simons
Eindhoven University of Technology

Op 15-7-2011 10:09, DrMajorBob schreef:
> 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,
>>