scatter_if.hpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2015 Jakub Pola <[email protected]>
  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_SCATTER_IF_HPP
  11. #define BOOST_COMPUTE_ALGORITHM_SCATTER_IF_HPP
  12. #include <boost/static_assert.hpp>
  13. #include <boost/algorithm/string/replace.hpp>
  14. #include <boost/compute/system.hpp>
  15. #include <boost/compute/exception.hpp>
  16. #include <boost/compute/command_queue.hpp>
  17. #include <boost/compute/iterator/buffer_iterator.hpp>
  18. #include <boost/compute/type_traits/type_name.hpp>
  19. #include <boost/compute/detail/iterator_range_size.hpp>
  20. #include <boost/compute/detail/meta_kernel.hpp>
  21. #include <boost/compute/type_traits/is_device_iterator.hpp>
  22. namespace boost {
  23. namespace compute {
  24. namespace detail {
  25. template<class InputIterator, class MapIterator, class StencilIterator, class OutputIterator, class Predicate>
  26. class scatter_if_kernel : meta_kernel
  27. {
  28. public:
  29. scatter_if_kernel() : meta_kernel("scatter_if")
  30. {}
  31. void set_range(InputIterator first,
  32. InputIterator last,
  33. MapIterator map,
  34. StencilIterator stencil,
  35. OutputIterator result,
  36. Predicate predicate)
  37. {
  38. m_count = iterator_range_size(first, last);
  39. m_input_offset = first.get_index();
  40. m_output_offset = result.get_index();
  41. m_input_offset_arg = add_arg<uint_>("input_offset");
  42. m_output_offset_arg = add_arg<uint_>("output_offset");
  43. *this <<
  44. "const uint i = get_global_id(0);\n" <<
  45. "uint i1 = " << map[expr<uint_>("i")] <<
  46. " + output_offset;\n" <<
  47. "uint i2 = i + input_offset;\n" <<
  48. if_(predicate(stencil[expr<uint_>("i")])) << "\n" <<
  49. result[expr<uint_>("i1")] << "=" <<
  50. first[expr<uint_>("i2")] << ";\n";
  51. }
  52. event exec(command_queue &queue)
  53. {
  54. if(m_count == 0) {
  55. return event();
  56. }
  57. set_arg(m_input_offset_arg, uint_(m_input_offset));
  58. set_arg(m_output_offset_arg, uint_(m_output_offset));
  59. return exec_1d(queue, 0, m_count);
  60. }
  61. private:
  62. size_t m_count;
  63. size_t m_input_offset;
  64. size_t m_input_offset_arg;
  65. size_t m_output_offset;
  66. size_t m_output_offset_arg;
  67. };
  68. } // end detail namespace
  69. /// Copies the elements from the range [\p first, \p last) to the range
  70. /// beginning at \p result using the output indices from the range beginning
  71. /// at \p map if stencil is resolved to true. By default the predicate is
  72. /// an identity
  73. ///
  74. /// Space complexity: \Omega(1)
  75. template<class InputIterator, class MapIterator, class StencilIterator, class OutputIterator,
  76. class Predicate>
  77. inline void scatter_if(InputIterator first,
  78. InputIterator last,
  79. MapIterator map,
  80. StencilIterator stencil,
  81. OutputIterator result,
  82. Predicate predicate,
  83. command_queue &queue = system::default_queue())
  84. {
  85. BOOST_STATIC_ASSERT(is_device_iterator<InputIterator>::value);
  86. BOOST_STATIC_ASSERT(is_device_iterator<MapIterator>::value);
  87. BOOST_STATIC_ASSERT(is_device_iterator<StencilIterator>::value);
  88. BOOST_STATIC_ASSERT(is_device_iterator<OutputIterator>::value);
  89. detail::scatter_if_kernel<InputIterator, MapIterator, StencilIterator, OutputIterator, Predicate> kernel;
  90. kernel.set_range(first, last, map, stencil, result, predicate);
  91. kernel.exec(queue);
  92. }
  93. template<class InputIterator, class MapIterator, class StencilIterator, class OutputIterator>
  94. inline void scatter_if(InputIterator first,
  95. InputIterator last,
  96. MapIterator map,
  97. StencilIterator stencil,
  98. OutputIterator result,
  99. command_queue &queue = system::default_queue())
  100. {
  101. BOOST_STATIC_ASSERT(is_device_iterator<InputIterator>::value);
  102. BOOST_STATIC_ASSERT(is_device_iterator<MapIterator>::value);
  103. BOOST_STATIC_ASSERT(is_device_iterator<StencilIterator>::value);
  104. BOOST_STATIC_ASSERT(is_device_iterator<OutputIterator>::value);
  105. typedef typename std::iterator_traits<StencilIterator>::value_type T;
  106. scatter_if(first, last, map, stencil, result, identity<T>(), queue);
  107. }
  108. } // end compute namespace
  109. } // end boost namespace
  110. #endif // BOOST_COMPUTE_ALGORITHM_SCATTER_IF_HPP