How to use a shared/dynamically linked library in Fortran

Hi Mary,

calling a C from Fortran is not a big issue per-se. However with your MATLAB generated routines there are other aspects which make things complicated.

  1. You will need to install the MATLAB Runtime Library - a standalone shared library of 3.9 GB (the 64-bit Linux version).
  2. To link your generated MATLAB C functions, it’s recommended to use mbuild, MATLAB’s C and C++ compiler and linker wrapper. This tool is aware of the MATLAB runtime path and settings. With your Fortran main program you will be on your own.
  3. The MATLAB runtime is stateful, it has to be initialized and terminated appropriately. In Fortan this would look like this:
program main

use, intrinsic :: iso_c_binding

! MATLAB C Library (C function interfaces)
use mcl, only: &
  mclInitializeApplication, &
  mclRunMain, &
  mclTerminateApplication

! Generated library YYY (C function interfaces)
use libYYY, only: &
  libYYYInitialize, libYYYTerminate, &
  mlfBodyfatFcn

implicit none

logical(c_bool) :: success
integer(c_int) :: stat

success = mclInitializeApplication(c_null_ptr, 0)
if (.not. success) then
   write(*,'(A)') "Error: Initialization failed."
   stop 1
end if

! ... main stuff goes here ...

success = mclTerminateApplication()
if (.not. success) then
   write(*,'(A)') "Error: termination failed"
end if
end program
  1. The shared library generated by MATLAB is stateful, it has it’s own initialization and termination routines (libBodyfatInitialize/libBodyfatTerminate). Each shared library you export will have it’s own. Your program will be like an onion of setup/teardown calls.
  2. Finally, everything is initialized… but it’s not time to celebrate yet - we need to create some mxArray instances. You can choose between the C Matrix API or the (old-fashioned) Fortran counterpart that requires a preprocessor and is probably bound to the MATLAB-provided compilers. Both API’s require a non-negligible amount of setup and pointer manipulation to fill the mxArray or retrieve values out of it. I would probably pick the C version.
  3. Now that the MATLAB mxArrays are ready, the generated function can be called. How? Either you write an explicit C interface for the generated function (the ones prepended with mlx and mlf) or you write a C wrapper with name-mangling matching your Fortran compiler (e.g. mlfBodyfatFcn_( /* ... */ ) ). You should probably only be using the MATLAB supported compilers with the generated functions, so heck why not.
  4. Now you are ready to call your function. nargout is the number of outputs you want (Y, Xf, and Af are output arguments). X is your input. I suspect that _U4b and _U4x are cell arrays for some neural-network parameters. Consult the MATLAB documentation for an explanation.
  5. If everything so far has worked, you will get the Y values that you can now reuse in your Fortran program. Hooray!

Personally, before settling on this plan I would investigate exporting the network to some other format. MATLAB provides exportONNXNetwork (maybe join forces with this other thread) and exportNetworkToTensorFlow. If you save the network in HDF5 format you may be able to import it into neural-fortran. (one caveat is the architecture you are dealing with might not be supported).

2 Likes