Fortran 数组分配溢出
Fortran array allocation overflow
我是 Fortran 的新手,在模块内的子例程中我试图声明以下变量:
real(kind = 8), dimension(dim*nnds, dim*nnds) :: Kgel
我得到以下信息:
Unhandled exception at 0x009F4029 in Solver.exe: 0xC00000FD: Stack overflow (parameters: 0x00000000, 0x00602000).
dim 和 nnds 是来自另一个模块的变量,我知道它们被正确传递为:
integer(kind = 8) :: nnds, dim
dim = 2
nnds = 937
如果我像这样声明变量:
real(kind = 8), dimension(2*937, 2*937) :: Kgel
甚至喜欢:
real(kind = 8), dimension(:, :), allocatable :: Kgel
allocate(Kgel(dim*nnds, dim*nnds))
它有效,为什么我不能像这样声明 'Kgel':
real(kind = 8), dimension(dim*nnds, dim*nnds) :: Kgel
非常感谢您抽出时间...
更新
我的代码是这样的:
program MainTest
use FirstModule
use SecondModule
call FirstSubRoutine
call SecondSubRoutine
end program
.
module FirstModule
integer(kind = 8) :: nnds, dim
contains
subroutine FirstSubRoutine()
!does stuff and eventually
dim = 2
nnds = 937
end subroutine
end module
.
module SecondModule
use FirstModule
contains
subroutine SecondSubRoutine()
real(kind = 8), dimension(nnds*dim, nnds*dim) :: Kgel
!print *, dim -> dim = 2
!print *, nnds -> nnds = 937
!pause
!but this works:
!real(kind = 8), dimension(:, :), allocatable :: Kgel
!allocate(Kgel(dim*nnds, dim*nnds))
end subroutine
end module
这个小测试代码将重现我的问题。
更新
通过更改 "Stack Reserve Size" 它现在似乎工作正常:
要解决这个问题,需要这样声明变量:
real(kind = 8), dimension(:, :), allocatable :: Kgel
!...
allocate(Kgel(dim*nnds, dim*nnds))
或者,由于我在 Visual Studio 上使用英特尔 Fortran 编译器,在项目属性 > 配置属性 > Fortran > 优化中将堆数组设置为从 0 到 n:
"Heap Arrays: Allocate temporary arrays of minimum size n (in kilobytes) on the heap rather than on the stack. Use 0 to always allocate them on the heap. Leave blank not to activate."
我是 Fortran 的新手,在模块内的子例程中我试图声明以下变量:
real(kind = 8), dimension(dim*nnds, dim*nnds) :: Kgel
我得到以下信息:
Unhandled exception at 0x009F4029 in Solver.exe: 0xC00000FD: Stack overflow (parameters: 0x00000000, 0x00602000).
dim 和 nnds 是来自另一个模块的变量,我知道它们被正确传递为:
integer(kind = 8) :: nnds, dim
dim = 2
nnds = 937
如果我像这样声明变量:
real(kind = 8), dimension(2*937, 2*937) :: Kgel
甚至喜欢:
real(kind = 8), dimension(:, :), allocatable :: Kgel
allocate(Kgel(dim*nnds, dim*nnds))
它有效,为什么我不能像这样声明 'Kgel':
real(kind = 8), dimension(dim*nnds, dim*nnds) :: Kgel
非常感谢您抽出时间...
更新
我的代码是这样的:
program MainTest
use FirstModule
use SecondModule
call FirstSubRoutine
call SecondSubRoutine
end program
.
module FirstModule
integer(kind = 8) :: nnds, dim
contains
subroutine FirstSubRoutine()
!does stuff and eventually
dim = 2
nnds = 937
end subroutine
end module
.
module SecondModule
use FirstModule
contains
subroutine SecondSubRoutine()
real(kind = 8), dimension(nnds*dim, nnds*dim) :: Kgel
!print *, dim -> dim = 2
!print *, nnds -> nnds = 937
!pause
!but this works:
!real(kind = 8), dimension(:, :), allocatable :: Kgel
!allocate(Kgel(dim*nnds, dim*nnds))
end subroutine
end module
这个小测试代码将重现我的问题。
更新
通过更改 "Stack Reserve Size" 它现在似乎工作正常:
要解决这个问题,需要这样声明变量:
real(kind = 8), dimension(:, :), allocatable :: Kgel
!...
allocate(Kgel(dim*nnds, dim*nnds))
或者,由于我在 Visual Studio 上使用英特尔 Fortran 编译器,在项目属性 > 配置属性 > Fortran > 优化中将堆数组设置为从 0 到 n:
"Heap Arrays: Allocate temporary arrays of minimum size n (in kilobytes) on the heap rather than on the stack. Use 0 to always allocate them on the heap. Leave blank not to activate."