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,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_CORE_MONITOR_HPP
33 #define VSMC_CORE_MONITOR_HPP
34 
35 #include <vsmc/internal/common.hpp>
38 
39 #define VSMC_RUNTIME_ASSERT_CORE_MONITOR_ID(func) \
40  VSMC_RUNTIME_ASSERT((id < dim()), \
41  ("**Monitor::"#func"** INVALID ID NUMBER ARGUMENT"))
42 
43 #define VSMC_RUNTIME_ASSERT_CORE_MONITOR_ITER(func) \
44  VSMC_RUNTIME_ASSERT((iter < iter_size()), \
45  ("**Monitor::"#func"** INVALID ITERATION NUMBER ARGUMENT"))
46 
47 #define VSMC_RUNTIME_ASSERT_CORE_MONITOR_FUNCTOR(func, caller, name) \
48  VSMC_RUNTIME_ASSERT(static_cast<bool>(func), \
49  ("**Monitor::"#caller"** INVALID "#name" OBJECT")) \
50 
51 namespace vsmc {
52 
55 template <typename T>
56 class Monitor
57 {
58  public :
59 
60  typedef T value_type;
61  typedef cxx11::function<
62  void (std::size_t, std::size_t, const Particle<T> &, double *)>
64 
98  explicit Monitor (std::size_t dim, const eval_type &eval,
99  bool record_only = false) :
100  dim_(dim), eval_(eval), recording_(true), record_only_(record_only),
101  name_(dim) {}
102 
104  std::size_t dim () const {return dim_;}
105 
113  std::size_t iter_size () const {return index_.size();}
114 
116  void reserve (std::size_t num)
117  {
118  index_.reserve(num);
119  record_.reserve(dim_ * num);
120  }
121 
123  bool empty () const {return !static_cast<bool>(eval_);}
124 
132  std::string &name (std::size_t id)
133  {
135 
136  return name_[id];
137  }
138 
140  const std::string &name (std::size_t id) const
141  {
143 
144  return name_[id];
145  }
146 
156  std::size_t index (std::size_t iter) const
157  {
159 
160  return index_[iter];
161  }
162 
168  double record (std::size_t id) const
169  {
170  std::size_t iter = iter_size() ? iter_size() - 1 : iter_size();
173 
174  return record_[iter * dim_ + id];
175  }
176 
182  double record (std::size_t id, std::size_t iter) const
183  {
186 
187  return record_[iter * dim_ + id];
188  }
189 
191  template <typename OutputIter>
192  void read_index (OutputIter first) const
193  {std::copy(index_.begin(), index_.end(), first);}
194 
196  const std::size_t *index_data () const {return &index_[0];}
197 
199  const double *record_data () const {return &record_[0];}
200 
203  template <typename OutputIter>
204  void read_record (std::size_t id, OutputIter first) const
205  {
206  const std::size_t N = iter_size();
207  const double *riter = &record_[id];
208  for (std::size_t i = 0; i != N; ++i, ++first, riter += dim_)
209  *first = *riter;
210  }
211 
216  template <typename OutputIterIter>
217  void read_record_matrix (OutputIterIter first) const
218  {
219  for (std::size_t d = 0; d != dim_; ++d, ++first)
220  read_record(d, *first);
221  }
222 
233  template <MatrixOrder Order, typename OutputIter>
234  void read_record_matrix (OutputIter first) const
235  {
236  const std::size_t N = iter_size();
237  if (Order == ColMajor) {
238  for (std::size_t d = 0; d != dim_; ++d) {
239  const double *riter = &record_[d];
240  for (std::size_t i = 0; i != N; ++i, ++first, riter += dim_)
241  *first = *riter;
242  }
243  }
244 
245  if (Order == RowMajor)
246  std::copy(record_.begin(), record_.end(), first);
247  }
248 
250  void set_eval (const eval_type &new_eval) {eval_ = new_eval;}
251 
261  void eval (std::size_t iter, const Particle<T> &particle)
262  {
263  if (!recording_)
264  return;
265 
267 
268  result_.resize(dim_);
269  double *const rptr = &result_[0];
270  if (record_only_) {
271  eval_(iter, dim_, particle, rptr);
272  push_back(iter, rptr);
273 
274  return;
275  }
276 
277  const std::size_t N = static_cast<std::size_t>(particle.size());
278  buffer_.resize(N * dim_);
279  double *const bptr = &buffer_[0];
280  const double *const wptr = particle.weight_set().weight_data();
281  eval_(iter, dim_, particle, bptr);
282  is_integrate_(static_cast<ISIntegrate::size_type>(N),
283  static_cast<ISIntegrate::size_type>(dim_), bptr, wptr, rptr);
284  push_back(iter, rptr);
285  }
286 
288  void clear ()
289  {
290  index_.clear();
291  record_.clear();
292  }
293 
295  bool recording () const {return recording_;}
296 
298  void turn_on () {recording_ = true;}
299 
301  void turn_off () {recording_ = false;}
302 
303  private :
304 
305  std::size_t dim_;
306  eval_type eval_;
307  bool recording_;
308  bool record_only_;
309  std::vector<std::string> name_;
310  std::vector<std::size_t> index_;
311  std::vector<double, AlignedAllocator<double> > record_;
312  std::vector<double, AlignedAllocator<double> > result_;
313  std::vector<double, AlignedAllocator<double> > buffer_;
314  ISIntegrate is_integrate_;
315 
316  void push_back (std::size_t iter, const double *rptr)
317  {
318  index_.push_back(iter);
319  for (std::size_t d = 0; d != dim_; ++d)
320  record_.push_back(rptr[d]);
321  }
322 }; // class Monitor
323 
324 } // namespace vsmc
325 
326 #endif // VSMC_CORE_MONITOR_HPP
bool empty() const
Whether the evaluation object is valid.
Definition: monitor.hpp:123
Definition: adapter.hpp:37
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:204
Particle class representing the whole particle set.
Definition: particle.hpp:48
Monitor(std::size_t dim, const eval_type &eval, bool record_only=false)
Construct a Monitor with an evaluation object.
Definition: monitor.hpp:98
void read_index(OutputIter first) const
Read the index history through an output iterator.
Definition: monitor.hpp:192
void eval(std::size_t iter, const Particle< T > &particle)
Perform the evaluation for a given iteration and a Particle object.
Definition: monitor.hpp:261
void clear()
Clear all records of the index and integrations.
Definition: monitor.hpp:288
void reserve(std::size_t num)
Reserve space for a specified number of iterations.
Definition: monitor.hpp:116
void turn_off()
Turn off the recording.
Definition: monitor.hpp:301
void turn_on()
Turn on the recording.
Definition: monitor.hpp:298
#define VSMC_RUNTIME_ASSERT_CORE_MONITOR_ITER(func)
Definition: monitor.hpp:43
Data are stored column by column in memory.
Definition: defines.hpp:104
cxx11::function< void(std::size_t, std::size_t, const Particle< T > &, double *)> eval_type
Definition: monitor.hpp:63
std::size_t dim() const
The dimension of the Monitor.
Definition: monitor.hpp:104
void set_eval(const eval_type &new_eval)
Set a new evaluation object of type eval_type.
Definition: monitor.hpp:250
const std::size_t * index_data() const
Read only access to the raw data of the index vector.
Definition: monitor.hpp:196
void read_record_matrix(OutputIter first) const
Read the record history of all variables through an output iterator.
Definition: monitor.hpp:234
std::size_t iter_size() const
The number of iterations has been recorded.
Definition: monitor.hpp:113
#define VSMC_RUNTIME_ASSERT_CORE_MONITOR_ID(func)
Definition: monitor.hpp:39
Compute the importance sampling integration of multivariate variable.
const double * record_data() const
Read only access to the raw data of records (a row major matrix)
Definition: monitor.hpp:199
Data are stored row by row in memory.
Definition: defines.hpp:103
std::string & name(std::size_t id)
Read and write access to the names of variables.
Definition: monitor.hpp:132
const std::string & name(std::size_t id) const
Read only access to the names of variables.
Definition: monitor.hpp:140
std::size_t index(std::size_t iter) const
Get the iteration index of the sampler of a given Monitor iteration.
Definition: monitor.hpp:156
double record(std::size_t id) const
Get the latest Monte Carlo integration record of a given variable.
Definition: monitor.hpp:168
Monitor for Monte Carlo integration.
Definition: monitor.hpp:56
void read_record_matrix(OutputIterIter first) const
Read the record history of all variables through an array of output iterators.
Definition: monitor.hpp:217
bool recording() const
Whether the Monitor is actively recording results.
Definition: monitor.hpp:295
size_type size() const
Number of particles.
Definition: particle.hpp:146
#define VSMC_RUNTIME_ASSERT_CORE_MONITOR_FUNCTOR(func, caller, name)
Definition: monitor.hpp:47
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:182
weight_set_type & weight_set()
Read and write access to the weight collection object.
Definition: particle.hpp:155