• 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 #ifndef BOOST_COMPUTE_ALGORITHM_TRANSFORM_REDUCE_HPP
12 #define BOOST_COMPUTE_ALGORITHM_TRANSFORM_REDUCE_HPP
13 
14 #include <boost/static_assert.hpp>
15 
16 #include <boost/compute/system.hpp>
17 #include <boost/compute/algorithm/reduce.hpp>
18 #include <boost/compute/iterator/transform_iterator.hpp>
19 #include <boost/compute/iterator/zip_iterator.hpp>
20 #include <boost/compute/functional/detail/unpack.hpp>
21 #include <boost/compute/detail/iterator_range_size.hpp>
22 #include <boost/compute/type_traits/is_device_iterator.hpp>
23 
24 namespace boost {
25 namespace compute {
26 
27 /// Transforms each value in the range [\p first, \p last) with the unary
28 /// \p transform_function and then reduces each transformed value with
29 /// \p reduce_function.
30 ///
31 /// For example, to calculate the sum of the absolute values of a vector
32 /// of integers:
33 ///
34 /// \snippet test/test_transform_reduce.cpp sum_abs_int
35 ///
36 /// Space complexity on GPUs: \Omega(n)<br>
37 /// Space complexity on CPUs: \Omega(1)
38 ///
39 /// \see reduce(), inner_product()
40 template<class InputIterator,
41          class OutputIterator,
42          class UnaryTransformFunction,
43          class BinaryReduceFunction>
transform_reduce(InputIterator first,InputIterator last,OutputIterator result,UnaryTransformFunction transform_function,BinaryReduceFunction reduce_function,command_queue & queue=system::default_queue ())44 inline void transform_reduce(InputIterator first,
45                              InputIterator last,
46                              OutputIterator result,
47                              UnaryTransformFunction transform_function,
48                              BinaryReduceFunction reduce_function,
49                              command_queue &queue = system::default_queue())
50 {
51     BOOST_STATIC_ASSERT(is_device_iterator<InputIterator>::value);
52     ::boost::compute::reduce(
53         ::boost::compute::make_transform_iterator(first, transform_function),
54         ::boost::compute::make_transform_iterator(last, transform_function),
55         result,
56         reduce_function,
57         queue
58     );
59 }
60 
61 /// \overload
62 template<class InputIterator1,
63          class InputIterator2,
64          class OutputIterator,
65          class BinaryTransformFunction,
66          class BinaryReduceFunction>
transform_reduce(InputIterator1 first1,InputIterator1 last1,InputIterator2 first2,OutputIterator result,BinaryTransformFunction transform_function,BinaryReduceFunction reduce_function,command_queue & queue=system::default_queue ())67 inline void transform_reduce(InputIterator1 first1,
68                              InputIterator1 last1,
69                              InputIterator2 first2,
70                              OutputIterator result,
71                              BinaryTransformFunction transform_function,
72                              BinaryReduceFunction reduce_function,
73                              command_queue &queue = system::default_queue())
74 {
75     BOOST_STATIC_ASSERT(is_device_iterator<InputIterator1>::value);
76     BOOST_STATIC_ASSERT(is_device_iterator<InputIterator2>::value);
77     BOOST_STATIC_ASSERT(is_device_iterator<OutputIterator>::value);
78 
79     typedef typename std::iterator_traits<InputIterator1>::difference_type difference_type;
80 
81     difference_type n = std::distance(first1, last1);
82 
83     ::boost::compute::transform_reduce(
84         ::boost::compute::make_zip_iterator(
85             boost::make_tuple(first1, first2)
86         ),
87         ::boost::compute::make_zip_iterator(
88             boost::make_tuple(last1, first2 + n)
89         ),
90         result,
91         detail::unpack(transform_function),
92         reduce_function,
93         queue
94     );
95 }
96 
97 } // end compute namespace
98 } // end boost namespace
99 
100 #endif // BOOST_COMPUTE_ALGORITHM_TRANSFORM_REDUCE_HPP
101