[SciPy-User] f2py segfault
Kurt Smith
kwmsmith at gmail.com
Sun Jan 17 16:43:44 EST 2010
On Sun, Jan 17, 2010 at 3:28 PM, Dag Sverre Seljebotn
<dagss at student.matnat.uio.no> wrote:
> Juan wrote:
>> Hi, I don't know if this is the right place (if it is not, please point me in
>> the right direction).
>> I am using f2py with some own programs and I am going insane with a segmentation
>> fault. It is probably a problem in my code but I'd like to know if someone has
>> any hint
>> to give me since I've been trying different things for two days already.
>>
>> I've got a few routines in fortran with in/out arrays. When I call one of the
>> routines it works well. The second routine I call crashes the program. I've been
>> changing routines and it seems that it does not matter with routines I use.
>>
>> Basically, the fortran routines have the signature:
>>
>> subroutine sub1(y, Np, Nt)
>> integer(4), intent(IN) :: Np
>> integer(4), intent(IN) :: Nt
>> real(8), intent(INOUT), dimension(6*Np, Nt) :: y
>>
>> and I call them from python as:
>>
>> import mymod
>> r= np.zeros((Ncoord,Ntraj),dtype=np.float64, order='Fortran')
>> mymod.sub1(r)
>>
>> I am using python 2.6. Probably the statement of the problem is to vague to get
>> an answer. But I'll settle for just some ideas on how to proceed. I've used the
>> option for debugging: --debug-capi
>> but it does not provide with more information. Only tells me that it checks for
>> the array and segfaults (before analyzing the integer arguments Np, Nt)
> From what little I know of f2py the "6*Np" seems like the problematic
> part. If f2py isn't smart enough to take the array shape and divide by 6
> (which, in general, requires solving a symbolic equation, and somehow I
> doubt f2py is that smart, though perhaps it deals with simple things
> like this explicitly? *shrug*), then Np is going to passed as a too big
> number (try to print out Np from your Fortran program to confirm...).
That's what I suspected at first, too. f2py tries to handle this,
although it uses integer division, and I think leads to a bug. I
don't get a segfault, though, so your problem might be something else.
If you can redo things to get rid of the '6*Np' it might be worth
trying.
ksmith at lothario:~/test-f2py$ cat foo.f90
subroutine sub1(y, Np, Nt)
integer(4), intent(in) :: Np
integer(4), intent(in) :: Nt
real(8), intent(inout), dimension(6*Np, Nt) :: y
y = 1.0
end subroutine sub1
ksmith at lothario:~/test-f2py$ f2py --debug-capi -c foo.f90
[f2py output]
ksmith at lothario:~/test-f2py$ cat test_foo.py
import numpy as np
import untitled
print untitled.sub1.__doc__
# this works -- note the array extents -- multiples of 6, so the
integer division works...
r = np.zeros((6, 6), dtype=np.float64, order="Fortran")
untitled.sub1(r)
print r
assert np.all(r == 1.0)
# this doesn't work -- note the array extents.
r = np.zeros((5, 5), dtype=np.float64, order="Fortran")
untitled.sub1(r)
print r
assert np.all(r == 1.0)
ksmith at lothario:~/test-f2py$ python test_foo.py
sub1 - Function signature:
sub1(y,[np,nt])
Required arguments:
y : in/output rank-2 array('d') with bounds (6 * np,nt)
Optional arguments:
np := (shape(y,0))/(6) input int
nt := shape(y,1) input int
debug-capi:Python C/API function
untitled.sub1(y,np=(shape(y,0))/(6),nt=shape(y,1))
debug-capi:double y=:inoutput,required,array,dims(6 * np|6 * np,nt|nt)
debug-capi:int np=(shape(y,0))/(6):input,optional,scalar
debug-capi:np=1
debug-capi:Checking `(shape(y,0))/(6)==np'
debug-capi:int nt=shape(y,1):input,optional,scalar
debug-capi:nt=6
debug-capi:Checking `shape(y,1)==nt'
debug-capi:Fortran subroutine `sub1(y,&np,&nt)'
debug-capi:np=1
debug-capi:nt=6
debug-capi:Building return value.
debug-capi:Python C/API function untitled.sub1: successful.
debug-capi:Freeing memory.
[[ 1. 1. 1. 1. 1. 1.]
[ 1. 1. 1. 1. 1. 1.]
[ 1. 1. 1. 1. 1. 1.]
[ 1. 1. 1. 1. 1. 1.]
[ 1. 1. 1. 1. 1. 1.]
[ 1. 1. 1. 1. 1. 1.]]
debug-capi:Python C/API function
untitled.sub1(y,np=(shape(y,0))/(6),nt=shape(y,1))
debug-capi:double y=:inoutput,required,array,dims(6 * np|6 * np,nt|nt)
debug-capi:int np=(shape(y,0))/(6):input,optional,scalar
debug-capi:np=0
debug-capi:Checking `(shape(y,0))/(6)==np'
debug-capi:int nt=shape(y,1):input,optional,scalar
debug-capi:nt=5
debug-capi:Checking `shape(y,1)==nt'
debug-capi:Fortran subroutine `sub1(y,&np,&nt)'
debug-capi:np=0
debug-capi:nt=5
debug-capi:Building return value.
debug-capi:Python C/API function untitled.sub1: successful.
debug-capi:Freeing memory.
[[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0.]]
Traceback (most recent call last):
File "test_foo.py", line 16, in <module>
assert np.all(r == 1.0)
AssertionError
More information about the SciPy-User
mailing list