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