Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:29:55

0001 //---------------------------------------------------------------------------//
0002 // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
0003 //
0004 // Distributed under the Boost Software License, Version 1.0
0005 // See accompanying file LICENSE_1_0.txt or copy at
0006 // http://www.boost.org/LICENSE_1_0.txt
0007 //
0008 // See http://boostorg.github.com/compute for more information.
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     // setup index buffer
0069     scalar<uint_> index(context);
0070     kernel.set_arg(index_arg_index, index.get_buffer());
0071 
0072     // setup count
0073     size_t count = iterator_range_size(first, last);
0074     kernel.set_arg(size_arg_index, static_cast<uint_>(count));
0075 
0076     // run kernel
0077     queue.enqueue_task(kernel);
0078 
0079     // read index and return iterator
0080     return first + static_cast<difference_type>(index.read(queue));
0081 }
0082 
0083 } // end detail namespace
0084 } // end compute namespace
0085 } // end boost namespace
0086 
0087 #endif // BOOST_COMPUTE_ALGORITHM_DETAIL_SERIAL_FIND_EXTREMA_HPP