possible bug when using omp_set_num_threads with -fno-underscoring

FX fxcoudert@gmail.com
Mon Oct 27 13:09:00 GMT 2014


> Using -fno-underscoring with omp_set_num_threads() followed by a !$OMP
> PARALLEL region leads to a Segmentation fault.
> (Without a following PARALLEL region it works but is useless... ;-) )
> As a work around the trailing "_" can be inserted manually:
> omp_set_num_threads_()

I was wondering why it segfaulted instead of simply not linking, but that’s because there are indeed functions without trailing underscore in libgomp, and they’re the C variants (why take arguments by value), with the underscored functions being wrappers.

I suspect we can prevent -fno-underscoring from interfering if we explicitly define function names with BIND(C). Take this from omp_lib.f90.in:

        interface omp_set_num_threads
          subroutine omp_set_num_threads (num_threads)
            integer (4), intent (in) :: num_threads
          end subroutine omp_set_num_threads
          subroutine omp_set_num_threads_8 (num_threads)
            integer (8), intent (in) :: num_threads
          end subroutine omp_set_num_threads_8
        end interface

we could have it be:

        interface omp_set_num_threads
          subroutine omp_set_num_threads (num_threads) bind(c,name="omp_set_num_threads_")
            integer (4), intent (in) :: num_threads
          end subroutine omp_set_num_threads
          subroutine omp_set_num_threads_8 (num_threads) bind(c,name="omp_set_num_threads_8_")
            integer (8), intent (in) :: num_threads
          end subroutine omp_set_num_threads_8
        end interface


Looking at this code, we could remove many of the wrapper functions with proper C-interoperability definitions. For example, we could replace:

        interface
          function omp_get_num_threads ()
            integer (4) :: omp_get_num_threads
          end function omp_get_num_threads
        end interface

with

        interface
          function omp_get_num_threads (), bind(c,name="omp_get_num_threads")
            integer (4), value :: omp_get_num_threads
          end function omp_get_num_threads
        end interface

and get rid of the omp_get_num_threads_ wrapper function (well, keep it for backward compatibility, but avoid the function call). I understand that it’s probably not performance-critical code in most cases, but I could help with the refactoring if you (Jakub? Tobias?) think it’s useful.

FX


More information about the Fortran mailing list