MathGroup Archive 2011

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Compile and Total

  • To: mathgroup at smc.vnet.net
  • Subject: [mg120308] Re: Compile and Total
  • From: "Oleksandr Rasputinov" <oleksandr_rasputinov at hmamail.com>
  • Date: Mon, 18 Jul 2011 06:13:55 -0400 (EDT)
  • References: <201107150120.VAA23682@smc.vnet.net> <ivosnc$q79$1@smc.vnet.net>

In[1] :=
On[Compile::noinfo]; (* New in Mathematica 8 *)
SetSystemOptions[
   "CompileOptions" -> {"CompileReportExternal" -> True}
  ];

In[2] :=
g[x_] := Total[x]
gC = Compile[{{x, _Real, 1}}, g@x]

During evaluation of In[2] :=
Compile::noinfo: No information is available for compilation of g[x]. The  
compiler will use an external evaluation and make assumptions about the  
return type. >>

During evaluation of In[2] :=
Compile::exttensor: g[x] cannot be compiled and will be evaluated  
externally. The result is assumed to be a rank 1 tensor of type Real. >>

Out[2] =
CompiledFunction[{x}, g[x], -CompiledCode-]

One note of caution: if nested Function calls are passed to Compile along  
with "InlineExternalDefinitions" -> True, the calls at level 1 and deeper  
are not inlined, although Compile can successfully un-nest and perform  
common subexpression elimination on them if they are inlined manually (or  
otherwise; the code to inline these explicitly is not very hard to write).

On Sat, 16 Jul 2011 10:47:00 +0100, Yves Klett <yves.klett at googlemail.com>  
wrote:

> I wonder if in general a message/warning about MainEvaluate-calls should
> be issued.
>
> Regards,
> Yves
>
> Am 15.07.2011 10:11, schrieb DrMajorBob:
>> 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


  • Prev by Date: Re: numeric Groebner bases et al [Was Re: Numerical accuracy/precision - this is a bug or a feature?]
  • Next by Date: Re: Solve never calls Equal?
  • Previous by thread: Re: Compile and Total
  • Next by thread: Keeping it real