Giovanni Bajo | 2 Aug 17:51
Favicon
Gravatar

Re: [Cython] Include paths for pyximport (continued)

On Fri, 31 Jul 2009 18:53:22 +0200, Cyrille Rosset wrote:

> Hi all,
> 
> I am using cython and numpy and looked for a way to get cython modules
> using numpy compiled with pyximport.
> I found on the list the following message
> (http://codespeak.net/pipermail/cython-dev/2009-May/005289.html) but
> didn't find any modification in the cython repository in this direction.
> 
> Following Lisandro Dalcín's suggestion, I made a patch that adds a
> ext_opts option to pyximport.install() which cascades to
> pyximport.get_distutils_extension() and is used as a dictionnary with
> options for distutils.Extension().
> Only the dictionnary type is tested (I assume Extension will tell if an
> option is not good).
> I also added a pyximport.unsinstall() function (so it's possible to
> change options of the importer).

Can't you simply use **kwargs instead of ext_opts?
--

-- 
Giovanni Bajo
Develer S.r.l.
http://www.develer.com

_______________________________________________
Cython-dev mailing list
Cython-dev <at> codespeak.net
http://codespeak.net/mailman/listinfo/cython-dev
(Continue reading)

Jon Olav Vik | 4 Aug 20:26
Picon

[Cython] Problems compiling C code that #include's my public Cython function

Background: I'd like to compute second derivatives of a Python-implemented 
function with the help of a Fortran library called NDL [1]. The function to be 
differentiated can be implemented in C (an "external f" statement in the main 
Fortran program will make available the functions in the "f.o" object file). 
Now, I'm hoping to generate the requisite C code from a thin Cython wrapper 
around my function, as in [2].

As a first step, I have declared a Cython function "cdef public", compiled it 
(command-line output of compilation follows at the end), and am trying to call 
it from C. However, the compilation fails at the line '#include "fun.h"' and I 
don't understand why.

bash-3.2$ gcc usefun.c
In file included from usefun.c:2:
fun.h: In function 'DL_IMPORT':
fun.h:11: error: expected declaration specifiers before 'fun'
fun.h:15: error: expected declaration specifiers before 'PyMODINIT_FUNC'
In file included from /usr/include/stdio.h:34,
                 from usefun.c:3:
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/include/stddef.h:214: error: storage 
class specified for parameter 'size_t'
In file included from /usr/include/stdio.h:36,
                 from usefun.c:3:
/usr/include/bits/types.h:34: error: storage class specified for parameter 
'__u_char'
/usr/include/bits/types.h:35: error: storage class specified for parameter 
'__u_short'
[...etc etc]
/usr/include/stdio.h:821: error: expected ')' before '*' token
usefun.c:4: error: expected '=', ',', ';', 'asm' or '__attribute__' before '{' 
(Continue reading)

Daniele Pianu | 5 Aug 11:18
Picon
Gravatar

[Cython] Reference count on object assignment

Hi all,
I've a simple question about the reference count automatically
performed by Cython/Pyrex. I've a cdef function in an ipotetic
extension type:

......

cdef TakeObject( self, void* obj ):
  self.obj = <object>obj
  Py_INCREF( self.obj )

.......

The function receives a void pointer that I know it's always a pointer
to a Python object. The extension type must mantain a reference to
this object in its obj attribute, so, I perform
the assignment "self.obj = <object>obj". The reference should be valid
after the object pointed by the obj method argument is deref-counted.
For this reason, I increment manually the reference count of the same
object (now pointed by the obj instance attribute too). The question
is: in an assignment where a <object> cast is performed, is the
reference count automatically handled? Or do I need to manually
increment-decrement the reference count as in the example above? And,
if self.obj previously pointed to another object whose reference
counted was incremented, do I have to decrement the reference count to
avoid a possible memory leak? I've checked a bit the code and it seems
the, first than the assignment is done, the self.obj ref count is
decremented, but I've not understood if it's incremented too after the
new assignment.

(Continue reading)

Picon
Picon

Re: [Cython] Reference count on object assignment

I'm not sure of this, but I know how I'd find out: Compile the pyx with the -a switch, open the generated html,
and click on the line in question.

Dag Sverre Seljebotn
-----Original Message-----
From: Daniele Pianu <muogoro@...>
Date: Wednesday, Aug 5, 2009 11:26 am
Subject: [Cython] Reference count on object assignment
To: cython-dev@...: cython-dev@...

Hi all,
>I've a simple question about the reference count automatically
>performed by Cython/Pyrex. I've a cdef function in an ipotetic
>extension type:
>
>......
>
>cdef TakeObject( self, void* obj ):
>  self.obj = <object>obj
>  Py_INCREF( self.obj )
>
>.......
>
>The function receives a void pointer that I know it's always a pointer to a Python object. The extension
type must mantain a reference to
>this object in its obj attribute, so, I perform
>the assignment "self.obj = <object>obj". The reference should be valid after the object pointed by the
obj method argument is deref-counted. For this reason, I increment manually the reference count of the
same object (now pointed by the obj instance attribute too). The question
>is: in an assignment where a <object> cast is performed, is the
(Continue reading)

Jon Olav Vik | 5 Aug 15:47
Picon

Re: [Cython] Problems compiling C code that #include's my public Cython function

Jon Olav Vik <jonovik@...> writes:
> Background: I'd like to compute second derivatives of a Python-implemented 
> function with the help of a Fortran library called NDL [1]. The function to 
be 
> differentiated can be implemented in C (an "external f" statement in the main 
> Fortran program will make available the functions in the "f.o" object file). 
> Now, I'm hoping to generate the requisite C code from a thin Cython wrapper 
> around my function, as in [2].
> 
> As a first step, I have declared a Cython function "cdef public", compiled it 
> (command-line output of compilation follows at the end), and am trying to 
call 
> it from C. However, the compilation fails at the line '#include "fun.h"' and 
I 
> don't understand why.
[...]

Maybe a step in a relevant direction:

While Googling for examples of how to call Cython code from pure C, I stumbled 
across this posting:
http://article.gmane.org/gmane.comp.python.cython.devel/5515
It describes how to "embed the interpreter and statically link your module", 
and allows a C program to run the main() function of a Cython module called 
mylib.pyx. 

In deciding whether to spend more time on this, it would be nice to know:
1) Is this how I need to proceed to call a Cython function from a C program?
2) Can a function implemented in Cython be used as a callback for a C or 
Fortran optimization or differentiation routine?
(Continue reading)

