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-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_SMP_BACKEND_SEQ_HPP
33 #define VSMC_SMP_BACKEND_SEQ_HPP
34 
36 
37 namespace vsmc
38 {
39 
41 
42 template <typename StateBase>
46 
49 template <typename T, typename Derived>
50 class InitializeSEQ : public InitializeBase<T, Derived>
51 {
52  public:
53  std::size_t operator()(Particle<T> &particle, void *param)
54  {
55  using size_type = typename Particle<T>::size_type;
56  const size_type N = particle.size();
57  this->eval_param(particle, param);
58  this->eval_pre(particle);
59  std::size_t accept = 0;
60  for (size_type i = 0; i != N; ++i)
61  accept += this->eval_sp(SingleParticle<T>(i, &particle));
62  this->eval_post(particle);
63 
64  return accept;
65  }
66 
67  protected:
69 }; // class InitializeSEQ
70 
73 template <typename T, typename Derived>
74 class MoveSEQ : public MoveBase<T, Derived>
75 {
76  public:
77  std::size_t operator()(std::size_t iter, Particle<T> &particle)
78  {
79  using size_type = typename Particle<T>::size_type;
80  const size_type N = particle.size();
81  this->eval_pre(iter, particle);
82  std::size_t accept = 0;
83  for (size_type i = 0; i != N; ++i)
84  accept += this->eval_sp(iter, SingleParticle<T>(i, &particle));
85  this->eval_post(iter, particle);
86 
87  return accept;
88  }
89 
90  protected:
92 }; // class MoveSEQ
93 
96 template <typename T, typename Derived>
97 class MonitorEvalSEQ : public MonitorEvalBase<T, Derived>
98 {
99  public:
101  std::size_t iter, std::size_t dim, Particle<T> &particle, double *r)
102  {
103  using size_type = typename Particle<T>::size_type;
104  const size_type N = particle.size();
105  this->eval_pre(iter, particle);
106  for (size_type i = 0; i != N; ++i) {
107  this->eval_sp(iter, dim, SingleParticle<T>(i, &particle),
108  r + static_cast<std::size_t>(i) * dim);
109  }
110  this->eval_post(iter, particle);
111  }
112 
113  protected:
115 }; // class MonitorEvalSEQ
116 
117 } // namespace vsmc
118 
119 #endif // VSMC_SMP_BACKEND_SEQ_HPP
Definition: monitor.hpp:49
SizeType< T > size_type
Definition: particle.hpp:51
Particle class representing the whole particle set.
Definition: particle.hpp:48
std::size_t operator()(Particle< T > &particle, void *param)
Definition: backend_seq.hpp:53
Monitor evalution base dispatch class.
Monitor<T>::eval_type subtype.
Definition: backend_seq.hpp:40
#define VSMC_DEFINE_SMP_BACKEND_FORWARD(Name)
Sampler<T>::init_type subtype.
Definition: backend_seq.hpp:40
std::size_t operator()(std::size_t iter, Particle< T > &particle)
Definition: backend_seq.hpp:77
void eval_post(std::size_t iter, Particle< T > &particle)
void eval_pre(std::size_t iter, Particle< T > &particle)
Move base dispatch class.
size_type size() const
Number of particles.
Definition: particle.hpp:122
std::size_t eval_sp(std::size_t iter, SingleParticle< T > sp)
#define VSMC_DEFINE_SMP_BACKEND_SPECIAL(SMP, Name)
A thin wrapper over a complete Particle.
void operator()(std::size_t iter, std::size_t dim, Particle< T > &particle, double *r)
Sampler<T>::move_type subtype.
Definition: backend_seq.hpp:40
Initialize base dispatch class.