繁体   English   中英

Fortran:Cray指针和派生数据类型

[英]Fortran: Cray Pointers and Derived Data Types

我在Fortran中有一个派生数据类型,看起来像这样:

TYPE mytype
        INTEGER a
        DOUBLE COMPLEX, dimension(:), allocatable :: elem
END TYPE mytype

现在,我想编写一个函数,该函数将DOUBLE COMPLEX数组作为参数。 该数组应成为“ mytype”的数组“ elem”,而无需分配内存和复制数据。 我试图通过以下方式使用cray指针:

DOUBLE COMPLEX, INTENT(IN) :: eleminput(5) !input array which should become x%elem
TYPE(mytype) :: x
pointer(xpntr,x%elem)
xpntr = LOC(eleminput)

当我尝试编译时,出现错误,提示它在行“ pointer(xpntr,x%elem)”中使用“)”而不是“%”。 因此,似乎cray指针不适用于派生数据类型的元素。 是否可以使用cray指针,也可以不使用cray指针? 派生的数据类型不能更改。 希望您能理解我的问题,并感谢您的帮助。

您可以移动分配。 如果eleminput参数是可分配的:

integer, parameter :: dp = kind(1.0d0)
type mytype
  integer :: a
  complex(kind=dp), dimension(:), allocatable :: elem
end type

...    

subroutine foo(eleminput)
  complex(kind=dp), intent(inout), allocatable :: eleminput(:)
  type(mytype) :: x
  call move_alloc(eleminput, x%elem)
  !...  work with x
end subroutine foo

与可分配的伪参数关联的实际参数本身必须是可分配的-也就是说,对foo的调用必须类似于:

complex(kind=dp), allocatable :: fred(:)
fred = [(0,0),(0,1),(1,1),(1,0)]
call foo(fred)

由于分配eleminput子例程foo中的伪参数eleminput ,因此当该子例程返回时,实际的参数fred将不会分配。

Cray指针不适用于可分配的指针。 我强烈建议不要在新代码中使用Cray指针,因为Cray指针在实现细节上有细微的差别,并且普通的Fortran指针在这里工作:

module bar
  integer, parameter :: dp = selected_real_kind(15)
  TYPE mytype
     INTEGER:: a
     complex(kind=dp), dimension(:), allocatable :: elem
  END TYPE mytype

contains

  subroutine foo(eleminput)
    complex(kind=dp), dimension(:), intent(out), pointer :: eleminput
    type(mytype), target, save : : x
    allocate (x%elem(.....))
    eleminput => x%elem
  end subroutine foo
end module bar

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM