Coordinator.hpp 11 KB

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