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