vSMC  v3.0.0
Scalable Monte Carlo
rng_set.hpp
Go to the documentation of this file.
1 //============================================================================
2 // vSMC/include/vsmc/rng/rng_set.hpp
3 //----------------------------------------------------------------------------
4 // vSMC: Scalable Monte Carlo
5 //----------------------------------------------------------------------------
6 // Copyright (c) 2013-2016, 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_RNG_RNG_SET_HPP
33 #define VSMC_RNG_RNG_SET_HPP
34 
36 #include <vsmc/rng/engine.hpp>
37 #include <vsmc/rng/seed.hpp>
38 
39 #if VSMC_HAS_TBB
40 #include <tbb/combinable.h>
41 #endif
42 
45 #ifndef VSMC_RNG_SET_TYPE
46 #if VSMC_USE_TBB_TLS
47 #define VSMC_RNG_SET_TYPE ::vsmc::RNGSetTBB
48 #else
49 #define VSMC_RNG_SET_TYPE ::vsmc::RNGSetVector
50 #endif
51 #endif
52 
53 namespace vsmc
54 {
55 
58 template <typename RNGType = RNG>
60 {
62 
63  public:
64  using rng_type = RNGType;
65  using size_type = std::size_t;
66 
67  explicit RNGSetScalar(size_type N = 0) : size_(N) { seed(); }
68 
69  size_type size() const { return size_; }
70 
71  void resize(std::size_t) {}
72 
73  void seed() { Seed::instance()(rng_); }
74 
75  rng_type &operator[](size_type) { return rng_; }
76 
77  private:
78  std::size_t size_;
79  rng_type rng_;
80 }; // class RNGSetScalar
81 
84 template <typename RNGType = RNGMini>
86 {
87  public:
88  using rng_type = RNGType;
90 
91  explicit RNGSetVector(size_type N = 0) : rng_(N, rng_type()) { seed(); }
92 
93  size_type size() const { return rng_.size(); }
94 
95  void resize(std::size_t n)
96  {
97  if (n == rng_.size())
98  return;
99 
100  if (n < rng_.size())
101  rng_.resize(n);
102 
103  size_type m = rng_.size();
104  rng_.resize(n);
105  Seed::instance()(n - m, rng_.begin() + m);
106  }
107 
108  void seed() { Seed::instance()(rng_.size(), rng_.begin()); }
109 
110  rng_type &operator[](size_type id) { return rng_[id % size()]; }
111 
112  private:
113  Vector<rng_type> rng_;
114 }; // class RNGSetVector
115 
116 #if VSMC_HAS_TBB
117 
120 template <typename RNGType = RNG>
122 {
123  public:
124  using rng_type = RNGType;
125  using size_type = std::size_t;
126 
127  explicit RNGSetTBB(size_type N = 0)
128  : size_(N), rng_([]() {
129  rng_type rng;
130  Seed::instance()(rng);
131  return rng;
132  })
133  {
134  seed();
135  }
136 
137  size_type size() const { return size_; }
138 
139  void resize(std::size_t) {}
140 
141  void seed() { rng_.clear(); }
142 
143  rng_type &operator[](size_type) { return rng_.local(); }
144 
145  private:
146  std::size_t size_;
147  ::tbb::combinable<rng_type> rng_;
148 }; // class RNGSetTBB
149 
150 #endif // VSMC_HAS_TBB
151 
154 template <typename RNGType = typename std::conditional<
155  std::is_same<VSMC_RNG_SET_TYPE<RNG>, RNGSetVector<RNG>>::value,
156  RNGMini, RNG>::type>
157 using RNGSet = VSMC_RNG_SET_TYPE<RNGType>;
158 
162 
163 } // namespace vsmc
164 
165 #endif // VSMC_RNG_RNG_SET_HPP
size_type size() const
Definition: rng_set.hpp:137
#define VSMC_DEFINE_TYPE_DISPATCH_TRAIT(Outer, Inner, Default)
Definition: traits.hpp:40
std::vector< T, Alloc > Vector
std::vector with Allocator as default allocator
RNGType rng_type
Definition: rng_set.hpp:64
static SeedGenerator< ID, ResultType > & instance()
Definition: seed.hpp:82
Definition: monitor.hpp:48
void resize(std::size_t)
Definition: rng_set.hpp:71
::vsmc::Philox2x32 RNGMini
The 32-bits RNG with smallest state.
Definition: engine.hpp:95
typename RNGSetTypeTrait< T >::type RNGSetType
Definition: rng_set.hpp:161
size_type size() const
Definition: rng_set.hpp:69
Vector RNG set.
Definition: rng_set.hpp:85
RNGType rng_type
Definition: rng_set.hpp:88
std::size_t size_type
Definition: rng_set.hpp:125
RNGSetVector(size_type N=0)
Definition: rng_set.hpp:91
STL namespace.
Counter based RNG engine.
Definition: counter.hpp:187
void resize(std::size_t)
Definition: rng_set.hpp:139
rng_type & operator[](size_type id)
Definition: rng_set.hpp:110
RNGType rng_type
Definition: rng_set.hpp:124
RNGSetTBB(size_type N=0)
Definition: rng_set.hpp:127
rng_type & operator[](size_type)
Definition: rng_set.hpp:75
size_type size() const
Definition: rng_set.hpp:93
rng_type & operator[](size_type)
Definition: rng_set.hpp:143
Thread-local storage RNG set using tbb::combinable.
Definition: rng_set.hpp:121
typename Vector< rng_type >::size_type size_type
Definition: rng_set.hpp:89
void resize(std::size_t n)
Definition: rng_set.hpp:95
Scalar RNG set.
Definition: rng_set.hpp:59
#define VSMC_DEFINE_NEW_DELETE(Class)
Define class member new and delete using AlignedMemory.
std::size_t size_type
Definition: rng_set.hpp:65