changeset 57c96df312a1 in /z/repo/gem5
details: http://repo.gem5.org/gem5?cmd=changeset;node=57c96df312a1
description:
        Stats: Add a sparse histogram stat object.

diffstat:

 src/base/statistics.hh   |  185 +++++++++++++++++++++++++++++++++++++++++++++++
 src/base/stats/info.hh   |   14 +++
 src/base/stats/output.hh |    2 +
 src/base/stats/text.cc   |   86 +++++++++++++++++++++
 src/base/stats/text.hh   |    1 +
 src/base/stats/types.hh  |    3 +
 src/python/swig/stats.i  |    1 +
 src/unittest/stattest.cc |   11 ++
 8 files changed, 303 insertions(+), 0 deletions(-)

diffs (truncated from 416 to 300 lines):

diff -r f4272aa61e74 -r 57c96df312a1 src/base/statistics.hh
--- a/src/base/statistics.hh    Fri Aug 19 15:08:05 2011 -0500
+++ b/src/base/statistics.hh    Fri Aug 19 15:08:05 2011 -0500
@@ -56,6 +56,7 @@
 #include <functional>
 #include <iosfwd>
 #include <list>
+#include <map>
 #include <string>
 #include <vector>
 
@@ -2608,6 +2609,190 @@
     std::string str() const { return this->s.str(); }
 };
 
+template <class Stat>
+class SparseHistInfoProxy : public InfoProxy<Stat, SparseHistInfo>
+{
+  public:
+    SparseHistInfoProxy(Stat &stat) : InfoProxy<Stat, SparseHistInfo>(stat) {}
+};
+
+/**
+ * Implementation of a sparse histogram stat. The storage class is
+ * determined by the Storage template.
+ */
+template <class Derived, class Stor>
+class SparseHistBase : public DataWrap<Derived, SparseHistInfoProxy>
+{
+  public:
+    typedef SparseHistInfoProxy<Derived> Info;
+    typedef Stor Storage;
+    typedef typename Stor::Params Params;
+
+  protected:
+    /** The storage for this stat. */
+    char storage[sizeof(Storage)];
+
+  protected:
+    /**
+     * Retrieve the storage.
+     * @return The storage object for this stat.
+     */
+    Storage *
+    data()
+    {
+        return reinterpret_cast<Storage *>(storage);
+    }
+
+    /**
+     * Retrieve a const pointer to the storage.
+     * @return A const pointer to the storage object for this stat.
+     */
+    const Storage *
+    data() const
+    {
+        return reinterpret_cast<const Storage *>(storage);
+    }
+
+    void
+    doInit()
+    {
+        new (storage) Storage(this->info());
+        this->setInit();
+    }
+
+  public:
+    SparseHistBase() { }
+
+    /**
+     * Add a value to the distribtion n times. Calls sample on the storage
+     * class.
+     * @param v The value to add.
+     * @param n The number of times to add it, defaults to 1.
+     */
+    template <typename U>
+    void sample(const U &v, int n = 1) { data()->sample(v, n); }
+
+    /**
+     * Return the number of entries in this stat.
+     * @return The number of entries.
+     */
+    size_type size() const { return data()->size(); }
+    /**
+     * Return true if no samples have been added.
+     * @return True if there haven't been any samples.
+     */
+    bool zero() const { return data()->zero(); }
+
+    void
+    prepare()
+    {
+        Info *info = this->info();
+        data()->prepare(info, info->data);
+    }
+
+    /**
+     * Reset stat value to default
+     */
+    void
+    reset()
+    {
+        data()->reset(this->info());
+    }
+};
+
+/**
+ * Templatized storage and interface for a sparse histogram stat.
+ */
+class SparseHistStor
+{
+  public:
+    /** The parameters for a sparse histogram stat. */
+    struct Params : public DistParams
+    {
+        Params() : DistParams(Hist) {}
+    };
+
+  private:
+    /** Counter for number of samples */
+    Counter samples;
+    /** Counter for each bucket. */
+    MCounter cmap;
+
+  public:
+    SparseHistStor(Info *info)
+    {
+        reset(info);
+    }
+
+    /**
+     * Add a value to the distribution for the given number of times.
+     * @param val The value to add.
+     * @param number The number of times to add the value.
+     */
+    void
+    sample(Counter val, int number)
+    {
+        cmap[val] += number;
+        samples += number;
+    }
+
+    /**
+     * Return the number of buckets in this distribution.
+     * @return the number of buckets.
+     */
+    size_type size() const { return cmap.size(); }
+
+    /**
+     * Returns true if any calls to sample have been made.
+     * @return True if any values have been sampled.
+     */
+    bool
+    zero() const
+    {
+        return samples == Counter();
+    }
+
+    void
+    prepare(Info *info, SparseHistData &data)
+    {
+        MCounter::iterator it;
+        data.cmap.clear();
+        for (it = cmap.begin(); it != cmap.end(); it++) {
+            data.cmap[(*it).first] = (*it).second;
+        }
+
+        data.samples = samples;
+    }
+
+    /**
+     * Reset stat value to default
+     */
+    void
+    reset(Info *info)
+    {
+        cmap.clear();
+        samples = 0;
+    }
+};
+
+class SparseHistogram : public SparseHistBase<SparseHistogram, SparseHistStor>
+{
+  public:
+    /**
+     * Set the parameters of this histogram. @sa HistStor::Params
+     * @param size The number of buckets in the histogram
+     * @return A reference to this histogram.
+     */
+    SparseHistogram &
+    init(size_type size)
+    {
+        SparseHistStor::Params *params = new SparseHistStor::Params;
+        this->setParams(params);
+        this->doInit();
+        return this->self();
+    }
+};
+
 class Temp;
 /**
  * A formula for statistics that is calculated when printed. A formula is
diff -r f4272aa61e74 -r 57c96df312a1 src/base/stats/info.hh
--- a/src/base/stats/info.hh    Fri Aug 19 15:08:05 2011 -0500
+++ b/src/base/stats/info.hh    Fri Aug 19 15:08:05 2011 -0500
@@ -234,6 +234,20 @@
     virtual std::string str() const = 0;
 };
 
+/** Data structure of sparse histogram */
+struct SparseHistData
+{
+    MCounter cmap;
+    Counter samples;
+};
+
+
+class SparseHistInfo : public Info
+{
+  public:
+    /** Local storage for the entry values, used for printing. */
+    SparseHistData data;
+};
 
 } // namespace Stats
 
