settings.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  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/settings.h
  21. * @brief Runtime settings and tuning parameters, heuristics to decide
  22. * whether to use parallelized algorithms.
  23. * This file is a GNU parallel extension to the Standard C++ Library.
  24. *
  25. * @section parallelization_decision
  26. * The decision 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. *
  60. * a. force_sequential, meaning the sequential algorithm is executed.
  61. * b. force_parallel, meaning the parallel algorithm is executed.
  62. * c. heuristic
  63. *
  64. * For heuristic, the parallel algorithm implementation is called
  65. * only if the input size is sufficiently large. For most
  66. * algorithms, the input size is the (combined) length of the input
  67. * sequence(s). The threshold can be set by the user, individually
  68. * for each algorithm. The according variables are called
  69. * __gnu_parallel::_Settings::[algorithm]_minimal_n .
  70. *
  71. * For some of the algorithms, there are even more tuning options,
  72. * e. g. the ability to choose from multiple algorithm variants. See
  73. * below for details.
  74. */
  75. // Written by Johannes Singler and Felix Putze.
  76. #ifndef _GLIBCXX_PARALLEL_SETTINGS_H
  77. #define _GLIBCXX_PARALLEL_SETTINGS_H 1
  78. #include <parallel/types.h>
  79. /**
  80. * @brief Determine at compile(?)-time if the parallel variant of an
  81. * algorithm should be called.
  82. * @param c A condition that is convertible to bool that is overruled by
  83. * __gnu_parallel::_Settings::algorithm_strategy. Usually a decision
  84. * based on the input size.
  85. */
  86. #define _GLIBCXX_PARALLEL_CONDITION(c) (__gnu_parallel::_Settings::get().algorithm_strategy != __gnu_parallel::force_sequential && ((__gnu_parallel::get_max_threads() > 1 && (c)) || __gnu_parallel::_Settings::get().algorithm_strategy == __gnu_parallel::force_parallel))
  87. /*
  88. inline bool
  89. parallel_condition(bool c)
  90. {
  91. bool ret = false;
  92. const _Settings& s = _Settings::get();
  93. if (s.algorithm_strategy != force_seqential)
  94. {
  95. if (s.algorithm_strategy == force_parallel)
  96. ret = true;
  97. else
  98. ret = get_max_threads() > 1 && c;
  99. }
  100. return ret;
  101. }
  102. */
  103. namespace __gnu_parallel
  104. {
  105. /// class _Settings
  106. /// Run-time settings for the parallel mode, including all tunable parameters.
  107. struct _Settings
  108. {
  109. _AlgorithmStrategy algorithm_strategy;
  110. _SortAlgorithm sort_algorithm;
  111. _PartialSumAlgorithm partial_sum_algorithm;
  112. _MultiwayMergeAlgorithm multiway_merge_algorithm;
  113. _FindAlgorithm find_algorithm;
  114. _SplittingAlgorithm sort_splitting;
  115. _SplittingAlgorithm merge_splitting;
  116. _SplittingAlgorithm multiway_merge_splitting;
  117. // Per-algorithm settings.
  118. /// Minimal input size for accumulate.
  119. sequence_index_t accumulate_minimal_n;
  120. /// Minimal input size for adjacent_difference.
  121. unsigned int adjacent_difference_minimal_n;
  122. /// Minimal input size for count and count_if.
  123. sequence_index_t count_minimal_n;
  124. /// Minimal input size for fill.
  125. sequence_index_t fill_minimal_n;
  126. /// Block size increase factor for find.
  127. double find_increasing_factor;
  128. /// Initial block size for find.
  129. sequence_index_t find_initial_block_size;
  130. /// Maximal block size for find.
  131. sequence_index_t find_maximum_block_size;
  132. /// Start with looking for this many elements sequentially, for find.
  133. sequence_index_t find_sequential_search_size;
  134. /// Minimal input size for for_each.
  135. sequence_index_t for_each_minimal_n;
  136. /// Minimal input size for generate.
  137. sequence_index_t generate_minimal_n;
  138. /// Minimal input size for max_element.
  139. sequence_index_t max_element_minimal_n;
  140. /// Minimal input size for merge.
  141. sequence_index_t merge_minimal_n;
  142. /// Oversampling factor for merge.
  143. unsigned int merge_oversampling;
  144. /// Minimal input size for min_element.
  145. sequence_index_t min_element_minimal_n;
  146. /// Minimal input size for multiway_merge.
  147. sequence_index_t multiway_merge_minimal_n;
  148. /// Oversampling factor for multiway_merge.
  149. int multiway_merge_minimal_k;
  150. /// Oversampling factor for multiway_merge.
  151. unsigned int multiway_merge_oversampling;
  152. /// Minimal input size for nth_element.
  153. sequence_index_t nth_element_minimal_n;
  154. /// Chunk size for partition.
  155. sequence_index_t partition_chunk_size;
  156. /// Chunk size for partition, relative to input size. If > 0.0,
  157. /// this value overrides partition_chunk_size.
  158. double partition_chunk_share;
  159. /// Minimal input size for partition.
  160. sequence_index_t partition_minimal_n;
  161. /// Minimal input size for partial_sort.
  162. sequence_index_t partial_sort_minimal_n;
  163. /// Ratio for partial_sum. Assume "sum and write result" to be
  164. /// this factor slower than just "sum".
  165. float partial_sum_dilation;
  166. /// Minimal input size for partial_sum.
  167. unsigned int partial_sum_minimal_n;
  168. /// Minimal input size for random_shuffle.
  169. unsigned int random_shuffle_minimal_n;
  170. /// Minimal input size for replace and replace_if.
  171. sequence_index_t replace_minimal_n;
  172. /// Minimal input size for set_difference.
  173. sequence_index_t set_difference_minimal_n;
  174. /// Minimal input size for set_intersection.
  175. sequence_index_t set_intersection_minimal_n;
  176. /// Minimal input size for set_symmetric_difference.
  177. sequence_index_t set_symmetric_difference_minimal_n;
  178. /// Minimal input size for set_union.
  179. sequence_index_t set_union_minimal_n;
  180. /// Minimal input size for parallel sorting.
  181. sequence_index_t sort_minimal_n;
  182. /// Oversampling factor for parallel std::sort (MWMS).
  183. unsigned int sort_mwms_oversampling;
  184. /// Such many samples to take to find a good pivot (quicksort).
  185. unsigned int sort_qs_num_samples_preset;
  186. /// Maximal subsequence length to switch to unbalanced base case.
  187. /// Applies to std::sort with dynamically load-balanced quicksort.
  188. sequence_index_t sort_qsb_base_case_maximal_n;
  189. /// Minimal input size for parallel std::transform.
  190. sequence_index_t transform_minimal_n;
  191. /// Minimal input size for unique_copy.
  192. sequence_index_t unique_copy_minimal_n;
  193. sequence_index_t workstealing_chunk_size;
  194. // Hardware dependent tuning parameters.
  195. /// Size of the L1 cache in bytes (underestimation).
  196. unsigned long long L1_cache_size;
  197. /// Size of the L2 cache in bytes (underestimation).
  198. unsigned long long L2_cache_size;
  199. /// Size of the Translation Lookaside Buffer (underestimation).
  200. unsigned int TLB_size;
  201. /// Overestimation of cache line size. Used to avoid false
  202. /// sharing, i. e. elements of different threads are at least this
  203. /// amount apart.
  204. unsigned int cache_line_size;
  205. // Statistics.
  206. /// The number of stolen ranges in load-balanced quicksort.
  207. sequence_index_t qsb_steals;
  208. /// Get the global settings.
  209. static const _Settings&
  210. get() throw();
  211. /// Set the global settings.
  212. static void
  213. set(_Settings&) throw();
  214. explicit
  215. _Settings() : algorithm_strategy(heuristic), sort_algorithm(MWMS), partial_sum_algorithm(LINEAR), multiway_merge_algorithm(LOSER_TREE), find_algorithm(CONSTANT_SIZE_BLOCKS), sort_splitting(EXACT), merge_splitting(EXACT), multiway_merge_splitting(EXACT), accumulate_minimal_n(1000), adjacent_difference_minimal_n(1000), count_minimal_n(1000), fill_minimal_n(1000), find_increasing_factor(2.0), find_initial_block_size(256), find_maximum_block_size(8192), find_sequential_search_size(256), for_each_minimal_n(1000), generate_minimal_n(1000), max_element_minimal_n(1000), merge_minimal_n(1000), merge_oversampling(10), min_element_minimal_n(1000), multiway_merge_minimal_n(1000), multiway_merge_minimal_k(2), multiway_merge_oversampling(10), nth_element_minimal_n(1000), partition_chunk_size(1000), partition_chunk_share(0.0), partition_minimal_n(1000), partial_sort_minimal_n(1000), partial_sum_dilation(1.0f), partial_sum_minimal_n(1000), random_shuffle_minimal_n(1000), replace_minimal_n(1000), set_difference_minimal_n(1000), set_intersection_minimal_n(1000), set_symmetric_difference_minimal_n(1000), set_union_minimal_n(1000), sort_minimal_n(1000), sort_mwms_oversampling(10), sort_qs_num_samples_preset(100), sort_qsb_base_case_maximal_n(100), transform_minimal_n(1000), unique_copy_minimal_n(10000), workstealing_chunk_size(100), L1_cache_size(16 << 10), L2_cache_size(256 << 10), TLB_size(128), cache_line_size(64), qsb_steals(0)
  216. { }
  217. };
  218. }
  219. #endif /* _GLIBCXX_PARALLEL_SETTINGS_H */