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