Lisandro Dalcin | 5 Aug 16:13
Picon
Gravatar

Re: [Cython] Reference count on object assignment

Yes, in that case Cython manages the reference count the right way.
You do NOT NEED to INCREF/DECREF. In fact, your code is introducing a
reference leak.

On Wed, Aug 5, 2009 at 7:46 AM, Dag Sverre
Seljebotn<dagss@...> wrote:
> I'm not sure of this, but I know how I'd find out: Compile the pyx with the -a switch, open the generated html,
and click on the line in question.
>
> Dag Sverre Seljebotn
> -----Original Message-----
> From: Daniele Pianu <muogoro@...>
> Date: Wednesday, Aug 5, 2009 11:26 am
> Subject: [Cython] Reference count on object assignment
> To: cython-dev@...: cython-dev@...
>
> Hi all,
>>I've a simple question about the reference count automatically
>>performed by Cython/Pyrex. I've a cdef function in an ipotetic
>>extension type:
>>
>>......
>>
>>cdef TakeObject( self, void* obj ):
>>  self.obj = <object>obj
>>  Py_INCREF( self.obj )
>>
>>.......
>>
>>The function receives a void pointer that I know it's always a pointer to a Python object. The extension
(Continue reading)

Lisandro Dalcin | 5 Aug 16:19
Picon
Gravatar

Re: [Cython] Problems compiling C code that #include's my public Cython function

Could you tell me what language are you going to use for your "main"
application? I mean, are you coding in Python, and then want to reuse
the Fortran library? Or perhaps you are coding in C/Fortran, but want
to be able to write your functions in Python? Depending on that, the
approach is quite different (at least from my POV).

On Wed, Aug 5, 2009 at 10:47 AM, Jon Olav Vik<jonovik@...> wrote:
> Jon Olav Vik <jonovik@...> writes:
>> Background: I'd like to compute second derivatives of a Python-implemented
>> function with the help of a Fortran library called NDL [1]. The function to
> be
>> differentiated can be implemented in C (an "external f" statement in the main
>> Fortran program will make available the functions in the "f.o" object file).
>> Now, I'm hoping to generate the requisite C code from a thin Cython wrapper
>> around my function, as in [2].
>>
>> As a first step, I have declared a Cython function "cdef public", compiled it
>> (command-line output of compilation follows at the end), and am trying to
> call
>> it from C. However, the compilation fails at the line '#include "fun.h"' and
> I
>> don't understand why.
> [...]
>
> Maybe a step in a relevant direction:
>
> While Googling for examples of how to call Cython code from pure C, I stumbled
> across this posting:
> http://article.gmane.org/gmane.comp.python.cython.devel/5515
> It describes how to "embed the interpreter and statically link your module",
(Continue reading)

