[Numpy-discussion] f2py call-back and newbie questions
Tue Nov 11 19:37:19 CST 2008
I apologize for this long listing, but I have a lot of pent-up f2py queries
occupying my mind [?]
I realize that f2py is a non-commercial service to the community, and am
highly impressed that it exists at all. I can only offer in return my own
assistance to others (say via this list), after I have crossed some of these
bridges and can in turn assist others who venture in.
I am new to this list, as well as Python and am trying to use the
f2py call-back feature . I am embarking upon a rather large Fortran->Python
(80,000 line code) conversion for a dynamics simulation system (
http://home.comcast.net/~GTOSS/), and will be willing to share all knowledge
of how this goes. I am converting code by evolving from 100% F90 -> xxx %
Python starting at the
"results-data-base/post-processing/interpretive-manipulation" side of the
So far, I can successfully create a wrap that allows me to call a Fortran
program from Python, but can't successfully do the reverse call-back scheme.
I think I have located just about every documentation available, including
numerous versions of some, but I have questions that I can't seem to resolve
through this documentation. At the end of this request is shown the actual
code (it is very simple).
Ubuntu 8.0.4; GNU C++/g95 Compiler (current release)
Python 2.5.2; Numpy 1.0.4; f2py 2.4422
The symptom is: Depending upon how I invoke f2py to create the wrap, one of
2 failures occur:
1. At Python execution, Python insists that my arguments to the Fortran
subroutine (CBSET) are incorrect (I use 2, it insists on 1), OR,
2. Python accepts my 2 arguments to the Fortran subroutine, and when I
execute Python, I end up getting a segmentation fault when I attempt to make
the call-back from Fortran. This would normally tell me that likely I have
some kind of argument issue (although the call-back routine defn itself has
NO arguments...although the "external" attribute routine name does appear as
an argument in the subroutine at the INSISTENCE of the g95 compiler).
General questions (maybe related to the problem, maybe not)....
1. I never get a "makefile" created by running f2py. Why?
2. I never get a module document (ie. "__xxx__") created by running f2py
(although I do get the ".pyf" sig-file). Why?
3. Is the Python "call-back" program code supposed to reside in the Python
program that originally makes the Python -> Fortran wrap-connection (I
assumed it is)?
4. Does the "XXXX.so" file need to be linked into the Fortran code via a
"makefile" operation to successfully "call-back"?
5. Where can I find some simple, but illustrative examples of using f2py to
"wrap a Fortran 90 data Class-definition"? I need to understand how the f2py
calls would look, and what is manifest with in the Python code to actually
lay hands on the elements of the Fortran "derived data types".
In general, it would be nice to find a detailed example of: a python
program, a corresponding consistent Fortran program that combines to
demonstrate the call-back scheme (and that one can type in, and will
actually function correctly), and the preferred (ie. correct?) f2py
statements that need to be executed to accomplish this (I would be glad to
contribute same to the body of knowledge, if I can ever get it to work).
I will be most grateful for comments on any or all of this.
The Fortran Code:
! PROGAM TO DEMONSTRATE GTOSS FORTRAN -> PYTHON CONNECTION
SUBROUTINE CBSET( N, call_back )
!f2py intent(in) N
!f2py intent(callback) call_back
! IDENTIFY THAT FORTRAN IS EXECUTING & IDENTIFY WHY CBSET IS CALLED
PRINT *,'Enter CBSET.F, N=', N
IF(N .EQ. 1) THEN
PRINT *,' in CBSET: this Call originated to test call-back'
PRINT *,' in CBSET: Make callback to PYTHON...EXPECT reply'
PRINT *,' in CBSET: Returned from call-back to PYTHON'
IF(N .EQ. 2) THEN
PRINT *,' in CBSET: Call came from Python to test Py->For'
*The Python Program:*
print 'Hello, pymain is about to call CBSET to test f2py operation'
#satisfy arg list for cbset ?????
#test "Python --> Fortran" (nominal wrap) from Python
n = 2
#test "Fortran --> Python" (call-back) scheme as seen from Python
n = 1
# define call-back routine
print '...Confirming...."call_back"is entered from CBSET call'
# end of call-back defn
if __name__ =='__main__':
NOTE: depending upon how f2py is invoked to create the wrap, a disagreement
can be experienced between Python and Fortran as to "how many args the F90
program 'CBSET' really exhibits"; in one case, Python aborts at execution,
insisting that "CBSET has exactly 1 argument", The other type event is that
Python is happy with the code as shown, but Fortran gets a runtime
"Segmentation fault" when the "call_back" is executed. BTW, at compile,
Fortran is NEVER happy UNLESS the EXTERNAL routine named "call_back"
actually appears as an arg in the CBSET subroutine definition.
-------------- next part --------------
An HTML attachment was scrubbed...
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Size: 226 bytes
Desc: not available
Url : http://projects.scipy.org/pipermail/numpy-discussion/attachments/20081111/bdcffa91/attachment.gif
More information about the Numpy-discussion