vSMC
vSMC: Scalable Monte Carlo
backend_base.hpp
Go to the documentation of this file.
1 //============================================================================
2 // vSMC/include/vsmc/smp/backend_base.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_BASE_HPP
33 #define VSMC_SMP_BACKEND_BASE_HPP
34 
35 #include <vsmc/internal/common.hpp>
36 
37 #define VSMC_DEFINE_SMP_BASE_COPY(Name) \
38 Name##Base () {} \
39 Name##Base (const Name##Base<T, Derived> &) {} \
40 Name##Base<T, Derived> &operator= (const Name##Base<T, Derived> &) \
41 {return *this;} \
42 VSMC_CRTP_DESTRUCTOR_PREFIX ~Name##Base () {}
43 
44 #define VSMC_DEFINE_SMP_BASE_COPY_VIRTUAL(Name) \
45 Name##Base () {} \
46 Name##Base (const Name##Base<T, Virtual> &) {} \
47 Name##Base<T, Virtual> &operator= (const Name##Base<T, Virtual> &) \
48 {return *this;} \
49 virtual ~Name##Base () {}
50 
51 #define VSMC_DEFINE_SMP_IMPL_COPY(Impl, Name) \
52 Name##Impl () {} \
53 Name##Impl (const Name##Impl<T, Derived> &other) : \
54  Name##Base<T, Derived>(other) {} \
55 Name##Impl<T, Derived> &operator= (const Name##Impl<T, Derived> &other) \
56 { \
57  if (this != &other) \
58  Name##Base<T, Derived>::operator=(other); \
59  \
60  return *this; \
61 } \
62 ~Name##Impl () {}
63 
64 #define VSMC_RUNTIME_ASSERT_SMP_BACKEND_BASE_DERIVED(basename) \
65  VSMC_RUNTIME_ASSERT((dynamic_cast<Derived *>(this) != VSMC_NULLPTR), \
66  ("DERIVED FROM " #basename \
67  " WITH INCORRECT **Derived** TEMPLATE PARAMTER"));
68 
69 #define VSMC_RUNTIME_ASSERT_SMP_BACKEND_BASE_COPY_SIZE_MISMATCH(name) \
70  VSMC_RUNTIME_ASSERT((N == static_cast<size_type>(this->size())), \
71  ("**State"#name"::copy** SIZE MISMATCH"))
72 
73 namespace vsmc {
74 
77 template <typename T, typename Derived>
79 {
80  public :
81 
83  {return initialize_state_dispatch(sp, &Derived::initialize_state);}
84 
85  void initialize_param (Particle<T> &particle, void *param)
86  {initialize_param_dispatch(particle, param, &Derived::initialize_param);}
87 
88  void pre_processor (Particle<T> &particle)
89  {pre_processor_dispatch(particle, &Derived::pre_processor);}
90 
91  void post_processor (Particle<T> &particle)
92  {post_processor_dispatch(particle, &Derived::post_processor);}
93 
94  protected :
95 
97 
98  private :
99 
100  // non-static non-const
101 
102  template <typename D>
103  std::size_t initialize_state_dispatch (SingleParticle<T> sp,
104  std::size_t (D::*) (SingleParticle<T>))
105  {return static_cast<Derived *>(this)->initialize_state(sp);}
106 
107  template <typename D>
108  void initialize_param_dispatch (Particle<T> &particle, void *param,
109  void (D::*) (Particle<T> &, void *))
110  {static_cast<Derived *>(this)->initialize_param(particle, param);}
111 
112  template <typename D>
113  void pre_processor_dispatch (Particle<T> &particle,
114  void (D::*) (Particle<T> &))
115  {static_cast<Derived *>(this)->pre_processor(particle);}
116 
117  template <typename D>
118  void post_processor_dispatch (Particle<T> &particle,
119  void (D::*) (Particle<T> &))
120  {static_cast<Derived *>(this)->post_processor(particle);}
121 
122  // non-static const
123 
124  template <typename D>
125  std::size_t initialize_state_dispatch (SingleParticle<T> sp,
126  std::size_t (D::*) (SingleParticle<T>) const)
127  {return static_cast<Derived *>(this)->initialize_state(sp);}
128 
129  template <typename D>
130  void initialize_param_dispatch (Particle<T> &particle, void *param,
131  void (D::*) (Particle<T> &, void *) const)
132  {static_cast<Derived *>(this)->initialize_param(particle, param);}
133 
134  template <typename D>
135  void pre_processor_dispatch (Particle<T> &particle,
136  void (D::*) (Particle<T> &) const)
137  {static_cast<Derived *>(this)->pre_processor(particle);}
138 
139  template <typename D>
140  void post_processor_dispatch (Particle<T> &particle,
141  void (D::*) (Particle<T> &) const)
142  {static_cast<Derived *>(this)->post_processor(particle);}
143 
144  // static
145 
146  std::size_t initialize_state_dispatch (SingleParticle<T> sp,
147  std::size_t (*) (SingleParticle<T>))
148  {return Derived::initialize_state(sp);}
149 
150  void initialize_param_dispatch (Particle<T> &particle, void *param,
151  void (*) (Particle<T> &, void *))
152  {Derived::initialize_param(particle, param);}
153 
154  void pre_processor_dispatch (Particle<T> &particle,
155  void (*) (Particle<T> &))
156  {Derived::pre_processor(particle);}
157 
158  void post_processor_dispatch (Particle<T> &particle,
159  void (*) (Particle<T> &))
160  {Derived::post_processor(particle);}
161 
162  // base
163 
164  std::size_t initialize_state_dispatch (SingleParticle<T>,
165  std::size_t (InitializeBase::*) (SingleParticle<T>))
166  {return 0;}
167 
168  void initialize_param_dispatch (Particle<T> &, void *,
169  void (InitializeBase::*) (Particle<T> &, void *)) {}
170 
171  void pre_processor_dispatch (Particle<T> &,
172  void (InitializeBase::*) (Particle<T> &)) {}
173 
174  void post_processor_dispatch (Particle<T> &,
175  void (InitializeBase::*) (Particle<T> &)) {}
176 }; // class InitializeBase
177 
180 template <typename T>
181 class InitializeBase<T, Virtual>
182 {
183  public :
184 
185  virtual std::size_t initialize_state (SingleParticle<T>) {return 0;}
186  virtual void initialize_param (Particle<T> &, void *) {}
187  virtual void pre_processor (Particle<T> &) {}
188  virtual void post_processor (Particle<T> &) {}
189 
190  protected :
191 
193 }; // class InitializeBase<T, Virtual>
194 
197 template <typename T, typename Derived>
198 class MoveBase
199 {
200  public :
201 
202  std::size_t move_state (std::size_t iter, SingleParticle<T> sp)
203  {return move_state_dispatch(iter, sp, &Derived::move_state);}
204 
205  void pre_processor (std::size_t iter, Particle<T> &particle)
206  {pre_processor_dispatch(iter, particle, &Derived::pre_processor);}
207 
208  void post_processor (std::size_t iter, Particle<T> &particle)
209  {post_processor_dispatch(iter, particle, &Derived::post_processor);}
210 
211  protected :
212 
214 
215  private :
216 
217  // non-static non-const
218 
219  template <typename D>
220  std::size_t move_state_dispatch (std::size_t iter, SingleParticle<T> sp,
221  std::size_t (D::*) (std::size_t, SingleParticle<T>))
222  {return static_cast<Derived *>(this)->move_state(iter, sp);}
223 
224  template <typename D>
225  void pre_processor_dispatch (std::size_t iter, Particle<T> &particle,
226  void (D::*) (std::size_t, Particle<T> &))
227  {static_cast<Derived *>(this)->pre_processor(iter, particle);}
228 
229  template <typename D>
230  void post_processor_dispatch (std::size_t iter, Particle<T> &particle,
231  void (D::*) (std::size_t, Particle<T> &))
232  {static_cast<Derived *>(this)->post_processor(iter, particle);}
233 
234  // non-static const
235 
236  template <typename D>
237  std::size_t move_state_dispatch (std::size_t iter, SingleParticle<T> sp,
238  std::size_t (D::*) (std::size_t, SingleParticle<T>) const)
239  {return static_cast<Derived *>(this)->move_state(iter, sp);}
240 
241  template <typename D>
242  void pre_processor_dispatch (std::size_t iter, Particle<T> &particle,
243  void (D::*) (std::size_t, Particle<T> &) const)
244  {static_cast<Derived *>(this)->pre_processor(iter, particle);}
245 
246  template <typename D>
247  void post_processor_dispatch (std::size_t iter, Particle<T> &particle,
248  void (D::*) (std::size_t, Particle<T> &) const)
249  {static_cast<Derived *>(this)->post_processor(iter, particle);}
250 
251  // static
252 
253  std::size_t move_state_dispatch (std::size_t iter, SingleParticle<T> sp,
254  std::size_t (*) (std::size_t, SingleParticle<T>))
255  {return Derived::move_state(iter, sp);}
256 
257  void pre_processor_dispatch (std::size_t iter, Particle<T> &particle,
258  void (*) (std::size_t, Particle<T> &))
259  {Derived::pre_processor(iter, particle);}
260 
261  void post_processor_dispatch (std::size_t iter, Particle<T> &particle,
262  void (*) (std::size_t, Particle<T> &))
263  {Derived::post_processor(iter, particle);}
264 
265  // base
266 
267  std::size_t move_state_dispatch (std::size_t, SingleParticle<T>,
268  std::size_t (MoveBase::*) (std::size_t, SingleParticle<T>))
269  {return 0;}
270 
271  void pre_processor_dispatch (std::size_t, Particle<T> &,
272  void (MoveBase::*) (std::size_t, Particle<T> &)) {}
273 
274  void post_processor_dispatch (std::size_t, Particle<T> &,
275  void (MoveBase::*) (std::size_t, Particle<T> &)) {}
276 }; // class MoveBase
277 
280 template <typename T>
281 class MoveBase<T, Virtual>
282 {
283  public :
284 
285  virtual std::size_t move_state (std::size_t, SingleParticle<T>) {return 0;}
286  virtual void pre_processor (std::size_t, Particle<T> &) {}
287  virtual void post_processor (std::size_t, Particle<T> &) {}
288 
289  protected :
290 
292 }; // class MoveBase<T, Virtual>
293 
296 template <typename T, typename Derived>
298 {
299  public :
300 
301  void monitor_state (std::size_t iter, std::size_t dim,
302  ConstSingleParticle<T> csp, double *res)
303  {monitor_state_dispatch(iter, dim, csp, res, &Derived::monitor_state);}
304 
305  void pre_processor (std::size_t iter, const Particle<T> &particle)
306  {pre_processor_dispatch(iter, particle, &Derived::pre_processor);}
307 
308  void post_processor (std::size_t iter, const Particle<T> &particle)
309  {post_processor_dispatch(iter, particle, &Derived::post_processor);}
310 
311  protected :
312 
314 
315  private :
316 
317  // non-static non-const
318 
319  template <typename D>
320  void monitor_state_dispatch (std::size_t iter, std::size_t dim,
321  ConstSingleParticle<T> csp, double *res,
322  void (D::*) (std::size_t, std::size_t, ConstSingleParticle<T>,
323  double *))
324  {static_cast<Derived *>(this)->monitor_state(iter, dim, csp, res);}
325 
326  template <typename D>
327  void pre_processor_dispatch (std::size_t iter,
328  const Particle<T> &particle,
329  void (D::*) (std::size_t, const Particle<T> &))
330  {static_cast<Derived *>(this)->pre_processor(iter, particle);}
331 
332  template <typename D>
333  void post_processor_dispatch (std::size_t iter,
334  const Particle<T> &particle,
335  void (D::*) (std::size_t, const Particle<T> &))
336  {static_cast<Derived *>(this)->post_processor(iter, particle);}
337 
338  // non-static const
339 
340  template <typename D>
341  void monitor_state_dispatch (std::size_t iter, std::size_t dim,
342  ConstSingleParticle<T> csp, double *res,
343  void (D::*) (std::size_t, std::size_t, ConstSingleParticle<T>,
344  double *) const)
345  {static_cast<Derived *>(this)->monitor_state(iter, dim, csp, res);}
346 
347  template <typename D>
348  void pre_processor_dispatch (std::size_t iter,
349  const Particle<T> &particle,
350  void (D::*) (std::size_t, const Particle<T> &) const)
351  {static_cast<Derived *>(this)->pre_processor(iter, particle);}
352 
353  template <typename D>
354  void post_processor_dispatch (std::size_t iter,
355  const Particle<T> &particle,
356  void (D::*) (std::size_t, const Particle<T> &) const)
357  {static_cast<Derived *>(this)->post_processor(iter, particle);}
358 
359  // static
360 
361  void monitor_state_dispatch (std::size_t iter, std::size_t dim,
362  ConstSingleParticle<T> csp, double *res,
363  void (*) (std::size_t, std::size_t, ConstSingleParticle<T>,
364  double *))
365  {Derived::monitor_state(iter, dim, csp, res);}
366 
367  void pre_processor_dispatch (std::size_t iter,
368  const Particle<T> &particle,
369  void (*) (std::size_t, const Particle<T> &))
370  {Derived::pre_processor(iter, particle);}
371 
372  void post_processor_dispatch (std::size_t iter,
373  const Particle<T> &particle,
374  void (*) (std::size_t, const Particle<T> &))
375  {Derived::post_processor(iter, particle);}
376 
377  // base
378 
379  void monitor_state_dispatch (std::size_t, std::size_t ,
380  ConstSingleParticle<T>, double *,
381  void (MonitorEvalBase::*)
382  (std::size_t, std::size_t, ConstSingleParticle<T>, double *)) {}
383 
384  void pre_processor_dispatch (std::size_t, const Particle<T> &,
385  void (MonitorEvalBase::*) (std::size_t, const Particle<T> &)) {}
386 
387  void post_processor_dispatch (std::size_t, const Particle<T> &,
388  void (MonitorEvalBase::*) (std::size_t, const Particle<T> &)) {}
389 }; // class MonitorBase
390 
393 template <typename T>
394 class MonitorEvalBase<T, Virtual>
395 {
396  public :
397 
398  virtual void monitor_state (std::size_t, std::size_t,
399  ConstSingleParticle<T>, double *) {}
400  virtual void pre_processor (std::size_t, const Particle<T> &) {}
401  virtual void post_processor (std::size_t, const Particle<T> &) {}
402 
403  protected :
404 
406 }; // class MonitorEvalBase<T, Virtual>
407 
410 template <typename T, typename Derived>
412 {
413  public :
414 
415  double path_state (std::size_t iter, ConstSingleParticle<T> csp)
416  {return path_state_dispatch(iter, csp, &Derived::path_state);}
417 
418  double path_grid (std::size_t iter, const Particle<T> &particle)
419  {return path_grid_dispatch(iter, particle, &Derived::path_grid);}
420 
421  void pre_processor (std::size_t iter, const Particle<T> &particle)
422  {pre_processor_dispatch(iter, particle, &Derived::pre_processor);}
423 
424  void post_processor (std::size_t iter, const Particle<T> &particle)
425  {post_processor_dispatch(iter, particle, &Derived::post_processor);}
426 
427  protected :
428 
430 
431  private :
432 
433  // non-static non-const
434 
435  template <typename D>
436  double path_state_dispatch (std::size_t iter, ConstSingleParticle<T> csp,
437  double (D::*) (std::size_t, ConstSingleParticle<T>))
438  {return static_cast<Derived *>(this)->path_state(iter, csp);}
439 
440  template <typename D>
441  double path_grid_dispatch (std::size_t iter, const Particle<T> &particle,
442  double (D::*) (std::size_t, const Particle<T> &))
443  {return static_cast<Derived *>(this)->path_grid(iter, particle);}
444 
445  template <typename D>
446  void pre_processor_dispatch (std::size_t iter,
447  const Particle<T> &particle,
448  void (D::*) (std::size_t, const Particle<T> &))
449  {static_cast<Derived *>(this)->pre_processor(iter, particle);}
450 
451  template <typename D>
452  void post_processor_dispatch (std::size_t iter,
453  const Particle<T> &particle,
454  void (D::*) (std::size_t, const Particle<T> &))
455  {static_cast<Derived *>(this)->post_processor(iter, particle);}
456 
457  // non-static const
458 
459  template <typename D>
460  double path_state_dispatch (std::size_t iter, ConstSingleParticle<T> csp,
461  double (D::*) (std::size_t, ConstSingleParticle<T>) const)
462  {return static_cast<Derived *>(this)->path_state(iter, csp);}
463 
464  template <typename D>
465  double path_grid_dispatch (std::size_t iter, const Particle<T> &particle,
466  double (D::*) (std::size_t, const Particle<T> &) const)
467  {return static_cast<Derived *>(this)->path_grid(iter, particle);}
468 
469  template <typename D>
470  void pre_processor_dispatch (std::size_t iter,
471  const Particle<T> &particle,
472  void (D::*) (std::size_t, const Particle<T> &) const)
473  {static_cast<Derived *>(this)->pre_processor(iter, particle);}
474 
475  template <typename D>
476  void post_processor_dispatch (std::size_t iter,
477  const Particle<T> &particle,
478  void (D::*) (std::size_t, const Particle<T> &) const)
479  {static_cast<Derived *>(this)->post_processor(iter, particle);}
480 
481  // static
482 
483  double path_state_dispatch (std::size_t iter, ConstSingleParticle<T> csp,
484  double (*) (std::size_t, ConstSingleParticle<T>))
485  {return Derived::path_state(iter, csp);}
486 
487  double path_grid_dispatch (std::size_t iter, const Particle<T> &particle,
488  double (*) (std::size_t, const Particle<T> &))
489  {return Derived::path_grid(iter, particle);}
490 
491  void pre_processor_dispatch (std::size_t iter, const Particle<T> &particle,
492  void (*) (std::size_t, const Particle<T> &))
493  {Derived::pre_processor(iter, particle);}
494 
495  void post_processor_dispatch (std::size_t iter,
496  const Particle<T> &particle,
497  void (*) (std::size_t, const Particle<T> &))
498  {Derived::post_processor(iter, particle);}
499 
500  // base
501 
502  double path_state_dispatch (std::size_t, ConstSingleParticle<T>,
503  double (PathEvalBase::*) (std::size_t, ConstSingleParticle<T>))
504  {return 0;}
505 
506  double path_grid_dispatch (std::size_t, const Particle<T> &,
507  double (PathEvalBase::*) (std::size_t, const Particle<T> &))
508  {return 0;}
509 
510  void pre_processor_dispatch (std::size_t, const Particle<T> &,
511  void (PathEvalBase::*) (std::size_t, const Particle<T> &)) {}
512 
513  void post_processor_dispatch (std::size_t, const Particle<T> &,
514  void (PathEvalBase::*) (std::size_t, const Particle<T> &)) {}
515 }; // class PathEvalBase
516 
519 template <typename T>
520 class PathEvalBase<T, Virtual>
521 {
522  public :
523 
524  virtual double path_state (std::size_t, ConstSingleParticle<T>) {return 0;}
525  virtual double path_grid (std::size_t, const Particle<T> &) {return 0;}
526  virtual void pre_processor (std::size_t, const Particle<T> &) {}
527  virtual void post_processor (std::size_t, const Particle<T> &) {}
528 
529  protected :
530 
532 }; // class PathEval<T, Virtual>
533 
534 } // namespace vsmc
535 
536 #endif // VSMC_SMP_BACKEND_BASE_HPP
Definition: adapter.hpp:37
Particle class representing the whole particle set.
Definition: particle.hpp:48
void initialize_param(Particle< T > &particle, void *param)
virtual void pre_processor(std::size_t, const Particle< T > &)
virtual void post_processor(std::size_t, const Particle< T > &)
virtual void pre_processor(std::size_t, Particle< T > &)
void pre_processor(Particle< T > &particle)
Monitor evalution base dispatch class.
STL namespace.
void post_processor(std::size_t iter, const Particle< T > &particle)
virtual void pre_processor(Particle< T > &)
virtual double path_state(std::size_t, ConstSingleParticle< T >)
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)
virtual std::size_t initialize_state(SingleParticle< T >)
void pre_processor(std::size_t iter, Particle< T > &particle)
virtual void post_processor(std::size_t, const Particle< T > &)
virtual std::size_t move_state(std::size_t, SingleParticle< T >)
void monitor_state(std::size_t iter, std::size_t dim, ConstSingleParticle< T > csp, double *res)
#define VSMC_DEFINE_SMP_BASE_COPY(Name)
void post_processor(std::size_t iter, const Particle< T > &particle)
std::size_t initialize_state(SingleParticle< T > sp)
void pre_processor(std::size_t iter, const Particle< T > &particle)
Move base dispatch class.
virtual void post_processor(std::size_t, Particle< T > &)
double path_state(std::size_t iter, ConstSingleParticle< T > csp)
virtual void post_processor(Particle< T > &)
virtual double path_grid(std::size_t, const Particle< T > &)
void post_processor(Particle< T > &particle)
Path evalution base dispatch class.
#define VSMC_DEFINE_SMP_BASE_COPY_VIRTUAL(Name)
virtual void pre_processor(std::size_t, const Particle< T > &)
A thin wrapper over a complete Particle.
virtual void initialize_param(Particle< T > &, void *)
void pre_processor(std::size_t iter, const Particle< T > &particle)
A const variant to SingleParticle.
Initialize base dispatch class.
virtual void monitor_state(std::size_t, std::size_t, ConstSingleParticle< T >, double *)