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_UNIFORM_REAL_DISTRIBUTION_HPP
0012 #define BOOST_COMPUTE_RANDOM_UNIFORM_REAL_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/detail/literal.hpp>
0020 #include <boost/compute/types/fundamental.hpp>
0021
0022 namespace boost {
0023 namespace compute {
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034 template<class RealType = float>
0035 class uniform_real_distribution
0036 {
0037 public:
0038 typedef RealType result_type;
0039
0040
0041
0042
0043 uniform_real_distribution(RealType a = 0.f, RealType b = 1.f)
0044 : m_a(a),
0045 m_b(b)
0046 {
0047 BOOST_ASSERT(a < b);
0048 }
0049
0050
0051 ~uniform_real_distribution()
0052 {
0053 }
0054
0055
0056 result_type a() const
0057 {
0058 return m_a;
0059 }
0060
0061
0062 result_type b() const
0063 {
0064 return m_b;
0065 }
0066
0067
0068
0069 template<class OutputIterator, class Generator>
0070 void generate(OutputIterator first,
0071 OutputIterator last,
0072 Generator &generator,
0073 command_queue &queue)
0074 {
0075 BOOST_COMPUTE_FUNCTION(RealType, scale_random, (const uint_ x),
0076 {
0077 return nextafter(LO + (convert_RealType(x) / MAX_RANDOM) * (HI - LO), (RealType) LO);
0078 });
0079
0080 scale_random.define("LO", detail::make_literal(m_a));
0081 scale_random.define("HI", detail::make_literal(m_b));
0082 scale_random.define("MAX_RANDOM", "UINT_MAX");
0083 scale_random.define(
0084 "convert_RealType", std::string("convert_") + type_name<RealType>()
0085 );
0086 scale_random.define("RealType", type_name<RealType>());
0087
0088 generator.generate(
0089 first, last, scale_random, queue
0090 );
0091 }
0092
0093
0094 template<class OutputIterator, class Generator>
0095 void fill(OutputIterator first,
0096 OutputIterator last,
0097 Generator &g,
0098 command_queue &queue)
0099 {
0100 generate(first, last, g, queue);
0101 }
0102
0103 private:
0104 RealType m_a;
0105 RealType m_b;
0106
0107 BOOST_STATIC_ASSERT_MSG(
0108 boost::is_floating_point<RealType>::value,
0109 "Template argument must be a floating point type"
0110 );
0111 };
0112
0113 }
0114 }
0115
0116 #endif