]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
1 | //---------------------------------------------------------------------------// |
2 | // Copyright (c) 2014 Roshan <thisisroshansmail@gmail.com> | |
3 | // | |
4 | // Distributed under the Boost Software License, Version 1.0 | |
5 | // See accompanying file LICENSE_1_0.txt or copy at | |
6 | // http://www.boost.org/LICENSE_1_0.txt | |
7 | // | |
8 | // See http://boostorg.github.com/compute for more information. | |
9 | //---------------------------------------------------------------------------// | |
10 | ||
11 | #ifndef BOOST_COMPUTE_RANDOM_BERNOULLI_DISTRIBUTION_HPP | |
12 | #define BOOST_COMPUTE_RANDOM_BERNOULLI_DISTRIBUTION_HPP | |
13 | ||
14 | #include <boost/assert.hpp> | |
15 | #include <boost/type_traits.hpp> | |
16 | ||
17 | #include <boost/compute/command_queue.hpp> | |
18 | #include <boost/compute/function.hpp> | |
19 | #include <boost/compute/types/fundamental.hpp> | |
20 | #include <boost/compute/detail/iterator_range_size.hpp> | |
21 | #include <boost/compute/detail/literal.hpp> | |
22 | ||
23 | namespace boost { | |
24 | namespace compute { | |
25 | ||
26 | /// | |
27 | /// \class bernoulli_distribution | |
28 | /// \brief Produces random boolean values according to the following | |
29 | /// discrete probability function with parameter p : | |
30 | /// P(true/p) = p and P(false/p) = (1 - p) | |
31 | /// | |
32 | /// The following example shows how to setup a bernoulli distribution to | |
33 | /// produce random boolean values with parameter p = 0.25 | |
34 | /// | |
35 | /// \snippet test/test_bernoulli_distribution.cpp generate | |
36 | /// | |
37 | template<class RealType = float> | |
38 | class bernoulli_distribution | |
39 | { | |
40 | public: | |
41 | ||
42 | /// Creates a new bernoulli distribution | |
43 | bernoulli_distribution(RealType p = 0.5f) | |
44 | : m_p(p) | |
45 | { | |
46 | } | |
47 | ||
48 | /// Destroys the bernoulli_distribution object | |
49 | ~bernoulli_distribution() | |
50 | { | |
51 | } | |
52 | ||
53 | /// Returns the value of the parameter p | |
54 | RealType p() const | |
55 | { | |
56 | return m_p; | |
57 | } | |
58 | ||
59 | /// Generates bernoulli distributed booleans and stores | |
60 | /// them in the range [\p first, \p last). | |
61 | template<class OutputIterator, class Generator> | |
62 | void generate(OutputIterator first, | |
63 | OutputIterator last, | |
64 | Generator &generator, | |
65 | command_queue &queue) | |
66 | { | |
67 | size_t count = detail::iterator_range_size(first, last); | |
68 | ||
69 | vector<uint_> tmp(count, queue.get_context()); | |
70 | generator.generate(tmp.begin(), tmp.end(), queue); | |
71 | ||
72 | BOOST_COMPUTE_FUNCTION(bool, scale_random, (const uint_ x), | |
73 | { | |
74 | return (convert_RealType(x) / MAX_RANDOM) < PARAM; | |
75 | }); | |
76 | ||
77 | scale_random.define("PARAM", detail::make_literal(m_p)); | |
78 | scale_random.define("MAX_RANDOM", "UINT_MAX"); | |
79 | scale_random.define( | |
80 | "convert_RealType", std::string("convert_") + type_name<RealType>() | |
81 | ); | |
82 | ||
83 | transform( | |
84 | tmp.begin(), tmp.end(), first, scale_random, queue | |
85 | ); | |
86 | } | |
87 | ||
88 | private: | |
89 | RealType m_p; | |
90 | ||
91 | BOOST_STATIC_ASSERT_MSG( | |
92 | boost::is_floating_point<RealType>::value, | |
93 | "Template argument must be a floating point type" | |
94 | ); | |
95 | }; | |
96 | ||
97 | } // end compute namespace | |
98 | } // end boost namespace | |
99 | ||
100 | #endif // BOOST_COMPUTE_RANDOM_BERNOULLI_DISTRIBUTION_HPP |