1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
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
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
11 #ifndef BOOST_COMPUTE_ALGORITHM_INNER_PRODUCT_HPP
12 #define BOOST_COMPUTE_ALGORITHM_INNER_PRODUCT_HPP
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>
26 /// Returns the inner product of the elements in the range
27 /// [\p first1, \p last1) with the elements in the range beginning
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,
35 InputIterator2 first2,
37 command_queue &queue = system::default_queue())
39 typedef typename std::iterator_traits<InputIterator1>::value_type input_type;
41 ptrdiff_t n = std::distance(first1, last1);
43 return ::boost::compute::accumulate(
44 ::boost::compute::make_transform_iterator(
45 ::boost::compute::make_zip_iterator(
46 boost::make_tuple(first1, first2)
48 detail::unpack(multiplies<input_type>())
50 ::boost::compute::make_transform_iterator(
51 ::boost::compute::make_zip_iterator(
52 boost::make_tuple(last1, first2 + n)
54 detail::unpack(multiplies<input_type>())
62 template<class InputIterator1,
65 class BinaryAccumulateFunction,
66 class BinaryTransformFunction>
67 inline T inner_product(InputIterator1 first1,
69 InputIterator2 first2,
71 BinaryAccumulateFunction accumulate_function,
72 BinaryTransformFunction transform_function,
73 command_queue &queue = system::default_queue())
75 typedef typename std::iterator_traits<InputIterator1>::value_type value_type;
77 size_t count = detail::iterator_range_size(first1, last1);
78 vector<value_type> result(count, queue.get_context());
86 return ::boost::compute::accumulate(result.begin(),
93 } // end compute namespace
94 } // end boost namespace
96 #endif // BOOST_COMPUTE_ALGORITHM_INNER_PRODUCT_HPP