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_ACCUMULATE_HPP
12 #define BOOST_COMPUTE_ALGORITHM_ACCUMULATE_HPP
14 #include <boost/preprocessor/seq/for_each.hpp>
16 #include <boost/compute/system.hpp>
17 #include <boost/compute/functional.hpp>
18 #include <boost/compute/command_queue.hpp>
19 #include <boost/compute/algorithm/reduce.hpp>
20 #include <boost/compute/algorithm/detail/serial_accumulate.hpp>
21 #include <boost/compute/container/array.hpp>
22 #include <boost/compute/container/vector.hpp>
23 #include <boost/compute/detail/iterator_range_size.hpp>
29 template<class InputIterator, class T, class BinaryFunction>
30 inline T generic_accumulate(InputIterator first,
33 BinaryFunction function,
36 const context &context = queue.get_context();
38 size_t size = iterator_range_size(first, last);
43 // accumulate on device
44 array<T, 1> device_result(context);
45 detail::serial_accumulate(
46 first, last, device_result.begin(), init, function, queue
49 // copy result to host
51 ::boost::compute::copy_n(device_result.begin(), 1, &result, queue);
55 // returns true if we can use reduce() instead of accumulate() when
56 // accumulate() this is true when the function is commutative (such as
57 // addition of integers) and the initial value is the identity value
58 // for the operation (zero for addition, one for multiplication).
59 template<class T, class F>
60 inline bool can_accumulate_with_reduce(T init, F function)
69 #define BOOST_COMPUTE_DETAIL_DECLARE_CAN_ACCUMULATE_WITH_REDUCE(r, data, type) \
70 inline bool can_accumulate_with_reduce(type init, plus<type>) \
72 return init == type(0); \
74 inline bool can_accumulate_with_reduce(type init, multiplies<type>) \
76 return init == type(1); \
79 BOOST_PP_SEQ_FOR_EACH(
80 BOOST_COMPUTE_DETAIL_DECLARE_CAN_ACCUMULATE_WITH_REDUCE,
82 (char_)(uchar_)(short_)(ushort_)(int_)(uint_)(long_)(ulong_)
86 inline bool can_accumulate_with_reduce(T init, min<T>)
88 return init == (std::numeric_limits<T>::max)();
92 inline bool can_accumulate_with_reduce(T init, max<T>)
94 return init == (std::numeric_limits<T>::min)();
97 #undef BOOST_COMPUTE_DETAIL_DECLARE_CAN_ACCUMULATE_WITH_REDUCE
99 template<class InputIterator, class T, class BinaryFunction>
100 inline T dispatch_accumulate(InputIterator first,
103 BinaryFunction function,
104 command_queue &queue)
106 size_t size = iterator_range_size(first, last);
111 if(can_accumulate_with_reduce(init, function)){
113 reduce(first, last, &result, function, queue);
117 return generic_accumulate(first, last, init, function, queue);
121 } // end detail namespace
123 /// Returns the result of applying \p function to the elements in the
124 /// range [\p first, \p last) and \p init.
126 /// If no function is specified, \c plus will be used.
128 /// \param first first element in the input range
129 /// \param last last element in the input range
130 /// \param init initial value
131 /// \param function binary reduction function
132 /// \param queue command queue to perform the operation
134 /// \return the accumulated result value
136 /// In specific situations the call to \c accumulate() can be automatically
137 /// optimized to a call to the more efficient \c reduce() algorithm. This
138 /// occurs when the binary reduction function is recognized as associative
139 /// (such as the \c plus<int> function).
141 /// Note that because floating-point addition is not associative, calling
142 /// \c accumulate() with \c plus<float> results in a less efficient serial
143 /// reduction algorithm being executed. If a slight loss in precision is
144 /// acceptable, the more efficient parallel \c reduce() algorithm should be
149 /// // with vec = boost::compute::vector<int>
150 /// accumulate(vec.begin(), vec.end(), 0, plus<int>()); // fast
151 /// reduce(vec.begin(), vec.end(), &result, plus<int>()); // fast
153 /// // with vec = boost::compute::vector<float>
154 /// accumulate(vec.begin(), vec.end(), 0, plus<float>()); // slow
155 /// reduce(vec.begin(), vec.end(), &result, plus<float>()); // fast
159 template<class InputIterator, class T, class BinaryFunction>
160 inline T accumulate(InputIterator first,
163 BinaryFunction function,
164 command_queue &queue = system::default_queue())
166 return detail::dispatch_accumulate(first, last, init, function, queue);
170 template<class InputIterator, class T>
171 inline T accumulate(InputIterator first,
174 command_queue &queue = system::default_queue())
176 typedef typename std::iterator_traits<InputIterator>::value_type IT;
178 return detail::dispatch_accumulate(first, last, init, plus<IT>(), queue);
181 } // end compute namespace
182 } // end boost namespace
184 #endif // BOOST_COMPUTE_ALGORITHM_ACCUMULATE_HPP