piranha  0.10
settings.hpp
1 /* Copyright 2009-2017 Francesco Biscani (bluescarni@gmail.com)
2 
3 This file is part of the Piranha library.
4 
5 The Piranha library is free software; you can redistribute it and/or modify
6 it under the terms of either:
7 
8  * the GNU Lesser General Public License as published by the Free
9  Software Foundation; either version 3 of the License, or (at your
10  option) any later version.
11 
12 or
13 
14  * the GNU General Public License as published by the Free Software
15  Foundation; either version 3 of the License, or (at your option) any
16  later version.
17 
18 or both in parallel, as here.
19 
20 The Piranha library is distributed in the hope that it will be useful, but
21 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
22 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
23 for more details.
24 
25 You should have received copies of the GNU General Public License and the
26 GNU Lesser General Public License along with the Piranha library. If not,
27 see https://www.gnu.org/licenses/. */
28 
29 #ifndef PIRANHA_SETTINGS_HPP
30 #define PIRANHA_SETTINGS_HPP
31 
32 #include <atomic>
33 #include <mutex>
34 #include <stdexcept>
35 
36 #include <piranha/config.hpp>
37 #include <piranha/exceptions.hpp>
38 #include <piranha/runtime_info.hpp>
39 #include <piranha/thread_pool.hpp>
40 
41 namespace piranha
42 {
43 
44 namespace detail
45 {
46 
47 template <typename = int>
48 struct base_settings {
49  static std::mutex m_mutex;
50  static unsigned m_cache_line_size;
51  static unsigned long m_max_term_output;
52  static const unsigned long m_default_max_term_output = 20ul;
53  static std::atomic_ullong s_min_work_per_thread;
54  // NOTE: this corresponds to circa 2% overhead from thread management on a common desktop
55  // machine around 2012 for the fastest series multiplication scenario.
56  static const unsigned long long s_default_min_work_per_thread = 250000ull;
57 };
58 
59 template <typename T>
60 std::mutex base_settings<T>::m_mutex;
61 
62 template <typename T>
63 unsigned base_settings<T>::m_cache_line_size = runtime_info::get_cache_line_size();
64 
65 template <typename T>
66 unsigned long base_settings<T>::m_max_term_output = base_settings<T>::m_default_max_term_output;
67 
68 template <typename T>
69 const unsigned long base_settings<T>::m_default_max_term_output;
70 
71 template <typename T>
72 const unsigned long long base_settings<T>::s_default_min_work_per_thread;
73 
74 template <typename T>
75 std::atomic_ullong base_settings<T>::s_min_work_per_thread(base_settings<T>::s_default_min_work_per_thread);
76 }
77 
79 
88 template <typename = void>
89 class settings_ : private detail::base_settings<>
90 {
91 public:
93 
101  static unsigned get_n_threads()
102  {
103  return thread_pool::size();
104  }
106 
113  static void set_n_threads(unsigned n)
114  {
116  }
118 
123  static void reset_n_threads()
124  {
125  const auto candidate = runtime_info::get_hardware_concurrency();
126  set_n_threads((candidate > 0u) ? candidate : 1u);
127  }
129 
140  static void set_thread_binding(bool flag)
141  {
143  }
145 
153  static bool get_thread_binding()
154  {
155  return thread_pool::get_binding();
156  }
158 
166  static unsigned get_cache_line_size()
167  {
168  std::lock_guard<std::mutex> lock(m_mutex);
169  return m_cache_line_size;
170  }
172 
180  static void set_cache_line_size(unsigned n)
181  {
182  std::lock_guard<std::mutex> lock(m_mutex);
183  m_cache_line_size = n;
184  }
186 
191  static void reset_cache_line_size()
192  {
193  std::lock_guard<std::mutex> lock(m_mutex);
194  m_cache_line_size = runtime_info::get_cache_line_size();
195  }
197 
202  static unsigned long get_max_term_output()
203  {
204  std::lock_guard<std::mutex> lock(m_mutex);
205  return m_max_term_output;
206  }
208 
213  static void set_max_term_output(unsigned long n)
214  {
215  std::lock_guard<std::mutex> lock(m_mutex);
216  m_max_term_output = n;
217  }
219 
224  static void reset_max_term_output()
225  {
226  std::lock_guard<std::mutex> lock(m_mutex);
227  m_max_term_output = m_default_max_term_output;
228  }
230 
233  static unsigned long long get_min_work_per_thread()
234  {
235  return s_min_work_per_thread.load();
236  }
238 
243  static void set_min_work_per_thread(unsigned long long n)
244  {
245  if (unlikely(n == 0u)) {
246  piranha_throw(std::invalid_argument, "the minimum work per thread value must be strictly positive");
247  }
248  return s_min_work_per_thread.store(n);
249  }
251 
255  {
256  s_min_work_per_thread.store(s_default_min_work_per_thread);
257  }
258 };
259 
261 
265 }
266 
267 #endif
static void set_min_work_per_thread(unsigned long long n)
Set the minimum work per thread.
Definition: settings.hpp:243
static unsigned get_cache_line_size()
Size of the data cache line.
static void reset_cache_line_size()
Reset the cache line size.
Definition: settings.hpp:191
static void set_max_term_output(unsigned long n)
Set max term output.
Definition: settings.hpp:213
static void reset_max_term_output()
Reset max term output.
Definition: settings.hpp:224
static unsigned get_cache_line_size()
Get the cache line size.
Definition: settings.hpp:166
Exceptions.
static bool get_thread_binding()
Get the thread binding policy.
Definition: settings.hpp:153
static unsigned long get_max_term_output()
Get max term output.
Definition: settings.hpp:202
static void reset_min_work_per_thread()
Reset the minimum work per thread.
Definition: settings.hpp:254
static void set_cache_line_size(unsigned n)
Set the cache line size.
Definition: settings.hpp:180
#define piranha_throw(exception_type,...)
Exception-throwing macro.
Definition: exceptions.hpp:118
static void set_n_threads(unsigned n)
Set the number of threads available for use by piranha.
Definition: settings.hpp:113
Root piranha namespace.
Definition: array_key.hpp:52
static void reset_n_threads()
Reset the number of threads available for use by piranha.
Definition: settings.hpp:123
static void set_thread_binding(bool flag)
Set the thread binding policy.
Definition: settings.hpp:140
static unsigned size()
Size.
Global settings.
Definition: settings.hpp:89
static unsigned get_hardware_concurrency()
Hardware concurrency.
static unsigned get_n_threads()
Get the number of threads available for use by piranha.
Definition: settings.hpp:101
static void set_binding(bool flag)
Set the thread binding policy.
static unsigned long long get_min_work_per_thread()
Get the minimum work per thread.
Definition: settings.hpp:233
static void resize(unsigned new_size)
Change the number of threads.
static bool get_binding()
Get the thread binding policy.