bernoulli_distribution.hpp 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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_BERNOULLI_DISTRIBUTION_HPP
  11. #define BOOST_COMPUTE_RANDOM_BERNOULLI_DISTRIBUTION_HPP
  12. #include <boost/compute/command_queue.hpp>
  13. #include <boost/compute/function.hpp>
  14. #include <boost/compute/types/fundamental.hpp>
  15. #include <boost/compute/detail/iterator_range_size.hpp>
  16. #include <boost/compute/detail/literal.hpp>
  17. namespace boost {
  18. namespace compute {
  19. ///
  20. /// \class bernoulli_distribution
  21. /// \brief Produces random boolean values according to the following
  22. /// discrete probability function with parameter p :
  23. /// P(true/p) = p and P(false/p) = (1 - p)
  24. ///
  25. /// The following example shows how to setup a bernoulli distribution to
  26. /// produce random boolean values with parameter p = 0.25
  27. ///
  28. /// \snippet test/test_bernoulli_distribution.cpp generate
  29. ///
  30. template<class RealType = float>
  31. class bernoulli_distribution
  32. {
  33. public:
  34. /// Creates a new bernoulli distribution
  35. bernoulli_distribution(RealType p = 0.5f)
  36. : m_p(p)
  37. {
  38. }
  39. /// Destroys the bernoulli_distribution object
  40. ~bernoulli_distribution()
  41. {
  42. }
  43. /// Returns the value of the parameter p
  44. RealType p() const
  45. {
  46. return m_p;
  47. }
  48. /// Generates bernoulli distributed booleans and stores
  49. /// them in the range [\p first, \p last).
  50. template<class OutputIterator, class Generator>
  51. void generate(OutputIterator first,
  52. OutputIterator last,
  53. Generator &generator,
  54. command_queue &queue)
  55. {
  56. size_t count = detail::iterator_range_size(first, last);
  57. vector<uint_> tmp(count, queue.get_context());
  58. generator.generate(tmp.begin(), tmp.end(), queue);
  59. BOOST_COMPUTE_FUNCTION(bool, scale_random, (const uint_ x),
  60. {
  61. return (convert_RealType(x) / MAX_RANDOM) < PARAM;
  62. });
  63. scale_random.define("PARAM", detail::make_literal(m_p));
  64. scale_random.define("MAX_RANDOM", "UINT_MAX");
  65. scale_random.define(
  66. "convert_RealType", std::string("convert_") + type_name<RealType>()
  67. );
  68. transform(
  69. tmp.begin(), tmp.end(), first, scale_random, queue
  70. );
  71. }
  72. private:
  73. RealType m_p;
  74. };
  75. } // end compute namespace
  76. } // end boost namespace
  77. #endif // BOOST_COMPUTE_RANDOM_BERNOULLI_DISTRIBUTION_HPP