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_GATHER_HPP
12 #define BOOST_COMPUTE_ALGORITHM_GATHER_HPP
14 #include <boost/compute/command_queue.hpp>
15 #include <boost/compute/detail/iterator_range_size.hpp>
16 #include <boost/compute/detail/meta_kernel.hpp>
17 #include <boost/compute/exception.hpp>
18 #include <boost/compute/iterator/buffer_iterator.hpp>
19 #include <boost/compute/system.hpp>
20 #include <boost/compute/type_traits/type_name.hpp>
26 template<class InputIterator, class MapIterator, class OutputIterator>
27 class gather_kernel : public meta_kernel
30 gather_kernel() : meta_kernel("gather")
33 void set_range(MapIterator first,
36 OutputIterator result)
38 m_count = iterator_range_size(first, last);
41 "const uint i = get_global_id(0);\n" <<
42 result[expr<uint_>("i")] << "=" <<
43 input[first[expr<uint_>("i")]] << ";\n";
46 event exec(command_queue &queue)
52 return exec_1d(queue, 0, m_count);
59 } // end detail namespace
61 /// Copies the elements using the indices from the range [\p first, \p last)
62 /// to the range beginning at \p result using the input values from the range
63 /// beginning at \p input.
66 template<class InputIterator, class MapIterator, class OutputIterator>
67 inline void gather(MapIterator first,
70 OutputIterator result,
71 command_queue &queue = system::default_queue())
73 detail::gather_kernel<InputIterator, MapIterator, OutputIterator> kernel;
75 kernel.set_range(first, last, input, result);
79 } // end compute namespace
80 } // end boost namespace
82 #endif // BOOST_COMPUTE_ALGORITHM_GATHER_HPP