uniform_int_distribution.hpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2014 Roshan <thisisroshansmail@gmail.com>
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. // See http://boostorg.github.com/compute for more information.
  9. //---------------------------------------------------------------------------//
  10. #ifndef BOOST_COMPUTE_RANDOM_UNIFORM_INT_DISTRIBUTION_HPP
  11. #define BOOST_COMPUTE_RANDOM_UNIFORM_INT_DISTRIBUTION_HPP
  12. #include <limits>
  13. #include <boost/type_traits.hpp>
  14. #include <boost/static_assert.hpp>
  15. #include <boost/compute/command_queue.hpp>
  16. #include <boost/compute/container/vector.hpp>
  17. #include <boost/compute/function.hpp>
  18. #include <boost/compute/types/fundamental.hpp>
  19. #include <boost/compute/algorithm/copy_if.hpp>
  20. #include <boost/compute/algorithm/transform.hpp>
  21. namespace boost {
  22. namespace compute {
  23. /// \class uniform_int_distribution
  24. /// \brief Produces uniformily distributed random integers
  25. ///
  26. /// The following example shows how to setup a uniform int distribution to
  27. /// produce random integers 0 and 1.
  28. ///
  29. /// \snippet test/test_uniform_int_distribution.cpp generate
  30. ///
  31. template<class IntType = uint_>
  32. class uniform_int_distribution
  33. {
  34. public:
  35. typedef IntType result_type;
  36. /// Creates a new uniform distribution producing numbers in the range
  37. /// [\p a, \p b].
  38. explicit uniform_int_distribution(IntType a = 0,
  39. IntType b = (std::numeric_limits<IntType>::max)())
  40. : m_a(a),
  41. m_b(b)
  42. {
  43. }
  44. /// Destroys the uniform_int_distribution object.
  45. ~uniform_int_distribution()
  46. {
  47. }
  48. /// Returns the minimum value of the distribution.
  49. result_type a() const
  50. {
  51. return m_a;
  52. }
  53. /// Returns the maximum value of the distribution.
  54. result_type b() const
  55. {
  56. return m_b;
  57. }
  58. /// Generates uniformily distributed integers and stores
  59. /// them to the range [\p first, \p last).
  60. template<class OutputIterator, class Generator>
  61. void generate(OutputIterator first,
  62. OutputIterator last,
  63. Generator &generator,
  64. command_queue &queue)
  65. {
  66. size_t size = std::distance(first, last);
  67. typedef typename Generator::result_type g_result_type;
  68. vector<g_result_type> tmp(size, queue.get_context());
  69. vector<g_result_type> tmp2(size, queue.get_context());
  70. uint_ bound = ((uint_(-1))/(m_b-m_a+1))*(m_b-m_a+1);
  71. buffer_iterator<g_result_type> tmp2_iter;
  72. while(size>0)
  73. {
  74. generator.generate(tmp.begin(), tmp.begin() + size, queue);
  75. tmp2_iter = copy_if(tmp.begin(), tmp.begin() + size, tmp2.begin(),
  76. _1 <= bound, queue);
  77. size = std::distance(tmp2_iter, tmp2.end());
  78. }
  79. BOOST_COMPUTE_FUNCTION(IntType, scale_random, (const g_result_type x),
  80. {
  81. return LO + (x % (HI-LO+1));
  82. });
  83. scale_random.define("LO", boost::lexical_cast<std::string>(m_a));
  84. scale_random.define("HI", boost::lexical_cast<std::string>(m_b));
  85. transform(tmp2.begin(), tmp2.end(), first, scale_random, queue);
  86. }
  87. private:
  88. IntType m_a;
  89. IntType m_b;
  90. BOOST_STATIC_ASSERT_MSG(
  91. boost::is_integral<IntType>::value,
  92. "Template argument must be integral"
  93. );
  94. };
  95. } // end compute namespace
  96. } // end boost namespace
  97. #endif // BOOST_COMPUTE_RANDOM_UNIFORM_INT_DISTRIBUTION_HPP