Coordinator.hpp 7.3 KB

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