-
Notifications
You must be signed in to change notification settings - Fork 10
Added energy consumption to Skyline #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ | |
|
||
import skyline.protocol_gen.innpv_pb2 as pm | ||
from skyline.analysis.static import StaticAnalyzer | ||
from skyline.energy.measurer import EnergyMeasurer | ||
from skyline.exceptions import AnalysisError, exceptions_as_analysis_errors | ||
from skyline.profiler.iteration import IterationProfiler | ||
from skyline.tracking.tracker import Tracker | ||
|
@@ -131,6 +132,38 @@ def new_from(cls, project_root, entry_point): | |
StaticAnalyzer(entry_point_code, entry_point_ast), | ||
) | ||
|
||
def energy_compute(self) -> pm.EnergyResponse: | ||
energy_measurer = EnergyMeasurer() | ||
|
||
model = self._model_provider() | ||
inputs = self._input_provider() | ||
iteration = self._iteration_provider(model) | ||
resp = pm.EnergyResponse() | ||
try: | ||
energy_measurer.begin_measurement() | ||
iterations = 20 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe we can declare this variable outside the try block There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the reason that it should be outside the try block? I'm not sure I understand why There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it will be easier to read if all the variables are declared at the beginning of the function. At least the ones that can be |
||
for _ in range(iterations): | ||
iteration(*inputs) | ||
energy_measurer.end_measurement() | ||
|
||
resp.total_consumption = energy_measurer.total_energy()/float(iterations) | ||
|
||
cpu_component = pm.EnergyConsumptionComponent() | ||
cpu_component.component_type = pm.ENERGY_CPU_DRAM | ||
cpu_component.consumption_joules = energy_measurer.cpu_energy()/float(iterations) | ||
|
||
gpu_component = pm.EnergyConsumptionComponent() | ||
gpu_component.component_type = pm.ENERGY_NVIDIA | ||
gpu_component.consumption_joules = energy_measurer.gpu_energy()/float(iterations) | ||
|
||
resp.components.extend([cpu_component, gpu_component]) | ||
|
||
#TODO save each response to a database and get the past energy measurements | ||
except PermissionError as err: | ||
# Remind user to set their CPU permissions | ||
print(err) | ||
return resp | ||
|
||
def habitat_compute_threshold(self, runnable, context): | ||
tracker = habitat.OperationTracker(context.origin_device) | ||
with tracker.track(): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import time | ||
from threading import Thread | ||
import numpy as np | ||
|
||
import pynvml as N | ||
from pyRAPL import Sensor | ||
|
||
class CPUMeasurer: | ||
def __init__(self, interval): | ||
self.interval = interval | ||
self.power = [] | ||
self.last_cpu = None | ||
self.last_dram = None | ||
|
||
def measurer_init(self): | ||
self.sensor = Sensor() | ||
energy = self.sensor.energy() | ||
self.last_cpu = np.array(energy[0::2]) | ||
self.last_dram = np.array(energy[1::2]) | ||
|
||
def measurer_measure(self): | ||
# Get energy consumed so far (since last CPU reset) | ||
energy = self.sensor.energy() | ||
cpu = np.array(energy[0::2]) | ||
dram = np.array(energy[1::2]) | ||
|
||
# Compare against last measurement to determine energy since last measure | ||
diff_cpu = cpu - self.last_cpu | ||
diff_dram = dram - self.last_dram | ||
|
||
# 1J = 10^6 uJ | ||
# The cpu used this much since the last measurement | ||
# We have mW = 1000*J/s = 1000*(uJ/10^6)/s | ||
cpu_total = np.sum(diff_cpu) | ||
cpu_mW = 1000 * (cpu_total / 1e6) / self.interval | ||
self.power.append(cpu_mW) | ||
|
||
self.last_cpu = cpu | ||
self.last_dram = dram | ||
|
||
def measurer_deallocate(self): | ||
pass | ||
|
||
def total_energy(self): | ||
# J = W * s, 1W = 1000 mW | ||
energy = self.interval * sum(self.power) / 1000.0 | ||
return energy | ||
|
||
class GPUMeasurer: | ||
def __init__(self, interval): | ||
self.interval = interval | ||
self.power = [] | ||
|
||
def measurer_init(self): | ||
N.nvmlInit() | ||
self.device_handle = N.nvmlDeviceGetHandleByIndex(0) | ||
|
||
def measurer_measure(self): | ||
power = N.nvmlDeviceGetPowerUsage(self.device_handle) | ||
self.power.append(power) | ||
|
||
def measurer_deallocate(self): | ||
N.nvmlShutdown() | ||
|
||
def total_energy(self): | ||
# J = W * s, 1W = 1000 mW | ||
energy = self.interval * sum(self.power) / 1000.0 | ||
return energy | ||
|
||
class EnergyMeasurer: | ||
def __init__(self): | ||
self.sleep_interval = 0.1 | ||
self.measuring = False | ||
self.measure_thread = None | ||
|
||
self.measurers = { | ||
"cpu": CPUMeasurer(self.sleep_interval), | ||
"gpu": GPUMeasurer(self.sleep_interval), | ||
} | ||
|
||
def run_measure(self): | ||
# Initialize | ||
for m in self.measurers: | ||
self.measurers[m].measurer_init() | ||
|
||
# Run measurement loop | ||
while self.measuring: | ||
for m in self.measurers: | ||
self.measurers[m].measurer_measure() | ||
time.sleep(self.sleep_interval) | ||
|
||
# Cleanup | ||
for m in self.measurers: | ||
self.measurers[m].measurer_deallocate() | ||
|
||
def begin_measurement(self): | ||
assert(self.measure_thread is None) | ||
self.measure_thread = Thread(target=self.run_measure) | ||
self.measuring = True | ||
self.measure_thread.start() | ||
|
||
def end_measurement(self): | ||
self.measuring = False | ||
self.measure_thread.join() | ||
self.measure_thread = None | ||
|
||
def total_energy(self): | ||
total_energy = 0. | ||
for m in self.measurers: | ||
total_energy += self.measurers[m].total_energy() | ||
return total_energy | ||
|
||
def cpu_energy(self): | ||
return self.measurers["cpu"].total_energy() | ||
|
||
def gpu_energy(self): | ||
return self.measurers["gpu"].total_energy() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should it be an error instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes makes sense. I'll update the other conditions as well