1 // -*- C++ -*-
2 // -*-===----------------------------------------------------------------------===//
3 //
4 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5 //
6 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
7 // See https://llvm.org/LICENSE.txt for license information.
8 //
9 //===----------------------------------------------------------------------===//
10
11 #ifndef _PSTL_INTERNAL_OMP_PARALLEL_FOR_EACH_H
12 #define _PSTL_INTERNAL_OMP_PARALLEL_FOR_EACH_H
13
14 #include "util.h"
15
16 namespace __pstl
17 {
18 namespace __omp_backend
19 {
20
21 template <class _ForwardIterator, class _Fp>
22 void
__parallel_for_each_body(_ForwardIterator __first,_ForwardIterator __last,_Fp __f)23 __parallel_for_each_body(_ForwardIterator __first, _ForwardIterator __last, _Fp __f)
24 {
25 using DifferenceType = typename std::iterator_traits<_ForwardIterator>::difference_type;
26 // TODO: Think of an approach to remove the std::distance call
27 auto __size = std::distance(__first, __last);
28
29 _PSTL_PRAGMA(omp taskloop untied mergeable)
30 for (DifferenceType __index = 0; __index < __size; ++__index)
31 {
32 // TODO: Think of an approach to remove the increment here each time.
33 auto __iter = std::next(__first, __index);
34 __f(*__iter);
35 }
36 }
37
38 template <class _ExecutionPolicy, class _ForwardIterator, class _Fp>
39 void
__parallel_for_each(_ExecutionPolicy &&,_ForwardIterator __first,_ForwardIterator __last,_Fp __f)40 __parallel_for_each(_ExecutionPolicy&&, _ForwardIterator __first, _ForwardIterator __last, _Fp __f)
41 {
42 if (omp_in_parallel())
43 {
44 // we don't create a nested parallel region in an existing parallel
45 // region: just create tasks
46 __pstl::__omp_backend::__parallel_for_each_body(__first, __last, __f);
47 }
48 else
49 {
50 // in any case (nested or non-nested) one parallel region is created and
51 // only one thread creates a set of tasks
52 _PSTL_PRAGMA(omp parallel)
53 _PSTL_PRAGMA(omp single nowait) { __pstl::__omp_backend::__parallel_for_each_body(__first, __last, __f); }
54 }
55 }
56
57 } // namespace __omp_backend
58 } // namespace __pstl
59 #endif // _PSTL_INTERNAL_OMP_PARALLEL_FOR_EACH_H
60