File indexing completed on 2025-01-18 09:30:03
0001
0002
0003
0004
0005
0006
0007
0008
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
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037 template<class RealType = float>
0038 class bernoulli_distribution
0039 {
0040 public:
0041
0042
0043 bernoulli_distribution(RealType p = 0.5f)
0044 : m_p(p)
0045 {
0046 }
0047
0048
0049 ~bernoulli_distribution()
0050 {
0051 }
0052
0053
0054 RealType p() const
0055 {
0056 return m_p;
0057 }
0058
0059
0060
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 }
0098 }
0099
0100 #endif