Andreas Sandberg has uploaded this change for review. ( https://gem5-review.googlesource.com/c/public/gem5/+/39218 )

Change subject: base, python: Add a Temperature type and associated param
......................................................................

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 <[email protected]>
---
M src/base/SConscript
A src/base/temperature.cc
A src/base/temperature.hh
M src/python/m5/params.py
M src/python/m5/util/convert.py
M src/python/pybind11/core.cc
6 files changed, 258 insertions(+), 2 deletions(-)



diff --git a/src/base/SConscript b/src/base/SConscript
index b3d9506..dd699f3 100644
--- a/src/base/SConscript
+++ b/src/base/SConscript
@@ -71,6 +71,7 @@
 GTest('str.test', 'str.test.cc', 'str.cc')
 Source('time.cc')
 Source('version.cc')
+Source('temperature.cc')
 Source('trace.cc')
 GTest('trie.test', 'trie.test.cc')
 Source('types.cc')
diff --git a/src/base/temperature.cc b/src/base/temperature.cc
new file mode 100644
index 0000000..d225b26
--- /dev/null
+++ b/src/base/temperature.cc
@@ -0,0 +1,57 @@
+/*
+ * Copyright (c) 2021 Arm Limited
+ * All rights reserved
+ *
+ * The license below extends only to copyright in the software and shall
+ * not be construed as granting a license to any other intellectual
+ * property including but not limited to intellectual property relating
+ * to a hardware implementation of the functionality of the software
+ * licensed hereunder.  You may use the software subject to the license
+ * terms below provided that you ensure that this notice is replicated
+ * unmodified and in its entirety in all distributions of the software,
+ * modified or unmodified, in source code or in binary form.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met: redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer;
+ * redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution;
+ * neither the name of the copyright holders nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include "base/temperature.hh"
+
+Temperature
+Temperature::fromKelvin(double _value)
+{
+    return Temperature(_value);
+}
+
+Temperature
+Temperature::fromCelsius(double _value)
+{
+    return Temperature(_value - 273.15);
+}
+
+std::ostream&
+operator<<(std::ostream &out, const Temperature &temp)
+{
+    out << temp.value << "K";
+    return out;
+}
diff --git a/src/base/temperature.hh b/src/base/temperature.hh
new file mode 100644
index 0000000..f36b1e1
--- /dev/null
+++ b/src/base/temperature.hh
@@ -0,0 +1,123 @@
+/*
+ * Copyright (c) 2021 Arm Limited
+ * All rights reserved
+ *
+ * The license below extends only to copyright in the software and shall
+ * not be construed as granting a license to any other intellectual
+ * property including but not limited to intellectual property relating
+ * to a hardware implementation of the functionality of the software
+ * licensed hereunder.  You may use the software subject to the license
+ * terms below provided that you ensure that this notice is replicated
+ * unmodified and in its entirety in all distributions of the software,
+ * modified or unmodified, in source code or in binary form.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are
+ * met: redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer;
+ * redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution;
+ * neither the name of the copyright holders nor the names of its
+ * contributors may be used to endorse or promote products derived from
+ * this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
+ * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
+ * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
+ * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
+ * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
+ * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
+ * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
+ * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
+ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifndef __BASE_TEMPERATURE_HH__
+#define __BASE_TEMPERATURE_HH__
+
+#include <ostream>
+
+class Temperature
+{
+
+  private:
+    /** Temperature in Kelvin */
+    double value;
+
+  public:
+    /** Explicit constructor assigning a value. */
+    explicit constexpr Temperature(double _value = 0.0)
+    : value(_value) { }
+
+    static Temperature fromKelvin(double _value);
+    static Temperature fromCelsius(double _value);
+
+    constexpr double toKelvin() const { return value; }
+    constexpr double toCelsius() const { return value + 273.15; }
+
+    constexpr bool operator>(const Temperature &rhs) const
+    {
+        return value > rhs.value;
+    }
+
+    constexpr bool operator<(const Temperature &rhs) const
+    {
+        return value < rhs.value;
+    }
+
+    constexpr bool operator==(const Temperature &rhs) const
+    {
+        return value == rhs.value;
+    }
+
+    constexpr Temperature operator+(const Temperature &rhs) const
+    {
+        return Temperature(value + rhs.value);
+    }
+
+    constexpr Temperature operator-(const Temperature &rhs) const
+    {
+        return Temperature(value - rhs.value);
+    }
+
+    constexpr Temperature operator*(const double &rhs) const
+    {
+        return Temperature(value * rhs);
+    }
+
+    constexpr Temperature operator/(const double &rhs) const
+    {
+        return Temperature(value / rhs);
+    }
+
+    Temperature &operator+=(const Temperature &rhs)
+    {
+        value += rhs.value;
+        return *this;
+    }
+
+    Temperature &operator-=(const Temperature &rhs)
+    {
+        value -= rhs.value;
+        return *this;
+    }
+
+    Temperature &operator*=(const double &rhs)
+    {
+        value *= rhs;
+        return *this;
+    }
+
+    Temperature &operator/=(const double &rhs)
+    {
+        value /= rhs;
+        return *this;
+    }
+
+ friend std::ostream &operator<<(std::ostream &out, const Temperature &t);
+};
+
+#endif // __BASE_TEMPERATURE_HH__
diff --git a/src/python/m5/params.py b/src/python/m5/params.py
index 45082d7..51a8c58 100644
--- a/src/python/m5/params.py
+++ b/src/python/m5/params.py
@@ -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
@@ -1707,6 +1707,43 @@
         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 Cycles.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"
