MathGroup Archive 2006

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

Search the Archive

Re: Problem with compiled function (is this a bug?)

  • To: mathgroup at smc.vnet.net
  • Subject: [mg65581] Re: [mg65570] Problem with compiled function (is this a bug?)
  • From: "Szabolcs Horvát" <szhorvat at gmail.com>
  • Date: Sun, 9 Apr 2006 04:32:06 -0400 (EDT)
  • References: <200604080445.AAA25147@smc.vnet.net> <4437B413.5080704@wolfram.com>
  • Sender: owner-wri-mathgroup at wolfram.com

Thank you. Your reply was really helpful.

However, it seemed really strange that using the Outer function could
result in such an impressive speedup. I experimented a little bit with
the functions and found that the speedup was due to using N[ ].

If I work with machine precision numbers, the uncompiled Table[ ] is just
as fast:

cpl3[x_, y_] := Ceiling[Sqrt[Outer[Plus, (Range[x] - x/2.0)^2,
(Range[y] - y/2.0)^2]]]

fun[x_, y_] := Table[Norm[{i, j} - {x , y}/2.0], {i, 1, x}, {j, 1, y}]

fun2[x_, y_] := Table[Sqrt[(i - x/2.0)^2 + (i - y/2.0)^2], {i, 1, x}, {j, 1, y}]

In[46]:=
Timing[fun[1000,1000];]

Out[46]=
{2.875 Second,Null}

In[47]:=
Timing[fun2[1000,1000];]

Out[47]=
{0.578 Second,Null}

In[53]:=
Timing[cpl3[1000,1000];]

Out[53]=
{0.734 Second,Null}

It was a good idea not to use Norm[ ]. I used Norm[ ] because I
assumed that, as a built-in function, it would be faster than
Sqrt[x^2+y^2], but foolishly I didn't test it.

Szabolcs Horvat

On 4/8/06, Carl K. Woll <carlw at wolfram.com> wrote:
> szhorvat at gmail.com wrote:
> > I have a problem with the following compiled function:
> >
> > cpl = Compile[{x,y}, Table[Ceiling[Norm[{i, j} - Ceiling[{
> >     x, y}/2]]], {i, 1, x}, {j, 1, y}]]
> >
> > When trying to use this function (for example, as idx = cpl[100,100]),
> > I get the following error:
> >
> > CompiledFunction::"cfte" :
> > Compiled expression 49 Sqrt[2] should be a rank 1 tensor of
> > machine-size integers.
> >
> > CompiledFunction::cfex: External evaluation error at instruction 23;
> > proceeding with uncompiled evaluation.
> >
> > I get the same error even if I try to specify that all variables are
> > Reals:
> >
> > cpl = Compile[{{x, _Real}, {y, _Real}}, Table[Ceiling[Norm[{i, j} -
> > Ceiling[{
> >     x, y}/2]]], {i, 1, x}, {j, 1, y}], {{i, _Real}, {j, _Real}}]
> >
> > Is this a bug in Mathematica? If not, what is the right way to compile
> > this function? Does this error occur with earlier versions than 5.2?
> >
> > I'd like to use this function to generate indices for a matrix whose
> > values I want to average radially (ie. I'd like to compute the mean
> > values in the ring around the center of the matrix). Unofrtunately this
> > function takes more than 16 seconds on an 500x500 matrix on my machine
> > which seems unrealistically long for such a simple function. I need to
> > use the function interactively on many datasets, often much larger than
> > 500x500.
> >
> > Szabolcs Horvat
>
> In my previous post I told you that using Norm wasn't wise because it
> wasn't compilable. However, if you really want to use Norm, the error
> message can be avoided by using the optional third argument of Compile.
>
> cpl=Compile[{x,y},
>    Table[Ceiling[Norm[{i,j}-Ceiling[{x,y}/2]]],{i,1,x},{j,1,
>      y}],{{_Norm,_Real}}];
>
> The third argument tells the compiler that the output of Norm is a real
> number. Without this information, the compiler thinks that Norm should
> return a vector, since it's input is a vector. Now there is no error
> message:
>
> In[17]:=
> cpl[2,3]
>
> Out[17]=
> {{1,0,1},{2,1,2}}
>
> Of course, this function is much, much slower than the one in my
> previous post.
>
> Carl Woll
> Wolfram Research
>


  • Prev by Date: Re: Problem with compiled function (is this a bug?)
  • Next by Date: To apply a function to a List
  • Previous by thread: Re: Problem with compiled function (is this a bug?)
  • Next by thread: Re: Problem with compiled function (is this a bug?)