| helics 3.6.1

GlobalFederateId.hpp
1 /*
2 Copyright (c) 2017-2025,
3 Battelle Memorial Institute; Lawrence Livermore National Security, LLC; Alliance for Sustainable
4 Energy, LLC. See the top-level NOTICE for additional details. All rights reserved.
5 SPDX-License-Identifier: BSD-3-Clause
6 */
7
8 #pragma once
9 #include "LocalFederateId.hpp"
10
11 namespace helics {
22 constexpr IdentifierBaseType gGlobalFederateIdShift{0x0002'0000};
24 constexpr IdentifierBaseType gGlobalBrokerIdShift{0x7000'0000};
26 constexpr IdentifierBaseType gGlobalPriorityBlockSize{100'000'000};
27
30 class GlobalBrokerId {
31 public:
32 using BaseType = IdentifierBaseType;
34 constexpr GlobalBrokerId() = default;
35
36 constexpr explicit GlobalBrokerId(BaseType val) noexcept: gid(val) {}
38 constexpr BaseType baseValue() const { return gid; }
40 constexpr bool operator==(GlobalBrokerId id) const noexcept { return (gid == id.gid); }
42 constexpr bool operator!=(GlobalBrokerId id) const noexcept { return (gid != id.gid); }
44 constexpr bool operator<(GlobalBrokerId id) const noexcept { return (gid < id.gid); }
45
46 constexpr bool isFederate() const
47 {
48 return ((gid >= gGlobalFederateIdShift) && (gid < gGlobalBrokerIdShift));
49 }
50 constexpr bool isBroker() const { return (gid >= gGlobalBrokerIdShift || gid == 1); }
51 constexpr bool isValid() const
52 {
53 return (gid != invalid_global_broker_id && gid != detail::gInvalidInterfaceHandle);
54 }
55 BaseType localIndex() const
56 {
57 return (gid >= gGlobalBrokerIdShift) ? (gid - gGlobalBrokerIdShift) : gid;
58 }
59
60 private:
61 BaseType gid = invalid_global_broker_id;
62 friend class GlobalFederateId; // for allowing comparison operators to work well
63 static constexpr BaseType invalid_global_broker_id{-2'010'000'000};
64 };
65
67 constexpr GlobalBrokerId parent_broker_id{0};
69 constexpr GlobalBrokerId gRootBrokerID{1};
70
73 std::ostream& operator<<(std::ostream& os, GlobalBrokerId id);
75 class GlobalFederateId {
76 public:
77 using BaseType = IdentifierBaseType;
79 constexpr GlobalFederateId() = default;
80
81 constexpr explicit GlobalFederateId(BaseType val) noexcept: gid(val) {}
83 constexpr GlobalFederateId(GlobalBrokerId id) noexcept: gid(id.gid) {} // NOLINT
84
85 constexpr operator GlobalBrokerId() const noexcept { return GlobalBrokerId{gid}; } // NOLINT
87 constexpr BaseType baseValue() const { return gid; }
89 constexpr bool operator==(GlobalFederateId id) const noexcept { return (gid == id.gid); }
91 constexpr bool operator!=(GlobalFederateId id) const noexcept { return (gid != id.gid); }
93 constexpr bool operator<(GlobalFederateId id) const noexcept { return (gid < id.gid); }
95 constexpr bool operator>(GlobalFederateId id) const noexcept { return (gid > id.gid); }
97 constexpr bool operator==(GlobalBrokerId id) const noexcept { return (gid == id.gid); }
99 constexpr bool operator!=(GlobalBrokerId id) const noexcept { return (gid != id.gid); }
101 constexpr bool operator<(GlobalBrokerId id) const noexcept { return (gid < id.gid); }
103 constexpr bool operator>(GlobalBrokerId id) const noexcept { return (gid > id.gid); }
105 constexpr bool isFederate() const
106 {
107 return ((gid >= gGlobalFederateIdShift) && (gid < gGlobalBrokerIdShift));
108 }
110 constexpr bool isBroker() const { return (gid >= gGlobalBrokerIdShift || gid == 1); }
112 constexpr bool isValid() const
113 {
114 return (gid != invalid_global_fed_id && gid != detail::gInvalidInterfaceHandle);
115 }
118 constexpr BaseType localIndex() const
119 {
120 return (gid >= gGlobalFederateIdShift) ? (gid - gGlobalFederateIdShift) : gid;
121 }
122
124 BaseType* getBaseTypePointer() { return &gid; }
125
126 private:
127 static constexpr BaseType invalid_global_fed_id{-2'010'000'000};
128 BaseType gid{invalid_global_fed_id};
129 };
131 constexpr GlobalFederateId gDirectCoreId{-235262};
132
134 constexpr GlobalFederateId getSpecialFederateId(GlobalBrokerId broker,
135 GlobalBrokerId::BaseType index)
136 {
137 // allow 3 special federates per subbroker/core + 3 special ones for the root broker
138 return (broker != gRootBrokerID) ?
139 (GlobalFederateId(gGlobalBrokerIdShift -
140 3 * (broker.baseValue() - gGlobalBrokerIdShift + 2) + index)) :
141 GlobalFederateId(gGlobalBrokerIdShift - index - 1);
142 }
144 std::ostream& operator<<(std::ostream& os, GlobalFederateId id);
145
147 class GlobalHandle {
148 public:
149 GlobalFederateId fed_id = GlobalFederateId{};
150 InterfaceHandle handle = InterfaceHandle{};
152 constexpr GlobalHandle() = default;
154 constexpr GlobalHandle(GlobalFederateId fed, InterfaceHandle hand): fed_id(fed), handle(hand) {}
157 explicit operator uint64_t() const
158 {
159 auto key = static_cast<uint64_t>(fed_id.baseValue()) << 32U;
160 key += static_cast<uint64_t>(handle.baseValue()) & (0x0000'0000'FFFF'FFFF);
161 return key;
162 }
164 constexpr bool operator==(GlobalHandle id) const noexcept
165 {
166 return ((fed_id == id.fed_id) && (handle == id.handle));
167 }
169 constexpr bool operator!=(GlobalHandle id) const noexcept
170 {
171 return ((fed_id != id.fed_id) || (handle != id.handle));
172 }
174 constexpr bool operator<(GlobalHandle id) const noexcept
175 {
176 return (fed_id < id.fed_id) ? true : ((fed_id != id.fed_id) ? false : (handle < id.handle));
177 }
179 constexpr bool isValid() const { return fed_id.isValid() && handle.isValid(); }
180 };
181
184 std::ostream& operator<<(std::ostream& os, GlobalHandle id);
185
187 class route_id {
188 public:
189 using BaseType = IdentifierBaseType;
191 constexpr route_id() = default;
193 constexpr explicit route_id(BaseType val) noexcept: rid(val) {}
195 constexpr BaseType baseValue() const { return rid; }
197 constexpr bool operator==(route_id id) const noexcept { return (rid == id.rid); }
199 constexpr bool operator!=(route_id id) const noexcept { return (rid != id.rid); }
201 constexpr bool operator<(route_id id) const noexcept { return (rid < id.rid); }
203 constexpr bool isValid() const { return (rid != invalid_route_id); }
204
205 private:
206 static constexpr BaseType invalid_route_id{-1'295'148'000};
207 BaseType rid{invalid_route_id};
208 };
209
210 constexpr route_id parent_route_id{0};
211 constexpr route_id control_route{-1};
212
213 constexpr route_id generateRouteId(int32_t route_type_code, int32_t index)
214 {
215 return route_id(route_type_code * 256 * 256 * 256 + index);
216 }
217
218 constexpr int32_t getRouteTypeCode(route_id rid)
219 {
220 return (rid.baseValue() >> 24);
221 }
222
223 constexpr int32_t normal_route_code{0};
224 constexpr int32_t json_route_code{10};
227 std::ostream& operator<<(std::ostream& os, route_id id);
228
229 } // namespace helics
230
231 namespace std {
234 template<>
235 struct hash<helics::GlobalFederateId> {
236 using argument_type = helics::GlobalFederateId;
237 using result_type = std::size_t;
239 result_type operator()(argument_type const& key) const noexcept
240 {
241 return std::hash<helics::GlobalFederateId::BaseType>{}(key.baseValue());
242 }
243 };
244
247 template<>
248 struct hash<helics::GlobalBrokerId> {
249 using argument_type = helics::GlobalBrokerId;
250 using result_type = std::size_t;
252 result_type operator()(argument_type const& key) const noexcept
253 {
254 return std::hash<helics::GlobalBrokerId::BaseType>{}(key.baseValue());
255 }
256 };
257
260 template<>
261 struct hash<helics::route_id> {
262 using argument_type = helics::route_id;
263 using result_type = std::size_t;
265 result_type operator()(argument_type const& key) const noexcept
266 {
267 return std::hash<helics::route_id::BaseType>{}(key.baseValue());
268 }
269 };
270
274 template<>
275 struct hash<helics::GlobalHandle> {
276 using argument_type = helics::GlobalHandle;
277 using result_type = std::size_t;
279 result_type operator()(argument_type const& key) const noexcept
280 {
281 return std::hash<uint64_t>{}(static_cast<uint64_t>(key));
282 }
283 };
284
285 } // namespace std
helics::GlobalBrokerId
Definition: GlobalFederateId.hpp:30
helics::GlobalBrokerId::operator==
constexpr bool operator==(GlobalBrokerId id) const noexcept
Definition: GlobalFederateId.hpp:40
helics::GlobalBrokerId::operator<
constexpr bool operator<(GlobalBrokerId id) const noexcept
Definition: GlobalFederateId.hpp:44
helics::GlobalBrokerId::baseValue
constexpr BaseType baseValue() const
Definition: GlobalFederateId.hpp:38
helics::GlobalBrokerId::operator!=
constexpr bool operator!=(GlobalBrokerId id) const noexcept
Definition: GlobalFederateId.hpp:42
helics::GlobalBrokerId::GlobalBrokerId
constexpr GlobalBrokerId()=default
helics::GlobalFederateId
Definition: GlobalFederateId.hpp:75
helics::GlobalFederateId::isFederate
constexpr bool isFederate() const
Definition: GlobalFederateId.hpp:105
helics::GlobalFederateId::operator>
constexpr bool operator>(GlobalFederateId id) const noexcept
Definition: GlobalFederateId.hpp:95
helics::GlobalFederateId::operator<
constexpr bool operator<(GlobalBrokerId id) const noexcept
Definition: GlobalFederateId.hpp:101
helics::GlobalFederateId::isValid
constexpr bool isValid() const
Definition: GlobalFederateId.hpp:112
helics::GlobalFederateId::GlobalFederateId
constexpr GlobalFederateId(GlobalBrokerId id) noexcept
Definition: GlobalFederateId.hpp:83
helics::GlobalFederateId::operator==
constexpr bool operator==(GlobalFederateId id) const noexcept
Definition: GlobalFederateId.hpp:89
helics::GlobalFederateId::baseValue
constexpr BaseType baseValue() const
Definition: GlobalFederateId.hpp:87
helics::GlobalFederateId::operator!=
constexpr bool operator!=(GlobalFederateId id) const noexcept
Definition: GlobalFederateId.hpp:91
helics::GlobalFederateId::operator>
constexpr bool operator>(GlobalBrokerId id) const noexcept
Definition: GlobalFederateId.hpp:103
helics::GlobalFederateId::localIndex
constexpr BaseType localIndex() const
Definition: GlobalFederateId.hpp:118
helics::GlobalFederateId::getBaseTypePointer
BaseType * getBaseTypePointer()
Definition: GlobalFederateId.hpp:124
helics::GlobalFederateId::operator==
constexpr bool operator==(GlobalBrokerId id) const noexcept
Definition: GlobalFederateId.hpp:97
helics::GlobalFederateId::operator<
constexpr bool operator<(GlobalFederateId id) const noexcept
Definition: GlobalFederateId.hpp:93
helics::GlobalFederateId::GlobalFederateId
constexpr GlobalFederateId()=default
helics::GlobalFederateId::operator!=
constexpr bool operator!=(GlobalBrokerId id) const noexcept
Definition: GlobalFederateId.hpp:99
helics::GlobalFederateId::isBroker
constexpr bool isBroker() const
Definition: GlobalFederateId.hpp:110
helics::GlobalHandle
Definition: GlobalFederateId.hpp:147
helics::GlobalHandle::GlobalHandle
constexpr GlobalHandle(GlobalFederateId fed, InterfaceHandle hand)
Definition: GlobalFederateId.hpp:154
helics::GlobalHandle::fed_id
GlobalFederateId fed_id
the federate id component
Definition: GlobalFederateId.hpp:149
helics::GlobalHandle::handle
InterfaceHandle handle
the interface handle component
Definition: GlobalFederateId.hpp:150
helics::GlobalHandle::isValid
constexpr bool isValid() const
Definition: GlobalFederateId.hpp:179
helics::GlobalHandle::operator<
constexpr bool operator<(GlobalHandle id) const noexcept
Definition: GlobalFederateId.hpp:174
helics::GlobalHandle::operator==
constexpr bool operator==(GlobalHandle id) const noexcept
Definition: GlobalFederateId.hpp:164
helics::GlobalHandle::operator!=
constexpr bool operator!=(GlobalHandle id) const noexcept
Definition: GlobalFederateId.hpp:169
helics::GlobalHandle::GlobalHandle
constexpr GlobalHandle()=default
helics::InterfaceHandle
Definition: LocalFederateId.hpp:65
helics::InterfaceHandle::baseValue
constexpr BaseType baseValue() const
Definition: LocalFederateId.hpp:73
helics::route_id
Definition: GlobalFederateId.hpp:187
helics::route_id::route_id
constexpr route_id(BaseType val) noexcept
Definition: GlobalFederateId.hpp:193
helics::route_id::operator==
constexpr bool operator==(route_id id) const noexcept
Definition: GlobalFederateId.hpp:197
helics::route_id::baseValue
constexpr BaseType baseValue() const
Definition: GlobalFederateId.hpp:195
helics::route_id::route_id
constexpr route_id()=default
helics::route_id::operator<
constexpr bool operator<(route_id id) const noexcept
Definition: GlobalFederateId.hpp:201
helics::route_id::operator!=
constexpr bool operator!=(route_id id) const noexcept
Definition: GlobalFederateId.hpp:199
helics::route_id::isValid
constexpr bool isValid() const
Definition: GlobalFederateId.hpp:203
helics
the main namespace for the helics co-simulation library User functions will be in the helics namespac...
Definition: AsyncFedCallInfo.hpp:14
helics::gGlobalPriorityBlockSize
constexpr IdentifierBaseType gGlobalPriorityBlockSize
Definition: GlobalFederateId.hpp:26
helics::getSpecialFederateId
constexpr GlobalFederateId getSpecialFederateId(GlobalBrokerId broker, GlobalBrokerId::BaseType index)
Definition: GlobalFederateId.hpp:134
helics::operator<<
std::ostream & operator<<(std::ostream &out, const ActionMessage &command)
Definition: ActionMessage.cpp:965
helics::gRootBrokerID
constexpr GlobalBrokerId gRootBrokerID
Definition: GlobalFederateId.hpp:69
helics::gDirectCoreId
constexpr GlobalFederateId gDirectCoreId
Definition: GlobalFederateId.hpp:131
helics::gGlobalBrokerIdShift
constexpr IdentifierBaseType gGlobalBrokerIdShift
Definition: GlobalFederateId.hpp:24
helics::parent_broker_id
constexpr GlobalBrokerId parent_broker_id
Definition: GlobalFederateId.hpp:67
helics::IdentifierBaseType
int32_t IdentifierBaseType
Definition: LocalFederateId.hpp:15
helics::gGlobalFederateIdShift
constexpr IdentifierBaseType gGlobalFederateIdShift
Definition: GlobalFederateId.hpp:22
std::hash< helics::GlobalBrokerId >::result_type
std::size_t result_type
typedef for output result
Definition: GlobalFederateId.hpp:250
std::hash< helics::GlobalBrokerId >::operator()
result_type operator()(argument_type const &key) const noexcept
Definition: GlobalFederateId.hpp:252
std::hash< helics::GlobalFederateId >::operator()
result_type operator()(argument_type const &key) const noexcept
Definition: GlobalFederateId.hpp:239
std::hash< helics::GlobalFederateId >::result_type
std::size_t result_type
typedef for output result
Definition: GlobalFederateId.hpp:237
std::hash< helics::GlobalHandle >::operator()
result_type operator()(argument_type const &key) const noexcept
Definition: GlobalFederateId.hpp:279
std::hash< helics::GlobalHandle >::result_type
std::size_t result_type
typedef for output result
Definition: GlobalFederateId.hpp:277
std::hash< helics::route_id >::result_type
std::size_t result_type
typedef for output result
Definition: GlobalFederateId.hpp:263
std::hash< helics::route_id >::operator()
result_type operator()(argument_type const &key) const noexcept
Definition: GlobalFederateId.hpp:265