Coordinator.hpp 4.8 KB

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