• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013 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 TestTransformReduce
12 #include <boost/test/unit_test.hpp>
13 
14 #include <boost/compute/lambda.hpp>
15 #include <boost/compute/system.hpp>
16 #include <boost/compute/functional.hpp>
17 #include <boost/compute/algorithm/transform_reduce.hpp>
18 #include <boost/compute/container/vector.hpp>
19 
20 #include "context_setup.hpp"
21 
22 namespace compute = boost::compute;
23 
BOOST_AUTO_TEST_CASE(sum_abs_int_doctest)24 BOOST_AUTO_TEST_CASE(sum_abs_int_doctest)
25 {
26     using boost::compute::abs;
27     using boost::compute::plus;
28 
29     int data[] = { 1, -2, -3, -4, 5 };
30     compute::vector<int> vec(data, data + 5, queue);
31 
32 //! [sum_abs_int]
33 int sum = 0;
34 boost::compute::transform_reduce(
35     vec.begin(), vec.end(), &sum, abs<int>(), plus<int>(), queue
36 );
37 //! [sum_abs_int]
38 
39     BOOST_CHECK_EQUAL(sum, 15);
40 }
41 
BOOST_AUTO_TEST_CASE(multiply_vector_length)42 BOOST_AUTO_TEST_CASE(multiply_vector_length)
43 {
44     float data[] = { 2.0f, 0.0f, 0.0f, 0.0f,
45                      0.0f, 3.0f, 0.0f, 0.0f,
46                      0.0f, 0.0f, 4.0f, 0.0f };
47     compute::vector<compute::float4_> vector(
48         reinterpret_cast<compute::float4_ *>(data),
49         reinterpret_cast<compute::float4_ *>(data) + 3,
50         queue
51     );
52 
53     float product;
54     compute::transform_reduce(
55         vector.begin(),
56         vector.end(),
57         &product,
58         compute::length<compute::float4_>(),
59         compute::multiplies<float>(),
60         queue
61     );
62     BOOST_CHECK_CLOSE(product, 24.0f, 1e-4f);
63 }
64 
BOOST_AUTO_TEST_CASE(mean_and_std_dev)65 BOOST_AUTO_TEST_CASE(mean_and_std_dev)
66 {
67     using compute::lambda::_1;
68     using compute::lambda::pow;
69 
70     float data[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
71     compute::vector<float> vector(data, data + 10, queue);
72 
73     float sum;
74     compute::reduce(
75         vector.begin(),
76         vector.end(),
77         &sum,
78         compute::plus<float>(),
79         queue
80     );
81 
82     float mean = sum / vector.size();
83     BOOST_CHECK_CLOSE(mean, 5.5f, 1e-4);
84 
85     compute::transform_reduce(
86         vector.begin(),
87         vector.end(),
88         &sum,
89         pow(_1 - mean, 2),
90         compute::plus<float>(),
91         queue
92     );
93 
94     float variance = sum / vector.size();
95     BOOST_CHECK_CLOSE(variance, 8.25f, 1e-4);
96 
97     float std_dev = std::sqrt(variance);
98     BOOST_CHECK_CLOSE(std_dev, 2.8722813232690143, 1e-4);
99 }
100 
101 BOOST_AUTO_TEST_SUITE_END()
102