vSMC
vSMC: Scalable Monte Carlo
backend_seq.hpp
Go to the documentation of this file.
1 //============================================================================
2 // vSMC/include/vsmc/smp/backend_seq.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_SMP_BACKEND_SEQ_HPP
33 #define VSMC_SMP_BACKEND_SEQ_HPP
34 
36 
37 namespace vsmc {
38 
40 
41 template <typename BaseState>
44 class StateSEQ : public BaseState
45 {
46  public :
47 
49 
50  explicit StateSEQ (size_type N) : BaseState(N) {}
51 }; // class StateSEQ
52 
55 template <typename T, typename Derived>
56 class InitializeSEQ : public InitializeBase<T, Derived>
57 {
58  public :
59 
60  std::size_t operator() (Particle<T> &particle, void *param)
61  {
62  typedef typename Particle<T>::size_type size_type;
63  const size_type N = static_cast<size_type>(particle.size());
64  this->initialize_param(particle, param);
65  this->pre_processor(particle);
66  std::size_t accept = 0;
67  for (size_type i = 0; i != N; ++i)
68  accept += this->initialize_state(SingleParticle<T>(i, &particle));
69  this->post_processor(particle);
70 
71  return accept;
72  }
73 
74  protected :
75 
76  VSMC_DEFINE_SMP_IMPL_COPY(SEQ, Initialize)
77 }; // class InitializeSEQ
78 
81 template <typename T, typename Derived>
82 class MoveSEQ : public MoveBase<T, Derived>
83 {
84  public :
85 
86  std::size_t operator() (std::size_t iter, Particle<T> &particle)
87  {
88  typedef typename Particle<T>::size_type size_type;
89  const size_type N = static_cast<size_type>(particle.size());
90  this->pre_processor(iter, particle);
91  std::size_t accept = 0;
92  for (size_type i = 0; i != N; ++i)
93  accept += this->move_state(iter, SingleParticle<T>(i, &particle));
94  this->post_processor(iter, particle);
95 
96  return accept;
97  }
98 
99  protected :
100 
102 }; // class MoveSEQ
103 
106 template <typename T, typename Derived>
107 class MonitorEvalSEQ : public MonitorEvalBase<T, Derived>
108 {
109  public :
110 
111  void operator() (std::size_t iter, std::size_t dim,
112  const Particle<T> &particle, double *res)
113  {
114  typedef typename Particle<T>::size_type size_type;
115  const size_type N = static_cast<size_type>(particle.size());
116  this->pre_processor(iter, particle);
117  for (size_type i = 0; i != N; ++i) {
118  this->monitor_state(iter, dim,
119  ConstSingleParticle<T>(i, &particle), res + i * dim);
120  }
121  this->post_processor(iter, particle);
122  }
123 
124  protected :
125 
126  VSMC_DEFINE_SMP_IMPL_COPY(SEQ, MonitorEval)
127 }; // class MonitorEvalSEQ
128 
131 template <typename T, typename Derived>
132 class PathEvalSEQ : public PathEvalBase<T, Derived>
133 {
134  public :
135 
136  double operator() (std::size_t iter, const Particle<T> &particle,
137  double *res)
138  {
139  typedef typename Particle<T>::size_type size_type;
140  const size_type N = static_cast<size_type>(particle.size());
141  this->pre_processor(iter, particle);
142  for (size_type i = 0; i != N; ++i) {
143  res[i] = this->path_state(iter,
144  ConstSingleParticle<T>(i, &particle));
145  }
146  this->post_processor(iter, particle);
147 
148  return this->path_grid(iter, particle);
149  }
150 
151  protected :
152 
154 }; // class PathEvalSEQ
155 
156 } // namespace vsmc
157 
158 #endif // VSMC_SMP_BACKEND_SEQ_HPP
double operator()(std::size_t iter, const Particle< T > &particle, double *res)
Definition: adapter.hpp:37
Particle class representing the whole particle set.
Definition: particle.hpp:48
void initialize_param(Particle< T > &particle, void *param)
traits::SizeTypeTrait< BaseState >::type size_type
Definition: backend_seq.hpp:48
std::size_t operator()(Particle< T > &particle, void *param)
Definition: backend_seq.hpp:60
void pre_processor(Particle< T > &particle)
StateSEQ(size_type N)
Definition: backend_seq.hpp:50
Monitor evalution base dispatch class.
void operator()(std::size_t iter, std::size_t dim, const Particle< T > &particle, double *res)
void post_processor(std::size_t iter, const Particle< T > &particle)
Monitor::eval_type subtype.
Definition: backend_seq.hpp:39
double path_grid(std::size_t iter, const Particle< T > &particle)
void post_processor(std::size_t iter, Particle< T > &particle)
std::size_t move_state(std::size_t iter, SingleParticle< T > sp)
void pre_processor(std::size_t iter, Particle< T > &particle)
void monitor_state(std::size_t iter, std::size_t dim, ConstSingleParticle< T > csp, double *res)
void post_processor(std::size_t iter, const Particle< T > &particle)
std::size_t operator()(std::size_t iter, Particle< T > &particle)
Definition: backend_seq.hpp:86
std::size_t initialize_state(SingleParticle< T > sp)
void pre_processor(std::size_t iter, const Particle< T > &particle)
Path::eval_type subtype.
Definition: backend_seq.hpp:39
Move base dispatch class.
traits::SizeTypeTrait< T >::type size_type
Definition: particle.hpp:52
internal::SizeTypeDispatch< T, value >::type type
Definition: traits.hpp:148
Particle::value_type subtype.
Definition: backend_seq.hpp:44
double path_state(std::size_t iter, ConstSingleParticle< T > csp)
void post_processor(Particle< T > &particle)
Path evalution base dispatch class.
size_type size() const
Number of particles.
Definition: particle.hpp:146
A thin wrapper over a complete Particle.
Sampler::move_type subtype.
Definition: backend_seq.hpp:39
void pre_processor(std::size_t iter, const Particle< T > &particle)
#define VSMC_DEFINE_SMP_FORWARD(Name)
Definition: forward.hpp:38
A const variant to SingleParticle.
#define VSMC_DEFINE_SMP_IMPL_COPY(Impl, Name)