SST 15.0
Structural Simulation Toolkit
portModule.h
1// Copyright 2009-2025 NTESS. Under the terms
2// of Contract DE-NA0003525 with NTESS, the U.S.
3// Government retains certain rights in this software.
4//
5// Copyright (c) 2009-2025, NTESS
6// All rights reserved.
7//
8// This file is part of the SST software package. For license
9// information, see the LICENSE file in the top level directory of the
10// distribution.
11
12#ifndef SST_CORE_PORTMODULE_H
13#define SST_CORE_PORTMODULE_H
14
15#include "sst/core/eli/elementinfo.h"
16#include "sst/core/event.h"
17#include "sst/core/link.h"
18#include "sst/core/serialization/serializable.h"
19
20namespace SST {
21
22class BaseComponent;
23
24/**
25 PortModules are modules that can be attached to the send and/or
26 receive side of ports. Each PortModule is attached to one port and
27 uses the Event::HandlerBase::InterceptPoint for intercepting
28 incoming events and the Link::AttachPoint to intercept outgoing
29 events. The intercepted events can be inspected, modified and/or
30 canceled. For canceled events, the PortModule is required to delete
31 the event.
32
33 NOTE: Not Final API - PortModules will continue to be supported in
34 the future, but the API will not be finalized until the SST 15
35 release, so there may be slight changes to the base class.
36
37 NOTE: Attaching to a port on the send-side has known performance
38 issues, so it is recommended to attach to the input port whenever
39 possible.
40
41 */
43 public Event::HandlerBase::InterceptPoint,
44 public Link::AttachPoint,
46{
47
48public:
49 SST_ELI_DECLARE_BASE(PortModule)
50 // declare extern to limit compile times
51 SST_ELI_DECLARE_CTOR_EXTERN(SST::Params&)
52 // These categories will print in sst-info in the order they are
53 // listed here
54 SST_ELI_DECLARE_INFO_EXTERN(
56 // ELI::ProvidesStats, // Will add stats in the future
58
59 PortModule() = default;
60 virtual ~PortModule() {};
61
62 /******* Functions inherited from Link::AttachPoint *******/
63 /**
64 Function that will be called when a PortModule is registered on
65 sends (i.e. installOnSend() returns true). The uintptr_t
66 returned from this function will be passed into the eventSent()
67 function.
68
69 The default implemenation will just return 0 and only needs to
70 be overwritten if the module needs any of the metadata and/or
71 needs to return a unique key.
72
73 @param mdata Metadata to be passed into the tool. NOTE: The
74 metadata will be of type EventHandlerMetaData (see
75 sst/core/event.h)
76
77 @return Opaque key that will be passed back into eventSent() to
78 identify the source of the call
79 */
80 virtual uintptr_t registerLinkAttachTool(const AttachPointMetaData& mdata) override;
81
82 /**
83 Function that will be called when an event is sent on a
84 link with registered PortModules. If ev is set to
85 nullptr, then the event will not be delivered and the function
86 should delete the original event.
87
88 NOTE: It is possible to delete the incoming event and replace
89 it with a new event, if this is done, the new event MUST call
90 copyAllDeliveryInfo(ev) or the event will not be properly
91 processed.
92
93 @param key Opaque key returned from registerLinkAttachTool()
94
95 @param ev Event to intercept
96 */
97 virtual void eventSent(uintptr_t key, Event*& ev) override = 0;
98
99
100 /**
101 Function that will be called to handle the key returned from
102 registerLinkAttachTool, if the AttachPoint tool is
103 serializable. This is needed because the key is opaque to the
104 Link, so it doesn't know how to handle it during serialization.
105 During SIZE and PACK phases of serialization, the tool needs to
106 store out any information that will be needed to recreate data
107 that is reliant on the key. On UNPACK, the function needs to
108 recreate the any state and reinitialize the passed in key
109 reference to the proper state to continue to make valid calls
110 to eventSent().
111
112 The default implemenation will just set key to 0 on UNPACK and
113 only needs to be overwritten if the module needs to return a
114 unique key from registerLinkAttachTool().
115
116 @param ser Serializer to use for serialization
117
118 @param key Key that would be passed into the eventSent() function.
119 */
120 virtual void serializeEventAttachPointKey(SST::Core::Serialization::serializer& ser, uintptr_t& key) override;
121
122
123 /******* Functions inherited from Event::HandlerBase::InterceptPoint *******/
124 /**
125 Function that will be called when a handler is registered with
126 recieves (i.e. installOnReceive() returns true). The uintptr_t
127 returned from this function will be passed into the intercept()
128 function.
129
130 The default implemenation will just return 0 and only needs to
131 be overwritten if the module needs any of the metadata and/or
132 needs to return a unique key.
133
134 @param mdata Metadata to be passed into the tool. NOTE: The
135 metadata will be of type EventHandlerMetaData (see
136 sst/core/event.h)
137
138 @return Opaque key that will be passed back into the
139 interceptHandler() calls
140 */
141 virtual uintptr_t registerHandlerIntercept(const AttachPointMetaData& mdata) override;
142
143
144 /**
145 Function that will be called before the event handler to let
146 the attach point intercept the data. The data can be modified,
147 and if cancel is set to true, the handler will not be executed.
148 If cancel is set to true, then the function should also delete
149 the event and set data to nullptr.
150
151 NOTE: It is possible to delete the incoming event and replace
152 the it with a new event, if this is done, the new event MUST
153 call copyAllDeliveryInfo(ev) or the event will not be properly
154 processed.
155
156 @param key Key returned from registerHandlerIntercept()
157 function
158
159 @param data Event that is to be passed to the handler
160
161 @param[out] cancel Set to true if the handler delivery should
162 be cancelled. If set to true, function must also delete the
163 event
164 */
165 virtual void interceptHandler(uintptr_t key, Event*& data, bool& cancel) override = 0;
166
167 /**
168 Function that will be called to handle the key returned from
169 registerHandlerIntercept, if the AttachPoint tool is
170 serializable. This is needed because the key is opaque to the
171 Link, so it doesn't know how to handle it during serialization.
172 During SIZE and PACK phases of serialization, the tool needs to
173 store out any information that will be needed to recreate data
174 that is reliant on the key. On UNPACK, the function needs to
175 recreate any state and reinitialize the passed in key reference
176 to the proper state to continue to make valid calls to
177 interceptHandler().
178
179 The default implemenation will just set key to 0 on UNPACK and
180 only needs to be overwritten if the module needs to return a
181 unique key from registerHandlerIntercept().
182
183 @param ser Serializer to use for serialization
184
185 @param key Key that would be passed into the interceptHandler() function.
186 */
187 virtual void serializeHandlerInterceptPointKey(SST::Core::Serialization::serializer& ser, uintptr_t& key) override;
188
189 /*** Functions that control whether module is install on send and/or receive ***/
190 /**
191 Called to determine if the PortModule should be installed on
192 receives
193
194 @return true if PortModule should be installed on receives
195 */
196 virtual bool installOnReceive() { return false; }
197
198 /**
199 Called to determine if the PortModule should be installed on
200 sends. NOTE: Installing PortModules on sends will have a
201 noticeable impact on performance, consider architecting things
202 so that you can intercept on receives.
203
204 @return true if PortModule should be installed on sends
205 */
206 virtual bool installOnSend() { return false; }
207
208protected:
209 void serialize_order(SST::Core::Serialization::serializer& ser) override;
210
211 /*** Core API functions available to PortModules ***/
212
213 /** Get the core timebase */
215
216 /** Return the current simulation time as a cycle count*/
217 SimTime_t getCurrentSimCycle() const;
218
219 /** Return the current priority */
220 int getCurrentPriority() const;
221
222 /** Return the elapsed simulation time as a time */
224
225 /** Return the base simulation Output class instance */
227
228 /**
229 Return the simulated time since the simulation began in units
230 specified by the parameter.
231
232 @param tc TimeConverter specifying the units
233 */
234 [[deprecated("Use of shared TimeConverter objects is deprecated. Use 'getCurrentSimTime(TimeConverter& timebase)' "
235 "(i.e., no pointer) instead.")]]
236 SimTime_t getCurrentSimTime(TimeConverter* tc) const;
237 SimTime_t getCurrentSimTime(TimeConverter& tc) const;
238
239 /**
240 Return the simulated time since the simulation began in
241 timebase specified
242
243 @param base Timebase frequency in SI Units
244 */
245 SimTime_t getCurrentSimTime(const std::string& base) const;
246
247 /**
248 Utility function to return the time since the simulation began
249 in nanoseconds
250 */
251 SimTime_t getCurrentSimTimeNano() const;
252
253 /**
254 Utility function to return the time since the simulation began
255 in microseconds
256 */
257 SimTime_t getCurrentSimTimeMicro() const;
258
259 /**
260 Utility function to return the time since the simulation began
261 in milliseconds
262 */
263 SimTime_t getCurrentSimTimeMilli() const;
264
265private:
266 friend class BaseComponent;
267
268 /**
269 Component that owns this PortModule
270 */
271 BaseComponent* component_;
272
273 /**
274 Set the component that owns this PortModule
275 */
276 void setComponent(SST::BaseComponent* comp);
277};
278
279} // namespace SST
280
281// Macro used to register PortModules in the ELI database
282#define SST_ELI_REGISTER_PORTMODULE(cls, lib, name, version, desc) \
283 SST_ELI_REGISTER_DERIVED(SST::PortModule,cls,lib,name,ELI_FORWARD_AS_ONE(version),desc)
284
285#endif // SST_CORE_PORTMODULE_H
Main component object for the simulation.
Definition baseComponent.h:62
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
Definition attributeInfo.h:40
Definition paramsInfo.h:39
Base class for Events - Items sent across links to communicate between components.
Definition event.h:35
Output object provides consistent method for outputting data to stdout, stderr and/or sst debug file.
Definition output.h:54
Parameter store.
Definition params.h:58
PortModules are modules that can be attached to the send and/or receive side of ports.
Definition portModule.h:46
virtual uintptr_t registerHandlerIntercept(const AttachPointMetaData &mdata) override
Function that will be called when a handler is registered with recieves (i.e.
Definition portModule.cc:39
virtual bool installOnReceive()
Called to determine if the PortModule should be installed on receives.
Definition portModule.h:196
virtual bool installOnSend()
Called to determine if the PortModule should be installed on sends.
Definition portModule.h:206
virtual void serializeHandlerInterceptPointKey(SST::Core::Serialization::serializer &ser, uintptr_t &key) override
Function that will be called to handle the key returned from registerHandlerIntercept,...
Definition portModule.cc:45
virtual void interceptHandler(uintptr_t key, Event *&data, bool &cancel) override=0
Function that will be called before the event handler to let the attach point intercept the data.
SimTime_t getCurrentSimTimeMicro() const
Utility function to return the time since the simulation began in microseconds.
Definition portModule.cc:121
virtual uintptr_t registerLinkAttachTool(const AttachPointMetaData &mdata) override
Function that will be called when a PortModule is registered on sends (i.e.
Definition portModule.cc:25
UnitAlgebra getCoreTimeBase() const
Get the core timebase.
Definition portModule.cc:67
SimTime_t getCurrentSimTimeMilli() const
Utility function to return the time since the simulation began in milliseconds.
Definition portModule.cc:127
UnitAlgebra getElapsedSimTime() const
Return the elapsed simulation time as a time.
Definition portModule.cc:85
virtual void serializeEventAttachPointKey(SST::Core::Serialization::serializer &ser, uintptr_t &key) override
Function that will be called to handle the key returned from registerLinkAttachTool,...
Definition portModule.cc:31
SimTime_t getCurrentSimCycle() const
Return the current simulation time as a cycle count.
Definition portModule.cc:73
SimTime_t getCurrentSimTimeNano() const
Utility function to return the time since the simulation began in nanoseconds.
Definition portModule.cc:115
int getCurrentPriority() const
Return the current priority.
Definition portModule.cc:79
Output & getSimulationOutput() const
Return the base simulation Output class instance.
Definition portModule.cc:91
virtual void eventSent(uintptr_t key, Event *&ev) override=0
Function that will be called when an event is sent on a link with registered PortModules.
SimTime_t getCurrentSimTime(TimeConverter *tc) const
Return the simulated time since the simulation began in units specified by the parameter.
Definition portModule.cc:103
A class to convert between a component's view of time and the core's view of time.
Definition timeConverter.h:28
Performs Unit math in full precision.
Definition unitAlgebra.h:107
Struct used as a base class for all AttachPoint metadata passed to registration functions.
Definition sst_types.h:73