Cortenn
data.hpp
Go to the documentation of this file.
1 
10 #include <memory>
11 
12 #include "ade/coord.hpp"
13 #include "ade/ileaf.hpp"
14 
15 #include "llo/generated/codes.hpp"
16 
17 #ifndef LLO_DATA_HPP
18 #define LLO_DATA_HPP
19 
20 namespace llo
21 {
22 
24 struct GenericData final
25 {
26  GenericData (void) = default;
27 
28  GenericData (ade::Shape shape, age::_GENERATED_DTYPE dtype);
29 
32  void copyover (const char* indata, age::_GENERATED_DTYPE intype);
33 
35  std::shared_ptr<char> data_;
36 
38  ade::Shape shape_;
39 
41  age::_GENERATED_DTYPE dtype_;
42 };
43 
46 struct GenericRef
47 {
48  GenericRef (char* data, ade::Shape shape, age::_GENERATED_DTYPE dtype) :
49  data_(data), shape_(shape), dtype_(dtype) {}
50 
51  GenericRef (GenericData& generic) :
52  data_(generic.data_.get()),
53  shape_(generic.shape_), dtype_(generic.dtype_) {}
54 
56  char* data_;
57 
59  ade::Shape shape_;
60 
62  age::_GENERATED_DTYPE dtype_;
63 };
64 
66 struct Variable final : public ade::iLeaf
67 {
68  Variable (const char* data, age::_GENERATED_DTYPE dtype,
69  ade::Shape shape, std::string label) :
70  label_(label), data_(shape, dtype)
71  {
72  if (nullptr != data)
73  {
74  std::memcpy(data_.data_.get(), data, nbytes());
75  }
76  else
77  {
78  std::memset(data_.data_.get(), 0, nbytes());
79  }
80  }
81 
82  Variable (const Variable& other) :
83  label_(other.label_), data_(other.shape(), (age::_GENERATED_DTYPE) other.type_code())
84  {
85  std::memcpy((char*) data_.data_.get(), (const char*) other.data(), nbytes());
86  }
87 
88  Variable (Variable&& other) :
89  label_(std::move(other.label_)), data_(std::move(other.data_)) {}
90 
91  Variable& operator = (const Variable& other)
92  {
93  if (this != &other)
94  {
95  label_ = other.label_;
96  data_ = GenericData(other.shape(), (age::_GENERATED_DTYPE) other.type_code());
97  std::memcpy((char*) data_.data_.get(), (const char*) other.data(), nbytes());
98  }
99  return *this;
100  }
101 
103  {
104  if (this != &other)
105  {
106  label_ = std::move(other.label_);
107  data_ = std::move(other.data_);
108  }
109  return *this;
110  }
111 
113  template <typename T>
114  Variable& operator = (std::vector<T> data)
115  {
116  GenericRef ref((char*) &data[0], shape(), age::get_type<T>());
117  return operator = (ref);
118  }
119 
122  {
123  if (false == data.shape_.compatible_after(shape(), 0))
124  {
125  logs::fatalf("cannot assign data of incompatible shaped %s to "
126  "internal data of shape %s", data.shape_.to_string().c_str(),
127  shape().to_string().c_str());
128  }
129  if (data.dtype_ != data_.dtype_)
130  {
131  logs::fatalf("cannot assign data of incompatible types %s "
132  "(external) and %s (internal)",
133  age::name_type(data.dtype_).c_str(), age::name_type(data_.dtype_).c_str());
134  }
135  std::memcpy(data_.data_.get(), data.data_, nbytes());
136  return *this;
137  }
138 
140  const ade::Shape& shape (void) const override
141  {
142  return data_.shape_;
143  }
144 
146  std::string to_string (void) const override
147  {
148  return label_ + "(" + data_.shape_.to_string() + ")";
149  }
150 
152  void* data (void) override
153  {
154  return data_.data_.get();
155  }
156 
158  const void* data (void) const override
159  {
160  return data_.data_.get();
161  }
162 
164  size_t type_code (void) const override
165  {
166  return data_.dtype_;
167  }
168 
170  size_t nbytes (void) const
171  {
172  return type_size(data_.dtype_) * data_.shape_.n_elems();
173  }
174 
176  std::string label_;
177 
178 private:
181 };
182 
184 using VarptrT = std::shared_ptr<llo::Variable>;
185 
189 template <typename T>
190 VarptrT get_variable (std::vector<T> data, ade::Shape shape,
191  std::string label = "")
192 {
193  if (data.size() != shape.n_elems())
194  {
195  logs::fatalf("cannot create variable with data size %d "
196  "against shape %s", data.size(), shape.to_string().c_str());
197  }
198  return VarptrT(new Variable((char*) &data[0],
199  age::get_type<T>(), shape, label));
200 }
201 
204 template <typename T>
205 VarptrT get_variable (ade::Shape shape, std::string label = "")
206 {
207  return get_variable(std::vector<T>(shape.n_elems(), 0), shape, label);
208 }
209 
212 template <typename T>
213 VarptrT get_scalar (T scalar, ade::Shape shape, std::string label = "")
214 {
215  if (label.empty())
216  {
217  label = fmts::to_string(scalar);
218  }
219  return llo::get_variable(std::vector<T>(shape.n_elems(),scalar),
220  shape, label);
221 }
222 
224 struct DataArg
225 {
227  std::shared_ptr<char> data_;
228 
230  ade::Shape shape_;
231 
233  ade::CoordptrT mapper_;
234 
237  bool fwd_;
238 };
239 
241 using DataArgsT = std::vector<DataArg>;
242 
245 template <typename T>
246 struct VecRef
247 {
249  const T* data;
250 
252  ade::Shape shape;
253 
255  ade::CoordptrT mapper;
256 
259  bool push;
260 };
261 
263 template <typename T>
265 {
266  return VecRef<T>{
267  (const T*) arg.data_.get(),
268  arg.shape_,
269  arg.mapper_,
270  arg.fwd_,
271  };
272 }
273 
275 template <typename T>
276 std::vector<VecRef<T>> to_refs (DataArgsT& args)
277 {
278  std::vector<VecRef<T>> out;
279  std::transform(args.begin(), args.end(), std::back_inserter(out),
280  [](DataArg& arg)
281  {
282  return to_ref<T>(arg);
283  });
284  return out;
285 }
286 
287 }
288 
289 #endif // LLO_DATA_HPP
Definition: data.hpp:46
size_t nbytes(void) const
Return number of bytes in data source.
Definition: data.hpp:170
ade::CoordptrT mapper_
Coordinate mapper.
Definition: data.hpp:233
const void * data(void) const override
Implementation of iLeaf.
Definition: data.hpp:158
Variable(const char *data, age::_GENERATED_DTYPE dtype, ade::Shape shape, std::string label)
Definition: data.hpp:68
const T * data
Raw input data.
Definition: data.hpp:249
Leaf node containing GenericData.
Definition: data.hpp:66
GenericRef(GenericData &generic)
Definition: data.hpp:51
std::vector< DataArg > DataArgsT
Vector of DataArgs to hold arguments.
Definition: data.hpp:241
Variable & operator=(const Variable &other)
Definition: data.hpp:91
VecRef< T > to_ref(DataArg &arg)
Converts DataArgs to VecRef of specific type.
Definition: data.hpp:264
VarptrT get_scalar(T scalar, ade::Shape shape, std::string label="")
Definition: data.hpp:213
std::vector< VecRef< T > > to_refs(DataArgsT &args)
Converts multiple DataArgs to multiple VecRefs of the same type.
Definition: data.hpp:276
void copyover(const char *indata, age::_GENERATED_DTYPE intype)
std::string label_
Label for distinguishing variable nodes.
Definition: data.hpp:176
Definition: data.hpp:246
Data to pass around when evaluating.
Definition: data.hpp:224
ade::Shape shape_
Shape of data_.
Definition: data.hpp:38
size_t type_code(void) const override
Implementation of iLeaf.
Definition: data.hpp:164
ade::Shape shape_
Shape of the generic data.
Definition: data.hpp:230
bool fwd_
Definition: data.hpp:237
std::string to_string(void) const override
Implementation of iTensor.
Definition: data.hpp:146
GenericRef(char *data, ade::Shape shape, age::_GENERATED_DTYPE dtype)
Definition: data.hpp:48
Variable(const Variable &other)
Definition: data.hpp:82
std::shared_ptr< llo::Variable > VarptrT
Smart pointer for variable nodes.
Definition: data.hpp:184
GenericData for holding data when passing up the tensor graph.
Definition: data.hpp:24
age::_GENERATED_DTYPE dtype_
Type encoding of data_.
Definition: data.hpp:41
Variable(Variable &&other)
Definition: data.hpp:88
ade::Shape shape_
Shape of data_.
Definition: data.hpp:59
const ade::Shape & shape(void) const override
Implementation of iTensor.
Definition: data.hpp:140
std::shared_ptr< char > data_
Smart pointer to generic data as bytes.
Definition: data.hpp:227
VarptrT get_variable(std::vector< T > data, ade::Shape shape, std::string label="")
Definition: data.hpp:190
age::_GENERATED_DTYPE dtype_
Data type of data_.
Definition: data.hpp:62
std::shared_ptr< char > data_
Smartpointer to a block of untyped data.
Definition: data.hpp:35
Definition: data.hpp:20
bool push
Definition: data.hpp:259
ade::Shape shape
Shape info of the raw input.
Definition: data.hpp:252
GenericData data_
Generic data source.
Definition: data.hpp:180
GenericData(void)=default
void * data(void) override
Implementation of iLeaf.
Definition: data.hpp:152
ade::CoordptrT mapper
Coordinate mapper of input to output.
Definition: data.hpp:255
char * data_
Raw pointer to a block of untyped data.
Definition: data.hpp:56