reduce_on_cpu.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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_REDUCE_ON_CPU_HPP
  11. #define BOOST_COMPUTE_ALGORITHM_DETAIL_REDUCE_ON_CPU_HPP
  12. #include <algorithm>
  13. #include <boost/compute/buffer.hpp>
  14. #include <boost/compute/command_queue.hpp>
  15. #include <boost/compute/detail/meta_kernel.hpp>
  16. #include <boost/compute/detail/iterator_range_size.hpp>
  17. #include <boost/compute/detail/parameter_cache.hpp>
  18. #include <boost/compute/iterator/buffer_iterator.hpp>
  19. #include <boost/compute/type_traits/result_of.hpp>
  20. #include <boost/compute/algorithm/detail/serial_reduce.hpp>
  21. namespace boost {
  22. namespace compute {
  23. namespace detail {
  24. template<class InputIterator, class OutputIterator, class BinaryFunction>
  25. inline void reduce_on_cpu(InputIterator first,
  26. InputIterator last,
  27. OutputIterator result,
  28. BinaryFunction function,
  29. command_queue &queue)
  30. {
  31. typedef typename
  32. std::iterator_traits<InputIterator>::value_type T;
  33. typedef typename
  34. ::boost::compute::result_of<BinaryFunction(T, T)>::type result_type;
  35. const device &device = queue.get_device();
  36. const uint_ compute_units = queue.get_device().compute_units();
  37. boost::shared_ptr<parameter_cache> parameters =
  38. detail::parameter_cache::get_global_cache(device);
  39. std::string cache_key =
  40. "__boost_reduce_cpu_" + boost::lexical_cast<std::string>(sizeof(T));
  41. // for inputs smaller than serial_reduce_threshold
  42. // serial_reduce algorithm is used
  43. uint_ serial_reduce_threshold =
  44. parameters->get(cache_key, "serial_reduce_threshold", 16384 * sizeof(T));
  45. serial_reduce_threshold =
  46. (std::max)(serial_reduce_threshold, uint_(compute_units));
  47. const context &context = queue.get_context();
  48. size_t count = detail::iterator_range_size(first, last);
  49. if(count == 0){
  50. return;
  51. }
  52. else if(count < serial_reduce_threshold) {
  53. return serial_reduce(first, last, result, function, queue);
  54. }
  55. meta_kernel k("reduce_on_cpu");
  56. buffer output(context, sizeof(result_type) * compute_units);
  57. size_t count_arg = k.add_arg<uint_>("count");
  58. size_t output_arg =
  59. k.add_arg<result_type *>(memory_object::global_memory, "output");
  60. k <<
  61. "uint block = " <<
  62. "(uint)ceil(((float)count)/get_global_size(0));\n" <<
  63. "uint index = get_global_id(0) * block;\n" <<
  64. "uint end = min(count, index + block);\n" <<
  65. k.decl<result_type>("result") << " = " << first[k.var<uint_>("index")] << ";\n" <<
  66. "index++;\n" <<
  67. "while(index < end){\n" <<
  68. "result = " << function(k.var<T>("result"),
  69. first[k.var<uint_>("index")]) << ";\n" <<
  70. "index++;\n" <<
  71. "}\n" <<
  72. "output[get_global_id(0)] = result;\n";
  73. size_t global_work_size = compute_units;
  74. kernel kernel = k.compile(context);
  75. // reduction to global_work_size elements
  76. kernel.set_arg(count_arg, static_cast<uint_>(count));
  77. kernel.set_arg(output_arg, output);
  78. queue.enqueue_1d_range_kernel(kernel, 0, global_work_size, 0);
  79. // final reduction
  80. reduce_on_cpu(
  81. make_buffer_iterator<result_type>(output),
  82. make_buffer_iterator<result_type>(output, global_work_size),
  83. result,
  84. function,
  85. queue
  86. );
  87. }
  88. } // end detail namespace
  89. } // end compute namespace
  90. } // end boost namespace
  91. #endif // BOOST_COMPUTE_ALGORITHM_DETAIL_REDUCE_ON_CPU_HPP