本文介绍了如何在fortran 2003-2008中实现一个链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在fortran 2003/2008中实现我的分子动力学代码的链接列表数据结构我正在使用最新的fortran编译器(Intel)。



我以最好的方式来实现链接列表,如果可能的话,我希望在Fortran中可以使用无锁等待实现。



谢谢。

解决方案

如果您创建一个用户定义的类型,数据项和指向下一个项的指针是最简单的。这是假设一个单链表。例如,

 键入MyList_type 
integer :: FirstItem
real :: SecondItem
etc
类型(MyList_type),pointer :: next_ptr => null()
end type MyList_type

然后使用allocate创建第一个成员。此后,您编写代码遍历列表,使用next_ptr来遍历列表。使用关联内在函数来测试next_ptr是否已定义,或者您已经到达列表的末尾。



如果您正在编写一个普通的顺序Fortran程序那么无锁/不等待不是一个问题。如果您正在编写多线程/并行程序,那么对变量的一致访问是一个问题。



以下是一些更多示例:。
更好的是,Fortran中的链表在Metcalf和Reid的Fortran 90/95解释一书中有明确的解释。


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).

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.

Thank you.

解决方案

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

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.

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.

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中实现一个链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 09:48