for_each.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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/for_each.h
  21. * @brief Main interface for embarrassingly parallel functions.
  22. *
  23. * The explicit implementation are in other header files, like
  24. * workstealing.h, par_loop.h, omp_loop.h, and omp_loop_static.h.
  25. * This file is a GNU parallel extension to the Standard C++ Library.
  26. */
  27. // Written by Felix Putze.
  28. #ifndef _GLIBCXX_PARALLEL_FOR_EACH_H
  29. #define _GLIBCXX_PARALLEL_FOR_EACH_H 1
  30. #include <parallel/settings.h>
  31. #include <parallel/par_loop.h>
  32. #include <parallel/omp_loop.h>
  33. #include <parallel/workstealing.h>
  34. namespace __gnu_parallel
  35. {
  36. /** @brief Chose the desired algorithm by evaluating @c __parallelism_tag.
  37. * @param __begin Begin iterator of input sequence.
  38. * @param __end End iterator of input sequence.
  39. * @param __user_op A user-specified functor (comparator, predicate,
  40. * associative operator,...)
  41. * @param __functionality functor to @a process an element with
  42. * __user_op (depends on desired functionality, e. g. accumulate,
  43. * for_each,...
  44. * @param __reduction Reduction functor.
  45. * @param __reduction_start Initial value for reduction.
  46. * @param __output Output iterator.
  47. * @param __bound Maximum number of elements processed.
  48. * @param __parallelism_tag Parallelization method */
  49. template<typename _IIter, typename _UserOp,
  50. typename _Functionality, typename _Red, typename _Result>
  51. _UserOp
  52. __for_each_template_random_access(_IIter __begin, _IIter __end,
  53. _UserOp __user_op,
  54. _Functionality& __functionality,
  55. _Red __reduction,
  56. _Result __reduction_start,
  57. _Result& __output, typename
  58. std::iterator_traits<_IIter>::
  59. difference_type __bound,
  60. _Parallelism __parallelism_tag)
  61. {
  62. if (__parallelism_tag == parallel_unbalanced)
  63. return __for_each_template_random_access_ed
  64. (__begin, __end, __user_op, __functionality, __reduction,
  65. __reduction_start, __output, __bound);
  66. else if (__parallelism_tag == parallel_omp_loop)
  67. return __for_each_template_random_access_omp_loop
  68. (__begin, __end, __user_op, __functionality, __reduction,
  69. __reduction_start, __output, __bound);
  70. else if (__parallelism_tag == parallel_omp_loop_static)
  71. return __for_each_template_random_access_omp_loop
  72. (__begin, __end, __user_op, __functionality, __reduction,
  73. __reduction_start, __output, __bound);
  74. else //e. g. parallel_balanced
  75. return __for_each_template_random_access_workstealing
  76. (__begin, __end, __user_op, __functionality, __reduction,
  77. __reduction_start, __output, __bound);
  78. }
  79. }
  80. #endif /* _GLIBCXX_PARALLEL_FOR_EACH_H */