pq_multi_thread_pass.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. // Copyright (C) 2014 Ian Forbed
  2. // Copyright (C) 2014 Vicente J. Botet Escriba
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. #include <boost/config.hpp>
  8. #if ! defined BOOST_NO_CXX11_DECLTYPE
  9. #define BOOST_RESULT_OF_USE_DECLTYPE
  10. #endif
  11. #define BOOST_THREAD_VERSION 4
  12. #define BOOST_THREAD_PROVIDES_EXECUTORS
  13. #include <exception>
  14. #include <boost/thread/thread.hpp>
  15. #include <boost/thread/barrier.hpp>
  16. #include <boost/thread/concurrent_queues/sync_priority_queue.hpp>
  17. #include <boost/core/lightweight_test.hpp>
  18. #ifdef BOOST_MSVC
  19. #pragma warning(disable: 4127) // conditional expression is constant
  20. #endif
  21. typedef boost::concurrent::sync_priority_queue<int> sync_pq;
  22. int call_pull(sync_pq* q, boost::barrier* go)
  23. {
  24. go->wait();
  25. return q->pull();
  26. }
  27. void call_push(sync_pq* q, boost::barrier* go, int val)
  28. {
  29. go->wait();
  30. q->push(val);
  31. }
  32. void test_pull(const int n)
  33. {
  34. sync_pq pq;
  35. BOOST_TEST(pq.empty());
  36. for(int i = 0; i < n; i++)
  37. {
  38. pq.push(i);
  39. }
  40. BOOST_TEST(!pq.empty());
  41. BOOST_TEST_EQ(pq.size(), std::size_t(n));
  42. pq.close();
  43. BOOST_TEST(pq.closed());
  44. boost::barrier b(n);
  45. boost::thread_group tg;
  46. for(int i = 0; i < n; i++)
  47. {
  48. tg.create_thread(boost::bind(call_pull, &pq, &b));
  49. }
  50. tg.join_all();
  51. BOOST_TEST(pq.empty());
  52. }
  53. void test_push(const int n)
  54. {
  55. sync_pq pq;
  56. BOOST_TEST(pq.empty());
  57. boost::barrier b(n);
  58. boost::thread_group tg;
  59. for(int i = 0; i < n; i++)
  60. {
  61. tg.create_thread(boost::bind(call_push, &pq, &b, i));
  62. }
  63. tg.join_all();
  64. BOOST_TEST(!pq.empty());
  65. BOOST_TEST_EQ(pq.size(), std::size_t(n));
  66. }
  67. void test_both(const int n)
  68. {
  69. sync_pq pq;
  70. BOOST_TEST(pq.empty());
  71. boost::barrier b(2*n);
  72. boost::thread_group tg;
  73. for(int i = 0; i < n; i++)
  74. {
  75. tg.create_thread(boost::bind(call_pull, &pq, &b));
  76. tg.create_thread(boost::bind(call_push, &pq, &b, i));
  77. }
  78. tg.join_all();
  79. BOOST_TEST(pq.empty());
  80. BOOST_TEST_EQ(pq.size(), std::size_t(0));
  81. }
  82. void push_range(sync_pq* q, const int begin, const int end)
  83. {
  84. for(int i = begin; i < end; i++)
  85. q->push(i);
  86. }
  87. void atomic_pull(sync_pq* q, boost::atomic<int>* sum)
  88. {
  89. while(1)
  90. {
  91. try{
  92. const int val = q->pull();
  93. sum->fetch_add(val);
  94. }
  95. catch(std::exception& ){
  96. break;
  97. }
  98. }
  99. }
  100. /**
  101. * This test computes the sum of the first N integers upto $limit using
  102. * $n threads for the push operation and $n threads for the pull and count
  103. * operation. The push operation push a range of numbers on the queue while
  104. * the pull operation pull from the queue and increments an atomic int.
  105. * At the end of execution the value of atomic<int> $sum should be the same
  106. * as n*(n+1)/2 as this is the closed form solution to this problem.
  107. */
  108. void compute_sum(const int n)
  109. {
  110. const int limit = 1000;
  111. sync_pq pq;
  112. BOOST_TEST(pq.empty());
  113. boost::atomic<int> sum(0);
  114. boost::thread_group tg1;
  115. boost::thread_group tg2;
  116. for(int i = 0; i < n; i++)
  117. {
  118. tg1.create_thread(boost::bind(push_range, &pq, i*(limit/n)+1, (i+1)*(limit/n)+1));
  119. tg2.create_thread(boost::bind(atomic_pull, &pq, &sum));
  120. }
  121. tg1.join_all();
  122. pq.close(); //Wait until all enqueuing is done before closing.
  123. BOOST_TEST(pq.closed());
  124. tg2.join_all();
  125. BOOST_TEST(pq.empty());
  126. BOOST_TEST_EQ(sum.load(), limit*(limit+1)/2);
  127. }
  128. void move_between_queues(sync_pq* q1, sync_pq* q2)
  129. {
  130. while(1){
  131. try{
  132. const int val = q1->pull();
  133. q2->push(val);
  134. }
  135. catch(std::exception& ){
  136. break;
  137. }
  138. }
  139. }
  140. /**
  141. * This test computes the sum of the first N integers upto $limit by moving
  142. * numbers between 2 sync_priority_queues. A range of numbers are pushed onto
  143. * one queue by $n threads while $n threads pull from this queue and push onto
  144. * another sync_pq. At the end the main thread ensures the the values in the
  145. * second queue are in proper order and then sums all the values from this
  146. * queue. The sum should match n*(n+1)/2, the closed form solution to this
  147. * problem.
  148. */
  149. void sum_with_moving(const int n)
  150. {
  151. const int limit = 1000;
  152. sync_pq pq1;
  153. sync_pq pq2;
  154. BOOST_TEST(pq1.empty());
  155. BOOST_TEST(pq2.empty());
  156. boost::thread_group tg1;
  157. boost::thread_group tg2;
  158. for(int i = 0; i < n; i++)
  159. {
  160. tg1.create_thread(boost::bind(push_range, &pq1, i*(limit/n)+1, (i+1)*(limit/n)+1));
  161. tg2.create_thread(boost::bind(move_between_queues, &pq1, &pq2));
  162. }
  163. tg1.join_all();
  164. pq1.close(); //Wait until all enqueuing is done before closing.
  165. BOOST_TEST(pq1.closed());
  166. tg2.join_all();
  167. BOOST_TEST(pq1.empty());
  168. BOOST_TEST(!pq2.empty());
  169. int sum = 0;
  170. for(int i = 1000; i > 0; i--){
  171. const int val = pq2.pull();
  172. BOOST_TEST_EQ(i,val);
  173. sum += val;
  174. }
  175. BOOST_TEST(pq2.empty());
  176. BOOST_TEST_EQ(sum, limit*(limit+1)/2);
  177. }
  178. int main()
  179. {
  180. for(int i = 1; i <= 64; i *= 2)
  181. {
  182. test_pull(i);
  183. test_push(i);
  184. test_both(i);
  185. }
  186. //These numbers must divide 1000
  187. compute_sum(1);
  188. compute_sum(4);
  189. compute_sum(10);
  190. compute_sum(25);
  191. compute_sum(50);
  192. sum_with_moving(1);
  193. sum_with_moving(4);
  194. sum_with_moving(10);
  195. sum_with_moving(25);
  196. sum_with_moving(50);
  197. return boost::report_errors();
  198. }