【发布时间】:2017-11-12 23:40:47
【问题描述】:
我有一些 Fortran 代码,其中包含一个子例程和对其的调用。它应该使用定义的窗口大小来计算矩阵中元素的平均值。例如,使用 winsize=2 在 (10,10) 数组上调用子例程将返回 (5,5) 数组。
代码如图:
SUBROUTINE avgwin(ts, sizelat,sizelon,winsize,size2,size3,ts_new)
implicit none
double precision, dimension(10,sizelat,sizelon) :: ts
double precision, dimension(winsize,winsize) :: store
double precision, dimension(10,size2,size3) :: ts_new
double precision :: par,ave
integer :: sizelat, sizelon,i,j,k,winsize,size2,size3
integer :: A, B,p,m,numb
A=0
B=0
par = 11 !Hypothetical value to be excluded
do i=1,10 !Looping through time
do j=1,sizelat !Looping through latitude
if ((j+winsize) > sizelat) then !Checks if it will exceed bounds
exit !If it'll exceed, discard remaining cells
end if
do k=1,sizelon !Looping through longitude
if ((k+winsize)>sizelon) then
exit
end if
store = ts(i,j:j+winsize,k:k+winsize) !Gets the values for that window
where (store == par) store = -99 !Replaces masked with -99
ave = 0
numb = 0 !Variable to count
do p=1,winsize
do m=1,winsize
if (store(p,m)==-99) then !Evaluates if it's masked, i.e., =-99
ave = ave
else
ave = ave + store(p,m) !Sum of existent values
numb = numb +1 !Updates counting variable
end if
end do
end do
ave = ave/numb !Calculates the mean
ts_new(i,A,B) = ave
B=B+1
end do
B=0
A=A+1
end do
A=0
B=0
end do
END SUBROUTINE
program testefor
implicit none
double precision, dimension(10,10,10) :: teste
double precision, dimension(10,5,5) :: oi
integer :: i,j,k
do i=1,10
do j=1,10
do k=1,10
teste(i,j,k)=i
end do
end do
end do
CALL avgwin(teste,10,10,2,5,5,oi)
print*, oi(1,5,5)
end program testefor
但是,当我运行它时,我遇到了分段错误。我尝试使用 GDB 对其进行调试,令我惊讶的是,它返回了正确的结果,但在退出程序时出现了段错误。我从 gdb 得到的可以在下面找到:
Breakpoint 1, testefor () at testefor.f90:56
56 do i=1,10
(gdb) cont
Continuing.
1.0000000000000000
Program received signal SIGSEGV, Segmentation fault.
0x0000000000400f73 in testefor () at testefor.f90:67
67 end program testefor
所以程序返回正确的 (1,5,5) 元素 = 1.0 但在其他地方出错。
有人可以帮我找出问题吗?谢谢
【问题讨论】:
标签: segmentation-fault fortran