vSMC
vSMC: Scalable Monte Carlo
common.hpp
Go to the documentation of this file.
1 //============================================================================
2 // vSMC/include/vsmc/resample/common.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_RESAMPLE_COMMON_HPP
33 #define VSMC_RESAMPLE_COMMON_HPP
34 
35 #include <vsmc/internal/common.hpp>
36 #if VSMC_HAS_AES_NI
37 #include <vsmc/rng/aes.hpp>
38 #include <vsmc/rng/ars.hpp>
39 #endif
40 #include <vsmc/rng/philox.hpp>
41 #include <vsmc/rng/threefry.hpp>
43 
46 #ifndef VSMC_RESAMPLE_RNG_TYPE
47 #define VSMC_RESAMPLE_RNG_TYPE ::vsmc::Threefry4x64
48 #endif
49 
50 namespace vsmc {
51 
52 namespace internal {
53 
54 class Inversion
55 {
56  public :
57 
58  // Given N U01 random variates,
59  // Compute M replication numbers based on M weights
60  template <typename IntType>
61  void operator() (std::size_t M, std::size_t N,
62  const double *weight, const double *u01, IntType *replication,
63  bool usorted = false)
64  {
65  if (M == 0)
66  return;
67 
68  if (M == 1) {
69  *replication = static_cast<IntType>(N);
70  return;
71  }
72 
73  std::memset(replication, 0, sizeof(IntType) * M);
74 
75  if (N == 0)
76  return;
77 
78  accw_.resize(M);
79  std::partial_sum(weight, weight + M, accw_.begin());
80  accw_.back() = 1;
81 
82  u01_.resize(N);
83  std::copy(u01, u01 + N, u01_.begin());
84  if (!usorted)
85  std::sort(u01_.begin(), u01_.end());
86  if (u01_.back() > 1)
87  u01_.back() = 1;
88 
89  std::size_t offset = 0;
90  for (std::size_t i = 0; i != M; ++i) {
91  while (offset != N && u01_[offset] <= accw_[i]) {
92  ++replication[i];
93  ++offset;
94  }
95  }
96  }
97 
98  private :
99 
100  std::vector<double, AlignedAllocator<double> > accw_;
101  std::vector<double, AlignedAllocator<double> > u01_;
102 }; // class Inversion
103 
104 } // namespace vsmc::internal
105 
115 }; // enum ResampleScheme
116 
119 template <typename> class Resample;
120 
123 template <ResampleScheme Scheme>
126 
133 {
134  public :
135 
136  template <typename IntType1, typename IntType2>
137  void operator() (std::size_t, std::size_t,
138  const IntType1 *, IntType2 *) const {}
139 }; // class ResampleCopyFromReplicationNoAaction
140 
144 {
145  public :
146 
147  template <typename IntType1, typename IntType2>
148  void operator() (std::size_t M, std::size_t N,
149  const IntType1 *replication, IntType2 *copy_from) const
150  {
151  if (M == N) {
152  IntType1 time = 0;
153  IntType2 from = 0;
154  for (std::size_t to = 0; to != N; ++to) {
155  if (replication[to] != 0) {
156  copy_from[to] = static_cast<IntType2>(to);
157  } else {
158  // replication[to] has zero child, copy from elsewhere
159  if (replication[from] < time + 2) {
160  // only 1 child left on replication[from]
161  time = 0;
162  do // move from to a position with at least 2 children
163  ++from;
164  while (replication[from] < 2);
165  }
166  copy_from[to] = from;
167  ++time;
168  }
169  }
170  } else {
171  std::size_t to = 0;
172  for (std::size_t from = 0; from != M; ++from)
173  for (IntType1 i = 0; i != replication[from]; ++i)
174  copy_from[to++] = static_cast<IntType2>(from);
175  }
176  }
177 }; // class ResampleCopyFromReplication
178 
180 {
181  public :
182 
183  template <typename WeightSetType>
184  void operator() (WeightSetType &weight_set) const
185  {weight_set.set_equal_weight();}
186 }; // class ResamplePostCopy
187 
188 namespace traits {
189 
192 VSMC_DEFINE_TYPE_DISPATCH_TRAIT(ResampleRngType, resample_rng_type,
194 
195 VSMC_DEFINE_TYPE_DISPATCH_TRAIT(ResampleCopyFromReplicationType,
198  resample_copy_from_replication_type, ResampleCopyFromReplication)
199 
202 VSMC_DEFINE_TYPE_DISPATCH_TRAIT(ResamplePostCopyType,
203  resample_post_copy_type, ResamplePostCopy)
204 
205 } // namespace vsmc::traits
206 
207 } // namespace vsmc
208 
209 #endif // VSMC_RESAMPLE_COMMON_HPP
Definition: adapter.hpp:37
Resampling type of the built-in schemes.
Definition: common.hpp:124
Transform replication numbers to parent particle locations.
Definition: common.hpp:132
Transform replication numbers to parent particle locations.
Definition: common.hpp:143
void operator()(std::size_t M, std::size_t N, const IntType1 *replication, IntType2 *copy_from) const
Definition: common.hpp:148
ResampleScheme
Resampling schemes.
Definition: common.hpp:108
Definition: common.hpp:179
Multinomial resampling.
Definition: common.hpp:109
void * memset(void *dst, int ch, std::size_t n)
SIMD optimized memset with non-temporal store for large buffers.
Definition: cstring.hpp:906
#define VSMC_DEFINE_TYPE_DISPATCH_TRAIT(Outer, Inner, Default)
Definition: traits.hpp:40
Stratified resampling.
Definition: common.hpp:111
#define VSMC_RESAMPLE_RNG_TYPE
Default RNG type for resampling.
Definition: common.hpp:47
Residual resampling.
Definition: common.hpp:110
Resample< cxx11::integral_constant< ResampleScheme, Scheme > > type
Definition: common.hpp:125
void operator()(WeightSetType &weight_set) const
Definition: common.hpp:184
Resample forward decleration.
Definition: common.hpp:119
Systematic resampling on residuals.
Definition: common.hpp:114
Stratified resampling on residuals.
Definition: common.hpp:113
void operator()(std::size_t M, std::size_t N, const double *weight, const double *u01, IntType *replication, bool usorted=false)
Definition: common.hpp:61
void operator()(std::size_t, std::size_t, const IntType1 *, IntType2 *) const
Definition: common.hpp:137
Systematic resampling.
Definition: common.hpp:112