Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

343 rindas
12KB

  1. // -*- C++ -*-
  2. // Copyright (C) 2007-2020 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/settings.h
  21. * @brief Runtime settings and tuning parameters, heuristics to decide
  22. * whether to use parallelized algorithms.
  23. *
  24. * This file is a GNU parallel extension to the Standard C++ Library.
  25. *
  26. * @section parallelization_decision Deciding whether to run an algorithm in parallel.
  27. *
  28. * There are several ways the user can switch on and off the parallel
  29. * execution of an algorithm, both at compile- and run-time.
  30. *
  31. * Only sequential execution can be forced at compile-time. This
  32. * reduces code size and protects code parts that have
  33. * non-thread-safe side effects.
  34. *
  35. * Ultimately, forcing parallel execution at compile-time makes
  36. * sense. Often, the sequential algorithm implementation is used as
  37. * a subroutine, so no reduction in code size can be achieved. Also,
  38. * the machine the program is run on might have only one processor
  39. * core, so to avoid overhead, the algorithm is executed
  40. * sequentially.
  41. *
  42. * To force sequential execution of an algorithm ultimately at
  43. * compile-time, the user must add the tag
  44. * gnu_parallel::sequential_tag() to the end of the parameter list,
  45. * e. g.
  46. *
  47. * \code
  48. * std::sort(__v.begin(), __v.end(), __gnu_parallel::sequential_tag());
  49. * \endcode
  50. *
  51. * This is compatible with all overloaded algorithm variants. No
  52. * additional code will be instantiated, at all. The same holds for
  53. * most algorithm calls with iterators not providing random access.
  54. *
  55. * If the algorithm call is not forced to be executed sequentially
  56. * at compile-time, the decision is made at run-time.
  57. * The global variable __gnu_parallel::_Settings::algorithm_strategy
  58. * is checked. It is a tristate variable corresponding to:
  59. * - a. force_sequential, meaning the sequential algorithm is executed.
  60. * - b. force_parallel, meaning the parallel algorithm is executed.
  61. * - c. heuristic
  62. *
  63. * For heuristic, the parallel algorithm implementation is called
  64. * only if the input size is sufficiently large. For most
  65. * algorithms, the input size is the (combined) length of the input
  66. * sequence(__s). The threshold can be set by the user, individually
  67. * for each algorithm. The according variables are called
  68. * gnu_parallel::_Settings::[algorithm]_minimal_n .
  69. *
  70. * For some of the algorithms, there are even more tuning options,
  71. * e. g. the ability to choose from multiple algorithm variants. See
  72. * below for details.
  73. */
  74. // Written by Johannes Singler and Felix Putze.
  75. #ifndef _GLIBCXX_PARALLEL_SETTINGS_H
  76. #define _GLIBCXX_PARALLEL_SETTINGS_H 1
  77. #include <parallel/types.h>
  78. /**
  79. * @brief Determine at compile(?)-time if the parallel variant of an
  80. * algorithm should be called.
  81. * @param __c A condition that is convertible to bool that is overruled by
  82. * __gnu_parallel::_Settings::algorithm_strategy. Usually a decision
  83. * based on the input size.
  84. */
  85. #define _GLIBCXX_PARALLEL_CONDITION(__c) \
  86. (__gnu_parallel::_Settings::get().algorithm_strategy \
  87. != __gnu_parallel::force_sequential \
  88. && ((__gnu_parallel::__get_max_threads() > 1 && (__c)) \
  89. || __gnu_parallel::_Settings::get().algorithm_strategy \
  90. == __gnu_parallel::force_parallel))
  91. /*
  92. inline bool
  93. parallel_condition(bool __c)
  94. {
  95. bool ret = false;
  96. const _Settings& __s = _Settings::get();
  97. if (__s.algorithm_strategy != force_seqential)
  98. {
  99. if (__s.algorithm_strategy == force_parallel)
  100. ret = true;
  101. else
  102. ret = __get_max_threads() > 1 && __c;
  103. }
  104. return ret;
  105. }
  106. */
  107. namespace __gnu_parallel
  108. {
  109. /// class _Settings
  110. /// Run-time settings for the parallel mode including all tunable parameters.
  111. struct _Settings
  112. {
  113. _AlgorithmStrategy algorithm_strategy;
  114. _SortAlgorithm sort_algorithm;
  115. _PartialSumAlgorithm partial_sum_algorithm;
  116. _MultiwayMergeAlgorithm multiway_merge_algorithm;
  117. _FindAlgorithm find_algorithm;
  118. _SplittingAlgorithm sort_splitting;
  119. _SplittingAlgorithm merge_splitting;
  120. _SplittingAlgorithm multiway_merge_splitting;
  121. // Per-algorithm settings.
  122. /// Minimal input size for accumulate.
  123. _SequenceIndex accumulate_minimal_n;
  124. /// Minimal input size for adjacent_difference.
  125. unsigned int adjacent_difference_minimal_n;
  126. /// Minimal input size for count and count_if.
  127. _SequenceIndex count_minimal_n;
  128. /// Minimal input size for fill.
  129. _SequenceIndex fill_minimal_n;
  130. /// Block size increase factor for find.
  131. double find_increasing_factor;
  132. /// Initial block size for find.
  133. _SequenceIndex find_initial_block_size;
  134. /// Maximal block size for find.
  135. _SequenceIndex find_maximum_block_size;
  136. /// Start with looking for this many elements sequentially, for find.
  137. _SequenceIndex find_sequential_search_size;
  138. /// Minimal input size for for_each.
  139. _SequenceIndex for_each_minimal_n;
  140. /// Minimal input size for generate.
  141. _SequenceIndex generate_minimal_n;
  142. /// Minimal input size for max_element.
  143. _SequenceIndex max_element_minimal_n;
  144. /// Minimal input size for merge.
  145. _SequenceIndex merge_minimal_n;
  146. /// Oversampling factor for merge.
  147. unsigned int merge_oversampling;
  148. /// Minimal input size for min_element.
  149. _SequenceIndex min_element_minimal_n;
  150. /// Minimal input size for multiway_merge.
  151. _SequenceIndex multiway_merge_minimal_n;
  152. /// Oversampling factor for multiway_merge.
  153. int multiway_merge_minimal_k;
  154. /// Oversampling factor for multiway_merge.
  155. unsigned int multiway_merge_oversampling;
  156. /// Minimal input size for nth_element.
  157. _SequenceIndex nth_element_minimal_n;
  158. /// Chunk size for partition.
  159. _SequenceIndex partition_chunk_size;
  160. /// Chunk size for partition, relative to input size. If > 0.0,
  161. /// this value overrides partition_chunk_size.
  162. double partition_chunk_share;
  163. /// Minimal input size for partition.
  164. _SequenceIndex partition_minimal_n;
  165. /// Minimal input size for partial_sort.
  166. _SequenceIndex partial_sort_minimal_n;
  167. /// Ratio for partial_sum. Assume "sum and write result" to be
  168. /// this factor slower than just "sum".
  169. float partial_sum_dilation;
  170. /// Minimal input size for partial_sum.
  171. unsigned int partial_sum_minimal_n;
  172. /// Minimal input size for random_shuffle.
  173. unsigned int random_shuffle_minimal_n;
  174. /// Minimal input size for replace and replace_if.
  175. _SequenceIndex replace_minimal_n;
  176. /// Minimal input size for set_difference.
  177. _SequenceIndex set_difference_minimal_n;
  178. /// Minimal input size for set_intersection.
  179. _SequenceIndex set_intersection_minimal_n;
  180. /// Minimal input size for set_symmetric_difference.
  181. _SequenceIndex set_symmetric_difference_minimal_n;
  182. /// Minimal input size for set_union.
  183. _SequenceIndex set_union_minimal_n;
  184. /// Minimal input size for parallel sorting.
  185. _SequenceIndex sort_minimal_n;
  186. /// Oversampling factor for parallel std::sort (MWMS).
  187. unsigned int sort_mwms_oversampling;
  188. /// Such many samples to take to find a good pivot (quicksort).
  189. unsigned int sort_qs_num_samples_preset;
  190. /// Maximal subsequence __length to switch to unbalanced __base case.
  191. /// Applies to std::sort with dynamically load-balanced quicksort.
  192. _SequenceIndex sort_qsb_base_case_maximal_n;
  193. /// Minimal input size for parallel std::transform.
  194. _SequenceIndex transform_minimal_n;
  195. /// Minimal input size for unique_copy.
  196. _SequenceIndex unique_copy_minimal_n;
  197. _SequenceIndex workstealing_chunk_size;
  198. // Hardware dependent tuning parameters.
  199. /// size of the L1 cache in bytes (underestimation).
  200. unsigned long long L1_cache_size;
  201. /// size of the L2 cache in bytes (underestimation).
  202. unsigned long long L2_cache_size;
  203. /// size of the Translation Lookaside Buffer (underestimation).
  204. unsigned int TLB_size;
  205. /// Overestimation of cache line size. Used to avoid false
  206. /// sharing, i.e. elements of different threads are at least this
  207. /// amount apart.
  208. unsigned int cache_line_size;
  209. // Statistics.
  210. /// The number of stolen ranges in load-balanced quicksort.
  211. _SequenceIndex qsb_steals;
  212. /// Minimal input size for search and search_n.
  213. _SequenceIndex search_minimal_n;
  214. /// Block size scale-down factor with respect to current position.
  215. float find_scale_factor;
  216. /// Get the global settings.
  217. _GLIBCXX_CONST static const _Settings&
  218. get() throw();
  219. /// Set the global settings.
  220. static void
  221. set(_Settings&) throw();
  222. explicit
  223. _Settings() :
  224. algorithm_strategy(heuristic),
  225. sort_algorithm(MWMS),
  226. partial_sum_algorithm(LINEAR),
  227. multiway_merge_algorithm(LOSER_TREE),
  228. find_algorithm(CONSTANT_SIZE_BLOCKS),
  229. sort_splitting(EXACT),
  230. merge_splitting(EXACT),
  231. multiway_merge_splitting(EXACT),
  232. accumulate_minimal_n(1000),
  233. adjacent_difference_minimal_n(1000),
  234. count_minimal_n(1000),
  235. fill_minimal_n(1000),
  236. find_increasing_factor(2.0),
  237. find_initial_block_size(256),
  238. find_maximum_block_size(8192),
  239. find_sequential_search_size(256),
  240. for_each_minimal_n(1000),
  241. generate_minimal_n(1000),
  242. max_element_minimal_n(1000),
  243. merge_minimal_n(1000),
  244. merge_oversampling(10),
  245. min_element_minimal_n(1000),
  246. multiway_merge_minimal_n(1000),
  247. multiway_merge_minimal_k(2), multiway_merge_oversampling(10),
  248. nth_element_minimal_n(1000),
  249. partition_chunk_size(1000),
  250. partition_chunk_share(0.0),
  251. partition_minimal_n(1000),
  252. partial_sort_minimal_n(1000),
  253. partial_sum_dilation(1.0f),
  254. partial_sum_minimal_n(1000),
  255. random_shuffle_minimal_n(1000),
  256. replace_minimal_n(1000),
  257. set_difference_minimal_n(1000),
  258. set_intersection_minimal_n(1000),
  259. set_symmetric_difference_minimal_n(1000),
  260. set_union_minimal_n(1000),
  261. sort_minimal_n(1000),
  262. sort_mwms_oversampling(10),
  263. sort_qs_num_samples_preset(100),
  264. sort_qsb_base_case_maximal_n(100),
  265. transform_minimal_n(1000),
  266. unique_copy_minimal_n(10000),
  267. workstealing_chunk_size(100),
  268. L1_cache_size(16 << 10),
  269. L2_cache_size(256 << 10),
  270. TLB_size(128),
  271. cache_line_size(64),
  272. qsb_steals(0),
  273. search_minimal_n(1000),
  274. find_scale_factor(0.01f)
  275. { }
  276. };
  277. }
  278. #endif /* _GLIBCXX_PARALLEL_SETTINGS_H */