@@ -2245,6 +2282,7 @@
            'IpAddress', 'IpNetmask', 'IpWithPort',
            'MemorySize', 'MemorySize32',
            'Latency', 'Frequency', 'Clock', 'Voltage', 'Current', 'Energy',
+           'Temperature',
            'NetworkBandwidth', 'MemoryBandwidth',
            'AddrRange',
            'MaxAddr', 'MaxTick', 'AllMemory',
diff --git a/src/python/m5/util/convert.py b/src/python/m5/util/convert.py
index 772fba2..2b75591 100644
--- a/src/python/m5/util/convert.py
+++ b/src/python/m5/util/convert.py
@@ -271,3 +271,26 @@

 def toEnergy(value):
     return toMetricFloat(value, 'energy', 'J')
+
+def toTemperature(value):
+    """Convert a string value specified to a temperature in Kelvin"""
+
+    def convert(magnitude, unit):
+        if unit == 'K':
+            return magnitude
+        elif unit == 'C':
+            return magnitude + 273.15
+        elif unit == 'F':
+            return (magnitude + 459.67) / 1.8
+        else:
+            raise ValueError(f"'{value}' needs a valid temperature unit.")
+
+    kelvin = convert(*toNum(value,
+                            target_type='temperature',
+                            units=('K', 'C', 'F'),
+                            prefixes=metric_prefixes,
+                            converter=float))
+    if kelvin < 0:
+        raise ValueError(f"{value} is an invalid temperature")
+
+    return kelvin
diff --git a/src/python/pybind11/core.cc b/src/python/pybind11/core.cc
index 8655d73..a372d47 100644
--- a/src/python/pybind11/core.cc
+++ b/src/python/pybind11/core.cc
@@ -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,19 @@
         .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("__add__", &Temperature::operator+)
+        .def("__sub__", &Temperature::operator-)
+        .def("__truediv__", &Temperature::operator/)
+        .def("__mul__", &Temperature::operator*)
+        .def("celsius", &Temperature::toCelsius)
+        .def("kelvin", &Temperature::toKelvin)
+        ;
+
     py::class_<tm>(m_core, "tm")
.def_static("gmtime", [](std::time_t t) { return *std::gmtime(&t); })
         .def_readwrite("tm_sec", &tm::tm_sec)

--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/39218
To unsubscribe, or for help writing mail filters, visit https://gem5-review.googlesource.com/settings

Gerrit-Project: public/gem5
Gerrit-Branch: develop
Gerrit-Change-Id: I5783cc4f4fecbea5aba9821dfc71bfa77c3f75a9
Gerrit-Change-Number: 39218
Gerrit-PatchSet: 1
Gerrit-Owner: Andreas Sandberg <[email protected]>
Gerrit-MessageType: newchange
_______________________________________________
gem5-dev mailing list -- [email protected]
To unsubscribe send an email to [email protected]
%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s

Reply via email to