What could be the maximum value of N in a NxN matrix in a Fortran 90 code so that no segmentation fault occurs?
For example, when I use matrices of size 2^8 x 2^8 and using them for multiplications (using intrinsic 'matmul' function), I don't have any problem.
However, when I used 2^12 x 2^12 matrices, I didn't find any compilation error, but I got segmentation fault in the middle of the program.
Am I running out the memory allocation or there could be a bug in my code?
I have used dynamic allocation and I using 64 bit RedHat OS with 64 GB RAM.
Also does the maximum size depend on the compiler or the system configuration?
Fortran compilers generally allocate allocatable arrays on the stack; or perhaps on the heap. Of this I am uncertain, but I am certain that the Fortran standard does not mandate where allocatable arrays are placed. Your compiler documentation will tell you where, by default, allocatable arrays are placed. You may find that the fault you are seeing can be avoided by either moving the locus of allocation or by extending the (operating-system determined) stack or heap size.
You could also do yourself a favour by using the stat
optional argument to any allocate
statements and catch, in your program, failures to allocate. You still won't, of course, get an array allocated but this will avoid a program crash.
You should also follow the advice in @Alexander's answer and calculate the size of the arrays you are trying to allocate, fiddling with heap and stack size won't provide you with more space than you have RAM available.
(2) The maximum allocatable size depends on your main memory, and the maximum size of the array constructor (for gfortran) this is 65535 by default and can be set by -fmax-array-constructor=n
, see here.
(1) This is quite easy to calculate:
Double precision complex variables are at 16 Byte. So, if you have an N x N
matrix and 64GB of RAM, this leads to N = \sqrt ( 64 GB / 16 B ) \approx 63245
.
So for double precision complex values, you are limited by the main memory.
Real
s are (typically) at 4 Byte for single precision and 8 Byte for double precision. Single precision complex variables use 8 Byte.
You should try setting the stack size to unlimited using
ulimit -s unlimited
I had similar problems, because ifort
creates large temporary arrays on the stack for matrixoperations and this can lead to unexpected segfaults.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With