Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
gay committed Nov 2, 2023
2 parents ad33e01 + 9721d0f commit a40877c
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
1 change: 0 additions & 1 deletion deep_learning_power_measure/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ def main():
# parse sys argv to obtain the output folder, and the period
output_folder = args.output_folder
cmd = args.cmd
period = 2
driver = parsers.JsonParser(output_folder)
exp = experiment.Experiment(driver)
p, q = exp.measure_yourself(period=args.period, measurement_period=args.measurement_period)
Expand Down
44 changes: 34 additions & 10 deletions deep_learning_power_measure/power_measure/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,14 @@ def integrate(metric, start=None, end=None, allow_None=False):
while metric[start_idx]['date'] < start:
start_idx += 1
if start_idx == len(metric):
raise Exception('period start time given in parameter is : ' + str(start) + ' and the metric starts at: ' +str(metric[0]['date']))
raise Exception('period start time given in parameter is : ' + str(start) + ' and the metric ends at: ' +str(metric[-1]['date']))

end_idx = len(metric)-1
if end != None:
while end < metric[end_idx]['date']:
end_idx -= 1
if end_idx < 0:
raise Exception('period end time given in parameter is : ' + str(end) + ' and the metric stops at: ' +str(metric[-1]['date']))
raise Exception('period end time given in parameter is : ' + str(end) + ' and the metric starts at: ' +str(metric[0]['date']))

for i in range(start_idx, end_idx):
x1 = metric[i]['date']
Expand All @@ -69,24 +69,48 @@ def integrate(metric, start=None, end=None, allow_None=False):
def get_usage_duration(curve):
pass

def is_iou(s1,e1,s2,e2):
return s2 < e1 and s1 < e2

def total(metric: list, start=None, end=None):
"""Return the integration over time for the metric. For instance if the metric is in watt and the time in seconds,
the return value is the energy consumed in Joules
the return value would be the total consumed energy in joules
Input:
- list a list containing different segments, each segment is a list where one item is a dictionnary with keys 'date' and 'value'f_____
"""
# case one, metric is directly a list of temporal segments
if isinstance(metric, list):
rs = [ integrate(segment,start=start,end=end) for segment in metric ]
if rs[0] is not None:
return sum([ r[-1] for r in rs])
rs = [] # store the total for all the temporal segments
for segment in metric:
# check if this temporal segment intersect the given start and end parameter
if is_iou(segment[0]['date'],segment[-1]['date'], start, end):
# if so, compute the integral
integral = integrate(segment,start=start,end=end)[-1]
else:
# if there is no intersect, say the metric value is 0
integral = 0
if integral is not None:
rs.append(integral)
if len(rs)>0:
# get the sum for all the segments
return sum(rs)
# case two, metric is a dictionnary where key is a device and value is a list of segments
elif isinstance(metric, dict):
totals = {}
for device_id, segments in metric.items():
rs = [ integrate(segment, start=start, end=end) for segment in segments ]
if rs is not None:
r = sum([ r[-1] for r in rs])
totals[device_id] = r
# and then, same as case one
rs = []
for segment in segments:
if is_iou(segment[0]['date'],segment[-1]['date'], start, end):
integral = integrate(segment,start=start,end=end)[-1]
else:
integral = 0
if integral is not None:
rs.append(integral)
if len(rs)>0:
totals[device_id] = sum(rs)
else:
# if no segments for this device, set None value
totals[device_id] = None
return totals

Expand Down

0 comments on commit a40877c

Please sign in to comment.