Skip to content

Minibook: Interfacing Fortran with Python #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
awvwgk opened this issue Apr 27, 2021 · 13 comments
Open

Minibook: Interfacing Fortran with Python #105

awvwgk opened this issue Apr 27, 2021 · 13 comments
Labels
section: learn Relevant for the learn section on the webpage

Comments

@awvwgk
Copy link
Member

awvwgk commented Apr 27, 2021

Tracking issue for creating a minibook on language intercompatibility of Fortran with Python.

This is a non-comprehensive list of method to interface Fortran and Python, feel free to edit this list:

  • ctypes: dynamic loader for C libraries
  • cffi: Python wrapper generator for C
  • SWIG: Python wrapper generator for C
  • numpy.f2py: wrapper generator for Fortran
  • cython: compiler for Python C-extensions
  • fwrap: wrapper generator for Fortran
  • forpy: bindings to CPython
  • f90wrap: wrapper generator for Fortran
  • fmodpy: wrapper generator for Fortran
  • F2x: template-based wrapper
  • Forthon: wrapper generator for Fortran
  • gfort2py

Aspects to cover:

  • ease of use, constraints (bind(C) requirement, derived type support, finalization, ...)
  • memory management, function callbacks
  • build system integration (compiling of extension modules, distribution, ...)
  • stability (frequent tool chain changes), portability (platforms, Python implementations), runtime overhead
  • integration with OpenMP, MPI, CAF (mpi4py, ...)
@awvwgk awvwgk added the learn label Apr 27, 2021
@Beliavsky
Copy link
Contributor

Beliavsky commented Apr 29, 2021

The Fortran Wiki has a Python page listing many of the packages above and few others.

Fortran should subsume other languages, not merely interoperate with them :).

But seriously, one way to speed up Python with Fortran is to translate it to Fortran with Pyccel, an active project.

@ivan-pi
Copy link
Member

ivan-pi commented Apr 29, 2021

I've added a few from the Python wiki. It would be interesting to learn what the standards committee thinks of this. There seems to be huge interest in using Fortran from Python (I suppose either for reasons of calling legacy code, or speeding up critical parts).

Personally, I've only ever used the Cython route. On Linux it worked very well, but on Windows I struggled with binary compatibility issues. It's not fully clear to me how the people who support numpy and SciPy manage to pull it off. I know I've read some interesting blog posts about it, but couldn't find them anymore.

@ivan-pi
Copy link
Member

ivan-pi commented May 2, 2021

Another route is to use pybind11 and follow the path Fortran --> C --> C++ --> Python. It was demonstrated by @hsnyder on Discourse: https://fortran-lang.discourse.group/t/iso-c-binding-pass-an-array-from-c-to-fortran-edit-python-interop-content/514/8

@kjelljorner
Copy link

Following from the discussion here, I would be interested in contributing to this. As I noted in the last post there, I think it would be useful to work on this in a real-time collaborative markdown environment (e.g. GitBook) at first and then transfer to fork/pull request route once its more finalised.

I see different levels of complexity and different use cases which could be covered here. For something on Fortran-lang.org I argue that focusing on established and well-maintained tools is the way to go. There are many packages out there that are either abandoned or have rapidly changing APIs. As I see it, there are two practical approaches to accessing Fortran code from Python:

  • Explicitly write explicit C interface, build shared library and access via ctypes, cffi, Cython etc.
  • Use interface generators which take care of everything under the hood and directly produces importable Python objects. Here the most mature would be f2py which is used in production-grade code like numpy and scipy.

Code generators like Pyccel, Numba, Cython are in my view a bit of a different topic as their main goal is to enable users to write Python-like code that runs faster and not to actually generate Fortran code that could be used for something else. The user would be fairly agnostic whether it is Fortran, C or something else that is generated under the hood.

@hsnyder
Copy link

hsnyder commented May 24, 2021

I’m actively working on mixed Python-Fortran projects with work right now.. I’ll keep this thread in mind and share anything I produce that I think might be useful. At the moment what I’m doing involves fortran calling python, rather than the opposite way around. It will be a few days before it’s useable outside my immediate application though.

@kjelljorner
Copy link

I made some prototype page on HackMD. If this way of working looks attractive to people I could create a "team" and we could create the minibook there.

@awvwgk
Copy link
Member Author

awvwgk commented May 25, 2021

Sure, I would be happy to contribute an example with intermediate complexity involving (polymorphic) derived types.

@LKedward
Copy link
Member

Thanks @kjelljorner! That looks like a nice way of working on the minibook

@kjelljorner
Copy link

kjelljorner commented May 25, 2021

I created a space on HackMD to work on this. It can be accessed only if you login, while the individual notes themselves can be shared to anyone. @awvwgk and others, If you create an account and let me know the username I can add you to the team as admins. There's a quite nice comments function that we can use to discuss different parts of the text.

@awvwgk
Copy link
Member Author

awvwgk commented May 25, 2021

If you create an account and let me know the username I can add you to the team as admins.

Thanks, I'm going under @awvwgk there as well.

@kjelljorner
Copy link

I'm currently working on a Python interface to a legacy Fortran code, involving sending Python callback functions to Fortran. Also considering the options for multi-platform packaging on PyPi using scikit-build + cmake and cibuildwheel. Maybe would be a good time to try to revive this effort @awvwgk?

@awvwgk
Copy link
Member Author

awvwgk commented Dec 6, 2021

I currently have shifted my priorities from the Fortran-lang webpage to the fpm documentation (see https://awvwgk.github.io/fpm-docs). Hopefully, the framework explored there will find adoption for other Fortran-lang resources in the future (stdlib? minibooks?), especially since we now actually have first class internationalization support there.

@kjelljorner
Copy link

I've seen that - it looks really good! I might add some of the work on callbacks and error handling that I have been investigating over the next few weeks and then see if there is anyone on the Discourse that wants to join in.

@awvwgk awvwgk added section: learn Relevant for the learn section on the webpage and removed learn labels Aug 19, 2022
@awvwgk awvwgk transferred this issue from fortran-lang/fortran-lang.org Aug 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
section: learn Relevant for the learn section on the webpage
Projects
None yet
Development

No branches or pull requests

6 participants