find_extrema_on_cpu.hpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. //---------------------------------------------------------------------------//
  2. // Copyright (c) 2016 Jakub Szuppe <j.szuppe@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_FIND_EXTREMA_ON_CPU_HPP
  11. #define BOOST_COMPUTE_ALGORITHM_DETAIL_FIND_EXTREMA_ON_CPU_HPP
  12. #include <algorithm>
  13. #include <boost/compute/algorithm/detail/find_extrema_with_reduce.hpp>
  14. #include <boost/compute/algorithm/detail/find_extrema_with_atomics.hpp>
  15. #include <boost/compute/algorithm/detail/serial_find_extrema.hpp>
  16. #include <boost/compute/detail/iterator_range_size.hpp>
  17. #include <boost/compute/iterator/buffer_iterator.hpp>
  18. namespace boost {
  19. namespace compute {
  20. namespace detail {
  21. template<class InputIterator, class Compare>
  22. inline InputIterator find_extrema_on_cpu(InputIterator first,
  23. InputIterator last,
  24. Compare compare,
  25. const bool find_minimum,
  26. command_queue &queue)
  27. {
  28. typedef typename std::iterator_traits<InputIterator>::value_type input_type;
  29. typedef typename std::iterator_traits<InputIterator>::difference_type difference_type;
  30. size_t count = iterator_range_size(first, last);
  31. const device &device = queue.get_device();
  32. const uint_ compute_units = queue.get_device().compute_units();
  33. boost::shared_ptr<parameter_cache> parameters =
  34. detail::parameter_cache::get_global_cache(device);
  35. std::string cache_key =
  36. "__boost_find_extrema_cpu_"
  37. + boost::lexical_cast<std::string>(sizeof(input_type));
  38. // for inputs smaller than serial_find_extrema_threshold
  39. // serial_find_extrema algorithm is used
  40. uint_ serial_find_extrema_threshold = parameters->get(
  41. cache_key,
  42. "serial_find_extrema_threshold",
  43. 16384 * sizeof(input_type)
  44. );
  45. serial_find_extrema_threshold =
  46. (std::max)(serial_find_extrema_threshold, uint_(2 * compute_units));
  47. const context &context = queue.get_context();
  48. if(count < serial_find_extrema_threshold) {
  49. return serial_find_extrema(first, last, compare, find_minimum, queue);
  50. }
  51. meta_kernel k("find_extrema_on_cpu");
  52. buffer output(context, sizeof(input_type) * compute_units);
  53. buffer output_idx(
  54. context, sizeof(uint_) * compute_units,
  55. buffer::read_write | buffer::alloc_host_ptr
  56. );
  57. size_t count_arg = k.add_arg<uint_>("count");
  58. size_t output_arg =
  59. k.add_arg<input_type *>(memory_object::global_memory, "output");
  60. size_t output_idx_arg =
  61. k.add_arg<uint_ *>(memory_object::global_memory, "output_idx");
  62. k <<
  63. "uint block = " <<
  64. "(uint)ceil(((float)count)/get_global_size(0));\n" <<
  65. "uint index = get_global_id(0) * block;\n" <<
  66. "uint end = min(count, index + block);\n" <<
  67. "uint value_index = index;\n" <<
  68. k.decl<input_type>("value") << " = " << first[k.var<uint_>("index")] << ";\n" <<
  69. "index++;\n" <<
  70. "while(index < end){\n" <<
  71. k.decl<input_type>("candidate") <<
  72. " = " << first[k.var<uint_>("index")] << ";\n" <<
  73. "#ifndef BOOST_COMPUTE_FIND_MAXIMUM\n" <<
  74. "bool compare = " << compare(k.var<input_type>("candidate"),
  75. k.var<input_type>("value")) << ";\n" <<
  76. "#else\n" <<
  77. "bool compare = " << compare(k.var<input_type>("value"),
  78. k.var<input_type>("candidate")) << ";\n" <<
  79. "#endif\n" <<
  80. "value = compare ? candidate : value;\n" <<
  81. "value_index = compare ? index : value_index;\n" <<
  82. "index++;\n" <<
  83. "}\n" <<
  84. "output[get_global_id(0)] = value;\n" <<
  85. "output_idx[get_global_id(0)] = value_index;\n";
  86. size_t global_work_size = compute_units;
  87. std::string options;
  88. if(!find_minimum){
  89. options = "-DBOOST_COMPUTE_FIND_MAXIMUM";
  90. }
  91. kernel kernel = k.compile(context, options);
  92. kernel.set_arg(count_arg, static_cast<uint_>(count));
  93. kernel.set_arg(output_arg, output);
  94. kernel.set_arg(output_idx_arg, output_idx);
  95. queue.enqueue_1d_range_kernel(kernel, 0, global_work_size, 0);
  96. buffer_iterator<input_type> result = serial_find_extrema(
  97. make_buffer_iterator<input_type>(output),
  98. make_buffer_iterator<input_type>(output, global_work_size),
  99. compare,
  100. find_minimum,
  101. queue
  102. );
  103. uint_* output_idx_host_ptr =
  104. static_cast<uint_*>(
  105. queue.enqueue_map_buffer(
  106. output_idx, command_queue::map_read,
  107. 0, global_work_size * sizeof(uint_)
  108. )
  109. );
  110. difference_type extremum_idx =
  111. static_cast<difference_type>(*(output_idx_host_ptr + result.get_index()));
  112. return first + extremum_idx;
  113. }
  114. } // end detail namespace
  115. } // end compute namespace
  116. } // end boost namespace
  117. #endif // BOOST_COMPUTE_ALGORITHM_DETAIL_FIND_EXTREMA_ON_CPU_HPP