Back to home page

EIC code displayed by LXR

 
 

    


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

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_TRANSFORM_REDUCE_HPP
0012 #define BOOST_COMPUTE_ALGORITHM_TRANSFORM_REDUCE_HPP
0013 
0014 #include <boost/static_assert.hpp>
0015 
0016 #include <boost/compute/system.hpp>
0017 #include <boost/compute/algorithm/reduce.hpp>
0018 #include <boost/compute/iterator/transform_iterator.hpp>
0019 #include <boost/compute/iterator/zip_iterator.hpp>
0020 #include <boost/compute/functional/detail/unpack.hpp>
0021 #include <boost/compute/detail/iterator_range_size.hpp>
0022 #include <boost/compute/type_traits/is_device_iterator.hpp>
0023 
0024 namespace boost {
0025 namespace compute {
0026 
0027 /// Transforms each value in the range [\p first, \p last) with the unary
0028 /// \p transform_function and then reduces each transformed value with
0029 /// \p reduce_function.
0030 ///
0031 /// For example, to calculate the sum of the absolute values of a vector
0032 /// of integers:
0033 ///
0034 /// \snippet test/test_transform_reduce.cpp sum_abs_int
0035 ///
0036 /// Space complexity on GPUs: \Omega(n)<br>
0037 /// Space complexity on CPUs: \Omega(1)
0038 ///
0039 /// \see reduce(), inner_product()
0040 template<class InputIterator,
0041          class OutputIterator,
0042          class UnaryTransformFunction,
0043          class BinaryReduceFunction>
0044 inline void transform_reduce(InputIterator first,
0045                              InputIterator last,
0046                              OutputIterator result,
0047                              UnaryTransformFunction transform_function,
0048                              BinaryReduceFunction reduce_function,
0049                              command_queue &queue = system::default_queue())
0050 {
0051     BOOST_STATIC_ASSERT(is_device_iterator<InputIterator>::value);
0052     ::boost::compute::reduce(
0053         ::boost::compute::make_transform_iterator(first, transform_function),
0054         ::boost::compute::make_transform_iterator(last, transform_function),
0055         result,
0056         reduce_function,
0057         queue
0058     );
0059 }
0060 
0061 /// \overload
0062 template<class InputIterator1,
0063          class InputIterator2,
0064          class OutputIterator,
0065          class BinaryTransformFunction,
0066          class BinaryReduceFunction>
0067 inline void transform_reduce(InputIterator1 first1,
0068                              InputIterator1 last1,
0069                              InputIterator2 first2,
0070                              OutputIterator result,
0071                              BinaryTransformFunction transform_function,
0072                              BinaryReduceFunction reduce_function,
0073                              command_queue &queue = system::default_queue())
0074 {
0075     BOOST_STATIC_ASSERT(is_device_iterator<InputIterator1>::value);
0076     BOOST_STATIC_ASSERT(is_device_iterator<InputIterator2>::value);
0077     BOOST_STATIC_ASSERT(is_device_iterator<OutputIterator>::value);
0078 
0079     typedef typename std::iterator_traits<InputIterator1>::difference_type difference_type;
0080 
0081     difference_type n = std::distance(first1, last1);
0082 
0083     ::boost::compute::transform_reduce(
0084         ::boost::compute::make_zip_iterator(
0085             boost::make_tuple(first1, first2)
0086         ),
0087         ::boost::compute::make_zip_iterator(
0088             boost::make_tuple(last1, first2 + n)
0089         ),
0090         result,
0091         detail::unpack(transform_function),
0092         reduce_function,
0093         queue
0094     );
0095 }
0096 
0097 } // end compute namespace
0098 } // end boost namespace
0099 
0100 #endif // BOOST_COMPUTE_ALGORITHM_TRANSFORM_REDUCE_HPP