问题
I am using Eclipse with GNU Fortran compiler to compute a large arrays to solve a matrix problem. However, I have read and notice that I am unable to read all my data into the array which causes my project.exe to crash when I invoke -fopenmp into my compiler settings; otherwise, the program works fine.
program Top_tier
integer, parameter:: n=145894, nz_num=4608168
integer ia(n+1), ja(nz_num)
double precision a(nz_num), rhs(n)
integer i
open (21, file='ia.dat')
do i=1, n+1
read(21,*) ia(i)
enddo
close(21)
open (21, file='a.dat')
do i=1, nz_num
read(21,*) a(i)
enddo
close(21)
open (21, file='ja.dat')
do i=1, nz_num
read(21,*) ja(i)
enddo
close(21)
open (21, file='b.dat')
do i=1, n
read(21,*) rhs(i)
enddo
close(21)
End
In my quest to find a solution around it, I have found the most probable cause is the limit of the stack size which can be seen by the fact that if I set nz_num to lesser or equal to 26561, the program will run properly. A possible solution is to set environment variable to increase stacksize but the program does not recognise when I type "setenv" or "export" OMP_STACKSIZE into the program. Am I doing something wrong? Is there any advise on how I can solve this problem?
Thanks!
回答1:
You are allocating a
, rhs
, ia
ja
on the stack, which is why you are running out of stack space in the first place. I would suggest to always allocate large arrays on the heap:
integer, parameter:: n=145894, nz_num=4608168
integer, dimension(:), allocatable :: ia, ja
double precision, dimension(:), allocatable :: a, rhs
integer i
allocate(ia(n+1), ja(nz_num))
allocate(a(nz_num), rhs(n))
! rest of your code...
deallocate(ia, ja)
deallocate(a, rhs)
Instead of directly declaring your four arrays of a certain size (causing them to be allocated on the stack) you declare them as allocatable and give the shape of the arrays. Further down you can then allocate your arrays to the size you want. This size can be chosen at runtime. That means, if you are reading your arrays from a file you could also store the size of the arrays at the beginning of the file and use this for your allocate call. Finally, as always with dynamically allocated memory, don't forget to deallocate them when you don't need them anymore.
Edit: And I forgot to say that this doesn't really have anything to do with openmp except for that openmp threads probably have much small stack size limits (in this case it would be only the openmp master thread).
来源:https://stackoverflow.com/questions/37383326/fortran-openmp-large-array-on-eclipse-program-crash