Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29422 Discussions

How to write Reusable software in modern Fortran

eliosh
Beginner
647 Views
My experience with Fortran is very limited, however, it seems that the more I learn it the more I like the language.
This, in turn, gives me all sort of ideas that could be implemented.

The main question is how to organize the code. Ultimately, the routines should be available for re-use as a static/shared libraries callable from C and Fortran. Producing the libraries is not an issue (I can learn the compiler flags). But the correct way to organize the code is not clear. Should the routines be put inside modules? It seems that most people who write a library do not use modules. However, in this case, calling such a routine from Fortran code would require an "extern" declaration or writing an interface...

In short, if you can share your regular workflow and/or tips on writing re-usable software in Fortran please do not hesitate :)

Thank you.

A real-life example: I would like to implement a framework for numerical optimization. The user will only have to provide the objective function and "press a button". Any ideas on the best way to organize such a framework are welcome.
0 Kudos
1 Reply
Izaak_Beekman
New Contributor II
647 Views

Eliosh: I must admit that I have zero experience building libraries and writing mixed language codes, although I plan to start looking at calling fortran routines from python. (From what I have seen this seems quite easy to do, at least for serial applications.) Therefore I am quite curious to see a proper response to this question.

In my experience writing purely modern fortran (i.e. >= 95) I find that using modules provides a great degree of modularity and re-usability. I typically only write subroutines in modules, and tend to treat a module as an object and the procedures which operate on this object (i.e. the object/data and routines operating on that data are contained in one unit). Also subroutines contained in modules get explicit interfaces automatically which helps in debugging and lets you avoid the verbose interface declarations and additional compiler options like gen-interfaces. From here procedures may be easily overloaded, and it is easy to control the scope of variables and procedures. In addition to limiting scope with PRIVATE/PUBLIC declarations/attributes fortran 2003 has added the PROTECTED attribute which causes variables to be read-only outside of the module. In this way create notional objects and the procedures which operate them and divide them into modules which are very reusable and general. I would love to hear more about compiling these into libraries and mixed language programming. (I believe fortran 2003 has also added features specifically for mixed language applications.)

Good luck!
-Z
0 Kudos
Reply