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/compute/system.hpp>
15 #include <boost/compute/algorithm/reduce.hpp>
16 #include <boost/compute/iterator/transform_iterator.hpp>
17 #include <boost/compute/iterator/zip_iterator.hpp>
18 #include <boost/compute/functional/detail/unpack.hpp>
19 #include <boost/compute/detail/iterator_range_size.hpp>
24 /// Transforms each value in the range [\p first, \p last) with the unary
25 /// \p transform_function and then reduces each transformed value with
26 /// \p reduce_function.
28 /// For example, to calculate the sum of the absolute values of a vector
31 /// \snippet test/test_transform_reduce.cpp sum_abs_int
33 /// \see reduce(), inner_product()
34 template<class InputIterator,
36 class UnaryTransformFunction,
37 class BinaryReduceFunction>
38 inline void transform_reduce(InputIterator first,
40 OutputIterator result,
41 UnaryTransformFunction transform_function,
42 BinaryReduceFunction reduce_function,
43 command_queue &queue = system::default_queue())
45 ::boost::compute::reduce(
46 ::boost::compute::make_transform_iterator(first, transform_function),
47 ::boost::compute::make_transform_iterator(last, transform_function),
55 template<class InputIterator1,
58 class BinaryTransformFunction,
59 class BinaryReduceFunction>
60 inline void transform_reduce(InputIterator1 first1,
62 InputIterator2 first2,
63 OutputIterator result,
64 BinaryTransformFunction transform_function,
65 BinaryReduceFunction reduce_function,
66 command_queue &queue = system::default_queue())
68 typedef typename std::iterator_traits<InputIterator1>::difference_type difference_type;
70 difference_type n = std::distance(first1, last1);
72 ::boost::compute::transform_reduce(
73 ::boost::compute::make_zip_iterator(
74 boost::make_tuple(first1, first2)
76 ::boost::compute::make_zip_iterator(
77 boost::make_tuple(last1, first2 + n)
80 detail::unpack(transform_function),
86 } // end compute namespace
87 } // end boost namespace
89 #endif // BOOST_COMPUTE_ALGORITHM_TRANSFORM_REDUCE_HPP