/* * Copyright 2008-2012 NVIDIA Corporation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #pragma once #include #include #include #include #include #include #include #include namespace thrust { namespace system { namespace tbb { namespace detail { namespace reduce_intervals_detail { template inline L divide_ri(const L x, const R y) { return (x + (y - 1)) / y; } template struct body { RandomAccessIterator1 first; RandomAccessIterator2 result; Size n, interval_size; BinaryFunction binary_op; body(RandomAccessIterator1 first, RandomAccessIterator2 result, Size n, Size interval_size, BinaryFunction binary_op) : first(first), result(result), n(n), interval_size(interval_size), binary_op(binary_op) {} void operator()(const ::tbb::blocked_range &r) const { assert(r.size() == 1); Size interval_idx = r.begin(); Size offset_to_first = interval_size * interval_idx; Size offset_to_last = thrust::min(n, offset_to_first + interval_size); RandomAccessIterator1 my_first = first + offset_to_first; RandomAccessIterator1 my_last = first + offset_to_last; thrust::cpp::tag seq; // carefully pass the init value for the interval with raw_reference_cast typedef typename BinaryFunction::result_type sum_type; result[interval_idx] = thrust::reduce(seq, my_first + 1, my_last, sum_type(thrust::raw_reference_cast(*my_first)), binary_op); } }; template body make_body(RandomAccessIterator1 first, RandomAccessIterator2 result, Size n, Size interval_size, BinaryFunction binary_op) { return body(first, result, n, interval_size, binary_op); } } // end reduce_intervals_detail template void reduce_intervals(thrust::tbb::execution_policy &, RandomAccessIterator1 first, RandomAccessIterator1 last, Size interval_size, RandomAccessIterator2 result, BinaryFunction binary_op) { typename thrust::iterator_difference::type n = last - first; Size num_intervals = reduce_intervals_detail::divide_ri(n, interval_size); ::tbb::parallel_for(::tbb::blocked_range(0, num_intervals, 1), reduce_intervals_detail::make_body(first, result, Size(n), interval_size, binary_op), ::tbb::simple_partitioner()); } template void reduce_intervals(thrust::tbb::execution_policy &exec, RandomAccessIterator1 first, RandomAccessIterator1 last, Size interval_size, RandomAccessIterator2 result) { typedef typename thrust::iterator_value::type value_type; return thrust::system::tbb::detail::reduce_intervals(exec, first, last, interval_size, result, thrust::plus()); } } // end detail } // end tbb } // end system } // end thrust