问题描述
我需要在 fortran 2003/2008 中为我的分子动力学代码实现链接列表数据结构,我使用的是最新的 fortran 编译器(英特尔).
I need to implement a link list data structure for my molecular dynamics code in fortran 2003/2008 I am using the newest fortran compilers (Intel).
我如何以最好的方式实现链表,如果可能,我更喜欢在 Fortran 中实现无锁无等待.
How do I come about implement the linked list in the best way possible I would prefer a lock-free no wait implementation if possible in Fortran.
谢谢.
推荐答案
如果您使用您的数据项和指向下一项的指针创建用户定义的类型是最简单的.这是假设一个单链表.例如,
It is easiest if you create a user defined type with your data items and the pointer to the next item. This is assuming a singly-linked list. e.g.,
type MyList_type
integer :: FirstItem
real :: SecondItem
etc
type (MyList_type), pointer :: next_ptr => null ()
end type MyList_type
然后用allocate"创建第一个成员.此后,您编写代码来遍历列表,使用 next_ptr 遍历列表.使用关联的"内部函数来测试 next_ptr 是否已定义,或者是否已到达列表末尾.
Then create the first member with "allocate". Thereafter you write code to traverse the list, using next_ptr to step through the list. Use the "associated" intrinsic function to test whether next_ptr is defined yet, or instead you have reached the end of the list.
如果您正在编写普通的顺序 Fortran 程序,那么无锁/无等待不是问题.如果您正在编写多线程/并行程序,那么对变量的一致访问是一个问题.
If you are writing an ordinary sequential Fortran program then lock-free/no-wait is not an issue. If you are writing a multi-threaded / parallel program, then consistent access to the variables is an issue.
以下是更多示例:http://fortranwiki.org/fortran/show/Linked+list.更好的是,Fortran 中的链表在 Metcalf 和 Reid 合着的Fortran 90/95 Explained"一书中有清楚的解释.
Here are some more examples: http://fortranwiki.org/fortran/show/Linked+list.Even better, linked lists in Fortran are clearly explained in the book "Fortran 90/95 Explained" by Metcalf and Reid.
这篇关于如何在 fortran 2003-2008 中实现链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!