It can be useful to produce a call graph of your code. For each routine, this shows which routines that routine calls, and which routines it is called by. This is also referred to as a call stack. There are several ways to obtain a textual representation and a few ways to obtain a visual representation.
Debugger
For example, use the GNU compiler and compile with the "-g" flag. You can then run your executable through the GNU debugger (gdb). Normally the code will run to completion but to see the stack at any point in time you can interrupt the debugger (CNTL-C) and then use the where command to see the stack. This will show you, typically, a "main" routine (or name of your program entry) and a linear path of routines called leading to the current breakpoint. You can also set breakpoints. For a fuller discussion of debuggers, see our Debugger page.
gprof
Depending upon your compiler version, your mileage will vary a lot with this as gprof is phased out in favour of gcov. However, older versions of GNU compilers support the -pg flag. Executables built with this produce a gmon.out file when they are run and this can be viewed with gprof to give a textual call graph (as well as the profiling data)
Intel VTune
VTune is typically used to profile codes but provides options to list routines and their caller and/or callees. It does not natively provide a graphical call graph. However, you can make use of José Fonseca's gprof2dot facility as outlined below. Note that there are various options that gprof2dot takes which control which nodes to show (or not).
- download and install Intel VTune
- run "vtune collect" to collect profiling data for the application executable "app.exe":
vtune -collect hotspots -result-dir vtuneResults -- ./app.exe
Note that you can include a parameter list to app.exe if required. See below for discussion of MPI
- use "vtune report" to convert the vtune results to a text file:
vtune -report gprof-cc -result-dir vtuneResults -format text -report-output vtuneProfile.txt
This creates a text file vtuneProfile.txt which is very similar to the gprof profile format. - convert the text profile into a graphviz (aka "dot") format:
gprof2dot -f axe vtuneProfile.txt -o vtuneProfile.dot
which can then be viewed using
xdot vtuneProfile.dot
(Alternatively, can pipe gprof2dot output to dot to create a PNG file:
gprof2dot -f axe vtuneProfile.txt | dot -Tpng -o vtuneProfile.png
To use VTune with MPI, the recommended deployment is to have mpirun launch vtune profiling of the application/ For example to profile running "app.exe" on 4 processes:
mpirun -np 4 vtune -collect hotspots -result-dir vtuneResults -- ./app.exe
Note that you can include a parameter list to app.exe if required. Note that the actual results directory that VTune creates mat be "vtuneResults.$HOSTNAME" (for each host that MPI process run on)
(You can also use the vtune option -trace-mpi as required)
Valgrind and Callgrind
valgrind is a very useful tool for exploring the memory characteristics of a code, particularly checking for memory leaks or misuses of memory. valgrind has a "tool" called callgrind that is a "a call-graph generating cache and branch prediction profiler" according to the official callgrind documentation.
To run callgrind for an MPI code "app.exe" using 4 processes, for example:
mpirun -np 4 valgrind --tool=callgrind ./app.exe
This creates an callgrind.out.$PID file (where PID is the process ID of the MPI rank) for each process (MPI rank or OpenMP thread). You can use the -callgrind-out-file option to callgrind if you wish to rename the output files (and can use "%p" to denote PID value.
Once the callgrind output files have been created, run kcachegrind on the relevant output files from the valgrind/callgrind run. For example,
kcachegrind callgrind.out.* will produce graphical callgraphs (and more) for all the callgrind.out files in the current working directory.
While this approach gives a nicer interface to the callgraph than using vtune, it is more intrusive with the binary taking 2-10x longer to run.