i am using openmp in fortran and before the program gets much done at all, it runs into a segfault by simply trying to write to a line as seen below:
783 module Lines
784
785 character*80 eline, dline
786
787 contains
788
789 subroutine InitializeLines
790 print*, 'writing to eline'
791 write(eline,'(1x,79(''#''))')
792 print*, 'writing to dline'
793 write(dline,'(1x,79(''-''))')
794 end subroutine InitializeLines
795
796 end module Lines
in the main routine, when i try to call InitializeLines, it simply prints out writing to eline before giving me a segfault. Now, the program runs perfectly fine when i compile without -fopenmp. Note also, that there is only one thread active in this section (a simply get_num_threads() reveals that I am entering InitializeLines with only one thread). As soon as i take off -fopenmp, there is no segfault and the program executes correctly.
note also that i have a -DUSEOPENMP flag in my makefile for when i want to use openmp. if i have not defined that, then no openmp actions will take place. this is important because when i compile without -DUSEOPENMP, but with -fopenmp, i get the same segfault problem even though i am 100% sure i am not even calling any extra threads or using any part of openmp.
Now, with a bit of playing around i have found that this problem goes away when i take out the -static as a compilation option. my question is – why? why is it that -static and -fopenmp don’t work together and is it a problem that i can address in some other way?
This is a glibc specific problem – static linking doesn’t play well with threaded programs, including OpenMP. There is a workaround though: force the linker to link the whole
libpthread.aarchive and not only the directly referenced symbols with-Wl,--whole-archive -lpthread -Wl,--no-whole-archive.Unless you have a really good reason to link system libraries statically, don’t do that.