vSMC
vSMC: Scalable Monte Carlo
backend_cilk.hpp
Go to the documentation of this file.
1 //============================================================================
2 // vSMC/include/vsmc/smp/backend_cilk.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_CILK_HPP
33 #define VSMC_SMP_BACKEND_CILK_HPP
34 
36 #include <cilk/cilk.h>
37 #include <cilk/cilk_api.h>
38 #include <cilk/reducer_opadd.h>
39 
40 namespace vsmc {
41 
43 
44 template <typename BaseState>
47 class StateCILK : public BaseState
48 {
49  public :
50 
52 
53  explicit StateCILK (size_type N) : BaseState(N) {}
54 
55  template <typename IntType>
56  void copy (size_type N, const IntType *copy_from)
57  {
59 
60  cilk_for (size_type to = 0; to != N; ++to)
61  this->copy_particle(copy_from[to], to);
62  }
63 }; // class StateCILK
64 
67 template <typename T, typename Derived>
68 class InitializeCILK : public InitializeBase<T, Derived>
69 {
70  public :
71 
72  std::size_t operator() (Particle<T> &particle, void *param)
73  {
74  typedef typename Particle<T>::size_type size_type;
75  const size_type N = static_cast<size_type>(particle.size());
76  this->initialize_param(particle, param);
77  this->pre_processor(particle);
78  cilk::reducer_opadd<std::size_t> accept;
79  cilk_for (size_type i = 0; i != N; ++i)
80  accept += this->initialize_state(SingleParticle<T>(i, &particle));
81  this->post_processor(particle);
82 
83  return accept.get_value();
84  }
85 
86  protected :
87 
88  VSMC_DEFINE_SMP_IMPL_COPY(CILK, Initialize)
89 }; // class InitializeCILK
90 
93 template <typename T, typename Derived>
94 class MoveCILK : public MoveBase<T, Derived>
95 {
96  public :
97 
98  std::size_t operator() (std::size_t iter, Particle<T> &particle)
99  {
100  typedef typename Particle<T>::size_type size_type;
101  const size_type N = static_cast<size_type>(particle.size());
102  this->pre_processor(iter, particle);
103  cilk::reducer_opadd<std::size_t> accept;
104  cilk_for (size_type i = 0; i != N; ++i)
105  accept += this->move_state(iter, SingleParticle<T>(i, &particle));
106  this->post_processor(iter, particle);
107 
108  return accept.get_value();
109  }
110 
111  protected :
112 
114 }; // class MoveCILK
115 
118 template <typename T, typename Derived>
119 class MonitorEvalCILK : public MonitorEvalBase<T, Derived>
120 {
121  public :
122 
123  void operator() (std::size_t iter, std::size_t dim,
124  const Particle<T> &particle, double *res)
125  {
126  typedef typename Particle<T>::size_type size_type;
127  const size_type N = static_cast<size_type>(particle.size());
128  this->pre_processor(iter, particle);
129  cilk_for (size_type i = 0; i != N; ++i) {
130  this->monitor_state(iter, dim,
131  ConstSingleParticle<T>(i, &particle), res + i * dim);
132  }
133  this->post_processor(iter, particle);
134  }
135 
136  protected :
137 
138  VSMC_DEFINE_SMP_IMPL_COPY(CILK, MonitorEval)
139 }; // class MonitorEvalCILK
140 
143 template <typename T, typename Derived>
144 class PathEvalCILK : public PathEvalBase<T, Derived>
145 {
146  public :
147 
148  double operator() (std::size_t iter, const Particle<T> &particle,
149  double *res)
150  {
151  typedef typename Particle<T>::size_type size_type;
152  const size_type N = static_cast<size_type>(particle.size());
153  this->pre_processor(iter, particle);
154  cilk_for (size_type i = 0; i != N; ++i) {
155  res[i] = this->path_state(iter,
156  ConstSingleParticle<T>(i, &particle));
157  }
158  this->post_processor(iter, particle);
159 
160  return this->path_grid(iter, particle);
161  }
162 
163  protected :
164 
166 }; // class PathEvalCILK
167 
168 } // namespace vsmc
169 
170 #endif // VSMC_SMP_BACKEND_CILK_HPP
Particle::value_type subtype using Intel Cilk Plus.
Definition: adapter.hpp:37
Particle class representing the whole particle set.
Definition: particle.hpp:48
void initialize_param(Particle< T > &particle, void *param)
traits::SizeTypeTrait< BaseState >::type size_type
#define VSMC_RUNTIME_ASSERT_SMP_BACKEND_BASE_COPY_SIZE_MISMATCH(name)
void pre_processor(Particle< T > &particle)
std::size_t operator()(std::size_t iter, Particle< T > &particle)
Monitor evalution base dispatch class.
void post_processor(std::size_t iter, const Particle< T > &particle)
void copy(size_type N, const IntType *copy_from)
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)
std::size_t operator()(Particle< T > &particle, void *param)
void pre_processor(std::size_t iter, Particle< T > &particle)
StateCILK(size_type N)
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)
Path::eval_type subtype using Intel Cilk Plus.
Move base dispatch class.
traits::SizeTypeTrait< T >::type size_type
Definition: particle.hpp:52
internal::SizeTypeDispatch< T, value >::type type
Definition: traits.hpp:148
Sampler::move_type subtype using Intel Cilk Plus.
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
A thin wrapper over a complete Particle.
Monitor::eval_type subtype using Intel Cilk Plus.
double operator()(std::size_t iter, const Particle< T > &particle, double *res)
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)