Re: [f2py] f2py-users Digest, Vol 72, Issue 1

Pearu Peterson <[email protected]> Sun, 6 Feb 2011 19:44:58 +0200
Newsgroups gmane.comp.python.f2py.user
Message-ID <[email protected]>
On Sun, Feb 6, 2011 at 6:37 PM, Gaetan Kenway <kenway-kBRAJjP0/PtSpjfjxSPG1fd9D2ou9A/[email protected]>wrote:

> Hi Pearu
>
> Thanks a lot for the (very!) speedy response. I managed to get it working
> exactly the way I intended.  The function I use to pass the python function
> in, (sub1 from original email below), I also have to pass two additional
> arguments.


f2py generates cb_extra_args keyword argument to wrapper functions. The
argument can be used to pass on extra (python) arguments to the (python) cb
function that is called from Fortran.


> I was having an issue with seg faults, but I've determined this is the
> correct syntax for the pyf file (I explicitly manage my own pyf file)
>
>     subroutine solver(states,nstates)
>        use solver__user__routines
>        real*8 dimension(nstates) :: states
>        integer
> optional,intent(in),check(len(states)>=nstates),depend(states) ::
> nstates=len(states)
>        intent(callback) getasres
>        external getasres
>        intent(callback) applyaspc
>        external applyaspc
>      end subroutine solver
>
> Is there a reason why the callbacks getasres and applyaspc are not in the
> argument list from solver?


Yes. The reason is explained in the last example of


http://cens.ioc.ee/projects/f2py2e/usersguide/index.html#call-back-arguments


> Also I seemed to find the callbacks have to be at the END of the calling
> sequence. Is this the case in general?


Hmm, the order should not matter. The above and what follows should be
equivalent:

subroutine solver(states,nstates)
       use solver__user__routines
       intent(callback) getasres
       external getasres
       intent(callback) applyaspc
       external applyaspc
      real*8 dimension(nstates) :: states
       integer
optional,intent(in),check(len(states)>=nstates),depend(states) ::
nstates=len(states)
 end subroutine solver



> The above function is called from python as:
> module.solver(states,callback1,callback2), where states is a numpy array.
>

Yes. I think you can also use

  module.solver(states,callback1,callback2, getasres_extra_args=(...),
applyaspc_extra_args=(...))

Also check

  print module.solver.__doc__

However, If you rearrange the pyf file (and .f90 file) to give
>
>     subroutine solver(getasres,applyaspc,states,nstates)
>        use solver__user__routines
>        intent(callback) getasres
>        external getasres
>        intent(callback) applyaspc
>        external applyaspc
>        real*8 dimension(nstates) :: states
>        integer
> optional,intent(in),check(len(states)>=nstates),depend(states) ::
> nstates=len(states)
>      end subroutine solver
>
> and call from python module.solver(callback1,callback2,states) it
> seg-faults.
>
>
This (including external functions to solver argument list) is not a
solution to your original problem. The generated wrapper function will have
incompatible code for the usage case that you have, so I would not be
surprised that segfaults may happen.

Pearu

