簡體   English   中英

f2py中的malloc錯誤

[英]malloc error in f2py

我試圖使用f2py在3維中運行一個簡單的集成問題。

調用 fortran代碼的python代碼如下:

#!/Library/Frameworks/EPD64.framework/Versions/Current/bin/python
import pymods as modules
import pygauleg as gauleg
import pyint as integrator
import pylab as pl
import sys
import math
import time

############################################
# main routine #############################
############################################
zero = 0.0
one = 1.0
pi = pl.pi

Nr = 10
Nt = 10
Np = 2*Nt
r0 = zero
rf = one
NNang = Nr*Nt*Np

print 'Nr Nt Np = ', Nr, Nt, Np
print 'NNang = ', NNang
print 'r0 rf = ', r0, rf

Nx = int(math.floor( (one*NNang)**(one/3.0) ))
Ny = int(math.floor( (one*NNang)**(one/3.0) ))
Nz = int(math.floor( (one*NNang)**(one/3.0) ))

Nx = int(pl.floor(float(Nx)*1.75))
Ny = int(pl.floor(float(Ny)*1.75))
Nz = int(pl.floor(float(Nz)*1.75))

NNxyz = Nx*Ny*Nz


print 'Nx Ny Nz = ', Nx, Ny, Nz
print 'NNxyz = ', NNxyz
xyz0 = -rf
xyzf = rf

t1 = time.time()
xt = pl.zeros(Nt)
wt = pl.zeros(Nt)
gauleg.gauleg(xt, wt, 0.0, pl.pi, Nt)
print 'outside of gauleg'

雖然fortran子程序有點冗長,但它的重要部分才是開始......

  2 subroutine gauleg(x,w,x1,x2,n)
  3 !Input:   x1,x2,n
  4 !Output:  x,w
  5 !implicit none
  6 !integer, parameter :: ikind = selected_int_kind(25)
  7 !integer, parameter :: rkind = selected_real_kind(15, 307)
  8 !
  9 !real(kind = rkind), parameter :: pi = 3.14159265358979323846d00
 10 !real(kind = rkind), parameter :: one = 1.0d00
 11 !real(kind = rkind), parameter :: zero = 0.0d00
 12 use mod_gvars
 13 
 14 real(kind = rkind) :: tol = 1d-15
 15 
 17 integer :: n
 18 !!!!!f2py intent(in) n
 19 real(kind = rkind), dimension(n) :: x
 20 real(kind = rkind), dimension(n) :: w
 22 real :: x1, x2
 23 
 24 real(kind = rkind) :: z1, z, xm, xl, pp, p3, p2, p1;
 25 
 26 integer(kind = ikind) :: m
 27 integer(kind = ikind) :: i,j
 28 integer(kind = ikind) :: countmax, counter, max_counter, min_counter
 29 
 30 integer(kind = ikind) :: tenth, hundredth, thousandth
 31 
 32 print*, 'n = ', n

結束......

 98 
 99 print*, 'returning'
100 
101 end subroutine

子程序頂部的注釋(第5-11行)是fortran模塊mod_gvars存在的結構。 似乎一切都按照計划進行* 直到 *這個子程序返回。 這是輸出:

Nr Nt Np =  10 10 20
NNang =  2000
r0 rf =  0.0 1.0
Nx Ny Nz =  21 21 21
NNxyz =  1728
 n =           10
 m =  5
 returning
python(14167) malloc: *** error for object 0x1081f77a8: incorrect checksum for freed object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug
Abort trap

看起來子程序只有在返回時才會遇到問題。 為什么會這樣?

這種問題通常發生在Python中的引用計數錯誤中,例如,如果f2py中存在錯誤,或者如果您在Fortran中覆蓋的內存多於在numpy數組中分配的內存,則可能會發生此錯誤。 所有這些錯誤僅在Fortran子例程退出后顯示,在隨機點,通常是在Python中釋放一些內存時。

要調試這個,嘗試打印你進入Fortran的所有數組,也就是打印數組x,w,以確保你可以訪問那里的所有內存(測試f2py使用相同的類型等等) 。

確保在Fortran中使用邊界檢查(至少-fbounds-check in gfortran,最好只檢查-fcheck=all以檢查所有問題)。

您也可以在調試器或valgrind下運行它,它可能會告訴您問題所在。

最后,我個人更喜歡使用Cython直接包裝Fortran。 然后我可以輕松訪問所有生成的文件,並使用iso_c_binding Fortran模塊,以便Fortran編譯器檢查所​​有類型在Fortran和C(Python)之間是否兼容,請參見此處的示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM