Class for timing analysis of performance critical functions. Some classes export a C++ function as __timing__, which returns a map of performance critical parts with their timings. The class can save these maps, load them and compare them. It can be saved as a benchmark to be compared against.
| 3 | from ngsolve import TaskManager |
| 4 | |
| 5 | class Timing(): |
| 6 | """ |
| 7 | Class for timing analysis of performance critical functions. Some |
| 8 | classes export a C++ function as __timing__, which returns a map |
| 9 | of performance critical parts with their timings. The class can save |
| 10 | these maps, load them and compare them. It can be saved as a benchmark |
| 11 | to be compared against. |
| 12 | |
| 13 | 2 overloaded __init__ functions: |
| 14 | |
| 15 | 1. __init__(name,obj,parallel=True,serial=True) |
| 16 | 2. __init__(filename) |
| 17 | |
| 18 | Parameters |
| 19 | ---------- |
| 20 | |
| 21 | name (str): Name for the timed class (for output formatting and |
| 22 | saving/loading of results) |
| 23 | obj (NGSolve object): Some NGSolve class which has the __timing__ |
| 24 | functionality implemented. Currently supported classes: |
| 25 | FESpace |
| 26 | filename (str): Filename to load a previously saved Timing |
| 27 | parallel (bool=True): Time in parallel (using TaskManager) |
| 28 | serial (bool=True): Time not in parallel (not using TaskManager) |
| 29 | |
| 30 | """ |
| 31 | def __init__(self,name=None,obj=None,filename=None,parallel=True,serial=True): |
| 32 | assert (not name and not obj and filename) or (name and obj and not filename) |
| 33 | if filename: |
| 34 | myself = pickle.load(open(filename,"rb")) |
| 35 | self.timings = myself.timings |
| 36 | self.name = myself.name |
| 37 | self.timings_par = myself.timings_par |
| 38 | else: |
| 39 | if serial: |
| 40 | self.timings = obj.__timing__() |
| 41 | else: |
| 42 | self.timings = None |
| 43 | if parallel: |
| 44 | with TaskManager(): |
| 45 | self.timings_par = obj.__timing__() |
| 46 | else: |
| 47 | self.timings_par = None |
| 48 | self.name = name |
| 49 | |
| 50 | def __str__(self): |
| 51 | string = "Timing for " + self.name + ":" |
| 52 | if self.timings: |
| 53 | for key, value in self.timings: |
| 54 | string += "\n" + key + ": " + value |
| 55 | if self.timings_par: |
| 56 | for key, value in self.timings_par: |
| 57 | string += "\n" + key + " parallel: " + value |
| 58 | return string |
| 59 | |
| 60 | def Save(self, folder): |
| 61 | """ Saves the pickled results in folder 'folder' """ |
| 62 | if not os.path.exists(folder): |
no outgoing calls
no test coverage detected