gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:34:02 24 4
gpt4 key购买 nike

我在 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 指针的情况下使其工作?无法更改派生数据类型。希望您能理解我的问题并感谢您的帮助。

最佳答案

您可以移动分配。如果 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)

由于分配已移出子例程 foo 内的虚拟参数 eleminput,因此当该子例程运行时,实际参数 fred 将被取消分配返回。

关于pointers - Fortran:Cray 指针和派生数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29220822/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com