MCKL
Monte Carlo Kernel Library
rng_set.hpp
Go to the documentation of this file.
1 //============================================================================
2 // MCKL/include/mckl/random/rng_set.hpp
3 //----------------------------------------------------------------------------
4 // MCKL: Monte Carlo Kernel Library
5 //----------------------------------------------------------------------------
6 // Copyright (c) 2013-2018, 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 MCKL_RANDOM_RNG_SET_HPP
33 #define MCKL_RANDOM_RNG_SET_HPP
34 
36 #include <mckl/random/rng.hpp>
37 #include <mckl/random/seed.hpp>
38 
39 #if MCKL_HAS_TBB
40 #include <tbb/enumerable_thread_specific.h>
41 #endif
42 
45 #ifndef MCKL_RNG_SET_TYPE
46 #if MCKL_USE_TBB_TLS
47 #define MCKL_RNG_SET_TYPE ::mckl::RNGSetTBB
48 #else
49 #define MCKL_RNG_SET_TYPE ::mckl::RNGSetVector
50 #endif
51 #endif
52 
53 namespace mckl {
54 
57 template <typename RNGType = RNG>
59 {
60  public:
61  using rng_type = RNGType;
62  using size_type = std::size_t;
63 
64  explicit RNGSetScalar(size_type = 0) { reset(); }
65 
66  size_type size() const { return 1; }
67 
68  void resize(std::size_t) {}
69 
70  void reset() { rng_.seed(Seed<rng_type>::instance().get()); }
71 
72  rng_type &operator[](size_type) { return rng_; }
73 
74  private:
75  std::size_t size_;
76  rng_type rng_;
77 }; // class RNGSetScalar
78 
81 template <typename RNGType = RNG>
83 {
84  public:
85  using rng_type = RNGType;
87 
88  explicit RNGSetVector(size_type N = 0) : rng_(N, rng_type()) { reset(); }
89 
90  size_type size() const { return rng_.size(); }
91 
92  void resize(std::size_t n)
93  {
94  if (n == rng_.size()) {
95  return;
96  }
97 
98  if (n < rng_.size()) {
99  rng_.resize(n);
100  }
101 
102  size_type m = rng_.size();
103  rng_.resize(n);
104  for (std::size_t i = m; i != n; ++i) {
105  rng_[i].seed(Seed<rng_type>::instance().get());
106  }
107  }
108 
109  void reset()
110  {
111  for (auto &rng : rng_) {
112  rng.seed(Seed<rng_type>::instance().get());
113  }
114  }
115 
116  rng_type &operator[](size_type id) { return rng_[id % size()]; }
117 
118  private:
119  Vector<rng_type> rng_;
120 }; // class RNGSetVector
121 
122 #if MCKL_HAS_TBB
123 
126 template <typename RNGType = RNG,
127  typename Alloc = ::tbb::cache_aligned_allocator<RNGType>,
128  ::tbb::ets_key_usage_type ETSKeyType = ::tbb::ets_no_key>
130 {
131  public:
132  using rng_type = RNGType;
133  using size_type = std::size_t;
134 
136  : rng_([]() { return rng_type(Seed<rng_type>::instance().get()); })
137  {
138  reset();
139  }
140 
141  size_type size() const { return rng_.size(); }
142 
143  void resize(std::size_t) {}
144 
145  void reset() { rng_.clear(); }
146 
147  rng_type &operator[](size_type) { return rng_.local(); }
148 
149  private:
150  std::size_t size_;
151  ::tbb::enumerable_thread_specific<rng_type, Alloc, ETSKeyType> rng_;
152 }; // class RNGSetTBBEnumerable
153 
157 template <typename RNGType = RNG>
158 using RNGSetTBB = RNGSetTBBEnumerable<RNGType,
159  ::tbb::cache_aligned_allocator<RNGType>, ::tbb::ets_no_key>;
160 
164 template <typename RNGType = RNG>
165 using RNGSetTBBKPI = RNGSetTBBEnumerable<RNGType,
166  ::tbb::cache_aligned_allocator<RNGType>, ::tbb::ets_key_per_instance>;
167 
168 #endif // MCKL_HAS_TBB
169 
172 template <typename RNGType = typename MCKL_RNG_SET_TYPE<>::rng_type>
173 using RNGSet = MCKL_RNG_SET_TYPE<RNGType>;
174 
178 
179 } // namespace mckl
180 
181 #endif // MCKL_RANDOM_RNG_SET_HPP
RNGType rng_type
Definition: rng_set.hpp:85
RNGSetTBBEnumerable(size_type=0)
Definition: rng_set.hpp:135
rng_type & operator[](size_type)
Definition: rng_set.hpp:72
Vector RNG set.
Definition: rng_set.hpp:82
void resize(std::size_t)
Definition: rng_set.hpp:143
void resize(std::size_t)
Definition: rng_set.hpp:68
size_type size() const
Definition: rng_set.hpp:66
std::vector< T, Alloc > Vector
std::vector with Allocator as the default allocator
Definition: memory.hpp:435
#define MCKL_DEFINE_TYPE_DISPATCH_TRAIT(Outer, Inner, Default)
Definition: traits.hpp:39
RNGSetVector(size_type N=0)
Definition: rng_set.hpp:88
Scalar RNG set.
Definition: rng_set.hpp:58
typename RNGSetTypeTrait< T >::type RNGSetType
Definition: rng_set.hpp:177
size_type size() const
Definition: rng_set.hpp:90
std::size_t size_type
Definition: rng_set.hpp:62
Thread-local storage RNG set using tbb::enumerable_thread_specific.
Definition: rng_set.hpp:129
typename Vector< rng_type >::size_type size_type
Definition: rng_set.hpp:86
size_type size() const
Definition: rng_set.hpp:141
::mckl::ARS RNG
The default 32-bit RNG.
Definition: rng.hpp:73
rng_type & operator[](size_type id)
Definition: rng_set.hpp:116
void resize(std::size_t n)
Definition: rng_set.hpp:92
Definition: mcmc.hpp:40
RNGType rng_type
Definition: rng_set.hpp:61
RNGSetScalar(size_type=0)
Definition: rng_set.hpp:64
RNG default seed generator.
Definition: seed.hpp:339
rng_type & operator[](size_type)
Definition: rng_set.hpp:147