find_extrema.hpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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_FIND_EXTREMA_HPP
  11. #define BOOST_COMPUTE_ALGORITHM_DETAIL_FIND_EXTREMA_HPP
  12. #include <boost/compute/detail/iterator_range_size.hpp>
  13. #include <boost/compute/algorithm/detail/find_extrema_on_cpu.hpp>
  14. #include <boost/compute/algorithm/detail/find_extrema_with_reduce.hpp>
  15. #include <boost/compute/algorithm/detail/find_extrema_with_atomics.hpp>
  16. #include <boost/compute/algorithm/detail/serial_find_extrema.hpp>
  17. namespace boost {
  18. namespace compute {
  19. namespace detail {
  20. template<class InputIterator, class Compare>
  21. inline InputIterator find_extrema(InputIterator first,
  22. InputIterator last,
  23. Compare compare,
  24. const bool find_minimum,
  25. command_queue &queue)
  26. {
  27. size_t count = iterator_range_size(first, last);
  28. // handle trivial cases
  29. if(count == 0 || count == 1){
  30. return first;
  31. }
  32. const device &device = queue.get_device();
  33. // CPU
  34. if(device.type() & device::cpu) {
  35. return find_extrema_on_cpu(first, last, compare, find_minimum, queue);
  36. }
  37. // GPU
  38. // use serial method for small inputs
  39. if(count < 512)
  40. {
  41. return serial_find_extrema(first, last, compare, find_minimum, queue);
  42. }
  43. // find_extrema_with_reduce() is used only if requirements are met
  44. if(find_extrema_with_reduce_requirements_met(first, last, queue))
  45. {
  46. return find_extrema_with_reduce(first, last, compare, find_minimum, queue);
  47. }
  48. // use serial method for OpenCL version 1.0 due to
  49. // problems with atomic_cmpxchg()
  50. #ifndef BOOST_COMPUTE_CL_VERSION_1_1
  51. return serial_find_extrema(first, last, compare, find_minimum, queue);
  52. #endif
  53. return find_extrema_with_atomics(first, last, compare, find_minimum, queue);
  54. }
  55. } // end detail namespace
  56. } // end compute namespace
  57. } // end boost namespace
  58. #endif // BOOST_COMPUTE_ALGORITHM_DETAIL_FIND_EXTREMA_HPP