Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

116 lines
3.9KB

  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/omp_loop.h
  21. * @brief Parallelization of embarrassingly parallel execution by
  22. * means of an OpenMP for loop.
  23. * This file is a GNU parallel extension to the Standard C++ Library.
  24. */
  25. // Written by Felix Putze.
  26. #ifndef _GLIBCXX_PARALLEL_OMP_LOOP_H
  27. #define _GLIBCXX_PARALLEL_OMP_LOOP_H 1
  28. #include <omp.h>
  29. #include <parallel/settings.h>
  30. #include <parallel/basic_iterator.h>
  31. #include <parallel/base.h>
  32. namespace __gnu_parallel
  33. {
  34. /** @brief Embarrassingly parallel algorithm for random access
  35. * iterators, using an OpenMP for loop.
  36. *
  37. * @param __begin Begin iterator of element sequence.
  38. * @param __end End iterator of element sequence.
  39. * @param __o User-supplied functor (comparator, predicate, adding
  40. * functor, etc.).
  41. * @param __f Functor to @a process an element with __op (depends on
  42. * desired functionality, e. g. for std::for_each(), ...).
  43. * @param __r Functor to @a add a single __result to the already
  44. * processed elements (depends on functionality).
  45. * @param __base Base value for reduction.
  46. * @param __output Pointer to position where final result is written to
  47. * @param __bound Maximum number of elements processed (e. g. for
  48. * std::count_n()).
  49. * @return User-supplied functor (that may contain a part of the result).
  50. */
  51. template<typename _RAIter,
  52. typename _Op,
  53. typename _Fu,
  54. typename _Red,
  55. typename _Result>
  56. _Op
  57. __for_each_template_random_access_omp_loop(_RAIter __begin, _RAIter __end,
  58. _Op __o, _Fu& __f, _Red __r,
  59. _Result __base,
  60. _Result& __output,
  61. typename std::iterator_traits<_RAIter>::difference_type __bound)
  62. {
  63. typedef typename std::iterator_traits<_RAIter>::difference_type
  64. _DifferenceType;
  65. _DifferenceType __length = __end - __begin;
  66. _ThreadIndex __num_threads = __gnu_parallel::min<_DifferenceType>
  67. (__get_max_threads(), __length);
  68. _Result *__thread_results;
  69. # pragma omp parallel num_threads(__num_threads)
  70. {
  71. # pragma omp single
  72. {
  73. __num_threads = omp_get_num_threads();
  74. __thread_results = new _Result[__num_threads];
  75. for (_ThreadIndex __i = 0; __i < __num_threads; ++__i)
  76. __thread_results[__i] = _Result();
  77. }
  78. _ThreadIndex __iam = omp_get_thread_num();
  79. #pragma omp for schedule(dynamic, _Settings::get().workstealing_chunk_size)
  80. for (_DifferenceType __pos = 0; __pos < __length; ++__pos)
  81. __thread_results[__iam] = __r(__thread_results[__iam],
  82. __f(__o, __begin+__pos));
  83. } //parallel
  84. for (_ThreadIndex __i = 0; __i < __num_threads; ++__i)
  85. __output = __r(__output, __thread_results[__i]);
  86. delete [] __thread_results;
  87. // Points to last element processed (needed as return value for
  88. // some algorithms like transform).
  89. __f._M_finish_iterator = __begin + __length;
  90. return __o;
  91. }
  92. } // end namespace
  93. #endif /* _GLIBCXX_PARALLEL_OMP_LOOP_H */