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/static_assert.hpp>
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>
29 /// Returns the inner product of the elements in the range
30 /// [\p first1, \p last1) with the elements in the range beginning
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>
36 inline T inner_product(InputIterator1 first1,
38 InputIterator2 first2,
40 command_queue &queue = system::default_queue())
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;
46 ptrdiff_t n = std::distance(first1, last1);
48 return ::boost::compute::accumulate(
49 ::boost::compute::make_transform_iterator(
50 ::boost::compute::make_zip_iterator(
51 boost::make_tuple(first1, first2)
53 detail::unpack(multiplies<input_type>())
55 ::boost::compute::make_transform_iterator(
56 ::boost::compute::make_zip_iterator(
57 boost::make_tuple(last1, first2 + n)
59 detail::unpack(multiplies<input_type>())
67 template<class InputIterator1,
70 class BinaryAccumulateFunction,
71 class BinaryTransformFunction>
72 inline T inner_product(InputIterator1 first1,
74 InputIterator2 first2,
76 BinaryAccumulateFunction accumulate_function,
77 BinaryTransformFunction transform_function,
78 command_queue &queue = system::default_queue())
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;
84 size_t count = detail::iterator_range_size(first1, last1);
85 vector<value_type> result(count, queue.get_context());
93 return ::boost::compute::accumulate(result.begin(),
100 } // end compute namespace
101 } // end boost namespace
103 #endif // BOOST_COMPUTE_ALGORITHM_INNER_PRODUCT_HPP