Coordinator.hpp 4.8 KB

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