Coordinator.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /**
  2. * @file kernel/sss/Coordinator.hpp
  3. * @author The ARTIS Development Team
  4. * See the AUTHORS or Authors.txt file
  5. */
  6. /*
  7. * ARTIS - the multimodeling and simulation environment
  8. * This file is a part of the ARTIS environment
  9. *
  10. * Copyright (C) 2013-2018 ULCO http://www.univ-littoral.fr
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation, either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. */
  25. #ifndef SSS_COORDINATOR
  26. #define SSS_COORDINATOR 1
  27. #include <artis-star/common/Coordinator.hpp>
  28. #include <artis-star/common/Parameters.hpp>
  29. #include <artis-star/common/utils/Trace.hpp>
  30. #include <artis-star/kernel/sss/Model.hpp>
  31. #include <cassert>
  32. #include <iostream>
  33. namespace artis { namespace sss {
  34. template < class Time >
  35. class Parameters
  36. {
  37. public:
  38. Parameters(typename Time::type time_step) : _time_step(time_step)
  39. { }
  40. typename Time::type _time_step;
  41. };
  42. template < class Time, class Policy, class GraphManager,
  43. class Parameters = Parameters < Time >,
  44. class GraphParameters = common::NoParameters >
  45. class Coordinator : public common::Coordinator < Time >,
  46. public sss::Model < Time >
  47. {
  48. typedef Coordinator < Time, Policy, GraphManager,
  49. Parameters, GraphParameters > type;
  50. public:
  51. typedef Parameters parameters_type;
  52. typedef GraphParameters graph_parameters_type;
  53. Coordinator(const std::string& name,
  54. const Parameters& parameters,
  55. const GraphParameters& graph_paramaters) :
  56. common::Model < Time >(name),
  57. common::Coordinator < Time >(name),
  58. sss::Model < Time >(name),
  59. _graph_manager(this, graph_paramaters),
  60. _time_step(parameters._time_step)
  61. { }
  62. virtual ~Coordinator()
  63. { }
  64. virtual bool is_atomic() const
  65. { return common::Coordinator < Time >::is_atomic(); }
  66. virtual std::string to_string(int level) const
  67. { return common::Coordinator < Time >::to_string(level); }
  68. typename Time::type start(const typename Time::type& t)
  69. {
  70. assert(_graph_manager.children().size() > 0);
  71. type::_tl = t;
  72. type::_tn = t;
  73. for (auto & child : _graph_manager.children()) {
  74. child->start(t);
  75. }
  76. return type::_tn;
  77. }
  78. typename Time::type dispatch_events(const common::Bag < Time >& bag,
  79. const typename Time::type& t)
  80. {
  81. _graph_manager.dispatch_events(bag, t);
  82. return type::_tn;
  83. }
  84. void observation(std::ostream& file) const
  85. {
  86. for (auto & child : _graph_manager.children()) {
  87. child->observation(file);
  88. }
  89. }
  90. void output(const typename Time::type& t)
  91. {
  92. if (t == type::_tn) {
  93. for (auto & model : _graph_manager.children()) {
  94. model->update_buffer(t);
  95. }
  96. for (auto & model : _graph_manager.children()) {
  97. if (not model->is_send() && model->is_marked()) {
  98. model->output(t);
  99. model->send();
  100. }
  101. }
  102. }
  103. }
  104. void post_event(const typename Time::type& t,
  105. const common::ExternalEvent < Time >& event)
  106. {
  107. if (t == type::_tn) {
  108. _graph_manager.post_event(t, event);
  109. } else {
  110. _policy(t, event, type::_tl, type::_tn);
  111. }
  112. }
  113. typename Time::type transition(const typename Time::type& t)
  114. {
  115. if (t == type::_tn) {
  116. bool end = true;
  117. for (auto & event : _policy.bag()) {
  118. post_event(t, event);
  119. }
  120. for (auto & model : _graph_manager.children()) {
  121. if (not model->is_marked()) {
  122. if (model->all_ports_are_assigned()) {
  123. model->transition(t);
  124. model->mark();
  125. end = false;
  126. } else {
  127. end = false;
  128. }
  129. } else {
  130. if (not model->is_send()) {
  131. end = false;
  132. } else {
  133. if (t == model->get_tn()) {
  134. model->transition(t);
  135. }
  136. }
  137. }
  138. }
  139. if (end) {
  140. for (auto & model : _graph_manager.children()) {
  141. model->unmark();
  142. model->unsend();
  143. }
  144. type::_tl = t;
  145. type::_tn = t + _time_step;
  146. }
  147. }
  148. type::clear_bag();
  149. return type::_tn;
  150. }
  151. virtual void update_buffer(typename Time::type /* time */)
  152. { }
  153. private:
  154. GraphManager _graph_manager;
  155. typename Time::type _time_step;
  156. Policy _policy;
  157. };
  158. } } // namespace artis sss
  159. #endif