Jon Olav Vik | 5 Aug 19:52
Picon

Re: [Cython] Problems compiling C code that #include's my public Cython function

Lisandro Dalcin <dalcinl@...> writes:

> Could you tell me what language are you going to use for your "main"
> application? I mean, are you coding in Python, and then want to reuse
> the Fortran library? Or perhaps you are coding in C/Fortran, but want
> to be able to write your functions in Python? Depending on that, the
> approach is quite different (at least from my POV).

* Python is my main language.
* C is used for a rate function in a differential equation (provided to me from 
elsewhere).
* Fortran has the numerical differentiation library, which I'd like to use for 
the following reasons:
- Don't have to code it myself 8-)
- Presumably robust, efficient and numerically accurate.
- Citable publication.
- Parallel implementations are available (though I'm unsure how that will play 
with a Cython callback)

I'd be very grateful if you could recommend a strategy for this use case, or 
suggest other options. I include some more detail below in case you're 
interested. As for the mixed-language programming, I can see the following 
scenarios:

== Ideal case: f2cy lets me call Fortran library from Cython ==
(f2cy is described at fortrancython.wordpress.com.)
Then I could do, in principle:

cimport f2cy
import my_model # heavy initialization goes here
(Continue reading)

Kurt Smith | 5 Aug 20:20
Picon

Re: [Cython] Problems compiling C code that #include's my public Cython function

On Wed, Aug 5, 2009 at 12:52 PM, Jon Olav Vik<jonovik@...> wrote:
> Lisandro Dalcin <dalcinl@...> writes:
>
>> Could you tell me what language are you going to use for your "main"
>> application? I mean, are you coding in Python, and then want to reuse
>> the Fortran library? Or perhaps you are coding in C/Fortran, but want
>> to be able to write your functions in Python? Depending on that, the
>> approach is quite different (at least from my POV).
>
> * Python is my main language.
> * C is used for a rate function in a differential equation (provided to me from
> elsewhere).
> * Fortran has the numerical differentiation library, which I'd like to use for
> the following reasons:
> - Don't have to code it myself 8-)
> - Presumably robust, efficient and numerically accurate.
> - Citable publication.
> - Parallel implementations are available (though I'm unsure how that will play
> with a Cython callback)
>
> I'd be very grateful if you could recommend a strategy for this use case, or
> suggest other options. I include some more detail below in case you're
> interested. As for the mixed-language programming, I can see the following
> scenarios:
>
> == Ideal case: f2cy lets me call Fortran library from Cython ==
> (f2cy is described at fortrancython.wordpress.com.)
> Then I could do, in principle:

Hi, Jon.  I'm the 'maintaner' of fwrap, which is the new name of
(Continue reading)

Jon Olav Vik | 5 Aug 21:12
Picon

Re: [Cython] Problems compiling C code that #include's my public Cython function

Kurt Smith <kwmsmith@...> writes:
> Hi, Jon.  I'm the 'maintaner' of fwrap, which is the new name of
> 'f2cy' (I clearly need to put an update post on the blog!).  It's
> still in development, and will have basic functionality ready to go in
> a few weeks.  I don't know if we'll have C (i.e. cdef) callbacks
> implemented by then, but it will be high on the priority list.  It
> will certainly help you wrap a Fortran library in Cython, as you
> desire, although the api is similar to f2py where the wrapping is
> automated, 

Looking at http://www.scipy.org/F2py (it was down the last time I researched 
this), it certainly looks promising. What is it that fwrap offers that f2py 
doesn't? Direct access from C(ython), rather than the overhead of a Python 
extension module?

Is there a separate mailing list for f2py?

How would I pass a function pointer (callback) to the f2py-generated extension 
module?

> rather than the ctypes-like example you have below.

I'm not really familiar with ctypes; I tried to imitate the Cython coding 
patterns I have acquired in the last few months 8-) Could you tell me what is 
ctypes-like about it?

> If you
> can't wait that long then you'll have to find another way.  I'm sorry
> that I can't devote the time to your particular mixed language
> programming questions, since I have to put all my time into fwrap.
(Continue reading)


Gmane