ソースを参照

Indexed trace

gbeurier 7 年 前
コミット
4deee7abc1
1 ファイル変更110 行追加63 行削除
  1. 110 63
      src/artis/utils/Trace.hpp

+ 110 - 63
src/artis/utils/Trace.hpp

@@ -30,6 +30,7 @@
 #include <sstream>
 #include <string>
 #include <vector>
+#include <map>
 
 #include <artis/utils/DateTime.hpp>
 #include <artis/kernel/Any.hpp>
@@ -40,16 +41,16 @@
 
 //fwd decl for friend classes in TraceElement
 namespace artis { namespace kernel {
-    template < typename T, typename U, typename V, typename W >
-    class AbstractCoupledModel;
-    template < typename T, typename U, typename V >
-    class AbstractAtomicModel;
-    template < typename Time, typename Parameters >
-    class AbstractModel;
-    template < typename T, typename U, typename V >
-    class Internals;
-    template < typename T, typename U, typename V >
-    class Externals;
+template < typename T, typename U, typename V, typename W >
+class AbstractCoupledModel;
+template < typename T, typename U, typename V >
+class AbstractAtomicModel;
+template < typename Time, typename Parameters >
+class AbstractModel;
+template < typename T, typename U, typename V >
+class Internals;
+template < typename T, typename U, typename V >
+class Externals;
 }}
 
 namespace artis { namespace utils {
@@ -65,46 +66,89 @@ static const std::vector <std::string> TraceTypesStr = {
 class KernelInfo
 {
 public:
+    static std::map < std::string, int > elt_dictionary;
+    static std::vector < std::string > elt_names;
+    static unsigned int term(const std::string& v) {
+        if(elt_dictionary.find(v) == elt_dictionary.end()) {
+            elt_dictionary[v] = elt_names.size();
+            elt_names.push_back(v);
+        }
+        return elt_dictionary[v];
+    }
+    static const std::string & term(unsigned int i) {return elt_names[i];}
+
+
     KernelInfo() :
-        _var(""), _internal_var(false), _value(""),
-        _tgt_model(""), _tgt_internal_var(""), _empty(true)
-    { }
+        _internal_var(false), _empty(true)
+    {
+        set_var("");
+        set_value("");
+        set_tgt_model("");
+        set_tgt_internal_var("");
+    }
 
     KernelInfo(const std::string& var, bool internal_var,
                const std::string& value) :
-        _var(var), _internal_var(internal_var), _value(value),
-        _tgt_model(""), _tgt_internal_var(""), _empty(false)
-    { }
+         _internal_var(internal_var), _empty(false)
+    {
+        set_var(var);
+        set_value(value);
+        set_tgt_model("");
+        set_tgt_internal_var("");
+    }
 
     KernelInfo(const std::string& var, const std::string& tgt_model,
                const std::string& tgt_internal_var) :
-        _var(var), _internal_var(true), _value(""),
-        _tgt_model(tgt_model), _tgt_internal_var(tgt_internal_var),
-        _empty(false)
-    { }
+        _internal_var(true), _empty(false)
+    {
+        set_var(var);
+        set_value("");
+        set_tgt_model(tgt_model);
+        set_tgt_internal_var(tgt_internal_var);
+    }
 
     KernelInfo(const std::string& var, bool internal_var) :
-        _var(var), _internal_var(internal_var), _value(""),
-        _tgt_model(""), _tgt_internal_var(""), _empty(false)
-    { }
+        _internal_var(internal_var), _empty(false)
+    {
+        set_var(var);
+        set_value("");
+        set_tgt_model("");
+        set_tgt_internal_var("");
+    }
 
     KernelInfo(const std::string& tgt_model) :
-        _var(""), _internal_var(false), _value(""),
-        _tgt_model(tgt_model), _tgt_internal_var(""),
-        _empty(false)
-    { }
+        _internal_var(false), _empty(false)
+    {
+        set_var("");
+        set_value("");
+        set_tgt_model(tgt_model);
+        set_tgt_internal_var("");
+    }
+
+    void set_var(const std::string& v)
+    { _var = KernelInfo::term(v); }
+
+    void set_value(const std::string& v)
+    { _value = KernelInfo::term(v); }
+
+    void set_tgt_model(const std::string& v)
+    { _tgt_model = KernelInfo::term(v); }
+
+    void set_tgt_internal_var(const std::string& v)
+    { _tgt_internal_var = KernelInfo::term(v); }
+
 
     const std::string& var() const
-    { return _var; }
+    { return KernelInfo::term(_var); }
 
     const std::string& value() const
-    { return _value; }
+    { return KernelInfo::term(_value); }
 
     const std::string& tgt_model() const
-    { return _tgt_model; }
+    { return KernelInfo::term(_tgt_model); }
 
     const std::string& tgt_internal_var() const
-    { return _tgt_internal_var; }
+    { return KernelInfo::term(_tgt_internal_var); }
 
     bool is_internal_var() const
     { return _internal_var; }
@@ -115,30 +159,30 @@ public:
     std::string to_string() const
     {
         std::ostringstream ss;
-        if( not _var.empty() ){
+        if( not KernelInfo::term(_var).empty() ){
             ss << ":";
             if (not _internal_var)
                 ss << "*";
-            ss << _var;
+            ss << KernelInfo::term(_var);
         }
-        if( not _value.empty()) {
-            ss << "=" << _value;
+        if( not _value == -1) {
+            ss << "=" << KernelInfo::term(_value);
         }
-        if(not _tgt_model.empty()) {
-            ss << " -> " << _tgt_model;
-            if(not _tgt_internal_var.empty()) {
-                ss << ":" << _tgt_internal_var;
+        if(not KernelInfo::term(_tgt_model).empty()) {
+            ss << " -> " << KernelInfo::term(_tgt_model);
+            if(not KernelInfo::term(_tgt_internal_var).empty()) {
+                ss << ":" << KernelInfo::term(_tgt_internal_var);
             }
         }
         return ss.str();
     }
 
 private:
-    std::string _var;
+    unsigned int _var;
+    unsigned int _value;
+    unsigned int _tgt_model;
+    unsigned int _tgt_internal_var;
     bool        _internal_var;
-    std::string _value;
-    std::string _tgt_model;
-    std::string _tgt_internal_var;
     bool        _empty;
 };
 
@@ -165,7 +209,7 @@ private:
 
 public:
     TraceElement() : _time(Time::null), _type(NONE),
-                     _from_kernel(false)
+        _from_kernel(false)
     { }
 
     TraceElement(const std::string& model_name, typename Time::type time,
@@ -226,16 +270,16 @@ public:
         TraceElements < Time > result;
 
         std::copy_if(
-            TraceElements < Time >::begin(),
-            TraceElements < Time >::end(), std::back_inserter(result),
-            [model_name](TraceElement < Time > const & x) {
-                if (not x.get_kernel_info().empty()) {
-                    if (x.get_kernel_info().tgt_model() == model_name) {
-                        return true;
-                    }
+                    TraceElements < Time >::begin(),
+                    TraceElements < Time >::end(), std::back_inserter(result),
+                    [model_name](TraceElement < Time > const & x) {
+            if (not x.get_kernel_info().empty()) {
+                if (x.get_kernel_info().tgt_model() == model_name) {
+                    return true;
                 }
-                return x.get_model_name() == model_name;
-            });
+            }
+            return x.get_model_name() == model_name;
+        });
         return result;
     }
 
@@ -277,7 +321,7 @@ public:
     }
 
     std::string to_string(artis::utils::DateFormat date_format =
-                          artis::utils::DATE_FORMAT_EXTENDED) const
+            artis::utils::DATE_FORMAT_EXTENDED) const
     {
         std::ostringstream ss;
 
@@ -315,7 +359,10 @@ public:
     static Trace& trace()
     {
         std::call_once(_flag, [] ()
-        { _instance.reset(new Trace());}
+        { _instance.reset(new Trace());
+//            artis::utils::KernelInfo::elt_dictionary = std::map < std::string, int >();
+//            artis::utils::KernelInfo::elt_names = std::vector < std::string > ();
+        }
         );
         return *_instance;
     }
@@ -370,8 +417,8 @@ private:
 
 template < class Time >
 artis::utils::Trace < Time >& operator<<(
-    artis::utils::Trace < Time >& trace,
-    const artis::utils::TraceElement < Time >& e)
+        artis::utils::Trace < Time >& trace,
+        const artis::utils::TraceElement < Time >& e)
 {
     std::lock_guard < std::mutex > lock(trace.mutex());
 
@@ -381,8 +428,8 @@ artis::utils::Trace < Time >& operator<<(
 
 template < class Time >
 artis::utils::Trace < Time >& operator<<(
-    artis::utils::Trace < Time >& trace,
-    const artis::utils::KernelInfo& info)
+        artis::utils::Trace < Time >& trace,
+        const artis::utils::KernelInfo& info)
 {
     std::lock_guard < std::mutex > lock(trace.mutex());
 
@@ -392,8 +439,8 @@ artis::utils::Trace < Time >& operator<<(
 
 template < class Time >
 artis::utils::Trace < Time >& operator<<(
-    artis::utils::Trace < Time >& trace,
-    const std::string& str)
+        artis::utils::Trace < Time >& trace,
+        const std::string& str)
 {
     std::lock_guard < std::mutex > lock(trace.mutex());
 
@@ -403,8 +450,8 @@ artis::utils::Trace < Time >& operator<<(
 
 template < class Time >
 artis::utils::Trace < Time >& operator<<(
-    artis::utils::Trace < Time >& trace,
-    typename Time::type t)
+        artis::utils::Trace < Time >& trace,
+        typename Time::type t)
 {
     std::lock_guard < std::mutex > lock(trace.mutex());