SST/macro
variable.h
Go to the documentation of this file.
1 
2 #ifndef fake_variable_h_
3 #define fake_variable_h_
4 
5 #include <type_traits>
6 #include <cstddef>
8 
9 #define DECLARE_OPERATOR(op,CONST,REF) \
10  template <class T, class U> \
11  Variable<T>REF \
12  operator op(CONST Variable<T>& t, const U& u); \
13  template <class T, class U> \
14  U REF \
15  operator op(CONST U& u, const Variable<T>& t); \
16  template <class T, class U> \
17  Variable<T> REF \
18  operator op(CONST Variable<T>& t, const Variable<U>& u);
19 
20 #define FRIEND_OPERATOR(op,CONST,REF) \
21  template <class T1, class U> \
22  friend Variable<T1> REF \
23  operator op(CONST Variable<T1>& t, const U& u); \
24  template <class T1, class U> \
25  friend U REF \
26  operator op(CONST U& u, const Variable<T1>& t); \
27  template <class T1, class U> \
28  friend Variable<T1> REF \
29  operator op(CONST Variable<T1>& t, const Variable<U>& u);
30 
31 template <class T> class VariablePtr;
32 template <class T> class Variable;
33 
34 DECLARE_OPERATOR(+,const,)
35 DECLARE_OPERATOR(-,const,)
36 DECLARE_OPERATOR(*,const,)
37 DECLARE_OPERATOR(/,const,)
38 DECLARE_OPERATOR(&,const,)
39 DECLARE_OPERATOR(|,const,)
40 DECLARE_OPERATOR(+=,,&)
41 DECLARE_OPERATOR(*=,,&)
42 DECLARE_OPERATOR(-=,,&)
43 DECLARE_OPERATOR(/=,,&)
44 DECLARE_OPERATOR(&=,,&)
45 DECLARE_OPERATOR(|=,,&)
46 
47 template <class T>
48 Variable<T>
49 sqrt(const Variable<T> &t);
50 
51 template <class T>
52 Variable<T>
53 cbrt(const Variable<T> &t);
54 
55 template <class T>
56 Variable<T>
57 fabs(const Variable<T> &t);
58 
59 template <class T>
60 void*
61 memset(const VariablePtr<T>& t, int value, size_t size);
62 
63 template <class T>
64 void*
65 memcpy(const VariablePtr<T>& dst, const VariablePtr<T>& src, size_t size);
66 
67 template <class T>
68 class Variable
69 {
70  FRIEND_OPERATOR(+,const,)
71  FRIEND_OPERATOR(-,const,)
72  FRIEND_OPERATOR(*,const,)
73  FRIEND_OPERATOR(/,const,)
74  FRIEND_OPERATOR(&,const,)
75  FRIEND_OPERATOR(|,const,)
76  FRIEND_OPERATOR(+=,,&)
77  FRIEND_OPERATOR(*=,,&)
78  FRIEND_OPERATOR(-=,,&)
79  FRIEND_OPERATOR(/=,,&)
80  FRIEND_OPERATOR(&=,,&)
81  FRIEND_OPERATOR(|=,,&)
82  friend Variable<T> sqrt<>(const Variable<T> &t);
83  friend Variable<T> cbrt<>(const Variable<T> &t);
84  friend Variable<T> fabs<>(const Variable<T> &t);
85 
86  public:
87  template <typename = std::enable_if<std::is_default_constructible<T>::value>>
88  Variable()
89  : nops(sstmac::sw::operating_system::current_thread()
90  ->register_perf_ctr_variable<uint64_t>(this)),
91  owns_nops(true)
92  {
93  }
94 
95  template <typename U,
96  typename = std::enable_if<std::is_convertible<T,U>::value>>
97  Variable(const U& u)
98  : nops(sstmac::sw::operating_system::current_thread()
99  ->register_perf_ctr_variable<uint64_t>(this)),
100  owns_nops(true)
101  {
102  }
103 
104  Variable(uint64_t &nops_array)
105  : nops(nops_array),
106  owns_nops(false)
107  {
108  }
109 
110  Variable(const Variable& v)
111  : nops(sstmac::sw::operating_system::current_thread()
112  ->register_perf_ctr_variable<uint64_t>(this)),
113  owns_nops(true)
114  {
115  }
116 
118  if (owns_nops)
120  ->remove_perf_ctr_variable(this);
121  }
122 
124  return VariablePtr<T>(nops);
125  }
126 
127  void* operator new[](std::size_t count) throw() {
128  return 0;
129  }
130 
131  Variable& operator=(const Variable& v) {
132  return *this;
133  }
134 
135  template <class U>
136  Variable& operator=(const U& u){
137  return *this;
138  }
139 
140  constexpr operator T() const {
141  return 0;
142  }
143 
144  Variable operator-() const {
145  nops++;
146  return *this;
147  }
148 
149  Variable& operator++(){
150  nops++;
151  return *this;
152  }
153 
154  Variable operator++(int u){
155  nops++;
156  return *this;
157  }
158 
159  Variable& operator--(){
160  nops++;
161  return *this;
162  }
163 
164  Variable operator--(int u){
165  nops++;
166  return *this;
167  }
168 
169  private:
170  uint64_t &nops;
171  bool owns_nops;
172 };
173 
174 #define COMPARE(op) \
175  template <class T, class U> \
176  bool \
177  operator op(const Variable<T>& t, const U& u){ \
178  return true; \
179  } \
180  template <class T, class U> \
181  bool \
182  operator op(const U& u, const Variable<T>& t){ \
183  return true;\
184  } \
185  template <class T, class U> \
186  bool \
187  operator op(const Variable<T>& t, const Variable<U>& u){ \
188  return true; \
189  }
190 
191 #define OPERATOR(op,CONST,REF) \
192  template <class T, class U> \
193  Variable<T>REF \
194  operator op(CONST Variable<T>& t, const U& u){ \
195  t.nops++; \
196  return t; \
197  } \
198  template <class T, class U> \
199  U REF \
200  operator op(CONST U& u, const Variable<T>& t){ \
201  t.nops++; \
202  return u;\
203  } \
204  template <class T, class U> \
205  Variable<T> REF \
206  operator op(CONST Variable<T>& t, const Variable<U>& u){ \
207  t.nops++; \
208  return t; \
209  }
210 
211 COMPARE(!=)
212 COMPARE(<)
213 COMPARE(>)
214 COMPARE(<=)
215 COMPARE(>=)
216 COMPARE(==)
217 OPERATOR(+,const,)
218 OPERATOR(-,const,)
219 OPERATOR(*,const,)
220 OPERATOR(/,const,)
221 OPERATOR(&,const,)
222 OPERATOR(|,const,)
223 OPERATOR(+=,,&)
224 OPERATOR(*=,,&)
225 OPERATOR(-=,,&)
226 OPERATOR(/=,,&)
227 OPERATOR(&=,,&)
228 OPERATOR(|=,,&)
229 
230 template <class T>
231 Variable<T>
232 sqrt(const Variable<T> &t){
233  t.nops++;
234  return t;
235 }
236 
237 template <class T>
239 cbrt(const Variable<T> &t){
240  t.nops++;
241  return t;
242 }
243 
244 template <class T>
246 fabs(const Variable<T> &t){
247  t.nops++;
248  return t;
249 }
250 
251 
252 template <class T>
253 class VariablePtr
254 {
255  friend void* memset<>(const VariablePtr<T>& t, int value, size_t size);
256  friend void* memcpy<>(const VariablePtr<T>& dst, const VariablePtr<T>& src, size_t size);
257 
258  public:
260  : nops(sstmac::sw::operating_system::current_thread()
261  ->register_perf_ctr_variable<uint64_t>(this)),
262  elem(nops)
263  {
264  }
265 
266  template <typename U,
267  typename = std::enable_if<std::is_convertible<T*,U>::value>>
268  VariablePtr(const U& u)
269  : nops(sstmac::sw::operating_system::current_thread()
270  ->register_perf_ctr_variable<uint64_t>(this)),
271  elem(nops)
272  {
273  }
274 
275  VariablePtr(uint64_t &nops_elem)
276  : nops(nops_elem)
277  {
278  }
279 
281  : nops(sstmac::sw::operating_system::current_thread()
282  ->register_perf_ctr_variable<uint64_t>(this)),
283  elem(nops)
284  {
285  }
286 
289  ->remove_perf_ctr_variable(this);
290  }
291 
292  void resize(size_t n){}
293 
294  void clear(){}
295 
297  return elem;
298  }
299 
300  const Variable<T>& operator[](int idx) const {
301  return elem;
302  }
303 
305  return elem;
306  }
307 
309  return elem;
310  }
311 
313  return elem;
314  }
315 
316  const Variable<T>& operator*() const {
317  return elem;
318  }
319 
321  operator=(const VariablePtr& ptr){
322  return *this;
323  }
324 
325  template <class U,
326  typename = std::enable_if<std::is_convertible<T*,U>::value>>
328  operator=(const U& ptr){
329  return *this;
330  }
331 
332  template <class U,
333  typename = std::enable_if<std::is_convertible<T*,U>::value>>
334  const VariablePtr<T>&
335  operator=(const U& ptr) const {
336  return *this;
337  }
338 
339  template <class U,
340  typename = std::enable_if<std::is_convertible<T*,U>::value>>
341  bool
342  operator==(const U& ptr) const {
343  return false;
344  }
345 
346  template <class U,
347  typename = std::enable_if<std::is_convertible<T*,U>::value>>
348  bool
349  operator!=(const U& ptr) const {
350  return true;
351  }
352 
353  constexpr operator void*() const {
354  return 0;
355  }
356 
358  operator+=(const ptrdiff_t &offset){
359  return *this;
360  }
361 
363  operator-=(const ptrdiff_t &offset){
364  return *this;
365  }
366 
367  private:
368  uint64_t &nops;
370 };
371 
372 template <class T>
373 void*
374 memset(const VariablePtr<T>& t, int value, size_t size){
375  t.nops += size / sizeof(T);
376  return 0;
377 }
378 
379 template <class T>
380 void*
381 memcpy(const VariablePtr<T>& dst, const VariablePtr<T>& src, size_t size){
382  dst.nops += size / sizeof(T);
383  return 0;
384 }
385 
390 
391 
392 #endif
393 
Variable< T > & operator[](int idx)
Definition: variable.h:296
bool owns_nops
Definition: variable.h:171
const VariablePtr< T > & operator=(const U &ptr) const
Definition: variable.h:335
#define FRIEND_OPERATOR(op, CONST, REF)
Definition: variable.h:20
~Variable()
Definition: variable.h:117
bool operator==(const U &ptr) const
Definition: variable.h:342
Variable< T > & operator[](Variable< int > idx)
Definition: variable.h:304
Variable operator-() const
Definition: variable.h:144
VariablePtr(const VariablePtr &vp)
Definition: variable.h:280
#define DECLARE_OPERATOR(op, CONST, REF)
Definition: variable.h:9
VariablePtr(const U &u)
Definition: variable.h:268
void * memcpy(const VariablePtr< T > &dst, const VariablePtr< T > &src, size_t size)
Definition: variable.h:381
VariablePtr< T > operator&()
Definition: variable.h:123
friend void * memset(const VariablePtr< T > &t, int value, size_t size)
Definition: variable.h:374
friend void * memcpy(const VariablePtr< T > &dst, const VariablePtr< T > &src, size_t size)
Definition: variable.h:381
Variable & operator=(const Variable &v)
Definition: variable.h:131
Variable< int > Int
Definition: variable.h:388
const Variable< T > & operator[](int idx) const
Definition: variable.h:300
~VariablePtr()
Definition: variable.h:287
VariablePtr< double > DoublePtr
Definition: variable.h:387
Variable operator--(int u)
Definition: variable.h:164
VariablePtr< T > & operator+=(const ptrdiff_t &offset)
Definition: variable.h:358
Variable< T > sqrt(const Variable< T > &t)
Definition: variable.h:232
const Variable< T > & operator*() const
Definition: variable.h:316
Variable(const U &u)
Definition: variable.h:97
static thread * current_thread()
void resize(size_t n)
Definition: variable.h:292
Variable(const Variable &v)
Definition: variable.h:110
SUMI = Simulator unified messagine interface It is also the name for a solid ink in Japanese - i...
Variable operator++(int u)
Definition: variable.h:154
Variable & operator=(const U &u)
Definition: variable.h:136
Variable< T > fabs(const Variable< T > &t)
Definition: variable.h:246
const Variable< T > & operator[](Variable< int > idx) const
Definition: variable.h:308
VariablePtr< int > IntPtr
Definition: variable.h:389
Variable & operator++()
Definition: variable.h:149
VariablePtr(uint64_t &nops_elem)
Definition: variable.h:275
void clear()
Definition: variable.h:294
#define COMPARE(op)
Definition: variable.h:174
bool operator!=(const U &ptr) const
Definition: variable.h:349
uint64_t & nops
Definition: variable.h:170
Variable< double > Double
Definition: variable.h:386
Variable(uint64_t &nops_array)
Definition: variable.h:104
Variable< T > & operator*()
Definition: variable.h:312
VariablePtr< T > & operator-=(const ptrdiff_t &offset)
Definition: variable.h:363
void * memset(const VariablePtr< T > &t, int value, size_t size)
Definition: variable.h:374
uint64_t & nops
Definition: variable.h:368
VariablePtr< T > & operator=(const VariablePtr &ptr)
Definition: variable.h:321
Variable< T > cbrt(const Variable< T > &t)
Definition: variable.h:239
Variable< T > elem
Definition: variable.h:369
void remove_perf_ctr_variable(void *ptr)
Definition: thread.h:275
VariablePtr< T > & operator=(const U &ptr)
Definition: variable.h:328
#define OPERATOR(op, CONST, REF)
Definition: variable.h:191
Variable & operator--()
Definition: variable.h:159