random_shuffle.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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_RANDOM_SHUFFLE_HPP
  11. #define BOOST_COMPUTE_ALGORITHM_RANDOM_SHUFFLE_HPP
  12. #include <vector>
  13. #include <algorithm>
  14. #ifdef BOOST_COMPUTE_USE_CPP11
  15. #include <random>
  16. #endif
  17. #include <boost/static_assert.hpp>
  18. #include <boost/range/algorithm_ext/iota.hpp>
  19. #include <boost/compute/system.hpp>
  20. #include <boost/compute/functional.hpp>
  21. #include <boost/compute/command_queue.hpp>
  22. #include <boost/compute/container/vector.hpp>
  23. #include <boost/compute/algorithm/scatter.hpp>
  24. #include <boost/compute/detail/iterator_range_size.hpp>
  25. #include <boost/compute/type_traits/is_device_iterator.hpp>
  26. namespace boost {
  27. namespace compute {
  28. /// Randomly shuffles the elements in the range [\p first, \p last).
  29. ///
  30. /// Space complexity: \Omega(2n)
  31. ///
  32. /// \see scatter()
  33. template<class Iterator>
  34. inline void random_shuffle(Iterator first,
  35. Iterator last,
  36. command_queue &queue = system::default_queue())
  37. {
  38. BOOST_STATIC_ASSERT(is_device_iterator<Iterator>::value);
  39. typedef typename std::iterator_traits<Iterator>::value_type value_type;
  40. size_t count = detail::iterator_range_size(first, last);
  41. if(count == 0){
  42. return;
  43. }
  44. // generate shuffled indices on the host
  45. std::vector<cl_uint> random_indices(count);
  46. boost::iota(random_indices, 0);
  47. #ifdef BOOST_COMPUTE_USE_CPP11
  48. std::random_device nondeterministic_randomness;
  49. std::default_random_engine random_engine(nondeterministic_randomness());
  50. std::shuffle(random_indices.begin(), random_indices.end(), random_engine);
  51. #else
  52. std::random_shuffle(random_indices.begin(), random_indices.end());
  53. #endif
  54. // copy random indices to the device
  55. const context &context = queue.get_context();
  56. vector<cl_uint> indices(count, context);
  57. ::boost::compute::copy(random_indices.begin(),
  58. random_indices.end(),
  59. indices.begin(),
  60. queue);
  61. // make a copy of the values on the device
  62. vector<value_type> tmp(count, context);
  63. ::boost::compute::copy(first,
  64. last,
  65. tmp.begin(),
  66. queue);
  67. // write values to their new locations
  68. ::boost::compute::scatter(tmp.begin(),
  69. tmp.end(),
  70. indices.begin(),
  71. first,
  72. queue);
  73. }
  74. } // end compute namespace
  75. } // end boost namespace
  76. #endif // BOOST_COMPUTE_ALGORITHM_RANDOM_SHUFFLE_HPP