Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion newbie question on function assignment

Received: by 10.42.158.4 with SMTP id f4mr15502153icx.4.1328743643972;
        Wed, 08 Feb 2012 15:27:23 -0800 (PST)
X-BeenThere: cython-users@googlegroups.com
Received: by 10.50.207.65 with SMTP id lu1ls14655878igc.0.canary; Wed, 08 Feb
 2012 15:27:22 -0800 (PST)
Received: by 10.50.203.67 with SMTP id ko3mr9895239igc.4.1328743642345;
        Wed, 08 Feb 2012 15:27:22 -0800 (PST)
Received: by 10.50.203.67 with SMTP id ko3mr9895238igc.4.1328743642333;
        Wed, 08 Feb 2012 15:27:22 -0800 (PST)
Return-Path: <rober...@gmail.com>
Received: from mail-tul01m020-f179.google.com (mail-tul01m020-f179.google.com [209.85.214.179])
        by gmr-mx.google.com with ESMTPS id dk6si568961igb.1.2012.02.08.15.27.22
        (version=TLSv1/SSLv3 cipher=OTHER);
        Wed, 08 Feb 2012 15:27:22 -0800 (PST)
Received-SPF: pass (google.com: domain of rober...@gmail.com designates 209.85.214.179 as permitted sender) client-ip=209.85.214.179;
Authentication-Results: gmr-mx.google.com; spf=pass (google.com: domain of rober...@gmail.com designates 209.85.214.179 as permitted sender) smtp.mail=rober...@gmail.com; dkim=pass header...@gmail.com
Received: by mail-tul01m020-f179.google.com with SMTP id up6so1565556obb.38
        for <cython-users@googlegroups.com>; Wed, 08 Feb 2012 15:27:22 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=gmail.com; s=gamma;
        h=mime-version:sender:in-reply-to:references:from:date
         :x-google-sender-auth:message-id:subject:to:content-type
         :content-transfer-encoding;
        bh=ijdyky3lZM1a35334SlLtzbucT4aKyxr5n6no4/rL+A=;
        b=DWAP/kyPEHE3Lv0PHE+HWmqhm6D1xeJYBIPjCGKeuH5vlExyjP13c5gh74oNQi4DP3
         w+UvU15aXJ68M/nOpZ00A+nWIFhf3HQfJqn2qUVrc6FQTyOW7/grGu68MDW4epRTS8cW
         zpLV5F/5maBjVUWyIHirH5ZiG4lBOmK+39QEI=
Received: by 10.182.182.69 with SMTP id ec5mr13030324obc.48.1328743642161;
 Wed, 08 Feb 2012 15:27:22 -0800 (PST)
MIME-Version: 1.0
Sender: rober...@gmail.com
Received: by 10.60.41.134 with HTTP; Wed, 8 Feb 2012 15:26:52 -0800 (PST)
In-Reply-To: <CAHNdQ4KkSnYtnwtQ_Z8-1OMa_SKG-WiOipARsCsFouPUSBs...@mail.gmail.com>
References: <062fea19-e760-4aca-84f4-e2db1e334...@m2g2000vbc.googlegroups.com>
 <CADiQ+QCnr906WPvxt90LSVdhwgP3xAN2tyUm8VNW2ER95r+...@mail.gmail.com>
 <4F2D9AE5.8060...@stoneleaf.us> <CANg26EU5j-ShiuwjWgS1BQqecf64cYcXp2PHJnmu-sG-Rv=...@mail.gmail.com>
 <4F306338.3010...@stoneleaf.us> <CADiQ+QAa6Xk7cKxMLYhZ_7fvMAH_fXAnTiFH7XuDdpwRe0i...@mail.gmail.com>
 <CAHNdQ4KkSnYtnwtQ_Z8-1OMa_SKG-WiOipARsCsFouPUSBs...@mail.gmail.com>
From: Robert Bradshaw <rober...@math.washington.edu>
Date: Wed, 8 Feb 2012 15:26:52 -0800
Message-ID: <CADiQ+QCLJGU9zqN-M2ugg3-D2TnNuWtFBFDQQjOJNn+MUrw...@mail.gmail.com>
Subject: Re: [cython-users] newbie question on function assignment
To: cython-users@googlegroups.com
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

