21 #include <pybind11/pybind11.h>
24 #include <pybind11/stl.h>
33 static const void *
get(
const ChannelPort *port,
const std::type_info *&type) {
49 py::class_<Type>(m,
"Type")
51 .def(
"__repr__", [](
Type &t) {
return "<" + t.
getID() +
">"; });
52 py::class_<ChannelType, Type>(m,
"ChannelType")
54 py::return_value_policy::reference);
55 py::enum_<BundleType::Direction>(m,
"Direction")
56 .value(
"To", BundleType::Direction::To)
57 .value(
"From", BundleType::Direction::From)
59 py::class_<BundleType, Type>(m,
"BundleType")
61 py::return_value_policy::reference);
62 py::class_<VoidType, Type>(m,
"VoidType");
63 py::class_<AnyType, Type>(m,
"AnyType");
64 py::class_<BitVectorType, Type>(m,
"BitVectorType")
66 py::class_<BitsType, BitVectorType>(m,
"BitsType");
67 py::class_<IntegerType, BitVectorType>(m,
"IntegerType");
68 py::class_<SIntType, IntegerType>(m,
"SIntType");
69 py::class_<UIntType, IntegerType>(m,
"UIntType");
70 py::class_<StructType, Type>(m,
"StructType")
72 py::return_value_policy::reference);
73 py::class_<ArrayType, Type>(m,
"ArrayType")
75 py::return_value_policy::reference)
78 py::class_<Context>(m,
"Context")
82 py::class_<ModuleInfo>(m,
"ModuleInfo")
83 .def_property_readonly(
"name", [](
ModuleInfo &info) {
return info.
name; })
84 .def_property_readonly(
"summary",
86 .def_property_readonly(
"version",
88 .def_property_readonly(
"repo", [](
ModuleInfo &info) {
return info.
repo; })
89 .def_property_readonly(
"commit_hash",
94 std::stringstream os(ret);
99 py::class_<SysInfo>(m,
"SysInfo")
103 py::class_<services::MMIO>(m,
"MMIO")
107 py::class_<AppID>(m,
"AppID")
108 .def(py::init<std::string, std::optional<uint32_t>>(), py::arg(
"name"),
109 py::arg(
"idx") = std::nullopt)
110 .def_property_readonly(
"name", [](
AppID &
id) {
return id.name; })
111 .def_property_readonly(
"idx",
112 [](
AppID &
id) -> py::object {
114 return py::cast(
id.idx);
119 std::string ret =
"<" +
id.name;
121 ret = ret +
"[" + std::to_string(*
id.idx) +
"]";
125 .def(
"__eq__", [](
AppID &a,
AppID &b) {
return a == b; })
126 .def(
"__hash__", [](
AppID &
id) {
128 return std::hash<std::string>{}(
id.name) ^
129 (std::hash<uint32_t>{}(
id.idx.value_or(-1)) << 1);
132 py::class_<std::future<MessageData>>(m,
"MessageDataFuture")
133 .def(
"valid", &std::future<MessageData>::valid)
134 .def(
"wait", &std::future<MessageData>::wait)
135 .def(
"get", [](std::future<MessageData> &f) {
137 return py::bytearray((
const char *)data.getBytes(), data.getSize());
140 py::class_<ChannelPort>(m,
"ChannelPort")
143 py::return_value_policy::reference);
145 py::class_<WriteChannelPort, ChannelPort>(m,
"WriteChannelPort")
147 py::buffer_info info(py::buffer(data).request());
148 std::vector<uint8_t> dataVec((uint8_t *)info.ptr,
149 (uint8_t *)info.ptr + info.size);
152 py::class_<ReadChannelPort, ChannelPort>(m,
"ReadChannelPort")
158 return py::bytearray((
const char *)data.getBytes(),
163 py::class_<BundlePort>(m,
"BundlePort")
166 py::return_value_policy::reference)
168 py::return_value_policy::reference)
170 py::return_value_policy::reference);
172 py::class_<ServicePort, BundlePort>(m,
"ServicePort");
173 py::class_<FuncService::Function, ServicePort>(m,
"Function")
177 py::bytearray msg) -> std::future<MessageData> {
178 py::buffer_info info(py::buffer(msg).request());
179 std::vector<uint8_t> dataVec((uint8_t *)info.ptr,
180 (uint8_t *)info.ptr + info.size);
182 return self.call(data);
184 py::return_value_policy::take_ownership)
190 py::class_<HWModule>(m,
"HWModule")
193 py::return_value_policy::reference);
196 py::class_<Instance, HWModule>(m,
"Instance")
199 py::class_<Accelerator, HWModule>(m,
"Accelerator");
204 py::return_value_policy::reference);
206 auto accConn = py::class_<AcceleratorConnection>(m,
"AcceleratorConnection")
213 py::return_value_policy::reference)
219 py::return_value_policy::reference);
225 py::enum_<backends::cosim::CosimAccelerator::ManifestMethod>(
226 m,
"CosimManifestMethod")
227 .value(
"ManifestCosim",
228 backends::cosim::CosimAccelerator::ManifestMethod::Cosim)
229 .value(
"ManifestMMIO",
230 backends::cosim::CosimAccelerator::ManifestMethod::MMIO)
234 "set_manifest_method",
236 backends::cosim::CosimAccelerator::ManifestMethod method) {
237 auto cosim =
dynamic_cast<backends::cosim::CosimAccelerator *
>(&acc);
239 throw std::runtime_error(
240 "set_manifest_method only supported for cosim connections");
241 cosim->setManifestMethod(method);
245 py::class_<Manifest>(m,
"Manifest")
246 .def(py::init<Context &, std::string>())
249 py::return_value_policy::take_ownership)
Abstract class representing a connection to an accelerator.
ServiceClass * getService(AppIDPath id={}, std::string implName={}, ServiceImplDetails details={}, HWClientDetails clients={})
Get a typed reference to a particular service type.
const Type * getElementType() const
uint64_t getWidth() const
ReadChannelPort & getRawRead(const std::string &name) const
WriteChannelPort & getRawWrite(const std::string &name) const
Get access to the raw byte streams of a channel.
const std::map< std::string, ChannelPort & > & getChannels() const
AppID getID() const
Get the ID of the port.
const ChannelVector & getChannels() const
Unidirectional channels are the basic communication primitive between the host and accelerator.
const Type * getType() const
const Type * getInner() const
std::unique_ptr< AcceleratorConnection > connect(std::string backend, std::string connection)
Connect to an accelerator backend.
const std::map< AppID, Instance * > & getChildren() const
Access the module's children by ID.
std::optional< ModuleInfo > getInfo() const
Access the module's metadata, if any.
const std::map< AppID, const BundlePort & > & getPorts() const
Access the module's ports by ID.
const AppID getID() const
Get the instance's ID, which it will always have.
const std::vector< const Type * > & getTypeTable() const
The Type Table is an ordered list of types.
std::unique_ptr< Accelerator > buildAccelerator(AcceleratorConnection &acc) const
uint32_t getApiVersion() const
std::vector< ModuleInfo > getModuleInfos() const
A logical chunk of data representing serialized data.
A ChannelPort which reads data from the accelerator.
virtual std::future< MessageData > readAsync()
Asynchronous read.
const FieldVector & getFields() const
Root class of the ESI type system.
A ChannelPort which sends data to the accelerator.
virtual void write(const MessageData &)=0
A very basic write API. Will likely change for performance reasons.
A function call which gets attached to a service port.
virtual void write(uint32_t addr, uint32_t data)=0
virtual uint32_t read(uint32_t addr) const =0
Information about the Accelerator system.
virtual std::string getJsonManifest() const
Return the JSON-formatted system manifest.
virtual uint32_t getEsiVersion() const =0
Get the ESI version number to check version compatibility.
PYBIND11_MODULE(esiCppAccel, m)
std::unique_ptr< AcceleratorConnection > connect(Context &ctxt, std::string backend, std::string connection)
const std::optional< std::string > commitHash
const std::optional< std::string > repo
const std::optional< std::string > version
const std::optional< std::string > summary
const std::optional< std::string > name
static const void * get(const ChannelPort *port, const std::type_info *&type)