Просмотр исходного кода

Add before/after hooks in trace

Eric Ramat 8 лет назад
Родитель
Сommit
b73e6475e4

+ 7 - 0
src/CMakeLists.txt

@@ -1,2 +1,9 @@
+IF ("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
+  message("TRACE on")
+  add_definitions(-DWITH_TRACE)
+ELSE ("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
+  message("TRACE off")
+ENDIF ("${CMAKE_BUILD_TYPE}" STREQUAL "Debug")
+
 ADD_SUBDIRECTORY(artis)
 ADD_SUBDIRECTORY(test)

+ 31 - 1
src/artis/kernel/AbstractAtomicModel.hpp

@@ -27,6 +27,8 @@
 #include <artis/kernel/AbstractCoupledModel.hpp>
 #include <artis/kernel/Internals.hpp>
 #include <artis/kernel/States.hpp>
+
+#include <artis/utils/DateTime.hpp>
 #include <artis/utils/Exception.hpp>
 
 namespace artis { namespace kernel {
@@ -40,12 +42,40 @@ class AbstractAtomicModel : public AbstractModel < U, V >,
     typedef AbstractModel < U, V > type;
 
 public:
-    AbstractAtomicModel()
+    AbstractAtomicModel(const type* parent = 0) : type(parent)
     { }
 
     virtual ~AbstractAtomicModel()
     { }
 
+    virtual void after(typename U::type t)
+    {
+
+#ifdef WITH_TRACE
+        utils::Trace < utils::DoubleTime >::trace()
+            << utils::TraceElement < utils::DoubleTime >("KERNEL", t,
+                                                         utils::KERNEL)
+            << " - AFTER - "
+            << AbstractAtomicModel < T, U, V >::path(this);
+        utils::Trace < utils::DoubleTime >::trace().flush();
+#endif
+
+    }
+
+    virtual void before(typename U::type t)
+    {
+
+#ifdef WITH_TRACE
+        utils::Trace < utils::DoubleTime >::trace()
+            << utils::TraceElement < utils::DoubleTime >("KERNEL", t,
+                                                         utils::KERNEL)
+            << " - BEFORE - "
+            << AbstractAtomicModel < T, U, V >::path(this);
+        utils::Trace < utils::DoubleTime >::trace().flush();
+#endif
+
+    }
+
     virtual bool check(typename U::type t) const
     { return Externals < T, U >::check(t); }
 

+ 76 - 2
src/artis/kernel/AbstractCoupledModel.hpp

@@ -28,6 +28,9 @@
 #include <artis/kernel/Internals.hpp>
 #include <artis/kernel/States.hpp>
 
+#include <artis/utils/DateTime.hpp>
+#include <artis/utils/Trace.hpp>
+
 #include <functional>
 #include <vector>
 
@@ -54,12 +57,40 @@ class AbstractCoupledModel : public AbstractModel < U, V >,
     };
 
 public:
-    AbstractCoupledModel()
+    AbstractCoupledModel(const type* parent = 0) : type(parent)
     { }
 
     virtual ~AbstractCoupledModel()
     { }
 
+    virtual void after(typename U::type t)
+    {
+
+#ifdef WITH_TRACE
+        utils::Trace < utils::DoubleTime >::trace()
+            << utils::TraceElement < utils::DoubleTime >("KERNEL", t,
+                                                         utils::KERNEL)
+            << " - AFTER - "
+            << AbstractCoupledModel < T, U, V, W >::path(this);
+        utils::Trace < utils::DoubleTime >::trace().flush();
+#endif
+
+    }
+
+    virtual void before(typename U::type t)
+    {
+
+#ifdef WITH_TRACE
+        utils::Trace < utils::DoubleTime >::trace()
+            << utils::TraceElement < utils::DoubleTime >("KERNEL", t,
+                                                         utils::KERNEL)
+            << " - BEFORE - "
+            << AbstractCoupledModel < T, U, V, W >::path(this);
+        utils::Trace < utils::DoubleTime >::trace().flush();
+#endif
+
+    }
+
     virtual void build(const W& /* parameters */)
     { }
 
@@ -166,6 +197,44 @@ public:
         }
     }
 
+    virtual std::string path_index(const AbstractModel < U, V >* child,
+                                   int& index) const
+    {
+        typename Setsubmodels::const_iterator it = setsubmodels.begin();
+        bool found = false;
+
+        index = -1;
+        while (not found and it != setsubmodels.end()) {
+            typename std::vector < type* >::const_iterator itm =
+                it->second.begin();
+
+            index = 0;
+            while (not found and itm != it->second.end()) {
+                found = *itm == child;
+                if (not found) {
+                    ++index;
+                    ++itm;
+                }
+            }
+            index = found ? index : -1;
+            ++it;
+        }
+        if (type::parent) {
+            int i;
+            std::string p = type::parent->path_index(this, i);
+
+            if (i >= 0) {
+                return p +
+                    "/[" + std::to_string(i) + "]" +
+                    boost::core::demangle(typeid(*this).name());
+            } else {
+                return p + "/" + boost::core::demangle(typeid(*this).name());
+            }
+        } else {
+            return boost::core::demangle(typeid(*this).name());
+        }
+    }
+
     virtual void restore(const State < U >& state)
     {
         typename AbstractCoupledModel::Submodels::const_iterator it =
@@ -248,7 +317,10 @@ protected:
     }
 
     void submodel(unsigned int index, type* model)
-    { submodels[index] = model; }
+    {
+        submodels[index] = model;
+        model->set_parent(this);
+    }
 
     void S(std::initializer_list < std::pair <  unsigned int,
            type* > > models)
@@ -257,6 +329,7 @@ protected:
                  type* > >::iterator it =
                  models.begin(); it != models.end(); ++it) {
             submodels[it->first] = it->second;
+            it->second->set_parent(this);
         }
     }
 
