123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- #ifndef BOOST_COMPUTE_ALGORITHM_ACCUMULATE_HPP
- #define BOOST_COMPUTE_ALGORITHM_ACCUMULATE_HPP
- #include <boost/static_assert.hpp>
- #include <boost/preprocessor/seq/for_each.hpp>
- #include <boost/compute/system.hpp>
- #include <boost/compute/functional.hpp>
- #include <boost/compute/command_queue.hpp>
- #include <boost/compute/algorithm/reduce.hpp>
- #include <boost/compute/algorithm/detail/serial_accumulate.hpp>
- #include <boost/compute/container/array.hpp>
- #include <boost/compute/container/vector.hpp>
- #include <boost/compute/type_traits/is_device_iterator.hpp>
- #include <boost/compute/detail/iterator_range_size.hpp>
- namespace boost {
- namespace compute {
- namespace detail {
- template<class InputIterator, class T, class BinaryFunction>
- inline T generic_accumulate(InputIterator first,
- InputIterator last,
- T init,
- BinaryFunction function,
- command_queue &queue)
- {
- const context &context = queue.get_context();
- size_t size = iterator_range_size(first, last);
- if(size == 0){
- return init;
- }
-
- array<T, 1> device_result(context);
- detail::serial_accumulate(
- first, last, device_result.begin(), init, function, queue
- );
-
- T result;
- ::boost::compute::copy_n(device_result.begin(), 1, &result, queue);
- return result;
- }
- template<class T, class F>
- inline bool can_accumulate_with_reduce(T init, F function)
- {
- (void) init;
- (void) function;
- return false;
- }
- #define BOOST_COMPUTE_DETAIL_DECLARE_CAN_ACCUMULATE_WITH_REDUCE(r, data, type) \
- inline bool can_accumulate_with_reduce(type init, plus<type>) \
- { \
- return init == type(0); \
- } \
- inline bool can_accumulate_with_reduce(type init, multiplies<type>) \
- { \
- return init == type(1); \
- }
- BOOST_PP_SEQ_FOR_EACH(
- BOOST_COMPUTE_DETAIL_DECLARE_CAN_ACCUMULATE_WITH_REDUCE,
- _,
- (char_)(uchar_)(short_)(ushort_)(int_)(uint_)(long_)(ulong_)
- )
- template<class T>
- inline bool can_accumulate_with_reduce(T init, min<T>)
- {
- return init == (std::numeric_limits<T>::max)();
- }
- template<class T>
- inline bool can_accumulate_with_reduce(T init, max<T>)
- {
- return init == (std::numeric_limits<T>::min)();
- }
- #undef BOOST_COMPUTE_DETAIL_DECLARE_CAN_ACCUMULATE_WITH_REDUCE
- template<class InputIterator, class T, class BinaryFunction>
- inline T dispatch_accumulate(InputIterator first,
- InputIterator last,
- T init,
- BinaryFunction function,
- command_queue &queue)
- {
- size_t size = iterator_range_size(first, last);
- if(size == 0){
- return init;
- }
- if(can_accumulate_with_reduce(init, function)){
- T result;
- reduce(first, last, &result, function, queue);
- return result;
- }
- else {
- return generic_accumulate(first, last, init, function, queue);
- }
- }
- }
- template<class InputIterator, class T, class BinaryFunction>
- inline T accumulate(InputIterator first,
- InputIterator last,
- T init,
- BinaryFunction function,
- command_queue &queue = system::default_queue())
- {
- BOOST_STATIC_ASSERT(is_device_iterator<InputIterator>::value);
- return detail::dispatch_accumulate(first, last, init, function, queue);
- }
- template<class InputIterator, class T>
- inline T accumulate(InputIterator first,
- InputIterator last,
- T init,
- command_queue &queue = system::default_queue())
- {
- BOOST_STATIC_ASSERT(is_device_iterator<InputIterator>::value);
- typedef typename std::iterator_traits<InputIterator>::value_type IT;
- return detail::dispatch_accumulate(first, last, init, plus<IT>(), queue);
- }
- }
- }
- #endif
|