Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:29:57

0001 //---------------------------------------------------------------------------//
0002 // Copyright (c) 2013 Kyle Lutz <kyle.r.lutz@gmail.com>
0003 //
0004 // Distributed under the Boost Software License, Version 1.0
0005 // See accompanying file LICENSE_1_0.txt or copy at
0006 // http://www.boost.org/LICENSE_1_0.txt
0007 //
0008 // See http://boostorg.github.com/compute for more information.
0009 //---------------------------------------------------------------------------//
0010 
0011 #ifndef BOOST_COMPUTE_ALGORITHM_SCATTER_HPP
0012 #define BOOST_COMPUTE_ALGORITHM_SCATTER_HPP
0013 
0014 #include <boost/static_assert.hpp>
0015 #include <boost/algorithm/string/replace.hpp>
0016 
0017 #include <boost/compute/system.hpp>
0018 #include <boost/compute/exception.hpp>
0019 #include <boost/compute/command_queue.hpp>
0020 #include <boost/compute/iterator/buffer_iterator.hpp>
0021 #include <boost/compute/type_traits/type_name.hpp>
0022 #include <boost/compute/detail/iterator_range_size.hpp>
0023 #include <boost/compute/detail/meta_kernel.hpp>
0024 #include <boost/compute/type_traits/is_device_iterator.hpp>
0025 
0026 namespace boost {
0027 namespace compute {
0028 namespace detail {
0029 
0030 template<class InputIterator, class MapIterator, class OutputIterator>
0031 class scatter_kernel : meta_kernel
0032 {
0033 public:
0034     scatter_kernel() : meta_kernel("scatter")
0035     {}
0036 
0037     void set_range(InputIterator first,
0038                    InputIterator last,
0039                    MapIterator map,
0040                    OutputIterator result)
0041     {
0042         m_count = iterator_range_size(first, last);
0043         m_input_offset = first.get_index();
0044         m_output_offset = result.get_index();
0045 
0046         m_input_offset_arg = add_arg<uint_>("input_offset");
0047         m_output_offset_arg = add_arg<uint_>("output_offset");
0048 
0049         *this <<
0050             "const uint i = get_global_id(0);\n" <<
0051             "uint i1 = " << map[expr<uint_>("i")] << 
0052                 " + output_offset;\n" <<
0053             "uint i2 = i + input_offset;\n" <<
0054             result[expr<uint_>("i1")] << "=" << 
0055                 first[expr<uint_>("i2")] << ";\n";
0056     }
0057 
0058     event exec(command_queue &queue)
0059     {
0060         if(m_count == 0) {
0061             return event();
0062         }
0063 
0064         set_arg(m_input_offset_arg, uint_(m_input_offset));
0065         set_arg(m_output_offset_arg, uint_(m_output_offset));
0066 
0067         return exec_1d(queue, 0, m_count);
0068     }
0069 
0070 private:
0071     size_t m_count;
0072     size_t m_input_offset;    
0073     size_t m_input_offset_arg;    
0074     size_t m_output_offset;    
0075     size_t m_output_offset_arg;    
0076 };
0077 
0078 } // end detail namespace
0079 
0080 /// Copies the elements from the range [\p first, \p last) to the range
0081 /// beginning at \p result using the output indices from the range beginning
0082 /// at \p map.
0083 ///
0084 /// Space complexity: \Omega(1)
0085 ///
0086 /// \see gather()
0087 template<class InputIterator, class MapIterator, class OutputIterator>
0088 inline void scatter(InputIterator first,
0089                     InputIterator last,
0090                     MapIterator map,
0091                     OutputIterator result,
0092                     command_queue &queue = system::default_queue())
0093 {
0094     BOOST_STATIC_ASSERT(is_device_iterator<InputIterator>::value);
0095     BOOST_STATIC_ASSERT(is_device_iterator<MapIterator>::value);
0096     BOOST_STATIC_ASSERT(is_device_iterator<OutputIterator>::value);
0097 
0098     detail::scatter_kernel<InputIterator, MapIterator, OutputIterator> kernel;
0099     
0100     kernel.set_range(first, last, map, result);
0101     kernel.exec(queue);
0102 }
0103 
0104 } // end compute namespace
0105 } // end boost namespace
0106 
0107 #endif // BOOST_COMPUTE_ALGORITHM_SCATTER_HPP