vSMC
vSMC: Scalable Monte Carlo
parallel_work.hpp
Go to the documentation of this file.
1 //============================================================================
2 // vSMC/include/vsmc/smp/internal/parallel_work.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_INTERNAL_PARALLEL_WORK_HPP
33 #define VSMC_SMP_INTERNAL_PARALLEL_WORK_HPP
34 
35 #include <vsmc/internal/common.hpp>
36 #include <vsmc/core/particle.hpp>
38 
39 namespace vsmc {
40 
41 namespace traits {
42 
43 VSMC_DEFINE_TYPE_DISPATCH_TRAIT(RangeTypeConstIterator, const_iterator,
44  std::size_t)
45 
46 } // namespace traits
47 
48 namespace internal {
49 
50 template <typename T, typename IntType>
52 {
53  public :
54 
55  ParallelCopyParticle (T *state, const IntType *copy_from) :
56  state_(state), copy_from_(copy_from) {}
57 
58  template <typename SizeType>
60  operator() (SizeType id) const
61  {
62  typedef typename traits::SizeTypeTrait<T>::type size_type;
63 
64  state_->copy_particle(
65  static_cast<size_type>(copy_from_[id]),
66  static_cast<size_type>(id));
67  }
68 
69  template <typename RangeType>
71  operator() (const RangeType &range) const
72  {
74  const_iterator;
75 
76  const const_iterator begin =
77  static_cast<const_iterator>(range.begin());
78  const const_iterator end =
79  static_cast<const_iterator>(range.end());
80  for (const_iterator id = begin; id != end; ++id)
81  operator()(id);
82  }
83 
84  private :
85 
86  T *const state_;
87  const IntType *const copy_from_;
88 }; // class ParallelCopyParticle
89 
90 template <typename T, typename InitType>
92 {
93  public :
94 
95  ParallelInitializeState (InitType *init, Particle<T> *particle) :
96  init_(init), particle_(particle), accept_(0) {}
97 
98  template <typename SplitType>
100  SplitType) :
101  init_(other.init_), particle_(other.particle_), accept_(0) {}
102 
103  template <typename SizeType>
105  operator() (SizeType id)
106  {
107  typedef typename traits::SizeTypeTrait<T>::type size_type;
108 
109  accept_ += init_->initialize_state(SingleParticle<T>(
110  static_cast<size_type>(id), particle_));
111  }
112 
113  template <typename RangeType>
115  operator() (const RangeType &range)
116  {
118  const_iterator;
119 
120  const const_iterator begin =
121  static_cast<const_iterator>(range.begin());
122  const const_iterator end =
123  static_cast<const_iterator>(range.end());
124  for (const_iterator id = begin; id != end; ++id)
125  operator()(id);
126  }
127 
129  {accept_ += other.accept_;}
130 
131  std::size_t accept () const {return accept_;}
132 
133  private :
134 
135  InitType *const init_;
136  Particle<T> *const particle_;
137  std::size_t accept_;
138 }; // class ParallelInitializeState
139 
140 template <typename T, typename MoveType>
142 {
143  public :
144 
146 
147  ParallelMoveState (MoveType *move, std::size_t iter,
148  Particle<T> *particle) :
149  move_(move), iter_(iter), particle_(particle), accept_(0) {}
150 
151  template <typename SplitType>
153  SplitType) :
154  move_(other.move_), iter_(other.iter_), particle_(other.particle_),
155  accept_(0) {}
156 
157  template <typename SizeType>
159  operator() (SizeType id)
160  {
161  typedef typename traits::SizeTypeTrait<T>::type size_type;
162 
163  accept_ += move_->move_state(iter_, SingleParticle<T>(
164  static_cast<size_type>(id), particle_));
165  }
166 
167  template <typename RangeType>
169  operator() (const RangeType &range)
170  {
172  const_iterator;
173 
174  const const_iterator begin =
175  static_cast<const_iterator>(range.begin());
176  const const_iterator end =
177  static_cast<const_iterator>(range.end());
178  for (const_iterator id = begin; id != end; ++id)
179  operator()(id);
180  }
181 
183  {accept_ += other.accept_;}
184 
185  std::size_t accept () const {return accept_;}
186 
187  private :
188 
189  MoveType *const move_;
190  const std::size_t iter_;
191  Particle<T> *const particle_;
192  std::size_t accept_;
193 }; // class ParallelMoveState
194 
195 template <typename T, typename MonitorEvalType>
197 {
198  public :
199 
200  ParallelMonitorState (MonitorEvalType *monitor,
201  std::size_t iter, std::size_t dim,
202  const Particle<T> *particle, double *res) :
203  monitor_(monitor), iter_(iter), dim_(dim),
204  particle_(particle), res_(res) {}
205 
206  template <typename SizeType>
208  operator() (SizeType id) const
209  {
210  typedef typename traits::SizeTypeTrait<T>::type size_type;
211 
212  monitor_->monitor_state(iter_, dim_,
213  ConstSingleParticle<T>(static_cast<size_type>(id), particle_),
214  res_ + id * dim_);
215  }
216 
217  template <typename RangeType>
219  operator() (const RangeType &range) const
220  {
222  const_iterator;
223 
224  const const_iterator begin =
225  static_cast<const_iterator>(range.begin());
226  const const_iterator end =
227  static_cast<const_iterator>(range.end());
228  for (const_iterator id = begin; id != end; ++id)
229  operator()(id);
230  }
231 
232  private :
233 
234  MonitorEvalType *const monitor_;
235  const std::size_t iter_;
236  const std::size_t dim_;
237  const Particle<T> *const particle_;
238  double *const res_;
239 }; // class ParallelMonitorState
240 
241 template <typename T, typename PathEvalType>
243 {
244  public :
245 
246  ParallelPathState (PathEvalType *path, std::size_t iter,
247  const Particle<T> *particle, double *res) :
248  path_(path), iter_(iter), particle_(particle), res_(res) {}
249 
250  template <typename SizeType>
252  operator() (SizeType id) const
253  {
254  typedef typename traits::SizeTypeTrait<T>::type size_type;
255 
256  res_[id] = path_->path_state(iter_,
257  ConstSingleParticle<T>(static_cast<size_type>(id), particle_));
258  }
259 
260  template <typename RangeType>
262  operator() (const RangeType &range) const
263  {
265  const_iterator;
266 
267  const const_iterator begin =
268  static_cast<const_iterator>(range.begin());
269  const const_iterator end =
270  static_cast<const_iterator>(range.end());
271  for (const_iterator id = begin; id != end; ++id)
272  operator()(id);
273  }
274 
275  private :
276 
277  PathEvalType *const path_;
278  const std::size_t iter_;
279  const Particle<T> *const particle_;
280  double *const res_;
281 }; // class ParallelPathState
282 
283 } // namespace vsmc::internal
284 
285 } // namespace vsmc
286 
287 #endif // VSMC_SMP_INTERNAL_PARALLEL_WORK_HPP
traits::SizeTypeTrait< T >::type size_type
Definition: adapter.hpp:37
void join(const ParallelInitializeState< T, InitType > &other)
Particle class representing the whole particle set.
Definition: particle.hpp:48
void join(const ParallelMoveState< T, MoveType > &other)
cxx11::enable_if< cxx11::is_integral< SizeType >::value >::type operator()(SizeType id)
internal::RangeTypeConstIteratorDispatch< T, value >::type type
cxx11::enable_if< cxx11::is_integral< SizeType >::value >::type operator()(SizeType id) const
ParallelInitializeState(const ParallelInitializeState< T, InitType > &other, SplitType)
#define VSMC_DEFINE_TYPE_DISPATCH_TRAIT(Outer, Inner, Default)
Definition: traits.hpp:40
cxx11::enable_if< cxx11::is_integral< SizeType >::value >::type operator()(SizeType id) const
remove_reference< T >::type && move(T &&t) noexcept
cxx11::enable_if< cxx11::is_integral< SizeType >::value >::type operator()(SizeType id)
ParallelCopyParticle(T *state, const IntType *copy_from)
internal::SizeTypeDispatch< T, value >::type type
Definition: traits.hpp:148
ParallelMoveState(MoveType *move, std::size_t iter, Particle< T > *particle)
ParallelPathState(PathEvalType *path, std::size_t iter, const Particle< T > *particle, double *res)
ParallelInitializeState(InitType *init, Particle< T > *particle)
A thin wrapper over a complete Particle.
ParallelMonitorState(MonitorEvalType *monitor, std::size_t iter, std::size_t dim, const Particle< T > *particle, double *res)
ParallelMoveState(const ParallelMoveState< T, MoveType > &other, SplitType)
A const variant to SingleParticle.
cxx11::enable_if< cxx11::is_integral< SizeType >::value >::type operator()(SizeType id) const