【发布时间】:2018-06-16 01:42:29
【问题描述】:
这是一个测试spread内在函数的简单代码:
program test_spread
implicit none
integer, allocatable, dimension(:) :: a
integer, allocatable, dimension(:,:) :: b
integer, allocatable, dimension(:,:,:) :: c
integer, allocatable, dimension(:,:,:) :: d
allocate(a(2), b(2,4), c(2,4,2), d(2,4,2))
a(:) = [1, 2]
print*, "a=", a
b(:,:) = spread((a + 1)**2, 2, size(b,2))
print*, "b=", b
c(:,:,:) = spread(b, 3, size(c,3))
print*, "c=", c
d(:,:,:) = spread(spread((a + 1)**2, 2, size(d,2)), 3, size(d,3))
print*, "d=", d
end program
使用 gfortran 8.1.1 编译:
gfortran -g -fcheck=all -Wall -Wtabs -fbacktrace -c test.f90
gfortran -g -fcheck=all -Wall -Wtabs -fbacktrace -o test_spread test.o
我得到以下结果:
a= 1 2
b= 4 9 4 9 4 9 4 9
c= 4 9 4 9 4 9 4 9 4 9 4 9 4 9 4 9
Fortran runtime error: rank mismatch in spread()
Error termination. Backtrace:
#0 0x55b46b14386e in test_spread
at /*****/test_spread/test.f90:20
#1 0x55b46b143966 in main
at /*****/test_spread/test.f90:22
如果我删除 allocatable 属性,代码会编译并给出正确的结果。我做错了什么还是编译器错误?
代码使用 Intel Fortran 18 编译并给出正确的结果。
PS:我在 Intel(R) Xeon(R) Silver 4114 CPU 上的 Arch Linux 上使用 GCC。
$ gfortran --version
GNU Fortran (GCC) 8.1.1 20180531
Copyright (C) 2018 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
【问题讨论】:
-
所有 Fortran 问题请使用标签 fortran。
-
FWIW 代码编译和运行没有抱怨我使用 GFortran 8.0.0 的安装。而且我看不出代码有什么问题。