vSMC
vSMC: Scalable Monte Carlo
thread_num.hpp
Go to the documentation of this file.
1 //============================================================================
2 // vSMC/include/vsmc/thread/thread_num.hpp
3 //----------------------------------------------------------------------------
4 // vSMC: Scalable Monte Carlo
5 //----------------------------------------------------------------------------
6 // Copyright (c) 2013,2014, Yan Zhou
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
11 //
12 // Redistributions of source code must retain the above copyright notice,
13 // this list of conditions and the following disclaimer.
14 //
15 // Redistributions in binary form must reproduce the above copyright notice,
16 // this list of conditions and the following disclaimer in the documentation
17 // and/or other materials provided with the distribution.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 // POSSIBILITY OF SUCH DAMAGE.
30 //============================================================================
31 
32 #ifndef VSMC_THREAD_THREAD_NUM_HPP
33 #define VSMC_THREAD_THREAD_NUM_HPP
34 
35 #include <vsmc/internal/common.hpp>
36 #include <thread>
37 
38 namespace vsmc {
39 
42 class ThreadNum
43 {
44  public :
45 
46  static ThreadNum &instance ()
47  {
48  static ThreadNum num;
49 
50  return num;
51  }
52 
53  std::size_t thread_num () const {return thread_num_;}
54 
56  std::size_t thread_num (std::size_t num)
57  {
58  std::size_t old_num = thread_num_;
59  thread_num_ = num;
60 
61  return old_num;
62  }
63 
64  template <typename Range>
65  std::vector<Range> partition (const Range &range) const
66  {
67  typedef typename Range::const_iterator size_type;
68  size_type N = range.end() - range.begin();
69  size_type tn = static_cast<size_type>(thread_num());
70  size_type block_size = 0;
71 
72  if (N < tn)
73  block_size = 1;
74  else if (N % tn)
75  block_size = N / tn + 1;
76  else
77  block_size = N / tn;
78 
79  std::vector<Range> range_vec;
80  range_vec.reserve(thread_num());
81  size_type B = range.begin();
82  while (N > 0) {
83  size_type next = N < block_size ? N : block_size;
84  range_vec.push_back(Range(B, B + next));
85  B += next;
86  N -= next;
87  }
88 
89  return range_vec;
90  }
91 
92  private :
93 
94  std::size_t thread_num_;
95 
96  ThreadNum () : thread_num_(
97  static_cast<std::size_t>(1) >
98  static_cast<std::size_t>(std::thread::hardware_concurrency()) ?
99  static_cast<std::size_t>(1) :
100  static_cast<std::size_t>(std::thread::hardware_concurrency()))
101  {
102 #ifdef VSMC_MSVC
103 #pragma warning(push)
104 #pragma warning(disable:4996)
105 #endif
106  const char *num_str = std::getenv("VSMC_THREAD_NUM");
107 #ifdef VSMC_MSVC
108 #pragma warning(pop)
109 #endif
110  if (num_str) {
111  int num = std::atoi(num_str);
112  thread_num_ = num > 0 ? static_cast<std::size_t>(num) : 1;
113  }
114  }
115 
116  ThreadNum (const ThreadNum &) = delete;
117  ThreadNum &operator= (const ThreadNum &) = delete;
118 }; // class ThreadInfo
119 
120 } // namespace vsmc
121 
122 #endif // VSMC_THREAD_THREAD_NUM_HPP
Definition: adapter.hpp:37
std::size_t thread_num(std::size_t num)
Set a new number of threads, return the old number.
Definition: thread_num.hpp:56
std::vector< Range > partition(const Range &range) const
Definition: thread_num.hpp:65
STL namespace.
std::size_t thread_num() const
Definition: thread_num.hpp:53
Number of threads used by algorithms.
Definition: thread_num.hpp:42
static ThreadNum & instance()
Definition: thread_num.hpp:46