Coordinator.hpp 6.3 KB

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