Fortran calling Julia

Looking for advice/experience/pitfalls on calling Julia from Fortran.

The situation:
Coupled Climate Model (Fortran coupler & Fortran ocean, ice, atmosphere, land models,…) which we’d like to be able to have the coupler call a prototype wave model written in Julia.

Looking at
https://docs.julialang.org/en/v1/manual/embedding/#Embedding-Julia
it seems possible, wondered if anyone had experience or recommendations for this.

Cheers,
Helen

2 Likes

Hi @hkershaw, welcome to the forum. Maybe there are others here who have already done it. Otherwise I would start with the link you posted, and call those C functions from Fortran via iso_c_binding, link everything together and see.

(In the long run, I would like LFortran and other Fortran compilers to gain support for out of the box support for interoperation with other languages including Python, Julia, JavaScript, WASM, etc., automatically converting arguments, and so on.)

1 Like

You may also have a look there:
https://docs.julialang.org/en/v1/manual/calling-c-and-fortran-code/

This shows how to create a C-compatible function pointer to a Julia function. This allows the possibly less intrusive pattern (not tried myself):

  • turn your Fortran main program into a main subroutine with BIND(C) attribute, accepting arguments of type C_FUNPTR corresponding to the Julia functions you want to call later
  • compile your Fortran code with -fPIC and create a shared library
  • write a Julia main script that loads your Julia modules, creates callbacks to Julia functions to be called from Fortran, loads your shared lib and calls your main Fortran subroutine with these callbacks as arguments.
1 Like

Have you considered compiling the Fortran into a Julia routine with a Julia call back? See f2py in Python. Maybe Julia has copied that too.