CIRCT 23.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// Metadata constants which may or may not be used by various backends. Provided
43// here since they are intended to be somewhat standard.
44//===----------------------------------------------------------------------===//
45
46constexpr uint32_t MetadataOffset = 8;
47
48constexpr uint64_t MagicNumberLo = 0xE5100E51;
49constexpr uint64_t MagicNumberHi = 0x207D98E5;
50constexpr uint64_t MagicNumber = MagicNumberLo | (MagicNumberHi << 32);
51constexpr uint64_t MagicNumberOffset = 0;
52
53constexpr uint32_t ExpectedVersionNumber = 0;
54constexpr uint64_t VersionNumberOffset = 8;
55
56constexpr uint32_t ManifestPtrOffset = 0x10;
57
58constexpr uint32_t CycleCountOffset = 0x20;
59constexpr uint32_t CoreFreqOffset = 0x28;
60
61/// Magic value which, when written to MMIO offset `ResetRequestOffset`,
62/// requests a design reset. Keep in sync with 'ResetMagicNumber' in the PyCDE
63/// BSP (python/esiaccel/bsp/common.py).
64constexpr uint64_t ResetMagicNumber = 0x00000E510000B007;
65/// Offset into the (global) MMIO space at which to request a design reset.
66constexpr uint32_t ResetRequestOffset = 0x38;
67
68//===----------------------------------------------------------------------===//
69// Accelerator design hierarchy root.
70//===----------------------------------------------------------------------===//
71
72/// Top level accelerator class. Maintains a shared pointer to the manifest,
73/// which owns objects used in the design hierarchy owned by this class. Since
74/// this class owns the entire design hierarchy, when it gets destroyed the
75/// entire design hierarchy gets destroyed so all of the instances, ports, etc.
76/// are no longer valid pointers.
77class Accelerator : public HWModule {
78public:
79 Accelerator() = delete;
80 Accelerator(const Accelerator &) = delete;
81 ~Accelerator() = default;
82 Accelerator(std::optional<ModuleInfo> info,
83 std::vector<std::unique_ptr<Instance>> children,
84 std::vector<services::Service *> services,
85 std::vector<std::unique_ptr<BundlePort>> &&ports)
86 : HWModule(info, std::move(children), services, std::move(ports)) {}
87};
88
89//===----------------------------------------------------------------------===//
90// Connection to the accelerator and its services.
91//===----------------------------------------------------------------------===//
92
93/// Abstract class representing a connection to an accelerator. Actual
94/// connections (e.g. to a co-simulation or actual device) are implemented by
95/// subclasses. No methods in here are thread safe.
97public:
99 virtual ~AcceleratorConnection();
100 Context &getCtxt() const { return ctxt; }
101 Logger &getLogger() const { return ctxt.getLogger(); }
102
103 /// Disconnect from the accelerator cleanly.
104 virtual void disconnect();
105
106 /// Request a reset of the accelerator design. Returns true if the reset was
107 /// successfully requested, false if it could not be performed for any reason
108 /// -- most commonly because the backend (BSP) does not support resets.
109 virtual bool reset();
110
111 /// Return a pointer to the accelerator 'service' thread (or threads). If the
112 /// thread(s) are not running, they will be started when this method is
113 /// called. `std::thread` is used. If users don't want the runtime to spin up
114 /// threads, don't call this method. `AcceleratorServiceThread` is owned by
115 /// AcceleratorConnection and governed by the lifetime of the this object.
117
119 /// Get a typed reference to a particular service type. Caller does *not* take
120 /// ownership of the returned pointer -- the Accelerator object owns it.
121 /// Pointer lifetime ends with the Accelerator lifetime.
122 template <typename ServiceClass>
123 ServiceClass *getService(AppIDPath id = {}, std::string implName = {},
124 ServiceImplDetails details = {},
125 HWClientDetails clients = {}) {
126 return dynamic_cast<ServiceClass *>(
127 getService(typeid(ServiceClass), id, implName, details, clients));
128 }
129 /// Calls `createService` and caches the result. Subclasses can override if
130 /// they want to use their own caching mechanism.
131 virtual Service *getService(Service::Type service, AppIDPath id = {},
132 std::string implName = {},
133 ServiceImplDetails details = {},
134 HWClientDetails clients = {});
135
136 /// Assume ownership of an accelerator object. Ties the lifetime of the
137 /// accelerator to this connection. Returns a raw pointer to the object.
138 Accelerator *takeOwnership(std::unique_ptr<Accelerator> accel);
139
140 /// Create a new engine for channel communication with the accelerator. The
141 /// default is to call the global `createEngine` to get an engine which has
142 /// registered itself. Individual accelerator connection backends can override
143 /// this to customize behavior.
144 virtual void createEngine(const std::string &engineTypeName, AppIDPath idPath,
145 const ServiceImplDetails &details,
146 const HWClientDetails &clients);
148 return clientEngines[id];
149 }
150
152 if (!ownedAccelerator)
153 throw std::runtime_error(
154 "AcceleratorConnection does not own an accelerator");
155 return *ownedAccelerator;
156 }
157
158protected:
159 /// If `createEngine` is overridden, this method should be called to register
160 /// the engine and all of the channels it services.
161 void registerEngine(AppIDPath idPath, std::unique_ptr<Engine> engine,
162 const HWClientDetails &clients);
163
164 /// Drop accelerator-owned objects before a derived backend destroys resources
165 /// that those objects may reference.
166 void clearOwnedObjects();
167
168 /// Called by `getServiceImpl` exclusively. It wraps the pointer returned by
169 /// this in a unique_ptr and caches it. Separate this from the
170 /// wrapping/caching since wrapping/caching is an implementation detail.
172 std::string implName,
173 const ServiceImplDetails &details,
174 const HWClientDetails &clients) = 0;
175
176 /// Collection of owned engines.
177 std::map<AppIDPath, std::unique_ptr<Engine>> ownedEngines;
178 /// Mapping of clients to their servicing engines.
179 std::map<AppIDPath, BundleEngineMap> clientEngines;
180
181private:
182 /// ESI accelerator context.
184
185 /// Cache services via a unique_ptr so they get free'd automatically when
186 /// Accelerator objects get deconstructed.
187 using ServiceCacheKey = std::tuple<std::string, AppIDPath>;
188 std::map<ServiceCacheKey, std::unique_ptr<Service>> serviceCache;
189
190 std::unique_ptr<AcceleratorServiceThread> serviceThread;
191
192 /// Accelerator object owned by this connection.
193 std::unique_ptr<Accelerator> ownedAccelerator;
194};
195
196namespace registry {
197
198namespace internal {
199
200/// Backends can register themselves to be connected via a connection string.
201using BackendCreate = std::function<std::unique_ptr<AcceleratorConnection>(
202 Context &, std::string)>;
203void registerBackend(const std::string &name, BackendCreate create);
204
205// Helper struct to
206template <typename TAccelerator>
208 RegisterAccelerator(const char *name) {
209 registerBackend(name, &TAccelerator::connect);
210 }
211};
212
213#define REGISTER_ACCELERATOR(Name, TAccelerator) \
214 static ::esi::registry::internal::RegisterAccelerator<TAccelerator> \
215 __register_accel____LINE__(Name)
216
217} // namespace internal
218} // namespace registry
219
220/// Background thread which services various requests. Currently, it listens on
221/// ports and calls callbacks for incoming messages on said ports.
223public:
226
227 /// When there's data on any of the listenPorts, call the callback. Callable
228 /// from any thread.
229 void
230 addListener(std::initializer_list<ReadChannelPort *> listenPorts,
231 std::function<void(ReadChannelPort *, MessageData)> callback);
232
233 /// Poll this module.
234 void addPoll(HWModule &module);
235
236 /// Instruct the service thread to stop running.
237 void stop();
238
239private:
240 struct Impl;
241 std::unique_ptr<Impl> impl;
242};
243} // namespace esi
244
245#endif // ESI_ACCELERATOR_H
Abstract class representing a connection to an accelerator.
Definition Accelerator.h:96
std::tuple< std::string, AppIDPath > ServiceCacheKey
Cache services via a unique_ptr so they get free'd automatically when Accelerator objects get deconst...
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
virtual bool reset()
Request a reset of the accelerator design.
void clearOwnedObjects()
Drop accelerator-owned objects before a derived backend destroys resources that those objects may ref...
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::unique_ptr< AcceleratorServiceThread > serviceThread
std::unique_ptr< Accelerator > ownedAccelerator
Accelerator object owned by this connection.
virtual void disconnect()
Disconnect from the accelerator cleanly.
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
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.
std::unique_ptr< Impl > impl
void stop()
Instruct the service thread to stop running.
void addPoll(HWModule &module)
Poll this module.
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.
Top level accelerator class.
Definition Accelerator.h:77
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:82
~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:34
Logger & getLogger()
Definition Context.h:69
Represents either the top level or an instance of a hardware module.
Definition Design.h:47
const std::optional< ModuleInfo > info
Definition Design.h:101
const std::vector< std::unique_ptr< BundlePort > > ports
Definition Design.h:105
const std::vector< std::unique_ptr< Instance > > children
Definition Design.h:102
const std::vector< services::Service * > services
Definition Design.h:104
A concrete flat message backed by a single vector of bytes.
Definition Common.h:155
A ChannelPort which reads data from the accelerator.
Definition Ports.h:453
Parent class of all APIs modeled as 'services'.
Definition Services.h:59
const std::type_info & Type
Definition Services.h:61
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.
Definition esi.py:1
constexpr uint32_t CoreFreqOffset
Definition Accelerator.h:59
constexpr uint64_t MagicNumber
Definition Accelerator.h:50
std::map< std::string, std::any > ServiceImplDetails
Definition Common.h:108
constexpr uint64_t MagicNumberHi
Definition Accelerator.h:49
constexpr uint64_t ResetMagicNumber
Magic value which, when written to MMIO offset ResetRequestOffset, requests a design reset.
Definition Accelerator.h:64
constexpr uint32_t ExpectedVersionNumber
Definition Accelerator.h:53
constexpr uint64_t MagicNumberOffset
Definition Accelerator.h:51
constexpr uint32_t MetadataOffset
Definition Accelerator.h:46
constexpr uint32_t CycleCountOffset
Definition Accelerator.h:58
constexpr uint32_t ManifestPtrOffset
Definition Accelerator.h:56
constexpr uint32_t ResetRequestOffset
Offset into the (global) MMIO space at which to request a design reset.
Definition Accelerator.h:66
constexpr uint64_t MagicNumberLo
Definition Accelerator.h:48
constexpr uint64_t VersionNumberOffset
Definition Accelerator.h:54
std::vector< HWClientDetail > HWClientDetails
Definition Common.h:107