Coordinator.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /**
  2. * @file kernel/pdevs/multithreading/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_MULTITHREADING_COORDINATOR
  26. #define PDEVS_MULTITHREADING_COORDINATOR
  27. #include <artis-star/common/utils/Multithreading.hpp>
  28. #include <artis-star/kernel/pdevs/Coordinator.hpp>
  29. #include <thread>
  30. namespace artis {
  31. namespace pdevs {
  32. namespace multithreading {
  33. template<class Time>
  34. struct start_message
  35. {
  36. explicit start_message(typename Time::type t)
  37. : _t(t)
  38. {}
  39. typename Time::type _t;
  40. };
  41. template<class Time>
  42. struct transition_message
  43. {
  44. explicit transition_message(typename Time::type t)
  45. : _t(t)
  46. {}
  47. typename Time::type _t;
  48. };
  49. template<class Time>
  50. struct done_start_message
  51. {
  52. explicit done_start_message(typename Time::type tn,
  53. common::Model<Time> *child)
  54. :
  55. _tn(tn), _child(child)
  56. {}
  57. typename Time::type _tn;
  58. common::Model<Time> *_child;
  59. };
  60. template<class Time>
  61. struct done_transition_message
  62. {
  63. explicit done_transition_message(typename Time::type tn,
  64. common::Model<Time> *child)
  65. :
  66. _tn(tn), _child(child)
  67. {}
  68. typename Time::type _tn;
  69. common::Model<Time> *_child;
  70. };
  71. template<class Time,
  72. class GraphManager,
  73. class Parameters = common::NoParameters,
  74. class GraphParameters = common::NoParameters>
  75. class Coordinator
  76. : public pdevs::Coordinator<Time, GraphManager, Parameters, GraphParameters>
  77. {
  78. typedef pdevs::Coordinator<Time, GraphManager,
  79. Parameters, GraphParameters> parent_type;
  80. typedef Coordinator<Time, GraphManager,
  81. Parameters, GraphParameters> type;
  82. typedef done_start_message<Time> done_start_message_type;
  83. typedef start_message<Time> start_message_type;
  84. typedef done_transition_message<Time> done_transition_message_type;
  85. typedef transition_message<Time> transition_message_type;
  86. public:
  87. Coordinator(const std::string &name, const Parameters &parameters,
  88. const GraphParameters &graph_parameters)
  89. :
  90. common::Model<Time>(name),
  91. pdevs::Coordinator<Time, GraphManager, Parameters, GraphParameters>(
  92. name, parameters, graph_parameters)
  93. {
  94. type::_graph_manager.init();
  95. _thread = new std::thread([&] { loop(); });
  96. }
  97. virtual ~Coordinator()
  98. {
  99. done();
  100. _thread->join();
  101. delete _thread;
  102. }
  103. void done()
  104. { get_sender().send(artis::common::Close()); }
  105. artis::common::Sender get_sender()
  106. { return _incoming.get_sender(); }
  107. void set_sender(common::Sender sender)
  108. { _sender = sender; }
  109. void loop()
  110. {
  111. try {
  112. for (;;) {
  113. _incoming.wait()
  114. .template handle<start_message_type>(
  115. [&](start_message_type const &msg) {
  116. typename Time::type tn = start(msg._t);
  117. _sender.send(done_start_message_type(tn, this));
  118. })
  119. .
  120. template handle<done_start_message_type>(
  121. [&](done_start_message_type const &msg) {
  122. type::_event_table.init(msg._tn, msg._child);
  123. --_received;
  124. if (_received == 0) {
  125. _received_mutex.unlock();
  126. }
  127. })
  128. .
  129. template handle<transition_message_type>(
  130. [&](transition_message_type const &msg) {
  131. typename Time::type tn = transition(msg._t);
  132. _sender.send(done_transition_message_type(tn,
  133. this));
  134. })
  135. .
  136. template handle<done_transition_message_type>(
  137. [&](done_transition_message_type const &msg) {
  138. type::_event_table.put(msg._tn, msg._child);
  139. --_received;
  140. if (_received == 0) {
  141. _received_mutex.unlock();
  142. }
  143. });
  144. }
  145. }
  146. catch (artis::common::Close const &) {
  147. }
  148. }
  149. typename Time::type start(const typename Time::type &t)
  150. {
  151. _received = 0;
  152. for (auto &child : parent_type::_graph_manager.children()) {
  153. if (child->is_atomic()) {
  154. type::_event_table.init(child->start(type::_tn), child);
  155. } else {
  156. ++_received;
  157. }
  158. }
  159. if (_received > 0) {
  160. _received_mutex.lock();
  161. type::_graph_manager.start(t);
  162. std::lock_guard<std::mutex> lock(_received_mutex);
  163. }
  164. type::_tl = t;
  165. type::_tn = type::_event_table.get_current_time();
  166. return type::_tn;
  167. }
  168. // TODO: to remove
  169. virtual int get_receiver_number(typename Time::type t)
  170. {
  171. return type::_event_table.get_current_models(t).size();
  172. }
  173. typename Time::type transition(const typename Time::type &t)
  174. {
  175. assert(t >= type::_tl and t <= type::_tn);
  176. common::Models<Time> receivers = type::get_receivers();
  177. // common::Models<Time> IMM = type::_event_table.get_current_models(t);
  178. common::Models<Time> IMM = type::_event_table.get_current_models(t,
  179. type::_graph_manager
  180. .lookahead(t));
  181. _received = 0;
  182. for (auto &model : receivers) {
  183. if (model->is_atomic()) {
  184. type::_event_table.put(model->transition(t), model);
  185. } else {
  186. ++_received;
  187. }
  188. }
  189. for (auto &model : IMM) {
  190. if (std::find(receivers.begin(), receivers.end(),
  191. model) == receivers.end()) {
  192. if (model->is_atomic()) {
  193. type::_event_table.put(model->transition(t), model);
  194. } else {
  195. ++_received;
  196. }
  197. }
  198. }
  199. if (_received > 0) {
  200. _received_mutex.lock();
  201. type::_graph_manager.transition(receivers, t);
  202. type::_graph_manager.transition(IMM, t);
  203. std::lock_guard<std::mutex> lock(_received_mutex);
  204. }
  205. parent_type::update_event_table(t);
  206. type::_tl = t;
  207. type::_tn = type::_event_table.get_current_time();
  208. type::clear_bag();
  209. return type::_tn;
  210. }
  211. private:
  212. std::thread *_thread;
  213. artis::common::Receiver _incoming;
  214. artis::common::Sender _sender;
  215. unsigned int _received;
  216. std::mutex _received_mutex;
  217. };
  218. }
  219. }
  220. } // namespace artis pdevs multithreading
  221. #endif