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-2016, 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 {
40 
42 
43 template <typename StateBase>
46 class StateOMP : public StateBase
47 {
48  public:
50 
51  explicit StateOMP(size_type N) : StateBase(N) {}
52 
53  template <typename IntType>
54  void copy(size_type N, const IntType *src_idx)
55  {
56 #pragma omp parallel for default(shared)
57  for (size_type i = 0; i < N; ++i)
58  this->copy_particle(static_cast<size_type>(src_idx[i]), i);
59  }
60 }; // class StateOMP
61 
64 template <typename T, typename Derived>
65 class InitializeOMP : public InitializeBase<T, Derived>
66 {
67  public:
68  std::size_t operator()(Particle<T> &particle, void *param)
69  {
70  using size_type = typename Particle<T>::size_type;
71  const size_type N = particle.size();
72  this->eval_param(particle, param);
73  this->eval_pre(particle);
74  std::size_t accept = 0;
75 #pragma omp parallel for reduction(+ : accept) default(shared)
76  for (size_type i = 0; i < N; ++i)
77  accept += this->eval_sp(particle.sp(i));
78  this->eval_post(particle);
79 
80  return accept;
81  }
82 
83  protected:
85 }; // class InitializeOMP
86 
89 template <typename T, typename Derived>
90 class MoveOMP : public MoveBase<T, Derived>
91 {
92  public:
93  std::size_t operator()(std::size_t iter, Particle<T> &particle)
94  {
95  using size_type = typename Particle<T>::size_type;
96  const size_type N = particle.size();
97  this->eval_pre(iter, particle);
98  std::size_t accept = 0;
99 #pragma omp parallel for reduction(+ : accept) default(shared)
100  for (size_type i = 0; i < N; ++i)
101  accept += this->eval_sp(iter, particle.sp(i));
102  this->eval_post(iter, particle);
103 
104  return accept;
105  }
106 
107  protected:
109 }; // class MoveOMP
110 
113 template <typename T, typename Derived>
114 class MonitorEvalOMP : public MonitorEvalBase<T, Derived>
115 {
116  public:
118  std::size_t iter, std::size_t dim, Particle<T> &particle, double *r)
119  {
120  using size_type = typename Particle<T>::size_type;
121  const size_type N = particle.size();
122  this->eval_pre(iter, particle);
123 #pragma omp parallel for default(shared)
124  for (size_type i = 0; i < N; ++i) {
125  this->eval_sp(iter, dim, particle.sp(i),
126  r + static_cast<std::size_t>(i) * dim);
127  }
128  this->eval_post(iter, particle);
129  }
130 
131  protected:
133 }; // class MonitorEvalOMP
134 
135 } // namespace vsmc
136 
137 #endif // VSMC_SMP_BACKEND_OMP_HPP
Definition: monitor.hpp:49
SizeType< T > size_type
Definition: particle.hpp:51
Particle class representing the whole particle set.
Definition: particle.hpp:48
sp_type sp(size_type id)
Get a SingleParticle<T> object.
Definition: particle.hpp:155
void eval_param(Particle< T > &particle, void *param)
Particle::value_type subtype using OpenMP.
Definition: backend_omp.hpp:46
std::size_t eval_sp(SingleParticle< T > sp)
void copy(size_type N, const IntType *src_idx)
Definition: backend_omp.hpp:54
Sampler<T>::move_type subtype using OpenMP.
Definition: backend_omp.hpp:41
Monitor evalution base dispatch class.
void operator()(std::size_t iter, std::size_t dim, Particle< T > &particle, double *r)
Monitor<T>::eval_type subtype using OpenMP.
Definition: backend_omp.hpp:41
std::size_t operator()(Particle< T > &particle, void *param)
Definition: backend_omp.hpp:68
StateOMP(size_type N)
Definition: backend_omp.hpp:51
#define VSMC_DEFINE_SMP_BACKEND_FORWARD(Name)
void eval_pre(Particle< T > &particle)
SizeType< StateBase > size_type
Definition: backend_omp.hpp:49
Move base dispatch class.
Sampler<T>::init_type subtype using OpenMP.
Definition: backend_omp.hpp:41
typename SizeTypeTrait< T >::type SizeType
Definition: traits.hpp:212
size_type size() const
Number of particles.
Definition: particle.hpp:122
void eval_post(Particle< T > &particle)
#define VSMC_DEFINE_SMP_BACKEND_SPECIAL(SMP, Name)
std::size_t operator()(std::size_t iter, Particle< T > &particle)
Definition: backend_omp.hpp:93
Initialize base dispatch class.