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_INT_DISTRIBUTION_HPP
0012 #define BOOST_COMPUTE_RANDOM_UNIFORM_INT_DISTRIBUTION_HPP
0013
0014 #include <limits>
0015
0016 #include <boost/type_traits.hpp>
0017 #include <boost/static_assert.hpp>
0018
0019 #include <boost/compute/command_queue.hpp>
0020 #include <boost/compute/container/vector.hpp>
0021 #include <boost/compute/function.hpp>
0022 #include <boost/compute/types/fundamental.hpp>
0023 #include <boost/compute/algorithm/copy_if.hpp>
0024 #include <boost/compute/algorithm/transform.hpp>
0025
0026 namespace boost {
0027 namespace compute {
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037 template<class IntType = uint_>
0038 class uniform_int_distribution
0039 {
0040 public:
0041 typedef IntType result_type;
0042
0043
0044
0045 explicit uniform_int_distribution(IntType a = 0,
0046 IntType b = (std::numeric_limits<IntType>::max)())
0047 : m_a(a),
0048 m_b(b)
0049 {
0050 }
0051
0052
0053 ~uniform_int_distribution()
0054 {
0055 }
0056
0057
0058 result_type a() const
0059 {
0060 return m_a;
0061 }
0062
0063
0064 result_type b() const
0065 {
0066 return m_b;
0067 }
0068
0069
0070
0071 template<class OutputIterator, class Generator>
0072 void generate(OutputIterator first,
0073 OutputIterator last,
0074 Generator &generator,
0075 command_queue &queue)
0076 {
0077 size_t size = std::distance(first, last);
0078 typedef typename Generator::result_type g_result_type;
0079
0080 vector<g_result_type> tmp(size, queue.get_context());
0081 vector<g_result_type> tmp2(size, queue.get_context());
0082
0083 uint_ bound = ((uint_(-1))/(m_b-m_a+1))*(m_b-m_a+1);
0084
0085 buffer_iterator<g_result_type> tmp2_iter;
0086
0087 while(size>0)
0088 {
0089 generator.generate(tmp.begin(), tmp.begin() + size, queue);
0090 tmp2_iter = copy_if(tmp.begin(), tmp.begin() + size, tmp2.begin(),
0091 _1 <= bound, queue);
0092 size = std::distance(tmp2_iter, tmp2.end());
0093 }
0094
0095 BOOST_COMPUTE_FUNCTION(IntType, scale_random, (const g_result_type x),
0096 {
0097 return LO + (x % (HI-LO+1));
0098 });
0099
0100 scale_random.define("LO", boost::lexical_cast<std::string>(m_a));
0101 scale_random.define("HI", boost::lexical_cast<std::string>(m_b));
0102
0103 transform(tmp2.begin(), tmp2.end(), first, scale_random, queue);
0104 }
0105
0106 private:
0107 IntType m_a;
0108 IntType m_b;
0109
0110 BOOST_STATIC_ASSERT_MSG(
0111 boost::is_integral<IntType>::value,
0112 "Template argument must be integral"
0113 );
0114 };
0115
0116 }
0117 }
0118
0119 #endif