Skip to content

Commit

Permalink
Merge pull request #68 from powerapi-ng/refactor/cpu-topology-freq-type
Browse files Browse the repository at this point in the history
refactor: Use integer instead of float for CPU topology frequencies
  • Loading branch information
gfieni authored Oct 5, 2023
2 parents 719f13e + 506f36d commit 9432683
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 23 deletions.
10 changes: 5 additions & 5 deletions src/smartwatts/model/cpu_topology.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class CPUTopology:
This class stores the necessary information about the CPU topology.
"""

def __init__(self, tdp: int, freq_bclk: float, ratio_min: int, ratio_base: int, ratio_max: int):
def __init__(self, tdp: int, freq_bclk: int, ratio_min: int, ratio_base: int, ratio_max: int):
"""
Create a new CPU topology object.
:param tdp: TDP of the CPU in Watt
Expand All @@ -52,28 +52,28 @@ def __init__(self, tdp: int, freq_bclk: float, ratio_min: int, ratio_base: int,
self.ratio_base = ratio_base
self.ratio_max = ratio_max

def get_min_frequency(self) -> float:
def get_min_frequency(self) -> int:
"""
Compute and return the CPU max efficiency frequency.
:return: The CPU max efficiency frequency in MHz
"""
return self.freq_bclk * self.ratio_min

def get_base_frequency(self) -> float:
def get_base_frequency(self) -> int:
"""
Compute and return the CPU base frequency.
:return: The CPU base frequency in MHz
"""
return self.freq_bclk * self.ratio_base

def get_max_frequency(self) -> float:
def get_max_frequency(self) -> int:
"""
Compute and return the CPU maximum frequency. (Turbo-Boost included)
:return: The CPU maximum frequency in MHz
"""
return self.freq_bclk * self.ratio_max

def get_supported_frequencies(self) -> List[float]:
def get_supported_frequencies(self) -> List[int]:
"""
Compute the supported frequencies for this CPU.
:return: A list of supported frequencies in MHz
Expand Down
4 changes: 2 additions & 2 deletions src/smartwatts/model/power_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ class PowerModel:
This Power model compute the power estimations and handle the learning of a new model when needed.
"""

def __init__(self, frequency: float, history_window_size: int):
def __init__(self, frequency: int, history_window_size: int):
"""
Initialize a new power model.
:param frequency: Frequency of the power model
:param frequency: Frequency of the power model (in MHz)
:param history_window_size: Size of the history window used to keep samples to learn from
"""
self.frequency = frequency
Expand Down
32 changes: 16 additions & 16 deletions tests/unit/model/test_cpu_topology.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright (c) 2023, INRIA
# Copyright (c) 2023, Inria
# Copyright (c) 2023, University of Lille
# All rights reserved.
#
Expand Down Expand Up @@ -35,31 +35,31 @@ def test_cpu_topology_amd_epyc_7351():
Test the CPUTopology class with an AMD EPYC 7351 CPU configuration.
https://www.amd.com/en/support/cpu/amd-epyc/amd-epyc-7001-series/amd-epyc-7351
"""
cpu_topology = CPUTopology(155, 100.0, 12, 24, 29)
assert cpu_topology.get_min_frequency() == 1200.0
assert cpu_topology.get_base_frequency() == 2400.0
assert cpu_topology.get_max_frequency() == 2900.0
assert cpu_topology.get_supported_frequencies() == [ratio * 100.0 for ratio in range(12, 29 + 1)]
cpu_topology = CPUTopology(155, 100, 12, 24, 29)
assert cpu_topology.get_min_frequency() == 1200
assert cpu_topology.get_base_frequency() == 2400
assert cpu_topology.get_max_frequency() == 2900
assert cpu_topology.get_supported_frequencies() == [ratio * 100 for ratio in range(12, 29 + 1)]


def test_cpu_topology_intel_xeon_gold_5220():
"""
Test the CPUTopology class with an Intel Xeon Gold 5220 CPU configuration.
https://www.intel.com/content/www/us/en/products/sku/193388/intel-xeon-gold-5220-processor-24-75m-cache-2-20-ghz/
"""
cpu_topology = CPUTopology(125, 100.0, 10, 22, 39)
assert cpu_topology.get_min_frequency() == 1000.0
assert cpu_topology.get_base_frequency() == 2200.0
assert cpu_topology.get_max_frequency() == 3900.0
assert cpu_topology.get_supported_frequencies() == [ratio * 100.0 for ratio in range(10, 39 + 1)]
cpu_topology = CPUTopology(125, 100, 10, 22, 39)
assert cpu_topology.get_min_frequency() == 1000
assert cpu_topology.get_base_frequency() == 2200
assert cpu_topology.get_max_frequency() == 3900
assert cpu_topology.get_supported_frequencies() == [ratio * 100 for ratio in range(10, 39 + 1)]


def test_cpu_topology_with_133mhz_base_clock():
"""
Test the CPUTopology class with a base clock of 133MHz.
"""
cpu_topology = CPUTopology(0, 133.0, 10, 20, 30)
assert cpu_topology.get_min_frequency() == 1330.0
assert cpu_topology.get_base_frequency() == 2660.0
assert cpu_topology.get_max_frequency() == 3990.0
assert cpu_topology.get_supported_frequencies() == [ratio * 133.0 for ratio in range(10, 30 + 1)]
cpu_topology = CPUTopology(0, 133, 10, 20, 30)
assert cpu_topology.get_min_frequency() == 1330
assert cpu_topology.get_base_frequency() == 2660
assert cpu_topology.get_max_frequency() == 3990
assert cpu_topology.get_supported_frequencies() == [ratio * 133 for ratio in range(10, 30 + 1)]

0 comments on commit 9432683

Please sign in to comment.