• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
3 //
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
7 //
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
10 
11 #ifndef BOOST_COMPUTE_ALGORITHM_TRANSFORM_HPP
12 #define BOOST_COMPUTE_ALGORITHM_TRANSFORM_HPP
13 
14 #include <boost/static_assert.hpp>
15 
16 #include <boost/compute/system.hpp>
17 #include <boost/compute/command_queue.hpp>
18 #include <boost/compute/algorithm/copy.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>
22 #include <boost/compute/type_traits/is_device_iterator.hpp>
23 
24 namespace boost {
25 namespace compute {
26 
27 /// Transforms the elements in the range [\p first, \p last) using
28 /// operator \p op and stores the results in the range beginning at
29 /// \p result.
30 ///
31 /// For example, to calculate the absolute value for each element in a vector:
32 ///
33 /// \snippet test/test_transform.cpp transform_abs
34 ///
35 /// Space complexity: \Omega(1)
36 ///
37 /// \see copy()
38 template<class InputIterator, class OutputIterator, class UnaryOperator>
transform(InputIterator first,InputIterator last,OutputIterator result,UnaryOperator op,command_queue & queue=system::default_queue ())39 inline OutputIterator transform(InputIterator first,
40                                 InputIterator last,
41                                 OutputIterator result,
42                                 UnaryOperator op,
43                                 command_queue &queue = system::default_queue())
44 {
45     BOOST_STATIC_ASSERT(is_device_iterator<InputIterator>::value);
46     BOOST_STATIC_ASSERT(is_device_iterator<OutputIterator>::value);
47     return copy(
48                ::boost::compute::make_transform_iterator(first, op),
49                ::boost::compute::make_transform_iterator(last, op),
50                result,
51                queue
52            );
53 }
54 
55 /// \overload
56 template<class InputIterator1,
57          class InputIterator2,
58          class OutputIterator,
59          class BinaryOperator>
transform(InputIterator1 first1,InputIterator1 last1,InputIterator2 first2,OutputIterator result,BinaryOperator op,command_queue & queue=system::default_queue ())60 inline OutputIterator transform(InputIterator1 first1,
61                                 InputIterator1 last1,
62                                 InputIterator2 first2,
63                                 OutputIterator result,
64                                 BinaryOperator op,
65                                 command_queue &queue = system::default_queue())
66 {
67     BOOST_STATIC_ASSERT(is_device_iterator<InputIterator1>::value);
68     BOOST_STATIC_ASSERT(is_device_iterator<InputIterator2>::value);
69     BOOST_STATIC_ASSERT(is_device_iterator<OutputIterator>::value);
70 
71     typedef typename std::iterator_traits<InputIterator1>::difference_type difference_type;
72 
73     difference_type n = std::distance(first1, last1);
74 
75     return transform(
76                ::boost::compute::make_zip_iterator(boost::make_tuple(first1, first2)),
77                ::boost::compute::make_zip_iterator(boost::make_tuple(last1, first2 + n)),
78                result,
79                detail::unpack(op),
80                queue
81            );
82 }
83 
84 } // end compute namespace
85 } // end boost namespace
86 
87 #endif // BOOST_COMPUTE_ALGORITHM_TRANSFORM_HPP
88