unique_copy.hpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2014 Roshan <thisisroshansmail@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_UNIQUE_COPY_HPP
  11. #define BOOST_COMPUTE_ALGORITHM_UNIQUE_COPY_HPP
  12. #include <boost/static_assert.hpp>
  13. #include <boost/compute/command_queue.hpp>
  14. #include <boost/compute/lambda.hpp>
  15. #include <boost/compute/system.hpp>
  16. #include <boost/compute/algorithm/copy_if.hpp>
  17. #include <boost/compute/algorithm/transform.hpp>
  18. #include <boost/compute/algorithm/gather.hpp>
  19. #include <boost/compute/container/vector.hpp>
  20. #include <boost/compute/detail/iterator_range_size.hpp>
  21. #include <boost/compute/detail/meta_kernel.hpp>
  22. #include <boost/compute/functional/operator.hpp>
  23. #include <boost/compute/type_traits/is_device_iterator.hpp>
  24. namespace boost {
  25. namespace compute {
  26. namespace detail {
  27. template<class InputIterator, class OutputIterator, class BinaryPredicate>
  28. inline OutputIterator serial_unique_copy(InputIterator first,
  29. InputIterator last,
  30. OutputIterator result,
  31. BinaryPredicate op,
  32. command_queue &queue)
  33. {
  34. if(first == last){
  35. return result;
  36. }
  37. typedef typename std::iterator_traits<InputIterator>::value_type value_type;
  38. const context &context = queue.get_context();
  39. size_t count = detail::iterator_range_size(first, last);
  40. detail::meta_kernel k("serial_unique_copy");
  41. vector<uint_> unique_count_vector(1, context);
  42. size_t size_arg = k.add_arg<const uint_>("size");
  43. size_t unique_count_arg = k.add_arg<uint_ *>(memory_object::global_memory, "unique_count");
  44. k << k.decl<uint_>("index") << " = 0;\n"
  45. << k.decl<value_type>("current") << " = " << first[k.var<uint_>("0")] << ";\n"
  46. << result[k.var<uint_>("0")] << " = current;\n"
  47. << "for(uint i = 1; i < size; i++){\n"
  48. << " " << k.decl<value_type>("next") << " = " << first[k.var<uint_>("i")] << ";\n"
  49. << " if(!" << op(k.var<value_type>("current"), k.var<value_type>("next")) << "){\n"
  50. << " " << result[k.var<uint_>("++index")] << " = next;\n"
  51. << " " << "current = next;\n"
  52. << " }\n"
  53. << "}\n"
  54. << "*unique_count = index + 1;\n";
  55. k.set_arg<const uint_>(size_arg, count);
  56. k.set_arg(unique_count_arg, unique_count_vector.get_buffer());
  57. k.exec_1d(queue, 0, 1, 1);
  58. uint_ unique_count;
  59. copy_n(unique_count_vector.begin(), 1, &unique_count, queue);
  60. return result + unique_count;
  61. }
  62. template<class InputIterator, class OutputIterator, class BinaryPredicate>
  63. inline OutputIterator unique_copy(InputIterator first,
  64. InputIterator last,
  65. OutputIterator result,
  66. BinaryPredicate op,
  67. command_queue &queue)
  68. {
  69. if(first == last){
  70. return result;
  71. }
  72. const context &context = queue.get_context();
  73. size_t count = detail::iterator_range_size(first, last);
  74. // flags marking unique elements
  75. vector<uint_> flags(count, context);
  76. // find each unique element and mark it with a one
  77. transform(
  78. first, last - 1, first + 1, flags.begin() + 1, not2(op), queue
  79. );
  80. // first element is always unique
  81. fill_n(flags.begin(), 1, 1, queue);
  82. // storage for desination indices
  83. vector<uint_> indices(count, context);
  84. // copy indices for each unique element
  85. vector<uint_>::iterator last_index = detail::copy_index_if(
  86. flags.begin(), flags.end(), indices.begin(), lambda::_1 == 1, queue
  87. );
  88. // copy unique values from input to output using the computed indices
  89. gather(indices.begin(), last_index, first, result, queue);
  90. // return an iterator to the end of the unique output range
  91. return result + std::distance(indices.begin(), last_index);
  92. }
  93. } // end detail namespace
  94. /// Makes a copy of the range [first, last) and removes all consecutive
  95. /// duplicate elements (determined by \p op) from the copy. If \p op is not
  96. /// provided, the equality operator is used.
  97. ///
  98. /// \param first first element in the input range
  99. /// \param last last element in the input range
  100. /// \param result first element in the result range
  101. /// \param op binary operator used to check for uniqueness
  102. /// \param queue command queue to perform the operation
  103. ///
  104. /// \return \c OutputIterator to the end of the result range
  105. ///
  106. /// Space complexity: \Omega(4n)
  107. ///
  108. /// \see unique()
  109. template<class InputIterator, class OutputIterator, class BinaryPredicate>
  110. inline OutputIterator unique_copy(InputIterator first,
  111. InputIterator last,
  112. OutputIterator result,
  113. BinaryPredicate op,
  114. command_queue &queue = system::default_queue())
  115. {
  116. BOOST_STATIC_ASSERT(is_device_iterator<InputIterator>::value);
  117. BOOST_STATIC_ASSERT(is_device_iterator<OutputIterator>::value);
  118. size_t count = detail::iterator_range_size(first, last);
  119. if(count < 32){
  120. return detail::serial_unique_copy(first, last, result, op, queue);
  121. }
  122. else {
  123. return detail::unique_copy(first, last, result, op, queue);
  124. }
  125. }
  126. /// \overload
  127. template<class InputIterator, class OutputIterator>
  128. inline OutputIterator unique_copy(InputIterator first,
  129. InputIterator last,
  130. OutputIterator result,
  131. command_queue &queue = system::default_queue())
  132. {
  133. BOOST_STATIC_ASSERT(is_device_iterator<InputIterator>::value);
  134. BOOST_STATIC_ASSERT(is_device_iterator<OutputIterator>::value);
  135. typedef typename std::iterator_traits<InputIterator>::value_type value_type;
  136. return ::boost::compute::unique_copy(
  137. first, last, result, ::boost::compute::equal_to<value_type>(), queue
  138. );
  139. }
  140. } // end compute namespace
  141. } // end boost namespace
  142. #endif // BOOST_COMPUTE_ALGORITHM_UNIQUE_COPY_HPP