How can I visualize Fortran (90 or later) source code, e.g. using Graphviz?

11,209

Solution 1

If you have money then Understand for Fortran is worth looking at. If you don't have money but intend to work quickly then you might get by with a trial download of the software.

For a static call graph, I've never found a free tool as useful as Understand; it's hard to find any free tools let alone a useful one. I'd write one myself but the market would be tiny :-(

For a dynamic call graph investigate your compiler options. I use the Intel Fortran Compiler which can generate a mound of useful information about an executing program. The TotalView debugger can also visualise the call graph of an executing program. You should also look at gprof2dot which makes a DOT file out of a GPROF call 'graph'. This is free and OK.

And I should also add, though it's not something I've ever used, that Callgrind may be of use.

Solution 2

I would recommend doxygen, which automatically generates documentation from source code (and is free). Usually you add some markup to comments describing your functions and variables. However, you can just run doxygen on undocumented source files, provided you set EXTRACT_ALL to YES in the configuration file, and have it create create relationship diagrams for all your functions (i.e. this function call these functions and is called by these other functions).

You need GraphViz installed to get diagrams generated and have the HAVE_DOT option set to YES in the configuration file.

See the doxygen documentation for graphs and diagrams for more information and this example class documentation for a example of the output generated.

Edit: Of course for Fortran you should set the OPTIMIZE_FOR_FORTRAN option to YES in the configuration file.

Solution 3

You can use callgrind from within Valgrind:

valgrind --tool=callgrind [your program]

This will produce a callgrind.out.[pid] file. This works best if you compile your program without optimisations, and with debug flags.

You then have a couple of options for viewing the data:

  1. Convert the callgrind output to a .dot file with grof2dot, and then view it with xdot, or convert it to a static graph with GraphViz.
  2. View it directly with Kcachegrind (includes source analysis, and call graphs).
Share:
11,209

Related videos on Youtube

EMiller
Author by

EMiller

Updated on January 14, 2020

Comments

  • EMiller
    EMiller over 4 years

    I've been thrown into a large Fortran project with a large number of source files.

    I need to contribute to this project and it would seem prudent that I first understand the source.

    As a first step, I'd like to visualize the interdependences between the various source files, i.e. which source files need which modules. As far as I can tell, automated methods exist for other languages and result in a graph that can be built using Graphviz.

    But is anyone aware of software out there that can do this for Fortran 90 code?

    [Searching the interwebs for Fortran help is a real pain as you end up searching the inter-cobwebs thanks to the painfully ubiquitous FORTRAN 77.]

  • EMiller
    EMiller almost 14 years
    Thanks for the response. Yeah, Understand for Fortran does seem to be the way to go.
  • ryanjdillon
    ryanjdillon over 10 years
    This method worked for me using the available Ubuntu packages for doxygen and graphviz. The options Chris refers to are found in the Doxyfile configuration file.
  • naught101
    naught101 almost 10 years
    There's also a doxywizard program in the doxygen-ui package that makes things pretty simple
  • netzpurist
    netzpurist over 9 years
    I tried both, doxygen and Understand, and I must say the doxygen does an awesome job. In the configuration file I use: HAVE_DOT=YES, CALL_GRAPH=YES, CALLER_GRAPH=YES, and finally EXTRACT_ALL=YES to include all the Fortran files in the subdirectories.
  • Raphael Roth
    Raphael Roth almost 8 years
    @naught101 It's doxygen-gui not doxygen-ui, at least it's on modern ubuntu distributions
  • daniel.heydebreck
    daniel.heydebreck over 7 years
    For readers new to Doxygen: there is a brief descriptions of Chris' solution available here. Possibly it is copied from here - not sure.
  • Cibin Joseph
    Cibin Joseph over 7 years
    For the sake of information, doxygen does not produce dynamic call graphs, it parses the source code without running it and creates call graphs. For run-time call graphs a debugger or compiler flags are probably the way to go...
  • MarkWayne
    MarkWayne about 4 years
    Thanks for the helpful instructions. I spent some time figuring out that I had to set CALL_GRAPH = YES in the configuration options file. Edit: In fact, this was already pointed out above by @netzpurist but I'll leave this comment in case someone like me doesn't read all the comments.

Related