在Fortran中分配不同形状的Allocatables [英] Assignment of Allocatables of Different Shapes in Fortran
问题描述
程序测试
隐式无
integer,allocatable :: v1(:, :)
integer,allocatable :: v2(:, :)
allocate(v1(2,4))
allocate(v2 (2,3))
v1(:,, :) = reshape([11,12,13,14,15,16,17,18],[2,4])
v2(: ,= =重塑([21,22,23,24,25,26],[2,3])
print *,v1
print *,'shape(v1) :',shape(v1)
print *
print *,v2
print *,'shape(v2):',shape(v2)
print *
v2 = v1
print *,v1
print *,'shape(v1):',shape(v1)
print *
print * ,v2
print *,'shape(v2):',shape(v2)
print *
deallocate(v1)
deallocate(v2)
结束程序测试
当我使用 gfortran 进行编译时,以下输出:
11 12 13 14 15 16 17 18
形状(v1):2 4
21 22 23 24 25 26
形状(v2):2 3
11 12 13 14 15 16 17 18
shape(v1):2 4
11 12 13 14 15 16 17 18
shape(v2):2 4
当我使用 ifort 对其进行编译时,我得到以下输出:
11 12 13 14 15 16 17 18
shape(v1):2 4
21 22 23 24 25 26
形状(v2):2 3
11 12 13 14 15 16 17 18
形状(v1):2 4
11 12 13 14 15 16
沙pe(v2):2 3
哪一个可靠?是否存在 ifort 或 gfortran 中的错误?
gfortran版本4.8.1
ifort version 14.0.0
默认情况下,版本前的 ifort
17不使用Fortran 2003语义重新分配赋值左侧的可分配类型。 ifort 15手册有这种说法(对于默认的 norealloc-lhs
假设):
编译器在解释赋值语句时使用标准Fortran规则。假定左侧分配有正确的形状以保持右侧。如果不是,则会出现不正确的行为。
为了让赋值的左边重新分配到合适的形状,编译选项 -assume realloc-lhs
。或者,您可以使用 -standard-semantics
进行编译,以使所有假设默认符合Fortran 2003标准,并具有Fortran 2008的某些功能。
Please look at the following code:
program test
implicit none
integer, allocatable :: v1(:, :)
integer, allocatable :: v2(:, :)
allocate(v1(2, 4))
allocate(v2(2, 3))
v1(:, :) = reshape([11, 12, 13, 14, 15, 16, 17, 18], [2, 4])
v2(:, :) = reshape([21, 22, 23, 24, 25, 26], [2, 3])
print *, v1
print *, 'shape(v1): ', shape(v1)
print *
print *, v2
print *, 'shape(v2): ', shape(v2)
print *
v2 = v1
print *, v1
print *, 'shape(v1): ', shape(v1)
print *
print *, v2
print *, 'shape(v2): ', shape(v2)
print *
deallocate(v1)
deallocate(v2)
end program test
When I compile it with gfortran, I get the following output:
11 12 13 14 15 16 17 18
shape(v1): 2 4
21 22 23 24 25 26
shape(v2): 2 3
11 12 13 14 15 16 17 18
shape(v1): 2 4
11 12 13 14 15 16 17 18
shape(v2): 2 4
When I compile it with ifort, I get the following output:
11 12 13 14 15 16 17 18
shape(v1): 2 4
21 22 23 24 25 26
shape(v2): 2 3
11 12 13 14 15 16 17 18
shape(v1): 2 4
11 12 13 14 15 16
shape(v2): 2 3
which one is reliable? is there a bug in ifort or in gfortran?
gfortran version 4.8.1
ifort version 14.0.0
By default, ifort
before version 17 does not use Fortran 2003 semantics for reallocating an allocatable type on the left side of an assignment. The ifort 15 manual has this to say (for the default norealloc-lhs
assumption):
The compiler uses Standard Fortran rules when interpreting assignment statements. The left-hand side is assumed to be allocated with the correct shape to hold the right-hand side. If it is not, incorrect behavior will occur.
To allow the left side of the assignment to be reallocated to the proper shape, compile with the option -assume realloc-lhs
. Alternatively you can compile with -standard-semantics
to make all assumptions default to compliance with the Fortran 2003 standard, with some Fortran 2008 features.
这篇关于在Fortran中分配不同形状的Allocatables的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!