MCPcopy Create free account
hub / github.com/mlco2/codecarbon / TDP

Class TDP

codecarbon/core/cpu.py:868–1019  ·  view source on GitHub ↗

Represents Thermal Design Power (TDP) for detecting and estimating the power consumption of the CPU on a machine. The class provides methods to identify the CPU model, match it with known TDP values from a dataset, and return the corresponding power consumption in watts. Attri

Source from the content-addressed store, hash-verified

866
867
868class TDP:
869 """
870 Represents Thermal Design Power (TDP) for detecting and estimating
871 the power consumption of the CPU on a machine.
872
873 The class provides methods to identify the CPU model, match it with known TDP
874 values from a dataset, and return the corresponding power consumption in watts.
875
876 Attributes:
877 model (str): The detected CPU model name.
878 tdp (int): The TDP value of the detected CPU in watts.
879
880 Methods:
881 start():
882 Placeholder method to initiate TDP analysis.
883
884 """
885
886 def __init__(self):
887 self.model, self.tdp = self._main()
888
889 @staticmethod
890 def _get_cpu_constant_power(match: str, cpu_power_df: pd.DataFrame) -> int:
891 """Extract constant power from matched CPU"""
892 return float(cpu_power_df[cpu_power_df["Name"] == match]["TDP"].values[0])
893
894 def _get_cpu_power_from_registry(self, cpu_model_raw: str) -> Optional[int]:
895 cpu_power_df = DataSource().get_cpu_power_data()
896 cpu_matching = self._get_matching_cpu(cpu_model_raw, cpu_power_df)
897 if cpu_matching:
898 power = self._get_cpu_constant_power(cpu_matching, cpu_power_df)
899 return power
900 return None
901
902 def _get_matching_cpu(
903 self, model_raw: str, cpu_df: pd.DataFrame, greedy=False
904 ) -> str:
905 """
906 Get matching cpu name
907
908 :args:
909 model_raw (str): raw name of the cpu model detected on the machine
910
911 cpu_df (DataFrame): table containing cpu models along their tdp
912
913 greedy (default False): if multiple cpu models match with an equal
914 ratio of similarity, greedy (True) selects the first model,
915 following the order of the cpu list provided, while non-greedy
916 returns None.
917
918 :return: name of the matching cpu model
919
920 :notes:
921 Thanks to the greedy mode, even though the match could be a model
922 with a tdp very different from the actual tdp of current cpu, it
923 still enables the relative comparison of models emissions running
924 on the same machine.
925

Calls

no outgoing calls