sofa-framework / sofa

Real-time multi-physics simulation with an emphasis on medical simulation.
https://www.sofa-framework.org
GNU Lesser General Public License v2.1
924 stars 311 forks source link

tracing execution time during simulation in python #336

Closed untereiner closed 6 years ago

untereiner commented 7 years ago

To trace execution time I wanted to bind in python the advanced timer class to be able to retrieve it and interogate it during a simulation. (without using -c option of runSofa) I wanted also to change the format of the output. For now my binding does not work. Can static methods be binded ?

I think your are the python master @damienmarchal. Do you have any idea on how I can do ?


Suggested labels:

damienmarchal commented 7 years ago

Hi @untereiner

I'm far from a python master, but thanks for the greeting ;) I don't know if static method can be binded directly. But I'm sure that if it is not possible you can create a static function that forward to the static method. So it should work...if you make a branch somewhere (or a PR with the WIP tag someone can probably help you to locate the problem).

untereiner commented 7 years ago

We have a branch with our work on https://github.com/mimesis-inria/sofa/tree/wip_pythonAdvancedTimer We try to create a class which wrap the advanced timer and that can be binded using Python... without success so far.

maxime-tournier commented 7 years ago

Can static methods be binded ?

If your question is: "can c++ static methods be bound in python", then yes. If your question is "can I bind something as a python class/static method", I think the answer is yes but in any case you can wrap it as a free function in a module.

If applicable, I suggest your binding class follows the context protocol (https://www.python.org/dev/peps/pep-0343/) so that you can time code fragments using with statements:

with Sofa.Timer('description'):
     # do something time-consuming here

In this case, you need to:

  1. create the type object and store needed state, in this case that would be a simple string for identifying the timed action
  2. bind beginStep as the __enter__ method
  3. bind endStep as the __exit__ method

and that should be it.

damienmarchal commented 7 years ago

+1 for the with statement.

untereiner commented 7 years ago

Thank you for your comments! I will try to follow this pep. For now I created a class and bind the static functions of AdvancedTimer as free functions in this but I have an undefined reference at linking to the class I created `AdvancedTimer_PyTypeObject'

guparan commented 7 years ago

Should we close this issue @untereiner ?

damienmarchal commented 7 years ago

If the issue is solved with the merge of the AdvnacedTimer branch. Could you close the issue.