vSMC
vSMC: Scalable Monte Carlo
backend_gcd.hpp
Go to the documentation of this file.
1 //============================================================================
2 // vSMC/include/vsmc/smp/backend_gcd.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_GCD_HPP
33 #define VSMC_SMP_BACKEND_GCD_HPP
34 
36 #include <vsmc/gcd/gcd.hpp>
37 
38 namespace vsmc {
39 
41 
42 template <typename BaseState>
45 class StateGCD : public BaseState
46 {
47  public :
48 
50 
51  explicit StateGCD (size_type N) : BaseState(N) {}
52 }; // class StateGCD
53 
56 template <typename T, typename Derived>
57 class InitializeGCD : public InitializeBase<T, Derived>
58 {
59  public :
60 
61  std::size_t operator() (Particle<T> &particle, void *param)
62  {
63  typedef typename Particle<T>::size_type size_type;
64  const size_type N = static_cast<size_type>(particle.size());
65  this->initialize_param(particle, param);
66  this->pre_processor(particle);
67  accept_.resize(N);
68  work_param_ wp(this, &particle, &accept_[0]);
69  queue_.apply_f(N, &wp, work_);
70  this->post_processor(particle);
71 
72  std::size_t acc = 0;
73  for (size_type i = 0; i != N; ++i)
74  acc += accept_[i];
75 
76  return acc;
77  }
78 
79  protected :
80 
81  VSMC_DEFINE_SMP_IMPL_COPY(GCD, Initialize)
82 
83  private :
84 
86  std::vector<std::size_t> accept_;
87 
88  struct work_param_
89  {
90  work_param_ (InitializeGCD<T, Derived> *dptr, Particle<T> *pptr,
91  std::size_t *aptr) :
92  dispatcher(dptr), particle(pptr), accept(aptr) {}
93 
94  InitializeGCD<T, Derived> *const dispatcher;
95  Particle<T> *const particle;
96  std::size_t *const accept;
97  };
98 
99  static void work_ (void *wp, std::size_t i)
100  {
101  typedef typename Particle<T>::size_type size_type;
102  const work_param_ *const wptr = static_cast<const work_param_ *>(wp);
103  wptr->accept[i] = wptr->dispatcher->initialize_state(
104  SingleParticle<T>(static_cast<size_type>(i), wptr->particle));
105  }
106 }; // class InitializeGCD
107 
110 template <typename T, typename Derived>
111 class MoveGCD : public MoveBase<T, Derived>
112 {
113  public :
114 
115  std::size_t operator() (std::size_t iter, Particle<T> &particle)
116  {
117  typedef typename Particle<T>::size_type size_type;
118  const size_type N = static_cast<size_type>(particle.size());
119  this->pre_processor(iter, particle);
120  accept_.resize(N);
121  work_param_ wp(this, &particle, &accept_[0], iter);
122  queue_.apply_f(N, &wp, work_);
123  this->post_processor(iter, particle);
124 
125  std::size_t acc = 0;
126  for (size_type i = 0; i != N; ++i)
127  acc += accept_[i];
128 
129  return acc;
130  }
131 
132  protected :
133 
135 
136  private :
137 
139  std::vector<std::size_t> accept_;
140 
141  struct work_param_
142  {
143  work_param_ (MoveGCD<T, Derived> *dptr, Particle<T> *pptr,
144  std::size_t *aptr, std::size_t i) :
145  dispatcher(dptr), particle(pptr), accept(aptr), iter(i) {}
146 
147  MoveGCD<T, Derived> *const dispatcher;
148  Particle<T> *const particle;
149  std::size_t *const accept;
150  std::size_t iter;
151  };
152 
153  static void work_ (void *wp, std::size_t i)
154  {
155  typedef typename Particle<T>::size_type size_type;
156  const work_param_ *const wptr = static_cast<const work_param_ *>(wp);
157  wptr->accept[i] = wptr->dispatcher->move_state(wptr->iter,
158  SingleParticle<T>(static_cast<size_type>(i), wptr->particle));
159  }
160 }; // class MoveGCD
161 
164 template <typename T, typename Derived>
165 class MonitorEvalGCD : public MonitorEvalBase<T, Derived>
166 {
167  public :
168 
169  void operator() (std::size_t iter, std::size_t dim,
170  const Particle<T> &particle, double *res)
171  {
172  typedef typename Particle<T>::size_type size_type;
173  const size_type N = static_cast<size_type>(particle.size());
174  this->pre_processor(iter, particle);
175  work_param_ wp(this, &particle, res, iter, dim);
176  queue_.apply_f(N, &wp, work_);
177  this->post_processor(iter, particle);
178  }
179 
180  protected :
181 
182  VSMC_DEFINE_SMP_IMPL_COPY(GCD, MonitorEval)
183 
184  private :
185 
187 
188  struct work_param_
189  {
190  work_param_ (MonitorEvalGCD<T, Derived> *dptr, const Particle<T> *pptr,
191  double *rptr, std::size_t i, std::size_t d) :
192  dispatcher(dptr), particle(pptr), res(rptr), iter(i), dim(d) {}
193 
194  MonitorEvalGCD<T, Derived> *const dispatcher;
195  const Particle<T> *const particle;
196  double *const res;
197  std::size_t iter;
198  std::size_t dim;
199  };
200 
201  static void work_ (void *wp, std::size_t i)
202  {
203  typedef typename Particle<T>::size_type size_type;
204  const work_param_ *const wptr = static_cast<const work_param_ *>(wp);
205  wptr->dispatcher->monitor_state(wptr->iter, wptr->dim,
207  static_cast<size_type>(i), wptr->particle),
208  wptr->res + i * wptr->dim);
209  }
210 }; // class MonitorEvalGCD
211 
214 template <typename T, typename Derived>
215 class PathEvalGCD : public PathEvalBase<T, Derived>
216 {
217  public :
218 
219  double operator() (std::size_t iter, const Particle<T> &particle,
220  double *res)
221  {
222  typedef typename Particle<T>::size_type size_type;
223  const size_type N = static_cast<size_type>(particle.size());
224  this->pre_processor(iter, particle);
225  work_param_ wp(this, &particle, res, iter);
226  queue_.apply_f(N, &wp, work_);
227  this->post_processor(iter, particle);
228 
229  return this->path_grid(iter, particle);
230  }
231 
232  protected :
233 
235 
236  private :
237 
239 
240  struct work_param_
241  {
242  work_param_ (PathEvalGCD<T, Derived> *dptr, const Particle<T> *pptr,
243  double *rptr, std::size_t i) :
244  dispatcher(dptr), particle(pptr), res(rptr), iter(i) {}
245 
246  PathEvalGCD<T, Derived> *const dispatcher;
247  const Particle<T> *const particle;
248  double *const res;
249  std::size_t iter;
250  };
251 
252  static void work_ (void *wp, std::size_t i)
253  {
254  typedef typename Particle<T>::size_type size_type;
255  const work_param_ *const wptr = static_cast<const work_param_ *>(wp);
256  wptr->res[i] = wptr->dispatcher->path_state(wptr->iter,
258  static_cast<size_type>(i), wptr->particle));
259  }
260 }; // class PathEvalGCD
261 
262 } // namespace vsmc
263 
264 #endif // VSMC_SMP_BACKEND_GCD_HPP
Definition: adapter.hpp:37
Particle class representing the whole particle set.
Definition: particle.hpp:48
std::size_t operator()(std::size_t iter, Particle< T > &particle)
void initialize_param(Particle< T > &particle, void *param)
traits::SizeTypeTrait< BaseState >::type size_type
Definition: backend_gcd.hpp:49
void pre_processor(Particle< T > &particle)
Sampler::move_type subtype usingt Apple Grand Central Dispatch.
Definition: backend_gcd.hpp:40
std::size_t operator()(Particle< T > &particle, void *param)
Definition: backend_gcd.hpp:61
StateGCD(size_type N)
Definition: backend_gcd.hpp:51
STL namespace.
void post_processor(std::size_t iter, const Particle< T > &particle)
void operator()(std::size_t iter, std::size_t dim, const Particle< T > &particle, double *res)
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 usingt Apple Grand Central Dispatch.
Definition: backend_gcd.hpp:40
void pre_processor(std::size_t iter, Particle< T > &particle)
void post_processor(std::size_t iter, const Particle< T > &particle)
Particle::value_type subtype usingt Apple Grand Central Dispatch.
Definition: backend_gcd.hpp:45
void apply_f(std::size_t iterations, void *context, void(*work)(void *, std::size_t)) const
Monitor::eval_type subtype usingt Apple Grand Central Dispatch.
Definition: backend_gcd.hpp:40
void pre_processor(std::size_t iter, const Particle< T > &particle)
Sampler::init_type subtype usingt Apple Grand Central Dispatch.
Definition: backend_gcd.hpp:40
The queue obtained by dispatch_get_gloal_queue
traits::SizeTypeTrait< T >::type size_type
Definition: particle.hpp:52
internal::SizeTypeDispatch< T, value >::type type
Definition: traits.hpp:148
void post_processor(Particle< T > &particle)
size_type size() const
Number of particles.
Definition: particle.hpp:146
double operator()(std::size_t iter, const Particle< T > &particle, double *res)
A thin wrapper over a complete Particle.
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)