Calling Julia from Fortran?

I’m looking at moving some of my coding to Julia, and while calling Fortran from Julia seems straight forward, I haven’t found too much about calling Julia from Fortran. The reason is I have some Fortran code that calls “user supplied subroutines”, but I would like to write those routines in Julia; hence, the flow would be: Julia → Fortran ->Julia.

Is this easily doable, not doable, or it depends? Any links to existing threads would be great!
Thanks!

1 Like

I don’t have any particular experience with this, but @cfunction is intended for these use cases. If the calling convention of your fortran compiler is the same as C, that might just work. Did you give that a try?

No I haven’t. I’m just looking at the idea of moving a project over to Julia and seeing if that’s feasible. Thanks, I’ll talk a look at @cfunction

this is not what your title says, if you start with Julia, why does Fortran needs to ‘call’ julia?

@joe_vallino is referring to callbacks.

2 Likes

to me it sounds like he can just pass the result of Julia into the Fortran (i.e calling Fortran with Julia). Because everything ends up being Julia anyways.

so instead of Julia calling a Fortran function that calls subrountine( which is in Julia), just call the Fortran function that would process the result of subroutine(which you pass to Fortran from Julia) in the first place.

As a library user, you don’t get to decide the API of the library you are using. If it expects a callback, you need to give it a callback, or else you can’t use it.

The point being, such a function might not exist (or might not be efficient, or whatever other reason).

Using callbacks is quite typical pattern, and Julia even has the do syntax for making it easier. You seem to argue it shouldn’t be used across language barriers, am I right? Why is that?

It’s worth noting that scalars are generally passed as pointers in Fortran, so that might need some manual care and consideration when using @cfunction to generate a callback which can be called from Fortran.

Thanks for the feedback everyone! I’m getting the impression that executing the callback, while perhaps doable, is non-trivial (at least for me).

To add some context, I’ve been using BACOLI95 (http://cs.stmarys.ca/~muir/BACOLI-3_Webpage.htm) to solve a 1D Advection-Dispersion-Reaction problem with a few hundred state variables. BACOLI95 calls user supplied Fortran subroutines that define the governing PDE equations, boundary and initial conditions. An alternative (and better) solution would be to use Julia code to solve the 1D ADR problem direction, but I have yet to find a “package” that does what I need. (Remember, I’m just starting to use Julia, so utilizing what others have developed is quite helpful). The code needs to be efficient, because right now the Fortran code takes several days to run on a 100 core mini cluster.

While that is true, one can also use the value attribute in Fortran to pass scalar dummy arguments by value to C and vice versa. Interfacing Fortran-C is super-easy with the use of iso_c_binding intrinsic module of Fortran 2003 and especially, with the additional C descriptors added to Fortran 2018.

If you find the solution (for callback) I’d appreciate sharing it here as I need the same functionality for a similar project. Thanks in advance! In Python, a Python callback from C/Fortran is rather straightforward to implement (via ctypes module). Julia claims to be even easier to work with, so I am sure someone has already come up with a trick to do callbacks in Julia as well. Also keep in mind that, any solution for C-Julia callback would be also applicable to Fortran-Julia via iso_c_binding intrinsic module of Fortran.