Tenncor
custom_functor.hpp
Go to the documentation of this file.
1 
9 #include "teq/iopfunc.hpp"
10 
11 #include "eteq/generated/opcode.hpp"
12 
13 #include "eteq/funcarg.hpp"
14 #include "eteq/constant.hpp"
15 #include "eteq/operator.hpp"
16 
17 #ifndef DBG_CUSTOM_FUNCTOR_HPP
18 #define DBG_CUSTOM_FUNCTOR_HPP
19 
20 namespace dbg
21 {
22 
24 template <typename T>
25 using DataMapT = std::vector<eteq::OpArg<T>>;
26 
28 template <typename T>
29 using CustomOpF = std::function<void(eteq::TensorT<T>&,const DataMapT<T>&)>;
30 
32 template <typename T>
33 struct CustomFunctor final : public teq::iOperableFunc
34 {
37 
38  CustomFunctor (const CustomFunctor<T>& other) = default;
39 
40  CustomFunctor (CustomFunctor<T>&& other) = default;
41 
42  CustomFunctor<T>& operator = (const CustomFunctor<T>& other) = delete;
43 
45 
47  const teq::Shape& shape (void) const override
48  {
49  return shape_;
50  }
51 
53  std::string to_string (void) const override
54  {
55  return "DBG_CUSTOM_FUNCTOR";
56  }
57 
59  teq::Opcode get_opcode (void) const override
60  {
61  return teq::Opcode{this->to_string(), 0};
62  }
63 
65  const teq::ArgsT& get_children (void) const override
66  {
67  return args_;
68  }
69 
71  void update_child (teq::FuncArg arg, size_t index) override
72  {
73  logs::fatal("cannot modify custom functor");
74  }
75 
77  void update (void) override
78  {
79  DataMapT<T> datamaps;
80  for (const teq::FuncArg& arg : args_)
81  {
82  auto tens = arg.get_tensor();
83  auto coorder = static_cast<eteq::CoordMap*>(arg.get_coorder().get());
84  datamaps.push_back(eteq::OpArg<T>{
85  eteq::TO_NODE(tens)->data(),
86  tens->shape(),
87  coorder
88  });
89  }
90  op_(out_, datamaps);
91  }
92 
94  void* data (void) override
95  {
96  return out_.data();
97  }
98 
100  const void* data (void) const override
101  {
102  return out_.data();
103  }
104 
106  size_t type_code (void) const override
107  {
108  return egen::get_type<T>();
109  }
110 
112  std::string type_label (void) const override
113  {
114  return egen::name_type(egen::get_type<T>());
115  }
116 
118  size_t nbytes (void) const override
119  {
120  return sizeof(T) * shape_.n_elems();
121  }
122 
123 private:
126  op_(op), shape_(shape), args_(args) {}
127 
129 
131 
134 
137 };
138 
140 template <typename T>
141 struct CustomFunctorNode final : public eteq::iNode<T>
142 {
143  CustomFunctorNode (std::shared_ptr<CustomFunctor<T>> f) : func_(f) {}
144 
147  {
148  return static_cast<CustomFunctorNode<T>*>(clone_impl());
149  }
150 
152  T* data (void) override
153  {
154  return (T*) func_->data();
155  }
156 
158  void update (void) override
159  {
160  func_->update();
161  }
162 
164  teq::TensptrT get_tensor (void) const override
165  {
166  return func_;
167  }
168 
169 protected:
170  eteq::iNode<T>* clone_impl (void) const override
171  {
172  return new CustomFunctorNode(
173  std::make_shared<CustomFunctor<T>>(*func_));
174  }
175 
176 private:
177  std::shared_ptr<CustomFunctor<T>> func_;
178 };
179 
180 template <typename T>
182 {
183  static bool registered = eteq::register_builder<CustomFunctor<T>,T>(
184  [](teq::TensptrT tens)
185  {
186  return std::make_shared<CustomFunctorNode<T>>(
187  std::static_pointer_cast<CustomFunctor<T>>(tens));
188  });
189  assert(registered);
190 
191  size_t nargs = args.size();
192  if (0 == nargs)
193  {
194  logs::fatal("cannot create custom functor without args");
195  }
196 
197  teq::Shape shape = args[0].shape();
198  for (size_t i = 1, n = nargs; i < n; ++i)
199  {
200  teq::Shape ishape = args[i].shape();
201  if (false == ishape.compatible_after(shape, 0))
202  {
203  logs::fatalf("cannot create custom functor with "
204  "incompatible shapes %s and %s",
205  shape.to_string().c_str(),
206  ishape.to_string().c_str());
207  }
208  }
209 
210  teq::ArgsT input_args;
211  input_args.reserve(nargs);
212  std::transform(args.begin(), args.end(),
213  std::back_inserter(input_args),
214  [](eteq::FuncArg<T>& arg)
215  {
216  return teq::FuncArg(
217  arg.get_tensor(),
218  arg.get_shaper(),
219  arg.map_io(),
220  arg.get_coorder());
221  });
222  return new CustomFunctor<T>(op, shape, input_args);
223 }
224 
226 template <typename T>
228 {
229  return std::make_shared<CustomFunctorNode<T>>(
230  std::shared_ptr<CustomFunctor<T>>(CustomFunctor<T>::get(op, args))
231  );
232 }
233 
234 }
235 
236 #endif // DBG_CUSTOM_FUNCTOR_HPP
std::string to_string(void) const override
Implementation of iTensor.
Definition: custom_functor.hpp:53
std::string to_string(void) const
Return string representation of shape.
Definition: shape.hpp:148
args
Definition: csv_to_png.py:105
CustomFunctor(const CustomFunctor< T > &other)=default
A functor node with direct access to evaluated data.
Definition: iopfunc.hpp:20
Encoding of operation.
Definition: ifunctor.hpp:18
CustomOpF< T > op_
Definition: custom_functor.hpp:130
const void * data(void) const override
Implementation of iData.
Definition: custom_functor.hpp:100
CustomFunctorNode(std::shared_ptr< CustomFunctor< T >> f)
Definition: custom_functor.hpp:143
eteq::iNode< T > * clone_impl(void) const override
Definition: custom_functor.hpp:170
static CustomFunctor< T > * get(CustomOpF< T > op, eteq::ArgsT< T > args)
Return a CustomFunctor with input function and meta arguments.
Definition: custom_functor.hpp:181
Definition: constant.hpp:17
std::vector< FuncArg > ArgsT
Type of functor arguments.
Definition: funcarg.hpp:101
Definition: custom_functor.hpp:20
nargs
Definition: csv_to_png.py:101
Definition: shape.hpp:62
void update(void) override
Implementation of iOperableFunc.
Definition: custom_functor.hpp:77
CustomFunctor&#39;s node wrapper.
Definition: custom_functor.hpp:141
Interface node for wrapping typed tensor.
Definition: inode.hpp:23
Eigen node version of teq::FuncArg.
Definition: funcarg.hpp:22
bool compatible_after(const Shape &other, RankT idx) const
Definition: shape.hpp:136
size_t nbytes(void) const override
Implementation of iData.
Definition: custom_functor.hpp:118
teq::Opcode get_opcode(void) const override
Implementation of iFunctor.
Definition: custom_functor.hpp:59
Coordinate mapper and tensor pair.
Definition: funcarg.hpp:21
std::shared_ptr< CustomFunctor< T > > func_
Definition: custom_functor.hpp:177
std::string type_label(void) const override
Implementation of iData.
Definition: custom_functor.hpp:112
Eigen transformation wrapper implementation of iCoordMap.
Definition: coord.hpp:18
void update(void) override
Implementation of iNode<T>
Definition: custom_functor.hpp:158
std::function< void(eteq::TensorT< T > &, const DataMapT< T > &)> CustomOpF
Custom functor to assign DataMap to Eigen tensor output.
Definition: custom_functor.hpp:29
teq::Shape shape_
Shape info built at construction time according to arguments.
Definition: custom_functor.hpp:133
eteq::NodeptrT< T > make_functor(CustomOpF< T > op, eteq::ArgsT< T > args)
Return custom functor node given custom function and arguments.
Definition: custom_functor.hpp:227
teq::TensptrT get_tensor(void) const override
Implementation of iNode<T>
Definition: custom_functor.hpp:164
const teq::ArgsT & get_children(void) const override
Implementation of iFunctor.
Definition: custom_functor.hpp:65
std::vector< eteq::OpArg< T > > DataMapT
Arguments of raw data and shapes.
Definition: custom_functor.hpp:25
std::shared_ptr< iTensor > TensptrT
Tensor smart pointer.
Definition: itensor.hpp:51
std::vector< FuncArg< T > > ArgsT
Type of typed functor arguments.
Definition: funcarg.hpp:84
Raw data, shape, and transformation argument struct.
Definition: operator.hpp:28
void update_child(teq::FuncArg arg, size_t index) override
Implementation of iFunctor.
Definition: custom_functor.hpp:71
T * data(void) override
Implementation of iNode<T>
Definition: custom_functor.hpp:152
void * data(void) override
Implementation of iData.
Definition: custom_functor.hpp:94
Eigen::Tensor< T, 8 > TensorT
Eigen Tensor.
Definition: eigen.hpp:35
Functor that runs a custom functor instead of Eigen operators.
Definition: custom_functor.hpp:33
NElemT index(Shape shape, CoordT coord)
std::shared_ptr< iNode< T > > NodeptrT
Smart pointer of node.
Definition: inode.hpp:63
CustomFunctor(CustomOpF< T > op, teq::Shape shape, teq::ArgsT args)
Definition: custom_functor.hpp:124
eteq::TensorT< T > out_
Definition: custom_functor.hpp:128
teq::ArgsT args_
Tensor arguments (and children)
Definition: custom_functor.hpp:136
const teq::Shape & shape(void) const override
Implementation of iTensor.
Definition: custom_functor.hpp:47
size_t type_code(void) const override
Implementation of iData.
Definition: custom_functor.hpp:106
NElemT n_elems(void) const
Return the total number of elements represented by the shape.
Definition: shape.hpp:118
#define TO_NODE(tens)
Macro for converting tensor to node.
Definition: inode.hpp:106
CustomFunctorNode< T > * clone(void) const
Return deep copy of this instance (with a copied functor)
Definition: custom_functor.hpp:146
DimensionsT shape_convert(teq::Shape shape)
Return Eigen shape of teq Shape.
CustomFunctor< T > & operator=(const CustomFunctor< T > &other)=delete