omp_loop_static.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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/omp_loop_static.h
  21. * @brief Parallelization of embarrassingly parallel execution by
  22. * means of an OpenMP for loop with static scheduling.
  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_STATIC_H
  27. #define _GLIBCXX_PARALLEL_OMP_LOOP_STATIC_H 1
  28. #include <omp.h>
  29. #include <parallel/settings.h>
  30. #include <parallel/basic_iterator.h>
  31. namespace __gnu_parallel
  32. {
  33. /** @brief Embarrassingly parallel algorithm for random access
  34. * iterators, using an OpenMP for loop with static scheduling.
  35. *
  36. * @param __begin Begin iterator of element sequence.
  37. * @param __end End iterator of element sequence.
  38. * @param __o User-supplied functor (comparator, predicate, adding
  39. * functor, ...).
  40. * @param __f Functor to @a process an element with __op (depends on
  41. * desired functionality, e. g. for std::for_each(), ...).
  42. * @param __r Functor to @a add a single __result to the already processed
  43. * __elements (depends on functionality).
  44. * @param __base Base value for reduction.
  45. * @param __output Pointer to position where final result is written to
  46. * @param __bound Maximum number of elements processed (e. g. for
  47. * std::count_n()).
  48. * @return User-supplied functor (that may contain a part of the result).
  49. */
  50. template<typename _RAIter,
  51. typename _Op,
  52. typename _Fu,
  53. typename _Red,
  54. typename _Result>
  55. _Op
  56. __for_each_template_random_access_omp_loop_static(_RAIter __begin,
  57. _RAIter __end, _Op __o,
  58. _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 = std::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(static, _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.finish_iterator = __begin + __length;
  90. return __o;
  91. }
  92. } // end namespace
  93. #endif /* _GLIBCXX_PARALLEL_OMP_LOOP_STATIC_H */