Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Fortran 派生类型的多态组件的类型绑定过程_Fortran_Fortran2003 - Fatal编程技术网

Fortran 派生类型的多态组件的类型绑定过程

Fortran 派生类型的多态组件的类型绑定过程,fortran,fortran2003,Fortran,Fortran2003,我正在(几乎)从头开始编写一些模拟代码,并希望使用fortran的OOP功能使其更易于维护。我在fortran研讨会上了解到,在代码的性能关键部分使用OOP功能时,应该非常小心 我天真的第一种方法(忽略警告)是以下代码片段。背景是计算需要相互作用势的系统能量。对于这个势,我使用一个抽象类型(势)。然后,所有用户都可以添加自己的潜力作为扩展,并定义一个用于分配潜力的关键字。主程序中的能量计算不变 module system_mod implicit none type, abstract :

我正在(几乎)从头开始编写一些模拟代码,并希望使用fortran的OOP功能使其更易于维护。我在fortran研讨会上了解到,在代码的性能关键部分使用OOP功能时,应该非常小心

我天真的第一种方法(忽略警告)是以下代码片段。背景是计算需要相互作用势的系统能量。对于这个势,我使用一个抽象类型(势)。然后,所有用户都可以添加自己的潜力作为扩展,并定义一个用于分配潜力的关键字。主程序中的能量计算不变

module system_mod
implicit none

  type, abstract :: potential
  contains
    procedure(init), deferred :: init
    procedure(eval), deferred :: eval
  end type

  type, extends(potential) :: my_potential
    ! some parameters
  contains
    procedure :: init => init_my_potential 
    procedure :: eval => eval_my_potential
  end type 

  ! omitted: other extensions of 'potential'
  ! and abstract interface of potential

  type :: system
    ! some components
    class(potential), allocatable :: pair_potential
  contains
    procedure :: init ! subroutine, which is used to allocate pair_potential depending on a passed keyword
  end type

contains 

  ! implementation of all routines

end module system_mod

program main
use system_mod, only: potential, my_potential, system
implicit none

  character(len=3) :: keyword !             
  integer      :: i
  real         :: coordinates(n,3)       ! n is a huge number
  real         :: energy, system_energy
  type(system) :: test_system

  call test_system%init ( keyword ) 
  ! keyword tells which of the extensions of 'potential' is allocated

  do i = 1, n
    energy = test_system%pair_potential%eval ( ... )
    system_energy = system_energy + energy
  end do

end program
我认为我关于性能的主要问题是,我不知道编译器在编译时实际做了什么,也不知道在运行时做了什么

因此,我的问题是:

  • 编译器如何或何时检查“eval”的哪个实例
  • 当关键字在编译时已知时,循环的每个迭代中是否都有类型检查
  • 例如,最好在主程序中使用过程指针,并在开始时将其与相应的“eval”过程相关联
  • 提前非常感谢

  • 它有一系列过程,可以为所有扩展类型的绑定调用这些过程,并在运行时从此表中决定

  • 设置关键字可能没有帮助,这取决于编译器有多聪明。如果类型在编译时已知,我将使用
    type
    而不是
    class
    。这很可能会跳过vtable查找

  • 过程指针也会影响指令流,尽管您可能会保存vtable查找的某些部分。这实际上取决于内部结构,值得一试和一些性能度量