SST/macro
vector_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_VECTORPAYLOAD_H_INCLUDED
13 #define SSTMAC_COMMON_MESSAGES_VECTORPAYLOAD_H_INCLUDED
14 
15 #include <sstream>
16 #include <sprockit/util.h>
17 #include <sstmac/common/messages/payload.h>
18 #include <cstddef>
19 
20 namespace sstmac {
21 
22 /**
23  * Network payload consisting of a boost multiarray.
24  *
25  */
26 template<typename Type, typename VectorType = std::vector<Type> >
28  public payload,
29  public serializable_type<vector1_payload<Type, VectorType> >
30 {
32 
33  protected:
34  /// The data we keep.
35  typedef VectorType arraytype;
36 
37  public:
38  typedef sprockit::refcount_ptr<vector1_payload<Type, VectorType> > ptr;
39  typedef sprockit::refcount_ptr<const vector1_payload<Type, VectorType> >
41 
42  public:
43  vector1_payload(){} //needed for serialization
44 
45  virtual ~vector1_payload() {
46  free(underneath_);
47  }
48 
49  void
50  assign(void *data) {
51  Type* casted = (Type*) data;
52  int count = dims_[0];
53  for (int i = 0; i < count; ++i) {
54  underneath_[i] = casted[i];
55  }
56  }
57 
58  static ptr
61  }
62 
63  static const_ptr
64  construct(const arraytype &inval, int dim1, ptrdiff_t stride = 1) {
65  return new vector1_payload<Type, VectorType> (inval, dim1, stride);
66  }
67 
68  /// Clone this object.
69  payload::const_ptr
70  clone() const {
71  return new vector1_payload<Type, VectorType> (underneath_, dims_[0], true, 1);
72  }
73 
74  /// Access the underlying data in a const context.
75  void*
76  data() const {
77  return underneath_;
78  }
79 
80  const Type*
81  typed_data() const {
82  return underneath_;
83  }
84 
85  int
86  get_dim() const {
87  return dims_[0];
88  }
89 
90  virtual long
91  byte_length() const {
92  int dimsize = 1;
93  for (int i = 0; i < dims_.size(); i++) {
94  dimsize *= dims_[i];
95  }
96  return sizeof(Type) * dimsize;
97  }
98 
100 
101  /**
102  * Add operator
103  * @param other
104  * @return a smart pointer to a new valuepayload object
105  */
106  virtual payload::const_ptr
107  add(const payload::const_ptr &other) const {
108  const_ptr casted = ptr_safe_cast(const Vector1Payload, other);
109 
110  Type* newval = (Type*) malloc(sizeof(Type) * dims_[0]);
111 
112  for (int i = 0; i < dims_[0]; i++) {
113 
114  newval[i] = underneath_[i] + casted->typed_data()[i];
115 
116  }
117 
118  payload::const_ptr ret = new Vector1Payload(newval, dims_[0], true, 1);
119 
120  free(newval);
121 
122  return ret;
123  }
124 
125  virtual payload::const_ptr
126  prod(const payload::const_ptr &other) const {
127  const_ptr casted = ptr_safe_cast(const Vector1Payload, other);
128 
129  Type* newval = (Type*) malloc(sizeof(Type) * dims_[0]);
130 
131  for (int i = 0; i < dims_[0]; i++) {
132 
133  newval[i] = underneath_[i] * casted->typed_data()[i];
134 
135  }
136 
137  payload::const_ptr ret = new Vector1Payload(newval, dims_[0], true, 1);
138 
139  free(newval);
140 
141  return ret;
142  }
143 
144  /**
145  * Less than comparator
146  * @param other
147  * @return
148  */
149  virtual payload::const_ptr
150  min(const payload::const_ptr &other) const {
151  const_ptr casted = ptr_safe_cast(const Vector1Payload, other);
152 
153  Type* newval = (Type*) malloc(sizeof(Type) * dims_[0]);
154 
155  for (int i = 0; i < dims_[0]; i++) {
156 
157  newval[i] = (underneath_[i] < casted->typed_data()[i]) ? underneath_[i]
158  : casted->typed_data()[i];
159 
160  }
161 
162  payload::const_ptr ret = new Vector1Payload(newval, dims_[0], true, 1);
163 
164  free(newval);
165 
166  return ret;
167  }
168 
169  /**
170  * Greater than comparator
171  * @param other
172  * @return
173  */
174  virtual payload::const_ptr
175  max(const payload::const_ptr &other) const {
176  const_ptr casted = ptr_safe_cast(const Vector1Payload, other);
177 
178  Type* newval = (Type*) malloc(sizeof(Type) * dims_[0]);
179 
180  for (int i = 0; i < dims_[0]; i++) {
181 
182  newval[i] = (underneath_[i] > casted->typed_data()[i]) ? underneath_[i]
183  : casted->typed_data()[i];
184 
185  }
186 
187  payload::const_ptr ret = new Vector1Payload(newval, dims_[0], true, 1);
188 
189  free(newval);
190 
191  return ret;
192 
193  }
194 
195  /**
196  * Equals comparator
197  * @param other
198  * @return
199  */
200  virtual bool
201  equals(const payload::const_ptr &other) const {
202  const_ptr casted = ptr_safe_cast(const Vector1Payload, other);
203 
204  for (int i = 0; i < dims_[0]; i++) {
205  if (underneath_[i] != casted->typed_data()[i]) {
206  return false;
207  }
208  }
209 
210  return true;
211  }
212 
213  /**
214  * Logical or comparator
215  * @param other
216  * @return
217  */
218  virtual payload::const_ptr
219  logical_or(const payload::const_ptr &other) const {
220  const_ptr casted = ptr_safe_cast(const Vector1Payload, other);
221 
222  Type* newval = (Type*) malloc(sizeof(Type) * dims_[0]);
223 
224  for (int i = 0; i < dims_[0]; i++) {
225  newval[i] = underneath_[i] || casted->typed_data()[i];
226  }
227 
228  payload::const_ptr ret = new Vector1Payload(newval, dims_[0], true, 1);
229 
230  free(newval);
231 
232  return ret;
233  }
234 
235  /**
236  * Logical xor comparator
237  * @param other
238  * @return
239  */
240  virtual payload::const_ptr
241  logical_xor(const payload::const_ptr &other) const {
242  const_ptr casted = ptr_safe_cast(const Vector1Payload, other);
243 
244  Type* newval = (Type*) malloc(sizeof(Type) * dims_[0]);
245 
246  for (int i = 0; i < dims_[0]; i++) {
247  newval[i] = ((underneath_[i] || casted->typed_data()[i])
248  && !(underneath_[i] && casted->typed_data()[i]));
249  }
250 
251  payload::const_ptr ret = new Vector1Payload(newval, dims_[0], true, 1);
252 
253  free(newval);
254 
255  return ret;
256  }
257 
258  /**
259  * Logical and comparator
260  * @param other
261  * @return
262  */
263  virtual payload::const_ptr
264  logical_and(const payload::const_ptr &other) const {
265  const_ptr casted = ptr_safe_cast(const Vector1Payload, other);
266 
267  Type* newval = (Type*) malloc(sizeof(Type) * dims_[0]);
268 
269  for (int i = 0; i < dims_[0]; i++) {
270  newval[i] = underneath_[i] && casted->typed_data()[i];
271  }
272 
273  payload::const_ptr ret = new Vector1Payload(newval, dims_[0], true, 1);
274 
275  free(newval);
276 
277  return ret;
278  }
279 
280  /**
281  * Logical or comparator
282  * @param other
283  * @return
284  */
285  virtual payload::const_ptr
286  bitwise_or(const payload::const_ptr &other) const {
287  spkt_throw_printf(sprockit::spkt_error, "vector1_payload::bitwise| not implemented");
288  }
289 
290  /**
291  * Logical xor comparator
292  * @param other
293  * @return
294  */
295  virtual payload::const_ptr
296  bitwise_xor(const payload::const_ptr &other) const {
297  spkt_throw_printf(sprockit::spkt_error, "vector1_payload::bitwise^ not implemented");
298  }
299 
300  /**
301  * Logical and comparator
302  * @param other
303  * @return
304  */
305  virtual payload::const_ptr
306  bitwise_and(const payload::const_ptr &other) const {
307  spkt_throw_printf(sprockit::spkt_error, "vector1_payload::bitwise& not implemented");
308  }
309 
310  virtual void
312  spkt_throw_printf(sprockit::unimplemented_error, "vector1_payload::serialize");
313  }
314 
315  /**
316  * Strinfier
317  * @return a std::string description
318  */
319  virtual std::string
320  to_string() const {
321  std::stringstream ss;
322  ss << "vector1_payload("
323  << typeid(Type).name() << ", " << dims_[0]
324  << ")";
325  return ss.str();
326  }
327 
328  protected:
329  vector1_payload(const arraytype &inval, int dim1, ptrdiff_t stride) {
330  dims_.push_back(dim1);
331 
332  //do a deep copy
333  underneath_ = (Type*) malloc(sizeof(Type) * dim1);
334  for (int i = 0; i < dim1; i++) {
335  underneath_[i] = inval[i * stride];
336  }
337  }
338 
339  //Used by clone
340  vector1_payload(Type* inval, int dim1, bool cloning, ptrdiff_t stride) {
341  dims_.push_back(dim1);
342 
343  //do a deep copy
344  underneath_ = (Type*) malloc(sizeof(Type) * dim1);
345  for (int i = 0; i < dim1; i++) {
346  underneath_[i] = inval[i * stride];
347  }
348  }
349 
350  protected:
351  std::vector<int> dims_;
352 
353  Type* underneath_;
354 };
355 
356 } // end of namespace sstmac
357 #endif
358 
virtual payload::const_ptr bitwise_or(const payload::const_ptr &other) const
Logical or comparator.
virtual bool equals(const payload::const_ptr &other) const
Equals comparator.
virtual payload::const_ptr logical_or(const payload::const_ptr &other) const
Logical or comparator.
#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
void * data() const
Access the underlying data in a const context.
virtual payload::const_ptr prod(const payload::const_ptr &other) const
virtual std::string to_string() const
Strinfier.
virtual payload::const_ptr add(const payload::const_ptr &other) const
Add operator.
virtual void serialize_order(serializer &ser)
Network payload consisting of a boost multiarray.
virtual payload::const_ptr min(const payload::const_ptr &other) const
Less than comparator.
virtual payload::const_ptr max(const payload::const_ptr &other) const
Greater than comparator.
virtual payload::const_ptr logical_and(const payload::const_ptr &other) const
Logical and comparator.
#define ImplementSerializable(obj)
Definition: serializable.h:77
vector1_payload(const arraytype &inval, int dim1, ptrdiff_t stride)
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:33
void assign(void *data)
payload::const_ptr clone() const
Clone this object.
SUMI = Simulator unified messagine interface It is also the name for a solid ink in Japanese - i...
virtual payload::const_ptr bitwise_xor(const payload::const_ptr &other) const
Logical xor comparator.
vector1_payload(Type *inval, int dim1, bool cloning, ptrdiff_t stride)
#define spkt_throw_printf(exc, template_str,...)
Definition: errors.h:37
virtual long byte_length() const
General errors, or base class for more specific errors.
Definition: errors.h:54
virtual payload::const_ptr logical_xor(const payload::const_ptr &other) const
Logical xor comparator.
virtual payload::const_ptr bitwise_and(const payload::const_ptr &other) const
Logical and comparator.
A function was intentionally unimplemented because it doesn&#39;t make sense, or it is ongoing work...
Definition: errors.h:181
std::vector< int > dims_
const Type * typed_data() const
static const_ptr construct(const arraytype &inval, int dim1, ptrdiff_t stride=1)
vector1_payload< Type, VectorType > Vector1Payload