32 #ifndef VSMC_CORE_MONITOR_HPP
33 #define VSMC_CORE_MONITOR_HPP
39 #define VSMC_RUNTIME_ASSERT_CORE_MONITOR_ID(func) \
40 VSMC_RUNTIME_ASSERT((id < dim()), \
41 ("**Monitor::"#func"** INVALID ID NUMBER ARGUMENT"))
43 #define VSMC_RUNTIME_ASSERT_CORE_MONITOR_ITER(func) \
44 VSMC_RUNTIME_ASSERT((iter < iter_size()), \
45 ("**Monitor::"#func"** INVALID ITERATION NUMBER ARGUMENT"))
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")) \
61 typedef cxx11::function<
62 void (std::size_t, std::size_t,
const Particle<T> &,
double *)>
99 bool record_only =
false) :
100 dim_(dim), eval_(eval), recording_(true), record_only_(record_only),
104 std::size_t
dim ()
const {
return dim_;}
119 record_.reserve(dim_ * num);
123 bool empty ()
const {
return !
static_cast<bool>(eval_);}
132 std::string &
name (std::size_t
id)
140 const std::string &
name (std::size_t
id)
const
156 std::size_t
index (std::size_t iter)
const
174 return record_[iter * dim_ + id];
182 double record (std::size_t
id, std::size_t iter)
const
187 return record_[iter * dim_ + id];
191 template <
typename OutputIter>
193 {std::copy(index_.begin(), index_.end(), first);}
203 template <
typename OutputIter>
207 const double *riter = &record_[id];
208 for (std::size_t i = 0; i != N; ++i, ++first, riter += dim_)
216 template <
typename OutputIterIter>
219 for (std::size_t d = 0; d != dim_; ++d, ++first)
233 template <MatrixOrder Order,
typename OutputIter>
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_)
246 std::copy(record_.begin(), record_.end(), first);
268 result_.resize(dim_);
269 double *
const rptr = &result_[0];
271 eval_(iter, dim_, particle, rptr);
272 push_back(iter, rptr);
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);
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_;
316 void push_back (std::size_t iter,
const double *rptr)
318 index_.push_back(iter);
319 for (std::size_t d = 0; d != dim_; ++d)
320 record_.push_back(rptr[d]);
326 #endif // VSMC_CORE_MONITOR_HPP
bool empty() const
Whether the evaluation object is valid.
void read_record(std::size_t id, OutputIter first) const
Read the record history for a given variable through an output iterator.
Particle class representing the whole particle set.
Monitor(std::size_t dim, const eval_type &eval, bool record_only=false)
Construct a Monitor with an evaluation object.
void read_index(OutputIter first) const
Read the index history through an output iterator.
void eval(std::size_t iter, const Particle< T > &particle)
Perform the evaluation for a given iteration and a Particle object.
void clear()
Clear all records of the index and integrations.
void reserve(std::size_t num)
Reserve space for a specified number of iterations.
void turn_off()
Turn off the recording.
void turn_on()
Turn on the recording.
#define VSMC_RUNTIME_ASSERT_CORE_MONITOR_ITER(func)
Data are stored column by column in memory.
cxx11::function< void(std::size_t, std::size_t, const Particle< T > &, double *)> eval_type
std::size_t dim() const
The dimension of the Monitor.
void set_eval(const eval_type &new_eval)
Set a new evaluation object of type eval_type.
const std::size_t * index_data() const
Read only access to the raw data of the index vector.
void read_record_matrix(OutputIter first) const
Read the record history of all variables through an output iterator.
std::size_t iter_size() const
The number of iterations has been recorded.
#define VSMC_RUNTIME_ASSERT_CORE_MONITOR_ID(func)
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)
Data are stored row by row in memory.
std::string & name(std::size_t id)
Read and write access to the names of variables.
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.
double record(std::size_t id) const
Get the latest Monte Carlo integration record of a given variable.
Monitor for Monte Carlo integration.
void read_record_matrix(OutputIterIter first) const
Read the record history of all variables through an array of output iterators.
bool recording() const
Whether the Monitor is actively recording results.
size_type size() const
Number of particles.
#define VSMC_RUNTIME_ASSERT_CORE_MONITOR_FUNCTOR(func, caller, name)
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.
weight_set_type & weight_set()
Read and write access to the weight collection object.