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-2015, 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/seed.hpp>
37 #include <vsmc/rng/engine.hpp>
38 #if VSMC_HAS_TBB
39 #include <tbb/tbb.h>
40 #endif
41 
44 #ifndef VSMC_RNG_SET_TYPE
45 #define VSMC_RNG_SET_TYPE ::vsmc::RNGSetVector<::vsmc::RNG>
46 #endif
47 
48 namespace vsmc
49 {
50 
53 template <typename RNGType>
55 {
56  public:
57  using rng_type = RNGType;
58  using size_type = std::size_t;
59 
60  explicit RNGSetScalar(size_type N = 0) : size_(N) { seed(); }
61 
62  size_type size() const { return size_; }
63 
64  void resize(std::size_t) {}
65 
66  void seed() { Seed::instance().seed_rng(rng_); }
67 
68  rng_type &operator[](size_type) { return rng_; }
69 
70  private:
71  std::size_t size_;
72  rng_type rng_;
73 }; // class RNGSetScalar
74 
77 template <typename RNGType>
79 {
80  public:
81  using rng_type = RNGType;
83 
84  explicit RNGSetVector(size_type N = 0) : size_(N), rng_(size_, rng_type())
85  {
86  seed();
87  }
88 
89  size_type size() const { return size_; }
90 
91  void resize(std::size_t n)
92  {
93  if (n == rng_.size())
94  return;
95 
96  if (n < rng_.size())
97  rng_.resize(n);
98 
99  rng_.reserve(n);
100  rng_type rng;
101  for (std::size_t i = rng_.size(); i != n; ++i) {
102  Seed::instance().seed_rng(rng);
103  rng_.push_back(rng);
104  }
105  }
106 
107  void seed()
108  {
109  for (auto &rng : rng_)
110  Seed::instance().seed_rng(rng);
111  }
112 
113  rng_type &operator[](size_type id) { return rng_[id % size_]; }
114 
115  private:
116  std::size_t size_;
118 }; // class RNGSetVector
119 
121 
125 
126 } // namespace vsmc
127 
128 #endif // VSMC_RNG_RNG_SET_HPP
#define VSMC_DEFINE_TYPE_DISPATCH_TRAIT(Outer, Inner, Default)
Definition: traits.hpp:40
RNGType rng_type
Definition: rng_set.hpp:57
static SeedGenerator< ID, ResultType > & instance()
Definition: seed.hpp:100
Definition: monitor.hpp:49
void resize(std::size_t)
Definition: rng_set.hpp:64
std::vector< T, AlignedAllocator< T >> AlignedVector
Vector type using AlignedAllocator.
typename RNGSetTypeTrait< T >::type RNGSetType
Definition: rng_set.hpp:124
size_type size() const
Definition: rng_set.hpp:62
Vector RNG set.
Definition: rng_set.hpp:78
RNGType rng_type
Definition: rng_set.hpp:81
RNGSetVector(size_type N=0)
Definition: rng_set.hpp:84
typename AlignedVector< rng_type >::size_type size_type
Definition: rng_set.hpp:82
rng_type & operator[](size_type id)
Definition: rng_set.hpp:113
#define VSMC_RNG_SET_TYPE
Default RNG set type.
Definition: rng_set.hpp:45
rng_type & operator[](size_type)
Definition: rng_set.hpp:68
size_type size() const
Definition: rng_set.hpp:89
RNGSetScalar(size_type N=0)
Definition: rng_set.hpp:60
void resize(std::size_t n)
Definition: rng_set.hpp:91
Scalar RNG set.
Definition: rng_set.hpp:54
std::size_t size_type
Definition: rng_set.hpp:58