public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
From: Andrew Benson <abensonca@gmail.com>
To: Fortran <fortran@gcc.gnu.org>
Subject: gfortran, OpenMP and static linking
Date: Fri, 16 Sep 2022 15:51:00 -0700	[thread overview]
Message-ID: <1720594.qhh6PHbjeT@abensonca-precision-7540> (raw)

I've been following the fix proposed by Bernhard in this thread:

https://gcc.gnu.org/pipermail/fortran/2021-April/055907.html

to allow static linking of Fortran codes compiled with -fopenmp. (I know that 
static linking of OpenMP codes isn't guaranteed to work, but this fix has 
worked well for me anyway.)

This fix seems to no longer work with glibc >= 2.34 - I think because the 
functionality of libpthread has been integrated into libc:

https://sourceware.org/pipermail/libc-alpha/2021-August/129718.html

When compiling the simple test code from that thread with glibc 2.34 or newer 
I get a segfault:

$ cat omp.f90 
use omp_lib
!$omp parallel
  write(*,*) "thread ", omp_get_thread_num()
!$omp end parallel
end
$ gfortran -o omp -fopenmp omp.f90 -static -Wl,--whole-archive -lpthread -
Wl,--no-whole-archive && OMP_NUM_THREADS=2 ./omp
./../../gcc-13-source/gcc-13-20220710/libgomp/config/linux/../../allocator.c:
102: warning: Using 'dlopen' in statically linked applications requires at 
runtime the shared libraries from the glibc version used for linking
./../../gcc-13-source/gcc-13-20220710/libgomp/oacc-profiling.c:137: warning: 
Using 'dlopen' in statically linked applications requires at runtime the 
shared libraries from the glibc version used for linking
 thread            0
 thread            1

Program received signal SIGSEGV: Segmentation fault - invalid memory 
reference.

Backtrace for this error:
#0  0x467b0f in ???
#1  0x0 in ???
Segmentation fault (core dumped)

Tracking this down in gdb it seems that pthread_mutex_destroy() is called, but 
that symbol is null. 

From comment #9 in this PR:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=58909#c9

I figured out a workaround, which is to create a C file with this content:

#include "pthread.h"

#define nullptr ((void*)0)

void pthread_workaround() {
  pthread_mutex_destroy((pthread_mutex_t *) nullptr);
}

which defines a function pthread_workaround() which calls 
pthread_mutex_destroy(), but which itself it never called. Compiling that C 
code, and linking with the Fortran allows the test code to run successfully.

$ gcc -c -o fix.o -fopenmp fix.c
$ gfortran -c -o omp.o -fopenmp omp.f90
$ gfortran -o omp -fopenmp -static omp.o fix.o
./../../gcc-13-source/gcc-13-20220710/libgomp/config/linux/../../allocator.c:
102: warning: Using 'dlopen' in statically linked applications requires at 
runtime the shared libraries from the glibc version used for linking
./../../gcc-13-source/gcc-13-20220710/libgomp/oacc-profiling.c:137: warning: 
Using 'dlopen' in statically linked applications requires at runtime the 
shared libraries from the glibc version used for linking
$ OMP_NUM_THREADS=2 ./omp
 thread            0
 thread            1

My understanding of glibc, pthreads, etc. etc. is insufficient to know if this 
is a good and/or safe workaround, but I figured I'd share it here in case:

a) it's useful to anyone;
b) it's indicative of a bug in gfortran;
c) anyone has a more elegant solution!

-Andrew

-- 

* Andrew Benson: https://abensonca.github.io

* Galacticus: https://bitbucket.org/abensonca/galacticus




             reply	other threads:[~2022-09-16 22:51 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-16 22:51 Andrew Benson [this message]
  -- strict thread matches above, loose matches on Subject: below --
2021-04-03 18:55 Harald Anlauf
2021-04-03 21:24 ` Bernhard Reutner-Fischer
2021-04-04 10:46   ` Bernhard Reutner-Fischer
2021-04-04 20:17     ` Harald Anlauf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1720594.qhh6PHbjeT@abensonca-precision-7540 \
    --to=abensonca@gmail.com \
    --cc=fortran@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).