STXXL  1.4-dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
settings.h
Go to the documentation of this file.
1 /***************************************************************************
2  * include/stxxl/bits/parallel/settings.h
3  *
4  * Settings and tuning parameters, heuristics to decide whether to use
5  * parallelized algorithms.
6  * Extracted from MCSTL http://algo2.iti.uni-karlsruhe.de/singler/mcstl/
7  *
8  * Part of the STXXL. See http://stxxl.sourceforge.net
9  *
10  * Copyright (C) 2007 Johannes Singler <[email protected]>
11  * Copyright (C) 2014 Timo Bingmann <[email protected]>
12  *
13  * Distributed under the Boost Software License, Version 1.0.
14  * (See accompanying file LICENSE_1_0.txt or copy at
15  * http://www.boost.org/LICENSE_1_0.txt)
16  **************************************************************************/
17 
18 #ifndef STXXL_PARALLEL_SETTINGS_HEADER
19 #define STXXL_PARALLEL_SETTINGS_HEADER
20 
21 #include <stxxl/bits/config.h>
22 #include <stxxl/bits/namespace.h>
24 
25 #if STXXL_PARALLEL
26  #include <omp.h>
27 #endif
28 
30 
31 namespace parallel {
32 
33 /** The extensible condition on whether the parallel variant of an algorithm sould be called.
34  * \param c A condition that is overruled by stxxl::parallel::SETTINGS::force_parallel,
35  * i. e. usually a decision based on the input size. */
36 #define STXXL_PARALLEL_CONDITION(c) (!(SETTINGS::force_sequential) && ((SETTINGS::num_threads > 1 && (c)) || SETTINGS::force_parallel))
37 
38 /** Pseudo-integer that gets its initial value from \c omp_get_max_threads(). */
40 {
41 public:
43 
45  {
46 #if STXXL_PARALLEL
47  num_threads = omp_get_max_threads();
48 #else
49  num_threads = 1;
50 #endif // STXXL_PARALLEL
51  if (num_threads < 1)
52  num_threads = 1;
53  }
54 
55  operator int ()
56  {
57  return num_threads;
58  }
59 
60  int operator = (int nt)
61  {
62  num_threads = nt;
63  return num_threads;
64  }
65 };
66 
67 /** Run-time settings for the MCSTL.
68  * \param must_be_int This template parameter exists only to avoid having an
69  * object file belonging to the library. It should always be set to int,
70  * to ensure deterministic behavior. */
71 template <typename must_be_int = int>
72 struct Settings
73 {
74 public:
75  /** Different parallel sorting algorithms to choose from: multi-way mergesort, quicksort, load-balanced quicksort. */
76  enum SortAlgorithm { MWMS, QS, QS_BALANCED };
77  /** Different merging algorithms: bubblesort-alike, loser-tree variants, enum sentinel */
78  enum MultiwayMergeAlgorithm { BUBBLE, LOSER_TREE, LOSER_TREE_COMBINED, LOSER_TREE_SENTINEL, MWM_ALGORITHM_LAST };
79  /** Different splitting strategies for sorting/merging: by sampling, exact */
80  enum Splitting { SAMPLING, EXACT };
81 
82  /** Number of thread to be used.
83  * Initialized to omp_get_max_threads(), but can be overridden by the user.
84  */
86 
87  /** Force all algorithms to be executed sequentially.
88  * This setting cannot be overwritten. */
89  static volatile bool force_sequential;
90 
91  /** Force all algorithms to be executed in parallel.
92  * This setting can be overriden by
93  * mcstl::sequential_tag (compile-time), and
94  * force_sequential (run-time). */
95  static volatile bool force_parallel;
96 
97  /** Algorithm to use for sorting. */
98  static volatile SortAlgorithm sort_algorithm;
99 
100  /** Strategy to use for splitting the input when sorting (MWMS). */
101  static volatile Splitting sort_splitting;
102 
103  /** Minimal input size for parallel sorting. */
105  /** Oversampling factor for parallel std::sort (MWMS). */
106  static volatile unsigned int sort_mwms_oversampling;
107 
108  /** Oversampling factor for parallel std::merge.
109  * Such many samples per thread are collected. */
110  static volatile unsigned int merge_oversampling;
111 
112  /** Algorithm to use for parallel mcstl::multiway_merge. */
114  /** Splitting strategy to use for parallel mcstl::multiway_merge. */
116  /** Oversampling factor for parallel mcstl::multiway_merge. */
117  static volatile unsigned int multiway_merge_oversampling;
118  /** Minimal input size for parallel mcstl::multiway_merge. */
120  /** Oversampling factor for parallel mcstl::multiway_merge. */
121  static volatile int multiway_merge_minimal_k;
122 
123 //hardware dependent tuning parameters
124  /** Size of the L1 cache in bytes (underestimation). */
125  static volatile unsigned long long L1_cache_size;
126  /** Size of the L2 cache in bytes (underestimation). */
127  static volatile unsigned long long L2_cache_size;
128  /** Size of the Translation Lookaside Buffer (underestimation). */
129  static volatile unsigned int TLB_size;
130 
131  /** Overestimation of cache line size.
132  * Used to avoid false sharing, i. e. elements of different threads are at least this amount apart. */
133  static unsigned int cache_line_size; //overestimation
134 };
135 
136 /** Convenience typedef to avoid to have write \c Settings<>. */
138 
139 template <typename must_be_int>
140 volatile bool Settings<must_be_int>::force_parallel = false;
141 
142 template <typename must_be_int>
143 volatile bool Settings<must_be_int>::force_sequential = false;
144 
145 template <typename must_be_int>
147 
148 template <typename must_be_int>
150 
151 template <typename must_be_int>
153 
154 template <typename must_be_int>
155 volatile unsigned int Settings<must_be_int>::sort_mwms_oversampling = 10;
156 
157 template <typename must_be_int>
158 volatile unsigned int Settings<must_be_int>::merge_oversampling = 10;
159 
160 template <typename must_be_int>
162 
163 template <typename must_be_int>
165 
166 template <typename must_be_int>
168 
169 template <typename must_be_int>
171 
172 template <typename must_be_int>
173 volatile unsigned int Settings<must_be_int>::multiway_merge_oversampling = 10;
174 
175 //hardware dependent tuning parameters
176 template <typename must_be_int>
177 volatile unsigned long long Settings<must_be_int>::L1_cache_size = 16 << 10;
178 
179 template <typename must_be_int>
180 volatile unsigned long long Settings<must_be_int>::L2_cache_size = 256 << 10;
181 
182 template <typename must_be_int>
183 volatile unsigned int Settings<must_be_int>::TLB_size = 128;
184 
185 template <typename must_be_int>
186 unsigned int Settings<must_be_int>::cache_line_size = 64; //overestimation
187 
188 template <typename must_be_int>
190 
191 } // namespace parallel
192 
194 
195 #endif // !STXXL_PARALLEL_SETTINGS_HEADER
static volatile Splitting sort_splitting
Definition: settings.h:101
static volatile unsigned long long L1_cache_size
Definition: settings.h:125
static volatile Splitting multiway_merge_splitting
Definition: settings.h:115
static volatile unsigned long long L2_cache_size
Definition: settings.h:127
static volatile int multiway_merge_minimal_k
Definition: settings.h:121
static volatile sequence_index_t multiway_merge_minimal_n
Definition: settings.h:119
static volatile unsigned int TLB_size
Definition: settings.h:129
static volatile MultiwayMergeAlgorithm multiway_merge_algorithm
Definition: settings.h:113
settings SETTINGS
Definition: settings.h:35
static volatile bool force_sequential
Definition: settings.h:89
static volatile unsigned int multiway_merge_oversampling
Definition: settings.h:117
static volatile bool force_parallel
Definition: settings.h:95
uint64 sequence_index_t
Definition: types.h:32
#define STXXL_BEGIN_NAMESPACE
Definition: namespace.h:16
static volatile unsigned int merge_oversampling
Definition: settings.h:110
static volatile sequence_index_t sort_minimal_n
Definition: settings.h:104
static NumberOfThreads num_threads
Definition: settings.h:85
static volatile unsigned int sort_mwms_oversampling
Definition: settings.h:106
static volatile SortAlgorithm sort_algorithm
Definition: settings.h:98
static unsigned int cache_line_size
Definition: settings.h:133
#define STXXL_END_NAMESPACE
Definition: namespace.h:17