vSMC
vSMC: Scalable Monte Carlo
backend_omp.hpp
Go to the documentation of this file.
1 //============================================================================
2 // vSMC/include/vsmc/smp/backend_omp.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_OMP_HPP
33 #define VSMC_SMP_BACKEND_OMP_HPP
34 
36 #include <omp.h>
37 
38 namespace vsmc {
39 
41 
42 template <typename BaseState>
45 class StateOMP : public BaseState
46 {
47  public :
48 
49  typedef typename traits::OMPSizeTypeTrait<
51 
52  explicit StateOMP (size_type N) :
53  BaseState(static_cast<
54  typename traits::SizeTypeTrait<BaseState>::type>(N)) {}
55 
56  size_type size () const {return static_cast<size_type>(BaseState::size());}
57 
58  template <typename IntType>
59  void copy (size_type N, const IntType *copy_from)
60  {
62 
63 #pragma omp parallel for default(shared)
64  for (size_type to = 0; to < N; ++to)
65  this->copy_particle(copy_from[to], to);
66  }
67 }; // class StateOMP
68 
71 template <typename T, typename Derived>
72 class InitializeOMP : public InitializeBase<T, Derived>
73 {
74  public :
75 
76  std::size_t operator() (Particle<T> &particle, void *param)
77  {
78  typedef typename traits::OMPSizeTypeTrait<
79  typename Particle<T>::size_type>::type size_type;
80  const size_type N = static_cast<size_type>(particle.size());
81  this->initialize_param(particle, param);
82  this->pre_processor(particle);
83  std::size_t accept = 0;
84 #pragma omp parallel for reduction(+ : accept) default(shared)
85  for (size_type i = 0; i < N; ++i)
86  accept += this->initialize_state(SingleParticle<T>(i, &particle));
87  this->post_processor(particle);
88 
89  return accept;
90  }
91 
92  protected :
93 
94  VSMC_DEFINE_SMP_IMPL_COPY(OMP, Initialize)
95 }; // class InitializeOMP
96 
99 template <typename T, typename Derived>
100 class MoveOMP : public MoveBase<T, Derived>
101 {
102  public :
103 
104  std::size_t operator() (std::size_t iter, Particle<T> &particle)
105  {
106  typedef typename traits::OMPSizeTypeTrait<
107  typename Particle<T>::size_type>::type size_type;
108  const size_type N = static_cast<size_type>(particle.size());
109  this->pre_processor(iter, particle);
110  std::size_t accept = 0;
111 #pragma omp parallel for reduction(+ : accept) default(shared)
112  for (size_type i = 0; i < N; ++i)
113  accept += this->move_state(iter, SingleParticle<T>(i, &particle));
114  this->post_processor(iter, particle);
115 
116  return accept;
117  }
118 
119  protected :
120 
122 }; // class MoveOMP
123 
126 template <typename T, typename Derived>
127 class MonitorEvalOMP : public MonitorEvalBase<T, Derived>
128 {
129  public :
130 
131  void operator() (std::size_t iter, std::size_t dim,
132  const Particle<T> &particle, double *res)
133  {
134  typedef typename traits::OMPSizeTypeTrait<
135  typename Particle<T>::size_type>::type size_type;
136  const size_type N = static_cast<size_type>(particle.size());
137  this->pre_processor(iter, particle);
138 #pragma omp parallel for default(shared)
139  for (size_type i = 0; i < N; ++i) {
140  this->monitor_state(iter, dim,
141  ConstSingleParticle<T>(i, &particle), res + i * dim);
142  }
143  this->post_processor(iter, particle);
144  }
145 
146  protected :
147 
148  VSMC_DEFINE_SMP_IMPL_COPY(OMP, MonitorEval)
149 }; // class MonitorEvalOMP
150 
153 template <typename T, typename Derived>
154 class PathEvalOMP : public PathEvalBase<T, Derived>
155 {
156  public :
157 
158  double operator() (std::size_t iter, const Particle<T> &particle,
159  double *res)
160  {
161  typedef typename traits::OMPSizeTypeTrait<
162  typename Particle<T>::size_type>::type size_type;
163  const size_type N = static_cast<size_type>(particle.size());
164  this->pre_processor(iter, particle);
165 #pragma omp parallel for default(shared)
166  for (size_type i = 0; i < N; ++i) {
167  res[i] = this->path_state(iter,
168  ConstSingleParticle<T>(i, &particle));
169  }
170  this->post_processor(iter, particle);
171 
172  return this->path_grid(iter, particle);
173  }
174 
175  protected :
176 
178 }; // class PathEvalOMP
179 
180 } // namespace vsmc
181 
182 #endif // VSMC_SMP_BACKEND_OMP_HPP
Definition: adapter.hpp:37
Particle class representing the whole particle set.
Definition: particle.hpp:48
void initialize_param(Particle< T > &particle, void *param)
double operator()(std::size_t iter, const Particle< T > &particle, double *res)
Particle::value_type subtype using OpenMP.
Definition: backend_omp.hpp:45
traits::OMPSizeTypeTrait< typename traits::SizeTypeTrait< BaseState >::type >::type size_type
Definition: backend_omp.hpp:50
#define VSMC_RUNTIME_ASSERT_SMP_BACKEND_BASE_COPY_SIZE_MISMATCH(name)
void pre_processor(Particle< T > &particle)
Sampler::move_type subtype using OpenMP.
Definition: backend_omp.hpp:40
Monitor evalution base dispatch class.
void post_processor(std::size_t iter, const Particle< T > &particle)
Monitor::eval_type subtype using OpenMP.
Definition: backend_omp.hpp:40
Path::eval_type subtype using OpenMP.
Definition: backend_omp.hpp:40
std::size_t operator()(Particle< T > &particle, void *param)
Definition: backend_omp.hpp:76
size_type size() const
Definition: backend_omp.hpp:56
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)
StateOMP(size_type N)
Definition: backend_omp.hpp:52
void pre_processor(std::size_t iter, Particle< T > &particle)
void copy(size_type N, const IntType *copy_from)
Definition: backend_omp.hpp:59
void monitor_state(std::size_t iter, std::size_t dim, ConstSingleParticle< T > csp, double *res)
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.
traits::SizeTypeTrait< T >::type size_type
Definition: particle.hpp:52
internal::SizeTypeDispatch< T, value >::type type
Definition: traits.hpp:148
double path_state(std::size_t iter, ConstSingleParticle< T > csp)
void post_processor(Particle< T > &particle)
Path evalution base dispatch class.
size_type size() const
Number of particles.
Definition: particle.hpp:146
std::size_t operator()(std::size_t iter, Particle< T > &particle)
A thin wrapper over a complete Particle.
void pre_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)
#define VSMC_DEFINE_SMP_FORWARD(Name)
Definition: forward.hpp:38
A const variant to SingleParticle.
#define VSMC_DEFINE_SMP_IMPL_COPY(Impl, Name)