2 * Copyright 2008-2012 NVIDIA Corporation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 #include <thrust/detail/config.h>
20 #include <thrust/system/tbb/detail/execution_policy.h>
22 #include <tbb/parallel_for.h>
23 #include <thrust/iterator/iterator_traits.h>
24 #include <thrust/detail/minmax.h>
25 #include <thrust/system/cpp/memory.h>
26 #include <thrust/reduce.h>
37 namespace reduce_intervals_detail
41 template<typename L, typename R>
42 inline L divide_ri(const L x, const R y)
44 return (x + (y - 1)) / y;
48 template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename Size, typename BinaryFunction>
51 RandomAccessIterator1 first;
52 RandomAccessIterator2 result;
53 Size n, interval_size;
54 BinaryFunction binary_op;
56 body(RandomAccessIterator1 first, RandomAccessIterator2 result, Size n, Size interval_size, BinaryFunction binary_op)
57 : first(first), result(result), n(n), interval_size(interval_size), binary_op(binary_op)
60 void operator()(const ::tbb::blocked_range<Size> &r) const
62 assert(r.size() == 1);
64 Size interval_idx = r.begin();
66 Size offset_to_first = interval_size * interval_idx;
67 Size offset_to_last = thrust::min(n, offset_to_first + interval_size);
69 RandomAccessIterator1 my_first = first + offset_to_first;
70 RandomAccessIterator1 my_last = first + offset_to_last;
74 // carefully pass the init value for the interval with raw_reference_cast
75 typedef typename BinaryFunction::result_type sum_type;
76 result[interval_idx] =
77 thrust::reduce(seq, my_first + 1, my_last, sum_type(thrust::raw_reference_cast(*my_first)), binary_op);
82 template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename Size, typename BinaryFunction>
83 body<RandomAccessIterator1,RandomAccessIterator2,Size,BinaryFunction>
84 make_body(RandomAccessIterator1 first, RandomAccessIterator2 result, Size n, Size interval_size, BinaryFunction binary_op)
86 return body<RandomAccessIterator1,RandomAccessIterator2,Size,BinaryFunction>(first, result, n, interval_size, binary_op);
90 } // end reduce_intervals_detail
93 template<typename DerivedPolicy, typename RandomAccessIterator1, typename Size, typename RandomAccessIterator2, typename BinaryFunction>
94 void reduce_intervals(thrust::tbb::execution_policy<DerivedPolicy> &,
95 RandomAccessIterator1 first,
96 RandomAccessIterator1 last,
98 RandomAccessIterator2 result,
99 BinaryFunction binary_op)
101 typename thrust::iterator_difference<RandomAccessIterator1>::type n = last - first;
103 Size num_intervals = reduce_intervals_detail::divide_ri(n, interval_size);
105 ::tbb::parallel_for(::tbb::blocked_range<Size>(0, num_intervals, 1), reduce_intervals_detail::make_body(first, result, Size(n), interval_size, binary_op), ::tbb::simple_partitioner());
109 template<typename DerivedPolicy, typename RandomAccessIterator1, typename Size, typename RandomAccessIterator2>
110 void reduce_intervals(thrust::tbb::execution_policy<DerivedPolicy> &exec,
111 RandomAccessIterator1 first,
112 RandomAccessIterator1 last,
114 RandomAccessIterator2 result)
116 typedef typename thrust::iterator_value<RandomAccessIterator1>::type value_type;
118 return thrust::system::tbb::detail::reduce_intervals(exec, first, last, interval_size, result, thrust::plus<value_type>());