SST  6.0.0
StructuralSimulationToolkit
simpleNetwork.h
1 // -*- mode: c++ -*-
2 // Copyright 2009-2016 Sandia Corporation. Under the terms
3 // of Contract DE-AC04-94AL85000 with Sandia Corporation, the U.S.
4 // Government retains certain rights in this software.
5 //
6 // Copyright (c) 2009-2016, Sandia Corporation
7 // All rights reserved.
8 //
9 // This file is part of the SST software package. For license
10 // information, see the LICENSE file in the top level directory of the
11 // distribution.
12 //
13 
14 #ifndef CORE_INTERFACES_SIMPLENETWORK_H_
15 #define CORE_INTERFACES_SIMPLENETWORK_H_
16 
17 #include <string>
18 #include <unordered_map>
19 
20 #include <sst/core/sst_types.h>
21 #include <sst/core/subcomponent.h>
22 #include <sst/core/params.h>
23 
24 #include <sst/core/serialization/serializable.h>
25 
26 namespace SST {
27 
28 class Component;
29 class Event;
30 class Link;
31 
32 namespace Interfaces {
33 
34 
35 /**
36  * Generic network interface
37  */
38 class SimpleNetwork : public SubComponent {
39 
40 public:
41  /** All Addresses can be 64-bit */
42  typedef int64_t nid_t;
43 
44  static const nid_t INIT_BROADCAST_ADDR;
45 
46  /**
47  * Represents both network sends and receives
48  */
50 
51  public:
52  nid_t dest; /*!< Node ID of destination */
53  nid_t src; /*!< Node ID of source */
54  int vn; /*!< Virtual network of packet */
55  size_t size_in_bits; /*!< Size of packet in bits */
56  bool head; /*!< True if this is the head of a stream */
57  bool tail; /*!< True if this is the tail of a steram */
58 
59  private:
60  Event* payload; /*!< Payload of the request */
61 
62  public:
63 
64  /**
65  Sets the payload field for this request
66  @param payload_in Event to set as payload.
67  */
68  inline void givePayload(Event *event) {
69  payload = event;
70  }
71 
72  /**
73  Returns the payload for the request. This will also set
74  the payload to NULL, so the call will only return valid
75  data one time after each givePayload call.
76  @return Event that was set as payload of the request.
77  */
78  inline Event* takePayload() {
79  Event* ret = payload;
80  payload = NULL;
81  return ret;
82  }
83 
84  /**
85  Returns the payload for the request for inspection. This
86  call does not set the payload to NULL, so deleting the
87  reqeust will also delete the payload. If the request is
88  going to be deleted, use takePayload instead.
89  @return Event that was set as payload of the request.
90  */
91  inline Event* inspectPayload() {
92  return payload;
93  }
94 
95  /**
96  * Trace types
97  */
98  typedef enum {
99  NONE, /*!< No tracing enabled */
100  ROUTE, /*!< Trace route information only */
101  FULL /*!< Trace all movements of packets through network */
102  } TraceType;
103 
104 
105  /** Constructor */
107  dest(0), src(0), size_in_bits(0), head(false), tail(false), payload(NULL),
108  trace(NONE), traceID(0)
109  {}
110 
111  Request(nid_t dest, nid_t src, size_t size_in_bits,
112  bool head, bool tail, Event* payload = NULL) :
113  dest(dest), src(src), size_in_bits(size_in_bits), head(head), tail(tail), payload(payload),
114  trace(NONE), traceID(0)
115  {
116  }
117 
118  virtual ~Request()
119  {
120  if ( payload != NULL ) delete payload;
121  }
122 
123  inline Request* clone() {
124  Request* req = new Request(*this);
125  // Copy constructor only makes a shallow copy, need to
126  // clone the event.
127  if ( payload != NULL ) req->payload = payload->clone();
128  return req;
129  }
130 
131  void setTraceID(int id) {traceID = id;}
132  void setTraceType(TraceType type) {trace = type;}
133  int getTraceID() {return traceID;}
134  TraceType getTraceType() {return trace;}
135 
136  void serialize_order(SST::Core::Serialization::serializer &ser) {
137  ser & dest;
138  ser & src;
139  ser & vn;
140  ser & size_in_bits;
141  ser & head;
142  ser & tail;
143  ser & payload;
144  ser & trace;
145  ser & traceID;
146  }
147 
148  protected:
149  TraceType trace;
150  int traceID;
151 
152  private:
153 
154  ImplementSerializable(SST::Interfaces::SimpleNetwork::Request)
155  };
156  /**
157  Class used to inspect network requests going through the network.
158  */
160 
161  public:
162  NetworkInspector(Component* parent) :
163  SubComponent(parent)
164  {}
165 
166  virtual ~NetworkInspector() {}
167 
168  virtual void inspectNetworkData(Request* req) = 0;
169 
170  /**
171  * The ID uniquely identifies the component in which this
172  * subcomponent is instantiated. It does not uniquely define
173  * this particular NetworkInspector, and all NetworkInspectors
174  * instantiated in the smae component will get the same ID. If
175  * registering statistics, the ID is intended to be used as the
176  * subfield of the statistic.
177  */
178  virtual void initialize(std::string id) = 0;
179  };
180 
181  /** Functor classes for handling of callbacks */
182  class HandlerBase {
183  public:
184  virtual bool operator()(int) = 0;
185  virtual ~HandlerBase() {}
186  };
187 
188 
189  /** Event Handler class with user-data argument
190  * @tparam classT Type of the Object
191  * @tparam argT Type of the argument
192  */
193  template <typename classT, typename argT = void>
194  class Handler : public HandlerBase {
195  private:
196  typedef bool (classT::*PtrMember)(int, argT);
197  classT* object;
198  const PtrMember member;
199  argT data;
200 
201  public:
202  /** Constructor
203  * @param object - Pointer to Object upon which to call the handler
204  * @param member - Member function to call as the handler
205  * @param data - Additional argument to pass to handler
206  */
207  Handler( classT* const object, PtrMember member, argT data ) :
208  object(object),
209  member(member),
210  data(data)
211  {}
212 
213  bool operator()(int vn) {
214  return (object->*member)(vn,data);
215  }
216  };
217 
218  /** Event Handler class without user-data
219  * @tparam classT Type of the Object
220  */
221  template <typename classT>
222  class Handler<classT, void> : public HandlerBase {
223  private:
224  typedef bool (classT::*PtrMember)(int);
225  classT* object;
226  const PtrMember member;
227 
228  public:
229  /** Constructor
230  * @param object - Pointer to Object upon which to call the handler
231  * @param member - Member function to call as the handler
232  */
233  Handler( classT* const object, PtrMember member ) :
234  object(object),
235  member(member)
236  {}
237 
238  bool operator()(int vn) {
239  return (object->*member)(vn);
240  }
241  };
242 
243 public:
244 
245  /** Constructor, designed to be used via 'loadSubComponent'. */
247  SubComponent(comp)
248  { }
249 
250  /** Second half of building the interface.
251  Initialize network interface
252  @param portName - Name of port to connect to
253  @param link_bw - Bandwidth of the link
254  @param vns - Number of virtual networks to be provided
255  @param in_buf_size - Size of input buffers (from router)
256  @param out_buf_size - Size of output buffers (to router)
257  * @return true if the link was able to be configured.
258  */
259  virtual bool initialize(const std::string &portName, const UnitAlgebra& link_bw,
260  int vns, const UnitAlgebra& in_buf_size,
261  const UnitAlgebra& out_buf_size) = 0;
262 
263  /**
264  * Sends a network request during the init() phase
265  */
266  virtual void sendInitData(Request *req) = 0;
267 
268  /**
269  * Receive any data during the init() phase.
270  * @see SST::Link::recvInitData()
271  */
272  virtual Request* recvInitData() = 0;
273 
274  // /**
275  // * Returns a handle to the underlying SST::Link
276  // */
277  // virtual Link* getLink(void) const = 0;
278 
279  /**
280  * Send a Request to the network.
281  */
282  virtual bool send(Request *req, int vn) = 0;
283 
284  /**
285  * Receive a Request from the network.
286  *
287  * Use this method for polling-based applications.
288  * Register a handler for push-based notification of responses.
289  *
290  * @param vn Virtual network to receive on
291  * @return NULL if nothing is available.
292  * @return Pointer to a Request response (that should be deleted)
293  */
294  virtual Request* recv(int vn) = 0;
295 
296  virtual void setup() {}
297  virtual void init(unsigned int phase) {}
298  virtual void finish() {}
299 
300  /**
301  * Checks if there is sufficient space to send on the specified
302  * virtual netork
303  * @param vn Virtual network to check
304  * @param num_bits Minimum size in bits required to have space
305  * to send
306  * @return true if there is space in the output, false otherwise
307  */
308  virtual bool spaceToSend(int vn, int num_bits) = 0;
309 
310 
311  /**
312  * Checks if there is a waiting network request request pending in
313  * the specified virtual network.
314  * @param vn Virtual network to check
315  * @return true if a network request is pending in the specified
316  * virtual network, false otherwise
317  */
318  virtual bool requestToReceive( int vn ) = 0;
319 
320  /**
321  * Registers a functor which will fire when a new request is
322  * received from the network. Note, the actual request that
323  * was received is not passed into the functor, it is only a
324  * notification that something is available.
325  * @param functor Functor to call when request is received
326  */
327  virtual void setNotifyOnReceive(HandlerBase* functor) = 0;
328  /**
329  * Registers a functor which will fire when a request is
330  * sent to the network. Note, this only tells you when data
331  * is sent, it does not guarentee any specified amount of
332  * available space.
333  * @param functor Functor to call when request is sent
334  */
335  virtual void setNotifyOnSend(HandlerBase* functor) = 0;
336 
337  /**
338  * Check to see if network is initialized. If network is not
339  * initialized, then no other functions other than init() can
340  * can be called on the interface.
341  * @return true if network is initialized, false otherwise
342  */
343  virtual bool isNetworkInitialized() const = 0;
344 
345  /**
346  * Returns the endpoint ID. Cannot be called until after the
347  * network is initialized.
348  * @return Endpoint ID
349  */
350  virtual nid_t getEndpointID() const = 0;
351 
352  /**
353  * Returns the final BW of the link managed by the simpleNetwork
354  * instance. Cannot be called until after the network is
355  * initialized.
356  * @return Link bandwidth of associated link
357  */
358  virtual const UnitAlgebra& getLinkBW() const = 0;
359 
360 
361 };
362 
363 }
364 }
365 
366 #endif
Definition: simpleNetwork.h:100
nid_t src
Definition: simpleNetwork.h:53
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:35
Event * inspectPayload()
Returns the payload for the request for inspection.
Definition: simpleNetwork.h:91
Definition: simpleNetwork.h:101
void givePayload(Event *event)
Sets the payload field for this request.
Definition: simpleNetwork.h:68
virtual Request * recv(int vn)=0
Receive a Request from the network.
virtual bool initialize(const std::string &portName, const UnitAlgebra &link_bw, int vns, const UnitAlgebra &in_buf_size, const UnitAlgebra &out_buf_size)=0
Second half of building the interface.
virtual void sendInitData(Request *req)=0
Sends a network request during the init() phase.
Main component object for the simulation.
Definition: component.h:56
Handler(classT *const object, PtrMember member, argT data)
Constructor.
Definition: simpleNetwork.h:207
bool tail
Definition: simpleNetwork.h:57
virtual void setup()
Called after all components have been constructed and inialization has completed, but before simulati...
Definition: simpleNetwork.h:296
Definition: action.cc:17
virtual void setNotifyOnReceive(HandlerBase *functor)=0
Registers a functor which will fire when a new request is received from the network.
Request()
Constructor.
Definition: simpleNetwork.h:106
nid_t dest
Definition: simpleNetwork.h:52
Functor classes for handling of callbacks.
Definition: simpleNetwork.h:182
Definition: serializable.h:108
virtual Request * recvInitData()=0
Receive any data during the init() phase.
SimpleNetwork(SST::Component *comp)
Constructor, designed to be used via 'loadSubComponent'.
Definition: simpleNetwork.h:246
Handler(classT *const object, PtrMember member)
Constructor.
Definition: simpleNetwork.h:233
virtual void finish()
Called after simulation completes, but before objects are destroyed.
Definition: simpleNetwork.h:298
Definition: simpleNetwork.h:99
Represents both network sends and receives.
Definition: simpleNetwork.h:49
virtual bool send(Request *req, int vn)=0
Returns a handle to the underlying SST::Link.
virtual bool isNetworkInitialized() const =0
Check to see if network is initialized.
TraceType
Trace types.
Definition: simpleNetwork.h:98
Event Handler class with user-data argument.
Definition: simpleNetwork.h:194
bool head
Definition: simpleNetwork.h:56
virtual bool requestToReceive(int vn)=0
Checks if there is a waiting network request request pending in the specified virtual network...
virtual const UnitAlgebra & getLinkBW() const =0
Returns the final BW of the link managed by the simpleNetwork instance.
virtual bool spaceToSend(int vn, int num_bits)=0
Checks if there is sufficient space to send on the specified virtual netork.
size_t size_in_bits
Definition: simpleNetwork.h:55
virtual nid_t getEndpointID() const =0
Returns the endpoint ID.
Event * takePayload()
Returns the payload for the request.
Definition: simpleNetwork.h:78
int64_t nid_t
All Addresses can be 64-bit.
Definition: simpleNetwork.h:42
virtual void initialize(std::string id)=0
The ID uniquely identifies the component in which this subcomponent is instantiated.
Generic network interface.
Definition: simpleNetwork.h:38
virtual void init(unsigned int phase)
Used during the init phase.
Definition: simpleNetwork.h:297
Base class for Events - Items sent across links to communicate between components.
Definition: event.h:31
Performs Unit math in full precision.
Definition: unitAlgebra.h:112
virtual void setNotifyOnSend(HandlerBase *functor)=0
Registers a functor which will fire when a request is sent to the network.
Class used to inspect network requests going through the network.
Definition: simpleNetwork.h:159
SubComponent is a class loadable through the factory which allows dynamic functionality to be added t...
Definition: subcomponent.h:27
virtual Event * clone()
Clones the event in for the case of a broadcast.
Definition: event.cc:33
int vn
Definition: simpleNetwork.h:54
Definition: serializable.h:128