@@ -266,6 +339,7 @@ protected:
             setsubmodels[index] = std::vector < type* >();
         }
         setsubmodels[index].push_back(model);
+        model->set_parent(this);
     }
 
 private:

+ 67 - 12
src/artis/kernel/AbstractModel.hpp

@@ -26,31 +26,35 @@
 #include <artis/kernel/Node.hpp>
 #include <artis/kernel/State.hpp>
 
+#include <artis/utils/DoubleTime.hpp>
+
+#include <boost/core/demangle.hpp>
+
+#include <iostream>
+#include <typeinfo>
+
 namespace artis { namespace kernel {
 
 template < typename U, typename V >
 class AbstractModel : public Node < U >
 {
+    typedef AbstractModel < U, V > type;
+
 public:
-    AbstractModel() : last_time(-1)
+    AbstractModel(const AbstractModel < U, V >* parent = 0) :
+        parent(parent), last_time(-1)
     { }
 
     virtual ~AbstractModel()
     { }
 
+    virtual void after(typename U::type t) = 0;
+
+    virtual void before(typename U::type t) = 0;
+
     virtual bool check(typename U::type /* t */) const
     { return true; }
 
-    virtual void operator()(typename U::type t)
-    {
-        if (check(t) and (last_time != t or
-                          (last_time == t and is_updated()))) {
-            compute(t, last_time == t);
-            last_time = t;
-            stable();
-        }
-    }
-
     virtual void compute(typename U::type t, bool update) = 0;
 
     virtual void init(typename U::type t, const V& parameters) = 0;
@@ -64,14 +68,65 @@ public:
 
     virtual bool is_updated() const = 0;
 
+    virtual void operator()(typename U::type t)
+    {
+        before(t);
+        if (check(t) and (last_time != t or
+                          (last_time == t and is_updated()))) {
+            compute(t, last_time == t);
+            last_time = t;
+            stable();
+        }
+        after(t);
+    }
+
+    virtual std::string path(const AbstractModel < U, V >* child) const
+    {
+        int index = -1;
+        std::string p = type::parent ? type::parent->path_index(child, index) :
+            "";
+
+        if (index >= 0) {
+            return p +
+                "/[" + std::to_string(index) + "]" +
+                boost::core::demangle(typeid(*child).name());
+        } else {
+            return (p.empty() ? "" : p + "/") +
+                boost::core::demangle(typeid(*child).name());
+        }
+    }
+
+    virtual std::string path_index(const AbstractModel < U, V >* child,
+                                   int& index) const
+    {
+        (void) child;
+        int i = -1;
+        std::string p = type::parent ? type::parent->path_index(this, index) :
+            "";
+
+        index = -1;
+        if (i >= 0) {
+            return p +
+                "/[" + std::to_string(i) + "]" +
+                boost::core::demangle(typeid(*this).name());
+        } else {
+            return (p.empty() ? "" : p + "/") +
+                boost::core::demangle(typeid(*this).name());
+        }
+    }
+
     virtual void restore(const State < U >& state) = 0;
 
     virtual void save(State < U >& state) const = 0;
 
+    void set_parent(const AbstractModel < U, V >* p)
+    { parent = p; }
+
     virtual void stable() = 0;
 
 protected:
-    typename U::type last_time;
+    const AbstractModel < U, V >* parent;
+    typename U::type              last_time;
 };
 
 } }

