Coordinator.hpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. virtual bool is_atomic() const { return common::Coordinator<Time>::is_atomic(); }
  62. virtual std::string to_string(int level) const { return common::Coordinator<Time>::to_string(level); }
  63. typename Time::type start(const typename Time::type& t)
  64. {
  65. assert(_graph_manager.children().size() > 0);
  66. type::_tl = t;
  67. type::_tn = t;
  68. for (auto& child : _graph_manager.children()) {
  69. child->start(t);
  70. }
  71. return type::_tn;
  72. }
  73. typename Time::type dispatch_events(const common::Bag<Time>& bag,
  74. const typename Time::type& t)
  75. {
  76. _graph_manager.dispatch_events(bag, t);
  77. return type::_tn;
  78. }
  79. void observation(std::ostream& file) const
  80. {
  81. for (auto& child : _graph_manager.children()) {
  82. child->observation(file);
  83. }
  84. }
  85. void output(const typename Time::type& t)
  86. {
  87. if (t == type::_tn) {
  88. for (auto& model : _graph_manager.children()) {
  89. model->update_buffer(t);
  90. }
  91. for (auto& model : _graph_manager.children()) {
  92. if (not model->is_send() && model->is_marked()) {
  93. model->output(t);
  94. model->send();
  95. }
  96. }
  97. }
  98. }
  99. void post_event(const typename Time::type& t,
  100. const common::ExternalEvent<Time>& event)
  101. {
  102. if (t == type::_tn) {
  103. _graph_manager.post_event(t, event);
  104. } else {
  105. _policy(t, event, type::_tl, type::_tn);
  106. }
  107. }
  108. typename Time::type transition(const typename Time::type& t)
  109. {
  110. if (t == type::_tn) {
  111. bool end = true;
  112. for (auto& event : _policy.bag()) {
  113. post_event(t, event);
  114. }
  115. for (auto& model : _graph_manager.children()) {
  116. if (not model->is_marked()) {
  117. if (model->all_ports_are_assigned()) {
  118. model->transition(t);
  119. model->mark();
  120. end = false;
  121. } else {
  122. end = false;
  123. }
  124. } else {
  125. if (not model->is_send()) {
  126. end = false;
  127. } else {
  128. if (t == model->get_tn()) {
  129. model->transition(t);
  130. }
  131. }
  132. }
  133. }
  134. if (end) {
  135. for (auto& model : _graph_manager.children()) {
  136. model->unmark();
  137. model->unsend();
  138. }
  139. type::_tl = t;
  140. type::_tn = t + _time_step;
  141. }
  142. }
  143. type::clear_bag();
  144. return type::_tn;
  145. }
  146. virtual void update_buffer(typename Time::type /* time */) { }
  147. private:
  148. GraphManager _graph_manager;
  149. typename Time::type _time_step;
  150. Policy _policy;
  151. };
  152. }
  153. } // namespace artis sss
  154. #endif