可分配字符分量的可分配向量的共阵列派生类型



请考虑以下代码,该代码尝试创建包含可变长度可分配字符类型数组的协数组派生类型。

program testCoarrayJaggedArray
implicit none
integer :: i
type                                :: CharVec_type
character(:), allocatable       :: record
end type
type                                :: List_type
type(CharVec_type), allocatable :: Item(:)
end type
type(List_type)                     :: List[*]
if (this_image()==1) then
allocate( List%Item(num_images()) )
do i = 1, num_images()
allocate( character(63) :: List%Item(i)%record )
write(List%Item(i)%record,*) i
List%Item(i)%record = "King" // trim(adjustl(List%Item(i)%record))
end do
sync images(*)
else
sync images(1)
allocate( List%Item( size(List[1]%Item(:)) ) )
do i = 1, size(List%Item(:))
allocate( character( len(List[1]%Item(i)%record) ) :: List%Item(i)%record )
List%Item(i)%record = List[1]%Item(i)%record
write(*,*) this_image(), List%Item(i)%record
end do
end if
end program testCoarrayJaggedArray

英特尔的ifort 2018在调试模式下,共享内存协同阵列,抱怨此代码的几个方面。这是第一个:

ifort /debug /Qcoarray=shared /standard-semantics /traceback /gen-interfaces /check /fpe:0 main.f90 -o run.exe
Intel(R) Visual Fortran Intel(R) 64 Compiler for applications running on Intel(R) 64, Version 18.0.2.185 Build 20180210
Copyright (C) 1985-2018 Intel Corporation.  All rights reserved.
main.f90(30): error #6457: This derived type name has not been declared.   [CHARACTER]
allocate( character( len(List[1]%Item(i)%record) ) :: List%Item(i)%record )
------------------^
main.f90(30): error #8235: If type specification appears, the type and kind type parameters of each object being allocated must be the same as type and kind type parameters of the type specification.   [RECORD]
allocate( character( len(List[1]%Item(i)%record) ) :: List%Item(i)%record )
---------------------------------------------------------------------------^
compilation aborted for main.f90 (code 1)

这段代码是非标准的Fortran吗?特别是使用第一个图像上相应字符的长度分配字符类型的行:

allocate( character( len(List[1]%Item(i)%record) ) :: List%Item(i)%record )

无论程序是否符合,第一条错误消息都是乱码:character不是派生类型的有效名称。 至少,这应该作为实施质量问题报告给英特尔支持部门。

现在,代码是否符合要求? 我不会回答这个问题,而是看一下以下程序:

integer :: i[*]
character(:), allocatable :: x
i=1
allocate(character(i[this_image()]) :: x)
end

当我使用 ifort 18.0.2 编译时

test.f90(5): error #6457: This derived type name has not been declared.   [CHARACTER]
allocate(character(i[this_image()]) :: x)
---------^
test.f90(5): error #8235: If type specification appears, the type and kind type parameters of each object being allocated must be the same as type and kind type parameters of the type specification.   [X]
allocate(character(i[this_image()]) :: x)
---------------------------------------^
compilation aborted for test.f90 (code 1)

让我们看看这个程序应该发生什么。

我们可能都同意,唯一有争议的行是分配语句,唯一棘手的部分是(再次(类型规范的长度类型参数。 那么,i[this_image()]这里的类型参数是有效的吗?

它是一个标量整数表达式,它是定义的。 我们只需要确定是否适用任何其他违反的限制。 Fortran 2008中没有。

编译器不应拒绝此程序。

作为问题程序的解决方法:创建要在分配语句中使用的长度表达式的非协数组临时副本。

最新更新