Coordinator.hpp 11 KB

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