]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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 TestNormalDistribution | |
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/container/vector.hpp> | |
18 | #include <boost/compute/random/default_random_engine.hpp> | |
19 | #include <boost/compute/random/normal_distribution.hpp> | |
20 | #include <boost/compute/lambda.hpp> | |
21 | ||
22 | #include <boost/accumulators/accumulators.hpp> | |
23 | #include <boost/accumulators/statistics/mean.hpp> | |
24 | #include <boost/accumulators/statistics/stats.hpp> | |
25 | #include <boost/accumulators/statistics/variance.hpp> | |
26 | ||
27 | #include "context_setup.hpp" | |
28 | ||
29 | template<class Stats, class T> | |
30 | boost::accumulators::accumulator_set<T, Stats> | |
31 | accumulate_statistics(const boost::compute::vector<T>& vector, | |
32 | boost::compute::command_queue& queue) { | |
33 | // copy vector to the host | |
34 | std::vector<T> host_vector(vector.size()); | |
35 | boost::compute::copy( | |
36 | vector.begin(), vector.end(), host_vector.begin(), queue | |
37 | ); | |
38 | ||
39 | // compute desired statistics and return accumulator object | |
40 | return std::for_each( | |
41 | host_vector.begin(), | |
42 | host_vector.end(), | |
43 | boost::accumulators::accumulator_set<T, Stats>() | |
44 | ); | |
45 | } | |
46 | ||
47 | BOOST_AUTO_TEST_CASE(normal_distribution_doctest) | |
48 | { | |
49 | using boost::compute::lambda::_1; | |
50 | ||
51 | boost::compute::vector<float> vec(10, context); | |
52 | ||
53 | //! [generate] | |
54 | // initialize the default random engine | |
55 | boost::compute::default_random_engine engine(queue); | |
56 | ||
57 | // setup the normal distribution to produce floats centered at 5 | |
58 | boost::compute::normal_distribution<float> distribution(5.0f, 1.0f); | |
59 | ||
60 | // generate the random values and store them to 'vec' | |
61 | distribution.generate(vec.begin(), vec.end(), engine, queue); | |
62 | //! [generate] | |
63 | } | |
64 | ||
65 | BOOST_AUTO_TEST_CASE(normal_distribution_statistics) | |
66 | { | |
67 | // generate normally distributed random numbers | |
68 | const size_t n = 10000; | |
69 | boost::compute::vector<float> vec(n, context); | |
70 | boost::compute::default_random_engine engine(queue); | |
71 | boost::compute::normal_distribution<float> distribution(10.0f, 2.0f); | |
72 | distribution.generate(vec.begin(), vec.end(), engine, queue); | |
73 | ||
74 | // compute mean and standard deviation | |
75 | using namespace boost::accumulators; | |
76 | accumulator_set<float, stats<tag::variance> > acc = | |
77 | accumulate_statistics<stats<tag::variance> >(vec, queue); | |
78 | ||
79 | // check mean and standard deviation are what we expect | |
80 | BOOST_CHECK_CLOSE(mean(acc), 10.f, 0.5f); | |
81 | BOOST_CHECK_CLOSE(std::sqrt(variance(acc)), 2.f, 0.5f); | |
82 | } | |
83 | ||
84 | BOOST_AUTO_TEST_SUITE_END() |