Coordinator.hpp 9.5 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 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 : public pdevs::Coordinator<Time, GraphManager, Parameters, GraphParameters> {
  68. typedef pdevs::Coordinator<Time, GraphManager,
  69. Parameters, GraphParameters> parent_type;
  70. typedef Coordinator<Time, GraphManager,
  71. Parameters, GraphParameters> type;
  72. typedef done_start_message<Time> done_start_message_type;
  73. typedef start_message<Time> start_message_type;
  74. typedef done_transition_message<Time> done_transition_message_type;
  75. typedef transition_message<Time> transition_message_type;
  76. public:
  77. Coordinator(const std::string& name, const Parameters& parameters,
  78. const GraphParameters& graph_parameters)
  79. :
  80. common::Model<Time>(name),
  81. pdevs::Coordinator<Time, GraphManager, Parameters, GraphParameters>(
  82. name, parameters, graph_parameters)
  83. {
  84. type::_graph_manager.init();
  85. _thread = new std::thread([&] { loop(); });
  86. }
  87. virtual ~Coordinator()
  88. {
  89. done();
  90. _thread->join();
  91. delete _thread;
  92. }
  93. void done() { get_sender().send(artis::common::Close()); }
  94. artis::common::Sender get_sender() { return _incoming; }
  95. void set_sender(common::Sender sender) { _sender = sender; }
  96. void loop()
  97. {
  98. try {
  99. for (;;) {
  100. _incoming.wait()
  101. .template handle<start_message_type>(
  102. [&](start_message_type const& msg) {
  103. typename Time::type tn = start(msg._t);
  104. _sender.send(done_start_message_type(tn, this));
  105. })
  106. .
  107. template handle<done_start_message_type>(
  108. [&](done_start_message_type const& msg) {
  109. type::_event_table.init(msg._tn, msg._child);
  110. --_received;
  111. if (_received == 0) {
  112. _received_mutex.unlock();
  113. }
  114. })
  115. .
  116. template handle<transition_message_type>(
  117. [&](transition_message_type const& msg) {
  118. typename Time::type tn = transition(msg._t);
  119. _sender.send(done_transition_message_type(tn,
  120. this));
  121. })
  122. .
  123. template handle<done_transition_message_type>(
  124. [&](done_transition_message_type const& msg) {
  125. type::_event_table.put(msg._tn, msg._child);
  126. --_received;
  127. if (_received == 0) {
  128. _received_mutex.unlock();
  129. }
  130. });
  131. }
  132. }
  133. catch (artis::common::Close const&) { }
  134. }
  135. typename Time::type start(const typename Time::type& t)
  136. {
  137. _received = 0;
  138. for (auto& child : parent_type::_graph_manager.children()) {
  139. if (child->is_atomic()) {
  140. type::_event_table.init(child->start(type::_tn), child);
  141. } else {
  142. ++_received;
  143. }
  144. }
  145. if (_received > 0) {
  146. _received_mutex.lock();
  147. type::_graph_manager.start(t);
  148. std::lock_guard<std::mutex> lock(_received_mutex);
  149. }
  150. type::_tl = t;
  151. type::_tn = type::_event_table.get_current_time();
  152. return type::_tn;
  153. }
  154. // TODO: to remove
  155. virtual int get_receiver_number(typename Time::type t)
  156. {
  157. return type::_event_table.get_current_models(t).size();
  158. }
  159. typename Time::type transition(const typename Time::type& t)
  160. {
  161. assert(t >= type::_tl and t <= type::_tn);
  162. common::Models<Time> receivers = type::get_receivers();
  163. common::Models<Time> IMM = type::_event_table.get_current_models(t);
  164. _received = 0;
  165. for (auto& model : receivers) {
  166. if (model->is_atomic()) {
  167. type::_event_table.put(model->transition(t), model);
  168. } else {
  169. ++_received;
  170. }
  171. }
  172. for (auto& model : IMM) {
  173. if (std::find(receivers.begin(), receivers.end(),
  174. model) == receivers.end()) {
  175. if (model->is_atomic()) {
  176. type::_event_table.put(model->transition(t), model);
  177. } else {
  178. ++_received;
  179. }
  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. }
  201. }
  202. } // namespace artis pdevs multithreading
  203. #endif