MCKL
Monte Carlo Kernel Library
backend_tbb.hpp
Go to the documentation of this file.
1 //============================================================================
2 // MCKL/include/mckl/smp/backend_tbb.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_TBB_HPP
33 #define MCKL_SMP_BACKEND_TBB_HPP
34 
36 #include <tbb/blocked_range.h>
37 #include <tbb/parallel_for.h>
38 
39 namespace mckl {
40 
41 namespace internal {
42 
43 template <typename IntType>
44 inline ::tbb::blocked_range<IntType> backend_tbb_range(
45  IntType N, std::size_t grainsize)
46 {
47  return grainsize == 0 ? ::tbb::blocked_range<IntType>(0, N) :
48  ::tbb::blocked_range<IntType>(0, N, grainsize);
49 }
50 
51 } // namespace internal
52 
56 template <typename T, typename Derived>
57 class SMCSamplerEvalSMP<T, Derived, BackendTBB>
58  : public SMCSamplerEvalBase<T, Derived>
59 {
60  class work_type
61  {
62  public:
63  using size_type = typename Particle<T>::size_type;
64 
66  std::size_t iter, Particle<T> *pptr)
67  : wptr_(wptr), iter_(iter), pptr_(pptr)
68  {
69  }
70 
71  void operator()(const ::tbb::blocked_range<size_type> &range) const
72  {
73  wptr_->eval_range(iter_, pptr_->range(range.begin(), range.end()));
74  }
75 
76  private:
78  const std::size_t iter_;
79  Particle<T> *const pptr_;
80  }; // class work_type
81 
82  public:
83  void operator()(std::size_t iter, Particle<T> &particle)
84  {
85  run(iter, particle);
86  }
87 
88  protected:
89  MCKL_DEFINE_SMP_BACKEND_SPECIAL(TBB, SMCSamplerEval)
90 
91  void run(std::size_t iter, Particle<T> &particle)
92  {
93  run(iter, particle, 1);
94  }
95 
96  template <typename... Args>
97  void run(std::size_t iter, Particle<T> &particle, std::size_t grainsize,
98  Args &&... args)
99  {
100  this->eval_first(iter, particle);
101  ::tbb::parallel_for(
102  internal::backend_tbb_range(particle.size(), grainsize),
103  work_type(this, iter, &particle), std::forward<Args>(args)...);
104  this->eval_last(iter, particle);
105  }
106 }; // class SMCSamplerEvalSMP
107 
111 template <typename T, typename Derived>
112 class SMCEstimatorEvalSMP<T, Derived, BackendTBB>
113  : public SMCEstimatorEvalBase<T, Derived>
114 {
115  class work_type
116  {
117  public:
118  using size_type = typename Particle<T>::size_type;
119 
121  std::size_t iter, std::size_t dim, Particle<T> *pptr, double *r)
122  : wptr_(wptr), iter_(iter), dim_(dim), pptr_(pptr), r_(r)
123  {
124  }
125 
126  void operator()(const ::tbb::blocked_range<size_type> &range) const
127  {
128  wptr_->eval_range(iter_, dim_,
129  pptr_->range(range.begin(), range.end()),
130  r_ + static_cast<std::size_t>(range.begin()) * dim_);
131  }
132 
133  private:
135  const std::size_t iter_;
136  const std::size_t dim_;
137  Particle<T> *const pptr_;
138  double *const r_;
139  }; // class work_type
140 
141  public:
143  std::size_t iter, std::size_t dim, Particle<T> &particle, double *r)
144  {
145  run(iter, dim, particle, r);
146  }
147 
148  protected:
149  MCKL_DEFINE_SMP_BACKEND_SPECIAL(TBB, SMCEstimatorEval)
150 
151  void run(
152  std::size_t iter, std::size_t dim, Particle<T> &particle, double *r)
153  {
154  run(iter, dim, particle, r, 1);
155  }
156 
157  template <typename... Args>
158  void run(std::size_t iter, std::size_t dim, Particle<T> &particle,
159  double *r, std::size_t grainsize, Args &&... args)
160  {
161  this->eval_first(iter, particle);
162  ::tbb::parallel_for(
163  internal::backend_tbb_range(particle.size(), grainsize),
164  work_type(this, iter, dim, &particle, r),
165  std::forward<Args>(args)...);
166  this->eval_last(iter, particle);
167  }
168 }; // class SMCEstimatorEvalSMP
169 
173 template <typename T, typename Derived>
175 
179 template <typename T, typename Derived>
181 
182 } // namespace mckl
183 
184 #endif // MCKL_SMP_BACKEND_TBB_HPP
void operator()(std::size_t iter, Particle< T > &particle)
Definition: backend_tbb.hpp:83
SMCSampler evaluation base dispatch class.
void run(std::size_t iter, std::size_t dim, Particle< T > &particle, double *r, std::size_t grainsize, Args &&... args)
STL namespace.
SMCSampler<T>::eval_type subtype using Intel Threading Building Blocks.
Definition: backend_tbb.hpp:57
SMCSMCSampler<T>::eval_type.
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.
void run(std::size_t iter, Particle< T > &particle, std::size_t grainsize, Args &&... args)
Definition: backend_tbb.hpp:97
void operator()(std::size_t iter, std::size_t dim, Particle< T > &particle, double *r)
inline ::tbb::blocked_range< IntType > backend_tbb_range(IntType N, std::size_t grainsize)
Definition: backend_tbb.hpp:44
Particle system.
Definition: particle.hpp:45
#define MCKL_DEFINE_SMP_BACKEND_SPECIAL(Impl, Name)
SMCEstimator<T>::eval_type subtype using Intel Threading Building Blocks.