sp_atomic_mt2_test.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. // Copyright (c) 2008 Peter Dimov
  2. //
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // See accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt
  6. #include <boost/config.hpp>
  7. #include <boost/shared_ptr.hpp>
  8. #include <boost/bind.hpp>
  9. #include <boost/thread/shared_mutex.hpp>
  10. #include <boost/thread/locks.hpp>
  11. #include <boost/detail/lightweight_mutex.hpp>
  12. #include <boost/detail/lightweight_thread.hpp>
  13. #include <vector>
  14. #include <numeric>
  15. #include <cstdio>
  16. #include <cstdlib>
  17. #include <cstddef>
  18. #include <ctime>
  19. //
  20. static void next_value( unsigned & v )
  21. {
  22. v = v % 2? 3 * v + 1: v / 2;
  23. }
  24. struct X
  25. {
  26. std::vector<unsigned> v_;
  27. explicit X( std::size_t n ): v_( n )
  28. {
  29. for( std::size_t i = 0; i < n; ++i )
  30. {
  31. v_[ i ] = i;
  32. }
  33. }
  34. unsigned get() const
  35. {
  36. return std::accumulate( v_.begin(), v_.end(), 0 );
  37. }
  38. void set()
  39. {
  40. std::for_each( v_.begin(), v_.end(), next_value );
  41. }
  42. };
  43. static boost::shared_ptr<X> ps;
  44. static boost::detail::lightweight_mutex lm;
  45. static boost::shared_mutex rw;
  46. enum prim_type
  47. {
  48. pt_mutex,
  49. pt_rwlock,
  50. pt_atomics
  51. };
  52. int read_access( prim_type pt )
  53. {
  54. switch( pt )
  55. {
  56. case pt_mutex:
  57. {
  58. boost::detail::lightweight_mutex::scoped_lock lock( lm );
  59. return ps->get();
  60. }
  61. case pt_rwlock:
  62. {
  63. boost::shared_lock<boost::shared_mutex> lock( rw );
  64. return ps->get();
  65. }
  66. case pt_atomics:
  67. {
  68. boost::shared_ptr<X> p2 = boost::atomic_load( &ps );
  69. return p2->get();
  70. }
  71. }
  72. }
  73. void write_access( prim_type pt )
  74. {
  75. switch( pt )
  76. {
  77. case pt_mutex:
  78. {
  79. boost::detail::lightweight_mutex::scoped_lock lock( lm );
  80. ps->set();
  81. }
  82. break;
  83. case pt_rwlock:
  84. {
  85. boost::unique_lock<boost::shared_mutex> lock( rw );
  86. ps->set();
  87. }
  88. break;
  89. case pt_atomics:
  90. {
  91. boost::shared_ptr<X> p1 = boost::atomic_load( &ps );
  92. for( ;; )
  93. {
  94. boost::shared_ptr<X> p2( new X( *p1 ) );
  95. p2->set();
  96. if( boost::atomic_compare_exchange( &ps, &p1, p2 ) ) break;
  97. }
  98. }
  99. break;
  100. }
  101. }
  102. void worker( int k, prim_type pt, int n, int r )
  103. {
  104. ++r;
  105. unsigned s = 0, nr = 0, nw = 0;
  106. for( int i = 0; i < n; ++i )
  107. {
  108. if( i % r )
  109. {
  110. s += read_access( pt );
  111. ++nr;
  112. }
  113. else
  114. {
  115. write_access( pt );
  116. ++s;
  117. ++nw;
  118. }
  119. }
  120. printf( "Worker %2d: %u:%u, %10u\n", k, nr, nw, s );
  121. }
  122. #if defined( BOOST_HAS_PTHREADS )
  123. char const * thmodel = "POSIX";
  124. #else
  125. char const * thmodel = "Windows";
  126. #endif
  127. char const * pt_to_string( prim_type pt )
  128. {
  129. switch( pt )
  130. {
  131. case pt_mutex:
  132. return "mutex";
  133. case pt_rwlock:
  134. return "rwlock";
  135. case pt_atomics:
  136. return "atomics";
  137. }
  138. }
  139. static void handle_pt_option( std::string const & opt, prim_type & pt, prim_type pt2 )
  140. {
  141. if( opt == pt_to_string( pt2 ) )
  142. {
  143. pt = pt2;
  144. }
  145. }
  146. static void handle_int_option( std::string const & opt, std::string const & prefix, int & k, int kmin, int kmax )
  147. {
  148. if( opt.substr( 0, prefix.size() ) == prefix )
  149. {
  150. int v = atoi( opt.substr( prefix.size() ).c_str() );
  151. if( v >= kmin && v <= kmax )
  152. {
  153. k = v;
  154. }
  155. }
  156. }
  157. int main( int ac, char const * av[] )
  158. {
  159. using namespace std; // printf, clock_t, clock
  160. int m = 4; // threads
  161. int n = 10000; // vector size
  162. int k = 1000000; // iterations
  163. int r = 100; // read/write ratio, r:1
  164. prim_type pt = pt_atomics;
  165. for( int i = 1; i < ac; ++i )
  166. {
  167. handle_pt_option( av[i], pt, pt_mutex );
  168. handle_pt_option( av[i], pt, pt_rwlock );
  169. handle_pt_option( av[i], pt, pt_atomics );
  170. handle_int_option( av[i], "n=", n, 1, INT_MAX );
  171. handle_int_option( av[i], "size=", n, 1, INT_MAX );
  172. handle_int_option( av[i], "k=", k, 1, INT_MAX );
  173. handle_int_option( av[i], "iterations=", k, 1, INT_MAX );
  174. handle_int_option( av[i], "m=", m, 1, INT_MAX );
  175. handle_int_option( av[i], "threads=", m, 1, INT_MAX );
  176. handle_int_option( av[i], "r=", r, 1, INT_MAX );
  177. handle_int_option( av[i], "ratio=", r, 1, INT_MAX );
  178. }
  179. printf( "%s: threads=%d size=%d iterations=%d ratio=%d %s\n\n", thmodel, m, n, k, r, pt_to_string( pt ) );
  180. ps.reset( new X( n ) );
  181. clock_t t = clock();
  182. std::vector<boost::detail::lw_thread_t> a( m );
  183. for( int i = 0; i < m; ++i )
  184. {
  185. boost::detail::lw_thread_create( a[ i ], boost::bind( worker, i, pt, k, r ) );
  186. }
  187. for( int j = 0; j < m; ++j )
  188. {
  189. boost::detail::lw_thread_join( a[ j ] );
  190. }
  191. t = clock() - t;
  192. double ts = static_cast<double>( t ) / CLOCKS_PER_SEC;
  193. printf( "%.3f seconds, %.3f accesses per microsecond.\n", ts, m * k / ts / 1e+6 );
  194. }