File indexing completed on 2025-01-18 09:29:57
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_COMPUTE_ALGORITHM_SCATTER_IF_HPP
0012 #define BOOST_COMPUTE_ALGORITHM_SCATTER_IF_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 StencilIterator, class OutputIterator, class Predicate>
0031 class scatter_if_kernel : meta_kernel
0032 {
0033 public:
0034 scatter_if_kernel() : meta_kernel("scatter_if")
0035 {}
0036
0037 void set_range(InputIterator first,
0038 InputIterator last,
0039 MapIterator map,
0040 StencilIterator stencil,
0041 OutputIterator result,
0042 Predicate predicate)
0043 {
0044 m_count = iterator_range_size(first, last);
0045 m_input_offset = first.get_index();
0046 m_output_offset = result.get_index();
0047
0048 m_input_offset_arg = add_arg<uint_>("input_offset");
0049 m_output_offset_arg = add_arg<uint_>("output_offset");
0050
0051 *this <<
0052 "const uint i = get_global_id(0);\n" <<
0053 "uint i1 = " << map[expr<uint_>("i")] <<
0054 " + output_offset;\n" <<
0055 "uint i2 = i + input_offset;\n" <<
0056 if_(predicate(stencil[expr<uint_>("i")])) << "\n" <<
0057 result[expr<uint_>("i1")] << "=" <<
0058 first[expr<uint_>("i2")] << ";\n";
0059 }
0060
0061 event exec(command_queue &queue)
0062 {
0063 if(m_count == 0) {
0064 return event();
0065 }
0066
0067 set_arg(m_input_offset_arg, uint_(m_input_offset));
0068 set_arg(m_output_offset_arg, uint_(m_output_offset));
0069
0070 return exec_1d(queue, 0, m_count);
0071 }
0072
0073 private:
0074 size_t m_count;
0075 size_t m_input_offset;
0076 size_t m_input_offset_arg;
0077 size_t m_output_offset;
0078 size_t m_output_offset_arg;
0079 };
0080
0081 }
0082
0083
0084
0085
0086
0087
0088
0089 template<class InputIterator, class MapIterator, class StencilIterator, class OutputIterator,
0090 class Predicate>
0091 inline void scatter_if(InputIterator first,
0092 InputIterator last,
0093 MapIterator map,
0094 StencilIterator stencil,
0095 OutputIterator result,
0096 Predicate predicate,
0097 command_queue &queue = system::default_queue())
0098 {
0099 BOOST_STATIC_ASSERT(is_device_iterator<InputIterator>::value);
0100 BOOST_STATIC_ASSERT(is_device_iterator<MapIterator>::value);
0101 BOOST_STATIC_ASSERT(is_device_iterator<StencilIterator>::value);
0102 BOOST_STATIC_ASSERT(is_device_iterator<OutputIterator>::value);
0103
0104 detail::scatter_if_kernel<InputIterator, MapIterator, StencilIterator, OutputIterator, Predicate> kernel;
0105
0106 kernel.set_range(first, last, map, stencil, result, predicate);
0107 kernel.exec(queue);
0108 }
0109
0110 template<class InputIterator, class MapIterator, class StencilIterator, class OutputIterator>
0111 inline void scatter_if(InputIterator first,
0112 InputIterator last,
0113 MapIterator map,
0114 StencilIterator stencil,
0115 OutputIterator result,
0116 command_queue &queue = system::default_queue())
0117 {
0118 BOOST_STATIC_ASSERT(is_device_iterator<InputIterator>::value);
0119 BOOST_STATIC_ASSERT(is_device_iterator<MapIterator>::value);
0120 BOOST_STATIC_ASSERT(is_device_iterator<StencilIterator>::value);
0121 BOOST_STATIC_ASSERT(is_device_iterator<OutputIterator>::value);
0122
0123 typedef typename std::iterator_traits<StencilIterator>::value_type T;
0124
0125 scatter_if(first, last, map, stencil, result, identity<T>(), queue);
0126 }
0127
0128 }
0129 }
0130
0131 #endif