0

When I link the following code

PROGRAM MAIN
implicit none
integer(8), PARAMETER :: N=2**9
complex(8) ::A(N,N),B(N,N),C(N,N)

C=matmul(A,B)
end program MAIN

with Lapack and OpenMP via:

gfortran test.f95 -O3 -Wall -g -std=f95 -cpp -I /usr/include/ -L /usr/lib -lm -fopenmp -lpthread -lblas -llapack -fexternal-blas

I get a segmentation fault. Reducing the dimension of the array to 2**8 or removing OpenMP removes the error. What is the reason for this?

Tarek
  • 1,060
  • 4
  • 17
  • 38
  • 1
    What is your reason for `-I /usr/include/ -L /usr/lib -lm -lpthread`? Why? Try to use as few flags as possible to be sure what causes it. This was enough for me : `gfortran statica.f90 -frecursive`. Ceterum censeo `integer(8)` or `kind=8` is very ugly code smell. – Vladimir F Героям слава Jun 03 '16 at 18:27

1 Answers1

1

This is because -fopenmp implies -frecursive (try that one instead). That will cause the arrays to be placed on the stack and you get a stack overflow. By default the arrays will be static.

Tho compiler does this internally (-fdump-tree-original):

MAIN__ ()
{
  complex(kind=8) a[262144];
  complex(kind=8) b[262144];
  complex(kind=8) c[262144];

You could argue that it is not necessary to affect the main program arrays, because the main program is not re-entrant, but -frecursive does that. If you make th arrays allocatable they won't be affected.

  • So in order to compile my code with OpenMP, I have to use allocatable arrays, right? – Tarek Jun 03 '16 at 18:53
  • Pretty much. You can also increase stack on the machine where it is run, but it may come again later. – Vladimir F Героям слава Jun 03 '16 at 19:10
  • 1
    I would have closed the question as duplicate of something like http://stackoverflow.com/questions/13264274/why-segmentation-fault-is-happening-in-this-openmp-code , but I was surprised you have no OpenMP directive, I didn't realize `-frecursive` is sufficient before. I also don't like the emphasis on enlarging the stack there. Maybe someone else will close it instead. – Vladimir F Героям слава Jun 03 '16 at 19:23