This isn't a huge issue, but it may be something to be aware of.
>
> Thanks again!
>
> Gaetan
>
>
> On Sun, Feb 6, 2011 at 5:00 AM, <f2py-users-request-Y4l6ocDipWCuvFJfX82//[email protected]> wrote:
>
>> Send f2py-users mailing list submissions to
>>        f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]
>>
>> To subscribe or unsubscribe via the World Wide Web, visit
>>        http://cens.ioc.ee/mailman/listinfo/f2py-users
>> or, via email, send a message with subject or body 'help' to
>>        f2py-users-request-Y4l6ocDipWCuvFJfX82//[email protected]
>>
>> You can reach the person managing the list at
>>        f2py-users-owner-Y4l6ocDipWCuvFJfX82//[email protected]
>>
>> When replying, please edit your Subject line so it is more specific
>> than "Re: Contents of f2py-users digest..."
>>
>>
>> Today's Topics:
>>
>>   1. Python Call Back in Fortran (Gaetan Kenway)
>>   2. Re: Python Call Back in Fortran (Pearu Peterson)
>>
>>
>> ----------------------------------------------------------------------
>>
>> Message: 1
>> Date: Sat, 5 Feb 2011 18:11:05 -0500
>> From: Gaetan Kenway <kenway-kBRAJjP0/PtSpjfjxSPG1fd9D2ou9A/[email protected]>
>> Subject: [f2py] Python Call Back in Fortran
>> To: f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]
>> Message-ID:
>>        <AANLkTi=tsNTZOFJzx2t1ZPRO2tu4R_2f7eQkv5QCjnM3-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
>> Content-Type: text/plain; charset="iso-8859-1"
>>
>> Hello
>>
>> I have a question about python callbacks in Fortran. I've been able to
>> setup
>> simple python call backs with no issue. The problem I'm having is I need a
>> way to "store" the call back function handle in Fortran. I need to call
>> the
>> python callback from a Fortran function that I can't explicitly pass the
>> python function handle to .  I need to have something like below.
>> Subroutine
>> sub1 is wrapped and sub2 has a fixed form that is actually called from and
>> internal PETSc function and I can't change the sequence of arguments.
>>
>> subroutine sub1(python_callback)
>>
>>    external python_callback
>>
>> end subroutine sub1
>>
>> subroutine sub2(arg1,arg2,arg3)
>>
>> external python_callback
>> ! I want to call python_callback here
>>    call python_callback(arg1,arg2,arg3)
>>
>> end subroutine sub2
>>
>> It would be nice if you could just put the callback into a module and make
>> that available in sub2. Is this possible in fortran? The code as above
>> compiles, but when you try to import it into Python, python complains that
>> python_callback is not defined. Is there a way to trick the compiler into
>> using the callback defined in the pyf file?  Is there some way of telling
>> f2py with an intent(callback) that the external python_callback in sub2 is
>> actually defined as python callback?
>>
>> If anyone has any suggestions or know this is entirely impossible in
>> fortran
>> it would be greatly appreciated.
>>
>> Gaetan Kenway
>> -------------- next part --------------
>> An HTML attachment was scrubbed...
>> URL:
>> http://cens.ioc.ee/pipermail/f2py-users/attachments/20110205/e733ea5d/attachment-0001.html
>>
>> ------------------------------
>>
>> Message: 2
>> Date: Sun, 6 Feb 2011 08:26:07 +0200
>> From: Pearu Peterson <[email protected]>
>> Subject: Re: [f2py] Python Call Back in Fortran
>> To: For users of the f2py program <f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]>
>> Message-ID:
>>        <AANLkTikUxHOr0C8EnDpXXuS1ZEaNBrowDwO_0CcQKSDW-JsoAwUIsXosN+BqQ9rBEUg@public.gmane.org>
>> Content-Type: text/plain; charset="iso-8859-1"
>>
>> Hi,
>>
>> Here follows an example that illustrates how to handle this usage case:
>>
>> !foo.f90:
>> subroutine sub1(python_callback)
>>
>>  !f2py intent(callback) python_callback
>>  external python_callback
>>
>>  !f2py call python_callback(0,0,0) !define python_callback signature
>>  call sub2(1,2,3)
>>
>> end subroutine sub1
>>
>> subroutine sub2(arg1,arg2,arg3)
>>  integer arg1, arg2, arg3
>>  external python_callback
>>  call python_callback(arg1,arg2,arg3)
>>
>> end subroutine sub2
>> !eof
>>
>> f2py -c -m ex foo.f90
>>
>> >>> def cb(arg1, arg2, arg3):
>> ...     print 'calling python cb(%s, %s, %s)' % (arg1, arg2, arg3)
>> ...
>> ...
>> >>> import ex
>> >>> ex.sub1(cb)
>> calling python cb(1, 2, 3)
>>
>> HTH,
>> Pearu
>>
>> On Sun, Feb 6, 2011 at 1:11 AM, Gaetan Kenway <kenway-kBRAJjP0/PtSpjfjxSPG1fd9D2ou9A/[email protected]
>> >wrote:
>>
>> > Hello
>> >
>> > I have a question about python callbacks in Fortran. I've been able to
>> > setup simple python call backs with no issue. The problem I'm having is
>> I
>> > need a way to "store" the call back function handle in Fortran. I need
>> to
>> > call the python callback from a Fortran function that I can't explicitly
>> > pass the python function handle to .  I need to have something like
>> below.
>> > Subroutine sub1 is wrapped and sub2 has a fixed form that is actually
>> called
>> > from and internal PETSc function and I can't change the sequence of
>> > arguments.
>> >
>> > subroutine sub1(python_callback)
>> >
>> >     external python_callback
>> >
>> > end subroutine sub1
>> >
>> > subroutine sub2(arg1,arg2,arg3)
>> >
>> > external python_callback
>> > ! I want to call python_callback here
>> >     call python_callback(arg1,arg2,arg3)
>> >
>> > end subroutine sub2
>> >
>> > It would be nice if you could just put the callback into a module and
>> make
>> > that available in sub2. Is this possible in fortran? The code as above
>> > compiles, but when you try to import it into Python, python complains
>> that
>> > python_callback is not defined. Is there a way to trick the compiler
>> into
>> > using the callback defined in the pyf file?  Is there some way of
>> telling
>> > f2py with an intent(callback) that the external python_callback in sub2
>> is
>> > actually defined as python callback?
>> >
>> > If anyone has any suggestions or know this is entirely impossible in
>> > fortran it would be greatly appreciated.
>> >
>> > Gaetan Kenway
>> >
>> > _______________________________________________
>> > f2py-users mailing list
>> > f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]
>> > http://cens.ioc.ee/mailman/listinfo/f2py-users
>> >
>> >
>> -------------- next part --------------
>> An HTML attachment was scrubbed...
>> URL:
>> http://cens.ioc.ee/pipermail/f2py-users/attachments/20110206/c832f7f2/attachment-0001.htm
>>
>> ------------------------------
>>
>> _______________________________________________
>> f2py-users mailing list
>> f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]
>> http://cens.ioc.ee/mailman/listinfo/f2py-users
>>
>>
>> End of f2py-users Digest, Vol 72, Issue 1
>> *****************************************
>>
>>
>
> _______________________________________________
> f2py-users mailing list
> f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]
> http://cens.ioc.ee/mailman/listinfo/f2py-users
>
>

_______________________________________________
f2py-users mailing list
f2py-users-Y4l6ocDipWCuvFJfX82//[email protected]
http://cens.ioc.ee/mailman/listinfo/f2py-users