diff -r f4272aa61e74 -r 57c96df312a1 src/base/stats/output.hh
--- a/src/base/stats/output.hh  Fri Aug 19 15:08:05 2011 -0500
+++ b/src/base/stats/output.hh  Fri Aug 19 15:08:05 2011 -0500
@@ -43,6 +43,7 @@
 class VectorDistInfo;
 class Vector2dInfo;
 class FormulaInfo;
+class SparseHistInfo; // Sparse histogram
 
 struct Output
 {
@@ -57,6 +58,7 @@
     virtual void visit(const VectorDistInfo &info) = 0;
     virtual void visit(const Vector2dInfo &info) = 0;
     virtual void visit(const FormulaInfo &info) = 0;
+    virtual void visit(const SparseHistInfo &info) = 0; // Sparse histogram
 };
 
 } // namespace Stats
diff -r f4272aa61e74 -r 57c96df312a1 src/base/stats/text.cc
--- a/src/base/stats/text.cc    Fri Aug 19 15:08:05 2011 -0500
+++ b/src/base/stats/text.cc    Fri Aug 19 15:08:05 2011 -0500
@@ -581,6 +581,92 @@
     visit((const VectorInfo &)info);
 }
 
+/*
+  This struct implements the output methods for the sparse
+  histogram stat
+*/
+struct SparseHistPrint
+{
+    string name;
+    string separatorString;
+    string desc;
+    Flags flags;
+    bool descriptions;
+    int precision;
+
+    const SparseHistData &data;
+
+    SparseHistPrint(const Text *text, const SparseHistInfo &info);
+    void init(const Text *text, const Info &info);
+    void operator()(ostream &stream) const;
+};
+
+/* Call initialization function */
+SparseHistPrint::SparseHistPrint(const Text *text, const SparseHistInfo &info)
+    : data(info.data)
+{
+    init(text, info);
+}
+
+/* Initialization function */
+void
+SparseHistPrint::init(const Text *text, const Info &info)
+{
+    name = info.name;
+    separatorString = info.separatorString;
+    desc = info.desc;
+    flags = info.flags;
+    precision = info.precision;
+    descriptions = text->descriptions;
+}
+
+/* Grab data from map and write to output stream */
+void
+SparseHistPrint::operator()(ostream &stream) const
+{
+    string base = name + separatorString;
+
+    ScalarPrint print;
+    print.precision = precision;
+    print.flags = flags;
_______________________________________________
gem5-dev mailing list
[email protected]
http://m5sim.org/mailman/listinfo/gem5-dev

Reply via email to