Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

STL analogue in Fortran

Tags:

c++

stl

fortran

Basis: i have very big parallel Fortran90/MPI program which represent complex physical model. I want to add new functionality to it: for example, i need to organize queue of messages, introduce mergesort somewhere and use hash tables.

Problem: i know how write hash table, create queue and code mergesort by my self, but i don't think it is a good idea to invent a bicycle.

Question: what Fortran guru should do in such situation? Should i build binds to C++ classes from Fortran and realize logic there using STL or you can suggest some Fortran STL-like libraries? Thank you.

like image 990
vovo Avatar asked Jul 27 '14 08:07

vovo


2 Answers

There are no templates in Fortran and hence no STL. You can try FLIBS for some generic libraries. It generally uses transfer() tricks to achieve generic programming.

There is a preprocessor which adds some templates to Fortran and comes with some small STL, you can try that too named PyF95++. If you have access to academic papers through some library, you can read about it here.

I would avoid mixing it with C++ in this specific case although it can be done. You must instantiate each case separately and interface it to Fortran using a wrapper (bind(C) and iso_c_binding). Only if you have a very limited number of types you want to use the algorithms for it could be worth it.

You can also try to implement some poor-man's templates using the C-preprocessors in Fortran, For smaller libraries it works, but can become too difficult to maintain or ugly for complex things. As an example you can see my implementation of a linked list https://github.com/LadaF/fortran-list .

Generally, there is no clearly right approach or answer, you always have to choose from more possibilities.

like image 176
Vladimir F Героям слава Avatar answered Oct 06 '22 00:10

Vladimir F Героям слава


In addition to everything that Vladimir F already mentioned, there is now also the Fortran Template Library (FTL). Much of the FTL is a reimplementation of C++'s STL in Fortran, where the C preprocessor is abused for template instantiation. You have to instatiate your templates manually, but otherwise it should be quite convenient from the end user perspective. The documentation can be found here.

The library is still fairly new and while it comes with a lot of unit tests, it hasn't seen much use in the wild yet. You'll also need a very recent Fortran compiler to use it.

Disclaimer: I am the author of this library.

like image 45
Robert Rüger Avatar answered Oct 05 '22 22:10

Robert Rüger