vSMC
vSMC: Scalable Monte Carlo
adapter.hpp
Go to the documentation of this file.
1 //============================================================================
2 // vSMC/include/vsmc/smp/adapter.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_ADAPTER_HPP
33 #define VSMC_SMP_ADAPTER_HPP
34 
35 #include <vsmc/internal/common.hpp>
36 #include <vsmc/core/adapter.hpp>
37 
38 namespace vsmc {
39 
42 template <typename T, template <typename, typename> class Impl, typename F>
43 class InitializeAdapter :
44  public InitializeAdapterBase<T, F,
45  Impl<T, InitializeAdapter<T, Impl, F> > >
46 {
47  typedef InitializeAdapterBase<T, F,
48  Impl<T, InitializeAdapter<T, Impl, F> > > base;
49 
50  public :
51 
53 
54  InitializeAdapter (const F &f) : base(f) {}
55 
57  {return this->implementation().initialize_state(part);}
58 }; // InitializeAdapter
59 
62 template <typename T, template <typename, typename> class Impl, typename F>
63 class MoveAdapter :
64  public MoveAdapterBase<T, F,
65  Impl<T, MoveAdapter<T, Impl, F> > >
66 {
67  typedef MoveAdapterBase<T, F,
68  Impl<T, MoveAdapter<T, Impl, F> > > base;
69 
70  public :
71 
73 
74  MoveAdapter (const F &f) : base(f) {}
75 
76  std::size_t move_state (std::size_t iter, SingleParticle<T> part)
77  {return this->implementation().move_state(iter, part);}
78 }; // MoveAdapter
79 
82 template <typename T, template <typename, typename> class Impl, typename F>
83 class MonitorEvalAdapter :
84  public MonitorEvalAdapterBase<T, F,
85  Impl<T, MonitorEvalAdapter<T, Impl, F> > >
86 {
87  typedef MonitorEvalAdapterBase<T, F,
88  Impl<T, MonitorEvalAdapter<T, Impl, F> > > base;
89 
90  public :
91 
93 
94  MonitorEvalAdapter (const F &f) : base(f) {}
95 
96  void monitor_state (std::size_t iter, std::size_t dim,
97  ConstSingleParticle<T> part, double *res)
98  {this->implementation().monitor_state(iter, dim, part, res);}
99 }; // MonitorEvalAdapter
100 
103 template <typename T, template <typename, typename> class Impl, typename F>
104 class PathEvalAdapter :
105  public PathEvalAdapterBase<T, F,
106  Impl<T, PathEvalAdapter<T, Impl, F> > >
107 {
108  typedef PathEvalAdapterBase<T, F,
109  Impl<T, PathEvalAdapter<T, Impl, F> > > base;
110 
111  public :
112 
114 
115  PathEvalAdapter (const F &f) : base(f) {}
116 
117  double path_state (std::size_t iter, ConstSingleParticle<T> part)
118  {return this->implementation().path_state(iter, part);}
119 }; // PathEvalAdapter
120 
123 template <typename T, template <typename, typename> class Impl>
124 class InitializeAdapter<T, Impl, NullType> :
125  public InitializeAdapterBase<T, NullType,
126  Impl<T, InitializeAdapter<T, Impl, NullType> > >
127 {
128  typedef InitializeAdapterBase<T, NullType,
129  Impl<T, InitializeAdapter<T, Impl, NullType> > > base;
130 
131  public :
132 
133  typedef cxx11::function<std::size_t (SingleParticle<T>)>
135  typedef typename base::initialize_param_type initialize_param_type;
136  typedef typename base::pre_processor_type pre_processor_type;
137  typedef typename base::post_processor_type post_processor_type;
138 
140  const initialize_param_type &init_param = initialize_param_type(),
141  const pre_processor_type &pre = pre_processor_type(),
142  const post_processor_type &post = post_processor_type()) :
143  base(init_param, pre, post), initialize_state_(init_state) {}
144 
146  {return initialize_state_(part);}
147 
148  private :
149 
150  const initialize_state_type initialize_state_;
151 }; // class InitializeAdapter
152 
155 template <typename T, template <typename, typename> class Impl>
156 class MoveAdapter<T, Impl, NullType> :
157  public MoveAdapterBase<T, NullType,
158  Impl<T, MoveAdapter<T, Impl, NullType> > >
159 {
160  typedef MoveAdapterBase<T, NullType,
161  Impl<T, MoveAdapter<T, Impl, NullType> > > base;
162 
163  public :
164 
165  typedef cxx11::function<std::size_t (std::size_t, SingleParticle<T>)>
167  typedef typename base::pre_processor_type pre_processor_type;
168  typedef typename base::post_processor_type post_processor_type;
169 
171  const pre_processor_type &pre = pre_processor_type(),
172  const post_processor_type &post = post_processor_type()) :
173  base(pre, post), move_state_(move_state) {}
174 
175  std::size_t move_state (std::size_t iter, SingleParticle<T> part)
176  {return move_state_(iter, part);}
177 
178  private :
179 
180  const move_state_type move_state_;
181 }; // class MoveAdapter
182 
185 template <typename T, template <typename, typename> class Impl>
186 class MonitorEvalAdapter<T, Impl, NullType> :
187  public MonitorEvalAdapterBase<T, NullType,
188  Impl<T, MonitorEvalAdapter<T, Impl, NullType> > >
189 {
190  typedef MonitorEvalAdapterBase<T, NullType,
191  Impl<T, MonitorEvalAdapter<T, Impl, NullType> > > base;
192 
193  public :
194 
195  typedef cxx11::function<
196  void (std::size_t, std::size_t, ConstSingleParticle<T>, double *)>
198  typedef typename base::pre_processor_type pre_processor_type;
199  typedef typename base::post_processor_type post_processor_type;
200 
202  const pre_processor_type &pre = pre_processor_type(),
203  const post_processor_type &post = post_processor_type()) :
204  base(pre, post), monitor_state_(monitor_state) {}
205 
206  void monitor_state (std::size_t iter, std::size_t dim,
207  ConstSingleParticle<T> part, double *res)
208  {monitor_state_(iter, dim, part, res);}
209 
210  private :
211 
212  const monitor_state_type monitor_state_;
213 }; // class MonitorEvalAdapter
214 
217 template <typename T, template <typename, typename> class Impl>
218 class PathEvalAdapter<T, Impl, NullType> :
219  public PathEvalAdapterBase<T, NullType,
220  Impl<T, PathEvalAdapter<T, Impl, NullType> > >
221 {
222  typedef PathEvalAdapterBase<T, NullType,
223  Impl<T, PathEvalAdapter<T, Impl, NullType> > > base;
224 
225  public :
226 
227  typedef cxx11::function<double (std::size_t, ConstSingleParticle<T>)>
229  typedef typename base::path_grid_type path_grid_type;
230  typedef typename base::pre_processor_type pre_processor_type;
231  typedef typename base::post_processor_type post_processor_type;
232 
234  const path_grid_type &path_grid,
235  const pre_processor_type &pre = pre_processor_type(),
236  const post_processor_type &post = post_processor_type()) :
237  base(path_grid, pre, post), path_state_(path_state) {}
238 
239  double path_state (std::size_t iter, ConstSingleParticle<T> part)
240  {return path_state_(iter, part);}
241 
242  private :
243 
244  const path_state_type path_state_;
245 }; // class PathEvalAdapter
246 
247 } // namespace vsmc
248 
249 #endif // VSMC_SMP_ADAPTER_HPP
Definition: adapter.hpp:37
std::size_t move_state(std::size_t iter, SingleParticle< T > part)
Definition: adapter.hpp:76
cxx11::function< void(std::size_t, std::size_t, ConstSingleParticle< T >, double *)> monitor_state_type
Definition: adapter.hpp:197
base::post_processor_type post_processor_type
Definition: adapter.hpp:137
InitializeAdapter(const F &f)
Definition: adapter.hpp:54
cxx11::function< std::size_t(std::size_t, SingleParticle< T >)> move_state_type
Definition: adapter.hpp:166
cxx11::function< double(std::size_t, ConstSingleParticle< T >)> path_state_type
Definition: adapter.hpp:228
MonitorEvalAdapter(const monitor_state_type &monitor_state, const pre_processor_type &pre=pre_processor_type(), const post_processor_type &post=post_processor_type())
Definition: adapter.hpp:201
double path_state(std::size_t iter, ConstSingleParticle< T > part)
Definition: adapter.hpp:117
std::size_t initialize_state(SingleParticle< T > part)
Definition: adapter.hpp:56
Monitor evaluation base.
Definition: adapter.hpp:163
Path evaluation class base.
Definition: adapter.hpp:213
Initialize class adapter base.
Definition: adapter.hpp:51
Path evaluation class adapter.
Definition: adapter.hpp:46
void monitor_state(std::size_t iter, std::size_t dim, ConstSingleParticle< T > part, double *res)
Definition: adapter.hpp:206
InitializeAdapter(const initialize_state_type &init_state, const initialize_param_type &init_param=initialize_param_type(), const pre_processor_type &pre=pre_processor_type(), const post_processor_type &post=post_processor_type())
Definition: adapter.hpp:139
base::initialize_param_type initialize_param_type
Definition: adapter.hpp:135
base::post_processor_type post_processor_type
Definition: adapter.hpp:199
PathEvalAdapter(const path_state_type &path_state, const path_grid_type &path_grid, const pre_processor_type &pre=pre_processor_type(), const post_processor_type &post=post_processor_type())
Definition: adapter.hpp:233
PathEvalAdapter(const F &f)
Definition: adapter.hpp:115
MoveAdapter(const move_state_type &move_state, const pre_processor_type &pre=pre_processor_type(), const post_processor_type &post=post_processor_type())
Definition: adapter.hpp:170
MonitorEvalAdapter(const F &f)
Definition: adapter.hpp:94
MoveAdapter(const F &f)
Definition: adapter.hpp:74
base::post_processor_type post_processor_type
Definition: adapter.hpp:168
base::pre_processor_type pre_processor_type
Definition: adapter.hpp:198
double path_grid(std::size_t iter, const Particle< T > &particle)
Definition: adapter.hpp:221
double path_state(std::size_t iter, ConstSingleParticle< T > part)
Definition: adapter.hpp:239
Move class adapter base.
Definition: adapter.hpp:113
std::size_t initialize_state(SingleParticle< T > part)
Definition: adapter.hpp:145
cxx11::function< std::size_t(SingleParticle< T >)> initialize_state_type
Definition: adapter.hpp:134
base::pre_processor_type pre_processor_type
Definition: adapter.hpp:230
void monitor_state(std::size_t iter, std::size_t dim, ConstSingleParticle< T > part, double *res)
Definition: adapter.hpp:96
Move class adapter.
Definition: adapter.hpp:42
base::pre_processor_type pre_processor_type
Definition: adapter.hpp:136
Monitor evaluation class adapter.
Definition: adapter.hpp:44
A thin wrapper over a complete Particle.
base::pre_processor_type pre_processor_type
Definition: adapter.hpp:167
base::post_processor_type post_processor_type
Definition: adapter.hpp:231
Initialize class adapter.
Definition: adapter.hpp:40
A const variant to SingleParticle.
std::size_t move_state(std::size_t iter, SingleParticle< T > part)
Definition: adapter.hpp:175