OpenCores
URL https://opencores.org/ocsvn/openrisc_me/openrisc_me/trunk

Subversion Repositories openrisc_me

[/] [openrisc/] [trunk/] [gnu-src/] [gcc-4.5.1/] [libstdc++-v3/] [include/] [parallel/] [find.h] - Blame information for rev 424

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 424 jeremybenn
// -*- 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/find.h
26
 *  @brief Parallel implementation base for std::find(), std::equal()
27
 *  and related functions.
28
 *  This file is a GNU parallel extension to the Standard C++ Library.
29
 */
30
 
31
// Written by Felix Putze and Johannes Singler.
32
 
33
#ifndef _GLIBCXX_PARALLEL_FIND_H
34
#define _GLIBCXX_PARALLEL_FIND_H 1
35
 
36
#include <bits/stl_algobase.h>
37
 
38
#include <parallel/features.h>
39
#include <parallel/parallel.h>
40
#include <parallel/compatibility.h>
41
#include <parallel/equally_split.h>
42
 
43
namespace __gnu_parallel
44
{
45
  /**
46
   *  @brief Parallel std::find, switch for different algorithms.
47
   *  @param __begin1 Begin iterator of first sequence.
48
   *  @param __end1 End iterator of first sequence.
49
   *  @param __begin2 Begin iterator of second sequence. Must have same
50
   *  length as first sequence.
51
   *  @param __pred Find predicate.
52
   *  @param __selector _Functionality (e. g. std::find_if(), std::equal(),...)
53
   *  @return Place of finding in both sequences.
54
   */
55
  template<typename _RAIter1,
56
           typename _RAIter2,
57
           typename _Pred,
58
           typename _Selector>
59
    inline std::pair<_RAIter1, _RAIter2>
60
    __find_template(_RAIter1 __begin1, _RAIter1 __end1,
61
                    _RAIter2 __begin2, _Pred __pred, _Selector __selector)
62
    {
63
      switch (_Settings::get().find_algorithm)
64
        {
65
        case GROWING_BLOCKS:
66
          return __find_template(__begin1, __end1, __begin2, __pred,
67
                                 __selector, growing_blocks_tag());
68
        case CONSTANT_SIZE_BLOCKS:
69
          return __find_template(__begin1, __end1, __begin2, __pred,
70
                                 __selector, constant_size_blocks_tag());
71
        case EQUAL_SPLIT:
72
          return __find_template(__begin1, __end1, __begin2, __pred,
73
                                 __selector, equal_split_tag());
74
        default:
75
          _GLIBCXX_PARALLEL_ASSERT(false);
76
          return std::make_pair(__begin1, __begin2);
77
        }
78
    }
79
 
80
#if _GLIBCXX_FIND_EQUAL_SPLIT
81
 
82
  /**
83
   *  @brief Parallel std::find, equal splitting variant.
84
   *  @param __begin1 Begin iterator of first sequence.
85
   *  @param __end1 End iterator of first sequence.
86
   *  @param __begin2 Begin iterator of second sequence. Second __sequence
87
   *  must have same length as first sequence.
88
   *  @param __pred Find predicate.
89
   *  @param __selector _Functionality (e. g. std::find_if(), std::equal(),...)
90
   *  @return Place of finding in both sequences.
91
   */
92
  template<typename _RAIter1,
93
           typename _RAIter2,
94
           typename _Pred,
95
           typename _Selector>
96
    std::pair<_RAIter1, _RAIter2>
97
    __find_template(_RAIter1 __begin1, _RAIter1 __end1,
98
                    _RAIter2 __begin2, _Pred __pred,
99
                    _Selector __selector, equal_split_tag)
100
    {
101
      _GLIBCXX_CALL(__end1 - __begin1)
102
 
103
      typedef std::iterator_traits<_RAIter1> _TraitsType;
104
      typedef typename _TraitsType::difference_type _DifferenceType;
105
      typedef typename _TraitsType::value_type _ValueType;
106
 
107
      _DifferenceType __length = __end1 - __begin1;
108
      _DifferenceType __result = __length;
109
      _DifferenceType* __borders;
110
 
111
      omp_lock_t __result_lock;
112
      omp_init_lock(&__result_lock);
113
 
114
      _ThreadIndex __num_threads = __get_max_threads();
115
#     pragma omp parallel num_threads(__num_threads)
116
      {
117
#     pragma omp single
118
        {
119
          __num_threads = omp_get_num_threads();
120
          __borders = new _DifferenceType[__num_threads + 1];
121
          equally_split(__length, __num_threads, __borders);
122
        } //single
123
 
124
        _ThreadIndex __iam = omp_get_thread_num();
125
        _DifferenceType __start = __borders[__iam],
126
                         __stop = __borders[__iam + 1];
127
 
128
        _RAIter1 __i1 = __begin1 + __start;
129
        _RAIter2 __i2 = __begin2 + __start;
130
        for (_DifferenceType __pos = __start; __pos < __stop; ++__pos)
131
          {
132
#           pragma omp flush(__result)
133
            // Result has been set to something lower.
134
            if (__result < __pos)
135
              break;
136
 
137
            if (__selector(__i1, __i2, __pred))
138
              {
139
                omp_set_lock(&__result_lock);
140
                if (__pos < __result)
141
                  __result = __pos;
142
                omp_unset_lock(&__result_lock);
143
                break;
144
              }
145
            ++__i1;
146
            ++__i2;
147
          }
148
      } //parallel
149
 
150
      omp_destroy_lock(&__result_lock);
151
      delete[] __borders;
152
 
153
      return std::pair<_RAIter1, _RAIter2>(__begin1 + __result,
154
                                           __begin2 + __result);
155
    }
156
 
157
#endif
158
 
159
#if _GLIBCXX_FIND_GROWING_BLOCKS
160
 
161
  /**
162
   *  @brief Parallel std::find, growing block size variant.
163
   *  @param __begin1 Begin iterator of first sequence.
164
   *  @param __end1 End iterator of first sequence.
165
   *  @param __begin2 Begin iterator of second sequence. Second __sequence
166
   *  must have same length as first sequence.
167
   *  @param __pred Find predicate.
168
   *  @param __selector _Functionality (e. g. std::find_if(), std::equal(),...)
169
   *  @return Place of finding in both sequences.
170
   *  @see __gnu_parallel::_Settings::find_sequential_search_size
171
   *  @see __gnu_parallel::_Settings::find_initial_block_size
172
   *  @see __gnu_parallel::_Settings::find_maximum_block_size
173
   *  @see __gnu_parallel::_Settings::find_increasing_factor
174
   *
175
   *  There are two main differences between the growing blocks and
176
   *  the constant-size blocks variants.
177
   *  1. For GB, the block size grows; for CSB, the block size is fixed.
178
   *  2. For GB, the blocks are allocated dynamically;
179
   *     for CSB, the blocks are allocated in a predetermined manner,
180
   *     namely spacial round-robin.
181
   */
182
  template<typename _RAIter1,
183
           typename _RAIter2,
184
           typename _Pred,
185
           typename _Selector>
186
    std::pair<_RAIter1, _RAIter2>
187
    __find_template(_RAIter1 __begin1, _RAIter1 __end1,
188
                    _RAIter2 __begin2, _Pred __pred, _Selector __selector,
189
                    growing_blocks_tag)
190
    {
191
      _GLIBCXX_CALL(__end1 - __begin1)
192
 
193
      typedef std::iterator_traits<_RAIter1> _TraitsType;
194
      typedef typename _TraitsType::difference_type _DifferenceType;
195
      typedef typename _TraitsType::value_type _ValueType;
196
 
197
      const _Settings& __s = _Settings::get();
198
 
199
      _DifferenceType __length = __end1 - __begin1;
200
 
201
      _DifferenceType
202
        __sequential_search_size = std::min<_DifferenceType>
203
        (__length, __s.find_sequential_search_size);
204
 
205
      // Try it sequentially first.
206
      std::pair<_RAIter1, _RAIter2>
207
        __find_seq_result = __selector._M_sequential_algorithm
208
        (__begin1, __begin1 + __sequential_search_size,
209
         __begin2, __pred);
210
 
211
      if (__find_seq_result.first != (__begin1 + __sequential_search_size))
212
        return __find_seq_result;
213
 
214
      // Index of beginning of next free block (after sequential find).
215
      _DifferenceType __next_block_start = __sequential_search_size;
216
      _DifferenceType __result = __length;
217
 
218
      omp_lock_t __result_lock;
219
      omp_init_lock(&__result_lock);
220
 
221
      _ThreadIndex __num_threads = __get_max_threads();
222
#     pragma omp parallel shared(__result) num_threads(__num_threads)
223
      {
224
#       pragma omp single
225
        __num_threads = omp_get_num_threads();
226
 
227
        // Not within first __k elements -> start parallel.
228
        _ThreadIndex __iam = omp_get_thread_num();
229
 
230
        _DifferenceType __block_size = __s.find_initial_block_size;
231
        _DifferenceType __start = __fetch_and_add<_DifferenceType>
232
          (&__next_block_start, __block_size);
233
 
234
        // Get new block, update pointer to next block.
235
        _DifferenceType __stop =
236
          std::min<_DifferenceType>(__length, __start + __block_size);
237
 
238
        std::pair<_RAIter1, _RAIter2> __local_result;
239
 
240
        while (__start < __length)
241
          {
242
#           pragma omp flush(__result)
243
            // Get new value of result.
244
            if (__result < __start)
245
              {
246
                // No chance to find first element.
247
                break;
248
              }
249
 
250
            __local_result = __selector._M_sequential_algorithm
251
              (__begin1 + __start, __begin1 + __stop,
252
               __begin2 + __start, __pred);
253
 
254
            if (__local_result.first != (__begin1 + __stop))
255
              {
256
                omp_set_lock(&__result_lock);
257
                if ((__local_result.first - __begin1) < __result)
258
                  {
259
                    __result = __local_result.first - __begin1;
260
 
261
                    // Result cannot be in future blocks, stop algorithm.
262
                    __fetch_and_add<_DifferenceType>(&__next_block_start,
263
                                                     __length);
264
                  }
265
                omp_unset_lock(&__result_lock);
266
              }
267
 
268
            __block_size = std::min<_DifferenceType>
269
              (__block_size * __s.find_increasing_factor,
270
               __s.find_maximum_block_size);
271
 
272
            // Get new block, update pointer to next block.
273
            __start = __fetch_and_add<_DifferenceType>(&__next_block_start,
274
                                                       __block_size);
275
            __stop = (__length < (__start + __block_size)
276
                      ? __length : (__start + __block_size));
277
          }
278
      } //parallel
279
 
280
      omp_destroy_lock(&__result_lock);
281
 
282
      // Return iterator on found element.
283
      return
284
        std::pair<_RAIter1, _RAIter2>(__begin1 + __result,
285
                                      __begin2 + __result);
286
    }
287
 
288
#endif
289
 
290
#if _GLIBCXX_FIND_CONSTANT_SIZE_BLOCKS
291
 
292
  /**
293
   *   @brief Parallel std::find, constant block size variant.
294
   *  @param __begin1 Begin iterator of first sequence.
295
   *  @param __end1 End iterator of first sequence.
296
   *  @param __begin2 Begin iterator of second sequence. Second __sequence
297
   *  must have same length as first sequence.
298
   *  @param __pred Find predicate.
299
   *  @param __selector _Functionality (e. g. std::find_if(), std::equal(),...)
300
   *  @return Place of finding in both sequences.
301
   *  @see __gnu_parallel::_Settings::find_sequential_search_size
302
   *  @see __gnu_parallel::_Settings::find_block_size
303
   *  There are two main differences between the growing blocks and the
304
   *  constant-size blocks variants.
305
   *  1. For GB, the block size grows; for CSB, the block size is fixed.
306
   *  2. For GB, the blocks are allocated dynamically; for CSB, the
307
   *  blocks are allocated in a predetermined manner, namely spacial
308
   *  round-robin.
309
   */
310
  template<typename _RAIter1,
311
           typename _RAIter2,
312
           typename _Pred,
313
           typename _Selector>
314
    std::pair<_RAIter1, _RAIter2>
315
    __find_template(_RAIter1 __begin1, _RAIter1 __end1,
316
                  _RAIter2 __begin2, _Pred __pred, _Selector __selector,
317
                  constant_size_blocks_tag)
318
    {
319
      _GLIBCXX_CALL(__end1 - __begin1)
320
      typedef std::iterator_traits<_RAIter1> _TraitsType;
321
      typedef typename _TraitsType::difference_type _DifferenceType;
322
      typedef typename _TraitsType::value_type _ValueType;
323
 
324
      const _Settings& __s = _Settings::get();
325
 
326
      _DifferenceType __length = __end1 - __begin1;
327
 
328
      _DifferenceType __sequential_search_size = std::min<_DifferenceType>
329
        (__length, __s.find_sequential_search_size);
330
 
331
      // Try it sequentially first.
332
      std::pair<_RAIter1, _RAIter2>
333
        __find_seq_result = __selector._M_sequential_algorithm
334
        (__begin1, __begin1 + __sequential_search_size, __begin2, __pred);
335
 
336
      if (__find_seq_result.first != (__begin1 + __sequential_search_size))
337
        return __find_seq_result;
338
 
339
      _DifferenceType __result = __length;
340
      omp_lock_t __result_lock;
341
      omp_init_lock(&__result_lock);
342
 
343
      // Not within first __sequential_search_size elements -> start parallel.
344
 
345
      _ThreadIndex __num_threads = __get_max_threads();
346
#     pragma omp parallel shared(__result) num_threads(__num_threads)
347
      {
348
#       pragma omp single
349
        __num_threads = omp_get_num_threads();
350
 
351
        _ThreadIndex __iam = omp_get_thread_num();
352
        _DifferenceType __block_size = __s.find_initial_block_size;
353
 
354
        // First element of thread's current iteration.
355
        _DifferenceType __iteration_start = __sequential_search_size;
356
 
357
        // Where to work (initialization).
358
        _DifferenceType __start = __iteration_start + __iam * __block_size;
359
        _DifferenceType __stop = std::min<_DifferenceType>(__length,
360
                                                           __start
361
                                                           + __block_size);
362
 
363
        std::pair<_RAIter1, _RAIter2> __local_result;
364
 
365
        while (__start < __length)
366
          {
367
            // Get new value of result.
368
#           pragma omp flush(__result)
369
            // No chance to find first element.
370
            if (__result < __start)
371
              break;
372
 
373
            __local_result = __selector._M_sequential_algorithm
374
              (__begin1 + __start, __begin1 + __stop,
375
               __begin2 + __start, __pred);
376
 
377
            if (__local_result.first != (__begin1 + __stop))
378
              {
379
                omp_set_lock(&__result_lock);
380
                if ((__local_result.first - __begin1) < __result)
381
                  __result = __local_result.first - __begin1;
382
                omp_unset_lock(&__result_lock);
383
                // Will not find better value in its interval.
384
                break;
385
              }
386
 
387
            __iteration_start += __num_threads * __block_size;
388
 
389
            // Where to work.
390
            __start = __iteration_start + __iam * __block_size;
391
            __stop = std::min<_DifferenceType>(__length,
392
                                               __start + __block_size);
393
          }
394
      } //parallel
395
 
396
      omp_destroy_lock(&__result_lock);
397
 
398
      // Return iterator on found element.
399
      return std::pair<_RAIter1, _RAIter2>(__begin1 + __result,
400
                                           __begin2 + __result);
401
    }
402
#endif
403
} // end namespace
404
 
405
#endif /* _GLIBCXX_PARALLEL_FIND_H */

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.