Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maximum size of a matrix in Fortran 90 avoiding segmentation fault

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

  2. Also does the maximum size depend on the compiler or the system configuration?

like image 940
hbaromega Avatar asked Sep 30 '13 11:09

hbaromega


3 Answers

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.

like image 175
High Performance Mark Avatar answered Oct 04 '22 03:10

High Performance Mark


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

Reals are (typically) at 4 Byte for single precision and 8 Byte for double precision. Single precision complex variables use 8 Byte.

like image 27
Alexander Vogt Avatar answered Oct 04 '22 05:10

Alexander Vogt


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.

like image 28
Stefan Avatar answered Oct 04 '22 03:10

Stefan