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 TestFunction
12 #include <boost/test/unit_test.hpp>
13
14 #include <iostream>
15
16 #include <boost/compute/system.hpp>
17 #include <boost/compute/function.hpp>
18 #include <boost/compute/algorithm/accumulate.hpp>
19 #include <boost/compute/algorithm/copy.hpp>
20 #include <boost/compute/algorithm/generate.hpp>
21 #include <boost/compute/algorithm/sort.hpp>
22 #include <boost/compute/algorithm/transform.hpp>
23 #include <boost/compute/container/vector.hpp>
24 #include <boost/compute/iterator/zip_iterator.hpp>
25 #include <boost/compute/types/pair.hpp>
26
27 #include "check_macros.hpp"
28 #include "context_setup.hpp"
29
30 namespace compute = boost::compute;
31
BOOST_AUTO_TEST_CASE(add_three)32 BOOST_AUTO_TEST_CASE(add_three)
33 {
34 BOOST_COMPUTE_FUNCTION(int, add_three, (int x),
35 {
36 return x + 3;
37 });
38
39 int data[] = { 1, 2, 3, 4 };
40 compute::vector<int> vector(data, data + 4, queue);
41
42 compute::transform(
43 vector.begin(), vector.end(), vector.begin(), add_three, queue
44 );
45 CHECK_RANGE_EQUAL(int, 4, vector, (4, 5, 6, 7));
46 }
47
BOOST_AUTO_TEST_CASE(sum_odd_values)48 BOOST_AUTO_TEST_CASE(sum_odd_values)
49 {
50 BOOST_COMPUTE_FUNCTION(int, add_odd_value, (int sum, int value),
51 {
52 if(value & 1){
53 return sum + value;
54 }
55 else {
56 return sum + 0;
57 }
58 });
59
60 int data[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
61 compute::vector<int> vector(data, data + 8, queue);
62
63 int result = compute::accumulate(
64 vector.begin(), vector.end(), 0, add_odd_value, queue
65 );
66 BOOST_CHECK_EQUAL(result, 16);
67 }
68
BOOST_AUTO_TEST_CASE(sort_pairs)69 BOOST_AUTO_TEST_CASE(sort_pairs)
70 {
71 if(device.vendor() == "NVIDIA" && device.platform().name() == "Apple"){
72 // FIXME: this test currently segfaults on NVIDIA GPUs on Apple
73 std::cerr << "skipping sort_pairs test on NVIDIA GPU on Apple platform" << std::endl;
74 return;
75 }
76
77 std::vector<std::pair<int, float> > data;
78 data.push_back(std::make_pair(1, 2.3f));
79 data.push_back(std::make_pair(0, 4.2f));
80 data.push_back(std::make_pair(2, 1.0f));
81
82 compute::vector<std::pair<int, float> > vector(data.begin(), data.end(), queue);
83
84 // sort by first component
85 BOOST_COMPUTE_FUNCTION(bool, compare_first, (std::pair<int, float> a, std::pair<int, float> b),
86 {
87 return a.first < b.first;
88 });
89
90 compute::sort(vector.begin(), vector.end(), compare_first, queue);
91 compute::copy(vector.begin(), vector.end(), data.begin(), queue);
92 BOOST_CHECK(data[0] == std::make_pair(0, 4.2f));
93 BOOST_CHECK(data[1] == std::make_pair(1, 2.3f));
94 BOOST_CHECK(data[2] == std::make_pair(2, 1.0f));
95
96 // sort by second component
97 BOOST_COMPUTE_FUNCTION(bool, compare_second, (std::pair<int, float> a, std::pair<int, float> b),
98 {
99 return a.second < b.second;
100 });
101
102 compute::sort(vector.begin(), vector.end(), compare_second, queue);
103 compute::copy(vector.begin(), vector.end(), data.begin(), queue);
104 BOOST_CHECK(data[0] == std::make_pair(2, 1.0f));
105 BOOST_CHECK(data[1] == std::make_pair(1, 2.3f));
106 BOOST_CHECK(data[2] == std::make_pair(0, 4.2f));
107 }
108
BOOST_AUTO_TEST_CASE(transform_zip_iterator)109 BOOST_AUTO_TEST_CASE(transform_zip_iterator)
110 {
111 float float_data[] = { 1.f, 2.f, 3.f, 4.f };
112 compute::vector<float> input_floats(float_data, float_data + 4, queue);
113
114 int int_data[] = { 2, 4, 6, 8 };
115 compute::vector<int> input_ints(int_data, int_data + 4, queue);
116
117 compute::vector<float> results(4, context);
118
119 BOOST_COMPUTE_FUNCTION(float, tuple_pown, (boost::tuple<float, int> x),
120 {
121 return pown(boost_tuple_get(x, 0), boost_tuple_get(x, 1));
122 });
123
124 compute::transform(
125 compute::make_zip_iterator(
126 boost::make_tuple(input_floats.begin(), input_ints.begin())
127 ),
128 compute::make_zip_iterator(
129 boost::make_tuple(input_floats.end(), input_ints.end())
130 ),
131 results.begin(),
132 tuple_pown,
133 queue
134 );
135
136 float results_data[4];
137 compute::copy(results.begin(), results.end(), results_data, queue);
138 BOOST_CHECK_CLOSE(results_data[0], 1.f, 1e-4);
139 BOOST_CHECK_CLOSE(results_data[1], 16.f, 1e-4);
140 BOOST_CHECK_CLOSE(results_data[2], 729.f, 1e-4);
141 BOOST_CHECK_CLOSE(results_data[3], 65536.f, 1e-4);
142 }
143
144 static BOOST_COMPUTE_FUNCTION(int, static_function, (int x),
145 {
146 return x + 5;
147 });
148
BOOST_AUTO_TEST_CASE(test_static_function)149 BOOST_AUTO_TEST_CASE(test_static_function)
150 {
151 int data[] = { 1, 2, 3, 4};
152 compute::vector<int> vec(data, data + 4, queue);
153
154 compute::transform(
155 vec.begin(), vec.end(), vec.begin(), static_function, queue
156 );
157 CHECK_RANGE_EQUAL(int, 4, vec, (6, 7, 8, 9));
158 }
159
160 template<class T>
make_negate_function()161 inline compute::function<T(T)> make_negate_function()
162 {
163 BOOST_COMPUTE_FUNCTION(T, negate, (const T x),
164 {
165 return -x;
166 });
167
168 return negate;
169 }
170
BOOST_AUTO_TEST_CASE(test_templated_function)171 BOOST_AUTO_TEST_CASE(test_templated_function)
172 {
173 int int_data[] = { 1, 2, 3, 4 };
174 compute::vector<int> int_vec(int_data, int_data + 4, queue);
175
176 compute::function<int(int)> negate_int = make_negate_function<int>();
177 compute::transform(
178 int_vec.begin(), int_vec.end(), int_vec.begin(), negate_int, queue
179 );
180 CHECK_RANGE_EQUAL(int, 4, int_vec, (-1, -2, -3, -4));
181
182 float float_data[] = { 1.1f, 2.2f, 3.3f, 4.4f };
183 compute::vector<float> float_vec(float_data, float_data + 4, queue);
184
185 compute::function<float(float)> negate_float = make_negate_function<float>();
186 compute::transform(
187 float_vec.begin(), float_vec.end(), float_vec.begin(), negate_float, queue
188 );
189 CHECK_RANGE_EQUAL(float, 4, float_vec, (-1.1f, -2.2f, -3.3f, -4.4f));
190 }
191
BOOST_AUTO_TEST_CASE(define)192 BOOST_AUTO_TEST_CASE(define)
193 {
194 BOOST_COMPUTE_FUNCTION(int, return_number, (),
195 {
196 return NUMBER;
197 });
198 return_number.define("NUMBER", "4");
199
200 compute::vector<int> vec(1, context);
201 compute::generate(vec.begin(), vec.end(), return_number, queue);
202 CHECK_RANGE_EQUAL(int, 1, vec, (4));
203
204 return_number.define("NUMBER", "2");
205 compute::generate(vec.begin(), vec.end(), return_number, queue);
206 CHECK_RANGE_EQUAL(int, 1, vec, (2));
207 }
208
209 BOOST_AUTO_TEST_SUITE_END()
210