2 * Copyright 2008-2013 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/iterator/iterator_traits.h>
33 /* Reduces [data, data + n) using binary_op and stores the result in data[0]
35 * Upon return the elements in [data + 1, data + n) have unspecified values.
37 template <typename Context, typename ValueIterator, typename BinaryFunction>
38 __device__ __thrust_forceinline__
39 void reduce_n(Context context, ValueIterator data, unsigned int n, BinaryFunction binary_op)
41 if (context.block_dimension() < n)
43 for (unsigned int i = context.block_dimension() + context.thread_index(); i < n; i += context.block_dimension())
44 data[context.thread_index()] = binary_op(data[context.thread_index()], data[i]);
51 unsigned int half = n / 2;
53 if (context.thread_index() < half)
54 data[context.thread_index()] = binary_op(data[context.thread_index()], data[n - context.thread_index() - 1]);
62 } // end namespace block
63 } // end namespace detail
64 } // end namespace cuda
65 } // end namespace system
66 } // end namespace thrust