+ 4 - 4
src/artis/kernel/CMakeLists.txt

@@ -4,11 +4,11 @@ INCLUDE_DIRECTORIES(
 LINK_DIRECTORIES()
 
 SET(ARTIS_KERNEL_HPP AbstractAtomicModel.hpp
-  AbstractCoupledModel.hpp AbstractModel.hpp Context.hpp Externals.hpp
-  Internals.hpp Node.hpp Simulator.hpp State.hpp States.hpp Value.hpp
-  Values.hpp)
+  AbstractCoupledModel.hpp AbstractModel.hpp Builder.hpp Context.hpp
+  Externals.hpp Internals.hpp Node.hpp Simulator.hpp State.hpp States.hpp
+  Value.hpp Values.hpp)
 
-SET(ARTIS_KERNEL_CPP Simulator.cpp)
+SET(ARTIS_KERNEL_CPP Simulator.cpp Builder.cpp)
 
 ADD_LIBRARY(artis-kernel SHARED ${ARTIS_KERNEL_HPP}
   ${ARTIS_KERNEL_CPP})

+ 6 - 3
src/artis/utils/Trace.hpp

@@ -35,7 +35,7 @@
 
 namespace artis { namespace utils {
 
-enum TraceType { NONE = 0, INIT, COMPUTE };
+enum TraceType { NONE = 0, CHECK, COMPUTE, INIT, KERNEL, PUT };
 
 template < class Time >
 class TraceElement
@@ -129,8 +129,11 @@ public:
             switch (it->get_type())
             {
             case NONE: ss << "none"; break;
-            case INIT: ss << "init"; break;
+            case CHECK: ss << "check"; break;
             case COMPUTE:  ss << "compute"; break;
+            case INIT: ss << "init"; break;
+            case KERNEL: ss << "kernel"; break;
+            case PUT: ss << "put"; break;
             };
             ss << ">";
             if (not it->get_comment().empty()) {
@@ -192,7 +195,7 @@ private:
     { _sstream = 0; }
 
     static std::shared_ptr < Trace < Time > > _instance;
-    static std::once_flag _flag;
+    static std::once_flag                     _flag;
 
     TraceElements < Time > _trace;
     TraceElement < Time >  _element;

+ 17 - 1
src/test/test.cpp

@@ -25,8 +25,11 @@
 
 #include <artis/kernel/AbstractAtomicModel.hpp>
 #include <artis/kernel/AbstractCoupledModel.hpp>
+#include <artis/kernel/Builder.hpp>
 #include <artis/kernel/Simulator.hpp>
 #include <artis/observer/Output.hpp>
+
+#include <artis/utils/DateTime.hpp>
 #include <artis/utils/Trace.hpp>
 
 struct GlobalParameters
@@ -38,6 +41,7 @@ struct ModelParameters
 using namespace artis::kernel;
 using namespace artis::utils;
 
+using Model = AbstractModel < DoubleTime, ModelParameters >;
 template < typename T >
 using AtomicModel = AbstractAtomicModel < T, DoubleTime, ModelParameters >;
 
@@ -177,6 +181,11 @@ typedef artis::kernel::Simulator < RootModel,
                                    ModelParameters,
                                    GlobalParameters > ASimulator;
 
+typedef artis::kernel::Builder < RootModel,
+                                 artis::utils::DoubleTime,
+                                 ModelParameters,
+                                 GlobalParameters > ABuilder;
+
 class AView : public artis::observer::View < artis::utils::DoubleTime,
                                              ModelParameters >
 {
@@ -202,13 +211,20 @@ TEST_CASE("Simulator_tests", "simple")
 {
     GlobalParameters globalParameters;
     ModelParameters modelParameters;
+    ABuilder builder("{ \"type\": \"RootModel\", "                      \
+                     "\"submodels\": [ "                                \
+                     "{ \"type\": \"AModel\", \"internals\": [] }, "    \
+                     "{ \"type\": \"BModel\", \"internals\": [] } "     \
+                     "] }");
     ASimulator simulator(new RootModel, globalParameters);
 
     simulator.attachView("Root", new AView);
 
     artis::utils::Trace < artis::utils::DoubleTime >::trace().clear();
+
     simulator.init(0, modelParameters);
-    simulator.run(0, 10);
+    simulator.run(artis::utils::DateTime::toJulianDayNumber(2016, 1, 1),
+                  artis::utils::DateTime::toJulianDayNumber(2016, 1, 10));
 
     AnOutput output(simulator.observer());