MathGroup Archive 2005

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

Search the Archive

Re: Nested iterators in Compile

  • To: mathgroup at smc.vnet.net
  • Subject: [mg55027] Re: [mg55010] Nested iterators in Compile
  • From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
  • Date: Thu, 10 Mar 2005 05:24:26 -0500 (EST)
  • References: <200503091134.GAA07027@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

On 9 Mar 2005, at 12:34, Maxim wrote:

> The fact that iterators work differently when used in Compile seems to
> lead to a serious limitation:
>
> In[1]:=
> Compile[{n},
>    Module[{s = 0},
>      Do[s += j, {i, n}, {j, i}];
>      s
> ]][10]
>
> CompiledFunction::cfse: Compiled expression i should be a machine-size
> real number.
>
> CompiledFunction::cfex: External evaluation error at instruction 4;
> proceeding with uncompiled evaluation.
>
> Out[1]=
> 220
>
> The iterator {j, i} is evaluated before any value is assigned to i, and
> the evaluation of the compiled code fails (and if we add the
> initialization i=0 before Do, we'll only get an empty loop as the 
> result,
> unlike the uncompiled version). So we have to resort to some 
> workarounds
> such as changing the inner iterator to {j, n} and adding If[j>i,
> Continue[]] to the loop body, which of course decreases the 
> performance.
>
> Maxim Rytin
> m.r at inbox.ru
>
>
I am not sure how to tell if performance is slower than a hypothetical  
(i.e. non-working) alternative  but:


n = 1000;

Module[{s = 0},
      Do[s += j, {i, n}, {j, i}];
      s
]//Timing

{3.81 Second,167167000}

vs

Compile[{n},
    Module[{s = 0},
     Do[ Do[s += j, {j, i}],{i,n}];
      s
]][1000]//Timing


{0.2 Second,167167000}

suggests to me the the limitation you are referring to may not be that 
bad.


Andrzej Kozlowski
Chiba, Japan
http://www.akikoz.net/andrzej/index.html
http://www.mimuw.edu.pl/~akoz/


  • Prev by Date: Re: String comparison
  • Next by Date: Re: findfit or solve?
  • Previous by thread: Nested iterators in Compile
  • Next by thread: Re: Nested iterators in Compile