par_loop.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. // -*- C++ -*-
  2. // Copyright (C) 2007-2022 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/par_loop.h
  21. * @brief Parallelization of embarrassingly parallel execution by
  22. * means of equal splitting.
  23. * This file is a GNU parallel extension to the Standard C++ Library.
  24. */
  25. // Written by Felix Putze.
  26. #ifndef _GLIBCXX_PARALLEL_PAR_LOOP_H
  27. #define _GLIBCXX_PARALLEL_PAR_LOOP_H 1
  28. #include <omp.h>
  29. #include <parallel/settings.h>
  30. #include <parallel/base.h>
  31. #include <parallel/equally_split.h>
  32. namespace __gnu_parallel
  33. {
  34. /** @brief Embarrassingly parallel algorithm for random access
  35. * iterators, using hand-crafted parallelization by equal splitting
  36. * the work.
  37. *
  38. * @param __begin Begin iterator of element sequence.
  39. * @param __end End iterator of element sequence.
  40. * @param __o User-supplied functor (comparator, predicate, adding
  41. * functor, ...)
  42. * @param __f Functor to "process" an element with __op (depends on
  43. * desired functionality, e. g. for std::for_each(), ...).
  44. * @param __r Functor to "add" a single __result to the already
  45. * processed elements (depends on functionality).
  46. * @param __base Base value for reduction.
  47. * @param __output Pointer to position where final result is written to
  48. * @param __bound Maximum number of elements processed (e. g. for
  49. * std::count_n()).
  50. * @return User-supplied functor (that may contain a part of the result).
  51. */
  52. template<typename _RAIter,
  53. typename _Op,
  54. typename _Fu,
  55. typename _Red,
  56. typename _Result>
  57. _Op
  58. __for_each_template_random_access_ed(_RAIter __begin, _RAIter __end,
  59. _Op __o, _Fu& __f, _Red __r,
  60. _Result __base, _Result& __output,
  61. typename std::iterator_traits<_RAIter>::difference_type __bound)
  62. {
  63. typedef std::iterator_traits<_RAIter> _TraitsType;
  64. typedef typename _TraitsType::difference_type _DifferenceType;
  65. const _DifferenceType __length = __end - __begin;
  66. _Result *__thread_results;
  67. bool* __constructed;
  68. _ThreadIndex __num_threads = __gnu_parallel::min<_DifferenceType>
  69. (__get_max_threads(), __length);
  70. # pragma omp parallel num_threads(__num_threads)
  71. {
  72. # pragma omp single
  73. {
  74. __num_threads = omp_get_num_threads();
  75. __thread_results = static_cast<_Result*>
  76. (::operator new(__num_threads * sizeof(_Result)));
  77. __constructed = new bool[__num_threads];
  78. }
  79. _ThreadIndex __iam = omp_get_thread_num();
  80. // Neutral element.
  81. _Result* __reduct;
  82. _DifferenceType
  83. __start = __equally_split_point(__length, __num_threads, __iam),
  84. __stop = __equally_split_point(__length, __num_threads, __iam + 1);
  85. if (__start < __stop)
  86. {
  87. __reduct = new _Result(__f(__o, __begin + __start));
  88. ++__start;
  89. __constructed[__iam] = true;
  90. }
  91. else
  92. __constructed[__iam] = false;
  93. for (; __start < __stop; ++__start)
  94. *__reduct = __r(*__reduct, __f(__o, __begin + __start));
  95. if (__constructed[__iam])
  96. {
  97. ::new(&__thread_results[__iam]) _Result(*__reduct);
  98. delete __reduct;
  99. }
  100. } //parallel
  101. for (_ThreadIndex __i = 0; __i < __num_threads; ++__i)
  102. if (__constructed[__i])
  103. {
  104. __output = __r(__output, __thread_results[__i]);
  105. __thread_results[__i].~_Result();
  106. }
  107. // Points to last element processed (needed as return value for
  108. // some algorithms like transform).
  109. __f._M_finish_iterator = __begin + __length;
  110. ::operator delete(__thread_results);
  111. delete[] __constructed;
  112. return __o;
  113. }
  114. } // end namespace
  115. #endif /* _GLIBCXX_PARALLEL_PAR_LOOP_H */