CIRCT 23.0.0git
Loading...
Searching...
No Matches
esiCppAccel.cpp
Go to the documentation of this file.
1//===- esiaccel.cpp - ESI runtime python bindings ---------------*- 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// Simply wrap the C++ API into a Python module called 'esiaccel'.
10//
11//===----------------------------------------------------------------------===//
12
13#include "esi/Accelerator.h"
14#include "esi/Services.h"
15
16#include "esi/backends/Cosim.h"
17
18#include <ranges>
19#include <sstream>
20
21// nanobind includes.
22// Python world does not respect constness. So it doesn't make sense to have
23// const checks. Disable related warnings.
24#if defined(__GNUC__)
25#pragma GCC diagnostic push
26#pragma GCC diagnostic ignored "-Wcast-qual"
27#endif
28#include <nanobind/nanobind.h>
29#include <nanobind/stl/function.h>
30#include <nanobind/stl/map.h>
31#include <nanobind/stl/optional.h>
32#include <nanobind/stl/pair.h>
33#include <nanobind/stl/string.h>
34#include <nanobind/stl/tuple.h>
35#include <nanobind/stl/unique_ptr.h>
36#include <nanobind/stl/vector.h>
37#if defined(__GNUC__)
38#pragma GCC diagnostic pop
39#endif
40
41namespace nb = nanobind;
42
43using namespace esi;
44using namespace esi::services;
45
46namespace nanobind {
47namespace detail {
48
49template <>
50struct type_hook<ChannelPort> {
51 static const std::type_info *get(const ChannelPort *port) {
52 if (dynamic_cast<const WriteChannelPort *>(port))
53 return &typeid(WriteChannelPort);
54 if (dynamic_cast<const ReadChannelPort *>(port))
55 return &typeid(ReadChannelPort);
56 return &typeid(ChannelPort);
57 }
58};
59
60template <>
61struct type_hook<Service> {
62 static const std::type_info *get(const Service *svc) {
63 if (dynamic_cast<const MMIO *>(svc))
64 return &typeid(MMIO);
65 if (dynamic_cast<const SysInfo *>(svc))
66 return &typeid(SysInfo);
67 if (dynamic_cast<const HostMem *>(svc))
68 return &typeid(HostMem);
69 if (dynamic_cast<const TelemetryService *>(svc))
70 return &typeid(TelemetryService);
71 return &typeid(Service);
72 }
73};
74
75/// Nanobind doesn't have a built-in type caster for std::any.
76/// We must provide one which knows about all of the potential types which the
77/// any might be.
78template <>
79struct type_caster<std::any> {
80 NB_TYPE_CASTER(std::any, const_name("object"))
81
82 static handle from_cpp(const std::any &src, rv_policy /* policy */,
83 cleanup_list * /* cleanup */) {
84 const std::type_info &t = src.type();
85 if (t == typeid(std::string))
86 return nb::str(std::any_cast<std::string>(src).c_str()).release();
87 else if (t == typeid(int64_t))
88 return nb::int_(std::any_cast<int64_t>(src)).release();
89 else if (t == typeid(uint64_t))
90 return nb::int_(std::any_cast<uint64_t>(src)).release();
91 else if (t == typeid(double))
92 return nb::float_(std::any_cast<double>(src)).release();
93 else if (t == typeid(bool))
94 return nb::bool_(std::any_cast<bool>(src)).release();
95 else if (t == typeid(std::nullptr_t))
96 return nb::none().release();
97 return nb::none().release();
98 }
99};
100} // namespace detail
101} // namespace nanobind
102
103/// Resolve a Type to the Python wrapper object.
104nb::object getPyType(std::optional<const Type *> t) {
105 nb::object typesModule = nb::module_::import_("esiaccel.types");
106 if (!t)
107 return nb::none();
108 return typesModule.attr("_get_esi_type")(*t);
109}
110
111// NOLINTNEXTLINE(readability-identifier-naming)
112NB_MODULE(esiCppAccel, m) {
113 // TODO: Fix leaks! The one I know of is in the callback code -- if one
114 // registers a python callback it creates a leak.
115 nb::set_leak_warnings(false);
116
117 nb::class_<Type>(m, "Type")
118 .def(nb::init<const Type::ID &>(), nb::arg("id"))
119 .def_prop_ro("id", &Type::getID)
120 .def_prop_ro("bit_width", &Type::getBitWidth)
121 .def("__repr__", [](Type &t) { return "<" + t.getID() + ">"; });
122 nb::class_<ChannelType, Type>(m, "ChannelType")
123 .def(nb::init<const Type::ID &, const Type *>(), nb::arg("id"),
124 nb::arg("inner"))
125 .def_prop_ro("inner", &ChannelType::getInner, nb::rv_policy::reference);
126 nb::enum_<BundleType::Direction>(m, "Direction")
127 .value("To", BundleType::Direction::To)
128 .value("From", BundleType::Direction::From)
129 .export_values();
130 nb::class_<BundleType, Type>(m, "BundleType")
131 .def(nb::init<const Type::ID &, const BundleType::ChannelVector &>(),
132 nb::arg("id"), nb::arg("channels"))
133 .def_prop_ro("channels", &BundleType::getChannels,
134 nb::rv_policy::reference);
135 nb::class_<VoidType, Type>(m, "VoidType")
136 .def(nb::init<const Type::ID &>(), nb::arg("id"));
137 nb::class_<AnyType, Type>(m, "AnyType")
138 .def(nb::init<const Type::ID &>(), nb::arg("id"));
139 nb::class_<TypeAliasType, Type>(m, "TypeAliasType")
140 .def(nb::init<const Type::ID &, std::string, const Type *>(),
141 nb::arg("id"), nb::arg("name"), nb::arg("inner_type"))
142 .def_prop_ro("name", &TypeAliasType::getName)
143 .def_prop_ro("inner", &TypeAliasType::getInnerType,
144 nb::rv_policy::reference);
145 nb::class_<BitVectorType, Type>(m, "BitVectorType")
146 .def(nb::init<const Type::ID &, uint64_t>(), nb::arg("id"),
147 nb::arg("width"))
148 .def_prop_ro("width", &BitVectorType::getWidth);
149 nb::class_<BitsType, BitVectorType>(m, "BitsType")
150 .def(nb::init<const Type::ID &, uint64_t>(), nb::arg("id"),
151 nb::arg("width"));
152 nb::class_<IntegerType, BitVectorType>(m, "IntegerType")
153 .def(nb::init<const Type::ID &, uint64_t>(), nb::arg("id"),
154 nb::arg("width"));
155 nb::class_<SIntType, IntegerType>(m, "SIntType")
156 .def(nb::init<const Type::ID &, uint64_t>(), nb::arg("id"),
157 nb::arg("width"));
158 nb::class_<UIntType, IntegerType>(m, "UIntType")
159 .def(nb::init<const Type::ID &, uint64_t>(), nb::arg("id"),
160 nb::arg("width"));
161 nb::class_<StructType, Type>(m, "StructType")
162 .def(nb::init<const Type::ID &, const StructType::FieldVector &, bool>(),
163 nb::arg("id"), nb::arg("fields"), nb::arg("reverse") = true)
164 .def_prop_ro("fields", &StructType::getFields, nb::rv_policy::reference)
165 .def_prop_ro("reverse", &StructType::isReverse);
166 nb::class_<ArrayType, Type>(m, "ArrayType")
167 .def(nb::init<const Type::ID &, const Type *, uint64_t>(), nb::arg("id"),
168 nb::arg("element_type"), nb::arg("size"))
169 .def_prop_ro("element", &ArrayType::getElementType,
170 nb::rv_policy::reference)
171 .def_prop_ro("size", &ArrayType::getSize);
172 nb::class_<WindowType::Field>(m, "WindowField")
173 .def(nb::init<>())
174 .def(nb::init<std::string, uint64_t, uint64_t>(), nb::arg("name"),
175 nb::arg("num_items") = 0, nb::arg("bulk_count_width") = 0)
176 .def_rw("name", &WindowType::Field::name)
177 .def_rw("num_items", &WindowType::Field::numItems)
178 .def_rw("bulk_count_width", &WindowType::Field::bulkCountWidth);
179 nb::class_<WindowType::Frame>(m, "WindowFrame")
180 .def(nb::init<>())
181 .def(nb::init<std::string, const std::vector<WindowType::Field> &>(),
182 nb::arg("name"), nb::arg("fields"))
183 .def_rw("name", &WindowType::Frame::name)
184 .def_rw("fields", &WindowType::Frame::fields);
185 nb::class_<WindowType, Type>(m, "WindowType")
186 .def(nb::init<const Type::ID &, const std::string &, const Type *,
187 const Type *, const std::vector<WindowType::Frame> &>(),
188 nb::arg("id"), nb::arg("name"), nb::arg("into_type"),
189 nb::arg("lowered_type"), nb::arg("frames"))
190 .def_prop_ro("name", &WindowType::getName)
191 .def_prop_ro("into", &WindowType::getIntoType, nb::rv_policy::reference)
192 .def_prop_ro("lowered", &WindowType::getLoweredType,
193 nb::rv_policy::reference)
194 .def_prop_ro("frames", &WindowType::getFrames, nb::rv_policy::reference);
195 nb::class_<ListType, Type>(m, "ListType")
196 .def(nb::init<const Type::ID &, const Type *>(), nb::arg("id"),
197 nb::arg("element_type"))
198 .def_prop_ro("element", &ListType::getElementType,
199 nb::rv_policy::reference);
200 nb::class_<UnionType, Type>(m, "UnionType")
201 .def(nb::init<const Type::ID &, const UnionType::FieldVector &>(),
202 nb::arg("id"), nb::arg("fields"))
203 .def_prop_ro("fields", &UnionType::getFields, nb::rv_policy::reference);
204
205 nb::class_<Constant>(m, "Constant")
206 .def_prop_ro("value", [](Constant &c) { return c.value; })
207 .def_prop_ro("type", [](Constant &c) { return getPyType(*c.type); });
208
209 nb::class_<AppID>(m, "AppID")
210 .def(nb::init<std::string, std::optional<uint32_t>>(), nb::arg("name"),
211 nb::arg("idx") = std::nullopt)
212 .def_prop_ro("name", [](AppID &id) { return id.name; })
213 .def_prop_ro("idx",
214 [](AppID &id) -> nb::object {
215 if (id.idx)
216 return nb::cast(id.idx);
217 return nb::none();
218 })
219 .def("__repr__",
220 [](AppID &id) {
221 std::string ret = "<" + id.name;
222 if (id.idx)
223 ret = ret + "[" + std::to_string(*id.idx) + "]";
224 ret = ret + ">";
225 return ret;
226 })
227 .def("__eq__", [](AppID &a, AppID &b) { return a == b; })
228 .def("__hash__", [](AppID &id) {
229 return utils::hash_combine(std::hash<std::string>{}(id.name),
230 std::hash<uint32_t>{}(id.idx.value_or(-1)));
231 });
232 nb::class_<AppIDPath>(m, "AppIDPath").def("__repr__", &AppIDPath::toStr);
233
234 nb::class_<ModuleInfo>(m, "ModuleInfo")
235 .def_prop_ro("name", [](ModuleInfo &info) { return info.name; })
236 .def_prop_ro("summary", [](ModuleInfo &info) { return info.summary; })
237 .def_prop_ro("version", [](ModuleInfo &info) { return info.version; })
238 .def_prop_ro("repo", [](ModuleInfo &info) { return info.repo; })
239 .def_prop_ro("commit_hash",
240 [](ModuleInfo &info) { return info.commitHash; })
241 .def_prop_ro("constants", [](ModuleInfo &info) { return info.constants; })
242 // TODO: "extra" field.
243 .def("__repr__", [](ModuleInfo &info) {
244 std::string ret;
245 std::stringstream os(ret);
246 os << info;
247 return os.str();
248 });
249
250 nb::enum_<Logger::Level>(m, "LogLevel")
251 .value("Debug", Logger::Level::Debug)
252 .value("Info", Logger::Level::Info)
253 .value("Warning", Logger::Level::Warning)
254 .value("Error", Logger::Level::Error)
255 .export_values();
256 nb::class_<Logger>(m, "Logger");
257
258 nb::class_<services::Service>(m, "Service")
259 .def("get_service_symbol", &services::Service::getServiceSymbol);
260
261 nb::class_<SysInfo, services::Service>(m, "SysInfo")
262 .def("esi_version", &SysInfo::getEsiVersion)
263 .def("json_manifest", &SysInfo::getJsonManifest)
264 .def("cycle_count", &SysInfo::getCycleCount,
265 "Get the current cycle count of the accelerator system")
266 .def("core_clock_frequency", &SysInfo::getCoreClockFrequency,
267 "Get the core clock frequency of the accelerator system in Hz");
268
269 nb::class_<MMIO::RegionDescriptor>(m, "MMIORegionDescriptor")
270 .def_prop_ro("base", [](MMIO::RegionDescriptor &r) { return r.base; })
271 .def_prop_ro("size", [](MMIO::RegionDescriptor &r) { return r.size; });
272 nb::class_<services::MMIO, services::Service>(m, "MMIO")
273 .def("read", &services::MMIO::read)
274 .def("write", &services::MMIO::write)
275 .def_prop_ro("regions", &services::MMIO::getRegions,
276 nb::rv_policy::reference);
277
278 nb::class_<services::HostMem::HostMemRegion>(m, "HostMemRegion")
279 .def_prop_ro("ptr",
281 return reinterpret_cast<uintptr_t>(mem.getPtr());
282 })
283 .def_prop_ro("size", &services::HostMem::HostMemRegion::getSize);
284
285 nb::class_<services::HostMem::Options>(m, "HostMemOptions")
286 .def(nb::init<>())
287 .def_rw("writeable", &services::HostMem::Options::writeable)
288 .def_rw("use_large_pages", &services::HostMem::Options::useLargePages)
289 .def("__repr__", [](services::HostMem::Options &opts) {
290 std::string ret = "HostMemOptions(";
291 if (opts.writeable)
292 ret += "writeable ";
293 if (opts.useLargePages)
294 ret += "use_large_pages";
295 ret += ")";
296 return ret;
297 });
298
299 nb::class_<services::HostMem, services::Service>(m, "HostMem")
300 .def("allocate", &services::HostMem::allocate, nb::arg("size"),
301 nb::arg("options") = services::HostMem::Options(),
302 nb::rv_policy::take_ownership)
303 .def(
304 "map_memory",
305 [](HostMem &self, uintptr_t ptr, size_t size, HostMem::Options opts) {
306 return self.mapMemory(reinterpret_cast<void *>(ptr), size, opts);
307 },
308 nb::arg("ptr"), nb::arg("size"),
309 nb::arg("options") = services::HostMem::Options())
310 .def(
311 "unmap_memory",
312 [](HostMem &self, uintptr_t ptr) {
313 return self.unmapMemory(reinterpret_cast<void *>(ptr));
314 },
315 nb::arg("ptr"));
316 nb::class_<services::TelemetryService, services::Service>(m,
317 "TelemetryService");
318
319 nb::class_<std::future<MessageData>>(m, "MessageDataFuture")
320 .def("valid", [](std::future<MessageData> &f) { return f.valid(); })
321 .def("wait",
322 [](std::future<MessageData> &f) {
323 // Yield the GIL while waiting for the future to complete, in case
324 // of python callbacks occurring from other threads while waiting.
325 nb::gil_scoped_release release{};
326 f.wait();
327 })
328 .def("get", [](std::future<MessageData> &f) {
329 std::optional<MessageData> data;
330 {
331 // Yield the GIL while waiting for the future to complete, in case of
332 // python callbacks occurring from other threads while waiting.
333 nb::gil_scoped_release release{};
334 data.emplace(f.get());
335 }
336 return nb::bytearray((const char *)data->getBytes(), data->getSize());
337 });
338
339 nb::class_<ChannelPort::ConnectOptions>(m, "ConnectOptions")
340 .def(nb::init<>())
341 .def_rw("buffer_size", &ChannelPort::ConnectOptions::bufferSize,
342 nb::arg("buffer_size").none())
343 .def_rw("translate_message",
345
346 nb::class_<ChannelPort>(m, "ChannelPort")
347 .def("connect", &ChannelPort::connect, nb::arg("options"),
348 "Connect with specified options")
349 .def("disconnect", &ChannelPort::disconnect)
350 .def_prop_ro("type", &ChannelPort::getType, nb::rv_policy::reference)
351 .def_prop_ro("windowType", &ChannelPort::getWindowType,
352 nb::rv_policy::reference);
353
354 nb::class_<WriteChannelPort, ChannelPort>(m, "WriteChannelPort")
355 .def("write",
356 [](WriteChannelPort &p, nb::bytearray data) {
357 std::vector<uint8_t> dataVec((const uint8_t *)data.c_str(),
358 (const uint8_t *)data.c_str() +
359 data.size());
360 p.write(dataVec);
361 })
362 .def("tryWrite", [](WriteChannelPort &p, nb::bytearray data) {
363 std::vector<uint8_t> dataVec((const uint8_t *)data.c_str(),
364 (const uint8_t *)data.c_str() +
365 data.size());
366 return p.tryWrite(dataVec);
367 });
368 nb::class_<ReadChannelPort, ChannelPort>(m, "ReadChannelPort")
369 .def(
370 "read",
371 [](ReadChannelPort &p) -> nb::bytearray {
372 MessageData data;
373 p.read(data);
374 return nb::bytearray((const char *)data.getBytes(), data.getSize());
375 },
376 "Read data from the channel. Blocking.")
377 .def("read_async", &ReadChannelPort::readAsync);
378
379 nb::class_<BundlePort>(m, "BundlePort")
380 .def_prop_ro("id", &BundlePort::getID)
381 .def_prop_ro("channels", &BundlePort::getChannels,
382 nb::rv_policy::reference)
383 .def("getWrite", &BundlePort::getRawWrite, nb::rv_policy::reference)
384 .def("getRead", &BundlePort::getRawRead, nb::rv_policy::reference);
385
386 nb::class_<ServicePort, BundlePort>(m, "ServicePort");
387
388 nb::class_<MMIO::MMIORegion, ServicePort>(m, "MMIORegion")
389 .def_prop_ro("descriptor", &MMIO::MMIORegion::getDescriptor)
390 .def("read", &MMIO::MMIORegion::read)
391 .def("write", &MMIO::MMIORegion::write);
392
393 nb::class_<FuncService::Function, ServicePort>(m, "Function")
394 .def("call",
395 [](FuncService::Function &self,
396 nb::bytearray msg) -> std::future<MessageData> {
397 std::vector<uint8_t> dataVec((const uint8_t *)msg.c_str(),
398 (const uint8_t *)msg.c_str() +
399 msg.size());
400 MessageData data(dataVec);
401 return self.call(data);
402 })
403 .def("connect", [](FuncService::Function &self) { self.connect(); });
404
405 nb::class_<CallService::Callback, ServicePort>(m, "Callback")
406 .def("connect", [](CallService::Callback &self,
407 std::function<nb::object(nb::object)> pyCallback) {
408 // TODO: Under certain conditions this will cause python to crash. I
409 // don't remember how to replicate these crashes, but IIRC they are
410 // deterministic.
411 self.connect([pyCallback](const MessageData &req) -> MessageData {
412 nb::gil_scoped_acquire acquire{};
413 std::vector<uint8_t> arg(req.getBytes(),
414 req.getBytes() + req.getSize());
415 nb::bytes argObj((const char *)arg.data(), arg.size());
416 auto ret = pyCallback(argObj);
417 if (ret.is_none())
418 return MessageData();
419 nb::bytearray retBytes = nb::cast<nb::bytearray>(ret);
420 std::vector<uint8_t> dataVec((const uint8_t *)retBytes.c_str(),
421 (const uint8_t *)retBytes.c_str() +
422 retBytes.size());
423 return MessageData(dataVec);
424 });
425 });
426
427 nb::class_<TelemetryService::Metric, ServicePort>(m, "Metric")
428 .def("connect", &TelemetryService::Metric::connect)
429 .def("read", &TelemetryService::Metric::read)
430 .def("readInt", &TelemetryService::Metric::readInt);
431
432 nb::class_<ChannelService::ToHost, ServicePort>(m, "ToHostChannel")
433 .def("connect", &ChannelService::ToHost::connect)
434 .def("read", &ChannelService::ToHost::read);
435
436 nb::class_<ChannelService::FromHost, ServicePort>(m, "FromHostChannel")
437 .def("connect", &ChannelService::FromHost::connect)
438 .def(
439 "write",
440 [](ChannelService::FromHost &self, nb::bytearray data) {
441 std::vector<uint8_t> dataVec((const uint8_t *)data.c_str(),
442 (const uint8_t *)data.c_str() +
443 data.size());
444 self.write(MessageData(dataVec));
445 },
446 nb::arg("data"));
447
448 // Store this variable (not commonly done) as the "children" method needs for
449 // "Instance" to be defined first.
450 auto hwmodule =
451 nb::class_<HWModule>(m, "HWModule")
452 .def_prop_ro("info", &HWModule::getInfo)
453 .def_prop_ro("ports", &HWModule::getPorts, nb::rv_policy::reference)
454 .def_prop_ro("services", &HWModule::getServices,
455 nb::rv_policy::reference);
456
457 // In order to inherit methods from "HWModule", it needs to be defined first.
458 nb::class_<Instance, HWModule>(m, "Instance")
459 .def_prop_ro("id", &Instance::getID);
460
461 nb::class_<Accelerator, HWModule>(m, "Accelerator");
462
463 // Since this returns a vector of Instance*, we need to define Instance first
464 // or else stubgen complains.
465 hwmodule.def_prop_ro("children", &HWModule::getChildren,
466 nb::rv_policy::reference);
467
468 auto accConn = nb::class_<AcceleratorConnection>(m, "AcceleratorConnection");
469
470 nb::class_<Context>(
471 m, "Context",
472 "An ESI context owns everything -- types, accelerator connections, and "
473 "the accelerator facade (aka Accelerator) itself. It MUST NOT be garbage "
474 "collected while the accelerator is still in use. When it is destroyed, "
475 "all accelerator connections are disconnected.")
476 .def(nb::init<>(), "Create a context with a default logger.")
477 .def("connect", &Context::connect, nb::rv_policy::reference)
478 .def("set_stdio_logger", [](Context &ctxt, Logger::Level level) {
479 ctxt.setLogger(std::make_unique<StreamLogger>(level));
480 });
481
482 accConn
483 .def(
484 "sysinfo",
485 [](AcceleratorConnection &acc) {
486 return acc.getService<services::SysInfo>({});
487 },
488 nb::rv_policy::reference)
489 .def(
490 "get_service_mmio",
491 [](AcceleratorConnection &acc) {
492 return acc.getService<services::MMIO>({});
493 },
494 nb::rv_policy::reference)
495 .def(
496 "get_service_hostmem",
497 [](AcceleratorConnection &acc) {
498 return acc.getService<services::HostMem>({});
499 },
500 nb::rv_policy::reference)
501 .def("get_accelerator", &AcceleratorConnection::getAccelerator,
502 nb::rv_policy::reference);
503
504 nb::class_<Manifest>(m, "Manifest")
505 .def(nb::init<Context &, std::string>())
506 .def_prop_ro("api_version", &Manifest::getApiVersion)
507 .def(
508 "build_accelerator",
509 [&](Manifest &m, AcceleratorConnection &conn) -> Accelerator * {
510 auto *acc = m.buildAccelerator(conn);
511 conn.getServiceThread()->addPoll(*acc);
512 return acc;
513 },
514 nb::rv_policy::reference)
515 .def_prop_ro("type_table",
516 [](Manifest &m) {
517 std::vector<nb::object> ret;
518 std::ranges::transform(m.getTypeTable(),
519 std::back_inserter(ret), getPyType);
520 return ret;
521 })
522 .def_prop_ro("module_infos", &Manifest::getModuleInfos);
523}
Abstract class representing a connection to an accelerator.
Definition Accelerator.h:89
Accelerator & getAccelerator()
Top level accelerator class.
Definition Accelerator.h:70
std::string toStr() const
Definition Manifest.cpp:814
uint64_t getWidth() const
Definition Types.h:197
Unidirectional channels are the basic communication primitive between the host and accelerator.
Definition Ports.h:115
const Type * getType() const
Definition Ports.h:219
virtual void connect(const ConnectOptions &options=ConnectOptions())=0
Set up a connection to the accelerator.
virtual void disconnect()=0
const WindowType * getWindowType() const
If this port carries a windowed type, return the original WindowType (whose intoType is what getType(...
Definition Ports.h:224
AcceleratorConnections, Accelerators, and Manifests must all share a context.
Definition Context.h:34
void setLogger(std::unique_ptr< Logger > logger)
Register a logger with the accelerator. Assumes ownership of the logger.
Definition Context.h:64
const std::map< AppID, BundlePort & > & getPorts() const
Access the module's ports by ID.
Definition Design.h:80
const std::map< AppID, Instance * > & getChildren() const
Access the module's children by ID.
Definition Design.h:71
const std::vector< services::Service * > & getServices() const
Access the services provided by this module.
Definition Design.h:82
std::optional< ModuleInfo > getInfo() const
Access the module's metadata, if any.
Definition Design.h:62
AppID getID() const
Get the instance's ID, which it will always have.
Definition Design.h:124
Class to parse a manifest.
Definition Manifest.h:39
A concrete flat message backed by a single vector of bytes.
Definition Common.h:155
const uint8_t * getBytes() const
Definition Common.h:166
size_t getSize() const
Get the size of the data in bytes.
Definition Common.h:180
A ChannelPort which reads data from the accelerator.
Definition Ports.h:453
virtual std::future< MessageData > readAsync()
Asynchronous polling read.
Definition Ports.cpp:225
const Type * getInnerType() const
Definition Types.h:177
const std::string & getName() const
Definition Types.h:176
Root class of the ESI type system.
Definition Types.h:36
std::string ID
Definition Types.h:38
ID getID() const
Definition Types.h:42
const FieldVector & getFields() const
Definition Types.h:364
A ChannelPort which sends data to the accelerator.
Definition Ports.h:308
void write(const MessageData &data)
A very basic blocking write API.
Definition Ports.h:327
bool tryWrite(const MessageData &data)
A basic non-blocking write API.
Definition Ports.h:357
A function call which gets attached to a service port.
Definition Services.h:405
A port which writes data to the accelerator (from_host).
Definition Services.h:315
std::future< MessageData > read()
Definition Services.cpp:239
A function call which gets attached to a service port.
Definition Services.h:353
virtual std::unique_ptr< HostMemRegion > allocate(std::size_t size, Options opts) const =0
Allocate a region of host memory in accelerator accessible address space.
virtual void unmapMemory(void *ptr) const
Unmap memory which was previously mapped with 'mapMemory'.
Definition Services.h:277
virtual bool mapMemory(void *ptr, std::size_t size, Options opts) const
Try to make a region of host memory accessible to the accelerator.
Definition Services.h:272
virtual uint64_t read(uint32_t addr) const
Read a 64-bit value from this region, not the global address space.
Definition Services.cpp:132
virtual void write(uint32_t addr, uint64_t data)
Write a 64-bit value to this region, not the global address space.
Definition Services.cpp:137
virtual RegionDescriptor getDescriptor() const
Get the offset (and size) of the region in the parent (usually global) MMIO address space.
Definition Services.h:188
virtual uint64_t read(uint32_t addr) const =0
Read a 64-bit value from the global MMIO space.
virtual void write(uint32_t addr, uint64_t data)=0
Write a 64-bit value to the global MMIO space.
const std::map< AppIDPath, RegionDescriptor > & getRegions() const
Get the regions of MMIO space that this service manages.
Definition Services.h:158
Parent class of all APIs modeled as 'services'.
Definition Services.h:59
virtual std::string getServiceSymbol() const =0
Information about the Accelerator system.
Definition Services.h:113
virtual std::optional< uint64_t > getCoreClockFrequency() const
Get the "core" clock frequency of the accelerator system in Hz.
Definition Services.h:127
virtual std::string getJsonManifest() const
Return the JSON-formatted system manifest.
Definition Services.cpp:40
virtual uint32_t getEsiVersion() const =0
Get the ESI version number to check version compatibility.
virtual std::optional< uint64_t > getCycleCount() const
Get the current cycle count of the accelerator system.
Definition Services.h:124
void connect()
Connect to a particular telemetry port. Offset should be non-nullopt.
Definition Services.cpp:459
std::future< MessageData > read()
Definition Services.cpp:466
Service for retrieving telemetry data from the accelerator.
Definition Services.h:453
nb::object getPyType(std::optional< const Type * > t)
Resolve a Type to the Python wrapper object.
NB_MODULE(esiCppAccel, m)
size_t hash_combine(size_t h1, size_t h2)
C++'s stdlib doesn't have a hash_combine function. This is a simple one.
Definition Utils.h:36
Definition esi.py:1
std::optional< unsigned > bufferSize
The buffer size is optional and should be considered merely a hint.
Definition Ports.h:124
bool translateMessage
If the type of this port is a window, translate the incoming/outgoing data into its underlying ('into...
Definition Ports.h:183
std::any value
Definition Common.h:68
std::optional< const Type * > type
Definition Common.h:69
uint64_t bulkCountWidth
Definition Types.h:322
std::string name
Definition Types.h:320
std::vector< Field > fields
Definition Types.h:329
std::string name
Definition Types.h:328
RAII memory region for host memory.
Definition Services.h:237
virtual void * getPtr() const =0
Get a pointer to the host memory.
virtual std::size_t getSize() const =0
Options for allocating host memory.
Definition Services.h:255
Describe a region (slice) of MMIO space.
Definition Services.h:143
static const std::type_info * get(const ChannelPort *port)
static const std::type_info * get(const Service *svc)