adjacent_difference.hpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013-2014 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_ADJACENT_DIFFERENCE_HPP
  11. #define BOOST_COMPUTE_ALGORITHM_ADJACENT_DIFFERENCE_HPP
  12. #include <iterator>
  13. #include <boost/static_assert.hpp>
  14. #include <boost/compute/system.hpp>
  15. #include <boost/compute/command_queue.hpp>
  16. #include <boost/compute/detail/meta_kernel.hpp>
  17. #include <boost/compute/detail/iterator_range_size.hpp>
  18. #include <boost/compute/functional/operator.hpp>
  19. #include <boost/compute/container/vector.hpp>
  20. #include <boost/compute/type_traits/is_device_iterator.hpp>
  21. namespace boost {
  22. namespace compute {
  23. namespace detail {
  24. template<class InputIterator, class OutputIterator, class BinaryFunction>
  25. inline OutputIterator
  26. dispatch_adjacent_difference(InputIterator first,
  27. InputIterator last,
  28. OutputIterator result,
  29. BinaryFunction op,
  30. command_queue &queue = system::default_queue())
  31. {
  32. size_t count = detail::iterator_range_size(first, last);
  33. detail::meta_kernel k("adjacent_difference");
  34. k << "const uint i = get_global_id(0);\n"
  35. << "if(i == 0){\n"
  36. << " " << result[k.var<uint_>("0")] << " = " << first[k.var<uint_>("0")] << ";\n"
  37. << "}\n"
  38. << "else {\n"
  39. << " " << result[k.var<uint_>("i")] << " = "
  40. << op(first[k.var<uint_>("i")], first[k.var<uint_>("i-1")]) << ";\n"
  41. << "}\n";
  42. k.exec_1d(queue, 0, count, 1);
  43. return result + count;
  44. }
  45. } // end detail namespace
  46. /// Stores the difference of each pair of consecutive values in the range
  47. /// [\p first, \p last) to the range beginning at \p result. If \p op is not
  48. /// provided, \c minus<T> is used.
  49. ///
  50. /// \param first first element in the input range
  51. /// \param last last element in the input range
  52. /// \param result first element in the output range
  53. /// \param op binary difference function
  54. /// \param queue command queue to perform the operation
  55. ///
  56. /// \return \c OutputIterator to the end of the result range
  57. ///
  58. /// Space complexity: \Omega(1)<br>
  59. /// Space complexity when \p result == \p first: \Omega(n)
  60. ///
  61. /// \see adjacent_find()
  62. template<class InputIterator, class OutputIterator, class BinaryFunction>
  63. inline OutputIterator
  64. adjacent_difference(InputIterator first,
  65. InputIterator last,
  66. OutputIterator result,
  67. BinaryFunction op,
  68. command_queue &queue = system::default_queue())
  69. {
  70. BOOST_STATIC_ASSERT(is_device_iterator<InputIterator>::value);
  71. BOOST_STATIC_ASSERT(is_device_iterator<OutputIterator>::value);
  72. typedef typename std::iterator_traits<InputIterator>::value_type value_type;
  73. if(first == last) {
  74. return result;
  75. }
  76. if (first == result) {
  77. vector<value_type> temp(detail::iterator_range_size(first, last),
  78. queue.get_context());
  79. copy(first, last, temp.begin(), queue);
  80. return ::boost::compute::detail::dispatch_adjacent_difference(
  81. temp.begin(), temp.end(), result, op, queue
  82. );
  83. }
  84. else {
  85. return ::boost::compute::detail::dispatch_adjacent_difference(
  86. first, last, result, op, queue
  87. );
  88. }
  89. }
  90. /// \overload
  91. template<class InputIterator, class OutputIterator>
  92. inline OutputIterator
  93. adjacent_difference(InputIterator first,
  94. InputIterator last,
  95. OutputIterator result,
  96. command_queue &queue = system::default_queue())
  97. {
  98. BOOST_STATIC_ASSERT(is_device_iterator<InputIterator>::value);
  99. BOOST_STATIC_ASSERT(is_device_iterator<OutputIterator>::value);
  100. typedef typename std::iterator_traits<InputIterator>::value_type value_type;
  101. return ::boost::compute::adjacent_difference(
  102. first, last, result, ::boost::compute::minus<value_type>(), queue
  103. );
  104. }
  105. } // end compute namespace
  106. } // end boost namespace
  107. #endif // BOOST_COMPUTE_ALGORITHM_ADJACENT_DIFFERENCE_HPP