File indexing completed on 2025-01-18 09:29:55
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_COMPUTE_ALGORITHM_DETAIL_SERIAL_FIND_EXTREMA_HPP
0012 #define BOOST_COMPUTE_ALGORITHM_DETAIL_SERIAL_FIND_EXTREMA_HPP
0013
0014 #include <boost/compute/command_queue.hpp>
0015 #include <boost/compute/types/fundamental.hpp>
0016 #include <boost/compute/detail/meta_kernel.hpp>
0017 #include <boost/compute/detail/iterator_range_size.hpp>
0018 #include <boost/compute/container/detail/scalar.hpp>
0019
0020 namespace boost {
0021 namespace compute {
0022 namespace detail {
0023
0024 template<class InputIterator, class Compare>
0025 inline InputIterator serial_find_extrema(InputIterator first,
0026 InputIterator last,
0027 Compare compare,
0028 const bool find_minimum,
0029 command_queue &queue)
0030 {
0031 typedef typename std::iterator_traits<InputIterator>::value_type value_type;
0032 typedef typename std::iterator_traits<InputIterator>::difference_type difference_type;
0033
0034 const context &context = queue.get_context();
0035
0036 meta_kernel k("serial_find_extrema");
0037
0038 k <<
0039 k.decl<value_type>("value") << " = " << first[k.expr<uint_>("0")] << ";\n" <<
0040 k.decl<uint_>("value_index") << " = 0;\n" <<
0041 "for(uint i = 1; i < size; i++){\n" <<
0042 " " << k.decl<value_type>("candidate") << "="
0043 << first[k.expr<uint_>("i")] << ";\n" <<
0044
0045 "#ifndef BOOST_COMPUTE_FIND_MAXIMUM\n" <<
0046 " if(" << compare(k.var<value_type>("candidate"),
0047 k.var<value_type>("value")) << "){\n" <<
0048 "#else\n" <<
0049 " if(" << compare(k.var<value_type>("value"),
0050 k.var<value_type>("candidate")) << "){\n" <<
0051 "#endif\n" <<
0052
0053 " value = candidate;\n" <<
0054 " value_index = i;\n" <<
0055 " }\n" <<
0056 "}\n" <<
0057 "*index = value_index;\n";
0058
0059 size_t index_arg_index = k.add_arg<uint_ *>(memory_object::global_memory, "index");
0060 size_t size_arg_index = k.add_arg<uint_>("size");
0061
0062 std::string options;
0063 if(!find_minimum){
0064 options = "-DBOOST_COMPUTE_FIND_MAXIMUM";
0065 }
0066 kernel kernel = k.compile(context, options);
0067
0068
0069 scalar<uint_> index(context);
0070 kernel.set_arg(index_arg_index, index.get_buffer());
0071
0072
0073 size_t count = iterator_range_size(first, last);
0074 kernel.set_arg(size_arg_index, static_cast<uint_>(count));
0075
0076
0077 queue.enqueue_task(kernel);
0078
0079
0080 return first + static_cast<difference_type>(index.read(queue));
0081 }
0082
0083 }
0084 }
0085 }
0086
0087 #endif