]>
Commit | Line | Data |
---|---|---|
c2ba9709 JS |
1 | // -*- C++ -*- |
2 | ||
748086b7 | 3 | // Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc. |
c2ba9709 JS |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free | |
6 | // software; you can redistribute it and/or modify it under the terms | |
7 | // of the GNU General Public License as published by the Free Software | |
748086b7 | 8 | // Foundation; either version 3, or (at your option) any later |
c2ba9709 JS |
9 | // version. |
10 | ||
11 | // This library is distributed in the hope that it will be useful, but | |
12 | // WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | // General Public License for more details. | |
15 | ||
748086b7 JJ |
16 | // Under Section 7 of GPL version 3, you are granted additional |
17 | // permissions described in the GCC Runtime Library Exception, version | |
18 | // 3.1, as published by the Free Software Foundation. | |
19 | ||
20 | // You should have received a copy of the GNU General Public License and | |
21 | // a copy of the GCC Runtime Library Exception along with this program; | |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see | |
23 | // <http://www.gnu.org/licenses/>. | |
c2ba9709 JS |
24 | |
25 | /** @file parallel/omp_loop.h | |
26 | * @brief Parallelization of embarrassingly parallel execution by | |
27 | * means of an OpenMP for loop. | |
28 | * This file is a GNU parallel extension to the Standard C++ Library. | |
29 | */ | |
30 | ||
31 | // Written by Felix Putze. | |
32 | ||
33 | #ifndef _GLIBCXX_PARALLEL_OMP_LOOP_H | |
34 | #define _GLIBCXX_PARALLEL_OMP_LOOP_H 1 | |
35 | ||
36 | #include <omp.h> | |
37 | ||
38 | #include <parallel/settings.h> | |
39 | #include <parallel/basic_iterator.h> | |
e683ee2a | 40 | #include <parallel/base.h> |
c2ba9709 JS |
41 | |
42 | namespace __gnu_parallel | |
43 | { | |
e683ee2a JS |
44 | /** @brief Embarrassingly parallel algorithm for random access |
45 | * iterators, using an OpenMP for loop. | |
46 | * | |
47 | * @param begin Begin iterator of element sequence. | |
48 | * @param end End iterator of element sequence. | |
49 | * @param o User-supplied functor (comparator, predicate, adding | |
50 | * functor, etc.). | |
51 | * @param f Functor to "process" an element with op (depends on | |
52 | * desired functionality, e. g. for std::for_each(), ...). | |
53 | * @param r Functor to "add" a single result to the already | |
54 | * processed elements (depends on functionality). | |
55 | * @param base Base value for reduction. | |
56 | * @param output Pointer to position where final result is written to | |
57 | * @param bound Maximum number of elements processed (e. g. for | |
58 | * std::count_n()). | |
59 | * @return User-supplied functor (that may contain a part of the result). | |
60 | */ | |
61 | template<typename RandomAccessIterator, | |
5817ff8e PC |
62 | typename Op, |
63 | typename Fu, | |
64 | typename Red, | |
65 | typename Result> | |
c2ba9709 | 66 | Op |
5817ff8e PC |
67 | for_each_template_random_access_omp_loop(RandomAccessIterator begin, |
68 | RandomAccessIterator end, | |
69 | Op o, Fu& f, Red r, Result base, | |
70 | Result& output, | |
71 | typename std::iterator_traits | |
72 | <RandomAccessIterator>:: | |
73 | difference_type bound) | |
c2ba9709 | 74 | { |
e683ee2a JS |
75 | typedef typename |
76 | std::iterator_traits<RandomAccessIterator>::difference_type | |
77 | difference_type; | |
c2ba9709 | 78 | |
c2ba9709 | 79 | difference_type length = end - begin; |
e683ee2a | 80 | thread_index_t num_threads = |
5817ff8e | 81 | __gnu_parallel::min<difference_type>(get_max_threads(), length); |
c2ba9709 | 82 | |
e683ee2a JS |
83 | Result *thread_results; |
84 | ||
85 | # pragma omp parallel num_threads(num_threads) | |
c2ba9709 | 86 | { |
e683ee2a JS |
87 | # pragma omp single |
88 | { | |
89 | num_threads = omp_get_num_threads(); | |
90 | thread_results = new Result[num_threads]; | |
91 | ||
5817ff8e | 92 | for (thread_index_t i = 0; i < num_threads; ++i) |
e683ee2a JS |
93 | thread_results[i] = Result(); |
94 | } | |
95 | ||
96 | thread_index_t iam = omp_get_thread_num(); | |
97 | ||
ee1b5fc5 | 98 | # pragma omp for schedule(dynamic, _Settings::get().workstealing_chunk_size) |
5817ff8e | 99 | for (difference_type pos = 0; pos < length; ++pos) |
e683ee2a JS |
100 | thread_results[iam] = |
101 | r(thread_results[iam], f(o, begin+pos)); | |
102 | } //parallel | |
c2ba9709 | 103 | |
5817ff8e | 104 | for (thread_index_t i = 0; i < num_threads; ++i) |
e683ee2a | 105 | output = r(output, thread_results[i]); |
c2ba9709 JS |
106 | |
107 | delete [] thread_results; | |
108 | ||
109 | // Points to last element processed (needed as return value for | |
110 | // some algorithms like transform). | |
111 | f.finish_iterator = begin + length; | |
112 | ||
113 | return o; | |
114 | } | |
e683ee2a | 115 | |
c2ba9709 JS |
116 | } // end namespace |
117 | ||
cbcd1e45 | 118 | #endif /* _GLIBCXX_PARALLEL_OMP_LOOP_H */ |