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,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)