- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个大致类似的 Fortran 链表
type :: node
type(node), pointer :: next => null()
integer :: value
end type node
理想情况下,我希望使用 Cpython 与之交互。我使用 f2py 程序创建共享对象时,经常将 fortran 子例程与 python 结合使用。但是,f2py 不能与派生类型一起使用。
我的问题很简单,是否可以使用 cpython 访问 Fortran 中的链表之类的东西。我想我需要遵循从 fortran 到 c 到 cpython 的路线。但是,我读过要使 fortran 派生类型与 c 互操作,“每个组件都必须具有可互操作的类型和类型参数,不能是指针,也不能是可分配的。”同样,帖子 c-fortran interoperability - derived types with pointers似乎证实了这一点。
我想知道是否有人知道是否绝对不可能从 cpython 访问 fortran 中的链表。如果可能的话,即使是间接或迂回的方式,我也很乐意听到更多信息。
谢谢你标记
最佳答案
正如 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
如您所见,访问“值”元素需要一个额外的“%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 指针。我已经添加了 node%c%self 元素,以便可以恢复 fortran 指针并将其用于正确的重新分配,但如果您不太关心它,那么它并不是绝对需要的。
这段代码需要编译成共享库才能被其他程序使用。我在我的 linux 机器上对 gfortran 使用了以下命令。
gfortran -fPIC -shared -o libnode.so node.f90
最后,分配 10 个节点的列表的 python 代码,打印出每个节点的 node%c%value,然后再次释放所有内容。此外,还显示了 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
有趣的是,两种节点类型都从相同的内存位置开始,所以 node%c%self 并不是真正需要的,但这只是因为我对类型定义很小心,这真的不应该指望。
就是这样。即使不必处理链表也很麻烦,但 ctypes 比 f2py 更强大、更健壮。希望这会带来一些好处。
关于python - Cpython 和 fortran 链表之间的互操作性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14595406/
我需要修复 getLineNumberFor 方法,以便如果 lastName 的第一个字符位于 A 和 M 之间,则返回 1;如果它位于 N 和 Z 之间,则返回 2。 在我看来听起来很简单,但我不
您好,感谢您的帮助!我有这个: 0 我必须在每次点击后增加“pinli
Javascript 中是否有一种方法可以在不使用 if 语句的情况下通过 switch case 结构将一个整数与另一个整数进行比较? 例如。 switch(integer) { case
我有一列是“日期”类型的。如何在自定义选项中使用“之间”选项? 最佳答案 请注意,您有2个盒子。 between(在SQL中)包含所有内容,因此将框1设置为:DATE >= startdate,将框2
我有一个表,其中包含年、月和一些数字列 Year Month Total 2011 10 100 2011 11 150 2011 12 100 20
这个问题已经有答案了: Extract a substring between double quotes with regular expression in Java (2 个回答) how to
我有一个带有类别的边栏。正如你在这里看到的:http://kees.een-site-bouwen.nl/ url 中类别的 ID。带有 uri 段(3)当您单击其中一个类别时,例如网页设计。显示了一
这个问题在这里已经有了答案: My regex is matching too much. How do I make it stop? [duplicate] (5 个答案) 关闭 4 年前。 我
我很不会写正则表达式。 我正在尝试获取括号“()”之间的值。像下面这样的东西...... $a = "POLYGON((1 1,2 2,3 3,1 1))"; preg_match_all("/\((
我必须添加一个叠加层 (ImageView),以便它稍微移动到包含布局的左边界的左侧。 执行此操作的最佳方法是什么? 尝试了一些简单的方法,比如将 ImageView 放在布局中并使用负边距 andr
Rx 中是否有一些扩展方法来完成下面的场景? 我有一个开始泵送的值(绿色圆圈)和其他停止泵送的值(簧片圆圈),蓝色圆圈应该是预期值,我不希望这个命令被取消并重新创建(即“TakeUntil”和“Ski
我有一个看起来像这样的数据框(Dataframe X): id number found 1 5225 NA 2 2222 NA 3 3121 NA 我有另一个看起来
所以,我正在尝试制作正则表达式,它将解析存储在对象中的所有全局函数声明,例如,像这样 const a = () => {} 我做了这样的事情: /(?:const|let|var)\s*([A-z0-
我正在尝试从 Intellivision 重新创建 Astro-Smash,我想让桶保持在两个 Angular 之间。我只是想不出在哪里以及如何让这个东西停留在两者之间。 我已经以各种方式交换了函数,
到处检查但找不到答案。 我有这个页面,我使用 INNER JOIN 将两个表连接在一起,获取它们的值并显示它们。我有这个表格,用来获取变量(例如开始日期、结束日期和卡号),这些变量将作为从表中调用值的
我陷入了两个不同的问题/错误之间,无法想出一个合适的解决方案。任何帮助将不胜感激 上下文、FFI 和调用大量 C 函数,并将 C 类型包装在 rust 结构中。 第一个问题是ICE: this pat
我在 MySQL 中有一个用户列表,在订阅时,时间戳是使用 CURRENT_TIMESTAMP 在数据库中设置的。 现在我想从此表中选择订阅日期介于第 X 天和第 Y 天之间的表我尝试了几个查询,但不
我的输入是开始日期和结束日期。我想检查它是在 12 月 1 日到 3 月 31 日之间。(年份可以更改,并且只有在此期间内或之外的日期)。 到目前为止,我还没有找到任何关于 Joda-time 的解决
我正在努力了解线程与 CPU 使用率的关系。有很多关于线程与多处理的讨论(一个很好的概述是 this answer )所以我决定通过在运行 Windows 10、Python 3.4 的 8 CPU
我正在尝试编写 PHP 代码来循环遍历数组以创建 HTML 表格。我一直在尝试做类似的事情: fetchAll(PDO::FETCH_ASSOC); ?>
我是一名优秀的程序员,十分优秀!