Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

231 Zeilen
7.3KB

  1. // -*- C++ -*-
  2. // Copyright (C) 2007-2020 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the terms
  6. // of the GNU General Public License as published by the Free Software
  7. // Foundation; either version 3, or (at your option) any later
  8. // version.
  9. // This library is distributed in the hope that it will be useful, but
  10. // WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. // General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file parallel/partial_sum.h
  21. * @brief Parallel implementation of std::partial_sum(), i.e. prefix
  22. * sums.
  23. * This file is a GNU parallel extension to the Standard C++ Library.
  24. */
  25. // Written by Johannes Singler.
  26. #ifndef _GLIBCXX_PARALLEL_PARTIAL_SUM_H
  27. #define _GLIBCXX_PARALLEL_PARTIAL_SUM_H 1
  28. #include <omp.h>
  29. #include <new>
  30. #include <bits/stl_algobase.h>
  31. #include <parallel/parallel.h>
  32. #include <parallel/numericfwd.h>
  33. namespace __gnu_parallel
  34. {
  35. // Problem: there is no 0-element given.
  36. /** @brief Base case prefix sum routine.
  37. * @param __begin Begin iterator of input sequence.
  38. * @param __end End iterator of input sequence.
  39. * @param __result Begin iterator of output sequence.
  40. * @param __bin_op Associative binary function.
  41. * @param __value Start value. Must be passed since the neutral
  42. * element is unknown in general.
  43. * @return End iterator of output sequence. */
  44. template<typename _IIter,
  45. typename _OutputIterator,
  46. typename _BinaryOperation>
  47. _OutputIterator
  48. __parallel_partial_sum_basecase(_IIter __begin, _IIter __end,
  49. _OutputIterator __result,
  50. _BinaryOperation __bin_op,
  51. typename std::iterator_traits <_IIter>::value_type __value)
  52. {
  53. if (__begin == __end)
  54. return __result;
  55. while (__begin != __end)
  56. {
  57. __value = __bin_op(__value, *__begin);
  58. *__result = __value;
  59. ++__result;
  60. ++__begin;
  61. }
  62. return __result;
  63. }
  64. /** @brief Parallel partial sum implementation, two-phase approach,
  65. no recursion.
  66. * @param __begin Begin iterator of input sequence.
  67. * @param __end End iterator of input sequence.
  68. * @param __result Begin iterator of output sequence.
  69. * @param __bin_op Associative binary function.
  70. * @param __n Length of sequence.
  71. * @return End iterator of output sequence.
  72. */
  73. template<typename _IIter,
  74. typename _OutputIterator,
  75. typename _BinaryOperation>
  76. _OutputIterator
  77. __parallel_partial_sum_linear(_IIter __begin, _IIter __end,
  78. _OutputIterator __result,
  79. _BinaryOperation __bin_op,
  80. typename std::iterator_traits<_IIter>::difference_type __n)
  81. {
  82. typedef std::iterator_traits<_IIter> _TraitsType;
  83. typedef typename _TraitsType::value_type _ValueType;
  84. typedef typename _TraitsType::difference_type _DifferenceType;
  85. if (__begin == __end)
  86. return __result;
  87. _ThreadIndex __num_threads =
  88. std::min<_DifferenceType>(__get_max_threads(), __n - 1);
  89. if (__num_threads < 2)
  90. {
  91. *__result = *__begin;
  92. return __parallel_partial_sum_basecase(__begin + 1, __end,
  93. __result + 1, __bin_op,
  94. *__begin);
  95. }
  96. _DifferenceType* __borders;
  97. _ValueType* __sums;
  98. const _Settings& __s = _Settings::get();
  99. # pragma omp parallel num_threads(__num_threads)
  100. {
  101. # pragma omp single
  102. {
  103. __num_threads = omp_get_num_threads();
  104. __borders = new _DifferenceType[__num_threads + 2];
  105. if (__s.partial_sum_dilation == 1.0f)
  106. __equally_split(__n, __num_threads + 1, __borders);
  107. else
  108. {
  109. _DifferenceType __first_part_length =
  110. std::max<_DifferenceType>(1,
  111. __n / (1.0f + __s.partial_sum_dilation * __num_threads));
  112. _DifferenceType __chunk_length =
  113. (__n - __first_part_length) / __num_threads;
  114. _DifferenceType __borderstart =
  115. __n - __num_threads * __chunk_length;
  116. __borders[0] = 0;
  117. for (_ThreadIndex __i = 1; __i < (__num_threads + 1); ++__i)
  118. {
  119. __borders[__i] = __borderstart;
  120. __borderstart += __chunk_length;
  121. }
  122. __borders[__num_threads + 1] = __n;
  123. }
  124. __sums = static_cast<_ValueType*>(::operator new(sizeof(_ValueType)
  125. * __num_threads));
  126. _OutputIterator __target_end;
  127. } //single
  128. _ThreadIndex __iam = omp_get_thread_num();
  129. if (__iam == 0)
  130. {
  131. *__result = *__begin;
  132. __parallel_partial_sum_basecase(__begin + 1,
  133. __begin + __borders[1],
  134. __result + 1,
  135. __bin_op, *__begin);
  136. ::new(&(__sums[__iam])) _ValueType(*(__result + __borders[1] - 1));
  137. }
  138. else
  139. {
  140. ::new(&(__sums[__iam]))
  141. _ValueType(__gnu_parallel::accumulate(
  142. __begin + __borders[__iam] + 1,
  143. __begin + __borders[__iam + 1],
  144. *(__begin + __borders[__iam]),
  145. __bin_op,
  146. __gnu_parallel::sequential_tag()));
  147. }
  148. # pragma omp barrier
  149. # pragma omp single
  150. __parallel_partial_sum_basecase(__sums + 1, __sums + __num_threads,
  151. __sums + 1, __bin_op, __sums[0]);
  152. # pragma omp barrier
  153. // Still same team.
  154. __parallel_partial_sum_basecase(__begin + __borders[__iam + 1],
  155. __begin + __borders[__iam + 2],
  156. __result + __borders[__iam + 1],
  157. __bin_op, __sums[__iam]);
  158. } //parallel
  159. for (_ThreadIndex __i = 0; __i < __num_threads; ++__i)
  160. __sums[__i].~_ValueType();
  161. ::operator delete(__sums);
  162. delete[] __borders;
  163. return __result + __n;
  164. }
  165. /** @brief Parallel partial sum front-__end.
  166. * @param __begin Begin iterator of input sequence.
  167. * @param __end End iterator of input sequence.
  168. * @param __result Begin iterator of output sequence.
  169. * @param __bin_op Associative binary function.
  170. * @return End iterator of output sequence. */
  171. template<typename _IIter,
  172. typename _OutputIterator,
  173. typename _BinaryOperation>
  174. _OutputIterator
  175. __parallel_partial_sum(_IIter __begin, _IIter __end,
  176. _OutputIterator __result, _BinaryOperation __bin_op)
  177. {
  178. _GLIBCXX_CALL(__begin - __end)
  179. typedef std::iterator_traits<_IIter> _TraitsType;
  180. typedef typename _TraitsType::value_type _ValueType;
  181. typedef typename _TraitsType::difference_type _DifferenceType;
  182. _DifferenceType __n = __end - __begin;
  183. switch (_Settings::get().partial_sum_algorithm)
  184. {
  185. case LINEAR:
  186. // Need an initial offset.
  187. return __parallel_partial_sum_linear(__begin, __end, __result,
  188. __bin_op, __n);
  189. default:
  190. // Partial_sum algorithm not implemented.
  191. _GLIBCXX_PARALLEL_ASSERT(0);
  192. return __result + __n;
  193. }
  194. }
  195. }
  196. #endif /* _GLIBCXX_PARALLEL_PARTIAL_SUM_H */