vSMC
vSMC: Scalable Monte Carlo
monitor.hpp
Go to the documentation of this file.
1 //============================================================================
2 // vSMC/include/vsmc/core/monitor.hpp
3 //----------------------------------------------------------------------------
4 // vSMC: Scalable Monte Carlo
5 //----------------------------------------------------------------------------
6 // Copyright (c) 2013-2015, 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_CORE_MONITOR_HPP
33 #define VSMC_CORE_MONITOR_HPP
34 
35 #include <vsmc/internal/common.hpp>
36 
37 #define VSMC_RUNTIME_ASSERT_CORE_MONITOR_ID(func) \
38  VSMC_RUNTIME_ASSERT( \
39  (id < dim()), "**Monitor::" #func "** INVALID ID NUMBER ARGUMENT")
40 
41 #define VSMC_RUNTIME_ASSERT_CORE_MONITOR_ITER(func) \
42  VSMC_RUNTIME_ASSERT((iter < iter_size()), \
43  "**Monitor::" #func "** INVALID ITERATION NUMBER ARGUMENT")
44 
45 #define VSMC_RUNTIME_ASSERT_CORE_MONITOR_EVAL \
46  VSMC_RUNTIME_ASSERT(static_cast<bool>(eval_), \
47  "**Monitor::eval** INVALID EVALUATION OBJECT")
48 
49 namespace vsmc
50 {
51 
58 }; // enum MonitorStage
59 
62 template <typename T>
63 class Monitor
64 {
65  public:
66  using value_type = T;
67  using eval_type =
68  std::function<void(std::size_t, std::size_t, Particle<T> &, double *)>;
69 
70  explicit Monitor(std::size_t dim, const eval_type &eval,
71  bool record_only = false, MonitorStage stage = MonitorMCMC)
72  : dim_(dim)
73  , eval_(eval)
74  , recording_(true)
75  , record_only_(record_only)
76  , stage_(stage)
77  , name_(dim)
78  {
79  }
80 
82  std::size_t dim() const { return dim_; }
83 
85  bool record_only() const { return record_only_; }
86 
88  MonitorStage stage() const { return stage_; }
89 
91  std::size_t iter_size() const { return index_.size(); }
92 
94  void reserve(std::size_t num)
95  {
96  index_.reserve(num);
97  record_.reserve(dim_ * num);
98  }
99 
101  bool empty() const { return !static_cast<bool>(eval_); }
102 
110  std::string &name(std::size_t id)
111  {
113 
114  return name_[id];
115  }
116 
118  const std::string &name(std::size_t id) const
119  {
121 
122  return name_[id];
123  }
124 
134  std::size_t index(std::size_t iter) const
135  {
137 
138  return index_[iter];
139  }
140 
146  double record(std::size_t id) const
147  {
148  std::size_t iter = iter_size() ? iter_size() - 1 : iter_size();
151 
152  return record_[iter * dim_ + id];
153  }
154 
160  double record(std::size_t id, std::size_t iter) const
161  {
164 
165  return record_[iter * dim_ + id];
166  }
167 
169  template <typename OutputIter>
170  void read_index(OutputIter first) const
171  {
172  std::copy(index_.begin(), index_.end(), first);
173  }
174 
176  const std::size_t *index_data() const { return index_.data(); }
177 
180  const double *record_data() const { return record_.data(); }
181 
184  const double *record_data(std::size_t iter) const
185  {
186  return record_.data() + iter * dim_;
187  }
188 
191  template <typename OutputIter>
192  void read_record(std::size_t id, OutputIter first) const
193  {
194  const std::size_t N = iter_size();
195  const double *riter = record_.data() + id;
196  for (std::size_t i = 0; i != N; ++i, ++first, riter += dim_)
197  *first = *riter;
198  }
199 
204  template <typename OutputIterIter>
205  void read_record_matrix(OutputIterIter first) const
206  {
207  for (std::size_t d = 0; d != dim_; ++d, ++first)
208  read_record(d, *first);
209  }
210 
221  template <MatrixOrder Order, typename OutputIter>
222  void read_record_matrix(OutputIter first) const
223  {
224  const std::size_t N = iter_size();
225  if (Order == ColMajor) {
226  for (std::size_t d = 0; d != dim_; ++d) {
227  const double *riter = record_.data() + d;
228  for (std::size_t i = 0; i != N; ++i, ++first, riter += dim_)
229  *first = *riter;
230  }
231  }
232 
233  if (Order == RowMajor)
234  std::copy(record_.begin(), record_.end(), first);
235  }
236 
238  void set_eval(const eval_type &new_eval) { eval_ = new_eval; }
239 
242  void eval(std::size_t iter, Particle<T> &particle, MonitorStage stage)
243  {
244  if (!recording_)
245  return;
246 
247  if (stage != stage_)
248  return;
249 
251 
252  result_.resize(dim_);
253  if (record_only_) {
254  eval_(iter, dim_, particle, result_.data());
255  push_back(iter);
256 
257  return;
258  }
259 
260  const std::size_t N = static_cast<std::size_t>(particle.size());
261  buffer_.resize(N * dim_);
262  eval_(iter, dim_, particle, buffer_.data());
263  gemv(ColMajor, NoTrans, dim_, N, 1.0, buffer_.data(), dim_,
264  particle.weight().data(), 1, 0.0, result_.data(), 1);
265  push_back(iter);
266  }
267 
269  void clear()
270  {
271  index_.clear();
272  record_.clear();
273  }
274 
276  bool recording() const { return recording_; }
277 
279  void turn_on() { recording_ = true; }
280 
282  void turn_off() { recording_ = false; }
283 
284  private:
285  std::size_t dim_;
286  eval_type eval_;
287  bool recording_;
288  bool record_only_;
289  MonitorStage stage_;
290  Vector<std::string> name_;
291  Vector<std::size_t> index_;
292  Vector<double> record_;
293  Vector<double> result_;
294  Vector<double> buffer_;
295 
296  void push_back(std::size_t iter)
297  {
298  index_.push_back(iter);
299  record_.insert(record_.end(), result_.begin(), result_.end());
300  }
301 }; // class Monitor
302 
303 } // namespace vsmc
304 
305 #endif // VSMC_CORE_MONITOR_HPP
bool empty() const
Whether the evaluation object is valid.
Definition: monitor.hpp:101
#define VSMC_RUNTIME_ASSERT_CORE_MONITOR_EVAL
Definition: monitor.hpp:45
Definition: monitor.hpp:49
void read_record(std::size_t id, OutputIter first) const
Read the record history for a given variable through an output iterator.
Definition: monitor.hpp:192
Particle class representing the whole particle set.
Definition: particle.hpp:48
bool record_only() const
If this is a record only Monitor.
Definition: monitor.hpp:85
MonitorStage
Monitor stage.
Definition: monitor.hpp:54
void read_index(OutputIter first) const
Read the index history through an output iterator.
Definition: monitor.hpp:170
void clear()
Clear all records of the index and integrations.
Definition: monitor.hpp:269
typename std::conditional< std::is_scalar< T >::value, std::vector< T, AlignedAllocator< T >>, std::vector< T >>::type Vector
AlignedVector for scalar type and std::vector for others.
weight_type & weight()
Read and write access to the weight collection object.
Definition: particle.hpp:131
void eval(std::size_t iter, Particle< T > &particle, MonitorStage stage)
Perform the evaluation for a given iteration and a Particle<T> object.
Definition: monitor.hpp:242
void reserve(std::size_t num)
Reserve space for a specified number of iterations.
Definition: monitor.hpp:94
void turn_off()
Turn off the recording.
Definition: monitor.hpp:282
Monitor evaluated after moves.
Definition: monitor.hpp:55
void turn_on()
Turn on the recording.
Definition: monitor.hpp:279
#define VSMC_RUNTIME_ASSERT_CORE_MONITOR_ITER(func)
Definition: monitor.hpp:41
void copy(std::size_t n, const T *x, std::size_t incx, T *y, std::size_t incy)
Copies vector to another vector.
Definition: cblas.hpp:82
Data are stored column by column in memory.
Definition: defines.hpp:55
Monitor(std::size_t dim, const eval_type &eval, bool record_only=false, MonitorStage stage=MonitorMCMC)
Definition: monitor.hpp:70
std::size_t dim() const
The dimension of the Monitor.
Definition: monitor.hpp:82
void set_eval(const eval_type &new_eval)
Set a new evaluation object of type eval_type.
Definition: monitor.hpp:238
const std::size_t * index_data() const
Read only access to the raw data of the index vector.
Definition: monitor.hpp:176
void read_record_matrix(OutputIter first) const
Read the record history of all variables through an output iterator.
Definition: monitor.hpp:222
std::size_t iter_size() const
The number of iterations has been recorded.
Definition: monitor.hpp:91
const double * record_data() const
Read only access to the raw data of records (a row major matrix)
Definition: monitor.hpp:180
Data are stored row by row in memory.
Definition: defines.hpp:54
std::string & name(std::size_t id)
Read and write access to the names of variables.
Definition: monitor.hpp:110
const std::string & name(std::size_t id) const
Read only access to the names of variables.
Definition: monitor.hpp:118
const double * record_data(std::size_t iter) const
Read only access to the raw data of records for a given Monitor iteration.
Definition: monitor.hpp:184
std::size_t index(std::size_t iter) const
Get the iteration index of the sampler of a given Monitor iteration.
Definition: monitor.hpp:134
MonitorStage stage() const
The stage of the Montior.
Definition: monitor.hpp:88
double record(std::size_t id) const
Get the latest Monte Carlo integration record of a given variable.
Definition: monitor.hpp:146
Monitor evaluated after MCMC moves.
Definition: monitor.hpp:57
Monitor for Monte Carlo integration.
Definition: monitor.hpp:63
#define VSMC_RUNTIME_ASSERT_CORE_MONITOR_ID(func)
Definition: monitor.hpp:37
void read_record_matrix(OutputIterIter first) const
Read the record history of all variables through an array of output iterators.
Definition: monitor.hpp:205
bool recording() const
Whether the Monitor is actively recording results.
Definition: monitor.hpp:276
size_type size() const
Number of particles.
Definition: particle.hpp:122
std::function< void(std::size_t, std::size_t, Particle< T > &, double *)> eval_type
Definition: monitor.hpp:68
double record(std::size_t id, std::size_t iter) const
Get the Monte Carlo integration record of a given variable and the Monitor iteration.
Definition: monitor.hpp:160
void gemv(MatrixOrder order, MatrixTrans trans, std::size_t m, std::size_t n, T alpha, const T *A, std::size_t lda, const T *x, std::size_t incx, T beta, T *y, std::size_t incy)
Computes a matrix-vector product using a general matrix.
Definition: cblas.hpp:129
The matrix shall not be transposed.
Definition: defines.hpp:61
Monitor evaluated after resampling.
Definition: monitor.hpp:56