[Date Index]
[Thread Index]
[Author Index]
Re: Compile forgets assigned values inside Do unless they are initialised
*To*: mathgroup at smc.vnet.net
*Subject*: [mg82312] Re: Compile forgets assigned values inside Do unless they are initialised
*From*: Andrew Moylan <andrew.j.moylan at gmail.com>
*Date*: Wed, 17 Oct 2007 04:07:23 -0400 (EDT)
*References*: <fepss7$gi3$1@smc.vnet.net><ff1pcn$8p2$1@smc.vnet.net>
I think your rule of thumb is a good one Jean-Marc, because it seems
to be required in general to make compiled code behave as expected.
However, I can't see any reason why the compiler can't in principle
systemically successfully parse functions like
Compile[{}, Module[{var}, Do[var = 0.;, {10}]; var]][]
without losing track of where/whether variables like var are assigned
values. Actually I wouldn't be surprised if this turned out to be just
a bug related to not setting (inside the compilation of the Do block)
a flag describing var as "set as a _Real" outside the Do block.
On Oct 16, 5:31 pm, "Jean-Marc Gulliet" <jeanmarc.gull... at gmail.com>
wrote:
> Giving more thoughts, I think you are right and my "thesis" would have
> been clearer if I had written, "As a rule of thumb, if one wants to
> compile an expression that uses local symbols, then these symbols
> should be initialized preferably at the beginning of the Module or
> Block construct, or as soon as possible."
>
> Why that? Because, as we are warned by the online documentation,
> "Compiled code does not handle numerical precision and local variables
> in the same way as ordinary Mathematica code."
>
> Moreover, "The number of times and the order in which objects are
> evaluated by Compile may be different from ordinary Mathematica code."
>
> In other words, ordinary evaluation rules do not necessarily and/or
> usually apply to compiled code.
>
> In[1]:= Block[{x}, Do[x = 2, {10}]; x]
>
> Out[1]= 2
>
> In[2]:= Compile[{}, Block[{x}, Do[x = 2, {10}]; x]][]
>
> During evaluation of In[2]:= Compile::initvar: The variable x has not \
> been initialized or has been initialized to Null. >>
>
> In[3]:= Compile[{}, Block[{x}, x = 2; Do[x = 2, {10}]; x]][]
>
> Out[3]= 2
>
> Regards,
> Jean-Marc
>
> On 10/15/07, DrMajorBob <drmajor... at bigfoot.com> wrote:
>
>
>
> > > Yes. If one wants to compile an expression that uses local symbols, then
> > > these local symbols must be initialized at the beginning of the Module
> > > (or Block) construct. See ref/message/Compile/initvar.
>
> > ref/message/Compile/initvar is precious little to go on, and the following
> > seems to contradict your thesis:
>
> > Compile[{},Block[{x},x=2]]
> > %[]
>
> > CompiledFunction[{},Block[{x},x=2],-CompiledCode-]
> > 2
>
> > Precisely how "late" is "too late"?
>
> > Bobby
>
> > On Mon, 15 Oct 2007 00:18:55 -0500, Jean-Marc Gulliet
> > <jeanmarc.gull... at gmail.com> wrote:
>
> > > Andrew Moylan wrote:
>
> > >> In the following example, although var is assigned a value, these are
>
> > > The value is set to late in your example (see below).
>
> > >> "forgotten" and an error occurs when it is returned as the result of the
>
> > > The value has not been "forgotten" since it has never been assigned to
> > > the symbol.
>
> > >> compiled function:
>
> > >> Compile[{},
> > >> Module[
> > >> {var},
> > >> Do[
> > >> var = 0.;,
> > >> {10}
> > >> ];
> > >> var
> > >> ]
> > >> ][]
>
> > >>>> Compile::initvar: The variable var has not been initialized or has
> > >>>> been
> > >> initialized to Null.
>
> > >> But if var is initialised (to any Real value at all), then all
> > >> subsequents
> > >> assignments to it are "remembered":
>
> > >> Compile[{},
> > >> Module[
> > >> {var = 1.},
> > >> Do[
> > >> var = 0.;,
> > >> {10}
> > >> ];
> > >> var
> > >> ]
> > >> ][]
>
> > >>>> 0.
>
> > >> Is this the expected behaviour?
>
> > > Yes. If one wants to compile an expression that uses local symbols, then
> > > these local symbols must be initialized at the beginning of the Module
> > > (or Block) construct. See ref/message/Compile/initvar.
>
> > > Regards,
>
> > --
> > DrMajor... at bigfoot.com
>
> --
> Jean-Marc
Prev by Date:
**Re: Integrate question**
Next by Date:
**Re: Re: format mixed integers & floats with text styling**
Previous by thread:
**Re: Re: Compile forgets assigned values inside Do unless they are initialised**
Next by thread:
**Re: Compile forgets assigned values inside Do unless they are initialised**
| |