Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:29:56

0001 //---------------------------------------------------------------------------//
0002 // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
0003 //
0004 // Distributed under the Boost Software License, Version 1.0
0005 // See accompanying file LICENSE_1_0.txt or copy at
0006 // http://www.boost.org/LICENSE_1_0.txt
0007 //
0008 // See http://boostorg.github.com/compute for more information.
0009 //---------------------------------------------------------------------------//
0010 
0011 #ifndef BOOST_COMPUTE_ALGORITHM_INNER_PRODUCT_HPP
0012 #define BOOST_COMPUTE_ALGORITHM_INNER_PRODUCT_HPP
0013 
0014 #include <boost/static_assert.hpp>
0015 
0016 #include <boost/compute/system.hpp>
0017 #include <boost/compute/functional.hpp>
0018 #include <boost/compute/command_queue.hpp>
0019 #include <boost/compute/algorithm/accumulate.hpp>
0020 #include <boost/compute/container/vector.hpp>
0021 #include <boost/compute/iterator/transform_iterator.hpp>
0022 #include <boost/compute/iterator/zip_iterator.hpp>
0023 #include <boost/compute/functional/detail/unpack.hpp>
0024 #include <boost/compute/type_traits/is_device_iterator.hpp>
0025 
0026 namespace boost {
0027 namespace compute {
0028 
0029 /// Returns the inner product of the elements in the range
0030 /// [\p first1, \p last1) with the elements in the range beginning
0031 /// at \p first2.
0032 ///
0033 /// Space complexity: \Omega(1)<br>
0034 /// Space complexity when binary operator is recognized as associative: \Omega(n)
0035 template<class InputIterator1, class InputIterator2, class T>
0036 inline T inner_product(InputIterator1 first1,
0037                        InputIterator1 last1,
0038                        InputIterator2 first2,
0039                        T init,
0040                        command_queue &queue = system::default_queue())
0041 {
0042     BOOST_STATIC_ASSERT(is_device_iterator<InputIterator1>::value);
0043     BOOST_STATIC_ASSERT(is_device_iterator<InputIterator2>::value);
0044     typedef typename std::iterator_traits<InputIterator1>::value_type input_type;
0045 
0046     ptrdiff_t n = std::distance(first1, last1);
0047 
0048     return ::boost::compute::accumulate(
0049         ::boost::compute::make_transform_iterator(
0050             ::boost::compute::make_zip_iterator(
0051                 boost::make_tuple(first1, first2)
0052             ),
0053             detail::unpack(multiplies<input_type>())
0054         ),
0055         ::boost::compute::make_transform_iterator(
0056             ::boost::compute::make_zip_iterator(
0057                 boost::make_tuple(last1, first2 + n)
0058             ),
0059             detail::unpack(multiplies<input_type>())
0060         ),
0061         init,
0062         queue
0063     );
0064 }
0065 
0066 /// \overload
0067 template<class InputIterator1,
0068          class InputIterator2,
0069          class T,
0070          class BinaryAccumulateFunction,
0071          class BinaryTransformFunction>
0072 inline T inner_product(InputIterator1 first1,
0073                        InputIterator1 last1,
0074                        InputIterator2 first2,
0075                        T init,
0076                        BinaryAccumulateFunction accumulate_function,
0077                        BinaryTransformFunction transform_function,
0078                        command_queue &queue = system::default_queue())
0079 {
0080     BOOST_STATIC_ASSERT(is_device_iterator<InputIterator1>::value);
0081     BOOST_STATIC_ASSERT(is_device_iterator<InputIterator2>::value);
0082     typedef typename std::iterator_traits<InputIterator1>::value_type value_type;
0083 
0084     size_t count = detail::iterator_range_size(first1, last1);
0085     vector<value_type> result(count, queue.get_context());
0086     transform(first1,
0087               last1,
0088               first2,
0089               result.begin(),
0090               transform_function,
0091               queue);
0092 
0093     return ::boost::compute::accumulate(result.begin(),
0094                                         result.end(),
0095                                         init,
0096                                         accumulate_function,
0097                                         queue);
0098 }
0099 
0100 } // end compute namespace
0101 } // end boost namespace
0102 
0103 #endif // BOOST_COMPUTE_ALGORITHM_INNER_PRODUCT_HPP