• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013 Muhammad Junaid Muzammil <mjunaidmuzammil@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://kylelutz.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
10 
11 #define BOOST_TEST_MODULE TestThreefry
12 #include <boost/test/unit_test.hpp>
13 
14 #include <boost/compute/random/threefry_engine.hpp>
15 #include <boost/compute/container/vector.hpp>
16 
17 #include <boost/compute/random/uniform_real_distribution.hpp>
18 
19 #include "check_macros.hpp"
20 #include "context_setup.hpp"
21 
BOOST_AUTO_TEST_CASE(generate_uint)22 BOOST_AUTO_TEST_CASE(generate_uint)
23 {
24     using boost::compute::uint_;
25 
26     boost::compute::threefry_engine<> random_engine(queue);
27     boost::compute::vector<uint_> random_values(19, context);
28 
29     random_engine.generate(random_values.begin(), random_values.end(), queue);
30     queue.finish();
31     CHECK_RANGE_EQUAL(
32         uint_, 19, random_values,
33         (uint_(0x6b200159),
34          uint_(0x99ba4efe),
35          uint_(0x508efb2c),
36          uint_(0xc0de3f32),
37          uint_(0x64a626ec),
38          uint_(0xfc15e573),
39          uint_(0xb8abc4d1),
40          uint_(0x537eb86),
41          uint_(0xac6dc2bb),
42          uint_(0xa7adb3c3),
43          uint_(0x5641e094),
44          uint_(0xe4ab4fd),
45          uint_(0xa53c1ce9),
46          uint_(0xabcf1dba),
47          uint_(0x2677a25a),
48          uint_(0x76cf5efc),
49          uint_(0x2d08247f),
50          uint_(0x815480f1),
51          uint_(0x2d1fa53a))
52     );
53 }
54 
BOOST_AUTO_TEST_CASE(generate_float)55 BOOST_AUTO_TEST_CASE(generate_float)
56 {
57     using boost::compute::float_;
58 
59     boost::compute::threefry_engine<> random_engine(queue);
60     boost::compute::uniform_real_distribution<float_> random_distribution(0.f, 4.f);
61 
62     boost::compute::vector<float_> random_values(1024, context);
63     random_distribution.generate(
64         random_values.begin(), random_values.end(), random_engine, queue
65     );
66 
67     std::vector<float_> random_values_host(1024);
68     boost::compute::copy(
69         random_values.begin(), random_values.end(),
70         random_values_host.begin(),
71         queue
72     );
73     queue.finish();
74 
75     double sum = 0.0;
76     for(size_t i = 0; i < random_values_host.size(); i++)
77     {
78         BOOST_CHECK_LT(random_values_host[i], 4.0f);
79         BOOST_CHECK_GE(random_values_host[i], 0.0f);
80         sum += random_values_host[i];
81     }
82     double mean = sum / random_values_host.size();
83     // For 1024 it can be 10% off
84     BOOST_CHECK_CLOSE(mean, 2.0f, 10.0);
85 }
86 
87 BOOST_AUTO_TEST_SUITE_END()
88