vSMC
vSMC: 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 #if VSMC_HAS_TBB
39 #include <tbb/combinable.h>
40 #endif
41 
44 #ifndef VSMC_RNG_SET_TYPE
45 #if VSMC_HAS_TBB
46 #define VSMC_RNG_SET_TYPE ::vsmc::RNGSetTBB<::vsmc::RNG>
47 #else
48 #define VSMC_RNG_SET_TYPE ::vsmc::RNGSetVector<::vsmc::RNG>
49 #endif
50 #endif
51 
52 namespace vsmc
53 {
54 
57 template <typename RNGType>
59 {
60  public:
61  using rng_type = RNGType;
62  using size_type = std::size_t;
63 
64  explicit RNGSetScalar(size_type N = 0) : size_(N) { seed(); }
65 
66  size_type size() const { return size_; }
67 
68  void resize(std::size_t) {}
69 
70  void seed() { Seed::instance().seed_rng(rng_); }
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>
83 {
84  public:
85  using rng_type = RNGType;
87 
88  explicit RNGSetVector(size_type N = 0) : size_(N), rng_(size_, rng_type())
89  {
90  seed();
91  }
92 
93  size_type size() const { return 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  rng_.reserve(n);
104  rng_type rng;
105  for (std::size_t i = rng_.size(); i != n; ++i) {
106  Seed::instance().seed_rng(rng);
107  rng_.push_back(rng);
108  }
109  }
110 
111  void seed()
112  {
113  for (auto &rng : rng_)
114  Seed::instance().seed_rng(rng);
115  }
116 
117  rng_type &operator[](size_type id) { return rng_[id % size_]; }
118 
119  private:
120  std::size_t size_;
122 }; // class RNGSetVector
123 
124 #if VSMC_HAS_TBB
125 
128 template <typename RNGType>
130 {
131  public:
132  using rng_type = RNGType;
133  using size_type = std::size_t;
134 
135  explicit RNGSetTBB(size_type N = 0)
136  : size_(N), rng_([]() {
137  rng_type rng;
138  Seed::instance().seed_rng(rng);
139  return rng;
140  })
141  {
142  seed();
143  }
144 
145  size_type size() const { return size_; }
146 
147  void resize(std::size_t) {}
148 
149  void seed() { rng_.clear(); }
150 
151  rng_type &operator[](size_type) { return rng_.local(); }
152 
153  private:
154  std::size_t size_;
155  ::tbb::combinable<rng_type> rng_;
156 }; // class RNGSetTBB
157 
158 #endif // VSMC_HAS_TBB
159 
161 
165 
166 } // namespace vsmc
167 
168 #endif // VSMC_RNG_RNG_SET_HPP
size_type size() const
Definition: rng_set.hpp:145
#define VSMC_DEFINE_TYPE_DISPATCH_TRAIT(Outer, Inner, Default)
Definition: traits.hpp:40
RNGType rng_type
Definition: rng_set.hpp:61
static SeedGenerator< ID, ResultType > & instance()
Definition: seed.hpp:100
Definition: monitor.hpp:49
void resize(std::size_t)
Definition: rng_set.hpp:68
std::vector< T, AlignedAllocator< T >> AlignedVector
Vector type using AlignedAllocator.
typename RNGSetTypeTrait< T >::type RNGSetType
Definition: rng_set.hpp:164
size_type size() const
Definition: rng_set.hpp:66
Vector RNG set.
Definition: rng_set.hpp:82
RNGType rng_type
Definition: rng_set.hpp:85
std::size_t size_type
Definition: rng_set.hpp:133
RNGSetVector(size_type N=0)
Definition: rng_set.hpp:88
typename AlignedVector< rng_type >::size_type size_type
Definition: rng_set.hpp:86
void resize(std::size_t)
Definition: rng_set.hpp:147
rng_type & operator[](size_type id)
Definition: rng_set.hpp:117
RNGType rng_type
Definition: rng_set.hpp:132
#define VSMC_RNG_SET_TYPE
Default RNG set type.
Definition: rng_set.hpp:46
RNGSetTBB(size_type N=0)
Definition: rng_set.hpp:135
rng_type & operator[](size_type)
Definition: rng_set.hpp:72
size_type size() const
Definition: rng_set.hpp:93
rng_type & operator[](size_type)
Definition: rng_set.hpp:151
Thread-local storage RNG set using tbb::combinable.
Definition: rng_set.hpp:129
RNGSetScalar(size_type N=0)
Definition: rng_set.hpp:64
void resize(std::size_t n)
Definition: rng_set.hpp:95
Scalar RNG set.
Definition: rng_set.hpp:58
std::size_t size_type
Definition: rng_set.hpp:62