MCKL
Monte Carlo Kernel Library
backend_omp.hpp
Go to the documentation of this file.
1 //============================================================================
2 // MCKL/include/mckl/smp/backend_omp.hpp
3 //----------------------------------------------------------------------------
4 // MCKL: Monte Carlo Kernel Library
5 //----------------------------------------------------------------------------
6 // Copyright (c) 2013-2018, 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 MCKL_SMP_BACKEND_OMP_HPP
33 #define MCKL_SMP_BACKEND_OMP_HPP
34 
36 
37 #if MCKL_HAS_OMP
38 #include <omp.h>
39 #endif
40 
41 namespace mckl {
42 
43 namespace internal {
44 
45 template <typename IntType>
46 inline void backend_omp_range(IntType N, IntType &ibegin, IntType &iend)
47 {
48 #if MCKL_HAS_OMP
49  const IntType np = static_cast<IntType>(::omp_get_num_threads());
50  const IntType id = static_cast<IntType>(::omp_get_thread_num());
51 #else
52  const IntType np = 1;
53  const IntType id = 0;
54 #endif
55  const IntType m = N / np;
56  const IntType r = N % np;
57  const IntType n = m + (id < r ? 1 : 0);
58  ibegin = id < r ? n * id : (n + 1) * r + n * (id - r);
59  iend = ibegin + n;
60 }
61 
62 } // namespace internal
63 
66 template <typename T, typename Derived>
67 class SMCSamplerEvalSMP<T, Derived, BackendOMP>
68  : public SMCSamplerEvalBase<T, Derived>
69 {
70  public:
71  void operator()(std::size_t iter, Particle<T> &particle)
72  {
73  run(iter, particle);
74  }
75 
76  protected:
77  MCKL_DEFINE_SMP_BACKEND_SPECIAL(OMP, SMCSamplerEval)
78 
79  void run(std::size_t iter, Particle<T> &particle)
80  {
81  run(iter, particle, 1);
82  }
83 
84  template <typename... Args>
85  void run(std::size_t iter, Particle<T> &particle, std::size_t, Args &&...)
86  {
87  using size_type = typename Particle<T>::size_type;
88 
89  this->eval_first(iter, particle);
90  Particle<T> *pptr = &particle;
91 #if MCKL_HAS_OMP
92 #pragma omp parallel default(none) firstprivate(pptr, iter)
93 #endif
94  {
95  size_type ibegin = 0;
96  size_type iend = 0;
97  internal::backend_omp_range(pptr->size(), ibegin, iend);
98  this->eval_range(iter, pptr->range(ibegin, iend));
99  }
100  this->eval_last(iter, particle);
101  }
102 }; // class SMCSamplerEvalSMP
103 
106 template <typename T, typename Derived>
107 class SMCEstimatorEvalSMP<T, Derived, BackendOMP>
108  : public SMCEstimatorEvalBase<T, Derived>
109 {
110  public:
112  std::size_t iter, std::size_t dim, Particle<T> &particle, double *r)
113  {
114  run(iter, dim, particle, r);
115  }
116 
117  protected:
118  MCKL_DEFINE_SMP_BACKEND_SPECIAL(OMP, SMCEstimatorEval)
119 
120  void run(
121  std::size_t iter, std::size_t dim, Particle<T> &particle, double *r)
122  {
123  run(iter, dim, particle, r, 1);
124  }
125 
126  template <typename... Args>
127  void run(std::size_t iter, std::size_t dim, Particle<T> &particle,
128  double *r, std::size_t, Args &&...)
129  {
130  using size_type = typename Particle<T>::size_type;
131 
132  this->eval_first(iter, particle);
133  Particle<T> *pptr = &particle;
134 #if MCKL_HAS_OMP
135 #pragma omp parallel default(none) firstprivate(pptr, iter, dim, r)
136 #endif
137  {
138  size_type ibegin = 0;
139  size_type iend = 0;
140  internal::backend_omp_range(pptr->size(), ibegin, iend);
141  this->eval_range(iter, dim, pptr->range(ibegin, iend),
142  r + static_cast<std::size_t>(ibegin) * dim);
143  }
144  this->eval_last(iter, particle);
145  }
146 }; // class SMCEstimatorEvalSMP
147 
150 template <typename T, typename Derived>
152 
155 template <typename T, typename Derived>
157 
158 } // namespace mckl
159 
160 #endif // MCKL_SMP_BACKEND_OMP_HPP
SMCSampler evaluation base dispatch class.
void run(std::size_t iter, std::size_t dim, Particle< T > &particle, double *r, std::size_t, Args &&...)
STL namespace.
SMCEstimator<T>::eval_type subtype using OpenMP.
void run(std::size_t iter, Particle< T > &particle, std::size_t, Args &&...)
Definition: backend_omp.hpp:85
void backend_omp_range(IntType N, IntType &ibegin, IntType &iend)
Definition: backend_omp.hpp:46
SMCSMCSampler<T>::eval_type.
void operator()(std::size_t iter, std::size_t dim, Particle< T > &particle, double *r)
ParticleRange< T > range(size_type grainsize=1)
Get a Range<ParticleItrator<T>> object of all particles.
Definition: particle.hpp:391
SMCEstimator<T>::eval_type.
size_type size() const
The number of particles.
Definition: particle.hpp:318
SizeType< T > size_type
Definition: particle.hpp:282
Definition: mcmc.hpp:40
SMCEstimator evalution base dispatch class.
Particle system.
Definition: particle.hpp:45
SMCSampler<T>::eval_type subtype using OpenMP.
Definition: backend_omp.hpp:67
void operator()(std::size_t iter, Particle< T > &particle)
Definition: backend_omp.hpp:71
#define MCKL_DEFINE_SMP_BACKEND_SPECIAL(Impl, Name)