//---------------------------------------------------------------------------// // Copyright (c) 2013 Kyle Lutz // // Distributed under the Boost Software License, Version 1.0 // See accompanying file LICENSE_1_0.txt or copy at // http://www.boost.org/LICENSE_1_0.txt // // See http://boostorg.github.com/compute for more information. //---------------------------------------------------------------------------// #ifndef BOOST_COMPUTE_ALGORITHM_DETAIL_SERIAL_FIND_EXTREMA_HPP #define BOOST_COMPUTE_ALGORITHM_DETAIL_SERIAL_FIND_EXTREMA_HPP #include #include #include #include #include namespace boost { namespace compute { namespace detail { template inline InputIterator serial_find_extrema(InputIterator first, InputIterator last, Compare compare, const bool find_minimum, command_queue &queue) { typedef typename std::iterator_traits::value_type value_type; typedef typename std::iterator_traits::difference_type difference_type; const context &context = queue.get_context(); meta_kernel k("serial_find_extrema"); k << k.decl("value") << " = " << first[k.expr("0")] << ";\n" << k.decl("value_index") << " = 0;\n" << "for(uint i = 1; i < size; i++){\n" << " " << k.decl("candidate") << "=" << first[k.expr("i")] << ";\n" << "#ifndef BOOST_COMPUTE_FIND_MAXIMUM\n" << " if(" << compare(k.var("candidate"), k.var("value")) << "){\n" << "#else\n" << " if(" << compare(k.var("value"), k.var("candidate")) << "){\n" << "#endif\n" << " value = candidate;\n" << " value_index = i;\n" << " }\n" << "}\n" << "*index = value_index;\n"; size_t index_arg_index = k.add_arg(memory_object::global_memory, "index"); size_t size_arg_index = k.add_arg("size"); std::string options; if(!find_minimum){ options = "-DBOOST_COMPUTE_FIND_MAXIMUM"; } kernel kernel = k.compile(context, options); // setup index buffer scalar index(context); kernel.set_arg(index_arg_index, index.get_buffer()); // setup count size_t count = iterator_range_size(first, last); kernel.set_arg(size_arg_index, static_cast(count)); // run kernel queue.enqueue_task(kernel); // read index and return iterator return first + static_cast(index.read(queue)); } } // end detail namespace } // end compute namespace } // end boost namespace #endif // BOOST_COMPUTE_ALGORITHM_DETAIL_SERIAL_FIND_EXTREMA_HPP