f2py 将 C 语言编译成 so 文件后运行对输入是不是很敏感呢?

2020-01-04 15:54:46 +08:00
 douglas1997

流程是这样的:

  1. 先写了一个 C 语言函数,接受的都是指针形式的输入( 2 个 int 类型的指针)
  2. 然后写了一个 f2py 的脚本,将 C 语言函数编译成 python 能 import 的库
  3. 在 python 中运行这个库,在少量数据的情况下( 10x512x512 int )的情况下正常,在大量输入的情况下 segmentation fault ( 100x512x512 )

这是因为 f2py 打包的问题还是因为 C 代码本身的问题呢,静态语言检查要爆炸了。。= =

将 C 语言运行前几行直接 return 0,发现还是会 segmentation fault,不知道是为啥了,f2py 脚本如下

python module region_growing
interface
    subroutine grow(m, n, p, img, seg, si, sj, sk, t)
        intent(c) grow
        intent(c)

        integer intent(hide), depend(img) :: m = shape(img,0)
        integer intent(hide), depend(img) :: n = shape(img,1)
        integer intent(hide), depend(img) :: p = shape(img,2)

        integer intent(in)                           :: img(m,n,p)
        integer(kind=1) intent(in)              :: seg(m,n,p)
        integer intent(in)                           :: si, sj, sk, t
    end subroutine grow
end interface
end python module region_growing
898 次点击
所在节点    问与答
2 条回复
douglas1997
2020-01-04 15:55:38 +08:00
但是其实输入本质上是差不多的,我个人感觉更多的可能是 C 语言的 bug,而不是 f2py 的问题。
douglas1997
2020-01-04 16:27:59 +08:00
解决了,是因为栈空间不足导致的。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/635040

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX