TVM  0.9.4
CompiledAssignmentWrapper.h
Go to the documentation of this file.
1 
3 #pragma once
4 
6 
7 #include <Eigen/Core>
8 
9 #include <memory>
10 
11 namespace tvm
12 {
13 
14 namespace scheme
15 {
16 
17 namespace internal
18 {
28 template<typename MatrixType>
30 {
31 public:
36 
39 
41  void run();
45  void from(double);
49  void from(const Eigen::Ref<const MatrixType> & from);
51  void to(const Eigen::Ref<MatrixType> &);
52 
64  template<AssignType A, WeightMult W, MatrixMult M, Source F = EXTERNAL, typename... Args>
65  static CompiledAssignmentWrapper make(Args &&... args);
66 
67 private:
68  CompiledAssignmentWrapper(void (*deleter)(void *));
69 
71  template<typename T>
72  static void srun(void * ca);
74  template<typename T>
75  static void sdelete(void * ca);
77  template<typename T>
78  static void sfrom(void * ca, const typename T::SourceType & f);
80  template<typename T>
81  static void sto(void * ca, const Eigen::Ref<MatrixType> & from);
83  template<typename T>
84  static void * sclone(void * ca);
85 
87  std::unique_ptr<void, void (*)(void *)> ca_;
89  void (*run_)(void *);
93  void (*fromd_)(void *, const double &);
97  void (*fromm_)(void *, const Eigen::Ref<const MatrixType> &);
99  void (*to_)(void *, const Eigen::Ref<MatrixType> &);
101  void * (*clone_)(void *);
102 };
103 
104 template<typename MatrixType>
105 template<typename T>
106 inline void CompiledAssignmentWrapper<MatrixType>::srun(void * ca)
107 { static_cast<T *>(ca)->run(); }
108 
109 template<typename MatrixType>
110 template<typename T>
111 inline void CompiledAssignmentWrapper<MatrixType>::sdelete(void * ca)
112 { delete static_cast<T *>(ca); }
113 
114 template<typename MatrixType>
115 template<typename T>
116 inline void CompiledAssignmentWrapper<MatrixType>::sfrom(void * ca, const typename T::SourceType & f)
117 { static_cast<T *>(ca)->from(f); }
118 
119 template<typename MatrixType>
120 template<typename T>
121 inline void CompiledAssignmentWrapper<MatrixType>::sto(void * ca, const Eigen::Ref<MatrixType> & t)
122 { static_cast<T *>(ca)->to(t); }
123 
124 template<typename MatrixType>
125 template<typename T>
126 inline void * CompiledAssignmentWrapper<MatrixType>::sclone(void * ca)
127 { return new T(*static_cast<T *>(ca)); }
128 
129 template<typename MatrixType>
130 template<AssignType A, WeightMult W, MatrixMult M, Source F, typename... Args>
132 {
135  w.run_ = srun<CA>;
136  if constexpr(F == CONSTANT)
137  {
138  w.fromd_ = &sfrom<CA>;
139  w.fromm_ = nullptr;
140  }
141  else
142  {
143  w.fromd_ = nullptr;
144  w.fromm_ = &sfrom<CA>;
145  }
146  w.to_ = sto<CA>;
147  w.clone_ = sclone<CA>;
148  w.ca_.reset(new CA(std::forward<Args>(args)...));
149  return w;
150 }
151 
152 template<typename MatrixType>
154 : ca_(nullptr, nullptr), run_(nullptr), fromd_(nullptr), fromm_(nullptr), to_(nullptr), clone_(nullptr)
155 {}
156 
157 template<typename MatrixType>
159 : ca_(nullptr, deleter), run_(nullptr), fromd_(nullptr), fromm_(nullptr), to_(nullptr), clone_(nullptr)
160 {}
161 
162 template<typename MatrixType>
164 : ca_(other.clone_(other.ca_.get()), other.ca_.get_deleter()), run_(other.run_), fromd_(other.fromd_),
165  fromm_(other.fromm_), to_(other.to_), clone_(other.clone_)
166 {}
167 
168 template<typename MatrixType>
170  const CompiledAssignmentWrapper & other)
171 {
172  ca_.reset(other.clone_(other.ca_.get()));
173  ca_.get_deleter() = other.ca_.get_deleter();
174  run_ = other.run_;
175  fromd_ = other.fromd_;
176  fromm_ = other.fromm_;
177  to_ = other.to_;
178  clone_ = other.clone_;
179 
180  return *this;
181 }
182 
183 template<typename MatrixType>
185 { run_(ca_.get()); }
186 
187 template<typename MatrixType>
189 {
190  if(fromd_)
191  {
192  fromd_(ca_.get(), d);
193  }
194  else
195  {
196  throw std::runtime_error(
197  "Method from(double) is invalid for this assignment, try from(const Eigen::Ref<const MatrixType>&) instead");
198  }
199 }
200 
201 template<typename MatrixType>
202 inline void CompiledAssignmentWrapper<MatrixType>::from(const Eigen::Ref<const MatrixType> & f)
203 {
204  if(fromm_)
205  {
206  fromm_(ca_.get(), f);
207  }
208  else
209  {
210  throw std::runtime_error(
211  "Method from(const Eigen::Ref<const MatrixType>&) is invalid for this assignment, try from(double) instead");
212  }
213 }
214 
215 template<typename MatrixType>
216 inline void CompiledAssignmentWrapper<MatrixType>::to(const Eigen::Ref<MatrixType> & t)
217 { to_(ca_.get(), t); }
218 
219 } // namespace internal
220 
221 } // namespace scheme
222 
223 } // namespace tvm
Definition: CompiledAssignmentWrapper.h:30
CompiledAssignmentWrapper & operator=(CompiledAssignmentWrapper &&)=default
void from(double)
Definition: CompiledAssignmentWrapper.h:188
CompiledAssignmentWrapper(CompiledAssignmentWrapper &&)=default
void from(const Eigen::Ref< const MatrixType > &from)
Definition: CompiledAssignmentWrapper.h:202
void run()
Definition: CompiledAssignmentWrapper.h:184
CompiledAssignmentWrapper(const CompiledAssignmentWrapper &)
Definition: CompiledAssignmentWrapper.h:163
void to(const Eigen::Ref< MatrixType > &)
Definition: CompiledAssignmentWrapper.h:216
CompiledAssignmentWrapper & operator=(const CompiledAssignmentWrapper &)
Definition: CompiledAssignmentWrapper.h:169
static CompiledAssignmentWrapper make(Args &&... args)
CompiledAssignmentWrapper()
Definition: CompiledAssignmentWrapper.h:153
Definition: CompiledAssignment.h:598
Source
Definition: CompiledAssignment.h:65
@ CONSTANT
Definition: CompiledAssignment.h:71
@ EXTERNAL
Definition: CompiledAssignment.h:67
WeightMult
Definition: CompiledAssignment.h:34
AssignType
Definition: CompiledAssignment.h:22
MatrixMult
Definition: CompiledAssignment.h:45
Definition: Clock.h:12