find.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  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/find.h
  21. * @brief Parallel implementation base for std::find(), std::equal()
  22. * and related functions.
  23. * This file is a GNU parallel extension to the Standard C++ Library.
  24. */
  25. // Written by Felix Putze and Johannes Singler.
  26. #ifndef _GLIBCXX_PARALLEL_FIND_H
  27. #define _GLIBCXX_PARALLEL_FIND_H 1
  28. #include <bits/stl_algobase.h>
  29. #include <parallel/features.h>
  30. #include <parallel/parallel.h>
  31. #include <parallel/compatibility.h>
  32. #include <parallel/equally_split.h>
  33. namespace __gnu_parallel
  34. {
  35. /**
  36. * @brief Parallel std::find, switch for different algorithms.
  37. * @param begin1 Begin iterator of first sequence.
  38. * @param end1 End iterator of first sequence.
  39. * @param begin2 Begin iterator of second sequence. Must have same
  40. * length as first sequence.
  41. * @param pred Find predicate.
  42. * @param selector Functionality (e. g. std::find_if (), std::equal(),...)
  43. * @return Place of finding in both sequences.
  44. */
  45. template<typename RandomAccessIterator1,
  46. typename RandomAccessIterator2,
  47. typename Pred,
  48. typename Selector>
  49. inline std::pair<RandomAccessIterator1, RandomAccessIterator2>
  50. find_template(RandomAccessIterator1 begin1, RandomAccessIterator1 end1,
  51. RandomAccessIterator2 begin2, Pred pred, Selector selector)
  52. {
  53. switch (_Settings::get().find_algorithm)
  54. {
  55. case GROWING_BLOCKS:
  56. return find_template(begin1, end1, begin2, pred, selector,
  57. growing_blocks_tag());
  58. case CONSTANT_SIZE_BLOCKS:
  59. return find_template(begin1, end1, begin2, pred, selector,
  60. constant_size_blocks_tag());
  61. case EQUAL_SPLIT:
  62. return find_template(begin1, end1, begin2, pred, selector,
  63. equal_split_tag());
  64. default:
  65. _GLIBCXX_PARALLEL_ASSERT(false);
  66. return std::make_pair(begin1, begin2);
  67. }
  68. }
  69. #if _GLIBCXX_FIND_EQUAL_SPLIT
  70. /**
  71. * @brief Parallel std::find, equal splitting variant.
  72. * @param begin1 Begin iterator of first sequence.
  73. * @param end1 End iterator of first sequence.
  74. * @param begin2 Begin iterator of second sequence. Second sequence
  75. * must have same length as first sequence.
  76. * @param pred Find predicate.
  77. * @param selector Functionality (e. g. std::find_if (), std::equal(),...)
  78. * @return Place of finding in both sequences.
  79. */
  80. template<typename RandomAccessIterator1,
  81. typename RandomAccessIterator2,
  82. typename Pred,
  83. typename Selector>
  84. std::pair<RandomAccessIterator1, RandomAccessIterator2>
  85. find_template(RandomAccessIterator1 begin1,
  86. RandomAccessIterator1 end1,
  87. RandomAccessIterator2 begin2,
  88. Pred pred,
  89. Selector selector,
  90. equal_split_tag)
  91. {
  92. _GLIBCXX_CALL(end1 - begin1)
  93. typedef std::iterator_traits<RandomAccessIterator1> traits_type;
  94. typedef typename traits_type::difference_type difference_type;
  95. typedef typename traits_type::value_type value_type;
  96. difference_type length = end1 - begin1;
  97. difference_type result = length;
  98. difference_type* borders;
  99. omp_lock_t result_lock;
  100. omp_init_lock(&result_lock);
  101. thread_index_t num_threads = get_max_threads();
  102. # pragma omp parallel num_threads(num_threads)
  103. {
  104. # pragma omp single
  105. {
  106. num_threads = omp_get_num_threads();
  107. borders = new difference_type[num_threads + 1];
  108. equally_split(length, num_threads, borders);
  109. } //single
  110. thread_index_t iam = omp_get_thread_num();
  111. difference_type start = borders[iam], stop = borders[iam + 1];
  112. RandomAccessIterator1 i1 = begin1 + start;
  113. RandomAccessIterator2 i2 = begin2 + start;
  114. for (difference_type pos = start; pos < stop; ++pos)
  115. {
  116. #pragma omp flush(result)
  117. // Result has been set to something lower.
  118. if (result < pos)
  119. break;
  120. if (selector(i1, i2, pred))
  121. {
  122. omp_set_lock(&result_lock);
  123. if (pos < result)
  124. result = pos;
  125. omp_unset_lock(&result_lock);
  126. break;
  127. }
  128. ++i1;
  129. ++i2;
  130. }
  131. } //parallel
  132. omp_destroy_lock(&result_lock);
  133. delete[] borders;
  134. return
  135. std::pair<RandomAccessIterator1, RandomAccessIterator2>(begin1 + result,
  136. begin2 + result);
  137. }
  138. #endif
  139. #if _GLIBCXX_FIND_GROWING_BLOCKS
  140. /**
  141. * @brief Parallel std::find, growing block size variant.
  142. * @param begin1 Begin iterator of first sequence.
  143. * @param end1 End iterator of first sequence.
  144. * @param begin2 Begin iterator of second sequence. Second sequence
  145. * must have same length as first sequence.
  146. * @param pred Find predicate.
  147. * @param selector Functionality (e. g. std::find_if (), std::equal(),...)
  148. * @return Place of finding in both sequences.
  149. * @see __gnu_parallel::_Settings::find_sequential_search_size
  150. * @see __gnu_parallel::_Settings::find_initial_block_size
  151. * @see __gnu_parallel::_Settings::find_maximum_block_size
  152. * @see __gnu_parallel::_Settings::find_increasing_factor
  153. *
  154. * There are two main differences between the growing blocks and
  155. * the constant-size blocks variants.
  156. * 1. For GB, the block size grows; for CSB, the block size is fixed.
  157. * 2. For GB, the blocks are allocated dynamically;
  158. * for CSB, the blocks are allocated in a predetermined manner,
  159. * namely spacial round-robin.
  160. */
  161. template<typename RandomAccessIterator1,
  162. typename RandomAccessIterator2,
  163. typename Pred,
  164. typename Selector>
  165. std::pair<RandomAccessIterator1, RandomAccessIterator2>
  166. find_template(RandomAccessIterator1 begin1, RandomAccessIterator1 end1,
  167. RandomAccessIterator2 begin2, Pred pred, Selector selector,
  168. growing_blocks_tag)
  169. {
  170. _GLIBCXX_CALL(end1 - begin1)
  171. typedef std::iterator_traits<RandomAccessIterator1> traits_type;
  172. typedef typename traits_type::difference_type difference_type;
  173. typedef typename traits_type::value_type value_type;
  174. const _Settings& __s = _Settings::get();
  175. difference_type length = end1 - begin1;
  176. difference_type sequential_search_size =
  177. std::min<difference_type>(length, __s.find_sequential_search_size);
  178. // Try it sequentially first.
  179. std::pair<RandomAccessIterator1, RandomAccessIterator2> find_seq_result =
  180. selector.sequential_algorithm(
  181. begin1, begin1 + sequential_search_size, begin2, pred);
  182. if (find_seq_result.first != (begin1 + sequential_search_size))
  183. return find_seq_result;
  184. // Index of beginning of next free block (after sequential find).
  185. difference_type next_block_start = sequential_search_size;
  186. difference_type result = length;
  187. omp_lock_t result_lock;
  188. omp_init_lock(&result_lock);
  189. thread_index_t num_threads = get_max_threads();
  190. # pragma omp parallel shared(result) num_threads(num_threads)
  191. {
  192. # pragma omp single
  193. num_threads = omp_get_num_threads();
  194. // Not within first k elements -> start parallel.
  195. thread_index_t iam = omp_get_thread_num();
  196. difference_type block_size = __s.find_initial_block_size;
  197. difference_type start =
  198. fetch_and_add<difference_type>(&next_block_start, block_size);
  199. // Get new block, update pointer to next block.
  200. difference_type stop =
  201. std::min<difference_type>(length, start + block_size);
  202. std::pair<RandomAccessIterator1, RandomAccessIterator2> local_result;
  203. while (start < length)
  204. {
  205. # pragma omp flush(result)
  206. // Get new value of result.
  207. if (result < start)
  208. {
  209. // No chance to find first element.
  210. break;
  211. }
  212. local_result = selector.sequential_algorithm(
  213. begin1 + start, begin1 + stop, begin2 + start, pred);
  214. if (local_result.first != (begin1 + stop))
  215. {
  216. omp_set_lock(&result_lock);
  217. if ((local_result.first - begin1) < result)
  218. {
  219. result = local_result.first - begin1;
  220. // Result cannot be in future blocks, stop algorithm.
  221. fetch_and_add<difference_type>(&next_block_start, length);
  222. }
  223. omp_unset_lock(&result_lock);
  224. }
  225. block_size =
  226. std::min<difference_type>(block_size * __s.find_increasing_factor,
  227. __s.find_maximum_block_size);
  228. // Get new block, update pointer to next block.
  229. start =
  230. fetch_and_add<difference_type>(&next_block_start, block_size);
  231. stop = ((length < (start + block_size))
  232. ? length : (start + block_size));
  233. }
  234. } //parallel
  235. omp_destroy_lock(&result_lock);
  236. // Return iterator on found element.
  237. return
  238. std::pair<RandomAccessIterator1, RandomAccessIterator2>(begin1 + result,
  239. begin2 + result);
  240. }
  241. #endif
  242. #if _GLIBCXX_FIND_CONSTANT_SIZE_BLOCKS
  243. /**
  244. * @brief Parallel std::find, constant block size variant.
  245. * @param begin1 Begin iterator of first sequence.
  246. * @param end1 End iterator of first sequence.
  247. * @param begin2 Begin iterator of second sequence. Second sequence
  248. * must have same length as first sequence.
  249. * @param pred Find predicate.
  250. * @param selector Functionality (e. g. std::find_if (), std::equal(),...)
  251. * @return Place of finding in both sequences.
  252. * @see __gnu_parallel::_Settings::find_sequential_search_size
  253. * @see __gnu_parallel::_Settings::find_block_size
  254. * There are two main differences between the growing blocks and the
  255. * constant-size blocks variants.
  256. * 1. For GB, the block size grows; for CSB, the block size is fixed.
  257. * 2. For GB, the blocks are allocated dynamically; for CSB, the
  258. * blocks are allocated in a predetermined manner, namely spacial
  259. * round-robin.
  260. */
  261. template<typename RandomAccessIterator1,
  262. typename RandomAccessIterator2,
  263. typename Pred,
  264. typename Selector>
  265. std::pair<RandomAccessIterator1, RandomAccessIterator2>
  266. find_template(RandomAccessIterator1 begin1, RandomAccessIterator1 end1,
  267. RandomAccessIterator2 begin2, Pred pred, Selector selector,
  268. constant_size_blocks_tag)
  269. {
  270. _GLIBCXX_CALL(end1 - begin1)
  271. typedef std::iterator_traits<RandomAccessIterator1> traits_type;
  272. typedef typename traits_type::difference_type difference_type;
  273. typedef typename traits_type::value_type value_type;
  274. const _Settings& __s = _Settings::get();
  275. difference_type length = end1 - begin1;
  276. difference_type sequential_search_size = std::min<difference_type>(
  277. length, __s.find_sequential_search_size);
  278. // Try it sequentially first.
  279. std::pair<RandomAccessIterator1, RandomAccessIterator2> find_seq_result =
  280. selector.sequential_algorithm(begin1, begin1 + sequential_search_size,
  281. begin2, pred);
  282. if (find_seq_result.first != (begin1 + sequential_search_size))
  283. return find_seq_result;
  284. difference_type result = length;
  285. omp_lock_t result_lock;
  286. omp_init_lock(&result_lock);
  287. // Not within first sequential_search_size elements -> start parallel.
  288. thread_index_t num_threads = get_max_threads();
  289. # pragma omp parallel shared(result) num_threads(num_threads)
  290. {
  291. # pragma omp single
  292. num_threads = omp_get_num_threads();
  293. thread_index_t iam = omp_get_thread_num();
  294. difference_type block_size = __s.find_initial_block_size;
  295. // First element of thread's current iteration.
  296. difference_type iteration_start = sequential_search_size;
  297. // Where to work (initialization).
  298. difference_type start = iteration_start + iam * block_size;
  299. difference_type stop =
  300. std::min<difference_type>(length, start + block_size);
  301. std::pair<RandomAccessIterator1, RandomAccessIterator2> local_result;
  302. while (start < length)
  303. {
  304. // Get new value of result.
  305. # pragma omp flush(result)
  306. // No chance to find first element.
  307. if (result < start)
  308. break;
  309. local_result = selector.sequential_algorithm(
  310. begin1 + start, begin1 + stop,
  311. begin2 + start, pred);
  312. if (local_result.first != (begin1 + stop))
  313. {
  314. omp_set_lock(&result_lock);
  315. if ((local_result.first - begin1) < result)
  316. result = local_result.first - begin1;
  317. omp_unset_lock(&result_lock);
  318. // Will not find better value in its interval.
  319. break;
  320. }
  321. iteration_start += num_threads * block_size;
  322. // Where to work.
  323. start = iteration_start + iam * block_size;
  324. stop = std::min<difference_type>(length, start + block_size);
  325. }
  326. } //parallel
  327. omp_destroy_lock(&result_lock);
  328. // Return iterator on found element.
  329. return
  330. std::pair<RandomAccessIterator1, RandomAccessIterator2>(begin1 + result,
  331. begin2 + result);
  332. }
  333. #endif
  334. } // end namespace
  335. #endif /* _GLIBCXX_PARALLEL_FIND_H */