OSDN Git Service

CUDA
[eos/hostdependX86LINUX64.git] / util / X86LINUX64 / cuda-6.5 / include / thrust / system / tbb / detail / reduce_intervals.h
1 /*
2  *  Copyright 2008-2013 NVIDIA Corporation
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #pragma once
18
19 #include <thrust/detail/config.h>
20 #include <thrust/system/tbb/detail/execution_policy.h>
21
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>
27 #include <cassert>
28
29 namespace thrust
30 {
31 namespace system
32 {
33 namespace tbb
34 {
35 namespace detail
36 {
37 namespace reduce_intervals_detail
38 {
39
40
41 template<typename L, typename R>
42   inline L divide_ri(const L x, const R y)
43 {
44   return (x + (y - 1)) / y;
45 }
46
47
48 template<typename RandomAccessIterator1, typename RandomAccessIterator2, typename Size, typename BinaryFunction>
49   struct body
50 {
51   RandomAccessIterator1 first;
52   RandomAccessIterator2 result;
53   Size n, interval_size;
54   BinaryFunction binary_op;
55
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)
58   {}
59
60   void operator()(const ::tbb::blocked_range<Size> &r) const
61   {
62     assert(r.size() == 1);
63
64     Size interval_idx = r.begin();
65
66     Size offset_to_first = interval_size * interval_idx;
67     Size offset_to_last = thrust::min(n, offset_to_first + interval_size);
68
69     RandomAccessIterator1 my_first = first + offset_to_first;
70     RandomAccessIterator1 my_last  = first + offset_to_last;
71
72     thrust::cpp::tag seq;
73
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);
78   }
79 };
80
81
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)
85 {
86   return body<RandomAccessIterator1,RandomAccessIterator2,Size,BinaryFunction>(first, result, n, interval_size, binary_op);
87 }
88
89
90 } // end reduce_intervals_detail
91
92
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,
97                         Size interval_size,
98                         RandomAccessIterator2 result,
99                         BinaryFunction binary_op)
100 {
101   typename thrust::iterator_difference<RandomAccessIterator1>::type n = last - first;
102
103   Size num_intervals = reduce_intervals_detail::divide_ri(n, interval_size);
104
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());
106 }
107
108
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,
113                         Size interval_size,
114                         RandomAccessIterator2 result)
115 {
116   typedef typename thrust::iterator_value<RandomAccessIterator1>::type value_type;
117
118   return thrust::system::tbb::detail::reduce_intervals(exec, first, last, interval_size, result, thrust::plus<value_type>());
119 }
120
121
122 } // end detail
123 } // end tbb
124 } // end system
125 } // end thrust
126