Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #2789 > unrolled thread

Evaluation control in Compile[]

Started byBen <benp84@gmail.com>
First post2011-05-30 10:33 +0000
Last post2011-06-01 08:30 +0000
Articles 5 — 3 participants

Back to article view | Back to comp.soft-sys.math.mathematica


Contents

  Evaluation control in Compile[] Ben <benp84@gmail.com> - 2011-05-30 10:33 +0000
    Re: Evaluation control in Compile[] Albert Retey <awnl@gmx-topmail.de> - 2011-05-30 11:48 +0000
      Re: Evaluation control in Compile[] Ben <benp84@gmail.com> - 2011-05-31 11:46 +0000
    Re: Evaluation control in Compile[] Szabolcs Horvát <szhorvat@gmail.com> - 2011-05-31 11:49 +0000
    Re: Evaluation control in Compile[] Szabolcs Horvát <szhorvat@gmail.com> - 2011-06-01 08:30 +0000

#2789 — Evaluation control in Compile[]

FromBen <benp84@gmail.com>
Date2011-05-30 10:33 +0000
SubjectEvaluation control in Compile[]
Message-ID<irvrqm$8hh$1@smc.vnet.net>
I'd like to compile a function that estimates (assuming a spherical
earth) the length of a degree of longitude at any particular
latitude.  Here's how I write it up and test its speed:

In[1]:=  radiusOfEarth=6378100.;
latitudeDegreeLength=2*Pi*radiusOfEarth/360
Out[2]= 111319.
In[3]:= X=Table[RandomReal[{0,90}],{100000}];
In[4]:=
GetLongitudeDegreeLength=Compile[{latitude},latitudeDegreeLength*Cos[latitude
Degree]]
Out[4]= CompiledFunction[{latitude},latitudeDegreeLength Cos[latitude
=B0],-CompiledCode-]
In[5]:= Timing[Map[GetLongitudeDegreeLength,X];]
Out[5]:= {0.312,Null}

Since the values of "latitudeDegreeLength" and "Degree" are constant,
I'd prefer to use numerical approximations in the definition so the
compiled code doesn't have to request the values of these global
variables at runtime.  Like this:

In[6]:= GetLongitudeDegreeLength=Compile[{latitude},
111318.*Cos[0.0174532925 latitude]]
Out[6]:= CompiledFunction[{latitude},111318. Cos[0.0174533 latitude],-
CompiledCode-]
In[7]:= Timing[Map[GetLongitudeDegreeLength,X];]
Out[7]:= {0.031,Null}

Is there any way to tell Mathematica that I'd like these symbols to be
evaluated before the compilation so that I don't have to copy and
paste the numerical values into the definition?

Thanks.

[toc] | [next] | [standalone]


#2814

FromAlbert Retey <awnl@gmx-topmail.de>
Date2011-05-30 11:48 +0000
Message-ID<is005s$9or$1@smc.vnet.net>
In reply to#2789
Am 30.05.2011 12:33, schrieb Ben:
> I'd like to compile a function that estimates (assuming a spherical
> earth) the length of a degree of longitude at any particular
> latitude.  Here's how I write it up and test its speed:
>
> In[1]:=  radiusOfEarth=6378100.;
> latitudeDegreeLength=2*Pi*radiusOfEarth/360
> Out[2]= 111319.
> In[3]:= X=Table[RandomReal[{0,90}],{100000}];
> In[4]:=
> GetLongitudeDegreeLength=Compile[{latitude},latitudeDegreeLength*Cos[latitude
> Degree]]
> Out[4]= CompiledFunction[{latitude},latitudeDegreeLength Cos[latitude
> =B0],-CompiledCode-]
> In[5]:= Timing[Map[GetLongitudeDegreeLength,X];]
> Out[5]:= {0.312,Null}
>
> Since the values of "latitudeDegreeLength" and "Degree" are constant,
> I'd prefer to use numerical approximations in the definition so the
> compiled code doesn't have to request the values of these global
> variables at runtime.  Like this:
>
> In[6]:= GetLongitudeDegreeLength=Compile[{latitude},
> 111318.*Cos[0.0174532925 latitude]]
> Out[6]:= CompiledFunction[{latitude},111318. Cos[0.0174533 latitude],-
> CompiledCode-]
> In[7]:= Timing[Map[GetLongitudeDegreeLength,X];]
> Out[7]:= {0.031,Null}
>
> Is there any way to tell Mathematica that I'd like these symbols to be
> evaluated before the compilation so that I don't have to copy and
> paste the numerical values into the definition?