n Wed, Feb 8, 2012 at 9:10 AM, Aronne Merrelli
<aronne.merre...@gmail.com> wrote:
>
>
> On Wed, Feb 8, 2012 at 4:48 AM, Robert Bradshaw
> <rober...@math.washington.edu> wrote:
>>
>> On Mon, Feb 6, 2012 at 3:33 PM, Ethan Furman <et...@stoneleaf.us> wrote:
>> > mark florisson wrote:
>> >>
>> >> On 4 February 2012 20:53, Ethan Furman wrote:
>> >>>
>> >>> Robert Bradshaw wrote:
>> >>>>
>> >>>> On Tue, Jan 31, 2012 at 1:12 PM, paomo wrote:
>> >>>>>
>> >>>>> cdef f1():
>> >>>>> =C2=A0...
>> >>>>> cdef f2():
>> >>>>> =C2=A0...
>> >>>>> my_functions =3D {'case_1' : f1,
>> >>>>> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 'case_2' : f2} =
=C2=A0 # this won't compile
>> >>>>> my_functions['case_1']()
>> >>>>
>> >>>>
>> >>>> Either use standard python "def" functions rather than cdef
>> >>>> functions,
>> >>>> or index into an array of function pointers with enums.
>> >>>
>> >>>
>> >>> Apologies for my ignorance, but could you show an example of an arra=
y
>> >>> of
>> >>> funtion pointers and their use with enums?
>> >>
>> >>
>> >> If you don't know what it means, you probably don't want it :) Is
>> >> there any reason you're not using def functions?
>>
>> I'll second Mark's point.
>
>
>
> I'm trying to do this right now - it actually doesn't seem like an unusua=
l
> use case. Inside the innermost loop I am doing a calculation that I now
> would like to change depending on the simulation settings. So it is
> imperative to use cdef functions. I tried several implementations but the=
se
> have been unsatisfying. I tried what I thought was meant by 'function
> pointer array' with the following code (I'm a C newbie so I might be way =
off
> here...):
>
> cdef double basefunc0(double x):
> =C2=A0=C2=A0=C2=A0 return x*x
> cdef double basefunc1(double x):
> =C2=A0=C2=A0=C2=A0 return x*x*x
> cdef double (*funcList[2])(double)
> funcList[0] =3D &basefunc0
> funcList[1] =3D &basefunc1

OK, looks like you know what you're doing :). Yes, this is what I meant.

> The cython function that uses these functions looks like:
>
> def cy_calc_fptr(double x, int selected_func):
> =C2=A0=C2=A0=C2=A0 ...
> =C2=A0=C2=A0=C2=A0 theResult =3D funcList[selected_func](x)
> =C2=A0=C2=A0=C2=A0 ...
>
> It compiles, and I get the expected ouput. However this is still 3 orders=
 of
> magnitude slower than the following ugly hard coded implementation:
>
> def cy_ref_calc2(double x, double y, int function_id):
> =C2=A0=C2=A0=C2=A0 ...
> =C2=A0=C2=A0=C2=A0 if function_id =3D=3D 0:
> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 theResult =3D basefunc0(x)
> =C2=A0=C2=A0=C2=A0 else:
> =C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0=C2=A0 theResult =3D basefunc1(x)
> =C2=A0=C2=A0=C2=A0 ...
>
> The cython -a results show this as being "direct" C code; so it would see=
m
> that using the function pointer causes a large overheard even in C, which=
 is
> unexpected. Am I missing something or is that just the way it works?

I think that's just the way it is. Function pointers require an extra
level of indirection (as well as missing out on a number of
optimizations that could be performed after inlining such a short
function, which is probably the larger issue at play here).

> I posted the full pyx file as a gist since it was getting a bit long (I a=
lso
> tried cdef classes, but that was about a factor of 4-5 slower than functi=
on
> pointer).
>
> https://gist.github.com/1771057
>
>
> Thanks,
> Aronne
>