SST/macro
pair_payload.h
Go to the documentation of this file.
1 /*
2  * This file is part of SST/macroscale:
3  * The macroscale architecture simulator from the SST suite.
4  * Copyright (c) 2009 Sandia Corporation.
5  * This software is distributed under the BSD License.
6  * Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
7  * the U.S. Government retains certain rights in this software.
8  * For more information, see the LICENSE file in the top
9  * SST/macroscale directory.
10  */
11 
12 #ifndef SSTMAC_COMMON_MESSAGES_PAIRPAYLOAD_H_INCLUDED
13 #define SSTMAC_COMMON_MESSAGES_PAIRPAYLOAD_H_INCLUDED
14 
15 #include <sstream>
16 
17 #include <sstmac/common/messages/payload.h>
18 
19 namespace sstmac {
20 
21 /**
22  * Network payload consisting of a value with sensible copy semantics.
23  *
24  * Don't use this for pointer types, since you will break assumptions
25  * about const-ness or protection of underlying data.
26  */
27 template<typename T1, typename T2>
28 class pair_payload : public payload
29 {
30  /// The data we keep.
31  mutable T1 data1_;
32  mutable T2 data2_;
33  size_t size_;
35 
36  /// Construction time.
37  pair_payload(const T1 &inval1, const T2 &inval2,
38  size_t size = sizeof(T1) + sizeof(T2)) :
39  data1_(inval1), data2_(inval2), size_(size) {
40  }
41 
42  /// Private copy constructor.
44  size_t size = sizeof(T1) + sizeof(T2)) :
45  data1_(other.data1_), data2_(other.data2_), size_(size) {
46  }
47 
48  public:
51 
52  virtual
54  }
55 
56  /// Construct a new valuepayload.
57  static typename pair_payload<T1, T2>::const_ptr
58  construct(const T1 &inval1, const T2 &inval2) {
59  return new pair_payload<T1, T2>(inval1, inval2);
60  }
61 
62  /// Clone this object.
63  payload::const_ptr
64  clone() const {
65  return new pair_payload<T1, T2>(*this);
66  }
67 
68  /// Access the underlying data in a non-const context.
69  std::pair<T1, T2>
71  return std::pair<T1, T2>(data1_, data2_);
72  }
73 
74  /// Access the underlying data in a const context.
75  const std::pair<T1, T2>
76  data() const {
77  return std::pair<T1, T2>(data1_, data2_);
78  }
79 
80  virtual long
81  byte_length() const {
82  return size_;
83  }
84 
85  /**
86  * Add operator
87  * @param other
88  * @return a smart pointer to a new valuepayload object
89  */
90  virtual const payload::const_ptr
91  add(const payload::const_ptr &other) const {
92  const_ptr casted = ptr_safe_cast<
93 const pair_payload<T1, T2> >(other);
94  T1 newval1 = casted->data().first + data().first;
95  T2 newval2 = casted->data().second + data().second;
96  payload::const_ptr ret = construct(newval1, newval2);
97  return ret;
98  }
99 
100  /**
101  * Less than comparator, to only be used for the minloc mpi operation
102  * @param other
103  * @return
104  */
105  virtual const payload::const_ptr
106  min(const payload::const_ptr &other) const {
107  const_ptr casted = ptr_safe_cast<
108  const pair_payload<T1, T2> >(other);
109  if (data1_ < casted->data().first) {
110  return pair_payload<T1, T2>::construct(data1_, data2_);
111  }
112  else {
113  return pair_payload<T1, T2>::construct(casted->data().first,
114  casted->data().second);
115  }
116 
117  }
118 
119  /**
120  * Greater than comparator, to only be used for the maxloc operation
121  * @param other
122  * @return
123  */
124  virtual const payload::const_ptr
125  max(const payload::const_ptr &other) const {
126  const_ptr casted = ptr_safe_cast<
127  const pair_payload<T1, T2> >(other);
128  if (data1_ > casted->data().first) {
129  return pair_payload<T1, T2>::construct(data1_, data2_);
130  }
131  else {
132  return pair_payload<T1, T2>::construct(casted->data().first,
133  casted->data().second);
134  }
135 
136  }
137 
138  /**
139  * Equals comparator
140  * @param other
141  * @return
142  */
143  virtual bool
144  equals(const payload::const_ptr &other) const {
145  const_ptr casted = ptr_safe_cast<
146  const pair_payload<T1, T2> >(other);
147  return data() == casted->data();
148 
149  }
150 
151  /**
152  * Logical or comparator
153  * @param other
154  * @return
155  */
156  const payload::const_ptr
157  logical_or(const payload::const_ptr &other) const {
159  "pairpayload: logical_or doesn't make sense");
160  }
161 
162  /**
163  * Logical xor comparator
164  * @param other
165  * @return
166  */
167  const payload::const_ptr
168  logical_xor(const payload::const_ptr &other) const {
170  "pairpayload: logical_xor doesn't make sense");
171  }
172 
173  /**
174  * Logical and comparator
175  * @param other
176  * @return
177  */
178  const payload::const_ptr
179  logical_and(const payload::const_ptr &other) const {
181  "pairpayload: logical_and doesn't make sense");
182  }
183 
184  /**
185  * Serialize this object into the spkt_serializer, for
186  * running in parallel simulation
187  * @param ser the serializer to use
188  */
189  virtual void
190  serialize_order(sprockit::spkt_serializer* ser) const {
191  spkt_throw_printf(sprockit::unimplemented_error, "pairpayload: serialize unimplemented");
192  }
193 
194  /**
195  * Strinfier
196  * @return a std::string description
197  */
198  virtual std::string
199  to_string() const {
200  std::stringstream ss;
201  ss << "pairpayload(" << data1_ << ", " << data2_ << ")";
202  return ss.str();
203  }
204 
205 };
206 
207 /**
208  * Network payload consisting of a value with sensible copy semantics.
209  *
210  * Don't use this for pointer types, since you will break assumptions
211  * about const-ness or protection of underlying data.
212  */
213 template<typename T1, typename T2>
215 {
216 
217  struct structtype {
218  T1 a;
219  T2 b;
220  };
221 
222  /// The data we keep.
223  void* buf_;
224  size_t size_;
225 
226  /// Construction time.
227  pairpayloadvector(void* start, size_t size, bool copy) :
228  size_(size) {
229  if (copy) {
230  size_t s = (sizeof(T1) + sizeof(T2)) * size;
231  buf_ = malloc(s);
232 
233  memcpy(buf_, start, s);
234  }
235  else {
236  buf_ = start;
237  }
238  }
239 
240  /// Private copy constructor.
241  pairpayloadvector(const pairpayloadvector<T1, T2> &other, size_t size) :
242  size_(size) {
243  size_t s = (sizeof(T1) + sizeof(T2)) * size;
244  buf_ = malloc(s);
245 
246  memcpy(buf_, other.buf_, s);
247  }
248 
249  public:
252 
253  virtual
255  free(buf_);
256  }
257 
258  /// Construct a new valuepayload.
260  construct(void* start, size_t size, bool copy = true) {
261  return new pairpayloadvector<T1, T2>(start, size, copy);
262  }
263 
264  /// Clone this object.
265  payload::const_ptr
266  clone() const {
267  return new pairpayloadvector<T1, T2>(*this);
268  }
269 
270  /// Access the underlying data in a non-const context.
271  void*
273  return buf_;
274  }
275 
276  /// Access the underlying data in a const context.
277  const void*
278  data() const {
279  return buf_;
280  }
281 
282  virtual long
283  byte_length() const {
284  return size_ * (sizeof(T1) + sizeof(T2));
285  }
286 
287  /**
288  * Add operator
289  * @param other
290  * @return a smart pointer to a new valuepayload object
291  */
292  virtual const payload::const_ptr
293  add(const payload::const_ptr &other) const {
294  const_ptr casted = ptr_safe_cast(PairPayload,other);
295  size_t s = (sizeof(T1) + sizeof(T2)) * size_;
296  structtype* newdata = (structtype*) malloc(s);
297  structtype* thisdata = (structtype*) data();
298  structtype* thatdata = (structtype*) casted->data();
299  for (int i = 0; i < size_; i++) {
300  newdata->a = thisdata->a + thatdata->a;
301  newdata->b = thisdata->b + thatdata->b;
302  newdata++;
303  thisdata++;
304  thatdata++;
305  }
306  payload::const_ptr ret = construct(newdata, size_, false);
307  return ret;
308  }
309 
310  /**
311  * Less than comparator, to only be used for the minloc mpi operation
312  * @param other
313  * @return
314  */
315  virtual const payload::const_ptr
316  min(const payload::const_ptr &other) const {
318  "pairpayloadvector::less_than - doesn't make sense");
319 
320  }
321 
322  /**
323  * Greater than comparator, to only be used for the maxloc operation
324  * @param other
325  * @return
326  */
327  virtual const payload::const_ptr
328  max(const payload::const_ptr &other) const {
330  "pairpayloadvector::greater_than - doesn't make sense");
331  }
332 
333  /**
334  * Equals comparator
335  * @param other
336  * @return
337  */
338  virtual bool
339  equals(const payload::const_ptr &other) const {
340  const_ptr casted = ptr_safe_cast(PairPayload, other);
341  bool ret = true;
342  structtype* thisdata = (structtype*) data();
343  structtype* thatdata = (structtype*) casted->data();
344  for (int i = 0; i < size_ && ret; i++) {
345  ret = ret && (thisdata->a == thatdata->a);
346  ret = ret && (thisdata->b == thatdata->b);
347  }
348 
349  return ret;
350 
351  }
352 
353  /**
354  * Logical or comparator
355  * @param other
356  * @return
357  */
358  const payload::const_ptr
359  logical_or(const payload::const_ptr &other) const {
361  "pairpayloadvector: logical_or doesn't make sense");
362  }
363 
364  /**
365  * Logical xor comparator
366  * @param other
367  * @return
368  */
369  const payload::const_ptr
370  logical_xor(const payload::const_ptr &other) const {
372  "pairpayloadvector: logical_xor doesn't make sense");
373  }
374 
375  /**
376  * Logical and comparator
377  * @param other
378  * @return
379  */
380  const payload::const_ptr
381  logical_and(const payload::const_ptr &other) const {
383  "pairpayloadvector: logical_and doesn't make sense");
384  }
385 
386  /**
387  * Serialize this object into the spkt_serializer, for
388  * running in parallel simulation
389  * @param ser the serializer to use
390  */
391  virtual void
392  serialize_order(sprockit::spkt_serializer* ser) const {
394  "pairpayloadvector: serialize unimplemented");
395  }
396 
397  /**
398  * Strinfier
399  * @return a std::string description
400  */
401  virtual std::string
402  to_string() const {
403  std::stringstream ss;
404  ss << "pairpayloadvector(" << size_ << ")";
405  return ss.str();
406  }
407 
408 };
409 
410 } // end of namespace sstmac
411 #endif
412 
virtual long byte_length() const
Definition: pair_payload.h:81
const payload::const_ptr logical_xor(const payload::const_ptr &other) const
Logical xor comparator.
Definition: pair_payload.h:168
Network payload consisting of a value with sensible copy semantics.
Definition: pair_payload.h:28
const payload::const_ptr logical_and(const payload::const_ptr &other) const
Logical and comparator.
Definition: pair_payload.h:381
sprockit::refcount_ptr< pairpayloadvector< T1, T2 > > ptr
Definition: pair_payload.h:250
virtual const payload::const_ptr add(const payload::const_ptr &other) const
Add operator.
Definition: pair_payload.h:293
const void * data() const
Access the underlying data in a const context.
Definition: pair_payload.h:278
void * memcpy(const VariablePtr< T > &dst, const VariablePtr< T > &src, size_t size)
Definition: variable.h:381
virtual void serialize_order(sprockit::spkt_serializer *ser) const
Serialize this object into the spkt_serializer, for running in parallel simulation.
Definition: pair_payload.h:190
virtual const payload::const_ptr max(const payload::const_ptr &other) const
Greater than comparator, to only be used for the maxloc operation.
Definition: pair_payload.h:328
pair_payload(const T1 &inval1, const T2 &inval2, size_t size=sizeof(T1)+sizeof(T2))
Construction time.
Definition: pair_payload.h:37
#define ptr_safe_cast(type,...)
First entry in VA_ARGS is the obj Second entry is optional being an error msg.
Definition: ptr_type.h:85
virtual bool equals(const payload::const_ptr &other) const
Equals comparator.
Definition: pair_payload.h:144
const std::pair< T1, T2 > data() const
Access the underlying data in a const context.
Definition: pair_payload.h:76
std::pair< T1, T2 > data_unconst()
Access the underlying data in a non-const context.
Definition: pair_payload.h:70
void * data_unconst()
Access the underlying data in a non-const context.
Definition: pair_payload.h:272
virtual std::string to_string() const
Strinfier.
Definition: pair_payload.h:199
const payload::const_ptr logical_or(const payload::const_ptr &other) const
Logical or comparator.
Definition: pair_payload.h:157
T1 data1_
The data we keep.
Definition: pair_payload.h:31
virtual const payload::const_ptr min(const payload::const_ptr &other) const
Less than comparator, to only be used for the minloc mpi operation.
Definition: pair_payload.h:316
static pairpayloadvector< T1, T2 >::const_ptr construct(void *start, size_t size, bool copy=true)
Construct a new valuepayload.
Definition: pair_payload.h:260
virtual bool equals(const payload::const_ptr &other) const
Equals comparator.
Definition: pair_payload.h:339
virtual long byte_length() const
Definition: pair_payload.h:283
pair_payload< T1, T2 > PairPayload
Definition: pair_payload.h:34
sprockit::refcount_ptr< const pairpayloadvector< T1, T2 > > const_ptr
Definition: pair_payload.h:251
virtual std::string to_string() const
Strinfier.
Definition: pair_payload.h:402
Network payload consisting of a value with sensible copy semantics.
Definition: pair_payload.h:214
SUMI = Simulator unified messagine interface It is also the name for a solid ink in Japanese - i...
virtual ~pair_payload()
Definition: pair_payload.h:53
pairpayloadvector(const pairpayloadvector< T1, T2 > &other, size_t size)
Private copy constructor.
Definition: pair_payload.h:241
virtual const payload::const_ptr add(const payload::const_ptr &other) const
Add operator.
Definition: pair_payload.h:91
sprockit::refcount_ptr< const pair_payload< T1, T2 > > const_ptr
Definition: pair_payload.h:50
#define spkt_throw_printf(exc, template_str,...)
Definition: errors.h:37
payload::const_ptr clone() const
Clone this object.
Definition: pair_payload.h:266
pair_payload(const pair_payload< T1, T2 > &other, size_t size=sizeof(T1)+sizeof(T2))
Private copy constructor.
Definition: pair_payload.h:43
virtual void serialize_order(sprockit::spkt_serializer *ser) const
Serialize this object into the spkt_serializer, for running in parallel simulation.
Definition: pair_payload.h:392
pairpayloadvector(void *start, size_t size, bool copy)
Construction time.
Definition: pair_payload.h:227
void * buf_
The data we keep.
Definition: pair_payload.h:223
const payload::const_ptr logical_xor(const payload::const_ptr &other) const
Logical xor comparator.
Definition: pair_payload.h:370
payload::const_ptr clone() const
Clone this object.
Definition: pair_payload.h:64
A function was intentionally unimplemented because it doesn&#39;t make sense, or it is ongoing work...
Definition: errors.h:181
virtual const payload::const_ptr max(const payload::const_ptr &other) const
Greater than comparator, to only be used for the maxloc operation.
Definition: pair_payload.h:125
virtual const payload::const_ptr min(const payload::const_ptr &other) const
Less than comparator, to only be used for the minloc mpi operation.
Definition: pair_payload.h:106
sprockit::refcount_ptr< pair_payload< T1, T2 > > ptr
Definition: pair_payload.h:49
const payload::const_ptr logical_and(const payload::const_ptr &other) const
Logical and comparator.
Definition: pair_payload.h:179
static pair_payload< T1, T2 >::const_ptr construct(const T1 &inval1, const T2 &inval2)
Construct a new valuepayload.
Definition: pair_payload.h:58
const payload::const_ptr logical_or(const payload::const_ptr &other) const
Logical or comparator.
Definition: pair_payload.h:359