1 //---------------------------------------------------------------------------//
2 // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
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
8 // See http://boostorg.github.com/compute for more information.
9 //---------------------------------------------------------------------------//
11 #ifndef BOOST_COMPUTE_ALGORITHM_RANDOM_SHUFFLE_HPP
12 #define BOOST_COMPUTE_ALGORITHM_RANDOM_SHUFFLE_HPP
17 #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>
29 /// Randomly shuffles the elements in the range [\p first, \p last).
32 template<class Iterator>
33 inline void random_shuffle(Iterator first,
35 command_queue &queue = system::default_queue())
37 typedef typename std::iterator_traits<Iterator>::value_type value_type;
39 size_t count = detail::iterator_range_size(first, last);
44 // generate shuffled indices on the host
45 std::vector<cl_uint> random_indices(count);
46 boost::iota(random_indices, 0);
47 std::random_shuffle(random_indices.begin(), random_indices.end());
49 // copy random indices to the device
50 const context &context = queue.get_context();
51 vector<cl_uint> indices(count, context);
52 ::boost::compute::copy(random_indices.begin(),
57 // make a copy of the values on the device
58 vector<value_type> tmp(count, context);
59 ::boost::compute::copy(first,
64 // write values to their new locations
65 ::boost::compute::scatter(tmp.begin(),
72 } // end compute namespace
73 } // end boost namespace
75 #endif // BOOST_COMPUTE_ALGORITHM_RANDOM_SHUFFLE_HPP