CIRCT 21.0.0git
Loading...
Searching...
No Matches
Accelerator.h
Go to the documentation of this file.
1//===- Accelerator.h - Base ESI runtime API ---------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Basic ESI APIs. The 'Accelerator' class is the superclass for all accelerator
10// backends. It should (usually) provide enough functionality such that users do
11// not have to interact with the platform-specific backend implementation with
12// the exception of connecting to the accelerator.
13//
14// DO NOT EDIT!
15// This file is distributed as part of an ESI package. The source for this file
16// should always be modified within CIRCT.
17//
18//===----------------------------------------------------------------------===//
19
20// NOLINTNEXTLINE(llvm-header-guard)
21#ifndef ESI_ACCELERATOR_H
22#define ESI_ACCELERATOR_H
23
24#include "esi/Context.h"
25#include "esi/Design.h"
26#include "esi/Engines.h"
27#include "esi/Manifest.h"
28#include "esi/Ports.h"
29#include "esi/Services.h"
30
31#include <functional>
32#include <map>
33#include <memory>
34#include <string>
35#include <typeinfo>
36
37namespace esi {
38// Forward declarations.
39class AcceleratorServiceThread;
40
41//===----------------------------------------------------------------------===//
42// Constants used by low-level APIs.
43//===----------------------------------------------------------------------===//
44
45constexpr uint32_t MetadataOffset = 8;
46constexpr uint64_t MagicNumberLo = 0xE5100E51;
47constexpr uint64_t MagicNumberHi = 0x207D98E5;
48constexpr uint64_t MagicNumber = MagicNumberLo | (MagicNumberHi << 32);
49constexpr uint32_t ExpectedVersionNumber = 0;
50
51//===----------------------------------------------------------------------===//
52// Accelerator design hierarchy root.
53//===----------------------------------------------------------------------===//
54
55/// Top level accelerator class. Maintains a shared pointer to the manifest,
56/// which owns objects used in the design hierarchy owned by this class. Since
57/// this class owns the entire design hierarchy, when it gets destroyed the
58/// entire design hierarchy gets destroyed so all of the instances, ports, etc.
59/// are no longer valid pointers.
60class Accelerator : public HWModule {
61public:
62 Accelerator() = delete;
63 Accelerator(const Accelerator &) = delete;
64 ~Accelerator() = default;
65 Accelerator(std::optional<ModuleInfo> info,
66 std::vector<std::unique_ptr<Instance>> children,
67 std::vector<services::Service *> services,
68 std::vector<std::unique_ptr<BundlePort>> &ports)
69 : HWModule(info, std::move(children), services, ports) {}
70};
71
72//===----------------------------------------------------------------------===//
73// Connection to the accelerator and its services.
74//===----------------------------------------------------------------------===//
75
76/// Abstract class representing a connection to an accelerator. Actual
77/// connections (e.g. to a co-simulation or actual device) are implemented by
78/// subclasses. No methods in here are thread safe.
80public:
82 virtual ~AcceleratorConnection();
83 Context &getCtxt() const { return ctxt; }
84 Logger &getLogger() const { return ctxt.getLogger(); }
85
86 /// Disconnect from the accelerator cleanly.
87 virtual void disconnect();
88
89 // While building the design, keep around a std::map of active services
90 // indexed by the service name. When a new service is encountered during
91 // descent, add it to the table (perhaps overwriting one). Modifications to
92 // the table only apply to the current branch, so copy this and update it at
93 // each level of the tree.
94 using ServiceTable = std::map<std::string, services::Service *>;
95
96 /// Return a pointer to the accelerator 'service' thread (or threads). If the
97 /// thread(s) are not running, they will be started when this method is
98 /// called. `std::thread` is used. If users don't want the runtime to spin up
99 /// threads, don't call this method. `AcceleratorServiceThread` is owned by
100 /// AcceleratorConnection and governed by the lifetime of the this object.
102
104 /// Get a typed reference to a particular service type. Caller does *not* take
105 /// ownership of the returned pointer -- the Accelerator object owns it.
106 /// Pointer lifetime ends with the Accelerator lifetime.
107 template <typename ServiceClass>
108 ServiceClass *getService(AppIDPath id = {}, std::string implName = {},
109 ServiceImplDetails details = {},
110 HWClientDetails clients = {}) {
111 return dynamic_cast<ServiceClass *>(
112 getService(typeid(ServiceClass), id, implName, details, clients));
113 }
114 /// Calls `createService` and caches the result. Subclasses can override if
115 /// they want to use their own caching mechanism.
116 virtual Service *getService(Service::Type service, AppIDPath id = {},
117 std::string implName = {},
118 ServiceImplDetails details = {},
119 HWClientDetails clients = {});
120
121 /// Assume ownership of an accelerator object. Ties the lifetime of the
122 /// accelerator to this connection. Returns a raw pointer to the object.
123 Accelerator *takeOwnership(std::unique_ptr<Accelerator> accel);
124
125 /// Create a new engine for channel communication with the accelerator. The
126 /// default is to call the global `createEngine` to get an engine which has
127 /// registered itself. Individual accelerator connection backends can override
128 /// this to customize behavior.
129 virtual void createEngine(const std::string &engineTypeName, AppIDPath idPath,
130 const ServiceImplDetails &details,
131 const HWClientDetails &clients);
133 return clientEngines[id];
134 }
135
137 if (!ownedAccelerator)
138 throw std::runtime_error(
139 "AcceleratorConnection does not own an accelerator");
140 return *ownedAccelerator;
141 }
142
143protected:
144 /// If `createEngine` is overridden, this method should be called to register
145 /// the engine and all of the channels it services.
146 void registerEngine(AppIDPath idPath, std::unique_ptr<Engine> engine,
147 const HWClientDetails &clients);
148
149 /// Called by `getServiceImpl` exclusively. It wraps the pointer returned by
150 /// this in a unique_ptr and caches it. Separate this from the
151 /// wrapping/caching since wrapping/caching is an implementation detail.
153 std::string implName,
154 const ServiceImplDetails &details,
155 const HWClientDetails &clients) = 0;
156
157 /// Collection of owned engines.
158 std::map<AppIDPath, std::unique_ptr<Engine>> ownedEngines;
159 /// Mapping of clients to their servicing engines.
160 std::map<AppIDPath, BundleEngineMap> clientEngines;
161
162private:
163 /// ESI accelerator context.
165
166 /// Cache services via a unique_ptr so they get free'd automatically when
167 /// Accelerator objects get deconstructed.
168 using ServiceCacheKey = std::tuple<const std::type_info *, AppIDPath>;
169 std::map<ServiceCacheKey, std::unique_ptr<Service>> serviceCache;
170
171 std::unique_ptr<AcceleratorServiceThread> serviceThread;
172
173 /// Accelerator object owned by this connection.
174 std::unique_ptr<Accelerator> ownedAccelerator;
175};
176
177namespace registry {
178
179// Connect to an ESI accelerator given a backend name and connection specifier.
180// Alternatively, instantiate the backend directly (if you're using C++).
181std::unique_ptr<AcceleratorConnection> connect(Context &ctxt,
182 const std::string &backend,
183 const std::string &connection);
184
185namespace internal {
186
187/// Backends can register themselves to be connected via a connection string.
188using BackendCreate = std::function<std::unique_ptr<AcceleratorConnection>(
189 Context &, std::string)>;
190void registerBackend(const std::string &name, BackendCreate create);
191
192// Helper struct to
193template <typename TAccelerator>
195 RegisterAccelerator(const char *name) {
196 registerBackend(name, &TAccelerator::connect);
197 }
198};
199
200#define REGISTER_ACCELERATOR(Name, TAccelerator) \
201 static ::esi::registry::internal::RegisterAccelerator<TAccelerator> \
202 __register_accel____LINE__(Name)
203
204} // namespace internal
205} // namespace registry
206
207/// Background thread which services various requests. Currently, it listens on
208/// ports and calls callbacks for incoming messages on said ports.
210public:
213
214 /// When there's data on any of the listenPorts, call the callback. Callable
215 /// from any thread.
216 void
217 addListener(std::initializer_list<ReadChannelPort *> listenPorts,
218 std::function<void(ReadChannelPort *, MessageData)> callback);
219
220 /// Poll this module.
221 void addPoll(HWModule &module);
222
223 /// Instruct the service thread to stop running.
224 void stop();
225
226private:
227 struct Impl;
228 std::unique_ptr<Impl> impl;
229};
230} // namespace esi
231
232#endif // ESI_ACCELERATOR_H
Abstract class representing a connection to an accelerator.
Definition Accelerator.h:79
virtual void disconnect()
Disconnect from the accelerator cleanly.
virtual Service * createService(Service::Type service, AppIDPath idPath, std::string implName, const ServiceImplDetails &details, const HWClientDetails &clients)=0
Called by getServiceImpl exclusively.
services::Service Service
ServiceClass * getService(AppIDPath id={}, std::string implName={}, ServiceImplDetails details={}, HWClientDetails clients={})
Get a typed reference to a particular service type.
Context & getCtxt() const
Definition Accelerator.h:83
std::map< std::string, services::Service * > ServiceTable
Definition Accelerator.h:94
std::map< AppIDPath, BundleEngineMap > clientEngines
Mapping of clients to their servicing engines.
Context & ctxt
ESI accelerator context.
void registerEngine(AppIDPath idPath, std::unique_ptr< Engine > engine, const HWClientDetails &clients)
If createEngine is overridden, this method should be called to register the engine and all of the cha...
std::map< ServiceCacheKey, std::unique_ptr< Service > > serviceCache
std::tuple< const std::type_info *, AppIDPath > ServiceCacheKey
Cache services via a unique_ptr so they get free'd automatically when Accelerator objects get deconst...
std::unique_ptr< AcceleratorServiceThread > serviceThread
std::unique_ptr< Accelerator > ownedAccelerator
Accelerator object owned by this connection.
std::map< AppIDPath, std::unique_ptr< Engine > > ownedEngines
Collection of owned engines.
Accelerator & getAccelerator()
virtual void createEngine(const std::string &engineTypeName, AppIDPath idPath, const ServiceImplDetails &details, const HWClientDetails &clients)
Create a new engine for channel communication with the accelerator.
virtual const BundleEngineMap & getEngineMapFor(AppIDPath id)
Logger & getLogger() const
Definition Accelerator.h:84
AcceleratorServiceThread * getServiceThread()
Return a pointer to the accelerator 'service' thread (or threads).
Accelerator * takeOwnership(std::unique_ptr< Accelerator > accel)
Assume ownership of an accelerator object.
Background thread which services various requests.
void stop()
Instruct the service thread to stop running.
void addListener(std::initializer_list< ReadChannelPort * > listenPorts, std::function< void(ReadChannelPort *, MessageData)> callback)
When there's data on any of the listenPorts, call the callback.
std::unique_ptr< Impl > impl
void addPoll(HWModule &module)
Poll this module.
Top level accelerator class.
Definition Accelerator.h:60
Accelerator(std::optional< ModuleInfo > info, std::vector< std::unique_ptr< Instance > > children, std::vector< services::Service * > services, std::vector< std::unique_ptr< BundlePort > > &ports)
Definition Accelerator.h:65
~Accelerator()=default
Accelerator()=delete
Accelerator(const Accelerator &)=delete
Since engines can support multiple channels BUT not necessarily all of the channels in a bundle,...
Definition Engines.h:73
AcceleratorConnections, Accelerators, and Manifests must all share a context.
Definition Context.h:31
Logger & getLogger()
Definition Context.h:62
Represents either the top level or an instance of a hardware module.
Definition Design.h:47
const std::optional< ModuleInfo > info
Definition Design.h:97
const std::vector< std::unique_ptr< BundlePort > > ports
Definition Design.h:101
const std::vector< std::unique_ptr< Instance > > children
Definition Design.h:98
const std::vector< services::Service * > services
Definition Design.h:100
A logical chunk of data representing serialized data.
Definition Common.h:103
A ChannelPort which reads data from the accelerator.
Definition Ports.h:124
Parent class of all APIs modeled as 'services'.
Definition Services.h:46
const std::type_info & Type
Definition Services.h:48
void registerBackend(const std::string &name, BackendCreate create)
std::function< std::unique_ptr< AcceleratorConnection >(Context &, std::string)> BackendCreate
Backends can register themselves to be connected via a connection string.
std::unique_ptr< AcceleratorConnection > connect(Context &ctxt, const std::string &backend, const std::string &connection)
Definition esi.py:1
constexpr uint64_t MagicNumber
Definition Accelerator.h:48
std::map< std::string, std::any > ServiceImplDetails
Definition Common.h:98
constexpr uint64_t MagicNumberHi
Definition Accelerator.h:47
constexpr uint32_t ExpectedVersionNumber
Definition Accelerator.h:49
constexpr uint32_t MetadataOffset
Definition Accelerator.h:45
constexpr uint64_t MagicNumberLo
Definition Accelerator.h:46
std::vector< HWClientDetail > HWClientDetails
Definition Common.h:97