我想将一些BLAS函数包装到Python模块中。例如,为了包装函数sswap
,我创建了一个包含内容的.f95
文件
subroutine wrap_sswap(n, x, y, x_rows, y_rows)
implicit none
integer, intent(in) :: n
real, intent(inout) :: x(x_rows), y(y_rows)
integer :: x_rows, y_rows, incx = 1, incy = 1
external :: sswap
call sswap(n, x, incx, y, incy)
end subroutine wrap_sswap
然后我使用numpy.f2py
创建一个python模块my_module
,以便在python中调用my_module.wrap_sswap()
。这很有效。但是,我希望能够调用my_module.sswap()
,也就是说,我希望将子例程sswap
命名为与外部BLAS函数sswap
相同的名称。这是不可能的,并导致错误EXTERNAL attribute conflicts with SUBROUTINE attribute
我怎样才能实现我想要的
(仅用于记录可能的第二种方法:-)
虽然不是严格可移植的,但另一种方法可能是使用
bind(C,name="sswap_")
,假定C导出的名称为sswap_
(在libblas.a等中)。例如我们像往常一样使用f2py:
那么
多亏了roygvib,我们现在可以提供一个工作示例。它基于更改f2py签名文件的建议
假设上述问题中的代码保存在
sswap_ext.f95
中。第一步是生成签名文件:在我的例子中,生成的签名文件
sswap_ext.pyf
具有以下内容:现在的诀窍是用
sswap
替换每个wrap_sswap
,并添加fortranname wrap_sswap
,如下所示:现在我们可以编译:
然后,我们可以在Python中使用
sswap_module.sswap
:相关问题 更多 >
编程相关推荐