min_element.hpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2013 Kyle Lutz <[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_MIN_ELEMENT_HPP
  11. #define BOOST_COMPUTE_ALGORITHM_MIN_ELEMENT_HPP
  12. #include <boost/static_assert.hpp>
  13. #include <boost/compute/system.hpp>
  14. #include <boost/compute/command_queue.hpp>
  15. #include <boost/compute/functional.hpp>
  16. #include <boost/compute/algorithm/detail/find_extrema.hpp>
  17. #include <boost/compute/type_traits/is_device_iterator.hpp>
  18. namespace boost {
  19. namespace compute {
  20. /// Returns an iterator pointing to the element in range
  21. /// [\p first, \p last) with the minimum value.
  22. ///
  23. /// \param first first element in the input range
  24. /// \param last last element in the input range
  25. /// \param compare comparison function object which returns true if the first
  26. /// argument is less than (i.e. is ordered before) the second.
  27. /// \param queue command queue to perform the operation
  28. ///
  29. /// For example, to find \c int2 value with minimum first component in given vector:
  30. /// \code
  31. /// // comparison function object
  32. /// BOOST_COMPUTE_FUNCTION(bool, compare_first, (const int2_ &a, const int2_ &b),
  33. /// {
  34. /// return a.x < b.x;
  35. /// });
  36. ///
  37. /// // create vector
  38. /// boost::compute::vector<uint2_> data = ...
  39. ///
  40. /// boost::compute::vector<uint2_>::iterator min =
  41. /// boost::compute::min_element(data.begin(), data.end(), compare_first, queue);
  42. /// \endcode
  43. ///
  44. /// Space complexity on CPUs: \Omega(1)<br>
  45. /// Space complexity on GPUs: \Omega(N)
  46. ///
  47. /// \see max_element()
  48. template<class InputIterator, class Compare>
  49. inline InputIterator
  50. min_element(InputIterator first,
  51. InputIterator last,
  52. Compare compare,
  53. command_queue &queue = system::default_queue())
  54. {
  55. BOOST_STATIC_ASSERT(is_device_iterator<InputIterator>::value);
  56. return detail::find_extrema(first, last, compare, true, queue);
  57. }
  58. ///\overload
  59. template<class InputIterator>
  60. inline InputIterator
  61. min_element(InputIterator first,
  62. InputIterator last,
  63. command_queue &queue = system::default_queue())
  64. {
  65. BOOST_STATIC_ASSERT(is_device_iterator<InputIterator>::value);
  66. typedef typename std::iterator_traits<InputIterator>::value_type value_type;
  67. return ::boost::compute::min_element(
  68. first, last, ::boost::compute::less<value_type>(), queue
  69. );
  70. }
  71. } // end compute namespace
  72. } // end boost namespace
  73. #endif // BOOST_COMPUTE_ALGORITHM_MIN_ELEMENT_HPP