1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
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
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
11 #ifndef BOOST_COMPUTE_ALGORITHM_TRANSFORM_REDUCE_HPP
12 #define BOOST_COMPUTE_ALGORITHM_TRANSFORM_REDUCE_HPP
14 #include <boost/static_assert.hpp>
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>
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.
31 /// For example, to calculate the sum of the absolute values of a vector
34 /// \snippet test/test_transform_reduce.cpp sum_abs_int
36 /// Space complexity on GPUs: \Omega(n)<br>
37 /// Space complexity on CPUs: \Omega(1)
39 /// \see reduce(), inner_product()
40 template<class InputIterator,
42 class UnaryTransformFunction,
43 class BinaryReduceFunction>
44 inline void transform_reduce(InputIterator first,
46 OutputIterator result,
47 UnaryTransformFunction transform_function,
48 BinaryReduceFunction reduce_function,
49 command_queue &queue = system::default_queue())
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),
62 template<class InputIterator1,
65 class BinaryTransformFunction,
66 class BinaryReduceFunction>
67 inline void transform_reduce(InputIterator1 first1,
69 InputIterator2 first2,
70 OutputIterator result,
71 BinaryTransformFunction transform_function,
72 BinaryReduceFunction reduce_function,
73 command_queue &queue = system::default_queue())
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);
79 typedef typename std::iterator_traits<InputIterator1>::difference_type difference_type;
81 difference_type n = std::distance(first1, last1);
83 ::boost::compute::transform_reduce(
84 ::boost::compute::make_zip_iterator(
85 boost::make_tuple(first1, first2)
87 ::boost::compute::make_zip_iterator(
88 boost::make_tuple(last1, first2 + n)
91 detail::unpack(transform_function),
97 } // end compute namespace
98 } // end boost namespace
100 #endif // BOOST_COMPUTE_ALGORITHM_TRANSFORM_REDUCE_HPP