inclusive_scan.hpp 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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_INCLUSIVE_SCAN_HPP
  11. #define BOOST_COMPUTE_ALGORITHM_INCLUSIVE_SCAN_HPP
  12. #include <boost/static_assert.hpp>
  13. #include <boost/compute/functional.hpp>
  14. #include <boost/compute/system.hpp>
  15. #include <boost/compute/command_queue.hpp>
  16. #include <boost/compute/algorithm/detail/scan.hpp>
  17. #include <boost/compute/type_traits/is_device_iterator.hpp>
  18. namespace boost {
  19. namespace compute {
  20. /// Performs an inclusive scan of the elements in the range [\p first, \p last)
  21. /// and stores the results in the range beginning at \p result.
  22. ///
  23. /// Each element in the output is assigned to the sum of the current value in
  24. /// the input with the sum of every previous value in the input.
  25. ///
  26. /// \param first first element in the range to scan
  27. /// \param last last element in the range to scan
  28. /// \param result first element in the result range
  29. /// \param binary_op associative binary operator
  30. /// \param queue command queue to perform the operation
  31. ///
  32. /// \return \c OutputIterator to the end of the result range
  33. ///
  34. /// The default operation is to add the elements up.
  35. ///
  36. /// \snippet test/test_scan.cpp inclusive_scan_int
  37. ///
  38. /// But different associative operation can be specified as \p binary_op
  39. /// instead (e.g., multiplication, maximum, minimum).
  40. ///
  41. /// \snippet test/test_scan.cpp inclusive_scan_int_multiplies
  42. ///
  43. /// Space complexity on GPUs: \Omega(n)<br>
  44. /// Space complexity on GPUs when \p first == \p result: \Omega(2n)<br>
  45. /// Space complexity on CPUs: \Omega(1)
  46. ///
  47. /// \see exclusive_scan()
  48. template<class InputIterator, class OutputIterator, class BinaryOperator>
  49. inline OutputIterator
  50. inclusive_scan(InputIterator first,
  51. InputIterator last,
  52. OutputIterator result,
  53. BinaryOperator binary_op,
  54. command_queue &queue = system::default_queue())
  55. {
  56. BOOST_STATIC_ASSERT(is_device_iterator<InputIterator>::value);
  57. BOOST_STATIC_ASSERT(is_device_iterator<OutputIterator>::value);
  58. typedef typename
  59. std::iterator_traits<OutputIterator>::value_type output_type;
  60. return detail::scan(first, last, result, false,
  61. output_type(0), binary_op,
  62. queue);
  63. }
  64. /// \overload
  65. template<class InputIterator, class OutputIterator>
  66. inline OutputIterator
  67. inclusive_scan(InputIterator first,
  68. InputIterator last,
  69. OutputIterator result,
  70. command_queue &queue = system::default_queue())
  71. {
  72. BOOST_STATIC_ASSERT(is_device_iterator<InputIterator>::value);
  73. BOOST_STATIC_ASSERT(is_device_iterator<OutputIterator>::value);
  74. typedef typename
  75. std::iterator_traits<OutputIterator>::value_type output_type;
  76. return detail::scan(first, last, result, false,
  77. output_type(0), boost::compute::plus<output_type>(),
  78. queue);
  79. }
  80. } // end compute namespace
  81. } // end boost namespace
  82. #endif // BOOST_COMPUTE_ALGORITHM_INCLUSIVE_SCAN_HPP