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