Coordinator.hpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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-2019 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
  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. namespace artis {
  33. namespace sss {
  34. template<class Time>
  35. class Parameters
  36. {
  37. public:
  38. Parameters(typename Time::type time_step)
  39. : _time_step(time_step)
  40. {}
  41. typename Time::type _time_step;
  42. };
  43. template<class Time, class Policy, class GraphManager,
  44. class Parameters = Parameters<Time>,
  45. class GraphParameters = common::NoParameters>
  46. class Coordinator : public common::Coordinator<Time>,
  47. public sss::Model<Time>
  48. {
  49. typedef Coordinator<Time, Policy, GraphManager,
  50. Parameters, GraphParameters> type;
  51. public:
  52. typedef Parameters parameters_type;
  53. typedef GraphParameters graph_parameters_type;
  54. Coordinator(const std::string &name,
  55. const Parameters &parameters,
  56. const GraphParameters &graph_parameters)
  57. :
  58. common::Model<Time>(name),
  59. common::Coordinator<Time>(name),
  60. sss::Model<Time>(name),
  61. _graph_manager(this, graph_parameters),
  62. _time_step(parameters._time_step)
  63. {}
  64. virtual ~Coordinator()
  65. {}
  66. GraphManager &get_graph_manager()
  67. { return _graph_manager; }
  68. const GraphManager &get_graph_manager() const
  69. { return _graph_manager; }
  70. virtual bool is_atomic() const
  71. { return common::Coordinator<Time>::is_atomic(); }
  72. virtual std::string to_string(int level) const
  73. { return common::Coordinator<Time>::to_string(level); }
  74. typename Time::type start(const typename Time::type &t)
  75. {
  76. assert(_graph_manager.children().size() > 0);
  77. type::_tl = t;
  78. type::_tn = t;
  79. for (auto &child : _graph_manager.children()) {
  80. child->start(t);
  81. }
  82. return type::_tn;
  83. }
  84. typename Time::type dispatch_events(const common::Bag<Time> &bag,
  85. const typename Time::type &t)
  86. {
  87. _graph_manager.dispatch_events(bag, t);
  88. return type::_tn;
  89. }
  90. void observation(std::ostream &file) const
  91. {
  92. for (auto &child : _graph_manager.children()) {
  93. child->observation(file);
  94. }
  95. }
  96. void output(const typename Time::type &t)
  97. {
  98. if (t == type::_tn) {
  99. for (auto &model : _graph_manager.children()) {
  100. model->update_buffer(t);
  101. }
  102. for (auto &model : _graph_manager.children()) {
  103. if (not model->is_send() && model->is_marked()) {
  104. model->output(t);
  105. model->send();
  106. }
  107. }
  108. }
  109. }
  110. void post_event(const typename Time::type &t,
  111. const common::ExternalEvent<Time> &event)
  112. {
  113. if (t == type::_tn) {
  114. _graph_manager.post_event(t, event);
  115. } else {
  116. _policy(t, event, type::_tl, type::_tn);
  117. }
  118. }
  119. typename Time::type transition(const typename Time::type &t)
  120. {
  121. if (t == type::_tn) {
  122. bool end = true;
  123. for (auto &event : _policy.bag()) {
  124. post_event(t, event);
  125. }
  126. for (auto &model : _graph_manager.children()) {
  127. if (not model->is_marked()) {
  128. if (model->all_ports_are_assigned()) {
  129. model->transition(t);
  130. model->mark();
  131. end = false;
  132. } else {
  133. end = false;
  134. }
  135. } else {
  136. if (not model->is_send()) {
  137. end = false;
  138. } else {
  139. if (t == model->get_tn()) {
  140. model->transition(t);
  141. }
  142. }
  143. }
  144. }
  145. if (end) {
  146. for (auto &model : _graph_manager.children()) {
  147. model->unmark();
  148. model->unsend();
  149. }
  150. type::_tl = t;
  151. type::_tn = t + _time_step;
  152. }
  153. }
  154. type::clear_bag();
  155. return type::_tn;
  156. }
  157. virtual void update_buffer(typename Time::type /* time */)
  158. {}
  159. private:
  160. GraphManager _graph_manager;
  161. typename Time::type _time_step;
  162. Policy _policy;
  163. };
  164. }
  165. } // namespace artis sss
  166. #endif