search_all.hpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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_DETAIL_SEARCH_ALL_HPP
  11. #define BOOST_COMPUTE_ALGORITHM_DETAIL_SEARCH_ALL_HPP
  12. #include <boost/compute/algorithm/copy.hpp>
  13. #include <boost/compute/container/vector.hpp>
  14. #include <boost/compute/detail/iterator_range_size.hpp>
  15. #include <boost/compute/detail/meta_kernel.hpp>
  16. #include <boost/compute/lambda.hpp>
  17. #include <boost/compute/system.hpp>
  18. namespace boost {
  19. namespace compute {
  20. namespace detail {
  21. ///
  22. /// \brief Search kernel class
  23. ///
  24. /// Subclass of meta_kernel which is capable of performing pattern matching
  25. ///
  26. template<class PatternIterator, class TextIterator, class OutputIterator>
  27. class search_kernel : public meta_kernel
  28. {
  29. public:
  30. search_kernel() : meta_kernel("search")
  31. {}
  32. void set_range(PatternIterator p_first,
  33. PatternIterator p_last,
  34. TextIterator t_first,
  35. TextIterator t_last,
  36. OutputIterator result)
  37. {
  38. m_p_count = iterator_range_size(p_first, p_last);
  39. m_p_count_arg = add_arg<uint_>("p_count");
  40. m_count = iterator_range_size(t_first, t_last);
  41. m_count = m_count + 1 - m_p_count;
  42. *this <<
  43. "uint i = get_global_id(0);\n" <<
  44. "const uint i1 = i;\n" <<
  45. "uint j;\n" <<
  46. "for(j = 0; j<p_count; j++,i++)\n" <<
  47. "{\n" <<
  48. " if(" << p_first[expr<uint_>("j")] << " != " <<
  49. t_first[expr<uint_>("i")] << ")\n" <<
  50. " j = p_count + 1;\n" <<
  51. "}\n" <<
  52. "if(j == p_count)\n" <<
  53. result[expr<uint_>("i1")] << " = 1;\n" <<
  54. "else\n" <<
  55. result[expr<uint_>("i1")] << " = 0;\n";
  56. }
  57. event exec(command_queue &queue)
  58. {
  59. if(m_count == 0) {
  60. return event();
  61. }
  62. set_arg(m_p_count_arg, uint_(m_p_count));
  63. return exec_1d(queue, 0, m_count);
  64. }
  65. private:
  66. size_t m_p_count;
  67. size_t m_p_count_arg;
  68. size_t m_count;
  69. };
  70. } //end detail namespace
  71. } //end compute namespace
  72. } //end boost namespace
  73. #endif // BOOST_COMPUTE_ALGORITHM_DETAIL_SEARCH_ALL_HPP