1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013-2014 Kyle Lutz <kyle.r.lutz@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 #define BOOST_TEST_MODULE TestUniformRealDistribution
12 #include <boost/test/unit_test.hpp>
13
14 #include <boost/compute/system.hpp>
15 #include <boost/compute/command_queue.hpp>
16 #include <boost/compute/algorithm/count_if.hpp>
17 #include <boost/compute/algorithm/transform.hpp>
18 #include <boost/compute/function.hpp>
19 #include <boost/compute/container/vector.hpp>
20 #include <boost/compute/random/default_random_engine.hpp>
21 #include <boost/compute/random/uniform_real_distribution.hpp>
22 #include <boost/compute/lambda.hpp>
23 #include <boost/compute/types/fundamental.hpp>
24
25 #include "context_setup.hpp"
26
BOOST_AUTO_TEST_CASE(uniform_real_distribution_doctest)27 BOOST_AUTO_TEST_CASE(uniform_real_distribution_doctest)
28 {
29 using boost::compute::lambda::_1;
30
31 boost::compute::vector<float> vec(128, context);
32
33 //! [generate]
34 // initialize the default random engine
35 boost::compute::default_random_engine engine(queue);
36
37 // setup the uniform distribution to produce floats between 1 and 100
38 boost::compute::uniform_real_distribution<float> distribution(1.0f, 100.0f);
39
40 // generate the random values and store them to 'vec'
41 distribution.generate(vec.begin(), vec.end(), engine, queue);
42 //! [generate]
43
44 BOOST_CHECK_EQUAL(
45 boost::compute::count_if(
46 vec.begin(), vec.end(), _1 < 1.0f || _1 >= 100.0f, queue
47 ),
48 size_t(0)
49 );
50 }
51
52 template<class T>
53 class range_test_engine
54 {
55 public:
range_test_engine(boost::compute::command_queue & queue)56 explicit range_test_engine(boost::compute::command_queue &queue) {
57 (void) queue;
58 }
59
60 template<class OutputIterator, class Function>
generate(OutputIterator first,OutputIterator last,Function op,boost::compute::command_queue & queue)61 void generate(OutputIterator first, OutputIterator last, Function op, boost::compute::command_queue &queue)
62 {
63 boost::compute::vector<T> tmp(std::distance(first, last), queue.get_context());
64
65 BOOST_COMPUTE_FUNCTION(T, max_random, (const T x),
66 {
67 if(get_global_id(0) < 1)
68 return (ValueType) MAX_RANDOM;
69 else
70 return (ValueType) 0;
71 });
72
73 max_random.define("MAX_RANDOM", "UINT_MAX");
74 max_random.define("ValueType", boost::compute::type_name<T>());
75
76 boost::compute::transform(tmp.begin(), tmp.end(), tmp.begin(), max_random, queue);
77 boost::compute::transform(tmp.begin(), tmp.end(), first, op, queue);
78 }
79 };
80
81 // For checking if result is in the correct range [low, hi)
BOOST_AUTO_TEST_CASE(uniform_real_distribution_range)82 BOOST_AUTO_TEST_CASE(uniform_real_distribution_range)
83 {
84 using boost::compute::lambda::_1;
85
86 boost::compute::vector<float> vec(32, context);
87
88 // initialize the range_test_engine
89 range_test_engine<boost::compute::uint_> engine(queue);
90
91 // setup the uniform distribution to produce floats between 0.9 and 1.0
92 boost::compute::uniform_real_distribution<float> distribution(0.9f, 1.0f);
93
94 // generate the random values and store them to 'vec'
95 distribution.generate(vec.begin(), vec.end(), engine, queue);
96
97 BOOST_CHECK_EQUAL(
98 boost::compute::count_if(
99 vec.begin(), vec.end(), _1 < 0.9f || _1 >= 1.0f, queue
100 ),
101 size_t(0)
102 );
103 }
104
105 BOOST_AUTO_TEST_SUITE_END()
106