• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1  //---------------------------------------------------------------------------//
2  // Copyright (c) 2015 Jakub Pola <jakub.pola@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 TestScatterIf
12  #include <boost/test/unit_test.hpp>
13  
14  #include <boost/compute/system.hpp>
15  #include <boost/compute/algorithm/scatter_if.hpp>
16  #include <boost/compute/container/vector.hpp>
17  #include <boost/compute/iterator/constant_buffer_iterator.hpp>
18  #include <boost/compute/iterator/counting_iterator.hpp>
19  #include <boost/compute/functional.hpp>
20  #include "check_macros.hpp"
21  #include "context_setup.hpp"
22  
23  namespace bc = boost::compute;
24  
BOOST_AUTO_TEST_CASE(scatter_if_int)25  BOOST_AUTO_TEST_CASE(scatter_if_int)
26  {
27      int input_data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
28      bc::vector<int> input(input_data, input_data + 10, queue);
29  
30      int map_data[] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
31  
32      bc::vector<int> map(map_data, map_data + 10, queue);
33  
34      int stencil_data[] = {0, 1, 0, 1, 0, 1, 0, 1, 0, 1};
35  
36      bc::vector<bc::uint_> stencil(stencil_data, stencil_data + 10, queue);
37  
38      bc::vector<int> output(input.size(), -1, queue);
39  
40      bc::scatter_if(input.begin(), input.end(),
41                     map.begin(), stencil.begin(),
42                     output.begin(),
43                     queue);
44  
45      CHECK_RANGE_EQUAL(int, 10, output, (9, -1, 7, -1, 5, -1, 3, -1, 1, -1) );
46  }
47  
BOOST_AUTO_TEST_CASE(scatter_if_constant_indices)48  BOOST_AUTO_TEST_CASE(scatter_if_constant_indices)
49  {
50      int input_data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
51      bc::vector<int> input(input_data, input_data + 10, queue);
52  
53      int map_data[] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
54      bc::buffer map_buffer(context,
55                            10 * sizeof(int),
56                            bc::buffer::read_only | bc::buffer::use_host_ptr,
57                            map_data);
58  
59      int stencil_data[] = {0, 1, 0, 1, 0, 1, 0, 1, 0, 1};
60      bc::buffer stencil_buffer(context,
61                                10 * sizeof(bc::uint_),
62                                bc::buffer::read_only | bc::buffer::use_host_ptr,
63                                stencil_data);
64  
65      bc::vector<int> output(input.size(), -1, queue);
66  
67      bc::scatter_if(input.begin(),
68                     input.end(),
69                     bc::make_constant_buffer_iterator<int>(map_buffer, 0),
70                     bc::make_constant_buffer_iterator<int>(stencil_buffer, 0),
71                     output.begin(),
72                     queue);
73  
74      CHECK_RANGE_EQUAL(int, 10, output, (9, -1, 7, -1, 5, -1, 3, -1, 1, -1) );
75  }
76  
77  
BOOST_AUTO_TEST_CASE(scatter_if_function)78  BOOST_AUTO_TEST_CASE(scatter_if_function)
79  {
80      int input_data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
81      bc::vector<int> input(input_data, input_data + 10, queue);
82  
83      int map_data[] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
84      bc::vector<int> map(map_data, map_data + 10, queue);
85  
86      int stencil_data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
87      bc::vector<bc::uint_> stencil(stencil_data, stencil_data + 10, queue);
88  
89      bc::vector<int> output(input.size(), -1, queue);
90  
91      BOOST_COMPUTE_FUNCTION(int, gt_than_5, (int x),
92      {
93          if (x > 5)
94              return true;
95          else
96              return false;
97      });
98  
99      bc::scatter_if(input.begin(),
100                     input.end(),
101                     map.begin(),
102                     stencil.begin(),
103                     output.begin(),
104                     gt_than_5,
105                     queue);
106  
107      CHECK_RANGE_EQUAL(int, 10, output, (9, 8, 7, 6, -1, -1, -1, -1, -1, -1) );
108  }
109  
110  
BOOST_AUTO_TEST_CASE(scatter_if_counting_iterator)111  BOOST_AUTO_TEST_CASE(scatter_if_counting_iterator)
112  {
113      int input_data[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
114      bc::vector<int> input(input_data, input_data + 10, queue);
115  
116      int map_data[] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
117      bc::vector<int> map(map_data, map_data + 10, queue);
118  
119      bc::vector<int> output(input.size(), -1, queue);
120  
121      BOOST_COMPUTE_FUNCTION(int, gt_than_5, (int x),
122      {
123          if (x > 5)
124              return true;
125          else
126              return false;
127      });
128  
129      bc::scatter_if(input.begin(),
130                     input.end(),
131                     map.begin(),
132                     bc::make_counting_iterator<int>(0),
133                     output.begin(),
134                     gt_than_5,
135                     queue);
136  
137      CHECK_RANGE_EQUAL(int, 10, output, (9, 8, 7, 6, -1, -1, -1, -1, -1, -1) );
138  
139  }
140  
141  BOOST_AUTO_TEST_SUITE_END()
142