base, python: Add a Temperature type and associated param

Add a class to represent a temperature. The class stores temperatures
in Kelvin and provides helper methods to convert to/from Celsius. The
corresponding param type automatically converts from Kelvin, Celsius,
and Fahrenheit to the underlying C++ type.

Change-Id: I5783cc4f4fecbea5aba9821dfc71bfa77c3f75a9
Signed-off-by: Andreas Sandberg <andreas.sandberg@arm.com>
Reviewed-on: https://gem5-review.googlesource.com/c/public/gem5/+/39218
Maintainer: Jason Lowe-Power <power.jg@gmail.com>
Reviewed-by: Daniel Carvalho <odanrc@yahoo.com.br>
Reviewed-by: Gabe Black <gabe.black@gmail.com>
Tested-by: kokoro <noreply+kokoro@google.com>
This commit is contained in:
Andreas Sandberg
2021-01-19 10:09:56 +00:00
parent e0441fa7a7
commit 69f4aee33c
8 changed files with 478 additions and 2 deletions

View File

@@ -1,4 +1,4 @@
# Copyright (c) 2012-2014, 2017-2019 ARM Limited
# Copyright (c) 2012-2014, 2017-2019, 2021 Arm Limited
# All rights reserved.
#
# The license below extends only to copyright in the software and shall
@@ -1693,6 +1693,43 @@ class Energy(Float):
value = convert.toEnergy(value)
super(Energy, self).__init__(value)
class Temperature(ParamValue):
cxx_type = 'Temperature'
cmd_line_settable = True
ex_str = "1C"
def __init__(self, value):
self.value = convert.toTemperature(value)
def __call__(self, value):
self.__init__(value)
return value
def getValue(self):
from _m5.core import Temperature
return Temperature.fromKelvin(self.value)
def config_value(self):
return self
@classmethod
def cxx_predecls(cls, code):
code('#include "base/temperature.hh"')
@classmethod
def cxx_ini_predecls(cls, code):
# Assume that base/str.hh will be included anyway
# code('#include "base/str.hh"')
pass
@classmethod
def cxx_ini_parse(self, code, src, dest, ret):
code('double _temp;')
code('bool _ret = to_number(%s, _temp);' % src)
code('if (_ret)')
code(' %s = Temperature(_temp);' % dest)
code('%s _ret;' % ret)
class NetworkBandwidth(float,ParamValue):
cxx_type = 'float'
ex_str = "1Gbps"
@@ -2231,6 +2268,7 @@ __all__ = ['Param', 'VectorParam',
'IpAddress', 'IpNetmask', 'IpWithPort',
'MemorySize', 'MemorySize32',
'Latency', 'Frequency', 'Clock', 'Voltage', 'Current', 'Energy',
'Temperature',
'NetworkBandwidth', 'MemoryBandwidth',
'AddrRange',
'MaxAddr', 'MaxTick', 'AllMemory',

View File

@@ -292,3 +292,25 @@ def toCurrent(value):
def toEnergy(value):
return toMetricFloat(value, 'energy', 'J')
def toTemperature(value):
"""Convert a string value specified to a temperature in Kelvin"""
magnitude, unit = toNum(value,
target_type='temperature',
units=('K', 'C', 'F'),
prefixes=metric_prefixes,
converter=float)
if unit == 'K':
kelvin = magnitude
elif unit == 'C':
kelvin = magnitude + 273.15
elif unit == 'F':
kelvin = (magnitude + 459.67) / 1.8
else:
raise ValueError(f"'{value}' needs a valid temperature unit.")
if kelvin < 0:
raise ValueError(f"{value} is an invalid temperature")
return kelvin

View File

@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2019 ARM Limited
* Copyright (c) 2017, 2019, 2021 ARM Limited
* All rights reserved
*
* The license below extends only to copyright in the software and shall
@@ -52,6 +52,7 @@
#include "base/logging.hh"
#include "base/random.hh"
#include "base/socket.hh"
#include "base/temperature.hh"
#include "base/types.hh"
#include "sim/core.hh"
#include "sim/drain.hh"
@@ -220,6 +221,38 @@ pybind_init_core(py::module &m_native)
.def("__sub__", &Cycles::operator-)
;
py::class_<Temperature>(m_core, "Temperature")
.def(py::init<>())
.def(py::init<double>())
.def_static("from_celsius", &Temperature::fromCelsius)
.def_static("from_kelvin", &Temperature::fromKelvin)
.def_static("from_fahrenheit", &Temperature::fromFahrenheit)
.def("celsius", &Temperature::toCelsius)
.def("kelvin", &Temperature::toKelvin)
.def("fahrenheit", &Temperature::toFahrenheit)
.def(py::self == py::self)
.def(py::self != py::self)
.def(py::self < py::self)
.def(py::self <= py::self)
.def(py::self > py::self)
.def(py::self >= py::self)
.def(py::self + py::self)
.def(py::self - py::self)
.def(py::self * float())
.def(float() * py::self)
.def(py::self / float())
.def("__str__", [](const Temperature &t) {
std::stringstream s;
s << t;
return s.str();
})
.def("__repr__", [](const Temperature &t) {
std::stringstream s;
s << "Temperature(" << t.toKelvin() << ")";
return s.str();
})
;
py::class_<tm>(m_core, "tm")
.def_static("gmtime", [](std::time_t t) { return *std::gmtime(&t); })
.def_readwrite("tm_sec", &tm::tm_sec)