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,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_RNG_RNG_SET_HPP
33 #define VSMC_RNG_RNG_SET_HPP
34 
36 #include <vsmc/rng/seed.hpp>
37 #if VSMC_HAS_AES_NI
38 #include <vsmc/rng/aes.hpp>
39 #include <vsmc/rng/ars.hpp>
40 #endif
41 #include <vsmc/rng/philox.hpp>
42 #include <vsmc/rng/threefry.hpp>
43 
46 #ifndef VSMC_RNG_SET_TYPE
47 #define VSMC_RNG_SET_TYPE ::vsmc::RngSet< ::vsmc::Threefry4x64, ::vsmc::Vector>
48 #endif
49 
50 namespace vsmc {
51 
52 template <typename = Threefry4x64, typename = Vector> class RngSet;
53 
56 template <typename RngType>
57 class RngSet<RngType, Scalar>
58 {
59  public :
60 
61  typedef RngType rng_type;
62  typedef std::size_t size_type;
63 
64  explicit RngSet (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 () {rng_.seed(Seed::instance().get());}
71 
72  rng_type &operator[] (size_type) {return rng_;}
73 
74  private :
75 
76  std::size_t size_;
77  rng_type rng_;
78 }; // class RngSet
79 
82 template <typename RngType>
83 class RngSet<RngType, Vector>
84 {
85  public :
86 
87  typedef RngType rng_type;
88  typedef typename std::vector<rng_type, AlignedAllocator<rng_type> >::
90 
91  explicit RngSet (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  rng_.reserve(n);
104  rng_type rng;
105  for (std::size_t i = rng_.size(); i != n; ++i) {
106  rng.seed(Seed::instance().get());
107  rng_.push_back(rng);
108  }
109  }
110 
111  void seed ()
112  {
113  for (size_type i = 0; i != rng_.size(); ++i)
114  rng_[i].seed(Seed::instance().get());
115  }
116 
117  rng_type &operator[] (size_type id) {return rng_[id];}
118 
119  private :
120 
121  std::vector<rng_type, AlignedAllocator<rng_type> > rng_;
122 }; // class RngSet
123 
124 namespace traits {
125 
129 
130 } // namespace vsmc::traits
131 
132 } // namespace vsmc
133 
134 #endif // VSMC_RNG_RNG_SET_HPP
static SeedGenerator< ID, ResultType > & instance()
Definition: seed.hpp:94
Definition: adapter.hpp:37
void resize(std::size_t)
Definition: rng_set.hpp:68
void resize(std::size_t n)
Definition: rng_set.hpp:95
std::vector< rng_type, AlignedAllocator< rng_type > >::size_type size_type
Definition: rng_set.hpp:89
Class template argument used for scalar variant.
Definition: defines.hpp:109
#define VSMC_DEFINE_TYPE_DISPATCH_TRAIT(Outer, Inner, Default)
Definition: traits.hpp:40
#define VSMC_RNG_SET_TYPE
Default RNG set type.
Definition: rng_set.hpp:47
size_type size() const
Definition: rng_set.hpp:66
Class template argument used for vector variant.
Definition: defines.hpp:117
size_type size() const
Definition: rng_set.hpp:93