scatter.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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/algorithm/string/replace.hpp>
  13. #include <boost/compute/system.hpp>
  14. #include <boost/compute/exception.hpp>
  15. #include <boost/compute/command_queue.hpp>
  16. #include <boost/compute/iterator/buffer_iterator.hpp>
  17. #include <boost/compute/type_traits/type_name.hpp>
  18. #include <boost/compute/detail/iterator_range_size.hpp>
  19. #include <boost/compute/detail/meta_kernel.hpp>
  20. namespace boost {
  21. namespace compute {
  22. namespace detail {
  23. template<class InputIterator, class MapIterator, class OutputIterator>
  24. class scatter_kernel : meta_kernel
  25. {
  26. public:
  27. scatter_kernel() : meta_kernel("scatter")
  28. {}
  29. void set_range(InputIterator first,
  30. InputIterator last,
  31. MapIterator map,
  32. OutputIterator result)
  33. {
  34. m_count = iterator_range_size(first, last);
  35. m_input_offset = first.get_index();
  36. m_output_offset = result.get_index();
  37. m_input_offset_arg = add_arg<uint_>("input_offset");
  38. m_output_offset_arg = add_arg<uint_>("output_offset");
  39. *this <<
  40. "const uint i = get_global_id(0);\n" <<
  41. "uint i1 = " << map[expr<uint_>("i")] <<
  42. " + output_offset;\n" <<
  43. "uint i2 = i + input_offset;\n" <<
  44. result[expr<uint_>("i1")] << "=" <<
  45. first[expr<uint_>("i2")] << ";\n";
  46. }
  47. event exec(command_queue &queue)
  48. {
  49. if(m_count == 0) {
  50. return event();
  51. }
  52. set_arg(m_input_offset_arg, uint_(m_input_offset));
  53. set_arg(m_output_offset_arg, uint_(m_output_offset));
  54. return exec_1d(queue, 0, m_count);
  55. }
  56. private:
  57. size_t m_count;
  58. size_t m_input_offset;
  59. size_t m_input_offset_arg;
  60. size_t m_output_offset;
  61. size_t m_output_offset_arg;
  62. };
  63. } // end detail namespace
  64. /// Copies the elements from the range [\p first, \p last) to the range
  65. /// beginning at \p result using the output indices from the range beginning
  66. /// at \p map.
  67. ///
  68. /// \see gather()
  69. template<class InputIterator, class MapIterator, class OutputIterator>
  70. inline void scatter(InputIterator first,
  71. InputIterator last,
  72. MapIterator map,
  73. OutputIterator result,
  74. command_queue &queue = system::default_queue())
  75. {
  76. detail::scatter_kernel<InputIterator, MapIterator, OutputIterator> kernel;
  77. kernel.set_range(first, last, map, result);
  78. kernel.exec(queue);
  79. }
  80. } // end compute namespace
  81. } // end boost namespace
  82. #endif // BOOST_COMPUTE_ALGORITHM_SCATTER_HPP