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("__repr__", [](Type &t) { return "<" + t.getID() + ">"; });
121 nb::class_<ChannelType, Type>(m, "ChannelType")
122 .def(nb::init<const Type::ID &, const Type *>(), nb::arg("id"),
123 nb::arg("inner"))
124 .def_prop_ro("inner", &ChannelType::getInner, nb::rv_policy::reference);
125 nb::enum_<BundleType::Direction>(m, "Direction")
126 .value("To", BundleType::Direction::To)
127 .value("From", BundleType::Direction::From)
128 .export_values();
129 nb::class_<BundleType, Type>(m, "BundleType")
130 .def(nb::init<const Type::ID &, const BundleType::ChannelVector &>(),
131 nb::arg("id"), nb::arg("channels"))
132 .def_prop_ro("channels", &BundleType::getChannels,
133 nb::rv_policy::reference);
134 nb::class_<VoidType, Type>(m, "VoidType")
135 .def(nb::init<const Type::ID &>(), nb::arg("id"));
136 nb::class_<AnyType, Type>(m, "AnyType")
137 .def(nb::init<const Type::ID &>(), nb::arg("id"));
138 nb::class_<TypeAliasType, Type>(m, "TypeAliasType")
139 .def(nb::init<const Type::ID &, std::string, const Type *>(),
140 nb::arg("id"), nb::arg("name"), nb::arg("inner_type"))
141 .def_prop_ro("name", &TypeAliasType::getName)
142 .def_prop_ro("inner", &TypeAliasType::getInnerType,
143 nb::rv_policy::reference);
144 nb::class_<BitVectorType, Type>(m, "BitVectorType")
145 .def(nb::init<const Type::ID &, uint64_t>(), nb::arg("id"),
146 nb::arg("width"))
147 .def_prop_ro("width", &BitVectorType::getWidth);
148 nb::class_<BitsType, BitVectorType>(m, "BitsType")
149 .def(nb::init<const Type::ID &, uint64_t>(), nb::arg("id"),
150 nb::arg("width"));
151 nb::class_<IntegerType, BitVectorType>(m, "IntegerType")
152 .def(nb::init<const Type::ID &, uint64_t>(), nb::arg("id"),
153 nb::arg("width"));
154 nb::class_<SIntType, IntegerType>(m, "SIntType")
155 .def(nb::init<const Type::ID &, uint64_t>(), nb::arg("id"),
156 nb::arg("width"));
157 nb::class_<UIntType, IntegerType>(m, "UIntType")
158 .def(nb::init<const Type::ID &, uint64_t>(), nb::arg("id"),
159 nb::arg("width"));
160 nb::class_<StructType, Type>(m, "StructType")
161 .def(nb::init<const Type::ID &, const StructType::FieldVector &, bool>(),
162 nb::arg("id"), nb::arg("fields"), nb::arg("reverse") = true)
163 .def_prop_ro("fields", &StructType::getFields, nb::rv_policy::reference)
164 .def_prop_ro("reverse", &StructType::isReverse);
165 nb::class_<ArrayType, Type>(m, "ArrayType")
166 .def(nb::init<const Type::ID &, const Type *, uint64_t>(), nb::arg("id"),
167 nb::arg("element_type"), nb::arg("size"))
168 .def_prop_ro("element", &ArrayType::getElementType,
169 nb::rv_policy::reference)
170 .def_prop_ro("size", &ArrayType::getSize);
171 nb::class_<WindowType::Field>(m, "WindowField")
172 .def(nb::init<>())
173 .def(nb::init<std::string, uint64_t, uint64_t>(), nb::arg("name"),
174 nb::arg("num_items") = 0, nb::arg("bulk_count_width") = 0)
175 .def_rw("name", &WindowType::Field::name)
176 .def_rw("num_items", &WindowType::Field::numItems)
177 .def_rw("bulk_count_width", &WindowType::Field::bulkCountWidth);
178 nb::class_<WindowType::Frame>(m, "WindowFrame")
179 .def(nb::init<>())
180 .def(nb::init<std::string, const std::vector<WindowType::Field> &>(),
181 nb::arg("name"), nb::arg("fields"))
182 .def_rw("name", &WindowType::Frame::name)
183 .def_rw("fields", &WindowType::Frame::fields);
184 nb::class_<WindowType, Type>(m, "WindowType")
185 .def(nb::init<const Type::ID &, const std::string &, const Type *,
186 const Type *, const std::vector<WindowType::Frame> &>(),
187 nb::arg("id"), nb::arg("name"), nb::arg("into_type"),
188 nb::arg("lowered_type"), nb::arg("frames"))
189 .def_prop_ro("name", &WindowType::getName)
190 .def_prop_ro("into", &WindowType::getIntoType, nb::rv_policy::reference)
191 .def_prop_ro("lowered", &WindowType::getLoweredType,
192 nb::rv_policy::reference)
193 .def_prop_ro("frames", &WindowType::getFrames, nb::rv_policy::reference);
194 nb::class_<ListType, Type>(m, "ListType")
195 .def(nb::init<const Type::ID &, const Type *>(), nb::arg("id"),
196 nb::arg("element_type"))
197 .def_prop_ro("element", &ListType::getElementType,
198 nb::rv_policy::reference);
199 nb::class_<UnionType, Type>(m, "UnionType")
200 .def(nb::init<const Type::ID &, const UnionType::FieldVector &>(),
201 nb::arg("id"), nb::arg("fields"))
202 .def_prop_ro("fields", &UnionType::getFields, nb::rv_policy::reference);
203
204 nb::class_<Constant>(m, "Constant")
205 .def_prop_ro("value", [](Constant &c) { return c.value; })
206 .def_prop_ro("type", [](Constant &c) { return getPyType(*c.type); });
207
208 nb::class_<AppID>(m, "AppID")
209 .def(nb::init<std::string, std::optional<uint32_t>>(), nb::arg("name"),
210 nb::arg("idx") = std::nullopt)
211 .def_prop_ro("name", [](AppID &id) { return id.name; })
212 .def_prop_ro("idx",
213 [](AppID &id) -> nb::object {
214 if (id.idx)
215 return nb::cast(id.idx);
216 return nb::none();
217 })
218 .def("__repr__",
219 [](AppID &id) {
220 std::string ret = "<" + id.name;
221 if (id.idx)
222 ret = ret + "[" + std::to_string(*id.idx) + "]";
223 ret = ret + ">";
224 return ret;
225 })
226 .def("__eq__", [](AppID &a, AppID &b) { return a == b; })
227 .def("__hash__", [](AppID &id) {
228 return utils::hash_combine(std::hash<std::string>{}(id.name),
229 std::hash<uint32_t>{}(id.idx.value_or(-1)));
230 });
231 nb::class_<AppIDPath>(m, "AppIDPath").def("__repr__", &AppIDPath::toStr);
232
233 nb::class_<ModuleInfo>(m, "ModuleInfo")
234 .def_prop_ro("name", [](ModuleInfo &info) { return info.name; })
235 .def_prop_ro("summary", [](ModuleInfo &info) { return info.summary; })
236 .def_prop_ro("version", [](ModuleInfo &info) { return info.version; })
237 .def_prop_ro("repo", [](ModuleInfo &info) { return info.repo; })
238 .def_prop_ro("commit_hash",
239 [](ModuleInfo &info) { return info.commitHash; })
240 .def_prop_ro("constants", [](ModuleInfo &info) { return info.constants; })
241 // TODO: "extra" field.
242 .def("__repr__", [](ModuleInfo &info) {
243 std::string ret;
244 std::stringstream os(ret);
245 os << info;
246 return os.str();
247 });
248
249 nb::enum_<Logger::Level>(m, "LogLevel")
250 .value("Debug", Logger::Level::Debug)
251 .value("Info", Logger::Level::Info)
252 .value("Warning", Logger::Level::Warning)
253 .value("Error", Logger::Level::Error)
254 .export_values();
255 nb::class_<Logger>(m, "Logger");
256
257 nb::class_<services::Service>(m, "Service")
258 .def("get_service_symbol", &services::Service::getServiceSymbol);
259
260 nb::class_<SysInfo, services::Service>(m, "SysInfo")
261 .def("esi_version", &SysInfo::getEsiVersion)
262 .def("json_manifest", &SysInfo::getJsonManifest)
263 .def("cycle_count", &SysInfo::getCycleCount,
264 "Get the current cycle count of the accelerator system")
265 .def("core_clock_frequency", &SysInfo::getCoreClockFrequency,
266 "Get the core clock frequency of the accelerator system in Hz");
267
268 nb::class_<MMIO::RegionDescriptor>(m, "MMIORegionDescriptor")
269 .def_prop_ro("base", [](MMIO::RegionDescriptor &r) { return r.base; })
270 .def_prop_ro("size", [](MMIO::RegionDescriptor &r) { return r.size; });
271 nb::class_<services::MMIO, services::Service>(m, "MMIO")
272 .def("read", &services::MMIO::read)
273 .def("write", &services::MMIO::write)
274 .def_prop_ro("regions", &services::MMIO::getRegions,
275 nb::rv_policy::reference);
276
277 nb::class_<services::HostMem::HostMemRegion>(m, "HostMemRegion")
278 .def_prop_ro("ptr",
280 return reinterpret_cast<uintptr_t>(mem.getPtr());
281 })
282 .def_prop_ro("size", &services::HostMem::HostMemRegion::getSize);
283
284 nb::class_<services::HostMem::Options>(m, "HostMemOptions")
285 .def(nb::init<>())
286 .def_rw("writeable", &services::HostMem::Options::writeable)
287 .def_rw("use_large_pages", &services::HostMem::Options::useLargePages)
288 .def("__repr__", [](services::HostMem::Options &opts) {
289 std::string ret = "HostMemOptions(";
290 if (opts.writeable)
291 ret += "writeable ";
292 if (opts.useLargePages)
293 ret += "use_large_pages";
294 ret += ")";
295 return ret;
296 });
297
298 nb::class_<services::HostMem, services::Service>(m, "HostMem")
299 .def("allocate", &services::HostMem::allocate, nb::arg("size"),
300 nb::arg("options") = services::HostMem::Options(),
301 nb::rv_policy::take_ownership)
302 .def(
303 "map_memory",
304 [](HostMem &self, uintptr_t ptr, size_t size, HostMem::Options opts) {
305 return self.mapMemory(reinterpret_cast<void *>(ptr), size, opts);
306 },
307 nb::arg("ptr"), nb::arg("size"),
308 nb::arg("options") = services::HostMem::Options())
309 .def(
310 "unmap_memory",
311 [](HostMem &self, uintptr_t ptr) {
312 return self.unmapMemory(reinterpret_cast<void *>(ptr));
313 },
314 nb::arg("ptr"));
315 nb::class_<services::TelemetryService, services::Service>(m,
316 "TelemetryService");
317
318 nb::class_<std::future<MessageData>>(m, "MessageDataFuture")
319 .def("valid", [](std::future<MessageData> &f) { return f.valid(); })
320 .def("wait",
321 [](std::future<MessageData> &f) {
322 // Yield the GIL while waiting for the future to complete, in case
323 // of python callbacks occurring from other threads while waiting.
324 nb::gil_scoped_release release{};
325 f.wait();
326 })
327 .def("get", [](std::future<MessageData> &f) {
328 std::optional<MessageData> data;
329 {
330 // Yield the GIL while waiting for the future to complete, in case of
331 // python callbacks occurring from other threads while waiting.
332 nb::gil_scoped_release release{};
333 data.emplace(f.get());
334 }
335 return nb::bytearray((const char *)data->getBytes(), data->getSize());
336 });
337
338 nb::class_<ChannelPort::ConnectOptions>(m, "ConnectOptions")
339 .def(nb::init<>())
340 .def_rw("buffer_size", &ChannelPort::ConnectOptions::bufferSize,
341 nb::arg("buffer_size").none())
342 .def_rw("translate_message",
344
345 nb::class_<ChannelPort>(m, "ChannelPort")
346 .def("connect", &ChannelPort::connect, nb::arg("options"),
347 "Connect with specified options")
348 .def("disconnect", &ChannelPort::disconnect)
349 .def_prop_ro("type", &ChannelPort::getType, nb::rv_policy::reference);
350
351 nb::class_<WriteChannelPort, ChannelPort>(m, "WriteChannelPort")
352 .def("write",
353 [](WriteChannelPort &p, nb::bytearray data) {
354 std::vector<uint8_t> dataVec((const uint8_t *)data.c_str(),
355 (const uint8_t *)data.c_str() +
356 data.size());
357 p.write(dataVec);
358 })
359 .def("tryWrite", [](WriteChannelPort &p, nb::bytearray data) {
360 std::vector<uint8_t> dataVec((const uint8_t *)data.c_str(),
361 (const uint8_t *)data.c_str() +
362 data.size());
363 return p.tryWrite(dataVec);
364 });
365 nb::class_<ReadChannelPort, ChannelPort>(m, "ReadChannelPort")
366 .def(
367 "read",
368 [](ReadChannelPort &p) -> nb::bytearray {
369 MessageData data;
370 p.read(data);
371 return nb::bytearray((const char *)data.getBytes(), data.getSize());
372 },
373 "Read data from the channel. Blocking.")
374 .def("read_async", &ReadChannelPort::readAsync);
375
376 nb::class_<BundlePort>(m, "BundlePort")
377 .def_prop_ro("id", &BundlePort::getID)
378 .def_prop_ro("channels", &BundlePort::getChannels,
379 nb::rv_policy::reference)
380 .def("getWrite", &BundlePort::getRawWrite, nb::rv_policy::reference)
381 .def("getRead", &BundlePort::getRawRead, nb::rv_policy::reference);
382
383 nb::class_<ServicePort, BundlePort>(m, "ServicePort");
384
385 nb::class_<MMIO::MMIORegion, ServicePort>(m, "MMIORegion")
386 .def_prop_ro("descriptor", &MMIO::MMIORegion::getDescriptor)
387 .def("read", &MMIO::MMIORegion::read)
388 .def("write", &MMIO::MMIORegion::write);
389
390 nb::class_<FuncService::Function, ServicePort>(m, "Function")
391 .def("call",
392 [](FuncService::Function &self,
393 nb::bytearray msg) -> std::future<MessageData> {
394 std::vector<uint8_t> dataVec((const uint8_t *)msg.c_str(),
395 (const uint8_t *)msg.c_str() +
396 msg.size());
397 MessageData data(dataVec);
398 return self.call(data);
399 })
400 .def("connect", [](FuncService::Function &self) { self.connect(); });
401
402 nb::class_<CallService::Callback, ServicePort>(m, "Callback")
403 .def("connect", [](CallService::Callback &self,
404 std::function<nb::object(nb::object)> pyCallback) {
405 // TODO: Under certain conditions this will cause python to crash. I
406 // don't remember how to replicate these crashes, but IIRC they are
407 // deterministic.
408 self.connect([pyCallback](const MessageData &req) -> MessageData {
409 nb::gil_scoped_acquire acquire{};
410 std::vector<uint8_t> arg(req.getBytes(),
411 req.getBytes() + req.getSize());
412 nb::bytes argObj((const char *)arg.data(), arg.size());
413 auto ret = pyCallback(argObj);
414 if (ret.is_none())
415 return MessageData();
416 nb::bytearray retBytes = nb::cast<nb::bytearray>(ret);
417 std::vector<uint8_t> dataVec((const uint8_t *)retBytes.c_str(),
418 (const uint8_t *)retBytes.c_str() +
419 retBytes.size());
420 return MessageData(dataVec);
421 });
422 });
423
424 nb::class_<TelemetryService::Metric, ServicePort>(m, "Metric")
425 .def("connect", &TelemetryService::Metric::connect)
426 .def("read", &TelemetryService::Metric::read)
427 .def("readInt", &TelemetryService::Metric::readInt);
428
429 nb::class_<ChannelService::ToHost, ServicePort>(m, "ToHostChannel")
430 .def("connect", &ChannelService::ToHost::connect)
431 .def("read", &ChannelService::ToHost::read);
432
433 nb::class_<ChannelService::FromHost, ServicePort>(m, "FromHostChannel")
434 .def("connect", &ChannelService::FromHost::connect)
435 .def(
436 "write",
437 [](ChannelService::FromHost &self, nb::bytearray data) {
438 std::vector<uint8_t> dataVec((const uint8_t *)data.c_str(),
439 (const uint8_t *)data.c_str() +
440 data.size());
441 self.write(MessageData(dataVec));
442 },
443 nb::arg("data"));
444
445 // Store this variable (not commonly done) as the "children" method needs for
446 // "Instance" to be defined first.
447 auto hwmodule =
448 nb::class_<HWModule>(m, "HWModule")
449 .def_prop_ro("info", &HWModule::getInfo)
450 .def_prop_ro("ports", &HWModule::getPorts, nb::rv_policy::reference)
451 .def_prop_ro("services", &HWModule::getServices,
452 nb::rv_policy::reference);
453
454 // In order to inherit methods from "HWModule", it needs to be defined first.
455 nb::class_<Instance, HWModule>(m, "Instance")
456 .def_prop_ro("id", &Instance::getID);
457
458 nb::class_<Accelerator, HWModule>(m, "Accelerator");
459
460 // Since this returns a vector of Instance*, we need to define Instance first
461 // or else stubgen complains.
462 hwmodule.def_prop_ro("children", &HWModule::getChildren,
463 nb::rv_policy::reference);
464
465 auto accConn = nb::class_<AcceleratorConnection>(m, "AcceleratorConnection");
466
467 nb::class_<Context>(
468 m, "Context",
469 "An ESI context owns everything -- types, accelerator connections, and "
470 "the accelerator facade (aka Accelerator) itself. It MUST NOT be garbage "
471 "collected while the accelerator is still in use. When it is destroyed, "
472 "all accelerator connections are disconnected.")
473 .def(nb::init<>(), "Create a context with a default logger.")
474 .def("connect", &Context::connect, nb::rv_policy::reference)
475 .def("set_stdio_logger", [](Context &ctxt, Logger::Level level) {
476 ctxt.setLogger(std::make_unique<StreamLogger>(level));
477 });
478
479 accConn
480 .def(
481 "sysinfo",
482 [](AcceleratorConnection &acc) {
483 return acc.getService<services::SysInfo>({});
484 },
485 nb::rv_policy::reference)
486 .def(
487 "get_service_mmio",
488 [](AcceleratorConnection &acc) {
489 return acc.getService<services::MMIO>({});
490 },
491 nb::rv_policy::reference)
492 .def(
493 "get_service_hostmem",
494 [](AcceleratorConnection &acc) {
495 return acc.getService<services::HostMem>({});
496 },
497 nb::rv_policy::reference)
498 .def("get_accelerator", &AcceleratorConnection::getAccelerator,
499 nb::rv_policy::reference);
500
501 nb::class_<Manifest>(m, "Manifest")
502 .def(nb::init<Context &, std::string>())
503 .def_prop_ro("api_version", &Manifest::getApiVersion)
504 .def(
505 "build_accelerator",
506 [&](Manifest &m, AcceleratorConnection &conn) -> Accelerator * {
507 auto *acc = m.buildAccelerator(conn);
508 conn.getServiceThread()->addPoll(*acc);
509 return acc;
510 },
511 nb::rv_policy::reference)
512 .def_prop_ro("type_table",
513 [](Manifest &m) {
514 std::vector<nb::object> ret;
515 std::ranges::transform(m.getTypeTable(),
516 std::back_inserter(ret), getPyType);
517 return ret;
518 })
519 .def_prop_ro("module_infos", &Manifest::getModuleInfos);
520}
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:195
Unidirectional channels are the basic communication primitive between the host and accelerator.
Definition Ports.h:36
const Type * getType() const
Definition Ports.h:137
virtual void connect(const ConnectOptions &options=ConnectOptions())=0
Set up a connection to the accelerator.
virtual void disconnect()=0
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 logical chunk of data representing serialized data.
Definition Common.h:113
const uint8_t * getBytes() const
Definition Common.h:124
size_t getSize() const
Get the size of the data in bytes.
Definition Common.h:138
A ChannelPort which reads data from the accelerator.
Definition Ports.h:341
virtual std::future< MessageData > readAsync()
Asynchronous read.
Definition Ports.cpp:126
const Type * getInnerType() const
Definition Types.h:175
const std::string & getName() const
Definition Types.h:174
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:362
A ChannelPort which sends data to the accelerator.
Definition Ports.h:215
void write(const MessageData &data)
A very basic blocking write API.
Definition Ports.h:231
bool tryWrite(const MessageData &data)
A basic non-blocking write API.
Definition Ports.h:261
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:32
Definition esi.py:1
std::optional< unsigned > bufferSize
The buffer size is optional and should be considered merely a hint.
Definition Ports.h:45
bool translateMessage
If the type of this port is a window, translate the incoming/outgoing data into its underlying ('into...
Definition Ports.h:104
std::any value
Definition Common.h:68
std::optional< const Type * > type
Definition Common.h:69
uint64_t bulkCountWidth
Definition Types.h:320
std::string name
Definition Types.h:318
std::vector< Field > fields
Definition Types.h:327
std::string name
Definition Types.h:326
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)