there are several, but for this case I think With is probably the clearest:

radiusOfEarth=6378100.;
With[{latitudeDegreeLength=2*Pi*radiusOfEarth/360,deg=N[Degree]},
GetLongitudeDegreeLength=Compile[{latitude},
	latitudeDegreeLength*Cos[latitude*deg]]
]

hth,

albert


[toc] | [prev] | [next] | [standalone]


#2830

FromBen <benp84@gmail.com>
Date2011-05-31 11:46 +0000
Message-ID<is2kdq$qmg$1@smc.vnet.net>
In reply to#2814
On May 30, 5:48 am, Albert Retey <a...@gmx-topmail.de> wrote:
> Am 30.05.2011 12:33, schrieb Ben:
>
>
>
>
>
>
>
>
>
> > I'd like to compile a function that estimates (assuming a spherical
> > earth) the length of a degree of longitude at any particular
> > latitude.  Here's how I write it up and test its speed:
>
> > In[1]:=  radiusOfEarth=6378100.;
> > latitudeDegreeLength=2*Pi*radiusOfEarth/360
> > Out[2]= 111319.
> > In[3]:= X=Table[RandomReal[{0,90}],{100000}];
> > In[4]:=
> > GetLongitudeDegreeLength=Compile[{latitude},latitudeDegreeLength*Cos[latitu de
> > Degree]]
> > Out[4]= CompiledFunction[{latitude},latitudeDegreeLength Cos[latitude
> > =B0],-CompiledCode-]
> > In[5]:= Timing[Map[GetLongitudeDegreeLength,X];]
> > Out[5]:= {0.312,Null}
>
> > Since the values of "latitudeDegreeLength" and "Degree" are constant,
> > I'd prefer to use numerical approximations in the definition so the
> > compiled code doesn't have to request the values of these global
> > variables at runtime.  Like this:
>
> > In[6]:= GetLongitudeDegreeLength=Compile[{latitude},
> > 111318.*Cos[0.0174532925 latitude]]
> > Out[6]:= CompiledFunction[{latitude},111318. Cos[0.0174533 latitude], -
> > CompiledCode-]
> > In[7]:= Timing[Map[GetLongitudeDegreeLength,X];]
> > Out[7]:= {0.031,Null}
>
> > Is there any way to tell Mathematica that I'd like these symbols to be
> > evaluated before the compilation so that I don't have to copy and
> > paste the numerical values into the definition?
>
> there are several, but for this case I think With is probably the clearest:
>
> radiusOfEarth=6378100.;
> With[{latitudeDegreeLength=2*Pi*radiusOfEarth/360,deg=N[Degree]},
> GetLongitudeDegreeLength=Compile[{latitude},
>         latitudeDegreeLength*Cos[latitude*deg]]
> ]
>
> hth,
>
> albert

Thank you!

[toc] | [prev] | [next] | [standalone]


#2837

FromSzabolcs Horvát <szhorvat@gmail.com>
Date2011-05-31 11:49 +0000
Message-ID<is2kjf$qre$1@smc.vnet.net>
In reply to#2789
Dear MathGroup members,

After reading this question, I started wondering if there is any way to 
receive warnings that there might be something "wrong" with a compiled 
function (e.g. that Mathematica might drop back to standard evaluation 
instead of running compiled code).

CCodeGenerate[] does issue some warnings:

CCodeGenerate[GetLongitudeDegreeLength, "fun"]

CCodeGenerate::wmreq: "The expression 
Function[{latitude},latitudeDegreeLength] requires Mathematica to be 
evaluated.   The function will be generated but can be expected to fail 
with a nonzero error code when executed"

Is there a way to get some similar warnings from Compile[]?  It is not 
obvious and intuitive that using an e.g. elsewhere defined constant 
might slow down a compiled function to non-compiled speed.

GetLongitudeDegreeLength =
    Function[{latitude}, latitudeDegreeLength*Cos[latitude Degree]]
Timing[Map[GetLongitudeDegreeLength,X];]

