Cpython和fortran链表之间的互操作性
我有一个fortran的链表,大致类似Cpython和fortran链表之间的互操作性,python,fortran,fortran-iso-c-binding,Python,Fortran,Fortran Iso C Binding,我有一个fortran的链表,大致类似 type :: node type(node), pointer :: next => null() integer :: value end type node 理想情况下,我希望使用Cpython与此进行交互。我在python中使用了很多fortran子例程,使用f2py程序创建共享对象。但是,f2py不能与派生类型一起使用 我的问题很简单,是否有可能使用cpython在Fortran中访问类似链表的内容。我想我需要遵循fort
type :: node
type(node), pointer :: next => null()
integer :: value
end type node
理想情况下,我希望使用Cpython与此进行交互。我在python中使用了很多fortran子例程,使用f2py程序创建共享对象。但是,f2py不能与派生类型一起使用
我的问题很简单,是否有可能使用cpython在Fortran中访问类似链表的内容。我想我需要遵循fortran到c到cpython的路线。然而,我已经读到,为了使fortran派生的类型能够与c进行互操作,“每个组件必须具有可互操作的类型和类型参数,不能是指针,也不能是可分配的。”同样,这篇文章似乎证实了这一点
我想知道是否有人知道从cpython访问fortran中的链表是绝对不可能的。如果可能的话,即使是间接地或间接地,我也希望听到更多
谢谢,,
Mark正如Bálint Aradi在评论中已经提到的,该节点不能以当前的形式与C进行互操作。为此,您需要将fortran指针更改为C指针,但这使得在fortran内部使用非常痛苦。我能想到的最优雅的解决方案是将C可互操作类型放在fortran类型中,并保存C和fortran指针的不同版本 实现如下所示,其中我还定义了便利函数,用于在fortran中分配、取消分配和初始化节点
module node_mod
use, intrinsic :: iso_c_binding
implicit none
! the C interoperable type
type, bind(c) :: cnode
type(c_ptr) :: self = c_null_ptr
type(c_ptr) :: next = c_null_ptr
integer(c_int) :: value
end type cnode
! the type used for work in fortran
type :: fnode
type(cnode) :: c
type(fnode), pointer :: next => null()
end type fnode
contains
recursive function allocate_nodes(n, v) result(node)
integer, intent(in) :: n
integer, optional, intent(in) :: v
type(fnode), pointer :: node
integer :: val
allocate(node)
if (present(v)) then
val = v
else
val = 1
end if
node%c%value = val
if (n > 1) then
node%next => allocate_nodes(n-1, val+1)
end if
end function allocate_nodes
recursive subroutine deallocate_nodes(node)
type(fnode), pointer, intent(inout) :: node
if (associated(node%next)) then
call deallocate_nodes(node%next)
end if
deallocate(node)
end subroutine deallocate_nodes
end module node_mod
如您所见,访问“value”元素需要额外的“%c”,这有点麻烦。要使用python中先前定义的例程检索链表,必须定义C可互操作包装器,并且必须链接C指针
module node_mod_cinter
use, intrinsic :: iso_c_binding
use, non_intrinsic :: node_mod
implicit none
contains
recursive subroutine set_cptr(node)
type(fnode), pointer, intent(in) :: node
node%c%self = c_loc(node)
if (associated(node%next)) then
node%c%next = c_loc(node%next%c)
call set_cptr(node%next)
end if
end subroutine set_cptr
function allocate_nodes_citer(n) bind(c, name="allocate_nodes") result(cptr)
integer(c_int), value, intent(in) :: n
type(c_ptr) :: cptr
type(fnode), pointer :: node
node => allocate_nodes(n)
call set_cptr(node)
cptr = c_loc(node%c)
end function allocate_nodes_citer
subroutine deallocate_nodes_citer(cptr) bind(c, name="deallocate_nodes")
type(c_ptr), value, intent(in) :: cptr
type(cnode), pointer :: subnode
type(fnode), pointer :: node
call c_f_pointer(cptr, subnode)
call c_f_pointer(subnode%self, node)
call deallocate_nodes(node)
end subroutine deallocate_nodes_citer
end module node_mod_cinter
“*_nodes_citer”例程只处理不同的指针类型,set_cptr子例程根据fortran指针链接C可互操作类型中的C指针。我添加了节点%c%self元素,以便恢复fortran指针并用于适当的释放,但是如果您不太关心这一点,那么严格来说就不需要了
此代码需要编译为共享库,以便其他程序使用。在我的linux机器上,我对gfortran使用了以下命令
gfortran -fPIC -shared -o libnode.so node.f90
最后,python代码将分配一个包含10个节点的列表,打印出每个节点的%c%值,然后再次取消分配所有节点。此外,还显示了fortran和C节点的内存位置
#!/usr/bin/env python
import ctypes
from ctypes import POINTER, c_int, c_void_p
class Node(ctypes.Structure):
pass
Node._fields_ = (
("self", c_void_p),
("next", POINTER(Node)),
("value", c_int),
)
def define_function(res, args, paramflags, name, lib):
prot = ctypes.CFUNCTYPE(res, *args)
return prot((name, lib), paramflags)
def main():
import os.path
libpath = os.path.abspath("libnode.so")
lib = ctypes.cdll.LoadLibrary(libpath)
allocate_nodes = define_function(
res=POINTER(Node),
args=(
c_int,
),
paramflags=(
(1, "n"),
),
name="allocate_nodes",
lib=lib,
)
deallocate_nodes = define_function(
res=None,
args=(
POINTER(Node),
),
paramflags=(
(1, "cptr"),
),
name="deallocate_nodes",
lib=lib,
)
node_ptr = allocate_nodes(10)
n = node_ptr[0]
print "value", "f_ptr", "c_ptr"
while True:
print n.value, n.self, ctypes.addressof(n)
if n.next:
n = n.next[0]
else:
break
deallocate_nodes(node_ptr)
if __name__ == "__main__":
main()
执行此操作将获得以下输出:
value f_ptr c_ptr
1 15356144 15356144
2 15220144 15220144
3 15320384 15320384
4 14700384 14700384
5 15661152 15661152
6 15661200 15661200
7 15661248 15661248
8 14886672 14886672
9 14886720 14886720
10 14886768 14886768
值得注意的是,这两种节点类型都从相同的内存位置开始,因此实际上并不需要节点%c%self,但这只是因为我对类型定义很小心,所以不应该将其作为基础
就在这里。即使不必处理链表,这也相当麻烦,但是ctypes比f2py强大得多。希望这能带来一些好处。我不确定fortran指针的实现是否在编译器之间实现了标准化,所以我怀疑您是否能够做到这一点。如果你只需要一个链表,你可以把一个链表放在一个模块中,然后让一堆函数使用这个模块来访问它——这当然是非常有技巧的。还要注意的是,python几乎不需要链表。。。您总是可以将python列表转换为numpy数组,并将其传递给其他人(但我知道使用遗留代码可能会使这一点变得困难)…这并不是C语言的互操作性。您可以使用
type(cptr)
作为指向下一个结构的指针,但是您必须在Fortran内部通过c\u-f\u-pointer()
转换此指针才能使用(如果需要,还可以使用f\u-c\u-pointer()
返回),因此,这是一种痛苦……到目前为止,最简单的方法可能是基于链表创建一个数组并将其传递给python。但这将是一个相当沉重的行动。从C直接访问现有列表是可能的,但不是以可移植的方式(您需要对fortran指针的表示方式以及派生类型在内存中的布局方式进行反向工程。然后希望它不会在下一个编译器版本中发生更改)(感谢mgilson、Baliant Aradi和amaurea),我将继续寻求解决这个问题的方法。如果我发现一些结论,我会发回。哇!这是一个很好的答案,我很惭愧我在今天(差不多一个月后)之前没有注意到这个帖子。我刚刚假设这项任务几乎不可能完成。谢谢你的全面回答。我已经在linux上运行了代码,它可以直接运行。尽管在cpython中使用fortran链表必须是间接的,但这是值得为我想要的付出的代价。我将在祝你将来愉快。非常感谢。