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