is only 1.5 slower on my machine than the compiled version (with 
non-inlined constants).  (I know that Map can auto-compile its argument, 
but setting SetSystemOptions["CompileOptions" -> "MapCompileLength" -> 
Infinity] doesn't seem to change anything here, so I assume that the 
timing is valid for the uncompiled case)

On 2011.05.30. 12:33, Ben wrote:
> I'd like to compile a function that estimates (assuming a spherical
> earth) the length of a degree of longitude at any particular
> latitude.  Here's how I write it up and test its speed:
>
> In[1]:=  radiusOfEarth=6378100.;
> latitudeDegreeLength=2*Pi*radiusOfEarth/360
> Out[2]= 111319.
> In[3]:= X=Table[RandomReal[{0,90}],{100000}];
> In[4]:=
> GetLongitudeDegreeLength=Compile[{latitude},latitudeDegreeLength*Cos[latitude
> Degree]]
> Out[4]= CompiledFunction[{latitude},latitudeDegreeLength Cos[latitude
> =B0],-CompiledCode-]
> In[5]:= Timing[Map[GetLongitudeDegreeLength,X];]
> Out[5]:= {0.312,Null}
>
> Since the values of "latitudeDegreeLength" and "Degree" are constant,
> I'd prefer to use numerical approximations in the definition so the
> compiled code doesn't have to request the values of these global
> variables at runtime.  Like this:
>
> In[6]:= GetLongitudeDegreeLength=Compile[{latitude},
> 111318.*Cos[0.0174532925 latitude]]
> Out[6]:= CompiledFunction[{latitude},111318. Cos[0.0174533 latitude],-
> CompiledCode-]
> In[7]:= Timing[Map[GetLongitudeDegreeLength,X];]
> Out[7]:= {0.031,Null}
>
> Is there any way to tell Mathematica that I'd like these symbols to be
> evaluated before the compilation so that I don't have to copy and
> paste the numerical values into the definition?
>
> Thanks.
>

[toc] | [prev] | [next] | [standalone]


#2845

FromSzabolcs Horvát <szhorvat@gmail.com>
Date2011-06-01 08:30 +0000
Message-ID<is4tao$aur$1@smc.vnet.net>
In reply to#2789
On 2011.05.30. 12:33, Ben wrote:
> I'd like to compile a function that estimates (assuming a spherical
> earth) the length of a degree of longitude at any particular
> latitude.  Here's how I write it up and test its speed:
>
> In[1]:=  radiusOfEarth=6378100.;
> latitudeDegreeLength=2*Pi*radiusOfEarth/360
> Out[2]= 111319.
> In[3]:= X=Table[RandomReal[{0,90}],{100000}];
> In[4]:=
> GetLongitudeDegreeLength=Compile[{latitude},latitudeDegreeLength*Cos[latitude
> Degree]]
> Out[4]= CompiledFunction[{latitude},latitudeDegreeLength Cos[latitude
> =B0],-CompiledCode-]
> In[5]:= Timing[Map[GetLongitudeDegreeLength,X];]
> Out[5]:= {0.312,Null}
>
> Since the values of "latitudeDegreeLength" and "Degree" are constant,
> I'd prefer to use numerical approximations in the definition so the
> compiled code doesn't have to request the values of these global
> variables at runtime.  Like this:
>
> In[6]:= GetLongitudeDegreeLength=Compile[{latitude},
> 111318.*Cos[0.0174532925 latitude]]
> Out[6]:= CompiledFunction[{latitude},111318. Cos[0.0174533 latitude],-
> CompiledCode-]
> In[7]:= Timing[Map[GetLongitudeDegreeLength,X];]
> Out[7]:= {0.031,Null}
>
> Is there any way to tell Mathematica that I'd like these symbols to be
> evaluated before the compilation so that I don't have to copy and
> paste the numerical values into the definition?
>

After reading up a bit more on Compile[] based on Oliver's replies, I 
came across another possible solution, which you might find interesting:

GetLongitudeDegreeLength =
  Compile[{{latitude, _Integer, 0}},
   latitudeDegreeLength*Cos[latitude Degree],
   CompilationOptions -> {"InlineExternalDefinitions" -> True}]


This is documented at ref/CompilationOptions and
Compile/tutorial/Operation

  -- Szabolcs

[toc] | [prev] | [standalone]


Back to top | Article view | comp.soft-sys.math.mathematica


csiph-web