transform.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. #ifndef BOOST_COMPUTE_ALGORITHM_TRANSFORM_HPP
  11. #define BOOST_COMPUTE_ALGORITHM_TRANSFORM_HPP
  12. #include <boost/static_assert.hpp>
  13. #include <boost/compute/system.hpp>
  14. #include <boost/compute/command_queue.hpp>
  15. #include <boost/compute/algorithm/copy.hpp>
  16. #include <boost/compute/iterator/transform_iterator.hpp>
  17. #include <boost/compute/iterator/zip_iterator.hpp>
  18. #include <boost/compute/functional/detail/unpack.hpp>
  19. #include <boost/compute/type_traits/is_device_iterator.hpp>
  20. namespace boost {
  21. namespace compute {
  22. /// Transforms the elements in the range [\p first, \p last) using
  23. /// operator \p op and stores the results in the range beginning at
  24. /// \p result.
  25. ///
  26. /// For example, to calculate the absolute value for each element in a vector:
  27. ///
  28. /// \snippet test/test_transform.cpp transform_abs
  29. ///
  30. /// Space complexity: \Omega(1)
  31. ///
  32. /// \see copy()
  33. template<class InputIterator, class OutputIterator, class UnaryOperator>
  34. inline OutputIterator transform(InputIterator first,
  35. InputIterator last,
  36. OutputIterator result,
  37. UnaryOperator op,
  38. command_queue &queue = system::default_queue())
  39. {
  40. BOOST_STATIC_ASSERT(is_device_iterator<InputIterator>::value);
  41. BOOST_STATIC_ASSERT(is_device_iterator<OutputIterator>::value);
  42. return copy(
  43. ::boost::compute::make_transform_iterator(first, op),
  44. ::boost::compute::make_transform_iterator(last, op),
  45. result,
  46. queue
  47. );
  48. }
  49. /// \overload
  50. template<class InputIterator1,
  51. class InputIterator2,
  52. class OutputIterator,
  53. class BinaryOperator>
  54. inline OutputIterator transform(InputIterator1 first1,
  55. InputIterator1 last1,
  56. InputIterator2 first2,
  57. OutputIterator result,
  58. BinaryOperator op,
  59. command_queue &queue = system::default_queue())
  60. {
  61. BOOST_STATIC_ASSERT(is_device_iterator<InputIterator1>::value);
  62. BOOST_STATIC_ASSERT(is_device_iterator<InputIterator2>::value);
  63. BOOST_STATIC_ASSERT(is_device_iterator<OutputIterator>::value);
  64. typedef typename std::iterator_traits<InputIterator1>::difference_type difference_type;
  65. difference_type n = std::distance(first1, last1);
  66. return transform(
  67. ::boost::compute::make_zip_iterator(boost::make_tuple(first1, first2)),
  68. ::boost::compute::make_zip_iterator(boost::make_tuple(last1, first2 + n)),
  69. result,
  70. detail::unpack(op),
  71. queue
  72. );
  73. }
  74. } // end compute namespace
  75. } // end boost namespace
  76. #endif // BOOST_COMPUTE_ALGORITHM_TRANSFORM_HPP