Coordinator.hpp 7.0 KB

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