1

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!

Hristo Iliev
  • 72,659
  • 12
  • 135
  • 186
ceeely
  • 135
  • 1
  • 10
  • 1
    Just search related questions. This problem is comming back again in regular intervals. – Vladimir F Героям слава May 23 '16 at 07:49
  • `OMP_STACKSIZE` does not affect the stack of the main thread. `ulimit -s ...` does. – Hristo Iliev May 23 '16 at 10:50
  • @HristoIliev I've tried your solutions, and they all didn't work unlike ohm's – ceeely May 24 '16 at 01:58
  • You did not specify that you are using Windows. The solution with `ulimit -s` is Unix-specific. On Windows, the stack size of the main thread is fixed during link time and can be increased without recompiling by editing the binary with [EDITBIN](https://msdn.microsoft.com/en-us/library/xd3shwhf.aspx). – Hristo Iliev May 24 '16 at 08:11
  • 1
    @HristoIliev I believe it is incorrect to mark it as duplicate of a question where there is no mention of the possibility of usage of allocatable arrays when there are plenty of other duplicates where both are mentioned. – Vladimir F Героям слава May 24 '16 at 09:12
  • @VladimirF, the OP explicitly tries to set the stack size. I've marked the question as a duplicate of one where my answer explains how to set the stack size. Besides, it was not obvious initially that (s)he is running Windows. – Hristo Iliev May 24 '16 at 09:18

1 Answers1

2

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).

Omar Awile
  • 140
  • 1
  • 6
  • Ah, thanks a lot, ohm314! That solves that problem. I was thinking what other ways I can go would the stack size problem. Thank you for the allocate solution. – ceeely May 23 '16 at 06:22
  • No worries, glad I could help ;) – Omar Awile May 23 '16 at 06:28
  • 1
    On the contrary, it has lots to do with OpenMP. Most Fortran compilers implement automatic heap allocation for large arrays and that feature gets turned off when OpenMP is enabled, even when no actual OpenMP directives are present. – Hristo Iliev May 23 '16 at 10:42
  • 1
    You cannot rely on the compiler doing automatic heap allocation, which is exactly in the first place why you should allocate large arrays on the heap. But yes, I had forgotten about the bit where openmp flags cause this "feature" to be disabled. – Omar Awile May 23 '16 at 19:56