Stack space typically comes from local variables and intermediate calculations. Try to avoid these for large variables. Use allocatable variables instead, which come from the heap. E.g., this causes the memory for x to come from the stack:
subroutine foo
real*8 x(10000000)
whereas this will cause the memory for x to come from the heap (generally much larger than the stack):
subroutine foo
real*8, allocatable :: x(:)
allocate(x(10000000))
:
deallocate(x)
If an intermediate calculation is causing a stack overflow, e.g. a large matrix calculation, try to break it down into old F77 style do-loops instead.
Best Answer