# Embedding Julia code in Fortran

**URL:** https://discourse.julialang.org/t/embedding-julia-code-in-fortran/96010
**Category:** New to Julia
**Tags:** question, fortran
**Created:** [March 13, 2023, 4:11pm UTC](https://discourse.julialang.org/t/embedding-julia-code-in-fortran/96010 "2023-03-13T16:11:12Z")
**Posts on this page:** 11
**Page:** 1

<div class="post-metadata">

### Author: ![gradureau](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gradureau](https://discourse.julialang.org/u/gradureau)
#### Post date: [March 13, 2023, 4:11pm UTC](https://discourse.julialang.org/t/embedding-julia-code-in-fortran/96010/1 "2023-03-13T16:11:12Z")

</div>

Hey everyone,

I am fairly new to Julia and am interested to do machine learning with it. So far I learnt how to code in Julia and how to use the Flux library. Yet, My goal is to use trained neural networks in a fortran code. I saw on the Julia page that it is possible to embed Julia code in C/C++, but I can’t manage to find the way to do it in fortran. Does anyone have an idea?

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [March 13, 2023, 4:49pm UTC](https://discourse.julialang.org/t/embedding-julia-code-in-fortran/96010/2 "2023-03-13T16:49:54Z")

</div>

> [@gradureau](#):
>
> use trained neural networks in a fortran code

A cleaner way is to bind to ONNXRuntime from Fortran so you don’t need Julia runtime for inference, after some searches, it appears that this [doesn’t exist.](https://github.com/microsoft/onnxruntime/issues/14633).

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [March 13, 2023, 5:06pm UTC](https://discourse.julialang.org/t/embedding-julia-code-in-fortran/96010/3 "2023-03-13T17:06:32Z")

</div>

It is much easier to interface the other way around. It is conceivable as a workflow to have a main julia code that calls your fortran routines?

In general Julia will be much friendlier for file manipulation, visualization, and the higher-level stuff, and usually one could let some lower level tasks to Fortran if there are nice routines available for one’s purpose.

---

<div class="post-metadata">

### Author: ![gradureau](https://avatars.discourse-cdn.com/v4/letter/g/67e7ee/32.png) [@gradureau](https://discourse.julialang.org/u/gradureau)
#### Post date: [March 14, 2023, 10:14am UTC](https://discourse.julialang.org/t/embedding-julia-code-in-fortran/96010/4 "2023-03-14T10:14:59Z")

</div>

Thank you very much for your answers, so I agree with the fact of using Julia for higher level stuff, yet I am working on a fortran code that already exists and we want to add some machine learning to replace some operations in the code. In order to use Julia as the main code to use machine learning algorithms, I would have to rewrite most of the existing optimized fortran code, which isn’t really what we want, hence my question of embedding Julia code in fortran.

---

<div class="post-metadata">

### Author: ![lmiq](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lmiq/32/18314_2.png) [@lmiq](https://discourse.julialang.org/u/lmiq)
#### Post date: [March 15, 2023, 11:43am UTC](https://discourse.julialang.org/t/embedding-julia-code-in-fortran/96010/5 "2023-03-15T11:43:11Z")

</div>

> [@gradureau](#):
>
> I would have to rewrite most of the existing optimized fortran code

Since there is no better answer, unfortunately, I would just point that probably you would need “only” a wrapper for the main Fortran program. You could convert the main fortran program into a subroutine, and call it from Julia. Or create a new “main” program in Julia and call all the subroutines of Fortran from it. Of course how hard is that depends a lot on the the complexity of the program.

The alternative, easy one, is just to use each code (Julia and Fortran) as just independent program to execute, and interface them by writing and reading stuff from and to files. Of course that’s only viable if the cost of this slow interface is not important.

---

<div class="post-metadata">

### Author: ![Eben60](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eben60/32/13475_2.png) [@Eben60](https://discourse.julialang.org/u/Eben60)
#### Post date: [March 15, 2023, 1:38pm UTC](https://discourse.julialang.org/t/embedding-julia-code-in-fortran/96010/6 "2023-03-15T13:38:24Z")

</div>

One more option would be not _embedding_ Julia, but _calling_ Julia. Meaning you have a Julia server and some ways of communication between your Fortran and Julia programs (e.g. ZMQ).

What are your requirements for throughput and latency?

---

<div class="post-metadata">

### Author: ![Alexander-Barth](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/alexander-barth/32/3692_2.png) [@Alexander-Barth](https://discourse.julialang.org/u/Alexander-Barth)
#### Post date: [March 15, 2023, 8:19pm UTC](https://discourse.julialang.org/t/embedding-julia-code-in-fortran/96010/7 "2023-03-15T20:19:18Z")

</div>

In Fortran, you call a C function ([Interoperability with C (The GNU Fortran Compiler)](https://gcc.gnu.org/onlinedocs/gfortran/Interoperability-with-C.html#Interoperability-with-C) ). So maybe you can call the jl\_init function (and others) by declaring a interface with the bind c attribute in Fortran. The problem would be C macros and potentially C data structures not suppoted by the Fortran’s C binding. So maybe you need some small C wrapper around your Julia code, that you can call from Fortran.

I have never tried this myself, but I would be interested to hear about your approach.

---

<div class="post-metadata">

### Author: ![Palli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/palli/32/3380_2.png) [@Palli](https://discourse.julialang.org/u/Palli)
#### Post date: [March 15, 2023, 9:16pm UTC](https://discourse.julialang.org/t/embedding-julia-code-in-fortran/96010/8 "2023-03-15T21:16:47Z")

</div>

There’s one other option, call some other language from Fortran, I suggest to Python.

I googled a bit, found this, not sure if it’s the best, seems good, see its blog post:

> **[GitHub - nbren12/call\_py\_fort: Call python from fortran](https://github.com/nbren12/call_py_fort)**
>
> Call python from fortran. Contribute to nbren12/call\_py\_fort development by creating an account on GitHub.

I don’t mean this necessarily as a substitute for Julia and Flux (though would be helpful for e.g. Tensorflow or PyTorch), but rather to then call from Python to Julia, with Pythoncall.jl (it’s also to call from Python, or use pyjulia). Learning to call to or from Python (for Julia and likely any for language) is very useful, likely to be helpful to know, also independently of what you’re trying to do.

This might be the easiest intermediate language, and then you don’t need to code anything for interop, just call a bit differently. And since calling to or from Python is in-process (and I suppose all three), it’s fast, most likely.

There are some issues with coding your own interop infrastructure, that I’m not to up-to-speed on. I just know you get rid of them for some minor inconvenience. If some of the code is multi-threaded, that might be an issue, but likely will be with and without this solution.

There are some other options for the middle language. I wouldn’t recommend e.g. Java (not in-process), Rust should work. It’s possible to call to Julia from it and from C++ (library available to make easier from C++ and another from Rust). Both would be an option to call _from_, but it’s likely easier to call to Python than them.

---

<div class="post-metadata">

### Author: ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)
#### Post date: [March 15, 2023, 9:28pm UTC](https://discourse.julialang.org/t/embedding-julia-code-in-fortran/96010/9 "2023-03-15T21:28:42Z")

</div>

> [@Alexander-Barth](#):
>
> The problem would be C macros and potentially C data structures not suppoted by the Fortran’s C binding. So maybe you need some small C wrapper around your Julia code, that you can call from Fortran.

It’s not mandatory interacting with the C macros or data structures to embed Julia. The approach in [GitHub - GunnarFarneback/DynamicallyLoadedEmbedding.jl: Embed Julia with dynamical loading of libjulia at runtime.](https://github.com/GunnarFarneback/DynamicallyLoadedEmbedding.jl) uses neither of those and minimizes the number of `libjulia` functions called.

---

<div class="post-metadata">

### Author: ![ranocha](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ranocha/32/35588_2.png) [@ranocha](https://discourse.julialang.org/u/ranocha)
#### Post date: [March 16, 2023, 8:18am UTC](https://discourse.julialang.org/t/embedding-julia-code-in-fortran/96010/10 "2023-03-16T08:18:30Z")

</div>

If I remember correctly, @sloede has a working prototype for this.

---

<div class="post-metadata">

### Author: ![sloede](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/sloede/32/44787_2.png) [@sloede](https://discourse.julialang.org/u/sloede)
#### Post date: [March 16, 2023, 8:41am UTC](https://discourse.julialang.org/t/embedding-julia-code-in-fortran/96010/11 "2023-03-16T08:41:11Z")

</div>

Thanks for pinging me, @ranocha!

Yes, we do indeed have a working prototype for calling Julia from Fortran. Since Fortran is not nice (some may call it horrible) when playing with others, I found that for me it was much easier to write a small shim library in C that itself is called from Fortran, and then have the C library interact with Julia.

You can fin examples for pure C-to-Julia usage and Fortran-to-C-to-Julia usage in this repository:

> **[GitHub - sloede/embedding-julia-in-c-examples: Examples for how to use Julia...](https://github.com/sloede/embedding-julia-in-c-examples)**
>
> Examples for how to use Julia from C. Contribute to sloede/embedding-julia-in-c-examples development by creating an account on GitHub.

Please feel free to ping me here (or better yet, in the repository using an issue) if there are questions/problems.
