Coordinator.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /**
  2. * @file kernel/pdevs/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-2018 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 PDEVS_COORDINATOR
  26. #define PDEVS_COORDINATOR 1
  27. #include <artis-star/common/Coordinator.hpp>
  28. #include <artis-star/common/Parameters.hpp>
  29. #include <artis-star/common/Scheduler.hpp>
  30. #include <artis-star/common/utils/String.hpp>
  31. #include <artis-star/common/utils/Trace.hpp>
  32. #include <cassert>
  33. #include <iostream>
  34. namespace artis { namespace pdevs {
  35. template < class Time,
  36. class GraphManager,
  37. class Parameters = common::NoParameters,
  38. class GraphParameters = common::NoParameters >
  39. class Coordinator : public common::Coordinator < Time >
  40. {
  41. typedef Coordinator < Time, GraphManager,
  42. Parameters, GraphParameters > type;
  43. public:
  44. typedef Parameters parameters_type;
  45. typedef GraphParameters graph_parameters_type;
  46. Coordinator(const std::string& name,
  47. const Parameters& parameters,
  48. const GraphParameters& graph_parameters) :
  49. common::Model < Time >(name),
  50. common::Coordinator < Time >(name),
  51. _graph_manager(this, parameters, graph_parameters)
  52. { }
  53. virtual ~Coordinator()
  54. { }
  55. const common::GraphManager < Time >& get_graph_manager() const
  56. { return _graph_manager; }
  57. virtual std::string to_string(int level) const
  58. {
  59. std::ostringstream ss;
  60. ss << common::String::make_spaces(level * 2) << "p-devs coordinator \""
  61. << type::get_name() << "\":" << std::endl;
  62. ss << _graph_manager.to_string(level + 1);
  63. return ss.str();
  64. }
  65. typename Time::type start(const typename Time::type& t)
  66. {
  67. #ifdef WITH_TRACE
  68. common::Trace < Time >::trace()
  69. << common::TraceElement < Time >(type::get_name(), t,
  70. common::I_MESSAGE)
  71. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  72. << type::_tn;
  73. common::Trace < Time >::trace().flush();
  74. #endif
  75. assert(_graph_manager.children().size() > 0);
  76. for (auto & child : _graph_manager.children()) {
  77. _event_table.init(child->start(t), child);
  78. }
  79. type::_tl = t;
  80. type::_tn = _event_table.get_current_time();
  81. #ifdef WITH_TRACE
  82. common::Trace < Time >::trace()
  83. << common::TraceElement < Time >(type::get_name(), t,
  84. common::I_MESSAGE)
  85. << ": AFTER => " << "tl = " << type::_tl
  86. << " ; tn = " << type::_tn;
  87. common::Trace < Time >::trace().flush();
  88. #endif
  89. return type::_tn;
  90. }
  91. /**************************************************
  92. * when *-message(t)
  93. * calculate IMM (models with tn = t in scheduler
  94. * calculate INF from IMM
  95. * for each e in IMM U INF
  96. * calculate influencer
  97. * ...
  98. * send done to parent
  99. **************************************************/
  100. void output(const typename Time::type& t)
  101. {
  102. #ifdef WITH_TRACE
  103. common::Trace < Time >::trace()
  104. << common::TraceElement < Time >(type::get_name(), t,
  105. common::OUTPUT)
  106. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  107. << type::_tn << " ; scheduler = " << _event_table.to_string();
  108. common::Trace < Time >::trace().flush();
  109. #endif
  110. assert(t == type::_tn);
  111. common::Models < Time > IMM =
  112. _event_table.get_current_models(t);
  113. #ifdef WITH_TRACE
  114. common::Trace < Time >::trace()
  115. << common::TraceElement < Time >(type::get_name(), t,
  116. common::OUTPUT)
  117. << ": IMM = " << IMM.to_string();
  118. common::Trace < Time >::trace().flush();
  119. #endif
  120. for (auto & model : IMM) {
  121. model->output(t);
  122. }
  123. #ifdef WITH_TRACE
  124. common::Trace < Time >::trace()
  125. << common::TraceElement < Time >(type::get_name(), t,
  126. common::OUTPUT)
  127. << ": AFTER => " << "tl = " << type::_tl << " ; tn = "
  128. << type::_tn << " ; scheduler = " << _event_table.to_string();
  129. common::Trace < Time >::trace().flush();
  130. #endif
  131. }
  132. /*******************************************************************
  133. * when x-message(t)
  134. * receivers = { r | r in children, N in Ir, Z(N,r)(x) isn't empty
  135. * for each r in receivers
  136. * send x-message(Z(N,r)(x), t) with input value Z(N,r)(x) to r
  137. * for each r in IMM and not in receivers
  138. * send x-message(empty, t) to r
  139. * sort event list acocrding to tn
  140. * tl = t
  141. * tn = min(tn_d | d in D)
  142. *******************************************************************/
  143. typename Time::type transition(const typename Time::type& t)
  144. {
  145. #ifdef WITH_TRACE
  146. common::Trace < Time >::trace()
  147. << common::TraceElement < Time >(type::get_name(), t,
  148. common::S_MESSAGE)
  149. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  150. << type::_tn << " ; scheduler = " << _event_table.to_string();
  151. common::Trace < Time >::trace().flush();
  152. #endif
  153. assert(t >= type::_tl and t <= type::_tn);
  154. common::Models < Time > receivers =
  155. _event_table.get_current_models(t);
  156. add_models_with_inputs(receivers);
  157. #ifdef WITH_TRACE
  158. common::Trace < Time >::trace()
  159. << common::TraceElement < Time >(type::get_name(), t,
  160. common::S_MESSAGE)
  161. << ": receivers = " << receivers.to_string();
  162. common::Trace < Time >::trace().flush();
  163. #endif
  164. for (auto & model : receivers) {
  165. _event_table.put(model->transition(t), model);
  166. }
  167. update_event_table(t);
  168. type::_tl = t;
  169. type::_tn = _event_table.get_current_time();
  170. type::clear_bag();
  171. #ifdef WITH_TRACE
  172. common::Trace < Time >::trace()
  173. << common::TraceElement < Time >(type::get_name(), t,
  174. common::S_MESSAGE)
  175. << ": AFTER => " << "tl = " << type::_tl << " ; tn = "
  176. << type::_tn << " ; scheduler = " << _event_table.to_string();
  177. common::Trace < Time >::trace().flush();
  178. #endif
  179. return type::_tn;
  180. }
  181. void post_event(const typename Time::type& t,
  182. const common::ExternalEvent < Time >& event)
  183. {
  184. #ifdef WITH_TRACE
  185. common::Trace < Time >::trace()
  186. << common::TraceElement < Time >(type::get_name(), t,
  187. common::POST_EVENT)
  188. << ": BEFORE => " << event.to_string();
  189. common::Trace < Time >::trace().flush();
  190. #endif
  191. type::add_event(event);
  192. _graph_manager.post_event(t, event);
  193. update_event_table(t);
  194. type::_tn = _event_table.get_current_time();
  195. #ifdef WITH_TRACE
  196. common::Trace < Time >::trace()
  197. << common::TraceElement < Time >(type::get_name(), t,
  198. common::POST_EVENT)
  199. << ": AFTER => " << event.to_string();
  200. common::Trace < Time >::trace().flush();
  201. #endif
  202. }
  203. /*******************************************************************
  204. * when y-message(y_d, t) with output y_d from d
  205. *******************************************************************/
  206. typename Time::type dispatch_events(
  207. const common::Bag < Time >& bag,
  208. const typename Time::type& t)
  209. {
  210. #ifdef WITH_TRACE
  211. common::Trace < Time >::trace()
  212. << common::TraceElement < Time >( type::get_name(), t,
  213. common::Y_MESSAGE)
  214. << ": BEFORE => " << "tl = " << type::_tl << " ; tn = "
  215. << type::_tn << " ; bag = " << bag.to_string()
  216. << " ; " << _event_table.to_string();
  217. common::Trace < Time >::trace().flush();
  218. #endif
  219. _graph_manager.dispatch_events(bag, t);
  220. update_event_table(t);
  221. type::_tn = _event_table.get_current_time();
  222. #ifdef WITH_TRACE
  223. common::Trace < Time >::trace()
  224. << common::TraceElement < Time >(type::get_name(), t,
  225. common::Y_MESSAGE)
  226. << ": AFTER => " << "tl = " << type::_tl << " ; tn = " << type::_tn
  227. << " ; " << _event_table.to_string();
  228. common::Trace < Time >::trace().flush();
  229. #endif
  230. return type::_tn;
  231. }
  232. common::Value observe(const typename Time::type& /* t */,
  233. unsigned int /* index */) const
  234. {
  235. assert(false);
  236. return common::Value();
  237. }
  238. void add_models_with_inputs(
  239. common::Models < Time >& receivers)
  240. {
  241. for (auto & model : _graph_manager.children()) {
  242. if (model->event_number() > 0) {
  243. if (std::find(receivers.begin(), receivers.end(),
  244. model) == receivers.end()) {
  245. receivers.push_back(model);
  246. }
  247. }
  248. }
  249. }
  250. void update_event_table(typename Time::type t)
  251. {
  252. for (auto & model : _graph_manager.children()) {
  253. if (model->event_number() > 0) {
  254. _event_table.put(t, model);
  255. }
  256. }
  257. }
  258. protected:
  259. GraphManager _graph_manager;
  260. common::SchedulerType _event_table;
  261. };
  262. } } // namespace artis pdevs
  263. #endif