]> gcc.gnu.org Git - gcc.git/blob - libstdc++-v3/include/parallel/search.h
a253126d90ac676f9125178168763df96dcb9641
[gcc.git] / libstdc++-v3 / include / parallel / search.h
1 // -*- C++ -*-
2
3 // Copyright (C) 2007, 2008, 2009 Free Software Foundation, Inc.
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
8 // Foundation; either version 3, or (at your option) any later
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
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/>.
24
25 /** @file parallel/search.h
26 * @brief Parallel implementation __base for std::search() and
27 * std::search_n().
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_SEARCH_H
34 #define _GLIBCXX_PARALLEL_SEARCH_H 1
35
36 #include <bits/stl_algobase.h>
37
38 #include <parallel/parallel.h>
39 #include <parallel/equally_split.h>
40
41
42 namespace __gnu_parallel
43 {
44 /**
45 * @brief Precalculate __advances for Knuth-Morris-Pratt algorithm.
46 * @param __elements Begin iterator of sequence to search for.
47 * @param __length Length of sequence to search for.
48 * @param __advances Returned __offsets.
49 */
50 template<typename _RAIter, typename _DifferenceTp>
51 void
52 __calc_borders(_RAIter __elements, _DifferenceTp __length,
53 _DifferenceTp* __off)
54 {
55 typedef _DifferenceTp _DifferenceType;
56
57 __off[0] = -1;
58 if (__length > 1)
59 __off[1] = 0;
60 _DifferenceType __k = 0;
61 for (_DifferenceType __j = 2; __j <= __length; __j++)
62 {
63 while ((__k >= 0) && !(__elements[__k] == __elements[__j-1]))
64 __k = __off[__k];
65 __off[__j] = ++__k;
66 }
67 }
68
69 // Generic parallel find algorithm (requires random access iterator).
70
71 /** @brief Parallel std::search.
72 * @param __begin1 Begin iterator of first sequence.
73 * @param __end1 End iterator of first sequence.
74 * @param __begin2 Begin iterator of second sequence.
75 * @param __end2 End iterator of second sequence.
76 * @param __pred Find predicate.
77 * @return Place of finding in first sequences. */
78 template<typename __RAIter1,
79 typename __RAIter2,
80 typename _Pred>
81 __RAIter1
82 __search_template(__RAIter1 __begin1, __RAIter1 __end1,
83 __RAIter2 __begin2, __RAIter2 __end2,
84 _Pred __pred)
85 {
86 typedef std::iterator_traits<__RAIter1> _TraitsType;
87 typedef typename _TraitsType::difference_type _DifferenceType;
88
89 _GLIBCXX_CALL((__end1 - __begin1) + (__end2 - __begin2));
90
91 _DifferenceType __pattern_length = __end2 - __begin2;
92
93 // Pattern too short.
94 if(__pattern_length <= 0)
95 return __end1;
96
97 // Last point to start search.
98 _DifferenceType __input_length = (__end1 - __begin1) - __pattern_length;
99
100 // Where is first occurrence of pattern? defaults to end.
101 _DifferenceType __result = (__end1 - __begin1);
102 _DifferenceType *__splitters;
103
104 // Pattern too long.
105 if (__input_length < 0)
106 return __end1;
107
108 omp_lock_t __result_lock;
109 omp_init_lock(&__result_lock);
110
111 _ThreadIndex __num_threads =
112 std::max<_DifferenceType>(1,
113 std::min<_DifferenceType>(__input_length, __get_max_threads()));
114
115 _DifferenceType __advances[__pattern_length];
116 __calc_borders(__begin2, __pattern_length, __advances);
117
118 # pragma omp parallel num_threads(__num_threads)
119 {
120 # pragma omp single
121 {
122 __num_threads = omp_get_num_threads();
123 __splitters = new _DifferenceType[__num_threads + 1];
124 equally_split(__input_length, __num_threads, __splitters);
125 }
126
127 _ThreadIndex __iam = omp_get_thread_num();
128
129 _DifferenceType __start = __splitters[__iam],
130 __stop = __splitters[__iam + 1];
131
132 _DifferenceType __pos_in_pattern = 0;
133 bool __found_pattern = false;
134
135 while (__start <= __stop && !__found_pattern)
136 {
137 // Get new value of result.
138 #pragma omp flush(__result)
139 // No chance for this thread to find first occurrence.
140 if (__result < __start)
141 break;
142 while (__pred(__begin1[__start + __pos_in_pattern],
143 __begin2[__pos_in_pattern]))
144 {
145 ++__pos_in_pattern;
146 if (__pos_in_pattern == __pattern_length)
147 {
148 // Found new candidate for result.
149 omp_set_lock(&__result_lock);
150 __result = std::min(__result, __start);
151 omp_unset_lock(&__result_lock);
152
153 __found_pattern = true;
154 break;
155 }
156 }
157 // Make safe jump.
158 __start += (__pos_in_pattern - __advances[__pos_in_pattern]);
159 __pos_in_pattern =
160 (__advances[__pos_in_pattern] < 0) ?
161 0 : __advances[__pos_in_pattern];
162 }
163 } //parallel
164
165 omp_destroy_lock(&__result_lock);
166
167 delete[] __splitters;
168
169 // Return iterator on found element.
170 return (__begin1 + __result);
171 }
172 } // end namespace
173
174 #endif /* _GLIBCXX_PARALLEL_SEARCH_H */
This page took 0.043834 seconds and 4 git commands to generate.