]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/boost/compute/algorithm/random_shuffle.hpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / boost / compute / algorithm / random_shuffle.hpp
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 #include <boost/range/algorithm_ext/iota.hpp>
18
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
26 namespace boost {
27 namespace compute {
28
29 /// Randomly shuffles the elements in the range [\p first, \p last).
30 ///
31 /// Space complexity: \Omega(2n)
32 ///
33 /// \see scatter()
34 template<class Iterator>
35 inline void random_shuffle(Iterator first,
36 Iterator last,
37 command_queue &queue = system::default_queue())
38 {
39 typedef typename std::iterator_traits<Iterator>::value_type value_type;
40
41 size_t count = detail::iterator_range_size(first, last);
42 if(count == 0){
43 return;
44 }
45
46 // generate shuffled indices on the host
47 std::vector<cl_uint> random_indices(count);
48 boost::iota(random_indices, 0);
49 std::random_shuffle(random_indices.begin(), random_indices.end());
50
51 // copy random indices to the device
52 const context &context = queue.get_context();
53 vector<cl_uint> indices(count, context);
54 ::boost::compute::copy(random_indices.begin(),
55 random_indices.end(),
56 indices.begin(),
57 queue);
58
59 // make a copy of the values on the device
60 vector<value_type> tmp(count, context);
61 ::boost::compute::copy(first,
62 last,
63 tmp.begin(),
64 queue);
65
66 // write values to their new locations
67 ::boost::compute::scatter(tmp.begin(),
68 tmp.end(),
69 indices.begin(),
70 first,
71 queue);
72 }
73
74 } // end compute namespace
75 } // end boost namespace
76
77 #endif // BOOST_COMPUTE_ALGORITHM_RANDOM_SHUFFLE_HPP