serial_find_extrema.hpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. #ifndef BOOST_COMPUTE_ALGORITHM_DETAIL_SERIAL_FIND_EXTREMA_HPP
  11. #define BOOST_COMPUTE_ALGORITHM_DETAIL_SERIAL_FIND_EXTREMA_HPP
  12. #include <boost/compute/command_queue.hpp>
  13. #include <boost/compute/types/fundamental.hpp>
  14. #include <boost/compute/detail/meta_kernel.hpp>
  15. #include <boost/compute/detail/iterator_range_size.hpp>
  16. #include <boost/compute/container/detail/scalar.hpp>
  17. namespace boost {
  18. namespace compute {
  19. namespace detail {
  20. template<class InputIterator, class Compare>
  21. inline InputIterator serial_find_extrema(InputIterator first,
  22. InputIterator last,
  23. Compare compare,
  24. const bool find_minimum,
  25. command_queue &queue)
  26. {
  27. typedef typename std::iterator_traits<InputIterator>::value_type value_type;
  28. typedef typename std::iterator_traits<InputIterator>::difference_type difference_type;
  29. const context &context = queue.get_context();
  30. meta_kernel k("serial_find_extrema");
  31. k <<
  32. k.decl<value_type>("value") << " = " << first[k.expr<uint_>("0")] << ";\n" <<
  33. k.decl<uint_>("value_index") << " = 0;\n" <<
  34. "for(uint i = 1; i < size; i++){\n" <<
  35. " " << k.decl<value_type>("candidate") << "="
  36. << first[k.expr<uint_>("i")] << ";\n" <<
  37. "#ifndef BOOST_COMPUTE_FIND_MAXIMUM\n" <<
  38. " if(" << compare(k.var<value_type>("candidate"),
  39. k.var<value_type>("value")) << "){\n" <<
  40. "#else\n" <<
  41. " if(" << compare(k.var<value_type>("value"),
  42. k.var<value_type>("candidate")) << "){\n" <<
  43. "#endif\n" <<
  44. " value = candidate;\n" <<
  45. " value_index = i;\n" <<
  46. " }\n" <<
  47. "}\n" <<
  48. "*index = value_index;\n";
  49. size_t index_arg_index = k.add_arg<uint_ *>(memory_object::global_memory, "index");
  50. size_t size_arg_index = k.add_arg<uint_>("size");
  51. std::string options;
  52. if(!find_minimum){
  53. options = "-DBOOST_COMPUTE_FIND_MAXIMUM";
  54. }
  55. kernel kernel = k.compile(context, options);
  56. // setup index buffer
  57. scalar<uint_> index(context);
  58. kernel.set_arg(index_arg_index, index.get_buffer());
  59. // setup count
  60. size_t count = iterator_range_size(first, last);
  61. kernel.set_arg(size_arg_index, static_cast<uint_>(count));
  62. // run kernel
  63. queue.enqueue_task(kernel);
  64. // read index and return iterator
  65. return first + static_cast<difference_type>(index.read(queue));
  66. }
  67. } // end detail namespace
  68. } // end compute namespace
  69. } // end boost namespace
  70. #endif // BOOST_COMPUTE_ALGORITHM_DETAIL_SERIAL_FIND_EXTREMA_HPP