vSMC
vSMC: Scalable Monte Carlo
backend_std.hpp
Go to the documentation of this file.
1 //============================================================================
2 // vSMC/include/vsmc/smp/backend_std.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_STD_HPP
33 #define VSMC_SMP_BACKEND_STD_HPP
34 
37 #include <vsmc/thread/thread.hpp>
38 
39 namespace vsmc {
40 
42 
43 template <typename BaseState>
46 class StateSTD : public BaseState
47 {
48  public :
49 
51 
52  explicit StateSTD (size_type N) : BaseState(N) {}
53 
54  template <typename IntType>
55  void copy (size_type N, const IntType *copy_from)
56  {
58 
61  StateSTD<BaseState>, IntType>(this, copy_from));
62  }
63 }; // class StateSTD
64 
67 template <typename T, typename Derived>
68 class InitializeSTD : public InitializeBase<T, Derived>
69 {
70  public :
71 
72  std::size_t operator() (Particle<T> &particle, void *param)
73  {
74  typedef typename Particle<T>::size_type size_type;
75  const size_type N = static_cast<size_type>(particle.size());
76  this->initialize_param(particle, param);
77  this->pre_processor(particle);
79  this, &particle);
81  this->post_processor(particle);
82 
83  return work.accept();
84  }
85 
86  protected :
87 
88  VSMC_DEFINE_SMP_IMPL_COPY(STD, Initialize)
89 }; // class InitializeSTD
90 
93 template <typename T, typename Derived>
94 class MoveSTD : public MoveBase<T, Derived>
95 {
96  public :
97 
98  std::size_t operator() (std::size_t iter, Particle<T> &particle)
99  {
100  typedef typename Particle<T>::size_type size_type;
101  const size_type N = static_cast<size_type>(particle.size());
102  this->pre_processor(iter, particle);
104  this, iter, &particle);
106  this->post_processor(iter, particle);
107 
108  return work.accept();
109  }
110 
111  protected :
112 
114 }; // class MoveSTD
115 
118 template <typename T, typename Derived>
119 class MonitorEvalSTD : public MonitorEvalBase<T, Derived>
120 {
121  public :
122 
123  void operator() (std::size_t iter, std::size_t dim,
124  const Particle<T> &particle, double *res)
125  {
126  typedef typename Particle<T>::size_type size_type;
127  const size_type N = static_cast<size_type>(particle.size());
128  this->pre_processor(iter, particle);
131  this, iter, dim, &particle, res));
132  this->post_processor(iter, particle);
133  }
134 
135  protected :
136 
137  VSMC_DEFINE_SMP_IMPL_COPY(STD, MonitorEval)
138 }; // class MonitorEvalSTD
139 
142 template <typename T, typename Derived>
143 class PathEvalSTD : public PathEvalBase<T, Derived>
144 {
145  public :
146 
147  double operator() (std::size_t iter, const Particle<T> &particle,
148  double *res)
149  {
150  typedef typename Particle<T>::size_type size_type;
151  const size_type N = static_cast<size_type>(particle.size());
152  this->pre_processor(iter, particle);
155  this, iter, &particle, res));
156  this->post_processor(iter, particle);
157 
158  return this->path_grid(iter, particle);
159  }
160 
161  protected :
162 
164 }; // PathEvalSTD
165 
166 } // namespace vsmc
167 
168 #endif // VSMC_SMP_BACKEND_STD_HPP
Sampler::move_type subtype using C++11 concurrency.
Definition: backend_std.hpp:41
Definition: adapter.hpp:37
Particle class representing the whole particle set.
Definition: particle.hpp:48
void initialize_param(Particle< T > &particle, void *param)
#define VSMC_RUNTIME_ASSERT_SMP_BACKEND_BASE_COPY_SIZE_MISMATCH(name)
void pre_processor(Particle< T > &particle)
Monitor evalution base dispatch class.
void parallel_reduce(const Range &range, WorkType &work)
Parallel reduce using C++11 concurrency.
void post_processor(std::size_t iter, const Particle< T > &particle)
double path_grid(std::size_t iter, const Particle< T > &particle)
void post_processor(std::size_t iter, Particle< T > &particle)
Path::eval_type subtype using C++11 concurrency.
Definition: backend_std.hpp:41
void pre_processor(std::size_t iter, Particle< T > &particle)
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)
Particle::value_type subtype using C++11 concurrency.
Definition: backend_std.hpp:46
void pre_processor(std::size_t iter, const Particle< T > &particle)
traits::SizeTypeTrait< BaseState >::type size_type
Definition: backend_std.hpp:50
void copy(size_type N, const IntType *copy_from)
Definition: backend_std.hpp:55
std::size_t operator()(Particle< T > &particle, void *param)
Definition: backend_std.hpp:72
Move base dispatch class.
double operator()(std::size_t iter, const Particle< T > &particle, double *res)
traits::SizeTypeTrait< T >::type size_type
Definition: particle.hpp:52
internal::SizeTypeDispatch< T, value >::type type
Definition: traits.hpp:148
Monitor::eval_type subtype using C++11 concurrency.
Definition: backend_std.hpp:41
Blocked range.
void post_processor(Particle< T > &particle)
Path evalution base dispatch class.
std::size_t operator()(std::size_t iter, Particle< T > &particle)
Definition: backend_std.hpp:98
size_type size() const
Number of particles.
Definition: particle.hpp:146
StateSTD(size_type N)
Definition: backend_std.hpp:52
void pre_processor(std::size_t iter, const Particle< T > &particle)
#define VSMC_DEFINE_SMP_FORWARD(Name)
Definition: forward.hpp:38
#define VSMC_DEFINE_SMP_IMPL_COPY(Impl, Name)
void parallel_for(const Range &range, WorkType &&work)
Parallel for using std::thread.