main.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477
  1. /**
  2. * @file app/main.cpp
  3. * @author The PARADEVS Development Team
  4. * See the AUTHORS or Authors.txt file
  5. */
  6. /*
  7. * PARADEVS - the multimodeling and simulation environment
  8. * This file is a part of the PARADEVS environment
  9. *
  10. * Copyright (C) 2013 ULCO http://www.univ-litoral.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. #include <boost/timer.hpp>
  26. #include <common/RootCoordinator.hpp>
  27. #include <common/scheduler/VectorScheduler.hpp>
  28. #include <common/scheduler/HeapScheduler.hpp>
  29. #include <kernel/pdevs/Coordinator.hpp>
  30. #include <tests/mixed/models.hpp>
  31. #include <tests/mixed/graph_manager.hpp>
  32. using namespace paradevs::tests::mixed;
  33. template < int size >
  34. void run_flat_with_heap()
  35. {
  36. boost::timer t;
  37. std::cout << "run_flat_with_heap [" << size << "] ..." << std::endl;
  38. paradevs::common::RootCoordinator <
  39. paradevs::common::DoubleTime, paradevs::pdevs::Coordinator <
  40. paradevs::common::DoubleTime,
  41. paradevs::common::scheduler::HeapScheduler <
  42. paradevs::common::DoubleTime, SchedulerHandle >,
  43. SchedulerHandle,
  44. LinearGraphManager < size, SchedulerHandle > >
  45. > rc(0, 100, "root", paradevs::common::NoParameters(),
  46. paradevs::common::NoParameters());
  47. paradevs::common::Trace < paradevs::common::DoubleTime >::trace().clear();
  48. rc.run();
  49. std::cout << "... OK -> " << t.elapsed() << std::endl;
  50. }
  51. template < int size >
  52. void run_flat_with_vector()
  53. {
  54. boost::timer t;
  55. std::cout << "run_flat_with_vector [" << size << "] ..." << std::endl;
  56. paradevs::common::RootCoordinator <
  57. paradevs::common::DoubleTime, paradevs::pdevs::Coordinator <
  58. paradevs::common::DoubleTime,
  59. paradevs::common::scheduler::VectorScheduler <
  60. paradevs::common::DoubleTime >,
  61. paradevs::common::scheduler::NoSchedulerHandle,
  62. LinearGraphManager <
  63. size, paradevs::common::scheduler::NoSchedulerHandle > >
  64. > rc(0, 100, "root", paradevs::common::NoParameters(),
  65. paradevs::common::NoParameters());
  66. paradevs::common::Trace < paradevs::common::DoubleTime >::trace().clear();
  67. rc.run();
  68. std::cout << "... OK -> " << t.elapsed() << std::endl;
  69. }
  70. void run_hierarchic_with_heap()
  71. {
  72. paradevs::common::RootCoordinator <
  73. paradevs::common::DoubleTime, paradevs::pdevs::Coordinator <
  74. paradevs::common::DoubleTime,
  75. paradevs::common::scheduler::HeapScheduler <
  76. paradevs::common::DoubleTime, SchedulerHandle >,
  77. SchedulerHandle,
  78. Root2GraphManager >
  79. > rc(0, 100, "root", paradevs::common::NoParameters(),
  80. paradevs::common::NoParameters());
  81. paradevs::common::Trace < paradevs::common::DoubleTime >::trace().clear();
  82. rc.run();
  83. }
  84. void run_hierarchic_with_vector()
  85. {
  86. paradevs::common::RootCoordinator <
  87. paradevs::common::DoubleTime, paradevs::pdevs::Coordinator <
  88. paradevs::common::DoubleTime,
  89. paradevs::common::scheduler::VectorScheduler <
  90. paradevs::common::DoubleTime >,
  91. paradevs::common::scheduler::NoSchedulerHandle,
  92. Root3GraphManager >
  93. > rc(0, 100, "root", paradevs::common::NoParameters(),
  94. paradevs::common::NoParameters());
  95. paradevs::common::Trace < paradevs::common::DoubleTime >::trace().clear();
  96. rc.run();
  97. }
  98. // int main()
  99. // {
  100. // boost::timer t;
  101. // srand(108364);
  102. // run_flat_with_heap < 10 >();
  103. // run_flat_with_heap < 20 >();
  104. // run_flat_with_heap < 30 >();
  105. // run_flat_with_heap < 40 >();
  106. // run_flat_with_heap < 50 >();
  107. // run_flat_with_heap < 60 >();
  108. // run_flat_with_heap < 70 >();
  109. // run_flat_with_heap < 80 >();
  110. // run_flat_with_heap < 90 >();
  111. // run_flat_with_heap < 100 >();
  112. // run_flat_with_heap < 200 >();
  113. // run_flat_with_vector < 10 >();
  114. // run_flat_with_vector < 20 >();
  115. // run_flat_with_vector < 30 >();
  116. // run_flat_with_vector < 40 >();
  117. // run_flat_with_vector < 50 >();
  118. // run_flat_with_vector < 60 >();
  119. // run_flat_with_vector < 70 >();
  120. // run_flat_with_vector < 80 >();
  121. // run_flat_with_vector < 90 >();
  122. // run_flat_with_vector < 100 >();
  123. // run_flat_with_vector < 200 >();
  124. // double t2 = t.elapsed();
  125. // std::cout << "run_hierarchic_with_heap ..." << std::endl;
  126. // run_hierarchic_with_heap();
  127. // double t3 = t.elapsed();
  128. // std::cout << "... OK -> " << (t3 - t2) << std::endl;
  129. // std::cout << "run_hierarchic_with_vector ..." << std::endl;
  130. // run_hierarchic_with_vector();
  131. // double t4 = t.elapsed();
  132. // std::cout << "... OK -> " << (t4 - t3) << std::endl;
  133. // return 0;
  134. // }
  135. #include <condition_variable>
  136. #include <queue>
  137. #include <memory>
  138. #include <mutex>
  139. #include <thread>
  140. namespace messaging {
  141. struct message_base
  142. {
  143. virtual ~message_base()
  144. {}
  145. };
  146. template < typename Msg >
  147. struct wrapped_message : message_base
  148. {
  149. Msg contents;
  150. explicit wrapped_message(Msg const& contents_) : contents(contents_)
  151. { }
  152. };
  153. class queue
  154. {
  155. public:
  156. template < typename T >
  157. void push(T const& msg)
  158. {
  159. std::lock_guard < std::mutex > lk(m);
  160. q.push(std::make_shared < wrapped_message < T > >(msg));
  161. c.notify_all();
  162. }
  163. std::shared_ptr < message_base > wait_and_pop()
  164. {
  165. std::unique_lock < std::mutex > lk(m);
  166. c.wait(lk, [&]{ return !q.empty(); });
  167. auto res = q.front();
  168. q.pop();
  169. return res;
  170. }
  171. private:
  172. std::mutex m;
  173. std::condition_variable c;
  174. std::queue < std::shared_ptr < message_base > > q;
  175. };
  176. class close_queue
  177. { };
  178. class sender
  179. {
  180. public:
  181. sender() : q(nullptr)
  182. { }
  183. explicit sender(queue* q_) : q(q_)
  184. { }
  185. template < typename Message >
  186. void send(Message const& msg)
  187. {
  188. if (q) {
  189. q->push(msg);
  190. }
  191. }
  192. private:
  193. queue* q;
  194. };
  195. template < typename PreviousDispatcher, typename Msg, typename Func >
  196. class TemplateDispatcher
  197. {
  198. public:
  199. TemplateDispatcher(TemplateDispatcher&& other) : q(other.q),
  200. prev(other.prev),
  201. f(std::move(other.f)),
  202. chained(other.chained)
  203. {
  204. other.chained = true;
  205. }
  206. TemplateDispatcher(queue* q_, PreviousDispatcher* prev_, Func&& f_) :
  207. q(q_), prev(prev_), f(std::forward < Func >(f_)), chained(false)
  208. {
  209. prev_->chained = true;
  210. }
  211. template < typename OtherMsg,
  212. typename OtherFunc >
  213. TemplateDispatcher < TemplateDispatcher, OtherMsg, OtherFunc >
  214. handle(OtherFunc&& of)
  215. {
  216. return TemplateDispatcher<
  217. TemplateDispatcher, OtherMsg, OtherFunc>(
  218. q, this, std::forward < OtherFunc >(of));
  219. }
  220. ~TemplateDispatcher() noexcept(false)
  221. {
  222. if (!chained) {
  223. wait_and_dispatch();
  224. }
  225. }
  226. private:
  227. TemplateDispatcher(TemplateDispatcher const&)=delete;
  228. TemplateDispatcher& operator=(TemplateDispatcher const&)=delete;
  229. template <typename Dispatcher, typename OtherMsg, typename OtherFunc >
  230. friend class TemplateDispatcher;
  231. void wait_and_dispatch()
  232. {
  233. for(;;) {
  234. auto msg = q->wait_and_pop();
  235. if (dispatch(msg)) {
  236. break;
  237. }
  238. }
  239. }
  240. bool dispatch(std::shared_ptr < message_base > const& msg)
  241. {
  242. if (wrapped_message < Msg >* wrapper =
  243. dynamic_cast < wrapped_message < Msg >* >(msg.get())) {
  244. f(wrapper->contents);
  245. return true;
  246. } else {
  247. return prev->dispatch(msg);
  248. }
  249. }
  250. queue* q;
  251. PreviousDispatcher* prev;
  252. Func f;
  253. bool chained;
  254. };
  255. class dispatcher
  256. {
  257. public:
  258. dispatcher(dispatcher&& other) : q(other.q), chained(other.chained)
  259. {
  260. other.chained = true;
  261. }
  262. explicit dispatcher(queue* q_) : q(q_), chained(false)
  263. {}
  264. template < typename Message, typename Func >
  265. TemplateDispatcher < dispatcher, Message, Func >
  266. handle(Func&& f)
  267. {
  268. return TemplateDispatcher < dispatcher, Message, Func >(
  269. q, this, std::forward < Func >(f));
  270. }
  271. ~dispatcher() noexcept(false)
  272. {
  273. if (!chained) {
  274. wait_and_dispatch();
  275. }
  276. }
  277. private:
  278. dispatcher(dispatcher const&)=delete;
  279. dispatcher& operator=(dispatcher const&)=delete;
  280. template<
  281. typename Dispatcher,
  282. typename Msg,
  283. typename Func>
  284. friend class TemplateDispatcher;
  285. void wait_and_dispatch()
  286. {
  287. for(;;) {
  288. auto msg = q->wait_and_pop();
  289. dispatch(msg);
  290. }
  291. }
  292. bool dispatch(std::shared_ptr < message_base > const& msg)
  293. {
  294. if (dynamic_cast < wrapped_message < close_queue >* >(msg.get())) {
  295. throw close_queue();
  296. }
  297. return false;
  298. }
  299. queue* q;
  300. bool chained;
  301. };
  302. class receiver
  303. {
  304. public:
  305. operator sender()
  306. {
  307. return sender(&q);
  308. }
  309. dispatcher wait()
  310. {
  311. return dispatcher(&q);
  312. }
  313. private:
  314. queue q;
  315. };
  316. } // namespace messaging
  317. struct start
  318. { };
  319. struct my_message
  320. { };
  321. class A
  322. {
  323. public:
  324. A(messaging::sender b_) : b(b_)
  325. { }
  326. void done()
  327. { get_sender().send(messaging::close_queue()); }
  328. messaging::sender get_sender()
  329. { return incoming; }
  330. void run()
  331. {
  332. try
  333. {
  334. for(;;) {
  335. incoming.wait().handle < start >(
  336. [&](start const& /* msg */)
  337. {
  338. std::cout << "start" << std::endl;
  339. b.send(my_message());
  340. }
  341. );;
  342. }
  343. }
  344. catch(messaging::close_queue const&)
  345. { }
  346. }
  347. private:
  348. messaging::receiver incoming;
  349. messaging::sender b;
  350. };
  351. class B
  352. {
  353. public:
  354. B()
  355. { }
  356. void done()
  357. { get_sender().send(messaging::close_queue()); }
  358. messaging::sender get_sender()
  359. { return incoming; }
  360. void run()
  361. {
  362. try
  363. {
  364. for(;;) {
  365. incoming.wait().handle < my_message >(
  366. [&](my_message const& /* msg */)
  367. { std::cout << "receive my message" << std::endl; }
  368. );
  369. }
  370. }
  371. catch(messaging::close_queue const&)
  372. { }
  373. }
  374. private:
  375. messaging::receiver incoming;
  376. };
  377. int main()
  378. {
  379. B b;
  380. A a(b.get_sender());
  381. std::thread A_thread(&A::run, &a);
  382. std::thread B_thread(&B::run, &b);
  383. a.get_sender().send(start());
  384. sleep(1);
  385. a.done();
  386. b.done();
  387. A_thread.join();
  388. B_thread.join();
  389. return 0;
  390. }