vSMC  v3.0.0
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 
41 namespace internal
42 {
43 
44 template <typename IntType>
45 inline void backend_omp_range(IntType N, IntType &begin, IntType &end)
46 {
47  const IntType np = static_cast<IntType>(::omp_get_num_threads());
48  const IntType id = static_cast<IntType>(::omp_get_thread_num());
49  const IntType m = N / np;
50  const IntType r = N % np;
51  const IntType n = m + (id < r ? 1 : 0);
52  begin = id < r ? n * id : (n + 1) * r + n * (id - r);
53  end = begin + n;
54 }
55 
56 } // namespace vsmc::internal
57 
60 template <typename T, typename Derived>
61 class SamplerEvalSMP<T, Derived, BackendOMP>
62  : public SamplerEvalBase<T, Derived>
63 {
64  public:
65  std::size_t operator()(std::size_t iter, Particle<T> &particle)
66  {
67  using size_type = typename Particle<T>::size_type;
68 
69  this->eval_pre(iter, particle);
70  std::size_t accept = 0;
71  Particle<T> *pptr = &particle;
72 #pragma omp parallel default(none) shared(accept) firstprivate(pptr, iter)
73  {
74  size_type begin = 0;
75  size_type end = 0;
76  internal::backend_omp_range(pptr->size(), begin, end);
77  std::size_t acc = this->eval_range(iter, pptr->range(begin, end));
78 #pragma omp atomic
79  accept += acc;
80  }
81  this->eval_post(iter, particle);
82 
83  return accept;
84  }
85 
86  protected:
87  VSMC_DEFINE_SMP_BACKEND_SPECIAL(OMP, SamplerEval)
88 }; // class SamplerEvalSMP
89 
92 template <typename T, typename Derived>
93 class MonitorEvalSMP<T, Derived, BackendOMP>
94  : public MonitorEvalBase<T, Derived>
95 {
96  public:
97  void operator()(
98  std::size_t iter, std::size_t dim, Particle<T> &particle, double *r)
99  {
100  using size_type = typename Particle<T>::size_type;
101 
102  this->eval_pre(iter, particle);
103  Particle<T> *pptr = &particle;
104 #pragma omp parallel default(none) firstprivate(pptr, iter, dim, r)
105  {
106  size_type begin = 0;
107  size_type end = 0;
108  internal::backend_omp_range(pptr->size(), begin, end);
109  this->eval_range(iter, dim, pptr->range(begin, end),
110  r + static_cast<std::size_t>(begin) * dim);
111  }
112  this->eval_post(iter, particle);
113  }
114 
115  protected:
116  VSMC_DEFINE_SMP_BACKEND_SPECIAL(OMP, MonitorEval)
117 }; // class MonitorEvalSMP
118 
121 template <typename T, typename Derived>
123 
126 template <typename T, typename Derived>
128 
129 } // namespace vsmc
130 
131 #endif // VSMC_SMP_BACKEND_OMP_HPP
range_type range(size_type begin, size_type end)
Get a ParticleRange<T> object.
Definition: particle.hpp:207
Definition: monitor.hpp:48
SizeType< T > size_type
Definition: particle.hpp:86
Particle class representing the whole particle set.
Definition: particle.hpp:83
void operator()(std::size_t iter, std::size_t dim, Particle< T > &particle, double *r)
Definition: backend_omp.hpp:97
Monitor<T>::eval_type.
Monitor evalution base dispatch class.
std::size_t operator()(std::size_t iter, Particle< T > &particle)
Definition: backend_omp.hpp:65
#define VSMC_DEFINE_SMP_BACKEND_SPECIAL(Impl, Name)
Monitor<T>::eval_type subtype using OpenMP.
Definition: backend_omp.hpp:93
void backend_omp_range(IntType N, IntType &begin, IntType &end)
Definition: backend_omp.hpp:45
Sampler evaluation base dispatch class.
Sampler<T>::eval_type.
Sampler<T>::eval_type subtype using OpenMP.
Definition: backend_omp.hpp:61
size_type size() const
Number of particles.
Definition: particle.hpp:120