Coordinator.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /**
  2. * @file kernel/sss/Coordinator.hpp
  3. * @author The PARADEVS Development Team
  4. * See the AUTHORS or Authors.txt file
  5. */
  6. /*
  7. * PARADEVS - the multimodeling and simulation environment
  8. * This file is a part of the PARADEVS environment
  9. *
  10. * Copyright (C) 2013-2015 ULCO http://www.univ-litoral.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 <paradevs/common/Coordinator.hpp>
  28. #include <paradevs/common/Parameters.hpp>
  29. #include <paradevs/common/utils/Trace.hpp>
  30. #include <paradevs/kernel/sss/Model.hpp>
  31. #include <cassert>
  32. #include <iostream>
  33. namespace paradevs { namespace sss {
  34. template < class Time >
  35. class Parameters
  36. {
  37. public:
  38. Parameters(typename Time::type time_step) : _time_step(time_step)
  39. { }
  40. typename Time::type _time_step;
  41. };
  42. template < class Time, class Policy, class GraphManager,
  43. class SchedulerHandle, class Parameters = Parameters < Time >,
  44. class GraphParameters = common::NoParameters >
  45. class Coordinator : public common::Coordinator < Time, SchedulerHandle >,
  46. public sss::Model < Time, SchedulerHandle >
  47. {
  48. typedef Coordinator < Time, Policy, GraphManager, SchedulerHandle,
  49. Parameters, GraphParameters > type;
  50. public:
  51. typedef Parameters parameters_type;
  52. typedef GraphParameters graph_parameters_type;
  53. Coordinator(const std::string& name,
  54. const Parameters& parameters,
  55. const GraphParameters& graph_paramaters) :
  56. common::Model < Time, SchedulerHandle >(name),
  57. common::Coordinator < Time, SchedulerHandle >(name),
  58. sss::Model < Time, SchedulerHandle >(name),
  59. _graph_manager(this, graph_paramaters),
  60. _time_step(parameters._time_step)
  61. { }
  62. virtual ~Coordinator()
  63. { }
  64. virtual bool is_atomic() const
  65. { return common::Coordinator < Time, SchedulerHandle >::is_atomic(); }
  66. virtual std::string to_string(int level) const
  67. { return common::Coordinator < Time, SchedulerHandle >::to_string(level); }
  68. typename Time::type start(typename Time::type t)
  69. {
  70. assert(_graph_manager.children().size() > 0);
  71. type::_tl = t;
  72. type::_tn = t;
  73. for (auto & child : _graph_manager.children()) {
  74. child->start(t);
  75. }
  76. return type::_tn;
  77. }
  78. typename Time::type dispatch_events(common::Bag < Time,
  79. SchedulerHandle > bag,
  80. typename Time::type t)
  81. {
  82. _graph_manager.dispatch_events(bag, t);
  83. return type::_tn;
  84. }
  85. void observation(std::ostream& file) const
  86. {
  87. for (auto & child : _graph_manager.children()) {
  88. child->observation(file);
  89. }
  90. }
  91. void output(typename Time::type t)
  92. {
  93. if (t == type::_tn) {
  94. for (auto & model : _graph_manager.children()) {
  95. model->update_buffer(t);
  96. }
  97. for (auto & model : _graph_manager.children()) {
  98. if (not model->is_send() && model->is_marked()) {
  99. model->output(t);
  100. model->send();
  101. }
  102. }
  103. }
  104. }
  105. void post_event(typename Time::type t,
  106. const common::ExternalEvent < Time,
  107. SchedulerHandle >& event)
  108. {
  109. if (t == type::_tn) {
  110. _graph_manager.post_event(t, event);
  111. } else {
  112. _policy(t, event, type::_tl, type::_tn);
  113. }
  114. }
  115. typename Time::type transition(typename Time::type t)
  116. {
  117. if (t == type::_tn) {
  118. bool end = true;
  119. for (auto & event : _policy.bag()) {
  120. post_event(t, event);
  121. }
  122. for (auto & model : _graph_manager.children()) {
  123. if (not model->is_marked()) {
  124. if (model->all_ports_are_assigned()) {
  125. model->transition(t);
  126. model->mark();
  127. end = false;
  128. } else {
  129. end = false;
  130. }
  131. } else {
  132. if (not model->is_send()) {
  133. end = false;
  134. } else {
  135. if (t == model->get_tn()) {
  136. model->transition(t);
  137. }
  138. }
  139. }
  140. }
  141. if (end) {
  142. for (auto & model : _graph_manager.children()) {
  143. model->unmark();
  144. model->unsend();
  145. }
  146. type::_tl = t;
  147. type::_tn = t + _time_step;
  148. }
  149. }
  150. type::clear_bag();
  151. return type::_tn;
  152. }
  153. virtual void update_buffer(typename Time::type /* time */)
  154. { }
  155. private:
  156. GraphManager _graph_manager;
  157. typename Time::type _time_step;
  158. Policy _policy;
  159. };
  160. } } // namespace paradevs sss
  161. #endif