• 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 TestInplaceReduce
12 #include <boost/test/unit_test.hpp>
13 
14 #include <iostream>
15 
16 #include <boost/compute/system.hpp>
17 #include <boost/compute/functional.hpp>
18 #include <boost/compute/algorithm/iota.hpp>
19 #include <boost/compute/algorithm/detail/inplace_reduce.hpp>
20 #include <boost/compute/container/vector.hpp>
21 
22 #include "quirks.hpp"
23 #include "context_setup.hpp"
24 
BOOST_AUTO_TEST_CASE(sum_int)25 BOOST_AUTO_TEST_CASE(sum_int)
26 {
27     if(is_apple_cpu_device(device)) {
28         std::cerr
29             << "skipping all inplace_reduce tests due to Apple platform"
30             << " behavior when local memory is used on a CPU device"
31             << std::endl;
32         return;
33     }
34 
35     int data[] = { 1, 5, 3, 4, 9, 3, 5, 3 };
36     boost::compute::vector<int> vector(data, data + 8, queue);
37 
38     boost::compute::detail::inplace_reduce(vector.begin(),
39                                            vector.end(),
40                                            boost::compute::plus<int>(),
41                                            queue);
42     queue.finish();
43     BOOST_CHECK_EQUAL(int(vector[0]), int(33));
44 
45     vector.assign(data, data + 8);
46     vector.push_back(3);
47     boost::compute::detail::inplace_reduce(vector.begin(),
48                                            vector.end(),
49                                            boost::compute::plus<int>(),
50                                            queue);
51     queue.finish();
52     BOOST_CHECK_EQUAL(int(vector[0]), int(36));
53 }
54 
BOOST_AUTO_TEST_CASE(multiply_int)55 BOOST_AUTO_TEST_CASE(multiply_int)
56 {
57     if(is_apple_cpu_device(device)) {
58         return;
59     }
60 
61     int data[] = { 1, 5, 3, 4, 9, 3, 5, 3 };
62     boost::compute::vector<int> vector(data, data + 8, queue);
63 
64     boost::compute::detail::inplace_reduce(vector.begin(),
65                                            vector.end(),
66                                            boost::compute::multiplies<int>(),
67                                            queue);
68     queue.finish();
69     BOOST_CHECK_EQUAL(int(vector[0]), int(24300));
70 
71     vector.assign(data, data + 8);
72     vector.push_back(3);
73     boost::compute::detail::inplace_reduce(vector.begin(),
74                                            vector.end(),
75                                            boost::compute::multiplies<int>(),
76                                            queue);
77     queue.finish();
78     BOOST_CHECK_EQUAL(int(vector[0]), int(72900));
79 }
80 
BOOST_AUTO_TEST_CASE(reduce_iota)81 BOOST_AUTO_TEST_CASE(reduce_iota)
82 {
83     if(is_apple_cpu_device(device)) {
84         return;
85     }
86 
87     // 1 value
88     boost::compute::vector<int> vector(1, context);
89     boost::compute::iota(vector.begin(), vector.end(), int(0), queue);
90     boost::compute::detail::inplace_reduce(vector.begin(),
91                                            vector.end(),
92                                            boost::compute::plus<int>(),
93                                            queue);
94     queue.finish();
95     BOOST_CHECK_EQUAL(int(vector[0]), int(0));
96 
97     // 1000 values
98     vector.resize(1000);
99     boost::compute::iota(vector.begin(), vector.end(), int(0), queue);
100     boost::compute::detail::inplace_reduce(vector.begin(),
101                                            vector.end(),
102                                            boost::compute::plus<int>(),
103                                            queue);
104     queue.finish();
105     BOOST_CHECK_EQUAL(int(vector[0]), int(499500));
106 
107     // 2499 values
108     vector.resize(2499);
109     boost::compute::iota(vector.begin(), vector.end(), int(0), queue);
110     boost::compute::detail::inplace_reduce(vector.begin(),
111                                            vector.end(),
112                                            boost::compute::plus<int>(),
113                                            queue);
114     queue.finish();
115     BOOST_CHECK_EQUAL(int(vector[0]), int(3121251));
116 
117     // 4096 values
118     vector.resize(4096);
119     boost::compute::iota(vector.begin(), vector.end(), int(0), queue);
120     boost::compute::detail::inplace_reduce(vector.begin(),
121                                            vector.end(),
122                                            boost::compute::plus<int>(),
123                                            queue);
124     queue.finish();
125     BOOST_CHECK_EQUAL(int(vector[0]), int(8386560));
126 
127     // 5000 values
128     vector.resize(5000);
129     boost::compute::iota(vector.begin(), vector.end(), int(0), queue);
130     boost::compute::detail::inplace_reduce(vector.begin(),
131                                            vector.end(),
132                                            boost::compute::plus<int>(),
133                                            queue);
134     queue.finish();
135     BOOST_CHECK_EQUAL(int(vector[0]), int(12497500));
136 }
137 
138 BOOST_AUTO_TEST_SUITE_END()
139