]>
Commit | Line | Data |
---|---|---|
7c673cae FG |
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/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> | |
20 | ||
21 | namespace boost { | |
22 | namespace compute { | |
23 | ||
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. | |
27 | /// | |
28 | /// For example, to calculate the sum of the absolute values of a vector | |
29 | /// of integers: | |
30 | /// | |
31 | /// \snippet test/test_transform_reduce.cpp sum_abs_int | |
32 | /// | |
33 | /// \see reduce(), inner_product() | |
34 | template<class InputIterator, | |
35 | class OutputIterator, | |
36 | class UnaryTransformFunction, | |
37 | class BinaryReduceFunction> | |
38 | inline void transform_reduce(InputIterator first, | |
39 | InputIterator last, | |
40 | OutputIterator result, | |
41 | UnaryTransformFunction transform_function, | |
42 | BinaryReduceFunction reduce_function, | |
43 | command_queue &queue = system::default_queue()) | |
44 | { | |
45 | ::boost::compute::reduce( | |
46 | ::boost::compute::make_transform_iterator(first, transform_function), | |
47 | ::boost::compute::make_transform_iterator(last, transform_function), | |
48 | result, | |
49 | reduce_function, | |
50 | queue | |
51 | ); | |
52 | } | |
53 | ||
54 | /// \overload | |
55 | template<class InputIterator1, | |
56 | class InputIterator2, | |
57 | class OutputIterator, | |
58 | class BinaryTransformFunction, | |
59 | class BinaryReduceFunction> | |
60 | inline void transform_reduce(InputIterator1 first1, | |
61 | InputIterator1 last1, | |
62 | InputIterator2 first2, | |
63 | OutputIterator result, | |
64 | BinaryTransformFunction transform_function, | |
65 | BinaryReduceFunction reduce_function, | |
66 | command_queue &queue = system::default_queue()) | |
67 | { | |
68 | typedef typename std::iterator_traits<InputIterator1>::difference_type difference_type; | |
69 | ||
70 | difference_type n = std::distance(first1, last1); | |
71 | ||
72 | ::boost::compute::transform_reduce( | |
73 | ::boost::compute::make_zip_iterator( | |
74 | boost::make_tuple(first1, first2) | |
75 | ), | |
76 | ::boost::compute::make_zip_iterator( | |
77 | boost::make_tuple(last1, first2 + n) | |
78 | ), | |
79 | result, | |
80 | detail::unpack(transform_function), | |
81 | reduce_function, | |
82 | queue | |
83 | ); | |
84 | } | |
85 | ||
86 | } // end compute namespace | |
87 | } // end boost namespace | |
88 | ||
89 | #endif // BOOST_COMPUTE_ALGORITHM_TRANSFORM_REDUCE_HPP |