Coordinator.hpp 6.1 KB

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