Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //---------------------------------------------------------------------------//
0002 // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@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_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 /// \class uniform_real_distribution
0026 /// \brief Produces uniformly distributed random floating-point numbers.
0027 ///
0028 /// The following example shows how to setup a uniform real distribution to
0029 /// produce random \c float values between \c 1 and \c 100.
0030 ///
0031 /// \snippet test/test_uniform_real_distribution.cpp generate
0032 ///
0033 /// \see default_random_engine, normal_distribution
0034 template<class RealType = float>
0035 class uniform_real_distribution
0036 {
0037 public:
0038     typedef RealType result_type;
0039 
0040     /// Creates a new uniform distribution producing numbers in the range
0041     /// [\p a, \p b).
0042     /// Requires a < b
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     /// Destroys the uniform_real_distribution object.
0051     ~uniform_real_distribution()
0052     {
0053     }
0054 
0055     /// Returns the minimum value of the distribution.
0056     result_type a() const
0057     {
0058         return m_a;
0059     }
0060 
0061     /// Returns the maximum value of the distribution.
0062     result_type b() const
0063     {
0064         return m_b;
0065     }
0066 
0067     /// Generates uniformly distributed floating-point numbers and stores
0068     /// them to the range [\p first, \p last).
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     /// \internal_ (deprecated)
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 } // end compute namespace
0114 } // end boost namespace
0115 
0116 #endif // BOOST_COMPUTE_RANDOM_UNIFORM_REAL_DISTRIBUTION_HPP