SST  13.1.0
Structural Simulation Toolkit
simpleNetwork.h
1 // -*- mode: c++ -*-
2 // Copyright 2009-2023 NTESS. Under the terms
3 // of Contract DE-NA0003525 with NTESS, the U.S.
4 // Government retains certain rights in this software.
5 //
6 // Copyright (c) 2009-2023, NTESS
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 SST_CORE_INTERFACES_SIMPLENETWORK_H
15 #define SST_CORE_INTERFACES_SIMPLENETWORK_H
16 
17 #include "sst/core/params.h"
18 #include "sst/core/serialization/serializable.h"
19 #include "sst/core/sst_types.h"
20 #include "sst/core/ssthandler.h"
21 #include "sst/core/subcomponent.h"
22 #include "sst/core/warnmacros.h"
23 
24 #include <string>
25 #include <unordered_map>
26 
27 namespace SST {
28 
29 class Component;
30 class Event;
31 class Link;
32 
33 namespace Interfaces {
34 
35 /**
36  * Generic network interface
37  */
39 {
40 
41  // Variables needed until send/recvInitData() functions are
42  // removed in SST 14
43  bool delegate_send = false;
44  bool delegate_recv = false;
45 
46 public:
47  SST_ELI_REGISTER_SUBCOMPONENT_API(SST::Interfaces::SimpleNetwork,int)
48 
49  /** All Addresses can be 64-bit */
50  typedef int64_t nid_t;
51 #define PRI_NID PRIi64
52 
53  static const nid_t INIT_BROADCAST_ADDR;
54 
55  /**
56  * Represents both network sends and receives
57  */
59  {
60 
61  public:
62  nid_t dest; /*!< Node ID of destination */
63  nid_t src; /*!< Node ID of source */
64  int vn; /*!< Virtual network of packet */
65  size_t size_in_bits; /*!< Size of packet in bits */
66  bool head; /*!< True if this is the head of a stream */
67  bool tail; /*!< True if this is the tail of a steram */
68  bool allow_adaptive; /*!< Indicates whether adaptive routing is allowed or not. */
69 
70  private:
71  Event* payload; /*!< Payload of the request */
72 
73  public:
74  /**
75  Sets the payload field for this request
76  @param payload_in Event to set as payload.
77  */
78  inline void givePayload(Event* event) { payload = event; }
79 
80  /**
81  Returns the payload for the request. This will also set
82  the payload to nullptr, so the call will only return valid
83  data one time after each givePayload call.
84  @return Event that was set as payload of the request.
85  */
86  inline Event* takePayload()
87  {
88  Event* ret = payload;
89  payload = nullptr;
90  return ret;
91  }
92 
93  /**
94  Returns the payload for the request for inspection. This
95  call does not set the payload to nullptr, so deleting the
96  request will also delete the payload. If the request is
97  going to be deleted, use takePayload instead.
98  @return Event that was set as payload of the request.
99  */
100  inline Event* inspectPayload() { return payload; }
101 
102  /**
103  * Trace types
104  */
105  typedef enum {
106  NONE, /*!< No tracing enabled */
107  ROUTE, /*!< Trace route information only */
108  FULL /*!< Trace all movements of packets through network */
110 
111  /** Constructor */
113  dest(0),
114  src(0),
115  size_in_bits(0),
116  head(false),
117  tail(false),
118  allow_adaptive(true),
119  payload(nullptr),
120  trace(NONE),
121  traceID(0)
122  {}
123 
124  Request(nid_t dest, nid_t src, size_t size_in_bits, bool head, bool tail, Event* payload = nullptr) :
125  dest(dest),
126  src(src),
128  head(head),
129  tail(tail),
130  allow_adaptive(true),
131  payload(payload),
132  trace(NONE),
133  traceID(0)
134  {}
135 
136  virtual ~Request()
137  {
138  if ( payload != nullptr ) delete payload;
139  }
140 
141  inline Request* clone()
142  {
143  Request* req = new Request(*this);
144  // Copy constructor only makes a shallow copy, need to
145  // clone the event.
146  if ( payload != nullptr ) req->payload = payload->clone();
147  return req;
148  }
149 
150  void setTraceID(int id) { traceID = id; }
151  void setTraceType(TraceType type) { trace = type; }
152  int getTraceID() { return traceID; }
153  TraceType getTraceType() { return trace; }
154 
155  void serialize_order(SST::Core::Serialization::serializer& ser) override
156  {
157  ser& dest;
158  ser& src;
159  ser& vn;
160  ser& size_in_bits;
161  ser& head;
162  ser& tail;
163  ser& payload;
164  ser& trace;
165  ser& traceID;
166  ser& allow_adaptive;
167  }
168 
169  protected:
170  TraceType trace;
171  int traceID;
172 
173  private:
174  ImplementSerializable(SST::Interfaces::SimpleNetwork::Request)
175  };
176  /**
177  Class used to inspect network requests going through the network.
178  */
180  {
181 
182  public:
183  SST_ELI_REGISTER_SUBCOMPONENT_API(SST::Interfaces::SimpleNetwork::NetworkInspector,std::string)
184 
185  NetworkInspector(ComponentId_t id) : SubComponent(id) {}
186 
187  virtual ~NetworkInspector() {}
188 
189  virtual void inspectNetworkData(Request* req) = 0;
190  };
191 
192  /**
193  Base handler for event delivery.
194  */
196 
197  /**
198  Used to create handlers to notify the endpoint when the
199  SimpleNetwork sends or recieves a packet.. The callback
200  function is expected to be in the form of:
201 
202  bool func(int vn)
203 
204  In which case, the class is created with:
205 
206  new SimpleNetwork::Handler<classname>(this, &classname::function_name)
207 
208  Or, to add static data, the callback function is:
209 
210  bool func(int vn, dataT data)
211 
212  and the class is created with:
213 
214  new SimpleNetwork::Handler<classname, dataT>(this, &classname::function_name, data)
215 
216  In both cases, the boolean that's returned indicates whether
217  the handler should be kept in the list or not. On return
218  of true, the handler will be kept. On return of false, the
219  handler will be removed from the clock list.
220  */
221  template <typename classT, typename dataT = void>
223 
224 
225 public:
226  /** Constructor, designed to be used via 'loadUserSubComponent or loadAnonymousSubComponent'. */
227  SimpleNetwork(SST::ComponentId_t id) : SubComponent(id) {}
228 
229  /**
230  * Sends a network request during the init() phase
231  */
232 #if !SST_BUILDING_CORE
233  [[deprecated(
234  "sendInitData() has been deprecated and will be removed in SST 14. Please use sendUntimedData() instead.")]]
235 #endif
236  virtual void
237  sendInitData(Request* req);
238 
239  /**
240  * Receive any data during the init() phase.
241  * @see SST::Link::recvInitData()
242  */
243 #if !SST_BUILDING_CORE
244  [[deprecated(
245  "recvInitData() has been deprecated and will be removed in SST 14. Please use recvUntimedData() instead.")]]
246 #endif
247  virtual Request*
248  recvInitData();
249 
250  /**
251  * Sends a network request during untimed phases (init() and
252  * complete()).
253  * @see SST::Link::sendUntimedData()
254  *
255  * For now, simply call sendInitData. Once that call is
256  * deprecated and removed, this will become a pure virtual
257  * function. This means that when classes implement
258  * SimpleNetwork, they will need to overload both sendUntimedData
259  * and sendInitData with identical functionality (or having the
260  * Init version call the Untimed version) until sendInitData is
261  * removed in SST 14.0.
262  */
263  virtual void sendUntimedData(Request* req);
264 
265  /**
266  * Receive any data during untimed phases (init() and complete()).
267  * @see SST::Link::recvUntimedData()
268  *
269  * For now, simply call recvInitData. Once that call is
270  * deprecated and removed, this will become a pure virtual
271  * function. This means that when classes implement
272  * SimpleNetwork, they will need to overload both recvUntimedData
273  * and recvInitData with identical functionality (or having the
274  * Init version call the Untimed version) until recvInitData is
275  * removed in SST 14.0.
276  */
277  virtual Request* recvUntimedData();
278 
279  // /**
280  // * Returns a handle to the underlying SST::Link
281  // */
282  // virtual Link* getLink(void) const = 0;
283 
284  /**
285  * Send a Request to the network.
286  */
287  virtual bool send(Request* req, int vn) = 0;
288 
289  /**
290  * Receive a Request from the network.
291  *
292  * Use this method for polling-based applications.
293  * Register a handler for push-based notification of responses.
294  *
295  * @param vn Virtual network to receive on
296  * @return nullptr if nothing is available.
297  * @return Pointer to a Request response (that should be deleted)
298  */
299  virtual Request* recv(int vn) = 0;
300 
301  virtual void setup() override {}
302  virtual void init(unsigned int UNUSED(phase)) override {}
303  virtual void complete(unsigned int UNUSED(phase)) override {}
304  virtual void finish() override {}
305 
306  /**
307  * Checks if there is sufficient space to send on the specified
308  * virtual network
309  * @param vn Virtual network to check
310  * @param num_bits Minimum size in bits required to have space
311  * to send
312  * @return true if there is space in the output, false otherwise
313  */
314  virtual bool spaceToSend(int vn, int num_bits) = 0;
315 
316  /**
317  * Checks if there is a waiting network request request pending in
318  * the specified virtual network.
319  * @param vn Virtual network to check
320  * @return true if a network request is pending in the specified
321  * virtual network, false otherwise
322  */
323  virtual bool requestToReceive(int vn) = 0;
324 
325  /**
326  * Registers a functor which will fire when a new request is
327  * received from the network. Note, the actual request that
328  * was received is not passed into the functor, it is only a
329  * notification that something is available.
330  * @param functor Functor to call when request is received
331  */
332  virtual void setNotifyOnReceive(HandlerBase* functor) = 0;
333  /**
334  * Registers a functor which will fire when a request is
335  * sent to the network. Note, this only tells you when data
336  * is sent, it does not guarantee any specified amount of
337  * available space.
338  * @param functor Functor to call when request is sent
339  */
340  virtual void setNotifyOnSend(HandlerBase* functor) = 0;
341 
342  /**
343  * Check to see if network is initialized. If network is not
344  * initialized, then no other functions other than init() can
345  * can be called on the interface.
346  * @return true if network is initialized, false otherwise
347  */
348  virtual bool isNetworkInitialized() const = 0;
349 
350  /**
351  * Returns the endpoint ID. Cannot be called until after the
352  * network is initialized.
353  * @return Endpoint ID
354  */
355  virtual nid_t getEndpointID() const = 0;
356 
357  /**
358  * Returns the final BW of the link managed by the simpleNetwork
359  * instance. Cannot be called until after the network is
360  * initialized.
361  * @return Link bandwidth of associated link
362  */
363  virtual const UnitAlgebra& getLinkBW() const = 0;
364 };
365 
366 } // namespace Interfaces
367 } // namespace SST
368 
369 #endif // SST_CORE_INTERFACES_SIMPLENETWORK_H
Definition: serializable.h:139
Definition: serializable.h:119
This class is basically a wrapper for objects to declare the order in which their members should be s...
Definition: serializer.h:35
Base class for Events - Items sent across links to communicate between components.
Definition: event.h:35
virtual Event * clone()
Clones the event in for the case of a broadcast.
Definition: event.cc:35
Class used to inspect network requests going through the network.
Definition: simpleNetwork.h:180
Represents both network sends and receives.
Definition: simpleNetwork.h:59
int vn
Definition: simpleNetwork.h:64
Event * inspectPayload()
Returns the payload for the request for inspection.
Definition: simpleNetwork.h:100
bool head
Definition: simpleNetwork.h:66
bool allow_adaptive
Definition: simpleNetwork.h:68
size_t size_in_bits
Definition: simpleNetwork.h:65
TraceType
Trace types.
Definition: simpleNetwork.h:105
@ ROUTE
Definition: simpleNetwork.h:107
@ NONE
Definition: simpleNetwork.h:106
@ FULL
Definition: simpleNetwork.h:108
nid_t dest
Definition: simpleNetwork.h:62
Request()
Constructor.
Definition: simpleNetwork.h:112
nid_t src
Definition: simpleNetwork.h:63
bool tail
Definition: simpleNetwork.h:67
void givePayload(Event *event)
Sets the payload field for this request.
Definition: simpleNetwork.h:78
Event * takePayload()
Returns the payload for the request.
Definition: simpleNetwork.h:86
Generic network interface.
Definition: simpleNetwork.h:39
virtual void setNotifyOnReceive(HandlerBase *functor)=0
Registers a functor which will fire when a new request is received from the network.
virtual void setNotifyOnSend(HandlerBase *functor)=0
Registers a functor which will fire when a request is sent to the network.
virtual Request * recv(int vn)=0
Receive a Request from the network.
int64_t nid_t
All Addresses can be 64-bit.
Definition: simpleNetwork.h:50
virtual bool isNetworkInitialized() const =0
Check to see if network is initialized.
SimpleNetwork(SST::ComponentId_t id)
Constructor, designed to be used via 'loadUserSubComponent or loadAnonymousSubComponent'.
Definition: simpleNetwork.h:227
virtual void complete(unsigned int UNUSED(phase)) override
Used during the complete phase after the end of simulation.
Definition: simpleNetwork.h:303
virtual Request * recvInitData()
Receive any data during the init() phase.
Definition: simpleNetwork.cc:69
virtual bool send(Request *req, int vn)=0
Send a Request to the network.
virtual void sendInitData(Request *req)
Sends a network request during the init() phase.
Definition: simpleNetwork.cc:55
virtual void sendUntimedData(Request *req)
Sends a network request during untimed phases (init() and complete()).
Definition: simpleNetwork.cc:26
virtual void finish() override
Called after simulation completes, but before objects are destroyed.
Definition: simpleNetwork.h:304
virtual const UnitAlgebra & getLinkBW() const =0
Returns the final BW of the link managed by the simpleNetwork instance.
virtual void init(unsigned int UNUSED(phase)) override
Used during the init phase.
Definition: simpleNetwork.h:302
virtual bool spaceToSend(int vn, int num_bits)=0
Checks if there is sufficient space to send on the specified virtual network.
virtual Request * recvUntimedData()
Receive any data during untimed phases (init() and complete()).
Definition: simpleNetwork.cc:40
virtual void setup() override
Called after all components have been constructed and initialization has completed,...
Definition: simpleNetwork.h:301
virtual nid_t getEndpointID() const =0
Returns the endpoint ID.
virtual bool requestToReceive(int vn)=0
Checks if there is a waiting network request request pending in the specified virtual network.
Handlers with 1 handler defined argument to callback from caller.
Definition: ssthandler.h:171
Handler class with user-data argument.
Definition: ssthandler.h:220
SubComponent is a class loadable through the factory which allows dynamic functionality to be added t...
Definition: subcomponent.h:29
Performs Unit math in full precision.
Definition: unitAlgebra.h:109