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