4

Is it possible to have a Fortran program load a Fortran library at run time? If so, would it be possible to modify a function and recompile only the library to have the originally-compiled program call the modified function in the library at run time?

If anyone can provide a minimal working example of how this could be achieved that would be great.

lenzinho
  • 391
  • 1
  • 11
  • 2
    Q: Is it possible to have a fortran program load a fortran library at run time? A: Sure. For example Gnu Fortran supports dynamic modules (.so) on Linux. – paulsm4 Aug 02 '16 at 00:52
  • Are you asking how to create a dynamic library? – haraldkl Aug 02 '16 at 06:08
  • Does the linker need the new library or will is know what to do? Things differ with using .a and a .so . I am assuming that you cannot recompile, or do not have source code, for the other parts? – Holmz Aug 02 '16 at 10:57
  • Is this Linux or Windows? It's possible on either one. On Linux you call dlopen and dlsym to get a pointer to the routine, on Windows LoadLibrary and GetProcAddress. Intel Visual Fortran provides a complete example of this for Windows. – Steve Lionel Aug 03 '16 at 15:27
  • Steve, it's actually on OS X. – lenzinho Aug 04 '16 at 00:29
  • Well, dlopen and dlsym may work there. Or maybe not - OS X is its own weird world that changes things incompatibly with every minor update. Of course, on OS X dynamic libraries are called .dylib. Whose Fortran are you using? You will probably need to use ISO_C_BINDING, the C_FUNPTR type and C_F_PROCPOINTER, with TRANSFER to move bits around. – Steve Lionel Aug 04 '16 at 00:57

1 Answers1

8

Here are some few links that can be helpfull:

  • This page on rosettacode.org which gives complete example with details and discuss implementation on linux and MACOS
  • This intel forum post where Steve Lionel give some advice on how to do the dynamic loading with ifort
  • this IBM page with a great explanation of dynamic libraries and their usage

If you want a small easy to understand code, keep reading. Few days ago, I was playing with dynamic loading. My test code below might be of help to you. However I work in the linux environment and you might have to adapt few thing here and there for it to work on your OS X environment. The rosettacode.org link above will come handy to help you.

Here is the code for the test dynamic lib

[username@hostname:~/test]$cat test.f90

module test
    use, intrinsic :: iso_c_binding
contains
    subroutine t_times2(v_in, v_out) bind(c, name='t_times2')
        integer, intent(in) :: v_in
        integer, intent(out) :: v_out
        !
        v_out=v_in*2
    end subroutine t_times2
    !
    subroutine t_square(v_in, v_out) bind(c, name='t_square')
        integer(c_int), intent(in) :: v_in
        integer(c_int), intent(out) :: v_out
        !
        v_out=v_in**2
    end subroutine t_square
end module test

Compiled as

[username@hostname:~/test]$gfortran -c test.f90
[username@hostname:~/test]$gfortran  -shared -o test.so test.o

Here is the test program

[username@hostname:~/test]$cat example.f90
program example
    use :: iso_c_binding
implicit none

    integer(c_int), parameter :: rtld_lazy=1 ! value extracte from the C header file
    integer(c_int), parameter :: rtld_now=2 ! value extracte from the C header file
    !
    ! interface to linux API
    interface
        function dlopen(filename,mode) bind(c,name="dlopen")
            ! void *dlopen(const char *filename, int mode);
            use iso_c_binding
            implicit none
            type(c_ptr) :: dlopen
            character(c_char), intent(in) :: filename(*)
            integer(c_int), value :: mode
        end function

        function dlsym(handle,name) bind(c,name="dlsym")
            ! void *dlsym(void *handle, const char *name);
            use iso_c_binding
            implicit none
            type(c_funptr) :: dlsym
            type(c_ptr), value :: handle
            character(c_char), intent(in) :: name(*)
        end function

        function dlclose(handle) bind(c,name="dlclose")
            ! int dlclose(void *handle);
            use iso_c_binding
            implicit none
            integer(c_int) :: dlclose
            type(c_ptr), value :: handle
        end function
    end interface

    ! Define interface of call-back routine.
    abstract interface
        subroutine called_proc (i, i2) bind(c)
            use, intrinsic :: iso_c_binding
            integer(c_int), intent(in) :: i
            integer(c_int), intent(out) :: i2
        end subroutine called_proc
    end interface

    ! testing the dynamic loading
    integer i, i2
    type(c_funptr) :: proc_addr
    type(c_ptr) :: handle
    character(256) :: pName, lName

    procedure(called_proc), bind(c), pointer :: proc
    !
    i = 15

    handle=dlopen("./test.so"//c_null_char, RTLD_LAZY)
    if (.not. c_associated(handle))then
        print*, 'Unable to load DLL ./test.so'
        stop
    end if
    !
    proc_addr=dlsym(handle, "t_times2"//c_null_char)
    if (.not. c_associated(proc_addr))then
        write(*,*) 'Unable to load the procedure t_times2'
        stop
    end if
    call c_f_procpointer( proc_addr, proc )
    call proc(i,i2)
    write(*,*) "t_times2, i2=", i2
    !
    proc_addr=dlsym( handle, "t_square"//c_null_char )
    if ( .not. c_associated(proc_addr) )then
        write(*,*)'Unable to load the procedure t_square'
        stop
    end if
    call c_f_procpointer(proc_addr, proc)
    call proc(i,i2)
    write(*,*) "t_square, i2=", i2
contains
end program example

Compiled and run as:

[username@hostname:~/test]$gfortran -o example example.f90 -ldl
[username@hostname:~/test]$./example
t_times2, i2=          30
t_square, i2=         225
[username@hostname:~/test]$
innoSPG
  • 4,588
  • 1
  • 29
  • 42