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 template<class InputIterator1, class InputIterator2, class T>
inner_product(InputIterator1 first1,InputIterator1 last1,InputIterator2 first2,T init,command_queue & queue=system::default_queue ())30 inline T inner_product(InputIterator1 first1,
31                        InputIterator1 last1,
32                        InputIterator2 first2,
33                        T init,
34                        command_queue &queue = system::default_queue())
35 {
36     typedef typename std::iterator_traits<InputIterator1>::value_type input_type;
37 
38     ptrdiff_t n = std::distance(first1, last1);
39 
40     return ::boost::compute::accumulate(
41         ::boost::compute::make_transform_iterator(
42             ::boost::compute::make_zip_iterator(
43                 boost::make_tuple(first1, first2)
44             ),
45             detail::unpack(multiplies<input_type>())
46         ),
47         ::boost::compute::make_transform_iterator(
48             ::boost::compute::make_zip_iterator(
49                 boost::make_tuple(last1, first2 + n)
50             ),
51             detail::unpack(multiplies<input_type>())
52         ),
53         init,
54         queue
55     );
56 }
57 
58 /// \overload
59 template<class InputIterator1,
60          class InputIterator2,
61          class T,
62          class BinaryAccumulateFunction,
63          class BinaryTransformFunction>
inner_product(InputIterator1 first1,InputIterator1 last1,InputIterator2 first2,T init,BinaryAccumulateFunction accumulate_function,BinaryTransformFunction transform_function,command_queue & queue=system::default_queue ())64 inline T inner_product(InputIterator1 first1,
65                        InputIterator1 last1,
66                        InputIterator2 first2,
67                        T init,
68                        BinaryAccumulateFunction accumulate_function,
69                        BinaryTransformFunction transform_function,
70                        command_queue &queue = system::default_queue())
71 {
72     typedef typename std::iterator_traits<InputIterator1>::value_type value_type;
73 
74     size_t count = detail::iterator_range_size(first1, last1);
75     vector<value_type> result(count, queue.get_context());
76     transform(first1,
77               last1,
78               first2,
79               result.begin(),
80               transform_function,
81               queue);
82 
83     return ::boost::compute::accumulate(result.begin(),
84                                         result.end(),
85                                         init,
86                                         accumulate_function,
87                                         queue);
88 }
89 
90 } // end compute namespace
91 } // end boost namespace
92 
93 #endif // BOOST_COMPUTE_ALGORITHM_INNER_PRODUCT_HPP
94