32 #ifndef VSMC_CORE_MONITOR_HPP 33 #define VSMC_CORE_MONITOR_HPP 37 #define VSMC_RUNTIME_ASSERT_CORE_MONITOR_ID(func) \ 38 VSMC_RUNTIME_ASSERT( \ 39 (id < dim()), "**Monitor::" #func "** INVALID ID NUMBER ARGUMENT") 41 #define VSMC_RUNTIME_ASSERT_CORE_MONITOR_ITER(func) \ 42 VSMC_RUNTIME_ASSERT((iter < iter_size()), \ 43 "**Monitor::" #func "** INVALID ITERATION NUMBER ARGUMENT") 45 #define VSMC_RUNTIME_ASSERT_CORE_MONITOR_EVAL \ 46 VSMC_RUNTIME_ASSERT(eval_, "**Monitor::eval** INVALID EVALUATION OBJECT") 66 std::function<void(std::size_t, std::size_t, Particle<T> &,
double *)>;
76 internal::size_check<VSMC_BLAS_INT>(dim_,
"Monitor::Monitor");
80 std::size_t
dim()
const {
return dim_; }
89 std::size_t
iter_size()
const {
return index_.size(); }
95 record_.reserve(dim_ * num);
99 bool empty()
const {
return !eval_; }
108 std::string &
name(std::size_t
id)
116 const std::string &
name(std::size_t
id)
const 141 std::size_t
index(std::size_t iter)
const 149 template <
typename OutputIter>
152 return std::copy(index_.begin(), index_.end(), first);
163 return record_[iter * dim_ + id];
168 double record(std::size_t
id, std::size_t iter)
const 173 return record_[iter * dim_ + id];
178 template <
typename OutputIter>
184 const double *riter = record_.data() + id;
185 for (std::size_t i = 0; i != N; ++i, ++first, riter += dim_)
193 template <
typename OutputIter>
197 return std::copy(record_.begin(), record_.end(), first);
200 for (std::size_t d = 0; d != dim_; ++d)
201 for (std::size_t i = 0; i !=
iter_size(); ++i)
209 std::map<std::string, Vector<double>>
summary()
const 211 std::map<std::string, Vector<double>> df;
214 std::copy(index_.begin(), index_.end(), data.begin());
217 for (std::size_t d = 0; d != dim_; ++d) {
218 for (std::size_t i = 0; i !=
iter_size(); ++i)
220 if (name_[d].
empty())
221 df[
"X." + std::to_string(d)] = data;
243 internal::size_check<VSMC_BLAS_INT>(particle.
size(),
"Monitor::eval");
250 result_.resize(dim_);
252 eval_(iter, dim_, particle, result_.data());
258 const std::size_t N =
static_cast<std::size_t
>(particle.
size());
259 buffer_.resize(N * dim_);
260 eval_(iter, dim_, particle, buffer_.data());
261 internal::cblas_dgemv(internal::CblasColMajor, internal::CblasNoTrans,
262 static_cast<VSMC_BLAS_INT>(dim_), static_cast<VSMC_BLAS_INT>(N),
264 particle.
weight().data(), 1, 0.0, result_.data(), 1);
286 void push_back(std::size_t iter)
288 index_.push_back(iter);
289 record_.insert(record_.end(), result_.begin(), result_.end());
295 #endif // VSMC_CORE_MONITOR_HPP std::vector< T, Alloc > Vector
std::vector with Allocator as default allocator
bool empty() const
Whether the evaluation object is valid.
#define VSMC_RUNTIME_ASSERT_CORE_MONITOR_EVAL
Particle class representing the whole particle set.
bool record_only() const
If this is a record only Monitor.
MonitorStage
Monitor stage.
MatrixLayout
Matrix layout.
void clear()
Clear all records of the index and integrations.
weight_type & weight()
Read and write access to the weight collection object.
std::map< std::string, Vector< double > > summary() const
Summary of monitor history.
OutputIter read_index(OutputIter first) const
Read the index history through an output iterator.
void reserve(std::size_t num)
Reserve space for a specified number of iterations.
Monitor evaluated after moves.
#define VSMC_RUNTIME_ASSERT_CORE_MONITOR_ITER(func)
Monitor(std::size_t dim, const eval_type &eval, bool record_only=false, MonitorStage stage=MonitorMCMC)
std::size_t dim() const
The dimension of the Monitor.
std::size_t index() const
Get the latest iteration index of the sampler.
std::size_t iter_size() const
The number of iterations has been recorded.
void eval(const eval_type &new_eval, bool record_only=false, MonitorStage stage=MonitorMCMC)
Set a new evaluation object of type eval_type.
OutputIter read_record_matrix(MatrixLayout layout, OutputIter first) const
Read the record history of all variables as a matrix through an output iterator.
std::string & name(std::size_t id)
Read and write access to the names of variables.
void operator()(std::size_t iter, Particle< T > &particle, MonitorStage stage)
Perform the evaluation for a given iteration and a Particle<T> object.
const std::string & name(std::size_t id) const
Read only access to the names of variables.
std::size_t index(std::size_t iter) const
Get the iteration index of the sampler of a given Monitor iteration.
MonitorStage stage() const
The stage of the Montior.
double record(std::size_t id) const
Get the latest Monte Carlo integration record of a given variable.
Monitor evaluated after MCMC moves.
Monitor for Monte Carlo integration.
#define VSMC_RUNTIME_ASSERT_CORE_MONITOR_ID(func)
size_type size() const
Number of particles.
std::function< void(std::size_t, std::size_t, Particle< T > &, double *)> eval_type
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.
OutputIter read_record(std::size_t id, OutputIter first) const
Read the record history for a given variable through an output iterator.
Monitor evaluated after resampling.