autoppl  v0.8
A C++ template library for probabilistic programming
data.hpp
Go to the documentation of this file.
1 #pragma once
2 #include <Eigen/Dense>
3 #include <fastad_bits/reverse/core/constant.hpp>
7 
8 #define PPL_DATA_SHAPE_UNSUPPORTED \
9  "Unsupported shape for Data. "
10 #define PPL_DATAVIEW_SHAPE_UNSUPPORTED \
11  "Unsupported shape for DataView. "
12 
13 namespace ppl {
14 
26 template <class ValueType
27  , class ShapeType = ppl::scl>
28 struct DataView
29 {
30  static_assert(util::is_shape_v<ShapeType>,
32 };
33 
34 template <class ValueType>
35 struct DataView<ValueType, ppl::scl>:
36  util::VarExprBase<DataView<ValueType, ppl::scl>>,
37  util::DataBase<DataView<ValueType, ppl::scl>>
38 {
39  using value_t = ValueType;
40  using var_t = value_t;
41  using id_t = const void*;
42  using shape_t = ppl::scl;
43  static constexpr bool has_param = false;
44 
45  DataView(const value_t* begin) noexcept
46  : var_{begin}
47  , id_{this}
48  {}
49 
50  template <class Func>
51  void traverse(Func&&) const {}
52 
53  const var_t& eval() const { return get(); }
54  const var_t& get() const { return *var_; }
55 
56  constexpr size_t size() const { return 1; }
57  constexpr size_t rows() const { return 1; }
58  constexpr size_t cols() const { return 1; }
59  id_t id() const { return id_; }
60 
61  template <class PtrPackType>
62  auto ad(const PtrPackType&) const
63  { return ad::constant(*var_); }
64 
65  template <class PtrType>
66  void bind(PtrType begin)
67  {
68  static_cast<void>(begin);
69  if constexpr (std::is_convertible_v<PtrType, value_t*>) {
70  var_ = begin;
71  }
72  }
73 
74  void activate_refcnt() const {}
75 
76 private:
77  const var_t* var_;
78  id_t id_;
79 };
80 
81 template <class ValueType>
82 struct DataView<ValueType, ppl::vec> :
83  util::VarExprBase<DataView<ValueType, ppl::vec>>,
84  util::DataBase<DataView<ValueType, ppl::vec>>
85 {
86  using value_t = ValueType;
87  using var_t = Eigen::Map<const Eigen::Matrix<value_t, Eigen::Dynamic, 1>>;
88  using id_t = const void*;
89  using shape_t = ppl::vec;
90  static constexpr bool has_param = false;
91 
92  DataView(const value_t* begin,
93  size_t rows) noexcept
94  : var_(begin, rows)
95  , id_{this}
96  {}
97 
98  const var_t& eval() const { return get(); }
99  const var_t& get() const { return var_; }
100  size_t size() const { return var_.size(); }
101  size_t rows() const { return var_.rows(); }
102  constexpr size_t cols() const { return 1; }
103  id_t id() const { return id_; }
104 
105  template <class PtrPackType>
106  auto ad(const PtrPackType&) const
107  { return ad::constant_view(var_.data(), size()); }
108 
109  template <class PtrType>
110  void bind(PtrType begin)
111  {
112  static_cast<void>(begin);
113  if constexpr (std::is_convertible_v<PtrType, value_t*>) {
114  new (&var_) var_t(begin, size());
115  }
116  }
117 
118  void activate_refcnt() const {}
119 
120 private:
121  var_t var_;
122  id_t id_;
123 };
124 
125 template <class ValueType>
126 struct DataView<ValueType, ppl::mat> :
127  util::VarExprBase<DataView<ValueType, ppl::mat>>,
128  util::DataBase<DataView<ValueType, ppl::mat>>
129 {
130  using value_t = ValueType;
131  using var_t = Eigen::Map<const Eigen::Matrix<value_t, Eigen::Dynamic, Eigen::Dynamic>>;
132  using id_t = const void*;
133  using shape_t = ppl::mat;
134  static constexpr bool has_param = false;
135 
136  DataView(const value_t* begin,
137  size_t rows,
138  size_t cols) noexcept
139  : var_(begin, rows, cols)
140  , id_{this}
141  {}
142 
143  const var_t& eval() const { return get(); }
144  const var_t& get() const { return var_; }
145  size_t size() const { return var_.size(); }
146  size_t rows() const { return var_.rows(); }
147  size_t cols() const { return var_.cols(); }
148  id_t id() const { return id_; }
149 
150  template <class PtrPackType>
151  auto ad(const PtrPackType&) const
152  { return ad::constant_view(var_.data(), rows(), cols()); }
153 
154  template <class PtrType>
155  void bind(PtrType begin)
156  {
157  static_cast<void>(begin);
158  if constexpr (std::is_convertible_v<PtrType, value_t*>) {
159  new (&var_) var_t(begin, rows(), cols());
160  }
161  }
162 
163  void activate_refcnt() const {}
164 
165 private:
166  var_t var_;
167  id_t id_;
168 };
169 
185 template <class ValueType
186  , class ShapeType = ppl::scl>
187 struct Data
188 {
189  static_assert(util::is_shape_v<ShapeType>,
191 };
192 
193 // Specialization: scalar
194 template <class ValueType>
195 struct Data<ValueType, ppl::scl>:
196  DataView<ValueType, ppl::scl>,
197  util::DataBase<Data<ValueType, ppl::scl>>
198 {
200  using typename base_t::value_t;
201  using base_t::get;
202 
203  Data(value_t v) noexcept
204  : base_t(&value_)
205  , value_(v)
206  {}
207  Data() noexcept : Data(0) {}
208 
209  auto& get() { return value_; }
210 
211 private:
212  value_t value_; // store value associated with data
213 };
214 
215 // Specialization: vector
216 template <class ValueType>
217 struct Data<ValueType, ppl::vec>:
218  DataView<ValueType, ppl::vec>,
219  util::DataBase<Data<ValueType, ppl::vec>>
220 {
222  using typename base_t::value_t;
223  using base_t::bind;
224  using base_t::get;
225 
226  Data(size_t n)
227  : base_t(nullptr, n)
228  , vec_(n)
229  { this->bind(vec_.data()); }
230 
231  auto& get() { return vec_; }
232 
233 private:
234  using vec_t = Eigen::Matrix<value_t, Eigen::Dynamic, 1>;
235  vec_t vec_;
236 };
237 
238 // Specialization: matrix
239 template <class ValueType>
240 struct Data<ValueType, ppl::mat>:
241  DataView<ValueType, ppl::mat>,
242  util::DataBase<Data<ValueType, ppl::mat>>
243 {
245  using typename base_t::value_t;
246  using base_t::bind;
247  using base_t::get;
248 
249  Data(size_t rows, size_t cols)
250  : base_t(nullptr, rows, cols)
251  , mat_(rows, cols)
252  { this->bind(mat_.data()); }
253 
254  auto& get() { return mat_; }
255 
256 private:
257  using mat_t = Eigen::Matrix<value_t, Eigen::Dynamic, Eigen::Dynamic>;
258  mat_t mat_;
259 };
260 
261 } // namespace ppl
262 
263 #undef PPL_DATA_SHAPE_UNSUPPORTED
264 #undef PPL_DATAVIEW_SHAPE_UNSUPPORTED
ppl::DataView< ValueType, ppl::vec >::rows
size_t rows() const
Definition: data.hpp:101
var_traits.hpp
ppl::Data< ValueType, ppl::scl >::Data
Data() noexcept
Definition: data.hpp:207
ppl::DataView< ValueType, ppl::scl >::activate_refcnt
void activate_refcnt() const
Definition: data.hpp:74
ppl::util::DataBase
Definition: var_traits.hpp:24
PPL_DATA_SHAPE_UNSUPPORTED
#define PPL_DATA_SHAPE_UNSUPPORTED
Definition: data.hpp:8
ppl::DataView< ValueType, ppl::scl >::size
constexpr size_t size() const
Definition: data.hpp:56
ppl::DataView< ValueType, ppl::scl >::traverse
void traverse(Func &&) const
Definition: data.hpp:51
ppl::DataView< ValueType, ppl::vec >::id_t
const void * id_t
Definition: data.hpp:88
ppl::DataView< ValueType, ppl::mat >::rows
size_t rows() const
Definition: data.hpp:146
ppl::util::get
auto & get(T &&x)
Definition: value.hpp:52
var_expr_traits.hpp
ppl::DataView< ValueType, ppl::scl >::id_t
const void * id_t
Definition: data.hpp:41
ppl::Data< ValueType, ppl::vec >::Data
Data(size_t n)
Definition: data.hpp:226
ppl::DataView< ValueType, ppl::mat >::id_t
const void * id_t
Definition: data.hpp:132
ppl::DataView< ValueType, ppl::scl >::shape_t
ppl::scl shape_t
Definition: data.hpp:42
shape_traits.hpp
ppl::Data< ValueType, ppl::scl >::get
auto & get()
Definition: data.hpp:209
ppl::DataView< ValueType, ppl::vec >
Definition: data.hpp:85
ppl::DataView< ValueType, ppl::scl >::ad
auto ad(const PtrPackType &) const
Definition: data.hpp:62
ppl::DataView< ValueType, ppl::vec >::size
size_t size() const
Definition: data.hpp:100
ppl::DataView< ValueType, ppl::scl >
Definition: data.hpp:38
ppl::vec
ad::vec vec
Definition: shape_traits.hpp:17
ppl::Data< ValueType, ppl::scl >::Data
Data(value_t v) noexcept
Definition: data.hpp:203
ppl::DataView< ValueType, ppl::mat >::size
size_t size() const
Definition: data.hpp:145
ppl::DataView< ValueType, ppl::mat >::value_t
ValueType value_t
Definition: data.hpp:130
ppl::DataView< ValueType, ppl::scl >::eval
const var_t & eval() const
Definition: data.hpp:53
ppl::DataView< ValueType, ppl::vec >::activate_refcnt
void activate_refcnt() const
Definition: data.hpp:118
ppl::DataView< ValueType, ppl::vec >::DataView
DataView(const value_t *begin, size_t rows) noexcept
Definition: data.hpp:92
PPL_DATAVIEW_SHAPE_UNSUPPORTED
#define PPL_DATAVIEW_SHAPE_UNSUPPORTED
Definition: data.hpp:10
ppl::DataView< ValueType, ppl::vec >::id
id_t id() const
Definition: data.hpp:103
ppl::util::bind
void bind(T &x, ValPtrType begin, size_t rows=1, size_t cols=1)
Definition: value.hpp:61
ppl::Data< ValueType, ppl::mat >::Data
Data(size_t rows, size_t cols)
Definition: data.hpp:249
ppl::util::cols
constexpr size_t cols(const T &x)
Definition: value.hpp:42
ppl::scl
ad::scl scl
Definition: shape_traits.hpp:16
ppl::DataView< ValueType, ppl::scl >::DataView
DataView(const value_t *begin) noexcept
Definition: data.hpp:45
ppl::DataView< ValueType, ppl::mat >::var_t
Eigen::Map< const Eigen::Matrix< value_t, Eigen::Dynamic, Eigen::Dynamic > > var_t
Definition: data.hpp:131
ppl::Data< ValueType, ppl::vec >::get
auto & get()
Definition: data.hpp:231
ppl::util::size
constexpr size_t size(const T &x)
Definition: value.hpp:22
ppl::DataView< ValueType, ppl::vec >::value_t
ValueType value_t
Definition: data.hpp:86
ppl::util::var_t
typename details::var< V, T >::type var_t
Definition: shape_traits.hpp:132
ppl::DataView< ValueType, ppl::scl >::value_t
ValueType value_t
Definition: data.hpp:39
ppl::Data
Definition: data.hpp:188
ppl::DataView
Definition: data.hpp:29
ppl::DataView< ValueType, ppl::vec >::shape_t
ppl::vec shape_t
Definition: data.hpp:89
ppl::DataView< ValueType, ppl::mat >::id
id_t id() const
Definition: data.hpp:148
ppl::DataView< ValueType, ppl::vec >::eval
const var_t & eval() const
Definition: data.hpp:98
ppl::DataView< ValueType, ppl::mat >::eval
const var_t & eval() const
Definition: data.hpp:143
ppl::DataView< ValueType, ppl::mat >::activate_refcnt
void activate_refcnt() const
Definition: data.hpp:163
ppl
Definition: bounded.hpp:11
ppl::DataView< ValueType, ppl::scl >::bind
void bind(PtrType begin)
Definition: data.hpp:66
ppl::util::VarExprBase
Definition: var_expr_traits.hpp:20
ppl::mat
ad::mat mat
Definition: shape_traits.hpp:18
ppl::Data< ValueType, ppl::mat >::get
auto & get()
Definition: data.hpp:254
ppl::DataView< ValueType, ppl::scl >::rows
constexpr size_t rows() const
Definition: data.hpp:57
ppl::DataView< ValueType, ppl::scl >::cols
constexpr size_t cols() const
Definition: data.hpp:58
ppl::DataView< ValueType, ppl::mat >::DataView
DataView(const value_t *begin, size_t rows, size_t cols) noexcept
Definition: data.hpp:136
ppl::util::rows
constexpr size_t rows(const T &x)
Definition: value.hpp:32
ppl::DataView< ValueType, ppl::vec >::var_t
Eigen::Map< const Eigen::Matrix< value_t, Eigen::Dynamic, 1 > > var_t
Definition: data.hpp:87
ppl::DataView< ValueType, ppl::scl >::id
id_t id() const
Definition: data.hpp:59
ppl::DataView< ValueType, ppl::mat >::shape_t
ppl::mat shape_t
Definition: data.hpp:133
ppl::DataView< ValueType, ppl::mat >::cols
size_t cols() const
Definition: data.hpp:147
ppl::DataView< ValueType, ppl::mat >
Definition: data.hpp:129
ppl::DataView< ValueType, ppl::scl >::var_t
value_t var_t
Definition: data.hpp:40
ppl::DataView< ValueType, ppl::vec >::ad
auto ad(const PtrPackType &) const
Definition: data.hpp:106
ppl::DataView< ValueType, ppl::vec >::cols
constexpr size_t cols() const
Definition: data.hpp:102
ppl::DataView< ValueType, ppl::mat >::ad
auto ad(const PtrPackType &) const
Definition: data.hpp:151