search.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. // -*- C++ -*-
  2. // Copyright (C) 2007, 2008, 2009 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/search.h
  21. * @brief Parallel implementation base for std::search() and
  22. * std::search_n().
  23. * This file is a GNU parallel extension to the Standard C++ Library.
  24. */
  25. // Written by Felix Putze.
  26. #ifndef _GLIBCXX_PARALLEL_SEARCH_H
  27. #define _GLIBCXX_PARALLEL_SEARCH_H 1
  28. #include <bits/stl_algobase.h>
  29. #include <parallel/parallel.h>
  30. #include <parallel/equally_split.h>
  31. namespace __gnu_parallel
  32. {
  33. /**
  34. * @brief Precalculate advances for Knuth-Morris-Pratt algorithm.
  35. * @param elements Begin iterator of sequence to search for.
  36. * @param length Length of sequence to search for.
  37. * @param advances Returned offsets.
  38. */
  39. template<typename RandomAccessIterator, typename _DifferenceTp>
  40. void
  41. calc_borders(RandomAccessIterator elements, _DifferenceTp length,
  42. _DifferenceTp* off)
  43. {
  44. typedef _DifferenceTp difference_type;
  45. off[0] = -1;
  46. if (length > 1)
  47. off[1] = 0;
  48. difference_type k = 0;
  49. for (difference_type j = 2; j <= length; j++)
  50. {
  51. while ((k >= 0) && !(elements[k] == elements[j-1]))
  52. k = off[k];
  53. off[j] = ++k;
  54. }
  55. }
  56. // Generic parallel find algorithm (requires random access iterator).
  57. /** @brief Parallel std::search.
  58. * @param begin1 Begin iterator of first sequence.
  59. * @param end1 End iterator of first sequence.
  60. * @param begin2 Begin iterator of second sequence.
  61. * @param end2 End iterator of second sequence.
  62. * @param pred Find predicate.
  63. * @return Place of finding in first sequences. */
  64. template<typename _RandomAccessIterator1,
  65. typename _RandomAccessIterator2,
  66. typename Pred>
  67. _RandomAccessIterator1
  68. search_template(_RandomAccessIterator1 begin1, _RandomAccessIterator1 end1,
  69. _RandomAccessIterator2 begin2, _RandomAccessIterator2 end2,
  70. Pred pred)
  71. {
  72. typedef std::iterator_traits<_RandomAccessIterator1> traits_type;
  73. typedef typename traits_type::difference_type difference_type;
  74. _GLIBCXX_CALL((end1 - begin1) + (end2 - begin2));
  75. difference_type pattern_length = end2 - begin2;
  76. // Pattern too short.
  77. if(pattern_length <= 0)
  78. return end1;
  79. // Last point to start search.
  80. difference_type input_length = (end1 - begin1) - pattern_length;
  81. // Where is first occurrence of pattern? defaults to end.
  82. difference_type result = (end1 - begin1);
  83. difference_type *splitters;
  84. // Pattern too long.
  85. if (input_length < 0)
  86. return end1;
  87. omp_lock_t result_lock;
  88. omp_init_lock(&result_lock);
  89. thread_index_t num_threads =
  90. std::max<difference_type>(1,
  91. std::min<difference_type>(input_length, get_max_threads()));
  92. difference_type advances[pattern_length];
  93. calc_borders(begin2, pattern_length, advances);
  94. # pragma omp parallel num_threads(num_threads)
  95. {
  96. # pragma omp single
  97. {
  98. num_threads = omp_get_num_threads();
  99. splitters = new difference_type[num_threads + 1];
  100. equally_split(input_length, num_threads, splitters);
  101. }
  102. thread_index_t iam = omp_get_thread_num();
  103. difference_type start = splitters[iam], stop = splitters[iam + 1];
  104. difference_type pos_in_pattern = 0;
  105. bool found_pattern = false;
  106. while (start <= stop && !found_pattern)
  107. {
  108. // Get new value of result.
  109. #pragma omp flush(result)
  110. // No chance for this thread to find first occurrence.
  111. if (result < start)
  112. break;
  113. while (pred(begin1[start + pos_in_pattern],
  114. begin2[pos_in_pattern]))
  115. {
  116. ++pos_in_pattern;
  117. if (pos_in_pattern == pattern_length)
  118. {
  119. // Found new candidate for result.
  120. omp_set_lock(&result_lock);
  121. result = std::min(result, start);
  122. omp_unset_lock(&result_lock);
  123. found_pattern = true;
  124. break;
  125. }
  126. }
  127. // Make safe jump.
  128. start += (pos_in_pattern - advances[pos_in_pattern]);
  129. pos_in_pattern =
  130. (advances[pos_in_pattern] < 0) ? 0 : advances[pos_in_pattern];
  131. }
  132. } //parallel
  133. omp_destroy_lock(&result_lock);
  134. delete[] splitters;
  135. // Return iterator on found element.
  136. return (begin1 + result);
  137. }
  138. } // end namespace
  139. #endif /* _GLIBCXX_PARALLEL_SEARCH_H */