includes.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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_INCLUDES_HPP
  11. #define BOOST_COMPUTE_ALGORITHM_INCLUDES_HPP
  12. #include <iterator>
  13. #include <boost/compute/algorithm/detail/balanced_path.hpp>
  14. #include <boost/compute/algorithm/fill_n.hpp>
  15. #include <boost/compute/algorithm/find.hpp>
  16. #include <boost/compute/container/vector.hpp>
  17. #include <boost/compute/detail/iterator_range_size.hpp>
  18. #include <boost/compute/detail/meta_kernel.hpp>
  19. #include <boost/compute/detail/read_write_single_value.hpp>
  20. #include <boost/compute/system.hpp>
  21. namespace boost {
  22. namespace compute {
  23. namespace detail {
  24. ///
  25. /// \brief Serial includes kernel class
  26. ///
  27. /// Subclass of meta_kernel to perform includes operation after tiling
  28. ///
  29. class serial_includes_kernel : meta_kernel
  30. {
  31. public:
  32. serial_includes_kernel() : meta_kernel("includes")
  33. {
  34. }
  35. template<class InputIterator1, class InputIterator2,
  36. class InputIterator3, class InputIterator4,
  37. class OutputIterator>
  38. void set_range(InputIterator1 first1,
  39. InputIterator2 first2,
  40. InputIterator3 tile_first1,
  41. InputIterator3 tile_last1,
  42. InputIterator4 tile_first2,
  43. OutputIterator result)
  44. {
  45. m_count = iterator_range_size(tile_first1, tile_last1) - 1;
  46. *this <<
  47. "uint i = get_global_id(0);\n" <<
  48. "uint start1 = " << tile_first1[expr<uint_>("i")] << ";\n" <<
  49. "uint end1 = " << tile_first1[expr<uint_>("i+1")] << ";\n" <<
  50. "uint start2 = " << tile_first2[expr<uint_>("i")] << ";\n" <<
  51. "uint end2 = " << tile_first2[expr<uint_>("i+1")] << ";\n" <<
  52. "uint includes = 1;\n" <<
  53. "while(start1<end1 && start2<end2)\n" <<
  54. "{\n" <<
  55. " if(" << first1[expr<uint_>("start1")] << " == " <<
  56. first2[expr<uint_>("start2")] << ")\n" <<
  57. " {\n" <<
  58. " start1++; start2++;\n" <<
  59. " }\n" <<
  60. " else if(" << first1[expr<uint_>("start1")] << " < " <<
  61. first2[expr<uint_>("start2")] << ")\n" <<
  62. " start1++;\n" <<
  63. " else\n" <<
  64. " {\n" <<
  65. " includes = 0;\n" <<
  66. " break;\n" <<
  67. " }\n" <<
  68. "}\n" <<
  69. "if(start2<end2)\n" <<
  70. " includes = 0;\n" <<
  71. result[expr<uint_>("i")] << " = includes;\n";
  72. }
  73. event exec(command_queue &queue)
  74. {
  75. if(m_count == 0) {
  76. return event();
  77. }
  78. return exec_1d(queue, 0, m_count);
  79. }
  80. private:
  81. size_t m_count;
  82. };
  83. } //end detail namespace
  84. ///
  85. /// \brief Includes algorithm
  86. ///
  87. /// Finds if the sorted range [first1, last1) includes the sorted
  88. /// range [first2, last2). In other words, it checks if [first1, last1) is
  89. /// a superset of [first2, last2).
  90. ///
  91. /// \return True, if [first1, last1) includes [first2, last2). False otherwise.
  92. ///
  93. /// \param first1 Iterator pointing to start of first set
  94. /// \param last1 Iterator pointing to end of first set
  95. /// \param first2 Iterator pointing to start of second set
  96. /// \param last2 Iterator pointing to end of second set
  97. /// \param queue Queue on which to execute
  98. ///
  99. template<class InputIterator1, class InputIterator2>
  100. inline bool includes(InputIterator1 first1,
  101. InputIterator1 last1,
  102. InputIterator2 first2,
  103. InputIterator2 last2,
  104. command_queue &queue = system::default_queue())
  105. {
  106. size_t tile_size = 1024;
  107. size_t count1 = detail::iterator_range_size(first1, last1);
  108. size_t count2 = detail::iterator_range_size(first2, last2);
  109. vector<uint_> tile_a((count1+count2+tile_size-1)/tile_size+1, queue.get_context());
  110. vector<uint_> tile_b((count1+count2+tile_size-1)/tile_size+1, queue.get_context());
  111. // Tile the sets
  112. detail::balanced_path_kernel tiling_kernel;
  113. tiling_kernel.tile_size = static_cast<unsigned int>(tile_size);
  114. tiling_kernel.set_range(first1, last1, first2, last2,
  115. tile_a.begin()+1, tile_b.begin()+1);
  116. fill_n(tile_a.begin(), 1, uint_(0), queue);
  117. fill_n(tile_b.begin(), 1, uint_(0), queue);
  118. tiling_kernel.exec(queue);
  119. fill_n(tile_a.end()-1, 1, static_cast<uint_>(count1), queue);
  120. fill_n(tile_b.end()-1, 1, static_cast<uint_>(count2), queue);
  121. vector<uint_> result((count1+count2+tile_size-1)/tile_size, queue.get_context());
  122. // Find individually
  123. detail::serial_includes_kernel includes_kernel;
  124. includes_kernel.set_range(first1, first2, tile_a.begin(), tile_a.end(),
  125. tile_b.begin(), result.begin());
  126. includes_kernel.exec(queue);
  127. return find(result.begin(), result.end(), 0, queue) == result.end();
  128. }
  129. } //end compute namespace
  130. } //end boost namespace
  131. #endif // BOOST_COMPUTE_ALGORITHM_SET_UNION_HPP