Skip to content

Commit f04473f

Browse files
committed
#617: tmp fix: add timer to all methods and use comm param
1 parent 9615e7a commit f04473f

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

src/lbaf/IO/lbsVTDataWriter.py

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@
4545
import os
4646
import sys
4747
import math
48+
import time
49+
import functools
50+
4851
from logging import Logger
4952
from typing import Optional
5053

@@ -54,6 +57,16 @@
5457
from ..Model.lbsRank import Rank
5558
from ..Model.lbsObject import Object
5659

60+
def timer(method):
61+
@functools.wraps(method)
62+
def wrapper(self, *args, **kwargs):
63+
start = time.time()
64+
res = method(self, *args, **kwargs)
65+
end = time.time()
66+
dur = end - start
67+
self._VTDataWriter__logger.info(f"{method.__name__}: {dur:.4f} s")
68+
return res
69+
return wrapper
5770

5871
class VTDataWriter:
5972
"""A class to write load directives for VT as JSON files
@@ -94,11 +107,13 @@ def __init__(
94107
try:
95108
self.__extension = parameters["json_output_suffix"]
96109
self.__compress = parameters["compressed"]
110+
self.__add_communications = parameters["communications"]
97111
except Exception as e:
98112
self.__logger.error(
99113
f"Missing JSON writer configuration parameter(s): {e}")
100114
raise SystemExit(1) from e
101115

116+
@timer
102117
def __create_tasks(self, rank_id, objects, migratable):
103118
"""Create per-object entries to be outputted to JSON."""
104119
tasks = []
@@ -145,6 +160,7 @@ def __create_tasks(self, rank_id, objects, migratable):
145160
# Return created tasks on this rank
146161
return tasks
147162

163+
@timer
148164
def __create_task_data(self, rank: Rank):
149165
"""Create task data."""
150166
return sorted(
@@ -155,6 +171,7 @@ def __create_task_data(self, rank: Rank):
155171
key=lambda x: x.get("entity").get(
156172
"id", x.get("entity").get("seq_id")))
157173

174+
@timer
158175
def __find_object_rank(self, phase: Phase, obj: Object):
159176
"""Determine which rank owns the object."""
160177
for r in phase.get_ranks():
@@ -166,6 +183,7 @@ def __find_object_rank(self, phase: Phase, obj: Object):
166183
f"Object id {object} cannot be located in any rank of phase {phase.get_id()}")
167184
raise SystemExit(1)
168185

186+
@timer
169187
def __get_communications(self, phase: Phase, rank: Rank):
170188
"""Create communication entries to be outputted to JSON."""
171189

@@ -254,6 +272,7 @@ def __get_communications(self, phase: Phase, rank: Rank):
254272
# Return created list of communications
255273
return communications
256274

275+
@timer
257276
def _json_serializer(self, rank_phases_double) -> str:
258277
"""Write one JSON per rank for list of phase instances."""
259278
# Unpack received double
@@ -305,9 +324,10 @@ def _json_serializer(self, rank_phases_double) -> str:
305324
phase_data["user_defined"]["num_homed_ratio"] = homed_ratio
306325

307326
# Add communication data if present
308-
communications = self.__get_communications(current_phase, rank)
309-
if communications:
310-
phase_data["communications"] = communications
327+
if self.__add_communications:
328+
communications = self.__get_communications(current_phase, rank)
329+
if communications:
330+
phase_data["communications"] = communications
311331

312332
# Add load balancing iterations if present
313333
lb_iterations = current_phase.get_lb_iterations()
@@ -343,9 +363,10 @@ def _json_serializer(self, rank_phases_double) -> str:
343363
iteration_data["user_defined"]["num_homed_ratio"] = homed_ratio
344364

345365
# Add communication data if present
346-
communications = self.__get_communications(it, it_r)
347-
if communications:
348-
iteration_data["communications"] = communications
366+
if self.__add_communications:
367+
communications = self.__get_communications(it, it_r)
368+
if communications:
369+
iteration_data["communications"] = communications
349370

350371
# Append load balancing iteration to phase data
351372
phase_data["lb_iterations"].append(iteration_data)
@@ -357,6 +378,7 @@ def _json_serializer(self, rank_phases_double) -> str:
357378
serial_json = json.dumps(output, separators=(',', ':'))
358379
return serial_json
359380

381+
@timer
360382
def _json_writer(self, rank_phases_double) -> str:
361383
"""Write one JSON per rank for list of phase instances."""
362384
# Unpack received double
@@ -378,6 +400,7 @@ def _json_writer(self, rank_phases_double) -> str:
378400
# Return JSON file name
379401
return file_name
380402

403+
@timer
381404
def write(self, phases: dict):
382405
""" Write one JSON per rank for dictonary of phases with possibly iterations."""
383406

0 commit comments

Comments
 (0)