]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/compute/algorithm/transform_reduce.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / compute / algorithm / transform_reduce.hpp
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 /// Space complexity on GPUs: \Omega(n)<br>
34 /// Space complexity on CPUs: \Omega(1)
35 ///
36 /// \see reduce(), inner_product()
37 template<class InputIterator,
38 class OutputIterator,
39 class UnaryTransformFunction,
40 class BinaryReduceFunction>
41 inline void transform_reduce(InputIterator first,
42 InputIterator last,
43 OutputIterator result,
44 UnaryTransformFunction transform_function,
45 BinaryReduceFunction reduce_function,
46 command_queue &queue = system::default_queue())
47 {
48 ::boost::compute::reduce(
49 ::boost::compute::make_transform_iterator(first, transform_function),
50 ::boost::compute::make_transform_iterator(last, transform_function),
51 result,
52 reduce_function,
53 queue
54 );
55 }
56
57 /// \overload
58 template<class InputIterator1,
59 class InputIterator2,
60 class OutputIterator,
61 class BinaryTransformFunction,
62 class BinaryReduceFunction>
63 inline void transform_reduce(InputIterator1 first1,
64 InputIterator1 last1,
65 InputIterator2 first2,
66 OutputIterator result,
67 BinaryTransformFunction transform_function,
68 BinaryReduceFunction reduce_function,
69 command_queue &queue = system::default_queue())
70 {
71 typedef typename std::iterator_traits<InputIterator1>::difference_type difference_type;
72
73 difference_type n = std::distance(first1, last1);
74
75 ::boost::compute::transform_reduce(
76 ::boost::compute::make_zip_iterator(
77 boost::make_tuple(first1, first2)
78 ),
79 ::boost::compute::make_zip_iterator(
80 boost::make_tuple(last1, first2 + n)
81 ),
82 result,
83 detail::unpack(transform_function),
84 reduce_function,
85 queue
86 );
87 }
88
89 } // end compute namespace
90 } // end boost namespace
91
92 #endif // BOOST_COMPUTE_ALGORITHM_TRANSFORM_REDUCE_HPP