Coordinator.hpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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-2021 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 = std::make_shared<std::thread>([&] { loop(); });
  96. }
  97. virtual ~Coordinator()
  98. {
  99. done();
  100. _thread->join();
  101. }
  102. void done()
  103. { get_sender().send(artis::common::Close()); }
  104. artis::common::Sender get_sender()
  105. { return _incoming.get_sender(); }
  106. void set_sender(common::Sender sender)
  107. { _sender = sender; }
  108. void loop()
  109. {
  110. try {
  111. while (true) {
  112. _incoming.wait()
  113. .template handle<start_message_type>(
  114. [this](start_message_type const &msg) {
  115. typename Time::type tn = start(msg._t);
  116. _sender.send(done_start_message_type(tn, this));
  117. })
  118. .
  119. template handle<done_start_message_type>(
  120. [this](done_start_message_type const &msg) {
  121. type::_event_table.init(msg._tn, msg._child);
  122. --_received;
  123. if (_received == 0) {
  124. std::unique_lock <std::mutex> lock(_received_mutex);
  125. _condition.notify_one();
  126. }
  127. })
  128. .
  129. template handle<transition_message_type>(
  130. [this](transition_message_type const &msg) {
  131. typename Time::type tn = transition(msg._t);
  132. _sender.send(done_transition_message_type(tn, this));
  133. })
  134. .
  135. template handle<done_transition_message_type>(
  136. [this](done_transition_message_type const &msg) {
  137. type::_event_table.put(msg._tn, msg._child);
  138. --_received;
  139. if (_received == 0) {
  140. std::unique_lock <std::mutex> lock(_received_mutex);
  141. _condition.notify_one();
  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. std::unique_lock <std::mutex> lock(_received_mutex);
  161. type::_graph_manager.start(t);
  162. _condition.wait(lock);
  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 =
  179. type::_event_table.get_current_models(t, type::_graph_manager.lookahead(t));
  180. _received = 0;
  181. for (auto &model : receivers) {
  182. if (model->is_atomic()) {
  183. type::_event_table.put(model->transition(t), model);
  184. } else {
  185. ++_received;
  186. }
  187. }
  188. for (auto &model : IMM) {
  189. if (std::find(receivers.begin(), receivers.end(), model) == receivers.end()) {
  190. if (model->is_atomic()) {
  191. type::_event_table.put(model->transition(t), model);
  192. } else {
  193. ++_received;
  194. }
  195. }
  196. }
  197. if (_received > 0) {
  198. std::unique_lock <std::mutex> lock(_received_mutex);
  199. type::_graph_manager.transition(receivers, t);
  200. type::_graph_manager.transition(IMM, t);
  201. _condition.wait(lock);
  202. }
  203. parent_type::update_event_table(t);
  204. type::_tl = t;
  205. type::_tn = type::_event_table.get_current_time();
  206. type::clear_bag();
  207. return type::_tn;
  208. }
  209. private:
  210. std::shared_ptr <std::thread> _thread;
  211. artis::common::Receiver _incoming;
  212. artis::common::Sender _sender;
  213. unsigned int _received;
  214. std::mutex _received_mutex;
  215. std::condition_variable _condition;
  216. };
  217. }
  218. }
  219. } // namespace artis pdevs multithreading
  220. #endif