]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/compute/algorithm/inner_product.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / compute / algorithm / inner_product.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_INNER_PRODUCT_HPP
12 #define BOOST_COMPUTE_ALGORITHM_INNER_PRODUCT_HPP
13
14 #include <boost/compute/system.hpp>
15 #include <boost/compute/functional.hpp>
16 #include <boost/compute/command_queue.hpp>
17 #include <boost/compute/algorithm/accumulate.hpp>
18 #include <boost/compute/container/vector.hpp>
19 #include <boost/compute/iterator/transform_iterator.hpp>
20 #include <boost/compute/iterator/zip_iterator.hpp>
21 #include <boost/compute/functional/detail/unpack.hpp>
22
23 namespace boost {
24 namespace compute {
25
26 /// Returns the inner product of the elements in the range
27 /// [\p first1, \p last1) with the elements in the range beginning
28 /// at \p first2.
29 ///
30 /// Space complexity: \Omega(1)<br>
31 /// Space complexity when binary operator is recognized as associative: \Omega(n)
32 template<class InputIterator1, class InputIterator2, class T>
33 inline T inner_product(InputIterator1 first1,
34 InputIterator1 last1,
35 InputIterator2 first2,
36 T init,
37 command_queue &queue = system::default_queue())
38 {
39 typedef typename std::iterator_traits<InputIterator1>::value_type input_type;
40
41 ptrdiff_t n = std::distance(first1, last1);
42
43 return ::boost::compute::accumulate(
44 ::boost::compute::make_transform_iterator(
45 ::boost::compute::make_zip_iterator(
46 boost::make_tuple(first1, first2)
47 ),
48 detail::unpack(multiplies<input_type>())
49 ),
50 ::boost::compute::make_transform_iterator(
51 ::boost::compute::make_zip_iterator(
52 boost::make_tuple(last1, first2 + n)
53 ),
54 detail::unpack(multiplies<input_type>())
55 ),
56 init,
57 queue
58 );
59 }
60
61 /// \overload
62 template<class InputIterator1,
63 class InputIterator2,
64 class T,
65 class BinaryAccumulateFunction,
66 class BinaryTransformFunction>
67 inline T inner_product(InputIterator1 first1,
68 InputIterator1 last1,
69 InputIterator2 first2,
70 T init,
71 BinaryAccumulateFunction accumulate_function,
72 BinaryTransformFunction transform_function,
73 command_queue &queue = system::default_queue())
74 {
75 typedef typename std::iterator_traits<InputIterator1>::value_type value_type;
76
77 size_t count = detail::iterator_range_size(first1, last1);
78 vector<value_type> result(count, queue.get_context());
79 transform(first1,
80 last1,
81 first2,
82 result.begin(),
83 transform_function,
84 queue);
85
86 return ::boost::compute::accumulate(result.begin(),
87 result.end(),
88 init,
89 accumulate_function,
90 queue);
91 }
92
93 } // end compute namespace
94 } // end boost namespace
95
96 #endif // BOOST_COMPUTE_ALGORITHM_INNER_PRODUCT_HPP