scatter.hpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013 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_SCATTER_HPP
  11. #define BOOST_COMPUTE_ALGORITHM_SCATTER_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 OutputIterator>
  26. class scatter_kernel : meta_kernel
  27. {
  28. public:
  29. scatter_kernel() : meta_kernel("scatter")
  30. {}
  31. void set_range(InputIterator first,
  32. InputIterator last,
  33. MapIterator map,
  34. OutputIterator result)
  35. {
  36. m_count = iterator_range_size(first, last);
  37. m_input_offset = first.get_index();
  38. m_output_offset = result.get_index();
  39. m_input_offset_arg = add_arg<uint_>("input_offset");
  40. m_output_offset_arg = add_arg<uint_>("output_offset");
  41. *this <<
  42. "const uint i = get_global_id(0);\n" <<
  43. "uint i1 = " << map[expr<uint_>("i")] <<
  44. " + output_offset;\n" <<
  45. "uint i2 = i + input_offset;\n" <<
  46. result[expr<uint_>("i1")] << "=" <<
  47. first[expr<uint_>("i2")] << ";\n";
  48. }
  49. event exec(command_queue &queue)
  50. {
  51. if(m_count == 0) {
  52. return event();
  53. }
  54. set_arg(m_input_offset_arg, uint_(m_input_offset));
  55. set_arg(m_output_offset_arg, uint_(m_output_offset));
  56. return exec_1d(queue, 0, m_count);
  57. }
  58. private:
  59. size_t m_count;
  60. size_t m_input_offset;
  61. size_t m_input_offset_arg;
  62. size_t m_output_offset;
  63. size_t m_output_offset_arg;
  64. };
  65. } // end detail namespace
  66. /// Copies the elements from the range [\p first, \p last) to the range
  67. /// beginning at \p result using the output indices from the range beginning
  68. /// at \p map.
  69. ///
  70. /// Space complexity: \Omega(1)
  71. ///
  72. /// \see gather()
  73. template<class InputIterator, class MapIterator, class OutputIterator>
  74. inline void scatter(InputIterator first,
  75. InputIterator last,
  76. MapIterator map,
  77. OutputIterator result,
  78. command_queue &queue = system::default_queue())
  79. {
  80. BOOST_STATIC_ASSERT(is_device_iterator<InputIterator>::value);
  81. BOOST_STATIC_ASSERT(is_device_iterator<MapIterator>::value);
  82. BOOST_STATIC_ASSERT(is_device_iterator<OutputIterator>::value);
  83. detail::scatter_kernel<InputIterator, MapIterator, OutputIterator> kernel;
  84. kernel.set_range(first, last, map, result);
  85. kernel.exec(queue);
  86. }
  87. } // end compute namespace
  88. } // end boost namespace
  89. #endif // BOOST_COMPUTE_ALGORITHM_SCATTER_HPP