MCKL
Monte Carlo Kernel Library
estimator.hpp
Go to the documentation of this file.
1 //============================================================================
2 // MCKL/include/mckl/core/estimator.hpp
3 //----------------------------------------------------------------------------
4 // MCKL: Monte Carlo Kernel Library
5 //----------------------------------------------------------------------------
6 // Copyright (c) 2013-2018, 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 MCKL_CORE_ESTIMATOR_HPP
33 #define MCKL_CORE_ESTIMATOR_HPP
34 
35 #include <mckl/internal/common.hpp>
37 
38 MCKL_PUSH_CLANG_WARNING("-Wpadded")
39 
40 namespace mckl {
41 
44 template <typename T, typename... Args>
45 class Estimator : public EstimateMatrix<T>
46 {
47  public:
48  Estimator() = default;
49 
50  Estimator(std::size_t dim) : EstimateMatrix<T>(0, dim) {}
51 
52  template <typename Eval>
53  Estimator(std::size_t dim, Eval &&eval)
54  : EstimateMatrix<T>(dim), eval_(std::forward<Eval>(eval))
55  {
56  }
57 
59 
61  template <typename Eval>
62  std::enable_if_t<!std::is_convertible<Eval, int>::value> estimate(
63  Eval &&eval)
64  {
65  eval_ = std::forward<Eval>(eval);
66  }
67 
70  template <typename OutputIter>
71  OutputIter average(
72  OutputIter first, std::size_t cut = 0, std::size_t thin = 1) const
73  {
74  const std::size_t n = this->num_iter();
75  const std::size_t d = this->dim();
76 
77  if (cut >= n) {
78  return first;
79  }
80 
81  thin = thin < 1 ? 1 : thin;
82  if (n - cut < thin) {
83  return this->read_estimate(cut, first);
84  }
85 
86  if (thin < 1) {
87  thin = 1;
88  }
89  Vector<T> sum(d, T());
90  std::size_t k = 0;
91  while (cut < n) {
92  add(d, this->row_data(cut), sum.data(), sum.data());
93  cut += thin;
94  ++k;
95  }
96  mul(d, static_cast<T>(1.0l / n), sum.data(), sum.data());
97 
98  return std::copy(sum.begin(), sum.end(), first);
99  }
100 
101  protected:
102  template <typename... CallArgs>
103  void eval(CallArgs &&... args)
104  {
105  runtime_assert(static_cast<bool>(eval_),
106  "**Estimator::eval** used with an invalid evaluation object");
107 
108  eval_(std::forward<CallArgs>(args)...);
109  }
110 
111  private:
112  std::function<void(Args...)> eval_;
113 }; // class Estimator
114 
115 } // namespace mckl
116 
118 
119 #endif // MCKL_CORE_ESTIMATOR_HPP
Estimate matrix for iterative Monte Carlo algorithms.
void mul(std::size_t n, const float *a, const float *b, float *y)
Definition: vmf.hpp:124
void eval(CallArgs &&... args)
Definition: estimator.hpp:103
#define MCKL_PUSH_CLANG_WARNING(warning)
Definition: compiler.h:63
std::vector< T, Alloc > Vector
std::vector with Allocator as the default allocator
Definition: memory.hpp:435
STL namespace.
Definition: mcmc.hpp:40
#define MCKL_POP_CLANG_WARNING
Definition: compiler.h:66
Estimator(std::size_t dim)
Definition: estimator.hpp:50
Estimator for iterative Monte Carlo algorithms.
Definition: estimator.hpp:45
std::enable_if_t<!std::is_convertible< Eval, int >::value > estimate(Eval &&eval)
Set a new estimate evaluation method.
Definition: estimator.hpp:62
Estimator(std::size_t dim, Eval &&eval)
Definition: estimator.hpp:53
OutputIter average(OutputIter first, std::size_t cut=0, std::size_t thin=1) const
Get the average of estimates after cut iterations using every thin elements.
Definition: estimator.hpp:71
void runtime_assert(bool cond, const char *msg, bool soft=false)
Definition: assert.hpp:65
void add(std::size_t n, const float *a, const float *b, float *y)
Definition: vmf.hpp:119