7

I'm using an extension to Python (2.7.2) written in Fortran (gfortran 4.4.7) compiled via f2py (Ver. 2).

I can profile the Python part with cProfile, but the result does not give any information about the Fortran functions. Instead the time is attributed to the Python function calling the Fortran function.

I have enabled the "-pg -O" flags for all Fortran objects I build, as well as in the f2py call creating the shared object via: f2py --opt="-pg -O" ...

Any hint on how to get the Fortran informations too is highly appreciated.

If anyone uses a similar set-up, with a different profiler, I'd also be interested.

NichtJens
  • 1,709
  • 19
  • 27
  • FWIW, I stopped using f2py for this reason... it is difficult to go in and see what is happening in the Fortran code. The issue is that the Fortran code is wrapped with C code, further muddying the situation. – SethMMorton May 14 '13 at 18:23
  • Maybe you should try it the pythonic way: it is good practice to put test routines in the `if __name__=="__main__":` part of python modules. So I suggest to write a separate Fortran program to profile this part separately. – Stefan May 15 '13 at 06:39
  • A Fortran function call appears as `:84()`. It's true you can't identify which module is being called but it gives you an idea. Another way is to wrap it into a Python function and then see timing for the Python function. – ilciavo Jun 23 '15 at 16:42
  • @ilciavo this is probably the best possible answer. Do you care to make it an answer? I'd probably accept it (considering the time this question has been open ;-)). – NichtJens Jun 24 '16 at 16:50

3 Answers3

2

Have a look at the python extension profiler yep.

Roland Smith
  • 42,427
  • 3
  • 64
  • 94
  • The manual says use `ppref` to read the `file.prof`. Could you please explain briefly how to do that? – ilciavo Jun 23 '15 at 16:46
  • I suspect that the name `pperf` is a typo. It whould probably say `pprof` (from Google's [perftools](http://google-perftools.googlecode.com/svn/trunk/doc/cpuprofile.html)) – Roland Smith Jun 24 '15 at 18:47
  • yep depends on google-perftools which has issue with 64 bits systems. From its doc : "The glibc built-in stack-unwinder on 64-bit systems has some problems with the perftools libraries". For this reason, I could not use it. – Florian Jul 10 '17 at 09:10
1

A Fortran function call appears as:

<ipython-input-51-f4bf36c6a947>:84(<module>). 

I know, you can't identify which module is being called but at least this gives you an idea.

Another way is wrapping it into a Python function and then see timing for the Python function.

ilciavo
  • 3,069
  • 7
  • 26
  • 40
0

This workflow seems to work pretty well :

    1. Use callgrind to profile your code (this generates a file like callgrind.out.27237) :

valgrind --tool=callgrind python my_python_script_calling_f2py_functions.py arg1 arg2

gprof2dot -f callgrind callgrind.out.27237 > callgrind.dot

dot -Tjpg callgrind.dot -o callgrind.jpg

Florian
  • 874
  • 1
  • 8
  • 17