gather.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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_GATHER_HPP
  11. #define BOOST_COMPUTE_ALGORITHM_GATHER_HPP
  12. #include <boost/compute/command_queue.hpp>
  13. #include <boost/compute/detail/iterator_range_size.hpp>
  14. #include <boost/compute/detail/meta_kernel.hpp>
  15. #include <boost/compute/exception.hpp>
  16. #include <boost/compute/iterator/buffer_iterator.hpp>
  17. #include <boost/compute/system.hpp>
  18. #include <boost/compute/type_traits/type_name.hpp>
  19. namespace boost {
  20. namespace compute {
  21. namespace detail {
  22. template<class InputIterator, class MapIterator, class OutputIterator>
  23. class gather_kernel : public meta_kernel
  24. {
  25. public:
  26. gather_kernel() : meta_kernel("gather")
  27. {}
  28. void set_range(MapIterator first,
  29. MapIterator last,
  30. InputIterator input,
  31. OutputIterator result)
  32. {
  33. m_count = iterator_range_size(first, last);
  34. m_offset = first.get_index();
  35. *this <<
  36. "const uint i = get_global_id(0);\n" <<
  37. result[expr<uint_>("i")] << "=" <<
  38. input[first[expr<uint_>("i")]] << ";\n";
  39. }
  40. event exec(command_queue &queue)
  41. {
  42. if(m_count == 0) {
  43. return event();
  44. }
  45. return exec_1d(queue, m_offset, m_count);
  46. }
  47. private:
  48. size_t m_count;
  49. size_t m_offset;
  50. };
  51. } // end detail namespace
  52. /// Copies the elements using the indices from the range [\p first, \p last)
  53. /// to the range beginning at \p result using the input values from the range
  54. /// beginning at \p input.
  55. ///
  56. /// \see scatter()
  57. template<class InputIterator, class MapIterator, class OutputIterator>
  58. inline void gather(MapIterator first,
  59. MapIterator last,
  60. InputIterator input,
  61. OutputIterator result,
  62. command_queue &queue = system::default_queue())
  63. {
  64. detail::gather_kernel<InputIterator, MapIterator, OutputIterator> kernel;
  65. kernel.set_range(first, last, input, result);
  66. kernel.exec(queue);
  67. }
  68. } // end compute namespace
  69. } // end boost namespace
  70. #endif // BOOST_COMPUTE_ALGORITHM_GATHER_HPP