Fortran:Cray 指针和派生数据类型
Fortran: Cray Pointers and Derived Data Types
我有一个 Fortran 派生数据类型,如下所示:
TYPE mytype
INTEGER a
DOUBLE COMPLEX, dimension(:), allocatable :: elem
END TYPE mytype
现在我想编写一个函数,它获取一个双精度复数数组作为参数。这个数组应该成为 "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 指针不适用于可分配项。我强烈建议不要在新代码中使用 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
您可以移动分配。如果 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
移出,所以当该子例程 returns 时,实际参数 fred
将被取消分配。
我有一个 Fortran 派生数据类型,如下所示:
TYPE mytype
INTEGER a
DOUBLE COMPLEX, dimension(:), allocatable :: elem
END TYPE mytype
现在我想编写一个函数,它获取一个双精度复数数组作为参数。这个数组应该成为 "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 指针不适用于可分配项。我强烈建议不要在新代码中使用 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
您可以移动分配。如果 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
移出,所以当该子例程 returns 时,实际参数 fred
将被取消分配。