2007-09-12 00:32:51 +02:00
|
|
|
// -*- C++ -*-
|
|
|
|
|
2009-04-09 17:00:19 +02:00
|
|
|
// Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
|
2007-09-12 00:32:51 +02:00
|
|
|
//
|
|
|
|
// This file is part of the GNU ISO C++ Library. This library is free
|
|
|
|
// software; you can redistribute it and/or modify it under the terms
|
|
|
|
// of the GNU General Public License as published by the Free Software
|
2009-04-09 17:00:19 +02:00
|
|
|
// Foundation; either version 3, or (at your option) any later
|
2007-09-12 00:32:51 +02:00
|
|
|
// version.
|
|
|
|
|
|
|
|
// This library is distributed in the hope that it will be useful, but
|
|
|
|
// WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// General Public License for more details.
|
|
|
|
|
2009-04-09 17:00:19 +02:00
|
|
|
// Under Section 7 of GPL version 3, you are granted additional
|
|
|
|
// permissions described in the GCC Runtime Library Exception, version
|
|
|
|
// 3.1, as published by the Free Software Foundation.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License and
|
|
|
|
// a copy of the GCC Runtime Library Exception along with this program;
|
|
|
|
// see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
|
|
|
|
// <http://www.gnu.org/licenses/>.
|
2007-09-12 00:32:51 +02:00
|
|
|
|
|
|
|
/** @file parallel/partial_sum.h
|
2009-09-16 11:47:25 +02:00
|
|
|
* @brief Parallel implementation of std::partial_sum(), i.e. prefix
|
|
|
|
* sums.
|
2007-09-12 00:32:51 +02:00
|
|
|
* This file is a GNU parallel extension to the Standard C++ Library.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// Written by Johannes Singler.
|
|
|
|
|
|
|
|
#ifndef _GLIBCXX_PARALLEL_PARTIAL_SUM_H
|
|
|
|
#define _GLIBCXX_PARALLEL_PARTIAL_SUM_H 1
|
|
|
|
|
|
|
|
#include <omp.h>
|
2007-12-15 17:19:37 +01:00
|
|
|
#include <new>
|
2007-09-12 00:32:51 +02:00
|
|
|
#include <bits/stl_algobase.h>
|
|
|
|
#include <parallel/parallel.h>
|
|
|
|
#include <parallel/numericfwd.h>
|
|
|
|
|
|
|
|
namespace __gnu_parallel
|
|
|
|
{
|
|
|
|
// Problem: there is no 0-element given.
|
|
|
|
|
2009-11-05 15:06:13 +01:00
|
|
|
/** @brief Base case prefix sum routine.
|
|
|
|
* @param __begin Begin iterator of input sequence.
|
|
|
|
* @param __end End iterator of input sequence.
|
|
|
|
* @param __result Begin iterator of output sequence.
|
|
|
|
* @param __bin_op Associative binary function.
|
|
|
|
* @param __value Start value. Must be passed since the neutral
|
|
|
|
* element is unknown in general.
|
|
|
|
* @return End iterator of output sequence. */
|
|
|
|
template<typename _IIter,
|
|
|
|
typename _OutputIterator,
|
|
|
|
typename _BinaryOperation>
|
|
|
|
_OutputIterator
|
|
|
|
__parallel_partial_sum_basecase(_IIter __begin, _IIter __end,
|
|
|
|
_OutputIterator __result,
|
|
|
|
_BinaryOperation __bin_op,
|
|
|
|
typename std::iterator_traits <_IIter>::value_type __value)
|
|
|
|
{
|
|
|
|
if (__begin == __end)
|
|
|
|
return __result;
|
|
|
|
|
|
|
|
while (__begin != __end)
|
|
|
|
{
|
|
|
|
__value = __bin_op(__value, *__begin);
|
|
|
|
*__result = __value;
|
|
|
|
++__result;
|
|
|
|
++__begin;
|
|
|
|
}
|
2009-09-16 11:47:25 +02:00
|
|
|
return __result;
|
2009-11-05 15:06:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** @brief Parallel partial sum implementation, two-phase approach,
|
|
|
|
no recursion.
|
|
|
|
* @param __begin Begin iterator of input sequence.
|
|
|
|
* @param __end End iterator of input sequence.
|
|
|
|
* @param __result Begin iterator of output sequence.
|
|
|
|
* @param __bin_op Associative binary function.
|
|
|
|
* @param __n Length of sequence.
|
|
|
|
* @param __num_threads Number of threads to use.
|
|
|
|
* @return End iterator of output sequence.
|
|
|
|
*/
|
|
|
|
template<typename _IIter,
|
|
|
|
typename _OutputIterator,
|
|
|
|
typename _BinaryOperation>
|
|
|
|
_OutputIterator
|
|
|
|
__parallel_partial_sum_linear(_IIter __begin, _IIter __end,
|
|
|
|
_OutputIterator __result,
|
|
|
|
_BinaryOperation __bin_op,
|
|
|
|
typename std::iterator_traits<_IIter>::difference_type __n)
|
|
|
|
{
|
|
|
|
typedef std::iterator_traits<_IIter> _TraitsType;
|
|
|
|
typedef typename _TraitsType::value_type _ValueType;
|
|
|
|
typedef typename _TraitsType::difference_type _DifferenceType;
|
|
|
|
|
|
|
|
if (__begin == __end)
|
|
|
|
return __result;
|
|
|
|
|
|
|
|
_ThreadIndex __num_threads =
|
2009-09-16 11:47:25 +02:00
|
|
|
std::min<_DifferenceType>(__get_max_threads(), __n - 1);
|
2007-11-22 11:13:08 +01:00
|
|
|
|
2009-11-05 15:06:13 +01:00
|
|
|
if (__num_threads < 2)
|
|
|
|
{
|
|
|
|
*__result = *__begin;
|
|
|
|
return __parallel_partial_sum_basecase(__begin + 1, __end,
|
|
|
|
__result + 1, __bin_op,
|
|
|
|
*__begin);
|
|
|
|
}
|
2007-09-12 00:32:51 +02:00
|
|
|
|
2009-11-05 15:06:13 +01:00
|
|
|
_DifferenceType* __borders;
|
|
|
|
_ValueType* __sums;
|
2007-09-12 00:32:51 +02:00
|
|
|
|
2009-11-05 15:06:13 +01:00
|
|
|
const _Settings& __s = _Settings::get();
|
2008-02-18 01:00:00 +01:00
|
|
|
|
2009-11-05 15:06:13 +01:00
|
|
|
# pragma omp parallel num_threads(__num_threads)
|
2007-09-12 00:32:51 +02:00
|
|
|
{
|
2007-11-22 11:13:08 +01:00
|
|
|
# pragma omp single
|
2009-11-05 15:06:13 +01:00
|
|
|
{
|
|
|
|
__num_threads = omp_get_num_threads();
|
|
|
|
|
|
|
|
__borders = new _DifferenceType[__num_threads + 2];
|
|
|
|
|
|
|
|
if (__s.partial_sum_dilation == 1.0f)
|
|
|
|
equally_split(__n, __num_threads + 1, __borders);
|
|
|
|
else
|
|
|
|
{
|
2010-06-08 11:11:49 +02:00
|
|
|
_DifferenceType __first_part_length =
|
|
|
|
std::max<_DifferenceType>(1,
|
|
|
|
__n / (1.0f + __s.partial_sum_dilation * __num_threads));
|
2009-11-05 15:06:13 +01:00
|
|
|
_DifferenceType __chunk_length =
|
2010-06-08 11:11:49 +02:00
|
|
|
(__n - __first_part_length) / __num_threads;
|
|
|
|
_DifferenceType __borderstart =
|
|
|
|
__n - __num_threads * __chunk_length;
|
2009-11-05 15:06:13 +01:00
|
|
|
__borders[0] = 0;
|
2009-11-09 17:36:59 +01:00
|
|
|
for (_ThreadIndex __i = 1; __i < (__num_threads + 1); ++__i)
|
2009-11-05 15:06:13 +01:00
|
|
|
{
|
|
|
|
__borders[__i] = __borderstart;
|
|
|
|
__borderstart += __chunk_length;
|
|
|
|
}
|
|
|
|
__borders[__num_threads + 1] = __n;
|
|
|
|
}
|
|
|
|
|
|
|
|
__sums = static_cast<_ValueType*>(::operator new(sizeof(_ValueType)
|
2009-09-17 10:42:39 +02:00
|
|
|
* __num_threads));
|
2009-11-05 15:06:13 +01:00
|
|
|
_OutputIterator __target_end;
|
|
|
|
} //single
|
2007-11-22 11:13:08 +01:00
|
|
|
|
2009-09-16 11:47:25 +02:00
|
|
|
_ThreadIndex __iam = omp_get_thread_num();
|
|
|
|
if (__iam == 0)
|
2007-11-22 11:13:08 +01:00
|
|
|
{
|
2009-09-16 11:47:25 +02:00
|
|
|
*__result = *__begin;
|
2009-11-06 12:39:35 +01:00
|
|
|
__parallel_partial_sum_basecase(__begin + 1,
|
|
|
|
__begin + __borders[1],
|
|
|
|
__result + 1,
|
|
|
|
__bin_op, *__begin);
|
2009-09-16 11:47:25 +02:00
|
|
|
::new(&(__sums[__iam])) _ValueType(*(__result + __borders[1] - 1));
|
2007-11-22 11:13:08 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-09-16 11:47:25 +02:00
|
|
|
::new(&(__sums[__iam]))
|
2010-03-22 18:06:50 +01:00
|
|
|
_ValueType(__gnu_parallel::accumulate(
|
|
|
|
__begin + __borders[__iam] + 1,
|
2009-09-17 10:42:39 +02:00
|
|
|
__begin + __borders[__iam + 1],
|
|
|
|
*(__begin + __borders[__iam]),
|
|
|
|
__bin_op,
|
|
|
|
__gnu_parallel::sequential_tag()));
|
2007-11-22 11:13:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
# pragma omp barrier
|
|
|
|
|
|
|
|
# pragma omp single
|
2009-11-05 15:06:13 +01:00
|
|
|
__parallel_partial_sum_basecase(__sums + 1, __sums + __num_threads,
|
2009-11-06 12:39:35 +01:00
|
|
|
__sums + 1, __bin_op, __sums[0]);
|
2007-11-22 11:13:08 +01:00
|
|
|
|
|
|
|
# pragma omp barrier
|
|
|
|
|
2009-11-05 15:06:13 +01:00
|
|
|
// Still same team.
|
|
|
|
__parallel_partial_sum_basecase(__begin + __borders[__iam + 1],
|
|
|
|
__begin + __borders[__iam + 2],
|
|
|
|
__result + __borders[__iam + 1],
|
|
|
|
__bin_op, __sums[__iam]);
|
2007-11-22 11:13:08 +01:00
|
|
|
} //parallel
|
|
|
|
|
2009-11-05 15:06:13 +01:00
|
|
|
::operator delete(__sums);
|
|
|
|
delete[] __borders;
|
|
|
|
|
|
|
|
return __result + __n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @brief Parallel partial sum front-__end.
|
|
|
|
* @param __begin Begin iterator of input sequence.
|
|
|
|
* @param __end End iterator of input sequence.
|
|
|
|
* @param __result Begin iterator of output sequence.
|
|
|
|
* @param __bin_op Associative binary function.
|
|
|
|
* @return End iterator of output sequence. */
|
|
|
|
template<typename _IIter,
|
|
|
|
typename _OutputIterator,
|
|
|
|
typename _BinaryOperation>
|
|
|
|
_OutputIterator
|
|
|
|
__parallel_partial_sum(_IIter __begin, _IIter __end,
|
|
|
|
_OutputIterator __result, _BinaryOperation __bin_op)
|
|
|
|
{
|
|
|
|
_GLIBCXX_CALL(__begin - __end)
|
|
|
|
|
|
|
|
typedef std::iterator_traits<_IIter> _TraitsType;
|
|
|
|
typedef typename _TraitsType::value_type _ValueType;
|
|
|
|
typedef typename _TraitsType::difference_type _DifferenceType;
|
|
|
|
|
|
|
|
_DifferenceType __n = __end - __begin;
|
|
|
|
|
|
|
|
switch (_Settings::get().partial_sum_algorithm)
|
|
|
|
{
|
|
|
|
case LINEAR:
|
|
|
|
// Need an initial offset.
|
|
|
|
return __parallel_partial_sum_linear(__begin, __end, __result,
|
|
|
|
__bin_op, __n);
|
|
|
|
default:
|
|
|
|
// Partial_sum algorithm not implemented.
|
|
|
|
_GLIBCXX_PARALLEL_ASSERT(0);
|
|
|
|
return __result + __n;
|
|
|
|
}
|
|
|
|
}
|
2007-09-12 00:32:51 +02:00
|
|
|
}
|
|
|
|
|
2008-07-15 12:06:58 +02:00
|
|
|
#endif /* _GLIBCXX_PARALLEL_PARTIAL_SUM_H */
|