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