1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
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
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
11 #ifndef BOOST_COMPUTE_ALGORITHM_DETAIL_SERIAL_FIND_EXTREMA_HPP
12 #define BOOST_COMPUTE_ALGORITHM_DETAIL_SERIAL_FIND_EXTREMA_HPP
14 #include <boost/compute/command_queue.hpp>
15 #include <boost/compute/types/fundamental.hpp>
16 #include <boost/compute/detail/meta_kernel.hpp>
17 #include <boost/compute/detail/iterator_range_size.hpp>
18 #include <boost/compute/container/detail/scalar.hpp>
24 template<class InputIterator, class Compare>
25 inline InputIterator serial_find_extrema(InputIterator first,
28 const bool find_minimum,
31 typedef typename std::iterator_traits<InputIterator>::value_type value_type;
32 typedef typename std::iterator_traits<InputIterator>::difference_type difference_type;
34 const context &context = queue.get_context();
36 meta_kernel k("serial_find_extrema");
39 k.decl<value_type>("value") << " = " << first[k.expr<uint_>("0")] << ";\n" <<
40 k.decl<uint_>("value_index") << " = 0;\n" <<
41 "for(uint i = 1; i < size; i++){\n" <<
42 " " << k.decl<value_type>("candidate") << "="
43 << first[k.expr<uint_>("i")] << ";\n" <<
45 "#ifndef BOOST_COMPUTE_FIND_MAXIMUM\n" <<
46 " if(" << compare(k.var<value_type>("candidate"),
47 k.var<value_type>("value")) << "){\n" <<
49 " if(" << compare(k.var<value_type>("value"),
50 k.var<value_type>("candidate")) << "){\n" <<
53 " value = candidate;\n" <<
54 " value_index = i;\n" <<
57 "*index = value_index;\n";
59 size_t index_arg_index = k.add_arg<uint_ *>(memory_object::global_memory, "index");
60 size_t size_arg_index = k.add_arg<uint_>("size");
64 options = "-DBOOST_COMPUTE_FIND_MAXIMUM";
66 kernel kernel = k.compile(context, options);
69 scalar<uint_> index(context);
70 kernel.set_arg(index_arg_index, index.get_buffer());
73 size_t count = iterator_range_size(first, last);
74 kernel.set_arg(size_arg_index, static_cast<uint_>(count));
77 queue.enqueue_task(kernel);
79 // read index and return iterator
80 return first + static_cast<difference_type>(index.read(queue));
83 } // end detail namespace
84 } // end compute namespace
85 } // end boost namespace
87 #endif // BOOST_COMPUTE_ALGORITHM_DETAIL_SERIAL_FIND_EXTREMA_HPP