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