Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:30:03

0001 //---------------------------------------------------------------------------//
0002 // Copyright (c) 2014 Roshan <thisisroshansmail@gmail.com>
0003 //
0004 // Distributed under the Boost Software License, Version 1.0
0005 // See accompanying file LICENSE_1_0.txt or copy at
0006 // http://www.boost.org/LICENSE_1_0.txt
0007 //
0008 // See http://boostorg.github.com/compute for more information.
0009 //---------------------------------------------------------------------------//
0010 
0011 #ifndef BOOST_COMPUTE_RANDOM_BERNOULLI_DISTRIBUTION_HPP
0012 #define BOOST_COMPUTE_RANDOM_BERNOULLI_DISTRIBUTION_HPP
0013 
0014 #include <boost/assert.hpp>
0015 #include <boost/type_traits.hpp>
0016 
0017 #include <boost/compute/command_queue.hpp>
0018 #include <boost/compute/function.hpp>
0019 #include <boost/compute/types/fundamental.hpp>
0020 #include <boost/compute/detail/iterator_range_size.hpp>
0021 #include <boost/compute/detail/literal.hpp>
0022 
0023 namespace boost {
0024 namespace compute {
0025 
0026 ///
0027 /// \class bernoulli_distribution
0028 /// \brief Produces random boolean values according to the following
0029 /// discrete probability function with parameter p :
0030 /// P(true/p) = p and P(false/p) = (1 - p)
0031 ///
0032 /// The following example shows how to setup a bernoulli distribution to
0033 /// produce random boolean values with parameter p = 0.25
0034 ///
0035 /// \snippet test/test_bernoulli_distribution.cpp generate
0036 ///
0037 template<class RealType = float>
0038 class bernoulli_distribution
0039 {
0040 public:
0041 
0042     /// Creates a new bernoulli distribution
0043     bernoulli_distribution(RealType p = 0.5f)
0044         : m_p(p)
0045     {
0046     }
0047 
0048     /// Destroys the bernoulli_distribution object
0049     ~bernoulli_distribution()
0050     {
0051     }
0052 
0053     /// Returns the value of the parameter p
0054     RealType p() const
0055     {
0056         return m_p;
0057     }
0058 
0059     /// Generates bernoulli distributed booleans and stores
0060     /// them in the range [\p first, \p last).
0061     template<class OutputIterator, class Generator>
0062     void generate(OutputIterator first,
0063                   OutputIterator last,
0064                   Generator &generator,
0065                   command_queue &queue)
0066     {
0067         size_t count = detail::iterator_range_size(first, last);
0068 
0069         vector<uint_> tmp(count, queue.get_context());
0070         generator.generate(tmp.begin(), tmp.end(), queue);
0071 
0072         BOOST_COMPUTE_FUNCTION(bool, scale_random, (const uint_ x),
0073         {
0074             return (convert_RealType(x) / MAX_RANDOM) < PARAM;
0075         });
0076 
0077         scale_random.define("PARAM", detail::make_literal(m_p));
0078         scale_random.define("MAX_RANDOM", "UINT_MAX");
0079         scale_random.define(
0080             "convert_RealType", std::string("convert_") + type_name<RealType>()
0081         );
0082 
0083         transform(
0084             tmp.begin(), tmp.end(), first, scale_random, queue
0085         );
0086     }
0087 
0088 private:
0089     RealType m_p;
0090 
0091     BOOST_STATIC_ASSERT_MSG(
0092         boost::is_floating_point<RealType>::value,
0093         "Template argument must be a floating point type"
0094     );
0095 };
0096 
0097 } // end compute namespace
0098 } // end boost namespace
0099 
0100 #endif // BOOST_COMPUTE_RANDOM_BERNOULLI_DISTRIBUTION_HPP