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_HPP
0012 #define BOOST_COMPUTE_ALGORITHM_TRANSFORM_HPP
0013 
0014 #include <boost/static_assert.hpp>
0015 
0016 #include <boost/compute/system.hpp>
0017 #include <boost/compute/command_queue.hpp>
0018 #include <boost/compute/algorithm/copy.hpp>
0019 #include <boost/compute/iterator/transform_iterator.hpp>
0020 #include <boost/compute/iterator/zip_iterator.hpp>
0021 #include <boost/compute/functional/detail/unpack.hpp>
0022 #include <boost/compute/type_traits/is_device_iterator.hpp>
0023 
0024 namespace boost {
0025 namespace compute {
0026 
0027 /// Transforms the elements in the range [\p first, \p last) using
0028 /// operator \p op and stores the results in the range beginning at
0029 /// \p result.
0030 ///
0031 /// For example, to calculate the absolute value for each element in a vector:
0032 ///
0033 /// \snippet test/test_transform.cpp transform_abs
0034 ///
0035 /// Space complexity: \Omega(1)
0036 ///
0037 /// \see copy()
0038 template<class InputIterator, class OutputIterator, class UnaryOperator>
0039 inline OutputIterator transform(InputIterator first,
0040                                 InputIterator last,
0041                                 OutputIterator result,
0042                                 UnaryOperator op,
0043                                 command_queue &queue = system::default_queue())
0044 {
0045     BOOST_STATIC_ASSERT(is_device_iterator<InputIterator>::value);
0046     BOOST_STATIC_ASSERT(is_device_iterator<OutputIterator>::value);
0047     return copy(
0048                ::boost::compute::make_transform_iterator(first, op),
0049                ::boost::compute::make_transform_iterator(last, op),
0050                result,
0051                queue
0052            );
0053 }
0054 
0055 /// \overload
0056 template<class InputIterator1,
0057          class InputIterator2,
0058          class OutputIterator,
0059          class BinaryOperator>
0060 inline OutputIterator transform(InputIterator1 first1,
0061                                 InputIterator1 last1,
0062                                 InputIterator2 first2,
0063                                 OutputIterator result,
0064                                 BinaryOperator op,
0065                                 command_queue &queue = system::default_queue())
0066 {
0067     BOOST_STATIC_ASSERT(is_device_iterator<InputIterator1>::value);
0068     BOOST_STATIC_ASSERT(is_device_iterator<InputIterator2>::value);
0069     BOOST_STATIC_ASSERT(is_device_iterator<OutputIterator>::value);
0070 
0071     typedef typename std::iterator_traits<InputIterator1>::difference_type difference_type;
0072 
0073     difference_type n = std::distance(first1, last1);
0074 
0075     return transform(
0076                ::boost::compute::make_zip_iterator(boost::make_tuple(first1, first2)),
0077                ::boost::compute::make_zip_iterator(boost::make_tuple(last1, first2 + n)),
0078                result,
0079                detail::unpack(op),
0080                queue
0081            );
0082 }
0083 
0084 } // end compute namespace
0085 } // end boost namespace
0086 
0087 #endif // BOOST_COMPUTE_ALGORITHM_TRANSFORM_HPP