Coordinator.hpp 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 1
  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. {
  85. type::_graph_manager.init();
  86. _thread = new std::thread([&] { loop(); });
  87. }
  88. virtual ~Coordinator()
  89. {
  90. done();
  91. _thread->join();
  92. delete _thread;
  93. }
  94. void done() { get_sender().send(artis::common::Close()); }
  95. artis::common::Sender get_sender() { return _incoming.get_sender(); }
  96. void set_sender(common::Sender sender) { _sender = sender; }
  97. void loop()
  98. {
  99. try {
  100. for (;;) {
  101. _incoming.wait()
  102. .template handle<start_message_type>(
  103. [&](start_message_type const& msg) {
  104. typename Time::type tn = start(msg._t);
  105. _sender.send(done_start_message_type(tn, this));
  106. })
  107. .
  108. template handle<done_start_message_type>(
  109. [&](done_start_message_type const& msg) {
  110. type::_event_table.init(msg._tn, msg._child);
  111. --_received;
  112. if (_received == 0) {
  113. _received_mutex.unlock();
  114. }
  115. })
  116. .
  117. template handle<transition_message_type>(
  118. [&](transition_message_type const& msg) {
  119. typename Time::type tn = transition(msg._t);
  120. _sender.send(done_transition_message_type(tn,
  121. this));
  122. })
  123. .
  124. template handle<done_transition_message_type>(
  125. [&](done_transition_message_type const& msg) {
  126. type::_event_table.put(msg._tn, msg._child);
  127. --_received;
  128. if (_received == 0) {
  129. _received_mutex.unlock();
  130. }
  131. });
  132. }
  133. }
  134. catch (artis::common::Close const&) { }
  135. }
  136. typename Time::type start(const typename Time::type& t)
  137. {
  138. _received = 0;
  139. for (auto& child : parent_type::_graph_manager.children()) {
  140. if (child->is_atomic()) {
  141. type::_event_table.init(child->start(type::_tn), child);
  142. } else {
  143. ++_received;
  144. }
  145. }
  146. if (_received > 0) {
  147. _received_mutex.lock();
  148. type::_graph_manager.start(t);
  149. std::lock_guard<std::mutex> lock(_received_mutex);
  150. }
  151. type::_tl = t;
  152. type::_tn = type::_event_table.get_current_time();
  153. return type::_tn;
  154. }
  155. // TODO: to remove
  156. virtual int get_receiver_number(typename Time::type t)
  157. {
  158. return type::_event_table.get_current_models(t).size();
  159. }
  160. typename Time::type transition(const typename Time::type& t)
  161. {
  162. assert(t >= type::_tl and t <= type::_tn);
  163. common::Models<Time> receivers = type::get_receivers();
  164. // common::Models<Time> IMM = type::_event_table.get_current_models(t);
  165. common::Models<Time> IMM = type::_event_table.get_current_models(t,
  166. type::_graph_manager.lookahead(t));
  167. _received = 0;
  168. for (auto& model : receivers) {
  169. if (model->is_atomic()) {
  170. type::_event_table.put(model->transition(t), model);
  171. } else {
  172. ++_received;
  173. }
  174. }
  175. for (auto& model : IMM) {
  176. if (std::find(receivers.begin(), receivers.end(),
  177. model) == receivers.end()) {
  178. if (model->is_atomic()) {
  179. type::_event_table.put(model->transition(t), model);
  180. } else {
  181. ++_received;
  182. }
  183. }
  184. }
  185. if (_received > 0) {
  186. _received_mutex.lock();
  187. type::_graph_manager.transition(receivers, t);
  188. type::_graph_manager.transition(IMM, t);
  189. std::lock_guard<std::mutex> lock(_received_mutex);
  190. }
  191. parent_type::update_event_table(t);
  192. type::_tl = t;
  193. type::_tn = type::_event_table.get_current_time();
  194. type::clear_bag();
  195. return type::_tn;
  196. }
  197. private:
  198. std::thread* _thread;
  199. artis::common::Receiver _incoming;
  200. artis::common::Sender _sender;
  201. unsigned int _received;
  202. std::mutex _received_mutex;
  203. };
  204. }
  205. }
  206. } // namespace artis pdevs multithreading
  207. #endif